FreeBSD/Linux Kernel Cross Reference
sys/net/if_ef.c
1 /*-
2 * Copyright (c) 1999, 2000 Boris Popov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29 #include "opt_inet.h"
30 #include "opt_ipx.h"
31 #include "opt_ef.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sockio.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/syslog.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42
43 #include <net/ethernet.h>
44 #include <net/if_llc.h>
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
51 #include <net/bpf.h>
52
53 #ifdef INET
54 #include <netinet/in.h>
55 #include <netinet/in_var.h>
56 #include <netinet/if_ether.h>
57 #endif
58
59 #ifdef IPX
60 #include <netipx/ipx.h>
61 #include <netipx/ipx_if.h>
62 #endif
63
64 /* internal frame types */
65 #define ETHER_FT_EII 0 /* Ethernet_II - default */
66 #define ETHER_FT_8023 1 /* 802.3 (Novell) */
67 #define ETHER_FT_8022 2 /* 802.2 */
68 #define ETHER_FT_SNAP 3 /* SNAP */
69 #define EF_NFT 4 /* total number of frame types */
70
71 #ifdef EF_DEBUG
72 #define EFDEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
73 #else
74 #define EFDEBUG(format, args...)
75 #endif
76
77 #define EFERROR(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
78
79 struct efnet {
80 struct arpcom ef_ac;
81 struct ifnet * ef_ifp;
82 };
83
84 struct ef_link {
85 SLIST_ENTRY(ef_link) el_next;
86 struct ifnet *el_ifp; /* raw device for this clones */
87 struct efnet *el_units[EF_NFT]; /* our clones */
88 };
89
90 static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
91 static int efcount;
92
93 extern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
94 extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
95 struct sockaddr *dst, short *tp, int *hlen);
96
97 /*
98 static void ef_reset (struct ifnet *);
99 */
100 static int ef_attach(struct efnet *sc);
101 static int ef_detach(struct efnet *sc);
102 static void ef_init(void *);
103 static int ef_ioctl(struct ifnet *, u_long, caddr_t);
104 static void ef_start(struct ifnet *);
105 static int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
106 static int ef_output(struct ifnet *ifp, struct mbuf **mp,
107 struct sockaddr *dst, short *tp, int *hlen);
108
109 static int ef_load(void);
110 static int ef_unload(void);
111
112 /*
113 * Install the interface, most of structure initialization done in ef_clone()
114 */
115 static int
116 ef_attach(struct efnet *sc)
117 {
118 struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
119 struct ifaddr *ifa1, *ifa2;
120 struct sockaddr_dl *sdl1, *sdl2;
121
122 ifp->if_output = ether_output;
123 ifp->if_start = ef_start;
124 ifp->if_watchdog = NULL;
125 ifp->if_init = ef_init;
126 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
127 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
128 /*
129 * Attach the interface
130 */
131 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
132
133 ifp->if_resolvemulti = 0;
134 ifp->if_type = IFT_XETHER;
135 ifp->if_flags |= IFF_RUNNING;
136
137 ifa1 = ifnet_addrs[ifp->if_index - 1];
138 ifa2 = ifnet_addrs[sc->ef_ifp->if_index - 1];
139 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
140 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
141 sdl1->sdl_type = IFT_ETHER;
142 sdl1->sdl_alen = ETHER_ADDR_LEN;
143 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
144 bcopy(LLADDR(sdl2), sc->ef_ac.ac_enaddr, ETHER_ADDR_LEN);
145
146 EFDEBUG("%s%d: attached\n", ifp->if_name, ifp->if_unit);
147 return 1;
148 }
149
150 /*
151 * This is for _testing_only_, just removes interface from interfaces list
152 */
153 static int
154 ef_detach(struct efnet *sc)
155 {
156 struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
157 int s;
158
159 s = splimp();
160
161 if (ifp->if_flags & IFF_UP) {
162 if_down(ifp);
163 if (ifp->if_flags & IFF_RUNNING) {
164 /* find internet addresses and delete routes */
165 register struct ifaddr *ifa;
166 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
167 rtinit(ifa, (int)RTM_DELETE, 0);
168 }
169 }
170 }
171
172 TAILQ_REMOVE(&ifnet, ifp, if_link);
173 splx(s);
174 return 0;
175 }
176
177 static void
178 ef_init(void *foo) {
179 return;
180 }
181
182 static int
183 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
184 {
185 /* struct ef_link *sc = (struct ef_link*)ifp->if_softc;*/
186 struct ifaddr *ifa = (struct ifaddr*)data;
187 int s, error;
188
189 EFDEBUG("IOCTL %ld for %s%d\n", cmd, ifp->if_name, ifp->if_unit);
190 error = 0;
191 s = splimp();
192 switch (cmd) {
193 case SIOCSIFADDR:
194 if (ifp->if_unit == ETHER_FT_8023 &&
195 ifa->ifa_addr->sa_family != AF_IPX) {
196 error = EAFNOSUPPORT;
197 break;
198 }
199 ifp->if_flags |= IFF_UP;
200 /* FALL THROUGH */
201 case SIOCGIFADDR:
202 case SIOCSIFMTU:
203 error = ether_ioctl(ifp, cmd, data);
204 break;
205 case SIOCSIFFLAGS:
206 error = 0;
207 break;
208 default:
209 error = EINVAL;
210 }
211 splx(s);
212 return error;
213 }
214
215 /*
216 * Currently packet prepared in the ether_output(), but this can be a better
217 * place.
218 */
219 static void
220 ef_start(struct ifnet *ifp)
221 {
222 struct efnet *sc = (struct efnet*)ifp->if_softc;
223 struct ifnet *p;
224 struct mbuf *m;
225
226 ifp->if_flags |= IFF_OACTIVE;
227 p = sc->ef_ifp;
228
229 EFDEBUG("\n");
230 for (;;) {
231 IF_DEQUEUE(&ifp->if_snd, m);
232 if (m == 0)
233 break;
234 if (ifp->if_bpf)
235 bpf_mtap(ifp, m);
236 if (IF_QFULL(&p->if_snd)) {
237 IF_DROP(&p->if_snd);
238 ifp->if_oerrors++;
239 m_freem(m);
240 continue;
241 }
242 IF_ENQUEUE(&p->if_snd, m);
243 if ((p->if_flags & IFF_OACTIVE) == 0) {
244 p->if_start(p);
245 ifp->if_opackets++;
246 }
247 }
248 ifp->if_flags &= ~IFF_OACTIVE;
249 return;
250 }
251
252 /*
253 * Inline functions do not put additional overhead to procedure call or
254 * parameter passing but simplify the code
255 */
256 static int __inline
257 ef_inputEII(struct mbuf *m, struct ether_header *eh, struct llc* l,
258 u_short ether_type, struct ifqueue **inq)
259 {
260 switch(ether_type) {
261 #ifdef IPX
262 case ETHERTYPE_IPX:
263 schednetisr(NETISR_IPX);
264 *inq = &ipxintrq;
265 break;
266 #endif
267 #ifdef INET
268 case ETHERTYPE_IP:
269 if (ipflow_fastforward(m))
270 return 1;
271 schednetisr(NETISR_IP);
272 *inq = &ipintrq;
273 break;
274
275 case ETHERTYPE_ARP:
276 schednetisr(NETISR_ARP);
277 *inq = &arpintrq;
278 break;
279 #endif
280 default:
281 return EPROTONOSUPPORT;
282 }
283 return 0;
284 }
285
286 static int __inline
287 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
288 u_short ether_type, struct ifqueue **inq)
289 {
290 switch(ether_type) {
291 #ifdef IPX
292 case ETHERTYPE_IPX:
293 m_adj(m, 8);
294 schednetisr(NETISR_IPX);
295 *inq = &ipxintrq;
296 break;
297 #endif
298 default:
299 return EPROTONOSUPPORT;
300 }
301 return 0;
302 }
303
304 static int __inline
305 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
306 u_short ether_type, struct ifqueue **inq)
307 {
308 switch(ether_type) {
309 #ifdef IPX
310 case 0xe0:
311 m_adj(m, 3);
312 schednetisr(NETISR_IPX);
313 *inq = &ipxintrq;
314 break;
315 #endif
316 default:
317 return EPROTONOSUPPORT;
318 }
319 return 0;
320 }
321 /*
322 * Called from ether_input()
323 */
324 static int
325 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
326 {
327 u_short ether_type;
328 int s, ft = -1;
329 struct ifqueue *inq;
330 struct efnet *efp;
331 struct ifnet *eifp;
332 struct llc *l;
333 struct ef_link *efl;
334
335 ether_type = ntohs(eh->ether_type);
336 if (ether_type < ETHERMTU) {
337 l = mtod(m, struct llc*);
338 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
339 /*
340 * Novell's "802.3" frame
341 */
342 ft = ETHER_FT_8023;
343 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
344 /*
345 * 802.2/SNAP
346 */
347 ft = ETHER_FT_SNAP;
348 ether_type = ntohs(l->llc_un.type_snap.ether_type);
349 } else if (l->llc_dsap == l->llc_ssap) {
350 /*
351 * 802.3/802.2
352 */
353 ft = ETHER_FT_8022;
354 ether_type = l->llc_ssap;
355 }
356 } else
357 ft = ETHER_FT_EII;
358
359 if (ft == -1) {
360 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
361 return EPROTONOSUPPORT;
362 }
363
364 /*
365 * Check if interface configured for the given frame
366 */
367 efp = NULL;
368 SLIST_FOREACH(efl, &efdev, el_next) {
369 if (efl->el_ifp == ifp) {
370 efp = efl->el_units[ft];
371 break;
372 }
373 }
374 if (efp == NULL) {
375 EFDEBUG("Can't find if for %d\n", ft);
376 return EPROTONOSUPPORT;
377 }
378 eifp = &efp->ef_ac.ac_if;
379 if ((eifp->if_flags & IFF_UP) == 0)
380 return EPROTONOSUPPORT;
381 eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
382 m->m_pkthdr.rcvif = eifp;
383
384 if (eifp->if_bpf) {
385 struct mbuf m0;
386 m0.m_next = m;
387 m0.m_len = sizeof(struct ether_header);
388 m0.m_data = (char *)eh;
389 bpf_mtap(eifp, &m0);
390 }
391 /*
392 * Now we ready to adjust mbufs and pass them to protocol intr's
393 */
394 inq = NULL;
395 switch(ft) {
396 case ETHER_FT_EII:
397 if (ef_inputEII(m, eh, l, ether_type, &inq) != 0)
398 return EPROTONOSUPPORT;
399 break;
400 #ifdef IPX
401 case ETHER_FT_8023: /* only IPX can be here */
402 schednetisr(NETISR_IPX);
403 inq = &ipxintrq;
404 break;
405 #endif
406 case ETHER_FT_SNAP:
407 if (ef_inputSNAP(m, eh, l, ether_type, &inq) != 0)
408 return EPROTONOSUPPORT;
409 break;
410 case ETHER_FT_8022:
411 if (ef_input8022(m, eh, l, ether_type, &inq) != 0)
412 return EPROTONOSUPPORT;
413 break;
414 }
415
416 if (inq == NULL) {
417 EFDEBUG("No support for frame %d and proto %04x\n",
418 ft, ether_type);
419 return EPROTONOSUPPORT;
420 }
421 s = splimp();
422 if (IF_QFULL(inq)) {
423 IF_DROP(inq);
424 m_freem(m);
425 } else
426 IF_ENQUEUE(inq, m);
427 splx(s);
428 return 0;
429 }
430
431 static int
432 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
433 int *hlen)
434 {
435 struct mbuf *m = *mp;
436 u_char *cp;
437 short type;
438
439 if (ifp->if_type != IFT_XETHER)
440 return ENETDOWN;
441 switch (ifp->if_unit) {
442 case ETHER_FT_EII:
443 #ifdef IPX
444 type = htons(ETHERTYPE_IPX);
445 #else
446 return EPFNOSUPPORT;
447 #endif
448 break;
449 case ETHER_FT_8023:
450 type = htons(m->m_pkthdr.len);
451 break;
452 case ETHER_FT_8022:
453 M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAIT);
454 if (m == NULL) {
455 *mp = NULL;
456 return ENOBUFS;
457 }
458 /*
459 * Ensure that ethernet header and next three bytes
460 * will fit into single mbuf
461 */
462 m = m_pullup(m, ETHER_HDR_LEN + 3);
463 if (m == NULL) {
464 *mp = NULL;
465 return ENOBUFS;
466 }
467 m_adj(m, ETHER_HDR_LEN);
468 type = htons(m->m_pkthdr.len);
469 cp = mtod(m, u_char *);
470 *cp++ = 0xE0;
471 *cp++ = 0xE0;
472 *cp++ = 0x03;
473 *hlen += 3;
474 break;
475 case ETHER_FT_SNAP:
476 M_PREPEND(m, 8, M_WAIT);
477 if (m == NULL) {
478 *mp = NULL;
479 return ENOBUFS;
480 }
481 type = htons(m->m_pkthdr.len);
482 cp = mtod(m, u_char *);
483 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
484 *hlen += 8;
485 break;
486 default:
487 return EPFNOSUPPORT;
488 }
489 *mp = m;
490 *tp = type;
491 return 0;
492 }
493
494 /*
495 * Create clone from the given interface
496 */
497 static int
498 ef_clone(struct ef_link *efl, int ft)
499 {
500 struct efnet *efp;
501 struct ifnet *eifp;
502 struct ifnet *ifp = efl->el_ifp;
503 char cbuf[IFNAMSIZ], *ifname;
504 int ifnlen;
505
506 efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
507 M_WAITOK | M_ZERO);
508 if (efp == NULL)
509 return ENOMEM;
510 efp->ef_ifp = ifp;
511 eifp = &efp->ef_ac.ac_if;
512 ifnlen = 1 + snprintf(cbuf, sizeof(cbuf), "%s%df", ifp->if_name,
513 ifp->if_unit);
514 ifname = (char*)malloc(ifnlen, M_IFADDR, M_WAITOK);
515 eifp->if_name = strcpy(ifname, cbuf);
516 eifp->if_unit = ft;
517 eifp->if_softc = efp;
518 if (ifp->if_ioctl)
519 eifp->if_ioctl = ef_ioctl;
520 efl->el_units[ft] = efp;
521 return 0;
522 }
523
524 static int
525 ef_load(void)
526 {
527 struct ifnet *ifp;
528 struct efnet *efp;
529 struct ef_link *efl = NULL;
530 int error = 0, d;
531
532 TAILQ_FOREACH(ifp, &ifnet, if_link) {
533 if (ifp->if_type != IFT_ETHER) continue;
534 EFDEBUG("Found interface %s%d\n", ifp->if_name, ifp->if_unit);
535 efl = (struct ef_link*)malloc(sizeof(struct ef_link),
536 M_IFADDR, M_WAITOK);
537 if (efl == NULL) {
538 error = ENOMEM;
539 break;
540 }
541 bzero(efl, sizeof(*efl));
542
543 efl->el_ifp = ifp;
544 #ifdef ETHER_II
545 error = ef_clone(efl, ETHER_FT_EII);
546 if (error) break;
547 #endif
548 #ifdef ETHER_8023
549 error = ef_clone(efl, ETHER_FT_8023);
550 if (error) break;
551 #endif
552 #ifdef ETHER_8022
553 error = ef_clone(efl, ETHER_FT_8022);
554 if (error) break;
555 #endif
556 #ifdef ETHER_SNAP
557 error = ef_clone(efl, ETHER_FT_SNAP);
558 if (error) break;
559 #endif
560 efcount++;
561 SLIST_INSERT_HEAD(&efdev, efl, el_next);
562 }
563 if (error) {
564 if (efl)
565 SLIST_INSERT_HEAD(&efdev, efl, el_next);
566 SLIST_FOREACH(efl, &efdev, el_next) {
567 for (d = 0; d < EF_NFT; d++)
568 if (efl->el_units[d])
569 free(efl->el_units[d], M_IFADDR);
570 free(efl, M_IFADDR);
571 }
572 return error;
573 }
574 SLIST_FOREACH(efl, &efdev, el_next) {
575 for (d = 0; d < EF_NFT; d++) {
576 efp = efl->el_units[d];
577 if (efp)
578 ef_attach(efp);
579 }
580 }
581 ef_inputp = ef_input;
582 ef_outputp = ef_output;
583 EFDEBUG("Loaded\n");
584 return 0;
585 }
586
587 static int
588 ef_unload(void)
589 {
590 struct efnet *efp;
591 struct ef_link *efl;
592 int d;
593
594 ef_inputp = NULL;
595 ef_outputp = NULL;
596 SLIST_FOREACH(efl, &efdev, el_next) {
597 for (d = 0; d < EF_NFT; d++) {
598 efp = efl->el_units[d];
599 if (efp) {
600 ef_detach(efp);
601 }
602 }
603 }
604 EFDEBUG("Unloaded\n");
605 return 0;
606 }
607
608 static int
609 if_ef_modevent(module_t mod, int type, void *data)
610 {
611 switch ((modeventtype_t)type) {
612 case MOD_LOAD:
613 return ef_load();
614 case MOD_UNLOAD:
615 return ef_unload();
616 default:
617 break;
618 }
619 return 0;
620 }
621
622 static moduledata_t if_ef_mod = {
623 "if_ef", if_ef_modevent, NULL
624 };
625
626 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
Cache object: 470f43b9b4df446d8a512a03ae9414c4
|