if_ef.c revision 59681
154558Sbp/*-
254558Sbp * Copyright (c) 1999, 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 59681 2000-04-27 10:13:52Z bp $
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
6454558Sbp/* internal frame types */
6554558Sbp#define ETHER_FT_EII		0	/* Ethernet_II - default */
6654558Sbp#define	ETHER_FT_8023		1	/* 802.3 (Novell) */
6754558Sbp#define	ETHER_FT_8022		2	/* 802.2 */
6854558Sbp#define	ETHER_FT_SNAP		3	/* SNAP */
6954558Sbp#define	EF_NFT			4	/* total number of frame types */
7054558Sbp
7154558Sbp#ifdef EF_DEBUG
7254558Sbp#define EFDEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
7354558Sbp#else
7454558Sbp#define EFDEBUG(format, args...)
7554558Sbp#endif
7654558Sbp
7754558Sbp#define EFERROR(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
7854558Sbp
7954558Sbpstruct efnet {
8054558Sbp	struct arpcom	ef_ac;
8154558Sbp	struct ifnet *  ef_ifp;
8254558Sbp};
8354558Sbp
8454558Sbpstruct ef_link {
8554558Sbp	SLIST_ENTRY(ef_link) el_next;
8654558Sbp	struct ifnet	*el_ifp;		/* raw device for this clones */
8754558Sbp	struct efnet	*el_units[EF_NFT];	/* our clones */
8854558Sbp};
8954558Sbp
9054558Sbpstatic SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
9154558Sbpstatic int efcount;
9254558Sbp
9354558Sbpextern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
9459681Sbpextern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
9554558Sbp		struct sockaddr *dst, short *tp);
9654558Sbp
9754558Sbp/*
9854558Sbpstatic void ef_reset (struct ifnet *);
9954558Sbp*/
10054558Sbpstatic int ef_attach(struct efnet *sc);
10154558Sbpstatic int ef_detach(struct efnet *sc);
10254558Sbpstatic void ef_init(void *);
10354558Sbpstatic int ef_ioctl(struct ifnet *, u_long, caddr_t);
10454558Sbpstatic void ef_start(struct ifnet *);
10554558Sbpstatic int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
10659681Sbpstatic int ef_output(struct ifnet *ifp, struct mbuf **mp,
10754558Sbp		struct sockaddr *dst, short *tp);
10854558Sbp
10954558Sbpstatic int ef_load(void);
11054558Sbpstatic int ef_unload(void);
11154558Sbp
11254558Sbp/*
11354558Sbp * Install the interface, most of structure initialization done in ef_clone()
11454558Sbp */
11554558Sbpstatic int
11654558Sbpef_attach(struct efnet *sc)
11754558Sbp{
11854558Sbp	struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
11954558Sbp	struct ifaddr *ifa1, *ifa2;
12054558Sbp	struct sockaddr_dl *sdl1, *sdl2;
12154558Sbp
12254558Sbp	ifp->if_output = ether_output;
12354558Sbp	ifp->if_start = ef_start;
12454558Sbp	ifp->if_watchdog = NULL;
12554558Sbp	ifp->if_init = ef_init;
12654558Sbp	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
12754558Sbp	ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
12854558Sbp	/*
12954558Sbp	 * Attach the interface
13054558Sbp	 */
13154558Sbp	if_attach(ifp);
13254558Sbp	ether_ifattach(ifp);
13354558Sbp	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
13454558Sbp
13554558Sbp	ifp->if_resolvemulti = 0;
13654558Sbp	ifp->if_type = IFT_XETHER;
13754558Sbp	ifp->if_flags |= IFF_RUNNING;
13854558Sbp
13954558Sbp	ifa1 = ifnet_addrs[ifp->if_index - 1];
14054558Sbp	ifa2 = ifnet_addrs[sc->ef_ifp->if_index - 1];
14154558Sbp	sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
14254558Sbp	sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
14354558Sbp	sdl1->sdl_type = IFT_ETHER;
14454558Sbp	sdl1->sdl_alen = ETHER_ADDR_LEN;
14554558Sbp	bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
14654558Sbp	bcopy(LLADDR(sdl2), sc->ef_ac.ac_enaddr, ETHER_ADDR_LEN);
14754558Sbp
14854558Sbp	EFDEBUG("%s%d: attached\n", ifp->if_name, ifp->if_unit);
14954558Sbp	return 1;
15054558Sbp}
15154558Sbp
15254558Sbp/*
15354558Sbp * This is for _testing_only_, just removes interface from interfaces list
15454558Sbp */
15554558Sbpstatic int
15654558Sbpef_detach(struct efnet *sc)
15754558Sbp{
15854558Sbp	struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
15954558Sbp	int s;
16054558Sbp
16154558Sbp	s = splimp();
16254558Sbp
16354558Sbp	if (ifp->if_flags & IFF_UP) {
16454558Sbp		if_down(ifp);
16554558Sbp		if (ifp->if_flags & IFF_RUNNING) {
16654558Sbp		    /* find internet addresses and delete routes */
16754558Sbp		    register struct ifaddr *ifa;
16854558Sbp		    for (ifa = ifp->if_addrhead.tqh_first; ifa;
16954558Sbp			 ifa = ifa->ifa_link.tqe_next) {
17054558Sbp			    rtinit(ifa, (int)RTM_DELETE, 0);
17154558Sbp		    }
17254558Sbp		}
17354558Sbp	}
17454558Sbp
17554558Sbp	TAILQ_REMOVE(&ifnet, ifp, if_link);
17654558Sbp	splx(s);
17754558Sbp	return 0;
17854558Sbp}
17954558Sbp
18054558Sbpstatic void
18154558Sbpef_init(void *foo) {
18254558Sbp	return;
18354558Sbp}
18454558Sbp
18554558Sbpstatic int
18654558Sbpef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
18754558Sbp{
18854558Sbp/*	struct ef_link *sc = (struct ef_link*)ifp->if_softc;*/
18954558Sbp	struct ifaddr *ifa = (struct ifaddr*)data;
19054558Sbp	int s, error;
19154558Sbp
19254558Sbp	EFDEBUG("IOCTL %ld for %s%d\n", cmd, ifp->if_name, ifp->if_unit);
19354558Sbp	error = 0;
19454558Sbp	s = splimp();
19554558Sbp	switch (cmd) {
19654558Sbp	    case SIOCSIFADDR:
19754558Sbp		if (ifp->if_unit == ETHER_FT_8023 &&
19854558Sbp		    ifa->ifa_addr->sa_family != AF_IPX) {
19954558Sbp			error = EAFNOSUPPORT;
20054558Sbp			break;
20154558Sbp		}
20254558Sbp		ifp->if_flags |= IFF_UP;
20354558Sbp		/* FALL THROUGH */
20454558Sbp	    case SIOCGIFADDR:
20554558Sbp	    case SIOCSIFMTU:
20654558Sbp		error = ether_ioctl(ifp, cmd, data);
20754558Sbp		break;
20854558Sbp	    case SIOCSIFFLAGS:
20954558Sbp		error = 0;
21054558Sbp		break;
21154558Sbp	    default:
21254558Sbp		error = EINVAL;
21354558Sbp	}
21454558Sbp	splx(s);
21554558Sbp	return error;
21654558Sbp}
21754558Sbp
21854558Sbp/*
21954558Sbp * Currently packet prepared in the ether_output(), but this can be a better
22054558Sbp * place.
22154558Sbp */
22254558Sbpstatic void
22354558Sbpef_start(struct ifnet *ifp)
22454558Sbp{
22554558Sbp	struct efnet *sc = (struct efnet*)ifp->if_softc;
22654558Sbp	struct ifnet *p;
22754558Sbp	struct mbuf *m;
22854558Sbp
22954558Sbp	ifp->if_flags |= IFF_OACTIVE;
23054558Sbp	p = sc->ef_ifp;
23154558Sbp
23254558Sbp	EFDEBUG("\n");
23354558Sbp	for (;;) {
23454558Sbp		IF_DEQUEUE(&ifp->if_snd, m);
23554558Sbp		if (m == 0)
23654558Sbp			break;
23754558Sbp		if (ifp->if_bpf)
23854558Sbp			bpf_mtap(ifp, m);
23954558Sbp		if (IF_QFULL(&p->if_snd)) {
24054558Sbp			IF_DROP(&p->if_snd);
24159681Sbp			ifp->if_oerrors++;
24259681Sbp			m_freem(m);
24359681Sbp			continue;
24454558Sbp		}
24554558Sbp		IF_ENQUEUE(&p->if_snd, m);
24659681Sbp		if ((p->if_flags & IFF_OACTIVE) == 0) {
24754558Sbp			p->if_start(p);
24859681Sbp			ifp->if_opackets++;
24959681Sbp		}
25054558Sbp	}
25154558Sbp	ifp->if_flags &= ~IFF_OACTIVE;
25254558Sbp	return;
25354558Sbp}
25454558Sbp
25554558Sbp/*
25654558Sbp * Inline functions do not put additional overhead to procedure call or
25754558Sbp * parameter passing but simplify the code
25854558Sbp */
25954558Sbpstatic int __inline
26054558Sbpef_inputEII(struct mbuf *m, struct ether_header *eh, struct llc* l,
26154558Sbp	u_short ether_type, struct ifqueue **inq)
26254558Sbp{
26354558Sbp	switch(ether_type) {
26454558Sbp#ifdef IPX
26554558Sbp	    case ETHERTYPE_IPX:
26654558Sbp		schednetisr(NETISR_IPX);
26754558Sbp		*inq = &ipxintrq;
26854558Sbp		break;
26954558Sbp#endif
27054558Sbp#ifdef INET
27154558Sbp	    case ETHERTYPE_IP:
27254558Sbp		if (ipflow_fastforward(m))
27354558Sbp			return 1;
27454558Sbp		schednetisr(NETISR_IP);
27554558Sbp		*inq = &ipintrq;
27654558Sbp		break;
27754558Sbp
27854558Sbp	    case ETHERTYPE_ARP:
27954558Sbp		schednetisr(NETISR_ARP);
28054558Sbp		*inq = &arpintrq;
28154558Sbp		break;
28254558Sbp#endif
28359681Sbp	    default:
28459681Sbp		return EPROTONOSUPPORT;
28554558Sbp	}
28654558Sbp	return 0;
28754558Sbp}
28854558Sbp
28954558Sbpstatic int __inline
29054558Sbpef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
29154558Sbp	u_short ether_type, struct ifqueue **inq)
29254558Sbp{
29354558Sbp	switch(ether_type) {
29454558Sbp#ifdef IPX
29554558Sbp	    case ETHERTYPE_IPX:
29654558Sbp		m_adj(m, 8);
29754558Sbp		schednetisr(NETISR_IPX);
29854558Sbp		*inq = &ipxintrq;
29954558Sbp		break;
30054558Sbp#endif
30159681Sbp	    default:
30259681Sbp		return EPROTONOSUPPORT;
30354558Sbp	}
30454558Sbp	return 0;
30554558Sbp}
30654558Sbp
30754558Sbpstatic int __inline
30854558Sbpef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
30954558Sbp	u_short ether_type, struct ifqueue **inq)
31054558Sbp{
31154558Sbp	switch(ether_type) {
31254558Sbp#ifdef IPX
31354558Sbp	    case 0xe0:
31454558Sbp		m_adj(m, 3);
31554558Sbp		schednetisr(NETISR_IPX);
31654558Sbp		*inq = &ipxintrq;
31754558Sbp		break;
31854558Sbp#endif
31959681Sbp	    default:
32059681Sbp		return EPROTONOSUPPORT;
32154558Sbp	}
32254558Sbp	return 0;
32354558Sbp}
32454558Sbp/*
32554558Sbp * Called from ether_input()
32654558Sbp */
32754558Sbpstatic int
32854558Sbpef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
32954558Sbp{
33054558Sbp	u_short ether_type;
33154558Sbp	int s, ft = -1;
33254558Sbp	struct ifqueue *inq;
33354558Sbp	struct efnet *efp;
33454558Sbp	struct ifnet *eifp;
33554558Sbp	struct llc *l;
33654558Sbp	struct ef_link *efl;
33754558Sbp
33854558Sbp	ether_type = ntohs(eh->ether_type);
33954558Sbp	if (ether_type < ETHERMTU) {
34054558Sbp		l = mtod(m, struct llc*);
34154558Sbp		if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
34254558Sbp			/*
34354558Sbp			 * Novell's "802.3" frame
34454558Sbp			 */
34554558Sbp			ft = ETHER_FT_8023;
34654558Sbp		} else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
34754558Sbp			/*
34854558Sbp			 * 802.2/SNAP
34954558Sbp			 */
35054558Sbp			ft = ETHER_FT_SNAP;
35154558Sbp			ether_type = ntohs(l->llc_un.type_snap.ether_type);
35254558Sbp		} else if (l->llc_dsap == l->llc_ssap) {
35354558Sbp			/*
35454558Sbp			 * 802.3/802.2
35554558Sbp			 */
35654558Sbp			ft = ETHER_FT_8022;
35754558Sbp			ether_type = l->llc_ssap;
35854558Sbp		}
35954558Sbp	} else
36054558Sbp		ft = ETHER_FT_EII;
36154558Sbp
36254558Sbp	if (ft == -1) {
36354558Sbp		EFDEBUG("Unrecognised ether_type %x\n", ether_type);
36459681Sbp		return EPROTONOSUPPORT;
36554558Sbp	}
36654558Sbp
36754558Sbp	/*
36854558Sbp	 * Check if interface configured for the given frame
36954558Sbp	 */
37054558Sbp	efp = NULL;
37154558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
37254558Sbp		if (efl->el_ifp == ifp) {
37354558Sbp			efp = efl->el_units[ft];
37454558Sbp			break;
37554558Sbp		}
37654558Sbp	}
37754558Sbp	if (efp == NULL) {
37854558Sbp		EFDEBUG("Can't find if for %d\n", ft);
37959681Sbp		return EPROTONOSUPPORT;
38054558Sbp	}
38154558Sbp	eifp = &efp->ef_ac.ac_if;
38254558Sbp	if ((eifp->if_flags & IFF_UP) == 0)
38359681Sbp		return EPROTONOSUPPORT;
38454558Sbp	eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
38554558Sbp	m->m_pkthdr.rcvif = eifp;
38654558Sbp
38754558Sbp	if (eifp->if_bpf) {
38854558Sbp		struct mbuf m0;
38954558Sbp		m0.m_next = m;
39054558Sbp		m0.m_len = sizeof(struct ether_header);
39154558Sbp		m0.m_data = (char *)eh;
39254558Sbp		bpf_mtap(eifp, &m0);
39354558Sbp	}
39454558Sbp	/*
39554558Sbp	 * Now we ready to adjust mbufs and pass them to protocol intr's
39654558Sbp	 */
39754558Sbp	inq = NULL;
39854558Sbp	switch(ft) {
39954558Sbp	    case ETHER_FT_EII:
40059681Sbp		if (ef_inputEII(m, eh, l, ether_type, &inq) != 0)
40159681Sbp			return EPROTONOSUPPORT;
40254558Sbp		break;
40354558Sbp#ifdef IPX
40454558Sbp	    case ETHER_FT_8023:		/* only IPX can be here */
40554558Sbp		schednetisr(NETISR_IPX);
40654558Sbp		inq = &ipxintrq;
40754558Sbp		break;
40854558Sbp#endif
40954558Sbp	    case ETHER_FT_SNAP:
41059681Sbp		if (ef_inputSNAP(m, eh, l, ether_type, &inq) != 0)
41159681Sbp			return EPROTONOSUPPORT;
41254558Sbp		break;
41354558Sbp	    case ETHER_FT_8022:
41459681Sbp		if (ef_input8022(m, eh, l, ether_type, &inq) != 0)
41559681Sbp			return EPROTONOSUPPORT;
41654558Sbp		break;
41754558Sbp	}
41854558Sbp
41954558Sbp	if (inq == NULL) {
42054558Sbp		EFDEBUG("No support for frame %d and proto %04x\n",
42154558Sbp			ft, ether_type);
42259681Sbp		return EPROTONOSUPPORT;
42354558Sbp	}
42454558Sbp	s = splimp();
42554558Sbp	if (IF_QFULL(inq)) {
42654558Sbp		IF_DROP(inq);
42754558Sbp		m_freem(m);
42854558Sbp	} else
42954558Sbp		IF_ENQUEUE(inq, m);
43054558Sbp	splx(s);
43154558Sbp	return 0;
43254558Sbp}
43354558Sbp
43454558Sbpstatic int
43559681Sbpef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp)
43654558Sbp{
43759681Sbp	struct mbuf *m = *mp;
43854558Sbp	u_char *cp;
43954558Sbp	short type;
44054558Sbp
44154558Sbp	if (ifp->if_type != IFT_XETHER)
44259681Sbp		return ENETDOWN;
44354558Sbp	switch (ifp->if_unit) {
44454558Sbp	    case ETHER_FT_EII:
44554558Sbp#ifdef IPX
44654558Sbp		type = htons(ETHERTYPE_IPX);
44754558Sbp#else
44859681Sbp		return EPFNOSUPPORT;
44954558Sbp#endif
45054558Sbp		break;
45154558Sbp	    case ETHER_FT_8023:
45254558Sbp		type = htons(m->m_pkthdr.len);
45354558Sbp		break;
45454558Sbp	    case ETHER_FT_8022:
45559681Sbp		M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAIT);
45659681Sbp		if (m == NULL) {
45759681Sbp			*mp = NULL;
45859681Sbp			return ENOBUFS;
45959681Sbp		}
46059681Sbp		/*
46159681Sbp		 * Ensure that ethernet header and next three bytes
46259681Sbp		 * will fit into single mbuf
46359681Sbp		 */
46459681Sbp		m = m_pullup(m, ETHER_HDR_LEN + 3);
46559681Sbp		if (m == NULL) {
46659681Sbp			*mp = NULL;
46759681Sbp			return ENOBUFS;
46859681Sbp		}
46959681Sbp		m_adj(m, ETHER_HDR_LEN);
47054558Sbp		type = htons(m->m_pkthdr.len);
47154558Sbp		cp = mtod(m, u_char *);
47254558Sbp		*cp++ = 0xE0;
47354558Sbp		*cp++ = 0xE0;
47454558Sbp		*cp++ = 0x03;
47554558Sbp		break;
47654558Sbp	    case ETHER_FT_SNAP:
47754558Sbp		M_PREPEND(m, 8, M_WAIT);
47859681Sbp		if (m == NULL) {
47959681Sbp			*mp = NULL;
48059681Sbp			return ENOBUFS;
48159681Sbp		}
48254558Sbp		type = htons(m->m_pkthdr.len);
48354558Sbp		cp = mtod(m, u_char *);
48454558Sbp		bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
48554558Sbp		break;
48654558Sbp	    default:
48759681Sbp		return EPFNOSUPPORT;
48854558Sbp	}
48959681Sbp	*mp = m;
49054558Sbp	*tp = type;
49154558Sbp	return 0;
49254558Sbp}
49354558Sbp
49454558Sbp/*
49554558Sbp * Create clone from the given interface
49654558Sbp */
49754558Sbpstatic int
49854558Sbpef_clone(struct ef_link *efl, int ft)
49954558Sbp{
50054558Sbp	struct efnet *efp;
50154558Sbp	struct ifnet *eifp;
50254558Sbp	struct ifnet *ifp = efl->el_ifp;
50354558Sbp	char cbuf[IFNAMSIZ], *ifname;
50454558Sbp	int ifnlen;
50554558Sbp
50654558Sbp	efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR, M_WAITOK);
50754558Sbp	if (efp == NULL)
50854558Sbp		return ENOMEM;
50954558Sbp	bzero(efp, sizeof(*efp));
51054558Sbp	efp->ef_ifp = ifp;
51154558Sbp	eifp = &efp->ef_ac.ac_if;
51254558Sbp	ifnlen = 1 + snprintf(cbuf, sizeof(cbuf), "%s%df", ifp->if_name,
51354558Sbp	    ifp->if_unit);
51454558Sbp	ifname = (char*)malloc(ifnlen, M_IFADDR, M_WAITOK);
51554558Sbp	eifp->if_name = strcpy(ifname, cbuf);
51654558Sbp	eifp->if_unit = ft;
51754558Sbp	eifp->if_softc = efp;
51854558Sbp	if (ifp->if_ioctl)
51954558Sbp		eifp->if_ioctl = ef_ioctl;
52054558Sbp	efl->el_units[ft] = efp;
52154558Sbp	return 0;
52254558Sbp}
52354558Sbp
52454558Sbpstatic int
52554558Sbpef_load(void)
52654558Sbp{
52754558Sbp	struct ifnet *ifp;
52854558Sbp	struct efnet *efp;
52954558Sbp	struct ef_link *efl = NULL;
53054558Sbp	int error = 0, d;
53154558Sbp
53254558Sbp	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
53354558Sbp		if (ifp->if_type != IFT_ETHER) continue;
53454558Sbp		EFDEBUG("Found interface %s%d\n", ifp->if_name, ifp->if_unit);
53554558Sbp		efl = (struct ef_link*)malloc(sizeof(struct ef_link),
53654558Sbp		    M_IFADDR, M_WAITOK);
53754558Sbp		if (efl == NULL) {
53854558Sbp			error = ENOMEM;
53954558Sbp			break;
54054558Sbp		}
54154558Sbp		bzero(efl, sizeof(*efl));
54254558Sbp
54354558Sbp		efl->el_ifp = ifp;
54454558Sbp#ifdef ETHER_II
54554558Sbp		error = ef_clone(efl, ETHER_FT_EII);
54654558Sbp		if (error) break;
54754558Sbp#endif
54854558Sbp#ifdef ETHER_8023
54954558Sbp		error = ef_clone(efl, ETHER_FT_8023);
55054558Sbp		if (error) break;
55154558Sbp#endif
55254558Sbp#ifdef ETHER_8022
55354558Sbp		error = ef_clone(efl, ETHER_FT_8022);
55454558Sbp		if (error) break;
55554558Sbp#endif
55654558Sbp#ifdef ETHER_SNAP
55754558Sbp		error = ef_clone(efl, ETHER_FT_SNAP);
55854558Sbp		if (error) break;
55954558Sbp#endif
56054558Sbp		efcount++;
56154558Sbp		SLIST_INSERT_HEAD(&efdev, efl, el_next);
56254558Sbp	}
56354558Sbp	if (error) {
56454558Sbp		if (efl)
56554558Sbp			SLIST_INSERT_HEAD(&efdev, efl, el_next);
56654558Sbp		SLIST_FOREACH(efl, &efdev, el_next) {
56754558Sbp			for (d = 0; d < EF_NFT; d++)
56854558Sbp				if (efl->el_units[d])
56954558Sbp					free(efl->el_units[d], M_IFADDR);
57054558Sbp			free(efl, M_IFADDR);
57154558Sbp		}
57254558Sbp		return error;
57354558Sbp	}
57454558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
57554558Sbp		for (d = 0; d < EF_NFT; d++) {
57654558Sbp			efp = efl->el_units[d];
57754558Sbp			if (efp)
57854558Sbp				ef_attach(efp);
57954558Sbp		}
58054558Sbp	}
58154558Sbp	ef_inputp = ef_input;
58254558Sbp	ef_outputp = ef_output;
58354558Sbp	EFDEBUG("Loaded\n");
58454558Sbp	return 0;
58554558Sbp}
58654558Sbp
58754558Sbpstatic int
58854558Sbpef_unload(void)
58954558Sbp{
59054558Sbp	struct efnet *efp;
59154558Sbp	struct ef_link *efl;
59254558Sbp	int d;
59354558Sbp
59454558Sbp	ef_inputp = NULL;
59554558Sbp	ef_outputp = NULL;
59654558Sbp	SLIST_FOREACH(efl, &efdev, el_next) {
59754558Sbp		for (d = 0; d < EF_NFT; d++) {
59854558Sbp			efp = efl->el_units[d];
59954558Sbp			if (efp) {
60054558Sbp				ef_detach(efp);
60154558Sbp			}
60254558Sbp		}
60354558Sbp	}
60454558Sbp	EFDEBUG("Unloaded\n");
60554558Sbp	return 0;
60654558Sbp}
60754558Sbp
60854558Sbpstatic int
60954558Sbpif_ef_modevent(module_t mod, int type, void *data)
61054558Sbp{
61154558Sbp	switch ((modeventtype_t)type) {
61254558Sbp	    case MOD_LOAD:
61354558Sbp		return ef_load();
61454558Sbp	    case MOD_UNLOAD:
61554558Sbp		return ef_unload();
61654558Sbp	    default:
61754558Sbp		break;
61854558Sbp	}
61954558Sbp	return 0;
62054558Sbp}
62154558Sbp
62254558Sbpstatic moduledata_t if_ef_mod = {
62354558Sbp	"if_ef", if_ef_modevent, NULL
62454558Sbp};
62554558Sbp
62654558SbpDECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);
627