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