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