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;
104 #if defined(INET) || defined(INET6)
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 #if defined(INET) || defined(INET6)
123 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
126 switch (dst->sa_family) {
129 error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
131 return (error == EWOULDBLOCK ? 0 : error);
132 type = htons(ETHERTYPE_IP);
138 ah = mtod(m, struct arphdr *);
139 ah->ar_hrd = htons(ARPHRD_ETHER);
141 loop_copy = -1; /* if this is for us, don't do it */
143 switch (ntohs(ah->ar_op)) {
144 case ARPOP_REVREQUEST:
146 type = htons(ETHERTYPE_REVARP);
151 type = htons(ETHERTYPE_ARP);
155 if (m->m_flags & M_BCAST)
156 bcopy(ifp->if_broadcastaddr, edst, FDDI_ADDR_LEN);
158 bcopy(ar_tha(ah), edst, FDDI_ADDR_LEN);
165 error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
167 return (error == EWOULDBLOCK ? 0 : error);
168 type = htons(ETHERTYPE_IPV6);
171 case pseudo_AF_HDRCMPLT:
173 const struct ether_header *eh;
176 eh = (const struct ether_header *)dst->sa_data;
177 bcopy(eh->ether_shost, esrc, FDDI_ADDR_LEN);
183 const struct ether_header *eh;
186 eh = (const struct ether_header *)dst->sa_data;
187 bcopy(eh->ether_dhost, edst, FDDI_ADDR_LEN);
189 m->m_flags |= (M_BCAST|M_MCAST);
190 type = eh->ether_type;
196 fh = mtod(m, struct fddi_header *);
197 error = EPROTONOSUPPORT;
198 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
199 case FDDIFC_LLC_ASYNC: {
200 /* legal priorities are 0 through 7 */
201 if ((fh->fddi_fc & FDDIFC_Z) > 7)
205 case FDDIFC_LLC_SYNC: {
206 /* FDDIFC_Z bits reserved, must be zero */
207 if (fh->fddi_fc & FDDIFC_Z)
212 /* FDDIFC_Z bits must be non zero */
213 if ((fh->fddi_fc & FDDIFC_Z) == 0)
218 /* anything else is too dangerous */
223 if (fh->fddi_dhost[0] & 1)
224 m->m_flags |= (M_BCAST|M_MCAST);
228 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
229 senderr(EAFNOSUPPORT);
237 M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
240 l = mtod(m, struct llc *);
241 l->llc_control = LLC_UI;
242 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
243 l->llc_snap.org_code[0] =
244 l->llc_snap.org_code[1] =
245 l->llc_snap.org_code[2] = 0;
246 l->llc_snap.ether_type = htons(type);
250 * Add local net header. If no space in first mbuf,
253 M_PREPEND(m, FDDI_HDR_LEN, M_NOWAIT);
256 fh = mtod(m, struct fddi_header *);
257 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
258 bcopy((caddr_t)edst, (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN);
261 bcopy((caddr_t)esrc, (caddr_t)fh->fddi_shost, FDDI_ADDR_LEN);
263 bcopy(IF_LLADDR(ifp), (caddr_t)fh->fddi_shost,
267 * If a simplex interface, and the packet is being sent to our
268 * Ethernet address or a broadcast address, loopback a copy.
269 * XXX To make a simplex device behave exactly like a duplex
270 * device, we should copy in the case of sending to our own
271 * ethernet address (thus letting the original actually appear
272 * on the wire). However, we don't do that here for security
273 * reasons and compatibility with the original behavior.
275 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
276 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
278 n = m_copy(m, 0, (int)M_COPYALL);
279 (void) if_simloop(ifp, n, dst->sa_family,
281 } else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
282 FDDI_ADDR_LEN) == 0) {
283 (void) if_simloop(ifp, m, dst->sa_family,
285 return (0); /* XXX */
289 error = (ifp->if_transmit)(ifp, m);
291 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
296 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
303 * Process a received FDDI packet.
312 struct fddi_header *fh;
315 * Do consistency checks to verify assumptions
316 * made by code past this point.
318 if ((m->m_flags & M_PKTHDR) == 0) {
319 if_printf(ifp, "discard frame w/o packet header\n");
320 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
324 if (m->m_pkthdr.rcvif == NULL) {
325 if_printf(ifp, "discard frame w/o interface pointer\n");
326 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
331 m = m_pullup(m, FDDI_HDR_LEN);
333 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
336 fh = mtod(m, struct fddi_header *);
339 * Discard packet if interface is not up.
341 if (!((ifp->if_flags & IFF_UP) &&
342 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
346 * Give bpf a chance at the packet.
351 * Interface marked for monitoring; discard packet.
353 if (ifp->if_flags & IFF_MONITOR) {
359 mac_ifnet_create_mbuf(ifp, m);
363 * Update interface statistics.
365 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
366 getmicrotime(&ifp->if_lastchange);
369 * Discard non local unicast packets when interface
370 * is in promiscuous mode.
372 if ((ifp->if_flags & IFF_PROMISC) && ((fh->fddi_dhost[0] & 1) == 0) &&
373 (bcmp(IF_LLADDR(ifp), (caddr_t)fh->fddi_dhost,
374 FDDI_ADDR_LEN) != 0))
378 * Set mbuf flags for bcast/mcast.
380 if (fh->fddi_dhost[0] & 1) {
381 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
383 m->m_flags |= M_BCAST;
385 m->m_flags |= M_MCAST;
386 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
391 * If this has a LLC priority of 0, then mark it so upper
392 * layers have a hint that it really came via a FDDI/Ethernet
395 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
396 m->m_flags |= M_LINK0;
399 /* Strip off FDDI header. */
400 m_adj(m, FDDI_HDR_LEN);
402 m = m_pullup(m, LLC_SNAPFRAMELEN);
404 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
407 l = mtod(m, struct llc *);
409 switch (l->llc_dsap) {
413 if ((l->llc_control != LLC_UI) ||
414 (l->llc_ssap != LLC_SNAP_LSAP)) {
415 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
418 if (l->llc_snap.org_code[0] != 0 ||
419 l->llc_snap.org_code[1] != 0 ||
420 l->llc_snap.org_code[2] != 0) {
421 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
425 type = ntohs(l->llc_snap.ether_type);
426 m_adj(m, LLC_SNAPFRAMELEN);
435 if (ifp->if_flags & IFF_NOARP)
446 case ETHERTYPE_DECNET:
451 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
452 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
459 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
460 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
463 M_SETFIB(m, ifp->if_fib);
464 netisr_dispatch(isr, m);
468 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
475 * Perform common duties while attaching to interface list
478 fddi_ifattach(ifp, lla, bpf)
484 struct sockaddr_dl *sdl;
486 ifp->if_type = IFT_FDDI;
487 ifp->if_addrlen = FDDI_ADDR_LEN;
490 if_attach(ifp); /* Must be called before additional assignments */
492 ifp->if_mtu = FDDIMTU;
493 ifp->if_output = fddi_output;
494 ifp->if_input = fddi_input;
495 ifp->if_resolvemulti = fddi_resolvemulti;
496 ifp->if_broadcastaddr = fddibroadcastaddr;
497 ifp->if_baudrate = 100000000;
498 #ifdef IFF_NOTRAILERS
499 ifp->if_flags |= IFF_NOTRAILERS;
502 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
504 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
505 sdl->sdl_type = IFT_FDDI;
506 sdl->sdl_alen = ifp->if_addrlen;
507 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
510 bpfattach(ifp, DLT_FDDI, FDDI_HDR_LEN);
516 fddi_ifdetach(ifp, bpf)
530 fddi_ioctl (ifp, command, data)
539 ifa = (struct ifaddr *) data;
540 ifr = (struct ifreq *) data;
545 ifp->if_flags |= IFF_UP;
547 switch (ifa->ifa_addr->sa_family) {
549 case AF_INET: /* before arpwhohas */
550 ifp->if_init(ifp->if_softc);
551 arp_ifinit(ifp, ifa);
555 ifp->if_init(ifp->if_softc);
562 sa = (struct sockaddr *) & ifr->ifr_data;
563 bcopy(IF_LLADDR(ifp),
564 (caddr_t) sa->sa_data, FDDI_ADDR_LEN);
570 * Set the interface MTU.
572 if (ifr->ifr_mtu > FDDIMTU) {
575 ifp->if_mtu = ifr->ifr_mtu;
587 fddi_resolvemulti(ifp, llsa, sa)
589 struct sockaddr **llsa;
592 struct sockaddr_dl *sdl;
594 struct sockaddr_in *sin;
597 struct sockaddr_in6 *sin6;
601 switch(sa->sa_family) {
604 * No mapping needed. Just check that it's a valid MC address.
606 sdl = (struct sockaddr_dl *)sa;
607 e_addr = LLADDR(sdl);
608 if ((e_addr[0] & 1) != 1)
609 return (EADDRNOTAVAIL);
615 sin = (struct sockaddr_in *)sa;
616 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
617 return (EADDRNOTAVAIL);
618 sdl = link_init_sdl(ifp, *llsa, IFT_FDDI);
620 sdl->sdl_alen = FDDI_ADDR_LEN;
622 e_addr = LLADDR(sdl);
623 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
624 *llsa = (struct sockaddr *)sdl;
629 sin6 = (struct sockaddr_in6 *)sa;
630 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
632 * An IP6 address of 0 means listen to all
633 * of the Ethernet multicast address used for IP6.
634 * (This is used for multicast routers.)
636 ifp->if_flags |= IFF_ALLMULTI;
640 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
641 return (EADDRNOTAVAIL);
642 sdl = link_init_sdl(ifp, *llsa, IFT_FDDI);
644 sdl->sdl_alen = FDDI_ADDR_LEN;
646 e_addr = LLADDR(sdl);
647 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
648 *llsa = (struct sockaddr *)sdl;
654 * Well, the text isn't quite right, but it's the name
657 return (EAFNOSUPPORT);
663 static moduledata_t fddi_mod = {
664 "fddi", /* module name */
665 NULL, /* event handler */
669 DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
670 MODULE_VERSION(fddi, 1);