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
33 * $FreeBSD: src/sys/net/if_iso88025subr.c,v 1.7.2.7 2002/06/18 00:15:31 kbyanc Exp $
34 * $DragonFly: src/sys/net/Attic/if_iso88025subr.c,v 1.6 2004/01/06 03:17:25 dillon Exp $
40 * General ISO 802.5 (Token Ring) support routines
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/sysctl.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
58 #include <net/if_llc.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
62 #include <net/if_arp.h>
64 #include <net/iso88025.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/if_ether.h>
74 #include <machine/clock.h>
75 #include <machine/md_var.h>
77 #include <bus/isa/i386/isa_device.h>
80 #include <vm/vm_param.h>
83 #include <sys/kernel.h>
84 #include <net/iso88025.h>
87 iso88025_ifattach(struct ifnet *ifp)
89 struct ifaddr *ifa = NULL;
90 struct sockaddr_dl *sdl;
92 ifp->if_type = IFT_ISO88025;
93 ifp->if_addrlen = ISO88025_ADDR_LEN;
94 ifp->if_hdrlen = ISO88025_HDR_LEN;
95 if (ifp->if_baudrate == 0)
96 ifp->if_baudrate = TR_16MBPS; /* 16Mbit should be a safe default */
98 ifp->if_mtu = ISO88025_DEFAULT_MTU;
100 ifa = ifnet_addrs[ifp->if_index - 1];
102 printf("iso88025_ifattach: no lladdr!\n");
105 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
106 sdl->sdl_type = IFT_ISO88025;
107 sdl->sdl_alen = ifp->if_addrlen;
108 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
112 iso88025_ioctl(struct ifnet *ifp, int command, caddr_t data)
114 struct ifaddr *ifa = (struct ifaddr *) data;
115 struct ifreq *ifr = (struct ifreq *) data;
120 ifp->if_flags |= IFF_UP;
122 switch (ifa->ifa_addr->sa_family) {
125 ifp->if_init(ifp->if_softc); /* before arpwhohas */
126 arp_ifinit(ifp, ifa);
130 ifp->if_init(ifp->if_softc);
139 sa = (struct sockaddr *) & ifr->ifr_data;
140 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
141 (caddr_t) sa->sa_data, ISO88025_ADDR_LEN);
147 * Set the interface MTU.
149 if (ifr->ifr_mtu > ISO88025_MAX_MTU) {
152 ifp->if_mtu = ifr->ifr_mtu;
160 * ISO88025 encapsulation
163 iso88025_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, struct rtentry *rt0)
165 struct iso88025_header *th;
166 struct iso88025_header gen_th;
167 struct iso88025_sockaddr_data *sd = (struct iso88025_sockaddr_data *)dst->sa_data;
169 struct sockaddr_dl *sdl = NULL;
170 int s, error = 0, rif_len = 0;
173 int len = m->m_pkthdr.len, loop_copy = 0;
174 struct arpcom *ac = (struct arpcom *)ifp;
176 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
180 if ((rt->rt_flags & RTF_UP) == 0) {
181 rt0 = rt = rtalloc1(dst, 1, 0UL);
185 senderr(EHOSTUNREACH);
187 if (rt->rt_flags & RTF_GATEWAY) {
188 if (rt->rt_gwroute == 0)
190 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
191 rtfree(rt); rt = rt0;
192 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
194 if ((rt = rt->rt_gwroute) == 0)
195 senderr(EHOSTUNREACH);
198 if (rt->rt_flags & RTF_REJECT)
199 if (rt->rt_rmx.rmx_expire == 0 ||
200 time_second < rt->rt_rmx.rmx_expire)
201 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
204 /* Calculate routing info length based on arp table entry */
205 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway))
206 if (SDL_ISO88025(sdl)->trld_rcf != NULL)
207 rif_len = TR_RCF_RIFLEN(SDL_ISO88025(sdl)->trld_rcf);
209 /* Generate a generic 802.5 header for the packet */
211 gen_th.fc = TR_LLC_FRAME;
212 memcpy(gen_th.iso88025_shost, ac->ac_enaddr, sizeof(ac->ac_enaddr));
214 gen_th.iso88025_shost[0] |= TR_RII;
216 gen_th.rcf = sdl->sdl_rcf;
217 memcpy(gen_th.rd, sdl->sdl_route, rif_len - 2);
222 switch (dst->sa_family) {
225 if (!arpresolve(ifp, rt, m, dst, edst, rt0))
226 return (0); /* if not yet resolved */
227 /* Add LLC and SNAP headers */
228 M_PREPEND(m, 8, M_DONTWAIT);
231 l = mtod(m, struct llc *);
232 l->llc_un.type_snap.ether_type = htons(ETHERTYPE_IP);
233 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
234 l->llc_un.type_snap.control = LLC_UI;
235 l->llc_un.type_snap.org_code[0] = 0x0;
236 l->llc_un.type_snap.org_code[1] = 0x0;
237 l->llc_un.type_snap.org_code[2] = 0x0;
238 memcpy(gen_th.iso88025_dhost, edst, sizeof(edst));
244 * For AF_UNSPEC sockaddr.sa_data must contain all of the
245 * mac information needed to send the packet. This allows
246 * full mac, llc, and source routing function to be controlled.
247 * llc and source routing information must already be in the
248 * mbuf provided, ac/fc are set in sa_data. sockaddr.sa_data
249 * should be a iso88025_sockaddr_data structure see iso88025.h
252 sd = (struct iso88025_sockaddr_data *)dst->sa_data;
255 memcpy(gen_th.iso88025_dhost, sd->ether_dhost, sizeof(sd->ether_dhost));
256 memcpy(gen_th.iso88025_shost, sd->ether_shost, sizeof(sd->ether_shost));
261 printf("%s: can't handle af%d\n", ifp->if_xname,
263 senderr(EAFNOSUPPORT);
267 * Add local net header. If no space in first mbuf,
271 M_PREPEND(m, ISO88025_HDR_LEN + rif_len, M_DONTWAIT);
275 /* Copy as much of the generic header as is needed into the mbuf */
276 th = mtod(m, struct iso88025_header *);
277 memcpy(th, &gen_th, ISO88025_HDR_LEN + rif_len);
280 * If a simplex interface, and the packet is being sent to our
281 * Ethernet address or a broadcast address, loopback a copy.
282 * XXX To make a simplex device behave exactly like a duplex
283 * device, we should copy in the case of sending to our own
284 * ethernet address (thus letting the original actually appear
285 * on the wire). However, we don't do that here for security
286 * reasons and compatibility with the original behavior.
288 if ((ifp->if_flags & IFF_SIMPLEX) &&
290 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
291 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
292 (void) if_simloop(ifp,
293 n, dst->sa_family, ISO88025_HDR_LEN);
294 } else if (bcmp(th->iso88025_dhost,
295 th->iso88025_shost, ETHER_ADDR_LEN) == 0) {
296 (void) if_simloop(ifp,
297 m, dst->sa_family, ISO88025_HDR_LEN);
304 * Queue message on interface, and start output if interface
307 if (IF_QFULL(&ifp->if_snd)) {
308 printf("iso88025_output: packet dropped QFULL.\n");
309 IF_DROP(&ifp->if_snd);
313 if (m->m_flags & M_MCAST)
315 IF_ENQUEUE(&ifp->if_snd, m);
316 if ((ifp->if_flags & IFF_OACTIVE) == 0)
317 (*ifp->if_start)(ifp);
319 ifp->if_obytes += len + ISO88025_HDR_LEN + 8;
329 * ISO 88025 de-encapsulation
332 iso88025_input(struct ifnet *ifp, struct iso88025_header *th, struct mbuf *m)
336 struct llc *l = mtod(m, struct llc *);
338 if ((ifp->if_flags & IFF_UP) == 0) {
343 switch (l->llc_control) {
350 struct arpcom *ac = (struct arpcom *)ifp;
351 struct iso88025_sockaddr_data *th2;
353 u_char c = l->llc_dsap;
355 if (th->iso88025_shost[0] & TR_RII) { /* XXX */
356 printf("iso88025_input: dropping source routed LLC_TEST\n");
360 l->llc_dsap = l->llc_ssap;
362 if (m->m_flags & (M_BCAST | M_MCAST))
363 bcopy((caddr_t)ac->ac_enaddr,
364 (caddr_t)th->iso88025_dhost, ISO88025_ADDR_LEN);
365 sa.sa_family = AF_UNSPEC;
366 sa.sa_len = sizeof(sa);
367 th2 = (struct iso88025_sockaddr_data *)sa.sa_data;
368 for (i = 0; i < ISO88025_ADDR_LEN; i++) {
369 th2->ether_shost[i] = c = th->iso88025_dhost[i];
370 th2->ether_dhost[i] = th->iso88025_dhost[i] = th->iso88025_shost[i];
371 th->iso88025_shost[i] = c;
374 th2->fc = TR_LLC_FRAME;
375 ifp->if_output(ifp, m, &sa, NULL);
379 printf("iso88025_input: unexpected llc control 0x%02x\n", l->llc_control);
384 m->m_pkthdr.len -= 8;
386 m->m_data += 8; /* Length of LLC header in our case */
388 ifp->if_ibytes += m->m_pkthdr.len + sizeof(*th);
389 if (th->iso88025_dhost[0] & 1) {
390 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)th->iso88025_dhost, sizeof(etherbroadcastaddr)) == 0)
391 m->m_flags |= M_BCAST;
393 m->m_flags |= M_MCAST;
395 if (m->m_flags & (M_BCAST|M_MCAST))
398 ether_type = ntohs(l->llc_un.type_snap.ether_type);
400 switch (ether_type) {
403 th->iso88025_shost[0] &= ~(TR_RII);
404 if (ipflow_fastforward(m))
410 if (ifp->if_flags & IFF_NOARP) {
422 netisr_dispatch(isr, m);