if_arcsubr.c revision 122702
189099Sfjoe/*	$NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $	*/
289099Sfjoe/*	$FreeBSD: head/sys/net/if_arcsubr.c 122702 2003-11-14 21:02:22Z andre $ */
389099Sfjoe
489099Sfjoe/*
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>
4889099Sfjoe#include <sys/malloc.h>
4989099Sfjoe#include <sys/mbuf.h>
5089099Sfjoe#include <sys/protosw.h>
5189099Sfjoe#include <sys/socket.h>
5289099Sfjoe#include <sys/sockio.h>
5389099Sfjoe#include <sys/errno.h>
5489099Sfjoe#include <sys/syslog.h>
5589099Sfjoe
5689099Sfjoe#include <machine/cpu.h>
5789099Sfjoe
5889099Sfjoe#include <net/if.h>
5989099Sfjoe#include <net/netisr.h>
6089099Sfjoe#include <net/route.h>
6189099Sfjoe#include <net/if_dl.h>
6289099Sfjoe#include <net/if_types.h>
6389099Sfjoe#include <net/if_arc.h>
6489099Sfjoe#include <net/if_arp.h>
6589099Sfjoe#include <net/bpf.h>
6689099Sfjoe
6789099Sfjoe#if defined(INET) || defined(INET6)
6889099Sfjoe#include <netinet/in.h>
6989099Sfjoe#include <netinet/in_var.h>
7089099Sfjoe#include <netinet/if_ether.h>
7189099Sfjoe#endif
7289099Sfjoe
7389099Sfjoe#ifdef INET6
7489099Sfjoe#include <netinet6/nd6.h>
7589099Sfjoe#endif
7689099Sfjoe
77109771Sfjoe#ifdef IPX
78109771Sfjoe#include <netipx/ipx.h>
79109771Sfjoe#include <netipx/ipx_if.h>
80109771Sfjoe#endif
81109771Sfjoe
8289099SfjoeMODULE_VERSION(arcnet, 1);
8389099Sfjoe
8489099Sfjoe#define ARCNET_ALLOW_BROKEN_ARP
8589099Sfjoe
8692725Salfredstatic struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
87109771Sfjoestatic int arc_resolvemulti(struct ifnet *, struct sockaddr **,
88109771Sfjoe			    struct sockaddr *);
8989099Sfjoe
9089099Sfjoeu_int8_t  arcbroadcastaddr = 0;
9189099Sfjoe
92110106Sfjoe#define ARC_LLADDR(ifp)	(*(u_int8_t *)IF_LLADDR(ifp))
93110106Sfjoe
9489099Sfjoe#define senderr(e) { error = (e); goto bad;}
95109771Sfjoe#define SIN(s)	((struct sockaddr_in *)s)
96109771Sfjoe#define SIPX(s)	((struct sockaddr_ipx *)s)
9789099Sfjoe
9889099Sfjoe/*
9989099Sfjoe * ARCnet output routine.
10089099Sfjoe * Encapsulate a packet of type family for the local net.
10189099Sfjoe * Assumes that ifp is actually pointer to arccom structure.
10289099Sfjoe */
10389099Sfjoeint
10489099Sfjoearc_output(ifp, m, dst, rt0)
10589099Sfjoe	struct ifnet *ifp;
10689099Sfjoe	struct mbuf *m;
10789099Sfjoe	struct sockaddr *dst;
10889099Sfjoe	struct rtentry *rt0;
10989099Sfjoe{
11089099Sfjoe	struct rtentry		*rt;
11189099Sfjoe	struct arccom		*ac;
11289099Sfjoe	struct arc_header	*ah;
11389099Sfjoe	int			error;
11489099Sfjoe	u_int8_t		atype, adst;
115109771Sfjoe	int			loop_copy = 0;
116110106Sfjoe	int			isphds;
11789099Sfjoe
11889099Sfjoe	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
11989099Sfjoe		return(ENETDOWN); /* m, m1 aren't initialized yet */
12089099Sfjoe
12189099Sfjoe	error = 0;
12289099Sfjoe	ac = (struct arccom *)ifp;
12389099Sfjoe
124111767Smdodd	error = rt_check(&rt, &rt0, dst);
125111767Smdodd	if (error)
126111767Smdodd		goto bad;
12789099Sfjoe
12889099Sfjoe	switch (dst->sa_family) {
12989099Sfjoe#ifdef INET
13089099Sfjoe	case AF_INET:
13189099Sfjoe
13289099Sfjoe		/*
13389099Sfjoe		 * For now, use the simple IP addr -> ARCnet addr mapping
13489099Sfjoe		 */
13589099Sfjoe		if (m->m_flags & (M_BCAST|M_MCAST))
13689099Sfjoe			adst = arcbroadcastaddr; /* ARCnet broadcast address */
13789099Sfjoe		else if (ifp->if_flags & IFF_NOARP)
13889099Sfjoe			adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
13989099Sfjoe		else if (!arpresolve(ifp, rt, m, dst, &adst, rt0))
14089099Sfjoe			return 0;	/* not resolved yet */
14189099Sfjoe
14289099Sfjoe		atype = (ifp->if_flags & IFF_LINK0) ?
14389099Sfjoe			ARCTYPE_IP_OLD : ARCTYPE_IP;
14489099Sfjoe		break;
14589099Sfjoe#endif
14689099Sfjoe#ifdef INET6
14789099Sfjoe	case AF_INET6:
14889099Sfjoe#ifdef OLDIP6OUTPUT
14989099Sfjoe		if (!nd6_resolve(ifp, rt, m, dst, (u_char *)&adst))
15089099Sfjoe			return(0);	/* if not yet resolves */
15189099Sfjoe#else
15289099Sfjoe		if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)&adst))
15389099Sfjoe			return(0); /* it must be impossible, but... */
15489099Sfjoe#endif /* OLDIP6OUTPUT */
15589099Sfjoe		atype = ARCTYPE_INET6;
15689099Sfjoe		break;
15789099Sfjoe#endif
158109771Sfjoe#ifdef IPX
159109771Sfjoe	case AF_IPX:
160109771Sfjoe		adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
161109771Sfjoe		atype = ARCTYPE_IPX;
162109771Sfjoe		if (adst == 0xff)
163109771Sfjoe			adst = arcbroadcastaddr;
164109771Sfjoe		break;
165109771Sfjoe#endif
16689099Sfjoe
16789099Sfjoe	case AF_UNSPEC:
168109771Sfjoe		loop_copy = -1;
16989099Sfjoe		ah = (struct arc_header *)dst->sa_data;
17089099Sfjoe		adst = ah->arc_dhost;
17189099Sfjoe		atype = ah->arc_type;
17289099Sfjoe
17389099Sfjoe		if (atype == ARCTYPE_ARP) {
17489099Sfjoe			atype = (ifp->if_flags & IFF_LINK0) ?
17589099Sfjoe			    ARCTYPE_ARP_OLD: ARCTYPE_ARP;
17689099Sfjoe
17789099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
17889099Sfjoe			/*
17989099Sfjoe			 * XXX It's not clear per RFC826 if this is needed, but
18089099Sfjoe			 * "assigned numbers" say this is wrong.
18189099Sfjoe			 * However, e.g., AmiTCP 3.0Beta used it... we make this
18289099Sfjoe			 * switchable for emergency cases. Not perfect, but...
18389099Sfjoe			 */
18489099Sfjoe			if (ifp->if_flags & IFF_LINK2)
185109771Sfjoe				mtod(m, struct arphdr *)->ar_pro = atype - 1;
18689099Sfjoe#endif
18789099Sfjoe		}
18889099Sfjoe		break;
18989099Sfjoe
19089099Sfjoe	default:
191105598Sbrooks		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
19289099Sfjoe		senderr(EAFNOSUPPORT);
19389099Sfjoe	}
19489099Sfjoe
195110106Sfjoe	isphds = arc_isphds(atype);
196111119Simp	M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_DONTWAIT);
19789099Sfjoe	if (m == 0)
19889099Sfjoe		senderr(ENOBUFS);
19989099Sfjoe	ah = mtod(m, struct arc_header *);
20089099Sfjoe	ah->arc_type = atype;
20189099Sfjoe	ah->arc_dhost = adst;
202110106Sfjoe	ah->arc_shost = ARC_LLADDR(ifp);
203110106Sfjoe	if (isphds) {
204110106Sfjoe		ah->arc_flag = 0;
205110106Sfjoe		ah->arc_seqid = 0;
206110106Sfjoe	}
20789099Sfjoe
208109771Sfjoe	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
209109771Sfjoe		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
210109771Sfjoe			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
211109771Sfjoe
212109771Sfjoe			(void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
213109771Sfjoe		} else if (ah->arc_dhost == ah->arc_shost) {
214109771Sfjoe			(void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
215109771Sfjoe			return (0);     /* XXX */
216109771Sfjoe		}
217109771Sfjoe	}
218109771Sfjoe
219106939Ssam	BPF_MTAP(ifp, m);
22089099Sfjoe
22189099Sfjoe	if (!IF_HANDOFF(&ifp->if_snd, m, ifp)) {
22289099Sfjoe		m = 0;
22389099Sfjoe		senderr(ENOBUFS);
22489099Sfjoe	}
22589099Sfjoe
22689099Sfjoe	return (error);
22789099Sfjoe
22889099Sfjoebad:
22989099Sfjoe	if (m)
23089099Sfjoe		m_freem(m);
23189099Sfjoe	return (error);
23289099Sfjoe}
23389099Sfjoe
23489099Sfjoevoid
23589099Sfjoearc_frag_init(ifp)
23689099Sfjoe	struct ifnet *ifp;
23789099Sfjoe{
23889099Sfjoe	struct arccom *ac;
23989099Sfjoe
24089099Sfjoe	ac = (struct arccom *)ifp;
24189099Sfjoe	ac->curr_frag = 0;
24289099Sfjoe}
24389099Sfjoe
24489099Sfjoestruct mbuf *
24589099Sfjoearc_frag_next(ifp)
24689099Sfjoe	struct ifnet *ifp;
24789099Sfjoe{
24889099Sfjoe	struct arccom *ac;
24989099Sfjoe	struct mbuf *m;
25089099Sfjoe	struct arc_header *ah;
25189099Sfjoe
25289099Sfjoe	ac = (struct arccom *)ifp;
25389099Sfjoe	if ((m = ac->curr_frag) == 0) {
25489099Sfjoe		int tfrags;
25589099Sfjoe
25689099Sfjoe		/* dequeue new packet */
25789099Sfjoe		IF_DEQUEUE(&ifp->if_snd, m);
25889099Sfjoe		if (m == 0)
25989099Sfjoe			return 0;
26089099Sfjoe
26189099Sfjoe		ah = mtod(m, struct arc_header *);
26289099Sfjoe		if (!arc_isphds(ah->arc_type))
26389099Sfjoe			return m;
26489099Sfjoe
26589099Sfjoe		++ac->ac_seqid;		/* make the seqid unique */
266109771Sfjoe		tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
26789099Sfjoe		ac->fsflag = 2 * tfrags - 3;
26889099Sfjoe		ac->sflag = 0;
26989099Sfjoe		ac->rsflag = ac->fsflag;
27089099Sfjoe		ac->arc_dhost = ah->arc_dhost;
27189099Sfjoe		ac->arc_shost = ah->arc_shost;
27289099Sfjoe		ac->arc_type = ah->arc_type;
27389099Sfjoe
274110106Sfjoe		m_adj(m, ARC_HDRNEWLEN);
27589099Sfjoe		ac->curr_frag = m;
27689099Sfjoe	}
27789099Sfjoe
27889099Sfjoe	/* split out next fragment and return it */
27989099Sfjoe	if (ac->sflag < ac->fsflag) {
28089099Sfjoe		/* we CAN'T have short packets here */
281111119Simp		ac->curr_frag = m_split(m, ARC_MAX_DATA, M_DONTWAIT);
28289099Sfjoe		if (ac->curr_frag == 0) {
28393750Sluigi			m_freem(m);
28489099Sfjoe			return 0;
28589099Sfjoe		}
28689099Sfjoe
287111119Simp		M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
28889099Sfjoe		if (m == 0) {
28993750Sluigi			m_freem(ac->curr_frag);
29089099Sfjoe			ac->curr_frag = 0;
29189099Sfjoe			return 0;
29289099Sfjoe		}
29389099Sfjoe
29489099Sfjoe		ah = mtod(m, struct arc_header *);
29589099Sfjoe		ah->arc_flag = ac->rsflag;
29689099Sfjoe		ah->arc_seqid = ac->ac_seqid;
29789099Sfjoe
29889099Sfjoe		ac->sflag += 2;
29989099Sfjoe		ac->rsflag = ac->sflag;
30089099Sfjoe	} else if ((m->m_pkthdr.len >=
30189099Sfjoe	    ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
30289099Sfjoe	    (m->m_pkthdr.len <=
30389099Sfjoe	    ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
30489099Sfjoe		ac->curr_frag = 0;
30589099Sfjoe
306111119Simp		M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
30789099Sfjoe		if (m == 0)
30889099Sfjoe			return 0;
30989099Sfjoe
31089099Sfjoe		ah = mtod(m, struct arc_header *);
31189099Sfjoe		ah->arc_flag = 0xFF;
31289099Sfjoe		ah->arc_seqid = 0xFFFF;
31389099Sfjoe		ah->arc_type2 = ac->arc_type;
31489099Sfjoe		ah->arc_flag2 = ac->sflag;
31589099Sfjoe		ah->arc_seqid2 = ac->ac_seqid;
31689099Sfjoe	} else {
31789099Sfjoe		ac->curr_frag = 0;
31889099Sfjoe
319111119Simp		M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
32089099Sfjoe		if (m == 0)
32189099Sfjoe			return 0;
32289099Sfjoe
32389099Sfjoe		ah = mtod(m, struct arc_header *);
32489099Sfjoe		ah->arc_flag = ac->sflag;
32589099Sfjoe		ah->arc_seqid = ac->ac_seqid;
32689099Sfjoe	}
32789099Sfjoe
32889099Sfjoe	ah->arc_dhost = ac->arc_dhost;
32989099Sfjoe	ah->arc_shost = ac->arc_shost;
33089099Sfjoe	ah->arc_type = ac->arc_type;
33189099Sfjoe
33289099Sfjoe	return m;
33389099Sfjoe}
33489099Sfjoe
33589099Sfjoe/*
33689099Sfjoe * Defragmenter. Returns mbuf if last packet found, else
33789099Sfjoe * NULL. frees imcoming mbuf as necessary.
33889099Sfjoe */
33989099Sfjoe
340105228Sphkstatic __inline struct mbuf *
34189099Sfjoearc_defrag(ifp, m)
34289099Sfjoe	struct ifnet *ifp;
34389099Sfjoe	struct mbuf *m;
34489099Sfjoe{
34589099Sfjoe	struct arc_header *ah, *ah1;
34689099Sfjoe	struct arccom *ac;
34789099Sfjoe	struct ac_frag *af;
34889099Sfjoe	struct mbuf *m1;
34989099Sfjoe	char *s;
35089099Sfjoe	int newflen;
35189099Sfjoe	u_char src,dst,typ;
35289099Sfjoe
35389099Sfjoe	ac = (struct arccom *)ifp;
35489099Sfjoe
35589099Sfjoe	if (m->m_len < ARC_HDRNEWLEN) {
35689099Sfjoe		m = m_pullup(m, ARC_HDRNEWLEN);
35789099Sfjoe		if (m == NULL) {
35889099Sfjoe			++ifp->if_ierrors;
35989099Sfjoe			return NULL;
36089099Sfjoe		}
36189099Sfjoe	}
36289099Sfjoe
36389099Sfjoe	ah = mtod(m, struct arc_header *);
36489099Sfjoe	typ = ah->arc_type;
36589099Sfjoe
36689099Sfjoe	if (!arc_isphds(typ))
36789099Sfjoe		return m;
36889099Sfjoe
36989099Sfjoe	src = ah->arc_shost;
37089099Sfjoe	dst = ah->arc_dhost;
37189099Sfjoe
37289099Sfjoe	if (ah->arc_flag == 0xff) {
37389099Sfjoe		m_adj(m, 4);
37489099Sfjoe
37589099Sfjoe		if (m->m_len < ARC_HDRNEWLEN) {
37689099Sfjoe			m = m_pullup(m, ARC_HDRNEWLEN);
37789099Sfjoe			if (m == NULL) {
37889099Sfjoe				++ifp->if_ierrors;
37989099Sfjoe				return NULL;
38089099Sfjoe			}
38189099Sfjoe		}
38289099Sfjoe
38389099Sfjoe		ah = mtod(m, struct arc_header *);
38489099Sfjoe	}
38589099Sfjoe
38689099Sfjoe	af = &ac->ac_fragtab[src];
38789099Sfjoe	m1 = af->af_packet;
38889099Sfjoe	s = "debug code error";
38989099Sfjoe
39089099Sfjoe	if (ah->arc_flag & 1) {
39189099Sfjoe		/*
39289099Sfjoe		 * first fragment. We always initialize, which is
39389099Sfjoe		 * about the right thing to do, as we only want to
39489099Sfjoe		 * accept one fragmented packet per src at a time.
39589099Sfjoe		 */
39689099Sfjoe		if (m1 != NULL)
39789099Sfjoe			m_freem(m1);
39889099Sfjoe
39989099Sfjoe		af->af_packet = m;
40089099Sfjoe		m1 = m;
40189099Sfjoe		af->af_maxflag = ah->arc_flag;
40289099Sfjoe		af->af_lastseen = 0;
40389099Sfjoe		af->af_seqid = ah->arc_seqid;
40489099Sfjoe
40589099Sfjoe		return NULL;
40689099Sfjoe		/* notreached */
40789099Sfjoe	} else {
40889099Sfjoe		/* check for unfragmented packet */
40989099Sfjoe		if (ah->arc_flag == 0)
41089099Sfjoe			return m;
41189099Sfjoe
41289099Sfjoe		/* do we have a first packet from that src? */
41389099Sfjoe		if (m1 == NULL) {
41489099Sfjoe			s = "no first frag";
41589099Sfjoe			goto outofseq;
41689099Sfjoe		}
41789099Sfjoe
41889099Sfjoe		ah1 = mtod(m1, struct arc_header *);
41989099Sfjoe
42089099Sfjoe		if (ah->arc_seqid != ah1->arc_seqid) {
42189099Sfjoe			s = "seqid differs";
42289099Sfjoe			goto outofseq;
42389099Sfjoe		}
42489099Sfjoe
42589099Sfjoe		if (typ != ah1->arc_type) {
42689099Sfjoe			s = "type differs";
42789099Sfjoe			goto outofseq;
42889099Sfjoe		}
42989099Sfjoe
43089099Sfjoe		if (dst != ah1->arc_dhost) {
43189099Sfjoe			s = "dest host differs";
43289099Sfjoe			goto outofseq;
43389099Sfjoe		}
43489099Sfjoe
43589099Sfjoe		/* typ, seqid and dst are ok here. */
43689099Sfjoe
43789099Sfjoe		if (ah->arc_flag == af->af_lastseen) {
43889099Sfjoe			m_freem(m);
43989099Sfjoe			return NULL;
44089099Sfjoe		}
44189099Sfjoe
44289099Sfjoe		if (ah->arc_flag == af->af_lastseen + 2) {
44389099Sfjoe			/* ok, this is next fragment */
44489099Sfjoe			af->af_lastseen = ah->arc_flag;
44589099Sfjoe			m_adj(m,ARC_HDRNEWLEN);
44689099Sfjoe
44789099Sfjoe			/*
44889099Sfjoe			 * m_cat might free the first mbuf (with pkthdr)
44989099Sfjoe			 * in 2nd chain; therefore:
45089099Sfjoe			 */
45189099Sfjoe
45289099Sfjoe			newflen = m->m_pkthdr.len;
45389099Sfjoe
45489099Sfjoe			m_cat(m1,m);
45589099Sfjoe
45689099Sfjoe			m1->m_pkthdr.len += newflen;
45789099Sfjoe
45889099Sfjoe			/* is it the last one? */
45989099Sfjoe			if (af->af_lastseen > af->af_maxflag) {
46089099Sfjoe				af->af_packet = NULL;
46189099Sfjoe				return(m1);
46289099Sfjoe			} else
46389099Sfjoe				return NULL;
46489099Sfjoe		}
46589099Sfjoe		s = "other reason";
46689099Sfjoe		/* if all else fails, it is out of sequence, too */
46789099Sfjoe	}
46889099Sfjoeoutofseq:
46989099Sfjoe	if (m1) {
47089099Sfjoe		m_freem(m1);
47189099Sfjoe		af->af_packet = NULL;
47289099Sfjoe	}
47389099Sfjoe
47489099Sfjoe	if (m)
47589099Sfjoe		m_freem(m);
47689099Sfjoe
477121816Sbrooks	log(LOG_INFO,"%s: got out of seq. packet: %s\n",
478121816Sbrooks	    ifp->if_xname, s);
47989099Sfjoe
48089099Sfjoe	return NULL;
48189099Sfjoe}
48289099Sfjoe
48389099Sfjoe/*
48489099Sfjoe * return 1 if Packet Header Definition Standard, else 0.
48589099Sfjoe * For now: old IP, old ARP aren't obviously. Lacking correct information,
48689099Sfjoe * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
48789099Sfjoe * (Apple and Novell corporations were involved, among others, in PHDS work).
48889099Sfjoe * Easiest is to assume that everybody else uses that, too.
48989099Sfjoe */
49089099Sfjoeint
49189099Sfjoearc_isphds(type)
49289099Sfjoe	u_int8_t type;
49389099Sfjoe{
49489099Sfjoe	return (type != ARCTYPE_IP_OLD &&
49589099Sfjoe		type != ARCTYPE_ARP_OLD &&
49689099Sfjoe		type != ARCTYPE_DIAGNOSE);
49789099Sfjoe}
49889099Sfjoe
49989099Sfjoe/*
50089099Sfjoe * Process a received Arcnet packet;
50189099Sfjoe * the packet is in the mbuf chain m with
50289099Sfjoe * the ARCnet header.
50389099Sfjoe */
50489099Sfjoevoid
50589099Sfjoearc_input(ifp, m)
50689099Sfjoe	struct ifnet *ifp;
50789099Sfjoe	struct mbuf *m;
50889099Sfjoe{
50989099Sfjoe	struct arc_header *ah;
510111888Sjlemon	int isr;
51189099Sfjoe	u_int8_t atype;
51289099Sfjoe
51389099Sfjoe	if ((ifp->if_flags & IFF_UP) == 0) {
51489099Sfjoe		m_freem(m);
51589099Sfjoe		return;
51689099Sfjoe	}
51789099Sfjoe
51889099Sfjoe	/* possibly defragment: */
51989099Sfjoe	m = arc_defrag(ifp, m);
52089099Sfjoe	if (m == NULL)
52189099Sfjoe		return;
52289099Sfjoe
523106939Ssam	BPF_MTAP(ifp, m);
52489099Sfjoe
52589099Sfjoe	ah = mtod(m, struct arc_header *);
526109771Sfjoe	/* does this belong to us? */
527110106Sfjoe	if ((ifp->if_flags & IFF_PROMISC) == 0
528109771Sfjoe	    && ah->arc_dhost != arcbroadcastaddr
529110106Sfjoe	    && ah->arc_dhost != ARC_LLADDR(ifp)) {
530109771Sfjoe		m_freem(m);
531109771Sfjoe		return;
532109771Sfjoe	}
53389099Sfjoe
53489099Sfjoe	ifp->if_ibytes += m->m_pkthdr.len;
53589099Sfjoe
536109771Sfjoe	if (ah->arc_dhost == arcbroadcastaddr) {
53789099Sfjoe		m->m_flags |= M_BCAST|M_MCAST;
53889099Sfjoe		ifp->if_imcasts++;
53989099Sfjoe	}
54089099Sfjoe
54189099Sfjoe	atype = ah->arc_type;
54289099Sfjoe	switch (atype) {
54389099Sfjoe#ifdef INET
54489099Sfjoe	case ARCTYPE_IP:
54589099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
546122702Sandre		if (ip_fastforward(m))
547109771Sfjoe			return;
548111888Sjlemon		isr = NETISR_IP;
54989099Sfjoe		break;
55089099Sfjoe
55189099Sfjoe	case ARCTYPE_IP_OLD:
55289099Sfjoe		m_adj(m, ARC_HDRLEN);
553122702Sandre		if (ip_fastforward(m))
554109771Sfjoe			return;
555111888Sjlemon		isr = NETISR_IP;
55689099Sfjoe		break;
55789099Sfjoe
55889099Sfjoe	case ARCTYPE_ARP:
55989099Sfjoe		if (ifp->if_flags & IFF_NOARP) {
56089099Sfjoe			/* Discard packet if ARP is disabled on interface */
56189099Sfjoe			m_freem(m);
56289099Sfjoe			return;
56389099Sfjoe		}
56489099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
565111888Sjlemon		isr = NETISR_ARP;
56689099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
56789099Sfjoe		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
56889099Sfjoe#endif
56989099Sfjoe		break;
57089099Sfjoe
57189099Sfjoe	case ARCTYPE_ARP_OLD:
57289099Sfjoe		if (ifp->if_flags & IFF_NOARP) {
57389099Sfjoe			/* Discard packet if ARP is disabled on interface */
57489099Sfjoe			m_freem(m);
57589099Sfjoe			return;
57689099Sfjoe		}
57789099Sfjoe		m_adj(m, ARC_HDRLEN);
578111888Sjlemon		isr = NETISR_ARP;
57989099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
58089099Sfjoe		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
58189099Sfjoe#endif
58289099Sfjoe		break;
58389099Sfjoe#endif
58489099Sfjoe#ifdef INET6
58589099Sfjoe	case ARCTYPE_INET6:
58689099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
587111888Sjlemon		isr = NETISR_IPV6;
58889099Sfjoe		break;
58989099Sfjoe#endif
590109771Sfjoe#ifdef IPX
591109771Sfjoe	case ARCTYPE_IPX:
592109771Sfjoe		m_adj(m, ARC_HDRNEWLEN);
593111888Sjlemon		isr = NETISR_IPX;
594109771Sfjoe		break;
595109771Sfjoe#endif
59689099Sfjoe	default:
59789099Sfjoe		m_freem(m);
59889099Sfjoe		return;
59989099Sfjoe	}
600111888Sjlemon	netisr_dispatch(isr, m);
60189099Sfjoe}
60289099Sfjoe
60389099Sfjoe/*
60489099Sfjoe * Register (new) link level address.
60589099Sfjoe */
60689099Sfjoevoid
60789099Sfjoearc_storelladdr(ifp, lla)
60889099Sfjoe	struct ifnet *ifp;
60989099Sfjoe	u_int8_t lla;
61089099Sfjoe{
611110106Sfjoe	ARC_LLADDR(ifp) = lla;
61289099Sfjoe}
61389099Sfjoe
61489099Sfjoe/*
61589099Sfjoe * Perform common duties while attaching to interface list
61689099Sfjoe */
61789099Sfjoevoid
61889099Sfjoearc_ifattach(ifp, lla)
61989099Sfjoe	struct ifnet *ifp;
62089099Sfjoe	u_int8_t lla;
62189099Sfjoe{
62289099Sfjoe	struct ifaddr *ifa;
62389099Sfjoe	struct sockaddr_dl *sdl;
62489099Sfjoe	struct arccom *ac;
62589099Sfjoe
62689099Sfjoe	if_attach(ifp);
62789099Sfjoe	ifp->if_type = IFT_ARCNET;
62889099Sfjoe	ifp->if_addrlen = 1;
62989099Sfjoe	ifp->if_hdrlen = ARC_HDRLEN;
63089099Sfjoe	ifp->if_mtu = 1500;
631109771Sfjoe	ifp->if_resolvemulti = arc_resolvemulti;
63289099Sfjoe	if (ifp->if_baudrate == 0)
63389099Sfjoe		ifp->if_baudrate = 2500000;
63489099Sfjoe#if __FreeBSD_version < 500000
63589099Sfjoe	ifa = ifnet_addrs[ifp->if_index - 1];
63689099Sfjoe#else
63789099Sfjoe	ifa = ifaddr_byindex(ifp->if_index);
63889099Sfjoe#endif
63989099Sfjoe	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
64089099Sfjoe	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
64189099Sfjoe	sdl->sdl_type = IFT_ARCNET;
64289099Sfjoe	sdl->sdl_alen = ifp->if_addrlen;
64389099Sfjoe
64489099Sfjoe	if (ifp->if_flags & IFF_BROADCAST)
64589099Sfjoe		ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
64689099Sfjoe
64789099Sfjoe	ac = (struct arccom *)ifp;
64889099Sfjoe	ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
64989099Sfjoe	if (lla == 0) {
65089099Sfjoe		/* XXX this message isn't entirely clear, to me -- cgd */
651121816Sbrooks		log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
652121816Sbrooks		   ifp->if_xname, ifp->if_xname);
65389099Sfjoe	}
65489099Sfjoe	arc_storelladdr(ifp, lla);
65589099Sfjoe
65689099Sfjoe	ifp->if_broadcastaddr = &arcbroadcastaddr;
65789099Sfjoe
65889099Sfjoe	bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
65989099Sfjoe}
66089099Sfjoe
66189099Sfjoevoid
66289099Sfjoearc_ifdetach(ifp)
66389099Sfjoe	struct ifnet *ifp;
66489099Sfjoe{
66589099Sfjoe	bpfdetach(ifp);
66689099Sfjoe	if_detach(ifp);
66789099Sfjoe}
66889099Sfjoe
66989099Sfjoeint
67089099Sfjoearc_ioctl(ifp, command, data)
67189099Sfjoe	struct ifnet *ifp;
67289099Sfjoe	int command;
67389099Sfjoe	caddr_t data;
67489099Sfjoe{
67589099Sfjoe	struct ifaddr *ifa = (struct ifaddr *) data;
67689099Sfjoe	struct ifreq *ifr = (struct ifreq *) data;
67789099Sfjoe	int error = 0;
67889099Sfjoe
67989099Sfjoe	switch (command) {
68089099Sfjoe	case SIOCSIFADDR:
68189099Sfjoe		ifp->if_flags |= IFF_UP;
68289099Sfjoe		switch (ifa->ifa_addr->sa_family) {
68389099Sfjoe#ifdef INET
68489099Sfjoe		case AF_INET:
68589099Sfjoe			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
68689099Sfjoe			arp_ifinit(ifp, ifa);
68789099Sfjoe			break;
68889099Sfjoe#endif
689109771Sfjoe#ifdef IPX
690109771Sfjoe		/*
691109771Sfjoe		 * XXX This code is probably wrong
692109771Sfjoe		 */
693109771Sfjoe		case AF_IPX:
694109771Sfjoe		{
695109771Sfjoe			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
696109771Sfjoe
697109771Sfjoe			if (ipx_nullhost(*ina))
698110106Sfjoe				ina->x_host.c_host[5] = ARC_LLADDR(ifp);
699109771Sfjoe			else
700109771Sfjoe				arc_storelladdr(ifp, ina->x_host.c_host[5]);
701109771Sfjoe
702109771Sfjoe			/*
703109771Sfjoe			 * Set new address
704109771Sfjoe			 */
705109771Sfjoe			ifp->if_init(ifp->if_softc);
706109771Sfjoe			break;
707109771Sfjoe		}
708109771Sfjoe#endif
70989099Sfjoe		default:
71089099Sfjoe			ifp->if_init(ifp->if_softc);
71189099Sfjoe			break;
71289099Sfjoe		}
71389099Sfjoe		break;
71489099Sfjoe
715109771Sfjoe	case SIOCGIFADDR:
716109771Sfjoe		{
717109771Sfjoe			struct sockaddr *sa;
718109771Sfjoe
719109771Sfjoe			sa = (struct sockaddr *) &ifr->ifr_data;
720110106Sfjoe			*(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
721109771Sfjoe		}
722109771Sfjoe		break;
723109771Sfjoe
72489099Sfjoe	case SIOCADDMULTI:
72589099Sfjoe	case SIOCDELMULTI:
72689099Sfjoe		if (ifr == NULL)
72789099Sfjoe			error = EAFNOSUPPORT;
72889099Sfjoe		else {
72989099Sfjoe			switch (ifr->ifr_addr.sa_family) {
73089099Sfjoe			case AF_INET:
73189099Sfjoe			case AF_INET6:
73289099Sfjoe				error = 0;
73389099Sfjoe				break;
73489099Sfjoe			default:
73589099Sfjoe				error = EAFNOSUPPORT;
73689099Sfjoe				break;
73789099Sfjoe			}
73889099Sfjoe		}
73989099Sfjoe		break;
74089099Sfjoe
74189099Sfjoe	case SIOCSIFMTU:
74289099Sfjoe		/*
74389099Sfjoe		 * Set the interface MTU.
74489099Sfjoe		 * mtu can't be larger than ARCMTU for RFC1051
74589099Sfjoe		 * and can't be larger than ARC_PHDS_MTU
74689099Sfjoe		 */
74789099Sfjoe		if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
74889099Sfjoe		    ifr->ifr_mtu > ARC_PHDS_MAXMTU)
74989099Sfjoe			error = EINVAL;
75089099Sfjoe		else
75189099Sfjoe			ifp->if_mtu = ifr->ifr_mtu;
75289099Sfjoe		break;
753109771Sfjoe	}
75489099Sfjoe
755109771Sfjoe	return (error);
756109771Sfjoe}
75789099Sfjoe
758109771Sfjoe/* based on ether_resolvemulti() */
759109771Sfjoeint
760109771Sfjoearc_resolvemulti(ifp, llsa, sa)
761109771Sfjoe	struct ifnet *ifp;
762109771Sfjoe	struct sockaddr **llsa;
763109771Sfjoe	struct sockaddr *sa;
764109771Sfjoe{
765109771Sfjoe	struct sockaddr_dl *sdl;
766109771Sfjoe	struct sockaddr_in *sin;
767109771Sfjoe#ifdef INET6
768109771Sfjoe	struct sockaddr_in6 *sin6;
769109771Sfjoe#endif
770109771Sfjoe
771109771Sfjoe	switch(sa->sa_family) {
772109771Sfjoe	case AF_LINK:
773109771Sfjoe		/*
774109771Sfjoe		* No mapping needed. Just check that it's a valid MC address.
775109771Sfjoe		*/
776109771Sfjoe		sdl = (struct sockaddr_dl *)sa;
777109771Sfjoe		if (*LLADDR(sdl) != arcbroadcastaddr)
778109771Sfjoe			return EADDRNOTAVAIL;
779109771Sfjoe		*llsa = 0;
780109771Sfjoe		return 0;
781109771Sfjoe#ifdef INET
782109771Sfjoe	case AF_INET:
783109771Sfjoe		sin = (struct sockaddr_in *)sa;
784109771Sfjoe		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
785109771Sfjoe			return EADDRNOTAVAIL;
786109771Sfjoe		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
787109771Sfjoe		       M_ZERO);
788109771Sfjoe		sdl->sdl_len = sizeof *sdl;
789109771Sfjoe		sdl->sdl_family = AF_LINK;
790109771Sfjoe		sdl->sdl_index = ifp->if_index;
791109771Sfjoe		sdl->sdl_type = IFT_ARCNET;
792109771Sfjoe		sdl->sdl_alen = ARC_ADDR_LEN;
793109771Sfjoe		*LLADDR(sdl) = 0;
794109771Sfjoe		*llsa = (struct sockaddr *)sdl;
795109771Sfjoe		return 0;
796109771Sfjoe#endif
797109771Sfjoe#ifdef INET6
798109771Sfjoe	case AF_INET6:
799109771Sfjoe		sin6 = (struct sockaddr_in6 *)sa;
800109771Sfjoe		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
801109771Sfjoe			/*
802109771Sfjoe			 * An IP6 address of 0 means listen to all
803109771Sfjoe			 * of the Ethernet multicast address used for IP6.
804109771Sfjoe			 * (This is used for multicast routers.)
805109771Sfjoe			 */
806109771Sfjoe			ifp->if_flags |= IFF_ALLMULTI;
807109771Sfjoe			*llsa = 0;
808109771Sfjoe			return 0;
80989099Sfjoe		}
810109771Sfjoe		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
811109771Sfjoe			return EADDRNOTAVAIL;
812109771Sfjoe		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
813109771Sfjoe		       M_ZERO);
814109771Sfjoe		sdl->sdl_len = sizeof *sdl;
815109771Sfjoe		sdl->sdl_family = AF_LINK;
816109771Sfjoe		sdl->sdl_index = ifp->if_index;
817109771Sfjoe		sdl->sdl_type = IFT_ARCNET;
818109771Sfjoe		sdl->sdl_alen = ARC_ADDR_LEN;
819109771Sfjoe		*LLADDR(sdl) = 0;
820109771Sfjoe		*llsa = (struct sockaddr *)sdl;
821109771Sfjoe		return 0;
82289099Sfjoe#endif
823109771Sfjoe
824109771Sfjoe	default:
825109771Sfjoe		/*
826109771Sfjoe		 * Well, the text isn't quite right, but it's the name
827109771Sfjoe		 * that counts...
828109771Sfjoe		 */
829109771Sfjoe		return EAFNOSUPPORT;
83089099Sfjoe	}
83189099Sfjoe}
832