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