if_ef.c revision 154317
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 154317 2006-01-13 23:20:46Z 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/route.h>
5154558Sbp#include <net/bpf.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);
222130549Smlaier		IFQ_HANDOFF(p, m, error);
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:
250122702Sandre		if (ip_fastforward(m))
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:
418111119Simp		M_PREPEND(m, ETHER_HDR_LEN + 3, M_TRYWAIT);
41959681Sbp		if (m == NULL) {
42059681Sbp			*mp = NULL;
42159681Sbp			return ENOBUFS;
42259681Sbp		}
42359681Sbp		/*
42459681Sbp		 * Ensure that ethernet header and next three bytes
42559681Sbp		 * will fit into single mbuf
42659681Sbp		 */
42759681Sbp		m = m_pullup(m, ETHER_HDR_LEN + 3);
42859681Sbp		if (m == NULL) {
42959681Sbp			*mp = NULL;
43059681Sbp			return ENOBUFS;
43159681Sbp		}
43259681Sbp		m_adj(m, ETHER_HDR_LEN);
43354558Sbp		type = htons(m->m_pkthdr.len);
43454558Sbp		cp = mtod(m, u_char *);
43554558Sbp		*cp++ = 0xE0;
43654558Sbp		*cp++ = 0xE0;
43754558Sbp		*cp++ = 0x03;
43866479Sbp		*hlen += 3;
43954558Sbp		break;
44054558Sbp	    case ETHER_FT_SNAP:
441111119Simp		M_PREPEND(m, 8, M_TRYWAIT);
44259681Sbp		if (m == NULL) {
44359681Sbp			*mp = NULL;
44459681Sbp			return ENOBUFS;
44559681Sbp		}
44654558Sbp		type = htons(m->m_pkthdr.len);
44754558Sbp		cp = mtod(m, u_char *);
44854558Sbp		bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
44966479Sbp		*hlen += 8;
45054558Sbp		break;
45154558Sbp	    default:
45259681Sbp		return EPFNOSUPPORT;
45354558Sbp	}
45459681Sbp	*mp = m;
45554558Sbp	*tp = type;
45654558Sbp	return 0;
45754558Sbp}
45854558Sbp
45954558Sbp/*
46054558Sbp * Create clone from the given interface
46154558Sbp */
46254558Sbpstatic int
46354558Sbpef_clone(struct ef_link *efl, int ft)
46454558Sbp{
46554558Sbp	struct efnet *efp;
46654558Sbp	struct ifnet *eifp;
46754558Sbp	struct ifnet *ifp = efl->el_ifp;
46854558Sbp
46969781Sdwmalone	efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
470111119Simp	    M_WAITOK | M_ZERO);
47154558Sbp	if (efp == NULL)
47254558Sbp		return ENOMEM;
473147256Sbrooks	efp->ef_pifp = ifp;
474121816Sbrooks	efp->ef_frametype = ft;
475147256Sbrooks	eifp = efp->ef_ifp = if_alloc(IFT_ETHER);
476147256Sbrooks	if (ifp == NULL)
477147256Sbrooks		return (ENOSPC);
478121816Sbrooks	snprintf(eifp->if_xname, IFNAMSIZ,
479121816Sbrooks	    "%sf%d", ifp->if_xname, efp->ef_frametype);
480121816Sbrooks	eifp->if_dname = "ef";
481121816Sbrooks	eifp->if_dunit = IF_DUNIT_NONE;
48254558Sbp	eifp->if_softc = efp;
48354558Sbp	if (ifp->if_ioctl)
48454558Sbp		eifp->if_ioctl = ef_ioctl;
48554558Sbp	efl->el_units[ft] = efp;
48654558Sbp	return 0;
48754558Sbp}
48854558Sbp
48954558Sbpstatic int
49054558Sbpef_load(void)
49154558Sbp{
49254558Sbp	struct ifnet *ifp;
49354558Sbp	struct efnet *efp;
494154317Srwatson	struct ef_link *efl = NULL, *efl_temp;
49554558Sbp	int error = 0, d;
49654558Sbp
497108172Shsu	IFNET_RLOCK();
49871999Sphk	TAILQ_FOREACH(ifp, &ifnet, if_link) {
49954558Sbp		if (ifp->if_type != IFT_ETHER) continue;
500121816Sbrooks		EFDEBUG("Found interface %s\n", ifp->if_xname);
50154558Sbp		efl = (struct ef_link*)malloc(sizeof(struct ef_link),
502111119Simp		    M_IFADDR, M_WAITOK | M_ZERO);
50354558Sbp		if (efl == NULL) {
50454558Sbp			error = ENOMEM;
50554558Sbp			break;
50654558Sbp		}
50754558Sbp
50854558Sbp		efl->el_ifp = ifp;
50954558Sbp#ifdef ETHER_II
51054558Sbp		error = ef_clone(efl, ETHER_FT_EII);
51154558Sbp		if (error) break;
51254558Sbp#endif
51354558Sbp#ifdef ETHER_8023
51454558Sbp		error = ef_clone(efl, ETHER_FT_8023);
51554558Sbp		if (error) break;
51654558Sbp#endif
51754558Sbp#ifdef ETHER_8022
51854558Sbp		error = ef_clone(efl, ETHER_FT_8022);
51954558Sbp		if (error) break;
52054558Sbp#endif
52154558Sbp#ifdef ETHER_SNAP
52254558Sbp		error = ef_clone(efl, ETHER_FT_SNAP);
52354558Sbp		if (error) break;
52454558Sbp#endif
52554558Sbp		efcount++;
52654558Sbp		SLIST_INSERT_HEAD(&efdev, efl, el_next);
52754558Sbp	}
528108172Shsu	IFNET_RUNLOCK();
52954558Sbp	if (error) {
53054558Sbp		if (efl)
53154558Sbp			SLIST_INSERT_HEAD(&efdev, efl, el_next);
532154317Srwatson		SLIST_FOREACH_SAFE(efl, &efdev, el_next, efl_temp) {
53354558Sbp			for (d = 0; d < EF_NFT; d++)
534147256Sbrooks				if (efl->el_units[d]) {
535147256Sbrooks					if (efl->el_units[d]->ef_pifp != NULL)
536147256Sbrooks						if_free(efl->el_units[d]->ef_pifp);
53754558Sbp					free(efl->el_units[d], M_IFADDR);
538147256Sbrooks				}
53954558Sbp			free(efl, M_IFADDR);
54054558Sbp		}
54154558Sbp		return error;
54254558Sbp	}
54354558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
54454558Sbp		for (d = 0; d < EF_NFT; d++) {
54554558Sbp			efp = efl->el_units[d];
54654558Sbp			if (efp)
54754558Sbp				ef_attach(efp);
54854558Sbp		}
54954558Sbp	}
55054558Sbp	ef_inputp = ef_input;
55154558Sbp	ef_outputp = ef_output;
55254558Sbp	EFDEBUG("Loaded\n");
55354558Sbp	return 0;
55454558Sbp}
55554558Sbp
55654558Sbpstatic int
55754558Sbpef_unload(void)
55854558Sbp{
55954558Sbp	struct efnet *efp;
56054558Sbp	struct ef_link *efl;
56154558Sbp	int d;
56254558Sbp
56354558Sbp	ef_inputp = NULL;
56454558Sbp	ef_outputp = NULL;
56554558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
56654558Sbp		for (d = 0; d < EF_NFT; d++) {
56754558Sbp			efp = efl->el_units[d];
56854558Sbp			if (efp) {
56954558Sbp				ef_detach(efp);
57054558Sbp			}
57154558Sbp		}
57254558Sbp	}
57354558Sbp	EFDEBUG("Unloaded\n");
57454558Sbp	return 0;
57554558Sbp}
57654558Sbp
57754558Sbpstatic int
57854558Sbpif_ef_modevent(module_t mod, int type, void *data)
57954558Sbp{
58054558Sbp	switch ((modeventtype_t)type) {
58154558Sbp	    case MOD_LOAD:
58254558Sbp		return ef_load();
58354558Sbp	    case MOD_UNLOAD:
58454558Sbp		return ef_unload();
58554558Sbp	    default:
586132199Sphk		return EOPNOTSUPP;
58754558Sbp	}
58854558Sbp	return 0;
58954558Sbp}
59054558Sbp
59154558Sbpstatic moduledata_t if_ef_mod = {
59254558Sbp	"if_ef", if_ef_modevent, NULL
59354558Sbp};
59454558Sbp
59554558SbpDECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
596