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