if_ef.c revision 196482
154558Sbp/*- 266479Sbp * Copyright (c) 1999, 2000 Boris Popov 354558Sbp * All rights reserved. 454558Sbp * 554558Sbp * Redistribution and use in source and binary forms, with or without 654558Sbp * modification, are permitted provided that the following conditions 754558Sbp * are met: 854558Sbp * 1. Redistributions of source code must retain the above copyright 954558Sbp * notice, this list of conditions and the following disclaimer. 1054558Sbp * 2. Redistributions in binary form must reproduce the above copyright 1154558Sbp * notice, this list of conditions and the following disclaimer in the 1254558Sbp * documentation and/or other materials provided with the distribution. 1354558Sbp * 1454558Sbp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1554558Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1654558Sbp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1754558Sbp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1854558Sbp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1954558Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2054558Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2154558Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2254558Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2354558Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2454558Sbp * SUCH DAMAGE. 2554558Sbp * 2654558Sbp * $FreeBSD: head/sys/net/if_ef.c 196482 2009-08-23 21:00:21Z rwatson $ 2754558Sbp */ 2854558Sbp 2954558Sbp#include "opt_inet.h" 3054558Sbp#include "opt_ipx.h" 3156424Sbp#include "opt_ef.h" 3254558Sbp 3354558Sbp#include <sys/param.h> 3454558Sbp#include <sys/systm.h> 3554558Sbp#include <sys/sockio.h> 3654558Sbp#include <sys/malloc.h> 3754558Sbp#include <sys/mbuf.h> 3854558Sbp#include <sys/socket.h> 3954558Sbp#include <sys/syslog.h> 4054558Sbp#include <sys/kernel.h> 4154558Sbp#include <sys/module.h> 4254558Sbp 4354558Sbp#include <net/ethernet.h> 4454558Sbp#include <net/if_llc.h> 4554558Sbp#include <net/if.h> 4654558Sbp#include <net/if_arp.h> 4754558Sbp#include <net/if_dl.h> 4854558Sbp#include <net/if_types.h> 4954558Sbp#include <net/netisr.h> 5054558Sbp#include <net/bpf.h> 51185571Sbz#include <net/vnet.h> 5254558Sbp 5354558Sbp#ifdef INET 5454558Sbp#include <netinet/in.h> 5554558Sbp#include <netinet/in_var.h> 5654558Sbp#include <netinet/if_ether.h> 5754558Sbp#endif 5854558Sbp 5954558Sbp#ifdef IPX 6054558Sbp#include <netipx/ipx.h> 6154558Sbp#include <netipx/ipx_if.h> 6254558Sbp#endif 6354558Sbp 64143196Ssobomax/* If none of the supported layers is enabled explicitly enable them all */ 65143196Ssobomax#if !defined(ETHER_II) && !defined(ETHER_8023) && !defined(ETHER_8022) && \ 66143196Ssobomax !defined(ETHER_SNAP) 67143196Ssobomax#define ETHER_II 1 68143196Ssobomax#define ETHER_8023 1 69143196Ssobomax#define ETHER_8022 1 70143196Ssobomax#define ETHER_SNAP 1 71143196Ssobomax#endif 72143196Ssobomax 7354558Sbp/* internal frame types */ 7454558Sbp#define ETHER_FT_EII 0 /* Ethernet_II - default */ 7554558Sbp#define ETHER_FT_8023 1 /* 802.3 (Novell) */ 7654558Sbp#define ETHER_FT_8022 2 /* 802.2 */ 7754558Sbp#define ETHER_FT_SNAP 3 /* SNAP */ 7854558Sbp#define EF_NFT 4 /* total number of frame types */ 7954558Sbp 8054558Sbp#ifdef EF_DEBUG 8187599Sobrien#define EFDEBUG(format, args...) printf("%s: "format, __func__ ,## args) 8254558Sbp#else 8354558Sbp#define EFDEBUG(format, args...) 8454558Sbp#endif 8554558Sbp 8687599Sobrien#define EFERROR(format, args...) printf("%s: "format, __func__ ,## args) 8754558Sbp 8854558Sbpstruct efnet { 89147256Sbrooks struct ifnet *ef_ifp; 90147256Sbrooks struct ifnet *ef_pifp; 91121816Sbrooks int ef_frametype; 9254558Sbp}; 9354558Sbp 9454558Sbpstruct ef_link { 9560938Sjake SLIST_ENTRY(ef_link) el_next; 9654558Sbp struct ifnet *el_ifp; /* raw device for this clones */ 9754558Sbp struct efnet *el_units[EF_NFT]; /* our clones */ 9854558Sbp}; 9954558Sbp 10060938Sjakestatic SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL}; 10154558Sbpstatic int efcount; 10254558Sbp 10354558Sbpextern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m); 10459681Sbpextern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, 10566479Sbp struct sockaddr *dst, short *tp, int *hlen); 10654558Sbp 10754558Sbp/* 10854558Sbpstatic void ef_reset (struct ifnet *); 10954558Sbp*/ 11054558Sbpstatic int ef_attach(struct efnet *sc); 11154558Sbpstatic int ef_detach(struct efnet *sc); 11254558Sbpstatic void ef_init(void *); 11354558Sbpstatic int ef_ioctl(struct ifnet *, u_long, caddr_t); 11454558Sbpstatic void ef_start(struct ifnet *); 11554558Sbpstatic int ef_input(struct ifnet*, struct ether_header *, struct mbuf *); 11659681Sbpstatic int ef_output(struct ifnet *ifp, struct mbuf **mp, 11766479Sbp struct sockaddr *dst, short *tp, int *hlen); 11854558Sbp 11954558Sbpstatic int ef_load(void); 12054558Sbpstatic int ef_unload(void); 12154558Sbp 12254558Sbp/* 12354558Sbp * Install the interface, most of structure initialization done in ef_clone() 12454558Sbp */ 12554558Sbpstatic int 12654558Sbpef_attach(struct efnet *sc) 12754558Sbp{ 128147256Sbrooks struct ifnet *ifp = sc->ef_ifp; 12954558Sbp 13054558Sbp ifp->if_start = ef_start; 13154558Sbp ifp->if_watchdog = NULL; 13254558Sbp ifp->if_init = ef_init; 13354558Sbp ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 13454558Sbp ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 13554558Sbp /* 13654558Sbp * Attach the interface 13754558Sbp */ 138152315Sru ether_ifattach(ifp, IF_LLADDR(sc->ef_pifp)); 13954558Sbp 14054558Sbp ifp->if_resolvemulti = 0; 14154558Sbp ifp->if_type = IFT_XETHER; 142148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 14354558Sbp 144121816Sbrooks EFDEBUG("%s: attached\n", ifp->if_xname); 14554558Sbp return 1; 14654558Sbp} 14754558Sbp 14854558Sbp/* 14954558Sbp * This is for _testing_only_, just removes interface from interfaces list 15054558Sbp */ 15154558Sbpstatic int 15254558Sbpef_detach(struct efnet *sc) 15354558Sbp{ 154147256Sbrooks struct ifnet *ifp = sc->ef_ifp; 15554558Sbp int s; 15654558Sbp 15754558Sbp s = splimp(); 15854558Sbp 159147256Sbrooks ether_ifdetach(ifp); 160147256Sbrooks if_free(ifp); 161147256Sbrooks 16254558Sbp splx(s); 16354558Sbp return 0; 16454558Sbp} 16554558Sbp 16654558Sbpstatic void 16754558Sbpef_init(void *foo) { 16854558Sbp return; 16954558Sbp} 17054558Sbp 17154558Sbpstatic int 17254558Sbpef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 17354558Sbp{ 174121816Sbrooks struct efnet *sc = ifp->if_softc; 17554558Sbp struct ifaddr *ifa = (struct ifaddr*)data; 17654558Sbp int s, error; 17754558Sbp 178121816Sbrooks EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname); 17954558Sbp error = 0; 18054558Sbp s = splimp(); 18154558Sbp switch (cmd) { 182106939Ssam case SIOCSIFFLAGS: 183106939Ssam error = 0; 184106939Ssam break; 18554558Sbp case SIOCSIFADDR: 186121816Sbrooks if (sc->ef_frametype == ETHER_FT_8023 && 18754558Sbp ifa->ifa_addr->sa_family != AF_IPX) { 18854558Sbp error = EAFNOSUPPORT; 18954558Sbp break; 19054558Sbp } 19154558Sbp ifp->if_flags |= IFF_UP; 19254558Sbp /* FALL THROUGH */ 193106939Ssam default: 19454558Sbp error = ether_ioctl(ifp, cmd, data); 19554558Sbp break; 19654558Sbp } 19754558Sbp splx(s); 19854558Sbp return error; 19954558Sbp} 20054558Sbp 20154558Sbp/* 20254558Sbp * Currently packet prepared in the ether_output(), but this can be a better 20354558Sbp * place. 20454558Sbp */ 20554558Sbpstatic void 20654558Sbpef_start(struct ifnet *ifp) 20754558Sbp{ 20854558Sbp struct efnet *sc = (struct efnet*)ifp->if_softc; 20954558Sbp struct ifnet *p; 21054558Sbp struct mbuf *m; 211130549Smlaier int error; 21254558Sbp 213148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 214147256Sbrooks p = sc->ef_pifp; 21554558Sbp 21654558Sbp EFDEBUG("\n"); 21754558Sbp for (;;) { 21854558Sbp IF_DEQUEUE(&ifp->if_snd, m); 21954558Sbp if (m == 0) 22054558Sbp break; 221106939Ssam BPF_MTAP(ifp, m); 222191607Skmacy error = p->if_transmit(p, m); 223130549Smlaier if (error) { 22459681Sbp ifp->if_oerrors++; 22559681Sbp continue; 22654558Sbp } 22769152Sjlemon ifp->if_opackets++; 22854558Sbp } 229148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 23054558Sbp return; 23154558Sbp} 23254558Sbp 23354558Sbp/* 23454558Sbp * Inline functions do not put additional overhead to procedure call or 23554558Sbp * parameter passing but simplify the code 23654558Sbp */ 23754558Sbpstatic int __inline 238111888Sjlemonef_inputEII(struct mbuf *m, struct ether_header *eh, u_short ether_type) 23954558Sbp{ 240111888Sjlemon int isr; 241111888Sjlemon 24254558Sbp switch(ether_type) { 24354558Sbp#ifdef IPX 244111888Sjlemon case ETHERTYPE_IPX: 245111888Sjlemon isr = NETISR_IPX; 24654558Sbp break; 24754558Sbp#endif 24854558Sbp#ifdef INET 249111888Sjlemon case ETHERTYPE_IP: 250154518Sandre if ((m = ip_fastforward(m)) == NULL) 251111888Sjlemon return (0); 252111888Sjlemon isr = NETISR_IP; 25354558Sbp break; 25454558Sbp 255111888Sjlemon case ETHERTYPE_ARP: 256111888Sjlemon isr = NETISR_ARP; 25754558Sbp break; 25854558Sbp#endif 259111888Sjlemon default: 260111888Sjlemon return (EPROTONOSUPPORT); 26154558Sbp } 262111888Sjlemon netisr_dispatch(isr, m); 263111888Sjlemon return (0); 26454558Sbp} 26554558Sbp 26654558Sbpstatic int __inline 26754558Sbpef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l, 268111888Sjlemon u_short ether_type) 26954558Sbp{ 270111888Sjlemon int isr; 271111888Sjlemon 27254558Sbp switch(ether_type) { 27354558Sbp#ifdef IPX 274111888Sjlemon case ETHERTYPE_IPX: 27554558Sbp m_adj(m, 8); 276111888Sjlemon isr = NETISR_IPX; 27754558Sbp break; 27854558Sbp#endif 279111888Sjlemon default: 280111888Sjlemon return (EPROTONOSUPPORT); 28154558Sbp } 282111888Sjlemon netisr_dispatch(isr, m); 283111888Sjlemon return (0); 28454558Sbp} 28554558Sbp 28654558Sbpstatic int __inline 28754558Sbpef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l, 288111888Sjlemon u_short ether_type) 28954558Sbp{ 290111888Sjlemon int isr; 291111888Sjlemon 29254558Sbp switch(ether_type) { 29354558Sbp#ifdef IPX 294111888Sjlemon case 0xe0: 29554558Sbp m_adj(m, 3); 296111888Sjlemon isr = NETISR_IPX; 29754558Sbp break; 29854558Sbp#endif 299111888Sjlemon default: 300111888Sjlemon return (EPROTONOSUPPORT); 30154558Sbp } 302111888Sjlemon netisr_dispatch(isr, m); 303111888Sjlemon return (0); 30454558Sbp} 305111888Sjlemon 30654558Sbp/* 30754558Sbp * Called from ether_input() 30854558Sbp */ 30954558Sbpstatic int 31054558Sbpef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m) 31154558Sbp{ 31254558Sbp u_short ether_type; 31371891Sbp int ft = -1; 31454558Sbp struct efnet *efp; 31554558Sbp struct ifnet *eifp; 31654558Sbp struct llc *l; 31754558Sbp struct ef_link *efl; 318111888Sjlemon int isr; 31954558Sbp 32054558Sbp ether_type = ntohs(eh->ether_type); 321132778Skan l = NULL; 32254558Sbp if (ether_type < ETHERMTU) { 32354558Sbp l = mtod(m, struct llc*); 32454558Sbp if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) { 32554558Sbp /* 32654558Sbp * Novell's "802.3" frame 32754558Sbp */ 32854558Sbp ft = ETHER_FT_8023; 32954558Sbp } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) { 33054558Sbp /* 33154558Sbp * 802.2/SNAP 33254558Sbp */ 33354558Sbp ft = ETHER_FT_SNAP; 33454558Sbp ether_type = ntohs(l->llc_un.type_snap.ether_type); 33554558Sbp } else if (l->llc_dsap == l->llc_ssap) { 33654558Sbp /* 33754558Sbp * 802.3/802.2 33854558Sbp */ 33954558Sbp ft = ETHER_FT_8022; 34054558Sbp ether_type = l->llc_ssap; 34154558Sbp } 34254558Sbp } else 34354558Sbp ft = ETHER_FT_EII; 34454558Sbp 34554558Sbp if (ft == -1) { 34654558Sbp EFDEBUG("Unrecognised ether_type %x\n", ether_type); 34759681Sbp return EPROTONOSUPPORT; 34854558Sbp } 34954558Sbp 35054558Sbp /* 35154558Sbp * Check if interface configured for the given frame 35254558Sbp */ 35354558Sbp efp = NULL; 35454558Sbp SLIST_FOREACH(efl, &efdev, el_next) { 35554558Sbp if (efl->el_ifp == ifp) { 35654558Sbp efp = efl->el_units[ft]; 35754558Sbp break; 35854558Sbp } 35954558Sbp } 36054558Sbp if (efp == NULL) { 36154558Sbp EFDEBUG("Can't find if for %d\n", ft); 36259681Sbp return EPROTONOSUPPORT; 36354558Sbp } 364147256Sbrooks eifp = efp->ef_ifp; 36554558Sbp if ((eifp->if_flags & IFF_UP) == 0) 36659681Sbp return EPROTONOSUPPORT; 36754558Sbp eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 36854558Sbp m->m_pkthdr.rcvif = eifp; 36954558Sbp 370123922Ssam BPF_MTAP2(eifp, eh, ETHER_HDR_LEN, m); 37154558Sbp /* 37254558Sbp * Now we ready to adjust mbufs and pass them to protocol intr's 37354558Sbp */ 37454558Sbp switch(ft) { 375111888Sjlemon case ETHER_FT_EII: 376111888Sjlemon return (ef_inputEII(m, eh, ether_type)); 37754558Sbp#ifdef IPX 378111888Sjlemon case ETHER_FT_8023: /* only IPX can be here */ 379111888Sjlemon isr = NETISR_IPX; 38054558Sbp break; 38154558Sbp#endif 382111888Sjlemon case ETHER_FT_SNAP: 383111888Sjlemon return (ef_inputSNAP(m, eh, l, ether_type)); 384111888Sjlemon case ETHER_FT_8022: 385111888Sjlemon return (ef_input8022(m, eh, l, ether_type)); 386111888Sjlemon default: 38754558Sbp EFDEBUG("No support for frame %d and proto %04x\n", 38854558Sbp ft, ether_type); 389111888Sjlemon return (EPROTONOSUPPORT); 39054558Sbp } 391111888Sjlemon netisr_dispatch(isr, m); 392111888Sjlemon return (0); 39354558Sbp} 39454558Sbp 39554558Sbpstatic int 39666479Sbpef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp, 39766479Sbp int *hlen) 39854558Sbp{ 399121816Sbrooks struct efnet *sc = (struct efnet*)ifp->if_softc; 40059681Sbp struct mbuf *m = *mp; 40154558Sbp u_char *cp; 40254558Sbp short type; 40354558Sbp 40454558Sbp if (ifp->if_type != IFT_XETHER) 40559681Sbp return ENETDOWN; 406121816Sbrooks switch (sc->ef_frametype) { 40754558Sbp case ETHER_FT_EII: 40854558Sbp#ifdef IPX 40954558Sbp type = htons(ETHERTYPE_IPX); 41054558Sbp#else 41159681Sbp return EPFNOSUPPORT; 41254558Sbp#endif 41354558Sbp break; 41454558Sbp case ETHER_FT_8023: 41554558Sbp type = htons(m->m_pkthdr.len); 41654558Sbp break; 41754558Sbp case ETHER_FT_8022: 418177599Sru M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAIT); 41959681Sbp /* 42059681Sbp * Ensure that ethernet header and next three bytes 42159681Sbp * will fit into single mbuf 42259681Sbp */ 42359681Sbp m = m_pullup(m, ETHER_HDR_LEN + 3); 42459681Sbp if (m == NULL) { 42559681Sbp *mp = NULL; 42659681Sbp return ENOBUFS; 42759681Sbp } 42859681Sbp m_adj(m, ETHER_HDR_LEN); 42954558Sbp type = htons(m->m_pkthdr.len); 43054558Sbp cp = mtod(m, u_char *); 43154558Sbp *cp++ = 0xE0; 43254558Sbp *cp++ = 0xE0; 43354558Sbp *cp++ = 0x03; 43466479Sbp *hlen += 3; 43554558Sbp break; 43654558Sbp case ETHER_FT_SNAP: 437177599Sru M_PREPEND(m, 8, M_WAIT); 43854558Sbp type = htons(m->m_pkthdr.len); 43954558Sbp cp = mtod(m, u_char *); 44054558Sbp bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8); 44166479Sbp *hlen += 8; 44254558Sbp break; 44354558Sbp default: 44459681Sbp return EPFNOSUPPORT; 44554558Sbp } 44659681Sbp *mp = m; 44754558Sbp *tp = type; 44854558Sbp return 0; 44954558Sbp} 45054558Sbp 45154558Sbp/* 45254558Sbp * Create clone from the given interface 45354558Sbp */ 45454558Sbpstatic int 45554558Sbpef_clone(struct ef_link *efl, int ft) 45654558Sbp{ 45754558Sbp struct efnet *efp; 45854558Sbp struct ifnet *eifp; 45954558Sbp struct ifnet *ifp = efl->el_ifp; 46054558Sbp 46169781Sdwmalone efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR, 462111119Simp M_WAITOK | M_ZERO); 46354558Sbp if (efp == NULL) 46454558Sbp return ENOMEM; 465147256Sbrooks efp->ef_pifp = ifp; 466121816Sbrooks efp->ef_frametype = ft; 467147256Sbrooks eifp = efp->ef_ifp = if_alloc(IFT_ETHER); 468154318Srwatson if (eifp == NULL) { 469154318Srwatson free(efp, M_IFADDR); 470147256Sbrooks return (ENOSPC); 471154318Srwatson } 472121816Sbrooks snprintf(eifp->if_xname, IFNAMSIZ, 473121816Sbrooks "%sf%d", ifp->if_xname, efp->ef_frametype); 474121816Sbrooks eifp->if_dname = "ef"; 475121816Sbrooks eifp->if_dunit = IF_DUNIT_NONE; 47654558Sbp eifp->if_softc = efp; 47754558Sbp if (ifp->if_ioctl) 47854558Sbp eifp->if_ioctl = ef_ioctl; 47954558Sbp efl->el_units[ft] = efp; 48054558Sbp return 0; 48154558Sbp} 48254558Sbp 48354558Sbpstatic int 48454558Sbpef_load(void) 48554558Sbp{ 486183550Szec VNET_ITERATOR_DECL(vnet_iter); 48754558Sbp struct ifnet *ifp; 48854558Sbp struct efnet *efp; 489154317Srwatson struct ef_link *efl = NULL, *efl_temp; 49054558Sbp int error = 0, d; 49154558Sbp 492183550Szec VNET_LIST_RLOCK(); 493183550Szec VNET_FOREACH(vnet_iter) { 494183550Szec CURVNET_SET(vnet_iter); 495196482Srwatson 496196482Srwatson /* 497196482Srwatson * XXXRW: The following loop walks the ifnet list while 498196482Srwatson * modifying it, something not well-supported by ifnet 499196482Srwatson * locking. To avoid lock upgrade/recursion issues, manually 500196482Srwatson * acquire a write lock of ifnet_sxlock here, rather than a 501196482Srwatson * read lock, so that when if_alloc() recurses the lock, we 502196482Srwatson * don't panic. This structure, in which if_ef automatically 503196482Srwatson * attaches to all ethernet interfaces, should be replaced 504196482Srwatson * with a model like that found in if_vlan, in which 505196482Srwatson * interfaces are explicitly configured, which would avoid 506196482Srwatson * this (and other) problems. 507196482Srwatson */ 508196482Srwatson sx_xlock(&ifnet_sxlock); 509183550Szec TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 510183550Szec if (ifp->if_type != IFT_ETHER) continue; 511183550Szec EFDEBUG("Found interface %s\n", ifp->if_xname); 512183550Szec efl = (struct ef_link*)malloc(sizeof(struct ef_link), 513183550Szec M_IFADDR, M_WAITOK | M_ZERO); 514183550Szec if (efl == NULL) { 515183550Szec error = ENOMEM; 516183550Szec break; 517183550Szec } 51854558Sbp 519183550Szec efl->el_ifp = ifp; 52054558Sbp#ifdef ETHER_II 521183550Szec error = ef_clone(efl, ETHER_FT_EII); 522183550Szec if (error) break; 52354558Sbp#endif 52454558Sbp#ifdef ETHER_8023 525183550Szec error = ef_clone(efl, ETHER_FT_8023); 526183550Szec if (error) break; 52754558Sbp#endif 52854558Sbp#ifdef ETHER_8022 529183550Szec error = ef_clone(efl, ETHER_FT_8022); 530183550Szec if (error) break; 53154558Sbp#endif 53254558Sbp#ifdef ETHER_SNAP 533183550Szec error = ef_clone(efl, ETHER_FT_SNAP); 534183550Szec if (error) break; 53554558Sbp#endif 536183550Szec efcount++; 537183550Szec SLIST_INSERT_HEAD(&efdev, efl, el_next); 538183550Szec } 539196482Srwatson sx_xunlock(&ifnet_sxlock); 540183550Szec CURVNET_RESTORE(); 54154558Sbp } 542183550Szec VNET_LIST_RUNLOCK(); 54354558Sbp if (error) { 54454558Sbp if (efl) 54554558Sbp SLIST_INSERT_HEAD(&efdev, efl, el_next); 546154317Srwatson SLIST_FOREACH_SAFE(efl, &efdev, el_next, efl_temp) { 54754558Sbp for (d = 0; d < EF_NFT; d++) 548147256Sbrooks if (efl->el_units[d]) { 549147256Sbrooks if (efl->el_units[d]->ef_pifp != NULL) 550147256Sbrooks if_free(efl->el_units[d]->ef_pifp); 55154558Sbp free(efl->el_units[d], M_IFADDR); 552147256Sbrooks } 55354558Sbp free(efl, M_IFADDR); 55454558Sbp } 55554558Sbp return error; 55654558Sbp } 55754558Sbp SLIST_FOREACH(efl, &efdev, el_next) { 55854558Sbp for (d = 0; d < EF_NFT; d++) { 55954558Sbp efp = efl->el_units[d]; 56054558Sbp if (efp) 56154558Sbp ef_attach(efp); 56254558Sbp } 56354558Sbp } 56454558Sbp ef_inputp = ef_input; 56554558Sbp ef_outputp = ef_output; 56654558Sbp EFDEBUG("Loaded\n"); 56754558Sbp return 0; 56854558Sbp} 56954558Sbp 57054558Sbpstatic int 57154558Sbpef_unload(void) 57254558Sbp{ 57354558Sbp struct efnet *efp; 57454558Sbp struct ef_link *efl; 57554558Sbp int d; 57654558Sbp 57754558Sbp ef_inputp = NULL; 57854558Sbp ef_outputp = NULL; 57954558Sbp SLIST_FOREACH(efl, &efdev, el_next) { 58054558Sbp for (d = 0; d < EF_NFT; d++) { 58154558Sbp efp = efl->el_units[d]; 58254558Sbp if (efp) { 58354558Sbp ef_detach(efp); 58454558Sbp } 58554558Sbp } 58654558Sbp } 58754558Sbp EFDEBUG("Unloaded\n"); 58854558Sbp return 0; 58954558Sbp} 59054558Sbp 59154558Sbpstatic int 59254558Sbpif_ef_modevent(module_t mod, int type, void *data) 59354558Sbp{ 59454558Sbp switch ((modeventtype_t)type) { 59554558Sbp case MOD_LOAD: 59654558Sbp return ef_load(); 59754558Sbp case MOD_UNLOAD: 59854558Sbp return ef_unload(); 59954558Sbp default: 600132199Sphk return EOPNOTSUPP; 60154558Sbp } 60254558Sbp return 0; 60354558Sbp} 60454558Sbp 60554558Sbpstatic moduledata_t if_ef_mod = { 60654558Sbp "if_ef", if_ef_modevent, NULL 60754558Sbp}; 60854558Sbp 60954558SbpDECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE); 610