2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998, Larry Lile
7 * For latest sources and information on this driver, please
8 * go to http://anarchy.stdio.com.
10 * Questions, comments or suggestions should be directed to
11 * Larry Lile <lile@stdio.com>.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice unmodified, this list of conditions, and the following
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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
41 * General ISO 802.5 (Token Ring) support routines
46 #include "opt_inet6.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
53 #include <sys/module.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
58 #include <net/if_var.h>
59 #include <net/if_arp.h>
60 #include <net/if_dl.h>
61 #include <net/if_llc.h>
62 #include <net/if_types.h>
63 #include <net/if_llatbl.h>
65 #include <net/ethernet.h>
66 #include <net/netisr.h>
67 #include <net/route.h>
69 #include <net/iso88025.h>
71 #if defined(INET) || defined(INET6)
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #include <netinet/if_ether.h>
77 #include <netinet6/nd6.h>
80 #include <security/mac/mac_framework.h>
82 static const u_char iso88025_broadcastaddr[ISO88025_ADDR_LEN] =
83 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
85 static int iso88025_resolvemulti (struct ifnet *, struct sockaddr **,
88 #define senderr(e) do { error = (e); goto bad; } while (0)
91 * Perform common duties while attaching to interface list
94 iso88025_ifattach(struct ifnet *ifp, const u_int8_t *lla, int bpf)
97 struct sockaddr_dl *sdl;
101 ifp->if_type = IFT_ISO88025;
102 ifp->if_addrlen = ISO88025_ADDR_LEN;
103 ifp->if_hdrlen = ISO88025_HDR_LEN;
105 if_attach(ifp); /* Must be called before additional assignments */
107 ifp->if_output = iso88025_output;
108 ifp->if_input = iso88025_input;
109 ifp->if_resolvemulti = iso88025_resolvemulti;
110 ifp->if_broadcastaddr = iso88025_broadcastaddr;
112 if (ifp->if_baudrate == 0)
113 ifp->if_baudrate = TR_16MBPS; /* 16Mbit should be a safe default */
114 if (ifp->if_mtu == 0)
115 ifp->if_mtu = ISO88025_DEFAULT_MTU;
118 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
120 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
121 sdl->sdl_type = IFT_ISO88025;
122 sdl->sdl_alen = ifp->if_addrlen;
123 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
126 bpfattach(ifp, DLT_IEEE802, ISO88025_HDR_LEN);
132 * Perform common duties while detaching a Token Ring interface
135 iso88025_ifdetach(ifp, bpf)
149 iso88025_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
155 ifa = (struct ifaddr *) data;
156 ifr = (struct ifreq *) data;
161 ifp->if_flags |= IFF_UP;
163 switch (ifa->ifa_addr->sa_family) {
166 ifp->if_init(ifp->if_softc); /* before arpwhohas */
167 arp_ifinit(ifp, ifa);
171 ifp->if_init(ifp->if_softc);
179 sa = (struct sockaddr *) & ifr->ifr_data;
180 bcopy(IF_LLADDR(ifp),
181 (caddr_t) sa->sa_data, ISO88025_ADDR_LEN);
187 * Set the interface MTU.
189 if (ifr->ifr_mtu > ISO88025_MAX_MTU) {
192 ifp->if_mtu = ifr->ifr_mtu;
196 error = EINVAL; /* XXX netbsd has ENOTTY??? */
204 * ISO88025 encapsulation
207 iso88025_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
210 u_int16_t snap_type = 0;
211 int loop_copy = 0, error = 0, rif_len = 0;
212 u_char edst[ISO88025_ADDR_LEN];
213 struct iso88025_header *th;
214 struct iso88025_header gen_th;
215 struct sockaddr_dl *sdl = NULL;
216 struct rtentry *rt0 = NULL;
220 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
222 error = mac_ifnet_check_transmit(ifp, m);
227 if (ifp->if_flags & IFF_MONITOR)
229 if (!((ifp->if_flags & IFF_UP) &&
230 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
232 getmicrotime(&ifp->if_lastchange);
234 /* Calculate routing info length based on arp table entry */
235 /* XXX any better way to do this ? */
237 if (rt0 && (sdl = (struct sockaddr_dl *)rt0->rt_gateway))
238 if (SDL_ISO88025(sdl)->trld_rcf != 0)
239 rif_len = TR_RCF_RIFLEN(SDL_ISO88025(sdl)->trld_rcf);
241 /* Generate a generic 802.5 header for the packet */
243 gen_th.fc = TR_LLC_FRAME;
244 (void)memcpy((caddr_t)gen_th.iso88025_shost, IF_LLADDR(ifp),
247 gen_th.iso88025_shost[0] |= TR_RII;
249 gen_th.rcf = SDL_ISO88025(sdl)->trld_rcf;
250 (void)memcpy((caddr_t)gen_th.rd,
251 (caddr_t)SDL_ISO88025(sdl)->trld_route,
256 switch (dst->sa_family) {
259 error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
261 return (error == EWOULDBLOCK ? 0 : error);
262 snap_type = ETHERTYPE_IP;
267 ah = mtod(m, struct arphdr *);
268 ah->ar_hrd = htons(ARPHRD_IEEE802);
270 loop_copy = -1; /* if this is for us, don't do it */
272 switch(ntohs(ah->ar_op)) {
273 case ARPOP_REVREQUEST:
275 snap_type = ETHERTYPE_REVARP;
280 snap_type = ETHERTYPE_ARP;
284 if (m->m_flags & M_BCAST)
285 bcopy(ifp->if_broadcastaddr, edst, ISO88025_ADDR_LEN);
287 bcopy(ar_tha(ah), edst, ISO88025_ADDR_LEN);
294 error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
296 return (error == EWOULDBLOCK ? 0 : error);
297 snap_type = ETHERTYPE_IPV6;
302 const struct iso88025_sockaddr_data *sd;
304 * For AF_UNSPEC sockaddr.sa_data must contain all of the
305 * mac information needed to send the packet. This allows
306 * full mac, llc, and source routing function to be controlled.
307 * llc and source routing information must already be in the
308 * mbuf provided, ac/fc are set in sa_data. sockaddr.sa_data
309 * should be an iso88025_sockaddr_data structure see iso88025.h
312 sd = (const struct iso88025_sockaddr_data *)dst->sa_data;
315 (void)memcpy(edst, sd->ether_dhost, ISO88025_ADDR_LEN);
316 (void)memcpy(gen_th.iso88025_shost, sd->ether_shost,
322 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
323 senderr(EAFNOSUPPORT);
330 if (snap_type != 0) {
332 M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
335 l = mtod(m, struct llc *);
336 l->llc_control = LLC_UI;
337 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
338 l->llc_snap.org_code[0] =
339 l->llc_snap.org_code[1] =
340 l->llc_snap.org_code[2] = 0;
341 l->llc_snap.ether_type = htons(snap_type);
345 * Add local net header. If no space in first mbuf,
348 M_PREPEND(m, ISO88025_HDR_LEN + rif_len, M_NOWAIT);
351 th = mtod(m, struct iso88025_header *);
352 bcopy((caddr_t)edst, (caddr_t)&gen_th.iso88025_dhost, ISO88025_ADDR_LEN);
354 /* Copy as much of the generic header as is needed into the mbuf */
355 memcpy(th, &gen_th, ISO88025_HDR_LEN + rif_len);
358 * If a simplex interface, and the packet is being sent to our
359 * Ethernet address or a broadcast address, loopback a copy.
360 * XXX To make a simplex device behave exactly like a duplex
361 * device, we should copy in the case of sending to our own
362 * ethernet address (thus letting the original actually appear
363 * on the wire). However, we don't do that here for security
364 * reasons and compatibility with the original behavior.
366 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
367 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
369 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
370 (void) if_simloop(ifp, n, dst->sa_family,
372 } else if (bcmp(th->iso88025_dhost, th->iso88025_shost,
373 ETHER_ADDR_LEN) == 0) {
374 (void) if_simloop(ifp, m, dst->sa_family,
380 IFQ_HANDOFF_ADJ(ifp, m, ISO88025_HDR_LEN + LLC_SNAPFRAMELEN, error);
382 printf("iso88025_output: packet dropped QFULL.\n");
383 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
388 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
395 * ISO 88025 de-encapsulation
398 iso88025_input(ifp, m)
402 struct iso88025_header *th;
408 * Do consistency checks to verify assumptions
409 * made by code past this point.
411 if ((m->m_flags & M_PKTHDR) == 0) {
412 if_printf(ifp, "discard frame w/o packet header\n");
413 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
417 if (m->m_pkthdr.rcvif == NULL) {
418 if_printf(ifp, "discard frame w/o interface pointer\n");
419 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
424 m = m_pullup(m, ISO88025_HDR_LEN);
426 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
429 th = mtod(m, struct iso88025_header *);
432 * Discard packet if interface is not up.
434 if (!((ifp->if_flags & IFF_UP) &&
435 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
439 * Give bpf a chance at the packet.
444 * Interface marked for monitoring; discard packet.
446 if (ifp->if_flags & IFF_MONITOR) {
452 mac_ifnet_create_mbuf(ifp, m);
456 * Update interface statistics.
458 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
459 getmicrotime(&ifp->if_lastchange);
462 * Discard non local unicast packets when interface
463 * is in promiscuous mode.
465 if ((ifp->if_flags & IFF_PROMISC) &&
466 ((th->iso88025_dhost[0] & 1) == 0) &&
467 (bcmp(IF_LLADDR(ifp), (caddr_t) th->iso88025_dhost,
468 ISO88025_ADDR_LEN) != 0))
472 * Set mbuf flags for bcast/mcast.
474 if (th->iso88025_dhost[0] & 1) {
475 if (bcmp(iso88025_broadcastaddr, th->iso88025_dhost,
476 ISO88025_ADDR_LEN) == 0)
477 m->m_flags |= M_BCAST;
479 m->m_flags |= M_MCAST;
480 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
483 mac_hdr_len = ISO88025_HDR_LEN;
484 /* Check for source routing info */
485 if (th->iso88025_shost[0] & TR_RII)
486 mac_hdr_len += TR_RCF_RIFLEN(th->rcf);
488 /* Strip off ISO88025 header. */
489 m_adj(m, mac_hdr_len);
491 m = m_pullup(m, LLC_SNAPFRAMELEN);
493 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
496 l = mtod(m, struct llc *);
498 switch (l->llc_dsap) {
499 case LLC_SNAP_LSAP: {
501 if ((l->llc_control != LLC_UI) ||
502 (l->llc_ssap != LLC_SNAP_LSAP)) {
503 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
507 if (l->llc_snap.org_code[0] != 0 ||
508 l->llc_snap.org_code[1] != 0 ||
509 l->llc_snap.org_code[2] != 0) {
510 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
514 type = ntohs(l->llc_snap.ether_type);
515 m_adj(m, LLC_SNAPFRAMELEN);
519 th->iso88025_shost[0] &= ~(TR_RII);
524 if (ifp->if_flags & IFF_NOARP)
531 th->iso88025_shost[0] &= ~(TR_RII);
536 printf("iso88025_input: unexpected llc_snap ether_type 0x%02x\n", type);
537 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
544 switch (l->llc_control) {
546 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
551 if(m->m_len < ISO88025_ADDR_LEN)
556 l->llc_dsap = l->llc_ssap = 0;
557 /* Fall through to */
562 struct iso88025_sockaddr_data *th2;
568 if (th->iso88025_shost[0] & TR_RII) { /* XXX */
569 printf("iso88025_input: dropping source routed LLC_TEST\n");
572 l->llc_dsap = l->llc_ssap;
574 if (m->m_flags & (M_BCAST | M_MCAST))
575 bcopy((caddr_t)IF_LLADDR(ifp),
576 (caddr_t)th->iso88025_dhost,
578 sa.sa_family = AF_UNSPEC;
579 sa.sa_len = sizeof(sa);
580 th2 = (struct iso88025_sockaddr_data *)sa.sa_data;
581 for (i = 0; i < ISO88025_ADDR_LEN; i++) {
582 th2->ether_shost[i] = c = th->iso88025_dhost[i];
583 th2->ether_dhost[i] = th->iso88025_dhost[i] =
584 th->iso88025_shost[i];
585 th->iso88025_shost[i] = c;
588 th2->fc = TR_LLC_FRAME;
589 ifp->if_output(ifp, m, &sa, NULL);
593 printf("iso88025_input: unexpected llc control 0x%02x\n", l->llc_control);
594 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
601 printf("iso88025_input: unknown dsap 0x%x\n", l->llc_dsap);
602 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
607 M_SETFIB(m, ifp->if_fib);
608 netisr_dispatch(isr, m);
612 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
619 iso88025_resolvemulti (ifp, llsa, sa)
621 struct sockaddr **llsa;
624 struct sockaddr_dl *sdl;
626 struct sockaddr_in *sin;
629 struct sockaddr_in6 *sin6;
633 switch(sa->sa_family) {
636 * No mapping needed. Just check that it's a valid MC address.
638 sdl = (struct sockaddr_dl *)sa;
639 e_addr = LLADDR(sdl);
640 if ((e_addr[0] & 1) != 1) {
641 return (EADDRNOTAVAIL);
648 sin = (struct sockaddr_in *)sa;
649 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
650 return (EADDRNOTAVAIL);
652 sdl = link_init_sdl(ifp, *llsa, IFT_ISO88025);
653 sdl->sdl_alen = ISO88025_ADDR_LEN;
654 e_addr = LLADDR(sdl);
655 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
656 *llsa = (struct sockaddr *)sdl;
661 sin6 = (struct sockaddr_in6 *)sa;
662 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
664 * An IP6 address of 0 means listen to all
665 * of the Ethernet multicast address used for IP6.
666 * (This is used for multicast routers.)
668 ifp->if_flags |= IFF_ALLMULTI;
672 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
673 return (EADDRNOTAVAIL);
675 sdl = link_init_sdl(ifp, *llsa, IFT_ISO88025);
676 sdl->sdl_alen = ISO88025_ADDR_LEN;
677 e_addr = LLADDR(sdl);
678 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
679 *llsa = (struct sockaddr *)sdl;
685 * Well, the text isn't quite right, but it's the name
688 return (EAFNOSUPPORT);
694 static moduledata_t iso88025_mod = {
698 DECLARE_MODULE(iso88025, iso88025_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
699 MODULE_VERSION(iso88025, 1);