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