xref: /freebsd/sys/netinet/if_ether.c (revision 722012cc0c44c7c33ef83a4b3ce8178205ac5f78)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
34722012ccSJulian Elischer  * $Id: if_ether.c,v 1.53 1999/02/16 10:49:51 dfr Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes /*
38df8bae1dSRodney W. Grimes  * Ethernet address resolution protocol.
39df8bae1dSRodney W. Grimes  * TODO:
40df8bae1dSRodney W. Grimes  *	add "inuse/lock" bit (or ref. count) along with valid bit
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
431d5e9e22SEivind Eklund #include "opt_inet.h"
44b715f178SLuigi Rizzo #include "opt_bdg.h"
451d5e9e22SEivind Eklund 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47df8bae1dSRodney W. Grimes #include <sys/kernel.h>
48ce02431fSDoug Rabson #include <sys/queue.h>
49885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h>
50885f1aa4SPoul-Henning Kamp #include <sys/systm.h>
51885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h>
52885f1aa4SPoul-Henning Kamp #include <sys/malloc.h>
534458ac71SBruce Evans #include <sys/socket.h>
54885f1aa4SPoul-Henning Kamp #include <sys/syslog.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <net/if.h>
57df8bae1dSRodney W. Grimes #include <net/if_dl.h>
58722012ccSJulian Elischer #include <net/if_types.h>
59df8bae1dSRodney W. Grimes #include <net/route.h>
60748e0b0aSGarrett Wollman #include <net/netisr.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
64df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s)
67df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s)
68df8bae1dSRodney W. Grimes 
69ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether);
70602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes /* timer values */
73602d513cSGarrett Wollman static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
74602d513cSGarrett Wollman static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
75602d513cSGarrett Wollman static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
76885f1aa4SPoul-Henning Kamp 
77602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
78602d513cSGarrett Wollman 	   &arpt_prune, 0, "");
79602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
80602d513cSGarrett Wollman 	   &arpt_keep, 0, "");
81602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
82602d513cSGarrett Wollman 	   &arpt_down, 0, "");
83885f1aa4SPoul-Henning Kamp 
84df8bae1dSRodney W. Grimes #define	rt_expire rt_rmx.rmx_expire
85df8bae1dSRodney W. Grimes 
8666800f57SGarrett Wollman struct llinfo_arp {
8766800f57SGarrett Wollman 	LIST_ENTRY(llinfo_arp) la_le;
8866800f57SGarrett Wollman 	struct	rtentry *la_rt;
8966800f57SGarrett Wollman 	struct	mbuf *la_hold;		/* last packet until resolved/timeout */
9066800f57SGarrett Wollman 	long	la_asked;		/* last time we QUERIED for this addr */
9166800f57SGarrett Wollman #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
9266800f57SGarrett Wollman };
9366800f57SGarrett Wollman 
94885f1aa4SPoul-Henning Kamp static	LIST_HEAD(, llinfo_arp) llinfo_arp;
95df8bae1dSRodney W. Grimes 
96df8bae1dSRodney W. Grimes struct	ifqueue arpintrq = {0, 0, 0, 50};
97f708ef1bSPoul-Henning Kamp static int	arp_inuse, arp_allocated;
98df8bae1dSRodney W. Grimes 
99885f1aa4SPoul-Henning Kamp static int	arp_maxtries = 5;
100602d513cSGarrett Wollman static int	useloopback = 1; /* use loopback interface for local traffic */
101885f1aa4SPoul-Henning Kamp static int	arp_proxyall = 0;
102602d513cSGarrett Wollman 
103602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
104602d513cSGarrett Wollman 	   &arp_maxtries, 0, "");
105602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
106602d513cSGarrett Wollman 	   &useloopback, 0, "");
107602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
108602d513cSGarrett Wollman 	   &arp_proxyall, 0, "");
109885f1aa4SPoul-Henning Kamp 
110081d3b93SBruce Evans static void	arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
111ed7509acSJulian Elischer static void	arprequest __P((struct arpcom *,
112ed7509acSJulian Elischer 			struct in_addr *, struct in_addr *, u_char *));
113885f1aa4SPoul-Henning Kamp static void	arpintr __P((void));
114885f1aa4SPoul-Henning Kamp static void	arptfree __P((struct llinfo_arp *));
115885f1aa4SPoul-Henning Kamp static void	arptimer __P((void *));
116885f1aa4SPoul-Henning Kamp static struct llinfo_arp
117885f1aa4SPoul-Henning Kamp 		*arplookup __P((u_long, int, int));
1181d5e9e22SEivind Eklund #ifdef INET
119885f1aa4SPoul-Henning Kamp static void	in_arpinput __P((struct mbuf *));
1201d5e9e22SEivind Eklund #endif
12128e82295SGarrett Wollman 
122df8bae1dSRodney W. Grimes /*
123df8bae1dSRodney W. Grimes  * Timeout routine.  Age arp_tab entries periodically.
124df8bae1dSRodney W. Grimes  */
125df8bae1dSRodney W. Grimes /* ARGSUSED */
126df8bae1dSRodney W. Grimes static void
127df8bae1dSRodney W. Grimes arptimer(ignored_arg)
128df8bae1dSRodney W. Grimes 	void *ignored_arg;
129df8bae1dSRodney W. Grimes {
130df8bae1dSRodney W. Grimes 	int s = splnet();
13166800f57SGarrett Wollman 	register struct llinfo_arp *la = llinfo_arp.lh_first;
13266800f57SGarrett Wollman 	struct llinfo_arp *ola;
133df8bae1dSRodney W. Grimes 
134df8bae1dSRodney W. Grimes 	timeout(arptimer, (caddr_t)0, arpt_prune * hz);
13566800f57SGarrett Wollman 	while ((ola = la) != 0) {
136df8bae1dSRodney W. Grimes 		register struct rtentry *rt = la->la_rt;
13766800f57SGarrett Wollman 		la = la->la_le.le_next;
138227ee8a1SPoul-Henning Kamp 		if (rt->rt_expire && rt->rt_expire <= time_second)
13966800f57SGarrett Wollman 			arptfree(ola); /* timer has expired, clear */
140df8bae1dSRodney W. Grimes 	}
141df8bae1dSRodney W. Grimes 	splx(s);
142df8bae1dSRodney W. Grimes }
143df8bae1dSRodney W. Grimes 
144df8bae1dSRodney W. Grimes /*
145df8bae1dSRodney W. Grimes  * Parallel to llc_rtrequest.
146df8bae1dSRodney W. Grimes  */
147b2774d00SGarrett Wollman static void
148df8bae1dSRodney W. Grimes arp_rtrequest(req, rt, sa)
149df8bae1dSRodney W. Grimes 	int req;
150df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
151df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
152df8bae1dSRodney W. Grimes {
153df8bae1dSRodney W. Grimes 	register struct sockaddr *gate = rt->rt_gateway;
154df8bae1dSRodney W. Grimes 	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
155df8bae1dSRodney W. Grimes 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
156885f1aa4SPoul-Henning Kamp 	static int arpinit_done;
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes 	if (!arpinit_done) {
159df8bae1dSRodney W. Grimes 		arpinit_done = 1;
16066800f57SGarrett Wollman 		LIST_INIT(&llinfo_arp);
161df8bae1dSRodney W. Grimes 		timeout(arptimer, (caddr_t)0, hz);
162df8bae1dSRodney W. Grimes 	}
163df8bae1dSRodney W. Grimes 	if (rt->rt_flags & RTF_GATEWAY)
164df8bae1dSRodney W. Grimes 		return;
165df8bae1dSRodney W. Grimes 	switch (req) {
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes 	case RTM_ADD:
168df8bae1dSRodney W. Grimes 		/*
169df8bae1dSRodney W. Grimes 		 * XXX: If this is a manually added route to interface
170df8bae1dSRodney W. Grimes 		 * such as older version of routed or gated might provide,
171df8bae1dSRodney W. Grimes 		 * restore cloning bit.
172df8bae1dSRodney W. Grimes 		 */
173df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_HOST) == 0 &&
174df8bae1dSRodney W. Grimes 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
175df8bae1dSRodney W. Grimes 			rt->rt_flags |= RTF_CLONING;
176df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_CLONING) {
177df8bae1dSRodney W. Grimes 			/*
178df8bae1dSRodney W. Grimes 			 * Case 1: This route should come from a route to iface.
179df8bae1dSRodney W. Grimes 			 */
180df8bae1dSRodney W. Grimes 			rt_setgate(rt, rt_key(rt),
181df8bae1dSRodney W. Grimes 					(struct sockaddr *)&null_sdl);
182df8bae1dSRodney W. Grimes 			gate = rt->rt_gateway;
183df8bae1dSRodney W. Grimes 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
184df8bae1dSRodney W. Grimes 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
185227ee8a1SPoul-Henning Kamp 			rt->rt_expire = time_second;
186df8bae1dSRodney W. Grimes 			break;
187df8bae1dSRodney W. Grimes 		}
188df8bae1dSRodney W. Grimes 		/* Announce a new entry if requested. */
189df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_ANNOUNCE)
190df8bae1dSRodney W. Grimes 			arprequest((struct arpcom *)rt->rt_ifp,
191ed7509acSJulian Elischer 			    &SIN(rt_key(rt))->sin_addr,
192ed7509acSJulian Elischer 			    &SIN(rt_key(rt))->sin_addr,
193df8bae1dSRodney W. Grimes 			    (u_char *)LLADDR(SDL(gate)));
194df8bae1dSRodney W. Grimes 		/*FALLTHROUGH*/
195df8bae1dSRodney W. Grimes 	case RTM_RESOLVE:
196df8bae1dSRodney W. Grimes 		if (gate->sa_family != AF_LINK ||
197df8bae1dSRodney W. Grimes 		    gate->sa_len < sizeof(null_sdl)) {
19838aa9fc3SDavid Greenman 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
199df8bae1dSRodney W. Grimes 			break;
200df8bae1dSRodney W. Grimes 		}
201df8bae1dSRodney W. Grimes 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
202df8bae1dSRodney W. Grimes 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
203df8bae1dSRodney W. Grimes 		if (la != 0)
204df8bae1dSRodney W. Grimes 			break; /* This happens on a route change */
205df8bae1dSRodney W. Grimes 		/*
206df8bae1dSRodney W. Grimes 		 * Case 2:  This route may come from cloning, or a manual route
207df8bae1dSRodney W. Grimes 		 * add with a LL address.
208df8bae1dSRodney W. Grimes 		 */
209df8bae1dSRodney W. Grimes 		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
210df8bae1dSRodney W. Grimes 		rt->rt_llinfo = (caddr_t)la;
211df8bae1dSRodney W. Grimes 		if (la == 0) {
212df8bae1dSRodney W. Grimes 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
213df8bae1dSRodney W. Grimes 			break;
214df8bae1dSRodney W. Grimes 		}
215df8bae1dSRodney W. Grimes 		arp_inuse++, arp_allocated++;
216df8bae1dSRodney W. Grimes 		Bzero(la, sizeof(*la));
217df8bae1dSRodney W. Grimes 		la->la_rt = rt;
218df8bae1dSRodney W. Grimes 		rt->rt_flags |= RTF_LLINFO;
21966800f57SGarrett Wollman 		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
220cbb0b46aSGarrett Wollman 
2211d5e9e22SEivind Eklund #ifdef INET
222cbb0b46aSGarrett Wollman 		/*
223cbb0b46aSGarrett Wollman 		 * This keeps the multicast addresses from showing up
224cbb0b46aSGarrett Wollman 		 * in `arp -a' listings as unresolved.  It's not actually
225cbb0b46aSGarrett Wollman 		 * functional.  Then the same for broadcast.
226cbb0b46aSGarrett Wollman 		 */
227cbb0b46aSGarrett Wollman 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
228cbb0b46aSGarrett Wollman 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
229cbb0b46aSGarrett Wollman 					       LLADDR(SDL(gate)));
230cbb0b46aSGarrett Wollman 			SDL(gate)->sdl_alen = 6;
23151a109a1SPeter Wemm 			rt->rt_expire = 0;
232cbb0b46aSGarrett Wollman 		}
233cbb0b46aSGarrett Wollman 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
234cbb0b46aSGarrett Wollman 			memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
235cbb0b46aSGarrett Wollman 			SDL(gate)->sdl_alen = 6;
23651a109a1SPeter Wemm 			rt->rt_expire = 0;
237cbb0b46aSGarrett Wollman 		}
2381d5e9e22SEivind Eklund #endif
239cbb0b46aSGarrett Wollman 
240df8bae1dSRodney W. Grimes 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
241df8bae1dSRodney W. Grimes 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
242df8bae1dSRodney W. Grimes 		    /*
243df8bae1dSRodney W. Grimes 		     * This test used to be
244df8bae1dSRodney W. Grimes 		     *	if (loif.if_flags & IFF_UP)
245df8bae1dSRodney W. Grimes 		     * It allowed local traffic to be forced
246df8bae1dSRodney W. Grimes 		     * through the hardware by configuring the loopback down.
247df8bae1dSRodney W. Grimes 		     * However, it causes problems during network configuration
248df8bae1dSRodney W. Grimes 		     * for boards that can't receive packets they send.
249df8bae1dSRodney W. Grimes 		     * It is now necessary to clear "useloopback" and remove
250df8bae1dSRodney W. Grimes 		     * the route to force traffic out to the hardware.
251df8bae1dSRodney W. Grimes 		     */
252df8bae1dSRodney W. Grimes 			rt->rt_expire = 0;
253df8bae1dSRodney W. Grimes 			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
254df8bae1dSRodney W. Grimes 				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
255df8bae1dSRodney W. Grimes 			if (useloopback)
256f5fea3ddSPaul Traina 				rt->rt_ifp = loif;
257df8bae1dSRodney W. Grimes 
258df8bae1dSRodney W. Grimes 		}
259df8bae1dSRodney W. Grimes 		break;
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes 	case RTM_DELETE:
262df8bae1dSRodney W. Grimes 		if (la == 0)
263df8bae1dSRodney W. Grimes 			break;
264df8bae1dSRodney W. Grimes 		arp_inuse--;
26566800f57SGarrett Wollman 		LIST_REMOVE(la, la_le);
266df8bae1dSRodney W. Grimes 		rt->rt_llinfo = 0;
267df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_LLINFO;
268df8bae1dSRodney W. Grimes 		if (la->la_hold)
269df8bae1dSRodney W. Grimes 			m_freem(la->la_hold);
270df8bae1dSRodney W. Grimes 		Free((caddr_t)la);
271df8bae1dSRodney W. Grimes 	}
272df8bae1dSRodney W. Grimes }
273df8bae1dSRodney W. Grimes 
274df8bae1dSRodney W. Grimes /*
275df8bae1dSRodney W. Grimes  * Broadcast an ARP request. Caller specifies:
276df8bae1dSRodney W. Grimes  *	- arp header source ip address
277df8bae1dSRodney W. Grimes  *	- arp header target ip address
278df8bae1dSRodney W. Grimes  *	- arp header source ethernet address
279df8bae1dSRodney W. Grimes  */
280df8bae1dSRodney W. Grimes static void
281df8bae1dSRodney W. Grimes arprequest(ac, sip, tip, enaddr)
282df8bae1dSRodney W. Grimes 	register struct arpcom *ac;
283ed7509acSJulian Elischer 	register struct in_addr *sip, *tip;
284df8bae1dSRodney W. Grimes 	register u_char *enaddr;
285df8bae1dSRodney W. Grimes {
286df8bae1dSRodney W. Grimes 	register struct mbuf *m;
287df8bae1dSRodney W. Grimes 	register struct ether_header *eh;
288df8bae1dSRodney W. Grimes 	register struct ether_arp *ea;
289df8bae1dSRodney W. Grimes 	struct sockaddr sa;
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
292df8bae1dSRodney W. Grimes 		return;
293df8bae1dSRodney W. Grimes 	m->m_len = sizeof(*ea);
294df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = sizeof(*ea);
295df8bae1dSRodney W. Grimes 	MH_ALIGN(m, sizeof(*ea));
296df8bae1dSRodney W. Grimes 	ea = mtod(m, struct ether_arp *);
297df8bae1dSRodney W. Grimes 	eh = (struct ether_header *)sa.sa_data;
298df8bae1dSRodney W. Grimes 	bzero((caddr_t)ea, sizeof (*ea));
29994a5d9b6SDavid Greenman 	(void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
30034bed8b0SDavid Greenman 	eh->ether_type = htons(ETHERTYPE_ARP);	/* if_output will not swap */
301722012ccSJulian Elischer         if (ac->ac_if.if_type == IFT_ETHER)
302df8bae1dSRodney W. Grimes 	        ea->arp_hrd = htons(ARPHRD_ETHER);
303722012ccSJulian Elischer         if (ac->ac_if.if_type == IFT_ISO88025)
304722012ccSJulian Elischer                 ea->arp_hrd = htons(ARPHRD_IEEE802);
305df8bae1dSRodney W. Grimes 	ea->arp_pro = htons(ETHERTYPE_IP);
306df8bae1dSRodney W. Grimes 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
307df8bae1dSRodney W. Grimes 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
308df8bae1dSRodney W. Grimes 	ea->arp_op = htons(ARPOP_REQUEST);
30994a5d9b6SDavid Greenman 	(void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
31094a5d9b6SDavid Greenman 	(void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
31194a5d9b6SDavid Greenman 	(void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
312df8bae1dSRodney W. Grimes 	sa.sa_family = AF_UNSPEC;
313df8bae1dSRodney W. Grimes 	sa.sa_len = sizeof(sa);
314df8bae1dSRodney W. Grimes 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
315df8bae1dSRodney W. Grimes }
316df8bae1dSRodney W. Grimes 
317df8bae1dSRodney W. Grimes /*
318df8bae1dSRodney W. Grimes  * Resolve an IP address into an ethernet address.  If success,
319df8bae1dSRodney W. Grimes  * desten is filled in.  If there is no entry in arptab,
320df8bae1dSRodney W. Grimes  * set one up and broadcast a request for the IP address.
321df8bae1dSRodney W. Grimes  * Hold onto this mbuf and resend it once the address
322df8bae1dSRodney W. Grimes  * is finally resolved.  A return value of 1 indicates
323df8bae1dSRodney W. Grimes  * that desten has been filled in and the packet should be sent
324df8bae1dSRodney W. Grimes  * normally; a 0 return indicates that the packet has been
325df8bae1dSRodney W. Grimes  * taken over here, either now or for later transmission.
326df8bae1dSRodney W. Grimes  */
327df8bae1dSRodney W. Grimes int
3285df72964SGarrett Wollman arpresolve(ac, rt, m, dst, desten, rt0)
329df8bae1dSRodney W. Grimes 	register struct arpcom *ac;
330df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
331df8bae1dSRodney W. Grimes 	struct mbuf *m;
332df8bae1dSRodney W. Grimes 	register struct sockaddr *dst;
333df8bae1dSRodney W. Grimes 	register u_char *desten;
3345df72964SGarrett Wollman 	struct rtentry *rt0;
335df8bae1dSRodney W. Grimes {
336cc133fa3SBill Fenner 	register struct llinfo_arp *la = 0;
337df8bae1dSRodney W. Grimes 	struct sockaddr_dl *sdl;
338df8bae1dSRodney W. Grimes 
339df8bae1dSRodney W. Grimes 	if (m->m_flags & M_BCAST) {	/* broadcast */
34094a5d9b6SDavid Greenman 		(void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
341df8bae1dSRodney W. Grimes 		return (1);
342df8bae1dSRodney W. Grimes 	}
343df8bae1dSRodney W. Grimes 	if (m->m_flags & M_MCAST) {	/* multicast */
344df8bae1dSRodney W. Grimes 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
345df8bae1dSRodney W. Grimes 		return(1);
346df8bae1dSRodney W. Grimes 	}
347df8bae1dSRodney W. Grimes 	if (rt)
348df8bae1dSRodney W. Grimes 		la = (struct llinfo_arp *)rt->rt_llinfo;
349e7bc5f27SBill Fenner 	if (la == 0) {
350623ae52eSPoul-Henning Kamp 		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
351623ae52eSPoul-Henning Kamp 		if (la)
352df8bae1dSRodney W. Grimes 			rt = la->la_rt;
353df8bae1dSRodney W. Grimes 	}
354df8bae1dSRodney W. Grimes 	if (la == 0 || rt == 0) {
355245086a0SPoul-Henning Kamp 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
356245086a0SPoul-Henning Kamp 			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
357245086a0SPoul-Henning Kamp 				rt ? "rt" : "");
358df8bae1dSRodney W. Grimes 		m_freem(m);
359df8bae1dSRodney W. Grimes 		return (0);
360df8bae1dSRodney W. Grimes 	}
361df8bae1dSRodney W. Grimes 	sdl = SDL(rt->rt_gateway);
362df8bae1dSRodney W. Grimes 	/*
363df8bae1dSRodney W. Grimes 	 * Check the address family and length is valid, the address
364df8bae1dSRodney W. Grimes 	 * is resolved; otherwise, try to resolve.
365df8bae1dSRodney W. Grimes 	 */
366227ee8a1SPoul-Henning Kamp 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
367df8bae1dSRodney W. Grimes 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
3680453d3cbSBruce Evans 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
369df8bae1dSRodney W. Grimes 		return 1;
370df8bae1dSRodney W. Grimes 	}
371df8bae1dSRodney W. Grimes 	/*
372df8bae1dSRodney W. Grimes 	 * There is an arptab entry, but no ethernet address
373df8bae1dSRodney W. Grimes 	 * response yet.  Replace the held mbuf with this
374df8bae1dSRodney W. Grimes 	 * latest one.
375df8bae1dSRodney W. Grimes 	 */
376df8bae1dSRodney W. Grimes 	if (la->la_hold)
377df8bae1dSRodney W. Grimes 		m_freem(la->la_hold);
378df8bae1dSRodney W. Grimes 	la->la_hold = m;
379df8bae1dSRodney W. Grimes 	if (rt->rt_expire) {
380df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_REJECT;
381227ee8a1SPoul-Henning Kamp 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
382227ee8a1SPoul-Henning Kamp 			rt->rt_expire = time_second;
383df8bae1dSRodney W. Grimes 			if (la->la_asked++ < arp_maxtries)
384203f0c07SBill Fenner 			    arprequest(ac,
385ed7509acSJulian Elischer 			        &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
386ed7509acSJulian Elischer 				&SIN(dst)->sin_addr, ac->ac_enaddr);
387df8bae1dSRodney W. Grimes 			else {
388df8bae1dSRodney W. Grimes 				rt->rt_flags |= RTF_REJECT;
389df8bae1dSRodney W. Grimes 				rt->rt_expire += arpt_down;
390df8bae1dSRodney W. Grimes 				la->la_asked = 0;
391df8bae1dSRodney W. Grimes 			}
392df8bae1dSRodney W. Grimes 
393df8bae1dSRodney W. Grimes 		}
394df8bae1dSRodney W. Grimes 	}
395df8bae1dSRodney W. Grimes 	return (0);
396df8bae1dSRodney W. Grimes }
397df8bae1dSRodney W. Grimes 
398df8bae1dSRodney W. Grimes /*
399df8bae1dSRodney W. Grimes  * Common length and type checks are done here,
400df8bae1dSRodney W. Grimes  * then the protocol-specific routine is called.
401df8bae1dSRodney W. Grimes  */
402885f1aa4SPoul-Henning Kamp static void
403c5a1016bSBruce Evans arpintr()
404df8bae1dSRodney W. Grimes {
405df8bae1dSRodney W. Grimes 	register struct mbuf *m;
406df8bae1dSRodney W. Grimes 	register struct arphdr *ar;
407df8bae1dSRodney W. Grimes 	int s;
408df8bae1dSRodney W. Grimes 
409df8bae1dSRodney W. Grimes 	while (arpintrq.ifq_head) {
410df8bae1dSRodney W. Grimes 		s = splimp();
411df8bae1dSRodney W. Grimes 		IF_DEQUEUE(&arpintrq, m);
412df8bae1dSRodney W. Grimes 		splx(s);
413df8bae1dSRodney W. Grimes 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
414df8bae1dSRodney W. Grimes 			panic("arpintr");
415df8bae1dSRodney W. Grimes 		if (m->m_len >= sizeof(struct arphdr) &&
416df8bae1dSRodney W. Grimes 		    (ar = mtod(m, struct arphdr *)) &&
417722012ccSJulian Elischer 		    (ntohs(ar->ar_hrd) == ARPHRD_ETHER ||
418722012ccSJulian Elischer                      ntohs(ar->ar_hrd) == ARPHRD_IEEE802) &&
419df8bae1dSRodney W. Grimes 		    m->m_len >=
420df8bae1dSRodney W. Grimes 		      sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
421df8bae1dSRodney W. Grimes 
422df8bae1dSRodney W. Grimes 			    switch (ntohs(ar->ar_pro)) {
423df8bae1dSRodney W. Grimes 
4241d5e9e22SEivind Eklund #ifdef INET
425df8bae1dSRodney W. Grimes 			    case ETHERTYPE_IP:
426df8bae1dSRodney W. Grimes 				    in_arpinput(m);
427df8bae1dSRodney W. Grimes 				    continue;
4281d5e9e22SEivind Eklund #endif
429df8bae1dSRodney W. Grimes 			    }
430df8bae1dSRodney W. Grimes 		m_freem(m);
431df8bae1dSRodney W. Grimes 	}
432df8bae1dSRodney W. Grimes }
433df8bae1dSRodney W. Grimes 
434748e0b0aSGarrett Wollman NETISR_SET(NETISR_ARP, arpintr);
435748e0b0aSGarrett Wollman 
4361d5e9e22SEivind Eklund 
4371d5e9e22SEivind Eklund #ifdef INET
438df8bae1dSRodney W. Grimes /*
439df8bae1dSRodney W. Grimes  * ARP for Internet protocols on 10 Mb/s Ethernet.
440df8bae1dSRodney W. Grimes  * Algorithm is that given in RFC 826.
441df8bae1dSRodney W. Grimes  * In addition, a sanity check is performed on the sender
442df8bae1dSRodney W. Grimes  * protocol address, to catch impersonators.
443df8bae1dSRodney W. Grimes  * We no longer handle negotiations for use of trailer protocol:
444df8bae1dSRodney W. Grimes  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
445df8bae1dSRodney W. Grimes  * along with IP replies if we wanted trailers sent to us,
446df8bae1dSRodney W. Grimes  * and also sent them in response to IP replies.
447df8bae1dSRodney W. Grimes  * This allowed either end to announce the desire to receive
448df8bae1dSRodney W. Grimes  * trailer packets.
449df8bae1dSRodney W. Grimes  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
450df8bae1dSRodney W. Grimes  * but formerly didn't normally send requests.
451df8bae1dSRodney W. Grimes  */
452df8bae1dSRodney W. Grimes static void
453df8bae1dSRodney W. Grimes in_arpinput(m)
454df8bae1dSRodney W. Grimes 	struct mbuf *m;
455df8bae1dSRodney W. Grimes {
456df8bae1dSRodney W. Grimes 	register struct ether_arp *ea;
457df8bae1dSRodney W. Grimes 	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
458df8bae1dSRodney W. Grimes 	struct ether_header *eh;
459df8bae1dSRodney W. Grimes 	register struct llinfo_arp *la = 0;
460df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
461df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia, *maybe_ia = 0;
462df8bae1dSRodney W. Grimes 	struct sockaddr_dl *sdl;
463df8bae1dSRodney W. Grimes 	struct sockaddr sa;
464df8bae1dSRodney W. Grimes 	struct in_addr isaddr, itaddr, myaddr;
465df8bae1dSRodney W. Grimes 	int op;
466df8bae1dSRodney W. Grimes 
467df8bae1dSRodney W. Grimes 	ea = mtod(m, struct ether_arp *);
468df8bae1dSRodney W. Grimes 	op = ntohs(ea->arp_op);
46994a5d9b6SDavid Greenman 	(void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
47094a5d9b6SDavid Greenman 	(void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
471cbf5d949SBruce Evans 	for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next)
472b715f178SLuigi Rizzo #ifdef BRIDGE
473b715f178SLuigi Rizzo 		/*
474b715f178SLuigi Rizzo 		 * For a bridge, we want to check the address irrespective
475b715f178SLuigi Rizzo 		 * of the receive interface. (This will change slightly
476b715f178SLuigi Rizzo 		 * when we have clusters of interfaces).
477b715f178SLuigi Rizzo 		 */
478b715f178SLuigi Rizzo 		{
479b715f178SLuigi Rizzo #else
480df8bae1dSRodney W. Grimes 		if (ia->ia_ifp == &ac->ac_if) {
481b715f178SLuigi Rizzo #endif
482df8bae1dSRodney W. Grimes 			maybe_ia = ia;
483df8bae1dSRodney W. Grimes 			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
484df8bae1dSRodney W. Grimes 			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
485df8bae1dSRodney W. Grimes 				break;
486df8bae1dSRodney W. Grimes 		}
487885f1aa4SPoul-Henning Kamp 	if (maybe_ia == 0) {
488885f1aa4SPoul-Henning Kamp 		m_freem(m);
489885f1aa4SPoul-Henning Kamp 		return;
490885f1aa4SPoul-Henning Kamp 	}
491df8bae1dSRodney W. Grimes 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
492df8bae1dSRodney W. Grimes 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
493885f1aa4SPoul-Henning Kamp 	    sizeof (ea->arp_sha))) {
494885f1aa4SPoul-Henning Kamp 		m_freem(m);	/* it's from me, ignore it. */
495885f1aa4SPoul-Henning Kamp 		return;
496885f1aa4SPoul-Henning Kamp 	}
497df8bae1dSRodney W. Grimes 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
498df8bae1dSRodney W. Grimes 	    sizeof (ea->arp_sha))) {
499df8bae1dSRodney W. Grimes 		log(LOG_ERR,
500ac234f93SGarrett Wollman 		    "arp: ether address is broadcast for IP address %s!\n",
501ef0cdf33SGarrett Wollman 		    inet_ntoa(isaddr));
502885f1aa4SPoul-Henning Kamp 		m_freem(m);
503885f1aa4SPoul-Henning Kamp 		return;
504df8bae1dSRodney W. Grimes 	}
505df8bae1dSRodney W. Grimes 	if (isaddr.s_addr == myaddr.s_addr) {
506df8bae1dSRodney W. Grimes 		log(LOG_ERR,
507254de4eaSBill Fenner 		   "arp: %6D is using my IP address %s!\n",
508254de4eaSBill Fenner 		   ea->arp_sha, ":", inet_ntoa(isaddr));
509df8bae1dSRodney W. Grimes 		itaddr = myaddr;
510df8bae1dSRodney W. Grimes 		goto reply;
511df8bae1dSRodney W. Grimes 	}
512df8bae1dSRodney W. Grimes 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
513df8bae1dSRodney W. Grimes 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
514a1a2de51SLuigi Rizzo #ifndef BRIDGE /* the following is not an error when doing bridging */
515dd9b6ddeSBill Fenner 		if (rt->rt_ifp != &ac->ac_if) {
516dd9b6ddeSBill Fenner 			log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n",
517dd9b6ddeSBill Fenner 			    inet_ntoa(isaddr),
518dd9b6ddeSBill Fenner 			    rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
519dd9b6ddeSBill Fenner 			    ea->arp_sha, ":",
520dd9b6ddeSBill Fenner 			    ac->ac_if.if_name, ac->ac_if.if_unit);
521dd9b6ddeSBill Fenner 			goto reply;
522dd9b6ddeSBill Fenner 		}
523a1a2de51SLuigi Rizzo #endif
524df8bae1dSRodney W. Grimes 		if (sdl->sdl_alen &&
525df8bae1dSRodney W. Grimes 		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
526dd9b6ddeSBill Fenner 			if (rt->rt_expire)
527dd9b6ddeSBill Fenner 			    log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n",
528254de4eaSBill Fenner 				inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
529dd9b6ddeSBill Fenner 				ea->arp_sha, ":",
530dd9b6ddeSBill Fenner 				ac->ac_if.if_name, ac->ac_if.if_unit);
531dd9b6ddeSBill Fenner 			else {
532dd9b6ddeSBill Fenner 			    log(LOG_ERR,
533dd9b6ddeSBill Fenner 				"arp: %6D attempts to modify permanent entry for %s on %s%d",
534dd9b6ddeSBill Fenner 				ea->arp_sha, ":", inet_ntoa(isaddr),
535dd9b6ddeSBill Fenner 				ac->ac_if.if_name, ac->ac_if.if_unit);
536dd9b6ddeSBill Fenner 			    goto reply;
537dd9b6ddeSBill Fenner 			}
53894a5d9b6SDavid Greenman 		(void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
53994a5d9b6SDavid Greenman 		sdl->sdl_alen = sizeof(ea->arp_sha);
540df8bae1dSRodney W. Grimes 		if (rt->rt_expire)
541227ee8a1SPoul-Henning Kamp 			rt->rt_expire = time_second + arpt_keep;
542df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_REJECT;
543df8bae1dSRodney W. Grimes 		la->la_asked = 0;
544df8bae1dSRodney W. Grimes 		if (la->la_hold) {
545df8bae1dSRodney W. Grimes 			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
546df8bae1dSRodney W. Grimes 				rt_key(rt), rt);
547df8bae1dSRodney W. Grimes 			la->la_hold = 0;
548df8bae1dSRodney W. Grimes 		}
549df8bae1dSRodney W. Grimes 	}
550df8bae1dSRodney W. Grimes reply:
551df8bae1dSRodney W. Grimes 	if (op != ARPOP_REQUEST) {
552df8bae1dSRodney W. Grimes 		m_freem(m);
553df8bae1dSRodney W. Grimes 		return;
554df8bae1dSRodney W. Grimes 	}
555df8bae1dSRodney W. Grimes 	if (itaddr.s_addr == myaddr.s_addr) {
556df8bae1dSRodney W. Grimes 		/* I am the target */
55794a5d9b6SDavid Greenman 		(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
55894a5d9b6SDavid Greenman 		(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
559df8bae1dSRodney W. Grimes 	} else {
560df8bae1dSRodney W. Grimes 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
56128e82295SGarrett Wollman 		if (la == NULL) {
56228e82295SGarrett Wollman 			struct sockaddr_in sin;
56328e82295SGarrett Wollman 
564885f1aa4SPoul-Henning Kamp 			if (!arp_proxyall) {
565885f1aa4SPoul-Henning Kamp 				m_freem(m);
566885f1aa4SPoul-Henning Kamp 				return;
567885f1aa4SPoul-Henning Kamp 			}
56828e82295SGarrett Wollman 
56928e82295SGarrett Wollman 			bzero(&sin, sizeof sin);
57028e82295SGarrett Wollman 			sin.sin_family = AF_INET;
57128e82295SGarrett Wollman 			sin.sin_len = sizeof sin;
57228e82295SGarrett Wollman 			sin.sin_addr = itaddr;
57328e82295SGarrett Wollman 
57431246bc2SGarrett Wollman 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
575885f1aa4SPoul-Henning Kamp 			if (!rt) {
576885f1aa4SPoul-Henning Kamp 				m_freem(m);
577885f1aa4SPoul-Henning Kamp 				return;
578885f1aa4SPoul-Henning Kamp 			}
57928e82295SGarrett Wollman 			/*
58028e82295SGarrett Wollman 			 * Don't send proxies for nodes on the same interface
58128e82295SGarrett Wollman 			 * as this one came out of, or we'll get into a fight
58228e82295SGarrett Wollman 			 * over who claims what Ether address.
58328e82295SGarrett Wollman 			 */
58428e82295SGarrett Wollman 			if (rt->rt_ifp == &ac->ac_if) {
58528e82295SGarrett Wollman 				rtfree(rt);
586885f1aa4SPoul-Henning Kamp 				m_freem(m);
587885f1aa4SPoul-Henning Kamp 				return;
58828e82295SGarrett Wollman 			}
58994a5d9b6SDavid Greenman 			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
59094a5d9b6SDavid Greenman 			(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
59128e82295SGarrett Wollman 			rtfree(rt);
592ac234f93SGarrett Wollman #ifdef DEBUG_PROXY
593ac234f93SGarrett Wollman 			printf("arp: proxying for %s\n",
594ef0cdf33SGarrett Wollman 			       inet_ntoa(itaddr));
595ac234f93SGarrett Wollman #endif
59628e82295SGarrett Wollman 		} else {
597df8bae1dSRodney W. Grimes 			rt = la->la_rt;
59894a5d9b6SDavid Greenman 			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
599df8bae1dSRodney W. Grimes 			sdl = SDL(rt->rt_gateway);
60094a5d9b6SDavid Greenman 			(void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
60128e82295SGarrett Wollman 		}
602df8bae1dSRodney W. Grimes 	}
603df8bae1dSRodney W. Grimes 
60494a5d9b6SDavid Greenman 	(void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
60594a5d9b6SDavid Greenman 	(void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
606df8bae1dSRodney W. Grimes 	ea->arp_op = htons(ARPOP_REPLY);
607df8bae1dSRodney W. Grimes 	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
608df8bae1dSRodney W. Grimes 	eh = (struct ether_header *)sa.sa_data;
60994a5d9b6SDavid Greenman 	(void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
61034bed8b0SDavid Greenman 	eh->ether_type = htons(ETHERTYPE_ARP);
611df8bae1dSRodney W. Grimes 	sa.sa_family = AF_UNSPEC;
612df8bae1dSRodney W. Grimes 	sa.sa_len = sizeof(sa);
613df8bae1dSRodney W. Grimes 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
614df8bae1dSRodney W. Grimes 	return;
615df8bae1dSRodney W. Grimes }
6161d5e9e22SEivind Eklund #endif
617df8bae1dSRodney W. Grimes 
618df8bae1dSRodney W. Grimes /*
619df8bae1dSRodney W. Grimes  * Free an arp entry.
620df8bae1dSRodney W. Grimes  */
621df8bae1dSRodney W. Grimes static void
622df8bae1dSRodney W. Grimes arptfree(la)
623df8bae1dSRodney W. Grimes 	register struct llinfo_arp *la;
624df8bae1dSRodney W. Grimes {
625df8bae1dSRodney W. Grimes 	register struct rtentry *rt = la->la_rt;
626df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
627df8bae1dSRodney W. Grimes 	if (rt == 0)
628df8bae1dSRodney W. Grimes 		panic("arptfree");
629df8bae1dSRodney W. Grimes 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
630df8bae1dSRodney W. Grimes 	    sdl->sdl_family == AF_LINK) {
631df8bae1dSRodney W. Grimes 		sdl->sdl_alen = 0;
632df8bae1dSRodney W. Grimes 		la->la_asked = 0;
633df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_REJECT;
634df8bae1dSRodney W. Grimes 		return;
635df8bae1dSRodney W. Grimes 	}
636df8bae1dSRodney W. Grimes 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
637df8bae1dSRodney W. Grimes 			0, (struct rtentry **)0);
638df8bae1dSRodney W. Grimes }
639df8bae1dSRodney W. Grimes /*
640df8bae1dSRodney W. Grimes  * Lookup or enter a new address in arptab.
641df8bae1dSRodney W. Grimes  */
642df8bae1dSRodney W. Grimes static struct llinfo_arp *
643df8bae1dSRodney W. Grimes arplookup(addr, create, proxy)
644df8bae1dSRodney W. Grimes 	u_long addr;
645df8bae1dSRodney W. Grimes 	int create, proxy;
646df8bae1dSRodney W. Grimes {
647df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
648df8bae1dSRodney W. Grimes 	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
649ac234f93SGarrett Wollman 	const char *why = 0;
650df8bae1dSRodney W. Grimes 
651df8bae1dSRodney W. Grimes 	sin.sin_addr.s_addr = addr;
652df8bae1dSRodney W. Grimes 	sin.sin_other = proxy ? SIN_PROXY : 0;
65331246bc2SGarrett Wollman 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
654df8bae1dSRodney W. Grimes 	if (rt == 0)
655df8bae1dSRodney W. Grimes 		return (0);
656df8bae1dSRodney W. Grimes 	rt->rt_refcnt--;
657ac234f93SGarrett Wollman 
658ac234f93SGarrett Wollman 	if (rt->rt_flags & RTF_GATEWAY)
659ac234f93SGarrett Wollman 		why = "host is not on local network";
660ac234f93SGarrett Wollman 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
661ac234f93SGarrett Wollman 		why = "could not allocate llinfo";
662ac234f93SGarrett Wollman 	else if (rt->rt_gateway->sa_family != AF_LINK)
663ac234f93SGarrett Wollman 		why = "gateway route is not ours";
664ac234f93SGarrett Wollman 
665ac234f93SGarrett Wollman 	if (why && create) {
666ac234f93SGarrett Wollman 		log(LOG_DEBUG, "arplookup %s failed: %s\n",
667ef0cdf33SGarrett Wollman 		    inet_ntoa(sin.sin_addr), why);
668ac234f93SGarrett Wollman 		return 0;
669ac234f93SGarrett Wollman 	} else if (why) {
670ac234f93SGarrett Wollman 		return 0;
671df8bae1dSRodney W. Grimes 	}
672df8bae1dSRodney W. Grimes 	return ((struct llinfo_arp *)rt->rt_llinfo);
673df8bae1dSRodney W. Grimes }
674df8bae1dSRodney W. Grimes 
675dd2e4102SGarrett Wollman void
676dd2e4102SGarrett Wollman arp_ifinit(ac, ifa)
677dd2e4102SGarrett Wollman 	struct arpcom *ac;
678dd2e4102SGarrett Wollman 	struct ifaddr *ifa;
679dd2e4102SGarrett Wollman {
680dd570d4dSTor Egge 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
681ed7509acSJulian Elischer 		arprequest(ac, &IA_SIN(ifa)->sin_addr,
682ed7509acSJulian Elischer 			       &IA_SIN(ifa)->sin_addr, ac->ac_enaddr);
683dd2e4102SGarrett Wollman 	ifa->ifa_rtrequest = arp_rtrequest;
684dd2e4102SGarrett Wollman 	ifa->ifa_flags |= RTF_CLONING;
685dd2e4102SGarrett Wollman }
686