if_ether.c revision 36308
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
34 * $Id: if_ether.c,v 1.44 1998/03/30 09:52:40 phk Exp $
35 */
36
37/*
38 * Ethernet address resolution protocol.
39 * TODO:
40 *	add "inuse/lock" bit (or ref. count) along with valid bit
41 */
42
43#include "opt_inet.h"
44
45#include <sys/param.h>
46#include <sys/kernel.h>
47#include <sys/sysctl.h>
48#include <sys/systm.h>
49#include <sys/mbuf.h>
50#include <sys/malloc.h>
51#include <sys/socket.h>
52#include <sys/syslog.h>
53
54#include <net/if.h>
55#include <net/if_dl.h>
56#include <net/route.h>
57#include <net/netisr.h>
58
59#include <netinet/in.h>
60#include <netinet/in_var.h>
61#include <netinet/if_ether.h>
62
63#define SIN(s) ((struct sockaddr_in *)s)
64#define SDL(s) ((struct sockaddr_dl *)s)
65
66SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
67
68/* timer values */
69static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
70static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
71static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
72
73SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
74	   &arpt_prune, 0, "");
75SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
76	   &arpt_keep, 0, "");
77SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
78	   &arpt_down, 0, "");
79
80#define	rt_expire rt_rmx.rmx_expire
81
82struct llinfo_arp {
83	LIST_ENTRY(llinfo_arp) la_le;
84	struct	rtentry *la_rt;
85	struct	mbuf *la_hold;		/* last packet until resolved/timeout */
86	long	la_asked;		/* last time we QUERIED for this addr */
87#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
88};
89
90static	LIST_HEAD(, llinfo_arp) llinfo_arp;
91
92struct	ifqueue arpintrq = {0, 0, 0, 50};
93static int	arp_inuse, arp_allocated;
94
95static int	arp_maxtries = 5;
96static int	useloopback = 1; /* use loopback interface for local traffic */
97static int	arp_proxyall = 0;
98
99SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
100	   &arp_maxtries, 0, "");
101SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
102	   &useloopback, 0, "");
103SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
104	   &arp_proxyall, 0, "");
105
106static void	arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
107static void	arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));
108static void	arpintr __P((void));
109static void	arptfree __P((struct llinfo_arp *));
110static void	arptimer __P((void *));
111static struct llinfo_arp
112		*arplookup __P((u_long, int, int));
113#ifdef INET
114static void	in_arpinput __P((struct mbuf *));
115#endif
116
117/*
118 * Timeout routine.  Age arp_tab entries periodically.
119 */
120/* ARGSUSED */
121static void
122arptimer(ignored_arg)
123	void *ignored_arg;
124{
125	int s = splnet();
126	register struct llinfo_arp *la = llinfo_arp.lh_first;
127	struct llinfo_arp *ola;
128
129	timeout(arptimer, (caddr_t)0, arpt_prune * hz);
130	while ((ola = la) != 0) {
131		register struct rtentry *rt = la->la_rt;
132		la = la->la_le.le_next;
133		if (rt->rt_expire && rt->rt_expire <= time_second)
134			arptfree(ola); /* timer has expired, clear */
135	}
136	splx(s);
137}
138
139/*
140 * Parallel to llc_rtrequest.
141 */
142static void
143arp_rtrequest(req, rt, sa)
144	int req;
145	register struct rtentry *rt;
146	struct sockaddr *sa;
147{
148	register struct sockaddr *gate = rt->rt_gateway;
149	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
150	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
151	static int arpinit_done;
152
153	if (!arpinit_done) {
154		arpinit_done = 1;
155		LIST_INIT(&llinfo_arp);
156		timeout(arptimer, (caddr_t)0, hz);
157	}
158	if (rt->rt_flags & RTF_GATEWAY)
159		return;
160	switch (req) {
161
162	case RTM_ADD:
163		/*
164		 * XXX: If this is a manually added route to interface
165		 * such as older version of routed or gated might provide,
166		 * restore cloning bit.
167		 */
168		if ((rt->rt_flags & RTF_HOST) == 0 &&
169		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
170			rt->rt_flags |= RTF_CLONING;
171		if (rt->rt_flags & RTF_CLONING) {
172			/*
173			 * Case 1: This route should come from a route to iface.
174			 */
175			rt_setgate(rt, rt_key(rt),
176					(struct sockaddr *)&null_sdl);
177			gate = rt->rt_gateway;
178			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
179			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
180			rt->rt_expire = time_second;
181			break;
182		}
183		/* Announce a new entry if requested. */
184		if (rt->rt_flags & RTF_ANNOUNCE)
185			arprequest((struct arpcom *)rt->rt_ifp,
186			    &SIN(rt_key(rt))->sin_addr.s_addr,
187			    &SIN(rt_key(rt))->sin_addr.s_addr,
188			    (u_char *)LLADDR(SDL(gate)));
189		/*FALLTHROUGH*/
190	case RTM_RESOLVE:
191		if (gate->sa_family != AF_LINK ||
192		    gate->sa_len < sizeof(null_sdl)) {
193			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
194			break;
195		}
196		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
197		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
198		if (la != 0)
199			break; /* This happens on a route change */
200		/*
201		 * Case 2:  This route may come from cloning, or a manual route
202		 * add with a LL address.
203		 */
204		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
205		rt->rt_llinfo = (caddr_t)la;
206		if (la == 0) {
207			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
208			break;
209		}
210		arp_inuse++, arp_allocated++;
211		Bzero(la, sizeof(*la));
212		la->la_rt = rt;
213		rt->rt_flags |= RTF_LLINFO;
214		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
215
216#ifdef INET
217		/*
218		 * This keeps the multicast addresses from showing up
219		 * in `arp -a' listings as unresolved.  It's not actually
220		 * functional.  Then the same for broadcast.
221		 */
222		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
223			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
224					       LLADDR(SDL(gate)));
225			SDL(gate)->sdl_alen = 6;
226			rt->rt_expire = 0;
227		}
228		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
229			memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
230			SDL(gate)->sdl_alen = 6;
231			rt->rt_expire = 0;
232		}
233#endif
234
235		if (SIN(rt_key(rt))->sin_addr.s_addr ==
236		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
237		    /*
238		     * This test used to be
239		     *	if (loif.if_flags & IFF_UP)
240		     * It allowed local traffic to be forced
241		     * through the hardware by configuring the loopback down.
242		     * However, it causes problems during network configuration
243		     * for boards that can't receive packets they send.
244		     * It is now necessary to clear "useloopback" and remove
245		     * the route to force traffic out to the hardware.
246		     */
247			rt->rt_expire = 0;
248			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
249				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
250			if (useloopback)
251				rt->rt_ifp = loif;
252
253		}
254		break;
255
256	case RTM_DELETE:
257		if (la == 0)
258			break;
259		arp_inuse--;
260		LIST_REMOVE(la, la_le);
261		rt->rt_llinfo = 0;
262		rt->rt_flags &= ~RTF_LLINFO;
263		if (la->la_hold)
264			m_freem(la->la_hold);
265		Free((caddr_t)la);
266	}
267}
268
269/*
270 * Broadcast an ARP request. Caller specifies:
271 *	- arp header source ip address
272 *	- arp header target ip address
273 *	- arp header source ethernet address
274 */
275static void
276arprequest(ac, sip, tip, enaddr)
277	register struct arpcom *ac;
278	register u_long *sip, *tip;
279	register u_char *enaddr;
280{
281	register struct mbuf *m;
282	register struct ether_header *eh;
283	register struct ether_arp *ea;
284	struct sockaddr sa;
285
286	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
287		return;
288	m->m_len = sizeof(*ea);
289	m->m_pkthdr.len = sizeof(*ea);
290	MH_ALIGN(m, sizeof(*ea));
291	ea = mtod(m, struct ether_arp *);
292	eh = (struct ether_header *)sa.sa_data;
293	bzero((caddr_t)ea, sizeof (*ea));
294	(void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
295	eh->ether_type = htons(ETHERTYPE_ARP);	/* if_output will not swap */
296	ea->arp_hrd = htons(ARPHRD_ETHER);
297	ea->arp_pro = htons(ETHERTYPE_IP);
298	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
299	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
300	ea->arp_op = htons(ARPOP_REQUEST);
301	(void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
302	(void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
303	(void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
304	sa.sa_family = AF_UNSPEC;
305	sa.sa_len = sizeof(sa);
306	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
307}
308
309/*
310 * Resolve an IP address into an ethernet address.  If success,
311 * desten is filled in.  If there is no entry in arptab,
312 * set one up and broadcast a request for the IP address.
313 * Hold onto this mbuf and resend it once the address
314 * is finally resolved.  A return value of 1 indicates
315 * that desten has been filled in and the packet should be sent
316 * normally; a 0 return indicates that the packet has been
317 * taken over here, either now or for later transmission.
318 */
319int
320arpresolve(ac, rt, m, dst, desten, rt0)
321	register struct arpcom *ac;
322	register struct rtentry *rt;
323	struct mbuf *m;
324	register struct sockaddr *dst;
325	register u_char *desten;
326	struct rtentry *rt0;
327{
328	register struct llinfo_arp *la;
329	struct sockaddr_dl *sdl;
330
331	if (m->m_flags & M_BCAST) {	/* broadcast */
332		(void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
333		return (1);
334	}
335	if (m->m_flags & M_MCAST) {	/* multicast */
336		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
337		return(1);
338	}
339	if (rt)
340		la = (struct llinfo_arp *)rt->rt_llinfo;
341	else {
342		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
343		if (la)
344			rt = la->la_rt;
345	}
346	if (la == 0 || rt == 0) {
347		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
348			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
349				rt ? "rt" : "");
350		m_freem(m);
351		return (0);
352	}
353	sdl = SDL(rt->rt_gateway);
354	/*
355	 * Check the address family and length is valid, the address
356	 * is resolved; otherwise, try to resolve.
357	 */
358	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
359	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
360		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
361		return 1;
362	}
363	/*
364	 * There is an arptab entry, but no ethernet address
365	 * response yet.  Replace the held mbuf with this
366	 * latest one.
367	 */
368	if (la->la_hold)
369		m_freem(la->la_hold);
370	la->la_hold = m;
371	if (rt->rt_expire) {
372		rt->rt_flags &= ~RTF_REJECT;
373		if (la->la_asked == 0 || rt->rt_expire != time_second) {
374			rt->rt_expire = time_second;
375			if (la->la_asked++ < arp_maxtries)
376			    arprequest(ac,
377			        &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr),
378				&(SIN(dst)->sin_addr.s_addr),
379				ac->ac_enaddr);
380			else {
381				rt->rt_flags |= RTF_REJECT;
382				rt->rt_expire += arpt_down;
383				la->la_asked = 0;
384			}
385
386		}
387	}
388	return (0);
389}
390
391/*
392 * Common length and type checks are done here,
393 * then the protocol-specific routine is called.
394 */
395static void
396arpintr()
397{
398	register struct mbuf *m;
399	register struct arphdr *ar;
400	int s;
401
402	while (arpintrq.ifq_head) {
403		s = splimp();
404		IF_DEQUEUE(&arpintrq, m);
405		splx(s);
406		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
407			panic("arpintr");
408		if (m->m_len >= sizeof(struct arphdr) &&
409		    (ar = mtod(m, struct arphdr *)) &&
410		    ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
411		    m->m_len >=
412		      sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
413
414			    switch (ntohs(ar->ar_pro)) {
415
416#ifdef INET
417			    case ETHERTYPE_IP:
418				    in_arpinput(m);
419				    continue;
420#endif
421			    }
422		m_freem(m);
423	}
424}
425
426NETISR_SET(NETISR_ARP, arpintr);
427
428
429#ifdef INET
430/*
431 * ARP for Internet protocols on 10 Mb/s Ethernet.
432 * Algorithm is that given in RFC 826.
433 * In addition, a sanity check is performed on the sender
434 * protocol address, to catch impersonators.
435 * We no longer handle negotiations for use of trailer protocol:
436 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
437 * along with IP replies if we wanted trailers sent to us,
438 * and also sent them in response to IP replies.
439 * This allowed either end to announce the desire to receive
440 * trailer packets.
441 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
442 * but formerly didn't normally send requests.
443 */
444static void
445in_arpinput(m)
446	struct mbuf *m;
447{
448	register struct ether_arp *ea;
449	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
450	struct ether_header *eh;
451	register struct llinfo_arp *la = 0;
452	register struct rtentry *rt;
453	struct in_ifaddr *ia, *maybe_ia = 0;
454	struct sockaddr_dl *sdl;
455	struct sockaddr sa;
456	struct in_addr isaddr, itaddr, myaddr;
457	int op;
458
459	ea = mtod(m, struct ether_arp *);
460	op = ntohs(ea->arp_op);
461	(void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
462	(void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
463	for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next)
464		if (ia->ia_ifp == &ac->ac_if) {
465			maybe_ia = ia;
466			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
467			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
468				break;
469		}
470	if (maybe_ia == 0) {
471		m_freem(m);
472		return;
473	}
474	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
475	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
476	    sizeof (ea->arp_sha))) {
477		m_freem(m);	/* it's from me, ignore it. */
478		return;
479	}
480	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
481	    sizeof (ea->arp_sha))) {
482		log(LOG_ERR,
483		    "arp: ether address is broadcast for IP address %s!\n",
484		    inet_ntoa(isaddr));
485		m_freem(m);
486		return;
487	}
488	if (isaddr.s_addr == myaddr.s_addr) {
489		log(LOG_ERR,
490		   "arp: %6D is using my IP address %s!\n",
491		   ea->arp_sha, ":", inet_ntoa(isaddr));
492		itaddr = myaddr;
493		goto reply;
494	}
495	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
496	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
497		if (sdl->sdl_alen &&
498		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
499			log(LOG_INFO, "arp: %s moved from %6D to %6D\n",
500			    inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
501			    ea->arp_sha, ":");
502		(void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
503		sdl->sdl_alen = sizeof(ea->arp_sha);
504		if (rt->rt_expire)
505			rt->rt_expire = time_second + arpt_keep;
506		rt->rt_flags &= ~RTF_REJECT;
507		la->la_asked = 0;
508		if (la->la_hold) {
509			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
510				rt_key(rt), rt);
511			la->la_hold = 0;
512		}
513	}
514reply:
515	if (op != ARPOP_REQUEST) {
516		m_freem(m);
517		return;
518	}
519	if (itaddr.s_addr == myaddr.s_addr) {
520		/* I am the target */
521		(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
522		(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
523	} else {
524		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
525		if (la == NULL) {
526			struct sockaddr_in sin;
527
528			if (!arp_proxyall) {
529				m_freem(m);
530				return;
531			}
532
533			bzero(&sin, sizeof sin);
534			sin.sin_family = AF_INET;
535			sin.sin_len = sizeof sin;
536			sin.sin_addr = itaddr;
537
538			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
539			if (!rt) {
540				m_freem(m);
541				return;
542			}
543			/*
544			 * Don't send proxies for nodes on the same interface
545			 * as this one came out of, or we'll get into a fight
546			 * over who claims what Ether address.
547			 */
548			if (rt->rt_ifp == &ac->ac_if) {
549				rtfree(rt);
550				m_freem(m);
551				return;
552			}
553			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
554			(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
555			rtfree(rt);
556#ifdef DEBUG_PROXY
557			printf("arp: proxying for %s\n",
558			       inet_ntoa(itaddr));
559#endif
560		} else {
561			rt = la->la_rt;
562			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
563			sdl = SDL(rt->rt_gateway);
564			(void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
565		}
566	}
567
568	(void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
569	(void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
570	ea->arp_op = htons(ARPOP_REPLY);
571	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
572	eh = (struct ether_header *)sa.sa_data;
573	(void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
574	eh->ether_type = htons(ETHERTYPE_ARP);
575	sa.sa_family = AF_UNSPEC;
576	sa.sa_len = sizeof(sa);
577	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
578	return;
579}
580#endif
581
582/*
583 * Free an arp entry.
584 */
585static void
586arptfree(la)
587	register struct llinfo_arp *la;
588{
589	register struct rtentry *rt = la->la_rt;
590	register struct sockaddr_dl *sdl;
591	if (rt == 0)
592		panic("arptfree");
593	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
594	    sdl->sdl_family == AF_LINK) {
595		sdl->sdl_alen = 0;
596		la->la_asked = 0;
597		rt->rt_flags &= ~RTF_REJECT;
598		return;
599	}
600	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
601			0, (struct rtentry **)0);
602}
603/*
604 * Lookup or enter a new address in arptab.
605 */
606static struct llinfo_arp *
607arplookup(addr, create, proxy)
608	u_long addr;
609	int create, proxy;
610{
611	register struct rtentry *rt;
612	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
613	const char *why = 0;
614
615	sin.sin_addr.s_addr = addr;
616	sin.sin_other = proxy ? SIN_PROXY : 0;
617	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
618	if (rt == 0)
619		return (0);
620	rt->rt_refcnt--;
621
622	if (rt->rt_flags & RTF_GATEWAY)
623		why = "host is not on local network";
624	else if ((rt->rt_flags & RTF_LLINFO) == 0)
625		why = "could not allocate llinfo";
626	else if (rt->rt_gateway->sa_family != AF_LINK)
627		why = "gateway route is not ours";
628
629	if (why && create) {
630		log(LOG_DEBUG, "arplookup %s failed: %s\n",
631		    inet_ntoa(sin.sin_addr), why);
632		return 0;
633	} else if (why) {
634		return 0;
635	}
636	return ((struct llinfo_arp *)rt->rt_llinfo);
637}
638
639void
640arp_ifinit(ac, ifa)
641	struct arpcom *ac;
642	struct ifaddr *ifa;
643{
644	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
645		arprequest(ac, &(IA_SIN(ifa)->sin_addr.s_addr),
646			       &(IA_SIN(ifa)->sin_addr.s_addr), ac->ac_enaddr);
647	ifa->ifa_rtrequest = arp_rtrequest;
648	ifa->ifa_flags |= RTF_CLONING;
649}
650