Merge from vendor branch OPENSSL:
[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.11 2004/07/23 07:16:30 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                 if (ifp->if_bpf)
229                         bpf_mtap(ifp, m);
230                 if (IF_QFULL(&p->if_snd)) {
231                         IF_DROP(&p->if_snd);
232                         ifp->if_oerrors++;
233                         m_freem(m);
234                         continue;
235                 }
236                 IF_ENQUEUE(&p->if_snd, m);
237                 if ((p->if_flags & IFF_OACTIVE) == 0) {
238                         p->if_start(p);
239                         ifp->if_opackets++;
240                 }
241         }
242         ifp->if_flags &= ~IFF_OACTIVE;
243         return;
244 }
245
246 /*
247  * Inline functions do not put additional overhead to procedure call or
248  * parameter passing but simplify the code
249  */
250 static int __inline
251 ef_inputEII(struct mbuf *m, struct ether_header *eh, struct llc* l,
252         u_short ether_type)
253 {
254         int isr;
255
256         switch(ether_type) {
257 #ifdef IPX
258         case ETHERTYPE_IPX:
259                 isr = NETISR_IPX;
260                 break;
261 #endif
262 #ifdef INET
263         case ETHERTYPE_IP:
264                 if (ipflow_fastforward(m))
265                         return (0);
266                 isr = NETISR_IP;
267                 break;
268         case ETHERTYPE_ARP:
269                 isr = NETISR_ARP;
270                 break;
271 #endif
272         default:
273                 return (EPROTONOSUPPORT);
274         }
275         netisr_dispatch(isr, m);
276         return (0);
277 }
278
279 static int __inline
280 ef_inputSNAP(struct mbuf *m, struct ether_header *eh, struct llc* l,
281         u_short ether_type)
282 {
283         int isr;
284
285         switch(ether_type) {
286 #ifdef IPX
287         case ETHERTYPE_IPX:
288                 m_adj(m, 8);
289                 isr = NETISR_IPX;
290                 break;
291 #endif
292         default:
293                 return (EPROTONOSUPPORT);
294         }
295         netisr_dispatch(isr, m);
296         return (0);
297 }
298
299 static int __inline
300 ef_input8022(struct mbuf *m, struct ether_header *eh, struct llc* l,
301         u_short ether_type)
302 {
303         int isr;
304
305         switch(ether_type) {
306 #ifdef IPX
307         case 0xe0:
308                 m_adj(m, 3);
309                 isr = NETISR_IPX;
310                 break;
311 #endif
312         default:
313                 return (EPROTONOSUPPORT);
314         }
315         netisr_dispatch(isr, m);
316         return (0);
317 }
318
319 /*
320  * Called from ether_input()
321  */
322 static int
323 ef_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
324 {
325         u_short ether_type;
326         int ft = -1;
327         struct efnet *efp;
328         struct ifnet *eifp;
329         struct llc *l;
330         struct ef_link *efl;
331         int isr;
332
333         ether_type = ntohs(eh->ether_type);
334         if (ether_type < ETHERMTU) {
335                 l = mtod(m, struct llc*);
336                 if (l->llc_dsap == 0xff && l->llc_ssap == 0xff) {
337                         /* 
338                          * Novell's "802.3" frame
339                          */
340                         ft = ETHER_FT_8023;
341                 } else if (l->llc_dsap == 0xaa && l->llc_ssap == 0xaa) {
342                         /*
343                          * 802.2/SNAP
344                          */
345                         ft = ETHER_FT_SNAP;
346                         ether_type = ntohs(l->llc_un.type_snap.ether_type);
347                 } else if (l->llc_dsap == l->llc_ssap) {
348                         /*
349                          * 802.3/802.2
350                          */
351                         ft = ETHER_FT_8022;
352                         ether_type = l->llc_ssap;
353                 }
354         } else
355                 ft = ETHER_FT_EII;
356
357         if (ft == -1) {
358                 EFDEBUG("Unrecognised ether_type %x\n", ether_type);
359                 return EPROTONOSUPPORT;
360         }
361
362         /*
363          * Check if interface configured for the given frame
364          */
365         efp = NULL;
366         SLIST_FOREACH(efl, &efdev, el_next) {
367                 if (efl->el_ifp == ifp) {
368                         efp = efl->el_units[ft];
369                         break;
370                 }
371         }
372         if (efp == NULL) {
373                 EFDEBUG("Can't find if for %d\n", ft);
374                 return EPROTONOSUPPORT;
375         }
376         eifp = &efp->ef_ac.ac_if;
377         if ((eifp->if_flags & IFF_UP) == 0)
378                 return EPROTONOSUPPORT;
379         eifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
380         m->m_pkthdr.rcvif = eifp;
381
382         if (eifp->if_bpf) {
383                 struct mbuf m0;
384                 m0.m_next = m;
385                 m0.m_len = sizeof(struct ether_header);
386                 m0.m_data = (char *)eh;
387                 bpf_mtap(eifp, &m0);
388         }
389         /*
390          * Now we ready to adjust mbufs and pass them to protocol intr's
391          */
392         switch(ft) {
393         case ETHER_FT_EII:
394                 return (ef_inputEII(m, eh, l, ether_type));
395                 break;
396 #ifdef IPX
397         case ETHER_FT_8023:             /* only IPX can be here */
398                 isr = NETISR_IPX;
399                 break;
400 #endif
401         case ETHER_FT_SNAP:
402                 return (ef_inputSNAP(m, eh, l, ether_type));
403                 break;
404         case ETHER_FT_8022:
405                 return (ef_input8022(m, eh, l, ether_type));
406                 break;
407         default:
408                 EFDEBUG("No support for frame %d and proto %04x\n",
409                         ft, ether_type);
410                 return (EPROTONOSUPPORT);
411         }
412         netisr_dispatch(isr, m);
413         return (0);
414 }
415
416 static int
417 ef_output(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, short *tp,
418         int *hlen)
419 {
420         struct efnet *sc = (struct efnet*)ifp->if_softc;
421         struct mbuf *m = *mp;
422         u_char *cp;
423         short type;
424
425         if (ifp->if_type != IFT_XETHER)
426                 return ENETDOWN;
427         switch (sc->ef_frametype) {
428             case ETHER_FT_EII:
429 #ifdef IPX
430                 type = htons(ETHERTYPE_IPX);
431 #else
432                 return EPFNOSUPPORT;
433 #endif
434                 break;
435             case ETHER_FT_8023:
436                 type = htons(m->m_pkthdr.len);
437                 break;
438             case ETHER_FT_8022:
439                 M_PREPEND(m, ETHER_HDR_LEN + 3, MB_WAIT);
440                 if (m == NULL) {
441                         *mp = NULL;
442                         return ENOBUFS;
443                 }
444                 /*
445                  * Ensure that ethernet header and next three bytes
446                  * will fit into single mbuf
447                  */
448                 m = m_pullup(m, ETHER_HDR_LEN + 3);
449                 if (m == NULL) {
450                         *mp = NULL;
451                         return ENOBUFS;
452                 }
453                 m_adj(m, ETHER_HDR_LEN);
454                 type = htons(m->m_pkthdr.len);
455                 cp = mtod(m, u_char *);
456                 *cp++ = 0xE0;
457                 *cp++ = 0xE0;
458                 *cp++ = 0x03;
459                 *hlen += 3;
460                 break;
461             case ETHER_FT_SNAP:
462                 M_PREPEND(m, 8, MB_WAIT);
463                 if (m == NULL) {
464                         *mp = NULL;
465                         return ENOBUFS;
466                 }
467                 type = htons(m->m_pkthdr.len);
468                 cp = mtod(m, u_char *);
469                 bcopy("\xAA\xAA\x03\x00\x00\x00\x81\x37", cp, 8);
470                 *hlen += 8;
471                 break;
472             default:
473                 return EPFNOSUPPORT;
474         }
475         *mp = m;
476         *tp = type;
477         return 0;
478 }
479
480 /*
481  * Create clone from the given interface
482  */
483 static int
484 ef_clone(struct ef_link *efl, int ft)
485 {
486         struct efnet *efp;
487         struct ifnet *eifp;
488         struct ifnet *ifp = efl->el_ifp;
489
490         efp = (struct efnet*)malloc(sizeof(struct efnet), M_IFADDR,
491             M_WAITOK | M_ZERO);
492         if (efp == NULL)
493                 return ENOMEM;
494         efp->ef_ifp = ifp;
495         efp->ef_frametype = ft;
496         eifp = &efp->ef_ac.ac_if;
497         snprintf(eifp->if_xname, IFNAMSIZ,
498             "%sf%d", ifp->if_xname, efp->ef_frametype);
499         eifp->if_dname = "ef";
500         eifp->if_dunit = IF_DUNIT_NONE;
501         eifp->if_softc = efp;
502         if (ifp->if_ioctl)
503                 eifp->if_ioctl = ef_ioctl;
504         efl->el_units[ft] = efp;
505         return 0;
506 }
507
508 static int
509 ef_load(void)
510 {
511         struct ifnet *ifp;
512         struct efnet *efp;
513         struct ef_link *efl = NULL;
514         int error = 0, d;
515
516         TAILQ_FOREACH(ifp, &ifnet, if_link) {
517                 if (ifp->if_type != IFT_ETHER) continue;
518                 EFDEBUG("Found interface %s\n", ifp->if_xname);
519                 efl = (struct ef_link*)malloc(sizeof(struct ef_link), 
520                     M_IFADDR, M_WAITOK);
521                 if (efl == NULL) {
522                         error = ENOMEM;
523                         break;
524                 }
525                 bzero(efl, sizeof(*efl));
526
527                 efl->el_ifp = ifp;
528 #ifdef ETHER_II
529                 error = ef_clone(efl, ETHER_FT_EII);
530                 if (error) break;
531 #endif
532 #ifdef ETHER_8023
533                 error = ef_clone(efl, ETHER_FT_8023);
534                 if (error) break;
535 #endif
536 #ifdef ETHER_8022
537                 error = ef_clone(efl, ETHER_FT_8022);
538                 if (error) break;
539 #endif
540 #ifdef ETHER_SNAP
541                 error = ef_clone(efl, ETHER_FT_SNAP);
542                 if (error) break;
543 #endif
544                 efcount++;
545                 SLIST_INSERT_HEAD(&efdev, efl, el_next);
546         }
547         if (error) {
548                 if (efl)
549                         SLIST_INSERT_HEAD(&efdev, efl, el_next);
550                 SLIST_FOREACH(efl, &efdev, el_next) {
551                         for (d = 0; d < EF_NFT; d++)
552                                 if (efl->el_units[d])
553                                         free(efl->el_units[d], M_IFADDR);
554                         free(efl, M_IFADDR);
555                 }
556                 return error;
557         }
558         SLIST_FOREACH(efl, &efdev, el_next) {
559                 for (d = 0; d < EF_NFT; d++) {
560                         efp = efl->el_units[d];
561                         if (efp)
562                                 ef_attach(efp);
563                 }
564         }
565         ef_inputp = ef_input;
566         ef_outputp = ef_output;
567         EFDEBUG("Loaded\n");
568         return 0;
569 }
570
571 static int
572 ef_unload(void)
573 {
574         struct efnet *efp;
575         struct ef_link *efl;
576         int d;
577
578         ef_inputp = NULL;
579         ef_outputp = NULL;
580         SLIST_FOREACH(efl, &efdev, el_next) {
581                 for (d = 0; d < EF_NFT; d++) {
582                         efp = efl->el_units[d];
583                         if (efp) {
584                                 ef_detach(efp);
585                         }
586                 }
587         }
588         EFDEBUG("Unloaded\n");
589         return 0;
590 }
591
592 static int 
593 if_ef_modevent(module_t mod, int type, void *data)
594 {
595         switch ((modeventtype_t)type) {
596             case MOD_LOAD:
597                 return ef_load();
598             case MOD_UNLOAD:
599                 return ef_unload();
600             default:
601                 break;
602         }
603         return 0;
604 }
605
606 static moduledata_t if_ef_mod = {
607         "if_ef", if_ef_modevent, NULL
608 };
609
610 DECLARE_MODULE(if_ef, if_ef_mod, SI_SUB_PSEUDO, SI_ORDER_MIDDLE);