2 * Copyright (c) 1999, 2000 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sockio.h>
36 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/syslog.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <net/ethernet.h>
44 #include <net/if_llc.h>
46 #include <net/if_arp.h>
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/netisr.h>
54 #include <netinet/in.h>
55 #include <netinet/in_var.h>
56 #include <netinet/if_ether.h>
60 #include <netipx/ipx.h>
61 #include <netipx/ipx_if.h>
64 /* If none of the supported layers is enabled explicitly enable them all */
65 #if !defined(ETHER_II) && !defined(ETHER_8023) && !defined(ETHER_8022) && \
73 /* internal frame types */
74 #define ETHER_FT_EII 0 /* Ethernet_II - default */
75 #define ETHER_FT_8023 1 /* 802.3 (Novell) */
76 #define ETHER_FT_8022 2 /* 802.2 */
77 #define ETHER_FT_SNAP 3 /* SNAP */
78 #define EF_NFT 4 /* total number of frame types */
81 #define EFDEBUG(format, args...) printf("%s: "format, __func__ ,## args)
83 #define EFDEBUG(format, args...)
86 #define EFERROR(format, args...) printf("%s: "format, __func__ ,## args)
90 struct ifnet *ef_pifp;
95 SLIST_ENTRY(ef_link) el_next;
96 struct ifnet *el_ifp; /* raw device for this clones */
97 struct efnet *el_units[EF_NFT]; /* our clones */
100 static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
103 extern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
104 extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
105 struct sockaddr *dst, short *tp, int *hlen);
108 static void ef_reset (struct ifnet *);
110 static int ef_attach(struct efnet *sc);
111 static int ef_detach(struct efnet *sc);
112 static void ef_init(void *);
113 static int ef_ioctl(struct ifnet *, u_long, caddr_t);
114 static void ef_start(struct ifnet *);
115 static int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
116 static int ef_output(struct ifnet *ifp, struct mbuf **mp,
117 struct sockaddr *dst, short *tp, int *hlen);
119 static int ef_load(void);
120 static int ef_unload(void);
123 * Install the interface, most of structure initialization done in ef_clone()
126 ef_attach(struct efnet *sc)
128 struct ifnet *ifp = sc->ef_ifp;
130 ifp->if_start = ef_start;
131 ifp->if_init = ef_init;
132 ifp->if_snd.ifq_maxlen = ifqmaxlen;
133 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
135 * Attach the interface
137 ether_ifattach(ifp, IF_LLADDR(sc->ef_pifp));
139 ifp->if_resolvemulti = 0;
140 ifp->if_type = IFT_XETHER;
141 ifp->if_drv_flags |= IFF_DRV_RUNNING;
143 EFDEBUG("%s: attached\n", ifp->if_xname);
148 * This is for _testing_only_, just removes interface from interfaces list
151 ef_detach(struct efnet *sc)
153 struct ifnet *ifp = sc->ef_ifp;
171 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
173 struct efnet *sc = ifp->if_softc;
174 struct ifaddr *ifa = (struct ifaddr*)data;
177 EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
185 if (sc->ef_frametype == ETHER_FT_8023 &&
186 ifa->ifa_addr->sa_family != AF_IPX) {
187 error = EAFNOSUPPORT;
190 ifp->if_flags |= IFF_UP;
193 error = ether_ioctl(ifp, cmd, data);
201 * Currently packet prepared in the ether_output(), but this can be a better
205 ef_start(struct ifnet *ifp)
207 struct efnet *sc = (struct efnet*)ifp->if_softc;
212 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
217 IF_DEQUEUE(&ifp->if_snd, m);
221 error = p->if_transmit(p, m);
228 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
233 * Inline functions do not put additional overhead to procedure call or
234 * parameter passing but simplify the code
237 ef_inputEII(struct mbuf *m, struct ether_header *eh, u_short ether_type)
249 if ((m = ip_fastforward(m)) == NULL)
259 return (EPROTONOSUPPORT);
261 netisr_dispatch(isr, m);
266 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
279 return (EPROTONOSUPPORT);
281 netisr_dispatch(isr, m);
286 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
299 return (EPROTONOSUPPORT);
301 netisr_dispatch(isr, m);
306 * Called from ether_input()
309 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
319 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) {
325 * Novell's "802.3" frame
328 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
333 ether_type = ntohs(l->llc_un.type_snap.ether_type);
334 } else if (l->llc_dsap == l->llc_ssap) {
339 ether_type = l->llc_ssap;
345 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
346 return EPROTONOSUPPORT;
350 * Check if interface configured for the given frame
353 SLIST_FOREACH(efl, &efdev, el_next) {
354 if (efl->el_ifp == ifp) {
355 efp = efl->el_units[ft];
360 EFDEBUG("Can't find if for %d\n", ft);
361 return EPROTONOSUPPORT;
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;
369 BPF_MTAP2(eifp, eh, ETHER_HDR_LEN, m);
371 * Now we ready to adjust mbufs and pass them to protocol intr's
375 return (ef_inputEII(m, eh, ether_type));
377 case ETHER_FT_8023: /* only IPX can be here */
382 return (ef_inputSNAP(m, eh, l, ether_type));
384 return (ef_input8022(m, eh, l, ether_type));
386 EFDEBUG("No support for frame %d and proto %04x\n",
388 return (EPROTONOSUPPORT);
390 netisr_dispatch(isr, m);
395 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
398 struct efnet *sc = (struct efnet*)ifp->if_softc;
399 struct mbuf *m = *mp;
403 if (ifp->if_type != IFT_XETHER)
405 switch (sc->ef_frametype) {
408 type = htons(ETHERTYPE_IPX);
414 type = htons(m->m_pkthdr.len);
417 M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAIT);
419 * Ensure that ethernet header and next three bytes
420 * will fit into single mbuf
422 m = m_pullup(m, ETHER_HDR_LEN + 3);
427 m_adj(m, ETHER_HDR_LEN);
428 type = htons(m->m_pkthdr.len);
429 cp = mtod(m, u_char *);
436 M_PREPEND(m, 8, M_WAIT);
437 type = htons(m->m_pkthdr.len);
438 cp = mtod(m, u_char *);
439 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
451 * Create clone from the given interface
454 ef_clone(struct ef_link *efl, int ft)
458 struct ifnet *ifp = efl->el_ifp;
460 efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
465 efp->ef_frametype = ft;
466 eifp = efp->ef_ifp = if_alloc(IFT_ETHER);
471 snprintf(eifp->if_xname, IFNAMSIZ,
472 "%sf%d", ifp->if_xname, efp->ef_frametype);
473 eifp->if_dname = "ef";
474 eifp->if_dunit = IF_DUNIT_NONE;
475 eifp->if_softc = efp;
477 eifp->if_ioctl = ef_ioctl;
478 efl->el_units[ft] = efp;
485 VNET_ITERATOR_DECL(vnet_iter);
488 struct ef_link *efl = NULL, *efl_temp;
492 VNET_FOREACH(vnet_iter) {
493 CURVNET_SET(vnet_iter);
496 * XXXRW: The following loop walks the ifnet list while
497 * modifying it, something not well-supported by ifnet
498 * locking. To avoid lock upgrade/recursion issues, manually
499 * acquire a write lock of ifnet_sxlock here, rather than a
500 * read lock, so that when if_alloc() recurses the lock, we
501 * don't panic. This structure, in which if_ef automatically
502 * attaches to all ethernet interfaces, should be replaced
503 * with a model like that found in if_vlan, in which
504 * interfaces are explicitly configured, which would avoid
505 * this (and other) problems.
507 sx_xlock(&ifnet_sxlock);
508 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
509 if (ifp->if_type != IFT_ETHER) continue;
510 EFDEBUG("Found interface %s\n", ifp->if_xname);
511 efl = (struct ef_link*)malloc(sizeof(struct ef_link),
512 M_IFADDR, M_WAITOK | M_ZERO);
520 error = ef_clone(efl, ETHER_FT_EII);
524 error = ef_clone(efl, ETHER_FT_8023);
528 error = ef_clone(efl, ETHER_FT_8022);
532 error = ef_clone(efl, ETHER_FT_SNAP);
536 SLIST_INSERT_HEAD(&efdev, efl, el_next);
538 sx_xunlock(&ifnet_sxlock);
544 SLIST_INSERT_HEAD(&efdev, efl, el_next);
545 SLIST_FOREACH_SAFE(efl, &efdev, el_next, efl_temp) {
546 for (d = 0; d < EF_NFT; d++)
547 if (efl->el_units[d]) {
548 if (efl->el_units[d]->ef_pifp != NULL)
549 if_free(efl->el_units[d]->ef_pifp);
550 free(efl->el_units[d], M_IFADDR);
556 SLIST_FOREACH(efl, &efdev, el_next) {
557 for (d = 0; d < EF_NFT; d++) {
558 efp = efl->el_units[d];
563 ef_inputp = ef_input;
564 ef_outputp = ef_output;
578 SLIST_FOREACH(efl, &efdev, el_next) {
579 for (d = 0; d < EF_NFT; d++) {
580 efp = efl->el_units[d];
586 EFDEBUG("Unloaded\n");
591 if_ef_modevent(module_t mod, int type, void *data)
593 switch ((modeventtype_t)type) {
604 static moduledata_t if_ef_mod = {
605 "if_ef", if_ef_modevent, NULL
608 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);