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>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
61 #include <net/if_arp.h>
63 #include <net/iso88025.h>
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/if_ether.h>
71 #include <netinet6/nd6.h>
75 #include <netipx/ipx.h>
76 #include <netipx/ipx_if.h>
81 #include <machine/md_var.h>
84 #include <vm/vm_param.h>
87 #include <net/iso88025.h>
89 #define IFP2AC(IFP) ((struct arpcom *)IFP)
92 iso88025_ifattach(struct ifnet *ifp)
94 register struct ifaddr *ifa = NULL;
95 register struct sockaddr_dl *sdl;
97 ifp->if_type = IFT_ISO88025;
98 ifp->if_addrlen = ISO88025_ADDR_LEN;
99 ifp->if_hdrlen = ISO88025_HDR_LEN;
100 if (ifp->if_baudrate == 0)
101 ifp->if_baudrate = TR_16MBPS; /* 16Mbit should be a safe default */
102 if (ifp->if_mtu == 0)
103 ifp->if_mtu = ISO88025_DEFAULT_MTU;
104 ifp->if_broadcastaddr = etherbroadcastaddr;
106 ifa = ifaddr_byindex(ifp->if_index);
108 printf("iso88025_ifattach: no lladdr!\n");
111 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
112 sdl->sdl_type = IFT_ISO88025;
113 sdl->sdl_alen = ifp->if_addrlen;
114 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
118 * Perform common duties while detaching a Token Ring interface
121 iso88025_ifdetach(ifp, bpf)
132 iso88025_ioctl(struct ifnet *ifp, int command, caddr_t data)
134 struct ifaddr *ifa = (struct ifaddr *) data;
135 struct ifreq *ifr = (struct ifreq *) data;
140 ifp->if_flags |= IFF_UP;
142 switch (ifa->ifa_addr->sa_family) {
145 ifp->if_init(ifp->if_softc); /* before arpwhohas */
146 arp_ifinit(ifp, ifa);
151 * XXX - This code is probably wrong
155 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
156 struct arpcom *ac = IFP2AC(ifp);
158 if (ipx_nullhost(*ina))
163 bcopy((caddr_t) ina->x_host.c_host,
164 (caddr_t) ac->ac_enaddr,
165 sizeof(ac->ac_enaddr));
171 ifp->if_init(ifp->if_softc);
176 ifp->if_init(ifp->if_softc);
185 sa = (struct sockaddr *) & ifr->ifr_data;
186 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
187 (caddr_t) sa->sa_data, ISO88025_ADDR_LEN);
193 * Set the interface MTU.
195 if (ifr->ifr_mtu > ISO88025_MAX_MTU) {
198 ifp->if_mtu = ifr->ifr_mtu;
206 * ISO88025 encapsulation
209 iso88025_output(ifp, m, dst, rt0)
212 struct sockaddr *dst;
215 u_int16_t snap_type = 0;
216 int loop_copy = 0, error = 0, rif_len = 0;
217 u_char edst[ISO88025_ADDR_LEN];
218 struct iso88025_header *th;
219 struct iso88025_header gen_th;
220 struct sockaddr_dl *sdl = NULL;
222 struct arpcom *ac = (struct arpcom *)ifp;
224 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
226 getmicrotime(&ifp->if_lastchange);
230 if ((rt->rt_flags & RTF_UP) == 0) {
231 rt0 = rt = rtalloc1(dst, 1, 0UL);
235 senderr(EHOSTUNREACH);
237 if (rt->rt_flags & RTF_GATEWAY) {
238 if (rt->rt_gwroute == 0)
240 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
241 rtfree(rt); rt = rt0;
242 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
244 if ((rt = rt->rt_gwroute) == 0)
245 senderr(EHOSTUNREACH);
248 if (rt->rt_flags & RTF_REJECT)
249 if (rt->rt_rmx.rmx_expire == 0 ||
250 time_second < rt->rt_rmx.rmx_expire)
251 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
254 /* Calculate routing info length based on arp table entry */
255 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway))
256 if (sdl->sdl_rcf != NULL)
257 rif_len = TR_RCF_RIFLEN(sdl->sdl_rcf);
259 /* Generate a generic 802.5 header for the packet */
261 gen_th.fc = TR_LLC_FRAME;
262 (void)memcpy((caddr_t)gen_th.iso88025_shost, (caddr_t)ac->ac_enaddr,
263 sizeof(ac->ac_enaddr));
265 gen_th.iso88025_shost[0] |= TR_RII;
267 gen_th.rcf = sdl->sdl_rcf;
268 (void)memcpy((caddr_t)gen_th.rd,
269 (caddr_t)sdl->sdl_route, rif_len - 2);
273 switch (dst->sa_family) {
276 if (!arpresolve(ifp, rt, m, dst, edst, rt0))
277 return (0); /* if not yet resolved */
278 snap_type = ETHERTYPE_IP;
284 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
285 /* Something bad happened */
288 snap_type = ETHERTYPE_IPV6;
298 bcopy((caddr_t)&(satoipx_addr(dst).x_host), (caddr_t)edst,
301 M_PREPEND(m, 3, M_TRYWAIT);
307 cp = mtod(m, u_int8_t *);
308 *cp++ = ETHERTYPE_IPX_8022;
309 *cp++ = ETHERTYPE_IPX_8022;
316 struct iso88025_sockaddr_data *sd;
318 * For AF_UNSPEC sockaddr.sa_data must contain all of the
319 * mac information needed to send the packet. This allows
320 * full mac, llc, and source routing function to be controlled.
321 * llc and source routing information must already be in the
322 * mbuf provided, ac/fc are set in sa_data. sockaddr.sa_data
323 * should be a iso88025_sockaddr_data structure see iso88025.h
326 sd = (struct iso88025_sockaddr_data *)dst->sa_data;
329 (void)memcpy((caddr_t)edst, (caddr_t)sd->ether_dhost,
330 sizeof(sd->ether_dhost));
331 (void)memcpy((caddr_t)gen_th.iso88025_shost,
332 (caddr_t)sd->ether_shost, sizeof(sd->ether_shost));
337 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
339 senderr(EAFNOSUPPORT);
343 if (snap_type != 0) {
345 M_PREPEND(m, sizeof (struct llc), M_DONTWAIT);
348 l = mtod(m, struct llc *);
349 l->llc_un.type_snap.ether_type = htons(snap_type);
350 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
351 l->llc_un.type_snap.control = LLC_UI;
352 l->llc_un.type_snap.org_code[0] =
353 l->llc_un.type_snap.org_code[1] =
354 l->llc_un.type_snap.org_code[2] = 0;
358 * Add local net header. If no space in first mbuf,
361 M_PREPEND(m, ISO88025_HDR_LEN + rif_len, M_DONTWAIT);
365 (void)memcpy((caddr_t)&gen_th.iso88025_dhost, (caddr_t)edst,
368 /* Copy as much of the generic header as is needed into the mbuf */
369 th = mtod(m, struct iso88025_header *);
370 memcpy(th, &gen_th, ISO88025_HDR_LEN + rif_len);
373 * If a simplex interface, and the packet is being sent to our
374 * Ethernet address or a broadcast address, loopback a copy.
375 * XXX To make a simplex device behave exactly like a duplex
376 * device, we should copy in the case of sending to our own
377 * ethernet address (thus letting the original actually appear
378 * on the wire). However, we don't do that here for security
379 * reasons and compatibility with the original behavior.
381 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
382 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
384 n = m_copy(m, 0, (int)M_COPYALL);
385 (void)if_simloop(ifp, n, dst->sa_family,
388 if (bcmp(th->iso88025_dhost, th->iso88025_shost,
389 ETHER_ADDR_LEN) == 0) {
390 (void)if_simloop(ifp, m, dst->sa_family,
396 if (! IF_HANDOFF_ADJ(&ifp->if_snd, m, ifp, ISO88025_HDR_LEN + (sizeof(struct llc))) ) {
397 printf("iso88025_output: packet dropped QFULL.\n");
409 * ISO 88025 de-encapsulation
412 iso88025_input(ifp, th, m)
414 struct iso88025_header *th;
417 register struct ifqueue *inq;
418 register struct llc *l;
420 if ((ifp->if_flags & IFF_UP) == 0) {
425 getmicrotime(&ifp->if_lastchange);
426 ifp->if_ibytes += m->m_pkthdr.len + sizeof(*th);
428 if (th->iso88025_dhost[0] & 1) {
429 if (bcmp((caddr_t)etherbroadcastaddr,
430 (caddr_t)th->iso88025_dhost,
431 sizeof(etherbroadcastaddr)) == 0)
432 m->m_flags |= M_BCAST;
434 m->m_flags |= M_MCAST;
438 l = mtod(m, struct llc *);
440 switch (l->llc_dsap) {
442 case ETHERTYPE_IPX_8022: /* Thanks a bunch Novell */
443 if ((l->llc_control != LLC_UI) ||
444 (l->llc_ssap != ETHERTYPE_IPX_8022))
447 th->iso88025_shost[0] &= ~(TR_RII);
449 schednetisr(NETISR_IPX);
453 case LLC_SNAP_LSAP: {
455 if ((l->llc_control != LLC_UI) ||
456 (l->llc_ssap != LLC_SNAP_LSAP))
459 if (l->llc_un.type_snap.org_code[0] != 0 ||
460 l->llc_un.type_snap.org_code[1] != 0 ||
461 l->llc_un.type_snap.org_code[2] != 0)
464 type = ntohs(l->llc_un.type_snap.ether_type);
465 m_adj(m, sizeof(struct llc));
469 th->iso88025_shost[0] &= ~(TR_RII);
470 if (ipflow_fastforward(m))
472 schednetisr(NETISR_IP);
477 if (ifp->if_flags & IFF_NOARP)
479 schednetisr(NETISR_ARP);
483 #ifdef IPX_SNAP /* XXX: Not supported! */
485 th->iso88025_shost[0] &= ~(TR_RII);
486 schednetisr(NETISR_IPX);
489 #endif /* IPX_SNAP */
493 th->iso88025_shost[0] &= ~(TR_RII);
494 schednetisr(NETISR_IPV6);
500 printf("iso88025_input: unexpected llc_snap ether_type 0x%02x\n", type);
507 switch (l->llc_control) {
513 if(m->m_len < ISO88025_ADDR_LEN)
518 l->llc_dsap = l->llc_ssap = 0;
519 /* Fall through to */
524 struct arpcom *ac = (struct arpcom *)ifp;
525 struct iso88025_sockaddr_data *th2;
527 u_char c = l->llc_dsap;
529 if (th->iso88025_shost[0] & TR_RII) { /* XXX */
530 printf("iso88025_input: dropping source routed LLC_TEST\n");
534 l->llc_dsap = l->llc_ssap;
536 if (m->m_flags & (M_BCAST | M_MCAST))
537 bcopy((caddr_t)ac->ac_enaddr,
538 (caddr_t)th->iso88025_dhost,
540 sa.sa_family = AF_UNSPEC;
541 sa.sa_len = sizeof(sa);
542 th2 = (struct iso88025_sockaddr_data *)sa.sa_data;
543 for (i = 0; i < ISO88025_ADDR_LEN; i++) {
544 th2->ether_shost[i] = c = th->iso88025_dhost[i];
545 th2->ether_dhost[i] = th->iso88025_dhost[i] =
546 th->iso88025_shost[i];
547 th->iso88025_shost[i] = c;
550 th2->fc = TR_LLC_FRAME;
551 ifp->if_output(ifp, m, &sa, NULL);
555 printf("iso88025_input: unexpected llc control 0x%02x\n", l->llc_control);
561 printf("iso88025_input: unknown dsap 0x%x\n", l->llc_dsap);
568 if (! IF_HANDOFF(inq, m, NULL))
569 printf("iso88025_input: Packet dropped (Queue full).\n");