if_ether.c revision 120626
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1988, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
3450477Speter * $FreeBSD: head/sys/netinet/if_ether.c 120626 2003-10-01 08:32:37Z ru $
351541Srgrimes */
361541Srgrimes
371541Srgrimes/*
381541Srgrimes * Ethernet address resolution protocol.
391541Srgrimes * TODO:
401541Srgrimes *	add "inuse/lock" bit (or ref. count) along with valid bit
411541Srgrimes */
421541Srgrimes
4332350Seivind#include "opt_inet.h"
4441793Sluigi#include "opt_bdg.h"
45101090Srwatson#include "opt_mac.h"
4632350Seivind
471541Srgrimes#include <sys/param.h>
4812693Sphk#include <sys/kernel.h>
4944078Sdfr#include <sys/queue.h>
5012693Sphk#include <sys/sysctl.h>
511541Srgrimes#include <sys/systm.h>
52101090Srwatson#include <sys/mac.h>
5312693Sphk#include <sys/mbuf.h>
541541Srgrimes#include <sys/malloc.h>
5518892Sbde#include <sys/socket.h>
561541Srgrimes#include <sys/syslog.h>
571541Srgrimes
581541Srgrimes#include <net/if.h>
591541Srgrimes#include <net/if_dl.h>
6044165Sjulian#include <net/if_types.h>
611541Srgrimes#include <net/route.h>
628426Swollman#include <net/netisr.h>
6358313Slile#include <net/if_llc.h>
6471963Sjulian#ifdef BRIDGE
6571963Sjulian#include <net/ethernet.h>
6671963Sjulian#include <net/bridge.h>
6771963Sjulian#endif
681541Srgrimes
691541Srgrimes#include <netinet/in.h>
701541Srgrimes#include <netinet/in_var.h>
711541Srgrimes#include <netinet/if_ether.h>
721541Srgrimes
7384931Sfjoe#include <net/if_arc.h>
7444627Sjulian#include <net/iso88025.h>
7544627Sjulian
761541Srgrimes#define SIN(s) ((struct sockaddr_in *)s)
771541Srgrimes#define SDL(s) ((struct sockaddr_dl *)s)
781541Srgrimes
7944078SdfrSYSCTL_DECL(_net_link_ether);
8012942SwollmanSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
811541Srgrimes
8212693Sphk/* timer values */
8312942Swollmanstatic int arpt_prune = (5*60*1); /* walk list every 5 minutes */
8412942Swollmanstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
8512942Swollmanstatic int arpt_down = 20;	/* once declared down, don't send for 20 sec */
861541Srgrimes
8712942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
8812942Swollman	   &arpt_prune, 0, "");
8912942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
9012942Swollman	   &arpt_keep, 0, "");
9112942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
9212942Swollman	   &arpt_down, 0, "");
9312693Sphk
941541Srgrimes#define	rt_expire rt_rmx.rmx_expire
951541Srgrimes
9611225Swollmanstruct llinfo_arp {
9760938Sjake	LIST_ENTRY(llinfo_arp) la_le;
9811225Swollman	struct	rtentry *la_rt;
99110308Sorion	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
100110544Sorion	u_short	la_preempt;	/* countdown for pre-expiry arps */
101110308Sorion	u_short	la_asked;	/* #times we QUERIED following expiration */
10211225Swollman#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
10311225Swollman};
10412693Sphk
10560938Sjakestatic	LIST_HEAD(, llinfo_arp) llinfo_arp;
10611225Swollman
107111888Sjlemonstatic struct	ifqueue arpintrq;
10898459Speterstatic int	arp_inuse, arp_allocated, arpinit_done;
1091541Srgrimes
11012693Sphkstatic int	arp_maxtries = 5;
11112942Swollmanstatic int	useloopback = 1; /* use loopback interface for local traffic */
11212942Swollmanstatic int	arp_proxyall = 0;
1133282Swollman
11412942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
11512942Swollman	   &arp_maxtries, 0, "");
11612942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
11712942Swollman	   &useloopback, 0, "");
11812942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
11912942Swollman	   &arp_proxyall, 0, "");
12012693Sphk
12192723Salfredstatic void	arp_init(void);
12292723Salfredstatic void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
12392723Salfredstatic void	arprequest(struct ifnet *,
12492723Salfred			struct in_addr *, struct in_addr *, u_char *);
125111888Sjlemonstatic void	arpintr(struct mbuf *);
12692723Salfredstatic void	arptfree(struct llinfo_arp *);
12792723Salfredstatic void	arptimer(void *);
12812693Sphkstatic struct llinfo_arp
12992723Salfred		*arplookup(u_long, int, int);
13032350Seivind#ifdef INET
13192723Salfredstatic void	in_arpinput(struct mbuf *);
13232350Seivind#endif
13312693Sphk
1341541Srgrimes/*
1351541Srgrimes * Timeout routine.  Age arp_tab entries periodically.
1361541Srgrimes */
1371541Srgrimes/* ARGSUSED */
1381541Srgrimesstatic void
1391541Srgrimesarptimer(ignored_arg)
1401541Srgrimes	void *ignored_arg;
1411541Srgrimes{
142109409Shsu	struct llinfo_arp *la, *ola;
1431541Srgrimes	int s = splnet();
1441541Srgrimes
145109996Shsu	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
146109409Shsu	la = LIST_FIRST(&llinfo_arp);
147109409Shsu	while (la != NULL) {
148109409Shsu		struct rtentry *rt = la->la_rt;
149109409Shsu		ola = la;
15071999Sphk		la = LIST_NEXT(la, la_le);
15134961Sphk		if (rt->rt_expire && rt->rt_expire <= time_second)
152109409Shsu			arptfree(ola);		/* timer has expired, clear */
1531541Srgrimes	}
154109996Shsu	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
1551541Srgrimes	splx(s);
156109996Shsu	timeout(arptimer, NULL, arpt_prune * hz);
1571541Srgrimes}
1581541Srgrimes
1591541Srgrimes/*
1601541Srgrimes * Parallel to llc_rtrequest.
1611541Srgrimes */
1625196Swollmanstatic void
16385074Sruarp_rtrequest(req, rt, info)
1641541Srgrimes	int req;
1651541Srgrimes	register struct rtentry *rt;
16685074Sru	struct rt_addrinfo *info;
1671541Srgrimes{
1681541Srgrimes	register struct sockaddr *gate = rt->rt_gateway;
1691541Srgrimes	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
1701541Srgrimes	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1711541Srgrimes
1721541Srgrimes	if (!arpinit_done) {
1731541Srgrimes		arpinit_done = 1;
1741541Srgrimes		timeout(arptimer, (caddr_t)0, hz);
1751541Srgrimes	}
1761541Srgrimes	if (rt->rt_flags & RTF_GATEWAY)
1771541Srgrimes		return;
1781541Srgrimes	switch (req) {
1791541Srgrimes
1801541Srgrimes	case RTM_ADD:
1811541Srgrimes		/*
1821541Srgrimes		 * XXX: If this is a manually added route to interface
1831541Srgrimes		 * such as older version of routed or gated might provide,
1841541Srgrimes		 * restore cloning bit.
1851541Srgrimes		 */
1861541Srgrimes		if ((rt->rt_flags & RTF_HOST) == 0 &&
1871541Srgrimes		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1881541Srgrimes			rt->rt_flags |= RTF_CLONING;
1891541Srgrimes		if (rt->rt_flags & RTF_CLONING) {
1901541Srgrimes			/*
1911541Srgrimes			 * Case 1: This route should come from a route to iface.
1921541Srgrimes			 */
1931541Srgrimes			rt_setgate(rt, rt_key(rt),
1941541Srgrimes					(struct sockaddr *)&null_sdl);
1951541Srgrimes			gate = rt->rt_gateway;
1961541Srgrimes			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
1971541Srgrimes			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
19834961Sphk			rt->rt_expire = time_second;
1991541Srgrimes			break;
2001541Srgrimes		}
2011541Srgrimes		/* Announce a new entry if requested. */
2021541Srgrimes		if (rt->rt_flags & RTF_ANNOUNCE)
20384931Sfjoe			arprequest(rt->rt_ifp,
20436908Sjulian			    &SIN(rt_key(rt))->sin_addr,
20536908Sjulian			    &SIN(rt_key(rt))->sin_addr,
2061541Srgrimes			    (u_char *)LLADDR(SDL(gate)));
2071541Srgrimes		/*FALLTHROUGH*/
2081541Srgrimes	case RTM_RESOLVE:
2091541Srgrimes		if (gate->sa_family != AF_LINK ||
2101541Srgrimes		    gate->sa_len < sizeof(null_sdl)) {
2116568Sdg			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
2121541Srgrimes			break;
2131541Srgrimes		}
2141541Srgrimes		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
2151541Srgrimes		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
2161541Srgrimes		if (la != 0)
2171541Srgrimes			break; /* This happens on a route change */
2181541Srgrimes		/*
2191541Srgrimes		 * Case 2:  This route may come from cloning, or a manual route
2201541Srgrimes		 * add with a LL address.
2211541Srgrimes		 */
2221541Srgrimes		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
2231541Srgrimes		rt->rt_llinfo = (caddr_t)la;
2241541Srgrimes		if (la == 0) {
2251541Srgrimes			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
2261541Srgrimes			break;
2271541Srgrimes		}
2281541Srgrimes		arp_inuse++, arp_allocated++;
2291541Srgrimes		Bzero(la, sizeof(*la));
2301541Srgrimes		la->la_rt = rt;
2311541Srgrimes		rt->rt_flags |= RTF_LLINFO;
232109996Shsu		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
23311225Swollman		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
23413926Swollman
23532350Seivind#ifdef INET
23613926Swollman		/*
23713926Swollman		 * This keeps the multicast addresses from showing up
23813926Swollman		 * in `arp -a' listings as unresolved.  It's not actually
23913926Swollman		 * functional.  Then the same for broadcast.
24013926Swollman		 */
24187776Sjlemon		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
24287776Sjlemon		    rt->rt_ifp->if_type != IFT_ARCNET) {
24313926Swollman			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
24413926Swollman					       LLADDR(SDL(gate)));
24513926Swollman			SDL(gate)->sdl_alen = 6;
24616576Speter			rt->rt_expire = 0;
24713926Swollman		}
24813926Swollman		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
24984931Sfjoe			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
25084931Sfjoe			       rt->rt_ifp->if_addrlen);
25184931Sfjoe			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
25216576Speter			rt->rt_expire = 0;
25313926Swollman		}
25432350Seivind#endif
25513926Swollman
2561541Srgrimes		if (SIN(rt_key(rt))->sin_addr.s_addr ==
2571541Srgrimes		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
2581541Srgrimes		    /*
2591541Srgrimes		     * This test used to be
2601541Srgrimes		     *	if (loif.if_flags & IFF_UP)
2611541Srgrimes		     * It allowed local traffic to be forced
2621541Srgrimes		     * through the hardware by configuring the loopback down.
2631541Srgrimes		     * However, it causes problems during network configuration
2641541Srgrimes		     * for boards that can't receive packets they send.
2651541Srgrimes		     * It is now necessary to clear "useloopback" and remove
2661541Srgrimes		     * the route to force traffic out to the hardware.
2671541Srgrimes		     */
2681541Srgrimes			rt->rt_expire = 0;
26984931Sfjoe			Bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
27084931Sfjoe			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
2711541Srgrimes			if (useloopback)
2728090Spst				rt->rt_ifp = loif;
2731541Srgrimes
2741541Srgrimes		}
2751541Srgrimes		break;
2761541Srgrimes
2771541Srgrimes	case RTM_DELETE:
2781541Srgrimes		if (la == 0)
2791541Srgrimes			break;
2801541Srgrimes		arp_inuse--;
281109996Shsu		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
28211225Swollman		LIST_REMOVE(la, la_le);
2831541Srgrimes		rt->rt_llinfo = 0;
2841541Srgrimes		rt->rt_flags &= ~RTF_LLINFO;
2851541Srgrimes		if (la->la_hold)
2861541Srgrimes			m_freem(la->la_hold);
2871541Srgrimes		Free((caddr_t)la);
2881541Srgrimes	}
2891541Srgrimes}
2901541Srgrimes
2911541Srgrimes/*
2921541Srgrimes * Broadcast an ARP request. Caller specifies:
2931541Srgrimes *	- arp header source ip address
2941541Srgrimes *	- arp header target ip address
2951541Srgrimes *	- arp header source ethernet address
2961541Srgrimes */
2971541Srgrimesstatic void
29884931Sfjoearprequest(ifp, sip, tip, enaddr)
29984931Sfjoe	register struct ifnet *ifp;
30036908Sjulian	register struct in_addr *sip, *tip;
3011541Srgrimes	register u_char *enaddr;
3021541Srgrimes{
3031541Srgrimes	register struct mbuf *m;
3041541Srgrimes	register struct ether_header *eh;
30584931Sfjoe	register struct arc_header *arh;
30684931Sfjoe	register struct arphdr *ah;
3071541Srgrimes	struct sockaddr sa;
30858313Slile	static u_char	llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
30958313Slile				   LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
31084931Sfjoe	u_short ar_hrd;
3111541Srgrimes
312111119Simp	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
3131541Srgrimes		return;
31444456Swpaul	m->m_pkthdr.rcvif = (struct ifnet *)0;
315101090Srwatson#ifdef MAC
316101090Srwatson	mac_create_mbuf_linklayer(ifp, m);
317101090Srwatson#endif
31884931Sfjoe	switch (ifp->if_type) {
31984931Sfjoe	case IFT_ARCNET:
32084931Sfjoe		ar_hrd = htons(ARPHRD_ARCNET);
32184931Sfjoe
32284931Sfjoe		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
32384931Sfjoe		m->m_pkthdr.len = m->m_len;
32484931Sfjoe		MH_ALIGN(m, m->m_len);
32584931Sfjoe
32684931Sfjoe		arh = (struct arc_header *)sa.sa_data;
32784931Sfjoe		arh->arc_dhost = *ifp->if_broadcastaddr;
32884931Sfjoe		arh->arc_type = ARCTYPE_ARP;
32984931Sfjoe
33084931Sfjoe		ah = mtod(m, struct arphdr *);
33184931Sfjoe		break;
33284931Sfjoe
33344627Sjulian	case IFT_ISO88025:
33484931Sfjoe		ar_hrd = htons(ARPHRD_IEEE802);
33584931Sfjoe
33684931Sfjoe		m->m_len = sizeof(llcx) +
33784931Sfjoe		    arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
33884931Sfjoe		m->m_pkthdr.len = m->m_len;
33984931Sfjoe		MH_ALIGN(m, m->m_len);
34084931Sfjoe
34158313Slile		(void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx));
34284931Sfjoe		(void)memcpy(sa.sa_data, ifp->if_broadcastaddr, 6);
34344627Sjulian		(void)memcpy(sa.sa_data + 6, enaddr, 6);
34458313Slile		sa.sa_data[6] |= TR_RII;
34558313Slile		sa.sa_data[12] = TR_AC;
34658313Slile		sa.sa_data[13] = TR_LLC_FRAME;
34784931Sfjoe
34884931Sfjoe		ah = (struct arphdr *)(mtod(m, char *) + sizeof(llcx));
34944627Sjulian		break;
35051320Slile	case IFT_FDDI:
35151320Slile	case IFT_ETHER:
35251320Slile		/*
35351320Slile		 * This may not be correct for types not explicitly
35451320Slile		 * listed, but this is our best guess
35551320Slile		 */
35644627Sjulian	default:
35784931Sfjoe		ar_hrd = htons(ARPHRD_ETHER);
35884931Sfjoe
35984931Sfjoe		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
36084931Sfjoe		m->m_pkthdr.len = m->m_len;
36184931Sfjoe		MH_ALIGN(m, m->m_len);
36284931Sfjoe
36351320Slile		eh = (struct ether_header *)sa.sa_data;
36451320Slile		/* if_output will not swap */
36551320Slile		eh->ether_type = htons(ETHERTYPE_ARP);
36684931Sfjoe		(void)memcpy(eh->ether_dhost, ifp->if_broadcastaddr,
36751320Slile		    sizeof(eh->ether_dhost));
36884931Sfjoe
36984931Sfjoe		ah = mtod(m, struct arphdr *);
37051320Slile		break;
37144627Sjulian	}
37284931Sfjoe
37384931Sfjoe	ah->ar_hrd = ar_hrd;
37484931Sfjoe	ah->ar_pro = htons(ETHERTYPE_IP);
37584931Sfjoe	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
37684931Sfjoe	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
37784931Sfjoe	ah->ar_op = htons(ARPOP_REQUEST);
37884931Sfjoe	(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
379109035Stmm	memset(ar_tha(ah), 0, ah->ar_hln);
38084931Sfjoe	(void)memcpy(ar_spa(ah), sip, ah->ar_pln);
38184931Sfjoe	(void)memcpy(ar_tpa(ah), tip, ah->ar_pln);
38284931Sfjoe
3831541Srgrimes	sa.sa_family = AF_UNSPEC;
3841541Srgrimes	sa.sa_len = sizeof(sa);
38584931Sfjoe	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
3861541Srgrimes}
3871541Srgrimes
3881541Srgrimes/*
3891541Srgrimes * Resolve an IP address into an ethernet address.  If success,
3901541Srgrimes * desten is filled in.  If there is no entry in arptab,
3911541Srgrimes * set one up and broadcast a request for the IP address.
3921541Srgrimes * Hold onto this mbuf and resend it once the address
3931541Srgrimes * is finally resolved.  A return value of 1 indicates
3941541Srgrimes * that desten has been filled in and the packet should be sent
3951541Srgrimes * normally; a 0 return indicates that the packet has been
3961541Srgrimes * taken over here, either now or for later transmission.
3971541Srgrimes */
3981541Srgrimesint
39984931Sfjoearpresolve(ifp, rt, m, dst, desten, rt0)
40084931Sfjoe	register struct ifnet *ifp;
4011541Srgrimes	register struct rtentry *rt;
4021541Srgrimes	struct mbuf *m;
4031541Srgrimes	register struct sockaddr *dst;
4041541Srgrimes	register u_char *desten;
4053514Swollman	struct rtentry *rt0;
4061541Srgrimes{
40778295Sjlemon	struct llinfo_arp *la = 0;
4081541Srgrimes	struct sockaddr_dl *sdl;
4091541Srgrimes
4101541Srgrimes	if (m->m_flags & M_BCAST) {	/* broadcast */
41184931Sfjoe		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
4121541Srgrimes		return (1);
4131541Srgrimes	}
41484931Sfjoe	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
4151541Srgrimes		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
4161541Srgrimes		return(1);
4171541Srgrimes	}
4181541Srgrimes	if (rt)
4191541Srgrimes		la = (struct llinfo_arp *)rt->rt_llinfo;
42042775Sfenner	if (la == 0) {
4213311Sphk		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
4223311Sphk		if (la)
4231541Srgrimes			rt = la->la_rt;
4241541Srgrimes	}
4251541Srgrimes	if (la == 0 || rt == 0) {
42636308Sphk		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
42736308Sphk			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
42836308Sphk				rt ? "rt" : "");
4291541Srgrimes		m_freem(m);
4301541Srgrimes		return (0);
4311541Srgrimes	}
4321541Srgrimes	sdl = SDL(rt->rt_gateway);
4331541Srgrimes	/*
4341541Srgrimes	 * Check the address family and length is valid, the address
4351541Srgrimes	 * is resolved; otherwise, try to resolve.
4361541Srgrimes	 */
43734961Sphk	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
4381541Srgrimes	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
43992802Sorion		/*
44092802Sorion		 * If entry has an expiry time and it is approaching,
44192802Sorion		 * see if we need to send an ARP request within this
44292802Sorion		 * arpt_down interval.
44392802Sorion		 */
44492802Sorion		if ((rt->rt_expire != 0) &&
445110544Sorion		    (time_second + la->la_preempt > rt->rt_expire)) {
44692802Sorion			arprequest(ifp,
44792802Sorion				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
44892802Sorion				   &SIN(dst)->sin_addr,
44992802Sorion				   IF_LLADDR(ifp));
450110544Sorion			la->la_preempt--;
45192802Sorion		}
45292802Sorion
45316206Sbde		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
4541541Srgrimes		return 1;
4551541Srgrimes	}
4561541Srgrimes	/*
457120626Sru	 * If ARP is disabled or static on this interface, stop.
45878295Sjlemon	 * XXX
45978295Sjlemon	 * Probably should not allocate empty llinfo struct if we are
46078295Sjlemon	 * not going to be sending out an arp request.
46178295Sjlemon	 */
462120626Sru	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
46387410Sru		m_freem(m);
46478295Sjlemon		return (0);
46587410Sru	}
46678295Sjlemon	/*
4671541Srgrimes	 * There is an arptab entry, but no ethernet address
4681541Srgrimes	 * response yet.  Replace the held mbuf with this
4691541Srgrimes	 * latest one.
4701541Srgrimes	 */
4711541Srgrimes	if (la->la_hold)
4721541Srgrimes		m_freem(la->la_hold);
4731541Srgrimes	la->la_hold = m;
4741541Srgrimes	if (rt->rt_expire) {
4751541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
47634961Sphk		if (la->la_asked == 0 || rt->rt_expire != time_second) {
47734961Sphk			rt->rt_expire = time_second;
478110308Sorion			if (la->la_asked++ < arp_maxtries) {
479110308Sorion				arprequest(ifp,
480110308Sorion					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
481110308Sorion					   &SIN(dst)->sin_addr,
482110308Sorion					   IF_LLADDR(ifp));
483110308Sorion			} else {
4841541Srgrimes				rt->rt_flags |= RTF_REJECT;
4851541Srgrimes				rt->rt_expire += arpt_down;
486110544Sorion				la->la_asked = 0;
487110544Sorion				la->la_preempt = arp_maxtries;
4881541Srgrimes			}
4891541Srgrimes
4901541Srgrimes		}
4911541Srgrimes	}
4921541Srgrimes	return (0);
4931541Srgrimes}
4941541Srgrimes
4951541Srgrimes/*
4961541Srgrimes * Common length and type checks are done here,
4971541Srgrimes * then the protocol-specific routine is called.
4981541Srgrimes */
49912693Sphkstatic void
500111888Sjlemonarpintr(struct mbuf *m)
5011541Srgrimes{
502111888Sjlemon	struct arphdr *ar;
5031541Srgrimes
50498459Speter	if (!arpinit_done) {
50598459Speter		arpinit_done = 1;
50698459Speter		timeout(arptimer, (caddr_t)0, hz);
50798459Speter	}
508111888Sjlemon	if (m->m_len < sizeof(struct arphdr) &&
509111888Sjlemon	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
510111888Sjlemon		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
511111888Sjlemon		return;
512111888Sjlemon	}
513111888Sjlemon	ar = mtod(m, struct arphdr *);
5141541Srgrimes
515111888Sjlemon	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
516111888Sjlemon	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
517111888Sjlemon	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET) {
518111888Sjlemon		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
519111888Sjlemon		    (unsigned char *)&ar->ar_hrd, "");
520111888Sjlemon		m_freem(m);
521111888Sjlemon		return;
522111888Sjlemon	}
5231541Srgrimes
524111888Sjlemon	if (m->m_pkthdr.len < arphdr_len(ar) &&
525111888Sjlemon	    (m = m_pullup(m, arphdr_len(ar))) == NULL) {
526111888Sjlemon		log(LOG_ERR, "arp: runt packet\n");
527111888Sjlemon		m_freem(m);
528111888Sjlemon		return;
529111888Sjlemon	}
53057900Srwatson
531111888Sjlemon	switch (ntohs(ar->ar_pro)) {
53232350Seivind#ifdef INET
533111888Sjlemon	case ETHERTYPE_IP:
534111888Sjlemon		in_arpinput(m);
535111888Sjlemon		return;
53632350Seivind#endif
5371541Srgrimes	}
538111888Sjlemon	m_freem(m);
5391541Srgrimes}
5401541Srgrimes
54132350Seivind#ifdef INET
5421541Srgrimes/*
5431541Srgrimes * ARP for Internet protocols on 10 Mb/s Ethernet.
5441541Srgrimes * Algorithm is that given in RFC 826.
5451541Srgrimes * In addition, a sanity check is performed on the sender
5461541Srgrimes * protocol address, to catch impersonators.
5471541Srgrimes * We no longer handle negotiations for use of trailer protocol:
5481541Srgrimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
5491541Srgrimes * along with IP replies if we wanted trailers sent to us,
5501541Srgrimes * and also sent them in response to IP replies.
5511541Srgrimes * This allowed either end to announce the desire to receive
5521541Srgrimes * trailer packets.
5531541Srgrimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
5541541Srgrimes * but formerly didn't normally send requests.
5551541Srgrimes */
55670699Salfredstatic int log_arp_wrong_iface = 1;
55782893Salfredstatic int log_arp_movements = 1;
55870699Salfred
55970699SalfredSYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
56070699Salfred	&log_arp_wrong_iface, 0,
56170699Salfred	"log arp packets arriving on the wrong interface");
56282893SalfredSYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
56382893Salfred        &log_arp_movements, 0,
56482966Salfred        "log arp replies from MACs different than the one in the cache");
56570699Salfred
56682893Salfred
5671541Srgrimesstatic void
5681541Srgrimesin_arpinput(m)
5691541Srgrimes	struct mbuf *m;
5701541Srgrimes{
57184931Sfjoe	register struct arphdr *ah;
57284931Sfjoe	register struct ifnet *ifp = m->m_pkthdr.rcvif;
5731541Srgrimes	struct ether_header *eh;
57484931Sfjoe	struct arc_header *arh;
57544627Sjulian	struct iso88025_header *th = (struct iso88025_header *)0;
57696184Skbyanc	struct iso88025_sockaddr_dl_data *trld;
5771541Srgrimes	register struct llinfo_arp *la = 0;
5781541Srgrimes	register struct rtentry *rt;
57984102Sjlemon	struct ifaddr *ifa;
58084102Sjlemon	struct in_ifaddr *ia;
5811541Srgrimes	struct sockaddr_dl *sdl;
5821541Srgrimes	struct sockaddr sa;
5831541Srgrimes	struct in_addr isaddr, itaddr, myaddr;
58458313Slile	int op, rif_len;
58584931Sfjoe	int req_len;
5861541Srgrimes
58784931Sfjoe	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
58884931Sfjoe	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
58974851Syar		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
59074851Syar		return;
59174851Syar	}
59274851Syar
59384931Sfjoe	ah = mtod(m, struct arphdr *);
59484931Sfjoe	op = ntohs(ah->ar_op);
59584931Sfjoe	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
59684931Sfjoe	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
59772056Sjulian#ifdef BRIDGE
59872056Sjulian#define BRIDGE_TEST (do_bridge)
59971963Sjulian#else
60072056Sjulian#define BRIDGE_TEST (0) /* cc will optimise the test away */
60141793Sluigi#endif
60284102Sjlemon	/*
60384102Sjlemon	 * For a bridge, we want to check the address irrespective
60484102Sjlemon	 * of the receive interface. (This will change slightly
60584102Sjlemon	 * when we have clusters of interfaces).
60684102Sjlemon	 */
60784102Sjlemon	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash)
60884931Sfjoe		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
60984102Sjlemon		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
61084102Sjlemon			goto match;
61184102Sjlemon	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
61284931Sfjoe		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
61384102Sjlemon		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
61484102Sjlemon			goto match;
61584102Sjlemon	/*
61685223Sjlemon	 * No match, use the first inet address on the receive interface
61784102Sjlemon	 * as a dummy address for the rest of the function.
61884102Sjlemon	 */
61985223Sjlemon	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
62085466Sjlemon		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
62185466Sjlemon			ia = ifatoia(ifa);
62285466Sjlemon			goto match;
62385466Sjlemon		}
62485466Sjlemon	/*
62585466Sjlemon	 * If bridging, fall back to using any inet address.
62685466Sjlemon	 */
62785466Sjlemon	if (!BRIDGE_TEST ||
62885466Sjlemon	    (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) {
62912693Sphk		m_freem(m);
63012693Sphk		return;
63112693Sphk	}
63284102Sjlemonmatch:
63384102Sjlemon	myaddr = ia->ia_addr.sin_addr;
63484931Sfjoe	if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) {
63512693Sphk		m_freem(m);	/* it's from me, ignore it. */
63612693Sphk		return;
63712693Sphk	}
63884931Sfjoe	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
6391541Srgrimes		log(LOG_ERR,
64084931Sfjoe		    "arp: link address is broadcast for IP address %s!\n",
6417088Swollman		    inet_ntoa(isaddr));
64212693Sphk		m_freem(m);
64312693Sphk		return;
6441541Srgrimes	}
6451541Srgrimes	if (isaddr.s_addr == myaddr.s_addr) {
6461541Srgrimes		log(LOG_ERR,
64784931Sfjoe		   "arp: %*D is using my IP address %s!\n",
64884931Sfjoe		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
64984931Sfjoe		   inet_ntoa(isaddr));
6501541Srgrimes		itaddr = myaddr;
6511541Srgrimes		goto reply;
6521541Srgrimes	}
653120626Sru	if (ifp->if_flags & IFF_STATICARP)
654120626Sru		goto reply;
6551541Srgrimes	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
6561541Srgrimes	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
65772270Sluigi		/* the following is not an error when doing bridging */
65884931Sfjoe		if (!BRIDGE_TEST && rt->rt_ifp != ifp) {
65972270Sluigi			if (log_arp_wrong_iface)
66084931Sfjoe				log(LOG_ERR, "arp: %s is on %s%d but got reply from %*D on %s%d\n",
66171963Sjulian				    inet_ntoa(isaddr),
66271963Sjulian				    rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
66384931Sfjoe				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
66484931Sfjoe				    ifp->if_name, ifp->if_unit);
66572270Sluigi			goto reply;
66639389Sfenner		}
6671541Srgrimes		if (sdl->sdl_alen &&
66884931Sfjoe		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
66982893Salfred			if (rt->rt_expire) {
67082893Salfred			    if (log_arp_movements)
67184931Sfjoe			        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s%d\n",
67284931Sfjoe				    inet_ntoa(isaddr),
67384931Sfjoe				    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
67484931Sfjoe				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
67584931Sfjoe				    ifp->if_name, ifp->if_unit);
67682893Salfred			} else {
67739389Sfenner			    log(LOG_ERR,
67884931Sfjoe				"arp: %*D attempts to modify permanent entry for %s on %s%d\n",
67984931Sfjoe				ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
68084931Sfjoe				inet_ntoa(isaddr), ifp->if_name, ifp->if_unit);
68139389Sfenner			    goto reply;
68239389Sfenner			}
68346568Speter		}
68484931Sfjoe		/*
68584931Sfjoe		 * sanity check for the address length.
68684931Sfjoe		 * XXX this does not work for protocols with variable address
68784931Sfjoe		 * length. -is
68884931Sfjoe		 */
68984931Sfjoe		if (sdl->sdl_alen &&
69084931Sfjoe		    sdl->sdl_alen != ah->ar_hln) {
69184931Sfjoe			log(LOG_WARNING,
69284931Sfjoe			    "arp from %*D: new addr len %d, was %d",
69384931Sfjoe			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
69484931Sfjoe			    ah->ar_hln, sdl->sdl_alen);
69584931Sfjoe		}
69684931Sfjoe		if (ifp->if_addrlen != ah->ar_hln) {
69784931Sfjoe			log(LOG_WARNING,
69884931Sfjoe			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
69984931Sfjoe			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
70084931Sfjoe			    ah->ar_hln, ifp->if_addrlen);
70184931Sfjoe			goto reply;
70284931Sfjoe		}
70384931Sfjoe		(void)memcpy(LLADDR(sdl), ar_sha(ah),
70484931Sfjoe		    sdl->sdl_alen = ah->ar_hln);
70545705Seivind		/*
70645705Seivind		 * If we receive an arp from a token-ring station over
70745705Seivind		 * a token-ring nic then try to save the source
70845705Seivind		 * routing info.
70945705Seivind		 */
71084931Sfjoe		if (ifp->if_type == IFT_ISO88025) {
71144627Sjulian			th = (struct iso88025_header *)m->m_pkthdr.header;
71296184Skbyanc			trld = SDL_ISO88025(sdl);
71358313Slile			rif_len = TR_RCF_RIFLEN(th->rcf);
71458313Slile			if ((th->iso88025_shost[0] & TR_RII) &&
71558313Slile			    (rif_len > 2)) {
71696184Skbyanc				trld->trld_rcf = th->rcf;
71796184Skbyanc				trld->trld_rcf ^= htons(TR_RCF_DIR);
71896184Skbyanc				memcpy(trld->trld_route, th->rd, rif_len - 2);
71996184Skbyanc				trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
72051320Slile				/*
72151320Slile				 * Set up source routing information for
72251320Slile				 * reply packet (XXX)
72351320Slile				 */
72458313Slile				m->m_data -= rif_len;
72558313Slile				m->m_len  += rif_len;
72658313Slile				m->m_pkthdr.len += rif_len;
72744627Sjulian			} else {
72858313Slile				th->iso88025_shost[0] &= ~TR_RII;
72996624Skbyanc				trld->trld_rcf = 0;
73044627Sjulian			}
73150512Slile			m->m_data -= 8;
73250512Slile			m->m_len  += 8;
73350512Slile			m->m_pkthdr.len += 8;
73496184Skbyanc			th->rcf = trld->trld_rcf;
73544627Sjulian		}
7361541Srgrimes		if (rt->rt_expire)
73734961Sphk			rt->rt_expire = time_second + arpt_keep;
7381541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
739110544Sorion		la->la_asked = 0;
740110544Sorion		la->la_preempt = arp_maxtries;
7411541Srgrimes		if (la->la_hold) {
74284931Sfjoe			(*ifp->if_output)(ifp, la->la_hold,
7431541Srgrimes				rt_key(rt), rt);
7441541Srgrimes			la->la_hold = 0;
7451541Srgrimes		}
7461541Srgrimes	}
7471541Srgrimesreply:
7481541Srgrimes	if (op != ARPOP_REQUEST) {
7491541Srgrimes		m_freem(m);
7501541Srgrimes		return;
7511541Srgrimes	}
7521541Srgrimes	if (itaddr.s_addr == myaddr.s_addr) {
7531541Srgrimes		/* I am the target */
75484931Sfjoe		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
75584931Sfjoe		(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
7561541Srgrimes	} else {
7571541Srgrimes		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
7583282Swollman		if (la == NULL) {
7593282Swollman			struct sockaddr_in sin;
7603282Swollman
76112693Sphk			if (!arp_proxyall) {
76212693Sphk				m_freem(m);
76312693Sphk				return;
76412693Sphk			}
7653282Swollman
7663282Swollman			bzero(&sin, sizeof sin);
7673282Swollman			sin.sin_family = AF_INET;
7683282Swollman			sin.sin_len = sizeof sin;
7693282Swollman			sin.sin_addr = itaddr;
7703282Swollman
7715101Swollman			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
77212693Sphk			if (!rt) {
77312693Sphk				m_freem(m);
77412693Sphk				return;
77512693Sphk			}
7763282Swollman			/*
7773282Swollman			 * Don't send proxies for nodes on the same interface
7783282Swollman			 * as this one came out of, or we'll get into a fight
7793282Swollman			 * over who claims what Ether address.
7803282Swollman			 */
78184931Sfjoe			if (rt->rt_ifp == ifp) {
7823282Swollman				rtfree(rt);
78312693Sphk				m_freem(m);
78412693Sphk				return;
7853282Swollman			}
78684931Sfjoe			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
78784931Sfjoe			(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
7883282Swollman			rtfree(rt);
78963080Sdwmalone
79063080Sdwmalone			/*
79163080Sdwmalone			 * Also check that the node which sent the ARP packet
79263080Sdwmalone			 * is on the the interface we expect it to be on. This
79363080Sdwmalone			 * avoids ARP chaos if an interface is connected to the
79463080Sdwmalone			 * wrong network.
79563080Sdwmalone			 */
79663080Sdwmalone			sin.sin_addr = isaddr;
79763080Sdwmalone
79863080Sdwmalone			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
79963080Sdwmalone			if (!rt) {
80063080Sdwmalone				m_freem(m);
80163080Sdwmalone				return;
80263080Sdwmalone			}
80384931Sfjoe			if (rt->rt_ifp != ifp) {
80463080Sdwmalone				log(LOG_INFO, "arp_proxy: ignoring request"
80563080Sdwmalone				    " from %s via %s%d, expecting %s%d\n",
80684931Sfjoe				    inet_ntoa(isaddr), ifp->if_name,
80784931Sfjoe				    ifp->if_unit, rt->rt_ifp->if_name,
80863080Sdwmalone				    rt->rt_ifp->if_unit);
80963080Sdwmalone				rtfree(rt);
81063080Sdwmalone				m_freem(m);
81163080Sdwmalone				return;
81263080Sdwmalone			}
81363080Sdwmalone			rtfree(rt);
81463080Sdwmalone
8154069Swollman#ifdef DEBUG_PROXY
8168876Srgrimes			printf("arp: proxying for %s\n",
8177088Swollman			       inet_ntoa(itaddr));
8184069Swollman#endif
8193282Swollman		} else {
8203282Swollman			rt = la->la_rt;
82184931Sfjoe			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
8223282Swollman			sdl = SDL(rt->rt_gateway);
82384931Sfjoe			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
8243282Swollman		}
8251541Srgrimes	}
8261541Srgrimes
82784931Sfjoe	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
82884931Sfjoe	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
82984931Sfjoe	ah->ar_op = htons(ARPOP_REPLY);
83084931Sfjoe	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
83184931Sfjoe	switch (ifp->if_type) {
83284931Sfjoe	case IFT_ARCNET:
83384931Sfjoe		arh = (struct arc_header *)sa.sa_data;
83484931Sfjoe		arh->arc_dhost = *ar_tha(ah);
83584931Sfjoe		arh->arc_type = ARCTYPE_ARP;
83684931Sfjoe		break;
83784931Sfjoe
83845705Seivind	case IFT_ISO88025:
83944627Sjulian		/* Re-arrange the source/dest address */
84051320Slile		memcpy(th->iso88025_dhost, th->iso88025_shost,
84151320Slile		    sizeof(th->iso88025_dhost));
84284931Sfjoe		memcpy(th->iso88025_shost, IF_LLADDR(ifp),
84351320Slile		    sizeof(th->iso88025_shost));
84444627Sjulian		/* Set the source routing bit if neccesary */
84558313Slile		if (th->iso88025_dhost[0] & TR_RII) {
84658313Slile			th->iso88025_dhost[0] &= ~TR_RII;
84758313Slile			if (TR_RCF_RIFLEN(th->rcf) > 2)
84858313Slile				th->iso88025_shost[0] |= TR_RII;
84944627Sjulian		}
85044627Sjulian		/* Copy the addresses, ac and fc into sa_data */
85151320Slile		memcpy(sa.sa_data, th->iso88025_dhost,
85251320Slile		    sizeof(th->iso88025_dhost) * 2);
85358313Slile		sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC;
85458313Slile		sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME;
85544627Sjulian		break;
85645705Seivind	case IFT_ETHER:
85751320Slile	case IFT_FDDI:
85851320Slile	/*
85951320Slile	 * May not be correct for types not explictly
86051320Slile	 * listed, but it is our best guess.
86151320Slile	 */
86251320Slile	default:
86344627Sjulian		eh = (struct ether_header *)sa.sa_data;
86484931Sfjoe		(void)memcpy(eh->ether_dhost, ar_tha(ah),
86551320Slile		    sizeof(eh->ether_dhost));
86644627Sjulian		eh->ether_type = htons(ETHERTYPE_ARP);
86744627Sjulian		break;
86844627Sjulian	}
8691541Srgrimes	sa.sa_family = AF_UNSPEC;
8701541Srgrimes	sa.sa_len = sizeof(sa);
87184931Sfjoe	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
8721541Srgrimes	return;
8731541Srgrimes}
87432350Seivind#endif
8751541Srgrimes
8761541Srgrimes/*
8771541Srgrimes * Free an arp entry.
8781541Srgrimes */
8791541Srgrimesstatic void
8801541Srgrimesarptfree(la)
8811541Srgrimes	register struct llinfo_arp *la;
8821541Srgrimes{
8831541Srgrimes	register struct rtentry *rt = la->la_rt;
8841541Srgrimes	register struct sockaddr_dl *sdl;
8851541Srgrimes	if (rt == 0)
8861541Srgrimes		panic("arptfree");
8871541Srgrimes	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
8881541Srgrimes	    sdl->sdl_family == AF_LINK) {
8891541Srgrimes		sdl->sdl_alen = 0;
890110308Sorion		la->la_preempt = la->la_asked = 0;
8911541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
8921541Srgrimes		return;
8931541Srgrimes	}
8941541Srgrimes	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
8951541Srgrimes			0, (struct rtentry **)0);
8961541Srgrimes}
8971541Srgrimes/*
8981541Srgrimes * Lookup or enter a new address in arptab.
8991541Srgrimes */
9001541Srgrimesstatic struct llinfo_arp *
9011541Srgrimesarplookup(addr, create, proxy)
9021541Srgrimes	u_long addr;
9031541Srgrimes	int create, proxy;
9041541Srgrimes{
9051541Srgrimes	register struct rtentry *rt;
9061541Srgrimes	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
9074069Swollman	const char *why = 0;
9081541Srgrimes
9091541Srgrimes	sin.sin_addr.s_addr = addr;
9101541Srgrimes	sin.sin_other = proxy ? SIN_PROXY : 0;
9115101Swollman	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
9121541Srgrimes	if (rt == 0)
9131541Srgrimes		return (0);
9141541Srgrimes	rt->rt_refcnt--;
9154069Swollman
91612693Sphk	if (rt->rt_flags & RTF_GATEWAY)
9174069Swollman		why = "host is not on local network";
91812693Sphk	else if ((rt->rt_flags & RTF_LLINFO) == 0)
9194069Swollman		why = "could not allocate llinfo";
92012693Sphk	else if (rt->rt_gateway->sa_family != AF_LINK)
9214069Swollman		why = "gateway route is not ours";
9224069Swollman
923120383Sbms	if (why) {
924120383Sbms		if (create)
925120383Sbms			log(LOG_DEBUG, "arplookup %s failed: %s\n",
926120383Sbms			    inet_ntoa(sin.sin_addr), why);
927120383Sbms
928120383Sbms		/* If there are no references to this route, purge it */
929120418Sbms		if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
930120383Sbms			rtrequest(RTM_DELETE,
931120383Sbms					(struct sockaddr *)rt_key(rt),
932120383Sbms					rt->rt_gateway, rt_mask(rt),
933120383Sbms					rt->rt_flags, 0);
934120383Sbms		}
935120383Sbms		return (0);
9361541Srgrimes	}
9371541Srgrimes	return ((struct llinfo_arp *)rt->rt_llinfo);
9381541Srgrimes}
9391541Srgrimes
9405195Swollmanvoid
94184931Sfjoearp_ifinit(ifp, ifa)
94284931Sfjoe	struct ifnet *ifp;
9435195Swollman	struct ifaddr *ifa;
9445195Swollman{
94525822Stegge	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
94684931Sfjoe		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
94784931Sfjoe				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
9485195Swollman	ifa->ifa_rtrequest = arp_rtrequest;
9495195Swollman	ifa->ifa_flags |= RTF_CLONING;
9505195Swollman}
95169152Sjlemon
95269152Sjlemonstatic void
95369152Sjlemonarp_init(void)
95469152Sjlemon{
95569152Sjlemon
95669152Sjlemon	arpintrq.ifq_maxlen = 50;
95793818Sjhb	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
95898459Speter	LIST_INIT(&llinfo_arp);
959111888Sjlemon	netisr_register(NETISR_ARP, arpintr, &arpintrq);
96069152Sjlemon}
96169152Sjlemon
96269152SjlemonSYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
963