if_arcsubr.c revision 128636
189099Sfjoe/*	$NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $	*/
289099Sfjoe/*	$FreeBSD: head/sys/net/if_arcsubr.c 128636 2004-04-25 09:24:52Z luigi $ */
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 arc_header	*ah;
11189099Sfjoe	int			error;
11289099Sfjoe	u_int8_t		atype, adst;
113109771Sfjoe	int			loop_copy = 0;
114110106Sfjoe	int			isphds;
11589099Sfjoe
11689099Sfjoe	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
11789099Sfjoe		return(ENETDOWN); /* m, m1 aren't initialized yet */
11889099Sfjoe
11989099Sfjoe	error = 0;
12089099Sfjoe
12189099Sfjoe	switch (dst->sa_family) {
12289099Sfjoe#ifdef INET
12389099Sfjoe	case AF_INET:
12489099Sfjoe
12589099Sfjoe		/*
12689099Sfjoe		 * For now, use the simple IP addr -> ARCnet addr mapping
12789099Sfjoe		 */
12889099Sfjoe		if (m->m_flags & (M_BCAST|M_MCAST))
12989099Sfjoe			adst = arcbroadcastaddr; /* ARCnet broadcast address */
13089099Sfjoe		else if (ifp->if_flags & IFF_NOARP)
13189099Sfjoe			adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
132128636Sluigi		else {
133128636Sluigi			error = arpresolve(ifp, rt0, m, dst, &adst);
134128636Sluigi			if (error)
135128636Sluigi				return (error == EWOULDBLOCK ? 0 : error);
136128636Sluigi		}
13789099Sfjoe
13889099Sfjoe		atype = (ifp->if_flags & IFF_LINK0) ?
13989099Sfjoe			ARCTYPE_IP_OLD : ARCTYPE_IP;
14089099Sfjoe		break;
141127260Smdodd	case AF_ARP:
142127260Smdodd	{
143127260Smdodd		struct arphdr *ah;
144127260Smdodd		ah = mtod(m, struct arphdr *);
145127260Smdodd		ah->ar_hrd = htons(ARPHRD_ARCNET);
146127260Smdodd
147127260Smdodd		loop_copy = -1; /* if this is for us, don't do it */
148127260Smdodd
149127260Smdodd		switch(ntohs(ah->ar_op)) {
150127260Smdodd		case ARPOP_REVREQUEST:
151127260Smdodd		case ARPOP_REVREPLY:
152127275Smdodd			atype = ARCTYPE_REVARP;
153127260Smdodd			break;
154127260Smdodd		case ARPOP_REQUEST:
155127260Smdodd		case ARPOP_REPLY:
156127260Smdodd		default:
157127275Smdodd			atype = ARCTYPE_ARP;
158127260Smdodd			break;
159127260Smdodd		}
160127260Smdodd
161127260Smdodd		if (m->m_flags & M_BCAST)
162127290Smdodd			bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN);
163127260Smdodd		else
164127290Smdodd			bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN);
165127260Smdodd
166127260Smdodd	}
167127260Smdodd	break;
16889099Sfjoe#endif
16989099Sfjoe#ifdef INET6
17089099Sfjoe	case AF_INET6:
171128636Sluigi		error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)&adst);
172128636Sluigi		if (error)
173128636Sluigi			return (error);
17489099Sfjoe		atype = ARCTYPE_INET6;
17589099Sfjoe		break;
17689099Sfjoe#endif
177109771Sfjoe#ifdef IPX
178109771Sfjoe	case AF_IPX:
179109771Sfjoe		adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
180109771Sfjoe		atype = ARCTYPE_IPX;
181109771Sfjoe		if (adst == 0xff)
182109771Sfjoe			adst = arcbroadcastaddr;
183109771Sfjoe		break;
184109771Sfjoe#endif
18589099Sfjoe
18689099Sfjoe	case AF_UNSPEC:
187109771Sfjoe		loop_copy = -1;
18889099Sfjoe		ah = (struct arc_header *)dst->sa_data;
18989099Sfjoe		adst = ah->arc_dhost;
19089099Sfjoe		atype = ah->arc_type;
19189099Sfjoe
19289099Sfjoe		if (atype == ARCTYPE_ARP) {
19389099Sfjoe			atype = (ifp->if_flags & IFF_LINK0) ?
19489099Sfjoe			    ARCTYPE_ARP_OLD: ARCTYPE_ARP;
19589099Sfjoe
19689099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
19789099Sfjoe			/*
19889099Sfjoe			 * XXX It's not clear per RFC826 if this is needed, but
19989099Sfjoe			 * "assigned numbers" say this is wrong.
20089099Sfjoe			 * However, e.g., AmiTCP 3.0Beta used it... we make this
20189099Sfjoe			 * switchable for emergency cases. Not perfect, but...
20289099Sfjoe			 */
20389099Sfjoe			if (ifp->if_flags & IFF_LINK2)
204109771Sfjoe				mtod(m, struct arphdr *)->ar_pro = atype - 1;
20589099Sfjoe#endif
20689099Sfjoe		}
20789099Sfjoe		break;
20889099Sfjoe
20989099Sfjoe	default:
210105598Sbrooks		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
21189099Sfjoe		senderr(EAFNOSUPPORT);
21289099Sfjoe	}
21389099Sfjoe
214110106Sfjoe	isphds = arc_isphds(atype);
215111119Simp	M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_DONTWAIT);
21689099Sfjoe	if (m == 0)
21789099Sfjoe		senderr(ENOBUFS);
21889099Sfjoe	ah = mtod(m, struct arc_header *);
21989099Sfjoe	ah->arc_type = atype;
22089099Sfjoe	ah->arc_dhost = adst;
221110106Sfjoe	ah->arc_shost = ARC_LLADDR(ifp);
222110106Sfjoe	if (isphds) {
223110106Sfjoe		ah->arc_flag = 0;
224110106Sfjoe		ah->arc_seqid = 0;
225110106Sfjoe	}
22689099Sfjoe
227109771Sfjoe	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
228109771Sfjoe		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
229109771Sfjoe			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
230109771Sfjoe
231109771Sfjoe			(void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
232109771Sfjoe		} else if (ah->arc_dhost == ah->arc_shost) {
233109771Sfjoe			(void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
234109771Sfjoe			return (0);     /* XXX */
235109771Sfjoe		}
236109771Sfjoe	}
237109771Sfjoe
238106939Ssam	BPF_MTAP(ifp, m);
23989099Sfjoe
24089099Sfjoe	if (!IF_HANDOFF(&ifp->if_snd, m, ifp)) {
24189099Sfjoe		m = 0;
24289099Sfjoe		senderr(ENOBUFS);
24389099Sfjoe	}
24489099Sfjoe
24589099Sfjoe	return (error);
24689099Sfjoe
24789099Sfjoebad:
24889099Sfjoe	if (m)
24989099Sfjoe		m_freem(m);
25089099Sfjoe	return (error);
25189099Sfjoe}
25289099Sfjoe
25389099Sfjoevoid
25489099Sfjoearc_frag_init(ifp)
25589099Sfjoe	struct ifnet *ifp;
25689099Sfjoe{
25789099Sfjoe	struct arccom *ac;
25889099Sfjoe
25989099Sfjoe	ac = (struct arccom *)ifp;
26089099Sfjoe	ac->curr_frag = 0;
26189099Sfjoe}
26289099Sfjoe
26389099Sfjoestruct mbuf *
26489099Sfjoearc_frag_next(ifp)
26589099Sfjoe	struct ifnet *ifp;
26689099Sfjoe{
26789099Sfjoe	struct arccom *ac;
26889099Sfjoe	struct mbuf *m;
26989099Sfjoe	struct arc_header *ah;
27089099Sfjoe
27189099Sfjoe	ac = (struct arccom *)ifp;
27289099Sfjoe	if ((m = ac->curr_frag) == 0) {
27389099Sfjoe		int tfrags;
27489099Sfjoe
27589099Sfjoe		/* dequeue new packet */
27689099Sfjoe		IF_DEQUEUE(&ifp->if_snd, m);
27789099Sfjoe		if (m == 0)
27889099Sfjoe			return 0;
27989099Sfjoe
28089099Sfjoe		ah = mtod(m, struct arc_header *);
28189099Sfjoe		if (!arc_isphds(ah->arc_type))
28289099Sfjoe			return m;
28389099Sfjoe
28489099Sfjoe		++ac->ac_seqid;		/* make the seqid unique */
285109771Sfjoe		tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA;
28689099Sfjoe		ac->fsflag = 2 * tfrags - 3;
28789099Sfjoe		ac->sflag = 0;
28889099Sfjoe		ac->rsflag = ac->fsflag;
28989099Sfjoe		ac->arc_dhost = ah->arc_dhost;
29089099Sfjoe		ac->arc_shost = ah->arc_shost;
29189099Sfjoe		ac->arc_type = ah->arc_type;
29289099Sfjoe
293110106Sfjoe		m_adj(m, ARC_HDRNEWLEN);
29489099Sfjoe		ac->curr_frag = m;
29589099Sfjoe	}
29689099Sfjoe
29789099Sfjoe	/* split out next fragment and return it */
29889099Sfjoe	if (ac->sflag < ac->fsflag) {
29989099Sfjoe		/* we CAN'T have short packets here */
300111119Simp		ac->curr_frag = m_split(m, ARC_MAX_DATA, M_DONTWAIT);
30189099Sfjoe		if (ac->curr_frag == 0) {
30293750Sluigi			m_freem(m);
30389099Sfjoe			return 0;
30489099Sfjoe		}
30589099Sfjoe
306111119Simp		M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
30789099Sfjoe		if (m == 0) {
30893750Sluigi			m_freem(ac->curr_frag);
30989099Sfjoe			ac->curr_frag = 0;
31089099Sfjoe			return 0;
31189099Sfjoe		}
31289099Sfjoe
31389099Sfjoe		ah = mtod(m, struct arc_header *);
31489099Sfjoe		ah->arc_flag = ac->rsflag;
31589099Sfjoe		ah->arc_seqid = ac->ac_seqid;
31689099Sfjoe
31789099Sfjoe		ac->sflag += 2;
31889099Sfjoe		ac->rsflag = ac->sflag;
31989099Sfjoe	} else if ((m->m_pkthdr.len >=
32089099Sfjoe	    ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
32189099Sfjoe	    (m->m_pkthdr.len <=
32289099Sfjoe	    ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
32389099Sfjoe		ac->curr_frag = 0;
32489099Sfjoe
325111119Simp		M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
32689099Sfjoe		if (m == 0)
32789099Sfjoe			return 0;
32889099Sfjoe
32989099Sfjoe		ah = mtod(m, struct arc_header *);
33089099Sfjoe		ah->arc_flag = 0xFF;
33189099Sfjoe		ah->arc_seqid = 0xFFFF;
33289099Sfjoe		ah->arc_type2 = ac->arc_type;
33389099Sfjoe		ah->arc_flag2 = ac->sflag;
33489099Sfjoe		ah->arc_seqid2 = ac->ac_seqid;
33589099Sfjoe	} else {
33689099Sfjoe		ac->curr_frag = 0;
33789099Sfjoe
338111119Simp		M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
33989099Sfjoe		if (m == 0)
34089099Sfjoe			return 0;
34189099Sfjoe
34289099Sfjoe		ah = mtod(m, struct arc_header *);
34389099Sfjoe		ah->arc_flag = ac->sflag;
34489099Sfjoe		ah->arc_seqid = ac->ac_seqid;
34589099Sfjoe	}
34689099Sfjoe
34789099Sfjoe	ah->arc_dhost = ac->arc_dhost;
34889099Sfjoe	ah->arc_shost = ac->arc_shost;
34989099Sfjoe	ah->arc_type = ac->arc_type;
35089099Sfjoe
35189099Sfjoe	return m;
35289099Sfjoe}
35389099Sfjoe
35489099Sfjoe/*
35589099Sfjoe * Defragmenter. Returns mbuf if last packet found, else
35689099Sfjoe * NULL. frees imcoming mbuf as necessary.
35789099Sfjoe */
35889099Sfjoe
359105228Sphkstatic __inline struct mbuf *
36089099Sfjoearc_defrag(ifp, m)
36189099Sfjoe	struct ifnet *ifp;
36289099Sfjoe	struct mbuf *m;
36389099Sfjoe{
36489099Sfjoe	struct arc_header *ah, *ah1;
36589099Sfjoe	struct arccom *ac;
36689099Sfjoe	struct ac_frag *af;
36789099Sfjoe	struct mbuf *m1;
36889099Sfjoe	char *s;
36989099Sfjoe	int newflen;
37089099Sfjoe	u_char src,dst,typ;
37189099Sfjoe
37289099Sfjoe	ac = (struct arccom *)ifp;
37389099Sfjoe
37489099Sfjoe	if (m->m_len < ARC_HDRNEWLEN) {
37589099Sfjoe		m = m_pullup(m, ARC_HDRNEWLEN);
37689099Sfjoe		if (m == NULL) {
37789099Sfjoe			++ifp->if_ierrors;
37889099Sfjoe			return NULL;
37989099Sfjoe		}
38089099Sfjoe	}
38189099Sfjoe
38289099Sfjoe	ah = mtod(m, struct arc_header *);
38389099Sfjoe	typ = ah->arc_type;
38489099Sfjoe
38589099Sfjoe	if (!arc_isphds(typ))
38689099Sfjoe		return m;
38789099Sfjoe
38889099Sfjoe	src = ah->arc_shost;
38989099Sfjoe	dst = ah->arc_dhost;
39089099Sfjoe
39189099Sfjoe	if (ah->arc_flag == 0xff) {
39289099Sfjoe		m_adj(m, 4);
39389099Sfjoe
39489099Sfjoe		if (m->m_len < ARC_HDRNEWLEN) {
39589099Sfjoe			m = m_pullup(m, ARC_HDRNEWLEN);
39689099Sfjoe			if (m == NULL) {
39789099Sfjoe				++ifp->if_ierrors;
39889099Sfjoe				return NULL;
39989099Sfjoe			}
40089099Sfjoe		}
40189099Sfjoe
40289099Sfjoe		ah = mtod(m, struct arc_header *);
40389099Sfjoe	}
40489099Sfjoe
40589099Sfjoe	af = &ac->ac_fragtab[src];
40689099Sfjoe	m1 = af->af_packet;
40789099Sfjoe	s = "debug code error";
40889099Sfjoe
40989099Sfjoe	if (ah->arc_flag & 1) {
41089099Sfjoe		/*
41189099Sfjoe		 * first fragment. We always initialize, which is
41289099Sfjoe		 * about the right thing to do, as we only want to
41389099Sfjoe		 * accept one fragmented packet per src at a time.
41489099Sfjoe		 */
41589099Sfjoe		if (m1 != NULL)
41689099Sfjoe			m_freem(m1);
41789099Sfjoe
41889099Sfjoe		af->af_packet = m;
41989099Sfjoe		m1 = m;
42089099Sfjoe		af->af_maxflag = ah->arc_flag;
42189099Sfjoe		af->af_lastseen = 0;
42289099Sfjoe		af->af_seqid = ah->arc_seqid;
42389099Sfjoe
42489099Sfjoe		return NULL;
42589099Sfjoe		/* notreached */
42689099Sfjoe	} else {
42789099Sfjoe		/* check for unfragmented packet */
42889099Sfjoe		if (ah->arc_flag == 0)
42989099Sfjoe			return m;
43089099Sfjoe
43189099Sfjoe		/* do we have a first packet from that src? */
43289099Sfjoe		if (m1 == NULL) {
43389099Sfjoe			s = "no first frag";
43489099Sfjoe			goto outofseq;
43589099Sfjoe		}
43689099Sfjoe
43789099Sfjoe		ah1 = mtod(m1, struct arc_header *);
43889099Sfjoe
43989099Sfjoe		if (ah->arc_seqid != ah1->arc_seqid) {
44089099Sfjoe			s = "seqid differs";
44189099Sfjoe			goto outofseq;
44289099Sfjoe		}
44389099Sfjoe
44489099Sfjoe		if (typ != ah1->arc_type) {
44589099Sfjoe			s = "type differs";
44689099Sfjoe			goto outofseq;
44789099Sfjoe		}
44889099Sfjoe
44989099Sfjoe		if (dst != ah1->arc_dhost) {
45089099Sfjoe			s = "dest host differs";
45189099Sfjoe			goto outofseq;
45289099Sfjoe		}
45389099Sfjoe
45489099Sfjoe		/* typ, seqid and dst are ok here. */
45589099Sfjoe
45689099Sfjoe		if (ah->arc_flag == af->af_lastseen) {
45789099Sfjoe			m_freem(m);
45889099Sfjoe			return NULL;
45989099Sfjoe		}
46089099Sfjoe
46189099Sfjoe		if (ah->arc_flag == af->af_lastseen + 2) {
46289099Sfjoe			/* ok, this is next fragment */
46389099Sfjoe			af->af_lastseen = ah->arc_flag;
46489099Sfjoe			m_adj(m,ARC_HDRNEWLEN);
46589099Sfjoe
46689099Sfjoe			/*
46789099Sfjoe			 * m_cat might free the first mbuf (with pkthdr)
46889099Sfjoe			 * in 2nd chain; therefore:
46989099Sfjoe			 */
47089099Sfjoe
47189099Sfjoe			newflen = m->m_pkthdr.len;
47289099Sfjoe
47389099Sfjoe			m_cat(m1,m);
47489099Sfjoe
47589099Sfjoe			m1->m_pkthdr.len += newflen;
47689099Sfjoe
47789099Sfjoe			/* is it the last one? */
47889099Sfjoe			if (af->af_lastseen > af->af_maxflag) {
47989099Sfjoe				af->af_packet = NULL;
48089099Sfjoe				return(m1);
48189099Sfjoe			} else
48289099Sfjoe				return NULL;
48389099Sfjoe		}
48489099Sfjoe		s = "other reason";
48589099Sfjoe		/* if all else fails, it is out of sequence, too */
48689099Sfjoe	}
48789099Sfjoeoutofseq:
48889099Sfjoe	if (m1) {
48989099Sfjoe		m_freem(m1);
49089099Sfjoe		af->af_packet = NULL;
49189099Sfjoe	}
49289099Sfjoe
49389099Sfjoe	if (m)
49489099Sfjoe		m_freem(m);
49589099Sfjoe
496121816Sbrooks	log(LOG_INFO,"%s: got out of seq. packet: %s\n",
497121816Sbrooks	    ifp->if_xname, s);
49889099Sfjoe
49989099Sfjoe	return NULL;
50089099Sfjoe}
50189099Sfjoe
50289099Sfjoe/*
50389099Sfjoe * return 1 if Packet Header Definition Standard, else 0.
50489099Sfjoe * For now: old IP, old ARP aren't obviously. Lacking correct information,
50589099Sfjoe * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
50689099Sfjoe * (Apple and Novell corporations were involved, among others, in PHDS work).
50789099Sfjoe * Easiest is to assume that everybody else uses that, too.
50889099Sfjoe */
50989099Sfjoeint
51089099Sfjoearc_isphds(type)
51189099Sfjoe	u_int8_t type;
51289099Sfjoe{
51389099Sfjoe	return (type != ARCTYPE_IP_OLD &&
51489099Sfjoe		type != ARCTYPE_ARP_OLD &&
51589099Sfjoe		type != ARCTYPE_DIAGNOSE);
51689099Sfjoe}
51789099Sfjoe
51889099Sfjoe/*
51989099Sfjoe * Process a received Arcnet packet;
52089099Sfjoe * the packet is in the mbuf chain m with
52189099Sfjoe * the ARCnet header.
52289099Sfjoe */
52389099Sfjoevoid
52489099Sfjoearc_input(ifp, m)
52589099Sfjoe	struct ifnet *ifp;
52689099Sfjoe	struct mbuf *m;
52789099Sfjoe{
52889099Sfjoe	struct arc_header *ah;
529111888Sjlemon	int isr;
53089099Sfjoe	u_int8_t atype;
53189099Sfjoe
53289099Sfjoe	if ((ifp->if_flags & IFF_UP) == 0) {
53389099Sfjoe		m_freem(m);
53489099Sfjoe		return;
53589099Sfjoe	}
53689099Sfjoe
53789099Sfjoe	/* possibly defragment: */
53889099Sfjoe	m = arc_defrag(ifp, m);
53989099Sfjoe	if (m == NULL)
54089099Sfjoe		return;
54189099Sfjoe
542106939Ssam	BPF_MTAP(ifp, m);
54389099Sfjoe
54489099Sfjoe	ah = mtod(m, struct arc_header *);
545109771Sfjoe	/* does this belong to us? */
546110106Sfjoe	if ((ifp->if_flags & IFF_PROMISC) == 0
547109771Sfjoe	    && ah->arc_dhost != arcbroadcastaddr
548110106Sfjoe	    && ah->arc_dhost != ARC_LLADDR(ifp)) {
549109771Sfjoe		m_freem(m);
550109771Sfjoe		return;
551109771Sfjoe	}
55289099Sfjoe
55389099Sfjoe	ifp->if_ibytes += m->m_pkthdr.len;
55489099Sfjoe
555109771Sfjoe	if (ah->arc_dhost == arcbroadcastaddr) {
55689099Sfjoe		m->m_flags |= M_BCAST|M_MCAST;
55789099Sfjoe		ifp->if_imcasts++;
55889099Sfjoe	}
55989099Sfjoe
56089099Sfjoe	atype = ah->arc_type;
56189099Sfjoe	switch (atype) {
56289099Sfjoe#ifdef INET
56389099Sfjoe	case ARCTYPE_IP:
56489099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
565122702Sandre		if (ip_fastforward(m))
566109771Sfjoe			return;
567111888Sjlemon		isr = NETISR_IP;
56889099Sfjoe		break;
56989099Sfjoe
57089099Sfjoe	case ARCTYPE_IP_OLD:
57189099Sfjoe		m_adj(m, ARC_HDRLEN);
572122702Sandre		if (ip_fastforward(m))
573109771Sfjoe			return;
574111888Sjlemon		isr = NETISR_IP;
57589099Sfjoe		break;
57689099Sfjoe
57789099Sfjoe	case ARCTYPE_ARP:
57889099Sfjoe		if (ifp->if_flags & IFF_NOARP) {
57989099Sfjoe			/* Discard packet if ARP is disabled on interface */
58089099Sfjoe			m_freem(m);
58189099Sfjoe			return;
58289099Sfjoe		}
58389099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
584111888Sjlemon		isr = NETISR_ARP;
58589099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
58689099Sfjoe		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
58789099Sfjoe#endif
58889099Sfjoe		break;
58989099Sfjoe
59089099Sfjoe	case ARCTYPE_ARP_OLD:
59189099Sfjoe		if (ifp->if_flags & IFF_NOARP) {
59289099Sfjoe			/* Discard packet if ARP is disabled on interface */
59389099Sfjoe			m_freem(m);
59489099Sfjoe			return;
59589099Sfjoe		}
59689099Sfjoe		m_adj(m, ARC_HDRLEN);
597111888Sjlemon		isr = NETISR_ARP;
59889099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
59989099Sfjoe		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
60089099Sfjoe#endif
60189099Sfjoe		break;
60289099Sfjoe#endif
60389099Sfjoe#ifdef INET6
60489099Sfjoe	case ARCTYPE_INET6:
60589099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
606111888Sjlemon		isr = NETISR_IPV6;
60789099Sfjoe		break;
60889099Sfjoe#endif
609109771Sfjoe#ifdef IPX
610109771Sfjoe	case ARCTYPE_IPX:
611109771Sfjoe		m_adj(m, ARC_HDRNEWLEN);
612111888Sjlemon		isr = NETISR_IPX;
613109771Sfjoe		break;
614109771Sfjoe#endif
61589099Sfjoe	default:
61689099Sfjoe		m_freem(m);
61789099Sfjoe		return;
61889099Sfjoe	}
619111888Sjlemon	netisr_dispatch(isr, m);
62089099Sfjoe}
62189099Sfjoe
62289099Sfjoe/*
62389099Sfjoe * Register (new) link level address.
62489099Sfjoe */
62589099Sfjoevoid
62689099Sfjoearc_storelladdr(ifp, lla)
62789099Sfjoe	struct ifnet *ifp;
62889099Sfjoe	u_int8_t lla;
62989099Sfjoe{
630110106Sfjoe	ARC_LLADDR(ifp) = lla;
63189099Sfjoe}
63289099Sfjoe
63389099Sfjoe/*
63489099Sfjoe * Perform common duties while attaching to interface list
63589099Sfjoe */
63689099Sfjoevoid
63789099Sfjoearc_ifattach(ifp, lla)
63889099Sfjoe	struct ifnet *ifp;
63989099Sfjoe	u_int8_t lla;
64089099Sfjoe{
64189099Sfjoe	struct ifaddr *ifa;
64289099Sfjoe	struct sockaddr_dl *sdl;
64389099Sfjoe	struct arccom *ac;
64489099Sfjoe
64589099Sfjoe	if_attach(ifp);
64689099Sfjoe	ifp->if_type = IFT_ARCNET;
64789099Sfjoe	ifp->if_addrlen = 1;
64889099Sfjoe	ifp->if_hdrlen = ARC_HDRLEN;
64989099Sfjoe	ifp->if_mtu = 1500;
650109771Sfjoe	ifp->if_resolvemulti = arc_resolvemulti;
65189099Sfjoe	if (ifp->if_baudrate == 0)
65289099Sfjoe		ifp->if_baudrate = 2500000;
65389099Sfjoe#if __FreeBSD_version < 500000
65489099Sfjoe	ifa = ifnet_addrs[ifp->if_index - 1];
65589099Sfjoe#else
65689099Sfjoe	ifa = ifaddr_byindex(ifp->if_index);
65789099Sfjoe#endif
65889099Sfjoe	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
65989099Sfjoe	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
66089099Sfjoe	sdl->sdl_type = IFT_ARCNET;
66189099Sfjoe	sdl->sdl_alen = ifp->if_addrlen;
66289099Sfjoe
66389099Sfjoe	if (ifp->if_flags & IFF_BROADCAST)
66489099Sfjoe		ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
66589099Sfjoe
66689099Sfjoe	ac = (struct arccom *)ifp;
66789099Sfjoe	ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
66889099Sfjoe	if (lla == 0) {
66989099Sfjoe		/* XXX this message isn't entirely clear, to me -- cgd */
670121816Sbrooks		log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
671121816Sbrooks		   ifp->if_xname, ifp->if_xname);
67289099Sfjoe	}
67389099Sfjoe	arc_storelladdr(ifp, lla);
67489099Sfjoe
67589099Sfjoe	ifp->if_broadcastaddr = &arcbroadcastaddr;
67689099Sfjoe
67789099Sfjoe	bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
67889099Sfjoe}
67989099Sfjoe
68089099Sfjoevoid
68189099Sfjoearc_ifdetach(ifp)
68289099Sfjoe	struct ifnet *ifp;
68389099Sfjoe{
68489099Sfjoe	bpfdetach(ifp);
68589099Sfjoe	if_detach(ifp);
68689099Sfjoe}
68789099Sfjoe
68889099Sfjoeint
68989099Sfjoearc_ioctl(ifp, command, data)
69089099Sfjoe	struct ifnet *ifp;
69189099Sfjoe	int command;
69289099Sfjoe	caddr_t data;
69389099Sfjoe{
69489099Sfjoe	struct ifaddr *ifa = (struct ifaddr *) data;
69589099Sfjoe	struct ifreq *ifr = (struct ifreq *) data;
69689099Sfjoe	int error = 0;
69789099Sfjoe
69889099Sfjoe	switch (command) {
69989099Sfjoe	case SIOCSIFADDR:
70089099Sfjoe		ifp->if_flags |= IFF_UP;
70189099Sfjoe		switch (ifa->ifa_addr->sa_family) {
70289099Sfjoe#ifdef INET
70389099Sfjoe		case AF_INET:
70489099Sfjoe			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
70589099Sfjoe			arp_ifinit(ifp, ifa);
70689099Sfjoe			break;
70789099Sfjoe#endif
708109771Sfjoe#ifdef IPX
709109771Sfjoe		/*
710109771Sfjoe		 * XXX This code is probably wrong
711109771Sfjoe		 */
712109771Sfjoe		case AF_IPX:
713109771Sfjoe		{
714109771Sfjoe			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
715109771Sfjoe
716109771Sfjoe			if (ipx_nullhost(*ina))
717110106Sfjoe				ina->x_host.c_host[5] = ARC_LLADDR(ifp);
718109771Sfjoe			else
719109771Sfjoe				arc_storelladdr(ifp, ina->x_host.c_host[5]);
720109771Sfjoe
721109771Sfjoe			/*
722109771Sfjoe			 * Set new address
723109771Sfjoe			 */
724109771Sfjoe			ifp->if_init(ifp->if_softc);
725109771Sfjoe			break;
726109771Sfjoe		}
727109771Sfjoe#endif
72889099Sfjoe		default:
72989099Sfjoe			ifp->if_init(ifp->if_softc);
73089099Sfjoe			break;
73189099Sfjoe		}
73289099Sfjoe		break;
73389099Sfjoe
734109771Sfjoe	case SIOCGIFADDR:
735109771Sfjoe		{
736109771Sfjoe			struct sockaddr *sa;
737109771Sfjoe
738109771Sfjoe			sa = (struct sockaddr *) &ifr->ifr_data;
739110106Sfjoe			*(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
740109771Sfjoe		}
741109771Sfjoe		break;
742109771Sfjoe
74389099Sfjoe	case SIOCADDMULTI:
74489099Sfjoe	case SIOCDELMULTI:
74589099Sfjoe		if (ifr == NULL)
74689099Sfjoe			error = EAFNOSUPPORT;
74789099Sfjoe		else {
74889099Sfjoe			switch (ifr->ifr_addr.sa_family) {
74989099Sfjoe			case AF_INET:
75089099Sfjoe			case AF_INET6:
75189099Sfjoe				error = 0;
75289099Sfjoe				break;
75389099Sfjoe			default:
75489099Sfjoe				error = EAFNOSUPPORT;
75589099Sfjoe				break;
75689099Sfjoe			}
75789099Sfjoe		}
75889099Sfjoe		break;
75989099Sfjoe
76089099Sfjoe	case SIOCSIFMTU:
76189099Sfjoe		/*
76289099Sfjoe		 * Set the interface MTU.
76389099Sfjoe		 * mtu can't be larger than ARCMTU for RFC1051
76489099Sfjoe		 * and can't be larger than ARC_PHDS_MTU
76589099Sfjoe		 */
76689099Sfjoe		if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
76789099Sfjoe		    ifr->ifr_mtu > ARC_PHDS_MAXMTU)
76889099Sfjoe			error = EINVAL;
76989099Sfjoe		else
77089099Sfjoe			ifp->if_mtu = ifr->ifr_mtu;
77189099Sfjoe		break;
772109771Sfjoe	}
77389099Sfjoe
774109771Sfjoe	return (error);
775109771Sfjoe}
77689099Sfjoe
777109771Sfjoe/* based on ether_resolvemulti() */
778109771Sfjoeint
779109771Sfjoearc_resolvemulti(ifp, llsa, sa)
780109771Sfjoe	struct ifnet *ifp;
781109771Sfjoe	struct sockaddr **llsa;
782109771Sfjoe	struct sockaddr *sa;
783109771Sfjoe{
784109771Sfjoe	struct sockaddr_dl *sdl;
785109771Sfjoe	struct sockaddr_in *sin;
786109771Sfjoe#ifdef INET6
787109771Sfjoe	struct sockaddr_in6 *sin6;
788109771Sfjoe#endif
789109771Sfjoe
790109771Sfjoe	switch(sa->sa_family) {
791109771Sfjoe	case AF_LINK:
792109771Sfjoe		/*
793109771Sfjoe		* No mapping needed. Just check that it's a valid MC address.
794109771Sfjoe		*/
795109771Sfjoe		sdl = (struct sockaddr_dl *)sa;
796109771Sfjoe		if (*LLADDR(sdl) != arcbroadcastaddr)
797109771Sfjoe			return EADDRNOTAVAIL;
798109771Sfjoe		*llsa = 0;
799109771Sfjoe		return 0;
800109771Sfjoe#ifdef INET
801109771Sfjoe	case AF_INET:
802109771Sfjoe		sin = (struct sockaddr_in *)sa;
803109771Sfjoe		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
804109771Sfjoe			return EADDRNOTAVAIL;
805109771Sfjoe		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
806109771Sfjoe		       M_ZERO);
807109771Sfjoe		sdl->sdl_len = sizeof *sdl;
808109771Sfjoe		sdl->sdl_family = AF_LINK;
809109771Sfjoe		sdl->sdl_index = ifp->if_index;
810109771Sfjoe		sdl->sdl_type = IFT_ARCNET;
811109771Sfjoe		sdl->sdl_alen = ARC_ADDR_LEN;
812109771Sfjoe		*LLADDR(sdl) = 0;
813109771Sfjoe		*llsa = (struct sockaddr *)sdl;
814109771Sfjoe		return 0;
815109771Sfjoe#endif
816109771Sfjoe#ifdef INET6
817109771Sfjoe	case AF_INET6:
818109771Sfjoe		sin6 = (struct sockaddr_in6 *)sa;
819109771Sfjoe		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
820109771Sfjoe			/*
821109771Sfjoe			 * An IP6 address of 0 means listen to all
822109771Sfjoe			 * of the Ethernet multicast address used for IP6.
823109771Sfjoe			 * (This is used for multicast routers.)
824109771Sfjoe			 */
825109771Sfjoe			ifp->if_flags |= IFF_ALLMULTI;
826109771Sfjoe			*llsa = 0;
827109771Sfjoe			return 0;
82889099Sfjoe		}
829109771Sfjoe		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
830109771Sfjoe			return EADDRNOTAVAIL;
831109771Sfjoe		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
832109771Sfjoe		       M_ZERO);
833109771Sfjoe		sdl->sdl_len = sizeof *sdl;
834109771Sfjoe		sdl->sdl_family = AF_LINK;
835109771Sfjoe		sdl->sdl_index = ifp->if_index;
836109771Sfjoe		sdl->sdl_type = IFT_ARCNET;
837109771Sfjoe		sdl->sdl_alen = ARC_ADDR_LEN;
838109771Sfjoe		*LLADDR(sdl) = 0;
839109771Sfjoe		*llsa = (struct sockaddr *)sdl;
840109771Sfjoe		return 0;
84189099Sfjoe#endif
842109771Sfjoe
843109771Sfjoe	default:
844109771Sfjoe		/*
845109771Sfjoe		 * Well, the text isn't quite right, but it's the name
846109771Sfjoe		 * that counts...
847109771Sfjoe		 */
848109771Sfjoe		return EAFNOSUPPORT;
84989099Sfjoe	}
85089099Sfjoe}
851