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