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