2 * Copyright (c) 1998, Larry Lile
5 * For latest sources and information on this driver, please
6 * go to http://anarchy.stdio.com.
8 * Questions, comments or suggestions should be directed to
9 * Larry Lile <lile@stdio.com>.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice unmodified, this list of conditions, and the following
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * General ISO 802.5 (Token Ring) support routines
44 #include "opt_inet6.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
57 #include <net/if_arp.h>
58 #include <net/if_dl.h>
59 #include <net/if_llc.h>
60 #include <net/if_types.h>
61 #include <net/if_llatbl.h>
63 #include <net/ethernet.h>
64 #include <net/netisr.h>
65 #include <net/route.h>
67 #include <net/iso88025.h>
69 #if defined(INET) || defined(INET6)
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/if_ether.h>
75 #include <netinet6/nd6.h>
79 #include <netipx/ipx.h>
80 #include <netipx/ipx_if.h>
83 #include <security/mac/mac_framework.h>
85 static const u_char iso88025_broadcastaddr[ISO88025_ADDR_LEN] =
86 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
88 static int iso88025_resolvemulti (struct ifnet *, struct sockaddr **,
91 #define senderr(e) do { error = (e); goto bad; } while (0)
94 * Perform common duties while attaching to interface list
97 iso88025_ifattach(struct ifnet *ifp, const u_int8_t *lla, int bpf)
100 struct sockaddr_dl *sdl;
104 ifp->if_type = IFT_ISO88025;
105 ifp->if_addrlen = ISO88025_ADDR_LEN;
106 ifp->if_hdrlen = ISO88025_HDR_LEN;
108 if_attach(ifp); /* Must be called before additional assignments */
110 ifp->if_output = iso88025_output;
111 ifp->if_input = iso88025_input;
112 ifp->if_resolvemulti = iso88025_resolvemulti;
113 ifp->if_broadcastaddr = iso88025_broadcastaddr;
115 if (ifp->if_baudrate == 0)
116 ifp->if_baudrate = TR_16MBPS; /* 16Mbit should be a safe default */
117 if (ifp->if_mtu == 0)
118 ifp->if_mtu = ISO88025_DEFAULT_MTU;
121 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
123 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
124 sdl->sdl_type = IFT_ISO88025;
125 sdl->sdl_alen = ifp->if_addrlen;
126 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
129 bpfattach(ifp, DLT_IEEE802, ISO88025_HDR_LEN);
135 * Perform common duties while detaching a Token Ring interface
138 iso88025_ifdetach(ifp, bpf)
152 iso88025_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
158 ifa = (struct ifaddr *) data;
159 ifr = (struct ifreq *) data;
164 ifp->if_flags |= IFF_UP;
166 switch (ifa->ifa_addr->sa_family) {
169 ifp->if_init(ifp->if_softc); /* before arpwhohas */
170 arp_ifinit(ifp, ifa);
175 * XXX - This code is probably wrong
178 struct ipx_addr *ina;
180 ina = &(IA_SIPX(ifa)->sipx_addr);
182 if (ipx_nullhost(*ina))
183 ina->x_host = *(union ipx_host *)
186 bcopy((caddr_t) ina->x_host.c_host,
187 (caddr_t) IF_LLADDR(ifp),
193 ifp->if_init(ifp->if_softc);
198 ifp->if_init(ifp->if_softc);
206 sa = (struct sockaddr *) & ifr->ifr_data;
207 bcopy(IF_LLADDR(ifp),
208 (caddr_t) sa->sa_data, ISO88025_ADDR_LEN);
214 * Set the interface MTU.
216 if (ifr->ifr_mtu > ISO88025_MAX_MTU) {
219 ifp->if_mtu = ifr->ifr_mtu;
223 error = EINVAL; /* XXX netbsd has ENOTTY??? */
231 * ISO88025 encapsulation
234 iso88025_output(ifp, m, dst, ro)
237 struct sockaddr *dst;
240 u_int16_t snap_type = 0;
241 int loop_copy = 0, error = 0, rif_len = 0;
242 u_char edst[ISO88025_ADDR_LEN];
243 struct iso88025_header *th;
244 struct iso88025_header gen_th;
245 struct sockaddr_dl *sdl = NULL;
246 struct rtentry *rt0 = NULL;
247 #if defined(INET) || defined(INET6)
255 error = mac_ifnet_check_transmit(ifp, m);
260 if (ifp->if_flags & IFF_MONITOR)
262 if (!((ifp->if_flags & IFF_UP) &&
263 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
265 getmicrotime(&ifp->if_lastchange);
267 /* Calculate routing info length based on arp table entry */
268 /* XXX any better way to do this ? */
270 if (rt0 && (sdl = (struct sockaddr_dl *)rt0->rt_gateway))
271 if (SDL_ISO88025(sdl)->trld_rcf != 0)
272 rif_len = TR_RCF_RIFLEN(SDL_ISO88025(sdl)->trld_rcf);
274 /* Generate a generic 802.5 header for the packet */
276 gen_th.fc = TR_LLC_FRAME;
277 (void)memcpy((caddr_t)gen_th.iso88025_shost, IF_LLADDR(ifp),
280 gen_th.iso88025_shost[0] |= TR_RII;
282 gen_th.rcf = SDL_ISO88025(sdl)->trld_rcf;
283 (void)memcpy((caddr_t)gen_th.rd,
284 (caddr_t)SDL_ISO88025(sdl)->trld_route,
289 switch (dst->sa_family) {
292 error = arpresolve(ifp, rt0, m, dst, edst, &lle);
294 return (error == EWOULDBLOCK ? 0 : error);
295 snap_type = ETHERTYPE_IP;
300 ah = mtod(m, struct arphdr *);
301 ah->ar_hrd = htons(ARPHRD_IEEE802);
303 loop_copy = -1; /* if this is for us, don't do it */
305 switch(ntohs(ah->ar_op)) {
306 case ARPOP_REVREQUEST:
308 snap_type = ETHERTYPE_REVARP;
313 snap_type = ETHERTYPE_ARP;
317 if (m->m_flags & M_BCAST)
318 bcopy(ifp->if_broadcastaddr, edst, ISO88025_ADDR_LEN);
320 bcopy(ar_tha(ah), edst, ISO88025_ADDR_LEN);
327 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
330 snap_type = ETHERTYPE_IPV6;
338 bcopy((caddr_t)&(satoipx_addr(dst).x_host), (caddr_t)edst,
341 M_PREPEND(m, 3, M_WAIT);
345 cp = mtod(m, u_int8_t *);
346 *cp++ = ETHERTYPE_IPX_8022;
347 *cp++ = ETHERTYPE_IPX_8022;
354 struct iso88025_sockaddr_data *sd;
356 * For AF_UNSPEC sockaddr.sa_data must contain all of the
357 * mac information needed to send the packet. This allows
358 * full mac, llc, and source routing function to be controlled.
359 * llc and source routing information must already be in the
360 * mbuf provided, ac/fc are set in sa_data. sockaddr.sa_data
361 * should be an iso88025_sockaddr_data structure see iso88025.h
364 sd = (struct iso88025_sockaddr_data *)dst->sa_data;
367 (void)memcpy((caddr_t)edst, (caddr_t)sd->ether_dhost,
369 (void)memcpy((caddr_t)gen_th.iso88025_shost,
370 (caddr_t)sd->ether_shost, ISO88025_ADDR_LEN);
375 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
376 senderr(EAFNOSUPPORT);
383 if (snap_type != 0) {
385 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
388 l = mtod(m, struct llc *);
389 l->llc_control = LLC_UI;
390 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
391 l->llc_snap.org_code[0] =
392 l->llc_snap.org_code[1] =
393 l->llc_snap.org_code[2] = 0;
394 l->llc_snap.ether_type = htons(snap_type);
398 * Add local net header. If no space in first mbuf,
401 M_PREPEND(m, ISO88025_HDR_LEN + rif_len, M_DONTWAIT);
404 th = mtod(m, struct iso88025_header *);
405 bcopy((caddr_t)edst, (caddr_t)&gen_th.iso88025_dhost, ISO88025_ADDR_LEN);
407 /* Copy as much of the generic header as is needed into the mbuf */
408 memcpy(th, &gen_th, ISO88025_HDR_LEN + rif_len);
411 * If a simplex interface, and the packet is being sent to our
412 * Ethernet address or a broadcast address, loopback a copy.
413 * XXX To make a simplex device behave exactly like a duplex
414 * device, we should copy in the case of sending to our own
415 * ethernet address (thus letting the original actually appear
416 * on the wire). However, we don't do that here for security
417 * reasons and compatibility with the original behavior.
419 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
420 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
422 n = m_copy(m, 0, (int)M_COPYALL);
423 (void) if_simloop(ifp, n, dst->sa_family,
425 } else if (bcmp(th->iso88025_dhost, th->iso88025_shost,
426 ETHER_ADDR_LEN) == 0) {
427 (void) if_simloop(ifp, m, dst->sa_family,
433 IFQ_HANDOFF_ADJ(ifp, m, ISO88025_HDR_LEN + LLC_SNAPFRAMELEN, error);
435 printf("iso88025_output: packet dropped QFULL.\n");
448 * ISO 88025 de-encapsulation
451 iso88025_input(ifp, m)
455 struct iso88025_header *th;
461 * Do consistency checks to verify assumptions
462 * made by code past this point.
464 if ((m->m_flags & M_PKTHDR) == 0) {
465 if_printf(ifp, "discard frame w/o packet header\n");
470 if (m->m_pkthdr.rcvif == NULL) {
471 if_printf(ifp, "discard frame w/o interface pointer\n");
477 m = m_pullup(m, ISO88025_HDR_LEN);
482 th = mtod(m, struct iso88025_header *);
483 m->m_pkthdr.header = (void *)th;
486 * Discard packet if interface is not up.
488 if (!((ifp->if_flags & IFF_UP) &&
489 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
493 * Give bpf a chance at the packet.
498 * Interface marked for monitoring; discard packet.
500 if (ifp->if_flags & IFF_MONITOR) {
506 mac_ifnet_create_mbuf(ifp, m);
510 * Update interface statistics.
512 ifp->if_ibytes += m->m_pkthdr.len;
513 getmicrotime(&ifp->if_lastchange);
516 * Discard non local unicast packets when interface
517 * is in promiscuous mode.
519 if ((ifp->if_flags & IFF_PROMISC) &&
520 ((th->iso88025_dhost[0] & 1) == 0) &&
521 (bcmp(IF_LLADDR(ifp), (caddr_t) th->iso88025_dhost,
522 ISO88025_ADDR_LEN) != 0))
526 * Set mbuf flags for bcast/mcast.
528 if (th->iso88025_dhost[0] & 1) {
529 if (bcmp(iso88025_broadcastaddr, th->iso88025_dhost,
530 ISO88025_ADDR_LEN) == 0)
531 m->m_flags |= M_BCAST;
533 m->m_flags |= M_MCAST;
537 mac_hdr_len = ISO88025_HDR_LEN;
538 /* Check for source routing info */
539 if (th->iso88025_shost[0] & TR_RII)
540 mac_hdr_len += TR_RCF_RIFLEN(th->rcf);
542 /* Strip off ISO88025 header. */
543 m_adj(m, mac_hdr_len);
545 m = m_pullup(m, LLC_SNAPFRAMELEN);
550 l = mtod(m, struct llc *);
552 switch (l->llc_dsap) {
554 case ETHERTYPE_IPX_8022: /* Thanks a bunch Novell */
555 if ((l->llc_control != LLC_UI) ||
556 (l->llc_ssap != ETHERTYPE_IPX_8022)) {
561 th->iso88025_shost[0] &= ~(TR_RII);
566 case LLC_SNAP_LSAP: {
568 if ((l->llc_control != LLC_UI) ||
569 (l->llc_ssap != LLC_SNAP_LSAP)) {
574 if (l->llc_snap.org_code[0] != 0 ||
575 l->llc_snap.org_code[1] != 0 ||
576 l->llc_snap.org_code[2] != 0) {
581 type = ntohs(l->llc_snap.ether_type);
582 m_adj(m, LLC_SNAPFRAMELEN);
586 th->iso88025_shost[0] &= ~(TR_RII);
587 if ((m = ip_fastforward(m)) == NULL)
593 if (ifp->if_flags & IFF_NOARP)
598 #ifdef IPX_SNAP /* XXX: Not supported! */
600 th->iso88025_shost[0] &= ~(TR_RII);
603 #endif /* IPX_SNAP */
606 th->iso88025_shost[0] &= ~(TR_RII);
611 printf("iso88025_input: unexpected llc_snap ether_type 0x%02x\n", type);
619 switch (l->llc_control) {
626 if(m->m_len < ISO88025_ADDR_LEN)
631 l->llc_dsap = l->llc_ssap = 0;
632 /* Fall through to */
638 struct iso88025_sockaddr_data *th2;
644 if (th->iso88025_shost[0] & TR_RII) { /* XXX */
645 printf("iso88025_input: dropping source routed LLC_TEST\n");
648 l->llc_dsap = l->llc_ssap;
650 if (m->m_flags & (M_BCAST | M_MCAST))
651 bcopy((caddr_t)IF_LLADDR(ifp),
652 (caddr_t)th->iso88025_dhost,
654 sa.sa_family = AF_UNSPEC;
655 sa.sa_len = sizeof(sa);
656 th2 = (struct iso88025_sockaddr_data *)sa.sa_data;
657 for (i = 0; i < ISO88025_ADDR_LEN; i++) {
658 th2->ether_shost[i] = c = th->iso88025_dhost[i];
659 th2->ether_dhost[i] = th->iso88025_dhost[i] =
660 th->iso88025_shost[i];
661 th->iso88025_shost[i] = c;
664 th2->fc = TR_LLC_FRAME;
665 ifp->if_output(ifp, m, &sa, NULL);
669 printf("iso88025_input: unexpected llc control 0x%02x\n", l->llc_control);
677 printf("iso88025_input: unknown dsap 0x%x\n", l->llc_dsap);
683 M_SETFIB(m, ifp->if_fib);
684 netisr_dispatch(isr, m);
695 iso88025_resolvemulti (ifp, llsa, sa)
697 struct sockaddr **llsa;
700 struct sockaddr_dl *sdl;
702 struct sockaddr_in *sin;
705 struct sockaddr_in6 *sin6;
709 switch(sa->sa_family) {
712 * No mapping needed. Just check that it's a valid MC address.
714 sdl = (struct sockaddr_dl *)sa;
715 e_addr = LLADDR(sdl);
716 if ((e_addr[0] & 1) != 1) {
717 return (EADDRNOTAVAIL);
724 sin = (struct sockaddr_in *)sa;
725 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
726 return (EADDRNOTAVAIL);
728 sdl = malloc(sizeof *sdl, M_IFMADDR,
732 sdl->sdl_len = sizeof *sdl;
733 sdl->sdl_family = AF_LINK;
734 sdl->sdl_index = ifp->if_index;
735 sdl->sdl_type = IFT_ISO88025;
736 sdl->sdl_alen = ISO88025_ADDR_LEN;
737 e_addr = LLADDR(sdl);
738 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
739 *llsa = (struct sockaddr *)sdl;
744 sin6 = (struct sockaddr_in6 *)sa;
745 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
747 * An IP6 address of 0 means listen to all
748 * of the Ethernet multicast address used for IP6.
749 * (This is used for multicast routers.)
751 ifp->if_flags |= IFF_ALLMULTI;
755 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
756 return (EADDRNOTAVAIL);
758 sdl = malloc(sizeof *sdl, M_IFMADDR,
762 sdl->sdl_len = sizeof *sdl;
763 sdl->sdl_family = AF_LINK;
764 sdl->sdl_index = ifp->if_index;
765 sdl->sdl_type = IFT_ISO88025;
766 sdl->sdl_alen = ISO88025_ADDR_LEN;
767 e_addr = LLADDR(sdl);
768 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
769 *llsa = (struct sockaddr *)sdl;
775 * Well, the text isn't quite right, but it's the name
778 return (EAFNOSUPPORT);
784 static MALLOC_DEFINE(M_ISO88025, "arpcom", "802.5 interface internals");
787 iso88025_alloc(u_char type, struct ifnet *ifp)
791 ac = malloc(sizeof(struct arpcom), M_ISO88025, M_WAITOK | M_ZERO);
798 iso88025_free(void *com, u_char type)
801 free(com, M_ISO88025);
805 iso88025_modevent(module_t mod, int type, void *data)
810 if_register_com_alloc(IFT_ISO88025, iso88025_alloc,
814 if_deregister_com_alloc(IFT_ISO88025);
823 static moduledata_t iso88025_mod = {
829 DECLARE_MODULE(iso88025, iso88025_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
830 MODULE_VERSION(iso88025, 1);