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