if_arcsubr.c revision 249925
189099Sfjoe/*	$NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $	*/
289099Sfjoe/*	$FreeBSD: head/sys/net/if_arcsubr.c 249925 2013-04-26 12:50:32Z glebius $ */
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>
67186119Sqingli#include <net/if_llatbl.h>
6889099Sfjoe
6989099Sfjoe#if defined(INET) || defined(INET6)
7089099Sfjoe#include <netinet/in.h>
7189099Sfjoe#include <netinet/in_var.h>
7289099Sfjoe#include <netinet/if_ether.h>
7389099Sfjoe#endif
7489099Sfjoe
7589099Sfjoe#ifdef INET6
7689099Sfjoe#include <netinet6/nd6.h>
7789099Sfjoe#endif
7889099Sfjoe
79109771Sfjoe#ifdef IPX
80109771Sfjoe#include <netipx/ipx.h>
81109771Sfjoe#include <netipx/ipx_if.h>
82109771Sfjoe#endif
83109771Sfjoe
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;}
95249925Sglebius#define SIN(s)	((const struct sockaddr_in *)(s))
96249925Sglebius#define SIPX(s)	((const 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
104249925Sglebiusarc_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
105191148Skmacy    struct route *ro)
10689099Sfjoe{
10789099Sfjoe	struct arc_header	*ah;
10889099Sfjoe	int			error;
10989099Sfjoe	u_int8_t		atype, adst;
110109771Sfjoe	int			loop_copy = 0;
111110106Sfjoe	int			isphds;
112193891Sbz#if defined(INET) || defined(INET6)
113186119Sqingli	struct llentry		*lle;
114193891Sbz#endif
11589099Sfjoe
116148887Srwatson	if (!((ifp->if_flags & IFF_UP) &&
117148887Srwatson	    (ifp->if_drv_flags & IFF_DRV_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 {
134191148Skmacy			error = arpresolve(ifp, ro ? ro->ro_rt : NULL,
135191148Skmacy			                   m, dst, &adst, &lle);
136128636Sluigi			if (error)
137128636Sluigi				return (error == EWOULDBLOCK ? 0 : error);
138128636Sluigi		}
13989099Sfjoe
14089099Sfjoe		atype = (ifp->if_flags & IFF_LINK0) ?
14189099Sfjoe			ARCTYPE_IP_OLD : ARCTYPE_IP;
14289099Sfjoe		break;
143127260Smdodd	case AF_ARP:
144127260Smdodd	{
145127260Smdodd		struct arphdr *ah;
146127260Smdodd		ah = mtod(m, struct arphdr *);
147127260Smdodd		ah->ar_hrd = htons(ARPHRD_ARCNET);
148127260Smdodd
149127260Smdodd		loop_copy = -1; /* if this is for us, don't do it */
150127260Smdodd
151127260Smdodd		switch(ntohs(ah->ar_op)) {
152127260Smdodd		case ARPOP_REVREQUEST:
153127260Smdodd		case ARPOP_REVREPLY:
154127275Smdodd			atype = ARCTYPE_REVARP;
155127260Smdodd			break;
156127260Smdodd		case ARPOP_REQUEST:
157127260Smdodd		case ARPOP_REPLY:
158127260Smdodd		default:
159127275Smdodd			atype = ARCTYPE_ARP;
160127260Smdodd			break;
161127260Smdodd		}
162127260Smdodd
163127260Smdodd		if (m->m_flags & M_BCAST)
164127290Smdodd			bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN);
165127260Smdodd		else
166127290Smdodd			bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN);
167127260Smdodd
168127260Smdodd	}
169127260Smdodd	break;
17089099Sfjoe#endif
17189099Sfjoe#ifdef INET6
17289099Sfjoe	case AF_INET6:
173186217Sqingli		error = nd6_storelladdr(ifp, m, dst, (u_char *)&adst, &lle);
174128636Sluigi		if (error)
175128636Sluigi			return (error);
17689099Sfjoe		atype = ARCTYPE_INET6;
17789099Sfjoe		break;
17889099Sfjoe#endif
179109771Sfjoe#ifdef IPX
180109771Sfjoe	case AF_IPX:
181109771Sfjoe		adst = SIPX(dst)->sipx_addr.x_host.c_host[5];
182109771Sfjoe		atype = ARCTYPE_IPX;
183109771Sfjoe		if (adst == 0xff)
184109771Sfjoe			adst = arcbroadcastaddr;
185109771Sfjoe		break;
186109771Sfjoe#endif
18789099Sfjoe
18889099Sfjoe	case AF_UNSPEC:
189249925Sglebius	    {
190249925Sglebius		const struct arc_header *ah;
191249925Sglebius
192109771Sfjoe		loop_copy = -1;
193249925Sglebius		ah = (const struct arc_header *)dst->sa_data;
19489099Sfjoe		adst = ah->arc_dhost;
19589099Sfjoe		atype = ah->arc_type;
19689099Sfjoe
19789099Sfjoe		if (atype == ARCTYPE_ARP) {
19889099Sfjoe			atype = (ifp->if_flags & IFF_LINK0) ?
19989099Sfjoe			    ARCTYPE_ARP_OLD: ARCTYPE_ARP;
20089099Sfjoe
20189099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
20289099Sfjoe			/*
20389099Sfjoe			 * XXX It's not clear per RFC826 if this is needed, but
20489099Sfjoe			 * "assigned numbers" say this is wrong.
20589099Sfjoe			 * However, e.g., AmiTCP 3.0Beta used it... we make this
20689099Sfjoe			 * switchable for emergency cases. Not perfect, but...
20789099Sfjoe			 */
20889099Sfjoe			if (ifp->if_flags & IFF_LINK2)
209109771Sfjoe				mtod(m, struct arphdr *)->ar_pro = atype - 1;
21089099Sfjoe#endif
21189099Sfjoe		}
21289099Sfjoe		break;
213249925Sglebius	    }
21489099Sfjoe	default:
215105598Sbrooks		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
21689099Sfjoe		senderr(EAFNOSUPPORT);
21789099Sfjoe	}
21889099Sfjoe
219110106Sfjoe	isphds = arc_isphds(atype);
220243882Sglebius	M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_NOWAIT);
22189099Sfjoe	if (m == 0)
22289099Sfjoe		senderr(ENOBUFS);
22389099Sfjoe	ah = mtod(m, struct arc_header *);
22489099Sfjoe	ah->arc_type = atype;
22589099Sfjoe	ah->arc_dhost = adst;
226110106Sfjoe	ah->arc_shost = ARC_LLADDR(ifp);
227110106Sfjoe	if (isphds) {
228110106Sfjoe		ah->arc_flag = 0;
229110106Sfjoe		ah->arc_seqid = 0;
230110106Sfjoe	}
23189099Sfjoe
232109771Sfjoe	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
233109771Sfjoe		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
234109771Sfjoe			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
235109771Sfjoe
236109771Sfjoe			(void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN);
237109771Sfjoe		} else if (ah->arc_dhost == ah->arc_shost) {
238109771Sfjoe			(void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN);
239109771Sfjoe			return (0);     /* XXX */
240109771Sfjoe		}
241109771Sfjoe	}
242109771Sfjoe
243106939Ssam	BPF_MTAP(ifp, m);
24489099Sfjoe
245191605Skmacy	error = ifp->if_transmit(ifp, m);
24689099Sfjoe
24789099Sfjoe	return (error);
24889099Sfjoe
24989099Sfjoebad:
25089099Sfjoe	if (m)
25189099Sfjoe		m_freem(m);
25289099Sfjoe	return (error);
25389099Sfjoe}
25489099Sfjoe
25589099Sfjoevoid
256157681Srwatsonarc_frag_init(struct ifnet *ifp)
25789099Sfjoe{
25889099Sfjoe	struct arccom *ac;
25989099Sfjoe
260147256Sbrooks	ac = (struct arccom *)ifp->if_l2com;
26189099Sfjoe	ac->curr_frag = 0;
26289099Sfjoe}
26389099Sfjoe
26489099Sfjoestruct mbuf *
265157681Srwatsonarc_frag_next(struct ifnet *ifp)
26689099Sfjoe{
26789099Sfjoe	struct arccom *ac;
26889099Sfjoe	struct mbuf *m;
26989099Sfjoe	struct arc_header *ah;
27089099Sfjoe
271147256Sbrooks	ac = (struct arccom *)ifp->if_l2com;
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 */
300243882Sglebius		ac->curr_frag = m_split(m, ARC_MAX_DATA, M_NOWAIT);
30189099Sfjoe		if (ac->curr_frag == 0) {
30293750Sluigi			m_freem(m);
30389099Sfjoe			return 0;
30489099Sfjoe		}
30589099Sfjoe
306243882Sglebius		M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
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
325243882Sglebius		M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_NOWAIT);
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
338243882Sglebius		M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT);
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 *
360157681Srwatsonarc_defrag(struct ifnet *ifp, 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
370147256Sbrooks	ac = (struct arccom *)ifp->if_l2com;
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
508157681Srwatsonarc_isphds(u_int8_t type)
50989099Sfjoe{
51089099Sfjoe	return (type != ARCTYPE_IP_OLD &&
51189099Sfjoe		type != ARCTYPE_ARP_OLD &&
51289099Sfjoe		type != ARCTYPE_DIAGNOSE);
51389099Sfjoe}
51489099Sfjoe
51589099Sfjoe/*
51689099Sfjoe * Process a received Arcnet packet;
51789099Sfjoe * the packet is in the mbuf chain m with
51889099Sfjoe * the ARCnet header.
51989099Sfjoe */
52089099Sfjoevoid
521157681Srwatsonarc_input(struct ifnet *ifp, struct mbuf *m)
52289099Sfjoe{
52389099Sfjoe	struct arc_header *ah;
524111888Sjlemon	int isr;
52589099Sfjoe	u_int8_t atype;
52689099Sfjoe
52789099Sfjoe	if ((ifp->if_flags & IFF_UP) == 0) {
52889099Sfjoe		m_freem(m);
52989099Sfjoe		return;
53089099Sfjoe	}
53189099Sfjoe
53289099Sfjoe	/* possibly defragment: */
53389099Sfjoe	m = arc_defrag(ifp, m);
53489099Sfjoe	if (m == NULL)
53589099Sfjoe		return;
53689099Sfjoe
537106939Ssam	BPF_MTAP(ifp, m);
53889099Sfjoe
53989099Sfjoe	ah = mtod(m, struct arc_header *);
540109771Sfjoe	/* does this belong to us? */
541110106Sfjoe	if ((ifp->if_flags & IFF_PROMISC) == 0
542109771Sfjoe	    && ah->arc_dhost != arcbroadcastaddr
543110106Sfjoe	    && ah->arc_dhost != ARC_LLADDR(ifp)) {
544109771Sfjoe		m_freem(m);
545109771Sfjoe		return;
546109771Sfjoe	}
54789099Sfjoe
54889099Sfjoe	ifp->if_ibytes += m->m_pkthdr.len;
54989099Sfjoe
550109771Sfjoe	if (ah->arc_dhost == arcbroadcastaddr) {
55189099Sfjoe		m->m_flags |= M_BCAST|M_MCAST;
55289099Sfjoe		ifp->if_imcasts++;
55389099Sfjoe	}
55489099Sfjoe
55589099Sfjoe	atype = ah->arc_type;
55689099Sfjoe	switch (atype) {
55789099Sfjoe#ifdef INET
55889099Sfjoe	case ARCTYPE_IP:
55989099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
560154518Sandre		if ((m = ip_fastforward(m)) == NULL)
561109771Sfjoe			return;
562111888Sjlemon		isr = NETISR_IP;
56389099Sfjoe		break;
56489099Sfjoe
56589099Sfjoe	case ARCTYPE_IP_OLD:
56689099Sfjoe		m_adj(m, ARC_HDRLEN);
567154518Sandre		if ((m = ip_fastforward(m)) == NULL)
568109771Sfjoe			return;
569111888Sjlemon		isr = NETISR_IP;
57089099Sfjoe		break;
57189099Sfjoe
57289099Sfjoe	case ARCTYPE_ARP:
57389099Sfjoe		if (ifp->if_flags & IFF_NOARP) {
57489099Sfjoe			/* Discard packet if ARP is disabled on interface */
57589099Sfjoe			m_freem(m);
57689099Sfjoe			return;
57789099Sfjoe		}
57889099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
579111888Sjlemon		isr = NETISR_ARP;
58089099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
58189099Sfjoe		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
58289099Sfjoe#endif
58389099Sfjoe		break;
58489099Sfjoe
58589099Sfjoe	case ARCTYPE_ARP_OLD:
58689099Sfjoe		if (ifp->if_flags & IFF_NOARP) {
58789099Sfjoe			/* Discard packet if ARP is disabled on interface */
58889099Sfjoe			m_freem(m);
58989099Sfjoe			return;
59089099Sfjoe		}
59189099Sfjoe		m_adj(m, ARC_HDRLEN);
592111888Sjlemon		isr = NETISR_ARP;
59389099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP
59489099Sfjoe		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
59589099Sfjoe#endif
59689099Sfjoe		break;
59789099Sfjoe#endif
59889099Sfjoe#ifdef INET6
59989099Sfjoe	case ARCTYPE_INET6:
60089099Sfjoe		m_adj(m, ARC_HDRNEWLEN);
601111888Sjlemon		isr = NETISR_IPV6;
60289099Sfjoe		break;
60389099Sfjoe#endif
604109771Sfjoe#ifdef IPX
605109771Sfjoe	case ARCTYPE_IPX:
606109771Sfjoe		m_adj(m, ARC_HDRNEWLEN);
607111888Sjlemon		isr = NETISR_IPX;
608109771Sfjoe		break;
609109771Sfjoe#endif
61089099Sfjoe	default:
61189099Sfjoe		m_freem(m);
61289099Sfjoe		return;
61389099Sfjoe	}
614223741Sbz	M_SETFIB(m, ifp->if_fib);
615111888Sjlemon	netisr_dispatch(isr, m);
61689099Sfjoe}
61789099Sfjoe
61889099Sfjoe/*
61989099Sfjoe * Register (new) link level address.
62089099Sfjoe */
62189099Sfjoevoid
622157681Srwatsonarc_storelladdr(struct ifnet *ifp, u_int8_t lla)
62389099Sfjoe{
624110106Sfjoe	ARC_LLADDR(ifp) = lla;
62589099Sfjoe}
62689099Sfjoe
62789099Sfjoe/*
62889099Sfjoe * Perform common duties while attaching to interface list
62989099Sfjoe */
63089099Sfjoevoid
631157681Srwatsonarc_ifattach(struct ifnet *ifp, u_int8_t lla)
63289099Sfjoe{
63389099Sfjoe	struct ifaddr *ifa;
63489099Sfjoe	struct sockaddr_dl *sdl;
63589099Sfjoe	struct arccom *ac;
63689099Sfjoe
63789099Sfjoe	if_attach(ifp);
63889099Sfjoe	ifp->if_addrlen = 1;
63989099Sfjoe	ifp->if_hdrlen = ARC_HDRLEN;
64089099Sfjoe	ifp->if_mtu = 1500;
641109771Sfjoe	ifp->if_resolvemulti = arc_resolvemulti;
64289099Sfjoe	if (ifp->if_baudrate == 0)
64389099Sfjoe		ifp->if_baudrate = 2500000;
644152315Sru	ifa = ifp->if_addr;
645135577Sstefanf	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
64689099Sfjoe	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
64789099Sfjoe	sdl->sdl_type = IFT_ARCNET;
64889099Sfjoe	sdl->sdl_alen = ifp->if_addrlen;
64989099Sfjoe
65089099Sfjoe	if (ifp->if_flags & IFF_BROADCAST)
65189099Sfjoe		ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
65289099Sfjoe
653147256Sbrooks	ac = (struct arccom *)ifp->if_l2com;
65489099Sfjoe	ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
65589099Sfjoe	if (lla == 0) {
65689099Sfjoe		/* XXX this message isn't entirely clear, to me -- cgd */
657121816Sbrooks		log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
658121816Sbrooks		   ifp->if_xname, ifp->if_xname);
65989099Sfjoe	}
66089099Sfjoe	arc_storelladdr(ifp, lla);
66189099Sfjoe
66289099Sfjoe	ifp->if_broadcastaddr = &arcbroadcastaddr;
66389099Sfjoe
66489099Sfjoe	bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN);
66589099Sfjoe}
66689099Sfjoe
66789099Sfjoevoid
668157681Srwatsonarc_ifdetach(struct ifnet *ifp)
66989099Sfjoe{
67089099Sfjoe	bpfdetach(ifp);
67189099Sfjoe	if_detach(ifp);
67289099Sfjoe}
67389099Sfjoe
67489099Sfjoeint
675194581Srdivackyarc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
67689099Sfjoe{
67789099Sfjoe	struct ifaddr *ifa = (struct ifaddr *) data;
67889099Sfjoe	struct ifreq *ifr = (struct ifreq *) data;
67989099Sfjoe	int error = 0;
68089099Sfjoe
68189099Sfjoe	switch (command) {
68289099Sfjoe	case SIOCSIFADDR:
68389099Sfjoe		ifp->if_flags |= IFF_UP;
68489099Sfjoe		switch (ifa->ifa_addr->sa_family) {
68589099Sfjoe#ifdef INET
68689099Sfjoe		case AF_INET:
68789099Sfjoe			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
68889099Sfjoe			arp_ifinit(ifp, ifa);
68989099Sfjoe			break;
69089099Sfjoe#endif
691109771Sfjoe#ifdef IPX
692109771Sfjoe		/*
693109771Sfjoe		 * XXX This code is probably wrong
694109771Sfjoe		 */
695109771Sfjoe		case AF_IPX:
696109771Sfjoe		{
697109771Sfjoe			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
698109771Sfjoe
699109771Sfjoe			if (ipx_nullhost(*ina))
700110106Sfjoe				ina->x_host.c_host[5] = ARC_LLADDR(ifp);
701109771Sfjoe			else
702109771Sfjoe				arc_storelladdr(ifp, ina->x_host.c_host[5]);
703109771Sfjoe
704109771Sfjoe			/*
705109771Sfjoe			 * Set new address
706109771Sfjoe			 */
707109771Sfjoe			ifp->if_init(ifp->if_softc);
708109771Sfjoe			break;
709109771Sfjoe		}
710109771Sfjoe#endif
71189099Sfjoe		default:
71289099Sfjoe			ifp->if_init(ifp->if_softc);
71389099Sfjoe			break;
71489099Sfjoe		}
71589099Sfjoe		break;
71689099Sfjoe
717109771Sfjoe	case SIOCGIFADDR:
718109771Sfjoe		{
719109771Sfjoe			struct sockaddr *sa;
720109771Sfjoe
721109771Sfjoe			sa = (struct sockaddr *) &ifr->ifr_data;
722110106Sfjoe			*(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp);
723109771Sfjoe		}
724109771Sfjoe		break;
725109771Sfjoe
72689099Sfjoe	case SIOCADDMULTI:
72789099Sfjoe	case SIOCDELMULTI:
72889099Sfjoe		if (ifr == NULL)
72989099Sfjoe			error = EAFNOSUPPORT;
73089099Sfjoe		else {
73189099Sfjoe			switch (ifr->ifr_addr.sa_family) {
73289099Sfjoe			case AF_INET:
73389099Sfjoe			case AF_INET6:
73489099Sfjoe				error = 0;
73589099Sfjoe				break;
73689099Sfjoe			default:
73789099Sfjoe				error = EAFNOSUPPORT;
73889099Sfjoe				break;
73989099Sfjoe			}
74089099Sfjoe		}
74189099Sfjoe		break;
74289099Sfjoe
74389099Sfjoe	case SIOCSIFMTU:
74489099Sfjoe		/*
74589099Sfjoe		 * Set the interface MTU.
74689099Sfjoe		 * mtu can't be larger than ARCMTU for RFC1051
74789099Sfjoe		 * and can't be larger than ARC_PHDS_MTU
74889099Sfjoe		 */
74989099Sfjoe		if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) ||
75089099Sfjoe		    ifr->ifr_mtu > ARC_PHDS_MAXMTU)
75189099Sfjoe			error = EINVAL;
75289099Sfjoe		else
75389099Sfjoe			ifp->if_mtu = ifr->ifr_mtu;
75489099Sfjoe		break;
755109771Sfjoe	}
75689099Sfjoe
757109771Sfjoe	return (error);
758109771Sfjoe}
75989099Sfjoe
760109771Sfjoe/* based on ether_resolvemulti() */
761109771Sfjoeint
762157681Srwatsonarc_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
763157681Srwatson    struct sockaddr *sa)
764109771Sfjoe{
765109771Sfjoe	struct sockaddr_dl *sdl;
766184680Sbz#ifdef INET
767109771Sfjoe	struct sockaddr_in *sin;
768184680Sbz#endif
769109771Sfjoe#ifdef INET6
770109771Sfjoe	struct sockaddr_in6 *sin6;
771109771Sfjoe#endif
772109771Sfjoe
773109771Sfjoe	switch(sa->sa_family) {
774109771Sfjoe	case AF_LINK:
775109771Sfjoe		/*
776109771Sfjoe		* No mapping needed. Just check that it's a valid MC address.
777109771Sfjoe		*/
778109771Sfjoe		sdl = (struct sockaddr_dl *)sa;
779109771Sfjoe		if (*LLADDR(sdl) != arcbroadcastaddr)
780109771Sfjoe			return EADDRNOTAVAIL;
781109771Sfjoe		*llsa = 0;
782109771Sfjoe		return 0;
783109771Sfjoe#ifdef INET
784109771Sfjoe	case AF_INET:
785109771Sfjoe		sin = (struct sockaddr_in *)sa;
786109771Sfjoe		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
787109771Sfjoe			return EADDRNOTAVAIL;
788184205Sdes		sdl = malloc(sizeof *sdl, M_IFMADDR,
789148641Srwatson		       M_NOWAIT | M_ZERO);
790148641Srwatson		if (sdl == NULL)
791148641Srwatson			return ENOMEM;
792109771Sfjoe		sdl->sdl_len = sizeof *sdl;
793109771Sfjoe		sdl->sdl_family = AF_LINK;
794109771Sfjoe		sdl->sdl_index = ifp->if_index;
795109771Sfjoe		sdl->sdl_type = IFT_ARCNET;
796109771Sfjoe		sdl->sdl_alen = ARC_ADDR_LEN;
797109771Sfjoe		*LLADDR(sdl) = 0;
798109771Sfjoe		*llsa = (struct sockaddr *)sdl;
799109771Sfjoe		return 0;
800109771Sfjoe#endif
801109771Sfjoe#ifdef INET6
802109771Sfjoe	case AF_INET6:
803109771Sfjoe		sin6 = (struct sockaddr_in6 *)sa;
804109771Sfjoe		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
805109771Sfjoe			/*
806109771Sfjoe			 * An IP6 address of 0 means listen to all
807109771Sfjoe			 * of the Ethernet multicast address used for IP6.
808109771Sfjoe			 * (This is used for multicast routers.)
809109771Sfjoe			 */
810109771Sfjoe			ifp->if_flags |= IFF_ALLMULTI;
811109771Sfjoe			*llsa = 0;
812109771Sfjoe			return 0;
81389099Sfjoe		}
814109771Sfjoe		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
815109771Sfjoe			return EADDRNOTAVAIL;
816184205Sdes		sdl = malloc(sizeof *sdl, M_IFMADDR,
817148641Srwatson		       M_NOWAIT | M_ZERO);
818148641Srwatson		if (sdl == NULL)
819148641Srwatson			return ENOMEM;
820109771Sfjoe		sdl->sdl_len = sizeof *sdl;
821109771Sfjoe		sdl->sdl_family = AF_LINK;
822109771Sfjoe		sdl->sdl_index = ifp->if_index;
823109771Sfjoe		sdl->sdl_type = IFT_ARCNET;
824109771Sfjoe		sdl->sdl_alen = ARC_ADDR_LEN;
825109771Sfjoe		*LLADDR(sdl) = 0;
826109771Sfjoe		*llsa = (struct sockaddr *)sdl;
827109771Sfjoe		return 0;
82889099Sfjoe#endif
829109771Sfjoe
830109771Sfjoe	default:
831109771Sfjoe		/*
832109771Sfjoe		 * Well, the text isn't quite right, but it's the name
833109771Sfjoe		 * that counts...
834109771Sfjoe		 */
835109771Sfjoe		return EAFNOSUPPORT;
83689099Sfjoe	}
83789099Sfjoe}
838147256Sbrooks
839227293Sedstatic MALLOC_DEFINE(M_ARCCOM, "arccom", "ARCNET interface internals");
840147256Sbrooks
841147256Sbrooksstatic void*
842147256Sbrooksarc_alloc(u_char type, struct ifnet *ifp)
843147256Sbrooks{
844147256Sbrooks	struct arccom	*ac;
845147256Sbrooks
846147256Sbrooks	ac = malloc(sizeof(struct arccom), M_ARCCOM, M_WAITOK | M_ZERO);
847147256Sbrooks	ac->ac_ifp = ifp;
848147256Sbrooks
849147256Sbrooks	return (ac);
850147256Sbrooks}
851147256Sbrooks
852147256Sbrooksstatic void
853147256Sbrooksarc_free(void *com, u_char type)
854147256Sbrooks{
855147256Sbrooks
856147256Sbrooks	free(com, M_ARCCOM);
857147256Sbrooks}
858147256Sbrooks
859147256Sbrooksstatic int
860147256Sbrooksarc_modevent(module_t mod, int type, void *data)
861147256Sbrooks{
862147256Sbrooks
863147256Sbrooks	switch (type) {
864147256Sbrooks	case MOD_LOAD:
865147256Sbrooks		if_register_com_alloc(IFT_ARCNET, arc_alloc, arc_free);
866147256Sbrooks		break;
867147256Sbrooks	case MOD_UNLOAD:
868147256Sbrooks		if_deregister_com_alloc(IFT_ARCNET);
869147256Sbrooks		break;
870147256Sbrooks	default:
871147256Sbrooks		return EOPNOTSUPP;
872147256Sbrooks	}
873147256Sbrooks
874147256Sbrooks	return (0);
875147256Sbrooks}
876147256Sbrooks
877147256Sbrooksstatic moduledata_t arc_mod = {
878147256Sbrooks	"arcnet",
879147256Sbrooks	arc_modevent,
880241394Skevlo	0
881147256Sbrooks};
882147256Sbrooks
883147256SbrooksDECLARE_MODULE(arcnet, arc_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
884147256SbrooksMODULE_VERSION(arcnet, 1);
885