xref: /freebsd/sys/netinet/if_ether.c (revision 7573cb47cf4b1f33b6794f82a8a5e0bcbdcc1ada)
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.27 1996/02/05 18:04:30 wollman 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 
64 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
65 
66 /* timer values */
67 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
68 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
69 static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
70 
71 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
72 	   &arpt_prune, 0, "");
73 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
74 	   &arpt_keep, 0, "");
75 SYSCTL_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 
80 struct 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 
88 static	LIST_HEAD(, llinfo_arp) llinfo_arp;
89 
90 struct	ifqueue arpintrq = {0, 0, 0, 50};
91 static int	arp_inuse, arp_allocated;
92 
93 static int	arp_maxtries = 5;
94 static int	useloopback = 1; /* use loopback interface for local traffic */
95 static int	arp_proxyall = 0;
96 
97 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
98 	   &arp_maxtries, 0, "");
99 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
100 	   &useloopback, 0, "");
101 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
102 	   &arp_proxyall, 0, "");
103 
104 static void	arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
105 static void	arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));
106 static void	arpintr __P((void));
107 static void	arptfree __P((struct llinfo_arp *));
108 static void	arptimer __P((void *));
109 static void	arpwhohas __P((struct arpcom *ac, struct in_addr *addr));
110 static struct llinfo_arp
111 		*arplookup __P((u_long, int, int));
112 static void	in_arpinput __P((struct mbuf *));
113 
114 /*
115  * Timeout routine.  Age arp_tab entries periodically.
116  */
117 /* ARGSUSED */
118 static void
119 arptimer(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  */
139 static void
140 arp_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 
213 		/*
214 		 * This keeps the multicast addresses from showing up
215 		 * in `arp -a' listings as unresolved.  It's not actually
216 		 * functional.  Then the same for broadcast.
217 		 */
218 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
219 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
220 					       LLADDR(SDL(gate)));
221 			SDL(gate)->sdl_alen = 6;
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 		}
227 
228 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
229 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
230 		    /*
231 		     * This test used to be
232 		     *	if (loif.if_flags & IFF_UP)
233 		     * It allowed local traffic to be forced
234 		     * through the hardware by configuring the loopback down.
235 		     * However, it causes problems during network configuration
236 		     * for boards that can't receive packets they send.
237 		     * It is now necessary to clear "useloopback" and remove
238 		     * the route to force traffic out to the hardware.
239 		     */
240 			rt->rt_expire = 0;
241 			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
242 				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
243 			if (useloopback)
244 				rt->rt_ifp = loif;
245 
246 		}
247 		break;
248 
249 	case RTM_DELETE:
250 		if (la == 0)
251 			break;
252 		arp_inuse--;
253 		LIST_REMOVE(la, la_le);
254 		rt->rt_llinfo = 0;
255 		rt->rt_flags &= ~RTF_LLINFO;
256 		if (la->la_hold)
257 			m_freem(la->la_hold);
258 		Free((caddr_t)la);
259 	}
260 }
261 /*
262  * Broadcast an ARP packet, asking who has addr on interface ac.
263  */
264 static void
265 arpwhohas(ac, addr)
266 	struct arpcom *ac;
267 	struct in_addr *addr;
268 {
269 	arprequest(ac, &ac->ac_ipaddr.s_addr, &addr->s_addr, ac->ac_enaddr);
270 }
271 
272 /*
273  * Broadcast an ARP request. Caller specifies:
274  *	- arp header source ip address
275  *	- arp header target ip address
276  *	- arp header source ethernet address
277  */
278 static void
279 arprequest(ac, sip, tip, enaddr)
280 	register struct arpcom *ac;
281 	register u_long *sip, *tip;
282 	register u_char *enaddr;
283 {
284 	register struct mbuf *m;
285 	register struct ether_header *eh;
286 	register struct ether_arp *ea;
287 	struct sockaddr sa;
288 
289 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
290 		return;
291 	m->m_len = sizeof(*ea);
292 	m->m_pkthdr.len = sizeof(*ea);
293 	MH_ALIGN(m, sizeof(*ea));
294 	ea = mtod(m, struct ether_arp *);
295 	eh = (struct ether_header *)sa.sa_data;
296 	bzero((caddr_t)ea, sizeof (*ea));
297 	(void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
298 	eh->ether_type = ETHERTYPE_ARP;		/* if_output will swap */
299 	ea->arp_hrd = htons(ARPHRD_ETHER);
300 	ea->arp_pro = htons(ETHERTYPE_IP);
301 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
302 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
303 	ea->arp_op = htons(ARPOP_REQUEST);
304 	(void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
305 	(void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
306 	(void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
307 	sa.sa_family = AF_UNSPEC;
308 	sa.sa_len = sizeof(sa);
309 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
310 }
311 
312 /*
313  * Resolve an IP address into an ethernet address.  If success,
314  * desten is filled in.  If there is no entry in arptab,
315  * set one up and broadcast a request for the IP address.
316  * Hold onto this mbuf and resend it once the address
317  * is finally resolved.  A return value of 1 indicates
318  * that desten has been filled in and the packet should be sent
319  * normally; a 0 return indicates that the packet has been
320  * taken over here, either now or for later transmission.
321  */
322 int
323 arpresolve(ac, rt, m, dst, desten, rt0)
324 	register struct arpcom *ac;
325 	register struct rtentry *rt;
326 	struct mbuf *m;
327 	register struct sockaddr *dst;
328 	register u_char *desten;
329 	struct rtentry *rt0;
330 {
331 	register struct llinfo_arp *la;
332 	struct sockaddr_dl *sdl;
333 
334 	if (m->m_flags & M_BCAST) {	/* broadcast */
335 		(void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
336 		return (1);
337 	}
338 	if (m->m_flags & M_MCAST) {	/* multicast */
339 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
340 		return(1);
341 	}
342 	if (rt)
343 		la = (struct llinfo_arp *)rt->rt_llinfo;
344 	else {
345 		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
346 		if (la)
347 			rt = la->la_rt;
348 	}
349 	if (la == 0 || rt == 0) {
350 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s\n",
351 			inet_ntoa(SIN(dst)->sin_addr));
352 		m_freem(m);
353 		return (0);
354 	}
355 	sdl = SDL(rt->rt_gateway);
356 	/*
357 	 * Check the address family and length is valid, the address
358 	 * is resolved; otherwise, try to resolve.
359 	 */
360 	if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
361 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
362 		(void)memcpy(desten, LLADDR(sdl), sdl->sdl_alen);
363 		return 1;
364 	}
365 	/*
366 	 * There is an arptab entry, but no ethernet address
367 	 * response yet.  Replace the held mbuf with this
368 	 * latest one.
369 	 */
370 	if (la->la_hold)
371 		m_freem(la->la_hold);
372 	la->la_hold = m;
373 	if (rt->rt_expire) {
374 		rt->rt_flags &= ~RTF_REJECT;
375 		if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
376 			rt->rt_expire = time.tv_sec;
377 			if (la->la_asked++ < arp_maxtries)
378 				arpwhohas(ac, &(SIN(dst)->sin_addr));
379 			else {
380 				rt->rt_flags |= RTF_REJECT;
381 				rt->rt_expire += arpt_down;
382 				la->la_asked = 0;
383 			}
384 
385 		}
386 	}
387 	return (0);
388 }
389 
390 /*
391  * Common length and type checks are done here,
392  * then the protocol-specific routine is called.
393  */
394 static void
395 arpintr(void)
396 {
397 	register struct mbuf *m;
398 	register struct arphdr *ar;
399 	int s;
400 
401 	while (arpintrq.ifq_head) {
402 		s = splimp();
403 		IF_DEQUEUE(&arpintrq, m);
404 		splx(s);
405 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
406 			panic("arpintr");
407 		if (m->m_len >= sizeof(struct arphdr) &&
408 		    (ar = mtod(m, struct arphdr *)) &&
409 		    ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
410 		    m->m_len >=
411 		      sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
412 
413 			    switch (ntohs(ar->ar_pro)) {
414 
415 			    case ETHERTYPE_IP:
416 				    in_arpinput(m);
417 				    continue;
418 			    }
419 		m_freem(m);
420 	}
421 }
422 
423 NETISR_SET(NETISR_ARP, arpintr);
424 
425 /*
426  * ARP for Internet protocols on 10 Mb/s Ethernet.
427  * Algorithm is that given in RFC 826.
428  * In addition, a sanity check is performed on the sender
429  * protocol address, to catch impersonators.
430  * We no longer handle negotiations for use of trailer protocol:
431  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
432  * along with IP replies if we wanted trailers sent to us,
433  * and also sent them in response to IP replies.
434  * This allowed either end to announce the desire to receive
435  * trailer packets.
436  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
437  * but formerly didn't normally send requests.
438  */
439 static void
440 in_arpinput(m)
441 	struct mbuf *m;
442 {
443 	register struct ether_arp *ea;
444 	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
445 	struct ether_header *eh;
446 	register struct llinfo_arp *la = 0;
447 	register struct rtentry *rt;
448 	struct in_ifaddr *ia, *maybe_ia = 0;
449 	struct sockaddr_dl *sdl;
450 	struct sockaddr sa;
451 	struct in_addr isaddr, itaddr, myaddr;
452 	int op;
453 
454 	ea = mtod(m, struct ether_arp *);
455 	op = ntohs(ea->arp_op);
456 	(void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
457 	(void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
458 	for (ia = in_ifaddr; ia; ia = ia->ia_next)
459 		if (ia->ia_ifp == &ac->ac_if) {
460 			maybe_ia = ia;
461 			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
462 			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
463 				break;
464 		}
465 	if (maybe_ia == 0) {
466 		m_freem(m);
467 		return;
468 	}
469 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
470 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
471 	    sizeof (ea->arp_sha))) {
472 		m_freem(m);	/* it's from me, ignore it. */
473 		return;
474 	}
475 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
476 	    sizeof (ea->arp_sha))) {
477 		log(LOG_ERR,
478 		    "arp: ether address is broadcast for IP address %s!\n",
479 		    inet_ntoa(isaddr));
480 		m_freem(m);
481 		return;
482 	}
483 	if (isaddr.s_addr == myaddr.s_addr) {
484 		log(LOG_ERR,
485 		   "duplicate IP address %s! sent from ethernet address: %6D\n",
486 		   inet_ntoa(isaddr), ea->arp_sha, ":");
487 		itaddr = myaddr;
488 		goto reply;
489 	}
490 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
491 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
492 		if (sdl->sdl_alen &&
493 		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
494 			log(LOG_INFO, "arp info overwritten for %s by %6D\n",
495 			    inet_ntoa(isaddr), ea->arp_sha, ":");
496 		(void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
497 		sdl->sdl_alen = sizeof(ea->arp_sha);
498 		if (rt->rt_expire)
499 			rt->rt_expire = time.tv_sec + arpt_keep;
500 		rt->rt_flags &= ~RTF_REJECT;
501 		la->la_asked = 0;
502 		if (la->la_hold) {
503 			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
504 				rt_key(rt), rt);
505 			la->la_hold = 0;
506 		}
507 	}
508 reply:
509 	if (op != ARPOP_REQUEST) {
510 		m_freem(m);
511 		return;
512 	}
513 	if (itaddr.s_addr == myaddr.s_addr) {
514 		/* I am the target */
515 		(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
516 		(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
517 	} else {
518 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
519 		if (la == NULL) {
520 			struct sockaddr_in sin;
521 
522 			if (!arp_proxyall) {
523 				m_freem(m);
524 				return;
525 			}
526 
527 			bzero(&sin, sizeof sin);
528 			sin.sin_family = AF_INET;
529 			sin.sin_len = sizeof sin;
530 			sin.sin_addr = itaddr;
531 
532 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
533 			if (!rt) {
534 				m_freem(m);
535 				return;
536 			}
537 			/*
538 			 * Don't send proxies for nodes on the same interface
539 			 * as this one came out of, or we'll get into a fight
540 			 * over who claims what Ether address.
541 			 */
542 			if (rt->rt_ifp == &ac->ac_if) {
543 				rtfree(rt);
544 				m_freem(m);
545 				return;
546 			}
547 			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
548 			(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
549 			rtfree(rt);
550 #ifdef DEBUG_PROXY
551 			printf("arp: proxying for %s\n",
552 			       inet_ntoa(itaddr));
553 #endif
554 		} else {
555 			rt = la->la_rt;
556 			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
557 			sdl = SDL(rt->rt_gateway);
558 			(void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
559 		}
560 	}
561 
562 	(void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
563 	(void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
564 	ea->arp_op = htons(ARPOP_REPLY);
565 	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
566 	eh = (struct ether_header *)sa.sa_data;
567 	(void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
568 	eh->ether_type = ETHERTYPE_ARP;
569 	sa.sa_family = AF_UNSPEC;
570 	sa.sa_len = sizeof(sa);
571 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
572 	return;
573 }
574 
575 /*
576  * Free an arp entry.
577  */
578 static void
579 arptfree(la)
580 	register struct llinfo_arp *la;
581 {
582 	register struct rtentry *rt = la->la_rt;
583 	register struct sockaddr_dl *sdl;
584 	if (rt == 0)
585 		panic("arptfree");
586 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
587 	    sdl->sdl_family == AF_LINK) {
588 		sdl->sdl_alen = 0;
589 		la->la_asked = 0;
590 		rt->rt_flags &= ~RTF_REJECT;
591 		return;
592 	}
593 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
594 			0, (struct rtentry **)0);
595 }
596 /*
597  * Lookup or enter a new address in arptab.
598  */
599 static struct llinfo_arp *
600 arplookup(addr, create, proxy)
601 	u_long addr;
602 	int create, proxy;
603 {
604 	register struct rtentry *rt;
605 	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
606 	const char *why = 0;
607 
608 	sin.sin_addr.s_addr = addr;
609 	sin.sin_other = proxy ? SIN_PROXY : 0;
610 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
611 	if (rt == 0)
612 		return (0);
613 	rt->rt_refcnt--;
614 
615 	if (rt->rt_flags & RTF_GATEWAY)
616 		why = "host is not on local network";
617 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
618 		why = "could not allocate llinfo";
619 	else if (rt->rt_gateway->sa_family != AF_LINK)
620 		why = "gateway route is not ours";
621 
622 	if (why && create) {
623 		log(LOG_DEBUG, "arplookup %s failed: %s\n",
624 		    inet_ntoa(sin.sin_addr), why);
625 		return 0;
626 	} else if (why) {
627 		return 0;
628 	}
629 	return ((struct llinfo_arp *)rt->rt_llinfo);
630 }
631 
632 void
633 arp_ifinit(ac, ifa)
634 	struct arpcom *ac;
635 	struct ifaddr *ifa;
636 {
637 	ac->ac_ipaddr = IA_SIN(ifa)->sin_addr;
638 	arpwhohas(ac, &ac->ac_ipaddr);
639 	ifa->ifa_rtrequest = arp_rtrequest;
640 	ifa->ifa_flags |= RTF_CLONING;
641 }
642