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.2 2003/06/17 04:28:47 dillon Exp $ */
6 * Copyright (c) 1994, 1995 Ignatios Souvatzis
7 * Copyright (c) 1982, 1989, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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
43 #include "opt_inet6.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.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>
57 #include <machine/cpu.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>
68 #if defined(INET) || defined(INET6)
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/if_ether.h>
75 #include <netinet6/nd6.h>
79 #include <netipx/ipx.h>
80 #include <netipx/ipx_if.h>
83 MODULE_VERSION(arcnet, 1);
85 #define ARCNET_ALLOW_BROKEN_ARP
87 static struct mbuf *arc_defrag __P((struct ifnet *, struct mbuf *));
88 static int arc_resolvemulti __P((struct ifnet *, struct sockaddr **,
91 #define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
93 #define senderr(e) { error = (e); goto bad;}
94 #define SIN(s) ((struct sockaddr_in *)s)
95 #define SIPX(s) ((struct sockaddr_ipx *)s)
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.
103 arc_output(ifp, m, dst, rt0)
106 struct sockaddr *dst;
111 struct arc_header *ah;
113 u_int8_t atype, adst;
117 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
118 return(ENETDOWN); /* m, m1 aren't initialized yet */
121 ac = (struct arccom *)ifp;
124 if ((rt->rt_flags & RTF_UP) == 0) {
125 if ((rt0 = rt = rtalloc1(dst, 1, 0UL)))
128 senderr(EHOSTUNREACH);
130 if (rt->rt_flags & RTF_GATEWAY) {
131 if (rt->rt_gwroute == 0)
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);
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);
146 switch (dst->sa_family) {
151 * For now, use the simple IP addr -> ARCnet addr mapping
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 */
160 atype = (ifp->if_flags & IFF_LINK0) ?
161 ARCTYPE_IP_OLD : ARCTYPE_IP;
167 if (!nd6_resolve(ifp, rt, m, dst, (u_char *)&adst))
168 return(0); /* if not yet resolves */
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;
178 adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
181 adst = arcbroadcastaddr;
187 ah = (struct arc_header *)dst->sa_data;
188 adst = ah->arc_dhost;
189 atype = ah->arc_type;
191 if (atype == ARCTYPE_ARP) {
192 atype = (ifp->if_flags & IFF_LINK0) ?
193 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
195 #ifdef ARCNET_ALLOW_BROKEN_ARP
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...
202 if (ifp->if_flags & IFF_LINK2)
203 mtod(m, struct arphdr *)->ar_pro = atype - 1;
209 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
211 senderr(EAFNOSUPPORT);
214 isphds = arc_isphds(atype);
215 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_DONTWAIT);
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);
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);
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 */
242 if (!IF_HANDOFF(&ifp->if_snd, m, ifp)) {
261 ac = (struct arccom *)ifp;
271 struct arc_header *ah;
273 ac = (struct arccom *)ifp;
274 if ((m = ac->curr_frag) == 0) {
277 /* dequeue new packet */
278 IF_DEQUEUE(&ifp->if_snd, m);
282 ah = mtod(m, struct arc_header *);
283 if (!arc_isphds(ah->arc_type))
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;
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;
295 m_adj(m, ARC_HDRNEWLEN);
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) {
308 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
310 m_freem(ac->curr_frag);
315 ah = mtod(m, struct arc_header *);
316 ah->arc_flag = ac->rsflag;
317 ah->arc_seqid = ac->ac_seqid;
320 ac->rsflag = ac->sflag;
321 } else if ((m->m_pkthdr.len >=
322 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
324 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
327 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
331 ah = mtod(m, struct arc_header *);
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;
340 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
344 ah = mtod(m, struct arc_header *);
345 ah->arc_flag = ac->sflag;
346 ah->arc_seqid = ac->ac_seqid;
349 ah->arc_dhost = ac->arc_dhost;
350 ah->arc_shost = ac->arc_shost;
351 ah->arc_type = ac->arc_type;
357 * Defragmenter. Returns mbuf if last packet found, else
358 * NULL. frees imcoming mbuf as necessary.
361 __inline struct mbuf *
366 struct arc_header *ah, *ah1;
374 ac = (struct arccom *)ifp;
376 if (m->m_len < ARC_HDRNEWLEN) {
377 m = m_pullup(m, ARC_HDRNEWLEN);
384 ah = mtod(m, struct arc_header *);
387 if (!arc_isphds(typ))
393 if (ah->arc_flag == 0xff) {
396 if (m->m_len < ARC_HDRNEWLEN) {
397 m = m_pullup(m, ARC_HDRNEWLEN);
404 ah = mtod(m, struct arc_header *);
407 af = &ac->ac_fragtab[src];
409 s = "debug code error";
411 if (ah->arc_flag & 1) {
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.
422 af->af_maxflag = ah->arc_flag;
424 af->af_seqid = ah->arc_seqid;
429 /* check for unfragmented packet */
430 if (ah->arc_flag == 0)
433 /* do we have a first packet from that src? */
439 ah1 = mtod(m1, struct arc_header *);
441 if (ah->arc_seqid != ah1->arc_seqid) {
446 if (typ != ah1->arc_type) {
451 if (dst != ah1->arc_dhost) {
452 s = "dest host differs";
456 /* typ, seqid and dst are ok here. */
458 if (ah->arc_flag == af->af_lastseen) {
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);
469 * m_cat might free the first mbuf (with pkthdr)
470 * in 2nd chain; therefore:
473 newflen = m->m_pkthdr.len;
477 m1->m_pkthdr.len += newflen;
479 /* is it the last one? */
480 if (af->af_lastseen > af->af_maxflag) {
481 af->af_packet = NULL;
487 /* if all else fails, it is out of sequence, too */
492 af->af_packet = NULL;
498 log(LOG_INFO,"%s%d: got out of seq. packet: %s\n",
499 ifp->if_name, ifp->if_unit, s);
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.
515 return (type != ARCTYPE_IP_OLD &&
516 type != ARCTYPE_ARP_OLD &&
517 type != ARCTYPE_DIAGNOSE);
521 * Process a received Arcnet packet;
522 * the packet is in the mbuf chain m with
530 struct arc_header *ah;
534 if ((ifp->if_flags & IFF_UP) == 0) {
539 /* possibly defragment: */
540 m = arc_defrag(ifp, m);
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)) {
556 ifp->if_ibytes += m->m_pkthdr.len;
558 if (ah->arc_dhost == arcbroadcastaddr) {
559 m->m_flags |= M_BCAST|M_MCAST;
563 atype = ah->arc_type;
567 m_adj(m, ARC_HDRNEWLEN);
568 if (ipflow_fastforward(m))
570 schednetisr(NETISR_IP);
575 m_adj(m, ARC_HDRLEN);
576 if (ipflow_fastforward(m))
578 schednetisr(NETISR_IP);
583 if (ifp->if_flags & IFF_NOARP) {
584 /* Discard packet if ARP is disabled on interface */
588 m_adj(m, ARC_HDRNEWLEN);
589 schednetisr(NETISR_ARP);
591 #ifdef ARCNET_ALLOW_BROKEN_ARP
592 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
596 case ARCTYPE_ARP_OLD:
597 if (ifp->if_flags & IFF_NOARP) {
598 /* Discard packet if ARP is disabled on interface */
602 m_adj(m, ARC_HDRLEN);
603 schednetisr(NETISR_ARP);
605 #ifdef ARCNET_ALLOW_BROKEN_ARP
606 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
612 m_adj(m, ARC_HDRNEWLEN);
613 schednetisr(NETISR_IPV6);
619 m_adj(m, ARC_HDRNEWLEN);
620 schednetisr(NETISR_IPX);
629 IF_HANDOFF(inq, m, NULL);
633 * Register (new) link level address.
636 arc_storelladdr(ifp, lla)
640 ARC_LLADDR(ifp) = lla;
644 * Perform common duties while attaching to interface list
647 arc_ifattach(ifp, lla)
652 struct sockaddr_dl *sdl;
656 ifp->if_type = IFT_ARCNET;
658 ifp->if_hdrlen = ARC_HDRLEN;
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];
666 ifa = ifaddr_byindex(ifp->if_index);
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;
673 if (ifp->if_flags & IFF_BROADCAST)
674 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
676 ac = (struct arccom *)ifp;
677 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
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);
683 arc_storelladdr(ifp, lla);
685 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
697 arc_ioctl(ifp, command, data)
702 struct ifaddr *ifa = (struct ifaddr *) data;
703 struct ifreq *ifr = (struct ifreq *) data;
708 ifp->if_flags |= IFF_UP;
709 switch (ifa->ifa_addr->sa_family) {
712 ifp->if_init(ifp->if_softc); /* before arpwhohas */
713 arp_ifinit(ifp, ifa);
718 * XXX This code is probably wrong
722 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
724 if (ipx_nullhost(*ina))
725 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
727 arc_storelladdr(ifp, ina->x_host.c_host[5]);
732 ifp->if_init(ifp->if_softc);
737 ifp->if_init(ifp->if_softc);
746 sa = (struct sockaddr *) &ifr->ifr_data;
747 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
754 error = EAFNOSUPPORT;
756 switch (ifr->ifr_addr.sa_family) {
762 error = EAFNOSUPPORT;
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
774 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
775 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
778 ifp->if_mtu = ifr->ifr_mtu;
785 /* based on ether_resolvemulti() */
787 arc_resolvemulti(ifp, llsa, sa)
789 struct sockaddr **llsa;
792 struct sockaddr_dl *sdl;
793 struct sockaddr_in *sin;
795 struct sockaddr_in6 *sin6;
798 switch(sa->sa_family) {
801 * No mapping needed. Just check that it's a valid MC address.
803 sdl = (struct sockaddr_dl *)sa;
804 if (*LLADDR(sdl) != arcbroadcastaddr)
805 return EADDRNOTAVAIL;
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,
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;
821 *llsa = (struct sockaddr *)sdl;
826 sin6 = (struct sockaddr_in6 *)sa;
827 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
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.)
833 ifp->if_flags |= IFF_ALLMULTI;
837 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
838 return EADDRNOTAVAIL;
839 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
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;
847 *llsa = (struct sockaddr *)sdl;
853 * Well, the text isn't quite right, but it's the name