if_ef.c revision 183550
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 183550 2008-10-02 15:37:58Z zec $
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>
5354558Sbp
5454558Sbp#ifdef INET
5554558Sbp#include <netinet/in.h>
5654558Sbp#include <netinet/in_var.h>
5754558Sbp#include <netinet/if_ether.h>
5854558Sbp#endif
5954558Sbp
6054558Sbp#ifdef IPX
6154558Sbp#include <netipx/ipx.h>
6254558Sbp#include <netipx/ipx_if.h>
6354558Sbp#endif
6454558Sbp
65143196Ssobomax/* If none of the supported layers is enabled explicitly enable them all */
66143196Ssobomax#if !defined(ETHER_II) && !defined(ETHER_8023) && !defined(ETHER_8022) && \
67143196Ssobomax    !defined(ETHER_SNAP)
68143196Ssobomax#define	ETHER_II	1
69143196Ssobomax#define	ETHER_8023	1
70143196Ssobomax#define	ETHER_8022	1
71143196Ssobomax#define	ETHER_SNAP	1
72143196Ssobomax#endif
73143196Ssobomax
7454558Sbp/* internal frame types */
7554558Sbp#define ETHER_FT_EII		0	/* Ethernet_II - default */
7654558Sbp#define	ETHER_FT_8023		1	/* 802.3 (Novell) */
7754558Sbp#define	ETHER_FT_8022		2	/* 802.2 */
7854558Sbp#define	ETHER_FT_SNAP		3	/* SNAP */
7954558Sbp#define	EF_NFT			4	/* total number of frame types */
8054558Sbp
8154558Sbp#ifdef EF_DEBUG
8287599Sobrien#define EFDEBUG(format, args...) printf("%s: "format, __func__ ,## args)
8354558Sbp#else
8454558Sbp#define EFDEBUG(format, args...)
8554558Sbp#endif
8654558Sbp
8787599Sobrien#define EFERROR(format, args...) printf("%s: "format, __func__ ,## args)
8854558Sbp
8954558Sbpstruct efnet {
90147256Sbrooks	struct ifnet	*ef_ifp;
91147256Sbrooks	struct ifnet	*ef_pifp;
92121816Sbrooks	int		ef_frametype;
9354558Sbp};
9454558Sbp
9554558Sbpstruct ef_link {
9660938Sjake	SLIST_ENTRY(ef_link) el_next;
9754558Sbp	struct ifnet	*el_ifp;		/* raw device for this clones */
9854558Sbp	struct efnet	*el_units[EF_NFT];	/* our clones */
9954558Sbp};
10054558Sbp
10160938Sjakestatic SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
10254558Sbpstatic int efcount;
10354558Sbp
10454558Sbpextern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
10559681Sbpextern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
10666479Sbp		struct sockaddr *dst, short *tp, int *hlen);
10754558Sbp
10854558Sbp/*
10954558Sbpstatic void ef_reset (struct ifnet *);
11054558Sbp*/
11154558Sbpstatic int ef_attach(struct efnet *sc);
11254558Sbpstatic int ef_detach(struct efnet *sc);
11354558Sbpstatic void ef_init(void *);
11454558Sbpstatic int ef_ioctl(struct ifnet *, u_long, caddr_t);
11554558Sbpstatic void ef_start(struct ifnet *);
11654558Sbpstatic int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
11759681Sbpstatic int ef_output(struct ifnet *ifp, struct mbuf **mp,
11866479Sbp		struct sockaddr *dst, short *tp, int *hlen);
11954558Sbp
12054558Sbpstatic int ef_load(void);
12154558Sbpstatic int ef_unload(void);
12254558Sbp
12354558Sbp/*
12454558Sbp * Install the interface, most of structure initialization done in ef_clone()
12554558Sbp */
12654558Sbpstatic int
12754558Sbpef_attach(struct efnet *sc)
12854558Sbp{
129147256Sbrooks	struct ifnet *ifp = sc->ef_ifp;
13054558Sbp
13154558Sbp	ifp->if_start = ef_start;
13254558Sbp	ifp->if_watchdog = NULL;
13354558Sbp	ifp->if_init = ef_init;
13454558Sbp	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
13554558Sbp	ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
13654558Sbp	/*
13754558Sbp	 * Attach the interface
13854558Sbp	 */
139152315Sru	ether_ifattach(ifp, IF_LLADDR(sc->ef_pifp));
14054558Sbp
14154558Sbp	ifp->if_resolvemulti = 0;
14254558Sbp	ifp->if_type = IFT_XETHER;
143148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
14454558Sbp
145121816Sbrooks	EFDEBUG("%s: attached\n", ifp->if_xname);
14654558Sbp	return 1;
14754558Sbp}
14854558Sbp
14954558Sbp/*
15054558Sbp * This is for _testing_only_, just removes interface from interfaces list
15154558Sbp */
15254558Sbpstatic int
15354558Sbpef_detach(struct efnet *sc)
15454558Sbp{
155147256Sbrooks	struct ifnet *ifp = sc->ef_ifp;
15654558Sbp	int s;
15754558Sbp
15854558Sbp	s = splimp();
15954558Sbp
160147256Sbrooks	ether_ifdetach(ifp);
161147256Sbrooks	if_free(ifp);
162147256Sbrooks
16354558Sbp	splx(s);
16454558Sbp	return 0;
16554558Sbp}
16654558Sbp
16754558Sbpstatic void
16854558Sbpef_init(void *foo) {
16954558Sbp	return;
17054558Sbp}
17154558Sbp
17254558Sbpstatic int
17354558Sbpef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
17454558Sbp{
175121816Sbrooks	struct efnet *sc = ifp->if_softc;
17654558Sbp	struct ifaddr *ifa = (struct ifaddr*)data;
17754558Sbp	int s, error;
17854558Sbp
179121816Sbrooks	EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
18054558Sbp	error = 0;
18154558Sbp	s = splimp();
18254558Sbp	switch (cmd) {
183106939Ssam	    case SIOCSIFFLAGS:
184106939Ssam		error = 0;
185106939Ssam		break;
18654558Sbp	    case SIOCSIFADDR:
187121816Sbrooks		if (sc->ef_frametype == ETHER_FT_8023 &&
18854558Sbp		    ifa->ifa_addr->sa_family != AF_IPX) {
18954558Sbp			error = EAFNOSUPPORT;
19054558Sbp			break;
19154558Sbp		}
19254558Sbp		ifp->if_flags |= IFF_UP;
19354558Sbp		/* FALL THROUGH */
194106939Ssam	    default:
19554558Sbp		error = ether_ioctl(ifp, cmd, data);
19654558Sbp		break;
19754558Sbp	}
19854558Sbp	splx(s);
19954558Sbp	return error;
20054558Sbp}
20154558Sbp
20254558Sbp/*
20354558Sbp * Currently packet prepared in the ether_output(), but this can be a better
20454558Sbp * place.
20554558Sbp */
20654558Sbpstatic void
20754558Sbpef_start(struct ifnet *ifp)
20854558Sbp{
20954558Sbp	struct efnet *sc = (struct efnet*)ifp->if_softc;
21054558Sbp	struct ifnet *p;
21154558Sbp	struct mbuf *m;
212130549Smlaier	int error;
21354558Sbp
214148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
215147256Sbrooks	p = sc->ef_pifp;
21654558Sbp
21754558Sbp	EFDEBUG("\n");
21854558Sbp	for (;;) {
21954558Sbp		IF_DEQUEUE(&ifp->if_snd, m);
22054558Sbp		if (m == 0)
22154558Sbp			break;
222106939Ssam		BPF_MTAP(ifp, m);
223130549Smlaier		IFQ_HANDOFF(p, m, error);
224130549Smlaier		if (error) {
22559681Sbp			ifp->if_oerrors++;
22659681Sbp			continue;
22754558Sbp		}
22869152Sjlemon		ifp->if_opackets++;
22954558Sbp	}
230148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
23154558Sbp	return;
23254558Sbp}
23354558Sbp
23454558Sbp/*
23554558Sbp * Inline functions do not put additional overhead to procedure call or
23654558Sbp * parameter passing but simplify the code
23754558Sbp */
23854558Sbpstatic int __inline
239111888Sjlemonef_inputEII(struct mbuf *m, struct ether_header *eh, u_short ether_type)
24054558Sbp{
241111888Sjlemon	int isr;
242111888Sjlemon
24354558Sbp	switch(ether_type) {
24454558Sbp#ifdef IPX
245111888Sjlemon	case ETHERTYPE_IPX:
246111888Sjlemon		isr = NETISR_IPX;
24754558Sbp		break;
24854558Sbp#endif
24954558Sbp#ifdef INET
250111888Sjlemon	case ETHERTYPE_IP:
251154518Sandre		if ((m = ip_fastforward(m)) == NULL)
252111888Sjlemon			return (0);
253111888Sjlemon		isr = NETISR_IP;
25454558Sbp		break;
25554558Sbp
256111888Sjlemon	case ETHERTYPE_ARP:
257111888Sjlemon		isr = NETISR_ARP;
25854558Sbp		break;
25954558Sbp#endif
260111888Sjlemon	default:
261111888Sjlemon		return (EPROTONOSUPPORT);
26254558Sbp	}
263111888Sjlemon	netisr_dispatch(isr, m);
264111888Sjlemon	return (0);
26554558Sbp}
26654558Sbp
26754558Sbpstatic int __inline
26854558Sbpef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
269111888Sjlemon	u_short ether_type)
27054558Sbp{
271111888Sjlemon	int isr;
272111888Sjlemon
27354558Sbp	switch(ether_type) {
27454558Sbp#ifdef IPX
275111888Sjlemon	case ETHERTYPE_IPX:
27654558Sbp		m_adj(m, 8);
277111888Sjlemon		isr = NETISR_IPX;
27854558Sbp		break;
27954558Sbp#endif
280111888Sjlemon	default:
281111888Sjlemon		return (EPROTONOSUPPORT);
28254558Sbp	}
283111888Sjlemon	netisr_dispatch(isr, m);
284111888Sjlemon	return (0);
28554558Sbp}
28654558Sbp
28754558Sbpstatic int __inline
28854558Sbpef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
289111888Sjlemon	u_short ether_type)
29054558Sbp{
291111888Sjlemon	int isr;
292111888Sjlemon
29354558Sbp	switch(ether_type) {
29454558Sbp#ifdef IPX
295111888Sjlemon	case 0xe0:
29654558Sbp		m_adj(m, 3);
297111888Sjlemon		isr = NETISR_IPX;
29854558Sbp		break;
29954558Sbp#endif
300111888Sjlemon	default:
301111888Sjlemon		return (EPROTONOSUPPORT);
30254558Sbp	}
303111888Sjlemon	netisr_dispatch(isr, m);
304111888Sjlemon	return (0);
30554558Sbp}
306111888Sjlemon
30754558Sbp/*
30854558Sbp * Called from ether_input()
30954558Sbp */
31054558Sbpstatic int
31154558Sbpef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
31254558Sbp{
31354558Sbp	u_short ether_type;
31471891Sbp	int ft = -1;
31554558Sbp	struct efnet *efp;
31654558Sbp	struct ifnet *eifp;
31754558Sbp	struct llc *l;
31854558Sbp	struct ef_link *efl;
319111888Sjlemon	int isr;
32054558Sbp
32154558Sbp	ether_type = ntohs(eh->ether_type);
322132778Skan	l = NULL;
32354558Sbp	if (ether_type < ETHERMTU) {
32454558Sbp		l = mtod(m, struct llc*);
32554558Sbp		if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
32654558Sbp			/*
32754558Sbp			 * Novell's "802.3" frame
32854558Sbp			 */
32954558Sbp			ft = ETHER_FT_8023;
33054558Sbp		} else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
33154558Sbp			/*
33254558Sbp			 * 802.2/SNAP
33354558Sbp			 */
33454558Sbp			ft = ETHER_FT_SNAP;
33554558Sbp			ether_type = ntohs(l->llc_un.type_snap.ether_type);
33654558Sbp		} else if (l->llc_dsap == l->llc_ssap) {
33754558Sbp			/*
33854558Sbp			 * 802.3/802.2
33954558Sbp			 */
34054558Sbp			ft = ETHER_FT_8022;
34154558Sbp			ether_type = l->llc_ssap;
34254558Sbp		}
34354558Sbp	} else
34454558Sbp		ft = ETHER_FT_EII;
34554558Sbp
34654558Sbp	if (ft == -1) {
34754558Sbp		EFDEBUG("Unrecognised ether_type %x\n", ether_type);
34859681Sbp		return EPROTONOSUPPORT;
34954558Sbp	}
35054558Sbp
35154558Sbp	/*
35254558Sbp	 * Check if interface configured for the given frame
35354558Sbp	 */
35454558Sbp	efp = NULL;
35554558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
35654558Sbp		if (efl->el_ifp == ifp) {
35754558Sbp			efp = efl->el_units[ft];
35854558Sbp			break;
35954558Sbp		}
36054558Sbp	}
36154558Sbp	if (efp == NULL) {
36254558Sbp		EFDEBUG("Can't find if for %d\n", ft);
36359681Sbp		return EPROTONOSUPPORT;
36454558Sbp	}
365147256Sbrooks	eifp = efp->ef_ifp;
36654558Sbp	if ((eifp->if_flags & IFF_UP) == 0)
36759681Sbp		return EPROTONOSUPPORT;
36854558Sbp	eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
36954558Sbp	m->m_pkthdr.rcvif = eifp;
37054558Sbp
371123922Ssam	BPF_MTAP2(eifp, eh, ETHER_HDR_LEN, m);
37254558Sbp	/*
37354558Sbp	 * Now we ready to adjust mbufs and pass them to protocol intr's
37454558Sbp	 */
37554558Sbp	switch(ft) {
376111888Sjlemon	case ETHER_FT_EII:
377111888Sjlemon		return (ef_inputEII(m, eh, ether_type));
37854558Sbp#ifdef IPX
379111888Sjlemon	case ETHER_FT_8023:		/* only IPX can be here */
380111888Sjlemon		isr = NETISR_IPX;
38154558Sbp		break;
38254558Sbp#endif
383111888Sjlemon	case ETHER_FT_SNAP:
384111888Sjlemon		return (ef_inputSNAP(m, eh, l, ether_type));
385111888Sjlemon	case ETHER_FT_8022:
386111888Sjlemon		return (ef_input8022(m, eh, l, ether_type));
387111888Sjlemon	default:
38854558Sbp		EFDEBUG("No support for frame %d and proto %04x\n",
38954558Sbp			ft, ether_type);
390111888Sjlemon		return (EPROTONOSUPPORT);
39154558Sbp	}
392111888Sjlemon	netisr_dispatch(isr, m);
393111888Sjlemon	return (0);
39454558Sbp}
39554558Sbp
39654558Sbpstatic int
39766479Sbpef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
39866479Sbp	int *hlen)
39954558Sbp{
400121816Sbrooks	struct efnet *sc = (struct efnet*)ifp->if_softc;
40159681Sbp	struct mbuf *m = *mp;
40254558Sbp	u_char *cp;
40354558Sbp	short type;
40454558Sbp
40554558Sbp	if (ifp->if_type != IFT_XETHER)
40659681Sbp		return ENETDOWN;
407121816Sbrooks	switch (sc->ef_frametype) {
40854558Sbp	    case ETHER_FT_EII:
40954558Sbp#ifdef IPX
41054558Sbp		type = htons(ETHERTYPE_IPX);
41154558Sbp#else
41259681Sbp		return EPFNOSUPPORT;
41354558Sbp#endif
41454558Sbp		break;
41554558Sbp	    case ETHER_FT_8023:
41654558Sbp		type = htons(m->m_pkthdr.len);
41754558Sbp		break;
41854558Sbp	    case ETHER_FT_8022:
419177599Sru		M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAIT);
42059681Sbp		/*
42159681Sbp		 * Ensure that ethernet header and next three bytes
42259681Sbp		 * will fit into single mbuf
42359681Sbp		 */
42459681Sbp		m = m_pullup(m, ETHER_HDR_LEN + 3);
42559681Sbp		if (m == NULL) {
42659681Sbp			*mp = NULL;
42759681Sbp			return ENOBUFS;
42859681Sbp		}
42959681Sbp		m_adj(m, ETHER_HDR_LEN);
43054558Sbp		type = htons(m->m_pkthdr.len);
43154558Sbp		cp = mtod(m, u_char *);
43254558Sbp		*cp++ = 0xE0;
43354558Sbp		*cp++ = 0xE0;
43454558Sbp		*cp++ = 0x03;
43566479Sbp		*hlen += 3;
43654558Sbp		break;
43754558Sbp	    case ETHER_FT_SNAP:
438177599Sru		M_PREPEND(m, 8, M_WAIT);
43954558Sbp		type = htons(m->m_pkthdr.len);
44054558Sbp		cp = mtod(m, u_char *);
44154558Sbp		bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
44266479Sbp		*hlen += 8;
44354558Sbp		break;
44454558Sbp	    default:
44559681Sbp		return EPFNOSUPPORT;
44654558Sbp	}
44759681Sbp	*mp = m;
44854558Sbp	*tp = type;
44954558Sbp	return 0;
45054558Sbp}
45154558Sbp
45254558Sbp/*
45354558Sbp * Create clone from the given interface
45454558Sbp */
45554558Sbpstatic int
45654558Sbpef_clone(struct ef_link *efl, int ft)
45754558Sbp{
45854558Sbp	struct efnet *efp;
45954558Sbp	struct ifnet *eifp;
46054558Sbp	struct ifnet *ifp = efl->el_ifp;
46154558Sbp
46269781Sdwmalone	efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
463111119Simp	    M_WAITOK | M_ZERO);
46454558Sbp	if (efp == NULL)
46554558Sbp		return ENOMEM;
466147256Sbrooks	efp->ef_pifp = ifp;
467121816Sbrooks	efp->ef_frametype = ft;
468147256Sbrooks	eifp = efp->ef_ifp = if_alloc(IFT_ETHER);
469154318Srwatson	if (eifp == NULL) {
470154318Srwatson		free(efp, M_IFADDR);
471147256Sbrooks		return (ENOSPC);
472154318Srwatson	}
473121816Sbrooks	snprintf(eifp->if_xname, IFNAMSIZ,
474121816Sbrooks	    "%sf%d", ifp->if_xname, efp->ef_frametype);
475121816Sbrooks	eifp->if_dname = "ef";
476121816Sbrooks	eifp->if_dunit = IF_DUNIT_NONE;
47754558Sbp	eifp->if_softc = efp;
47854558Sbp	if (ifp->if_ioctl)
47954558Sbp		eifp->if_ioctl = ef_ioctl;
48054558Sbp	efl->el_units[ft] = efp;
48154558Sbp	return 0;
48254558Sbp}
48354558Sbp
48454558Sbpstatic int
48554558Sbpef_load(void)
48654558Sbp{
487183550Szec	VNET_ITERATOR_DECL(vnet_iter);
48854558Sbp	struct ifnet *ifp;
48954558Sbp	struct efnet *efp;
490154317Srwatson	struct ef_link *efl = NULL, *efl_temp;
49154558Sbp	int error = 0, d;
49254558Sbp
493183550Szec	VNET_LIST_RLOCK();
494183550Szec	VNET_FOREACH(vnet_iter) {
495183550Szec		CURVNET_SET(vnet_iter);
496183550Szec		INIT_VNET_NET(vnet_iter);
497183550Szec		IFNET_RLOCK();
498183550Szec		TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
499183550Szec			if (ifp->if_type != IFT_ETHER) continue;
500183550Szec			EFDEBUG("Found interface %s\n", ifp->if_xname);
501183550Szec			efl = (struct ef_link*)malloc(sizeof(struct ef_link),
502183550Szec			    M_IFADDR, M_WAITOK | M_ZERO);
503183550Szec			if (efl == NULL) {
504183550Szec				error = ENOMEM;
505183550Szec				break;
506183550Szec			}
50754558Sbp
508183550Szec			efl->el_ifp = ifp;
50954558Sbp#ifdef ETHER_II
510183550Szec			error = ef_clone(efl, ETHER_FT_EII);
511183550Szec			if (error) break;
51254558Sbp#endif
51354558Sbp#ifdef ETHER_8023
514183550Szec			error = ef_clone(efl, ETHER_FT_8023);
515183550Szec			if (error) break;
51654558Sbp#endif
51754558Sbp#ifdef ETHER_8022
518183550Szec			error = ef_clone(efl, ETHER_FT_8022);
519183550Szec			if (error) break;
52054558Sbp#endif
52154558Sbp#ifdef ETHER_SNAP
522183550Szec			error = ef_clone(efl, ETHER_FT_SNAP);
523183550Szec			if (error) break;
52454558Sbp#endif
525183550Szec			efcount++;
526183550Szec			SLIST_INSERT_HEAD(&efdev, efl, el_next);
527183550Szec		}
528183550Szec		IFNET_RUNLOCK();
529183550Szec		CURVNET_RESTORE();
53054558Sbp	}
531183550Szec	VNET_LIST_RUNLOCK();
53254558Sbp	if (error) {
53354558Sbp		if (efl)
53454558Sbp			SLIST_INSERT_HEAD(&efdev, efl, el_next);
535154317Srwatson		SLIST_FOREACH_SAFE(efl, &efdev, el_next, efl_temp) {
53654558Sbp			for (d = 0; d < EF_NFT; d++)
537147256Sbrooks				if (efl->el_units[d]) {
538147256Sbrooks					if (efl->el_units[d]->ef_pifp != NULL)
539147256Sbrooks						if_free(efl->el_units[d]->ef_pifp);
54054558Sbp					free(efl->el_units[d], M_IFADDR);
541147256Sbrooks				}
54254558Sbp			free(efl, M_IFADDR);
54354558Sbp		}
54454558Sbp		return error;
54554558Sbp	}
54654558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
54754558Sbp		for (d = 0; d < EF_NFT; d++) {
54854558Sbp			efp = efl->el_units[d];
54954558Sbp			if (efp)
55054558Sbp				ef_attach(efp);
55154558Sbp		}
55254558Sbp	}
55354558Sbp	ef_inputp = ef_input;
55454558Sbp	ef_outputp = ef_output;
55554558Sbp	EFDEBUG("Loaded\n");
55654558Sbp	return 0;
55754558Sbp}
55854558Sbp
55954558Sbpstatic int
56054558Sbpef_unload(void)
56154558Sbp{
56254558Sbp	struct efnet *efp;
56354558Sbp	struct ef_link *efl;
56454558Sbp	int d;
56554558Sbp
56654558Sbp	ef_inputp = NULL;
56754558Sbp	ef_outputp = NULL;
56854558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
56954558Sbp		for (d = 0; d < EF_NFT; d++) {
57054558Sbp			efp = efl->el_units[d];
57154558Sbp			if (efp) {
57254558Sbp				ef_detach(efp);
57354558Sbp			}
57454558Sbp		}
57554558Sbp	}
57654558Sbp	EFDEBUG("Unloaded\n");
57754558Sbp	return 0;
57854558Sbp}
57954558Sbp
58054558Sbpstatic int
58154558Sbpif_ef_modevent(module_t mod, int type, void *data)
58254558Sbp{
58354558Sbp	switch ((modeventtype_t)type) {
58454558Sbp	    case MOD_LOAD:
58554558Sbp		return ef_load();
58654558Sbp	    case MOD_UNLOAD:
58754558Sbp		return ef_unload();
58854558Sbp	    default:
589132199Sphk		return EOPNOTSUPP;
59054558Sbp	}
59154558Sbp	return 0;
59254558Sbp}
59354558Sbp
59454558Sbpstatic moduledata_t if_ef_mod = {
59554558Sbp	"if_ef", if_ef_modevent, NULL
59654558Sbp};
59754558Sbp
59854558SbpDECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
599