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 const 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 const 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;
167 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
169 struct efnet *sc = ifp->if_softc;
170 struct ifaddr *ifa = (struct ifaddr*)data;
173 EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
180 if (sc->ef_frametype == ETHER_FT_8023 &&
181 ifa->ifa_addr->sa_family != AF_IPX) {
182 error = EAFNOSUPPORT;
185 ifp->if_flags |= IFF_UP;
188 error = ether_ioctl(ifp, cmd, data);
195 * Currently packet prepared in the ether_output(), but this can be a better
199 ef_start(struct ifnet *ifp)
201 struct efnet *sc = (struct efnet*)ifp->if_softc;
206 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
211 IF_DEQUEUE(&ifp->if_snd, m);
215 error = p->if_transmit(p, m);
222 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
227 * Inline functions do not put additional overhead to procedure call or
228 * parameter passing but simplify the code
231 ef_inputEII(struct mbuf *m, struct ether_header *eh, u_short ether_type)
243 if ((m = ip_fastforward(m)) == NULL)
253 return (EPROTONOSUPPORT);
255 netisr_dispatch(isr, m);
260 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
273 return (EPROTONOSUPPORT);
275 netisr_dispatch(isr, m);
280 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
293 return (EPROTONOSUPPORT);
295 netisr_dispatch(isr, m);
300 * Called from ether_input()
303 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
313 ether_type = ntohs(eh->ether_type);
315 if (ether_type < ETHERMTU) {
316 l = mtod(m, struct llc*);
317 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
319 * Novell's "802.3" frame
322 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
327 ether_type = ntohs(l->llc_un.type_snap.ether_type);
328 } else if (l->llc_dsap == l->llc_ssap) {
333 ether_type = l->llc_ssap;
339 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
340 return EPROTONOSUPPORT;
344 * Check if interface configured for the given frame
347 SLIST_FOREACH(efl, &efdev, el_next) {
348 if (efl->el_ifp == ifp) {
349 efp = efl->el_units[ft];
354 EFDEBUG("Can't find if for %d\n", ft);
355 return EPROTONOSUPPORT;
358 if ((eifp->if_flags & IFF_UP) == 0)
359 return EPROTONOSUPPORT;
360 eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
361 m->m_pkthdr.rcvif = eifp;
363 BPF_MTAP2(eifp, eh, ETHER_HDR_LEN, m);
365 * Now we ready to adjust mbufs and pass them to protocol intr's
369 return (ef_inputEII(m, eh, ether_type));
371 case ETHER_FT_8023: /* only IPX can be here */
376 return (ef_inputSNAP(m, eh, l, ether_type));
378 return (ef_input8022(m, eh, l, ether_type));
380 EFDEBUG("No support for frame %d and proto %04x\n",
382 return (EPROTONOSUPPORT);
384 netisr_dispatch(isr, m);
389 ef_output(struct ifnet *ifp, struct mbuf **mp, const struct sockaddr *dst,
390 short *tp, int *hlen)
392 struct efnet *sc = (struct efnet*)ifp->if_softc;
393 struct mbuf *m = *mp;
397 if (ifp->if_type != IFT_XETHER)
399 switch (sc->ef_frametype) {
402 type = htons(ETHERTYPE_IPX);
408 type = htons(m->m_pkthdr.len);
411 M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAITOK);
413 * Ensure that ethernet header and next three bytes
414 * will fit into single mbuf
416 m = m_pullup(m, ETHER_HDR_LEN + 3);
421 m_adj(m, ETHER_HDR_LEN);
422 type = htons(m->m_pkthdr.len);
423 cp = mtod(m, u_char *);
430 M_PREPEND(m, 8, M_WAITOK);
431 type = htons(m->m_pkthdr.len);
432 cp = mtod(m, u_char *);
433 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
445 * Create clone from the given interface
448 ef_clone(struct ef_link *efl, int ft)
452 struct ifnet *ifp = efl->el_ifp;
454 efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
459 efp->ef_frametype = ft;
460 eifp = efp->ef_ifp = if_alloc(IFT_ETHER);
465 snprintf(eifp->if_xname, IFNAMSIZ,
466 "%sf%d", ifp->if_xname, efp->ef_frametype);
467 eifp->if_dname = "ef";
468 eifp->if_dunit = IF_DUNIT_NONE;
469 eifp->if_softc = efp;
471 eifp->if_ioctl = ef_ioctl;
472 efl->el_units[ft] = efp;
479 VNET_ITERATOR_DECL(vnet_iter);
482 struct ef_link *efl = NULL, *efl_temp;
486 VNET_FOREACH(vnet_iter) {
487 CURVNET_SET(vnet_iter);
490 * XXXRW: The following loop walks the ifnet list while
491 * modifying it, something not well-supported by ifnet
492 * locking. To avoid lock upgrade/recursion issues, manually
493 * acquire a write lock of ifnet_sxlock here, rather than a
494 * read lock, so that when if_alloc() recurses the lock, we
495 * don't panic. This structure, in which if_ef automatically
496 * attaches to all ethernet interfaces, should be replaced
497 * with a model like that found in if_vlan, in which
498 * interfaces are explicitly configured, which would avoid
499 * this (and other) problems.
501 sx_xlock(&ifnet_sxlock);
502 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
503 if (ifp->if_type != IFT_ETHER) continue;
504 EFDEBUG("Found interface %s\n", ifp->if_xname);
505 efl = (struct ef_link*)malloc(sizeof(struct ef_link),
506 M_IFADDR, M_WAITOK | M_ZERO);
514 error = ef_clone(efl, ETHER_FT_EII);
518 error = ef_clone(efl, ETHER_FT_8023);
522 error = ef_clone(efl, ETHER_FT_8022);
526 error = ef_clone(efl, ETHER_FT_SNAP);
530 SLIST_INSERT_HEAD(&efdev, efl, el_next);
532 sx_xunlock(&ifnet_sxlock);
538 SLIST_INSERT_HEAD(&efdev, efl, el_next);
539 SLIST_FOREACH_SAFE(efl, &efdev, el_next, efl_temp) {
540 for (d = 0; d < EF_NFT; d++)
541 if (efl->el_units[d]) {
542 if (efl->el_units[d]->ef_pifp != NULL)
543 if_free(efl->el_units[d]->ef_pifp);
544 free(efl->el_units[d], M_IFADDR);
550 SLIST_FOREACH(efl, &efdev, el_next) {
551 for (d = 0; d < EF_NFT; d++) {
552 efp = efl->el_units[d];
557 ef_inputp = ef_input;
558 ef_outputp = ef_output;
572 SLIST_FOREACH(efl, &efdev, el_next) {
573 for (d = 0; d < EF_NFT; d++) {
574 efp = efl->el_units[d];
580 EFDEBUG("Unloaded\n");
585 if_ef_modevent(module_t mod, int type, void *data)
587 switch ((modeventtype_t)type) {
598 static moduledata_t if_ef_mod = {
599 "if_ef", if_ef_modevent, NULL
602 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);