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