xref: /dflybsd-src/sys/netinet/if_ether.c (revision 770899e9c633f5aeaebae171f27676258a386117)
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.39 2007/08/16 20:03:57 dillon 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 
89 #include <net/if.h>
90 #include <net/if_dl.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93 #include <net/netisr.h>
94 #include <net/if_llc.h>
95 
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/if_ether.h>
99 
100 #include <net/if_arc.h>
101 #include <net/iso88025.h>
102 
103 #include <sys/thread2.h>
104 #include <sys/msgport2.h>
105 #include <net/netmsg2.h>
106 
107 
108 #ifdef CARP
109 #include <netinet/ip_carp.h>
110 #endif
111 
112 #define SIN(s) ((struct sockaddr_in *)s)
113 #define SDL(s) ((struct sockaddr_dl *)s)
114 
115 SYSCTL_DECL(_net_link_ether);
116 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
117 
118 /* timer values */
119 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
120 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
121 static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
122 
123 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
124 	   &arpt_prune, 0, "");
125 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
126 	   &arpt_keep, 0, "");
127 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
128 	   &arpt_down, 0, "");
129 
130 #define	rt_expire	rt_rmx.rmx_expire
131 
132 struct llinfo_arp {
133 	LIST_ENTRY(llinfo_arp) la_le;
134 	struct	rtentry *la_rt;
135 	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
136 	u_short	la_preempt;	/* countdown for pre-expiry arps */
137 	u_short	la_asked;	/* #times we QUERIED following expiration */
138 };
139 
140 static	LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
141 
142 static int	arp_maxtries = 5;
143 static int	useloopback = 1; /* use loopback interface for local traffic */
144 static int	arp_proxyall = 0;
145 
146 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
147 	   &arp_maxtries, 0, "");
148 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
149 	   &useloopback, 0, "");
150 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
151 	   &arp_proxyall, 0, "");
152 
153 void 		arprequest_acces(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,  u_char *enaddr);
154 static void	arp_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
155 static void	arprequest (struct ifnet *,
156 			struct in_addr *, struct in_addr *, u_char *);
157 static void	arpintr(struct netmsg *);
158 static void	arptfree (struct llinfo_arp *);
159 static void	arptimer (void *);
160 static struct llinfo_arp
161 		*arplookup (in_addr_t addr, boolean_t create, boolean_t proxy);
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 			gate = rt->rt_gateway;
223 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
224 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
225 			rt->rt_expire = time_second;
226 			break;
227 		}
228 		/* Announce a new entry if requested. */
229 		if (rt->rt_flags & RTF_ANNOUNCE)
230 			arprequest(rt->rt_ifp,
231 			    &SIN(rt_key(rt))->sin_addr,
232 			    &SIN(rt_key(rt))->sin_addr,
233 			    LLADDR(SDL(gate)));
234 		/*FALLTHROUGH*/
235 	case RTM_RESOLVE:
236 		if (gate->sa_family != AF_LINK ||
237 		    gate->sa_len < sizeof(struct sockaddr_dl)) {
238 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
239 			break;
240 		}
241 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
242 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
243 		if (la != NULL)
244 			break; /* This happens on a route change */
245 		/*
246 		 * Case 2:  This route may come from cloning, or a manual route
247 		 * add with a LL address.
248 		 */
249 		R_Malloc(la, struct llinfo_arp *, sizeof *la);
250 		rt->rt_llinfo = la;
251 		if (la == NULL) {
252 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
253 			break;
254 		}
255 		bzero(la, sizeof *la);
256 		la->la_rt = rt;
257 		rt->rt_flags |= RTF_LLINFO;
258 		LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
259 
260 #ifdef INET
261 		/*
262 		 * This keeps the multicast addresses from showing up
263 		 * in `arp -a' listings as unresolved.  It's not actually
264 		 * functional.  Then the same for broadcast.
265 		 */
266 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
267 		    rt->rt_ifp->if_type != IFT_ARCNET) {
268 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
269 					       LLADDR(SDL(gate)));
270 			SDL(gate)->sdl_alen = 6;
271 			rt->rt_expire = 0;
272 		}
273 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
274 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
275 			       rt->rt_ifp->if_addrlen);
276 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
277 			rt->rt_expire = 0;
278 		}
279 #endif
280 
281 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
282 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
283 			/*
284 			 * This test used to be
285 			 *	if (loif.if_flags & IFF_UP)
286 			 * It allowed local traffic to be forced
287 			 * through the hardware by configuring the
288 			 * loopback down.  However, it causes problems
289 			 * during network configuration for boards
290 			 * that can't receive packets they send.  It
291 			 * is now necessary to clear "useloopback" and
292 			 * remove the route to force traffic out to
293 			 * the hardware.
294 			 */
295 			rt->rt_expire = 0;
296 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
297 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
298 			if (useloopback)
299 				rt->rt_ifp = loif;
300 		}
301 		break;
302 
303 	case RTM_DELETE:
304 		if (la == NULL)
305 			break;
306 		LIST_REMOVE(la, la_le);
307 		rt->rt_llinfo = NULL;
308 		rt->rt_flags &= ~RTF_LLINFO;
309 		if (la->la_hold != NULL)
310 			m_freem(la->la_hold);
311 		Free(la);
312 	}
313 }
314 
315 /*
316  * Broadcast an ARP request. Caller specifies:
317  *	- arp header source ip address
318  *	- arp header target ip address
319  *	- arp header source ethernet address
320  */
321 static void
322 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
323 	   u_char *enaddr)
324 {
325 	struct mbuf *m;
326 	struct ether_header *eh;
327 	struct arc_header *arh;
328 	struct arphdr *ah;
329 	struct sockaddr sa;
330 	static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
331 				 LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
332 	u_short ar_hrd;
333 
334 	if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
335 		return;
336 	m->m_pkthdr.rcvif = (struct ifnet *)NULL;
337 
338 	switch (ifp->if_type) {
339 	case IFT_ARCNET:
340 		ar_hrd = htons(ARPHRD_ARCNET);
341 
342 		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
343 		m->m_pkthdr.len = m->m_len;
344 		MH_ALIGN(m, m->m_len);
345 
346 		arh = (struct arc_header *)sa.sa_data;
347 		arh->arc_dhost = ifp->if_broadcastaddr[0];
348 		arh->arc_type = ARCTYPE_ARP;
349 
350 		ah = mtod(m, struct arphdr *);
351 		break;
352 
353 	case IFT_ISO88025:
354 		ar_hrd = htons(ARPHRD_IEEE802);
355 
356 		m->m_len = (sizeof llcx) +
357 		    arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
358 		m->m_pkthdr.len = m->m_len;
359 		MH_ALIGN(m, m->m_len);
360 
361 		memcpy(mtod(m, caddr_t), llcx, sizeof llcx);
362 		memcpy(sa.sa_data, ifp->if_broadcastaddr, ifp->if_addrlen);
363 		memcpy(sa.sa_data + 6, enaddr, 6);
364 		sa.sa_data[6] |= TR_RII;
365 		sa.sa_data[12] = TR_AC;
366 		sa.sa_data[13] = TR_LLC_FRAME;
367 
368 		ah = (struct arphdr *)(mtod(m, char *) + sizeof llcx);
369 		break;
370 	case IFT_FDDI:
371 	case IFT_ETHER:
372 		/*
373 		 * This may not be correct for types not explicitly
374 		 * listed, but this is our best guess
375 		 */
376 	default:
377 		ar_hrd = htons(ARPHRD_ETHER);
378 
379 		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
380 		m->m_pkthdr.len = m->m_len;
381 		MH_ALIGN(m, m->m_len);
382 
383 		eh = (struct ether_header *)sa.sa_data;
384 		/* if_output() will not swap */
385 		eh->ether_type = htons(ETHERTYPE_ARP);
386 		memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
387 
388 		ah = mtod(m, struct arphdr *);
389 		break;
390 	}
391 
392 	ah->ar_hrd = ar_hrd;
393 	ah->ar_pro = htons(ETHERTYPE_IP);
394 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
395 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
396 	ah->ar_op = htons(ARPOP_REQUEST);
397 	memcpy(ar_sha(ah), enaddr, ah->ar_hln);
398 	memset(ar_tha(ah), 0, ah->ar_hln);
399 	memcpy(ar_spa(ah), sip, ah->ar_pln);
400 	memcpy(ar_tpa(ah), tip, ah->ar_pln);
401 
402 	sa.sa_family = AF_UNSPEC;
403 	sa.sa_len = sizeof sa;
404 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)NULL);
405 }
406 
407 /*
408  * Resolve an IP address into an ethernet address.  If success,
409  * desten is filled in.  If there is no entry in arptab,
410  * set one up and broadcast a request for the IP address.
411  * Hold onto this mbuf and resend it once the address
412  * is finally resolved.  A return value of 1 indicates
413  * that desten has been filled in and the packet should be sent
414  * normally; a 0 return indicates that the packet has been
415  * taken over here, either now or for later transmission.
416  */
417 int
418 arpresolve(
419 	struct ifnet *ifp,
420 	struct rtentry *rt0,
421 	struct mbuf *m,
422 	struct sockaddr *dst,
423 	u_char *desten)
424 {
425 	struct rtentry *rt;
426 	struct llinfo_arp *la = NULL;
427 	struct sockaddr_dl *sdl;
428 
429 	if (m->m_flags & M_BCAST) {	/* broadcast */
430 		memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
431 		return (1);
432 	}
433 	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
434 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
435 		return (1);
436 	}
437 	if (rt0 != NULL) {
438 		if (rt_llroute(dst, rt0, &rt) != 0) {
439 			m_freem(m);
440 			return 0;
441 		}
442 		la = rt->rt_llinfo;
443 	}
444 	if (la == NULL) {
445 		la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE);
446 		if (la != NULL)
447 			rt = la->la_rt;
448 	}
449 	if (la == NULL || rt == NULL) {
450 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
451 		    inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
452 		    rt ? "rt" : "");
453 		m_freem(m);
454 		return (0);
455 	}
456 	sdl = SDL(rt->rt_gateway);
457 	/*
458 	 * Check the address family and length is valid, the address
459 	 * is resolved; otherwise, try to resolve.
460 	 */
461 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
462 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
463 		/*
464 		 * If entry has an expiry time and it is approaching,
465 		 * see if we need to send an ARP request within this
466 		 * arpt_down interval.
467 		 */
468 		if ((rt->rt_expire != 0) &&
469 		    (time_second + la->la_preempt > rt->rt_expire)) {
470 			arprequest(ifp,
471 				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
472 				   &SIN(dst)->sin_addr,
473 				   IF_LLADDR(ifp));
474 			la->la_preempt--;
475 		}
476 
477 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
478 		return 1;
479 	}
480 	/*
481 	 * If ARP is disabled on this interface, stop.
482 	 * XXX
483 	 * Probably should not allocate empty llinfo struct if we are
484 	 * not going to be sending out an arp request.
485 	 */
486 	if (ifp->if_flags & IFF_NOARP) {
487 		m_freem(m);
488 		return (0);
489 	}
490 	/*
491 	 * There is an arptab entry, but no ethernet address
492 	 * response yet.  Replace the held mbuf with this
493 	 * latest one.
494 	 */
495 	if (la->la_hold != NULL)
496 		m_freem(la->la_hold);
497 	la->la_hold = m;
498 	if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
499 		rt->rt_flags &= ~RTF_REJECT;
500 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
501 			rt->rt_expire = time_second;
502 			if (la->la_asked++ < arp_maxtries) {
503 				arprequest(ifp,
504 					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
505 					   &SIN(dst)->sin_addr,
506 					   IF_LLADDR(ifp));
507 			} else {
508 				rt->rt_flags |= RTF_REJECT;
509 				rt->rt_expire += arpt_down;
510 				la->la_asked = 0;
511 				la->la_preempt = arp_maxtries;
512 			}
513 
514 		}
515 	}
516 	return (0);
517 }
518 
519 /*
520  * Common length and type checks are done here,
521  * then the protocol-specific routine is called.
522  */
523 static void
524 arpintr(struct netmsg *msg)
525 {
526 	struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
527 	struct arphdr *ar;
528 	u_short ar_hrd;
529 
530 	if (m->m_len < sizeof(struct arphdr) &&
531 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
532 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
533 		goto out2;
534 	}
535 	ar = mtod(m, struct arphdr *);
536 
537 	ar_hrd = ntohs(ar->ar_hrd);
538 	if (ar_hrd != ARPHRD_ETHER &&
539 	    ar_hrd != ARPHRD_IEEE802 &&
540 	    ar_hrd != ARPHRD_ARCNET) {
541 		log(LOG_ERR,
542 		    "arp: unknown hardware address format (0x%2D)\n",
543 		    (unsigned char *)&ar->ar_hrd, "");
544 		goto out1;
545 	}
546 
547 	if (m->m_pkthdr.len < arphdr_len(ar) &&
548 	    (m = m_pullup(m, arphdr_len(ar))) == NULL) {
549 		log(LOG_ERR, "arp: runt packet\n");
550 		goto out1;
551 	}
552 
553 	switch (ntohs(ar->ar_pro)) {
554 #ifdef INET
555 		case ETHERTYPE_IP:
556 			in_arpinput(m);
557 			goto out2;
558 #endif
559 	}
560 out1:
561 	m_freem(m);
562 out2:
563 	;
564 	/* msg was embedded in the mbuf, do not reply! */
565 }
566 
567 #ifdef INET
568 /*
569  * ARP for Internet protocols on 10 Mb/s Ethernet.
570  * Algorithm is that given in RFC 826.
571  * In addition, a sanity check is performed on the sender
572  * protocol address, to catch impersonators.
573  * We no longer handle negotiations for use of trailer protocol:
574  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
575  * along with IP replies if we wanted trailers sent to us,
576  * and also sent them in response to IP replies.
577  * This allowed either end to announce the desire to receive
578  * trailer packets.
579  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
580  * but formerly didn't normally send requests.
581  */
582 static int log_arp_wrong_iface = 1;
583 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
584 	&log_arp_wrong_iface, 0,
585 	"log arp packets arriving on the wrong interface");
586 
587 static void
588 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
589 		 boolean_t dologging)
590 {
591 	struct arphdr *ah = mtod(m, struct arphdr *);
592 	struct ifnet *ifp = m->m_pkthdr.rcvif;
593 	struct llinfo_arp *la;
594 	struct sockaddr_dl *sdl;
595 	struct rtentry *rt;
596 	int cpu = mycpuid;
597 
598 	la = arplookup(saddr, create, FALSE);
599 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
600 		struct in_addr isaddr = { saddr };
601 
602 		/* the following is not an error when doing bridging */
603 		if (rt->rt_ifp != ifp) {
604 			if (dologging && log_arp_wrong_iface && cpu == 0) {
605 				log(LOG_ERR,
606 				    "arp: %s is on %s "
607 				    "but got reply from %*D on %s\n",
608 				    inet_ntoa(isaddr),
609 				    rt->rt_ifp->if_xname,
610 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
611 				    ifp->if_xname);
612 			}
613 			return;
614 		}
615 		if (sdl->sdl_alen &&
616 		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
617 			if (rt->rt_expire != 0) {
618 				if (dologging && cpu == 0) {
619 			    		log(LOG_INFO,
620 			    		"arp: %s moved from %*D to %*D on %s\n",
621 			    		inet_ntoa(isaddr),
622 			    		ifp->if_addrlen, (u_char *)LLADDR(sdl),
623 			    		":", ifp->if_addrlen,
624 			    		(u_char *)ar_sha(ah), ":",
625 			    		ifp->if_xname);
626 				}
627 			} else {
628 				if (dologging && cpu == 0) {
629 					log(LOG_ERR,
630 					"arp: %*D attempts to modify permanent entry for %s on %s\n",
631 					ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
632 					inet_ntoa(isaddr), ifp->if_xname);
633 				}
634 				return;
635 			}
636 		}
637 		/*
638 		 * sanity check for the address length.
639 		 * XXX this does not work for protocols with variable address
640 		 * length. -is
641 		 */
642 		if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln &&
643 		    cpu == 0)
644 		{
645 			log(LOG_WARNING,
646 			    "arp from %*D: new addr len %d, was %d",
647 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
648 			    ah->ar_hln, sdl->sdl_alen);
649 		}
650 		if (ifp->if_addrlen != ah->ar_hln) {
651 			if (dologging && cpu == 0) {
652 				log(LOG_WARNING,
653 				"arp from %*D: addr len: new %d, i/f %d (ignored)",
654 				ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
655 				ah->ar_hln, ifp->if_addrlen);
656 			}
657 			return;
658 		}
659 		memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
660 		/*
661 		 * If we receive an arp from a token-ring station over
662 		 * a token-ring nic then try to save the source
663 		 * routing info.
664 		 */
665 		if (ifp->if_type == IFT_ISO88025) {
666 			struct iso88025_header *th =
667 			    (struct iso88025_header *)m->m_pkthdr.header;
668 			struct iso88025_sockaddr_dl_data *trld =
669 			    SDL_ISO88025(sdl);
670 			int rif_len;
671 
672 			rif_len = TR_RCF_RIFLEN(th->rcf);
673 			if ((th->iso88025_shost[0] & TR_RII) &&
674 			    (rif_len > 2)) {
675 				trld->trld_rcf = th->rcf;
676 				trld->trld_rcf ^= htons(TR_RCF_DIR);
677 				memcpy(trld->trld_route, th->rd, rif_len - 2);
678 				trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
679 				/*
680 				 * Set up source routing information for
681 				 * reply packet (XXX)
682 				 */
683 				m->m_data -= rif_len;
684 				m->m_len  += rif_len;
685 				m->m_pkthdr.len += rif_len;
686 			} else {
687 				th->iso88025_shost[0] &= ~TR_RII;
688 				trld->trld_rcf = 0;
689 			}
690 			m->m_data -= 8;
691 			m->m_len  += 8;
692 			m->m_pkthdr.len += 8;
693 			th->rcf = trld->trld_rcf;
694 		}
695 		if (rt->rt_expire != 0)
696 			rt->rt_expire = time_second + arpt_keep;
697 		rt->rt_flags &= ~RTF_REJECT;
698 		la->la_asked = 0;
699 		la->la_preempt = arp_maxtries;
700 
701 		/*
702 		 * This particular cpu might have been holding an mbuf
703 		 * pending ARP resolution.  If so, transmit the mbuf now.
704 		 */
705 		if (la->la_hold != NULL) {
706 			m_adj(la->la_hold, sizeof(struct ether_header));
707 			lwkt_serialize_enter(ifp->if_serializer);
708 			(*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
709 			lwkt_serialize_exit(ifp->if_serializer);
710 			la->la_hold = NULL;
711 		}
712 	}
713 }
714 
715 #ifdef SMP
716 
717 struct netmsg_arp_update {
718 	struct netmsg	netmsg;
719 	struct mbuf	*m;
720 	in_addr_t	saddr;
721 	boolean_t	create;
722 };
723 
724 static void arp_update_msghandler(struct netmsg *netmsg);
725 
726 #endif
727 
728 /*
729  * Called from arpintr() - this routine is run from a single cpu.
730  */
731 static void
732 in_arpinput(struct mbuf *m)
733 {
734 	struct arphdr *ah;
735 	struct ifnet *ifp = m->m_pkthdr.rcvif;
736 	struct ether_header *eh;
737 	struct arc_header *arh;
738 	struct iso88025_header *th = (struct iso88025_header *)NULL;
739 	struct rtentry *rt;
740 	struct ifaddr *ifa;
741 	struct in_ifaddr *ia;
742 	struct sockaddr sa;
743 	struct in_addr isaddr, itaddr, myaddr;
744 #ifdef SMP
745 	struct netmsg_arp_update msg;
746 #endif
747 	u_int8_t *enaddr = NULL;
748 	int op;
749 	int req_len;
750 
751 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
752 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
753 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
754 		return;
755 	}
756 
757 	ah = mtod(m, struct arphdr *);
758 	op = ntohs(ah->ar_op);
759 	memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
760 	memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
761 	/*
762 	 * Check both target and sender IP addresses:
763 	 *
764 	 * If we receive the packet on the interface owning the address,
765 	 * then accept the address.
766 	 *
767 	 * For a bridge, we accept the address if the receive interface and
768 	 * the interface owning the address are on the same bridge.
769 	 * (This will change slightly when we have clusters of interfaces).
770 	 */
771 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
772 		/* Skip all ia's which don't match */
773 		if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
774 			continue;
775 
776 		if (ia->ia_ifp == ifp)
777 			goto match;
778 
779 		if (ifp->if_bridge && ia->ia_ifp &&
780 		    ifp->if_bridge == ia->ia_ifp->if_bridge)
781 			goto match;
782 
783 #ifdef CARP
784 	/*
785 	 * If the interface does not match, but the recieving interface
786 	 * is part of carp, we call carp_iamatch to see if this is a
787 	 * request for the virtual host ip.
788 	 * XXX: This is really ugly!
789 	 */
790         	if (ifp->if_carp != NULL &&
791 		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
792 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
793                         goto match;
794 #endif
795 	}
796 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) {
797 		/* Skip all ia's which don't match */
798 		if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
799 			continue;
800 
801 		if (ia->ia_ifp == ifp)
802 			goto match;
803 
804 		if (ifp->if_bridge && ia->ia_ifp &&
805 		    ifp->if_bridge == ia->ia_ifp->if_bridge)
806 			goto match;
807 	}
808 	/*
809 	 * No match, use the first inet address on the receive interface
810 	 * as a dummy address for the rest of the function.
811 	 */
812 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
813 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
814 			ia = ifatoia(ifa);
815 			goto match;
816 		}
817 	}
818 	/*
819 	 * If we got here, we didn't find any suitable interface,
820 	 * so drop the packet.
821 	 */
822 	m_freem(m);
823 	return;
824 
825 match:
826 	if (!enaddr)
827 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
828 	myaddr = ia->ia_addr.sin_addr;
829 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
830 		m_freem(m);	/* it's from me, ignore it. */
831 		return;
832 	}
833 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
834 		log(LOG_ERR,
835 		    "arp: link address is broadcast for IP address %s!\n",
836 		    inet_ntoa(isaddr));
837 		m_freem(m);
838 		return;
839 	}
840 	if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
841 		log(LOG_ERR,
842 		   "arp: %*D is using my IP address %s!\n",
843 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
844 		   inet_ntoa(isaddr));
845 		itaddr = myaddr;
846 		goto reply;
847 	}
848 #ifdef SMP
849 	netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
850 		    arp_update_msghandler);
851 	msg.m = m;
852 	msg.saddr = isaddr.s_addr;
853 	msg.create = (itaddr.s_addr == myaddr.s_addr);
854 	lwkt_domsg(rtable_portfn(0), &msg.netmsg.nm_lmsg, 0);
855 #endif
856 	arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
857 			 TRUE);
858 reply:
859 	if (op != ARPOP_REQUEST) {
860 		m_freem(m);
861 		return;
862 	}
863 	if (itaddr.s_addr == myaddr.s_addr) {
864 		/* I am the target */
865 		memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
866 		memcpy(ar_sha(ah), enaddr, ah->ar_hln);
867 	} else {
868 		struct llinfo_arp *la;
869 
870 		la = arplookup(itaddr.s_addr, FALSE, SIN_PROXY);
871 		if (la == NULL) {
872 			struct sockaddr_in sin;
873 
874 			if (!arp_proxyall) {
875 				m_freem(m);
876 				return;
877 			}
878 
879 			bzero(&sin, sizeof sin);
880 			sin.sin_family = AF_INET;
881 			sin.sin_len = sizeof sin;
882 			sin.sin_addr = itaddr;
883 
884 			rt = rtpurelookup((struct sockaddr *)&sin);
885 			if (rt == NULL) {
886 				m_freem(m);
887 				return;
888 			}
889 			--rt->rt_refcnt;
890 			/*
891 			 * Don't send proxies for nodes on the same interface
892 			 * as this one came out of, or we'll get into a fight
893 			 * over who claims what Ether address.
894 			 */
895 			if (rt->rt_ifp == ifp) {
896 				m_freem(m);
897 				return;
898 			}
899 			memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
900 			memcpy(ar_sha(ah), enaddr, ah->ar_hln);
901 #ifdef DEBUG_PROXY
902 			kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
903 #endif
904 		} else {
905 			struct sockaddr_dl *sdl;
906 
907 			rt = la->la_rt;
908 			memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
909 			sdl = SDL(rt->rt_gateway);
910 			memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
911 		}
912 	}
913 
914 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
915 	memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
916 	ah->ar_op = htons(ARPOP_REPLY);
917 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
918 	switch (ifp->if_type) {
919 	case IFT_ARCNET:
920 		arh = (struct arc_header *)sa.sa_data;
921 		arh->arc_dhost = *ar_tha(ah);
922 		arh->arc_type = ARCTYPE_ARP;
923 		break;
924 	case IFT_ISO88025:
925 		/* Re-arrange the source/dest address */
926 		memcpy(th->iso88025_dhost, th->iso88025_shost,
927 		    sizeof th->iso88025_dhost);
928 		memcpy(th->iso88025_shost, IF_LLADDR(ifp),
929 		    sizeof th->iso88025_shost);
930 		/* Set the source routing bit if neccesary */
931 		if (th->iso88025_dhost[0] & TR_RII) {
932 			th->iso88025_dhost[0] &= ~TR_RII;
933 			if (TR_RCF_RIFLEN(th->rcf) > 2)
934 				th->iso88025_shost[0] |= TR_RII;
935 		}
936 		/* Copy the addresses, ac and fc into sa_data */
937 		memcpy(sa.sa_data, th->iso88025_dhost,
938 		    (sizeof th->iso88025_dhost) * 2);
939 		sa.sa_data[(sizeof th->iso88025_dhost) * 2] = TR_AC;
940 		sa.sa_data[(sizeof th->iso88025_dhost) * 2 + 1] = TR_LLC_FRAME;
941 		break;
942 	case IFT_ETHER:
943 	case IFT_FDDI:
944 	/*
945 	 * May not be correct for types not explictly
946 	 * listed, but it is our best guess.
947 	 */
948 	default:
949 		eh = (struct ether_header *)sa.sa_data;
950 		memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
951 		eh->ether_type = htons(ETHERTYPE_ARP);
952 		break;
953 	}
954 	sa.sa_family = AF_UNSPEC;
955 	sa.sa_len = sizeof sa;
956 	lwkt_serialize_enter(ifp->if_serializer);
957 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
958 	lwkt_serialize_exit(ifp->if_serializer);
959 	return;
960 }
961 
962 #ifdef SMP
963 
964 static
965 void
966 arp_update_msghandler(struct netmsg *netmsg)
967 {
968 	struct netmsg_arp_update *msg = (struct netmsg_arp_update *)netmsg;
969 	int nextcpu;
970 
971 	arp_update_oncpu(msg->m, msg->saddr, msg->create, FALSE);
972 
973 	nextcpu = mycpuid + 1;
974 	if (nextcpu < ncpus) {
975 		lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->netmsg.nm_lmsg);
976 	} else {
977 		lwkt_replymsg(&msg->netmsg.nm_lmsg, 0);
978 	}
979 }
980 
981 #endif
982 
983 #endif
984 
985 /*
986  * Free an arp entry.  If the arp entry is actively referenced or represents
987  * a static entry we only clear it back to an unresolved state, otherwise
988  * we destroy the entry entirely.
989  *
990  * Note that static entries are created when route add ... -interface is used
991  * to create an interface route to a (direct) destination.
992  */
993 static void
994 arptfree(struct llinfo_arp *la)
995 {
996 	struct rtentry *rt = la->la_rt;
997 	struct sockaddr_dl *sdl;
998 
999 	if (rt == NULL)
1000 		panic("arptfree");
1001 	sdl = SDL(rt->rt_gateway);
1002 	if (sdl != NULL &&
1003 	    ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1004 	     (rt->rt_flags & RTF_STATIC))) {
1005 		sdl->sdl_alen = 0;
1006 		la->la_preempt = la->la_asked = 0;
1007 		rt->rt_flags &= ~RTF_REJECT;
1008 		return;
1009 	}
1010 	rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1011 }
1012 
1013 /*
1014  * Lookup or enter a new address in arptab.
1015  */
1016 static struct llinfo_arp *
1017 arplookup(in_addr_t addr, boolean_t create, boolean_t proxy)
1018 {
1019 	struct rtentry *rt;
1020 	struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1021 	const char *why = NULL;
1022 
1023 	sin.sin_addr.s_addr = addr;
1024 	sin.sin_other = proxy ? SIN_PROXY : 0;
1025 	if (create)
1026 		rt = rtlookup((struct sockaddr *)&sin);
1027 	else
1028 		rt = rtpurelookup((struct sockaddr *)&sin);
1029 	if (rt == NULL)
1030 		return (NULL);
1031 	rt->rt_refcnt--;
1032 
1033 	if (rt->rt_flags & RTF_GATEWAY)
1034 		why = "host is not on local network";
1035 	else if (!(rt->rt_flags & RTF_LLINFO))
1036 		why = "could not allocate llinfo";
1037 	else if (rt->rt_gateway->sa_family != AF_LINK)
1038 		why = "gateway route is not ours";
1039 
1040 	if (why) {
1041 		if (create) {
1042 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
1043 			    inet_ntoa(sin.sin_addr), why);
1044 		}
1045 		if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1046 			/* No references to this route.  Purge it. */
1047 			rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1048 				  rt_mask(rt), rt->rt_flags, NULL);
1049 		}
1050 		return (NULL);
1051 	}
1052 	return (rt->rt_llinfo);
1053 }
1054 
1055 void
1056 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1057 {
1058 	if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY)
1059 		arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr,
1060 			   IF_LLADDR(ifp));
1061 	ifa->ifa_rtrequest = arp_rtrequest;
1062 	ifa->ifa_flags |= RTF_CLONING;
1063 }
1064 
1065 void
1066 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1067 {
1068 	if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY)
1069 		arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr,
1070 			   enaddr);
1071 	ifa->ifa_rtrequest = arp_rtrequest;
1072 	ifa->ifa_flags |= RTF_CLONING;
1073 }
1074 
1075 static void
1076 arp_init(void)
1077 {
1078 	int cpu;
1079 
1080 	for (cpu = 0; cpu < ncpus2; cpu++)
1081 		LIST_INIT(&llinfo_arp_list[cpu]);
1082 	netisr_register(NETISR_ARP, cpu0_portfn, arpintr);
1083 }
1084 
1085 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
1086