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