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
26 * $FreeBSD: src/sys/net/if_ef.c,v 1.2.2.4 2001/02/22 09:27:04 bp Exp $
27 * $DragonFly: src/sys/net/ef/if_ef.c,v 1.5 2003/09/15 23:38:13 hsu Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sockio.h>
37 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/syslog.h>
41 #include <sys/kernel.h>
42 #include <sys/module.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 <netproto/ipx/ipx.h>
62 #include <netproto/ipx/ipx_if.h>
65 /* internal frame types */
66 #define ETHER_FT_EII 0 /* Ethernet_II - default */
67 #define ETHER_FT_8023 1 /* 802.3 (Novell) */
68 #define ETHER_FT_8022 2 /* 802.2 */
69 #define ETHER_FT_SNAP 3 /* SNAP */
70 #define EF_NFT 4 /* total number of frame types */
73 #define EFDEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
75 #define EFDEBUG(format, args...)
78 #define EFERROR(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
82 struct ifnet * ef_ifp;
86 SLIST_ENTRY(ef_link) el_next;
87 struct ifnet *el_ifp; /* raw device for this clones */
88 struct efnet *el_units[EF_NFT]; /* our clones */
91 static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
94 extern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
95 extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
96 struct sockaddr *dst, short *tp, int *hlen);
99 static void ef_reset (struct ifnet *);
101 static int ef_attach(struct efnet *sc);
102 static int ef_detach(struct efnet *sc);
103 static void ef_init(void *);
104 static int ef_ioctl(struct ifnet *, u_long, caddr_t);
105 static void ef_start(struct ifnet *);
106 static int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
107 static int ef_output(struct ifnet *ifp, struct mbuf **mp,
108 struct sockaddr *dst, short *tp, int *hlen);
110 static int ef_load(void);
111 static int ef_unload(void);
114 * Install the interface, most of structure initialization done in ef_clone()
117 ef_attach(struct efnet *sc)
119 struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
120 struct ifaddr *ifa1, *ifa2;
121 struct sockaddr_dl *sdl1, *sdl2;
123 ifp->if_output = ether_output;
124 ifp->if_start = ef_start;
125 ifp->if_watchdog = NULL;
126 ifp->if_init = ef_init;
127 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
128 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
130 * Attach the interface
132 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
134 ifp->if_resolvemulti = 0;
135 ifp->if_type = IFT_XETHER;
136 ifp->if_flags |= IFF_RUNNING;
138 ifa1 = ifnet_addrs[ifp->if_index - 1];
139 ifa2 = ifnet_addrs[sc->ef_ifp->if_index - 1];
140 sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
141 sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
142 sdl1->sdl_type = IFT_ETHER;
143 sdl1->sdl_alen = ETHER_ADDR_LEN;
144 bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
145 bcopy(LLADDR(sdl2), sc->ef_ac.ac_enaddr, ETHER_ADDR_LEN);
147 EFDEBUG("%s%d: attached\n", ifp->if_name, ifp->if_unit);
152 * This is for _testing_only_, just removes interface from interfaces list
155 ef_detach(struct efnet *sc)
157 struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
162 if (ifp->if_flags & IFF_UP) {
164 if (ifp->if_flags & IFF_RUNNING) {
165 /* find internet addresses and delete routes */
167 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
168 rtinit(ifa, (int)RTM_DELETE, 0);
173 TAILQ_REMOVE(&ifnet, ifp, if_link);
184 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
186 /* struct ef_link *sc = (struct ef_link*)ifp->if_softc;*/
187 struct ifaddr *ifa = (struct ifaddr*)data;
190 EFDEBUG("IOCTL %ld for %s%d\n", cmd, ifp->if_name, ifp->if_unit);
195 if (ifp->if_unit == ETHER_FT_8023 &&
196 ifa->ifa_addr->sa_family != AF_IPX) {
197 error = EAFNOSUPPORT;
200 ifp->if_flags |= IFF_UP;
204 error = ether_ioctl(ifp, cmd, data);
217 * Currently packet prepared in the ether_output(), but this can be a better
221 ef_start(struct ifnet *ifp)
223 struct efnet *sc = (struct efnet*)ifp->if_softc;
227 ifp->if_flags |= IFF_OACTIVE;
232 IF_DEQUEUE(&ifp->if_snd, m);
237 if (IF_QFULL(&p->if_snd)) {
243 IF_ENQUEUE(&p->if_snd, m);
244 if ((p->if_flags & IFF_OACTIVE) == 0) {
249 ifp->if_flags &= ~IFF_OACTIVE;
254 * Inline functions do not put additional overhead to procedure call or
255 * parameter passing but simplify the code
258 ef_inputEII(struct mbuf *m, struct ether_header *eh, struct llc* l,
271 if (ipflow_fastforward(m))
280 return (EPROTONOSUPPORT);
282 netisr_dispatch(isr, m);
287 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
300 return (EPROTONOSUPPORT);
302 netisr_dispatch(isr, m);
307 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
320 return (EPROTONOSUPPORT);
322 netisr_dispatch(isr, m);
327 * Called from ether_input()
330 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
340 ether_type = ntohs(eh->ether_type);
341 if (ether_type < ETHERMTU) {
342 l = mtod(m, struct llc*);
343 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
345 * Novell's "802.3" frame
348 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
353 ether_type = ntohs(l->llc_un.type_snap.ether_type);
354 } else if (l->llc_dsap == l->llc_ssap) {
359 ether_type = l->llc_ssap;
365 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
366 return EPROTONOSUPPORT;
370 * Check if interface configured for the given frame
373 SLIST_FOREACH(efl, &efdev, el_next) {
374 if (efl->el_ifp == ifp) {
375 efp = efl->el_units[ft];
380 EFDEBUG("Can't find if for %d\n", ft);
381 return EPROTONOSUPPORT;
383 eifp = &efp->ef_ac.ac_if;
384 if ((eifp->if_flags & IFF_UP) == 0)
385 return EPROTONOSUPPORT;
386 eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
387 m->m_pkthdr.rcvif = eifp;
392 m0.m_len = sizeof(struct ether_header);
393 m0.m_data = (char *)eh;
397 * Now we ready to adjust mbufs and pass them to protocol intr's
401 return (ef_inputEII(m, eh, l, ether_type));
404 case ETHER_FT_8023: /* only IPX can be here */
409 return (ef_inputSNAP(m, eh, l, ether_type));
412 return (ef_input8022(m, eh, l, ether_type));
415 EFDEBUG("No support for frame %d and proto %04x\n",
417 return (EPROTONOSUPPORT);
419 netisr_dispatch(isr, m);
424 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
427 struct mbuf *m = *mp;
431 if (ifp->if_type != IFT_XETHER)
433 switch (ifp->if_unit) {
436 type = htons(ETHERTYPE_IPX);
442 type = htons(m->m_pkthdr.len);
445 M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAIT);
451 * Ensure that ethernet header and next three bytes
452 * will fit into single mbuf
454 m = m_pullup(m, ETHER_HDR_LEN + 3);
459 m_adj(m, ETHER_HDR_LEN);
460 type = htons(m->m_pkthdr.len);
461 cp = mtod(m, u_char *);
468 M_PREPEND(m, 8, M_WAIT);
473 type = htons(m->m_pkthdr.len);
474 cp = mtod(m, u_char *);
475 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
487 * Create clone from the given interface
490 ef_clone(struct ef_link *efl, int ft)
494 struct ifnet *ifp = efl->el_ifp;
495 char cbuf[IFNAMSIZ], *ifname;
498 efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
503 eifp = &efp->ef_ac.ac_if;
504 ifnlen = 1 + snprintf(cbuf, sizeof(cbuf), "%s%df", ifp->if_name,
506 ifname = (char*)malloc(ifnlen, M_IFADDR, M_WAITOK);
507 eifp->if_name = strcpy(ifname, cbuf);
509 eifp->if_softc = efp;
511 eifp->if_ioctl = ef_ioctl;
512 efl->el_units[ft] = efp;
521 struct ef_link *efl = NULL;
524 TAILQ_FOREACH(ifp, &ifnet, if_link) {
525 if (ifp->if_type != IFT_ETHER) continue;
526 EFDEBUG("Found interface %s%d\n", ifp->if_name, ifp->if_unit);
527 efl = (struct ef_link*)malloc(sizeof(struct ef_link),
533 bzero(efl, sizeof(*efl));
537 error = ef_clone(efl, ETHER_FT_EII);
541 error = ef_clone(efl, ETHER_FT_8023);
545 error = ef_clone(efl, ETHER_FT_8022);
549 error = ef_clone(efl, ETHER_FT_SNAP);
553 SLIST_INSERT_HEAD(&efdev, efl, el_next);
557 SLIST_INSERT_HEAD(&efdev, efl, el_next);
558 SLIST_FOREACH(efl, &efdev, el_next) {
559 for (d = 0; d < EF_NFT; d++)
560 if (efl->el_units[d])
561 free(efl->el_units[d], M_IFADDR);
566 SLIST_FOREACH(efl, &efdev, el_next) {
567 for (d = 0; d < EF_NFT; d++) {
568 efp = efl->el_units[d];
573 ef_inputp = ef_input;
574 ef_outputp = ef_output;
588 SLIST_FOREACH(efl, &efdev, el_next) {
589 for (d = 0; d < EF_NFT; d++) {
590 efp = efl->el_units[d];
596 EFDEBUG("Unloaded\n");
601 if_ef_modevent(module_t mod, int type, void *data)
603 switch ((modeventtype_t)type) {
614 static moduledata_t if_ef_mod = {
615 "if_ef", if_ef_modevent, NULL
618 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);