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