Home | History | Annotate | Line # | Download | only in net
if_arcsubr.c revision 1.36.2.2
      1  1.36.2.2  jdolecek /*	$NetBSD: if_arcsubr.c,v 1.36.2.2 2002/03/16 16:02:04 jdolecek Exp $	*/
      2       1.1     glass 
      3       1.1     glass /*
      4       1.1     glass  * Copyright (c) 1994, 1995 Ignatios Souvatzis
      5       1.1     glass  * Copyright (c) 1982, 1989, 1993
      6       1.1     glass  *	The Regents of the University of California.  All rights reserved.
      7       1.1     glass  *
      8       1.1     glass  * Redistribution and use in source and binary forms, with or without
      9       1.1     glass  * modification, are permitted provided that the following conditions
     10       1.1     glass  * are met:
     11       1.1     glass  * 1. Redistributions of source code must retain the above copyright
     12       1.1     glass  *    notice, this list of conditions and the following disclaimer.
     13       1.1     glass  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1     glass  *    notice, this list of conditions and the following disclaimer in the
     15       1.1     glass  *    documentation and/or other materials provided with the distribution.
     16       1.1     glass  * 3. All advertising materials mentioning features or use of this software
     17       1.1     glass  *    must display the following acknowledgement:
     18       1.1     glass  *	This product includes software developed by the University of
     19       1.1     glass  *	California, Berkeley and its contributors.
     20       1.1     glass  * 4. Neither the name of the University nor the names of its contributors
     21       1.1     glass  *    may be used to endorse or promote products derived from this software
     22       1.1     glass  *    without specific prior written permission.
     23       1.1     glass  *
     24       1.1     glass  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25       1.1     glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26       1.1     glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27       1.1     glass  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28       1.1     glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29       1.1     glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30       1.1     glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31       1.1     glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32       1.1     glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33       1.1     glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34       1.1     glass  * SUCH DAMAGE.
     35       1.1     glass  *
     36       1.1     glass  * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
     37       1.1     glass  *       @(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
     38       1.1     glass  *
     39       1.1     glass  */
     40  1.36.2.1   thorpej 
     41  1.36.2.1   thorpej #include <sys/cdefs.h>
     42  1.36.2.2  jdolecek __KERNEL_RCSID(0, "$NetBSD: if_arcsubr.c,v 1.36.2.2 2002/03/16 16:02:04 jdolecek Exp $");
     43  1.36.2.1   thorpej 
     44      1.17  jonathan #include "opt_inet.h"
     45       1.1     glass 
     46      1.32   thorpej #include "bpfilter.h"
     47      1.32   thorpej 
     48       1.1     glass #include <sys/param.h>
     49       1.1     glass #include <sys/systm.h>
     50       1.1     glass #include <sys/kernel.h>
     51       1.1     glass #include <sys/malloc.h>
     52       1.1     glass #include <sys/mbuf.h>
     53       1.6   mycroft #include <sys/protosw.h>
     54       1.1     glass #include <sys/socket.h>
     55       1.6   mycroft #include <sys/ioctl.h>
     56       1.6   mycroft #include <sys/errno.h>
     57       1.3    chopps #include <sys/syslog.h>
     58       1.1     glass 
     59       1.1     glass #include <machine/cpu.h>
     60       1.1     glass 
     61       1.1     glass #include <net/if.h>
     62       1.1     glass #include <net/netisr.h>
     63       1.1     glass #include <net/route.h>
     64       1.1     glass #include <net/if_dl.h>
     65       1.1     glass #include <net/if_types.h>
     66      1.13        is #include <net/if_arc.h>
     67      1.12        is #include <net/if_arp.h>
     68      1.12        is #include <net/if_ether.h>
     69       1.1     glass 
     70      1.32   thorpej #if NBPFILTER > 0
     71      1.32   thorpej #include <net/bpf.h>
     72      1.32   thorpej #endif
     73      1.32   thorpej 
     74       1.1     glass #ifdef INET
     75       1.1     glass #include <netinet/in.h>
     76       1.1     glass #include <netinet/in_var.h>
     77      1.14        is #include <netinet/if_inarp.h>
     78       1.1     glass #endif
     79       1.1     glass 
     80      1.27        is #ifdef INET6
     81      1.27        is #ifndef INET
     82      1.27        is #include <netinet/in.h>
     83      1.27        is #endif
     84      1.27        is #include <netinet6/in6_var.h>
     85      1.27        is #include <netinet6/nd6.h>
     86      1.27        is #endif
     87      1.27        is 
     88      1.12        is #define ARCNET_ALLOW_BROKEN_ARP
     89      1.12        is 
     90      1.28        is #ifndef ARC_IPMTU
     91      1.28        is #define ARC_IPMTU	1500
     92       1.4       cgd #endif
     93       1.4       cgd 
     94       1.4       cgd static struct mbuf *arc_defrag __P((struct ifnet *, struct mbuf *));
     95       1.4       cgd 
     96       1.4       cgd /*
     97       1.4       cgd  * RC1201 requires us to have this configurable. We have it only per
     98       1.4       cgd  * machine at the moment... there is no generic "set mtu" ioctl, AFAICS.
     99       1.4       cgd  * Anyway, it is possible to binpatch this or set it per kernel config
    100       1.4       cgd  * option.
    101       1.4       cgd  */
    102      1.28        is #if ARC_IPMTU > 60480
    103      1.28        is ERROR: The arc_ipmtu is ARC_IPMTU, but must not exceed 60480.
    104       1.4       cgd #endif
    105      1.28        is int arc_ipmtu = ARC_IPMTU;
    106       1.4       cgd u_int8_t  arcbroadcastaddr = 0;
    107       1.1     glass 
    108       1.1     glass #define senderr(e) { error = (e); goto bad;}
    109       1.1     glass #define SIN(s) ((struct sockaddr_in *)s)
    110       1.1     glass 
    111      1.21   thorpej static	int arc_output __P((struct ifnet *, struct mbuf *,
    112      1.21   thorpej 	    struct sockaddr *, struct rtentry *));
    113      1.21   thorpej static	void arc_input __P((struct ifnet *, struct mbuf *));
    114      1.21   thorpej 
    115       1.1     glass /*
    116       1.1     glass  * ARCnet output routine.
    117       1.1     glass  * Encapsulate a packet of type family for the local net.
    118       1.1     glass  * Assumes that ifp is actually pointer to arccom structure.
    119       1.1     glass  */
    120      1.21   thorpej static int
    121       1.1     glass arc_output(ifp, m0, dst, rt0)
    122      1.30  augustss 	struct ifnet *ifp;
    123       1.1     glass 	struct mbuf *m0;
    124       1.1     glass 	struct sockaddr *dst;
    125       1.1     glass 	struct rtentry *rt0;
    126       1.1     glass {
    127       1.4       cgd 	struct mbuf		*m, *m1, *mcopy;
    128       1.4       cgd 	struct rtentry		*rt;
    129       1.4       cgd 	struct arccom		*ac;
    130      1.14        is 	struct arc_header	*ah;
    131      1.14        is 	struct arphdr		*arph;
    132  1.36.2.2  jdolecek 	int			s, error, newencoding, len;
    133      1.12        is 	u_int8_t		atype, adst, myself;
    134       1.4       cgd 	int			tfrags, sflag, fsflag, rsflag;
    135  1.36.2.2  jdolecek 	ALTQ_DECL(struct altq_pktattr pktattr;)
    136       1.1     glass 
    137       1.6   mycroft 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    138       1.7        is 		return(ENETDOWN); /* m, m1 aren't initialized yet */
    139       1.4       cgd 
    140       1.4       cgd 	error = newencoding = 0;
    141       1.4       cgd 	ac = (struct arccom *)ifp;
    142       1.4       cgd 	m = m0;
    143       1.4       cgd 	mcopy = m1 = NULL;
    144       1.4       cgd 
    145      1.12        is 	myself = *LLADDR(ifp->if_sadl);
    146      1.12        is 
    147       1.6   mycroft 	if ((rt = rt0)) {
    148       1.1     glass 		if ((rt->rt_flags & RTF_UP) == 0) {
    149       1.6   mycroft 			if ((rt0 = rt = rtalloc1(dst, 1)))
    150       1.1     glass 				rt->rt_refcnt--;
    151       1.1     glass 			else
    152       1.1     glass 				senderr(EHOSTUNREACH);
    153       1.1     glass 		}
    154       1.1     glass 		if (rt->rt_flags & RTF_GATEWAY) {
    155       1.1     glass 			if (rt->rt_gwroute == 0)
    156       1.1     glass 				goto lookup;
    157       1.1     glass 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
    158       1.1     glass 				rtfree(rt); rt = rt0;
    159       1.1     glass 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
    160       1.1     glass 				if ((rt = rt->rt_gwroute) == 0)
    161       1.1     glass 					senderr(EHOSTUNREACH);
    162       1.1     glass 			}
    163       1.1     glass 		}
    164       1.1     glass 		if (rt->rt_flags & RTF_REJECT)
    165       1.1     glass 			if (rt->rt_rmx.rmx_expire == 0 ||
    166       1.1     glass 			    time.tv_sec < rt->rt_rmx.rmx_expire)
    167       1.1     glass 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
    168       1.1     glass 	}
    169       1.4       cgd 
    170  1.36.2.2  jdolecek 	/*
    171  1.36.2.2  jdolecek 	 * if the queueing discipline needs packet classification,
    172  1.36.2.2  jdolecek 	 * do it before prepending link headers.
    173  1.36.2.2  jdolecek 	 */
    174  1.36.2.2  jdolecek 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    175  1.36.2.2  jdolecek 
    176       1.1     glass 	switch (dst->sa_family) {
    177       1.1     glass #ifdef INET
    178       1.1     glass 	case AF_INET:
    179       1.1     glass 
    180       1.1     glass 		/*
    181       1.1     glass 		 * For now, use the simple IP addr -> ARCnet addr mapping
    182       1.1     glass 		 */
    183       1.9        is 		if (m->m_flags & (M_BCAST|M_MCAST))
    184       1.1     glass 			adst = arcbroadcastaddr; /* ARCnet broadcast address */
    185      1.12        is 		else if (ifp->if_flags & IFF_NOARP)
    186       1.1     glass 			adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
    187      1.12        is 		else if (!arpresolve(ifp, rt, m, dst, &adst))
    188      1.12        is 			return 0;	/* not resolved yet */
    189       1.1     glass 
    190       1.1     glass 		/* If broadcasting on a simplex interface, loopback a copy */
    191       1.9        is 		if ((m->m_flags & (M_BCAST|M_MCAST)) &&
    192       1.9        is 		    (ifp->if_flags & IFF_SIMPLEX))
    193       1.1     glass 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    194       1.4       cgd 		if (ifp->if_flags & IFF_LINK0) {
    195       1.4       cgd 			atype = ARCTYPE_IP;
    196       1.4       cgd 			newencoding = 1;
    197       1.4       cgd 		} else {
    198       1.4       cgd 			atype = ARCTYPE_IP_OLD;
    199       1.4       cgd 			newencoding = 0;
    200       1.4       cgd 		}
    201       1.1     glass 		break;
    202      1.12        is 
    203      1.12        is 	case AF_ARP:
    204      1.14        is 		arph = mtod(m, struct arphdr *);
    205      1.12        is 		if (m->m_flags & M_BCAST)
    206      1.12        is 			adst = arcbroadcastaddr;
    207      1.12        is 		else
    208      1.14        is 			adst = *ar_tha(arph);
    209      1.12        is 
    210      1.15        is 		arph->ar_hrd = htons(ARPHRD_ARCNET);
    211      1.15        is 
    212      1.14        is 		switch(ntohs(arph->ar_op)) {
    213      1.15        is 
    214      1.12        is 		case ARPOP_REVREQUEST:
    215      1.12        is 		case ARPOP_REVREPLY:
    216      1.12        is 			if (!(ifp->if_flags & IFF_LINK0)) {
    217      1.12        is 				printf("%s: can't handle af%d\n",
    218      1.12        is 				    ifp->if_xname, dst->sa_family);
    219      1.12        is 				senderr(EAFNOSUPPORT);
    220      1.12        is 			}
    221      1.12        is 
    222      1.12        is 			atype = htons(ARCTYPE_REVARP);
    223      1.12        is 			newencoding = 1;
    224      1.12        is 			break;
    225      1.12        is 
    226      1.12        is 		case ARPOP_REQUEST:
    227      1.12        is 		case ARPOP_REPLY:
    228      1.12        is 		default:
    229      1.12        is 			if (ifp->if_flags & IFF_LINK0) {
    230      1.12        is 				atype = htons(ARCTYPE_ARP);
    231      1.12        is 				newencoding = 1;
    232      1.12        is 			} else {
    233      1.12        is 				atype = htons(ARCTYPE_ARP_OLD);
    234      1.12        is 				newencoding = 0;
    235      1.12        is 			}
    236      1.12        is 		}
    237      1.12        is #ifdef ARCNET_ALLOW_BROKEN_ARP
    238      1.12        is 		/*
    239      1.12        is 		 * XXX It's not clear per RFC826 if this is needed, but
    240      1.12        is 		 * "assigned numbers" say this is wrong.
    241      1.12        is 		 * However, e.g., AmiTCP 3.0Beta used it... we make this
    242      1.12        is 		 * switchable for emergency cases. Not perfect, but...
    243      1.12        is 		 */
    244      1.15        is 		if (ifp->if_flags & IFF_LINK2)
    245      1.15        is 			arph->ar_pro = atype - 1;
    246      1.12        is #endif
    247      1.12        is 		break;
    248       1.1     glass #endif
    249      1.27        is #ifdef INET6
    250      1.27        is 	case AF_INET6:
    251      1.27        is 		if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)&adst))
    252      1.27        is 			return(0); /* it must be impossible, but... */
    253      1.27        is 		atype = htons(ARCTYPE_INET6);
    254      1.27        is 		newencoding = 1;
    255      1.27        is 		break;
    256      1.27        is #endif
    257       1.4       cgd 
    258       1.1     glass 	case AF_UNSPEC:
    259       1.1     glass 		ah = (struct arc_header *)dst->sa_data;
    260       1.1     glass  		adst = ah->arc_dhost;
    261       1.1     glass 		atype = ah->arc_type;
    262       1.1     glass 		break;
    263       1.1     glass 
    264       1.1     glass 	default:
    265      1.11  christos 		printf("%s: can't handle af%d\n", ifp->if_xname,
    266      1.10  christos 		    dst->sa_family);
    267       1.1     glass 		senderr(EAFNOSUPPORT);
    268       1.1     glass 	}
    269       1.1     glass 
    270       1.1     glass 	if (mcopy)
    271       1.1     glass 		(void) looutput(ifp, mcopy, dst, rt);
    272       1.4       cgd 
    273       1.1     glass 	/*
    274       1.1     glass 	 * Add local net header.  If no space in first mbuf,
    275       1.1     glass 	 * allocate another.
    276       1.1     glass 	 *
    277       1.1     glass 	 * For ARCnet, this is just symbolic. The header changes
    278       1.1     glass 	 * form and position on its way into the hardware and out of
    279       1.1     glass 	 * the wire.  At this point, it contains source, destination and
    280       1.1     glass 	 * packet type.
    281       1.1     glass 	 */
    282       1.4       cgd 	if (newencoding) {
    283       1.4       cgd 		++ac->ac_seqid; /* make the seqid unique */
    284       1.4       cgd 
    285       1.6   mycroft 		tfrags = (m->m_pkthdr.len + 503) / 504;
    286       1.6   mycroft 		fsflag = 2 * tfrags - 3;
    287       1.4       cgd 		sflag = 0;
    288       1.4       cgd 		rsflag = fsflag;
    289       1.4       cgd 
    290       1.4       cgd 		while (sflag < fsflag) {
    291       1.4       cgd 			/* we CAN'T have short packets here */
    292       1.4       cgd 			m1 = m_split(m, 504, M_DONTWAIT);
    293       1.4       cgd 			if (m1 == 0)
    294       1.4       cgd 				senderr(ENOBUFS);
    295       1.4       cgd 
    296       1.4       cgd 			M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
    297       1.4       cgd 			if (m == 0)
    298       1.4       cgd 				senderr(ENOBUFS);
    299       1.4       cgd 			ah = mtod(m, struct arc_header *);
    300       1.4       cgd 			ah->arc_type = atype;
    301       1.4       cgd 			ah->arc_dhost = adst;
    302      1.12        is 			ah->arc_shost = myself;
    303       1.4       cgd 			ah->arc_flag = rsflag;
    304       1.4       cgd 			ah->arc_seqid = ac->ac_seqid;
    305       1.4       cgd 
    306  1.36.2.2  jdolecek 			len = m->m_pkthdr.len;
    307      1.35   thorpej 			s = splnet();
    308       1.4       cgd 			/*
    309       1.4       cgd 			 * Queue message on interface, and start output if
    310       1.4       cgd 			 * interface not yet active.
    311       1.4       cgd 			 */
    312  1.36.2.2  jdolecek 			IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    313  1.36.2.2  jdolecek 			if (error) {
    314  1.36.2.2  jdolecek 				/* mbuf is already freed */
    315       1.4       cgd 				splx(s);
    316  1.36.2.2  jdolecek 				return (error);
    317       1.4       cgd 			}
    318  1.36.2.2  jdolecek 			ifp->if_obytes += len;
    319       1.4       cgd 			if ((ifp->if_flags & IFF_OACTIVE) == 0)
    320       1.4       cgd 				(*ifp->if_start)(ifp);
    321       1.4       cgd 			splx(s);
    322       1.4       cgd 
    323       1.4       cgd 			m = m1;
    324       1.4       cgd 			sflag += 2;
    325       1.4       cgd 			rsflag = sflag;
    326       1.4       cgd 		}
    327       1.4       cgd 		m1 = NULL;
    328       1.4       cgd 
    329       1.4       cgd 
    330       1.4       cgd 		/* here we can have small, especially forbidden packets */
    331       1.4       cgd 
    332      1.18        is 		if ((m->m_pkthdr.len >=
    333      1.18        is 		    ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
    334      1.18        is 		    (m->m_pkthdr.len <=
    335      1.18        is 		    ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
    336      1.18        is 
    337      1.20        is 			M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
    338       1.4       cgd 			if (m == 0)
    339       1.4       cgd 				senderr(ENOBUFS);
    340      1.18        is 			ah = mtod(m, struct arc_header *);
    341      1.18        is 			ah->arc_flag = 0xFF;
    342      1.18        is 			ah->arc_seqid = 0xFFFF;
    343      1.18        is 			ah->arc_type2 = atype;
    344      1.18        is 			ah->arc_flag2 = sflag;
    345      1.18        is 			ah->arc_seqid2 = ac->ac_seqid;
    346      1.18        is 		} else {
    347      1.18        is 			M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
    348       1.4       cgd 			if (m == 0)
    349       1.4       cgd 				senderr(ENOBUFS);
    350       1.4       cgd 			ah = mtod(m, struct arc_header *);
    351      1.18        is 			ah->arc_flag = sflag;
    352      1.18        is 			ah->arc_seqid = ac->ac_seqid;
    353       1.4       cgd 		}
    354       1.4       cgd 
    355       1.6   mycroft 		ah->arc_dhost = adst;
    356      1.12        is 		ah->arc_shost = myself;
    357      1.18        is 		ah->arc_type = atype;
    358       1.4       cgd 	} else {
    359       1.4       cgd 		M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
    360       1.4       cgd 		if (m == 0)
    361       1.4       cgd 			senderr(ENOBUFS);
    362       1.4       cgd 		ah = mtod(m, struct arc_header *);
    363       1.4       cgd 		ah->arc_type = atype;
    364       1.6   mycroft 		ah->arc_dhost = adst;
    365      1.12        is 		ah->arc_shost = myself;
    366       1.6   mycroft 	}
    367  1.36.2.2  jdolecek 
    368  1.36.2.2  jdolecek 	len = m->m_pkthdr.len;
    369      1.35   thorpej 	s = splnet();
    370       1.6   mycroft 	/*
    371       1.6   mycroft 	 * Queue message on interface, and start output if interface
    372       1.6   mycroft 	 * not yet active.
    373       1.6   mycroft 	 */
    374  1.36.2.2  jdolecek 	IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    375  1.36.2.2  jdolecek 	if (error) {
    376  1.36.2.2  jdolecek 		/* mbuf is already freed */
    377       1.1     glass 		splx(s);
    378  1.36.2.2  jdolecek 		return (error);
    379       1.1     glass 	}
    380  1.36.2.2  jdolecek 	ifp->if_obytes += len;
    381       1.6   mycroft 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    382       1.6   mycroft 		(*ifp->if_start)(ifp);
    383       1.6   mycroft 	splx(s);
    384       1.6   mycroft 
    385       1.1     glass 	return (error);
    386       1.1     glass 
    387       1.1     glass bad:
    388       1.4       cgd 	if (m1)
    389       1.4       cgd 		m_freem(m1);
    390       1.1     glass 	if (m)
    391       1.1     glass 		m_freem(m);
    392       1.1     glass 	return (error);
    393       1.1     glass }
    394       1.1     glass 
    395       1.1     glass /*
    396       1.4       cgd  * Defragmenter. Returns mbuf if last packet found, else
    397       1.4       cgd  * NULL. frees imcoming mbuf as necessary.
    398       1.4       cgd  */
    399       1.4       cgd 
    400       1.4       cgd __inline struct mbuf *
    401       1.4       cgd arc_defrag(ifp, m)
    402       1.4       cgd 	struct ifnet *ifp;
    403       1.4       cgd 	struct mbuf *m;
    404       1.4       cgd {
    405       1.4       cgd 	struct arc_header *ah, *ah1;
    406       1.4       cgd 	struct arccom *ac;
    407       1.4       cgd 	struct ac_frag *af;
    408       1.4       cgd 	struct mbuf *m1;
    409       1.4       cgd 	char *s;
    410       1.4       cgd 	int newflen;
    411       1.4       cgd 	u_char src,dst,typ;
    412       1.4       cgd 
    413       1.4       cgd 	ac = (struct arccom *)ifp;
    414       1.4       cgd 
    415      1.27        is 	if (m->m_len < ARC_HDRNEWLEN) {
    416      1.27        is 		m = m_pullup(m, ARC_HDRNEWLEN);
    417      1.27        is 		if (m == NULL) {
    418      1.27        is 			++ifp->if_ierrors;
    419      1.27        is 			return NULL;
    420      1.27        is 		}
    421       1.4       cgd 	}
    422       1.4       cgd 
    423       1.4       cgd 	ah = mtod(m, struct arc_header *);
    424       1.4       cgd 	typ = ah->arc_type;
    425       1.4       cgd 
    426       1.4       cgd 	if (!arc_isphds(typ))
    427       1.4       cgd 		return m;
    428       1.4       cgd 
    429       1.4       cgd 	src = ah->arc_shost;
    430       1.4       cgd 	dst = ah->arc_dhost;
    431       1.4       cgd 
    432       1.4       cgd 	if (ah->arc_flag == 0xff) {
    433       1.4       cgd 		m_adj(m, 4);
    434       1.4       cgd 
    435      1.27        is 		if (m->m_len < ARC_HDRNEWLEN) {
    436      1.27        is 			m = m_pullup(m, ARC_HDRNEWLEN);
    437      1.27        is 			if (m == NULL) {
    438      1.27        is 				++ifp->if_ierrors;
    439      1.27        is 				return NULL;
    440      1.27        is 			}
    441       1.4       cgd 		}
    442       1.4       cgd 
    443       1.4       cgd 		ah = mtod(m, struct arc_header *);
    444       1.4       cgd 	}
    445       1.4       cgd 
    446       1.4       cgd 	af = &ac->ac_fragtab[src];
    447       1.4       cgd 	m1 = af->af_packet;
    448       1.4       cgd 	s = "debug code error";
    449       1.4       cgd 
    450       1.4       cgd 	if (ah->arc_flag & 1) {
    451       1.4       cgd 		/*
    452       1.4       cgd 		 * first fragment. We always initialize, which is
    453       1.4       cgd 		 * about the right thing to do, as we only want to
    454       1.4       cgd 		 * accept one fragmented packet per src at a time.
    455       1.4       cgd 		 */
    456       1.4       cgd 		if (m1 != NULL)
    457       1.4       cgd 			m_freem(m1);
    458       1.4       cgd 
    459       1.4       cgd 		af->af_packet = m;
    460       1.4       cgd 		m1 = m;
    461       1.4       cgd 		af->af_maxflag = ah->arc_flag;
    462       1.4       cgd 		af->af_lastseen = 0;
    463       1.4       cgd 		af->af_seqid = ah->arc_seqid;
    464       1.4       cgd 
    465       1.4       cgd 		return NULL;
    466       1.4       cgd 		/* notreached */
    467       1.4       cgd 	} else {
    468       1.4       cgd 		/* check for unfragmented packet */
    469       1.4       cgd 		if (ah->arc_flag == 0)
    470       1.4       cgd 			return m;
    471       1.4       cgd 
    472       1.4       cgd 		/* do we have a first packet from that src? */
    473       1.4       cgd 		if (m1 == NULL) {
    474       1.4       cgd 			s = "no first frag";
    475       1.4       cgd 			goto outofseq;
    476       1.4       cgd 		}
    477       1.4       cgd 
    478       1.4       cgd 		ah1 = mtod(m1, struct arc_header *);
    479       1.4       cgd 
    480       1.4       cgd 		if (ah->arc_seqid != ah1->arc_seqid) {
    481       1.4       cgd 			s = "seqid differs";
    482       1.4       cgd 			goto outofseq;
    483       1.4       cgd 		}
    484       1.4       cgd 
    485      1.19        is 		if (typ != ah1->arc_type) {
    486       1.4       cgd 			s = "type differs";
    487       1.4       cgd 			goto outofseq;
    488       1.4       cgd 		}
    489       1.4       cgd 
    490      1.19        is 		if (dst != ah1->arc_dhost) {
    491       1.4       cgd 			s = "dest host differs";
    492       1.4       cgd 			goto outofseq;
    493       1.4       cgd 		}
    494       1.4       cgd 
    495       1.4       cgd 		/* typ, seqid and dst are ok here. */
    496       1.4       cgd 
    497       1.4       cgd 		if (ah->arc_flag == af->af_lastseen) {
    498       1.4       cgd 			m_freem(m);
    499       1.4       cgd 			return NULL;
    500       1.4       cgd 		}
    501       1.4       cgd 
    502       1.4       cgd 		if (ah->arc_flag == af->af_lastseen + 2) {
    503       1.4       cgd 			/* ok, this is next fragment */
    504       1.4       cgd 			af->af_lastseen = ah->arc_flag;
    505       1.4       cgd 			m_adj(m,ARC_HDRNEWLEN);
    506       1.4       cgd 
    507       1.4       cgd 			/*
    508       1.4       cgd 			 * m_cat might free the first mbuf (with pkthdr)
    509       1.4       cgd 			 * in 2nd chain; therefore:
    510       1.4       cgd 			 */
    511       1.4       cgd 
    512       1.4       cgd 			newflen = m->m_pkthdr.len;
    513       1.4       cgd 
    514       1.4       cgd 			m_cat(m1,m);
    515       1.4       cgd 
    516       1.4       cgd 			m1->m_pkthdr.len += newflen;
    517       1.4       cgd 
    518       1.4       cgd 			/* is it the last one? */
    519       1.4       cgd 			if (af->af_lastseen > af->af_maxflag) {
    520       1.4       cgd 				af->af_packet = NULL;
    521       1.4       cgd 				return(m1);
    522       1.4       cgd 			} else
    523       1.4       cgd 				return NULL;
    524       1.4       cgd 		}
    525       1.4       cgd 		s = "other reason";
    526       1.4       cgd 		/* if all else fails, it is out of sequence, too */
    527       1.4       cgd 	}
    528       1.4       cgd outofseq:
    529       1.4       cgd 	if (m1) {
    530       1.4       cgd 		m_freem(m1);
    531       1.4       cgd 		af->af_packet = NULL;
    532       1.4       cgd 	}
    533       1.4       cgd 
    534       1.4       cgd 	if (m)
    535       1.4       cgd 		m_freem(m);
    536       1.4       cgd 
    537       1.8   thorpej 	log(LOG_INFO,"%s: got out of seq. packet: %s\n",
    538       1.8   thorpej 	    ifp->if_xname, s);
    539       1.4       cgd 
    540       1.4       cgd 	return NULL;
    541       1.4       cgd }
    542       1.4       cgd 
    543       1.4       cgd /*
    544       1.4       cgd  * return 1 if Packet Header Definition Standard, else 0.
    545       1.4       cgd  * For now: old IP, old ARP aren't obviously. Lacking correct information,
    546       1.4       cgd  * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
    547       1.4       cgd  * (Apple and Novell corporations were involved, among others, in PHDS work).
    548       1.4       cgd  * Easiest is to assume that everybody else uses that, too.
    549       1.4       cgd  */
    550       1.4       cgd int
    551       1.4       cgd arc_isphds(type)
    552       1.6   mycroft 	u_int8_t type;
    553       1.4       cgd {
    554      1.27        is 	return (type != ARCTYPE_IP_OLD &&
    555      1.27        is 		type != ARCTYPE_ARP_OLD &&
    556      1.27        is 		type != ARCTYPE_DIAGNOSE);
    557       1.4       cgd }
    558       1.4       cgd 
    559       1.4       cgd /*
    560       1.1     glass  * Process a received Arcnet packet;
    561       1.3    chopps  * the packet is in the mbuf chain m with
    562       1.3    chopps  * the ARCnet header.
    563       1.1     glass  */
    564      1.21   thorpej static void
    565       1.3    chopps arc_input(ifp, m)
    566       1.1     glass 	struct ifnet *ifp;
    567       1.1     glass 	struct mbuf *m;
    568       1.1     glass {
    569      1.30  augustss 	struct arc_header *ah;
    570      1.30  augustss 	struct ifqueue *inq;
    571       1.4       cgd 	u_int8_t atype;
    572       1.1     glass 	int s;
    573      1.15        is 	struct arphdr *arph;
    574       1.1     glass 
    575       1.1     glass 	if ((ifp->if_flags & IFF_UP) == 0) {
    576       1.1     glass 		m_freem(m);
    577       1.1     glass 		return;
    578       1.1     glass 	}
    579       1.4       cgd 
    580       1.4       cgd 	/* possibly defragment: */
    581       1.4       cgd 	m = arc_defrag(ifp, m);
    582       1.4       cgd 	if (m == NULL)
    583       1.4       cgd 		return;
    584       1.4       cgd 
    585       1.4       cgd 	ah = mtod(m, struct arc_header *);
    586       1.3    chopps 
    587       1.3    chopps 	ifp->if_ibytes += m->m_pkthdr.len;
    588       1.1     glass 
    589       1.1     glass 	if (arcbroadcastaddr == ah->arc_dhost) {
    590       1.9        is 		m->m_flags |= M_BCAST|M_MCAST;
    591       1.1     glass 		ifp->if_imcasts++;
    592       1.1     glass 	}
    593       1.1     glass 
    594       1.1     glass 	atype = ah->arc_type;
    595       1.1     glass 	switch (atype) {
    596       1.1     glass #ifdef INET
    597       1.4       cgd 	case ARCTYPE_IP:
    598       1.6   mycroft 		m_adj(m, ARC_HDRNEWLEN);
    599       1.4       cgd 		schednetisr(NETISR_IP);
    600       1.4       cgd 		inq = &ipintrq;
    601       1.4       cgd 		break;
    602       1.4       cgd 
    603       1.1     glass 	case ARCTYPE_IP_OLD:
    604       1.6   mycroft 		m_adj(m, ARC_HDRLEN);
    605       1.1     glass 		schednetisr(NETISR_IP);
    606       1.1     glass 		inq = &ipintrq;
    607       1.1     glass 		break;
    608      1.12        is 
    609      1.12        is 	case ARCTYPE_ARP:
    610      1.12        is 		m_adj(m, ARC_HDRNEWLEN);
    611      1.12        is 		schednetisr(NETISR_ARP);
    612      1.12        is 		inq = &arpintrq;
    613      1.12        is #ifdef ARCNET_ALLOW_BROKEN_ARP
    614      1.15        is 		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
    615      1.12        is #endif
    616      1.12        is 		break;
    617      1.12        is 
    618      1.12        is 	case ARCTYPE_ARP_OLD:
    619      1.12        is 		m_adj(m, ARC_HDRLEN);
    620      1.12        is 		schednetisr(NETISR_ARP);
    621      1.12        is 		inq = &arpintrq;
    622      1.15        is 		arph = mtod(m, struct arphdr *);
    623      1.12        is #ifdef ARCNET_ALLOW_BROKEN_ARP
    624      1.15        is 		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
    625      1.12        is #endif
    626      1.12        is 		break;
    627       1.1     glass #endif
    628      1.27        is #ifdef INET6
    629      1.27        is 	case ARCTYPE_INET6:
    630      1.27        is 		m_adj(m, ARC_HDRNEWLEN);
    631      1.27        is 		schednetisr(NETISR_IPV6);
    632      1.27        is 		inq = &ip6intrq;
    633      1.27        is 		break;
    634      1.27        is #endif
    635       1.1     glass 	default:
    636       1.1     glass 		m_freem(m);
    637       1.1     glass 		return;
    638       1.1     glass 	}
    639       1.1     glass 
    640      1.35   thorpej 	s = splnet();
    641       1.1     glass 	if (IF_QFULL(inq)) {
    642       1.1     glass 		IF_DROP(inq);
    643       1.1     glass 		m_freem(m);
    644       1.1     glass 	} else
    645       1.1     glass 		IF_ENQUEUE(inq, m);
    646       1.1     glass 	splx(s);
    647       1.1     glass }
    648       1.1     glass 
    649       1.1     glass /*
    650       1.1     glass  * Convert Arcnet address to printable (loggable) representation.
    651       1.1     glass  */
    652       1.1     glass static char digits[] = "0123456789abcdef";
    653       1.1     glass char *
    654       1.1     glass arc_sprintf(ap)
    655      1.30  augustss 	u_int8_t *ap;
    656       1.1     glass {
    657       1.1     glass 	static char arcbuf[3];
    658      1.30  augustss 	char *cp = arcbuf;
    659       1.1     glass 
    660       1.1     glass 	*cp++ = digits[*ap >> 4];
    661       1.1     glass 	*cp++ = digits[*ap++ & 0xf];
    662       1.1     glass 	*cp   = 0;
    663       1.1     glass 	return (arcbuf);
    664       1.1     glass }
    665       1.1     glass 
    666       1.1     glass /*
    667      1.24        is  * Register (new) link level address.
    668      1.24        is  */
    669      1.24        is void
    670      1.24        is arc_storelladdr(ifp, lla)
    671      1.24        is 	struct ifnet *ifp;
    672      1.24        is 	u_int8_t lla;
    673      1.24        is {
    674      1.34   thorpej 
    675      1.34   thorpej 	*(LLADDR(ifp->if_sadl)) = lla;
    676      1.28        is 	ifp->if_mtu = ARC_PHDS_MAXMTU;
    677      1.24        is }
    678      1.24        is 
    679      1.24        is /*
    680       1.1     glass  * Perform common duties while attaching to interface list
    681       1.1     glass  */
    682       1.1     glass void
    683      1.12        is arc_ifattach(ifp, lla)
    684      1.30  augustss 	struct ifnet *ifp;
    685      1.12        is 	u_int8_t lla;
    686       1.1     glass {
    687      1.30  augustss 	struct arccom *ac;
    688       1.1     glass 
    689       1.1     glass 	ifp->if_type = IFT_ARCNET;
    690       1.1     glass 	ifp->if_addrlen = 1;
    691       1.1     glass 	ifp->if_hdrlen = ARC_HDRLEN;
    692      1.33   thorpej 	ifp->if_dlt = DLT_ARCNET;
    693       1.9        is 	if (ifp->if_flags & IFF_BROADCAST)
    694       1.9        is 		ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
    695      1.28        is 	if (ifp->if_flags & IFF_LINK0 && arc_ipmtu > ARC_PHDS_MAXMTU)
    696       1.4       cgd 		log(LOG_ERR,
    697      1.28        is 		    "%s: arc_ipmtu is %d, but must not exceed %d",
    698      1.28        is 		    ifp->if_xname, arc_ipmtu, ARC_PHDS_MAXMTU);
    699       1.4       cgd 
    700      1.21   thorpej 	ifp->if_output = arc_output;
    701      1.21   thorpej 	ifp->if_input = arc_input;
    702       1.4       cgd 	ac = (struct arccom *)ifp;
    703       1.4       cgd 	ac->ac_seqid = (time.tv_sec) & 0xFFFF; /* try to make seqid unique */
    704      1.12        is 	if (lla == 0) {
    705       1.4       cgd 		/* XXX this message isn't entirely clear, to me -- cgd */
    706       1.8   thorpej 		log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
    707       1.8   thorpej 		   ifp->if_xname, ifp->if_xname);
    708       1.3    chopps 	}
    709      1.23        is 	if_attach(ifp);
    710      1.34   thorpej 	if_alloc_sadl(ifp);
    711      1.24        is 	arc_storelladdr(ifp, lla);
    712      1.24        is 
    713      1.16        is 	ifp->if_broadcastaddr = &arcbroadcastaddr;
    714      1.32   thorpej 
    715      1.32   thorpej #if NBPFILTER > 0
    716      1.32   thorpej 	bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
    717      1.32   thorpej #endif
    718       1.1     glass }
    719