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"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/malloc.h>
54 #include <sys/module.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
59 #include <net/if_dl.h>
60 #include <net/if_llc.h>
61 #include <net/if_types.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
66 #include <net/iso88025.h>
68 #if defined(INET) || defined(INET6)
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/if_ether.h>
74 #include <netinet6/nd6.h>
78 #include <netipx/ipx.h>
79 #include <netipx/ipx_if.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, int 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);
172 * XXX - This code is probably wrong
175 struct ipx_addr *ina;
177 ina = &(IA_SIPX(ifa)->sipx_addr);
179 if (ipx_nullhost(*ina))
180 ina->x_host = *(union ipx_host *)
183 bcopy((caddr_t) ina->x_host.c_host,
184 (caddr_t) IF_LLADDR(ifp),
190 ifp->if_init(ifp->if_softc);
195 ifp->if_init(ifp->if_softc);
203 sa = (struct sockaddr *) & ifr->ifr_data;
204 bcopy(IF_LLADDR(ifp),
205 (caddr_t) sa->sa_data, ISO88025_ADDR_LEN);
211 * Set the interface MTU.
213 if (ifr->ifr_mtu > ISO88025_MAX_MTU) {
216 ifp->if_mtu = ifr->ifr_mtu;
220 error = EINVAL; /* XXX netbsd has ENOTTY??? */
228 * ISO88025 encapsulation
231 iso88025_output(ifp, m, dst, rt0)
234 struct sockaddr *dst;
237 u_int16_t snap_type = 0;
238 int loop_copy = 0, error = 0, rif_len = 0;
239 u_char edst[ISO88025_ADDR_LEN];
240 struct iso88025_header *th;
241 struct iso88025_header gen_th;
242 struct sockaddr_dl *sdl = NULL;
243 struct rtentry *rt = NULL;
246 error = mac_check_ifnet_transmit(ifp, m);
251 if (ifp->if_flags & IFF_MONITOR)
253 if (!((ifp->if_flags & IFF_UP) &&
254 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
256 getmicrotime(&ifp->if_lastchange);
258 /* Calculate routing info length based on arp table entry */
259 /* XXX any better way to do this ? */
261 error = rt_check(&rt, &rt0, dst);
267 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway))
268 if (SDL_ISO88025(sdl)->trld_rcf != 0)
269 rif_len = TR_RCF_RIFLEN(SDL_ISO88025(sdl)->trld_rcf);
271 /* Generate a generic 802.5 header for the packet */
273 gen_th.fc = TR_LLC_FRAME;
274 (void)memcpy((caddr_t)gen_th.iso88025_shost, IF_LLADDR(ifp),
277 gen_th.iso88025_shost[0] |= TR_RII;
279 gen_th.rcf = SDL_ISO88025(sdl)->trld_rcf;
280 (void)memcpy((caddr_t)gen_th.rd,
281 (caddr_t)SDL_ISO88025(sdl)->trld_route,
286 switch (dst->sa_family) {
289 error = arpresolve(ifp, rt0, m, dst, edst);
291 return (error == EWOULDBLOCK ? 0 : error);
292 snap_type = ETHERTYPE_IP;
297 ah = mtod(m, struct arphdr *);
298 ah->ar_hrd = htons(ARPHRD_IEEE802);
300 loop_copy = -1; /* if this is for us, don't do it */
302 switch(ntohs(ah->ar_op)) {
303 case ARPOP_REVREQUEST:
305 snap_type = ETHERTYPE_REVARP;
310 snap_type = ETHERTYPE_ARP;
314 if (m->m_flags & M_BCAST)
315 bcopy(ifp->if_broadcastaddr, edst, ISO88025_ADDR_LEN);
317 bcopy(ar_tha(ah), edst, ISO88025_ADDR_LEN);
324 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
327 snap_type = ETHERTYPE_IPV6;
335 bcopy((caddr_t)&(satoipx_addr(dst).x_host), (caddr_t)edst,
338 M_PREPEND(m, 3, M_TRYWAIT);
344 cp = mtod(m, u_int8_t *);
345 *cp++ = ETHERTYPE_IPX_8022;
346 *cp++ = ETHERTYPE_IPX_8022;
353 struct iso88025_sockaddr_data *sd;
355 * For AF_UNSPEC sockaddr.sa_data must contain all of the
356 * mac information needed to send the packet. This allows
357 * full mac, llc, and source routing function to be controlled.
358 * llc and source routing information must already be in the
359 * mbuf provided, ac/fc are set in sa_data. sockaddr.sa_data
360 * should be an iso88025_sockaddr_data structure see iso88025.h
363 sd = (struct iso88025_sockaddr_data *)dst->sa_data;
366 (void)memcpy((caddr_t)edst, (caddr_t)sd->ether_dhost,
368 (void)memcpy((caddr_t)gen_th.iso88025_shost,
369 (caddr_t)sd->ether_shost, ISO88025_ADDR_LEN);
374 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
375 senderr(EAFNOSUPPORT);
382 if (snap_type != 0) {
384 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
387 l = mtod(m, struct llc *);
388 l->llc_control = LLC_UI;
389 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
390 l->llc_snap.org_code[0] =
391 l->llc_snap.org_code[1] =
392 l->llc_snap.org_code[2] = 0;
393 l->llc_snap.ether_type = htons(snap_type);
397 * Add local net header. If no space in first mbuf,
400 M_PREPEND(m, ISO88025_HDR_LEN + rif_len, M_DONTWAIT);
403 th = mtod(m, struct iso88025_header *);
404 bcopy((caddr_t)edst, (caddr_t)&gen_th.iso88025_dhost, ISO88025_ADDR_LEN);
406 /* Copy as much of the generic header as is needed into the mbuf */
407 memcpy(th, &gen_th, ISO88025_HDR_LEN + rif_len);
410 * If a simplex interface, and the packet is being sent to our
411 * Ethernet address or a broadcast address, loopback a copy.
412 * XXX To make a simplex device behave exactly like a duplex
413 * device, we should copy in the case of sending to our own
414 * ethernet address (thus letting the original actually appear
415 * on the wire). However, we don't do that here for security
416 * reasons and compatibility with the original behavior.
418 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
419 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
421 n = m_copy(m, 0, (int)M_COPYALL);
422 (void) if_simloop(ifp, n, dst->sa_family,
424 } else if (bcmp(th->iso88025_dhost, th->iso88025_shost,
425 ETHER_ADDR_LEN) == 0) {
426 (void) if_simloop(ifp, m, dst->sa_family,
432 IFQ_HANDOFF_ADJ(ifp, m, ISO88025_HDR_LEN + LLC_SNAPFRAMELEN, error);
434 printf("iso88025_output: packet dropped QFULL.\n");
447 * ISO 88025 de-encapsulation
450 iso88025_input(ifp, m)
454 struct iso88025_header *th;
460 * Do consistency checks to verify assumptions
461 * made by code past this point.
463 if ((m->m_flags & M_PKTHDR) == 0) {
464 if_printf(ifp, "discard frame w/o packet header\n");
469 if (m->m_pkthdr.rcvif == NULL) {
470 if_printf(ifp, "discard frame w/o interface pointer\n");
476 m = m_pullup(m, ISO88025_HDR_LEN);
481 th = mtod(m, struct iso88025_header *);
482 m->m_pkthdr.header = (void *)th;
485 * Discard packet if interface is not up.
487 if (!((ifp->if_flags & IFF_UP) &&
488 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
492 * Give bpf a chance at the packet.
497 * Interface marked for monitoring; discard packet.
499 if (ifp->if_flags & IFF_MONITOR) {
505 mac_create_mbuf_from_ifnet(ifp, m);
509 * Update interface statistics.
511 ifp->if_ibytes += m->m_pkthdr.len;
512 getmicrotime(&ifp->if_lastchange);
515 * Discard non local unicast packets when interface
516 * is in promiscuous mode.
518 if ((ifp->if_flags & IFF_PROMISC) &&
519 ((th->iso88025_dhost[0] & 1) == 0) &&
520 (bcmp(IF_LLADDR(ifp), (caddr_t) th->iso88025_dhost,
521 ISO88025_ADDR_LEN) != 0))
525 * Set mbuf flags for bcast/mcast.
527 if (th->iso88025_dhost[0] & 1) {
528 if (bcmp(iso88025_broadcastaddr, th->iso88025_dhost,
529 ISO88025_ADDR_LEN) == 0)
530 m->m_flags |= M_BCAST;
532 m->m_flags |= M_MCAST;
536 mac_hdr_len = ISO88025_HDR_LEN;
537 /* Check for source routing info */
538 if (th->iso88025_shost[0] & TR_RII)
539 mac_hdr_len += TR_RCF_RIFLEN(th->rcf);
541 /* Strip off ISO88025 header. */
542 m_adj(m, mac_hdr_len);
544 m = m_pullup(m, LLC_SNAPFRAMELEN);
549 l = mtod(m, struct llc *);
551 switch (l->llc_dsap) {
553 case ETHERTYPE_IPX_8022: /* Thanks a bunch Novell */
554 if ((l->llc_control != LLC_UI) ||
555 (l->llc_ssap != ETHERTYPE_IPX_8022)) {
560 th->iso88025_shost[0] &= ~(TR_RII);
565 case LLC_SNAP_LSAP: {
567 if ((l->llc_control != LLC_UI) ||
568 (l->llc_ssap != LLC_SNAP_LSAP)) {
573 if (l->llc_snap.org_code[0] != 0 ||
574 l->llc_snap.org_code[1] != 0 ||
575 l->llc_snap.org_code[2] != 0) {
580 type = ntohs(l->llc_snap.ether_type);
581 m_adj(m, LLC_SNAPFRAMELEN);
585 th->iso88025_shost[0] &= ~(TR_RII);
586 if ((m = ip_fastforward(m)) == NULL)
592 if (ifp->if_flags & IFF_NOARP)
597 #ifdef IPX_SNAP /* XXX: Not supported! */
599 th->iso88025_shost[0] &= ~(TR_RII);
602 #endif /* IPX_SNAP */
605 th->iso88025_shost[0] &= ~(TR_RII);
610 printf("iso88025_input: unexpected llc_snap ether_type 0x%02x\n", type);
618 switch (l->llc_control) {
625 if(m->m_len < ISO88025_ADDR_LEN)
630 l->llc_dsap = l->llc_ssap = 0;
631 /* Fall through to */
637 struct iso88025_sockaddr_data *th2;
643 if (th->iso88025_shost[0] & TR_RII) { /* XXX */
644 printf("iso88025_input: dropping source routed LLC_TEST\n");
647 l->llc_dsap = l->llc_ssap;
649 if (m->m_flags & (M_BCAST | M_MCAST))
650 bcopy((caddr_t)IF_LLADDR(ifp),
651 (caddr_t)th->iso88025_dhost,
653 sa.sa_family = AF_UNSPEC;
654 sa.sa_len = sizeof(sa);
655 th2 = (struct iso88025_sockaddr_data *)sa.sa_data;
656 for (i = 0; i < ISO88025_ADDR_LEN; i++) {
657 th2->ether_shost[i] = c = th->iso88025_dhost[i];
658 th2->ether_dhost[i] = th->iso88025_dhost[i] =
659 th->iso88025_shost[i];
660 th->iso88025_shost[i] = c;
663 th2->fc = TR_LLC_FRAME;
664 ifp->if_output(ifp, m, &sa, NULL);
668 printf("iso88025_input: unexpected llc control 0x%02x\n", l->llc_control);
676 printf("iso88025_input: unknown dsap 0x%x\n", l->llc_dsap);
682 netisr_dispatch(isr, m);
693 iso88025_resolvemulti (ifp, llsa, sa)
695 struct sockaddr **llsa;
698 struct sockaddr_dl *sdl;
699 struct sockaddr_in *sin;
701 struct sockaddr_in6 *sin6;
705 switch(sa->sa_family) {
708 * No mapping needed. Just check that it's a valid MC address.
710 sdl = (struct sockaddr_dl *)sa;
711 e_addr = LLADDR(sdl);
712 if ((e_addr[0] & 1) != 1) {
713 return (EADDRNOTAVAIL);
720 sin = (struct sockaddr_in *)sa;
721 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
722 return (EADDRNOTAVAIL);
724 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
728 sdl->sdl_len = sizeof *sdl;
729 sdl->sdl_family = AF_LINK;
730 sdl->sdl_index = ifp->if_index;
731 sdl->sdl_type = IFT_ISO88025;
732 sdl->sdl_alen = ISO88025_ADDR_LEN;
733 e_addr = LLADDR(sdl);
734 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
735 *llsa = (struct sockaddr *)sdl;
740 sin6 = (struct sockaddr_in6 *)sa;
741 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
743 * An IP6 address of 0 means listen to all
744 * of the Ethernet multicast address used for IP6.
745 * (This is used for multicast routers.)
747 ifp->if_flags |= IFF_ALLMULTI;
751 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
752 return (EADDRNOTAVAIL);
754 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
758 sdl->sdl_len = sizeof *sdl;
759 sdl->sdl_family = AF_LINK;
760 sdl->sdl_index = ifp->if_index;
761 sdl->sdl_type = IFT_ISO88025;
762 sdl->sdl_alen = ISO88025_ADDR_LEN;
763 e_addr = LLADDR(sdl);
764 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
765 *llsa = (struct sockaddr *)sdl;
771 * Well, the text isn't quite right, but it's the name
774 return (EAFNOSUPPORT);
780 MALLOC_DEFINE(M_ISO88025, "arpcom", "802.5 interface internals");
783 iso88025_alloc(u_char type, struct ifnet *ifp)
787 ac = malloc(sizeof(struct arpcom), M_ISO88025, M_WAITOK | M_ZERO);
794 iso88025_free(void *com, u_char type)
797 free(com, M_ISO88025);
801 iso88025_modevent(module_t mod, int type, void *data)
806 if_register_com_alloc(IFT_ISO88025, iso88025_alloc,
810 if_deregister_com_alloc(IFT_ISO88025);
819 static moduledata_t iso88025_mod = {
825 DECLARE_MODULE(iso88025, iso88025_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
826 MODULE_VERSION(iso88025, 1);