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: src/sys/net/if_ef.c,v 1.42 2008/10/02 15:37:58 zec Exp $
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 #include <sys/vimage.h>
43
44 #include <net/ethernet.h>
45 #include <net/if_llc.h>
46 #include <net/if.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/netisr.h>
51 #include <net/route.h>
52 #include <net/bpf.h>
53
54 #ifdef INET
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/if_ether.h>
58 #endif
59
60 #ifdef IPX
61 #include <netipx/ipx.h>
62 #include <netipx/ipx_if.h>
63 #endif
64
65 /* If none of the supported layers is enabled explicitly enable them all */
66 #if !defined(ETHER_II) && !defined(ETHER_8023) && !defined(ETHER_8022) && \
67 !defined(ETHER_SNAP)
68 #define ETHER_II 1
69 #define ETHER_8023 1
70 #define ETHER_8022 1
71 #define ETHER_SNAP 1
72 #endif
73
74 /* internal frame types */
75 #define ETHER_FT_EII 0 /* Ethernet_II - default */
76 #define ETHER_FT_8023 1 /* 802.3 (Novell) */
77 #define ETHER_FT_8022 2 /* 802.2 */
78 #define ETHER_FT_SNAP 3 /* SNAP */
79 #define EF_NFT 4 /* total number of frame types */
80
81 #ifdef EF_DEBUG
82 #define EFDEBUG(format, args...) printf("%s: "format, __func__ ,## args)
83 #else
84 #define EFDEBUG(format, args...)
85 #endif
86
87 #define EFERROR(format, args...) printf("%s: "format, __func__ ,## args)
88
89 struct efnet {
90 struct ifnet *ef_ifp;
91 struct ifnet *ef_pifp;
92 int ef_frametype;
93 };
94
95 struct ef_link {
96 SLIST_ENTRY(ef_link) el_next;
97 struct ifnet *el_ifp; /* raw device for this clones */
98 struct efnet *el_units[EF_NFT]; /* our clones */
99 };
100
101 static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
102 static int efcount;
103
104 extern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
105 extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
106 struct sockaddr *dst, short *tp, int *hlen);
107
108 /*
109 static void ef_reset (struct ifnet *);
110 */
111 static int ef_attach(struct efnet *sc);
112 static int ef_detach(struct efnet *sc);
113 static void ef_init(void *);
114 static int ef_ioctl(struct ifnet *, u_long, caddr_t);
115 static void ef_start(struct ifnet *);
116 static int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
117 static int ef_output(struct ifnet *ifp, struct mbuf **mp,
118 struct sockaddr *dst, short *tp, int *hlen);
119
120 static int ef_load(void);
121 static int ef_unload(void);
122
123 /*
124 * Install the interface, most of structure initialization done in ef_clone()
125 */
126 static int
127 ef_attach(struct efnet *sc)
128 {
129 struct ifnet *ifp = sc->ef_ifp;
130
131 ifp->if_start = ef_start;
132 ifp->if_watchdog = NULL;
133 ifp->if_init = ef_init;
134 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
135 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
136 /*
137 * Attach the interface
138 */
139 ether_ifattach(ifp, IF_LLADDR(sc->ef_pifp));
140
141 ifp->if_resolvemulti = 0;
142 ifp->if_type = IFT_XETHER;
143 ifp->if_drv_flags |= IFF_DRV_RUNNING;
144
145 EFDEBUG("%s: attached\n", ifp->if_xname);
146 return 1;
147 }
148
149 /*
150 * This is for _testing_only_, just removes interface from interfaces list
151 */
152 static int
153 ef_detach(struct efnet *sc)
154 {
155 struct ifnet *ifp = sc->ef_ifp;
156 int s;
157
158 s = splimp();
159
160 ether_ifdetach(ifp);
161 if_free(ifp);
162
163 splx(s);
164 return 0;
165 }
166
167 static void
168 ef_init(void *foo) {
169 return;
170 }
171
172 static int
173 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
174 {
175 struct efnet *sc = ifp->if_softc;
176 struct ifaddr *ifa = (struct ifaddr*)data;
177 int s, error;
178
179 EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
180 error = 0;
181 s = splimp();
182 switch (cmd) {
183 case SIOCSIFFLAGS:
184 error = 0;
185 break;
186 case SIOCSIFADDR:
187 if (sc->ef_frametype == ETHER_FT_8023 &&
188 ifa->ifa_addr->sa_family != AF_IPX) {
189 error = EAFNOSUPPORT;
190 break;
191 }
192 ifp->if_flags |= IFF_UP;
193 /* FALL THROUGH */
194 default:
195 error = ether_ioctl(ifp, cmd, data);
196 break;
197 }
198 splx(s);
199 return error;
200 }
201
202 /*
203 * Currently packet prepared in the ether_output(), but this can be a better
204 * place.
205 */
206 static void
207 ef_start(struct ifnet *ifp)
208 {
209 struct efnet *sc = (struct efnet*)ifp->if_softc;
210 struct ifnet *p;
211 struct mbuf *m;
212 int error;
213
214 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
215 p = sc->ef_pifp;
216
217 EFDEBUG("\n");
218 for (;;) {
219 IF_DEQUEUE(&ifp->if_snd, m);
220 if (m == 0)
221 break;
222 BPF_MTAP(ifp, m);
223 IFQ_HANDOFF(p, m, error);
224 if (error) {
225 ifp->if_oerrors++;
226 continue;
227 }
228 ifp->if_opackets++;
229 }
230 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
231 return;
232 }
233
234 /*
235 * Inline functions do not put additional overhead to procedure call or
236 * parameter passing but simplify the code
237 */
238 static int __inline
239 ef_inputEII(struct mbuf *m, struct ether_header *eh, u_short ether_type)
240 {
241 int isr;
242
243 switch(ether_type) {
244 #ifdef IPX
245 case ETHERTYPE_IPX:
246 isr = NETISR_IPX;
247 break;
248 #endif
249 #ifdef INET
250 case ETHERTYPE_IP:
251 if ((m = ip_fastforward(m)) == NULL)
252 return (0);
253 isr = NETISR_IP;
254 break;
255
256 case ETHERTYPE_ARP:
257 isr = NETISR_ARP;
258 break;
259 #endif
260 default:
261 return (EPROTONOSUPPORT);
262 }
263 netisr_dispatch(isr, m);
264 return (0);
265 }
266
267 static int __inline
268 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
269 u_short ether_type)
270 {
271 int isr;
272
273 switch(ether_type) {
274 #ifdef IPX
275 case ETHERTYPE_IPX:
276 m_adj(m, 8);
277 isr = NETISR_IPX;
278 break;
279 #endif
280 default:
281 return (EPROTONOSUPPORT);
282 }
283 netisr_dispatch(isr, m);
284 return (0);
285 }
286
287 static int __inline
288 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
289 u_short ether_type)
290 {
291 int isr;
292
293 switch(ether_type) {
294 #ifdef IPX
295 case 0xe0:
296 m_adj(m, 3);
297 isr = NETISR_IPX;
298 break;
299 #endif
300 default:
301 return (EPROTONOSUPPORT);
302 }
303 netisr_dispatch(isr, m);
304 return (0);
305 }
306
307 /*
308 * Called from ether_input()
309 */
310 static int
311 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
312 {
313 u_short ether_type;
314 int ft = -1;
315 struct efnet *efp;
316 struct ifnet *eifp;
317 struct llc *l;
318 struct ef_link *efl;
319 int isr;
320
321 ether_type = ntohs(eh->ether_type);
322 l = NULL;
323 if (ether_type < ETHERMTU) {
324 l = mtod(m, struct llc*);
325 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
326 /*
327 * Novell's "802.3" frame
328 */
329 ft = ETHER_FT_8023;
330 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
331 /*
332 * 802.2/SNAP
333 */
334 ft = ETHER_FT_SNAP;
335 ether_type = ntohs(l->llc_un.type_snap.ether_type);
336 } else if (l->llc_dsap == l->llc_ssap) {
337 /*
338 * 802.3/802.2
339 */
340 ft = ETHER_FT_8022;
341 ether_type = l->llc_ssap;
342 }
343 } else
344 ft = ETHER_FT_EII;
345
346 if (ft == -1) {
347 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
348 return EPROTONOSUPPORT;
349 }
350
351 /*
352 * Check if interface configured for the given frame
353 */
354 efp = NULL;
355 SLIST_FOREACH(efl, &efdev, el_next) {
356 if (efl->el_ifp == ifp) {
357 efp = efl->el_units[ft];
358 break;
359 }
360 }
361 if (efp == NULL) {
362 EFDEBUG("Can't find if for %d\n", ft);
363 return EPROTONOSUPPORT;
364 }
365 eifp = efp->ef_ifp;
366 if ((eifp->if_flags & IFF_UP) == 0)
367 return EPROTONOSUPPORT;
368 eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
369 m->m_pkthdr.rcvif = eifp;
370
371 BPF_MTAP2(eifp, eh, ETHER_HDR_LEN, m);
372 /*
373 * Now we ready to adjust mbufs and pass them to protocol intr's
374 */
375 switch(ft) {
376 case ETHER_FT_EII:
377 return (ef_inputEII(m, eh, ether_type));
378 #ifdef IPX
379 case ETHER_FT_8023: /* only IPX can be here */
380 isr = NETISR_IPX;
381 break;
382 #endif
383 case ETHER_FT_SNAP:
384 return (ef_inputSNAP(m, eh, l, ether_type));
385 case ETHER_FT_8022:
386 return (ef_input8022(m, eh, l, ether_type));
387 default:
388 EFDEBUG("No support for frame %d and proto %04x\n",
389 ft, ether_type);
390 return (EPROTONOSUPPORT);
391 }
392 netisr_dispatch(isr, m);
393 return (0);
394 }
395
396 static int
397 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
398 int *hlen)
399 {
400 struct efnet *sc = (struct efnet*)ifp->if_softc;
401 struct mbuf *m = *mp;
402 u_char *cp;
403 short type;
404
405 if (ifp->if_type != IFT_XETHER)
406 return ENETDOWN;
407 switch (sc->ef_frametype) {
408 case ETHER_FT_EII:
409 #ifdef IPX
410 type = htons(ETHERTYPE_IPX);
411 #else
412 return EPFNOSUPPORT;
413 #endif
414 break;
415 case ETHER_FT_8023:
416 type = htons(m->m_pkthdr.len);
417 break;
418 case ETHER_FT_8022:
419 M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAIT);
420 /*
421 * Ensure that ethernet header and next three bytes
422 * will fit into single mbuf
423 */
424 m = m_pullup(m, ETHER_HDR_LEN + 3);
425 if (m == NULL) {
426 *mp = NULL;
427 return ENOBUFS;
428 }
429 m_adj(m, ETHER_HDR_LEN);
430 type = htons(m->m_pkthdr.len);
431 cp = mtod(m, u_char *);
432 *cp++ = 0xE0;
433 *cp++ = 0xE0;
434 *cp++ = 0x03;
435 *hlen += 3;
436 break;
437 case ETHER_FT_SNAP:
438 M_PREPEND(m, 8, M_WAIT);
439 type = htons(m->m_pkthdr.len);
440 cp = mtod(m, u_char *);
441 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
442 *hlen += 8;
443 break;
444 default:
445 return EPFNOSUPPORT;
446 }
447 *mp = m;
448 *tp = type;
449 return 0;
450 }
451
452 /*
453 * Create clone from the given interface
454 */
455 static int
456 ef_clone(struct ef_link *efl, int ft)
457 {
458 struct efnet *efp;
459 struct ifnet *eifp;
460 struct ifnet *ifp = efl->el_ifp;
461
462 efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
463 M_WAITOK | M_ZERO);
464 if (efp == NULL)
465 return ENOMEM;
466 efp->ef_pifp = ifp;
467 efp->ef_frametype = ft;
468 eifp = efp->ef_ifp = if_alloc(IFT_ETHER);
469 if (eifp == NULL) {
470 free(efp, M_IFADDR);
471 return (ENOSPC);
472 }
473 snprintf(eifp->if_xname, IFNAMSIZ,
474 "%sf%d", ifp->if_xname, efp->ef_frametype);
475 eifp->if_dname = "ef";
476 eifp->if_dunit = IF_DUNIT_NONE;
477 eifp->if_softc = efp;
478 if (ifp->if_ioctl)
479 eifp->if_ioctl = ef_ioctl;
480 efl->el_units[ft] = efp;
481 return 0;
482 }
483
484 static int
485 ef_load(void)
486 {
487 VNET_ITERATOR_DECL(vnet_iter);
488 struct ifnet *ifp;
489 struct efnet *efp;
490 struct ef_link *efl = NULL, *efl_temp;
491 int error = 0, d;
492
493 VNET_LIST_RLOCK();
494 VNET_FOREACH(vnet_iter) {
495 CURVNET_SET(vnet_iter);
496 INIT_VNET_NET(vnet_iter);
497 IFNET_RLOCK();
498 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
499 if (ifp->if_type != IFT_ETHER) continue;
500 EFDEBUG("Found interface %s\n", ifp->if_xname);
501 efl = (struct ef_link*)malloc(sizeof(struct ef_link),
502 M_IFADDR, M_WAITOK | M_ZERO);
503 if (efl == NULL) {
504 error = ENOMEM;
505 break;
506 }
507
508 efl->el_ifp = ifp;
509 #ifdef ETHER_II
510 error = ef_clone(efl, ETHER_FT_EII);
511 if (error) break;
512 #endif
513 #ifdef ETHER_8023
514 error = ef_clone(efl, ETHER_FT_8023);
515 if (error) break;
516 #endif
517 #ifdef ETHER_8022
518 error = ef_clone(efl, ETHER_FT_8022);
519 if (error) break;
520 #endif
521 #ifdef ETHER_SNAP
522 error = ef_clone(efl, ETHER_FT_SNAP);
523 if (error) break;
524 #endif
525 efcount++;
526 SLIST_INSERT_HEAD(&efdev, efl, el_next);
527 }
528 IFNET_RUNLOCK();
529 CURVNET_RESTORE();
530 }
531 VNET_LIST_RUNLOCK();
532 if (error) {
533 if (efl)
534 SLIST_INSERT_HEAD(&efdev, efl, el_next);
535 SLIST_FOREACH_SAFE(efl, &efdev, el_next, efl_temp) {
536 for (d = 0; d < EF_NFT; d++)
537 if (efl->el_units[d]) {
538 if (efl->el_units[d]->ef_pifp != NULL)
539 if_free(efl->el_units[d]->ef_pifp);
540 free(efl->el_units[d], M_IFADDR);
541 }
542 free(efl, M_IFADDR);
543 }
544 return error;
545 }
546 SLIST_FOREACH(efl, &efdev, el_next) {
547 for (d = 0; d < EF_NFT; d++) {
548 efp = efl->el_units[d];
549 if (efp)
550 ef_attach(efp);
551 }
552 }
553 ef_inputp = ef_input;
554 ef_outputp = ef_output;
555 EFDEBUG("Loaded\n");
556 return 0;
557 }
558
559 static int
560 ef_unload(void)
561 {
562 struct efnet *efp;
563 struct ef_link *efl;
564 int d;
565
566 ef_inputp = NULL;
567 ef_outputp = NULL;
568 SLIST_FOREACH(efl, &efdev, el_next) {
569 for (d = 0; d < EF_NFT; d++) {
570 efp = efl->el_units[d];
571 if (efp) {
572 ef_detach(efp);
573 }
574 }
575 }
576 EFDEBUG("Unloaded\n");
577 return 0;
578 }
579
580 static int
581 if_ef_modevent(module_t mod, int type, void *data)
582 {
583 switch ((modeventtype_t)type) {
584 case MOD_LOAD:
585 return ef_load();
586 case MOD_UNLOAD:
587 return ef_unload();
588 default:
589 return EOPNOTSUPP;
590 }
591 return 0;
592 }
593
594 static moduledata_t if_ef_mod = {
595 "if_ef", if_ef_modevent, NULL
596 };
597
598 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
599
|