Make all network interrupt service routines MPSAFE part 1/3.
[dragonfly.git] / sys / net / if_arcsubr.c
CommitLineData
984263bc
MD
1/* $NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $ */
2/* $FreeBSD: src/sys/net/if_arcsubr.c,v 1.1.2.5 2003/02/05 18:42:15 fjoe Exp $ */
78195a76 3/* $DragonFly: src/sys/net/Attic/if_arcsubr.c,v 1.19 2005/11/28 17:13:45 dillon Exp $ */
984263bc
MD
4
5/*
6 * Copyright (c) 1994, 1995 Ignatios Souvatzis
7 * Copyright (c) 1982, 1989, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
39 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
40 *
41 */
42#include "opt_inet.h"
43#include "opt_inet6.h"
44#include "opt_ipx.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/protosw.h>
52#include <sys/socket.h>
53#include <sys/sockio.h>
54#include <sys/errno.h>
55#include <sys/syslog.h>
78195a76 56#include <sys/serialize.h>
984263bc
MD
57
58#include <machine/cpu.h>
59
60#include <net/if.h>
61#include <net/netisr.h>
62#include <net/route.h>
63#include <net/if_dl.h>
64#include <net/if_types.h>
65#include <net/if_arc.h>
66#include <net/if_arp.h>
4d723e5a 67#include <net/ifq_var.h>
984263bc
MD
68#include <net/bpf.h>
69
70#if defined(INET) || defined(INET6)
71#include <netinet/in.h>
72#include <netinet/in_var.h>
73#include <netinet/if_ether.h>
74#endif
75
76#ifdef INET6
77#include <netinet6/nd6.h>
78#endif
79
80#ifdef IPX
d2438d69
MD
81#include <netproto/ipx/ipx.h>
82#include <netproto/ipx/ipx_if.h>
984263bc
MD
83#endif
84
85MODULE_VERSION(arcnet, 1);
86
87#define ARCNET_ALLOW_BROKEN_ARP
88
158abb01
RG
89static struct mbuf *arc_defrag (struct ifnet *, struct mbuf *);
90static int arc_resolvemulti (struct ifnet *, struct sockaddr **,
91 struct sockaddr *);
3013ac0e
JS
92static void arc_input(struct ifnet *, struct mbuf *);
93static int arc_output(struct ifnet *, struct mbuf *, struct sockaddr *,
94 struct rtentry *);
984263bc
MD
95
96#define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
97
0c3c561c 98#define gotoerr(e) { error = (e); goto bad;}
984263bc
MD
99#define SIN(s) ((struct sockaddr_in *)s)
100#define SIPX(s) ((struct sockaddr_ipx *)s)
101
c401f0fd
JS
102const uint8_t arcbroadcastaddr[1] = {0};
103
984263bc
MD
104/*
105 * ARCnet output routine.
106 * Encapsulate a packet of type family for the local net.
107 * Assumes that ifp is actually pointer to arccom structure.
108 */
3013ac0e
JS
109static int
110arc_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
f23061d4 111 struct rtentry *rt)
984263bc 112{
984263bc
MD
113 struct arc_header *ah;
114 int error;
115 u_int8_t atype, adst;
116 int loop_copy = 0;
117 int isphds;
4d723e5a 118 struct altq_pktattr pktattr;
984263bc 119
f23061d4
JH
120 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
121 return (ENETDOWN); /* m, m1 aren't initialized yet */
984263bc 122
4d723e5a
JS
123 /*
124 * If the queueing discipline needs packet classification,
125 * do it before prepending link headers.
126 */
127 ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
128
984263bc
MD
129 switch (dst->sa_family) {
130#ifdef INET
131 case AF_INET:
132
133 /*
134 * For now, use the simple IP addr -> ARCnet addr mapping
135 */
136 if (m->m_flags & (M_BCAST|M_MCAST))
c401f0fd 137 adst = ifp->if_broadcastaddr[0];
984263bc
MD
138 else if (ifp->if_flags & IFF_NOARP)
139 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
f23061d4 140 else if (!arpresolve(ifp, rt, m, dst, &adst))
984263bc
MD
141 return 0; /* not resolved yet */
142
143 atype = (ifp->if_flags & IFF_LINK0) ?
144 ARCTYPE_IP_OLD : ARCTYPE_IP;
145 break;
146#endif
147#ifdef INET6
148 case AF_INET6:
984263bc 149 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)&adst))
f23061d4 150 return (0); /* it must be impossible, but... */
984263bc
MD
151 atype = ARCTYPE_INET6;
152 break;
153#endif
154#ifdef IPX
155 case AF_IPX:
156 adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
157 atype = ARCTYPE_IPX;
158 if (adst == 0xff)
c401f0fd 159 adst = ifp->if_broadcastaddr[0];
984263bc
MD
160 break;
161#endif
162
163 case AF_UNSPEC:
164 loop_copy = -1;
165 ah = (struct arc_header *)dst->sa_data;
166 adst = ah->arc_dhost;
167 atype = ah->arc_type;
168
169 if (atype == ARCTYPE_ARP) {
170 atype = (ifp->if_flags & IFF_LINK0) ?
171 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
172
173#ifdef ARCNET_ALLOW_BROKEN_ARP
174 /*
175 * XXX It's not clear per RFC826 if this is needed, but
176 * "assigned numbers" say this is wrong.
177 * However, e.g., AmiTCP 3.0Beta used it... we make this
178 * switchable for emergency cases. Not perfect, but...
179 */
180 if (ifp->if_flags & IFF_LINK2)
181 mtod(m, struct arphdr *)->ar_pro = atype - 1;
182#endif
183 }
184 break;
185
186 default:
3e4a09e7 187 printf("%s: can't handle af%d\n", ifp->if_xname,
984263bc 188 dst->sa_family);
0c3c561c 189 gotoerr(EAFNOSUPPORT);
984263bc
MD
190 }
191
192 isphds = arc_isphds(atype);
74f1caca 193 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, MB_DONTWAIT);
f23061d4 194 if (m == NULL)
0c3c561c 195 gotoerr(ENOBUFS);
984263bc
MD
196 ah = mtod(m, struct arc_header *);
197 ah->arc_type = atype;
198 ah->arc_dhost = adst;
199 ah->arc_shost = *IF_LLADDR(ifp);
200 ah->arc_shost = ARC_LLADDR(ifp);
201 if (isphds) {
202 ah->arc_flag = 0;
203 ah->arc_seqid = 0;
204 }
205
206 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
207 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
f23061d4 208 struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
984263bc 209
f23061d4 210 if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
984263bc 211 } else if (ah->arc_dhost == ah->arc_shost) {
f23061d4 212 if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
984263bc
MD
213 return (0); /* XXX */
214 }
215 }
216
7600679e 217 BPF_MTAP(ifp, m);
984263bc 218
4d723e5a
JS
219 error = ifq_handoff(ifp, m, &pktattr);
220 return (error);
984263bc
MD
221
222bad:
0c3c561c 223 if (m != NULL)
984263bc
MD
224 m_freem(m);
225 return (error);
226}
227
228void
229arc_frag_init(ifp)
230 struct ifnet *ifp;
231{
232 struct arccom *ac;
233
234 ac = (struct arccom *)ifp;
235 ac->curr_frag = 0;
236}
237
238struct mbuf *
239arc_frag_next(ifp)
240 struct ifnet *ifp;
241{
242 struct arccom *ac;
243 struct mbuf *m;
244 struct arc_header *ah;
245
246 ac = (struct arccom *)ifp;
247 if ((m = ac->curr_frag) == 0) {
248 int tfrags;
249
250 /* dequeue new packet */
251 IF_DEQUEUE(&ifp->if_snd, m);
252 if (m == 0)
253 return 0;
254
255 ah = mtod(m, struct arc_header *);
256 if (!arc_isphds(ah->arc_type))
257 return m;
258
259 ++ac->ac_seqid; /* make the seqid unique */
260 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
261 ac->fsflag = 2 * tfrags - 3;
262 ac->sflag = 0;
263 ac->rsflag = ac->fsflag;
264 ac->arc_dhost = ah->arc_dhost;
265 ac->arc_shost = ah->arc_shost;
266 ac->arc_type = ah->arc_type;
267
268 m_adj(m, ARC_HDRNEWLEN);
269 ac->curr_frag = m;
270 }
271
272 /* split out next fragment and return it */
273 if (ac->sflag < ac->fsflag) {
274 /* we CAN'T have short packets here */
74f1caca 275 ac->curr_frag = m_split(m, ARC_MAX_DATA, MB_DONTWAIT);
984263bc
MD
276 if (ac->curr_frag == 0) {
277 m_freem(m);
278 return 0;
279 }
280
74f1caca 281 M_PREPEND(m, ARC_HDRNEWLEN, MB_DONTWAIT);
984263bc
MD
282 if (m == 0) {
283 m_freem(ac->curr_frag);
284 ac->curr_frag = 0;
285 return 0;
286 }
287
288 ah = mtod(m, struct arc_header *);
289 ah->arc_flag = ac->rsflag;
290 ah->arc_seqid = ac->ac_seqid;
291
292 ac->sflag += 2;
293 ac->rsflag = ac->sflag;
294 } else if ((m->m_pkthdr.len >=
295 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
296 (m->m_pkthdr.len <=
297 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
298 ac->curr_frag = 0;
299
74f1caca 300 M_PREPEND(m, ARC_HDRNEWLEN_EXC, MB_DONTWAIT);
984263bc
MD
301 if (m == 0)
302 return 0;
303
304 ah = mtod(m, struct arc_header *);
305 ah->arc_flag = 0xFF;
306 ah->arc_seqid = 0xFFFF;
307 ah->arc_type2 = ac->arc_type;
308 ah->arc_flag2 = ac->sflag;
309 ah->arc_seqid2 = ac->ac_seqid;
310 } else {
311 ac->curr_frag = 0;
312
74f1caca 313 M_PREPEND(m, ARC_HDRNEWLEN, MB_DONTWAIT);
984263bc
MD
314 if (m == 0)
315 return 0;
316
317 ah = mtod(m, struct arc_header *);
318 ah->arc_flag = ac->sflag;
319 ah->arc_seqid = ac->ac_seqid;
320 }
321
322 ah->arc_dhost = ac->arc_dhost;
323 ah->arc_shost = ac->arc_shost;
324 ah->arc_type = ac->arc_type;
325
326 return m;
327}
328
329/*
330 * Defragmenter. Returns mbuf if last packet found, else
331 * NULL. frees imcoming mbuf as necessary.
332 */
333
334__inline struct mbuf *
335arc_defrag(ifp, m)
336 struct ifnet *ifp;
337 struct mbuf *m;
338{
339 struct arc_header *ah, *ah1;
340 struct arccom *ac;
341 struct ac_frag *af;
342 struct mbuf *m1;
343 char *s;
344 int newflen;
345 u_char src,dst,typ;
346
347 ac = (struct arccom *)ifp;
348
349 if (m->m_len < ARC_HDRNEWLEN) {
350 m = m_pullup(m, ARC_HDRNEWLEN);
351 if (m == NULL) {
352 ++ifp->if_ierrors;
353 return NULL;
354 }
355 }
356
357 ah = mtod(m, struct arc_header *);
358 typ = ah->arc_type;
359
360 if (!arc_isphds(typ))
361 return m;
362
363 src = ah->arc_shost;
364 dst = ah->arc_dhost;
365
366 if (ah->arc_flag == 0xff) {
367 m_adj(m, 4);
368
369 if (m->m_len < ARC_HDRNEWLEN) {
370 m = m_pullup(m, ARC_HDRNEWLEN);
371 if (m == NULL) {
372 ++ifp->if_ierrors;
373 return NULL;
374 }
375 }
376
377 ah = mtod(m, struct arc_header *);
378 }
379
380 af = &ac->ac_fragtab[src];
381 m1 = af->af_packet;
382 s = "debug code error";
383
384 if (ah->arc_flag & 1) {
385 /*
386 * first fragment. We always initialize, which is
387 * about the right thing to do, as we only want to
388 * accept one fragmented packet per src at a time.
389 */
390 if (m1 != NULL)
391 m_freem(m1);
392
393 af->af_packet = m;
394 m1 = m;
395 af->af_maxflag = ah->arc_flag;
396 af->af_lastseen = 0;
397 af->af_seqid = ah->arc_seqid;
398
399 return NULL;
400 /* notreached */
401 } else {
402 /* check for unfragmented packet */
403 if (ah->arc_flag == 0)
404 return m;
405
406 /* do we have a first packet from that src? */
407 if (m1 == NULL) {
408 s = "no first frag";
409 goto outofseq;
410 }
411
412 ah1 = mtod(m1, struct arc_header *);
413
414 if (ah->arc_seqid != ah1->arc_seqid) {
415 s = "seqid differs";
416 goto outofseq;
417 }
418
419 if (typ != ah1->arc_type) {
420 s = "type differs";
421 goto outofseq;
422 }
423
424 if (dst != ah1->arc_dhost) {
425 s = "dest host differs";
426 goto outofseq;
427 }
428
429 /* typ, seqid and dst are ok here. */
430
431 if (ah->arc_flag == af->af_lastseen) {
432 m_freem(m);
433 return NULL;
434 }
435
436 if (ah->arc_flag == af->af_lastseen + 2) {
437 /* ok, this is next fragment */
438 af->af_lastseen = ah->arc_flag;
439 m_adj(m,ARC_HDRNEWLEN);
440
441 /*
442 * m_cat might free the first mbuf (with pkthdr)
443 * in 2nd chain; therefore:
444 */
445
446 newflen = m->m_pkthdr.len;
447
448 m_cat(m1,m);
449
450 m1->m_pkthdr.len += newflen;
451
452 /* is it the last one? */
453 if (af->af_lastseen > af->af_maxflag) {
454 af->af_packet = NULL;
f23061d4 455 return (m1);
984263bc
MD
456 } else
457 return NULL;
458 }
459 s = "other reason";
460 /* if all else fails, it is out of sequence, too */
461 }
462outofseq:
0c3c561c 463 if (m1 != NULL) {
984263bc
MD
464 m_freem(m1);
465 af->af_packet = NULL;
466 }
467
0c3c561c 468 if (m != NULL)
984263bc
MD
469 m_freem(m);
470
3e4a09e7
MD
471 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
472 ifp->if_xname, s);
984263bc
MD
473
474 return NULL;
475}
476
477/*
478 * return 1 if Packet Header Definition Standard, else 0.
479 * For now: old IP, old ARP aren't obviously. Lacking correct information,
480 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
481 * (Apple and Novell corporations were involved, among others, in PHDS work).
482 * Easiest is to assume that everybody else uses that, too.
483 */
484int
485arc_isphds(type)
486 u_int8_t type;
487{
488 return (type != ARCTYPE_IP_OLD &&
489 type != ARCTYPE_ARP_OLD &&
490 type != ARCTYPE_DIAGNOSE);
491}
492
493/*
494 * Process a received Arcnet packet;
495 * the packet is in the mbuf chain m with
496 * the ARCnet header.
497 */
3013ac0e
JS
498static void
499arc_input(struct ifnet *ifp, struct mbuf *m)
984263bc
MD
500{
501 struct arc_header *ah;
8bde602d 502 int isr;
984263bc
MD
503 u_int8_t atype;
504
78195a76
MD
505 ASSERT_SERIALIZED(ifp->if_serializer);
506
0c3c561c 507 if (!(ifp->if_flags & IFF_UP)) {
984263bc
MD
508 m_freem(m);
509 return;
510 }
511
512 /* possibly defragment: */
513 m = arc_defrag(ifp, m);
514 if (m == NULL)
515 return;
516
7600679e 517 BPF_MTAP(ifp, m);
984263bc
MD
518
519 ah = mtod(m, struct arc_header *);
520 /* does this belong to us? */
0c3c561c
JH
521 if (!(ifp->if_flags & IFF_PROMISC) &&
522 ah->arc_dhost != ifp->if_broadcastaddr[0] &&
523 ah->arc_dhost != ARC_LLADDR(ifp)) {
984263bc
MD
524 m_freem(m);
525 return;
526 }
527
528 ifp->if_ibytes += m->m_pkthdr.len;
529
c401f0fd 530 if (ah->arc_dhost == ifp->if_broadcastaddr[0]) {
984263bc
MD
531 m->m_flags |= M_BCAST|M_MCAST;
532 ifp->if_imcasts++;
533 }
534
535 atype = ah->arc_type;
536 switch (atype) {
537#ifdef INET
538 case ARCTYPE_IP:
539 m_adj(m, ARC_HDRNEWLEN);
78195a76 540 if (ipflow_fastforward(m, ifp->if_serializer))
984263bc 541 return;
8bde602d 542 isr = NETISR_IP;
984263bc
MD
543 break;
544
545 case ARCTYPE_IP_OLD:
546 m_adj(m, ARC_HDRLEN);
78195a76 547 if (ipflow_fastforward(m, ifp->if_serializer))
984263bc 548 return;
8bde602d 549 isr = NETISR_IP;
984263bc
MD
550 break;
551
552 case ARCTYPE_ARP:
553 if (ifp->if_flags & IFF_NOARP) {
554 /* Discard packet if ARP is disabled on interface */
555 m_freem(m);
556 return;
557 }
558 m_adj(m, ARC_HDRNEWLEN);
8bde602d 559 isr = NETISR_ARP;
984263bc
MD
560#ifdef ARCNET_ALLOW_BROKEN_ARP
561 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
562#endif
563 break;
564
565 case ARCTYPE_ARP_OLD:
566 if (ifp->if_flags & IFF_NOARP) {
567 /* Discard packet if ARP is disabled on interface */
568 m_freem(m);
569 return;
570 }
571 m_adj(m, ARC_HDRLEN);
8bde602d 572 isr = NETISR_ARP;
984263bc
MD
573#ifdef ARCNET_ALLOW_BROKEN_ARP
574 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
575#endif
576 break;
577#endif
578#ifdef INET6
579 case ARCTYPE_INET6:
580 m_adj(m, ARC_HDRNEWLEN);
8bde602d 581 isr = NETISR_IPV6;
984263bc
MD
582 break;
583#endif
584#ifdef IPX
585 case ARCTYPE_IPX:
586 m_adj(m, ARC_HDRNEWLEN);
8bde602d 587 isr = NETISR_IPX;
984263bc
MD
588 break;
589#endif
590 default:
591 m_freem(m);
592 return;
593 }
594
8bde602d 595 netisr_dispatch(isr, m);
984263bc
MD
596}
597
598/*
599 * Register (new) link level address.
600 */
601void
602arc_storelladdr(ifp, lla)
603 struct ifnet *ifp;
604 u_int8_t lla;
605{
606 ARC_LLADDR(ifp) = lla;
607}
608
609/*
610 * Perform common duties while attaching to interface list
611 */
612void
78195a76 613arc_ifattach(struct ifnet *ifp, u_int8_t lla, lwkt_serialize_t serializer)
984263bc 614{
984263bc
MD
615 struct sockaddr_dl *sdl;
616 struct arccom *ac;
617
3013ac0e
JS
618 ifp->if_input = arc_input;
619 ifp->if_output = arc_output;
78195a76 620 if_attach(ifp, serializer);
984263bc
MD
621 ifp->if_type = IFT_ARCNET;
622 ifp->if_addrlen = 1;
c401f0fd 623 ifp->if_broadcastaddr = arcbroadcastaddr;
984263bc
MD
624 ifp->if_hdrlen = ARC_HDRLEN;
625 ifp->if_mtu = 1500;
626 ifp->if_resolvemulti = arc_resolvemulti;
627 if (ifp->if_baudrate == 0)
628 ifp->if_baudrate = 2500000;
f2682cb9 629 sdl = IF_LLSOCKADDR(ifp);
984263bc
MD
630 sdl->sdl_type = IFT_ARCNET;
631 sdl->sdl_alen = ifp->if_addrlen;
632
633 if (ifp->if_flags & IFF_BROADCAST)
634 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
635
636 ac = (struct arccom *)ifp;
637 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
638 if (lla == 0) {
639 /* XXX this message isn't entirely clear, to me -- cgd */
3e4a09e7
MD
640 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
641 ifp->if_xname, ifp->if_xname);
984263bc
MD
642 }
643 arc_storelladdr(ifp, lla);
644
645 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
9cd38cb4
SZ
646
647 if_printf(ifp, "Link Address 0x%02x (%u)\n", lla, lla);
984263bc
MD
648}
649
650void
651arc_ifdetach(ifp)
652 struct ifnet *ifp;
653{
654 bpfdetach(ifp);
655 if_detach(ifp);
656}
657
658int
659arc_ioctl(ifp, command, data)
660 struct ifnet *ifp;
661 int command;
662 caddr_t data;
663{
664 struct ifaddr *ifa = (struct ifaddr *) data;
665 struct ifreq *ifr = (struct ifreq *) data;
666 int error = 0;
667
668 switch (command) {
669 case SIOCSIFADDR:
670 ifp->if_flags |= IFF_UP;
671 switch (ifa->ifa_addr->sa_family) {
672#ifdef INET
673 case AF_INET:
78195a76 674 lwkt_serialize_enter(ifp->if_serializer);
984263bc 675 ifp->if_init(ifp->if_softc); /* before arpwhohas */
78195a76 676 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
677 arp_ifinit(ifp, ifa);
678 break;
679#endif
680#ifdef IPX
681 /*
682 * XXX This code is probably wrong
683 */
684 case AF_IPX:
685 {
686 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
687
688 if (ipx_nullhost(*ina))
689 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
690 else
691 arc_storelladdr(ifp, ina->x_host.c_host[5]);
692
693 /*
694 * Set new address
695 */
78195a76 696 lwkt_serialize_enter(ifp->if_serializer);
984263bc 697 ifp->if_init(ifp->if_softc);
78195a76 698 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
699 break;
700 }
701#endif
702 default:
78195a76 703 lwkt_serialize_enter(ifp->if_serializer);
984263bc 704 ifp->if_init(ifp->if_softc);
78195a76 705 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
706 break;
707 }
708 break;
709
710 case SIOCGIFADDR:
711 {
712 struct sockaddr *sa;
713
714 sa = (struct sockaddr *) &ifr->ifr_data;
715 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
716 }
717 break;
718
719 case SIOCADDMULTI:
720 case SIOCDELMULTI:
721 if (ifr == NULL)
722 error = EAFNOSUPPORT;
723 else {
724 switch (ifr->ifr_addr.sa_family) {
725 case AF_INET:
726 case AF_INET6:
727 error = 0;
728 break;
729 default:
730 error = EAFNOSUPPORT;
731 break;
732 }
733 }
734 break;
735
736 case SIOCSIFMTU:
737 /*
738 * Set the interface MTU.
739 * mtu can't be larger than ARCMTU for RFC1051
740 * and can't be larger than ARC_PHDS_MTU
741 */
742 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
743 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
744 error = EINVAL;
745 else
746 ifp->if_mtu = ifr->ifr_mtu;
747 break;
bf6831e8
JS
748 default:
749 error = EINVAL;
750 break;
984263bc
MD
751 }
752
753 return (error);
754}
755
756/* based on ether_resolvemulti() */
757int
758arc_resolvemulti(ifp, llsa, sa)
759 struct ifnet *ifp;
760 struct sockaddr **llsa;
761 struct sockaddr *sa;
762{
763 struct sockaddr_dl *sdl;
764 struct sockaddr_in *sin;
765#ifdef INET6
766 struct sockaddr_in6 *sin6;
767#endif
768
769 switch(sa->sa_family) {
770 case AF_LINK:
771 /*
772 * No mapping needed. Just check that it's a valid MC address.
773 */
774 sdl = (struct sockaddr_dl *)sa;
c401f0fd 775 if (*LLADDR(sdl) != ifp->if_broadcastaddr[0])
984263bc
MD
776 return EADDRNOTAVAIL;
777 *llsa = 0;
778 return 0;
779#ifdef INET
780 case AF_INET:
781 sin = (struct sockaddr_in *)sa;
782 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
783 return EADDRNOTAVAIL;
784 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
785 M_WAITOK|M_ZERO);
786 sdl->sdl_len = sizeof *sdl;
787 sdl->sdl_family = AF_LINK;
788 sdl->sdl_index = ifp->if_index;
789 sdl->sdl_type = IFT_ARCNET;
790 sdl->sdl_alen = ARC_ADDR_LEN;
791 *LLADDR(sdl) = 0;
792 *llsa = (struct sockaddr *)sdl;
793 return 0;
794#endif
795#ifdef INET6
796 case AF_INET6:
797 sin6 = (struct sockaddr_in6 *)sa;
798 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
799 /*
800 * An IP6 address of 0 means listen to all
801 * of the Ethernet multicast address used for IP6.
802 * (This is used for multicast routers.)
803 */
804 ifp->if_flags |= IFF_ALLMULTI;
805 *llsa = 0;
806 return 0;
807 }
808 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
809 return EADDRNOTAVAIL;
810 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
811 M_WAITOK|M_ZERO);
812 sdl->sdl_len = sizeof *sdl;
813 sdl->sdl_family = AF_LINK;
814 sdl->sdl_index = ifp->if_index;
815 sdl->sdl_type = IFT_ARCNET;
816 sdl->sdl_alen = ARC_ADDR_LEN;
817 *LLADDR(sdl) = 0;
818 *llsa = (struct sockaddr *)sdl;
819 return 0;
820#endif
821
822 default:
823 /*
824 * Well, the text isn't quite right, but it's the name
825 * that counts...
826 */
827 return EAFNOSUPPORT;
828 }
829}