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"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
56 #include <net/if_var.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>
78 #include <security/mac/mac_framework.h>
80 static const u_char iso88025_broadcastaddr[ISO88025_ADDR_LEN] =
81 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
83 static int iso88025_resolvemulti (struct ifnet *, struct sockaddr **,
86 #define senderr(e) do { error = (e); goto bad; } while (0)
89 * Perform common duties while attaching to interface list
92 iso88025_ifattach(struct ifnet *ifp, const u_int8_t *lla, int bpf)
95 struct sockaddr_dl *sdl;
99 ifp->if_type = IFT_ISO88025;
100 ifp->if_addrlen = ISO88025_ADDR_LEN;
101 ifp->if_hdrlen = ISO88025_HDR_LEN;
103 if_attach(ifp); /* Must be called before additional assignments */
105 ifp->if_output = iso88025_output;
106 ifp->if_input = iso88025_input;
107 ifp->if_resolvemulti = iso88025_resolvemulti;
108 ifp->if_broadcastaddr = iso88025_broadcastaddr;
110 if (ifp->if_baudrate == 0)
111 ifp->if_baudrate = TR_16MBPS; /* 16Mbit should be a safe default */
112 if (ifp->if_mtu == 0)
113 ifp->if_mtu = ISO88025_DEFAULT_MTU;
116 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
118 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
119 sdl->sdl_type = IFT_ISO88025;
120 sdl->sdl_alen = ifp->if_addrlen;
121 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
124 bpfattach(ifp, DLT_IEEE802, ISO88025_HDR_LEN);
130 * Perform common duties while detaching a Token Ring interface
133 iso88025_ifdetach(ifp, bpf)
147 iso88025_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
153 ifa = (struct ifaddr *) data;
154 ifr = (struct ifreq *) data;
159 ifp->if_flags |= IFF_UP;
161 switch (ifa->ifa_addr->sa_family) {
164 ifp->if_init(ifp->if_softc); /* before arpwhohas */
165 arp_ifinit(ifp, ifa);
169 ifp->if_init(ifp->if_softc);
175 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
181 * Set the interface MTU.
183 if (ifr->ifr_mtu > ISO88025_MAX_MTU) {
186 ifp->if_mtu = ifr->ifr_mtu;
190 error = EINVAL; /* XXX netbsd has ENOTTY??? */
198 * ISO88025 encapsulation
201 iso88025_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
204 u_int16_t snap_type = 0;
205 int loop_copy = 0, error = 0, rif_len = 0;
206 u_char edst[ISO88025_ADDR_LEN];
207 struct iso88025_header *th;
208 struct iso88025_header gen_th;
209 struct sockaddr_dl *sdl = NULL;
210 struct rtentry *rt0 = NULL;
214 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
216 error = mac_ifnet_check_transmit(ifp, m);
221 if (ifp->if_flags & IFF_MONITOR)
223 if (!((ifp->if_flags & IFF_UP) &&
224 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
226 getmicrotime(&ifp->if_lastchange);
228 /* Calculate routing info length based on arp table entry */
229 /* XXX any better way to do this ? */
231 if (rt0 && (sdl = (struct sockaddr_dl *)rt0->rt_gateway))
232 if (SDL_ISO88025(sdl)->trld_rcf != 0)
233 rif_len = TR_RCF_RIFLEN(SDL_ISO88025(sdl)->trld_rcf);
235 /* Generate a generic 802.5 header for the packet */
237 gen_th.fc = TR_LLC_FRAME;
238 (void)memcpy((caddr_t)gen_th.iso88025_shost, IF_LLADDR(ifp),
241 gen_th.iso88025_shost[0] |= TR_RII;
243 gen_th.rcf = SDL_ISO88025(sdl)->trld_rcf;
244 (void)memcpy((caddr_t)gen_th.rd,
245 (caddr_t)SDL_ISO88025(sdl)->trld_route,
250 switch (dst->sa_family) {
253 error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
255 return (error == EWOULDBLOCK ? 0 : error);
256 snap_type = ETHERTYPE_IP;
261 ah = mtod(m, struct arphdr *);
262 ah->ar_hrd = htons(ARPHRD_IEEE802);
264 loop_copy = -1; /* if this is for us, don't do it */
266 switch(ntohs(ah->ar_op)) {
267 case ARPOP_REVREQUEST:
269 snap_type = ETHERTYPE_REVARP;
274 snap_type = ETHERTYPE_ARP;
278 if (m->m_flags & M_BCAST)
279 bcopy(ifp->if_broadcastaddr, edst, ISO88025_ADDR_LEN);
281 bcopy(ar_tha(ah), edst, ISO88025_ADDR_LEN);
288 error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
290 return (error == EWOULDBLOCK ? 0 : error);
291 snap_type = ETHERTYPE_IPV6;
296 const struct iso88025_sockaddr_data *sd;
298 * For AF_UNSPEC sockaddr.sa_data must contain all of the
299 * mac information needed to send the packet. This allows
300 * full mac, llc, and source routing function to be controlled.
301 * llc and source routing information must already be in the
302 * mbuf provided, ac/fc are set in sa_data. sockaddr.sa_data
303 * should be an iso88025_sockaddr_data structure see iso88025.h
306 sd = (const struct iso88025_sockaddr_data *)dst->sa_data;
309 (void)memcpy(edst, sd->ether_dhost, ISO88025_ADDR_LEN);
310 (void)memcpy(gen_th.iso88025_shost, sd->ether_shost,
316 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
317 senderr(EAFNOSUPPORT);
324 if (snap_type != 0) {
326 M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
329 l = mtod(m, struct llc *);
330 l->llc_control = LLC_UI;
331 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
332 l->llc_snap.org_code[0] =
333 l->llc_snap.org_code[1] =
334 l->llc_snap.org_code[2] = 0;
335 l->llc_snap.ether_type = htons(snap_type);
339 * Add local net header. If no space in first mbuf,
342 M_PREPEND(m, ISO88025_HDR_LEN + rif_len, M_NOWAIT);
345 th = mtod(m, struct iso88025_header *);
346 bcopy((caddr_t)edst, (caddr_t)&gen_th.iso88025_dhost, ISO88025_ADDR_LEN);
348 /* Copy as much of the generic header as is needed into the mbuf */
349 memcpy(th, &gen_th, ISO88025_HDR_LEN + rif_len);
352 * If a simplex interface, and the packet is being sent to our
353 * Ethernet address or a broadcast address, loopback a copy.
354 * XXX To make a simplex device behave exactly like a duplex
355 * device, we should copy in the case of sending to our own
356 * ethernet address (thus letting the original actually appear
357 * on the wire). However, we don't do that here for security
358 * reasons and compatibility with the original behavior.
360 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
361 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
363 n = m_copy(m, 0, (int)M_COPYALL);
364 (void) if_simloop(ifp, n, dst->sa_family,
366 } else if (bcmp(th->iso88025_dhost, th->iso88025_shost,
367 ETHER_ADDR_LEN) == 0) {
368 (void) if_simloop(ifp, m, dst->sa_family,
374 IFQ_HANDOFF_ADJ(ifp, m, ISO88025_HDR_LEN + LLC_SNAPFRAMELEN, error);
376 printf("iso88025_output: packet dropped QFULL.\n");
377 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
382 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
389 * ISO 88025 de-encapsulation
392 iso88025_input(ifp, m)
396 struct iso88025_header *th;
402 * Do consistency checks to verify assumptions
403 * made by code past this point.
405 if ((m->m_flags & M_PKTHDR) == 0) {
406 if_printf(ifp, "discard frame w/o packet header\n");
407 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
411 if (m->m_pkthdr.rcvif == NULL) {
412 if_printf(ifp, "discard frame w/o interface pointer\n");
413 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
418 m = m_pullup(m, ISO88025_HDR_LEN);
420 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
423 th = mtod(m, struct iso88025_header *);
426 * Discard packet if interface is not up.
428 if (!((ifp->if_flags & IFF_UP) &&
429 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
433 * Give bpf a chance at the packet.
438 * Interface marked for monitoring; discard packet.
440 if (ifp->if_flags & IFF_MONITOR) {
446 mac_ifnet_create_mbuf(ifp, m);
450 * Update interface statistics.
452 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
453 getmicrotime(&ifp->if_lastchange);
456 * Discard non local unicast packets when interface
457 * is in promiscuous mode.
459 if ((ifp->if_flags & IFF_PROMISC) &&
460 ((th->iso88025_dhost[0] & 1) == 0) &&
461 (bcmp(IF_LLADDR(ifp), (caddr_t) th->iso88025_dhost,
462 ISO88025_ADDR_LEN) != 0))
466 * Set mbuf flags for bcast/mcast.
468 if (th->iso88025_dhost[0] & 1) {
469 if (bcmp(iso88025_broadcastaddr, th->iso88025_dhost,
470 ISO88025_ADDR_LEN) == 0)
471 m->m_flags |= M_BCAST;
473 m->m_flags |= M_MCAST;
474 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
477 mac_hdr_len = ISO88025_HDR_LEN;
478 /* Check for source routing info */
479 if (th->iso88025_shost[0] & TR_RII)
480 mac_hdr_len += TR_RCF_RIFLEN(th->rcf);
482 /* Strip off ISO88025 header. */
483 m_adj(m, mac_hdr_len);
485 m = m_pullup(m, LLC_SNAPFRAMELEN);
487 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
490 l = mtod(m, struct llc *);
492 switch (l->llc_dsap) {
493 case LLC_SNAP_LSAP: {
495 if ((l->llc_control != LLC_UI) ||
496 (l->llc_ssap != LLC_SNAP_LSAP)) {
497 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
501 if (l->llc_snap.org_code[0] != 0 ||
502 l->llc_snap.org_code[1] != 0 ||
503 l->llc_snap.org_code[2] != 0) {
504 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
508 type = ntohs(l->llc_snap.ether_type);
509 m_adj(m, LLC_SNAPFRAMELEN);
513 th->iso88025_shost[0] &= ~(TR_RII);
518 if (ifp->if_flags & IFF_NOARP)
525 th->iso88025_shost[0] &= ~(TR_RII);
530 printf("iso88025_input: unexpected llc_snap ether_type 0x%02x\n", type);
531 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
538 switch (l->llc_control) {
540 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
545 if(m->m_len < ISO88025_ADDR_LEN)
550 l->llc_dsap = l->llc_ssap = 0;
551 /* Fall through to */
556 struct iso88025_sockaddr_data *th2;
562 if (th->iso88025_shost[0] & TR_RII) { /* XXX */
563 printf("iso88025_input: dropping source routed LLC_TEST\n");
566 l->llc_dsap = l->llc_ssap;
568 if (m->m_flags & (M_BCAST | M_MCAST))
569 bcopy((caddr_t)IF_LLADDR(ifp),
570 (caddr_t)th->iso88025_dhost,
572 sa.sa_family = AF_UNSPEC;
573 sa.sa_len = sizeof(sa);
574 th2 = (struct iso88025_sockaddr_data *)sa.sa_data;
575 for (i = 0; i < ISO88025_ADDR_LEN; i++) {
576 th2->ether_shost[i] = c = th->iso88025_dhost[i];
577 th2->ether_dhost[i] = th->iso88025_dhost[i] =
578 th->iso88025_shost[i];
579 th->iso88025_shost[i] = c;
582 th2->fc = TR_LLC_FRAME;
583 ifp->if_output(ifp, m, &sa, NULL);
587 printf("iso88025_input: unexpected llc control 0x%02x\n", l->llc_control);
588 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
595 printf("iso88025_input: unknown dsap 0x%x\n", l->llc_dsap);
596 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
601 M_SETFIB(m, ifp->if_fib);
602 netisr_dispatch(isr, m);
606 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
613 iso88025_resolvemulti (ifp, llsa, sa)
615 struct sockaddr **llsa;
618 struct sockaddr_dl *sdl;
620 struct sockaddr_in *sin;
623 struct sockaddr_in6 *sin6;
627 switch(sa->sa_family) {
630 * No mapping needed. Just check that it's a valid MC address.
632 sdl = (struct sockaddr_dl *)sa;
633 e_addr = LLADDR(sdl);
634 if ((e_addr[0] & 1) != 1) {
635 return (EADDRNOTAVAIL);
642 sin = (struct sockaddr_in *)sa;
643 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
644 return (EADDRNOTAVAIL);
646 sdl = link_init_sdl(ifp, *llsa, IFT_ISO88025);
647 sdl->sdl_alen = ISO88025_ADDR_LEN;
648 e_addr = LLADDR(sdl);
649 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
650 *llsa = (struct sockaddr *)sdl;
655 sin6 = (struct sockaddr_in6 *)sa;
656 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
658 * An IP6 address of 0 means listen to all
659 * of the Ethernet multicast address used for IP6.
660 * (This is used for multicast routers.)
662 ifp->if_flags |= IFF_ALLMULTI;
666 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
667 return (EADDRNOTAVAIL);
669 sdl = link_init_sdl(ifp, *llsa, IFT_ISO88025);
670 sdl->sdl_alen = ISO88025_ADDR_LEN;
671 e_addr = LLADDR(sdl);
672 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
673 *llsa = (struct sockaddr *)sdl;
679 * Well, the text isn't quite right, but it's the name
682 return (EAFNOSUPPORT);
688 static moduledata_t iso88025_mod = {
692 DECLARE_MODULE(iso88025, iso88025_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
693 MODULE_VERSION(iso88025, 1);