xref: /freebsd/sys/netinet/if_ether.c (revision 84ee9401a3fc8d3c22424266f421a928989cd692)
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 				rt->rt_rmx.rmx_mtu = loif->if_mtu;
300 			}
301 
302 		    /*
303 		     * make sure to set rt->rt_ifa to the interface
304 		     * address we are using, otherwise we will have trouble
305 		     * with source address selection.
306 		     */
307 			ifa = &ia->ia_ifa;
308 			if (ifa != rt->rt_ifa) {
309 				IFAFREE(rt->rt_ifa);
310 				IFAREF(ifa);
311 				rt->rt_ifa = ifa;
312 			}
313 		}
314 		break;
315 
316 	case RTM_DELETE:
317 		if (la == 0)
318 			break;
319 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
320 		LIST_REMOVE(la, la_le);
321 		RT_REMREF(rt);
322 		rt->rt_llinfo = 0;
323 		rt->rt_flags &= ~RTF_LLINFO;
324 		if (la->la_hold)
325 			m_freem(la->la_hold);
326 		Free((caddr_t)la);
327 	}
328 }
329 
330 /*
331  * Broadcast an ARP request. Caller specifies:
332  *	- arp header source ip address
333  *	- arp header target ip address
334  *	- arp header source ethernet address
335  */
336 static void
337 arprequest(ifp, sip, tip, enaddr)
338 	struct ifnet *ifp;
339 	struct in_addr *sip, *tip;
340 	u_char *enaddr;
341 {
342 	struct mbuf *m;
343 	struct arphdr *ah;
344 	struct sockaddr sa;
345 
346 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
347 		return;
348 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
349 		2*ifp->if_data.ifi_addrlen;
350 	m->m_pkthdr.len = m->m_len;
351 	MH_ALIGN(m, m->m_len);
352 	ah = mtod(m, struct arphdr *);
353 	bzero((caddr_t)ah, m->m_len);
354 #ifdef MAC
355 	mac_create_mbuf_linklayer(ifp, m);
356 #endif
357 	ah->ar_pro = htons(ETHERTYPE_IP);
358 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
359 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
360 	ah->ar_op = htons(ARPOP_REQUEST);
361 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
362 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
363 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
364 	sa.sa_family = AF_ARP;
365 	sa.sa_len = 2;
366 	m->m_flags |= M_BCAST;
367 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
368 
369 	return;
370 }
371 
372 /*
373  * Resolve an IP address into an ethernet address.
374  * On input:
375  *    ifp is the interface we use
376  *    dst is the next hop,
377  *    rt0 is the route to the final destination (possibly useless)
378  *    m is the mbuf
379  *    desten is where we want the address.
380  *
381  * On success, desten is filled in and the function returns 0;
382  * If the packet must be held pending resolution, we return EWOULDBLOCK
383  * On other errors, we return the corresponding error code.
384  */
385 int
386 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
387 	struct sockaddr *dst, u_char *desten)
388 {
389 	struct llinfo_arp *la = NULL;
390 	struct rtentry *rt = NULL;
391 	struct sockaddr_dl *sdl;
392 	int error;
393 
394 	if (m->m_flags & M_BCAST) {	/* broadcast */
395 		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
396 		return (0);
397 	}
398 	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
399 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
400 		return (0);
401 	}
402 
403 	if (rt0 != NULL) {
404 		error = rt_check(&rt, &rt0, dst);
405 		if (error) {
406 			m_freem(m);
407 			return error;
408 		}
409 		la = (struct llinfo_arp *)rt->rt_llinfo;
410 		if (la == NULL)
411 			RT_UNLOCK(rt);
412 	}
413 	if (la == NULL) {
414 		/*
415 		 * We enter this block in case if rt0 was NULL,
416 		 * or if rt found by rt_check() didn't have llinfo.
417 		 */
418 		rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
419 		if (rt == NULL) {
420 			log(LOG_DEBUG,
421 			    "arpresolve: can't allocate route for %s\n",
422 			    inet_ntoa(SIN(dst)->sin_addr));
423 			m_freem(m);
424 			return (EINVAL); /* XXX */
425 		}
426 		la = (struct llinfo_arp *)rt->rt_llinfo;
427 		if (la == NULL) {
428 			RT_UNLOCK(rt);
429 			log(LOG_DEBUG,
430 			    "arpresolve: can't allocate llinfo for %s\n",
431 			    inet_ntoa(SIN(dst)->sin_addr));
432 			m_freem(m);
433 			return (EINVAL); /* XXX */
434 		}
435 	}
436 	sdl = SDL(rt->rt_gateway);
437 	/*
438 	 * Check the address family and length is valid, the address
439 	 * is resolved; otherwise, try to resolve.
440 	 */
441 	if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
442 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
443 
444 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
445 
446 		/*
447 		 * If entry has an expiry time and it is approaching,
448 		 * send an ARP request.
449 		 */
450 		if ((rt->rt_expire != 0) &&
451 		    (time_uptime + la->la_preempt > rt->rt_expire)) {
452 			struct in_addr sin =
453 			    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
454 
455 			la->la_preempt--;
456 			RT_UNLOCK(rt);
457 			arprequest(ifp, &sin, &SIN(dst)->sin_addr,
458 			    IF_LLADDR(ifp));
459 			return (0);
460 		}
461 
462 		RT_UNLOCK(rt);
463 		return (0);
464 	}
465 	/*
466 	 * If ARP is disabled or static on this interface, stop.
467 	 * XXX
468 	 * Probably should not allocate empty llinfo struct if we are
469 	 * not going to be sending out an arp request.
470 	 */
471 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
472 		RT_UNLOCK(rt);
473 		m_freem(m);
474 		return (EINVAL);
475 	}
476 	/*
477 	 * There is an arptab entry, but no ethernet address
478 	 * response yet.  Replace the held mbuf with this
479 	 * latest one.
480 	 */
481 	if (la->la_hold)
482 		m_freem(la->la_hold);
483 	la->la_hold = m;
484 
485 	KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
486 
487 	/*
488 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
489 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
490 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
491 	 * ARP request, but not faster than one request per second.
492 	 */
493 	if (la->la_asked < arp_maxtries)
494 		error = EWOULDBLOCK;	/* First request. */
495 	else
496 		error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
497 
498 	if (la->la_asked++ == 0 || rt->rt_expire != time_uptime) {
499 		struct in_addr sin =
500 		    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
501 
502 		rt->rt_expire = time_uptime;
503 		RT_UNLOCK(rt);
504 
505 		arprequest(ifp, &sin, &SIN(dst)->sin_addr,
506 		    IF_LLADDR(ifp));
507 	} else
508 		RT_UNLOCK(rt);
509 
510 	return (error);
511 }
512 
513 /*
514  * Common length and type checks are done here,
515  * then the protocol-specific routine is called.
516  */
517 static void
518 arpintr(struct mbuf *m)
519 {
520 	struct arphdr *ar;
521 
522 	if (m->m_len < sizeof(struct arphdr) &&
523 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
524 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
525 		return;
526 	}
527 	ar = mtod(m, struct arphdr *);
528 
529 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
530 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
531 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
532 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
533 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
534 		    (unsigned char *)&ar->ar_hrd, "");
535 		m_freem(m);
536 		return;
537 	}
538 
539 	if (m->m_len < arphdr_len(ar)) {
540 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
541 			log(LOG_ERR, "arp: runt packet\n");
542 			m_freem(m);
543 			return;
544 		}
545 		ar = mtod(m, struct arphdr *);
546 	}
547 
548 	switch (ntohs(ar->ar_pro)) {
549 #ifdef INET
550 	case ETHERTYPE_IP:
551 		in_arpinput(m);
552 		return;
553 #endif
554 	}
555 	m_freem(m);
556 }
557 
558 #ifdef INET
559 /*
560  * ARP for Internet protocols on 10 Mb/s Ethernet.
561  * Algorithm is that given in RFC 826.
562  * In addition, a sanity check is performed on the sender
563  * protocol address, to catch impersonators.
564  * We no longer handle negotiations for use of trailer protocol:
565  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
566  * along with IP replies if we wanted trailers sent to us,
567  * and also sent them in response to IP replies.
568  * This allowed either end to announce the desire to receive
569  * trailer packets.
570  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
571  * but formerly didn't normally send requests.
572  */
573 static int log_arp_wrong_iface = 1;
574 static int log_arp_movements = 1;
575 static int log_arp_permanent_modify = 1;
576 
577 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
578 	&log_arp_wrong_iface, 0,
579 	"log arp packets arriving on the wrong interface");
580 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
581         &log_arp_movements, 0,
582         "log arp replies from MACs different than the one in the cache");
583 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
584         &log_arp_permanent_modify, 0,
585         "log arp replies from MACs different than the one in the permanent arp entry");
586 
587 
588 static void
589 in_arpinput(m)
590 	struct mbuf *m;
591 {
592 	struct arphdr *ah;
593 	struct ifnet *ifp = m->m_pkthdr.rcvif;
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->if_bridge != NULL) ||
635 		    (ia->ia_ifp == ifp)) &&
636 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
637 			goto match;
638 #ifdef DEV_CARP
639 		if (ifp->if_carp != NULL &&
640 		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
641 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
642 			carp_match = 1;
643 			goto match;
644 		}
645 #endif
646 	}
647 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
648 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
649 		    (ia->ia_ifp == ifp)) &&
650 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
651 			goto match;
652 	/*
653 	 * No match, use the first inet address on the receive interface
654 	 * as a dummy address for the rest of the function.
655 	 */
656 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
657 		if (ifa->ifa_addr->sa_family == AF_INET) {
658 			ia = ifatoia(ifa);
659 			goto match;
660 		}
661 	/*
662 	 * If bridging, fall back to using any inet address.
663 	 */
664 	if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
665 		goto drop;
666 match:
667 	if (!enaddr)
668 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
669 	myaddr = ia->ia_addr.sin_addr;
670 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
671 		goto drop;	/* it's from me, ignore it. */
672 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
673 		log(LOG_ERR,
674 		    "arp: link address is broadcast for IP address %s!\n",
675 		    inet_ntoa(isaddr));
676 		goto drop;
677 	}
678 	/*
679 	 * Warn if another host is using the same IP address, but only if the
680 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
681 	 * case we suppress the warning to avoid false positive complaints of
682 	 * potential misconfiguration.
683 	 */
684 	if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
685 		log(LOG_ERR,
686 		   "arp: %*D is using my IP address %s!\n",
687 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
688 		   inet_ntoa(isaddr));
689 		itaddr = myaddr;
690 		goto reply;
691 	}
692 	if (ifp->if_flags & IFF_STATICARP)
693 		goto reply;
694 	rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
695 	if (rt != NULL) {
696 		la = (struct llinfo_arp *)rt->rt_llinfo;
697 		if (la == NULL) {
698 			RT_UNLOCK(rt);
699 			goto reply;
700 		}
701 	} else
702 		goto reply;
703 
704 	/* The following is not an error when doing bridging. */
705 	if (!bridged && rt->rt_ifp != ifp
706 #ifdef DEV_CARP
707 	    && (ifp->if_type != IFT_CARP || !carp_match)
708 #endif
709 							) {
710 		if (log_arp_wrong_iface)
711 			log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
712 			    inet_ntoa(isaddr),
713 			    rt->rt_ifp->if_xname,
714 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
715 			    ifp->if_xname);
716 		RT_UNLOCK(rt);
717 		goto reply;
718 	}
719 	sdl = SDL(rt->rt_gateway);
720 	if (sdl->sdl_alen &&
721 	    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
722 		if (rt->rt_expire) {
723 		    if (log_arp_movements)
724 		        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
725 			    inet_ntoa(isaddr),
726 			    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
727 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
728 			    ifp->if_xname);
729 		} else {
730 			RT_UNLOCK(rt);
731 			if (log_arp_permanent_modify)
732 				log(LOG_ERR, "arp: %*D attempts to modify "
733 				    "permanent entry for %s on %s\n",
734 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
735 				    inet_ntoa(isaddr), ifp->if_xname);
736 			goto reply;
737 		}
738 	}
739 	/*
740 	 * sanity check for the address length.
741 	 * XXX this does not work for protocols with variable address
742 	 * length. -is
743 	 */
744 	if (sdl->sdl_alen &&
745 	    sdl->sdl_alen != ah->ar_hln) {
746 		log(LOG_WARNING,
747 		    "arp from %*D: new addr len %d, was %d",
748 		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
749 		    ah->ar_hln, sdl->sdl_alen);
750 	}
751 	if (ifp->if_addrlen != ah->ar_hln) {
752 		log(LOG_WARNING,
753 		    "arp from %*D: addr len: new %d, i/f %d (ignored)",
754 		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
755 		    ah->ar_hln, ifp->if_addrlen);
756 		RT_UNLOCK(rt);
757 		goto reply;
758 	}
759 	(void)memcpy(LLADDR(sdl), ar_sha(ah),
760 	    sdl->sdl_alen = ah->ar_hln);
761 	/*
762 	 * If we receive an arp from a token-ring station over
763 	 * a token-ring nic then try to save the source
764 	 * routing info.
765 	 */
766 	if (ifp->if_type == IFT_ISO88025) {
767 		struct iso88025_header *th = NULL;
768 		struct iso88025_sockaddr_dl_data *trld;
769 
770 		th = (struct iso88025_header *)m->m_pkthdr.header;
771 		trld = SDL_ISO88025(sdl);
772 		rif_len = TR_RCF_RIFLEN(th->rcf);
773 		if ((th->iso88025_shost[0] & TR_RII) &&
774 		    (rif_len > 2)) {
775 			trld->trld_rcf = th->rcf;
776 			trld->trld_rcf ^= htons(TR_RCF_DIR);
777 			memcpy(trld->trld_route, th->rd, rif_len - 2);
778 			trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
779 			/*
780 			 * Set up source routing information for
781 			 * reply packet (XXX)
782 			 */
783 			m->m_data -= rif_len;
784 			m->m_len  += rif_len;
785 			m->m_pkthdr.len += rif_len;
786 		} else {
787 			th->iso88025_shost[0] &= ~TR_RII;
788 			trld->trld_rcf = 0;
789 		}
790 		m->m_data -= 8;
791 		m->m_len  += 8;
792 		m->m_pkthdr.len += 8;
793 		th->rcf = trld->trld_rcf;
794 	}
795 	if (rt->rt_expire)
796 		rt->rt_expire = time_uptime + arpt_keep;
797 	la->la_asked = 0;
798 	la->la_preempt = arp_maxtries;
799 	hold = la->la_hold;
800 	la->la_hold = NULL;
801 	RT_UNLOCK(rt);
802 	if (hold != NULL)
803 		(*ifp->if_output)(ifp, hold, rt_key(rt), rt);
804 
805 reply:
806 	if (op != ARPOP_REQUEST)
807 		goto drop;
808 	if (itaddr.s_addr == myaddr.s_addr) {
809 		/* I am the target */
810 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
811 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
812 	} else {
813 		rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
814 		if (rt == NULL) {
815 			struct sockaddr_in sin;
816 
817 			if (!arp_proxyall)
818 				goto drop;
819 
820 			bzero(&sin, sizeof sin);
821 			sin.sin_family = AF_INET;
822 			sin.sin_len = sizeof sin;
823 			sin.sin_addr = itaddr;
824 
825 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
826 			if (!rt)
827 				goto drop;
828 			/*
829 			 * Don't send proxies for nodes on the same interface
830 			 * as this one came out of, or we'll get into a fight
831 			 * over who claims what Ether address.
832 			 */
833 			if (rt->rt_ifp == ifp) {
834 				rtfree(rt);
835 				goto drop;
836 			}
837 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
838 			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
839 			rtfree(rt);
840 
841 			/*
842 			 * Also check that the node which sent the ARP packet
843 			 * is on the the interface we expect it to be on. This
844 			 * avoids ARP chaos if an interface is connected to the
845 			 * wrong network.
846 			 */
847 			sin.sin_addr = isaddr;
848 
849 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
850 			if (!rt)
851 				goto drop;
852 			if (rt->rt_ifp != ifp) {
853 				log(LOG_INFO, "arp_proxy: ignoring request"
854 				    " from %s via %s, expecting %s\n",
855 				    inet_ntoa(isaddr), ifp->if_xname,
856 				    rt->rt_ifp->if_xname);
857 				rtfree(rt);
858 				goto drop;
859 			}
860 			rtfree(rt);
861 
862 #ifdef DEBUG_PROXY
863 			printf("arp: proxying for %s\n",
864 			       inet_ntoa(itaddr));
865 #endif
866 		} else {
867 			/*
868 			 * Return proxied ARP replies only on the interface
869 			 * or bridge cluster where this network resides.
870 			 * Otherwise we may conflict with the host we are
871 			 * proxying for.
872 			 */
873 			if (rt->rt_ifp != ifp &&
874 			    (rt->rt_ifp->if_bridge != ifp->if_bridge ||
875 			    ifp->if_bridge == NULL)) {
876 				RT_UNLOCK(rt);
877 				goto drop;
878 			}
879 			sdl = SDL(rt->rt_gateway);
880 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
881 			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
882 			RT_UNLOCK(rt);
883 		}
884 	}
885 
886 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
887 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
888 	ah->ar_op = htons(ARPOP_REPLY);
889 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
890 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
891 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
892 	m->m_pkthdr.len = m->m_len;
893 	sa.sa_family = AF_ARP;
894 	sa.sa_len = 2;
895 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
896 	return;
897 
898 drop:
899 	m_freem(m);
900 }
901 #endif
902 
903 /*
904  * Lookup or enter a new address in arptab.
905  */
906 static struct rtentry *
907 arplookup(addr, create, proxy)
908 	u_long addr;
909 	int create, proxy;
910 {
911 	struct rtentry *rt;
912 	struct sockaddr_inarp sin;
913 	const char *why = 0;
914 
915 	bzero(&sin, sizeof(sin));
916 	sin.sin_len = sizeof(sin);
917 	sin.sin_family = AF_INET;
918 	sin.sin_addr.s_addr = addr;
919 	if (proxy)
920 		sin.sin_other = SIN_PROXY;
921 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
922 	if (rt == 0)
923 		return (0);
924 
925 	if (rt->rt_flags & RTF_GATEWAY)
926 		why = "host is not on local network";
927 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
928 		why = "could not allocate llinfo";
929 	else if (rt->rt_gateway->sa_family != AF_LINK)
930 		why = "gateway route is not ours";
931 
932 	if (why) {
933 #define	ISDYNCLONE(_rt) \
934 	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
935 		if (create)
936 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
937 			    inet_ntoa(sin.sin_addr), why);
938 		/*
939 		 * If there are no references to this Layer 2 route,
940 		 * and it is a cloned route, and not static, and
941 		 * arplookup() is creating the route, then purge
942 		 * it from the routing table as it is probably bogus.
943 		 */
944 		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
945 			rtexpunge(rt);
946 		RTFREE_LOCKED(rt);
947 		return (0);
948 #undef ISDYNCLONE
949 	} else {
950 		RT_REMREF(rt);
951 		return (rt);
952 	}
953 }
954 
955 void
956 arp_ifinit(ifp, ifa)
957 	struct ifnet *ifp;
958 	struct ifaddr *ifa;
959 {
960 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
961 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
962 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
963 	ifa->ifa_rtrequest = arp_rtrequest;
964 	ifa->ifa_flags |= RTF_CLONING;
965 }
966 
967 void
968 arp_ifinit2(ifp, ifa, enaddr)
969 	struct ifnet *ifp;
970 	struct ifaddr *ifa;
971 	u_char *enaddr;
972 {
973 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
974 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
975 				&IA_SIN(ifa)->sin_addr, enaddr);
976 	ifa->ifa_rtrequest = arp_rtrequest;
977 	ifa->ifa_flags |= RTF_CLONING;
978 }
979 
980 static void
981 arp_init(void)
982 {
983 
984 	arpintrq.ifq_maxlen = 50;
985 	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
986 	LIST_INIT(&llinfo_arp);
987 	callout_init(&arp_callout, CALLOUT_MPSAFE);
988 	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
989 	callout_reset(&arp_callout, hz, arptimer, NULL);
990 }
991 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
992