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