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