xref: /openbsd-src/sys/netinet/if_ether.c (revision fb8aa7497fded39583f40e800732f9c046411717)
1 /*	$OpenBSD: if_ether.c,v 1.216 2016/06/28 17:18:24 chris Exp $	*/
2 /*	$NetBSD: if_ether.c,v 1.31 1996/05/11 12:59:58 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
33  */
34 
35 /*
36  * Ethernet address resolution protocol.
37  * TODO:
38  *	add "inuse/lock" bit (or ref. count) along with valid bit
39  */
40 
41 #include "carp.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/timeout.h>
48 #include <sys/kernel.h>
49 #include <sys/syslog.h>
50 #include <sys/queue.h>
51 #include <sys/pool.h>
52 
53 #include <net/if.h>
54 #include <net/if_var.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <net/if_types.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 #if NCARP > 0
64 #include <netinet/ip_carp.h>
65 #endif
66 
67 struct llinfo_arp {
68 	LIST_ENTRY(llinfo_arp)	 la_list;
69 	struct rtentry		*la_rt;		/* backpointer to rtentry */
70 	long			 la_asked;	/* last time we QUERIED */
71 	struct mbuf_list	 la_ml;		/* packet hold queue */
72 };
73 #define LA_HOLD_QUEUE 10
74 #define LA_HOLD_TOTAL 100
75 
76 /* timer values */
77 int	arpt_prune = (5 * 60);	/* walk list every 5 minutes */
78 int	arpt_keep = (20 * 60);	/* once resolved, cache for 20 minutes */
79 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
80 
81 void arptfree(struct rtentry *);
82 void arptimer(void *);
83 struct rtentry *arplookup(struct in_addr *, int, int, unsigned int);
84 void in_arpinput(struct ifnet *, struct mbuf *);
85 void in_revarpinput(struct ifnet *, struct mbuf *);
86 int arpcache(struct ifnet *, struct ether_arp *, struct rtentry *);
87 void arpreply(struct ifnet *, struct mbuf *, struct in_addr *, uint8_t *);
88 
89 LIST_HEAD(, llinfo_arp) arp_list;
90 struct	pool arp_pool;		/* pool for llinfo_arp structures */
91 int	arp_maxtries = 5;
92 int	arpinit_done;
93 int	la_hold_total;
94 
95 #ifdef NFSCLIENT
96 /* revarp state */
97 struct in_addr revarp_myip, revarp_srvip;
98 int revarp_finished;
99 unsigned int revarp_ifidx;
100 #endif /* NFSCLIENT */
101 
102 /*
103  * Timeout routine.  Age arp_tab entries periodically.
104  */
105 /* ARGSUSED */
106 void
107 arptimer(void *arg)
108 {
109 	struct timeout *to = (struct timeout *)arg;
110 	int s;
111 	struct llinfo_arp *la, *nla;
112 
113 	s = splsoftnet();
114 	timeout_add_sec(to, arpt_prune);
115 	LIST_FOREACH_SAFE(la, &arp_list, la_list, nla) {
116 		struct rtentry *rt = la->la_rt;
117 
118 		if (rt->rt_expire && rt->rt_expire <= time_uptime)
119 			arptfree(rt); /* timer has expired; clear */
120 	}
121 	splx(s);
122 }
123 
124 void
125 arp_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt)
126 {
127 	struct sockaddr *gate = rt->rt_gateway;
128 	struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
129 	struct ifaddr *ifa;
130 
131 	if (!arpinit_done) {
132 		static struct timeout arptimer_to;
133 
134 		arpinit_done = 1;
135 		pool_init(&arp_pool, sizeof(struct llinfo_arp), 0, 0, 0, "arp",
136 		    NULL);
137 
138 		timeout_set(&arptimer_to, arptimer, &arptimer_to);
139 		timeout_add_sec(&arptimer_to, 1);
140 	}
141 
142 	if (rt->rt_flags & (RTF_GATEWAY|RTF_BROADCAST))
143 		return;
144 
145 	switch (req) {
146 
147 	case RTM_ADD:
148 		if (rt->rt_flags & RTF_CLONING ||
149 		    ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !la)) {
150 			/*
151 			 * Give this route an expiration time, even though
152 			 * it's a "permanent" route, so that routes cloned
153 			 * from it do not need their expiration time set.
154 			 */
155 			rt->rt_expire = time_uptime;
156 			if ((rt->rt_flags & RTF_CLONING) != 0)
157 				break;
158 		}
159 		/*
160 		 * Announce a new entry if requested or warn the user
161 		 * if another station has this IP address.
162 		 */
163 		if (rt->rt_flags & (RTF_ANNOUNCE|RTF_LOCAL))
164 			arprequest(ifp,
165 			    &satosin(rt_key(rt))->sin_addr.s_addr,
166 			    &satosin(rt_key(rt))->sin_addr.s_addr,
167 			    (u_char *)LLADDR(satosdl(gate)));
168 		/*FALLTHROUGH*/
169 	case RTM_RESOLVE:
170 		if (gate->sa_family != AF_LINK ||
171 		    gate->sa_len < sizeof(struct sockaddr_dl)) {
172 			log(LOG_DEBUG, "%s: bad gateway value: %s\n", __func__,
173 			    ifp->if_xname);
174 			break;
175 		}
176 		satosdl(gate)->sdl_type = ifp->if_type;
177 		satosdl(gate)->sdl_index = ifp->if_index;
178 		if (la != 0)
179 			break; /* This happens on a route change */
180 		/*
181 		 * Case 2:  This route may come from cloning, or a manual route
182 		 * add with a LL address.
183 		 */
184 		la = pool_get(&arp_pool, PR_NOWAIT | PR_ZERO);
185 		rt->rt_llinfo = (caddr_t)la;
186 		if (la == NULL) {
187 			log(LOG_DEBUG, "%s: pool get failed\n", __func__);
188 			break;
189 		}
190 
191 		ml_init(&la->la_ml);
192 		la->la_rt = rt;
193 		rt->rt_flags |= RTF_LLINFO;
194 		LIST_INSERT_HEAD(&arp_list, la, la_list);
195 
196 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
197 			if ((ifa->ifa_addr->sa_family == AF_INET) &&
198 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr ==
199 			    satosin(rt_key(rt))->sin_addr.s_addr)
200 				break;
201 		}
202 		if (ifa) {
203 			KASSERT(ifa == rt->rt_ifa);
204 			rt->rt_expire = 0;
205 		}
206 		break;
207 
208 	case RTM_DELETE:
209 		if (la == NULL)
210 			break;
211 		LIST_REMOVE(la, la_list);
212 		rt->rt_llinfo = 0;
213 		rt->rt_flags &= ~RTF_LLINFO;
214 		la_hold_total -= ml_purge(&la->la_ml);
215 		pool_put(&arp_pool, la);
216 	}
217 }
218 
219 /*
220  * Broadcast an ARP request. Caller specifies:
221  *	- arp header source ip address
222  *	- arp header target ip address
223  *	- arp header source ethernet address
224  */
225 void
226 arprequest(struct ifnet *ifp, u_int32_t *sip, u_int32_t *tip, u_int8_t *enaddr)
227 {
228 	struct mbuf *m;
229 	struct ether_header *eh;
230 	struct ether_arp *ea;
231 	struct sockaddr sa;
232 
233 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
234 		return;
235 	m->m_len = sizeof(*ea);
236 	m->m_pkthdr.len = sizeof(*ea);
237 	m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
238 	m->m_pkthdr.pf.prio = ifp->if_llprio;
239 	MH_ALIGN(m, sizeof(*ea));
240 	ea = mtod(m, struct ether_arp *);
241 	eh = (struct ether_header *)sa.sa_data;
242 	memset(ea, 0, sizeof(*ea));
243 	memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
244 	eh->ether_type = htons(ETHERTYPE_ARP);	/* if_output will not swap */
245 	ea->arp_hrd = htons(ARPHRD_ETHER);
246 	ea->arp_pro = htons(ETHERTYPE_IP);
247 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
248 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
249 	ea->arp_op = htons(ARPOP_REQUEST);
250 	memcpy(eh->ether_shost, enaddr, sizeof(eh->ether_shost));
251 	memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
252 	memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
253 	memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
254 	sa.sa_family = pseudo_AF_HDRCMPLT;
255 	sa.sa_len = sizeof(sa);
256 	m->m_flags |= M_BCAST;
257 	ifp->if_output(ifp, m, &sa, NULL);
258 }
259 
260 void
261 arpreply(struct ifnet *ifp, struct mbuf *m, struct in_addr *sip, uint8_t *eaddr)
262 {
263 	struct ether_header *eh;
264 	struct ether_arp *ea;
265 	struct sockaddr sa;
266 
267 	ea = mtod(m, struct ether_arp *);
268 	ea->arp_op = htons(ARPOP_REPLY);
269 	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
270 
271 	/* We're replying to a request. */
272 	memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
273 	memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
274 
275 	memcpy(ea->arp_sha, eaddr, sizeof(ea->arp_sha));
276 	memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
277 
278 	eh = (struct ether_header *)sa.sa_data;
279 	memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
280 	memcpy(eh->ether_shost, eaddr, sizeof(eh->ether_shost));
281 	eh->ether_type = htons(ETHERTYPE_ARP);
282 	sa.sa_family = pseudo_AF_HDRCMPLT;
283 	sa.sa_len = sizeof(sa);
284 	ifp->if_output(ifp, m, &sa, NULL);
285 }
286 
287 /*
288  * Resolve an IP address into an ethernet address.  If success,
289  * desten is filled in.  If there is no entry in arptab,
290  * set one up and broadcast a request for the IP address.
291  * Hold onto this mbuf and resend it once the address
292  * is finally resolved.  A return value of 0 indicates
293  * that desten has been filled in and the packet should be sent
294  * normally; A return value of EAGAIN indicates that the packet
295  * has been taken over here, either now or for later transmission.
296  * Any other return value indicates an error.
297  */
298 int
299 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
300     struct sockaddr *dst, u_char *desten)
301 {
302 	struct arpcom *ac = (struct arpcom *)ifp;
303 	struct llinfo_arp *la = NULL;
304 	struct sockaddr_dl *sdl;
305 	struct rtentry *rt = NULL;
306 	char addr[INET_ADDRSTRLEN];
307 	int error;
308 
309 	if (m->m_flags & M_BCAST) {	/* broadcast */
310 		memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
311 		return (0);
312 	}
313 	if (m->m_flags & M_MCAST) {	/* multicast */
314 		ETHER_MAP_IP_MULTICAST(&satosin(dst)->sin_addr, desten);
315 		return (0);
316 	}
317 
318 	error = rt_checkgate(rt0, &rt);
319 	if (error) {
320 		m_freem(m);
321 		return (error);
322 	}
323 
324 	if (!ISSET(rt->rt_flags, RTF_LLINFO)) {
325 		log(LOG_DEBUG, "%s: %s: route contains no arp information\n",
326 		    __func__, inet_ntop(AF_INET, &satosin(rt_key(rt))->sin_addr,
327 		    addr, sizeof(addr)));
328 		m_freem(m);
329 		return (EINVAL);
330 	}
331 
332 	sdl = satosdl(rt->rt_gateway);
333 	if (sdl->sdl_alen > 0 && sdl->sdl_alen != ETHER_ADDR_LEN) {
334 		log(LOG_DEBUG, "%s: %s: incorrect arp information\n", __func__,
335 		    inet_ntop(AF_INET, &satosin(dst)->sin_addr,
336 			addr, sizeof(addr)));
337 		goto bad;
338 	}
339 
340 	/*
341 	 * Check the address family and length is valid, the address
342 	 * is resolved; otherwise, try to resolve.
343 	 */
344 	if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
345 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
346 		memcpy(desten, LLADDR(sdl), sdl->sdl_alen);
347 		return (0);
348 	}
349 
350 	if (ifp->if_flags & IFF_NOARP)
351 		goto bad;
352 
353 	/*
354 	 * There is an arptab entry, but no ethernet address
355 	 * response yet. Insert mbuf in hold queue if below limit
356 	 * if above the limit free the queue without queuing the new packet.
357 	 */
358 	la = (struct llinfo_arp *)rt->rt_llinfo;
359 	KASSERT(la != NULL);
360 	if (la_hold_total < LA_HOLD_TOTAL && la_hold_total < nmbclust / 64) {
361 		struct mbuf *mh;
362 
363 		if (ml_len(&la->la_ml) >= LA_HOLD_QUEUE) {
364 			mh = ml_dequeue(&la->la_ml);
365 			la_hold_total--;
366 			m_freem(mh);
367 		}
368 		ml_enqueue(&la->la_ml, m);
369 		la_hold_total++;
370 	} else {
371 		la_hold_total -= ml_purge(&la->la_ml);
372 		m_freem(m);
373 	}
374 
375 	/*
376 	 * Re-send the ARP request when appropriate.
377 	 */
378 #ifdef	DIAGNOSTIC
379 	if (rt->rt_expire == 0) {
380 		/* This should never happen. (Should it? -gwr) */
381 		printf("%s: unresolved and rt_expire == 0\n", __func__);
382 		/* Set expiration time to now (expired). */
383 		rt->rt_expire = time_uptime;
384 	}
385 #endif
386 	if (rt->rt_expire) {
387 		rt->rt_flags &= ~RTF_REJECT;
388 		if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
389 			rt->rt_expire = time_uptime;
390 			if (la->la_asked++ < arp_maxtries)
391 				arprequest(ifp,
392 				    &satosin(rt->rt_addr)->sin_addr.s_addr,
393 				    &satosin(dst)->sin_addr.s_addr,
394 				    ac->ac_enaddr);
395 			else {
396 				rt->rt_flags |= RTF_REJECT;
397 				rt->rt_expire += arpt_down;
398 				la->la_asked = 0;
399 				la_hold_total -= ml_purge(&la->la_ml);
400 			}
401 		}
402 	}
403 
404 	return (EAGAIN);
405 
406 bad:
407 	m_freem(m);
408 	return (EINVAL);
409 }
410 
411 /*
412  * Common length and type checks are done here,
413  * then the protocol-specific routine is called.
414  */
415 void
416 arpinput(struct ifnet *ifp, struct mbuf *m)
417 {
418 	struct arphdr *ar;
419 	int len;
420 
421 #ifdef DIAGNOSTIC
422 	if ((m->m_flags & M_PKTHDR) == 0)
423 		panic("arpintr");
424 #endif
425 
426 	len = sizeof(struct arphdr);
427 	if (m->m_len < len && (m = m_pullup(m, len)) == NULL)
428 		return;
429 
430 	ar = mtod(m, struct arphdr *);
431 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER ||
432 	    ntohs(ar->ar_pro) != ETHERTYPE_IP) {
433 		m_freem(m);
434 		return;
435 	}
436 
437 	len += 2 * (ar->ar_hln + ar->ar_pln);
438 	if (m->m_len < len && (m = m_pullup(m, len)) == NULL)
439 		return;
440 
441 	in_arpinput(ifp, m);
442 }
443 
444 /*
445  * ARP for Internet protocols on Ethernet, RFC 826.
446  * In addition, a sanity check is performed on the sender
447  * protocol address, to catch impersonators.
448  */
449 void
450 in_arpinput(struct ifnet *ifp, struct mbuf *m)
451 {
452 	struct ether_arp *ea;
453 	struct rtentry *rt = NULL;
454 	struct sockaddr_in sin;
455 	struct in_addr isaddr, itaddr;
456 	char addr[INET_ADDRSTRLEN];
457 	int op, target = 0;
458 	unsigned int rdomain;
459 
460 	rdomain = rtable_l2(m->m_pkthdr.ph_rtableid);
461 
462 	ea = mtod(m, struct ether_arp *);
463 	op = ntohs(ea->arp_op);
464 	if ((op != ARPOP_REQUEST) && (op != ARPOP_REPLY))
465 		goto out;
466 
467 	memcpy(&itaddr, ea->arp_tpa, sizeof(itaddr));
468 	memcpy(&isaddr, ea->arp_spa, sizeof(isaddr));
469 	memset(&sin, 0, sizeof(sin));
470 	sin.sin_len = sizeof(sin);
471 	sin.sin_family = AF_INET;
472 
473 	if (ETHER_IS_MULTICAST(&ea->arp_sha[0]) &&
474 	    !memcmp(ea->arp_sha, etherbroadcastaddr, sizeof(ea->arp_sha))) {
475 		inet_ntop(AF_INET, &isaddr, addr, sizeof(addr));
476 		log(LOG_ERR, "arp: ether address is broadcast for IP address "
477 		    "%s!\n", addr);
478 		goto out;
479 	}
480 
481 	if (!memcmp(ea->arp_sha, LLADDR(ifp->if_sadl), sizeof(ea->arp_sha)))
482 		goto out;	/* it's from me, ignore it. */
483 
484 	/* Check target against our interface addresses. */
485 	sin.sin_addr = itaddr;
486 	rt = rtalloc(sintosa(&sin), 0, rdomain);
487 	if (rtisvalid(rt) && ISSET(rt->rt_flags, RTF_LOCAL) &&
488 	    rt->rt_ifidx == ifp->if_index)
489 		target = 1;
490 	rtfree(rt);
491 	rt = NULL;
492 
493 #if NCARP > 0
494 	if (target && op == ARPOP_REQUEST && ifp->if_type == IFT_CARP &&
495 	    !carp_iamatch(ifp))
496 		goto out;
497 #endif
498 
499 	/* Do we have an ARP cache for the sender? Create if we are target. */
500 	rt = arplookup(&isaddr, target, 0, rdomain);
501 
502 	/* Check sender against our interface addresses. */
503 	if (rtisvalid(rt) && ISSET(rt->rt_flags, RTF_LOCAL) &&
504 	    rt->rt_ifidx == ifp->if_index && isaddr.s_addr != INADDR_ANY) {
505 		inet_ntop(AF_INET, &isaddr, addr, sizeof(addr));
506 		log(LOG_ERR, "duplicate IP address %s sent from ethernet "
507 		    "address %s\n", addr, ether_sprintf(ea->arp_sha));
508 		itaddr = isaddr;
509 	} else if (rt != NULL) {
510 		if (arpcache(ifp, ea, rt))
511 			goto out;
512 	}
513 
514 	if (op == ARPOP_REQUEST) {
515 		uint8_t *eaddr;
516 
517 		if (target) {
518 			/* We already have all info for the reply */
519 			eaddr = LLADDR(ifp->if_sadl);
520 		} else {
521 			rtfree(rt);
522 			rt = arplookup(&itaddr, 0, SIN_PROXY, rdomain);
523 			/*
524 			 * Protect from possible duplicates, only owner
525 			 * should respond
526 			 */
527 			if ((rt == NULL) || (rt->rt_ifidx != ifp->if_index))
528 				goto out;
529 			eaddr = LLADDR(satosdl(rt->rt_gateway));
530 		}
531 		arpreply(ifp, m, &itaddr, eaddr);
532 		rtfree(rt);
533 		return;
534 	}
535 
536 out:
537 	rtfree(rt);
538 	m_freem(m);
539 }
540 
541 int
542 arpcache(struct ifnet *ifp, struct ether_arp *ea, struct rtentry *rt)
543 {
544 	struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
545 	struct sockaddr_dl *sdl = satosdl(rt->rt_gateway);
546 	struct in_addr *spa = (struct in_addr *)ea->arp_spa;
547 	char addr[INET_ADDRSTRLEN];
548 	struct ifnet *rifp;
549 	unsigned int len;
550 	int changed = 0;
551 
552 	KASSERT(sdl != NULL);
553 
554 	if (sdl->sdl_alen > 0) {
555 		if (memcmp(ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) {
556 			if (ISSET(rt->rt_flags, RTF_PERMANENT_ARP|RTF_LOCAL)) {
557 				inet_ntop(AF_INET, spa, addr, sizeof(addr));
558 				log(LOG_WARNING, "arp: attempt to overwrite "
559 				   "permanent entry for %s by %s on %s\n", addr,
560 				   ether_sprintf(ea->arp_sha), ifp->if_xname);
561 				return (-1);
562 			} else if (rt->rt_ifidx != ifp->if_index) {
563 #if NCARP > 0
564 				if (ifp->if_type != IFT_CARP)
565 #endif
566 				{
567 					rifp = if_get(rt->rt_ifidx);
568 					if (rifp == NULL)
569 						return (-1);
570 					inet_ntop(AF_INET, spa, addr,
571 					    sizeof(addr));
572 					log(LOG_WARNING, "arp: attempt to "
573 					    "overwrite entry for %s on %s by "
574 					    "%s on %s\n", addr, rifp->if_xname,
575 					    ether_sprintf(ea->arp_sha),
576 					    ifp->if_xname);
577 					if_put(rifp);
578 				}
579 				return (-1);
580 			} else {
581 				inet_ntop(AF_INET, spa, addr, sizeof(addr));
582 				log(LOG_INFO, "arp info overwritten for %s by "
583 				    "%s on %s\n", addr,
584 				    ether_sprintf(ea->arp_sha), ifp->if_xname);
585 				rt->rt_expire = 1;/* no longer static */
586 			}
587 			changed = 1;
588 		}
589 	} else if (!if_isconnected(ifp, rt->rt_ifidx)) {
590 		rifp = if_get(rt->rt_ifidx);
591 		if (rifp == NULL)
592 			return (-1);
593 		inet_ntop(AF_INET, spa, addr, sizeof(addr));
594 		log(LOG_WARNING, "arp: attempt to add entry for %s on %s by %s"
595 		    " on %s\n", addr, rifp->if_xname,
596 		    ether_sprintf(ea->arp_sha), ifp->if_xname);
597 		if_put(rifp);
598 		return (-1);
599 	}
600 	sdl->sdl_alen = sizeof(ea->arp_sha);
601 	memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
602 	if (rt->rt_expire)
603 		rt->rt_expire = time_uptime + arpt_keep;
604 	rt->rt_flags &= ~RTF_REJECT;
605 
606 	/* Notify userland that an ARP resolution has been done. */
607 	if (la->la_asked || changed) {
608 		KERNEL_LOCK();
609 		rt_sendmsg(rt, RTM_RESOLVE, ifp->if_rdomain);
610 		KERNEL_UNLOCK();
611 	}
612 
613 	la->la_asked = 0;
614 	while ((len = ml_len(&la->la_ml)) != 0) {
615 		struct mbuf *mh;
616 
617 		mh = ml_dequeue(&la->la_ml);
618 		la_hold_total--;
619 
620 		ifp->if_output(ifp, mh, rt_key(rt), rt);
621 
622 		if (ml_len(&la->la_ml) == len) {
623 			/* mbuf is back in queue. Discard. */
624 			while ((mh = ml_dequeue(&la->la_ml)) != NULL) {
625 				la_hold_total--;
626 				m_freem(mh);
627 			}
628 			break;
629 		}
630 	}
631 
632 	return (0);
633 }
634 /*
635  * Free an arp entry.
636  */
637 void
638 arptfree(struct rtentry *rt)
639 {
640 	struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
641 	struct sockaddr_dl *sdl = satosdl(rt->rt_gateway);
642 	struct ifnet *ifp;
643 
644 	ifp = if_get(rt->rt_ifidx);
645 	if ((sdl != NULL) && (sdl->sdl_family == AF_LINK)) {
646 		sdl->sdl_alen = 0;
647 		la->la_asked = 0;
648 	}
649 
650 	if (!ISSET(rt->rt_flags, RTF_STATIC))
651 		rtdeletemsg(rt, ifp, ifp->if_rdomain);
652 	if_put(ifp);
653 }
654 
655 /*
656  * Lookup or enter a new address in arptab.
657  */
658 struct rtentry *
659 arplookup(struct in_addr *inp, int create, int proxy, u_int tableid)
660 {
661 	struct rtentry *rt;
662 	struct sockaddr_inarp sin;
663 	int flags;
664 
665 	memset(&sin, 0, sizeof(sin));
666 	sin.sin_len = sizeof(sin);
667 	sin.sin_family = AF_INET;
668 	sin.sin_addr.s_addr = inp->s_addr;
669 	sin.sin_other = proxy ? SIN_PROXY : 0;
670 	flags = (create) ? RT_RESOLVE : 0;
671 
672 	rt = rtalloc((struct sockaddr *)&sin, flags, tableid);
673 	if (!rtisvalid(rt) || ISSET(rt->rt_flags, RTF_GATEWAY) ||
674 	    !ISSET(rt->rt_flags, RTF_LLINFO) ||
675 	    rt->rt_gateway->sa_family != AF_LINK) {
676 		rtfree(rt);
677 		return (NULL);
678 	}
679 
680 	if (proxy && !ISSET(rt->rt_flags, RTF_ANNOUNCE)) {
681 		struct rtentry *mrt = NULL;
682 #if defined(ART) && !defined(SMALL_KERNEL)
683 		mrt = rt;
684 		KERNEL_LOCK();
685 		while ((mrt = rtable_mpath_next(mrt)) != NULL) {
686 			if (ISSET(mrt->rt_flags, RTF_ANNOUNCE)) {
687 				rtref(mrt);
688 				break;
689 			}
690 		}
691 		KERNEL_UNLOCK();
692 #endif /* ART && !SMALL_KERNEL */
693 		rtfree(rt);
694 		return (mrt);
695 	}
696 
697 	return (rt);
698 }
699 
700 /*
701  * Check whether we do proxy ARP for this address and we point to ourselves.
702  */
703 int
704 arpproxy(struct in_addr in, unsigned int rtableid)
705 {
706 	struct sockaddr_dl *sdl;
707 	struct rtentry *rt;
708 	struct ifnet *ifp;
709 	int found = 0;
710 
711 	rt = arplookup(&in, 0, SIN_PROXY, rtableid);
712 	if (!rtisvalid(rt)) {
713 		rtfree(rt);
714 		return (0);
715 	}
716 
717 	/* Check that arp information are correct. */
718 	sdl = satosdl(rt->rt_gateway);
719 	if (sdl->sdl_alen != ETHER_ADDR_LEN) {
720 		rtfree(rt);
721 		return (0);
722 	}
723 
724 	ifp = if_get(rt->rt_ifidx);
725 	if (ifp == NULL) {
726 		rtfree(rt);
727 		return (0);
728 	}
729 
730 	if (!memcmp(LLADDR(sdl), LLADDR(ifp->if_sadl), sdl->sdl_alen))
731 		found = 1;
732 
733 	if_put(ifp);
734 	rtfree(rt);
735 	return (found);
736 }
737 
738 /*
739  * Called from Ethernet interrupt handlers
740  * when ether packet type ETHERTYPE_REVARP
741  * is received.  Common length and type checks are done here,
742  * then the protocol-specific routine is called.
743  */
744 void
745 revarpinput(struct ifnet *ifp, struct mbuf *m)
746 {
747 	struct arphdr *ar;
748 
749 	if (m->m_len < sizeof(struct arphdr))
750 		goto out;
751 	ar = mtod(m, struct arphdr *);
752 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
753 		goto out;
754 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
755 		goto out;
756 	switch (ntohs(ar->ar_pro)) {
757 
758 	case ETHERTYPE_IP:
759 		in_revarpinput(ifp, m);
760 		return;
761 
762 	default:
763 		break;
764 	}
765 out:
766 	m_freem(m);
767 }
768 
769 /*
770  * RARP for Internet protocols on Ethernet.
771  * Algorithm is that given in RFC 903.
772  * We are only using for bootstrap purposes to get an ip address for one of
773  * our interfaces.  Thus we support no user-interface.
774  *
775  * Since the contents of the RARP reply are specific to the interface that
776  * sent the request, this code must ensure that they are properly associated.
777  *
778  * Note: also supports ARP via RARP packets, per the RFC.
779  */
780 void
781 in_revarpinput(struct ifnet *ifp, struct mbuf *m)
782 {
783 	struct ether_arp *ar;
784 	int op;
785 
786 	ar = mtod(m, struct ether_arp *);
787 	op = ntohs(ar->arp_op);
788 	switch (op) {
789 	case ARPOP_REQUEST:
790 	case ARPOP_REPLY:	/* per RFC */
791 		in_arpinput(ifp, m);
792 		return;
793 	case ARPOP_REVREPLY:
794 		break;
795 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
796 	default:
797 		goto out;
798 	}
799 #ifdef NFSCLIENT
800 	if (revarp_ifidx == 0)
801 		goto out;
802 	if (revarp_ifidx != m->m_pkthdr.ph_ifidx) /* !same interface */
803 		goto out;
804 	if (revarp_finished)
805 		goto wake;
806 	if (memcmp(ar->arp_tha, LLADDR(ifp->if_sadl), sizeof(ar->arp_tha)))
807 		goto out;
808 	memcpy(&revarp_srvip, ar->arp_spa, sizeof(revarp_srvip));
809 	memcpy(&revarp_myip, ar->arp_tpa, sizeof(revarp_myip));
810 	revarp_finished = 1;
811 wake:	/* Do wakeup every time in case it was missed. */
812 	wakeup((caddr_t)&revarp_myip);
813 #endif /* NFSCLIENT */
814 
815 out:
816 	m_freem(m);
817 }
818 
819 /*
820  * Send a RARP request for the ip address of the specified interface.
821  * The request should be RFC 903-compliant.
822  */
823 void
824 revarprequest(struct ifnet *ifp)
825 {
826 	struct sockaddr sa;
827 	struct mbuf *m;
828 	struct ether_header *eh;
829 	struct ether_arp *ea;
830 	struct arpcom *ac = (struct arpcom *)ifp;
831 
832 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
833 		return;
834 	m->m_len = sizeof(*ea);
835 	m->m_pkthdr.len = sizeof(*ea);
836 	m->m_pkthdr.pf.prio = ifp->if_llprio;
837 	MH_ALIGN(m, sizeof(*ea));
838 	ea = mtod(m, struct ether_arp *);
839 	eh = (struct ether_header *)sa.sa_data;
840 	memset(ea, 0, sizeof(*ea));
841 	memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
842 	eh->ether_type = htons(ETHERTYPE_REVARP);
843 	ea->arp_hrd = htons(ARPHRD_ETHER);
844 	ea->arp_pro = htons(ETHERTYPE_IP);
845 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
846 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
847 	ea->arp_op = htons(ARPOP_REVREQUEST);
848 	memcpy(eh->ether_shost, ac->ac_enaddr, sizeof(ea->arp_tha));
849 	memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
850 	memcpy(ea->arp_tha, ac->ac_enaddr, sizeof(ea->arp_tha));
851 	sa.sa_family = pseudo_AF_HDRCMPLT;
852 	sa.sa_len = sizeof(sa);
853 	m->m_flags |= M_BCAST;
854 	ifp->if_output(ifp, m, &sa, NULL);
855 }
856 
857 #ifdef NFSCLIENT
858 /*
859  * RARP for the ip address of the specified interface, but also
860  * save the ip address of the server that sent the answer.
861  * Timeout if no response is received.
862  */
863 int
864 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
865     struct in_addr *clnt_in)
866 {
867 	int result, count = 20;
868 
869 	if (revarp_finished)
870 		return EIO;
871 
872 	revarp_ifidx = ifp->if_index;
873 	while (count--) {
874 		revarprequest(ifp);
875 		result = tsleep((caddr_t)&revarp_myip, PSOCK, "revarp", hz/2);
876 		if (result != EWOULDBLOCK)
877 			break;
878 	}
879 	revarp_ifidx = 0;
880 	if (!revarp_finished)
881 		return ENETUNREACH;
882 
883 	memcpy(serv_in, &revarp_srvip, sizeof(*serv_in));
884 	memcpy(clnt_in, &revarp_myip, sizeof(*clnt_in));
885 	return 0;
886 }
887 
888 /* For compatibility: only saves interface address. */
889 int
890 revarpwhoami(struct in_addr *in, struct ifnet *ifp)
891 {
892 	struct in_addr server;
893 	return (revarpwhoarewe(ifp, &server, in));
894 }
895 #endif /* NFSCLIENT */
896