xref: /dragonfly/sys/netinet/if_ether.c (revision 8a0bcd56)
1 /*
2  * Copyright (c) 2004, 2005 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Jeffrey M. Hsu.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of The DragonFly Project nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific, prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1988, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
66  * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
67  * $DragonFly: src/sys/netinet/if_ether.c,v 1.59 2008/11/22 11:03:35 sephe Exp $
68  */
69 
70 /*
71  * Ethernet address resolution protocol.
72  * TODO:
73  *	add "inuse/lock" bit (or ref. count) along with valid bit
74  */
75 
76 #include "opt_inet.h"
77 #include "opt_carp.h"
78 
79 #include <sys/param.h>
80 #include <sys/kernel.h>
81 #include <sys/queue.h>
82 #include <sys/sysctl.h>
83 #include <sys/systm.h>
84 #include <sys/mbuf.h>
85 #include <sys/malloc.h>
86 #include <sys/socket.h>
87 #include <sys/syslog.h>
88 #include <sys/lock.h>
89 
90 #include <net/if.h>
91 #include <net/if_dl.h>
92 #include <net/if_types.h>
93 #include <net/route.h>
94 #include <net/netisr.h>
95 #include <net/if_llc.h>
96 
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #include <netinet/if_ether.h>
100 
101 #include <sys/thread2.h>
102 #include <sys/msgport2.h>
103 #include <net/netmsg2.h>
104 #include <sys/mplock2.h>
105 
106 #ifdef CARP
107 #include <netinet/ip_carp.h>
108 #endif
109 
110 #define SIN(s) ((struct sockaddr_in *)s)
111 #define SDL(s) ((struct sockaddr_dl *)s)
112 
113 SYSCTL_DECL(_net_link_ether);
114 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
115 
116 /* timer values */
117 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
118 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
119 static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
120 
121 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
122 	   &arpt_prune, 0, "");
123 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
124 	   &arpt_keep, 0, "");
125 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
126 	   &arpt_down, 0, "");
127 
128 #define	rt_expire	rt_rmx.rmx_expire
129 
130 struct llinfo_arp {
131 	LIST_ENTRY(llinfo_arp) la_le;
132 	struct	rtentry *la_rt;
133 	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
134 	struct	lwkt_port *la_msgport; /* last packet's msgport */
135 	u_short	la_preempt;	/* countdown for pre-expiry arps */
136 	u_short	la_asked;	/* #times we QUERIED following expiration */
137 };
138 
139 static	LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
140 
141 static int	arp_maxtries = 5;
142 static int	useloopback = 1; /* use loopback interface for local traffic */
143 static int	arp_proxyall = 0;
144 
145 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
146 	   &arp_maxtries, 0, "ARP resolution attempts before returning error");
147 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
148 	   &useloopback, 0, "Use the loopback interface for local traffic");
149 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
150 	   &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
151 
152 static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
153 static void	arprequest(struct ifnet *, const struct in_addr *,
154 			   const struct in_addr *, const u_char *);
155 static void	arprequest_async(struct ifnet *, const struct in_addr *,
156 				 const struct in_addr *, const u_char *);
157 static void	arpintr(netmsg_t msg);
158 static void	arptfree(struct llinfo_arp *);
159 static void	arptimer(void *);
160 static struct llinfo_arp *
161 		arplookup(in_addr_t, boolean_t, boolean_t, boolean_t);
162 #ifdef INET
163 static void	in_arpinput(struct mbuf *);
164 #endif
165 
166 static struct callout	arptimer_ch[MAXCPU];
167 
168 /*
169  * Timeout routine.  Age arp_tab entries periodically.
170  */
171 /* ARGSUSED */
172 static void
173 arptimer(void *ignored_arg)
174 {
175 	struct llinfo_arp *la, *nla;
176 
177 	crit_enter();
178 	LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
179 		if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
180 			arptfree(la);
181 	}
182 	callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
183 	crit_exit();
184 }
185 
186 /*
187  * Parallel to llc_rtrequest.
188  */
189 static void
190 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
191 {
192 	struct sockaddr *gate = rt->rt_gateway;
193 	struct llinfo_arp *la = rt->rt_llinfo;
194 
195 	struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
196 	static boolean_t arpinit_done[MAXCPU];
197 
198 	if (!arpinit_done[mycpuid]) {
199 		arpinit_done[mycpuid] = TRUE;
200 		callout_init(&arptimer_ch[mycpuid]);
201 		callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
202 	}
203 	if (rt->rt_flags & RTF_GATEWAY)
204 		return;
205 
206 	switch (req) {
207 	case RTM_ADD:
208 		/*
209 		 * XXX: If this is a manually added route to interface
210 		 * such as older version of routed or gated might provide,
211 		 * restore cloning bit.
212 		 */
213 		if (!(rt->rt_flags & RTF_HOST) &&
214 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
215 			rt->rt_flags |= RTF_CLONING;
216 		if (rt->rt_flags & RTF_CLONING) {
217 			/*
218 			 * Case 1: This route should come from a route to iface.
219 			 */
220 			rt_setgate(rt, rt_key(rt),
221 				   (struct sockaddr *)&null_sdl,
222 				   RTL_DONTREPORT);
223 			gate = rt->rt_gateway;
224 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
225 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
226 			rt->rt_expire = time_second;
227 			break;
228 		}
229 		/* Announce a new entry if requested. */
230 		if (rt->rt_flags & RTF_ANNOUNCE) {
231 			arprequest_async(rt->rt_ifp,
232 			    &SIN(rt_key(rt))->sin_addr,
233 			    &SIN(rt_key(rt))->sin_addr,
234 			    LLADDR(SDL(gate)));
235 		}
236 		/*FALLTHROUGH*/
237 	case RTM_RESOLVE:
238 		if (gate->sa_family != AF_LINK ||
239 		    gate->sa_len < sizeof(struct sockaddr_dl)) {
240 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
241 			break;
242 		}
243 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
244 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
245 		if (la != NULL)
246 			break; /* This happens on a route change */
247 		/*
248 		 * Case 2:  This route may come from cloning, or a manual route
249 		 * add with a LL address.
250 		 */
251 		R_Malloc(la, struct llinfo_arp *, sizeof *la);
252 		rt->rt_llinfo = la;
253 		if (la == NULL) {
254 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
255 			break;
256 		}
257 		bzero(la, sizeof *la);
258 		la->la_rt = rt;
259 		rt->rt_flags |= RTF_LLINFO;
260 		LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
261 
262 #ifdef INET
263 		/*
264 		 * This keeps the multicast addresses from showing up
265 		 * in `arp -a' listings as unresolved.  It's not actually
266 		 * functional.  Then the same for broadcast.
267 		 */
268 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
269 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
270 					       LLADDR(SDL(gate)));
271 			SDL(gate)->sdl_alen = 6;
272 			rt->rt_expire = 0;
273 		}
274 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
275 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
276 			       rt->rt_ifp->if_addrlen);
277 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
278 			rt->rt_expire = 0;
279 		}
280 #endif
281 
282 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
283 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
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
289 			 * loopback down.  However, it causes problems
290 			 * during network configuration for boards
291 			 * that can't receive packets they send.  It
292 			 * is now necessary to clear "useloopback" and
293 			 * remove the route to force traffic out to
294 			 * the hardware.
295 			 */
296 			rt->rt_expire = 0;
297 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
298 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
299 			if (useloopback)
300 				rt->rt_ifp = loif;
301 		}
302 		break;
303 
304 	case RTM_DELETE:
305 		if (la == NULL)
306 			break;
307 		LIST_REMOVE(la, la_le);
308 		rt->rt_llinfo = NULL;
309 		rt->rt_flags &= ~RTF_LLINFO;
310 		if (la->la_hold != NULL)
311 			m_freem(la->la_hold);
312 		Free(la);
313 		break;
314 	}
315 }
316 
317 static struct mbuf *
318 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
319 	     const struct in_addr *tip, const u_char *enaddr)
320 {
321 	struct mbuf *m;
322 	struct arphdr *ah;
323 	u_short ar_hrd;
324 
325 	if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
326 		return NULL;
327 	m->m_pkthdr.rcvif = NULL;
328 
329 	switch (ifp->if_type) {
330 	case IFT_ETHER:
331 		/*
332 		 * This may not be correct for types not explicitly
333 		 * listed, but this is our best guess
334 		 */
335 	default:
336 		ar_hrd = htons(ARPHRD_ETHER);
337 
338 		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
339 		m->m_pkthdr.len = m->m_len;
340 		MH_ALIGN(m, m->m_len);
341 
342 		ah = mtod(m, struct arphdr *);
343 		break;
344 	}
345 
346 	ah->ar_hrd = ar_hrd;
347 	ah->ar_pro = htons(ETHERTYPE_IP);
348 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
349 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
350 	ah->ar_op = htons(ARPOP_REQUEST);
351 	memcpy(ar_sha(ah), enaddr, ah->ar_hln);
352 	memset(ar_tha(ah), 0, ah->ar_hln);
353 	memcpy(ar_spa(ah), sip, ah->ar_pln);
354 	memcpy(ar_tpa(ah), tip, ah->ar_pln);
355 
356 	return m;
357 }
358 
359 static void
360 arpreq_send(struct ifnet *ifp, struct mbuf *m)
361 {
362 	struct sockaddr sa;
363 	struct ether_header *eh;
364 
365 	switch (ifp->if_type) {
366 	case IFT_ETHER:
367 		/*
368 		 * This may not be correct for types not explicitly
369 		 * listed, but this is our best guess
370 		 */
371 	default:
372 		eh = (struct ether_header *)sa.sa_data;
373 		/* if_output() will not swap */
374 		eh->ether_type = htons(ETHERTYPE_ARP);
375 		memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
376 		break;
377 	}
378 
379 	sa.sa_family = AF_UNSPEC;
380 	sa.sa_len = sizeof(sa);
381 	ifp->if_output(ifp, m, &sa, NULL);
382 }
383 
384 static void
385 arpreq_send_handler(netmsg_t msg)
386 {
387 	struct mbuf *m = msg->packet.nm_packet;
388 	struct ifnet *ifp = msg->lmsg.u.ms_resultp;
389 
390 	arpreq_send(ifp, m);
391 	/* nmsg was embedded in the mbuf, do not reply! */
392 }
393 
394 /*
395  * Broadcast an ARP request. Caller specifies:
396  *	- arp header source ip address
397  *	- arp header target ip address
398  *	- arp header source ethernet address
399  *
400  * NOTE: Caller MUST NOT hold ifp's serializer
401  */
402 static void
403 arprequest(struct ifnet *ifp, const struct in_addr *sip,
404 	   const struct in_addr *tip, const u_char *enaddr)
405 {
406 	struct mbuf *m;
407 
408 	m = arpreq_alloc(ifp, sip, tip, enaddr);
409 	if (m == NULL)
410 		return;
411 	arpreq_send(ifp, m);
412 }
413 
414 /*
415  * Same as arprequest(), except:
416  * - Caller is allowed to hold ifp's serializer
417  * - Network output is done in protocol thead
418  */
419 static void
420 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
421 		 const struct in_addr *tip, const u_char *enaddr)
422 {
423 	struct mbuf *m;
424 	struct netmsg_packet *pmsg;
425 
426 	m = arpreq_alloc(ifp, sip, tip, enaddr);
427 	if (m == NULL)
428 		return;
429 
430 	pmsg = &m->m_hdr.mh_netmsg;
431 	netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
432 		    0, arpreq_send_handler);
433 	pmsg->nm_packet = m;
434 	pmsg->base.lmsg.u.ms_resultp = ifp;
435 
436 	lwkt_sendmsg(cpu_portfn(mycpuid), &pmsg->base.lmsg);
437 }
438 
439 /*
440  * Resolve an IP address into an ethernet address.  If success,
441  * desten is filled in.  If there is no entry in arptab,
442  * set one up and broadcast a request for the IP address.
443  * Hold onto this mbuf and resend it once the address
444  * is finally resolved.  A return value of 1 indicates
445  * that desten has been filled in and the packet should be sent
446  * normally; a 0 return indicates that the packet has been
447  * taken over here, either now or for later transmission.
448  */
449 int
450 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
451 	   struct sockaddr *dst, u_char *desten)
452 {
453 	struct rtentry *rt;
454 	struct llinfo_arp *la = NULL;
455 	struct sockaddr_dl *sdl;
456 
457 	if (m->m_flags & M_BCAST) {	/* broadcast */
458 		memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
459 		return (1);
460 	}
461 	if (m->m_flags & M_MCAST) {/* multicast */
462 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
463 		return (1);
464 	}
465 	if (rt0 != NULL) {
466 		if (rt_llroute(dst, rt0, &rt) != 0) {
467 			m_freem(m);
468 			return 0;
469 		}
470 		la = rt->rt_llinfo;
471 	}
472 	if (la == NULL) {
473 		la = arplookup(SIN(dst)->sin_addr.s_addr,
474 			       TRUE, RTL_REPORTMSG, FALSE);
475 		if (la != NULL)
476 			rt = la->la_rt;
477 	}
478 	if (la == NULL || rt == NULL) {
479 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
480 		    inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
481 		    rt ? "rt" : "");
482 		m_freem(m);
483 		return (0);
484 	}
485 	sdl = SDL(rt->rt_gateway);
486 	/*
487 	 * Check the address family and length is valid, the address
488 	 * is resolved; otherwise, try to resolve.
489 	 */
490 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
491 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
492 		/*
493 		 * If entry has an expiry time and it is approaching,
494 		 * see if we need to send an ARP request within this
495 		 * arpt_down interval.
496 		 */
497 		if ((rt->rt_expire != 0) &&
498 		    (time_second + la->la_preempt > rt->rt_expire)) {
499 			arprequest(ifp,
500 				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
501 				   &SIN(dst)->sin_addr,
502 				   IF_LLADDR(ifp));
503 			la->la_preempt--;
504 		}
505 
506 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
507 		return 1;
508 	}
509 	/*
510 	 * If ARP is disabled or static on this interface, stop.
511 	 * XXX
512 	 * Probably should not allocate empty llinfo struct if we are
513 	 * not going to be sending out an arp request.
514 	 */
515 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
516 		m_freem(m);
517 		return (0);
518 	}
519 	/*
520 	 * There is an arptab entry, but no ethernet address
521 	 * response yet.  Replace the held mbuf with this
522 	 * latest one.
523 	 */
524 	if (la->la_hold != NULL)
525 		m_freem(la->la_hold);
526 	la->la_hold = m;
527 	la->la_msgport = cur_netport();
528 	if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
529 		rt->rt_flags &= ~RTF_REJECT;
530 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
531 			rt->rt_expire = time_second;
532 			if (la->la_asked++ < arp_maxtries) {
533 				arprequest(ifp,
534 					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
535 					   &SIN(dst)->sin_addr,
536 					   IF_LLADDR(ifp));
537 			} else {
538 				rt->rt_flags |= RTF_REJECT;
539 				rt->rt_expire += arpt_down;
540 				la->la_asked = 0;
541 				la->la_preempt = arp_maxtries;
542 			}
543 		}
544 	}
545 	return (0);
546 }
547 
548 /*
549  * Common length and type checks are done here,
550  * then the protocol-specific routine is called.
551  */
552 static void
553 arpintr(netmsg_t msg)
554 {
555 	struct mbuf *m = msg->packet.nm_packet;
556 	struct arphdr *ar;
557 	u_short ar_hrd;
558 
559 	if (m->m_len < sizeof(struct arphdr) &&
560 	    (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
561 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
562 		return;
563 	}
564 	ar = mtod(m, struct arphdr *);
565 
566 	ar_hrd = ntohs(ar->ar_hrd);
567 	if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
568 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
569 		    (unsigned char *)&ar->ar_hrd, "");
570 		m_freem(m);
571 		return;
572 	}
573 
574 	if (m->m_pkthdr.len < arphdr_len(ar)) {
575 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
576 			log(LOG_ERR, "arp: runt packet\n");
577 			return;
578 		}
579 		ar = mtod(m, struct arphdr *);
580 	}
581 
582 	switch (ntohs(ar->ar_pro)) {
583 #ifdef INET
584 	case ETHERTYPE_IP:
585 		in_arpinput(m);
586 		return;
587 #endif
588 	}
589 	m_freem(m);
590 	/* msg was embedded in the mbuf, do not reply! */
591 }
592 
593 #ifdef INET
594 /*
595  * ARP for Internet protocols on 10 Mb/s Ethernet.
596  * Algorithm is that given in RFC 826.
597  * In addition, a sanity check is performed on the sender
598  * protocol address, to catch impersonators.
599  * We no longer handle negotiations for use of trailer protocol:
600  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
601  * along with IP replies if we wanted trailers sent to us,
602  * and also sent them in response to IP replies.
603  * This allowed either end to announce the desire to receive
604  * trailer packets.
605  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
606  * but formerly didn't normally send requests.
607  */
608 
609 static int	log_arp_wrong_iface = 1;
610 static int	log_arp_movements = 1;
611 static int	log_arp_permanent_modify = 1;
612 
613 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
614 	   &log_arp_wrong_iface, 0,
615 	   "Log arp packets arriving on the wrong interface");
616 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
617 	   &log_arp_movements, 0,
618 	   "Log arp replies from MACs different than the one in the cache");
619 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
620 	   &log_arp_permanent_modify, 0,
621 	   "Log arp replies from MACs different than the one "
622 	   "in the permanent arp entry");
623 
624 
625 static void
626 arp_hold_output(netmsg_t msg)
627 {
628 	struct mbuf *m = msg->packet.nm_packet;
629 	struct rtentry *rt;
630 	struct ifnet *ifp;
631 
632 	rt = msg->lmsg.u.ms_resultp;
633 	ifp = m->m_pkthdr.rcvif;
634 	m->m_pkthdr.rcvif = NULL;
635 
636 	ifp->if_output(ifp, m, rt_key(rt), rt);
637 
638 	/* Drop the reference count bumped by the sender */
639 	RTFREE(rt);
640 
641 	/* nmsg was embedded in the mbuf, do not reply! */
642 }
643 
644 static void
645 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
646 		 boolean_t generate_report, boolean_t dologging)
647 {
648 	struct arphdr *ah = mtod(m, struct arphdr *);
649 	struct ifnet *ifp = m->m_pkthdr.rcvif;
650 	struct llinfo_arp *la;
651 	struct sockaddr_dl *sdl;
652 	struct rtentry *rt;
653 
654 	la = arplookup(saddr, create, generate_report, FALSE);
655 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
656 		struct in_addr isaddr = { saddr };
657 
658 		/* the following is not an error when doing bridging */
659 		if (rt->rt_ifp != ifp) {
660 			if (dologging && log_arp_wrong_iface) {
661 				log(LOG_ERR,
662 				    "arp: %s is on %s "
663 				    "but got reply from %*D on %s\n",
664 				    inet_ntoa(isaddr),
665 				    rt->rt_ifp->if_xname,
666 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
667 				    ifp->if_xname);
668 			}
669 			return;
670 		}
671 		if (sdl->sdl_alen &&
672 		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
673 			if (rt->rt_expire != 0) {
674 				if (dologging && log_arp_movements) {
675 			    		log(LOG_INFO,
676 			    		"arp: %s moved from %*D to %*D on %s\n",
677 			    		inet_ntoa(isaddr),
678 			    		ifp->if_addrlen, (u_char *)LLADDR(sdl),
679 			    		":", ifp->if_addrlen,
680 			    		(u_char *)ar_sha(ah), ":",
681 			    		ifp->if_xname);
682 				}
683 			} else {
684 				if (dologging && log_arp_permanent_modify) {
685 					log(LOG_ERR,
686 					"arp: %*D attempts to modify "
687 					"permanent entry for %s on %s\n",
688 					ifp->if_addrlen, (u_char *)ar_sha(ah),
689 					":", inet_ntoa(isaddr), ifp->if_xname);
690 				}
691 				return;
692 			}
693 		}
694 		/*
695 		 * sanity check for the address length.
696 		 * XXX this does not work for protocols with variable address
697 		 * length. -is
698 		 */
699 		if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
700 			log(LOG_WARNING,
701 			    "arp from %*D: new addr len %d, was %d",
702 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
703 			    ah->ar_hln, sdl->sdl_alen);
704 		}
705 		if (ifp->if_addrlen != ah->ar_hln) {
706 			if (dologging) {
707 				log(LOG_WARNING,
708 				"arp from %*D: addr len: new %d, i/f %d "
709 				"(ignored)",
710 				ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
711 				ah->ar_hln, ifp->if_addrlen);
712 			}
713 			return;
714 		}
715 		memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
716 		if (rt->rt_expire != 0)
717 			rt->rt_expire = time_second + arpt_keep;
718 		rt->rt_flags &= ~RTF_REJECT;
719 		la->la_asked = 0;
720 		la->la_preempt = arp_maxtries;
721 
722 		/*
723 		 * This particular cpu might have been holding an mbuf
724 		 * pending ARP resolution.  If so, transmit the mbuf now.
725 		 */
726 		if (la->la_hold != NULL) {
727 			struct mbuf *m = la->la_hold;
728 			struct lwkt_port *port = la->la_msgport;
729 			struct netmsg_packet *pmsg;
730 
731 			la->la_hold = NULL;
732 			la->la_msgport = NULL;
733 
734 			m_adj(m, sizeof(struct ether_header));
735 
736 			/*
737 			 * Make sure that this rtentry will not be freed
738 			 * before the packet is processed on the target
739 			 * msgport.  The reference count will be dropped
740 			 * in the handler associated with this packet.
741 			 */
742 			rt->rt_refcnt++;
743 
744 			pmsg = &m->m_hdr.mh_netmsg;
745 			netmsg_init(&pmsg->base, NULL,
746 				    &netisr_apanic_rport,
747 				    MSGF_PRIORITY, arp_hold_output);
748 			pmsg->nm_packet = m;
749 
750 			/* Record necessary information */
751 			m->m_pkthdr.rcvif = ifp;
752 			pmsg->base.lmsg.u.ms_resultp = rt;
753 
754 			lwkt_sendmsg(port, &pmsg->base.lmsg);
755 		}
756 	}
757 }
758 
759 #ifdef SMP
760 
761 struct netmsg_arp_update {
762 	struct netmsg_base base;
763 	struct mbuf	*m;
764 	in_addr_t	saddr;
765 	boolean_t	create;
766 };
767 
768 static void arp_update_msghandler(netmsg_t msg);
769 
770 #endif
771 
772 /*
773  * Called from arpintr() - this routine is run from a single cpu.
774  */
775 static void
776 in_arpinput(struct mbuf *m)
777 {
778 	struct arphdr *ah;
779 	struct ifnet *ifp = m->m_pkthdr.rcvif;
780 	struct ether_header *eh;
781 	struct rtentry *rt;
782 	struct ifaddr_container *ifac;
783 	struct in_ifaddr_container *iac;
784 	struct in_ifaddr *ia = NULL;
785 	struct sockaddr sa;
786 	struct in_addr isaddr, itaddr, myaddr;
787 #ifdef SMP
788 	struct netmsg_arp_update msg;
789 #endif
790 	uint8_t *enaddr = NULL;
791 	int op;
792 	int req_len;
793 
794 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
795 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
796 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
797 		return;
798 	}
799 
800 	ah = mtod(m, struct arphdr *);
801 	op = ntohs(ah->ar_op);
802 	memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
803 	memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
804 
805 	myaddr.s_addr = INADDR_ANY;
806 #ifdef CARP
807 	if (ifp->if_carp != NULL) {
808 		get_mplock();
809 		if (ifp->if_carp != NULL &&
810 		    carp_iamatch(ifp->if_carp, &itaddr, &isaddr, &enaddr)) {
811 			rel_mplock();
812 			myaddr = itaddr;
813 			goto match;
814 		}
815 		rel_mplock();
816 	}
817 #endif
818 
819 	/*
820 	 * Check both target and sender IP addresses:
821 	 *
822 	 * If we receive the packet on the interface owning the address,
823 	 * then accept the address.
824 	 *
825 	 * For a bridge, we accept the address if the receive interface and
826 	 * the interface owning the address are on the same bridge.
827 	 * (This will change slightly when we have clusters of interfaces).
828 	 */
829 	LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
830 		ia = iac->ia;
831 
832 		/* Skip all ia's which don't match */
833 		if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
834 			continue;
835 #ifdef CARP
836 		if (ia->ia_ifp->if_type == IFT_CARP)
837 			continue;
838 #endif
839 		if (ia->ia_ifp == ifp)
840 			goto match;
841 
842 		if (ifp->if_bridge && ia->ia_ifp &&
843 		    ifp->if_bridge == ia->ia_ifp->if_bridge)
844 			goto match;
845 	}
846 	LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
847 		ia = iac->ia;
848 
849 		/* Skip all ia's which don't match */
850 		if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
851 			continue;
852 #ifdef CARP
853 		if (ia->ia_ifp->if_type == IFT_CARP)
854 			continue;
855 #endif
856 		if (ia->ia_ifp == ifp)
857 			goto match;
858 
859 		if (ifp->if_bridge && ia->ia_ifp &&
860 		    ifp->if_bridge == ia->ia_ifp->if_bridge)
861 			goto match;
862 	}
863 	/*
864 	 * No match, use the first inet address on the receive interface
865 	 * as a dummy address for the rest of the function.
866 	 */
867 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
868 		struct ifaddr *ifa = ifac->ifa;
869 
870 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
871 			ia = ifatoia(ifa);
872 			goto match;
873 		}
874 	}
875 	/*
876 	 * If we got here, we didn't find any suitable interface,
877 	 * so drop the packet.
878 	 */
879 	m_freem(m);
880 	return;
881 
882 match:
883 	if (!enaddr)
884 		enaddr = (uint8_t *)IF_LLADDR(ifp);
885 	if (myaddr.s_addr == INADDR_ANY)
886 		myaddr = ia->ia_addr.sin_addr;
887 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
888 		m_freem(m);	/* it's from me, ignore it. */
889 		return;
890 	}
891 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
892 		log(LOG_ERR,
893 		    "arp: link address is broadcast for IP address %s!\n",
894 		    inet_ntoa(isaddr));
895 		m_freem(m);
896 		return;
897 	}
898 	if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
899 		log(LOG_ERR,
900 		   "arp: %*D is using my IP address %s!\n",
901 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
902 		   inet_ntoa(isaddr));
903 		itaddr = myaddr;
904 		goto reply;
905 	}
906 	if (ifp->if_flags & IFF_STATICARP)
907 		goto reply;
908 #ifdef SMP
909 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
910 		    0, arp_update_msghandler);
911 	msg.m = m;
912 	msg.saddr = isaddr.s_addr;
913 	msg.create = (itaddr.s_addr == myaddr.s_addr);
914 	lwkt_domsg(rtable_portfn(0), &msg.base.lmsg, 0);
915 #else
916 	arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
917 			 RTL_REPORTMSG, TRUE);
918 #endif
919 reply:
920 	if (op != ARPOP_REQUEST) {
921 		m_freem(m);
922 		return;
923 	}
924 	if (itaddr.s_addr == myaddr.s_addr) {
925 		/* I am the target */
926 		memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
927 		memcpy(ar_sha(ah), enaddr, ah->ar_hln);
928 	} else {
929 		struct llinfo_arp *la;
930 
931 		la = arplookup(itaddr.s_addr, FALSE, RTL_DONTREPORT, SIN_PROXY);
932 		if (la == NULL) {
933 			struct sockaddr_in sin;
934 
935 			if (!arp_proxyall) {
936 				m_freem(m);
937 				return;
938 			}
939 
940 			bzero(&sin, sizeof sin);
941 			sin.sin_family = AF_INET;
942 			sin.sin_len = sizeof sin;
943 			sin.sin_addr = itaddr;
944 
945 			rt = rtpurelookup((struct sockaddr *)&sin);
946 			if (rt == NULL) {
947 				m_freem(m);
948 				return;
949 			}
950 			--rt->rt_refcnt;
951 			/*
952 			 * Don't send proxies for nodes on the same interface
953 			 * as this one came out of, or we'll get into a fight
954 			 * over who claims what Ether address.
955 			 */
956 			if (rt->rt_ifp == ifp) {
957 				m_freem(m);
958 				return;
959 			}
960 			memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
961 			memcpy(ar_sha(ah), enaddr, ah->ar_hln);
962 #ifdef DEBUG_PROXY
963 			kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
964 #endif
965 		} else {
966 			struct sockaddr_dl *sdl;
967 
968 			rt = la->la_rt;
969 			memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
970 			sdl = SDL(rt->rt_gateway);
971 			memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
972 		}
973 	}
974 
975 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
976 	memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
977 	ah->ar_op = htons(ARPOP_REPLY);
978 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
979 	switch (ifp->if_type) {
980 	case IFT_ETHER:
981 	/*
982 	 * May not be correct for types not explictly
983 	 * listed, but it is our best guess.
984 	 */
985 	default:
986 		eh = (struct ether_header *)sa.sa_data;
987 		memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
988 		eh->ether_type = htons(ETHERTYPE_ARP);
989 		break;
990 	}
991 	sa.sa_family = AF_UNSPEC;
992 	sa.sa_len = sizeof sa;
993 	ifp->if_output(ifp, m, &sa, NULL);
994 }
995 
996 #ifdef SMP
997 
998 static void
999 arp_update_msghandler(netmsg_t msg)
1000 {
1001 	struct netmsg_arp_update *rmsg = (struct netmsg_arp_update *)msg;
1002 	int nextcpu;
1003 
1004 	/*
1005 	 * This message handler will be called on all of the CPUs,
1006 	 * however, we only need to generate rtmsg on CPU0.
1007 	 */
1008 	arp_update_oncpu(rmsg->m, rmsg->saddr, rmsg->create,
1009 			 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT,
1010 			 mycpuid == 0);
1011 
1012 	nextcpu = mycpuid + 1;
1013 	if (nextcpu < ncpus)
1014 		lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg);
1015 	else
1016 		lwkt_replymsg(&rmsg->base.lmsg, 0);
1017 }
1018 
1019 #endif	/* SMP */
1020 
1021 #endif	/* INET */
1022 
1023 /*
1024  * Free an arp entry.  If the arp entry is actively referenced or represents
1025  * a static entry we only clear it back to an unresolved state, otherwise
1026  * we destroy the entry entirely.
1027  *
1028  * Note that static entries are created when route add ... -interface is used
1029  * to create an interface route to a (direct) destination.
1030  */
1031 static void
1032 arptfree(struct llinfo_arp *la)
1033 {
1034 	struct rtentry *rt = la->la_rt;
1035 	struct sockaddr_dl *sdl;
1036 
1037 	if (rt == NULL)
1038 		panic("arptfree");
1039 	sdl = SDL(rt->rt_gateway);
1040 	if (sdl != NULL &&
1041 	    ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1042 	     (rt->rt_flags & RTF_STATIC))) {
1043 		sdl->sdl_alen = 0;
1044 		la->la_preempt = la->la_asked = 0;
1045 		rt->rt_flags &= ~RTF_REJECT;
1046 		return;
1047 	}
1048 	rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1049 }
1050 
1051 /*
1052  * Lookup or enter a new address in arptab.
1053  */
1054 static struct llinfo_arp *
1055 arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report,
1056 	  boolean_t proxy)
1057 {
1058 	struct rtentry *rt;
1059 	struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1060 	const char *why = NULL;
1061 
1062 	sin.sin_addr.s_addr = addr;
1063 	sin.sin_other = proxy ? SIN_PROXY : 0;
1064 	if (create) {
1065 		rt = _rtlookup((struct sockaddr *)&sin,
1066 			       generate_report, RTL_DOCLONE);
1067 	} else {
1068 		rt = rtpurelookup((struct sockaddr *)&sin);
1069 	}
1070 	if (rt == NULL)
1071 		return (NULL);
1072 	rt->rt_refcnt--;
1073 
1074 	if (rt->rt_flags & RTF_GATEWAY)
1075 		why = "host is not on local network";
1076 	else if (!(rt->rt_flags & RTF_LLINFO))
1077 		why = "could not allocate llinfo";
1078 	else if (rt->rt_gateway->sa_family != AF_LINK)
1079 		why = "gateway route is not ours";
1080 
1081 	if (why) {
1082 		if (create) {
1083 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
1084 			    inet_ntoa(sin.sin_addr), why);
1085 		}
1086 		if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1087 			/* No references to this route.  Purge it. */
1088 			rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1089 				  rt_mask(rt), rt->rt_flags, NULL);
1090 		}
1091 		return (NULL);
1092 	}
1093 	return (rt->rt_llinfo);
1094 }
1095 
1096 void
1097 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1098 {
1099 	if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1100 		arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1101 				 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
1102 	}
1103 	ifa->ifa_rtrequest = arp_rtrequest;
1104 	ifa->ifa_flags |= RTF_CLONING;
1105 }
1106 
1107 void
1108 arp_iainit(struct ifnet *ifp, const struct in_addr *addr, const u_char *enaddr)
1109 {
1110 	if (addr->s_addr != INADDR_ANY)
1111 		arprequest_async(ifp, addr, addr, enaddr);
1112 }
1113 
1114 static void
1115 arp_init(void)
1116 {
1117 	int cpu;
1118 
1119 	for (cpu = 0; cpu < ncpus2; cpu++)
1120 		LIST_INIT(&llinfo_arp_list[cpu]);
1121 
1122 	netisr_register(NETISR_ARP, arpintr, NULL);
1123 }
1124 
1125 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
1126