2 * Copyright (c) 1995, 1996
3 * Matt Thomas <matt@3am-software.com>. All rights reserved.
4 * Copyright (c) 1982, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * from: if_ethersubr.c,v 1.5 1994/12/13 22:31:45 wollman Exp
40 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
52 #include <net/if_var.h>
53 #include <net/if_dl.h>
54 #include <net/if_llc.h>
55 #include <net/if_types.h>
56 #include <net/if_llatbl.h>
58 #include <net/ethernet.h>
59 #include <net/netisr.h>
60 #include <net/route.h>
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
70 #include <netinet6/nd6.h>
74 #include <netdnet/dn.h>
77 #include <security/mac/mac_framework.h>
79 static const u_char fddibroadcastaddr[FDDI_ADDR_LEN] =
80 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
82 static int fddi_resolvemulti(struct ifnet *, struct sockaddr **,
84 static int fddi_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
86 static void fddi_input(struct ifnet *ifp, struct mbuf *m);
88 #define senderr(e) do { error = (e); goto bad; } while (0)
91 * FDDI output routine.
92 * Encapsulate a packet of type family for the local net.
93 * Use trailer local net encapsulation if enough data in first
94 * packet leaves a multiple of 512 bytes of data in remainder.
97 fddi_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
101 int loop_copy = 0, error = 0, hdrcmplt = 0;
102 u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN];
103 struct fddi_header *fh;
109 error = mac_ifnet_check_transmit(ifp, m);
114 if (ifp->if_flags & IFF_MONITOR)
116 if (!((ifp->if_flags & IFF_UP) &&
117 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
119 getmicrotime(&ifp->if_lastchange);
121 switch (dst->sa_family) {
125 if (ro != NULL && ro->ro_rt != NULL &&
126 (ro->ro_rt->rt_flags & RTF_GATEWAY) != 0)
128 error = arpresolve(ifp, is_gw, m, dst, edst, NULL);
130 return (error == EWOULDBLOCK ? 0 : error);
131 type = htons(ETHERTYPE_IP);
137 ah = mtod(m, struct arphdr *);
138 ah->ar_hrd = htons(ARPHRD_ETHER);
140 loop_copy = -1; /* if this is for us, don't do it */
142 switch (ntohs(ah->ar_op)) {
143 case ARPOP_REVREQUEST:
145 type = htons(ETHERTYPE_REVARP);
150 type = htons(ETHERTYPE_ARP);
154 if (m->m_flags & M_BCAST)
155 bcopy(ifp->if_broadcastaddr, edst, FDDI_ADDR_LEN);
157 bcopy(ar_tha(ah), edst, FDDI_ADDR_LEN);
164 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, NULL);
166 return (error); /* Something bad happened */
167 type = htons(ETHERTYPE_IPV6);
170 case pseudo_AF_HDRCMPLT:
172 const struct ether_header *eh;
175 eh = (const struct ether_header *)dst->sa_data;
176 bcopy(eh->ether_shost, esrc, FDDI_ADDR_LEN);
182 const struct ether_header *eh;
185 eh = (const struct ether_header *)dst->sa_data;
186 bcopy(eh->ether_dhost, edst, FDDI_ADDR_LEN);
188 m->m_flags |= (M_BCAST|M_MCAST);
189 type = eh->ether_type;
195 fh = mtod(m, struct fddi_header *);
196 error = EPROTONOSUPPORT;
197 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
198 case FDDIFC_LLC_ASYNC: {
199 /* legal priorities are 0 through 7 */
200 if ((fh->fddi_fc & FDDIFC_Z) > 7)
204 case FDDIFC_LLC_SYNC: {
205 /* FDDIFC_Z bits reserved, must be zero */
206 if (fh->fddi_fc & FDDIFC_Z)
211 /* FDDIFC_Z bits must be non zero */
212 if ((fh->fddi_fc & FDDIFC_Z) == 0)
217 /* anything else is too dangerous */
222 if (fh->fddi_dhost[0] & 1)
223 m->m_flags |= (M_BCAST|M_MCAST);
227 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
228 senderr(EAFNOSUPPORT);
236 M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
239 l = mtod(m, struct llc *);
240 l->llc_control = LLC_UI;
241 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
242 l->llc_snap.org_code[0] =
243 l->llc_snap.org_code[1] =
244 l->llc_snap.org_code[2] = 0;
245 l->llc_snap.ether_type = htons(type);
249 * Add local net header. If no space in first mbuf,
252 M_PREPEND(m, FDDI_HDR_LEN, M_NOWAIT);
255 fh = mtod(m, struct fddi_header *);
256 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
257 bcopy((caddr_t)edst, (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN);
260 bcopy((caddr_t)esrc, (caddr_t)fh->fddi_shost, FDDI_ADDR_LEN);
262 bcopy(IF_LLADDR(ifp), (caddr_t)fh->fddi_shost,
266 * If a simplex interface, and the packet is being sent to our
267 * Ethernet address or a broadcast address, loopback a copy.
268 * XXX To make a simplex device behave exactly like a duplex
269 * device, we should copy in the case of sending to our own
270 * ethernet address (thus letting the original actually appear
271 * on the wire). However, we don't do that here for security
272 * reasons and compatibility with the original behavior.
274 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
275 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
277 n = m_copy(m, 0, (int)M_COPYALL);
278 (void) if_simloop(ifp, n, dst->sa_family,
280 } else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
281 FDDI_ADDR_LEN) == 0) {
282 (void) if_simloop(ifp, m, dst->sa_family,
284 return (0); /* XXX */
288 error = (ifp->if_transmit)(ifp, m);
290 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
295 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
302 * Process a received FDDI packet.
311 struct fddi_header *fh;
314 * Do consistency checks to verify assumptions
315 * made by code past this point.
317 if ((m->m_flags & M_PKTHDR) == 0) {
318 if_printf(ifp, "discard frame w/o packet header\n");
319 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
323 if (m->m_pkthdr.rcvif == NULL) {
324 if_printf(ifp, "discard frame w/o interface pointer\n");
325 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
330 m = m_pullup(m, FDDI_HDR_LEN);
332 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
335 fh = mtod(m, struct fddi_header *);
338 * Discard packet if interface is not up.
340 if (!((ifp->if_flags & IFF_UP) &&
341 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
345 * Give bpf a chance at the packet.
350 * Interface marked for monitoring; discard packet.
352 if (ifp->if_flags & IFF_MONITOR) {
358 mac_ifnet_create_mbuf(ifp, m);
362 * Update interface statistics.
364 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
365 getmicrotime(&ifp->if_lastchange);
368 * Discard non local unicast packets when interface
369 * is in promiscuous mode.
371 if ((ifp->if_flags & IFF_PROMISC) && ((fh->fddi_dhost[0] & 1) == 0) &&
372 (bcmp(IF_LLADDR(ifp), (caddr_t)fh->fddi_dhost,
373 FDDI_ADDR_LEN) != 0))
377 * Set mbuf flags for bcast/mcast.
379 if (fh->fddi_dhost[0] & 1) {
380 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
382 m->m_flags |= M_BCAST;
384 m->m_flags |= M_MCAST;
385 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
390 * If this has a LLC priority of 0, then mark it so upper
391 * layers have a hint that it really came via a FDDI/Ethernet
394 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
395 m->m_flags |= M_LINK0;
398 /* Strip off FDDI header. */
399 m_adj(m, FDDI_HDR_LEN);
401 m = m_pullup(m, LLC_SNAPFRAMELEN);
403 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
406 l = mtod(m, struct llc *);
408 switch (l->llc_dsap) {
412 if ((l->llc_control != LLC_UI) ||
413 (l->llc_ssap != LLC_SNAP_LSAP)) {
414 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
417 if (l->llc_snap.org_code[0] != 0 ||
418 l->llc_snap.org_code[1] != 0 ||
419 l->llc_snap.org_code[2] != 0) {
420 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
424 type = ntohs(l->llc_snap.ether_type);
425 m_adj(m, LLC_SNAPFRAMELEN);
430 if ((m = ip_fastforward(m)) == NULL)
436 if (ifp->if_flags & IFF_NOARP)
447 case ETHERTYPE_DECNET:
452 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
453 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
460 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
461 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
464 M_SETFIB(m, ifp->if_fib);
465 netisr_dispatch(isr, m);
469 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
476 * Perform common duties while attaching to interface list
479 fddi_ifattach(ifp, lla, bpf)
485 struct sockaddr_dl *sdl;
487 ifp->if_type = IFT_FDDI;
488 ifp->if_addrlen = FDDI_ADDR_LEN;
491 if_attach(ifp); /* Must be called before additional assignments */
493 ifp->if_mtu = FDDIMTU;
494 ifp->if_output = fddi_output;
495 ifp->if_input = fddi_input;
496 ifp->if_resolvemulti = fddi_resolvemulti;
497 ifp->if_broadcastaddr = fddibroadcastaddr;
498 ifp->if_baudrate = 100000000;
499 #ifdef IFF_NOTRAILERS
500 ifp->if_flags |= IFF_NOTRAILERS;
503 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
505 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
506 sdl->sdl_type = IFT_FDDI;
507 sdl->sdl_alen = ifp->if_addrlen;
508 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
511 bpfattach(ifp, DLT_FDDI, FDDI_HDR_LEN);
517 fddi_ifdetach(ifp, bpf)
531 fddi_ioctl (ifp, command, data)
540 ifa = (struct ifaddr *) data;
541 ifr = (struct ifreq *) data;
546 ifp->if_flags |= IFF_UP;
548 switch (ifa->ifa_addr->sa_family) {
550 case AF_INET: /* before arpwhohas */
551 ifp->if_init(ifp->if_softc);
552 arp_ifinit(ifp, ifa);
556 ifp->if_init(ifp->if_softc);
563 sa = (struct sockaddr *) & ifr->ifr_data;
564 bcopy(IF_LLADDR(ifp),
565 (caddr_t) sa->sa_data, FDDI_ADDR_LEN);
571 * Set the interface MTU.
573 if (ifr->ifr_mtu > FDDIMTU) {
576 ifp->if_mtu = ifr->ifr_mtu;
588 fddi_resolvemulti(ifp, llsa, sa)
590 struct sockaddr **llsa;
593 struct sockaddr_dl *sdl;
595 struct sockaddr_in *sin;
598 struct sockaddr_in6 *sin6;
602 switch(sa->sa_family) {
605 * No mapping needed. Just check that it's a valid MC address.
607 sdl = (struct sockaddr_dl *)sa;
608 e_addr = LLADDR(sdl);
609 if ((e_addr[0] & 1) != 1)
610 return (EADDRNOTAVAIL);
616 sin = (struct sockaddr_in *)sa;
617 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
618 return (EADDRNOTAVAIL);
619 sdl = link_init_sdl(ifp, *llsa, IFT_FDDI);
621 sdl->sdl_alen = FDDI_ADDR_LEN;
623 e_addr = LLADDR(sdl);
624 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
625 *llsa = (struct sockaddr *)sdl;
630 sin6 = (struct sockaddr_in6 *)sa;
631 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
633 * An IP6 address of 0 means listen to all
634 * of the Ethernet multicast address used for IP6.
635 * (This is used for multicast routers.)
637 ifp->if_flags |= IFF_ALLMULTI;
641 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
642 return (EADDRNOTAVAIL);
643 sdl = link_init_sdl(ifp, *llsa, IFT_FDDI);
645 sdl->sdl_alen = FDDI_ADDR_LEN;
647 e_addr = LLADDR(sdl);
648 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
649 *llsa = (struct sockaddr *)sdl;
655 * Well, the text isn't quite right, but it's the name
658 return (EAFNOSUPPORT);
664 static moduledata_t fddi_mod = {
665 "fddi", /* module name */
666 NULL, /* event handler */
670 DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
671 MODULE_VERSION(fddi, 1);