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