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