1 /* $NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $ */
5 * Copyright (c) 1994, 1995 Ignatios Souvatzis
6 * Copyright (c) 1982, 1989, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
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
42 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.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>
56 #include <machine/cpu.h>
59 #include <net/netisr.h>
60 #include <net/route.h>
61 #include <net/if_dl.h>
62 #include <net/if_types.h>
63 #include <net/if_arc.h>
64 #include <net/if_arp.h>
67 #if defined(INET) || defined(INET6)
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <netinet/if_ether.h>
74 #include <netinet6/nd6.h>
78 #include <netipx/ipx.h>
79 #include <netipx/ipx_if.h>
82 MODULE_VERSION(arcnet, 1);
84 #define ARCNET_ALLOW_BROKEN_ARP
86 static struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
87 static int arc_resolvemulti(struct ifnet *, struct sockaddr **,
90 u_int8_t arcbroadcastaddr = 0;
92 #define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
94 #define senderr(e) { error = (e); goto bad;}
95 #define SIN(s) ((struct sockaddr_in *)s)
96 #define SIPX(s) ((struct sockaddr_ipx *)s)
99 * ARCnet output routine.
100 * Encapsulate a packet of type family for the local net.
101 * Assumes that ifp is actually pointer to arccom structure.
104 arc_output(ifp, m, dst, rt0)
107 struct sockaddr *dst;
112 struct arc_header *ah;
114 u_int8_t atype, adst;
118 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
119 return(ENETDOWN); /* m, m1 aren't initialized yet */
122 ac = (struct arccom *)ifp;
124 error = rt_check(&rt, &rt0, dst);
128 switch (dst->sa_family) {
133 * For now, use the simple IP addr -> ARCnet addr mapping
135 if (m->m_flags & (M_BCAST|M_MCAST))
136 adst = arcbroadcastaddr; /* ARCnet broadcast address */
137 else if (ifp->if_flags & IFF_NOARP)
138 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
139 else if (!arpresolve(ifp, rt, m, dst, &adst, rt0))
140 return 0; /* not resolved yet */
142 atype = (ifp->if_flags & IFF_LINK0) ?
143 ARCTYPE_IP_OLD : ARCTYPE_IP;
149 if (!nd6_resolve(ifp, rt, m, dst, (u_char *)&adst))
150 return(0); /* if not yet resolves */
152 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)&adst))
153 return(0); /* it must be impossible, but... */
154 #endif /* OLDIP6OUTPUT */
155 atype = ARCTYPE_INET6;
160 adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
163 adst = arcbroadcastaddr;
169 ah = (struct arc_header *)dst->sa_data;
170 adst = ah->arc_dhost;
171 atype = ah->arc_type;
173 if (atype == ARCTYPE_ARP) {
174 atype = (ifp->if_flags & IFF_LINK0) ?
175 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
177 #ifdef ARCNET_ALLOW_BROKEN_ARP
179 * XXX It's not clear per RFC826 if this is needed, but
180 * "assigned numbers" say this is wrong.
181 * However, e.g., AmiTCP 3.0Beta used it... we make this
182 * switchable for emergency cases. Not perfect, but...
184 if (ifp->if_flags & IFF_LINK2)
185 mtod(m, struct arphdr *)->ar_pro = atype - 1;
191 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
192 senderr(EAFNOSUPPORT);
195 isphds = arc_isphds(atype);
196 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_DONTWAIT);
199 ah = mtod(m, struct arc_header *);
200 ah->arc_type = atype;
201 ah->arc_dhost = adst;
202 ah->arc_shost = ARC_LLADDR(ifp);
208 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
209 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
210 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
212 (void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
213 } else if (ah->arc_dhost == ah->arc_shost) {
214 (void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
215 return (0); /* XXX */
221 if (!IF_HANDOFF(&ifp->if_snd, m, ifp)) {
240 ac = (struct arccom *)ifp;
250 struct arc_header *ah;
252 ac = (struct arccom *)ifp;
253 if ((m = ac->curr_frag) == 0) {
256 /* dequeue new packet */
257 IF_DEQUEUE(&ifp->if_snd, m);
261 ah = mtod(m, struct arc_header *);
262 if (!arc_isphds(ah->arc_type))
265 ++ac->ac_seqid; /* make the seqid unique */
266 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
267 ac->fsflag = 2 * tfrags - 3;
269 ac->rsflag = ac->fsflag;
270 ac->arc_dhost = ah->arc_dhost;
271 ac->arc_shost = ah->arc_shost;
272 ac->arc_type = ah->arc_type;
274 m_adj(m, ARC_HDRNEWLEN);
278 /* split out next fragment and return it */
279 if (ac->sflag < ac->fsflag) {
280 /* we CAN'T have short packets here */
281 ac->curr_frag = m_split(m, ARC_MAX_DATA, M_DONTWAIT);
282 if (ac->curr_frag == 0) {
287 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
289 m_freem(ac->curr_frag);
294 ah = mtod(m, struct arc_header *);
295 ah->arc_flag = ac->rsflag;
296 ah->arc_seqid = ac->ac_seqid;
299 ac->rsflag = ac->sflag;
300 } else if ((m->m_pkthdr.len >=
301 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
303 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
306 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
310 ah = mtod(m, struct arc_header *);
312 ah->arc_seqid = 0xFFFF;
313 ah->arc_type2 = ac->arc_type;
314 ah->arc_flag2 = ac->sflag;
315 ah->arc_seqid2 = ac->ac_seqid;
319 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
323 ah = mtod(m, struct arc_header *);
324 ah->arc_flag = ac->sflag;
325 ah->arc_seqid = ac->ac_seqid;
328 ah->arc_dhost = ac->arc_dhost;
329 ah->arc_shost = ac->arc_shost;
330 ah->arc_type = ac->arc_type;
336 * Defragmenter. Returns mbuf if last packet found, else
337 * NULL. frees imcoming mbuf as necessary.
340 static __inline struct mbuf *
345 struct arc_header *ah, *ah1;
353 ac = (struct arccom *)ifp;
355 if (m->m_len < ARC_HDRNEWLEN) {
356 m = m_pullup(m, ARC_HDRNEWLEN);
363 ah = mtod(m, struct arc_header *);
366 if (!arc_isphds(typ))
372 if (ah->arc_flag == 0xff) {
375 if (m->m_len < ARC_HDRNEWLEN) {
376 m = m_pullup(m, ARC_HDRNEWLEN);
383 ah = mtod(m, struct arc_header *);
386 af = &ac->ac_fragtab[src];
388 s = "debug code error";
390 if (ah->arc_flag & 1) {
392 * first fragment. We always initialize, which is
393 * about the right thing to do, as we only want to
394 * accept one fragmented packet per src at a time.
401 af->af_maxflag = ah->arc_flag;
403 af->af_seqid = ah->arc_seqid;
408 /* check for unfragmented packet */
409 if (ah->arc_flag == 0)
412 /* do we have a first packet from that src? */
418 ah1 = mtod(m1, struct arc_header *);
420 if (ah->arc_seqid != ah1->arc_seqid) {
425 if (typ != ah1->arc_type) {
430 if (dst != ah1->arc_dhost) {
431 s = "dest host differs";
435 /* typ, seqid and dst are ok here. */
437 if (ah->arc_flag == af->af_lastseen) {
442 if (ah->arc_flag == af->af_lastseen + 2) {
443 /* ok, this is next fragment */
444 af->af_lastseen = ah->arc_flag;
445 m_adj(m,ARC_HDRNEWLEN);
448 * m_cat might free the first mbuf (with pkthdr)
449 * in 2nd chain; therefore:
452 newflen = m->m_pkthdr.len;
456 m1->m_pkthdr.len += newflen;
458 /* is it the last one? */
459 if (af->af_lastseen > af->af_maxflag) {
460 af->af_packet = NULL;
466 /* if all else fails, it is out of sequence, too */
471 af->af_packet = NULL;
477 log(LOG_INFO,"%s%d: got out of seq. packet: %s\n",
478 ifp->if_name, ifp->if_unit, s);
484 * return 1 if Packet Header Definition Standard, else 0.
485 * For now: old IP, old ARP aren't obviously. Lacking correct information,
486 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
487 * (Apple and Novell corporations were involved, among others, in PHDS work).
488 * Easiest is to assume that everybody else uses that, too.
494 return (type != ARCTYPE_IP_OLD &&
495 type != ARCTYPE_ARP_OLD &&
496 type != ARCTYPE_DIAGNOSE);
500 * Process a received Arcnet packet;
501 * the packet is in the mbuf chain m with
509 struct arc_header *ah;
513 if ((ifp->if_flags & IFF_UP) == 0) {
518 /* possibly defragment: */
519 m = arc_defrag(ifp, m);
525 ah = mtod(m, struct arc_header *);
526 /* does this belong to us? */
527 if ((ifp->if_flags & IFF_PROMISC) == 0
528 && ah->arc_dhost != arcbroadcastaddr
529 && ah->arc_dhost != ARC_LLADDR(ifp)) {
534 ifp->if_ibytes += m->m_pkthdr.len;
536 if (ah->arc_dhost == arcbroadcastaddr) {
537 m->m_flags |= M_BCAST|M_MCAST;
541 atype = ah->arc_type;
545 m_adj(m, ARC_HDRNEWLEN);
546 if (ipflow_fastforward(m))
552 m_adj(m, ARC_HDRLEN);
553 if (ipflow_fastforward(m))
559 if (ifp->if_flags & IFF_NOARP) {
560 /* Discard packet if ARP is disabled on interface */
564 m_adj(m, ARC_HDRNEWLEN);
566 #ifdef ARCNET_ALLOW_BROKEN_ARP
567 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
571 case ARCTYPE_ARP_OLD:
572 if (ifp->if_flags & IFF_NOARP) {
573 /* Discard packet if ARP is disabled on interface */
577 m_adj(m, ARC_HDRLEN);
579 #ifdef ARCNET_ALLOW_BROKEN_ARP
580 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
586 m_adj(m, ARC_HDRNEWLEN);
592 m_adj(m, ARC_HDRNEWLEN);
600 netisr_dispatch(isr, m);
604 * Register (new) link level address.
607 arc_storelladdr(ifp, lla)
611 ARC_LLADDR(ifp) = lla;
615 * Perform common duties while attaching to interface list
618 arc_ifattach(ifp, lla)
623 struct sockaddr_dl *sdl;
627 ifp->if_type = IFT_ARCNET;
629 ifp->if_hdrlen = ARC_HDRLEN;
631 ifp->if_resolvemulti = arc_resolvemulti;
632 if (ifp->if_baudrate == 0)
633 ifp->if_baudrate = 2500000;
634 #if __FreeBSD_version < 500000
635 ifa = ifnet_addrs[ifp->if_index - 1];
637 ifa = ifaddr_byindex(ifp->if_index);
639 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
640 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
641 sdl->sdl_type = IFT_ARCNET;
642 sdl->sdl_alen = ifp->if_addrlen;
644 if (ifp->if_flags & IFF_BROADCAST)
645 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
647 ac = (struct arccom *)ifp;
648 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
650 /* XXX this message isn't entirely clear, to me -- cgd */
651 log(LOG_ERR,"%s%d: link address 0 reserved for broadcasts. Please change it and ifconfig %s%d down up\n",
652 ifp->if_name, ifp->if_unit, ifp->if_name, ifp->if_unit);
654 arc_storelladdr(ifp, lla);
656 ifp->if_broadcastaddr = &arcbroadcastaddr;
658 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
670 arc_ioctl(ifp, command, data)
675 struct ifaddr *ifa = (struct ifaddr *) data;
676 struct ifreq *ifr = (struct ifreq *) data;
681 ifp->if_flags |= IFF_UP;
682 switch (ifa->ifa_addr->sa_family) {
685 ifp->if_init(ifp->if_softc); /* before arpwhohas */
686 arp_ifinit(ifp, ifa);
691 * XXX This code is probably wrong
695 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
697 if (ipx_nullhost(*ina))
698 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
700 arc_storelladdr(ifp, ina->x_host.c_host[5]);
705 ifp->if_init(ifp->if_softc);
710 ifp->if_init(ifp->if_softc);
719 sa = (struct sockaddr *) &ifr->ifr_data;
720 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
727 error = EAFNOSUPPORT;
729 switch (ifr->ifr_addr.sa_family) {
735 error = EAFNOSUPPORT;
743 * Set the interface MTU.
744 * mtu can't be larger than ARCMTU for RFC1051
745 * and can't be larger than ARC_PHDS_MTU
747 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
748 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
751 ifp->if_mtu = ifr->ifr_mtu;
758 /* based on ether_resolvemulti() */
760 arc_resolvemulti(ifp, llsa, sa)
762 struct sockaddr **llsa;
765 struct sockaddr_dl *sdl;
766 struct sockaddr_in *sin;
768 struct sockaddr_in6 *sin6;
771 switch(sa->sa_family) {
774 * No mapping needed. Just check that it's a valid MC address.
776 sdl = (struct sockaddr_dl *)sa;
777 if (*LLADDR(sdl) != arcbroadcastaddr)
778 return EADDRNOTAVAIL;
783 sin = (struct sockaddr_in *)sa;
784 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
785 return EADDRNOTAVAIL;
786 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
788 sdl->sdl_len = sizeof *sdl;
789 sdl->sdl_family = AF_LINK;
790 sdl->sdl_index = ifp->if_index;
791 sdl->sdl_type = IFT_ARCNET;
792 sdl->sdl_alen = ARC_ADDR_LEN;
794 *llsa = (struct sockaddr *)sdl;
799 sin6 = (struct sockaddr_in6 *)sa;
800 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
802 * An IP6 address of 0 means listen to all
803 * of the Ethernet multicast address used for IP6.
804 * (This is used for multicast routers.)
806 ifp->if_flags |= IFF_ALLMULTI;
810 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
811 return EADDRNOTAVAIL;
812 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
814 sdl->sdl_len = sizeof *sdl;
815 sdl->sdl_family = AF_LINK;
816 sdl->sdl_index = ifp->if_index;
817 sdl->sdl_type = IFT_ARCNET;
818 sdl->sdl_alen = ARC_ADDR_LEN;
820 *llsa = (struct sockaddr *)sdl;
826 * Well, the text isn't quite right, but it's the name