The cam_sim structure was being deallocated unconditionally by device
[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.7 2004/02/13 17:45:49 joerg 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 (struct ifnet *, struct mbuf *);
88 static int arc_resolvemulti (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: can't handle af%d\n", ifp->if_xname,
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: got out of seq. packet: %s\n",
499             ifp->if_xname, 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         int isr;
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                 isr = NETISR_IP;
571                 break;
572
573         case ARCTYPE_IP_OLD:
574                 m_adj(m, ARC_HDRLEN);
575                 if (ipflow_fastforward(m))
576                         return;
577                 isr = NETISR_IP;
578                 break;
579
580         case ARCTYPE_ARP:
581                 if (ifp->if_flags & IFF_NOARP) {
582                         /* Discard packet if ARP is disabled on interface */
583                         m_freem(m);
584                         return;
585                 }
586                 m_adj(m, ARC_HDRNEWLEN);
587                 isr = NETISR_ARP;
588 #ifdef ARCNET_ALLOW_BROKEN_ARP
589                 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
590 #endif
591                 break;
592
593         case ARCTYPE_ARP_OLD:
594                 if (ifp->if_flags & IFF_NOARP) {
595                         /* Discard packet if ARP is disabled on interface */
596                         m_freem(m);
597                         return;
598                 }
599                 m_adj(m, ARC_HDRLEN);
600                 isr = NETISR_ARP;
601 #ifdef ARCNET_ALLOW_BROKEN_ARP
602                 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
603 #endif
604                 break;
605 #endif
606 #ifdef INET6
607         case ARCTYPE_INET6:
608                 m_adj(m, ARC_HDRNEWLEN);
609                 isr = NETISR_IPV6;
610                 break;
611 #endif
612 #ifdef IPX
613         case ARCTYPE_IPX:
614                 m_adj(m, ARC_HDRNEWLEN);
615                 isr = NETISR_IPX;
616                 break;
617 #endif
618         default:
619                 m_freem(m);
620                 return;
621         }
622
623         netisr_dispatch(isr, m);
624 }
625
626 /*
627  * Register (new) link level address.
628  */
629 void
630 arc_storelladdr(ifp, lla)
631         struct ifnet *ifp;
632         u_int8_t lla;
633 {
634         ARC_LLADDR(ifp) = lla;
635 }
636
637 /*
638  * Perform common duties while attaching to interface list
639  */
640 void
641 arc_ifattach(ifp, lla)
642         struct ifnet *ifp;
643         u_int8_t lla;
644 {
645         struct ifaddr *ifa;
646         struct sockaddr_dl *sdl;
647         struct arccom *ac;
648
649         if_attach(ifp);
650         ifp->if_type = IFT_ARCNET;
651         ifp->if_addrlen = 1;
652         ifp->if_hdrlen = ARC_HDRLEN;
653         ifp->if_mtu = 1500;
654         ifp->if_resolvemulti = arc_resolvemulti;
655         if (ifp->if_baudrate == 0)
656                 ifp->if_baudrate = 2500000;
657 #if defined(__DragonFly__) || __FreeBSD_version < 500000
658         ifa = ifnet_addrs[ifp->if_index - 1];
659 #else
660         ifa = ifaddr_byindex(ifp->if_index);
661 #endif
662         KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
663         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
664         sdl->sdl_type = IFT_ARCNET;
665         sdl->sdl_alen = ifp->if_addrlen;
666
667         if (ifp->if_flags & IFF_BROADCAST)
668                 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
669
670         ac = (struct arccom *)ifp;
671         ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
672         if (lla == 0) {
673                 /* XXX this message isn't entirely clear, to me -- cgd */
674                 log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
675                    ifp->if_xname, ifp->if_xname);
676         }
677         arc_storelladdr(ifp, lla);
678
679         bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
680 }
681
682 void
683 arc_ifdetach(ifp)
684         struct ifnet *ifp;
685 {
686         bpfdetach(ifp);
687         if_detach(ifp);
688 }
689
690 int
691 arc_ioctl(ifp, command, data)
692         struct ifnet *ifp;
693         int command;
694         caddr_t data;
695 {
696         struct ifaddr *ifa = (struct ifaddr *) data;
697         struct ifreq *ifr = (struct ifreq *) data;
698         int error = 0;
699
700         switch (command) {
701         case SIOCSIFADDR:
702                 ifp->if_flags |= IFF_UP;
703                 switch (ifa->ifa_addr->sa_family) {
704 #ifdef INET
705                 case AF_INET:
706                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
707                         arp_ifinit(ifp, ifa);
708                         break;
709 #endif
710 #ifdef IPX
711                 /*
712                  * XXX This code is probably wrong
713                  */
714                 case AF_IPX:
715                 {
716                         struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
717
718                         if (ipx_nullhost(*ina))
719                                 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
720                         else
721                                 arc_storelladdr(ifp, ina->x_host.c_host[5]);
722
723                         /*
724                          * Set new address
725                          */
726                         ifp->if_init(ifp->if_softc);
727                         break;
728                 }
729 #endif
730                 default:
731                         ifp->if_init(ifp->if_softc);
732                         break;
733                 }
734                 break;
735
736         case SIOCGIFADDR:
737                 {
738                         struct sockaddr *sa;
739
740                         sa = (struct sockaddr *) &ifr->ifr_data;
741                         *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
742                 }
743                 break;
744
745         case SIOCADDMULTI:
746         case SIOCDELMULTI:
747                 if (ifr == NULL)
748                         error = EAFNOSUPPORT;
749                 else {
750                         switch (ifr->ifr_addr.sa_family) {
751                         case AF_INET:
752                         case AF_INET6:
753                                 error = 0;
754                                 break;
755                         default:
756                                 error = EAFNOSUPPORT;
757                                 break;
758                         }
759                 }
760                 break;
761
762         case SIOCSIFMTU:
763                 /*
764                  * Set the interface MTU.
765                  * mtu can't be larger than ARCMTU for RFC1051
766                  * and can't be larger than ARC_PHDS_MTU
767                  */
768                 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
769                     ifr->ifr_mtu > ARC_PHDS_MAXMTU)
770                         error = EINVAL;
771                 else
772                         ifp->if_mtu = ifr->ifr_mtu;
773                 break;
774         }
775
776         return (error);
777 }
778
779 /* based on ether_resolvemulti() */
780 int
781 arc_resolvemulti(ifp, llsa, sa)
782         struct ifnet *ifp;
783         struct sockaddr **llsa;
784         struct sockaddr *sa;
785 {
786         struct sockaddr_dl *sdl;
787         struct sockaddr_in *sin;
788 #ifdef INET6
789         struct sockaddr_in6 *sin6;
790 #endif
791
792         switch(sa->sa_family) {
793         case AF_LINK:
794                 /*
795                 * No mapping needed. Just check that it's a valid MC address.
796                 */
797                 sdl = (struct sockaddr_dl *)sa;
798                 if (*LLADDR(sdl) != arcbroadcastaddr)
799                         return EADDRNOTAVAIL;
800                 *llsa = 0;
801                 return 0;
802 #ifdef INET
803         case AF_INET:
804                 sin = (struct sockaddr_in *)sa;
805                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
806                         return EADDRNOTAVAIL;
807                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
808                        M_WAITOK|M_ZERO);
809                 sdl->sdl_len = sizeof *sdl;
810                 sdl->sdl_family = AF_LINK;
811                 sdl->sdl_index = ifp->if_index;
812                 sdl->sdl_type = IFT_ARCNET;
813                 sdl->sdl_alen = ARC_ADDR_LEN;
814                 *LLADDR(sdl) = 0;
815                 *llsa = (struct sockaddr *)sdl;
816                 return 0;
817 #endif
818 #ifdef INET6
819         case AF_INET6:
820                 sin6 = (struct sockaddr_in6 *)sa;
821                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
822                         /*
823                          * An IP6 address of 0 means listen to all
824                          * of the Ethernet multicast address used for IP6.
825                          * (This is used for multicast routers.)
826                          */
827                         ifp->if_flags |= IFF_ALLMULTI;
828                         *llsa = 0;
829                         return 0;
830                 }
831                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
832                         return EADDRNOTAVAIL;
833                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
834                        M_WAITOK|M_ZERO);
835                 sdl->sdl_len = sizeof *sdl;
836                 sdl->sdl_family = AF_LINK;
837                 sdl->sdl_index = ifp->if_index;
838                 sdl->sdl_type = IFT_ARCNET;
839                 sdl->sdl_alen = ARC_ADDR_LEN;
840                 *LLADDR(sdl) = 0;
841                 *llsa = (struct sockaddr *)sdl;
842                 return 0;
843 #endif
844
845         default:
846                 /*
847                  * Well, the text isn't quite right, but it's the name
848                  * that counts...
849                  */
850                 return EAFNOSUPPORT;
851         }
852 }