Commit | Line | Data |
---|---|---|
984263bc MD |
1 | /* |
2 | * Copyright (c) 1998, Larry Lile | |
3 | * All rights reserved. | |
4 | * | |
5 | * For latest sources and information on this driver, please | |
6 | * go to http://anarchy.stdio.com. | |
7 | * | |
8 | * Questions, comments or suggestions should be directed to | |
9 | * Larry Lile <lile@stdio.com>. | |
f23061d4 | 10 | * |
984263bc MD |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions | |
13 | * are met: | |
14 | * 1. Redistributions of source code must retain the above copyright | |
15 | * notice unmodified, this list of conditions, and the following | |
16 | * disclaimer. | |
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. | |
20 | * | |
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 | |
31 | * SUCH DAMAGE. | |
32 | * | |
33 | * $FreeBSD: src/sys/net/if_iso88025subr.c,v 1.7.2.7 2002/06/18 00:15:31 kbyanc Exp $ | |
f2682cb9 | 34 | * $DragonFly: src/sys/net/Attic/if_iso88025subr.c,v 1.12 2005/06/03 23:23:03 joerg Exp $ |
984263bc MD |
35 | * |
36 | */ | |
37 | ||
38 | /* | |
39 | * | |
40 | * General ISO 802.5 (Token Ring) support routines | |
f23061d4 | 41 | * |
984263bc MD |
42 | */ |
43 | ||
44 | #include "opt_inet.h" | |
45 | ||
46 | #include <sys/param.h> | |
47 | #include <sys/systm.h> | |
48 | #include <sys/kernel.h> | |
49 | #include <sys/malloc.h> | |
50 | #include <sys/mbuf.h> | |
51 | #include <sys/socket.h> | |
f23061d4 | 52 | #include <sys/sockio.h> |
984263bc MD |
53 | #include <sys/sysctl.h> |
54 | ||
55 | #include <net/if.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> | |
61 | ||
62 | #include <net/if_arp.h> | |
63 | ||
64 | #include <net/iso88025.h> | |
65 | ||
66 | #ifdef INET | |
67 | #include <netinet/in.h> | |
68 | #include <netinet/in_var.h> | |
69 | #include <netinet/if_ether.h> | |
70 | #endif | |
71 | ||
72 | #include <net/bpf.h> | |
73 | ||
74 | #include <machine/clock.h> | |
75 | #include <machine/md_var.h> | |
76 | ||
1f2de5d4 | 77 | #include <bus/isa/i386/isa_device.h> |
984263bc MD |
78 | |
79 | #include <vm/vm.h> | |
80 | #include <vm/vm_param.h> | |
81 | #include <vm/pmap.h> | |
82 | ||
83 | #include <sys/kernel.h> | |
84 | #include <net/iso88025.h> | |
85 | ||
3013ac0e JS |
86 | static int iso88025_output(struct ifnet *, struct mbuf *, |
87 | struct sockaddr *, struct rtentry *); | |
88 | static void iso88025_input(struct ifnet *, struct mbuf *); | |
89 | ||
984263bc MD |
90 | void |
91 | iso88025_ifattach(struct ifnet *ifp) | |
92 | { | |
3013ac0e JS |
93 | struct sockaddr_dl *sdl; |
94 | ||
95 | ifp->if_input = iso88025_input; | |
96 | ifp->if_output = iso88025_output; | |
97 | if_attach(ifp); | |
98 | ifp->if_type = IFT_ISO88025; | |
99 | ifp->if_addrlen = ISO88025_ADDR_LEN; | |
100 | ifp->if_hdrlen = ISO88025_HDR_LEN; | |
101 | if (ifp->if_baudrate == 0) | |
102 | ifp->if_baudrate = TR_16MBPS; /* 16Mbit should be a safe default */ | |
103 | if (ifp->if_mtu == 0) | |
104 | ifp->if_mtu = ISO88025_DEFAULT_MTU; | |
984263bc | 105 | |
f2682cb9 | 106 | sdl = IF_LLSOCKADDR(ifp); |
f23061d4 JH |
107 | sdl->sdl_type = IFT_ISO88025; |
108 | sdl->sdl_alen = ifp->if_addrlen; | |
109 | bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen); | |
3013ac0e | 110 | bpfattach(ifp, DLT_IEEE802, sizeof(struct iso88025_header)); |
984263bc MD |
111 | } |
112 | ||
113 | int | |
114 | iso88025_ioctl(struct ifnet *ifp, int command, caddr_t data) | |
115 | { | |
f23061d4 JH |
116 | struct ifaddr *ifa = (struct ifaddr *) data; |
117 | struct ifreq *ifr = (struct ifreq *) data; | |
118 | int error = 0; | |
984263bc | 119 | |
f23061d4 JH |
120 | switch (command) { |
121 | case SIOCSIFADDR: | |
122 | ifp->if_flags |= IFF_UP; | |
984263bc | 123 | |
f23061d4 | 124 | switch (ifa->ifa_addr->sa_family) { |
984263bc | 125 | #ifdef INET |
f23061d4 JH |
126 | case AF_INET: |
127 | ifp->if_init(ifp->if_softc); /* before arpwhohas */ | |
128 | arp_ifinit(ifp, ifa); | |
129 | break; | |
984263bc | 130 | #endif |
f23061d4 JH |
131 | default: |
132 | ifp->if_init(ifp->if_softc); | |
133 | break; | |
134 | } | |
135 | break; | |
136 | ||
137 | case SIOCGIFADDR: | |
138 | bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, | |
139 | ((struct sockaddr *)ifr->ifr_data)->sa_data, | |
140 | ISO88025_ADDR_LEN); | |
141 | break; | |
142 | ||
143 | case SIOCSIFMTU: | |
144 | /* | |
145 | * Set the interface MTU. | |
146 | */ | |
147 | if (ifr->ifr_mtu > ISO88025_MAX_MTU) { | |
148 | error = EINVAL; | |
149 | } else { | |
150 | ifp->if_mtu = ifr->ifr_mtu; | |
151 | } | |
152 | break; | |
153 | } | |
154 | return (error); | |
984263bc MD |
155 | } |
156 | ||
157 | /* | |
158 | * ISO88025 encapsulation | |
159 | */ | |
3013ac0e | 160 | static int |
f23061d4 JH |
161 | iso88025_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, |
162 | struct rtentry *rt0) | |
984263bc | 163 | { |
82ed7fc2 | 164 | struct iso88025_header *th; |
984263bc | 165 | struct iso88025_header gen_th; |
82ed7fc2 | 166 | struct iso88025_sockaddr_data *sd = (struct iso88025_sockaddr_data *)dst->sa_data; |
f23061d4 | 167 | struct llc *l; |
82ed7fc2 | 168 | struct sockaddr_dl *sdl = NULL; |
f23061d4 JH |
169 | int s, error = 0, rif_len = 0; |
170 | u_char edst[6]; | |
82ed7fc2 | 171 | struct rtentry *rt; |
984263bc MD |
172 | int len = m->m_pkthdr.len, loop_copy = 0; |
173 | struct arpcom *ac = (struct arpcom *)ifp; | |
174 | ||
175 | if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) | |
176 | senderr(ENETDOWN); | |
984263bc | 177 | |
f23061d4 JH |
178 | if (rt0 == NULL) |
179 | senderr(EHOSTUNREACH); | |
180 | error = rt_llroute(dst, rt0, &rt); | |
181 | if (error != 0) | |
182 | senderr(error); | |
183 | ||
184 | /* Calculate routing info length based on arp table entry. */ | |
984263bc MD |
185 | if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway)) |
186 | if (SDL_ISO88025(sdl)->trld_rcf != NULL) | |
187 | rif_len = TR_RCF_RIFLEN(SDL_ISO88025(sdl)->trld_rcf); | |
188 | ||
f23061d4 | 189 | /* Generate a generic 802.5 header for the packet. */ |
984263bc MD |
190 | gen_th.ac = TR_AC; |
191 | gen_th.fc = TR_LLC_FRAME; | |
f23061d4 | 192 | memcpy(gen_th.iso88025_shost, ac->ac_enaddr, sizeof ac->ac_enaddr); |
984263bc MD |
193 | if (rif_len) { |
194 | gen_th.iso88025_shost[0] |= TR_RII; | |
195 | if (rif_len > 2) { | |
196 | gen_th.rcf = sdl->sdl_rcf; | |
197 | memcpy(gen_th.rd, sdl->sdl_route, rif_len - 2); | |
198 | } | |
199 | } | |
f23061d4 | 200 | |
984263bc MD |
201 | |
202 | switch (dst->sa_family) { | |
203 | #ifdef INET | |
204 | case AF_INET: | |
f23061d4 | 205 | if (!arpresolve(ifp, rt0, m, dst, edst)) |
984263bc MD |
206 | return (0); /* if not yet resolved */ |
207 | /* Add LLC and SNAP headers */ | |
74f1caca | 208 | M_PREPEND(m, 8, MB_DONTWAIT); |
f23061d4 JH |
209 | if (m == NULL) |
210 | return (ENOBUFS); | |
984263bc | 211 | l = mtod(m, struct llc *); |
f23061d4 JH |
212 | l->llc_un.type_snap.ether_type = htons(ETHERTYPE_IP); |
213 | l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP; | |
984263bc MD |
214 | l->llc_un.type_snap.control = LLC_UI; |
215 | l->llc_un.type_snap.org_code[0] = 0x0; | |
216 | l->llc_un.type_snap.org_code[1] = 0x0; | |
217 | l->llc_un.type_snap.org_code[2] = 0x0; | |
218 | memcpy(gen_th.iso88025_dhost, edst, sizeof(edst)); | |
219 | break; | |
220 | #endif | |
221 | ||
222 | case AF_UNSPEC: | |
223 | /* | |
224 | * For AF_UNSPEC sockaddr.sa_data must contain all of the | |
225 | * mac information needed to send the packet. This allows | |
226 | * full mac, llc, and source routing function to be controlled. | |
227 | * llc and source routing information must already be in the | |
228 | * mbuf provided, ac/fc are set in sa_data. sockaddr.sa_data | |
229 | * should be a iso88025_sockaddr_data structure see iso88025.h | |
230 | */ | |
f23061d4 | 231 | loop_copy = -1; |
984263bc MD |
232 | sd = (struct iso88025_sockaddr_data *)dst->sa_data; |
233 | gen_th.ac = sd->ac; | |
234 | gen_th.fc = sd->fc; | |
235 | memcpy(gen_th.iso88025_dhost, sd->ether_dhost, sizeof(sd->ether_dhost)); | |
236 | memcpy(gen_th.iso88025_shost, sd->ether_shost, sizeof(sd->ether_shost)); | |
237 | rif_len = 0; | |
238 | break; | |
239 | ||
240 | default: | |
3e4a09e7 | 241 | printf("%s: can't handle af%d\n", ifp->if_xname, |
984263bc MD |
242 | dst->sa_family); |
243 | senderr(EAFNOSUPPORT); | |
244 | } | |
245 | ||
246 | /* | |
247 | * Add local net header. If no space in first mbuf, | |
248 | * allocate another. | |
249 | */ | |
f23061d4 | 250 | |
74f1caca | 251 | M_PREPEND(m, ISO88025_HDR_LEN + rif_len, MB_DONTWAIT); |
f23061d4 JH |
252 | if (m == NULL) |
253 | return (ENOBUFS); | |
984263bc MD |
254 | |
255 | /* Copy as much of the generic header as is needed into the mbuf */ | |
256 | th = mtod(m, struct iso88025_header *); | |
257 | memcpy(th, &gen_th, ISO88025_HDR_LEN + rif_len); | |
258 | ||
f23061d4 JH |
259 | /* |
260 | * If a simplex interface, and the packet is being sent to our | |
261 | * Ethernet address or a broadcast address, loopback a copy. | |
262 | * XXX To make a simplex device behave exactly like a duplex | |
263 | * device, we should copy in the case of sending to our own | |
264 | * ethernet address (thus letting the original actually appear | |
265 | * on the wire). However, we don't do that here for security | |
266 | * reasons and compatibility with the original behavior. | |
267 | */ | |
268 | if ((ifp->if_flags & IFF_SIMPLEX) && | |
269 | (loop_copy != -1)) { | |
270 | if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { | |
271 | struct mbuf *n = m_copypacket(m, MB_DONTWAIT); | |
272 | ||
273 | if_simloop(ifp, n, dst->sa_family, ISO88025_HDR_LEN); | |
274 | } else if (bcmp(th->iso88025_dhost, | |
275 | th->iso88025_shost, ETHER_ADDR_LEN) == 0) { | |
276 | if_simloop(ifp, m, dst->sa_family, ISO88025_HDR_LEN); | |
277 | return (0); /* XXX */ | |
278 | } | |
279 | } | |
280 | ||
281 | s = splimp(); | |
984263bc MD |
282 | /* |
283 | * Queue message on interface, and start output if interface | |
284 | * not yet active. | |
285 | */ | |
286 | if (IF_QFULL(&ifp->if_snd)) { | |
f23061d4 | 287 | printf("iso88025_output: packet dropped QFULL.\n"); |
984263bc MD |
288 | IF_DROP(&ifp->if_snd); |
289 | splx(s); | |
290 | senderr(ENOBUFS); | |
291 | } | |
292 | if (m->m_flags & M_MCAST) | |
293 | ifp->if_omcasts++; | |
294 | IF_ENQUEUE(&ifp->if_snd, m); | |
f23061d4 | 295 | if (!(ifp->if_flags & IFF_OACTIVE)) |
984263bc MD |
296 | (*ifp->if_start)(ifp); |
297 | splx(s); | |
298 | ifp->if_obytes += len + ISO88025_HDR_LEN + 8; | |
299 | return (error); | |
300 | ||
301 | bad: | |
0c3c561c | 302 | m_freem(m); |
984263bc MD |
303 | return (error); |
304 | } | |
305 | ||
306 | /* | |
307 | * ISO 88025 de-encapsulation | |
308 | */ | |
3013ac0e JS |
309 | static void |
310 | iso88025_input(struct ifnet *ifp, struct mbuf *m) | |
984263bc | 311 | { |
3013ac0e JS |
312 | struct llc *l; |
313 | struct iso88025_header *th = mtod(m, struct iso88025_header *); | |
314 | int isr, hdr_len; | |
984263bc | 315 | u_short ether_type; |
984263bc | 316 | |
f23061d4 | 317 | if (!(ifp->if_flags & IFF_UP)) { |
984263bc MD |
318 | m_freem(m); |
319 | return; | |
320 | } | |
321 | ||
3013ac0e JS |
322 | hdr_len = ISO88025_HDR_LEN; |
323 | if (th->iso88025_shost[0] & 0x80) | |
324 | hdr_len += (ntohs(th->rcf) & 0x1f00) >> 8; | |
325 | ||
326 | if (m->m_len < hdr_len) { | |
327 | m_freem(m); | |
328 | return; | |
329 | } | |
330 | m->m_pkthdr.len -= hdr_len; | |
331 | m->m_len -= hdr_len; | |
332 | m->m_data += hdr_len; | |
333 | ||
334 | l = mtod(m, struct llc *); | |
335 | ||
984263bc MD |
336 | switch (l->llc_control) { |
337 | case LLC_UI: | |
338 | break; | |
339 | case LLC_TEST: | |
340 | case LLC_TEST_P: | |
341 | { | |
342 | struct sockaddr sa; | |
343 | struct arpcom *ac = (struct arpcom *)ifp; | |
344 | struct iso88025_sockaddr_data *th2; | |
345 | int i; | |
346 | u_char c = l->llc_dsap; | |
347 | ||
348 | if (th->iso88025_shost[0] & TR_RII) { /* XXX */ | |
349 | printf("iso88025_input: dropping source routed LLC_TEST\n"); | |
350 | m_free(m); | |
351 | return; | |
352 | } | |
353 | l->llc_dsap = l->llc_ssap; | |
354 | l->llc_ssap = c; | |
355 | if (m->m_flags & (M_BCAST | M_MCAST)) | |
f23061d4 JH |
356 | bcopy(ac->ac_enaddr, th->iso88025_dhost, |
357 | ISO88025_ADDR_LEN); | |
984263bc MD |
358 | sa.sa_family = AF_UNSPEC; |
359 | sa.sa_len = sizeof(sa); | |
360 | th2 = (struct iso88025_sockaddr_data *)sa.sa_data; | |
361 | for (i = 0; i < ISO88025_ADDR_LEN; i++) { | |
362 | th2->ether_shost[i] = c = th->iso88025_dhost[i]; | |
363 | th2->ether_dhost[i] = th->iso88025_dhost[i] = th->iso88025_shost[i]; | |
364 | th->iso88025_shost[i] = c; | |
365 | } | |
366 | th2->ac = TR_AC; | |
367 | th2->fc = TR_LLC_FRAME; | |
368 | ifp->if_output(ifp, m, &sa, NULL); | |
369 | return; | |
370 | } | |
371 | default: | |
372 | printf("iso88025_input: unexpected llc control 0x%02x\n", l->llc_control); | |
373 | m_freem(m); | |
374 | return; | |
375 | } | |
376 | ||
f23061d4 JH |
377 | m->m_pkthdr.len -= 8; |
378 | m->m_len -= 8; | |
379 | m->m_data += 8; /* Length of LLC header in our case */ | |
984263bc MD |
380 | |
381 | ifp->if_ibytes += m->m_pkthdr.len + sizeof(*th); | |
382 | if (th->iso88025_dhost[0] & 1) { | |
c401f0fd JS |
383 | if (bcmp(ifp->if_broadcastaddr, th->iso88025_dhost, |
384 | ifp->if_addrlen) == 0) | |
984263bc MD |
385 | m->m_flags |= M_BCAST; |
386 | else | |
387 | m->m_flags |= M_MCAST; | |
f23061d4 | 388 | } |
984263bc MD |
389 | if (m->m_flags & (M_BCAST|M_MCAST)) |
390 | ifp->if_imcasts++; | |
391 | ||
392 | ether_type = ntohs(l->llc_un.type_snap.ether_type); | |
393 | ||
394 | switch (ether_type) { | |
395 | #ifdef INET | |
396 | case ETHERTYPE_IP: | |
f23061d4 | 397 | th->iso88025_shost[0] &= ~(TR_RII); |
984263bc MD |
398 | if (ipflow_fastforward(m)) |
399 | return; | |
8bde602d | 400 | isr = NETISR_IP; |
984263bc MD |
401 | break; |
402 | ||
403 | case ETHERTYPE_ARP: | |
404 | if (ifp->if_flags & IFF_NOARP) { | |
405 | m_freem(m); | |
406 | return; | |
407 | } | |
8bde602d | 408 | isr = NETISR_ARP; |
f23061d4 | 409 | break; |
984263bc MD |
410 | #endif |
411 | default: | |
412 | m_freem(m); | |
413 | return; | |
414 | } | |
415 | ||
8bde602d | 416 | netisr_dispatch(isr, m); |
984263bc | 417 | } |