if_ef.c revision 185571
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 185571 2008-12-02 21:37:28Z bz $
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>
42181803Sbz#include <sys/vimage.h>
4354558Sbp
4454558Sbp#include <net/ethernet.h>
4554558Sbp#include <net/if_llc.h>
4654558Sbp#include <net/if.h>
4754558Sbp#include <net/if_arp.h>
4854558Sbp#include <net/if_dl.h>
4954558Sbp#include <net/if_types.h>
5054558Sbp#include <net/netisr.h>
5154558Sbp#include <net/route.h>
5254558Sbp#include <net/bpf.h>
53185571Sbz#include <net/vnet.h>
5454558Sbp
5554558Sbp#ifdef INET
5654558Sbp#include <netinet/in.h>
5754558Sbp#include <netinet/in_var.h>
5854558Sbp#include <netinet/if_ether.h>
5954558Sbp#endif
6054558Sbp
6154558Sbp#ifdef IPX
6254558Sbp#include <netipx/ipx.h>
6354558Sbp#include <netipx/ipx_if.h>
6454558Sbp#endif
6554558Sbp
66143196Ssobomax/* If none of the supported layers is enabled explicitly enable them all */
67143196Ssobomax#if !defined(ETHER_II) && !defined(ETHER_8023) && !defined(ETHER_8022) && \
68143196Ssobomax    !defined(ETHER_SNAP)
69143196Ssobomax#define	ETHER_II	1
70143196Ssobomax#define	ETHER_8023	1
71143196Ssobomax#define	ETHER_8022	1
72143196Ssobomax#define	ETHER_SNAP	1
73143196Ssobomax#endif
74143196Ssobomax
7554558Sbp/* internal frame types */
7654558Sbp#define ETHER_FT_EII		0	/* Ethernet_II - default */
7754558Sbp#define	ETHER_FT_8023		1	/* 802.3 (Novell) */
7854558Sbp#define	ETHER_FT_8022		2	/* 802.2 */
7954558Sbp#define	ETHER_FT_SNAP		3	/* SNAP */
8054558Sbp#define	EF_NFT			4	/* total number of frame types */
8154558Sbp
8254558Sbp#ifdef EF_DEBUG
8387599Sobrien#define EFDEBUG(format, args...) printf("%s: "format, __func__ ,## args)
8454558Sbp#else
8554558Sbp#define EFDEBUG(format, args...)
8654558Sbp#endif
8754558Sbp
8887599Sobrien#define EFERROR(format, args...) printf("%s: "format, __func__ ,## args)
8954558Sbp
9054558Sbpstruct efnet {
91147256Sbrooks	struct ifnet	*ef_ifp;
92147256Sbrooks	struct ifnet	*ef_pifp;
93121816Sbrooks	int		ef_frametype;
9454558Sbp};
9554558Sbp
9654558Sbpstruct ef_link {
9760938Sjake	SLIST_ENTRY(ef_link) el_next;
9854558Sbp	struct ifnet	*el_ifp;		/* raw device for this clones */
9954558Sbp	struct efnet	*el_units[EF_NFT];	/* our clones */
10054558Sbp};
10154558Sbp
10260938Sjakestatic SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
10354558Sbpstatic int efcount;
10454558Sbp
10554558Sbpextern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
10659681Sbpextern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
10766479Sbp		struct sockaddr *dst, short *tp, int *hlen);
10854558Sbp
10954558Sbp/*
11054558Sbpstatic void ef_reset (struct ifnet *);
11154558Sbp*/
11254558Sbpstatic int ef_attach(struct efnet *sc);
11354558Sbpstatic int ef_detach(struct efnet *sc);
11454558Sbpstatic void ef_init(void *);
11554558Sbpstatic int ef_ioctl(struct ifnet *, u_long, caddr_t);
11654558Sbpstatic void ef_start(struct ifnet *);
11754558Sbpstatic int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
11859681Sbpstatic int ef_output(struct ifnet *ifp, struct mbuf **mp,
11966479Sbp		struct sockaddr *dst, short *tp, int *hlen);
12054558Sbp
12154558Sbpstatic int ef_load(void);
12254558Sbpstatic int ef_unload(void);
12354558Sbp
12454558Sbp/*
12554558Sbp * Install the interface, most of structure initialization done in ef_clone()
12654558Sbp */
12754558Sbpstatic int
12854558Sbpef_attach(struct efnet *sc)
12954558Sbp{
130147256Sbrooks	struct ifnet *ifp = sc->ef_ifp;
13154558Sbp
13254558Sbp	ifp->if_start = ef_start;
13354558Sbp	ifp->if_watchdog = NULL;
13454558Sbp	ifp->if_init = ef_init;
13554558Sbp	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
13654558Sbp	ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
13754558Sbp	/*
13854558Sbp	 * Attach the interface
13954558Sbp	 */
140152315Sru	ether_ifattach(ifp, IF_LLADDR(sc->ef_pifp));
14154558Sbp
14254558Sbp	ifp->if_resolvemulti = 0;
14354558Sbp	ifp->if_type = IFT_XETHER;
144148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
14554558Sbp
146121816Sbrooks	EFDEBUG("%s: attached\n", ifp->if_xname);
14754558Sbp	return 1;
14854558Sbp}
14954558Sbp
15054558Sbp/*
15154558Sbp * This is for _testing_only_, just removes interface from interfaces list
15254558Sbp */
15354558Sbpstatic int
15454558Sbpef_detach(struct efnet *sc)
15554558Sbp{
156147256Sbrooks	struct ifnet *ifp = sc->ef_ifp;
15754558Sbp	int s;
15854558Sbp
15954558Sbp	s = splimp();
16054558Sbp
161147256Sbrooks	ether_ifdetach(ifp);
162147256Sbrooks	if_free(ifp);
163147256Sbrooks
16454558Sbp	splx(s);
16554558Sbp	return 0;
16654558Sbp}
16754558Sbp
16854558Sbpstatic void
16954558Sbpef_init(void *foo) {
17054558Sbp	return;
17154558Sbp}
17254558Sbp
17354558Sbpstatic int
17454558Sbpef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
17554558Sbp{
176121816Sbrooks	struct efnet *sc = ifp->if_softc;
17754558Sbp	struct ifaddr *ifa = (struct ifaddr*)data;
17854558Sbp	int s, error;
17954558Sbp
180121816Sbrooks	EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
18154558Sbp	error = 0;
18254558Sbp	s = splimp();
18354558Sbp	switch (cmd) {
184106939Ssam	    case SIOCSIFFLAGS:
185106939Ssam		error = 0;
186106939Ssam		break;
18754558Sbp	    case SIOCSIFADDR:
188121816Sbrooks		if (sc->ef_frametype == ETHER_FT_8023 &&
18954558Sbp		    ifa->ifa_addr->sa_family != AF_IPX) {
19054558Sbp			error = EAFNOSUPPORT;
19154558Sbp			break;
19254558Sbp		}
19354558Sbp		ifp->if_flags |= IFF_UP;
19454558Sbp		/* FALL THROUGH */
195106939Ssam	    default:
19654558Sbp		error = ether_ioctl(ifp, cmd, data);
19754558Sbp		break;
19854558Sbp	}
19954558Sbp	splx(s);
20054558Sbp	return error;
20154558Sbp}
20254558Sbp
20354558Sbp/*
20454558Sbp * Currently packet prepared in the ether_output(), but this can be a better
20554558Sbp * place.
20654558Sbp */
20754558Sbpstatic void
20854558Sbpef_start(struct ifnet *ifp)
20954558Sbp{
21054558Sbp	struct efnet *sc = (struct efnet*)ifp->if_softc;
21154558Sbp	struct ifnet *p;
21254558Sbp	struct mbuf *m;
213130549Smlaier	int error;
21454558Sbp
215148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
216147256Sbrooks	p = sc->ef_pifp;
21754558Sbp
21854558Sbp	EFDEBUG("\n");
21954558Sbp	for (;;) {
22054558Sbp		IF_DEQUEUE(&ifp->if_snd, m);
22154558Sbp		if (m == 0)
22254558Sbp			break;
223106939Ssam		BPF_MTAP(ifp, m);
224130549Smlaier		IFQ_HANDOFF(p, m, error);
225130549Smlaier		if (error) {
22659681Sbp			ifp->if_oerrors++;
22759681Sbp			continue;
22854558Sbp		}
22969152Sjlemon		ifp->if_opackets++;
23054558Sbp	}
231148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
23254558Sbp	return;
23354558Sbp}
23454558Sbp
23554558Sbp/*
23654558Sbp * Inline functions do not put additional overhead to procedure call or
23754558Sbp * parameter passing but simplify the code
23854558Sbp */
23954558Sbpstatic int __inline
240111888Sjlemonef_inputEII(struct mbuf *m, struct ether_header *eh, u_short ether_type)
24154558Sbp{
242111888Sjlemon	int isr;
243111888Sjlemon
24454558Sbp	switch(ether_type) {
24554558Sbp#ifdef IPX
246111888Sjlemon	case ETHERTYPE_IPX:
247111888Sjlemon		isr = NETISR_IPX;
24854558Sbp		break;
24954558Sbp#endif
25054558Sbp#ifdef INET
251111888Sjlemon	case ETHERTYPE_IP:
252154518Sandre		if ((m = ip_fastforward(m)) == NULL)
253111888Sjlemon			return (0);
254111888Sjlemon		isr = NETISR_IP;
25554558Sbp		break;
25654558Sbp
257111888Sjlemon	case ETHERTYPE_ARP:
258111888Sjlemon		isr = NETISR_ARP;
25954558Sbp		break;
26054558Sbp#endif
261111888Sjlemon	default:
262111888Sjlemon		return (EPROTONOSUPPORT);
26354558Sbp	}
264111888Sjlemon	netisr_dispatch(isr, m);
265111888Sjlemon	return (0);
26654558Sbp}
26754558Sbp
26854558Sbpstatic int __inline
26954558Sbpef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
270111888Sjlemon	u_short ether_type)
27154558Sbp{
272111888Sjlemon	int isr;
273111888Sjlemon
27454558Sbp	switch(ether_type) {
27554558Sbp#ifdef IPX
276111888Sjlemon	case ETHERTYPE_IPX:
27754558Sbp		m_adj(m, 8);
278111888Sjlemon		isr = NETISR_IPX;
27954558Sbp		break;
28054558Sbp#endif
281111888Sjlemon	default:
282111888Sjlemon		return (EPROTONOSUPPORT);
28354558Sbp	}
284111888Sjlemon	netisr_dispatch(isr, m);
285111888Sjlemon	return (0);
28654558Sbp}
28754558Sbp
28854558Sbpstatic int __inline
28954558Sbpef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
290111888Sjlemon	u_short ether_type)
29154558Sbp{
292111888Sjlemon	int isr;
293111888Sjlemon
29454558Sbp	switch(ether_type) {
29554558Sbp#ifdef IPX
296111888Sjlemon	case 0xe0:
29754558Sbp		m_adj(m, 3);
298111888Sjlemon		isr = NETISR_IPX;
29954558Sbp		break;
30054558Sbp#endif
301111888Sjlemon	default:
302111888Sjlemon		return (EPROTONOSUPPORT);
30354558Sbp	}
304111888Sjlemon	netisr_dispatch(isr, m);
305111888Sjlemon	return (0);
30654558Sbp}
307111888Sjlemon
30854558Sbp/*
30954558Sbp * Called from ether_input()
31054558Sbp */
31154558Sbpstatic int
31254558Sbpef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
31354558Sbp{
31454558Sbp	u_short ether_type;
31571891Sbp	int ft = -1;
31654558Sbp	struct efnet *efp;
31754558Sbp	struct ifnet *eifp;
31854558Sbp	struct llc *l;
31954558Sbp	struct ef_link *efl;
320111888Sjlemon	int isr;
32154558Sbp
32254558Sbp	ether_type = ntohs(eh->ether_type);
323132778Skan	l = NULL;
32454558Sbp	if (ether_type < ETHERMTU) {
32554558Sbp		l = mtod(m, struct llc*);
32654558Sbp		if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
32754558Sbp			/*
32854558Sbp			 * Novell's "802.3" frame
32954558Sbp			 */
33054558Sbp			ft = ETHER_FT_8023;
33154558Sbp		} else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
33254558Sbp			/*
33354558Sbp			 * 802.2/SNAP
33454558Sbp			 */
33554558Sbp			ft = ETHER_FT_SNAP;
33654558Sbp			ether_type = ntohs(l->llc_un.type_snap.ether_type);
33754558Sbp		} else if (l->llc_dsap == l->llc_ssap) {
33854558Sbp			/*
33954558Sbp			 * 802.3/802.2
34054558Sbp			 */
34154558Sbp			ft = ETHER_FT_8022;
34254558Sbp			ether_type = l->llc_ssap;
34354558Sbp		}
34454558Sbp	} else
34554558Sbp		ft = ETHER_FT_EII;
34654558Sbp
34754558Sbp	if (ft == -1) {
34854558Sbp		EFDEBUG("Unrecognised ether_type %x\n", ether_type);
34959681Sbp		return EPROTONOSUPPORT;
35054558Sbp	}
35154558Sbp
35254558Sbp	/*
35354558Sbp	 * Check if interface configured for the given frame
35454558Sbp	 */
35554558Sbp	efp = NULL;
35654558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
35754558Sbp		if (efl->el_ifp == ifp) {
35854558Sbp			efp = efl->el_units[ft];
35954558Sbp			break;
36054558Sbp		}
36154558Sbp	}
36254558Sbp	if (efp == NULL) {
36354558Sbp		EFDEBUG("Can't find if for %d\n", ft);
36459681Sbp		return EPROTONOSUPPORT;
36554558Sbp	}
366147256Sbrooks	eifp = efp->ef_ifp;
36754558Sbp	if ((eifp->if_flags & IFF_UP) == 0)
36859681Sbp		return EPROTONOSUPPORT;
36954558Sbp	eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
37054558Sbp	m->m_pkthdr.rcvif = eifp;
37154558Sbp
372123922Ssam	BPF_MTAP2(eifp, eh, ETHER_HDR_LEN, m);
37354558Sbp	/*
37454558Sbp	 * Now we ready to adjust mbufs and pass them to protocol intr's
37554558Sbp	 */
37654558Sbp	switch(ft) {
377111888Sjlemon	case ETHER_FT_EII:
378111888Sjlemon		return (ef_inputEII(m, eh, ether_type));
37954558Sbp#ifdef IPX
380111888Sjlemon	case ETHER_FT_8023:		/* only IPX can be here */
381111888Sjlemon		isr = NETISR_IPX;
38254558Sbp		break;
38354558Sbp#endif
384111888Sjlemon	case ETHER_FT_SNAP:
385111888Sjlemon		return (ef_inputSNAP(m, eh, l, ether_type));
386111888Sjlemon	case ETHER_FT_8022:
387111888Sjlemon		return (ef_input8022(m, eh, l, ether_type));
388111888Sjlemon	default:
38954558Sbp		EFDEBUG("No support for frame %d and proto %04x\n",
39054558Sbp			ft, ether_type);
391111888Sjlemon		return (EPROTONOSUPPORT);
39254558Sbp	}
393111888Sjlemon	netisr_dispatch(isr, m);
394111888Sjlemon	return (0);
39554558Sbp}
39654558Sbp
39754558Sbpstatic int
39866479Sbpef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
39966479Sbp	int *hlen)
40054558Sbp{
401121816Sbrooks	struct efnet *sc = (struct efnet*)ifp->if_softc;
40259681Sbp	struct mbuf *m = *mp;
40354558Sbp	u_char *cp;
40454558Sbp	short type;
40554558Sbp
40654558Sbp	if (ifp->if_type != IFT_XETHER)
40759681Sbp		return ENETDOWN;
408121816Sbrooks	switch (sc->ef_frametype) {
40954558Sbp	    case ETHER_FT_EII:
41054558Sbp#ifdef IPX
41154558Sbp		type = htons(ETHERTYPE_IPX);
41254558Sbp#else
41359681Sbp		return EPFNOSUPPORT;
41454558Sbp#endif
41554558Sbp		break;
41654558Sbp	    case ETHER_FT_8023:
41754558Sbp		type = htons(m->m_pkthdr.len);
41854558Sbp		break;
41954558Sbp	    case ETHER_FT_8022:
420177599Sru		M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAIT);
42159681Sbp		/*
42259681Sbp		 * Ensure that ethernet header and next three bytes
42359681Sbp		 * will fit into single mbuf
42459681Sbp		 */
42559681Sbp		m = m_pullup(m, ETHER_HDR_LEN + 3);
42659681Sbp		if (m == NULL) {
42759681Sbp			*mp = NULL;
42859681Sbp			return ENOBUFS;
42959681Sbp		}
43059681Sbp		m_adj(m, ETHER_HDR_LEN);
43154558Sbp		type = htons(m->m_pkthdr.len);
43254558Sbp		cp = mtod(m, u_char *);
43354558Sbp		*cp++ = 0xE0;
43454558Sbp		*cp++ = 0xE0;
43554558Sbp		*cp++ = 0x03;
43666479Sbp		*hlen += 3;
43754558Sbp		break;
43854558Sbp	    case ETHER_FT_SNAP:
439177599Sru		M_PREPEND(m, 8, M_WAIT);
44054558Sbp		type = htons(m->m_pkthdr.len);
44154558Sbp		cp = mtod(m, u_char *);
44254558Sbp		bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
44366479Sbp		*hlen += 8;
44454558Sbp		break;
44554558Sbp	    default:
44659681Sbp		return EPFNOSUPPORT;
44754558Sbp	}
44859681Sbp	*mp = m;
44954558Sbp	*tp = type;
45054558Sbp	return 0;
45154558Sbp}
45254558Sbp
45354558Sbp/*
45454558Sbp * Create clone from the given interface
45554558Sbp */
45654558Sbpstatic int
45754558Sbpef_clone(struct ef_link *efl, int ft)
45854558Sbp{
45954558Sbp	struct efnet *efp;
46054558Sbp	struct ifnet *eifp;
46154558Sbp	struct ifnet *ifp = efl->el_ifp;
46254558Sbp
46369781Sdwmalone	efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
464111119Simp	    M_WAITOK | M_ZERO);
46554558Sbp	if (efp == NULL)
46654558Sbp		return ENOMEM;
467147256Sbrooks	efp->ef_pifp = ifp;
468121816Sbrooks	efp->ef_frametype = ft;
469147256Sbrooks	eifp = efp->ef_ifp = if_alloc(IFT_ETHER);
470154318Srwatson	if (eifp == NULL) {
471154318Srwatson		free(efp, M_IFADDR);
472147256Sbrooks		return (ENOSPC);
473154318Srwatson	}
474121816Sbrooks	snprintf(eifp->if_xname, IFNAMSIZ,
475121816Sbrooks	    "%sf%d", ifp->if_xname, efp->ef_frametype);
476121816Sbrooks	eifp->if_dname = "ef";
477121816Sbrooks	eifp->if_dunit = IF_DUNIT_NONE;
47854558Sbp	eifp->if_softc = efp;
47954558Sbp	if (ifp->if_ioctl)
48054558Sbp		eifp->if_ioctl = ef_ioctl;
48154558Sbp	efl->el_units[ft] = efp;
48254558Sbp	return 0;
48354558Sbp}
48454558Sbp
48554558Sbpstatic int
48654558Sbpef_load(void)
48754558Sbp{
488183550Szec	VNET_ITERATOR_DECL(vnet_iter);
48954558Sbp	struct ifnet *ifp;
49054558Sbp	struct efnet *efp;
491154317Srwatson	struct ef_link *efl = NULL, *efl_temp;
49254558Sbp	int error = 0, d;
49354558Sbp
494183550Szec	VNET_LIST_RLOCK();
495183550Szec	VNET_FOREACH(vnet_iter) {
496183550Szec		CURVNET_SET(vnet_iter);
497183550Szec		INIT_VNET_NET(vnet_iter);
498183550Szec		IFNET_RLOCK();
499183550Szec		TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
500183550Szec			if (ifp->if_type != IFT_ETHER) continue;
501183550Szec			EFDEBUG("Found interface %s\n", ifp->if_xname);
502183550Szec			efl = (struct ef_link*)malloc(sizeof(struct ef_link),
503183550Szec			    M_IFADDR, M_WAITOK | M_ZERO);
504183550Szec			if (efl == NULL) {
505183550Szec				error = ENOMEM;
506183550Szec				break;
507183550Szec			}
50854558Sbp
509183550Szec			efl->el_ifp = ifp;
51054558Sbp#ifdef ETHER_II
511183550Szec			error = ef_clone(efl, ETHER_FT_EII);
512183550Szec			if (error) break;
51354558Sbp#endif
51454558Sbp#ifdef ETHER_8023
515183550Szec			error = ef_clone(efl, ETHER_FT_8023);
516183550Szec			if (error) break;
51754558Sbp#endif
51854558Sbp#ifdef ETHER_8022
519183550Szec			error = ef_clone(efl, ETHER_FT_8022);
520183550Szec			if (error) break;
52154558Sbp#endif
52254558Sbp#ifdef ETHER_SNAP
523183550Szec			error = ef_clone(efl, ETHER_FT_SNAP);
524183550Szec			if (error) break;
52554558Sbp#endif
526183550Szec			efcount++;
527183550Szec			SLIST_INSERT_HEAD(&efdev, efl, el_next);
528183550Szec		}
529183550Szec		IFNET_RUNLOCK();
530183550Szec		CURVNET_RESTORE();
53154558Sbp	}
532183550Szec	VNET_LIST_RUNLOCK();
53354558Sbp	if (error) {
53454558Sbp		if (efl)
53554558Sbp			SLIST_INSERT_HEAD(&efdev, efl, el_next);
536154317Srwatson		SLIST_FOREACH_SAFE(efl, &efdev, el_next, efl_temp) {
53754558Sbp			for (d = 0; d < EF_NFT; d++)
538147256Sbrooks				if (efl->el_units[d]) {
539147256Sbrooks					if (efl->el_units[d]->ef_pifp != NULL)
540147256Sbrooks						if_free(efl->el_units[d]->ef_pifp);
54154558Sbp					free(efl->el_units[d], M_IFADDR);
542147256Sbrooks				}
54354558Sbp			free(efl, M_IFADDR);
54454558Sbp		}
54554558Sbp		return error;
54654558Sbp	}
54754558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
54854558Sbp		for (d = 0; d < EF_NFT; d++) {
54954558Sbp			efp = efl->el_units[d];
55054558Sbp			if (efp)
55154558Sbp				ef_attach(efp);
55254558Sbp		}
55354558Sbp	}
55454558Sbp	ef_inputp = ef_input;
55554558Sbp	ef_outputp = ef_output;
55654558Sbp	EFDEBUG("Loaded\n");
55754558Sbp	return 0;
55854558Sbp}
55954558Sbp
56054558Sbpstatic int
56154558Sbpef_unload(void)
56254558Sbp{
56354558Sbp	struct efnet *efp;
56454558Sbp	struct ef_link *efl;
56554558Sbp	int d;
56654558Sbp
56754558Sbp	ef_inputp = NULL;
56854558Sbp	ef_outputp = NULL;
56954558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
57054558Sbp		for (d = 0; d < EF_NFT; d++) {
57154558Sbp			efp = efl->el_units[d];
57254558Sbp			if (efp) {
57354558Sbp				ef_detach(efp);
57454558Sbp			}
57554558Sbp		}
57654558Sbp	}
57754558Sbp	EFDEBUG("Unloaded\n");
57854558Sbp	return 0;
57954558Sbp}
58054558Sbp
58154558Sbpstatic int
58254558Sbpif_ef_modevent(module_t mod, int type, void *data)
58354558Sbp{
58454558Sbp	switch ((modeventtype_t)type) {
58554558Sbp	    case MOD_LOAD:
58654558Sbp		return ef_load();
58754558Sbp	    case MOD_UNLOAD:
58854558Sbp		return ef_unload();
58954558Sbp	    default:
590132199Sphk		return EOPNOTSUPP;
59154558Sbp	}
59254558Sbp	return 0;
59354558Sbp}
59454558Sbp
59554558Sbpstatic moduledata_t if_ef_mod = {
59654558Sbp	"if_ef", if_ef_modevent, NULL
59754558Sbp};
59854558Sbp
59954558SbpDECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
600