2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1995, 1996
5 * Matt Thomas <matt@3am-software.com>. All rights reserved.
6 * Copyright (c) 1982, 1989, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: if_ethersubr.c,v 1.5 1994/12/13 22:31:45 wollman Exp
42 #include "opt_inet6.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
54 #include <net/if_var.h>
55 #include <net/if_dl.h>
56 #include <net/if_llc.h>
57 #include <net/if_types.h>
58 #include <net/if_llatbl.h>
60 #include <net/ethernet.h>
61 #include <net/netisr.h>
62 #include <net/route.h>
66 #if defined(INET) || defined(INET6)
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/if_ether.h>
72 #include <netinet6/nd6.h>
76 #include <netdnet/dn.h>
79 #include <security/mac/mac_framework.h>
81 static const u_char fddibroadcastaddr[FDDI_ADDR_LEN] =
82 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
84 static int fddi_resolvemulti(struct ifnet *, struct sockaddr **,
86 static int fddi_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
88 static void fddi_input(struct ifnet *ifp, struct mbuf *m);
90 #define senderr(e) do { error = (e); goto bad; } while (0)
93 * FDDI output routine.
94 * Encapsulate a packet of type family for the local net.
95 * Use trailer local net encapsulation if enough data in first
96 * packet leaves a multiple of 512 bytes of data in remainder.
99 fddi_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
103 int loop_copy = 0, error = 0, hdrcmplt = 0;
104 u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN];
105 struct fddi_header *fh;
106 #if defined(INET) || defined(INET6)
111 error = mac_ifnet_check_transmit(ifp, m);
116 if (ifp->if_flags & IFF_MONITOR)
118 if (!((ifp->if_flags & IFF_UP) &&
119 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
121 getmicrotime(&ifp->if_lastchange);
123 #if defined(INET) || defined(INET6)
125 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
128 switch (dst->sa_family) {
131 error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
133 return (error == EWOULDBLOCK ? 0 : error);
134 type = htons(ETHERTYPE_IP);
140 ah = mtod(m, struct arphdr *);
141 ah->ar_hrd = htons(ARPHRD_ETHER);
143 loop_copy = -1; /* if this is for us, don't do it */
145 switch (ntohs(ah->ar_op)) {
146 case ARPOP_REVREQUEST:
148 type = htons(ETHERTYPE_REVARP);
153 type = htons(ETHERTYPE_ARP);
157 if (m->m_flags & M_BCAST)
158 bcopy(ifp->if_broadcastaddr, edst, FDDI_ADDR_LEN);
160 bcopy(ar_tha(ah), edst, FDDI_ADDR_LEN);
167 error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
169 return (error == EWOULDBLOCK ? 0 : error);
170 type = htons(ETHERTYPE_IPV6);
173 case pseudo_AF_HDRCMPLT:
175 const struct ether_header *eh;
178 eh = (const struct ether_header *)dst->sa_data;
179 bcopy(eh->ether_shost, esrc, FDDI_ADDR_LEN);
185 const struct ether_header *eh;
188 eh = (const struct ether_header *)dst->sa_data;
189 bcopy(eh->ether_dhost, edst, FDDI_ADDR_LEN);
191 m->m_flags |= (M_BCAST|M_MCAST);
192 type = eh->ether_type;
198 fh = mtod(m, struct fddi_header *);
199 error = EPROTONOSUPPORT;
200 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
201 case FDDIFC_LLC_ASYNC: {
202 /* legal priorities are 0 through 7 */
203 if ((fh->fddi_fc & FDDIFC_Z) > 7)
207 case FDDIFC_LLC_SYNC: {
208 /* FDDIFC_Z bits reserved, must be zero */
209 if (fh->fddi_fc & FDDIFC_Z)
214 /* FDDIFC_Z bits must be non zero */
215 if ((fh->fddi_fc & FDDIFC_Z) == 0)
220 /* anything else is too dangerous */
225 if (fh->fddi_dhost[0] & 1)
226 m->m_flags |= (M_BCAST|M_MCAST);
230 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
231 senderr(EAFNOSUPPORT);
239 M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
242 l = mtod(m, struct llc *);
243 l->llc_control = LLC_UI;
244 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
245 l->llc_snap.org_code[0] =
246 l->llc_snap.org_code[1] =
247 l->llc_snap.org_code[2] = 0;
248 l->llc_snap.ether_type = htons(type);
252 * Add local net header. If no space in first mbuf,
255 M_PREPEND(m, FDDI_HDR_LEN, M_NOWAIT);
258 fh = mtod(m, struct fddi_header *);
259 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
260 bcopy((caddr_t)edst, (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN);
263 bcopy((caddr_t)esrc, (caddr_t)fh->fddi_shost, FDDI_ADDR_LEN);
265 bcopy(IF_LLADDR(ifp), (caddr_t)fh->fddi_shost,
269 * If a simplex interface, and the packet is being sent to our
270 * Ethernet address or a broadcast address, loopback a copy.
271 * XXX To make a simplex device behave exactly like a duplex
272 * device, we should copy in the case of sending to our own
273 * ethernet address (thus letting the original actually appear
274 * on the wire). However, we don't do that here for security
275 * reasons and compatibility with the original behavior.
277 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
278 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
280 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
281 (void) if_simloop(ifp, n, dst->sa_family,
283 } else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
284 FDDI_ADDR_LEN) == 0) {
285 (void) if_simloop(ifp, m, dst->sa_family,
287 return (0); /* XXX */
291 error = (ifp->if_transmit)(ifp, m);
293 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
298 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
305 * Process a received FDDI packet.
314 struct fddi_header *fh;
317 * Do consistency checks to verify assumptions
318 * made by code past this point.
320 if ((m->m_flags & M_PKTHDR) == 0) {
321 if_printf(ifp, "discard frame w/o packet header\n");
322 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
326 if (m->m_pkthdr.rcvif == NULL) {
327 if_printf(ifp, "discard frame w/o interface pointer\n");
328 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
333 m = m_pullup(m, FDDI_HDR_LEN);
335 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
338 fh = mtod(m, struct fddi_header *);
341 * Discard packet if interface is not up.
343 if (!((ifp->if_flags & IFF_UP) &&
344 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
348 * Give bpf a chance at the packet.
353 * Interface marked for monitoring; discard packet.
355 if (ifp->if_flags & IFF_MONITOR) {
361 mac_ifnet_create_mbuf(ifp, m);
365 * Update interface statistics.
367 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
368 getmicrotime(&ifp->if_lastchange);
371 * Discard non local unicast packets when interface
372 * is in promiscuous mode.
374 if ((ifp->if_flags & IFF_PROMISC) && ((fh->fddi_dhost[0] & 1) == 0) &&
375 (bcmp(IF_LLADDR(ifp), (caddr_t)fh->fddi_dhost,
376 FDDI_ADDR_LEN) != 0))
380 * Set mbuf flags for bcast/mcast.
382 if (fh->fddi_dhost[0] & 1) {
383 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
385 m->m_flags |= M_BCAST;
387 m->m_flags |= M_MCAST;
388 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
393 * If this has a LLC priority of 0, then mark it so upper
394 * layers have a hint that it really came via a FDDI/Ethernet
397 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
398 m->m_flags |= M_LINK0;
401 /* Strip off FDDI header. */
402 m_adj(m, FDDI_HDR_LEN);
404 m = m_pullup(m, LLC_SNAPFRAMELEN);
406 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
409 l = mtod(m, struct llc *);
411 switch (l->llc_dsap) {
415 if ((l->llc_control != LLC_UI) ||
416 (l->llc_ssap != LLC_SNAP_LSAP)) {
417 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
420 if (l->llc_snap.org_code[0] != 0 ||
421 l->llc_snap.org_code[1] != 0 ||
422 l->llc_snap.org_code[2] != 0) {
423 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
427 type = ntohs(l->llc_snap.ether_type);
428 m_adj(m, LLC_SNAPFRAMELEN);
437 if (ifp->if_flags & IFF_NOARP)
448 case ETHERTYPE_DECNET:
453 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
454 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
461 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
462 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
465 M_SETFIB(m, ifp->if_fib);
466 netisr_dispatch(isr, m);
470 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
477 * Perform common duties while attaching to interface list
480 fddi_ifattach(ifp, lla, bpf)
486 struct sockaddr_dl *sdl;
488 ifp->if_type = IFT_FDDI;
489 ifp->if_addrlen = FDDI_ADDR_LEN;
492 if_attach(ifp); /* Must be called before additional assignments */
494 ifp->if_mtu = FDDIMTU;
495 ifp->if_output = fddi_output;
496 ifp->if_input = fddi_input;
497 ifp->if_resolvemulti = fddi_resolvemulti;
498 ifp->if_broadcastaddr = fddibroadcastaddr;
499 ifp->if_baudrate = 100000000;
500 #ifdef IFF_NOTRAILERS
501 ifp->if_flags |= IFF_NOTRAILERS;
504 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
506 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
507 sdl->sdl_type = IFT_FDDI;
508 sdl->sdl_alen = ifp->if_addrlen;
509 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
512 bpfattach(ifp, DLT_FDDI, FDDI_HDR_LEN);
518 fddi_ifdetach(ifp, bpf)
532 fddi_ioctl (ifp, command, data)
541 ifa = (struct ifaddr *) data;
542 ifr = (struct ifreq *) data;
547 ifp->if_flags |= IFF_UP;
549 switch (ifa->ifa_addr->sa_family) {
551 case AF_INET: /* before arpwhohas */
552 ifp->if_init(ifp->if_softc);
553 arp_ifinit(ifp, ifa);
557 ifp->if_init(ifp->if_softc);
564 sa = (struct sockaddr *) & ifr->ifr_data;
565 bcopy(IF_LLADDR(ifp),
566 (caddr_t) sa->sa_data, FDDI_ADDR_LEN);
572 * Set the interface MTU.
574 if (ifr->ifr_mtu > FDDIMTU) {
577 ifp->if_mtu = ifr->ifr_mtu;
589 fddi_resolvemulti(ifp, llsa, sa)
591 struct sockaddr **llsa;
594 struct sockaddr_dl *sdl;
596 struct sockaddr_in *sin;
599 struct sockaddr_in6 *sin6;
603 switch(sa->sa_family) {
606 * No mapping needed. Just check that it's a valid MC address.
608 sdl = (struct sockaddr_dl *)sa;
609 e_addr = LLADDR(sdl);
610 if ((e_addr[0] & 1) != 1)
611 return (EADDRNOTAVAIL);
617 sin = (struct sockaddr_in *)sa;
618 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
619 return (EADDRNOTAVAIL);
620 sdl = link_init_sdl(ifp, *llsa, IFT_FDDI);
622 sdl->sdl_alen = FDDI_ADDR_LEN;
624 e_addr = LLADDR(sdl);
625 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
626 *llsa = (struct sockaddr *)sdl;
631 sin6 = (struct sockaddr_in6 *)sa;
632 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
634 * An IP6 address of 0 means listen to all
635 * of the Ethernet multicast address used for IP6.
636 * (This is used for multicast routers.)
638 ifp->if_flags |= IFF_ALLMULTI;
642 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
643 return (EADDRNOTAVAIL);
644 sdl = link_init_sdl(ifp, *llsa, IFT_FDDI);
646 sdl->sdl_alen = FDDI_ADDR_LEN;
648 e_addr = LLADDR(sdl);
649 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
650 *llsa = (struct sockaddr *)sdl;
656 * Well, the text isn't quite right, but it's the name
659 return (EAFNOSUPPORT);
665 static moduledata_t fddi_mod = {
666 "fddi", /* module name */
667 NULL, /* event handler */
671 DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
672 MODULE_VERSION(fddi, 1);