if_arcsubr.c revision 139823
189099Sfjoe/*	$NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $	*/
289099Sfjoe/*	$FreeBSD: head/sys/net/if_arcsubr.c 139823 2005-01-07 01:45:51Z imp $ */
389099Sfjoe
4139823Simp/*-
589099Sfjoe * Copyright (c) 1994, 1995 Ignatios Souvatzis
689099Sfjoe * Copyright (c) 1982, 1989, 1993
789099Sfjoe *	The Regents of the University of California.  All rights reserved.
889099Sfjoe *
989099Sfjoe * Redistribution and use in source and binary forms, with or without
1089099Sfjoe * modification, are permitted provided that the following conditions
1189099Sfjoe * are met:
1289099Sfjoe * 1. Redistributions of source code must retain the above copyright
1389099Sfjoe *    notice, this list of conditions and the following disclaimer.
1489099Sfjoe * 2. Redistributions in binary form must reproduce the above copyright
1589099Sfjoe *    notice, this list of conditions and the following disclaimer in the
1689099Sfjoe *    documentation and/or other materials provided with the distribution.
1789099Sfjoe * 3. All advertising materials mentioning features or use of this software
1889099Sfjoe *    must display the following acknowledgement:
1989099Sfjoe *	This product includes software developed by the University of
2089099Sfjoe *	California, Berkeley and its contributors.
2189099Sfjoe * 4. Neither the name of the University nor the names of its contributors
2289099Sfjoe *    may be used to endorse or promote products derived from this software
2389099Sfjoe *    without specific prior written permission.
2489099Sfjoe *
2589099Sfjoe * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2689099Sfjoe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2789099Sfjoe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2889099Sfjoe * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2989099Sfjoe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3089099Sfjoe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3189099Sfjoe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3289099Sfjoe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3389099Sfjoe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3489099Sfjoe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3589099Sfjoe * SUCH DAMAGE.
3689099Sfjoe *
3789099Sfjoe * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
3889099Sfjoe *       @(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
3989099Sfjoe *
4089099Sfjoe */
4189099Sfjoe#include "opt_inet.h"
4289099Sfjoe#include "opt_inet6.h"
43109771Sfjoe#include "opt_ipx.h"
4489099Sfjoe
4589099Sfjoe#include <sys/param.h>
4689099Sfjoe#include <sys/systm.h>
4789099Sfjoe#include <sys/kernel.h>
48129880Sphk#include <sys/module.h>
4989099Sfjoe#include <sys/malloc.h>
5089099Sfjoe#include <sys/mbuf.h>
5189099Sfjoe#include <sys/protosw.h>
5289099Sfjoe#include <sys/socket.h>
5389099Sfjoe#include <sys/sockio.h>
5489099Sfjoe#include <sys/errno.h>
5589099Sfjoe#include <sys/syslog.h>
5689099Sfjoe
5789099Sfjoe#include <machine/cpu.h>
5889099Sfjoe
5989099Sfjoe#include <net/if.h>
6089099Sfjoe#include <net/netisr.h>
6189099Sfjoe#include <net/route.h>
6289099Sfjoe#include <net/if_dl.h>
6389099Sfjoe#include <net/if_types.h>
6489099Sfjoe#include <net/if_arc.h>
6589099Sfjoe#include <net/if_arp.h>
6689099Sfjoe#include <net/bpf.h>
6789099Sfjoe
6889099Sfjoe#if defined(INET) || defined(INET6)
6989099Sfjoe#include <netinet/in.h>
7089099Sfjoe#include <netinet/in_var.h>
7189099Sfjoe#include <netinet/if_ether.h>
7289099Sfjoe#endif
7389099Sfjoe
7489099Sfjoe#ifdef INET6
7589099Sfjoe#include <netinet6/nd6.h>
7689099Sfjoe#endif
7789099Sfjoe
78109771Sfjoe#ifdef IPX
79109771Sfjoe#include <netipx/ipx.h>
80109771Sfjoe#include <netipx/ipx_if.h>
81109771Sfjoe#endif
82109771Sfjoe
8389099SfjoeMODULE_VERSION(arcnet, 1);
8489099Sfjoe
8589099Sfjoe#define ARCNET_ALLOW_BROKEN_ARP
8689099Sfjoe
8792725Salfredstatic struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
88109771Sfjoestatic int arc_resolvemulti(struct ifnet *, struct sockaddr **,
89109771Sfjoe			    struct sockaddr *);
9089099Sfjoe
9189099Sfjoeu_int8_t  arcbroadcastaddr = 0;
9289099Sfjoe
93110106Sfjoe#define ARC_LLADDR(ifp)	(*(u_int8_t *)IF_LLADDR(ifp))
94110106Sfjoe
9589099Sfjoe#define senderr(e) { error = (e); goto bad;}
96109771Sfjoe#define SIN(s)	((struct sockaddr_in *)s)
97109771Sfjoe#define SIPX(s)	((struct sockaddr_ipx *)s)
9889099Sfjoe
9989099Sfjoe/*
10089099Sfjoe * ARCnet output routine.
10189099Sfjoe * Encapsulate a packet of type family for the local net.
10289099Sfjoe * Assumes that ifp is actually pointer to arccom structure.
10389099Sfjoe */
10489099Sfjoeint
10589099Sfjoearc_output(ifp, m, dst, rt0)
10689099Sfjoe	struct ifnet *ifp;
10789099Sfjoe	struct mbuf *m;
10889099Sfjoe	struct sockaddr *dst;
10989099Sfjoe	struct rtentry *rt0;
11089099Sfjoe{
11189099Sfjoe	struct arc_header	*ah;
11289099Sfjoe	int			error;
11389099Sfjoe	u_int8_t		atype, adst;
114109771Sfjoe	int			loop_copy = 0;
115110106Sfjoe	int			isphds;
11689099Sfjoe
11789099Sfjoe	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
11889099Sfjoe		return(ENETDOWN); /* m, m1 aren't initialized yet */
11989099Sfjoe
12089099Sfjoe	error = 0;
12189099Sfjoe
12289099Sfjoe	switch (dst->sa_family) {
12389099Sfjoe#ifdef INET
12489099Sfjoe	case AF_INET:
12589099Sfjoe
12689099Sfjoe		/*
12789099Sfjoe		 * For now, use the simple IP addr -> ARCnet addr mapping
12889099Sfjoe		 */
12989099Sfjoe		if (m->m_flags & (M_BCAST|M_MCAST))
13089099Sfjoe			adst = arcbroadcastaddr; /* ARCnet broadcast address */
13189099Sfjoe		else if (ifp->if_flags & IFF_NOARP)
13289099Sfjoe			adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
133128636Sluigi		else {
134128636Sluigi			error = arpresolve(ifp, rt0, m, dst, &adst);
135128636Sluigi			if (error)
136128636Sluigi				return (error == EWOULDBLOCK ? 0 : error);
137128636Sluigi		}
13889099Sfjoe
13989099Sfjoe		atype = (ifp->if_flags & IFF_LINK0) ?
14089099Sfjoe			ARCTYPE_IP_OLD : ARCTYPE_IP;
14189099Sfjoe		break;
142127260Smdodd	case AF_ARP:
143127260Smdodd	{
144127260Smdodd		struct arphdr *ah;
145127260Smdodd		ah = mtod(m, struct arphdr *);
146127260Smdodd		ah->ar_hrd = htons(ARPHRD_ARCNET);
147127260Smdodd
148127260Smdodd		loop_copy = -1; /* if this is for us, don't do it */
149127260Smdodd
150127260Smdodd		switch(ntohs(ah->ar_op)) {
151127260Smdodd		case ARPOP_REVREQUEST:
152127260Smdodd		case ARPOP_REVREPLY:
153127275Smdodd			atype = ARCTYPE_REVARP;
154127260Smdodd			break;
155127260Smdodd		case ARPOP_REQUEST:
156127260Smdodd		case ARPOP_REPLY:
157127260Smdodd		default:
158127275Smdodd			atype = ARCTYPE_ARP;
159127260Smdodd			break;
160127260Smdodd		}
161127260Smdodd
162127260Smdodd		if (m->m_flags & M_BCAST)
163127290Smdodd			bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN);
164127260Smdodd		else
165127290Smdodd			bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN);
166127260Smdodd
167127260Smdodd	}
168127260Smdodd	break;
16989099Sfjoe#endif
17089099Sfjoe#ifdef INET6
17189099Sfjoe	case AF_INET6:
172128636Sluigi		error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)&adst);
173128636Sluigi		if (error)
174128636Sluigi			return (error);
17589099Sfjoe		atype = ARCTYPE_INET6;
17689099Sfjoe		break;
17789099Sfjoe#endif
178109771Sfjoe#ifdef IPX
179109771Sfjoe	case AF_IPX:
180109771Sfjoe		adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
181109771Sfjoe		atype = ARCTYPE_IPX;
182109771Sfjoe		if (adst == 0xff)
183109771Sfjoe			adst = arcbroadcastaddr;
184109771Sfjoe		break;
185109771Sfjoe#endif
18689099Sfjoe
18789099Sfjoe	case AF_UNSPEC:
188109771Sfjoe		loop_copy = -1;
18989099Sfjoe		ah = (struct arc_header *)dst->sa_data;
19089099Sfjoe		adst = ah->arc_dhost;
19189099Sfjoe		atype = ah->arc_type;
19289099Sfjoe
19389099Sfjoe		if (atype == ARCTYPE_ARP) {
19489099Sfjoe			atype = (ifp->if_flags & IFF_LINK0) ?
19589099Sfjoe			    ARCTYPE_ARP_OLD: ARCTYPE_ARP;
19689099Sfjoe
19789099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
19889099Sfjoe			/*
19989099Sfjoe			 * XXX It's not clear per RFC826 if this is needed, but
20089099Sfjoe			 * "assigned numbers" say this is wrong.
20189099Sfjoe			 * However, e.g., AmiTCP 3.0Beta used it... we make this
20289099Sfjoe			 * switchable for emergency cases. Not perfect, but...
20389099Sfjoe			 */
20489099Sfjoe			if (ifp->if_flags & IFF_LINK2)
205109771Sfjoe				mtod(m, struct arphdr *)->ar_pro = atype - 1;
20689099Sfjoe#endif
20789099Sfjoe		}
20889099Sfjoe		break;
20989099Sfjoe
21089099Sfjoe	default:
211105598Sbrooks		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
21289099Sfjoe		senderr(EAFNOSUPPORT);
21389099Sfjoe	}
21489099Sfjoe
215110106Sfjoe	isphds = arc_isphds(atype);
216111119Simp	M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_DONTWAIT);
21789099Sfjoe	if (m == 0)
21889099Sfjoe		senderr(ENOBUFS);
21989099Sfjoe	ah = mtod(m, struct arc_header *);
22089099Sfjoe	ah->arc_type = atype;
22189099Sfjoe	ah->arc_dhost = adst;
222110106Sfjoe	ah->arc_shost = ARC_LLADDR(ifp);
223110106Sfjoe	if (isphds) {
224110106Sfjoe		ah->arc_flag = 0;
225110106Sfjoe		ah->arc_seqid = 0;
226110106Sfjoe	}
22789099Sfjoe
228109771Sfjoe	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
229109771Sfjoe		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
230109771Sfjoe			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
231109771Sfjoe
232109771Sfjoe			(void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
233109771Sfjoe		} else if (ah->arc_dhost == ah->arc_shost) {
234109771Sfjoe			(void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
235109771Sfjoe			return (0);     /* XXX */
236109771Sfjoe		}
237109771Sfjoe	}
238109771Sfjoe
239106939Ssam	BPF_MTAP(ifp, m);
24089099Sfjoe
241130549Smlaier	IFQ_HANDOFF(ifp, m, error);
24289099Sfjoe
24389099Sfjoe	return (error);
24489099Sfjoe
24589099Sfjoebad:
24689099Sfjoe	if (m)
24789099Sfjoe		m_freem(m);
24889099Sfjoe	return (error);
24989099Sfjoe}
25089099Sfjoe
25189099Sfjoevoid
25289099Sfjoearc_frag_init(ifp)
25389099Sfjoe	struct ifnet *ifp;
25489099Sfjoe{
25589099Sfjoe	struct arccom *ac;
25689099Sfjoe
25789099Sfjoe	ac = (struct arccom *)ifp;
25889099Sfjoe	ac->curr_frag = 0;
25989099Sfjoe}
26089099Sfjoe
26189099Sfjoestruct mbuf *
26289099Sfjoearc_frag_next(ifp)
26389099Sfjoe	struct ifnet *ifp;
26489099Sfjoe{
26589099Sfjoe	struct arccom *ac;
26689099Sfjoe	struct mbuf *m;
26789099Sfjoe	struct arc_header *ah;
26889099Sfjoe
26989099Sfjoe	ac = (struct arccom *)ifp;
27089099Sfjoe	if ((m = ac->curr_frag) == 0) {
27189099Sfjoe		int tfrags;
27289099Sfjoe
27389099Sfjoe		/* dequeue new packet */
27489099Sfjoe		IF_DEQUEUE(&ifp->if_snd, m);
27589099Sfjoe		if (m == 0)
27689099Sfjoe			return 0;
27789099Sfjoe
27889099Sfjoe		ah = mtod(m, struct arc_header *);
27989099Sfjoe		if (!arc_isphds(ah->arc_type))
28089099Sfjoe			return m;
28189099Sfjoe
28289099Sfjoe		++ac->ac_seqid;		/* make the seqid unique */
283109771Sfjoe		tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
28489099Sfjoe		ac->fsflag = 2 * tfrags - 3;
28589099Sfjoe		ac->sflag = 0;
28689099Sfjoe		ac->rsflag = ac->fsflag;
28789099Sfjoe		ac->arc_dhost = ah->arc_dhost;
28889099Sfjoe		ac->arc_shost = ah->arc_shost;
28989099Sfjoe		ac->arc_type = ah->arc_type;
29089099Sfjoe
291110106Sfjoe		m_adj(m, ARC_HDRNEWLEN);
29289099Sfjoe		ac->curr_frag = m;
29389099Sfjoe	}
29489099Sfjoe
29589099Sfjoe	/* split out next fragment and return it */
29689099Sfjoe	if (ac->sflag < ac->fsflag) {
29789099Sfjoe		/* we CAN'T have short packets here */
298111119Simp		ac->curr_frag = m_split(m, ARC_MAX_DATA, M_DONTWAIT);
29989099Sfjoe		if (ac->curr_frag == 0) {
30093750Sluigi			m_freem(m);
30189099Sfjoe			return 0;
30289099Sfjoe		}
30389099Sfjoe
304111119Simp		M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
30589099Sfjoe		if (m == 0) {
30693750Sluigi			m_freem(ac->curr_frag);
30789099Sfjoe			ac->curr_frag = 0;
30889099Sfjoe			return 0;
30989099Sfjoe		}
31089099Sfjoe
31189099Sfjoe		ah = mtod(m, struct arc_header *);
31289099Sfjoe		ah->arc_flag = ac->rsflag;
31389099Sfjoe		ah->arc_seqid = ac->ac_seqid;
31489099Sfjoe
31589099Sfjoe		ac->sflag += 2;
31689099Sfjoe		ac->rsflag = ac->sflag;
31789099Sfjoe	} else if ((m->m_pkthdr.len >=
31889099Sfjoe	    ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
31989099Sfjoe	    (m->m_pkthdr.len <=
32089099Sfjoe	    ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
32189099Sfjoe		ac->curr_frag = 0;
32289099Sfjoe
323111119Simp		M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
32489099Sfjoe		if (m == 0)
32589099Sfjoe			return 0;
32689099Sfjoe
32789099Sfjoe		ah = mtod(m, struct arc_header *);
32889099Sfjoe		ah->arc_flag = 0xFF;
32989099Sfjoe		ah->arc_seqid = 0xFFFF;
33089099Sfjoe		ah->arc_type2 = ac->arc_type;
33189099Sfjoe		ah->arc_flag2 = ac->sflag;
33289099Sfjoe		ah->arc_seqid2 = ac->ac_seqid;
33389099Sfjoe	} else {
33489099Sfjoe		ac->curr_frag = 0;
33589099Sfjoe
336111119Simp		M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
33789099Sfjoe		if (m == 0)
33889099Sfjoe			return 0;
33989099Sfjoe
34089099Sfjoe		ah = mtod(m, struct arc_header *);
34189099Sfjoe		ah->arc_flag = ac->sflag;
34289099Sfjoe		ah->arc_seqid = ac->ac_seqid;
34389099Sfjoe	}
34489099Sfjoe
34589099Sfjoe	ah->arc_dhost = ac->arc_dhost;
34689099Sfjoe	ah->arc_shost = ac->arc_shost;
34789099Sfjoe	ah->arc_type = ac->arc_type;
34889099Sfjoe
34989099Sfjoe	return m;
35089099Sfjoe}
35189099Sfjoe
35289099Sfjoe/*
35389099Sfjoe * Defragmenter. Returns mbuf if last packet found, else
35489099Sfjoe * NULL. frees imcoming mbuf as necessary.
35589099Sfjoe */
35689099Sfjoe
357105228Sphkstatic __inline struct mbuf *
35889099Sfjoearc_defrag(ifp, m)
35989099Sfjoe	struct ifnet *ifp;
36089099Sfjoe	struct mbuf *m;
36189099Sfjoe{
36289099Sfjoe	struct arc_header *ah, *ah1;
36389099Sfjoe	struct arccom *ac;
36489099Sfjoe	struct ac_frag *af;
36589099Sfjoe	struct mbuf *m1;
36689099Sfjoe	char *s;
36789099Sfjoe	int newflen;
36889099Sfjoe	u_char src,dst,typ;
36989099Sfjoe
37089099Sfjoe	ac = (struct arccom *)ifp;
37189099Sfjoe
37289099Sfjoe	if (m->m_len < ARC_HDRNEWLEN) {
37389099Sfjoe		m = m_pullup(m, ARC_HDRNEWLEN);
37489099Sfjoe		if (m == NULL) {
37589099Sfjoe			++ifp->if_ierrors;
37689099Sfjoe			return NULL;
37789099Sfjoe		}
37889099Sfjoe	}
37989099Sfjoe
38089099Sfjoe	ah = mtod(m, struct arc_header *);
38189099Sfjoe	typ = ah->arc_type;
38289099Sfjoe
38389099Sfjoe	if (!arc_isphds(typ))
38489099Sfjoe		return m;
38589099Sfjoe
38689099Sfjoe	src = ah->arc_shost;
38789099Sfjoe	dst = ah->arc_dhost;
38889099Sfjoe
38989099Sfjoe	if (ah->arc_flag == 0xff) {
39089099Sfjoe		m_adj(m, 4);
39189099Sfjoe
39289099Sfjoe		if (m->m_len < ARC_HDRNEWLEN) {
39389099Sfjoe			m = m_pullup(m, ARC_HDRNEWLEN);
39489099Sfjoe			if (m == NULL) {
39589099Sfjoe				++ifp->if_ierrors;
39689099Sfjoe				return NULL;
39789099Sfjoe			}
39889099Sfjoe		}
39989099Sfjoe
40089099Sfjoe		ah = mtod(m, struct arc_header *);
40189099Sfjoe	}
40289099Sfjoe
40389099Sfjoe	af = &ac->ac_fragtab[src];
40489099Sfjoe	m1 = af->af_packet;
40589099Sfjoe	s = "debug code error";
40689099Sfjoe
40789099Sfjoe	if (ah->arc_flag & 1) {
40889099Sfjoe		/*
40989099Sfjoe		 * first fragment. We always initialize, which is
41089099Sfjoe		 * about the right thing to do, as we only want to
41189099Sfjoe		 * accept one fragmented packet per src at a time.
41289099Sfjoe		 */
41389099Sfjoe		if (m1 != NULL)
41489099Sfjoe			m_freem(m1);
41589099Sfjoe
41689099Sfjoe		af->af_packet = m;
41789099Sfjoe		m1 = m;
41889099Sfjoe		af->af_maxflag = ah->arc_flag;
41989099Sfjoe		af->af_lastseen = 0;
42089099Sfjoe		af->af_seqid = ah->arc_seqid;
42189099Sfjoe
42289099Sfjoe		return NULL;
42389099Sfjoe		/* notreached */
42489099Sfjoe	} else {
42589099Sfjoe		/* check for unfragmented packet */
42689099Sfjoe		if (ah->arc_flag == 0)
42789099Sfjoe			return m;
42889099Sfjoe
42989099Sfjoe		/* do we have a first packet from that src? */
43089099Sfjoe		if (m1 == NULL) {
43189099Sfjoe			s = "no first frag";
43289099Sfjoe			goto outofseq;
43389099Sfjoe		}
43489099Sfjoe
43589099Sfjoe		ah1 = mtod(m1, struct arc_header *);
43689099Sfjoe
43789099Sfjoe		if (ah->arc_seqid != ah1->arc_seqid) {
43889099Sfjoe			s = "seqid differs";
43989099Sfjoe			goto outofseq;
44089099Sfjoe		}
44189099Sfjoe
44289099Sfjoe		if (typ != ah1->arc_type) {
44389099Sfjoe			s = "type differs";
44489099Sfjoe			goto outofseq;
44589099Sfjoe		}
44689099Sfjoe
44789099Sfjoe		if (dst != ah1->arc_dhost) {
44889099Sfjoe			s = "dest host differs";
44989099Sfjoe			goto outofseq;
45089099Sfjoe		}
45189099Sfjoe
45289099Sfjoe		/* typ, seqid and dst are ok here. */
45389099Sfjoe
45489099Sfjoe		if (ah->arc_flag == af->af_lastseen) {
45589099Sfjoe			m_freem(m);
45689099Sfjoe			return NULL;
45789099Sfjoe		}
45889099Sfjoe
45989099Sfjoe		if (ah->arc_flag == af->af_lastseen + 2) {
46089099Sfjoe			/* ok, this is next fragment */
46189099Sfjoe			af->af_lastseen = ah->arc_flag;
46289099Sfjoe			m_adj(m,ARC_HDRNEWLEN);
46389099Sfjoe
46489099Sfjoe			/*
46589099Sfjoe			 * m_cat might free the first mbuf (with pkthdr)
46689099Sfjoe			 * in 2nd chain; therefore:
46789099Sfjoe			 */
46889099Sfjoe
46989099Sfjoe			newflen = m->m_pkthdr.len;
47089099Sfjoe
47189099Sfjoe			m_cat(m1,m);
47289099Sfjoe
47389099Sfjoe			m1->m_pkthdr.len += newflen;
47489099Sfjoe
47589099Sfjoe			/* is it the last one? */
47689099Sfjoe			if (af->af_lastseen > af->af_maxflag) {
47789099Sfjoe				af->af_packet = NULL;
47889099Sfjoe				return(m1);
47989099Sfjoe			} else
48089099Sfjoe				return NULL;
48189099Sfjoe		}
48289099Sfjoe		s = "other reason";
48389099Sfjoe		/* if all else fails, it is out of sequence, too */
48489099Sfjoe	}
48589099Sfjoeoutofseq:
48689099Sfjoe	if (m1) {
48789099Sfjoe		m_freem(m1);
48889099Sfjoe		af->af_packet = NULL;
48989099Sfjoe	}
49089099Sfjoe
49189099Sfjoe	if (m)
49289099Sfjoe		m_freem(m);
49389099Sfjoe
494121816Sbrooks	log(LOG_INFO,"%s: got out of seq. packet: %s\n",
495121816Sbrooks	    ifp->if_xname, s);
49689099Sfjoe
49789099Sfjoe	return NULL;
49889099Sfjoe}
49989099Sfjoe
50089099Sfjoe/*
50189099Sfjoe * return 1 if Packet Header Definition Standard, else 0.
50289099Sfjoe * For now: old IP, old ARP aren't obviously. Lacking correct information,
50389099Sfjoe * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
50489099Sfjoe * (Apple and Novell corporations were involved, among others, in PHDS work).
50589099Sfjoe * Easiest is to assume that everybody else uses that, too.
50689099Sfjoe */
50789099Sfjoeint
50889099Sfjoearc_isphds(type)
50989099Sfjoe	u_int8_t type;
51089099Sfjoe{
51189099Sfjoe	return (type != ARCTYPE_IP_OLD &&
51289099Sfjoe		type != ARCTYPE_ARP_OLD &&
51389099Sfjoe		type != ARCTYPE_DIAGNOSE);
51489099Sfjoe}
51589099Sfjoe
51689099Sfjoe/*
51789099Sfjoe * Process a received Arcnet packet;
51889099Sfjoe * the packet is in the mbuf chain m with
51989099Sfjoe * the ARCnet header.
52089099Sfjoe */
52189099Sfjoevoid
52289099Sfjoearc_input(ifp, m)
52389099Sfjoe	struct ifnet *ifp;
52489099Sfjoe	struct mbuf *m;
52589099Sfjoe{
52689099Sfjoe	struct arc_header *ah;
527111888Sjlemon	int isr;
52889099Sfjoe	u_int8_t atype;
52989099Sfjoe
53089099Sfjoe	if ((ifp->if_flags & IFF_UP) == 0) {
53189099Sfjoe		m_freem(m);
53289099Sfjoe		return;
53389099Sfjoe	}
53489099Sfjoe
53589099Sfjoe	/* possibly defragment: */
53689099Sfjoe	m = arc_defrag(ifp, m);
53789099Sfjoe	if (m == NULL)
53889099Sfjoe		return;
53989099Sfjoe
540106939Ssam	BPF_MTAP(ifp, m);
54189099Sfjoe
54289099Sfjoe	ah = mtod(m, struct arc_header *);
543109771Sfjoe	/* does this belong to us? */
544110106Sfjoe	if ((ifp->if_flags & IFF_PROMISC) == 0
545109771Sfjoe	    && ah->arc_dhost != arcbroadcastaddr
546110106Sfjoe	    && ah->arc_dhost != ARC_LLADDR(ifp)) {
547109771Sfjoe		m_freem(m);
548109771Sfjoe		return;
549109771Sfjoe	}
55089099Sfjoe
55189099Sfjoe	ifp->if_ibytes += m->m_pkthdr.len;
55289099Sfjoe
553109771Sfjoe	if (ah->arc_dhost == arcbroadcastaddr) {
55489099Sfjoe		m->m_flags |= M_BCAST|M_MCAST;
55589099Sfjoe		ifp->if_imcasts++;
55689099Sfjoe	}
55789099Sfjoe
55889099Sfjoe	atype = ah->arc_type;
55989099Sfjoe	switch (atype) {
56089099Sfjoe#ifdef INET
56189099Sfjoe	case ARCTYPE_IP:
56289099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
563122702Sandre		if (ip_fastforward(m))
564109771Sfjoe			return;
565111888Sjlemon		isr = NETISR_IP;
56689099Sfjoe		break;
56789099Sfjoe
56889099Sfjoe	case ARCTYPE_IP_OLD:
56989099Sfjoe		m_adj(m, ARC_HDRLEN);
570122702Sandre		if (ip_fastforward(m))
571109771Sfjoe			return;
572111888Sjlemon		isr = NETISR_IP;
57389099Sfjoe		break;
57489099Sfjoe
57589099Sfjoe	case ARCTYPE_ARP:
57689099Sfjoe		if (ifp->if_flags & IFF_NOARP) {
57789099Sfjoe			/* Discard packet if ARP is disabled on interface */
57889099Sfjoe			m_freem(m);
57989099Sfjoe			return;
58089099Sfjoe		}
58189099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
582111888Sjlemon		isr = NETISR_ARP;
58389099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
58489099Sfjoe		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
58589099Sfjoe#endif
58689099Sfjoe		break;
58789099Sfjoe
58889099Sfjoe	case ARCTYPE_ARP_OLD:
58989099Sfjoe		if (ifp->if_flags & IFF_NOARP) {
59089099Sfjoe			/* Discard packet if ARP is disabled on interface */
59189099Sfjoe			m_freem(m);
59289099Sfjoe			return;
59389099Sfjoe		}
59489099Sfjoe		m_adj(m, ARC_HDRLEN);
595111888Sjlemon		isr = NETISR_ARP;
59689099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
59789099Sfjoe		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
59889099Sfjoe#endif
59989099Sfjoe		break;
60089099Sfjoe#endif
60189099Sfjoe#ifdef INET6
60289099Sfjoe	case ARCTYPE_INET6:
60389099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
604111888Sjlemon		isr = NETISR_IPV6;
60589099Sfjoe		break;
60689099Sfjoe#endif
607109771Sfjoe#ifdef IPX
608109771Sfjoe	case ARCTYPE_IPX:
609109771Sfjoe		m_adj(m, ARC_HDRNEWLEN);
610111888Sjlemon		isr = NETISR_IPX;
611109771Sfjoe		break;
612109771Sfjoe#endif
61389099Sfjoe	default:
61489099Sfjoe		m_freem(m);
61589099Sfjoe		return;
61689099Sfjoe	}
617111888Sjlemon	netisr_dispatch(isr, m);
61889099Sfjoe}
61989099Sfjoe
62089099Sfjoe/*
62189099Sfjoe * Register (new) link level address.
62289099Sfjoe */
62389099Sfjoevoid
62489099Sfjoearc_storelladdr(ifp, lla)
62589099Sfjoe	struct ifnet *ifp;
62689099Sfjoe	u_int8_t lla;
62789099Sfjoe{
628110106Sfjoe	ARC_LLADDR(ifp) = lla;
62989099Sfjoe}
63089099Sfjoe
63189099Sfjoe/*
63289099Sfjoe * Perform common duties while attaching to interface list
63389099Sfjoe */
63489099Sfjoevoid
63589099Sfjoearc_ifattach(ifp, lla)
63689099Sfjoe	struct ifnet *ifp;
63789099Sfjoe	u_int8_t lla;
63889099Sfjoe{
63989099Sfjoe	struct ifaddr *ifa;
64089099Sfjoe	struct sockaddr_dl *sdl;
64189099Sfjoe	struct arccom *ac;
64289099Sfjoe
64389099Sfjoe	if_attach(ifp);
64489099Sfjoe	ifp->if_type = IFT_ARCNET;
64589099Sfjoe	ifp->if_addrlen = 1;
64689099Sfjoe	ifp->if_hdrlen = ARC_HDRLEN;
64789099Sfjoe	ifp->if_mtu = 1500;
648109771Sfjoe	ifp->if_resolvemulti = arc_resolvemulti;
64989099Sfjoe	if (ifp->if_baudrate == 0)
65089099Sfjoe		ifp->if_baudrate = 2500000;
65189099Sfjoe#if __FreeBSD_version < 500000
65289099Sfjoe	ifa = ifnet_addrs[ifp->if_index - 1];
65389099Sfjoe#else
65489099Sfjoe	ifa = ifaddr_byindex(ifp->if_index);
65589099Sfjoe#endif
656135577Sstefanf	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
65789099Sfjoe	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
65889099Sfjoe	sdl->sdl_type = IFT_ARCNET;
65989099Sfjoe	sdl->sdl_alen = ifp->if_addrlen;
66089099Sfjoe
66189099Sfjoe	if (ifp->if_flags & IFF_BROADCAST)
66289099Sfjoe		ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
66389099Sfjoe
66489099Sfjoe	ac = (struct arccom *)ifp;
66589099Sfjoe	ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
66689099Sfjoe	if (lla == 0) {
66789099Sfjoe		/* XXX this message isn't entirely clear, to me -- cgd */
668121816Sbrooks		log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
669121816Sbrooks		   ifp->if_xname, ifp->if_xname);
67089099Sfjoe	}
67189099Sfjoe	arc_storelladdr(ifp, lla);
67289099Sfjoe
67389099Sfjoe	ifp->if_broadcastaddr = &arcbroadcastaddr;
67489099Sfjoe
67589099Sfjoe	bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
67689099Sfjoe}
67789099Sfjoe
67889099Sfjoevoid
67989099Sfjoearc_ifdetach(ifp)
68089099Sfjoe	struct ifnet *ifp;
68189099Sfjoe{
68289099Sfjoe	bpfdetach(ifp);
68389099Sfjoe	if_detach(ifp);
68489099Sfjoe}
68589099Sfjoe
68689099Sfjoeint
68789099Sfjoearc_ioctl(ifp, command, data)
68889099Sfjoe	struct ifnet *ifp;
68989099Sfjoe	int command;
69089099Sfjoe	caddr_t data;
69189099Sfjoe{
69289099Sfjoe	struct ifaddr *ifa = (struct ifaddr *) data;
69389099Sfjoe	struct ifreq *ifr = (struct ifreq *) data;
69489099Sfjoe	int error = 0;
69589099Sfjoe
69689099Sfjoe	switch (command) {
69789099Sfjoe	case SIOCSIFADDR:
69889099Sfjoe		ifp->if_flags |= IFF_UP;
69989099Sfjoe		switch (ifa->ifa_addr->sa_family) {
70089099Sfjoe#ifdef INET
70189099Sfjoe		case AF_INET:
70289099Sfjoe			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
70389099Sfjoe			arp_ifinit(ifp, ifa);
70489099Sfjoe			break;
70589099Sfjoe#endif
706109771Sfjoe#ifdef IPX
707109771Sfjoe		/*
708109771Sfjoe		 * XXX This code is probably wrong
709109771Sfjoe		 */
710109771Sfjoe		case AF_IPX:
711109771Sfjoe		{
712109771Sfjoe			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
713109771Sfjoe
714109771Sfjoe			if (ipx_nullhost(*ina))
715110106Sfjoe				ina->x_host.c_host[5] = ARC_LLADDR(ifp);
716109771Sfjoe			else
717109771Sfjoe				arc_storelladdr(ifp, ina->x_host.c_host[5]);
718109771Sfjoe
719109771Sfjoe			/*
720109771Sfjoe			 * Set new address
721109771Sfjoe			 */
722109771Sfjoe			ifp->if_init(ifp->if_softc);
723109771Sfjoe			break;
724109771Sfjoe		}
725109771Sfjoe#endif
72689099Sfjoe		default:
72789099Sfjoe			ifp->if_init(ifp->if_softc);
72889099Sfjoe			break;
72989099Sfjoe		}
73089099Sfjoe		break;
73189099Sfjoe
732109771Sfjoe	case SIOCGIFADDR:
733109771Sfjoe		{
734109771Sfjoe			struct sockaddr *sa;
735109771Sfjoe
736109771Sfjoe			sa = (struct sockaddr *) &ifr->ifr_data;
737110106Sfjoe			*(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
738109771Sfjoe		}
739109771Sfjoe		break;
740109771Sfjoe
74189099Sfjoe	case SIOCADDMULTI:
74289099Sfjoe	case SIOCDELMULTI:
74389099Sfjoe		if (ifr == NULL)
74489099Sfjoe			error = EAFNOSUPPORT;
74589099Sfjoe		else {
74689099Sfjoe			switch (ifr->ifr_addr.sa_family) {
74789099Sfjoe			case AF_INET:
74889099Sfjoe			case AF_INET6:
74989099Sfjoe				error = 0;
75089099Sfjoe				break;
75189099Sfjoe			default:
75289099Sfjoe				error = EAFNOSUPPORT;
75389099Sfjoe				break;
75489099Sfjoe			}
75589099Sfjoe		}
75689099Sfjoe		break;
75789099Sfjoe
75889099Sfjoe	case SIOCSIFMTU:
75989099Sfjoe		/*
76089099Sfjoe		 * Set the interface MTU.
76189099Sfjoe		 * mtu can't be larger than ARCMTU for RFC1051
76289099Sfjoe		 * and can't be larger than ARC_PHDS_MTU
76389099Sfjoe		 */
76489099Sfjoe		if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
76589099Sfjoe		    ifr->ifr_mtu > ARC_PHDS_MAXMTU)
76689099Sfjoe			error = EINVAL;
76789099Sfjoe		else
76889099Sfjoe			ifp->if_mtu = ifr->ifr_mtu;
76989099Sfjoe		break;
770109771Sfjoe	}
77189099Sfjoe
772109771Sfjoe	return (error);
773109771Sfjoe}
77489099Sfjoe
775109771Sfjoe/* based on ether_resolvemulti() */
776109771Sfjoeint
777109771Sfjoearc_resolvemulti(ifp, llsa, sa)
778109771Sfjoe	struct ifnet *ifp;
779109771Sfjoe	struct sockaddr **llsa;
780109771Sfjoe	struct sockaddr *sa;
781109771Sfjoe{
782109771Sfjoe	struct sockaddr_dl *sdl;
783109771Sfjoe	struct sockaddr_in *sin;
784109771Sfjoe#ifdef INET6
785109771Sfjoe	struct sockaddr_in6 *sin6;
786109771Sfjoe#endif
787109771Sfjoe
788109771Sfjoe	switch(sa->sa_family) {
789109771Sfjoe	case AF_LINK:
790109771Sfjoe		/*
791109771Sfjoe		* No mapping needed. Just check that it's a valid MC address.
792109771Sfjoe		*/
793109771Sfjoe		sdl = (struct sockaddr_dl *)sa;
794109771Sfjoe		if (*LLADDR(sdl) != arcbroadcastaddr)
795109771Sfjoe			return EADDRNOTAVAIL;
796109771Sfjoe		*llsa = 0;
797109771Sfjoe		return 0;
798109771Sfjoe#ifdef INET
799109771Sfjoe	case AF_INET:
800109771Sfjoe		sin = (struct sockaddr_in *)sa;
801109771Sfjoe		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
802109771Sfjoe			return EADDRNOTAVAIL;
803109771Sfjoe		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
804109771Sfjoe		       M_ZERO);
805109771Sfjoe		sdl->sdl_len = sizeof *sdl;
806109771Sfjoe		sdl->sdl_family = AF_LINK;
807109771Sfjoe		sdl->sdl_index = ifp->if_index;
808109771Sfjoe		sdl->sdl_type = IFT_ARCNET;
809109771Sfjoe		sdl->sdl_alen = ARC_ADDR_LEN;
810109771Sfjoe		*LLADDR(sdl) = 0;
811109771Sfjoe		*llsa = (struct sockaddr *)sdl;
812109771Sfjoe		return 0;
813109771Sfjoe#endif
814109771Sfjoe#ifdef INET6
815109771Sfjoe	case AF_INET6:
816109771Sfjoe		sin6 = (struct sockaddr_in6 *)sa;
817109771Sfjoe		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
818109771Sfjoe			/*
819109771Sfjoe			 * An IP6 address of 0 means listen to all
820109771Sfjoe			 * of the Ethernet multicast address used for IP6.
821109771Sfjoe			 * (This is used for multicast routers.)
822109771Sfjoe			 */
823109771Sfjoe			ifp->if_flags |= IFF_ALLMULTI;
824109771Sfjoe			*llsa = 0;
825109771Sfjoe			return 0;
82689099Sfjoe		}
827109771Sfjoe		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
828109771Sfjoe			return EADDRNOTAVAIL;
829109771Sfjoe		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
830109771Sfjoe		       M_ZERO);
831109771Sfjoe		sdl->sdl_len = sizeof *sdl;
832109771Sfjoe		sdl->sdl_family = AF_LINK;
833109771Sfjoe		sdl->sdl_index = ifp->if_index;
834109771Sfjoe		sdl->sdl_type = IFT_ARCNET;
835109771Sfjoe		sdl->sdl_alen = ARC_ADDR_LEN;
836109771Sfjoe		*LLADDR(sdl) = 0;
837109771Sfjoe		*llsa = (struct sockaddr *)sdl;
838109771Sfjoe		return 0;
83989099Sfjoe#endif
840109771Sfjoe
841109771Sfjoe	default:
842109771Sfjoe		/*
843109771Sfjoe		 * Well, the text isn't quite right, but it's the name
844109771Sfjoe		 * that counts...
845109771Sfjoe		 */
846109771Sfjoe		return EAFNOSUPPORT;
84789099Sfjoe	}
84889099Sfjoe}
849