2 * Copyright (c) 1999, 2000 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sockio.h>
36 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/syslog.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/vimage.h>
44 #include <net/ethernet.h>
45 #include <net/if_llc.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/netisr.h>
51 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/if_ether.h>
61 #include <netipx/ipx.h>
62 #include <netipx/ipx_if.h>
65 /* If none of the supported layers is enabled explicitly enable them all */
66 #if !defined(ETHER_II) && !defined(ETHER_8023) && !defined(ETHER_8022) && \
74 /* internal frame types */
75 #define ETHER_FT_EII 0 /* Ethernet_II - default */
76 #define ETHER_FT_8023 1 /* 802.3 (Novell) */
77 #define ETHER_FT_8022 2 /* 802.2 */
78 #define ETHER_FT_SNAP 3 /* SNAP */
79 #define EF_NFT 4 /* total number of frame types */
82 #define EFDEBUG(format, args...) printf("%s: "format, __func__ ,## args)
84 #define EFDEBUG(format, args...)
87 #define EFERROR(format, args...) printf("%s: "format, __func__ ,## args)
91 struct ifnet *ef_pifp;
96 SLIST_ENTRY(ef_link) el_next;
97 struct ifnet *el_ifp; /* raw device for this clones */
98 struct efnet *el_units[EF_NFT]; /* our clones */
101 static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
104 extern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
105 extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
106 struct sockaddr *dst, short *tp, int *hlen);
109 static void ef_reset (struct ifnet *);
111 static int ef_attach(struct efnet *sc);
112 static int ef_detach(struct efnet *sc);
113 static void ef_init(void *);
114 static int ef_ioctl(struct ifnet *, u_long, caddr_t);
115 static void ef_start(struct ifnet *);
116 static int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
117 static int ef_output(struct ifnet *ifp, struct mbuf **mp,
118 struct sockaddr *dst, short *tp, int *hlen);
120 static int ef_load(void);
121 static int ef_unload(void);
124 * Install the interface, most of structure initialization done in ef_clone()
127 ef_attach(struct efnet *sc)
129 struct ifnet *ifp = sc->ef_ifp;
131 ifp->if_start = ef_start;
132 ifp->if_watchdog = NULL;
133 ifp->if_init = ef_init;
134 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
135 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
137 * Attach the interface
139 ether_ifattach(ifp, IF_LLADDR(sc->ef_pifp));
141 ifp->if_resolvemulti = 0;
142 ifp->if_type = IFT_XETHER;
143 ifp->if_drv_flags |= IFF_DRV_RUNNING;
145 EFDEBUG("%s: attached\n", ifp->if_xname);
150 * This is for _testing_only_, just removes interface from interfaces list
153 ef_detach(struct efnet *sc)
155 struct ifnet *ifp = sc->ef_ifp;
173 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
175 struct efnet *sc = ifp->if_softc;
176 struct ifaddr *ifa = (struct ifaddr*)data;
179 EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
187 if (sc->ef_frametype == ETHER_FT_8023 &&
188 ifa->ifa_addr->sa_family != AF_IPX) {
189 error = EAFNOSUPPORT;
192 ifp->if_flags |= IFF_UP;
195 error = ether_ioctl(ifp, cmd, data);
203 * Currently packet prepared in the ether_output(), but this can be a better
207 ef_start(struct ifnet *ifp)
209 struct efnet *sc = (struct efnet*)ifp->if_softc;
214 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
219 IF_DEQUEUE(&ifp->if_snd, m);
223 IFQ_HANDOFF(p, m, error);
230 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
235 * Inline functions do not put additional overhead to procedure call or
236 * parameter passing but simplify the code
239 ef_inputEII(struct mbuf *m, struct ether_header *eh, u_short ether_type)
251 if ((m = ip_fastforward(m)) == NULL)
261 return (EPROTONOSUPPORT);
263 netisr_dispatch(isr, m);
268 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
281 return (EPROTONOSUPPORT);
283 netisr_dispatch(isr, m);
288 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
301 return (EPROTONOSUPPORT);
303 netisr_dispatch(isr, m);
308 * Called from ether_input()
311 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
321 ether_type = ntohs(eh->ether_type);
323 if (ether_type < ETHERMTU) {
324 l = mtod(m, struct llc*);
325 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
327 * Novell's "802.3" frame
330 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
335 ether_type = ntohs(l->llc_un.type_snap.ether_type);
336 } else if (l->llc_dsap == l->llc_ssap) {
341 ether_type = l->llc_ssap;
347 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
348 return EPROTONOSUPPORT;
352 * Check if interface configured for the given frame
355 SLIST_FOREACH(efl, &efdev, el_next) {
356 if (efl->el_ifp == ifp) {
357 efp = efl->el_units[ft];
362 EFDEBUG("Can't find if for %d\n", ft);
363 return EPROTONOSUPPORT;
366 if ((eifp->if_flags & IFF_UP) == 0)
367 return EPROTONOSUPPORT;
368 eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
369 m->m_pkthdr.rcvif = eifp;
371 BPF_MTAP2(eifp, eh, ETHER_HDR_LEN, m);
373 * Now we ready to adjust mbufs and pass them to protocol intr's
377 return (ef_inputEII(m, eh, ether_type));
379 case ETHER_FT_8023: /* only IPX can be here */
384 return (ef_inputSNAP(m, eh, l, ether_type));
386 return (ef_input8022(m, eh, l, ether_type));
388 EFDEBUG("No support for frame %d and proto %04x\n",
390 return (EPROTONOSUPPORT);
392 netisr_dispatch(isr, m);
397 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
400 struct efnet *sc = (struct efnet*)ifp->if_softc;
401 struct mbuf *m = *mp;
405 if (ifp->if_type != IFT_XETHER)
407 switch (sc->ef_frametype) {
410 type = htons(ETHERTYPE_IPX);
416 type = htons(m->m_pkthdr.len);
419 M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAIT);
421 * Ensure that ethernet header and next three bytes
422 * will fit into single mbuf
424 m = m_pullup(m, ETHER_HDR_LEN + 3);
429 m_adj(m, ETHER_HDR_LEN);
430 type = htons(m->m_pkthdr.len);
431 cp = mtod(m, u_char *);
438 M_PREPEND(m, 8, M_WAIT);
439 type = htons(m->m_pkthdr.len);
440 cp = mtod(m, u_char *);
441 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
453 * Create clone from the given interface
456 ef_clone(struct ef_link *efl, int ft)
460 struct ifnet *ifp = efl->el_ifp;
462 efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
467 efp->ef_frametype = ft;
468 eifp = efp->ef_ifp = if_alloc(IFT_ETHER);
473 snprintf(eifp->if_xname, IFNAMSIZ,
474 "%sf%d", ifp->if_xname, efp->ef_frametype);
475 eifp->if_dname = "ef";
476 eifp->if_dunit = IF_DUNIT_NONE;
477 eifp->if_softc = efp;
479 eifp->if_ioctl = ef_ioctl;
480 efl->el_units[ft] = efp;
487 VNET_ITERATOR_DECL(vnet_iter);
490 struct ef_link *efl = NULL, *efl_temp;
494 VNET_FOREACH(vnet_iter) {
495 CURVNET_SET(vnet_iter);
496 INIT_VNET_NET(vnet_iter);
498 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
499 if (ifp->if_type != IFT_ETHER) continue;
500 EFDEBUG("Found interface %s\n", ifp->if_xname);
501 efl = (struct ef_link*)malloc(sizeof(struct ef_link),
502 M_IFADDR, M_WAITOK | M_ZERO);
510 error = ef_clone(efl, ETHER_FT_EII);
514 error = ef_clone(efl, ETHER_FT_8023);
518 error = ef_clone(efl, ETHER_FT_8022);
522 error = ef_clone(efl, ETHER_FT_SNAP);
526 SLIST_INSERT_HEAD(&efdev, efl, el_next);
534 SLIST_INSERT_HEAD(&efdev, efl, el_next);
535 SLIST_FOREACH_SAFE(efl, &efdev, el_next, efl_temp) {
536 for (d = 0; d < EF_NFT; d++)
537 if (efl->el_units[d]) {
538 if (efl->el_units[d]->ef_pifp != NULL)
539 if_free(efl->el_units[d]->ef_pifp);
540 free(efl->el_units[d], M_IFADDR);
546 SLIST_FOREACH(efl, &efdev, el_next) {
547 for (d = 0; d < EF_NFT; d++) {
548 efp = efl->el_units[d];
553 ef_inputp = ef_input;
554 ef_outputp = ef_output;
568 SLIST_FOREACH(efl, &efdev, el_next) {
569 for (d = 0; d < EF_NFT; d++) {
570 efp = efl->el_units[d];
576 EFDEBUG("Unloaded\n");
581 if_ef_modevent(module_t mod, int type, void *data)
583 switch ((modeventtype_t)type) {
594 static moduledata_t if_ef_mod = {
595 "if_ef", if_ef_modevent, NULL
598 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);