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