xref: /freebsd/sys/netinet/if_ether.c (revision d74e86d9e30043893d6b308468008b65640ddcae)
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.47 1998/06/12 03:48:14 julian 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 
66 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
67 
68 /* timer values */
69 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
70 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
71 static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
72 
73 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
74 	   &arpt_prune, 0, "");
75 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
76 	   &arpt_keep, 0, "");
77 SYSCTL_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 
82 struct 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 
90 static	LIST_HEAD(, llinfo_arp) llinfo_arp;
91 
92 struct	ifqueue arpintrq = {0, 0, 0, 50};
93 static int	arp_inuse, arp_allocated;
94 
95 static int	arp_maxtries = 5;
96 static int	useloopback = 1; /* use loopback interface for local traffic */
97 static int	arp_proxyall = 0;
98 
99 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
100 	   &arp_maxtries, 0, "");
101 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
102 	   &useloopback, 0, "");
103 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
104 	   &arp_proxyall, 0, "");
105 
106 static void	arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
107 static void	arprequest __P((struct arpcom *,
108 			struct in_addr *, struct in_addr *, u_char *));
109 static void	arpintr __P((void));
110 static void	arptfree __P((struct llinfo_arp *));
111 static void	arptimer __P((void *));
112 static struct llinfo_arp
113 		*arplookup __P((u_long, int, int));
114 #ifdef INET
115 static void	in_arpinput __P((struct mbuf *));
116 #endif
117 
118 /*
119  * Timeout routine.  Age arp_tab entries periodically.
120  */
121 /* ARGSUSED */
122 static void
123 arptimer(ignored_arg)
124 	void *ignored_arg;
125 {
126 	int s = splnet();
127 	register struct llinfo_arp *la = llinfo_arp.lh_first;
128 	struct llinfo_arp *ola;
129 
130 	timeout(arptimer, (caddr_t)0, arpt_prune * hz);
131 	while ((ola = la) != 0) {
132 		register struct rtentry *rt = la->la_rt;
133 		la = la->la_le.le_next;
134 		if (rt->rt_expire && rt->rt_expire <= time_second)
135 			arptfree(ola); /* timer has expired, clear */
136 	}
137 	splx(s);
138 }
139 
140 /*
141  * Parallel to llc_rtrequest.
142  */
143 static void
144 arp_rtrequest(req, rt, sa)
145 	int req;
146 	register struct rtentry *rt;
147 	struct sockaddr *sa;
148 {
149 	register struct sockaddr *gate = rt->rt_gateway;
150 	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
151 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
152 	static int arpinit_done;
153 
154 	if (!arpinit_done) {
155 		arpinit_done = 1;
156 		LIST_INIT(&llinfo_arp);
157 		timeout(arptimer, (caddr_t)0, hz);
158 	}
159 	if (rt->rt_flags & RTF_GATEWAY)
160 		return;
161 	switch (req) {
162 
163 	case RTM_ADD:
164 		/*
165 		 * XXX: If this is a manually added route to interface
166 		 * such as older version of routed or gated might provide,
167 		 * restore cloning bit.
168 		 */
169 		if ((rt->rt_flags & RTF_HOST) == 0 &&
170 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
171 			rt->rt_flags |= RTF_CLONING;
172 		if (rt->rt_flags & RTF_CLONING) {
173 			/*
174 			 * Case 1: This route should come from a route to iface.
175 			 */
176 			rt_setgate(rt, rt_key(rt),
177 					(struct sockaddr *)&null_sdl);
178 			gate = rt->rt_gateway;
179 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
180 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
181 			rt->rt_expire = time_second;
182 			break;
183 		}
184 		/* Announce a new entry if requested. */
185 		if (rt->rt_flags & RTF_ANNOUNCE)
186 			arprequest((struct arpcom *)rt->rt_ifp,
187 			    &SIN(rt_key(rt))->sin_addr,
188 			    &SIN(rt_key(rt))->sin_addr,
189 			    (u_char *)LLADDR(SDL(gate)));
190 		/*FALLTHROUGH*/
191 	case RTM_RESOLVE:
192 		if (gate->sa_family != AF_LINK ||
193 		    gate->sa_len < sizeof(null_sdl)) {
194 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
195 			break;
196 		}
197 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
198 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
199 		if (la != 0)
200 			break; /* This happens on a route change */
201 		/*
202 		 * Case 2:  This route may come from cloning, or a manual route
203 		 * add with a LL address.
204 		 */
205 		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
206 		rt->rt_llinfo = (caddr_t)la;
207 		if (la == 0) {
208 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
209 			break;
210 		}
211 		arp_inuse++, arp_allocated++;
212 		Bzero(la, sizeof(*la));
213 		la->la_rt = rt;
214 		rt->rt_flags |= RTF_LLINFO;
215 		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
216 
217 #ifdef INET
218 		/*
219 		 * This keeps the multicast addresses from showing up
220 		 * in `arp -a' listings as unresolved.  It's not actually
221 		 * functional.  Then the same for broadcast.
222 		 */
223 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
224 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
225 					       LLADDR(SDL(gate)));
226 			SDL(gate)->sdl_alen = 6;
227 			rt->rt_expire = 0;
228 		}
229 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
230 			memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
231 			SDL(gate)->sdl_alen = 6;
232 			rt->rt_expire = 0;
233 		}
234 #endif
235 
236 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
237 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
238 		    /*
239 		     * This test used to be
240 		     *	if (loif.if_flags & IFF_UP)
241 		     * It allowed local traffic to be forced
242 		     * through the hardware by configuring the loopback down.
243 		     * However, it causes problems during network configuration
244 		     * for boards that can't receive packets they send.
245 		     * It is now necessary to clear "useloopback" and remove
246 		     * the route to force traffic out to the hardware.
247 		     */
248 			rt->rt_expire = 0;
249 			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
250 				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
251 			if (useloopback)
252 				rt->rt_ifp = loif;
253 
254 		}
255 		break;
256 
257 	case RTM_DELETE:
258 		if (la == 0)
259 			break;
260 		arp_inuse--;
261 		LIST_REMOVE(la, la_le);
262 		rt->rt_llinfo = 0;
263 		rt->rt_flags &= ~RTF_LLINFO;
264 		if (la->la_hold)
265 			m_freem(la->la_hold);
266 		Free((caddr_t)la);
267 	}
268 }
269 
270 /*
271  * Broadcast an ARP request. Caller specifies:
272  *	- arp header source ip address
273  *	- arp header target ip address
274  *	- arp header source ethernet address
275  */
276 static void
277 arprequest(ac, sip, tip, enaddr)
278 	register struct arpcom *ac;
279 	register struct in_addr *sip, *tip;
280 	register u_char *enaddr;
281 {
282 	register struct mbuf *m;
283 	register struct ether_header *eh;
284 	register struct ether_arp *ea;
285 	struct sockaddr sa;
286 
287 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
288 		return;
289 	m->m_len = sizeof(*ea);
290 	m->m_pkthdr.len = sizeof(*ea);
291 	MH_ALIGN(m, sizeof(*ea));
292 	ea = mtod(m, struct ether_arp *);
293 	eh = (struct ether_header *)sa.sa_data;
294 	bzero((caddr_t)ea, sizeof (*ea));
295 	(void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
296 	eh->ether_type = htons(ETHERTYPE_ARP);	/* if_output will not swap */
297 	ea->arp_hrd = htons(ARPHRD_ETHER);
298 	ea->arp_pro = htons(ETHERTYPE_IP);
299 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
300 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
301 	ea->arp_op = htons(ARPOP_REQUEST);
302 	(void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
303 	(void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
304 	(void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
305 	sa.sa_family = AF_UNSPEC;
306 	sa.sa_len = sizeof(sa);
307 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
308 }
309 
310 /*
311  * Resolve an IP address into an ethernet address.  If success,
312  * desten is filled in.  If there is no entry in arptab,
313  * set one up and broadcast a request for the IP address.
314  * Hold onto this mbuf and resend it once the address
315  * is finally resolved.  A return value of 1 indicates
316  * that desten has been filled in and the packet should be sent
317  * normally; a 0 return indicates that the packet has been
318  * taken over here, either now or for later transmission.
319  */
320 int
321 arpresolve(ac, rt, m, dst, desten, rt0)
322 	register struct arpcom *ac;
323 	register struct rtentry *rt;
324 	struct mbuf *m;
325 	register struct sockaddr *dst;
326 	register u_char *desten;
327 	struct rtentry *rt0;
328 {
329 	register struct llinfo_arp *la;
330 	struct sockaddr_dl *sdl;
331 
332 	if (m->m_flags & M_BCAST) {	/* broadcast */
333 		(void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
334 		return (1);
335 	}
336 	if (m->m_flags & M_MCAST) {	/* multicast */
337 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
338 		return(1);
339 	}
340 	if (rt)
341 		la = (struct llinfo_arp *)rt->rt_llinfo;
342 	else {
343 		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
344 		if (la)
345 			rt = la->la_rt;
346 	}
347 	if (la == 0 || rt == 0) {
348 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
349 			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
350 				rt ? "rt" : "");
351 		m_freem(m);
352 		return (0);
353 	}
354 	sdl = SDL(rt->rt_gateway);
355 	/*
356 	 * Check the address family and length is valid, the address
357 	 * is resolved; otherwise, try to resolve.
358 	 */
359 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
360 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
361 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
362 		return 1;
363 	}
364 	/*
365 	 * There is an arptab entry, but no ethernet address
366 	 * response yet.  Replace the held mbuf with this
367 	 * latest one.
368 	 */
369 	if (la->la_hold)
370 		m_freem(la->la_hold);
371 	la->la_hold = m;
372 	if (rt->rt_expire) {
373 		rt->rt_flags &= ~RTF_REJECT;
374 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
375 			rt->rt_expire = time_second;
376 			if (la->la_asked++ < arp_maxtries)
377 			    arprequest(ac,
378 			        &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
379 				&SIN(dst)->sin_addr, 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  */
395 static void
396 arpintr()
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 
426 NETISR_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  */
444 static void
445 in_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 (rt->rt_ifp != &ac->ac_if) {
498 			log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n",
499 			    inet_ntoa(isaddr),
500 			    rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
501 			    ea->arp_sha, ":",
502 			    ac->ac_if.if_name, ac->ac_if.if_unit);
503 			goto reply;
504 		}
505 		if (sdl->sdl_alen &&
506 		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
507 			if (rt->rt_expire)
508 			    log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n",
509 				inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
510 				ea->arp_sha, ":",
511 				ac->ac_if.if_name, ac->ac_if.if_unit);
512 			else {
513 			    log(LOG_ERR,
514 				"arp: %6D attempts to modify permanent entry for %s on %s%d",
515 				ea->arp_sha, ":", inet_ntoa(isaddr),
516 				ac->ac_if.if_name, ac->ac_if.if_unit);
517 			    goto reply;
518 			}
519 		(void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
520 		sdl->sdl_alen = sizeof(ea->arp_sha);
521 		if (rt->rt_expire)
522 			rt->rt_expire = time_second + arpt_keep;
523 		rt->rt_flags &= ~RTF_REJECT;
524 		la->la_asked = 0;
525 		if (la->la_hold) {
526 			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
527 				rt_key(rt), rt);
528 			la->la_hold = 0;
529 		}
530 	}
531 reply:
532 	if (op != ARPOP_REQUEST) {
533 		m_freem(m);
534 		return;
535 	}
536 	if (itaddr.s_addr == myaddr.s_addr) {
537 		/* I am the target */
538 		(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
539 		(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
540 	} else {
541 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
542 		if (la == NULL) {
543 			struct sockaddr_in sin;
544 
545 			if (!arp_proxyall) {
546 				m_freem(m);
547 				return;
548 			}
549 
550 			bzero(&sin, sizeof sin);
551 			sin.sin_family = AF_INET;
552 			sin.sin_len = sizeof sin;
553 			sin.sin_addr = itaddr;
554 
555 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
556 			if (!rt) {
557 				m_freem(m);
558 				return;
559 			}
560 			/*
561 			 * Don't send proxies for nodes on the same interface
562 			 * as this one came out of, or we'll get into a fight
563 			 * over who claims what Ether address.
564 			 */
565 			if (rt->rt_ifp == &ac->ac_if) {
566 				rtfree(rt);
567 				m_freem(m);
568 				return;
569 			}
570 			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
571 			(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
572 			rtfree(rt);
573 #ifdef DEBUG_PROXY
574 			printf("arp: proxying for %s\n",
575 			       inet_ntoa(itaddr));
576 #endif
577 		} else {
578 			rt = la->la_rt;
579 			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
580 			sdl = SDL(rt->rt_gateway);
581 			(void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
582 		}
583 	}
584 
585 	(void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
586 	(void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
587 	ea->arp_op = htons(ARPOP_REPLY);
588 	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
589 	eh = (struct ether_header *)sa.sa_data;
590 	(void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
591 	eh->ether_type = htons(ETHERTYPE_ARP);
592 	sa.sa_family = AF_UNSPEC;
593 	sa.sa_len = sizeof(sa);
594 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
595 	return;
596 }
597 #endif
598 
599 /*
600  * Free an arp entry.
601  */
602 static void
603 arptfree(la)
604 	register struct llinfo_arp *la;
605 {
606 	register struct rtentry *rt = la->la_rt;
607 	register struct sockaddr_dl *sdl;
608 	if (rt == 0)
609 		panic("arptfree");
610 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
611 	    sdl->sdl_family == AF_LINK) {
612 		sdl->sdl_alen = 0;
613 		la->la_asked = 0;
614 		rt->rt_flags &= ~RTF_REJECT;
615 		return;
616 	}
617 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
618 			0, (struct rtentry **)0);
619 }
620 /*
621  * Lookup or enter a new address in arptab.
622  */
623 static struct llinfo_arp *
624 arplookup(addr, create, proxy)
625 	u_long addr;
626 	int create, proxy;
627 {
628 	register struct rtentry *rt;
629 	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
630 	const char *why = 0;
631 
632 	sin.sin_addr.s_addr = addr;
633 	sin.sin_other = proxy ? SIN_PROXY : 0;
634 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
635 	if (rt == 0)
636 		return (0);
637 	rt->rt_refcnt--;
638 
639 	if (rt->rt_flags & RTF_GATEWAY)
640 		why = "host is not on local network";
641 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
642 		why = "could not allocate llinfo";
643 	else if (rt->rt_gateway->sa_family != AF_LINK)
644 		why = "gateway route is not ours";
645 
646 	if (why && create) {
647 		log(LOG_DEBUG, "arplookup %s failed: %s\n",
648 		    inet_ntoa(sin.sin_addr), why);
649 		return 0;
650 	} else if (why) {
651 		return 0;
652 	}
653 	return ((struct llinfo_arp *)rt->rt_llinfo);
654 }
655 
656 void
657 arp_ifinit(ac, ifa)
658 	struct arpcom *ac;
659 	struct ifaddr *ifa;
660 {
661 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
662 		arprequest(ac, &IA_SIN(ifa)->sin_addr,
663 			       &IA_SIN(ifa)->sin_addr, ac->ac_enaddr);
664 	ifa->ifa_rtrequest = arp_rtrequest;
665 	ifa->ifa_flags |= RTF_CLONING;
666 }
667