BPF has been in the kernel for ages and is supported by all NICs but I4B.
[dragonfly.git] / sys / net / ef / if_ef.c
1 /*-
2  * Copyright (c) 1999, 2000 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
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.8 2004/03/14 15:36:54 joerg Exp $
28  */
29
30 #include "opt_inet.h"
31 #include "opt_ipx.h"
32 #include "opt_ef.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sockio.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/syslog.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43
44 #include <net/ethernet.h>
45 #include <net/if_llc.h>
46 #include <net/if.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>
52 #include <net/bpf.h>
53
54 #ifdef INET
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/if_ether.h>
58 #endif
59
60 #ifdef IPX
61 #include <netproto/ipx/ipx.h>
62 #include <netproto/ipx/ipx_if.h>
63 #endif
64
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 */
71
72 #ifdef EF_DEBUG
73 #define EFDEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
74 #else
75 #define EFDEBUG(format, args...)
76 #endif
77
78 #define EFERROR(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
79
80 struct efnet {
81         struct arpcom   ef_ac;
82         struct ifnet *  ef_ifp;
83         int             ef_frametype;
84 };
85
86 struct ef_link {
87         SLIST_ENTRY(ef_link) el_next;
88         struct ifnet    *el_ifp;                /* raw device for this clones */
89         struct efnet    *el_units[EF_NFT];      /* our clones */
90 };
91
92 static SLIST_HEAD(ef_link_head, ef_link) efdev = {NULL};
93 static int efcount;
94
95 extern int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
96 extern int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
97                 struct sockaddr *dst, short *tp, int *hlen);
98
99 /*
100 static void ef_reset (struct ifnet *);
101 */
102 static int ef_attach(struct efnet *sc);
103 static int ef_detach(struct efnet *sc);
104 static void ef_init(void *);
105 static int ef_ioctl(struct ifnet *, u_long, caddr_t);
106 static void ef_start(struct ifnet *);
107 static int ef_input(struct ifnet*, struct ether_header *, struct mbuf *);
108 static int ef_output(struct ifnet *ifp, struct mbuf **mp,
109                 struct sockaddr *dst, short *tp, int *hlen);
110
111 static int ef_load(void);
112 static int ef_unload(void);
113
114 /*
115  * Install the interface, most of structure initialization done in ef_clone()
116  */
117 static int
118 ef_attach(struct efnet *sc)
119 {
120         struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
121         struct ifaddr *ifa;
122         struct sockaddr_dl *sdl;
123
124         ifp->if_output = ether_output;
125         ifp->if_start = ef_start;
126         ifp->if_watchdog = NULL;
127         ifp->if_init = ef_init;
128         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
129         ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
130         /*
131          * Attach the interface
132          */
133         ifa = ifnet_addrs[sc->ef_ifp->if_index];
134         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
135         ether_ifattach(ifp, LLADDR(sdl));
136
137         ifp->if_resolvemulti = 0;
138         ifp->if_type = IFT_XETHER;
139         ifp->if_flags |= IFF_RUNNING;
140
141         EFDEBUG("%s: attached\n", ifp->if_xname);
142         return 1;
143 }
144
145 /*
146  * This is for _testing_only_, just removes interface from interfaces list
147  */
148 static int
149 ef_detach(struct efnet *sc)
150 {
151         struct ifnet *ifp = (struct ifnet*)&sc->ef_ac.ac_if;
152         int s;
153
154         s = splimp();
155
156         if (ifp->if_flags & IFF_UP) {
157                 if_down(ifp);
158                 if (ifp->if_flags & IFF_RUNNING) {
159                     /* find internet addresses and delete routes */
160                     struct ifaddr *ifa;
161                     TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
162                             rtinit(ifa, (int)RTM_DELETE, 0);
163                     }
164                 }
165         }
166
167         TAILQ_REMOVE(&ifnet, ifp, if_link);
168         splx(s);
169         return 0;
170 }
171
172 static void
173 ef_init(void *foo) {
174         return;
175 }
176
177 static int
178 ef_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
179 {
180         struct efnet *sc = ifp->if_softc;
181         struct ifaddr *ifa = (struct ifaddr*)data;
182         int s, error;
183
184         EFDEBUG("IOCTL %ld for %s\n", cmd, ifp->if_xname);
185         error = 0;
186         s = splimp();
187         switch (cmd) {
188             case SIOCSIFADDR:
189                 if (sc->ef_frametype == ETHER_FT_8023 && 
190                     ifa->ifa_addr->sa_family != AF_IPX) {
191                         error = EAFNOSUPPORT;
192                         break;
193                 }
194                 ifp->if_flags |= IFF_UP; 
195                 /* FALL THROUGH */
196             case SIOCGIFADDR:
197             case SIOCSIFMTU:
198                 error = ether_ioctl(ifp, cmd, data);
199                 break;
200             case SIOCSIFFLAGS:
201                 error = 0;
202                 break;
203             default:
204                 error = EINVAL;
205         }
206         splx(s);
207         return error;
208 }
209
210 /*
211  * Currently packet prepared in the ether_output(), but this can be a better
212  * place.
213  */
214 static void
215 ef_start(struct ifnet *ifp)
216 {
217         struct efnet *sc = (struct efnet*)ifp->if_softc;
218         struct ifnet *p;
219         struct mbuf *m;
220
221         ifp->if_flags |= IFF_OACTIVE;
222         p = sc->ef_ifp;
223
224         EFDEBUG("\n");
225         for (;;) {
226                 IF_DEQUEUE(&ifp->if_snd, m);
227                 if (m == 0)
228                         break;
229                 if (ifp->if_bpf)
230                         bpf_mtap(ifp, m);
231                 if (IF_QFULL(&p->if_snd)) {
232                         IF_DROP(&p->if_snd);
233                         ifp->if_oerrors++;
234                         m_freem(m);
235                         continue;
236                 }
237                 IF_ENQUEUE(&p->if_snd, m);
238                 if ((p->if_flags & IFF_OACTIVE) == 0) {
239                         p->if_start(p);
240                         ifp->if_opackets++;
241                 }
242         }
243         ifp->if_flags &= ~IFF_OACTIVE;
244         return;
245 }
246
247 /*
248  * Inline functions do not put additional overhead to procedure call or
249  * parameter passing but simplify the code
250  */
251 static int __inline
252 ef_inputEII(struct mbuf *m, struct ether_header *eh, struct llc* l,
253         u_short ether_type)
254 {
255         int isr;
256
257         switch(ether_type) {
258 #ifdef IPX
259         case ETHERTYPE_IPX:
260                 isr = NETISR_IPX;
261                 break;
262 #endif
263 #ifdef INET
264         case ETHERTYPE_IP:
265                 if (ipflow_fastforward(m))
266                         return (0);
267                 isr = NETISR_IP;
268                 break;
269         case ETHERTYPE_ARP:
270                 isr = NETISR_ARP;
271                 break;
272 #endif
273         default:
274                 return (EPROTONOSUPPORT);
275         }
276         netisr_dispatch(isr, m);
277         return (0);
278 }
279
280 static int __inline
281 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
282         u_short ether_type)
283 {
284         int isr;
285
286         switch(ether_type) {
287 #ifdef IPX
288         case ETHERTYPE_IPX:
289                 m_adj(m, 8);
290                 isr = NETISR_IPX;
291                 break;
292 #endif
293         default:
294                 return (EPROTONOSUPPORT);
295         }
296         netisr_dispatch(isr, m);
297         return (0);
298 }
299
300 static int __inline
301 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
302         u_short ether_type)
303 {
304         int isr;
305
306         switch(ether_type) {
307 #ifdef IPX
308         case 0xe0:
309                 m_adj(m, 3);
310                 isr = NETISR_IPX;
311                 break;
312 #endif
313         default:
314                 return (EPROTONOSUPPORT);
315         }
316         netisr_dispatch(isr, m);
317         return (0);
318 }
319
320 /*
321  * Called from ether_input()
322  */
323 static int
324 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
325 {
326         u_short ether_type;
327         int ft = -1;
328         struct efnet *efp;
329         struct ifnet *eifp;
330         struct llc *l;
331         struct ef_link *efl;
332         int isr;
333
334         ether_type = ntohs(eh->ether_type);
335         if (ether_type < ETHERMTU) {
336                 l = mtod(m, struct llc*);
337                 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
338                         /* 
339                          * Novell's "802.3" frame
340                          */
341                         ft = ETHER_FT_8023;
342                 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
343                         /*
344                          * 802.2/SNAP
345                          */
346                         ft = ETHER_FT_SNAP;
347                         ether_type = ntohs(l->llc_un.type_snap.ether_type);
348                 } else if (l->llc_dsap == l->llc_ssap) {
349                         /*
350                          * 802.3/802.2
351                          */
352                         ft = ETHER_FT_8022;
353                         ether_type = l->llc_ssap;
354                 }
355         } else
356                 ft = ETHER_FT_EII;
357
358         if (ft == -1) {
359                 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
360                 return EPROTONOSUPPORT;
361         }
362
363         /*
364          * Check if interface configured for the given frame
365          */
366         efp = NULL;
367         SLIST_FOREACH(efl, &efdev, el_next) {
368                 if (efl->el_ifp == ifp) {
369                         efp = efl->el_units[ft];
370                         break;
371                 }
372         }
373         if (efp == NULL) {
374                 EFDEBUG("Can't find if for %d\n", ft);
375                 return EPROTONOSUPPORT;
376         }
377         eifp = &efp->ef_ac.ac_if;
378         if ((eifp->if_flags & IFF_UP) == 0)
379                 return EPROTONOSUPPORT;
380         eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
381         m->m_pkthdr.rcvif = eifp;
382
383         if (eifp->if_bpf) {
384                 struct mbuf m0;
385                 m0.m_next = m;
386                 m0.m_len = sizeof(struct ether_header);
387                 m0.m_data = (char *)eh;
388                 bpf_mtap(eifp, &m0);
389         }
390         /*
391          * Now we ready to adjust mbufs and pass them to protocol intr's
392          */
393         switch(ft) {
394         case ETHER_FT_EII:
395                 return (ef_inputEII(m, eh, l, ether_type));
396                 break;
397 #ifdef IPX
398         case ETHER_FT_8023:             /* only IPX can be here */
399                 isr = NETISR_IPX;
400                 break;
401 #endif
402         case ETHER_FT_SNAP:
403                 return (ef_inputSNAP(m, eh, l, ether_type));
404                 break;
405         case ETHER_FT_8022:
406                 return (ef_input8022(m, eh, l, ether_type));
407                 break;
408         default:
409                 EFDEBUG("No support for frame %d and proto %04x\n",
410                         ft, ether_type);
411                 return (EPROTONOSUPPORT);
412         }
413         netisr_dispatch(isr, m);
414         return (0);
415 }
416
417 static int
418 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
419         int *hlen)
420 {
421         struct efnet *sc = (struct efnet*)ifp->if_softc;
422         struct mbuf *m = *mp;
423         u_char *cp;
424         short type;
425
426         if (ifp->if_type != IFT_XETHER)
427                 return ENETDOWN;
428         switch (sc->ef_frametype) {
429             case ETHER_FT_EII:
430 #ifdef IPX
431                 type = htons(ETHERTYPE_IPX);
432 #else
433                 return EPFNOSUPPORT;
434 #endif
435                 break;
436             case ETHER_FT_8023:
437                 type = htons(m->m_pkthdr.len);
438                 break;
439             case ETHER_FT_8022:
440                 M_PREPEND(m, ETHER_HDR_LEN + 3, M_WAIT);
441                 if (m == NULL) {
442                         *mp = NULL;
443                         return ENOBUFS;
444                 }
445                 /*
446                  * Ensure that ethernet header and next three bytes
447                  * will fit into single mbuf
448                  */
449                 m = m_pullup(m, ETHER_HDR_LEN + 3);
450                 if (m == NULL) {
451                         *mp = NULL;
452                         return ENOBUFS;
453                 }
454                 m_adj(m, ETHER_HDR_LEN);
455                 type = htons(m->m_pkthdr.len);
456                 cp = mtod(m, u_char *);
457                 *cp++ = 0xE0;
458                 *cp++ = 0xE0;
459                 *cp++ = 0x03;
460                 *hlen += 3;
461                 break;
462             case ETHER_FT_SNAP:
463                 M_PREPEND(m, 8, M_WAIT);
464                 if (m == NULL) {
465                         *mp = NULL;
466                         return ENOBUFS;
467                 }
468                 type = htons(m->m_pkthdr.len);
469                 cp = mtod(m, u_char *);
470                 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
471                 *hlen += 8;
472                 break;
473             default:
474                 return EPFNOSUPPORT;
475         }
476         *mp = m;
477         *tp = type;
478         return 0;
479 }
480
481 /*
482  * Create clone from the given interface
483  */
484 static int
485 ef_clone(struct ef_link *efl, int ft)
486 {
487         struct efnet *efp;
488         struct ifnet *eifp;
489         struct ifnet *ifp = efl->el_ifp;
490
491         efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
492             M_WAITOK | M_ZERO);
493         if (efp == NULL)
494                 return ENOMEM;
495         efp->ef_ifp = ifp;
496         efp->ef_frametype = ft;
497         eifp = &efp->ef_ac.ac_if;
498         snprintf(eifp->if_xname, IFNAMSIZ,
499             "%sf%d", ifp->if_xname, efp->ef_frametype);
500         eifp->if_dname = "ef";
501         eifp->if_dunit = IF_DUNIT_NONE;
502         eifp->if_softc = efp;
503         if (ifp->if_ioctl)
504                 eifp->if_ioctl = ef_ioctl;
505         efl->el_units[ft] = efp;
506         return 0;
507 }
508
509 static int
510 ef_load(void)
511 {
512         struct ifnet *ifp;
513         struct efnet *efp;
514         struct ef_link *efl = NULL;
515         int error = 0, d;
516
517         TAILQ_FOREACH(ifp, &ifnet, if_link) {
518                 if (ifp->if_type != IFT_ETHER) continue;
519                 EFDEBUG("Found interface %s\n", ifp->if_xname);
520                 efl = (struct ef_link*)malloc(sizeof(struct ef_link), 
521                     M_IFADDR, M_WAITOK);
522                 if (efl == NULL) {
523                         error = ENOMEM;
524                         break;
525                 }
526                 bzero(efl, sizeof(*efl));
527
528                 efl->el_ifp = ifp;
529 #ifdef ETHER_II
530                 error = ef_clone(efl, ETHER_FT_EII);
531                 if (error) break;
532 #endif
533 #ifdef ETHER_8023
534                 error = ef_clone(efl, ETHER_FT_8023);
535                 if (error) break;
536 #endif
537 #ifdef ETHER_8022
538                 error = ef_clone(efl, ETHER_FT_8022);
539                 if (error) break;
540 #endif
541 #ifdef ETHER_SNAP
542                 error = ef_clone(efl, ETHER_FT_SNAP);
543                 if (error) break;
544 #endif
545                 efcount++;
546                 SLIST_INSERT_HEAD(&efdev, efl, el_next);
547         }
548         if (error) {
549                 if (efl)
550                         SLIST_INSERT_HEAD(&efdev, efl, el_next);
551                 SLIST_FOREACH(efl, &efdev, el_next) {
552                         for (d = 0; d < EF_NFT; d++)
553                                 if (efl->el_units[d])
554                                         free(efl->el_units[d], M_IFADDR);
555                         free(efl, M_IFADDR);
556                 }
557                 return error;
558         }
559         SLIST_FOREACH(efl, &efdev, el_next) {
560                 for (d = 0; d < EF_NFT; d++) {
561                         efp = efl->el_units[d];
562                         if (efp)
563                                 ef_attach(efp);
564                 }
565         }
566         ef_inputp = ef_input;
567         ef_outputp = ef_output;
568         EFDEBUG("Loaded\n");
569         return 0;
570 }
571
572 static int
573 ef_unload(void)
574 {
575         struct efnet *efp;
576         struct ef_link *efl;
577         int d;
578
579         ef_inputp = NULL;
580         ef_outputp = NULL;
581         SLIST_FOREACH(efl, &efdev, el_next) {
582                 for (d = 0; d < EF_NFT; d++) {
583                         efp = efl->el_units[d];
584                         if (efp) {
585                                 ef_detach(efp);
586                         }
587                 }
588         }
589         EFDEBUG("Unloaded\n");
590         return 0;
591 }
592
593 static int 
594 if_ef_modevent(module_t mod, int type, void *data)
595 {
596         switch ((modeventtype_t)type) {
597             case MOD_LOAD:
598                 return ef_load();
599             case MOD_UNLOAD:
600                 return ef_unload();
601             default:
602                 break;
603         }
604         return 0;
605 }
606
607 static moduledata_t if_ef_mod = {
608         "if_ef", if_ef_modevent, NULL
609 };
610
611 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);