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>
43 #include <net/ethernet.h>
44 #include <net/if_llc.h>
46 #include <net/if_arp.h>
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_var.h>
56 #include <netinet/if_ether.h>
60 #include <netipx/ipx.h>
61 #include <netipx/ipx_if.h>
64 /* If none of the supported layers is enabled explicitly enable them all */
65 #if !defined(ETHER_II) && !defined(ETHER_8023) && !defined(ETHER_8022) && \
73 /* internal frame types */
74 #define ETHER_FT_EII 0 /* Ethernet_II - default */
75 #define ETHER_FT_8023 1 /* 802.3 (Novell) */
76 #define ETHER_FT_8022 2 /* 802.2 */
77 #define ETHER_FT_SNAP 3 /* SNAP */
78 #define EF_NFT 4 /* total number of frame types */
81 #define EFDEBUG(format, args...) printf("%s: "format, __func__ ,## args)
83 #define EFDEBUG(format, args...)
86 #define EFERROR(format, args...) printf("%s: "format, __func__ ,## args)
90 struct ifnet *ef_pifp;
95 SLIST_ENTRY(ef_link) el_next;
96 struct ifnet *el_ifp; /* raw device for this clones */
97 struct efnet *el_units[EF_NFT]; /* our clones */
100 static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
103 extern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
104 extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
105 struct sockaddr *dst, short *tp, int *hlen);
108 static void ef_reset (struct ifnet *);
110 static int ef_attach(struct efnet *sc);
111 static int ef_detach(struct efnet *sc);
112 static void ef_init(void *);
113 static int ef_ioctl(struct ifnet *, u_long, caddr_t);
114 static void ef_start(struct ifnet *);
115 static int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
116 static int ef_output(struct ifnet *ifp, struct mbuf **mp,
117 struct sockaddr *dst, short *tp, int *hlen);
119 static int ef_load(void);
120 static int ef_unload(void);
123 * Install the interface, most of structure initialization done in ef_clone()
126 ef_attach(struct efnet *sc)
128 struct ifnet *ifp = sc->ef_ifp;
130 ifp->if_start = ef_start;
131 ifp->if_watchdog = NULL;
132 ifp->if_init = ef_init;
133 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
134 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
136 * Attach the interface
138 ether_ifattach(ifp, IF_LLADDR(sc->ef_pifp));
140 ifp->if_resolvemulti = 0;
141 ifp->if_type = IFT_XETHER;
142 ifp->if_drv_flags |= IFF_DRV_RUNNING;
144 EFDEBUG("%s: attached\n", ifp->if_xname);
149 * This is for _testing_only_, just removes interface from interfaces list
152 ef_detach(struct efnet *sc)
154 struct ifnet *ifp = sc->ef_ifp;
172 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
174 struct efnet *sc = ifp->if_softc;
175 struct ifaddr *ifa = (struct ifaddr*)data;
178 EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
186 if (sc->ef_frametype == ETHER_FT_8023 &&
187 ifa->ifa_addr->sa_family != AF_IPX) {
188 error = EAFNOSUPPORT;
191 ifp->if_flags |= IFF_UP;
194 error = ether_ioctl(ifp, cmd, data);
202 * Currently packet prepared in the ether_output(), but this can be a better
206 ef_start(struct ifnet *ifp)
208 struct efnet *sc = (struct efnet*)ifp->if_softc;
213 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
218 IF_DEQUEUE(&ifp->if_snd, m);
222 IFQ_HANDOFF(p, m, error);
229 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
234 * Inline functions do not put additional overhead to procedure call or
235 * parameter passing but simplify the code
238 ef_inputEII(struct mbuf *m, struct ether_header *eh, u_short ether_type)
250 if ((m = ip_fastforward(m)) == NULL)
260 return (EPROTONOSUPPORT);
262 netisr_dispatch(isr, m);
267 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
280 return (EPROTONOSUPPORT);
282 netisr_dispatch(isr, m);
287 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
300 return (EPROTONOSUPPORT);
302 netisr_dispatch(isr, m);
307 * Called from ether_input()
310 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
320 ether_type = ntohs(eh->ether_type);
322 if (ether_type < ETHERMTU) {
323 l = mtod(m, struct llc*);
324 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
326 * Novell's "802.3" frame
329 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
334 ether_type = ntohs(l->llc_un.type_snap.ether_type);
335 } else if (l->llc_dsap == l->llc_ssap) {
340 ether_type = l->llc_ssap;
346 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
347 return EPROTONOSUPPORT;
351 * Check if interface configured for the given frame
354 SLIST_FOREACH(efl, &efdev, el_next) {
355 if (efl->el_ifp == ifp) {
356 efp = efl->el_units[ft];
361 EFDEBUG("Can't find if for %d\n", ft);
362 return EPROTONOSUPPORT;
365 if ((eifp->if_flags & IFF_UP) == 0)
366 return EPROTONOSUPPORT;
367 eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
368 m->m_pkthdr.rcvif = eifp;
370 BPF_MTAP2(eifp, eh, ETHER_HDR_LEN, m);
372 * Now we ready to adjust mbufs and pass them to protocol intr's
376 return (ef_inputEII(m, eh, ether_type));
378 case ETHER_FT_8023: /* only IPX can be here */
383 return (ef_inputSNAP(m, eh, l, ether_type));
385 return (ef_input8022(m, eh, l, ether_type));
387 EFDEBUG("No support for frame %d and proto %04x\n",
389 return (EPROTONOSUPPORT);
391 netisr_dispatch(isr, m);
396 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
399 struct efnet *sc = (struct efnet*)ifp->if_softc;
400 struct mbuf *m = *mp;
404 if (ifp->if_type != IFT_XETHER)
406 switch (sc->ef_frametype) {
409 type = htons(ETHERTYPE_IPX);
415 type = htons(m->m_pkthdr.len);
418 M_PREPEND(m, ETHER_HDR_LEN + 3, M_TRYWAIT);
424 * Ensure that ethernet header and next three bytes
425 * will fit into single mbuf
427 m = m_pullup(m, ETHER_HDR_LEN + 3);
432 m_adj(m, ETHER_HDR_LEN);
433 type = htons(m->m_pkthdr.len);
434 cp = mtod(m, u_char *);
441 M_PREPEND(m, 8, M_TRYWAIT);
446 type = htons(m->m_pkthdr.len);
447 cp = mtod(m, u_char *);
448 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
460 * Create clone from the given interface
463 ef_clone(struct ef_link *efl, int ft)
467 struct ifnet *ifp = efl->el_ifp;
469 efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
474 efp->ef_frametype = ft;
475 eifp = efp->ef_ifp = if_alloc(IFT_ETHER);
480 snprintf(eifp->if_xname, IFNAMSIZ,
481 "%sf%d", ifp->if_xname, efp->ef_frametype);
482 eifp->if_dname = "ef";
483 eifp->if_dunit = IF_DUNIT_NONE;
484 eifp->if_softc = efp;
486 eifp->if_ioctl = ef_ioctl;
487 efl->el_units[ft] = efp;
496 struct ef_link *efl = NULL, *efl_temp;
500 TAILQ_FOREACH(ifp, &ifnet, if_link) {
501 if (ifp->if_type != IFT_ETHER) continue;
502 EFDEBUG("Found interface %s\n", ifp->if_xname);
503 efl = (struct ef_link*)malloc(sizeof(struct ef_link),
504 M_IFADDR, M_WAITOK | M_ZERO);
512 error = ef_clone(efl, ETHER_FT_EII);
516 error = ef_clone(efl, ETHER_FT_8023);
520 error = ef_clone(efl, ETHER_FT_8022);
524 error = ef_clone(efl, ETHER_FT_SNAP);
528 SLIST_INSERT_HEAD(&efdev, efl, el_next);
533 SLIST_INSERT_HEAD(&efdev, efl, el_next);
534 SLIST_FOREACH_SAFE(efl, &efdev, el_next, efl_temp) {
535 for (d = 0; d < EF_NFT; d++)
536 if (efl->el_units[d]) {
537 if (efl->el_units[d]->ef_pifp != NULL)
538 if_free(efl->el_units[d]->ef_pifp);
539 free(efl->el_units[d], M_IFADDR);
545 SLIST_FOREACH(efl, &efdev, el_next) {
546 for (d = 0; d < EF_NFT; d++) {
547 efp = efl->el_units[d];
552 ef_inputp = ef_input;
553 ef_outputp = ef_output;
567 SLIST_FOREACH(efl, &efdev, el_next) {
568 for (d = 0; d < EF_NFT; d++) {
569 efp = efl->el_units[d];
575 EFDEBUG("Unloaded\n");
580 if_ef_modevent(module_t mod, int type, void *data)
582 switch ((modeventtype_t)type) {
593 static moduledata_t if_ef_mod = {
594 "if_ef", if_ef_modevent, NULL
597 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);