xref: /freebsd/sys/netinet/if_ether.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
30  * $FreeBSD$
31  */
32 
33 /*
34  * Ethernet address resolution protocol.
35  * TODO:
36  *	add "inuse/lock" bit (or ref. count) along with valid bit
37  */
38 
39 #include "opt_inet.h"
40 #include "opt_mac.h"
41 #include "opt_carp.h"
42 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/syslog.h>
52 
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
57 #include <net/netisr.h>
58 #include <net/if_llc.h>
59 #include <net/ethernet.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/if_ether.h>
64 
65 #include <net/if_arc.h>
66 #include <net/iso88025.h>
67 
68 #ifdef DEV_CARP
69 #include <netinet/ip_carp.h>
70 #endif
71 
72 #include <security/mac/mac_framework.h>
73 
74 #define SIN(s) ((struct sockaddr_in *)s)
75 #define SDL(s) ((struct sockaddr_dl *)s)
76 
77 SYSCTL_DECL(_net_link_ether);
78 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
79 
80 /* timer values */
81 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
82 
83 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
84 	   &arpt_keep, 0, "ARP entry lifetime in seconds");
85 
86 #define	rt_expire rt_rmx.rmx_expire
87 
88 struct llinfo_arp {
89 	struct	callout la_timer;
90 	struct	rtentry *la_rt;
91 	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
92 	u_short	la_preempt;	/* countdown for pre-expiry arps */
93 	u_short	la_asked;	/* # requests sent */
94 };
95 
96 static struct	ifqueue arpintrq;
97 static int	arp_allocated;
98 
99 static int	arp_maxtries = 5;
100 static int	useloopback = 1; /* use loopback interface for local traffic */
101 static int	arp_proxyall = 0;
102 
103 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
104 	   &arp_maxtries, 0, "ARP resolution attempts before returning error");
105 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
106 	   &useloopback, 0, "Use the loopback interface for local traffic");
107 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
108 	   &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
109 
110 static void	arp_init(void);
111 static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
112 static void	arprequest(struct ifnet *,
113 			struct in_addr *, struct in_addr *, u_char *);
114 static void	arpintr(struct mbuf *);
115 static void	arptimer(void *);
116 static struct rtentry
117 		*arplookup(u_long, int, int);
118 #ifdef INET
119 static void	in_arpinput(struct mbuf *);
120 #endif
121 
122 /*
123  * Timeout routine.
124  */
125 static void
126 arptimer(void *arg)
127 {
128 	struct rtentry *rt = (struct rtentry *)arg;
129 
130 	RT_LOCK_ASSERT(rt);
131 	/*
132 	 * The lock is needed to close a theoretical race
133 	 * between spontaneous expiry and intentional removal.
134 	 * We still got an extra reference on rtentry, so can
135 	 * safely pass pointers to its contents.
136 	 */
137 	RT_UNLOCK(rt);
138 
139 	rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
140 }
141 
142 /*
143  * Parallel to llc_rtrequest.
144  */
145 static void
146 arp_rtrequest(req, rt, info)
147 	int req;
148 	struct rtentry *rt;
149 	struct rt_addrinfo *info;
150 {
151 	struct sockaddr *gate;
152 	struct llinfo_arp *la;
153 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
154 	struct in_ifaddr *ia;
155 	struct ifaddr *ifa;
156 
157 	RT_LOCK_ASSERT(rt);
158 
159 	if (rt->rt_flags & RTF_GATEWAY)
160 		return;
161 	gate = rt->rt_gateway;
162 	la = (struct llinfo_arp *)rt->rt_llinfo;
163 	switch (req) {
164 
165 	case RTM_ADD:
166 		/*
167 		 * XXX: If this is a manually added route to interface
168 		 * such as older version of routed or gated might provide,
169 		 * restore cloning bit.
170 		 */
171 		if ((rt->rt_flags & RTF_HOST) == 0 &&
172 		    rt_mask(rt) != NULL &&
173 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
174 			rt->rt_flags |= RTF_CLONING;
175 		if (rt->rt_flags & RTF_CLONING) {
176 			/*
177 			 * Case 1: This route should come from a route to iface.
178 			 */
179 			rt_setgate(rt, rt_key(rt),
180 					(struct sockaddr *)&null_sdl);
181 			gate = rt->rt_gateway;
182 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
183 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
184 			rt->rt_expire = time_uptime;
185 			break;
186 		}
187 		/* Announce a new entry if requested. */
188 		if (rt->rt_flags & RTF_ANNOUNCE)
189 			arprequest(rt->rt_ifp,
190 			    &SIN(rt_key(rt))->sin_addr,
191 			    &SIN(rt_key(rt))->sin_addr,
192 			    (u_char *)LLADDR(SDL(gate)));
193 		/*FALLTHROUGH*/
194 	case RTM_RESOLVE:
195 		if (gate->sa_family != AF_LINK ||
196 		    gate->sa_len < sizeof(null_sdl)) {
197 			log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
198 			    inet_ntoa(SIN(rt_key(rt))->sin_addr),
199 			    (gate->sa_family != AF_LINK) ?
200 			    " (!AF_LINK)": "");
201 			break;
202 		}
203 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
204 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
205 		if (la != 0)
206 			break; /* This happens on a route change */
207 		/*
208 		 * Case 2:  This route may come from cloning, or a manual route
209 		 * add with a LL address.
210 		 */
211 		R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
212 		rt->rt_llinfo = (caddr_t)la;
213 		if (la == 0) {
214 			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
215 			break;
216 		}
217 		arp_allocated++;
218 		/*
219 		 * We are storing a route entry outside of radix tree. So,
220 		 * it can be found and accessed by other means than radix
221 		 * lookup. The routing code assumes that any rtentry detached
222 		 * from radix can be destroyed safely. To prevent this, we
223 		 * add an additional reference.
224 		 */
225 		RT_ADDREF(rt);
226 		la->la_rt = rt;
227 		rt->rt_flags |= RTF_LLINFO;
228 		callout_init_mtx(&la->la_timer, &rt->rt_mtx,
229 		    CALLOUT_RETURNUNLOCKED);
230 
231 #ifdef INET
232 		/*
233 		 * This keeps the multicast addresses from showing up
234 		 * in `arp -a' listings as unresolved.  It's not actually
235 		 * functional.  Then the same for broadcast.
236 		 */
237 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
238 		    rt->rt_ifp->if_type != IFT_ARCNET) {
239 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
240 					       LLADDR(SDL(gate)));
241 			SDL(gate)->sdl_alen = 6;
242 			rt->rt_expire = 0;
243 		}
244 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
245 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
246 			       rt->rt_ifp->if_addrlen);
247 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
248 			rt->rt_expire = 0;
249 		}
250 #endif
251 
252 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
253 			if (ia->ia_ifp == rt->rt_ifp &&
254 			    SIN(rt_key(rt))->sin_addr.s_addr ==
255 			    (IA_SIN(ia))->sin_addr.s_addr)
256 				break;
257 		}
258 		if (ia) {
259 		    /*
260 		     * This test used to be
261 		     *	if (loif.if_flags & IFF_UP)
262 		     * It allowed local traffic to be forced
263 		     * through the hardware by configuring the loopback down.
264 		     * However, it causes problems during network configuration
265 		     * for boards that can't receive packets they send.
266 		     * It is now necessary to clear "useloopback" and remove
267 		     * the route to force traffic out to the hardware.
268 		     */
269 			rt->rt_expire = 0;
270 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
271 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
272 			if (useloopback) {
273 				rt->rt_ifp = loif;
274 				rt->rt_rmx.rmx_mtu = loif->if_mtu;
275 			}
276 
277 		    /*
278 		     * make sure to set rt->rt_ifa to the interface
279 		     * address we are using, otherwise we will have trouble
280 		     * with source address selection.
281 		     */
282 			ifa = &ia->ia_ifa;
283 			if (ifa != rt->rt_ifa) {
284 				IFAFREE(rt->rt_ifa);
285 				IFAREF(ifa);
286 				rt->rt_ifa = ifa;
287 			}
288 		}
289 		break;
290 
291 	case RTM_DELETE:
292 		if (la == NULL)	/* XXX: at least CARP does this. */
293 			break;
294 		callout_stop(&la->la_timer);
295 		rt->rt_llinfo = NULL;
296 		rt->rt_flags &= ~RTF_LLINFO;
297 		RT_REMREF(rt);
298 		if (la->la_hold)
299 			m_freem(la->la_hold);
300 		Free((caddr_t)la);
301 	}
302 }
303 
304 /*
305  * Broadcast an ARP request. Caller specifies:
306  *	- arp header source ip address
307  *	- arp header target ip address
308  *	- arp header source ethernet address
309  */
310 static void
311 arprequest(ifp, sip, tip, enaddr)
312 	struct ifnet *ifp;
313 	struct in_addr *sip, *tip;
314 	u_char *enaddr;
315 {
316 	struct mbuf *m;
317 	struct arphdr *ah;
318 	struct sockaddr sa;
319 
320 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
321 		return;
322 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
323 		2*ifp->if_data.ifi_addrlen;
324 	m->m_pkthdr.len = m->m_len;
325 	MH_ALIGN(m, m->m_len);
326 	ah = mtod(m, struct arphdr *);
327 	bzero((caddr_t)ah, m->m_len);
328 #ifdef MAC
329 	mac_create_mbuf_linklayer(ifp, m);
330 #endif
331 	ah->ar_pro = htons(ETHERTYPE_IP);
332 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
333 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
334 	ah->ar_op = htons(ARPOP_REQUEST);
335 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
336 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
337 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
338 	sa.sa_family = AF_ARP;
339 	sa.sa_len = 2;
340 	m->m_flags |= M_BCAST;
341 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
342 
343 	return;
344 }
345 
346 /*
347  * Resolve an IP address into an ethernet address.
348  * On input:
349  *    ifp is the interface we use
350  *    dst is the next hop,
351  *    rt0 is the route to the final destination (possibly useless)
352  *    m is the mbuf
353  *    desten is where we want the address.
354  *
355  * On success, desten is filled in and the function returns 0;
356  * If the packet must be held pending resolution, we return EWOULDBLOCK
357  * On other errors, we return the corresponding error code.
358  */
359 int
360 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
361 	struct sockaddr *dst, u_char *desten)
362 {
363 	struct llinfo_arp *la = NULL;
364 	struct rtentry *rt = NULL;
365 	struct sockaddr_dl *sdl;
366 	int error;
367 
368 	if (m->m_flags & M_BCAST) {	/* broadcast */
369 		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
370 		return (0);
371 	}
372 	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
373 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
374 		return (0);
375 	}
376 
377 	if (rt0 != NULL) {
378 		error = rt_check(&rt, &rt0, dst);
379 		if (error) {
380 			m_freem(m);
381 			return error;
382 		}
383 		la = (struct llinfo_arp *)rt->rt_llinfo;
384 		if (la == NULL)
385 			RT_UNLOCK(rt);
386 	}
387 	if (la == NULL) {
388 		/*
389 		 * We enter this block in case if rt0 was NULL,
390 		 * or if rt found by rt_check() didn't have llinfo.
391 		 */
392 		rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
393 		if (rt == NULL) {
394 			log(LOG_DEBUG,
395 			    "arpresolve: can't allocate route for %s\n",
396 			    inet_ntoa(SIN(dst)->sin_addr));
397 			m_freem(m);
398 			return (EINVAL); /* XXX */
399 		}
400 		la = (struct llinfo_arp *)rt->rt_llinfo;
401 		if (la == NULL) {
402 			RT_UNLOCK(rt);
403 			log(LOG_DEBUG,
404 			    "arpresolve: can't allocate llinfo for %s\n",
405 			    inet_ntoa(SIN(dst)->sin_addr));
406 			m_freem(m);
407 			return (EINVAL); /* XXX */
408 		}
409 	}
410 	sdl = SDL(rt->rt_gateway);
411 	/*
412 	 * Check the address family and length is valid, the address
413 	 * is resolved; otherwise, try to resolve.
414 	 */
415 	if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
416 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
417 
418 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
419 
420 		/*
421 		 * If entry has an expiry time and it is approaching,
422 		 * send an ARP request.
423 		 */
424 		if ((rt->rt_expire != 0) &&
425 		    (time_uptime + la->la_preempt > rt->rt_expire)) {
426 			struct in_addr sin =
427 			    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
428 
429 			la->la_preempt--;
430 			RT_UNLOCK(rt);
431 			arprequest(ifp, &sin, &SIN(dst)->sin_addr,
432 			    IF_LLADDR(ifp));
433 			return (0);
434 		}
435 
436 		RT_UNLOCK(rt);
437 		return (0);
438 	}
439 	/*
440 	 * If ARP is disabled or static on this interface, stop.
441 	 * XXX
442 	 * Probably should not allocate empty llinfo struct if we are
443 	 * not going to be sending out an arp request.
444 	 */
445 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
446 		RT_UNLOCK(rt);
447 		m_freem(m);
448 		return (EINVAL);
449 	}
450 	/*
451 	 * There is an arptab entry, but no ethernet address
452 	 * response yet.  Replace the held mbuf with this
453 	 * latest one.
454 	 */
455 	if (la->la_hold)
456 		m_freem(la->la_hold);
457 	la->la_hold = m;
458 
459 	KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
460 
461 	/*
462 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
463 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
464 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
465 	 * ARP request, but not faster than one request per second.
466 	 */
467 	if (la->la_asked < arp_maxtries)
468 		error = EWOULDBLOCK;	/* First request. */
469 	else
470 		error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
471 
472 	if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
473 		struct in_addr sin =
474 		    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
475 
476 		rt->rt_expire = time_uptime;
477 		callout_reset(&la->la_timer, hz, arptimer, rt);
478 		la->la_asked++;
479 		RT_UNLOCK(rt);
480 
481 		arprequest(ifp, &sin, &SIN(dst)->sin_addr,
482 		    IF_LLADDR(ifp));
483 	} else
484 		RT_UNLOCK(rt);
485 
486 	return (error);
487 }
488 
489 /*
490  * Common length and type checks are done here,
491  * then the protocol-specific routine is called.
492  */
493 static void
494 arpintr(struct mbuf *m)
495 {
496 	struct arphdr *ar;
497 
498 	if (m->m_len < sizeof(struct arphdr) &&
499 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
500 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
501 		return;
502 	}
503 	ar = mtod(m, struct arphdr *);
504 
505 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
506 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
507 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
508 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
509 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
510 		    (unsigned char *)&ar->ar_hrd, "");
511 		m_freem(m);
512 		return;
513 	}
514 
515 	if (m->m_len < arphdr_len(ar)) {
516 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
517 			log(LOG_ERR, "arp: runt packet\n");
518 			m_freem(m);
519 			return;
520 		}
521 		ar = mtod(m, struct arphdr *);
522 	}
523 
524 	switch (ntohs(ar->ar_pro)) {
525 #ifdef INET
526 	case ETHERTYPE_IP:
527 		in_arpinput(m);
528 		return;
529 #endif
530 	}
531 	m_freem(m);
532 }
533 
534 #ifdef INET
535 /*
536  * ARP for Internet protocols on 10 Mb/s Ethernet.
537  * Algorithm is that given in RFC 826.
538  * In addition, a sanity check is performed on the sender
539  * protocol address, to catch impersonators.
540  * We no longer handle negotiations for use of trailer protocol:
541  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
542  * along with IP replies if we wanted trailers sent to us,
543  * and also sent them in response to IP replies.
544  * This allowed either end to announce the desire to receive
545  * trailer packets.
546  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
547  * but formerly didn't normally send requests.
548  */
549 static int log_arp_wrong_iface = 1;
550 static int log_arp_movements = 1;
551 static int log_arp_permanent_modify = 1;
552 
553 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
554 	&log_arp_wrong_iface, 0,
555 	"log arp packets arriving on the wrong interface");
556 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
557         &log_arp_movements, 0,
558         "log arp replies from MACs different than the one in the cache");
559 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
560         &log_arp_permanent_modify, 0,
561         "log arp replies from MACs different than the one in the permanent arp entry");
562 
563 
564 static void
565 in_arpinput(m)
566 	struct mbuf *m;
567 {
568 	struct arphdr *ah;
569 	struct ifnet *ifp = m->m_pkthdr.rcvif;
570 	struct llinfo_arp *la;
571 	struct rtentry *rt;
572 	struct ifaddr *ifa;
573 	struct in_ifaddr *ia;
574 	struct sockaddr_dl *sdl;
575 	struct sockaddr sa;
576 	struct in_addr isaddr, itaddr, myaddr;
577 	struct mbuf *hold;
578 	u_int8_t *enaddr = NULL;
579 	int op, rif_len;
580 	int req_len;
581 	int bridged = 0;
582 #ifdef DEV_CARP
583 	int carp_match = 0;
584 #endif
585 
586 	if (ifp->if_bridge)
587 		bridged = 1;
588 
589 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
590 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
591 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
592 		return;
593 	}
594 
595 	ah = mtod(m, struct arphdr *);
596 	op = ntohs(ah->ar_op);
597 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
598 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
599 
600 	/*
601 	 * For a bridge, we want to check the address irrespective
602 	 * of the receive interface. (This will change slightly
603 	 * when we have clusters of interfaces).
604 	 * If the interface does not match, but the recieving interface
605 	 * is part of carp, we call carp_iamatch to see if this is a
606 	 * request for the virtual host ip.
607 	 * XXX: This is really ugly!
608 	 */
609 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
610 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
611 		    (ia->ia_ifp == ifp)) &&
612 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
613 			goto match;
614 #ifdef DEV_CARP
615 		if (ifp->if_carp != NULL &&
616 		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
617 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
618 			carp_match = 1;
619 			goto match;
620 		}
621 #endif
622 	}
623 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
624 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
625 		    (ia->ia_ifp == ifp)) &&
626 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
627 			goto match;
628 	/*
629 	 * No match, use the first inet address on the receive interface
630 	 * as a dummy address for the rest of the function.
631 	 */
632 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
633 		if (ifa->ifa_addr->sa_family == AF_INET) {
634 			ia = ifatoia(ifa);
635 			goto match;
636 		}
637 	/*
638 	 * If bridging, fall back to using any inet address.
639 	 */
640 	if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
641 		goto drop;
642 match:
643 	if (!enaddr)
644 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
645 	myaddr = ia->ia_addr.sin_addr;
646 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
647 		goto drop;	/* it's from me, ignore it. */
648 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
649 		log(LOG_ERR,
650 		    "arp: link address is broadcast for IP address %s!\n",
651 		    inet_ntoa(isaddr));
652 		goto drop;
653 	}
654 	/*
655 	 * Warn if another host is using the same IP address, but only if the
656 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
657 	 * case we suppress the warning to avoid false positive complaints of
658 	 * potential misconfiguration.
659 	 */
660 	if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
661 		log(LOG_ERR,
662 		   "arp: %*D is using my IP address %s!\n",
663 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
664 		   inet_ntoa(isaddr));
665 		itaddr = myaddr;
666 		goto reply;
667 	}
668 	if (ifp->if_flags & IFF_STATICARP)
669 		goto reply;
670 	rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
671 	if (rt != NULL) {
672 		la = (struct llinfo_arp *)rt->rt_llinfo;
673 		if (la == NULL) {
674 			RT_UNLOCK(rt);
675 			goto reply;
676 		}
677 	} else
678 		goto reply;
679 
680 	/* The following is not an error when doing bridging. */
681 	if (!bridged && rt->rt_ifp != ifp
682 #ifdef DEV_CARP
683 	    && (ifp->if_type != IFT_CARP || !carp_match)
684 #endif
685 							) {
686 		if (log_arp_wrong_iface)
687 			log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
688 			    inet_ntoa(isaddr),
689 			    rt->rt_ifp->if_xname,
690 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
691 			    ifp->if_xname);
692 		RT_UNLOCK(rt);
693 		goto reply;
694 	}
695 	sdl = SDL(rt->rt_gateway);
696 	if (sdl->sdl_alen &&
697 	    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
698 		if (rt->rt_expire) {
699 		    if (log_arp_movements)
700 		        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
701 			    inet_ntoa(isaddr),
702 			    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
703 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
704 			    ifp->if_xname);
705 		} else {
706 			RT_UNLOCK(rt);
707 			if (log_arp_permanent_modify)
708 				log(LOG_ERR, "arp: %*D attempts to modify "
709 				    "permanent entry for %s on %s\n",
710 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
711 				    inet_ntoa(isaddr), ifp->if_xname);
712 			goto reply;
713 		}
714 	}
715 	/*
716 	 * sanity check for the address length.
717 	 * XXX this does not work for protocols with variable address
718 	 * length. -is
719 	 */
720 	if (sdl->sdl_alen &&
721 	    sdl->sdl_alen != ah->ar_hln) {
722 		log(LOG_WARNING,
723 		    "arp from %*D: new addr len %d, was %d",
724 		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
725 		    ah->ar_hln, sdl->sdl_alen);
726 	}
727 	if (ifp->if_addrlen != ah->ar_hln) {
728 		log(LOG_WARNING,
729 		    "arp from %*D: addr len: new %d, i/f %d (ignored)",
730 		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
731 		    ah->ar_hln, ifp->if_addrlen);
732 		RT_UNLOCK(rt);
733 		goto reply;
734 	}
735 	(void)memcpy(LLADDR(sdl), ar_sha(ah),
736 	    sdl->sdl_alen = ah->ar_hln);
737 	/*
738 	 * If we receive an arp from a token-ring station over
739 	 * a token-ring nic then try to save the source
740 	 * routing info.
741 	 */
742 	if (ifp->if_type == IFT_ISO88025) {
743 		struct iso88025_header *th = NULL;
744 		struct iso88025_sockaddr_dl_data *trld;
745 
746 		th = (struct iso88025_header *)m->m_pkthdr.header;
747 		trld = SDL_ISO88025(sdl);
748 		rif_len = TR_RCF_RIFLEN(th->rcf);
749 		if ((th->iso88025_shost[0] & TR_RII) &&
750 		    (rif_len > 2)) {
751 			trld->trld_rcf = th->rcf;
752 			trld->trld_rcf ^= htons(TR_RCF_DIR);
753 			memcpy(trld->trld_route, th->rd, rif_len - 2);
754 			trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
755 			/*
756 			 * Set up source routing information for
757 			 * reply packet (XXX)
758 			 */
759 			m->m_data -= rif_len;
760 			m->m_len  += rif_len;
761 			m->m_pkthdr.len += rif_len;
762 		} else {
763 			th->iso88025_shost[0] &= ~TR_RII;
764 			trld->trld_rcf = 0;
765 		}
766 		m->m_data -= 8;
767 		m->m_len  += 8;
768 		m->m_pkthdr.len += 8;
769 		th->rcf = trld->trld_rcf;
770 	}
771 	if (rt->rt_expire) {
772 		rt->rt_expire = time_uptime + arpt_keep;
773 		callout_reset(&la->la_timer, hz * arpt_keep, arptimer, rt);
774 	}
775 	la->la_asked = 0;
776 	la->la_preempt = arp_maxtries;
777 	hold = la->la_hold;
778 	la->la_hold = NULL;
779 	RT_UNLOCK(rt);
780 	if (hold != NULL)
781 		(*ifp->if_output)(ifp, hold, rt_key(rt), rt);
782 
783 reply:
784 	if (op != ARPOP_REQUEST)
785 		goto drop;
786 	if (itaddr.s_addr == myaddr.s_addr) {
787 		/* I am the target */
788 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
789 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
790 	} else {
791 		rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
792 		if (rt == NULL) {
793 			struct sockaddr_in sin;
794 
795 			if (!arp_proxyall)
796 				goto drop;
797 
798 			bzero(&sin, sizeof sin);
799 			sin.sin_family = AF_INET;
800 			sin.sin_len = sizeof sin;
801 			sin.sin_addr = itaddr;
802 
803 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
804 			if (!rt)
805 				goto drop;
806 			/*
807 			 * Don't send proxies for nodes on the same interface
808 			 * as this one came out of, or we'll get into a fight
809 			 * over who claims what Ether address.
810 			 */
811 			if (rt->rt_ifp == ifp) {
812 				rtfree(rt);
813 				goto drop;
814 			}
815 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
816 			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
817 			rtfree(rt);
818 
819 			/*
820 			 * Also check that the node which sent the ARP packet
821 			 * is on the the interface we expect it to be on. This
822 			 * avoids ARP chaos if an interface is connected to the
823 			 * wrong network.
824 			 */
825 			sin.sin_addr = isaddr;
826 
827 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
828 			if (!rt)
829 				goto drop;
830 			if (rt->rt_ifp != ifp) {
831 				log(LOG_INFO, "arp_proxy: ignoring request"
832 				    " from %s via %s, expecting %s\n",
833 				    inet_ntoa(isaddr), ifp->if_xname,
834 				    rt->rt_ifp->if_xname);
835 				rtfree(rt);
836 				goto drop;
837 			}
838 			rtfree(rt);
839 
840 #ifdef DEBUG_PROXY
841 			printf("arp: proxying for %s\n",
842 			       inet_ntoa(itaddr));
843 #endif
844 		} else {
845 			/*
846 			 * Return proxied ARP replies only on the interface
847 			 * or bridge cluster where this network resides.
848 			 * Otherwise we may conflict with the host we are
849 			 * proxying for.
850 			 */
851 			if (rt->rt_ifp != ifp &&
852 			    (rt->rt_ifp->if_bridge != ifp->if_bridge ||
853 			    ifp->if_bridge == NULL)) {
854 				RT_UNLOCK(rt);
855 				goto drop;
856 			}
857 			sdl = SDL(rt->rt_gateway);
858 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
859 			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
860 			RT_UNLOCK(rt);
861 		}
862 	}
863 
864 	if (itaddr.s_addr == myaddr.s_addr &&
865 	    IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
866 		/* RFC 3927 link-local IPv4; always reply by broadcast. */
867 #ifdef DEBUG_LINKLOCAL
868 		printf("arp: sending reply for link-local addr %s\n",
869 		    inet_ntoa(itaddr));
870 #endif
871 		m->m_flags |= M_BCAST;
872 		m->m_flags &= ~M_MCAST;
873 	} else {
874 		/* default behaviour; never reply by broadcast. */
875 		m->m_flags &= ~(M_BCAST|M_MCAST);
876 	}
877 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
878 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
879 	ah->ar_op = htons(ARPOP_REPLY);
880 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
881 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
882 	m->m_pkthdr.len = m->m_len;
883 	sa.sa_family = AF_ARP;
884 	sa.sa_len = 2;
885 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
886 	return;
887 
888 drop:
889 	m_freem(m);
890 }
891 #endif
892 
893 /*
894  * Lookup or enter a new address in arptab.
895  */
896 static struct rtentry *
897 arplookup(addr, create, proxy)
898 	u_long addr;
899 	int create, proxy;
900 {
901 	struct rtentry *rt;
902 	struct sockaddr_inarp sin;
903 	const char *why = 0;
904 
905 	bzero(&sin, sizeof(sin));
906 	sin.sin_len = sizeof(sin);
907 	sin.sin_family = AF_INET;
908 	sin.sin_addr.s_addr = addr;
909 	if (proxy)
910 		sin.sin_other = SIN_PROXY;
911 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
912 	if (rt == 0)
913 		return (0);
914 
915 	if (rt->rt_flags & RTF_GATEWAY)
916 		why = "host is not on local network";
917 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
918 		why = "could not allocate llinfo";
919 	else if (rt->rt_gateway->sa_family != AF_LINK)
920 		why = "gateway route is not ours";
921 
922 	if (why) {
923 #define	ISDYNCLONE(_rt) \
924 	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
925 		if (create)
926 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
927 			    inet_ntoa(sin.sin_addr), why);
928 		/*
929 		 * If there are no references to this Layer 2 route,
930 		 * and it is a cloned route, and not static, and
931 		 * arplookup() is creating the route, then purge
932 		 * it from the routing table as it is probably bogus.
933 		 */
934 		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
935 			rtexpunge(rt);
936 		RTFREE_LOCKED(rt);
937 		return (0);
938 #undef ISDYNCLONE
939 	} else {
940 		RT_REMREF(rt);
941 		return (rt);
942 	}
943 }
944 
945 void
946 arp_ifinit(ifp, ifa)
947 	struct ifnet *ifp;
948 	struct ifaddr *ifa;
949 {
950 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
951 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
952 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
953 	ifa->ifa_rtrequest = arp_rtrequest;
954 	ifa->ifa_flags |= RTF_CLONING;
955 }
956 
957 void
958 arp_ifinit2(ifp, ifa, enaddr)
959 	struct ifnet *ifp;
960 	struct ifaddr *ifa;
961 	u_char *enaddr;
962 {
963 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
964 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
965 				&IA_SIN(ifa)->sin_addr, enaddr);
966 	ifa->ifa_rtrequest = arp_rtrequest;
967 	ifa->ifa_flags |= RTF_CLONING;
968 }
969 
970 static void
971 arp_init(void)
972 {
973 
974 	arpintrq.ifq_maxlen = 50;
975 	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
976 	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
977 }
978 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
979