463503f5023507197146dd0c301043702d617ac8
[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_gettoken();
371                 if (eifp->if_bpf)
372                         bpf_ptap(eifp->if_bpf, m, eh, ETHER_HDR_LEN);
373                 bpf_reltoken();
374         }
375
376         /*
377          * Now we ready to adjust mbufs and pass them to protocol intr's
378          */
379         switch(ft) {
380         case ETHER_FT_EII:
381                 return (ef_inputEII(m, l, ether_type));
382                 break;
383 #ifdef IPX
384         case ETHER_FT_8023:             /* only IPX can be here */
385                 isr = NETISR_IPX;
386                 break;
387 #endif
388         case ETHER_FT_SNAP:
389                 return (ef_inputSNAP(m, l, ether_type));
390                 break;
391         case ETHER_FT_8022:
392                 return (ef_input8022(m, l, ether_type));
393                 break;
394         default:
395                 EFDEBUG("No support for frame %d and proto %04x\n",
396                         ft, ether_type);
397                 return (EPROTONOSUPPORT);
398         }
399         netisr_queue(isr, m);
400         return (0);
401 }
402
403 static int
404 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
405         int *hlen)
406 {
407         struct efnet *sc = (struct efnet*)ifp->if_softc;
408         struct mbuf *m = *mp;
409         u_char *cp;
410         short type;
411
412         if (ifp->if_type != IFT_XETHER)
413                 return ENETDOWN;
414         switch (sc->ef_frametype) {
415             case ETHER_FT_EII:
416 #ifdef IPX
417                 type = htons(ETHERTYPE_IPX);
418 #else
419                 return EPFNOSUPPORT;
420 #endif
421                 break;
422             case ETHER_FT_8023:
423                 type = htons(m->m_pkthdr.len);
424                 break;
425             case ETHER_FT_8022:
426                 M_PREPEND(m, ETHER_HDR_LEN + 3, MB_WAIT);
427                 if (m == NULL) {
428                         *mp = NULL;
429                         return ENOBUFS;
430                 }
431                 /*
432                  * Ensure that ethernet header and next three bytes
433                  * will fit into single mbuf
434                  */
435                 m = m_pullup(m, ETHER_HDR_LEN + 3);
436                 if (m == NULL) {
437                         *mp = NULL;
438                         return ENOBUFS;
439                 }
440                 m_adj(m, ETHER_HDR_LEN);
441                 type = htons(m->m_pkthdr.len);
442                 cp = mtod(m, u_char *);
443                 *cp++ = 0xE0;
444                 *cp++ = 0xE0;
445                 *cp++ = 0x03;
446                 *hlen += 3;
447                 break;
448             case ETHER_FT_SNAP:
449                 M_PREPEND(m, 8, MB_WAIT);
450                 if (m == NULL) {
451                         *mp = NULL;
452                         return ENOBUFS;
453                 }
454                 type = htons(m->m_pkthdr.len);
455                 cp = mtod(m, u_char *);
456                 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
457                 *hlen += 8;
458                 break;
459             default:
460                 return EPFNOSUPPORT;
461         }
462         *mp = m;
463         *tp = type;
464         return 0;
465 }
466
467 /*
468  * Create clone from the given interface
469  */
470 static int
471 ef_clone(struct ef_link *efl, int ft)
472 {
473         struct efnet *efp;
474         struct ifnet *eifp;
475         struct ifnet *ifp = efl->el_ifp;
476
477         efp = (struct efnet*)kmalloc(sizeof(struct efnet), M_IFADDR,
478             M_WAITOK | M_ZERO);
479         efp->ef_ifp = ifp;
480         efp->ef_frametype = ft;
481         eifp = &efp->ef_ac.ac_if;
482         ksnprintf(eifp->if_xname, IFNAMSIZ,
483             "%sf%d", ifp->if_xname, efp->ef_frametype);
484         eifp->if_dname = "ef";
485         eifp->if_dunit = IF_DUNIT_NONE;
486         eifp->if_softc = efp;
487         if (ifp->if_ioctl)
488                 eifp->if_ioctl = ef_ioctl;
489         efl->el_units[ft] = efp;
490         return 0;
491 }
492
493 static int
494 ef_load(void)
495 {
496         struct ifnet *ifp;
497         struct efnet *efp;
498         struct ef_link *efl = NULL;
499         int error = 0, d;
500
501         TAILQ_FOREACH(ifp, &ifnet, if_link) {
502                 if (ifp->if_type != IFT_ETHER) continue;
503                 EFDEBUG("Found interface %s\n", ifp->if_xname);
504                 efl = (struct ef_link*)kmalloc(sizeof(struct ef_link), 
505                     M_IFADDR, M_WAITOK | M_ZERO);
506
507                 efl->el_ifp = ifp;
508 #ifdef ETHER_II
509                 error = ef_clone(efl, ETHER_FT_EII);
510                 if (error) break;
511 #endif
512 #ifdef ETHER_8023
513                 error = ef_clone(efl, ETHER_FT_8023);
514                 if (error) break;
515 #endif
516 #ifdef ETHER_8022
517                 error = ef_clone(efl, ETHER_FT_8022);
518                 if (error) break;
519 #endif
520 #ifdef ETHER_SNAP
521                 error = ef_clone(efl, ETHER_FT_SNAP);
522                 if (error) break;
523 #endif
524                 efcount++;
525                 SLIST_INSERT_HEAD(&efdev, efl, el_next);
526         }
527         if (error) {
528                 if (efl)
529                         SLIST_INSERT_HEAD(&efdev, efl, el_next);
530                 SLIST_FOREACH(efl, &efdev, el_next) {
531                         for (d = 0; d < EF_NFT; d++)
532                                 if (efl->el_units[d])
533                                         kfree(efl->el_units[d], M_IFADDR);
534                         kfree(efl, M_IFADDR);
535                 }
536                 return error;
537         }
538         SLIST_FOREACH(efl, &efdev, el_next) {
539                 for (d = 0; d < EF_NFT; d++) {
540                         efp = efl->el_units[d];
541                         if (efp)
542                                 ef_attach(efp);
543                 }
544         }
545         ef_inputp = ef_input;
546         ef_outputp = ef_output;
547         EFDEBUG("Loaded\n");
548         return 0;
549 }
550
551 static int
552 ef_unload(void)
553 {
554         struct efnet *efp;
555         struct ef_link *efl;
556         int d;
557
558         ef_inputp = NULL;
559         ef_outputp = NULL;
560         SLIST_FOREACH(efl, &efdev, el_next) {
561                 for (d = 0; d < EF_NFT; d++) {
562                         efp = efl->el_units[d];
563                         if (efp) {
564                                 ef_detach(efp);
565                         }
566                 }
567         }
568         EFDEBUG("Unloaded\n");
569         return 0;
570 }
571
572 static int 
573 if_ef_modevent(module_t mod, int type, void *data)
574 {
575         switch ((modeventtype_t)type) {
576             case MOD_LOAD:
577                 return ef_load();
578             case MOD_UNLOAD:
579                 return ef_unload();
580             default:
581                 break;
582         }
583         return 0;
584 }
585
586 static moduledata_t if_ef_mod = {
587         "if_ef", if_ef_modevent, NULL
588 };
589
590 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);