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/module.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(struct ifnet *, struct mbuf *);
88 static int arc_resolvemulti(struct ifnet *, struct sockaddr **,
91 u_int8_t arcbroadcastaddr = 0;
93 #define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp))
95 #define senderr(e) { error = (e); goto bad;}
96 #define SIN(s) ((struct sockaddr_in *)s)
97 #define SIPX(s) ((struct sockaddr_ipx *)s)
100 * ARCnet output routine.
101 * Encapsulate a packet of type family for the local net.
102 * Assumes that ifp is actually pointer to arccom structure.
105 arc_output(ifp, m, dst, rt0)
108 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 */
122 switch (dst->sa_family) {
127 * For now, use the simple IP addr -> ARCnet addr mapping
129 if (m->m_flags & (M_BCAST|M_MCAST))
130 adst = arcbroadcastaddr; /* ARCnet broadcast address */
131 else if (ifp->if_flags & IFF_NOARP)
132 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
134 error = arpresolve(ifp, rt0, m, dst, &adst);
136 return (error == EWOULDBLOCK ? 0 : error);
139 atype = (ifp->if_flags & IFF_LINK0) ?
140 ARCTYPE_IP_OLD : ARCTYPE_IP;
145 ah = mtod(m, struct arphdr *);
146 ah->ar_hrd = htons(ARPHRD_ARCNET);
148 loop_copy = -1; /* if this is for us, don't do it */
150 switch(ntohs(ah->ar_op)) {
151 case ARPOP_REVREQUEST:
153 atype = ARCTYPE_REVARP;
162 if (m->m_flags & M_BCAST)
163 bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN);
165 bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN);
172 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)&adst);
175 atype = ARCTYPE_INET6;
180 adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
183 adst = arcbroadcastaddr;
189 ah = (struct arc_header *)dst->sa_data;
190 adst = ah->arc_dhost;
191 atype = ah->arc_type;
193 if (atype == ARCTYPE_ARP) {
194 atype = (ifp->if_flags & IFF_LINK0) ?
195 ARCTYPE_ARP_OLD: ARCTYPE_ARP;
197 #ifdef ARCNET_ALLOW_BROKEN_ARP
199 * XXX It's not clear per RFC826 if this is needed, but
200 * "assigned numbers" say this is wrong.
201 * However, e.g., AmiTCP 3.0Beta used it... we make this
202 * switchable for emergency cases. Not perfect, but...
204 if (ifp->if_flags & IFF_LINK2)
205 mtod(m, struct arphdr *)->ar_pro = atype - 1;
211 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
212 senderr(EAFNOSUPPORT);
215 isphds = arc_isphds(atype);
216 M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_DONTWAIT);
219 ah = mtod(m, struct arc_header *);
220 ah->arc_type = atype;
221 ah->arc_dhost = adst;
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 */
241 if (!IF_HANDOFF(&ifp->if_snd, m, ifp)) {
260 ac = (struct arccom *)ifp;
270 struct arc_header *ah;
272 ac = (struct arccom *)ifp;
273 if ((m = ac->curr_frag) == 0) {
276 /* dequeue new packet */
277 IF_DEQUEUE(&ifp->if_snd, m);
281 ah = mtod(m, struct arc_header *);
282 if (!arc_isphds(ah->arc_type))
285 ++ac->ac_seqid; /* make the seqid unique */
286 tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
287 ac->fsflag = 2 * tfrags - 3;
289 ac->rsflag = ac->fsflag;
290 ac->arc_dhost = ah->arc_dhost;
291 ac->arc_shost = ah->arc_shost;
292 ac->arc_type = ah->arc_type;
294 m_adj(m, ARC_HDRNEWLEN);
298 /* split out next fragment and return it */
299 if (ac->sflag < ac->fsflag) {
300 /* we CAN'T have short packets here */
301 ac->curr_frag = m_split(m, ARC_MAX_DATA, M_DONTWAIT);
302 if (ac->curr_frag == 0) {
307 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
309 m_freem(ac->curr_frag);
314 ah = mtod(m, struct arc_header *);
315 ah->arc_flag = ac->rsflag;
316 ah->arc_seqid = ac->ac_seqid;
319 ac->rsflag = ac->sflag;
320 } else if ((m->m_pkthdr.len >=
321 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
323 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
326 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
330 ah = mtod(m, struct arc_header *);
332 ah->arc_seqid = 0xFFFF;
333 ah->arc_type2 = ac->arc_type;
334 ah->arc_flag2 = ac->sflag;
335 ah->arc_seqid2 = ac->ac_seqid;
339 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
343 ah = mtod(m, struct arc_header *);
344 ah->arc_flag = ac->sflag;
345 ah->arc_seqid = ac->ac_seqid;
348 ah->arc_dhost = ac->arc_dhost;
349 ah->arc_shost = ac->arc_shost;
350 ah->arc_type = ac->arc_type;
356 * Defragmenter. Returns mbuf if last packet found, else
357 * NULL. frees imcoming mbuf as necessary.
360 static __inline struct mbuf *
365 struct arc_header *ah, *ah1;
373 ac = (struct arccom *)ifp;
375 if (m->m_len < ARC_HDRNEWLEN) {
376 m = m_pullup(m, ARC_HDRNEWLEN);
383 ah = mtod(m, struct arc_header *);
386 if (!arc_isphds(typ))
392 if (ah->arc_flag == 0xff) {
395 if (m->m_len < ARC_HDRNEWLEN) {
396 m = m_pullup(m, ARC_HDRNEWLEN);
403 ah = mtod(m, struct arc_header *);
406 af = &ac->ac_fragtab[src];
408 s = "debug code error";
410 if (ah->arc_flag & 1) {
412 * first fragment. We always initialize, which is
413 * about the right thing to do, as we only want to
414 * accept one fragmented packet per src at a time.
421 af->af_maxflag = ah->arc_flag;
423 af->af_seqid = ah->arc_seqid;
428 /* check for unfragmented packet */
429 if (ah->arc_flag == 0)
432 /* do we have a first packet from that src? */
438 ah1 = mtod(m1, struct arc_header *);
440 if (ah->arc_seqid != ah1->arc_seqid) {
445 if (typ != ah1->arc_type) {
450 if (dst != ah1->arc_dhost) {
451 s = "dest host differs";
455 /* typ, seqid and dst are ok here. */
457 if (ah->arc_flag == af->af_lastseen) {
462 if (ah->arc_flag == af->af_lastseen + 2) {
463 /* ok, this is next fragment */
464 af->af_lastseen = ah->arc_flag;
465 m_adj(m,ARC_HDRNEWLEN);
468 * m_cat might free the first mbuf (with pkthdr)
469 * in 2nd chain; therefore:
472 newflen = m->m_pkthdr.len;
476 m1->m_pkthdr.len += newflen;
478 /* is it the last one? */
479 if (af->af_lastseen > af->af_maxflag) {
480 af->af_packet = NULL;
486 /* if all else fails, it is out of sequence, too */
491 af->af_packet = NULL;
497 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
504 * return 1 if Packet Header Definition Standard, else 0.
505 * For now: old IP, old ARP aren't obviously. Lacking correct information,
506 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
507 * (Apple and Novell corporations were involved, among others, in PHDS work).
508 * Easiest is to assume that everybody else uses that, too.
514 return (type != ARCTYPE_IP_OLD &&
515 type != ARCTYPE_ARP_OLD &&
516 type != ARCTYPE_DIAGNOSE);
520 * Process a received Arcnet packet;
521 * the packet is in the mbuf chain m with
529 struct arc_header *ah;
533 if ((ifp->if_flags & IFF_UP) == 0) {
538 /* possibly defragment: */
539 m = arc_defrag(ifp, m);
545 ah = mtod(m, struct arc_header *);
546 /* does this belong to us? */
547 if ((ifp->if_flags & IFF_PROMISC) == 0
548 && ah->arc_dhost != arcbroadcastaddr
549 && ah->arc_dhost != ARC_LLADDR(ifp)) {
554 ifp->if_ibytes += m->m_pkthdr.len;
556 if (ah->arc_dhost == arcbroadcastaddr) {
557 m->m_flags |= M_BCAST|M_MCAST;
561 atype = ah->arc_type;
565 m_adj(m, ARC_HDRNEWLEN);
566 if (ip_fastforward(m))
572 m_adj(m, ARC_HDRLEN);
573 if (ip_fastforward(m))
579 if (ifp->if_flags & IFF_NOARP) {
580 /* Discard packet if ARP is disabled on interface */
584 m_adj(m, ARC_HDRNEWLEN);
586 #ifdef ARCNET_ALLOW_BROKEN_ARP
587 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
591 case ARCTYPE_ARP_OLD:
592 if (ifp->if_flags & IFF_NOARP) {
593 /* Discard packet if ARP is disabled on interface */
597 m_adj(m, ARC_HDRLEN);
599 #ifdef ARCNET_ALLOW_BROKEN_ARP
600 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
606 m_adj(m, ARC_HDRNEWLEN);
612 m_adj(m, ARC_HDRNEWLEN);
620 netisr_dispatch(isr, m);
624 * Register (new) link level address.
627 arc_storelladdr(ifp, lla)
631 ARC_LLADDR(ifp) = lla;
635 * Perform common duties while attaching to interface list
638 arc_ifattach(ifp, lla)
643 struct sockaddr_dl *sdl;
647 ifp->if_type = IFT_ARCNET;
649 ifp->if_hdrlen = ARC_HDRLEN;
651 ifp->if_resolvemulti = arc_resolvemulti;
652 if (ifp->if_baudrate == 0)
653 ifp->if_baudrate = 2500000;
654 #if __FreeBSD_version < 500000
655 ifa = ifnet_addrs[ifp->if_index - 1];
657 ifa = ifaddr_byindex(ifp->if_index);
659 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
660 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
661 sdl->sdl_type = IFT_ARCNET;
662 sdl->sdl_alen = ifp->if_addrlen;
664 if (ifp->if_flags & IFF_BROADCAST)
665 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
667 ac = (struct arccom *)ifp;
668 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
670 /* XXX this message isn't entirely clear, to me -- cgd */
671 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
672 ifp->if_xname, ifp->if_xname);
674 arc_storelladdr(ifp, lla);
676 ifp->if_broadcastaddr = &arcbroadcastaddr;
678 bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
690 arc_ioctl(ifp, command, data)
695 struct ifaddr *ifa = (struct ifaddr *) data;
696 struct ifreq *ifr = (struct ifreq *) data;
701 ifp->if_flags |= IFF_UP;
702 switch (ifa->ifa_addr->sa_family) {
705 ifp->if_init(ifp->if_softc); /* before arpwhohas */
706 arp_ifinit(ifp, ifa);
711 * XXX This code is probably wrong
715 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
717 if (ipx_nullhost(*ina))
718 ina->x_host.c_host[5] = ARC_LLADDR(ifp);
720 arc_storelladdr(ifp, ina->x_host.c_host[5]);
725 ifp->if_init(ifp->if_softc);
730 ifp->if_init(ifp->if_softc);
739 sa = (struct sockaddr *) &ifr->ifr_data;
740 *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
747 error = EAFNOSUPPORT;
749 switch (ifr->ifr_addr.sa_family) {
755 error = EAFNOSUPPORT;
763 * Set the interface MTU.
764 * mtu can't be larger than ARCMTU for RFC1051
765 * and can't be larger than ARC_PHDS_MTU
767 if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
768 ifr->ifr_mtu > ARC_PHDS_MAXMTU)
771 ifp->if_mtu = ifr->ifr_mtu;
778 /* based on ether_resolvemulti() */
780 arc_resolvemulti(ifp, llsa, sa)
782 struct sockaddr **llsa;
785 struct sockaddr_dl *sdl;
786 struct sockaddr_in *sin;
788 struct sockaddr_in6 *sin6;
791 switch(sa->sa_family) {
794 * No mapping needed. Just check that it's a valid MC address.
796 sdl = (struct sockaddr_dl *)sa;
797 if (*LLADDR(sdl) != arcbroadcastaddr)
798 return EADDRNOTAVAIL;
803 sin = (struct sockaddr_in *)sa;
804 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
805 return EADDRNOTAVAIL;
806 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
808 sdl->sdl_len = sizeof *sdl;
809 sdl->sdl_family = AF_LINK;
810 sdl->sdl_index = ifp->if_index;
811 sdl->sdl_type = IFT_ARCNET;
812 sdl->sdl_alen = ARC_ADDR_LEN;
814 *llsa = (struct sockaddr *)sdl;
819 sin6 = (struct sockaddr_in6 *)sa;
820 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
822 * An IP6 address of 0 means listen to all
823 * of the Ethernet multicast address used for IP6.
824 * (This is used for multicast routers.)
826 ifp->if_flags |= IFF_ALLMULTI;
830 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
831 return EADDRNOTAVAIL;
832 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
834 sdl->sdl_len = sizeof *sdl;
835 sdl->sdl_family = AF_LINK;
836 sdl->sdl_index = ifp->if_index;
837 sdl->sdl_type = IFT_ARCNET;
838 sdl->sdl_alen = ARC_ADDR_LEN;
840 *llsa = (struct sockaddr *)sdl;
846 * Well, the text isn't quite right, but it's the name