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;
110 struct arc_header *ah;
112 u_int8_t atype, adst;
116 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
117 return(ENETDOWN); /* m, m1 aren't initialized yet */
121 switch (dst->sa_family) {
126 * For now, use the simple IP addr -> ARCnet addr mapping
128 if (m->m_flags & (M_BCAST|M_MCAST))
129 adst = arcbroadcastaddr; /* ARCnet broadcast address */
130 else if (ifp->if_flags & IFF_NOARP)
131 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
133 error = arpresolve(ifp, rt0, m, dst, &adst);
135 return (error == EWOULDBLOCK ? 0 : error);
138 atype = (ifp->if_flags & IFF_LINK0) ?
139 ARCTYPE_IP_OLD : ARCTYPE_IP;
144 ah = mtod(m, struct arphdr *);
145 ah->ar_hrd = htons(ARPHRD_ARCNET);
147 loop_copy = -1; /* if this is for us, don't do it */
149 switch(ntohs(ah->ar_op)) {
150 case ARPOP_REVREQUEST:
152 atype = ARCTYPE_REVARP;
161 if (m->m_flags & M_BCAST)
162 bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN);
164 bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN);
171 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)&adst);
174 atype = ARCTYPE_INET6;
179 adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
182 adst = arcbroadcastaddr;
188 ah = (struct arc_header *)dst->sa_data;
189 adst = ah->arc_dhost;
190 atype = ah->arc_type;
192 if (atype == ARCTYPE_ARP) {
193 atype = (ifp->if_flags & IFF_LINK0) ?
194 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
196 #ifdef ARCNET_ALLOW_BROKEN_ARP
198 * XXX It's not clear per RFC826 if this is needed, but
199 * "assigned numbers" say this is wrong.
200 * However, e.g., AmiTCP 3.0Beta used it... we make this
201 * switchable for emergency cases. Not perfect, but...
203 if (ifp->if_flags & IFF_LINK2)
204 mtod(m, struct arphdr *)->ar_pro = atype - 1;
210 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
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 = ARC_LLADDR(ifp);
227 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
228 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
229 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
231 (void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
232 } else if (ah->arc_dhost == ah->arc_shost) {
233 (void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
234 return (0); /* XXX */
240 if (!IF_HANDOFF(&ifp->if_snd, m, ifp)) {
259 ac = (struct arccom *)ifp;
269 struct arc_header *ah;
271 ac = (struct arccom *)ifp;
272 if ((m = ac->curr_frag) == 0) {
275 /* dequeue new packet */
276 IF_DEQUEUE(&ifp->if_snd, m);
280 ah = mtod(m, struct arc_header *);
281 if (!arc_isphds(ah->arc_type))
284 ++ac->ac_seqid; /* make the seqid unique */
285 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
286 ac->fsflag = 2 * tfrags - 3;
288 ac->rsflag = ac->fsflag;
289 ac->arc_dhost = ah->arc_dhost;
290 ac->arc_shost = ah->arc_shost;
291 ac->arc_type = ah->arc_type;
293 m_adj(m, ARC_HDRNEWLEN);
297 /* split out next fragment and return it */
298 if (ac->sflag < ac->fsflag) {
299 /* we CAN'T have short packets here */
300 ac->curr_frag = m_split(m, ARC_MAX_DATA, M_DONTWAIT);
301 if (ac->curr_frag == 0) {
306 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
308 m_freem(ac->curr_frag);
313 ah = mtod(m, struct arc_header *);
314 ah->arc_flag = ac->rsflag;
315 ah->arc_seqid = ac->ac_seqid;
318 ac->rsflag = ac->sflag;
319 } else if ((m->m_pkthdr.len >=
320 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
322 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
325 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
329 ah = mtod(m, struct arc_header *);
331 ah->arc_seqid = 0xFFFF;
332 ah->arc_type2 = ac->arc_type;
333 ah->arc_flag2 = ac->sflag;
334 ah->arc_seqid2 = ac->ac_seqid;
338 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
342 ah = mtod(m, struct arc_header *);
343 ah->arc_flag = ac->sflag;
344 ah->arc_seqid = ac->ac_seqid;
347 ah->arc_dhost = ac->arc_dhost;
348 ah->arc_shost = ac->arc_shost;
349 ah->arc_type = ac->arc_type;
355 * Defragmenter. Returns mbuf if last packet found, else
356 * NULL. frees imcoming mbuf as necessary.
359 static __inline struct mbuf *
364 struct arc_header *ah, *ah1;
372 ac = (struct arccom *)ifp;
374 if (m->m_len < ARC_HDRNEWLEN) {
375 m = m_pullup(m, ARC_HDRNEWLEN);
382 ah = mtod(m, struct arc_header *);
385 if (!arc_isphds(typ))
391 if (ah->arc_flag == 0xff) {
394 if (m->m_len < ARC_HDRNEWLEN) {
395 m = m_pullup(m, ARC_HDRNEWLEN);
402 ah = mtod(m, struct arc_header *);
405 af = &ac->ac_fragtab[src];
407 s = "debug code error";
409 if (ah->arc_flag & 1) {
411 * first fragment. We always initialize, which is
412 * about the right thing to do, as we only want to
413 * accept one fragmented packet per src at a time.
420 af->af_maxflag = ah->arc_flag;
422 af->af_seqid = ah->arc_seqid;
427 /* check for unfragmented packet */
428 if (ah->arc_flag == 0)
431 /* do we have a first packet from that src? */
437 ah1 = mtod(m1, struct arc_header *);
439 if (ah->arc_seqid != ah1->arc_seqid) {
444 if (typ != ah1->arc_type) {
449 if (dst != ah1->arc_dhost) {
450 s = "dest host differs";
454 /* typ, seqid and dst are ok here. */
456 if (ah->arc_flag == af->af_lastseen) {
461 if (ah->arc_flag == af->af_lastseen + 2) {
462 /* ok, this is next fragment */
463 af->af_lastseen = ah->arc_flag;
464 m_adj(m,ARC_HDRNEWLEN);
467 * m_cat might free the first mbuf (with pkthdr)
468 * in 2nd chain; therefore:
471 newflen = m->m_pkthdr.len;
475 m1->m_pkthdr.len += newflen;
477 /* is it the last one? */
478 if (af->af_lastseen > af->af_maxflag) {
479 af->af_packet = NULL;
485 /* if all else fails, it is out of sequence, too */
490 af->af_packet = NULL;
496 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
503 * return 1 if Packet Header Definition Standard, else 0.
504 * For now: old IP, old ARP aren't obviously. Lacking correct information,
505 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
506 * (Apple and Novell corporations were involved, among others, in PHDS work).
507 * Easiest is to assume that everybody else uses that, too.
513 return (type != ARCTYPE_IP_OLD &&
514 type != ARCTYPE_ARP_OLD &&
515 type != ARCTYPE_DIAGNOSE);
519 * Process a received Arcnet packet;
520 * the packet is in the mbuf chain m with
528 struct arc_header *ah;
532 if ((ifp->if_flags & IFF_UP) == 0) {
537 /* possibly defragment: */
538 m = arc_defrag(ifp, m);
544 ah = mtod(m, struct arc_header *);
545 /* does this belong to us? */
546 if ((ifp->if_flags & IFF_PROMISC) == 0
547 && ah->arc_dhost != arcbroadcastaddr
548 && ah->arc_dhost != ARC_LLADDR(ifp)) {
553 ifp->if_ibytes += m->m_pkthdr.len;
555 if (ah->arc_dhost == arcbroadcastaddr) {
556 m->m_flags |= M_BCAST|M_MCAST;
560 atype = ah->arc_type;
564 m_adj(m, ARC_HDRNEWLEN);
565 if (ip_fastforward(m))
571 m_adj(m, ARC_HDRLEN);
572 if (ip_fastforward(m))
578 if (ifp->if_flags & IFF_NOARP) {
579 /* Discard packet if ARP is disabled on interface */
583 m_adj(m, ARC_HDRNEWLEN);
585 #ifdef ARCNET_ALLOW_BROKEN_ARP
586 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
590 case ARCTYPE_ARP_OLD:
591 if (ifp->if_flags & IFF_NOARP) {
592 /* Discard packet if ARP is disabled on interface */
596 m_adj(m, ARC_HDRLEN);
598 #ifdef ARCNET_ALLOW_BROKEN_ARP
599 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
605 m_adj(m, ARC_HDRNEWLEN);
611 m_adj(m, ARC_HDRNEWLEN);
619 netisr_dispatch(isr, m);
623 * Register (new) link level address.
626 arc_storelladdr(ifp, lla)
630 ARC_LLADDR(ifp) = lla;
634 * Perform common duties while attaching to interface list
637 arc_ifattach(ifp, lla)
642 struct sockaddr_dl *sdl;
646 ifp->if_type = IFT_ARCNET;
648 ifp->if_hdrlen = ARC_HDRLEN;
650 ifp->if_resolvemulti = arc_resolvemulti;
651 if (ifp->if_baudrate == 0)
652 ifp->if_baudrate = 2500000;
653 #if __FreeBSD_version < 500000
654 ifa = ifnet_addrs[ifp->if_index - 1];
656 ifa = ifaddr_byindex(ifp->if_index);
658 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
659 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
660 sdl->sdl_type = IFT_ARCNET;
661 sdl->sdl_alen = ifp->if_addrlen;
663 if (ifp->if_flags & IFF_BROADCAST)
664 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
666 ac = (struct arccom *)ifp;
667 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
669 /* XXX this message isn't entirely clear, to me -- cgd */
670 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
671 ifp->if_xname, ifp->if_xname);
673 arc_storelladdr(ifp, lla);
675 ifp->if_broadcastaddr = &arcbroadcastaddr;
677 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
689 arc_ioctl(ifp, command, data)
694 struct ifaddr *ifa = (struct ifaddr *) data;
695 struct ifreq *ifr = (struct ifreq *) data;
700 ifp->if_flags |= IFF_UP;
701 switch (ifa->ifa_addr->sa_family) {
704 ifp->if_init(ifp->if_softc); /* before arpwhohas */
705 arp_ifinit(ifp, ifa);
710 * XXX This code is probably wrong
714 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
716 if (ipx_nullhost(*ina))
717 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
719 arc_storelladdr(ifp, ina->x_host.c_host[5]);
724 ifp->if_init(ifp->if_softc);
729 ifp->if_init(ifp->if_softc);
738 sa = (struct sockaddr *) &ifr->ifr_data;
739 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
746 error = EAFNOSUPPORT;
748 switch (ifr->ifr_addr.sa_family) {
754 error = EAFNOSUPPORT;
762 * Set the interface MTU.
763 * mtu can't be larger than ARCMTU for RFC1051
764 * and can't be larger than ARC_PHDS_MTU
766 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
767 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
770 ifp->if_mtu = ifr->ifr_mtu;
777 /* based on ether_resolvemulti() */
779 arc_resolvemulti(ifp, llsa, sa)
781 struct sockaddr **llsa;
784 struct sockaddr_dl *sdl;
785 struct sockaddr_in *sin;
787 struct sockaddr_in6 *sin6;
790 switch(sa->sa_family) {
793 * No mapping needed. Just check that it's a valid MC address.
795 sdl = (struct sockaddr_dl *)sa;
796 if (*LLADDR(sdl) != arcbroadcastaddr)
797 return EADDRNOTAVAIL;
802 sin = (struct sockaddr_in *)sa;
803 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
804 return EADDRNOTAVAIL;
805 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
807 sdl->sdl_len = sizeof *sdl;
808 sdl->sdl_family = AF_LINK;
809 sdl->sdl_index = ifp->if_index;
810 sdl->sdl_type = IFT_ARCNET;
811 sdl->sdl_alen = ARC_ADDR_LEN;
813 *llsa = (struct sockaddr *)sdl;
818 sin6 = (struct sockaddr_in6 *)sa;
819 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
821 * An IP6 address of 0 means listen to all
822 * of the Ethernet multicast address used for IP6.
823 * (This is used for multicast routers.)
825 ifp->if_flags |= IFF_ALLMULTI;
829 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
830 return EADDRNOTAVAIL;
831 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
833 sdl->sdl_len = sizeof *sdl;
834 sdl->sdl_family = AF_LINK;
835 sdl->sdl_index = ifp->if_index;
836 sdl->sdl_type = IFT_ARCNET;
837 sdl->sdl_alen = ARC_ADDR_LEN;
839 *llsa = (struct sockaddr *)sdl;
845 * Well, the text isn't quite right, but it's the name