Home | History | Annotate | Line # | Download | only in net
if_arcsubr.c revision 1.87
      1  1.87  christos /*	$NetBSD: if_arcsubr.c,v 1.87 2025/09/21 15:11:52 christos 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.87  christos __KERNEL_RCSID(0, "$NetBSD: if_arcsubr.c,v 1.87 2025/09/21 15:11:52 christos Exp $");
     39  1.38     lukem 
     40  1.68     pooka #ifdef _KERNEL_OPT
     41  1.17  jonathan #include "opt_inet.h"
     42  1.68     pooka #endif
     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/ioctl.h>
     50   1.6   mycroft #include <sys/errno.h>
     51   1.3    chopps #include <sys/syslog.h>
     52   1.1     glass 
     53  1.57        ad #include <sys/cpu.h>
     54   1.1     glass 
     55   1.1     glass #include <net/if.h>
     56   1.1     glass #include <net/route.h>
     57   1.1     glass #include <net/if_dl.h>
     58   1.1     glass #include <net/if_types.h>
     59  1.13        is #include <net/if_arc.h>
     60  1.12        is #include <net/if_arp.h>
     61  1.12        is #include <net/if_ether.h>
     62   1.1     glass 
     63  1.32   thorpej #include <net/bpf.h>
     64  1.32   thorpej 
     65   1.1     glass #ifdef INET
     66   1.1     glass #include <netinet/in.h>
     67   1.1     glass #include <netinet/in_var.h>
     68  1.14        is #include <netinet/if_inarp.h>
     69   1.1     glass #endif
     70   1.1     glass 
     71  1.27        is #ifdef INET6
     72  1.27        is #ifndef INET
     73  1.27        is #include <netinet/in.h>
     74  1.27        is #endif
     75  1.27        is #include <netinet6/in6_var.h>
     76  1.27        is #include <netinet6/nd6.h>
     77  1.27        is #endif
     78  1.27        is 
     79  1.12        is #define ARCNET_ALLOW_BROKEN_ARP
     80  1.12        is 
     81  1.28        is #ifndef ARC_IPMTU
     82  1.28        is #define ARC_IPMTU	1500
     83   1.4       cgd #endif
     84   1.4       cgd 
     85  1.51   thorpej static struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
     86   1.4       cgd 
     87   1.4       cgd /*
     88  1.46     perry  * RC1201 requires us to have this configurable. We have it only per
     89   1.4       cgd  * machine at the moment... there is no generic "set mtu" ioctl, AFAICS.
     90   1.4       cgd  * Anyway, it is possible to binpatch this or set it per kernel config
     91   1.4       cgd  * option.
     92   1.4       cgd  */
     93  1.28        is #if ARC_IPMTU > 60480
     94  1.28        is ERROR: The arc_ipmtu is ARC_IPMTU, but must not exceed 60480.
     95   1.4       cgd #endif
     96  1.28        is int arc_ipmtu = ARC_IPMTU;
     97  1.59      matt uint8_t  arcbroadcastaddr = 0;
     98   1.1     glass 
     99   1.1     glass #define senderr(e) { error = (e); goto bad;}
    100   1.1     glass 
    101  1.51   thorpej static	int arc_output(struct ifnet *, struct mbuf *,
    102  1.73     ozaki 	    const struct sockaddr *, const struct rtentry *);
    103  1.51   thorpej static	void arc_input(struct ifnet *, struct mbuf *);
    104  1.21   thorpej 
    105   1.1     glass /*
    106   1.1     glass  * ARCnet output routine.
    107   1.1     glass  * Encapsulate a packet of type family for the local net.
    108   1.1     glass  * Assumes that ifp is actually pointer to arccom structure.
    109   1.1     glass  */
    110  1.21   thorpej static int
    111  1.54    dyoung arc_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
    112  1.73     ozaki     const struct rtentry *rt)
    113   1.1     glass {
    114   1.4       cgd 	struct mbuf		*m, *m1, *mcopy;
    115   1.4       cgd 	struct arccom		*ac;
    116  1.54    dyoung 	const struct arc_header	*cah;
    117  1.14        is 	struct arc_header	*ah;
    118  1.14        is 	struct arphdr		*arph;
    119  1.47  christos 	int			error, newencoding;
    120  1.59      matt 	uint8_t			atype, adst, myself;
    121   1.4       cgd 	int			tfrags, sflag, fsflag, rsflag;
    122   1.1     glass 
    123  1.46     perry 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    124  1.40    itojun 		return (ENETDOWN); /* m, m1 aren't initialized yet */
    125   1.4       cgd 
    126   1.4       cgd 	error = newencoding = 0;
    127   1.4       cgd 	ac = (struct arccom *)ifp;
    128   1.4       cgd 	m = m0;
    129   1.4       cgd 	mcopy = m1 = NULL;
    130   1.4       cgd 
    131  1.56    dyoung 	myself = *CLLADDR(ifp->if_sadl);
    132  1.12        is 
    133  1.39    itojun 	/*
    134  1.39    itojun 	 * if the queueing discipline needs packet classification,
    135  1.39    itojun 	 * do it before prepending link headers.
    136  1.39    itojun 	 */
    137  1.72  knakahar 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    138  1.39    itojun 
    139   1.1     glass 	switch (dst->sa_family) {
    140   1.1     glass #ifdef INET
    141   1.1     glass 	case AF_INET:
    142   1.1     glass 
    143   1.1     glass 		/*
    144   1.1     glass 		 * For now, use the simple IP addr -> ARCnet addr mapping
    145   1.1     glass 		 */
    146  1.46     perry 		if (m->m_flags & (M_BCAST|M_MCAST))
    147   1.1     glass 			adst = arcbroadcastaddr; /* ARCnet broadcast address */
    148  1.12        is 		else if (ifp->if_flags & IFF_NOARP)
    149  1.54    dyoung 			adst = ntohl(satocsin(dst)->sin_addr.s_addr) & 0xFF;
    150  1.71  christos 		else if ((error = arpresolve(ifp, rt, m, dst, &adst,
    151  1.71  christos 		    sizeof(adst))) != 0)
    152  1.69       roy 			return error == EWOULDBLOCK ? 0 : error;
    153   1.1     glass 
    154   1.1     glass 		/* If broadcasting on a simplex interface, loopback a copy */
    155  1.46     perry 		if ((m->m_flags & (M_BCAST|M_MCAST)) &&
    156   1.9        is 		    (ifp->if_flags & IFF_SIMPLEX))
    157  1.80      maxv 			mcopy = m_copypacket(m, M_DONTWAIT);
    158   1.4       cgd 		if (ifp->if_flags & IFF_LINK0) {
    159   1.4       cgd 			atype = ARCTYPE_IP;
    160   1.4       cgd 			newencoding = 1;
    161   1.4       cgd 		} else {
    162   1.4       cgd 			atype = ARCTYPE_IP_OLD;
    163   1.4       cgd 			newencoding = 0;
    164   1.4       cgd 		}
    165   1.1     glass 		break;
    166  1.12        is 
    167  1.12        is 	case AF_ARP:
    168  1.14        is 		arph = mtod(m, struct arphdr *);
    169  1.12        is 		if (m->m_flags & M_BCAST)
    170  1.12        is 			adst = arcbroadcastaddr;
    171  1.61  christos 		else {
    172  1.61  christos 			uint8_t *tha = ar_tha(arph);
    173  1.76      maxv 			if (tha == NULL) {
    174  1.76      maxv 				m_freem(m);
    175  1.61  christos 				return 0;
    176  1.76      maxv 			}
    177  1.61  christos 			adst = *tha;
    178  1.61  christos 		}
    179  1.12        is 
    180  1.15        is 		arph->ar_hrd = htons(ARPHRD_ARCNET);
    181  1.15        is 
    182  1.42    itojun 		switch (ntohs(arph->ar_op)) {
    183  1.12        is 		case ARPOP_REVREQUEST:
    184  1.12        is 		case ARPOP_REVREPLY:
    185  1.12        is 			if (!(ifp->if_flags & IFF_LINK0)) {
    186  1.87  christos 				rt_unhandled(__func__, ifp, dst);
    187  1.12        is 				senderr(EAFNOSUPPORT);
    188  1.12        is 			}
    189  1.12        is 
    190  1.12        is 			atype = htons(ARCTYPE_REVARP);
    191  1.12        is 			newencoding = 1;
    192  1.12        is 			break;
    193  1.12        is 
    194  1.12        is 		case ARPOP_REQUEST:
    195  1.12        is 		case ARPOP_REPLY:
    196  1.12        is 		default:
    197  1.12        is 			if (ifp->if_flags & IFF_LINK0) {
    198  1.12        is 				atype = htons(ARCTYPE_ARP);
    199  1.12        is 				newencoding = 1;
    200  1.12        is 			} else {
    201  1.12        is 				atype = htons(ARCTYPE_ARP_OLD);
    202  1.12        is 				newencoding = 0;
    203  1.12        is 			}
    204  1.12        is 		}
    205  1.12        is #ifdef ARCNET_ALLOW_BROKEN_ARP
    206  1.12        is 		/*
    207  1.12        is 		 * XXX It's not clear per RFC826 if this is needed, but
    208  1.12        is 		 * "assigned numbers" say this is wrong.
    209  1.12        is 		 * However, e.g., AmiTCP 3.0Beta used it... we make this
    210  1.12        is 		 * switchable for emergency cases. Not perfect, but...
    211  1.12        is 		 */
    212  1.15        is 		if (ifp->if_flags & IFF_LINK2)
    213  1.15        is 			arph->ar_pro = atype - 1;
    214  1.12        is #endif
    215  1.12        is 		break;
    216   1.1     glass #endif
    217  1.27        is #ifdef INET6
    218  1.27        is 	case AF_INET6:
    219  1.77     ozaki 		if (m->m_flags & M_MCAST) {
    220  1.77     ozaki 			adst = 0;
    221  1.77     ozaki 		} else {
    222  1.77     ozaki 			error = nd6_resolve(ifp, rt, m, dst, &adst,
    223  1.77     ozaki 			    sizeof(adst));
    224  1.77     ozaki 			if (error != 0)
    225  1.77     ozaki 				return error == EWOULDBLOCK ? 0 : error;
    226  1.77     ozaki 		}
    227  1.27        is 		atype = htons(ARCTYPE_INET6);
    228  1.27        is 		newencoding = 1;
    229  1.27        is 		break;
    230  1.27        is #endif
    231   1.4       cgd 
    232   1.1     glass 	case AF_UNSPEC:
    233  1.54    dyoung 		cah = (const struct arc_header *)dst->sa_data;
    234  1.54    dyoung  		adst = cah->arc_dhost;
    235  1.54    dyoung 		atype = cah->arc_type;
    236   1.1     glass 		break;
    237   1.1     glass 
    238   1.1     glass 	default:
    239  1.87  christos 		rt_unhandled(__func__, ifp, dst);
    240   1.1     glass 		senderr(EAFNOSUPPORT);
    241   1.1     glass 	}
    242   1.1     glass 
    243   1.1     glass 	if (mcopy)
    244   1.1     glass 		(void) looutput(ifp, mcopy, dst, rt);
    245   1.4       cgd 
    246   1.1     glass 	/*
    247   1.1     glass 	 * Add local net header.  If no space in first mbuf,
    248   1.1     glass 	 * allocate another.
    249  1.46     perry 	 *
    250   1.1     glass 	 * For ARCnet, this is just symbolic. The header changes
    251   1.1     glass 	 * form and position on its way into the hardware and out of
    252   1.1     glass 	 * the wire.  At this point, it contains source, destination and
    253   1.1     glass 	 * packet type.
    254   1.1     glass 	 */
    255   1.4       cgd 	if (newencoding) {
    256   1.4       cgd 		++ac->ac_seqid; /* make the seqid unique */
    257   1.4       cgd 
    258   1.6   mycroft 		tfrags = (m->m_pkthdr.len + 503) / 504;
    259   1.6   mycroft 		fsflag = 2 * tfrags - 3;
    260   1.4       cgd 		sflag = 0;
    261   1.4       cgd 		rsflag = fsflag;
    262   1.4       cgd 
    263   1.4       cgd 		while (sflag < fsflag) {
    264   1.4       cgd 			/* we CAN'T have short packets here */
    265   1.4       cgd 			m1 = m_split(m, 504, M_DONTWAIT);
    266   1.4       cgd 			if (m1 == 0)
    267   1.4       cgd 				senderr(ENOBUFS);
    268   1.4       cgd 
    269   1.4       cgd 			M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
    270   1.4       cgd 			if (m == 0)
    271   1.4       cgd 				senderr(ENOBUFS);
    272   1.4       cgd 			ah = mtod(m, struct arc_header *);
    273   1.4       cgd 			ah->arc_type = atype;
    274   1.4       cgd 			ah->arc_dhost = adst;
    275  1.12        is 			ah->arc_shost = myself;
    276   1.4       cgd 			ah->arc_flag = rsflag;
    277   1.4       cgd 			ah->arc_seqid = ac->ac_seqid;
    278   1.4       cgd 
    279  1.72  knakahar 			if ((error = ifq_enqueue(ifp, m)) != 0)
    280  1.39    itojun 				return (error);
    281  1.46     perry 
    282   1.4       cgd 			m = m1;
    283   1.4       cgd 			sflag += 2;
    284   1.4       cgd 			rsflag = sflag;
    285   1.4       cgd 		}
    286   1.4       cgd 		m1 = NULL;
    287   1.4       cgd 
    288   1.4       cgd 
    289   1.4       cgd 		/* here we can have small, especially forbidden packets */
    290   1.4       cgd 
    291  1.18        is 		if ((m->m_pkthdr.len >=
    292  1.18        is 		    ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
    293  1.18        is 		    (m->m_pkthdr.len <=
    294  1.18        is 		    ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
    295  1.18        is 
    296  1.20        is 			M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
    297   1.4       cgd 			if (m == 0)
    298   1.4       cgd 				senderr(ENOBUFS);
    299  1.18        is 			ah = mtod(m, struct arc_header *);
    300  1.18        is 			ah->arc_flag = 0xFF;
    301  1.18        is 			ah->arc_seqid = 0xFFFF;
    302  1.18        is 			ah->arc_type2 = atype;
    303  1.18        is 			ah->arc_flag2 = sflag;
    304  1.18        is 			ah->arc_seqid2 = ac->ac_seqid;
    305  1.18        is 		} else {
    306  1.18        is 			M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
    307   1.4       cgd 			if (m == 0)
    308   1.4       cgd 				senderr(ENOBUFS);
    309   1.4       cgd 			ah = mtod(m, struct arc_header *);
    310  1.18        is 			ah->arc_flag = sflag;
    311  1.18        is 			ah->arc_seqid = ac->ac_seqid;
    312   1.4       cgd 		}
    313   1.4       cgd 
    314   1.6   mycroft 		ah->arc_dhost = adst;
    315  1.12        is 		ah->arc_shost = myself;
    316  1.18        is 		ah->arc_type = atype;
    317   1.4       cgd 	} else {
    318   1.4       cgd 		M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
    319   1.4       cgd 		if (m == 0)
    320   1.4       cgd 			senderr(ENOBUFS);
    321   1.4       cgd 		ah = mtod(m, struct arc_header *);
    322   1.4       cgd 		ah->arc_type = atype;
    323   1.6   mycroft 		ah->arc_dhost = adst;
    324  1.12        is 		ah->arc_shost = myself;
    325   1.6   mycroft 	}
    326  1.39    itojun 
    327  1.72  knakahar 	return ifq_enqueue(ifp, m);
    328   1.1     glass 
    329   1.1     glass bad:
    330  1.86       rin 	m_freem(m1);
    331  1.86       rin 	m_freem(m);
    332   1.1     glass 	return (error);
    333   1.1     glass }
    334   1.1     glass 
    335   1.1     glass /*
    336  1.46     perry  * Defragmenter. Returns mbuf if last packet found, else
    337   1.4       cgd  * NULL. frees imcoming mbuf as necessary.
    338   1.4       cgd  */
    339   1.4       cgd 
    340  1.51   thorpej static struct mbuf *
    341  1.51   thorpej arc_defrag(struct ifnet *ifp, struct mbuf *m)
    342   1.4       cgd {
    343   1.4       cgd 	struct arc_header *ah, *ah1;
    344   1.4       cgd 	struct arccom *ac;
    345   1.4       cgd 	struct ac_frag *af;
    346   1.4       cgd 	struct mbuf *m1;
    347  1.49        he 	const char *s;
    348   1.4       cgd 	int newflen;
    349  1.44    itojun 	u_char src, dst, typ;
    350  1.46     perry 
    351   1.4       cgd 	ac = (struct arccom *)ifp;
    352   1.4       cgd 
    353  1.27        is 	if (m->m_len < ARC_HDRNEWLEN) {
    354  1.27        is 		m = m_pullup(m, ARC_HDRNEWLEN);
    355  1.27        is 		if (m == NULL) {
    356  1.81   thorpej 			if_statinc(ifp, if_ierrors);
    357  1.27        is 			return NULL;
    358  1.27        is 		}
    359   1.4       cgd 	}
    360   1.4       cgd 
    361   1.4       cgd 	ah = mtod(m, struct arc_header *);
    362   1.4       cgd 	typ = ah->arc_type;
    363   1.4       cgd 
    364   1.4       cgd 	if (!arc_isphds(typ))
    365   1.4       cgd 		return m;
    366   1.4       cgd 
    367   1.4       cgd 	src = ah->arc_shost;
    368   1.4       cgd 	dst = ah->arc_dhost;
    369   1.4       cgd 
    370   1.4       cgd 	if (ah->arc_flag == 0xff) {
    371   1.4       cgd 		m_adj(m, 4);
    372   1.4       cgd 
    373  1.27        is 		if (m->m_len < ARC_HDRNEWLEN) {
    374  1.27        is 			m = m_pullup(m, ARC_HDRNEWLEN);
    375  1.27        is 			if (m == NULL) {
    376  1.81   thorpej 				if_statinc(ifp, if_ierrors);
    377  1.27        is 				return NULL;
    378  1.27        is 			}
    379   1.4       cgd 		}
    380   1.4       cgd 
    381   1.4       cgd 		ah = mtod(m, struct arc_header *);
    382   1.4       cgd 	}
    383   1.4       cgd 
    384   1.4       cgd 	af = &ac->ac_fragtab[src];
    385   1.4       cgd 	m1 = af->af_packet;
    386   1.4       cgd 	s = "debug code error";
    387   1.4       cgd 
    388   1.4       cgd 	if (ah->arc_flag & 1) {
    389  1.46     perry 		/*
    390   1.4       cgd 		 * first fragment. We always initialize, which is
    391   1.4       cgd 		 * about the right thing to do, as we only want to
    392   1.4       cgd 		 * accept one fragmented packet per src at a time.
    393   1.4       cgd 		 */
    394  1.86       rin 		m_freem(m1);
    395   1.4       cgd 
    396   1.4       cgd 		af->af_packet = m;
    397   1.4       cgd 		m1 = m;
    398   1.4       cgd 		af->af_maxflag = ah->arc_flag;
    399   1.4       cgd 		af->af_lastseen = 0;
    400   1.4       cgd 		af->af_seqid = ah->arc_seqid;
    401   1.4       cgd 
    402   1.4       cgd 		return NULL;
    403   1.4       cgd 		/* notreached */
    404   1.4       cgd 	} else {
    405   1.4       cgd 		/* check for unfragmented packet */
    406   1.4       cgd 		if (ah->arc_flag == 0)
    407   1.4       cgd 			return m;
    408   1.4       cgd 
    409   1.4       cgd 		/* do we have a first packet from that src? */
    410   1.4       cgd 		if (m1 == NULL) {
    411   1.4       cgd 			s = "no first frag";
    412   1.4       cgd 			goto outofseq;
    413   1.4       cgd 		}
    414   1.4       cgd 
    415   1.4       cgd 		ah1 = mtod(m1, struct arc_header *);
    416   1.4       cgd 
    417   1.4       cgd 		if (ah->arc_seqid != ah1->arc_seqid) {
    418   1.4       cgd 			s = "seqid differs";
    419   1.4       cgd 			goto outofseq;
    420   1.4       cgd 		}
    421   1.4       cgd 
    422  1.19        is 		if (typ != ah1->arc_type) {
    423   1.4       cgd 			s = "type differs";
    424   1.4       cgd 			goto outofseq;
    425   1.4       cgd 		}
    426   1.4       cgd 
    427  1.19        is 		if (dst != ah1->arc_dhost) {
    428   1.4       cgd 			s = "dest host differs";
    429   1.4       cgd 			goto outofseq;
    430   1.4       cgd 		}
    431   1.4       cgd 
    432   1.4       cgd 		/* typ, seqid and dst are ok here. */
    433   1.4       cgd 
    434   1.4       cgd 		if (ah->arc_flag == af->af_lastseen) {
    435   1.4       cgd 			m_freem(m);
    436   1.4       cgd 			return NULL;
    437   1.4       cgd 		}
    438   1.4       cgd 
    439   1.4       cgd 		if (ah->arc_flag == af->af_lastseen + 2) {
    440   1.4       cgd 			/* ok, this is next fragment */
    441   1.4       cgd 			af->af_lastseen = ah->arc_flag;
    442  1.44    itojun 			m_adj(m, ARC_HDRNEWLEN);
    443   1.4       cgd 
    444  1.46     perry 			/*
    445   1.4       cgd 			 * m_cat might free the first mbuf (with pkthdr)
    446   1.4       cgd 			 * in 2nd chain; therefore:
    447   1.4       cgd 			 */
    448   1.4       cgd 
    449  1.46     perry 			newflen = m->m_pkthdr.len;
    450   1.4       cgd 
    451  1.44    itojun 			m_cat(m1, m);
    452   1.4       cgd 
    453   1.4       cgd 			m1->m_pkthdr.len += newflen;
    454   1.4       cgd 
    455   1.4       cgd 			/* is it the last one? */
    456   1.4       cgd 			if (af->af_lastseen > af->af_maxflag) {
    457   1.4       cgd 				af->af_packet = NULL;
    458  1.40    itojun 				return (m1);
    459   1.4       cgd 			} else
    460   1.4       cgd 				return NULL;
    461   1.4       cgd 		}
    462   1.4       cgd 		s = "other reason";
    463   1.4       cgd 		/* if all else fails, it is out of sequence, too */
    464   1.4       cgd 	}
    465   1.4       cgd outofseq:
    466   1.4       cgd 	if (m1) {
    467   1.4       cgd 		m_freem(m1);
    468   1.4       cgd 		af->af_packet = NULL;
    469   1.4       cgd 	}
    470   1.4       cgd 
    471  1.86       rin 	m_freem(m);
    472   1.4       cgd 
    473   1.8   thorpej 	log(LOG_INFO,"%s: got out of seq. packet: %s\n",
    474   1.8   thorpej 	    ifp->if_xname, s);
    475   1.4       cgd 
    476   1.4       cgd 	return NULL;
    477   1.4       cgd }
    478   1.4       cgd 
    479   1.4       cgd /*
    480   1.4       cgd  * return 1 if Packet Header Definition Standard, else 0.
    481   1.4       cgd  * For now: old IP, old ARP aren't obviously. Lacking correct information,
    482   1.4       cgd  * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
    483   1.4       cgd  * (Apple and Novell corporations were involved, among others, in PHDS work).
    484   1.4       cgd  * Easiest is to assume that everybody else uses that, too.
    485   1.4       cgd  */
    486   1.4       cgd int
    487  1.51   thorpej arc_isphds(uint8_t type)
    488   1.4       cgd {
    489  1.46     perry 	return (type != ARCTYPE_IP_OLD &&
    490  1.27        is 		type != ARCTYPE_ARP_OLD &&
    491  1.27        is 		type != ARCTYPE_DIAGNOSE);
    492   1.4       cgd }
    493   1.4       cgd 
    494   1.4       cgd /*
    495   1.1     glass  * Process a received Arcnet packet;
    496   1.3    chopps  * the packet is in the mbuf chain m with
    497   1.3    chopps  * the ARCnet header.
    498   1.1     glass  */
    499  1.21   thorpej static void
    500  1.51   thorpej arc_input(struct ifnet *ifp, struct mbuf *m)
    501   1.1     glass {
    502  1.66     rmind 	pktqueue_t *pktq = NULL;
    503  1.30  augustss 	struct arc_header *ah;
    504  1.59      matt 	uint8_t atype;
    505   1.1     glass 
    506   1.1     glass 	if ((ifp->if_flags & IFF_UP) == 0) {
    507   1.1     glass 		m_freem(m);
    508   1.1     glass 		return;
    509   1.1     glass 	}
    510   1.4       cgd 
    511   1.4       cgd 	/* possibly defragment: */
    512   1.4       cgd 	m = arc_defrag(ifp, m);
    513  1.46     perry 	if (m == NULL)
    514   1.4       cgd 		return;
    515   1.4       cgd 
    516   1.4       cgd 	ah = mtod(m, struct arc_header *);
    517   1.3    chopps 
    518  1.81   thorpej 	if_statadd(ifp, if_ibytes, m->m_pkthdr.len);
    519   1.1     glass 
    520   1.1     glass 	if (arcbroadcastaddr == ah->arc_dhost) {
    521   1.9        is 		m->m_flags |= M_BCAST|M_MCAST;
    522  1.81   thorpej 		if_statinc(ifp, if_imcasts);
    523   1.1     glass 	}
    524   1.1     glass 
    525   1.1     glass 	atype = ah->arc_type;
    526   1.1     glass 	switch (atype) {
    527   1.1     glass #ifdef INET
    528   1.4       cgd 	case ARCTYPE_IP:
    529   1.6   mycroft 		m_adj(m, ARC_HDRNEWLEN);
    530  1.66     rmind 		pktq = ip_pktq;
    531   1.4       cgd 		break;
    532   1.4       cgd 
    533   1.1     glass 	case ARCTYPE_IP_OLD:
    534   1.6   mycroft 		m_adj(m, ARC_HDRLEN);
    535  1.66     rmind 		pktq = ip_pktq;
    536   1.1     glass 		break;
    537  1.12        is 
    538  1.12        is 	case ARCTYPE_ARP:
    539  1.12        is 		m_adj(m, ARC_HDRNEWLEN);
    540  1.84   thorpej 		pktq = arp_pktq;
    541  1.12        is #ifdef ARCNET_ALLOW_BROKEN_ARP
    542  1.15        is 		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
    543  1.12        is #endif
    544  1.12        is 		break;
    545  1.12        is 
    546  1.12        is 	case ARCTYPE_ARP_OLD:
    547  1.12        is 		m_adj(m, ARC_HDRLEN);
    548  1.84   thorpej 		pktq = arp_pktq;
    549  1.12        is #ifdef ARCNET_ALLOW_BROKEN_ARP
    550  1.15        is 		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
    551  1.12        is #endif
    552  1.12        is 		break;
    553   1.1     glass #endif
    554  1.27        is #ifdef INET6
    555  1.27        is 	case ARCTYPE_INET6:
    556  1.27        is 		m_adj(m, ARC_HDRNEWLEN);
    557  1.66     rmind 		pktq = ip6_pktq;
    558  1.27        is 		break;
    559  1.27        is #endif
    560   1.1     glass 	default:
    561   1.1     glass 		m_freem(m);
    562   1.1     glass 		return;
    563   1.1     glass 	}
    564   1.1     glass 
    565  1.84   thorpej 	KASSERT(pktq != NULL);
    566  1.84   thorpej 	if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
    567  1.84   thorpej 		m_freem(m);
    568  1.66     rmind 	}
    569   1.1     glass }
    570   1.1     glass 
    571   1.1     glass /*
    572   1.1     glass  * Convert Arcnet address to printable (loggable) representation.
    573   1.1     glass  */
    574   1.1     glass char *
    575  1.51   thorpej arc_sprintf(uint8_t *ap)
    576   1.1     glass {
    577   1.1     glass 	static char arcbuf[3];
    578  1.30  augustss 	char *cp = arcbuf;
    579   1.1     glass 
    580  1.48  christos 	*cp++ = hexdigits[*ap >> 4];
    581  1.48  christos 	*cp++ = hexdigits[*ap++ & 0xf];
    582   1.1     glass 	*cp   = 0;
    583   1.1     glass 	return (arcbuf);
    584   1.1     glass }
    585   1.1     glass 
    586   1.1     glass /*
    587   1.1     glass  * Perform common duties while attaching to interface list
    588   1.1     glass  */
    589  1.78   msaitoh int
    590  1.51   thorpej arc_ifattach(struct ifnet *ifp, uint8_t lla)
    591   1.1     glass {
    592  1.30  augustss 	struct arccom *ac;
    593   1.1     glass 
    594   1.1     glass 	ifp->if_type = IFT_ARCNET;
    595   1.1     glass 	ifp->if_addrlen = 1;
    596   1.1     glass 	ifp->if_hdrlen = ARC_HDRLEN;
    597  1.33   thorpej 	ifp->if_dlt = DLT_ARCNET;
    598   1.9        is 	if (ifp->if_flags & IFF_BROADCAST)
    599   1.9        is 		ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
    600  1.28        is 	if (ifp->if_flags & IFF_LINK0 && arc_ipmtu > ARC_PHDS_MAXMTU)
    601   1.4       cgd 		log(LOG_ERR,
    602  1.64   msaitoh 		    "%s: arc_ipmtu is %d, but must not exceed %d\n",
    603  1.28        is 		    ifp->if_xname, arc_ipmtu, ARC_PHDS_MAXMTU);
    604   1.4       cgd 
    605  1.21   thorpej 	ifp->if_output = arc_output;
    606  1.70     ozaki 	ifp->_if_input = arc_input;
    607   1.4       cgd 	ac = (struct arccom *)ifp;
    608  1.52    kardel 	ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
    609  1.12        is 	if (lla == 0) {
    610   1.4       cgd 		/* XXX this message isn't entirely clear, to me -- cgd */
    611   1.8   thorpej 		log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
    612  1.46     perry 		   ifp->if_xname, ifp->if_xname);
    613   1.3    chopps 	}
    614  1.83  riastrad 	if_attach(ifp);
    615  1.78   msaitoh 
    616  1.60    dyoung 	if_set_sadl(ifp, &lla, sizeof(lla), true);
    617  1.24        is 
    618  1.16        is 	ifp->if_broadcastaddr = &arcbroadcastaddr;
    619  1.32   thorpej 
    620  1.63     joerg 	bpf_attach(ifp, DLT_ARCNET, ARC_HDRLEN);
    621  1.78   msaitoh 
    622  1.78   msaitoh 	return 0;
    623   1.1     glass }
    624