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