]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_arcsubr.c
Merge r262907 from ^/projects/release-embedded:
[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 #if defined(INET) || defined(INET6)
107         struct llentry          *lle;
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                         error = arpresolve(ifp, ro ? ro->ro_rt : NULL,
129                                            m, dst, &adst, &lle);
130                         if (error)
131                                 return (error == EWOULDBLOCK ? 0 : error);
132                 }
133
134                 atype = (ifp->if_flags & IFF_LINK0) ?
135                         ARCTYPE_IP_OLD : ARCTYPE_IP;
136                 break;
137         case AF_ARP:
138         {
139                 struct arphdr *ah;
140                 ah = mtod(m, struct arphdr *);
141                 ah->ar_hrd = htons(ARPHRD_ARCNET);
142
143                 loop_copy = -1; /* if this is for us, don't do it */
144
145                 switch(ntohs(ah->ar_op)) {
146                 case ARPOP_REVREQUEST:
147                 case ARPOP_REVREPLY:
148                         atype = ARCTYPE_REVARP;
149                         break;
150                 case ARPOP_REQUEST:
151                 case ARPOP_REPLY:
152                 default:
153                         atype = ARCTYPE_ARP;
154                         break;
155                 }
156
157                 if (m->m_flags & M_BCAST)
158                         bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN);
159                 else
160                         bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN);
161         
162         }
163         break;
164 #endif
165 #ifdef INET6
166         case AF_INET6:
167                 error = nd6_storelladdr(ifp, m, dst, (u_char *)&adst, &lle);
168                 if (error)
169                         return (error);
170                 atype = ARCTYPE_INET6;
171                 break;
172 #endif
173         case AF_UNSPEC:
174             {
175                 const struct arc_header *ah;
176
177                 loop_copy = -1;
178                 ah = (const struct arc_header *)dst->sa_data;
179                 adst = ah->arc_dhost;
180                 atype = ah->arc_type;
181
182                 if (atype == ARCTYPE_ARP) {
183                         atype = (ifp->if_flags & IFF_LINK0) ?
184                             ARCTYPE_ARP_OLD: ARCTYPE_ARP;
185
186 #ifdef ARCNET_ALLOW_BROKEN_ARP
187                         /*
188                          * XXX It's not clear per RFC826 if this is needed, but
189                          * "assigned numbers" say this is wrong.
190                          * However, e.g., AmiTCP 3.0Beta used it... we make this
191                          * switchable for emergency cases. Not perfect, but...
192                          */
193                         if (ifp->if_flags & IFF_LINK2)
194                                 mtod(m, struct arphdr *)->ar_pro = atype - 1;
195 #endif
196                 }
197                 break;
198             }
199         default:
200                 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
201                 senderr(EAFNOSUPPORT);
202         }
203
204         isphds = arc_isphds(atype);
205         M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_NOWAIT);
206         if (m == 0)
207                 senderr(ENOBUFS);
208         ah = mtod(m, struct arc_header *);
209         ah->arc_type = atype;
210         ah->arc_dhost = adst;
211         ah->arc_shost = ARC_LLADDR(ifp);
212         if (isphds) {
213                 ah->arc_flag = 0;
214                 ah->arc_seqid = 0;
215         }
216
217         if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
218                 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
219                         struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
220
221                         (void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
222                 } else if (ah->arc_dhost == ah->arc_shost) {
223                         (void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
224                         return (0);     /* XXX */
225                 }
226         }
227
228         BPF_MTAP(ifp, m);
229
230         error = ifp->if_transmit(ifp, m);
231
232         return (error);
233
234 bad:
235         if (m)
236                 m_freem(m);
237         return (error);
238 }
239
240 void
241 arc_frag_init(struct ifnet *ifp)
242 {
243         struct arccom *ac;
244
245         ac = (struct arccom *)ifp->if_l2com;
246         ac->curr_frag = 0;
247 }
248
249 struct mbuf *
250 arc_frag_next(struct ifnet *ifp)
251 {
252         struct arccom *ac;
253         struct mbuf *m;
254         struct arc_header *ah;
255
256         ac = (struct arccom *)ifp->if_l2com;
257         if ((m = ac->curr_frag) == 0) {
258                 int tfrags;
259
260                 /* dequeue new packet */
261                 IF_DEQUEUE(&ifp->if_snd, m);
262                 if (m == 0)
263                         return 0;
264
265                 ah = mtod(m, struct arc_header *);
266                 if (!arc_isphds(ah->arc_type))
267                         return m;
268
269                 ++ac->ac_seqid;         /* make the seqid unique */
270                 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
271                 ac->fsflag = 2 * tfrags - 3;
272                 ac->sflag = 0;
273                 ac->rsflag = ac->fsflag;
274                 ac->arc_dhost = ah->arc_dhost;
275                 ac->arc_shost = ah->arc_shost;
276                 ac->arc_type = ah->arc_type;
277
278                 m_adj(m, ARC_HDRNEWLEN);
279                 ac->curr_frag = m;
280         }
281
282         /* split out next fragment and return it */
283         if (ac->sflag < ac->fsflag) {
284                 /* we CAN'T have short packets here */
285                 ac->curr_frag = m_split(m, ARC_MAX_DATA, M_NOWAIT);
286                 if (ac->curr_frag == 0) {
287                         m_freem(m);
288                         return 0;
289                 }
290
291                 M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
292                 if (m == 0) {
293                         m_freem(ac->curr_frag);
294                         ac->curr_frag = 0;
295                         return 0;
296                 }
297
298                 ah = mtod(m, struct arc_header *);
299                 ah->arc_flag = ac->rsflag;
300                 ah->arc_seqid = ac->ac_seqid;
301
302                 ac->sflag += 2;
303                 ac->rsflag = ac->sflag;
304         } else if ((m->m_pkthdr.len >=
305             ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
306             (m->m_pkthdr.len <=
307             ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
308                 ac->curr_frag = 0;
309
310                 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_NOWAIT);
311                 if (m == 0)
312                         return 0;
313
314                 ah = mtod(m, struct arc_header *);
315                 ah->arc_flag = 0xFF;
316                 ah->arc_seqid = 0xFFFF;
317                 ah->arc_type2 = ac->arc_type;
318                 ah->arc_flag2 = ac->sflag;
319                 ah->arc_seqid2 = ac->ac_seqid;
320         } else {
321                 ac->curr_frag = 0;
322
323                 M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
324                 if (m == 0)
325                         return 0;
326
327                 ah = mtod(m, struct arc_header *);
328                 ah->arc_flag = ac->sflag;
329                 ah->arc_seqid = ac->ac_seqid;
330         }
331
332         ah->arc_dhost = ac->arc_dhost;
333         ah->arc_shost = ac->arc_shost;
334         ah->arc_type = ac->arc_type;
335
336         return m;
337 }
338
339 /*
340  * Defragmenter. Returns mbuf if last packet found, else
341  * NULL. frees imcoming mbuf as necessary.
342  */
343
344 static __inline struct mbuf *
345 arc_defrag(struct ifnet *ifp, struct mbuf *m)
346 {
347         struct arc_header *ah, *ah1;
348         struct arccom *ac;
349         struct ac_frag *af;
350         struct mbuf *m1;
351         char *s;
352         int newflen;
353         u_char src,dst,typ;
354
355         ac = (struct arccom *)ifp->if_l2com;
356
357         if (m->m_len < ARC_HDRNEWLEN) {
358                 m = m_pullup(m, ARC_HDRNEWLEN);
359                 if (m == NULL) {
360                         ++ifp->if_ierrors;
361                         return NULL;
362                 }
363         }
364
365         ah = mtod(m, struct arc_header *);
366         typ = ah->arc_type;
367
368         if (!arc_isphds(typ))
369                 return m;
370
371         src = ah->arc_shost;
372         dst = ah->arc_dhost;
373
374         if (ah->arc_flag == 0xff) {
375                 m_adj(m, 4);
376
377                 if (m->m_len < ARC_HDRNEWLEN) {
378                         m = m_pullup(m, ARC_HDRNEWLEN);
379                         if (m == NULL) {
380                                 ++ifp->if_ierrors;
381                                 return NULL;
382                         }
383                 }
384
385                 ah = mtod(m, struct arc_header *);
386         }
387
388         af = &ac->ac_fragtab[src];
389         m1 = af->af_packet;
390         s = "debug code error";
391
392         if (ah->arc_flag & 1) {
393                 /*
394                  * first fragment. We always initialize, which is
395                  * about the right thing to do, as we only want to
396                  * accept one fragmented packet per src at a time.
397                  */
398                 if (m1 != NULL)
399                         m_freem(m1);
400
401                 af->af_packet = m;
402                 m1 = m;
403                 af->af_maxflag = ah->arc_flag;
404                 af->af_lastseen = 0;
405                 af->af_seqid = ah->arc_seqid;
406
407                 return NULL;
408                 /* notreached */
409         } else {
410                 /* check for unfragmented packet */
411                 if (ah->arc_flag == 0)
412                         return m;
413
414                 /* do we have a first packet from that src? */
415                 if (m1 == NULL) {
416                         s = "no first frag";
417                         goto outofseq;
418                 }
419
420                 ah1 = mtod(m1, struct arc_header *);
421
422                 if (ah->arc_seqid != ah1->arc_seqid) {
423                         s = "seqid differs";
424                         goto outofseq;
425                 }
426
427                 if (typ != ah1->arc_type) {
428                         s = "type differs";
429                         goto outofseq;
430                 }
431
432                 if (dst != ah1->arc_dhost) {
433                         s = "dest host differs";
434                         goto outofseq;
435                 }
436
437                 /* typ, seqid and dst are ok here. */
438
439                 if (ah->arc_flag == af->af_lastseen) {
440                         m_freem(m);
441                         return NULL;
442                 }
443
444                 if (ah->arc_flag == af->af_lastseen + 2) {
445                         /* ok, this is next fragment */
446                         af->af_lastseen = ah->arc_flag;
447                         m_adj(m,ARC_HDRNEWLEN);
448
449                         /*
450                          * m_cat might free the first mbuf (with pkthdr)
451                          * in 2nd chain; therefore:
452                          */
453
454                         newflen = m->m_pkthdr.len;
455
456                         m_cat(m1,m);
457
458                         m1->m_pkthdr.len += newflen;
459
460                         /* is it the last one? */
461                         if (af->af_lastseen > af->af_maxflag) {
462                                 af->af_packet = NULL;
463                                 return(m1);
464                         } else
465                                 return NULL;
466                 }
467                 s = "other reason";
468                 /* if all else fails, it is out of sequence, too */
469         }
470 outofseq:
471         if (m1) {
472                 m_freem(m1);
473                 af->af_packet = NULL;
474         }
475
476         if (m)
477                 m_freem(m);
478
479         log(LOG_INFO,"%s: got out of seq. packet: %s\n",
480             ifp->if_xname, s);
481
482         return NULL;
483 }
484
485 /*
486  * return 1 if Packet Header Definition Standard, else 0.
487  * For now: old IP, old ARP aren't obviously. Lacking correct information,
488  * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
489  * (Apple and Novell corporations were involved, among others, in PHDS work).
490  * Easiest is to assume that everybody else uses that, too.
491  */
492 int
493 arc_isphds(u_int8_t type)
494 {
495         return (type != ARCTYPE_IP_OLD &&
496                 type != ARCTYPE_ARP_OLD &&
497                 type != ARCTYPE_DIAGNOSE);
498 }
499
500 /*
501  * Process a received Arcnet packet;
502  * the packet is in the mbuf chain m with
503  * the ARCnet header.
504  */
505 void
506 arc_input(struct ifnet *ifp, struct mbuf *m)
507 {
508         struct arc_header *ah;
509         int isr;
510         u_int8_t atype;
511
512         if ((ifp->if_flags & IFF_UP) == 0) {
513                 m_freem(m);
514                 return;
515         }
516
517         /* possibly defragment: */
518         m = arc_defrag(ifp, m);
519         if (m == NULL)
520                 return;
521
522         BPF_MTAP(ifp, m);
523
524         ah = mtod(m, struct arc_header *);
525         /* does this belong to us? */
526         if ((ifp->if_flags & IFF_PROMISC) == 0
527             && ah->arc_dhost != arcbroadcastaddr
528             && ah->arc_dhost != ARC_LLADDR(ifp)) {
529                 m_freem(m);
530                 return;
531         }
532
533         ifp->if_ibytes += m->m_pkthdr.len;
534
535         if (ah->arc_dhost == arcbroadcastaddr) {
536                 m->m_flags |= M_BCAST|M_MCAST;
537                 ifp->if_imcasts++;
538         }
539
540         atype = ah->arc_type;
541         switch (atype) {
542 #ifdef INET
543         case ARCTYPE_IP:
544                 m_adj(m, ARC_HDRNEWLEN);
545                 if ((m = ip_fastforward(m)) == NULL)
546                         return;
547                 isr = NETISR_IP;
548                 break;
549
550         case ARCTYPE_IP_OLD:
551                 m_adj(m, ARC_HDRLEN);
552                 if ((m = ip_fastforward(m)) == NULL)
553                         return;
554                 isr = NETISR_IP;
555                 break;
556
557         case ARCTYPE_ARP:
558                 if (ifp->if_flags & IFF_NOARP) {
559                         /* Discard packet if ARP is disabled on interface */
560                         m_freem(m);
561                         return;
562                 }
563                 m_adj(m, ARC_HDRNEWLEN);
564                 isr = NETISR_ARP;
565 #ifdef ARCNET_ALLOW_BROKEN_ARP
566                 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
567 #endif
568                 break;
569
570         case ARCTYPE_ARP_OLD:
571                 if (ifp->if_flags & IFF_NOARP) {
572                         /* Discard packet if ARP is disabled on interface */
573                         m_freem(m);
574                         return;
575                 }
576                 m_adj(m, ARC_HDRLEN);
577                 isr = NETISR_ARP;
578 #ifdef ARCNET_ALLOW_BROKEN_ARP
579                 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
580 #endif
581                 break;
582 #endif
583 #ifdef INET6
584         case ARCTYPE_INET6:
585                 m_adj(m, ARC_HDRNEWLEN);
586                 isr = NETISR_IPV6;
587                 break;
588 #endif
589         default:
590                 m_freem(m);
591                 return;
592         }
593         M_SETFIB(m, ifp->if_fib);
594         netisr_dispatch(isr, m);
595 }
596
597 /*
598  * Register (new) link level address.
599  */
600 void
601 arc_storelladdr(struct ifnet *ifp, u_int8_t lla)
602 {
603         ARC_LLADDR(ifp) = lla;
604 }
605
606 /*
607  * Perform common duties while attaching to interface list
608  */
609 void
610 arc_ifattach(struct ifnet *ifp, u_int8_t lla)
611 {
612         struct ifaddr *ifa;
613         struct sockaddr_dl *sdl;
614         struct arccom *ac;
615
616         if_attach(ifp);
617         ifp->if_addrlen = 1;
618         ifp->if_hdrlen = ARC_HDRLEN;
619         ifp->if_mtu = 1500;
620         ifp->if_resolvemulti = arc_resolvemulti;
621         if (ifp->if_baudrate == 0)
622                 ifp->if_baudrate = 2500000;
623         ifa = ifp->if_addr;
624         KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
625         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
626         sdl->sdl_type = IFT_ARCNET;
627         sdl->sdl_alen = ifp->if_addrlen;
628
629         if (ifp->if_flags & IFF_BROADCAST)
630                 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
631
632         ac = (struct arccom *)ifp->if_l2com;
633         ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
634         if (lla == 0) {
635                 /* XXX this message isn't entirely clear, to me -- cgd */
636                 log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
637                    ifp->if_xname, ifp->if_xname);
638         }
639         arc_storelladdr(ifp, lla);
640
641         ifp->if_broadcastaddr = &arcbroadcastaddr;
642
643         bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
644 }
645
646 void
647 arc_ifdetach(struct ifnet *ifp)
648 {
649         bpfdetach(ifp);
650         if_detach(ifp);
651 }
652
653 int
654 arc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
655 {
656         struct ifaddr *ifa = (struct ifaddr *) data;
657         struct ifreq *ifr = (struct ifreq *) data;
658         int error = 0;
659
660         switch (command) {
661         case SIOCSIFADDR:
662                 ifp->if_flags |= IFF_UP;
663                 switch (ifa->ifa_addr->sa_family) {
664 #ifdef INET
665                 case AF_INET:
666                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
667                         arp_ifinit(ifp, ifa);
668                         break;
669 #endif
670                 default:
671                         ifp->if_init(ifp->if_softc);
672                         break;
673                 }
674                 break;
675
676         case SIOCGIFADDR:
677                 {
678                         struct sockaddr *sa;
679
680                         sa = (struct sockaddr *) &ifr->ifr_data;
681                         *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
682                 }
683                 break;
684
685         case SIOCADDMULTI:
686         case SIOCDELMULTI:
687                 if (ifr == NULL)
688                         error = EAFNOSUPPORT;
689                 else {
690                         switch (ifr->ifr_addr.sa_family) {
691                         case AF_INET:
692                         case AF_INET6:
693                                 error = 0;
694                                 break;
695                         default:
696                                 error = EAFNOSUPPORT;
697                                 break;
698                         }
699                 }
700                 break;
701
702         case SIOCSIFMTU:
703                 /*
704                  * Set the interface MTU.
705                  * mtu can't be larger than ARCMTU for RFC1051
706                  * and can't be larger than ARC_PHDS_MTU
707                  */
708                 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
709                     ifr->ifr_mtu > ARC_PHDS_MAXMTU)
710                         error = EINVAL;
711                 else
712                         ifp->if_mtu = ifr->ifr_mtu;
713                 break;
714         }
715
716         return (error);
717 }
718
719 /* based on ether_resolvemulti() */
720 int
721 arc_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
722     struct sockaddr *sa)
723 {
724         struct sockaddr_dl *sdl;
725 #ifdef INET
726         struct sockaddr_in *sin;
727 #endif
728 #ifdef INET6
729         struct sockaddr_in6 *sin6;
730 #endif
731
732         switch(sa->sa_family) {
733         case AF_LINK:
734                 /*
735                 * No mapping needed. Just check that it's a valid MC address.
736                 */
737                 sdl = (struct sockaddr_dl *)sa;
738                 if (*LLADDR(sdl) != arcbroadcastaddr)
739                         return EADDRNOTAVAIL;
740                 *llsa = 0;
741                 return 0;
742 #ifdef INET
743         case AF_INET:
744                 sin = (struct sockaddr_in *)sa;
745                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
746                         return EADDRNOTAVAIL;
747                 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
748                 sdl->sdl_alen = ARC_ADDR_LEN;
749                 *LLADDR(sdl) = 0;
750                 *llsa = (struct sockaddr *)sdl;
751                 return 0;
752 #endif
753 #ifdef INET6
754         case AF_INET6:
755                 sin6 = (struct sockaddr_in6 *)sa;
756                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
757                         /*
758                          * An IP6 address of 0 means listen to all
759                          * of the Ethernet multicast address used for IP6.
760                          * (This is used for multicast routers.)
761                          */
762                         ifp->if_flags |= IFF_ALLMULTI;
763                         *llsa = 0;
764                         return 0;
765                 }
766                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
767                         return EADDRNOTAVAIL;
768                 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
769                 sdl->sdl_alen = ARC_ADDR_LEN;
770                 *LLADDR(sdl) = 0;
771                 *llsa = (struct sockaddr *)sdl;
772                 return 0;
773 #endif
774
775         default:
776                 /*
777                  * Well, the text isn't quite right, but it's the name
778                  * that counts...
779                  */
780                 return EAFNOSUPPORT;
781         }
782 }
783
784 static MALLOC_DEFINE(M_ARCCOM, "arccom", "ARCNET interface internals");
785
786 static void*
787 arc_alloc(u_char type, struct ifnet *ifp)
788 {
789         struct arccom   *ac;
790         
791         ac = malloc(sizeof(struct arccom), M_ARCCOM, M_WAITOK | M_ZERO);
792         ac->ac_ifp = ifp;
793
794         return (ac);
795 }
796
797 static void
798 arc_free(void *com, u_char type)
799 {
800
801         free(com, M_ARCCOM);
802 }
803
804 static int
805 arc_modevent(module_t mod, int type, void *data)
806 {
807
808         switch (type) {
809         case MOD_LOAD:
810                 if_register_com_alloc(IFT_ARCNET, arc_alloc, arc_free);
811                 break;
812         case MOD_UNLOAD:
813                 if_deregister_com_alloc(IFT_ARCNET);
814                 break;
815         default:
816                 return EOPNOTSUPP;
817         }
818
819         return (0);
820 }
821
822 static moduledata_t arc_mod = {
823         "arcnet",
824         arc_modevent,
825         0
826 };
827
828 DECLARE_MODULE(arcnet, arc_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
829 MODULE_VERSION(arcnet, 1);