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