- Test m_pkthdr.fw_flags against DUMMYNET_MBUF_TAGGED before trying to locate
[dragonfly/netmp.git] / sys / net / ef / if_ef.c
blob3f6ead08d950a341e946145d00cc47277b4ec031
1 /*-
2 * Copyright (c) 1999, 2000 Boris Popov
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/sys/net/if_ef.c,v 1.2.2.4 2001/02/22 09:27:04 bp Exp $
27 * $DragonFly: src/sys/net/ef/if_ef.c,v 1.27 2008/07/27 10:06:57 sephe Exp $
30 #include "opt_inet.h"
31 #include "opt_ipx.h"
32 #include "opt_ef.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sockio.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/syslog.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/thread2.h>
45 #include <net/ethernet.h>
46 #include <net/if_llc.h>
47 #include <net/if.h>
48 #include <net/if_arp.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 #include <net/netisr.h>
52 #include <net/route.h>
53 #include <net/bpf.h>
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netinet/if_ether.h>
59 #endif
61 #ifdef IPX
62 #include <netproto/ipx/ipx.h>
63 #include <netproto/ipx/ipx_if.h>
64 #endif
66 /* internal frame types */
67 #define ETHER_FT_EII 0 /* Ethernet_II - default */
68 #define ETHER_FT_8023 1 /* 802.3 (Novell) */
69 #define ETHER_FT_8022 2 /* 802.2 */
70 #define ETHER_FT_SNAP 3 /* SNAP */
71 #define EF_NFT 4 /* total number of frame types */
73 #ifdef EF_DEBUG
74 #define EFDEBUG(format, args...) kprintf("%s: "format, __func__ ,## args)
75 #else
76 #define EFDEBUG(format, args...)
77 #endif
79 #define EFERROR(format, args...) kprintf("%s: "format, __func__ ,## args)
81 struct efnet {
82 struct arpcom ef_ac;
83 struct ifnet * ef_ifp;
84 int ef_frametype;
87 struct ef_link {
88 SLIST_ENTRY(ef_link) el_next;
89 struct ifnet *el_ifp; /* raw device for this clones */
90 struct efnet *el_units[EF_NFT]; /* our clones */
93 static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
94 static int efcount;
96 extern int (*ef_inputp)(struct ifnet*, const struct ether_header *eh,
97 struct mbuf *m);
98 extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
99 struct sockaddr *dst, short *tp, int *hlen);
102 static void ef_reset (struct ifnet *);
104 static int ef_attach(struct efnet *sc);
105 static int ef_detach(struct efnet *sc);
106 static void ef_init(void *);
107 static int ef_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
108 static void ef_start(struct ifnet *);
109 static int ef_input(struct ifnet*, const struct ether_header *, struct mbuf *);
110 static int ef_output(struct ifnet *ifp, struct mbuf **mp,
111 struct sockaddr *dst, short *tp, int *hlen);
113 static int ef_load(void);
114 static int ef_unload(void);
117 * Install the interface, most of structure initialization done in ef_clone()
119 static int
120 ef_attach(struct efnet *sc)
122 struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
124 ifp->if_start = ef_start;
125 ifp->if_watchdog = NULL;
126 ifp->if_init = ef_init;
127 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
128 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
130 * Attach the interface
132 ether_ifattach(ifp, IF_LLADDR(sc->ef_ifp), NULL);
134 ifp->if_resolvemulti = 0;
135 ifp->if_type = IFT_XETHER;
136 ifp->if_flags |= IFF_RUNNING;
138 EFDEBUG("%s: attached\n", ifp->if_xname);
139 return 1;
143 * This is for _testing_only_, just removes interface from interfaces list
145 static int
146 ef_detach(struct efnet *sc)
148 ether_ifdetach(&sc->ef_ac.ac_if);
149 return 0;
152 static void
153 ef_init(void *foo __unused)
157 static int
158 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
160 struct efnet *sc = ifp->if_softc;
161 struct ifaddr *ifa = (struct ifaddr*)data;
162 int error;
164 EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
165 error = 0;
166 crit_enter();
167 switch (cmd) {
168 case SIOCSIFADDR:
169 if (sc->ef_frametype == ETHER_FT_8023 &&
170 ifa->ifa_addr->sa_family != AF_IPX) {
171 error = EAFNOSUPPORT;
172 break;
174 ifp->if_flags |= IFF_UP;
175 /* FALL THROUGH */
176 case SIOCGIFADDR:
177 case SIOCSIFMTU:
178 error = ether_ioctl(ifp, cmd, data);
179 break;
180 case SIOCSIFFLAGS:
181 error = 0;
182 break;
183 default:
184 error = EINVAL;
186 crit_exit();
187 return error;
191 * Currently packet prepared in the ether_output(), but this can be a better
192 * place.
194 static void
195 ef_start(struct ifnet *ifp)
197 struct efnet *sc = (struct efnet*)ifp->if_softc;
198 struct ifnet *p;
199 struct mbuf *m;
201 ifp->if_flags |= IFF_OACTIVE;
202 p = sc->ef_ifp;
204 EFDEBUG("\n");
205 for (;;) {
206 IF_DEQUEUE(&ifp->if_snd, m);
207 if (m == 0)
208 break;
209 BPF_MTAP(ifp, m);
210 if (IF_QFULL(&p->if_snd)) {
211 IF_DROP(&p->if_snd);
212 ifp->if_oerrors++;
213 m_freem(m);
214 continue;
216 IF_ENQUEUE(&p->if_snd, m);
217 if ((p->if_flags & IFF_OACTIVE) == 0) {
218 p->if_start(p);
219 ifp->if_opackets++;
222 ifp->if_flags &= ~IFF_OACTIVE;
223 return;
227 * Inline functions do not put additional overhead to procedure call or
228 * parameter passing but simplify the code
230 static int __inline
231 ef_inputEII(struct mbuf *m, struct llc* l, u_short ether_type)
233 int isr;
235 switch(ether_type) {
236 #ifdef IPX
237 case ETHERTYPE_IPX:
238 isr = NETISR_IPX;
239 break;
240 #endif
241 #ifdef INET
242 case ETHERTYPE_IP:
243 #ifdef notyet
244 if (ipflow_fastforward(m))
245 return (0);
246 #endif
247 isr = NETISR_IP;
248 break;
249 case ETHERTYPE_ARP:
250 isr = NETISR_ARP;
251 break;
252 #endif
253 default:
254 return (EPROTONOSUPPORT);
256 netisr_dispatch(isr, m);
257 return (0);
260 static int __inline
261 ef_inputSNAP(struct mbuf *m, struct llc* l, u_short ether_type)
263 int isr;
265 switch(ether_type) {
266 #ifdef IPX
267 case ETHERTYPE_IPX:
268 m_adj(m, 8);
269 isr = NETISR_IPX;
270 break;
271 #endif
272 default:
273 return (EPROTONOSUPPORT);
275 netisr_dispatch(isr, m);
276 return (0);
279 static int __inline
280 ef_input8022(struct mbuf *m, struct llc* l, u_short ether_type)
282 int isr;
284 switch(ether_type) {
285 #ifdef IPX
286 case 0xe0:
287 m_adj(m, 3);
288 isr = NETISR_IPX;
289 break;
290 #endif
291 default:
292 return (EPROTONOSUPPORT);
294 netisr_dispatch(isr, m);
295 return (0);
299 * Called from ether_input()
301 static int
302 ef_input(struct ifnet *ifp, const struct ether_header *eh, struct mbuf *m)
304 u_short ether_type;
305 int ft = -1;
306 struct efnet *efp;
307 struct ifnet *eifp;
308 struct llc *l;
309 struct ef_link *efl;
310 int isr;
312 ether_type = ntohs(eh->ether_type);
313 if (ether_type < ETHERMTU) {
314 l = mtod(m, struct llc*);
315 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
317 * Novell's "802.3" frame
319 ft = ETHER_FT_8023;
320 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
322 * 802.2/SNAP
324 ft = ETHER_FT_SNAP;
325 ether_type = ntohs(l->llc_un.type_snap.ether_type);
326 } else if (l->llc_dsap == l->llc_ssap) {
328 * 802.3/802.2
330 ft = ETHER_FT_8022;
331 ether_type = l->llc_ssap;
333 } else
334 ft = ETHER_FT_EII;
336 if (ft == -1) {
337 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
338 return EPROTONOSUPPORT;
342 * Check if interface configured for the given frame
344 efp = NULL;
345 SLIST_FOREACH(efl, &efdev, el_next) {
346 if (efl->el_ifp == ifp) {
347 efp = efl->el_units[ft];
348 break;
351 if (efp == NULL) {
352 EFDEBUG("Can't find if for %d\n", ft);
353 return EPROTONOSUPPORT;
355 eifp = &efp->ef_ac.ac_if;
356 if ((eifp->if_flags & IFF_UP) == 0)
357 return EPROTONOSUPPORT;
358 eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
359 m->m_pkthdr.rcvif = eifp;
361 if (eifp->if_bpf)
362 bpf_ptap(eifp->if_bpf, m, eh, ETHER_HDR_LEN);
365 * Now we ready to adjust mbufs and pass them to protocol intr's
367 switch(ft) {
368 case ETHER_FT_EII:
369 return (ef_inputEII(m, l, ether_type));
370 break;
371 #ifdef IPX
372 case ETHER_FT_8023: /* only IPX can be here */
373 isr = NETISR_IPX;
374 break;
375 #endif
376 case ETHER_FT_SNAP:
377 return (ef_inputSNAP(m, l, ether_type));
378 break;
379 case ETHER_FT_8022:
380 return (ef_input8022(m, l, ether_type));
381 break;
382 default:
383 EFDEBUG("No support for frame %d and proto %04x\n",
384 ft, ether_type);
385 return (EPROTONOSUPPORT);
387 netisr_dispatch(isr, m);
388 return (0);
391 static int
392 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
393 int *hlen)
395 struct efnet *sc = (struct efnet*)ifp->if_softc;
396 struct mbuf *m = *mp;
397 u_char *cp;
398 short type;
400 if (ifp->if_type != IFT_XETHER)
401 return ENETDOWN;
402 switch (sc->ef_frametype) {
403 case ETHER_FT_EII:
404 #ifdef IPX
405 type = htons(ETHERTYPE_IPX);
406 #else
407 return EPFNOSUPPORT;
408 #endif
409 break;
410 case ETHER_FT_8023:
411 type = htons(m->m_pkthdr.len);
412 break;
413 case ETHER_FT_8022:
414 M_PREPEND(m, ETHER_HDR_LEN + 3, MB_WAIT);
415 if (m == NULL) {
416 *mp = NULL;
417 return ENOBUFS;
420 * Ensure that ethernet header and next three bytes
421 * will fit into single mbuf
423 m = m_pullup(m, ETHER_HDR_LEN + 3);
424 if (m == NULL) {
425 *mp = NULL;
426 return ENOBUFS;
428 m_adj(m, ETHER_HDR_LEN);
429 type = htons(m->m_pkthdr.len);
430 cp = mtod(m, u_char *);
431 *cp++ = 0xE0;
432 *cp++ = 0xE0;
433 *cp++ = 0x03;
434 *hlen += 3;
435 break;
436 case ETHER_FT_SNAP:
437 M_PREPEND(m, 8, MB_WAIT);
438 if (m == NULL) {
439 *mp = NULL;
440 return ENOBUFS;
442 type = htons(m->m_pkthdr.len);
443 cp = mtod(m, u_char *);
444 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
445 *hlen += 8;
446 break;
447 default:
448 return EPFNOSUPPORT;
450 *mp = m;
451 *tp = type;
452 return 0;
456 * Create clone from the given interface
458 static int
459 ef_clone(struct ef_link *efl, int ft)
461 struct efnet *efp;
462 struct ifnet *eifp;
463 struct ifnet *ifp = efl->el_ifp;
465 efp = (struct efnet*)kmalloc(sizeof(struct efnet), M_IFADDR,
466 M_WAITOK | M_ZERO);
467 efp->ef_ifp = ifp;
468 efp->ef_frametype = ft;
469 eifp = &efp->ef_ac.ac_if;
470 ksnprintf(eifp->if_xname, IFNAMSIZ,
471 "%sf%d", ifp->if_xname, efp->ef_frametype);
472 eifp->if_dname = "ef";
473 eifp->if_dunit = IF_DUNIT_NONE;
474 eifp->if_softc = efp;
475 if (ifp->if_ioctl)
476 eifp->if_ioctl = ef_ioctl;
477 efl->el_units[ft] = efp;
478 return 0;
481 static int
482 ef_load(void)
484 struct ifnet *ifp;
485 struct efnet *efp;
486 struct ef_link *efl = NULL;
487 int error = 0, d;
489 TAILQ_FOREACH(ifp, &ifnet, if_link) {
490 if (ifp->if_type != IFT_ETHER) continue;
491 EFDEBUG("Found interface %s\n", ifp->if_xname);
492 efl = (struct ef_link*)kmalloc(sizeof(struct ef_link),
493 M_IFADDR, M_WAITOK | M_ZERO);
495 efl->el_ifp = ifp;
496 #ifdef ETHER_II
497 error = ef_clone(efl, ETHER_FT_EII);
498 if (error) break;
499 #endif
500 #ifdef ETHER_8023
501 error = ef_clone(efl, ETHER_FT_8023);
502 if (error) break;
503 #endif
504 #ifdef ETHER_8022
505 error = ef_clone(efl, ETHER_FT_8022);
506 if (error) break;
507 #endif
508 #ifdef ETHER_SNAP
509 error = ef_clone(efl, ETHER_FT_SNAP);
510 if (error) break;
511 #endif
512 efcount++;
513 SLIST_INSERT_HEAD(&efdev, efl, el_next);
515 if (error) {
516 if (efl)
517 SLIST_INSERT_HEAD(&efdev, efl, el_next);
518 SLIST_FOREACH(efl, &efdev, el_next) {
519 for (d = 0; d < EF_NFT; d++)
520 if (efl->el_units[d])
521 kfree(efl->el_units[d], M_IFADDR);
522 kfree(efl, M_IFADDR);
524 return error;
526 SLIST_FOREACH(efl, &efdev, el_next) {
527 for (d = 0; d < EF_NFT; d++) {
528 efp = efl->el_units[d];
529 if (efp)
530 ef_attach(efp);
533 ef_inputp = ef_input;
534 ef_outputp = ef_output;
535 EFDEBUG("Loaded\n");
536 return 0;
539 static int
540 ef_unload(void)
542 struct efnet *efp;
543 struct ef_link *efl;
544 int d;
546 ef_inputp = NULL;
547 ef_outputp = NULL;
548 SLIST_FOREACH(efl, &efdev, el_next) {
549 for (d = 0; d < EF_NFT; d++) {
550 efp = efl->el_units[d];
551 if (efp) {
552 ef_detach(efp);
556 EFDEBUG("Unloaded\n");
557 return 0;
560 static int
561 if_ef_modevent(module_t mod, int type, void *data)
563 switch ((modeventtype_t)type) {
564 case MOD_LOAD:
565 return ef_load();
566 case MOD_UNLOAD:
567 return ef_unload();
568 default:
569 break;
571 return 0;
574 static moduledata_t if_ef_mod = {
575 "if_ef", if_ef_modevent, NULL
578 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);