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: releng/5.0/sys/net/if_ef.c 106939 2002-11-15 00:00:15Z sam $
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, __func__ ,## args)
73 #else
74 #define EFDEBUG(format, args...)
75 #endif
76
77 #define EFERROR(format, args...) printf("%s: "format, __func__ ,## 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 *ifa2;
120 struct sockaddr_dl *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 ifa2 = ifaddr_byindex(sc->ef_ifp->if_index);
132 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
133 ether_ifattach(ifp, LLADDR(sdl2));
134
135 ifp->if_resolvemulti = 0;
136 ifp->if_type = IFT_XETHER;
137 ifp->if_flags |= IFF_RUNNING;
138
139 bcopy(LLADDR(sdl2), sc->ef_ac.ac_enaddr, ETHER_ADDR_LEN);
140
141 EFDEBUG("%s%d: attached\n", ifp->if_name, ifp->if_unit);
142 return 1;
143 }
144
145 /*
146 * This is for _testing_only_, just removes interface from interfaces list
147 */
148 static int
149 ef_detach(struct efnet *sc)
150 {
151 struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
152 int s;
153
154 s = splimp();
155
156 if (ifp->if_flags & IFF_UP) {
157 if_down(ifp);
158 if (ifp->if_flags & IFF_RUNNING) {
159 /* find internet addresses and delete routes */
160 register struct ifaddr *ifa;
161 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
162 rtinit(ifa, (int)RTM_DELETE, 0);
163 }
164 }
165 }
166
167 TAILQ_REMOVE(&ifnet, ifp, if_link);
168 splx(s);
169 return 0;
170 }
171
172 static void
173 ef_init(void *foo) {
174 return;
175 }
176
177 static int
178 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
179 {
180 /* struct ef_link *sc = (struct ef_link*)ifp->if_softc;*/
181 struct ifaddr *ifa = (struct ifaddr*)data;
182 int s, error;
183
184 EFDEBUG("IOCTL %ld for %s%d\n", cmd, ifp->if_name, ifp->if_unit);
185 error = 0;
186 s = splimp();
187 switch (cmd) {
188 case SIOCSIFFLAGS:
189 error = 0;
190 break;
191 case SIOCSIFADDR:
192 if (ifp->if_unit == ETHER_FT_8023 &&
193 ifa->ifa_addr->sa_family != AF_IPX) {
194 error = EAFNOSUPPORT;
195 break;
196 }
197 ifp->if_flags |= IFF_UP;
198 /* FALL THROUGH */
199 default:
200 error = ether_ioctl(ifp, cmd, data);
201 break;
202 }
203 splx(s);
204 return error;
205 }
206
207 /*
208 * Currently packet prepared in the ether_output(), but this can be a better
209 * place.
210 */
211 static void
212 ef_start(struct ifnet *ifp)
213 {
214 struct efnet *sc = (struct efnet*)ifp->if_softc;
215 struct ifnet *p;
216 struct mbuf *m;
217
218 ifp->if_flags |= IFF_OACTIVE;
219 p = sc->ef_ifp;
220
221 EFDEBUG("\n");
222 for (;;) {
223 IF_DEQUEUE(&ifp->if_snd, m);
224 if (m == 0)
225 break;
226 BPF_MTAP(ifp, m);
227 if (! IF_HANDOFF(&p->if_snd, m, p)) {
228 ifp->if_oerrors++;
229 continue;
230 }
231 ifp->if_opackets++;
232 }
233 ifp->if_flags &= ~IFF_OACTIVE;
234 return;
235 }
236
237 /*
238 * Inline functions do not put additional overhead to procedure call or
239 * parameter passing but simplify the code
240 */
241 static int __inline
242 ef_inputEII(struct mbuf *m, struct ether_header *eh,
243 u_short ether_type, struct ifqueue **inq)
244 {
245 switch(ether_type) {
246 #ifdef IPX
247 case ETHERTYPE_IPX:
248 schednetisr(NETISR_IPX);
249 *inq = &ipxintrq;
250 break;
251 #endif
252 #ifdef INET
253 case ETHERTYPE_IP:
254 if (ipflow_fastforward(m))
255 return 1;
256 schednetisr(NETISR_IP);
257 *inq = &ipintrq;
258 break;
259
260 case ETHERTYPE_ARP:
261 schednetisr(NETISR_ARP);
262 *inq = &arpintrq;
263 break;
264 #endif
265 default:
266 return EPROTONOSUPPORT;
267 }
268 return 0;
269 }
270
271 static int __inline
272 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
273 u_short ether_type, struct ifqueue **inq)
274 {
275 switch(ether_type) {
276 #ifdef IPX
277 case ETHERTYPE_IPX:
278 m_adj(m, 8);
279 schednetisr(NETISR_IPX);
280 *inq = &ipxintrq;
281 break;
282 #endif
283 default:
284 return EPROTONOSUPPORT;
285 }
286 return 0;
287 }
288
289 static int __inline
290 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
291 u_short ether_type, struct ifqueue **inq)
292 {
293 switch(ether_type) {
294 #ifdef IPX
295 case 0xe0:
296 m_adj(m, 3);
297 schednetisr(NETISR_IPX);
298 *inq = &ipxintrq;
299 break;
300 #endif
301 default:
302 return EPROTONOSUPPORT;
303 }
304 return 0;
305 }
306 /*
307 * Called from ether_input()
308 */
309 static int
310 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
311 {
312 u_short ether_type;
313 int ft = -1;
314 struct ifqueue *inq;
315 struct efnet *efp;
316 struct ifnet *eifp;
317 struct llc *l;
318 struct ef_link *efl;
319
320 ether_type = ntohs(eh->ether_type);
321 if (ether_type < ETHERMTU) {
322 l = mtod(m, struct llc*);
323 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
324 /*
325 * Novell's "802.3" frame
326 */
327 ft = ETHER_FT_8023;
328 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
329 /*
330 * 802.2/SNAP
331 */
332 ft = ETHER_FT_SNAP;
333 ether_type = ntohs(l->llc_un.type_snap.ether_type);
334 } else if (l->llc_dsap == l->llc_ssap) {
335 /*
336 * 802.3/802.2
337 */
338 ft = ETHER_FT_8022;
339 ether_type = l->llc_ssap;
340 }
341 } else
342 ft = ETHER_FT_EII;
343
344 if (ft == -1) {
345 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
346 return EPROTONOSUPPORT;
347 }
348
349 /*
350 * Check if interface configured for the given frame
351 */
352 efp = NULL;
353 SLIST_FOREACH(efl, &efdev, el_next) {
354 if (efl->el_ifp == ifp) {
355 efp = efl->el_units[ft];
356 break;
357 }
358 }
359 if (efp == NULL) {
360 EFDEBUG("Can't find if for %d\n", ft);
361 return EPROTONOSUPPORT;
362 }
363 eifp = &efp->ef_ac.ac_if;
364 if ((eifp->if_flags & IFF_UP) == 0)
365 return EPROTONOSUPPORT;
366 eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
367 m->m_pkthdr.rcvif = eifp;
368
369 if (eifp->if_bpf) {
370 struct mbuf m0;
371 m0.m_next = m;
372 m0.m_len = sizeof(struct ether_header);
373 m0.m_data = (char *)eh;
374 BPF_MTAP(eifp, &m0);
375 }
376 /*
377 * Now we ready to adjust mbufs and pass them to protocol intr's
378 */
379 inq = NULL;
380 switch(ft) {
381 case ETHER_FT_EII:
382 if (ef_inputEII(m, eh, ether_type, &inq) != 0)
383 return EPROTONOSUPPORT;
384 break;
385 #ifdef IPX
386 case ETHER_FT_8023: /* only IPX can be here */
387 schednetisr(NETISR_IPX);
388 inq = &ipxintrq;
389 break;
390 #endif
391 case ETHER_FT_SNAP:
392 if (ef_inputSNAP(m, eh, l, ether_type, &inq) != 0)
393 return EPROTONOSUPPORT;
394 break;
395 case ETHER_FT_8022:
396 if (ef_input8022(m, eh, l, ether_type, &inq) != 0)
397 return EPROTONOSUPPORT;
398 break;
399 }
400
401 if (inq == NULL) {
402 EFDEBUG("No support for frame %d and proto %04x\n",
403 ft, ether_type);
404 return EPROTONOSUPPORT;
405 }
406 (void) IF_HANDOFF(inq, m, NULL);
407 return 0;
408 }
409
410 static int
411 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
412 int *hlen)
413 {
414 struct mbuf *m = *mp;
415 u_char *cp;
416 short type;
417
418 if (ifp->if_type != IFT_XETHER)
419 return ENETDOWN;
420 switch (ifp->if_unit) {
421 case ETHER_FT_EII:
422 #ifdef IPX
423 type = htons(ETHERTYPE_IPX);
424 #else
425 return EPFNOSUPPORT;
426 #endif
427 break;
428 case ETHER_FT_8023:
429 type = htons(m->m_pkthdr.len);
430 break;
431 case ETHER_FT_8022:
432 M_PREPEND(m, ETHER_HDR_LEN + 3, M_TRYWAIT);
433 if (m == NULL) {
434 *mp = NULL;
435 return ENOBUFS;
436 }
437 /*
438 * Ensure that ethernet header and next three bytes
439 * will fit into single mbuf
440 */
441 m = m_pullup(m, ETHER_HDR_LEN + 3);
442 if (m == NULL) {
443 *mp = NULL;
444 return ENOBUFS;
445 }
446 m_adj(m, ETHER_HDR_LEN);
447 type = htons(m->m_pkthdr.len);
448 cp = mtod(m, u_char *);
449 *cp++ = 0xE0;
450 *cp++ = 0xE0;
451 *cp++ = 0x03;
452 *hlen += 3;
453 break;
454 case ETHER_FT_SNAP:
455 M_PREPEND(m, 8, M_TRYWAIT);
456 if (m == NULL) {
457 *mp = NULL;
458 return ENOBUFS;
459 }
460 type = htons(m->m_pkthdr.len);
461 cp = mtod(m, u_char *);
462 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
463 *hlen += 8;
464 break;
465 default:
466 return EPFNOSUPPORT;
467 }
468 *mp = m;
469 *tp = type;
470 return 0;
471 }
472
473 /*
474 * Create clone from the given interface
475 */
476 static int
477 ef_clone(struct ef_link *efl, int ft)
478 {
479 struct efnet *efp;
480 struct ifnet *eifp;
481 struct ifnet *ifp = efl->el_ifp;
482 char cbuf[IFNAMSIZ], *ifname;
483 int ifnlen;
484
485 efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
486 M_WAITOK | M_ZERO);
487 if (efp == NULL)
488 return ENOMEM;
489 efp->ef_ifp = ifp;
490 eifp = &efp->ef_ac.ac_if;
491 ifnlen = 1 + snprintf(cbuf, sizeof(cbuf), "%s%df", ifp->if_name,
492 ifp->if_unit);
493 ifname = (char*)malloc(ifnlen, M_IFADDR, M_WAITOK);
494 eifp->if_name = strcpy(ifname, cbuf);
495 eifp->if_unit = ft;
496 eifp->if_softc = efp;
497 if (ifp->if_ioctl)
498 eifp->if_ioctl = ef_ioctl;
499 efl->el_units[ft] = efp;
500 return 0;
501 }
502
503 static int
504 ef_load(void)
505 {
506 struct ifnet *ifp;
507 struct efnet *efp;
508 struct ef_link *efl = NULL;
509 int error = 0, d;
510
511 TAILQ_FOREACH(ifp, &ifnet, if_link) {
512 if (ifp->if_type != IFT_ETHER) continue;
513 EFDEBUG("Found interface %s%d\n", ifp->if_name, ifp->if_unit);
514 efl = (struct ef_link*)malloc(sizeof(struct ef_link),
515 M_IFADDR, M_WAITOK | M_ZERO);
516 if (efl == NULL) {
517 error = ENOMEM;
518 break;
519 }
520
521 efl->el_ifp = ifp;
522 #ifdef ETHER_II
523 error = ef_clone(efl, ETHER_FT_EII);
524 if (error) break;
525 #endif
526 #ifdef ETHER_8023
527 error = ef_clone(efl, ETHER_FT_8023);
528 if (error) break;
529 #endif
530 #ifdef ETHER_8022
531 error = ef_clone(efl, ETHER_FT_8022);
532 if (error) break;
533 #endif
534 #ifdef ETHER_SNAP
535 error = ef_clone(efl, ETHER_FT_SNAP);
536 if (error) break;
537 #endif
538 efcount++;
539 SLIST_INSERT_HEAD(&efdev, efl, el_next);
540 }
541 if (error) {
542 if (efl)
543 SLIST_INSERT_HEAD(&efdev, efl, el_next);
544 SLIST_FOREACH(efl, &efdev, el_next) {
545 for (d = 0; d < EF_NFT; d++)
546 if (efl->el_units[d])
547 free(efl->el_units[d], M_IFADDR);
548 free(efl, M_IFADDR);
549 }
550 return error;
551 }
552 SLIST_FOREACH(efl, &efdev, el_next) {
553 for (d = 0; d < EF_NFT; d++) {
554 efp = efl->el_units[d];
555 if (efp)
556 ef_attach(efp);
557 }
558 }
559 ef_inputp = ef_input;
560 ef_outputp = ef_output;
561 EFDEBUG("Loaded\n");
562 return 0;
563 }
564
565 static int
566 ef_unload(void)
567 {
568 struct efnet *efp;
569 struct ef_link *efl;
570 int d;
571
572 ef_inputp = NULL;
573 ef_outputp = NULL;
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_detach(efp);
579 }
580 }
581 }
582 EFDEBUG("Unloaded\n");
583 return 0;
584 }
585
586 static int
587 if_ef_modevent(module_t mod, int type, void *data)
588 {
589 switch ((modeventtype_t)type) {
590 case MOD_LOAD:
591 return ef_load();
592 case MOD_UNLOAD:
593 return ef_unload();
594 default:
595 break;
596 }
597 return 0;
598 }
599
600 static moduledata_t if_ef_mod = {
601 "if_ef", if_ef_modevent, NULL
602 };
603
604 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
Cache object: 752fd4bf1530d96c52713795a7be7843
|