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