xref: /freebsd/sys/netinet/if_ether.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
30  * $FreeBSD$
31  */
32 
33 /*
34  * Ethernet address resolution protocol.
35  * TODO:
36  *	add "inuse/lock" bit (or ref. count) along with valid bit
37  */
38 
39 #include "opt_inet.h"
40 #include "opt_bdg.h"
41 #include "opt_mac.h"
42 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/mac.h>
49 #include <sys/mbuf.h>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/syslog.h>
53 
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <net/netisr.h>
59 #include <net/if_llc.h>
60 #ifdef BRIDGE
61 #include <net/ethernet.h>
62 #include <net/bridge.h>
63 #endif
64 
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
68 
69 #include <net/if_arc.h>
70 #include <net/iso88025.h>
71 
72 #define SIN(s) ((struct sockaddr_in *)s)
73 #define SDL(s) ((struct sockaddr_dl *)s)
74 
75 SYSCTL_DECL(_net_link_ether);
76 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
77 
78 /* timer values */
79 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
80 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
81 static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
82 
83 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
84 	   &arpt_prune, 0, "");
85 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
86 	   &arpt_keep, 0, "");
87 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
88 	   &arpt_down, 0, "");
89 
90 #define	rt_expire rt_rmx.rmx_expire
91 
92 struct llinfo_arp {
93 	LIST_ENTRY(llinfo_arp) la_le;
94 	struct	rtentry *la_rt;
95 	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
96 	u_short	la_preempt;	/* countdown for pre-expiry arps */
97 	u_short	la_asked;	/* #times we QUERIED following expiration */
98 #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
99 };
100 
101 static	LIST_HEAD(, llinfo_arp) llinfo_arp;
102 
103 static struct	ifqueue arpintrq;
104 static int	arp_allocated;
105 
106 static int	arp_maxtries = 5;
107 static int	useloopback = 1; /* use loopback interface for local traffic */
108 static int	arp_proxyall = 0;
109 static struct callout arp_callout;
110 
111 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
112 	   &arp_maxtries, 0, "");
113 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
114 	   &useloopback, 0, "");
115 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
116 	   &arp_proxyall, 0, "");
117 
118 static void	arp_init(void);
119 static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
120 static void	arprequest(struct ifnet *,
121 			struct in_addr *, struct in_addr *, u_char *);
122 static void	arpintr(struct mbuf *);
123 static void	arptfree(struct llinfo_arp *);
124 static void	arptimer(void *);
125 static struct llinfo_arp
126 		*arplookup(u_long, int, int);
127 #ifdef INET
128 static void	in_arpinput(struct mbuf *);
129 #endif
130 
131 /*
132  * Timeout routine.  Age arp_tab entries periodically.
133  */
134 /* ARGSUSED */
135 static void
136 arptimer(ignored_arg)
137 	void *ignored_arg;
138 {
139 	struct llinfo_arp *la, *ola;
140 
141 	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
142 	la = LIST_FIRST(&llinfo_arp);
143 	while (la != NULL) {
144 		struct rtentry *rt = la->la_rt;
145 		ola = la;
146 		la = LIST_NEXT(la, la_le);
147 		if (rt->rt_expire && rt->rt_expire <= time_second)
148 			arptfree(ola);		/* timer has expired, clear */
149 	}
150 	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
151 
152 	callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
153 }
154 
155 /*
156  * Parallel to llc_rtrequest.
157  */
158 static void
159 arp_rtrequest(req, rt, info)
160 	int req;
161 	struct rtentry *rt;
162 	struct rt_addrinfo *info;
163 {
164 	struct sockaddr *gate;
165 	struct llinfo_arp *la;
166 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
167 
168 	RT_LOCK_ASSERT(rt);
169 
170 	if (rt->rt_flags & RTF_GATEWAY)
171 		return;
172 	gate = rt->rt_gateway;
173 	la = (struct llinfo_arp *)rt->rt_llinfo;
174 	switch (req) {
175 
176 	case RTM_ADD:
177 		/*
178 		 * XXX: If this is a manually added route to interface
179 		 * such as older version of routed or gated might provide,
180 		 * restore cloning bit.
181 		 */
182 		if ((rt->rt_flags & RTF_HOST) == 0 &&
183 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
184 			rt->rt_flags |= RTF_CLONING;
185 		if (rt->rt_flags & RTF_CLONING) {
186 			/*
187 			 * Case 1: This route should come from a route to iface.
188 			 */
189 			rt_setgate(rt, rt_key(rt),
190 					(struct sockaddr *)&null_sdl);
191 			gate = rt->rt_gateway;
192 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
193 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
194 			rt->rt_expire = time_second;
195 			break;
196 		}
197 		/* Announce a new entry if requested. */
198 		if (rt->rt_flags & RTF_ANNOUNCE)
199 			arprequest(rt->rt_ifp,
200 			    &SIN(rt_key(rt))->sin_addr,
201 			    &SIN(rt_key(rt))->sin_addr,
202 			    (u_char *)LLADDR(SDL(gate)));
203 		/*FALLTHROUGH*/
204 	case RTM_RESOLVE:
205 		if (gate->sa_family != AF_LINK ||
206 		    gate->sa_len < sizeof(null_sdl)) {
207 			log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
208 			    inet_ntoa(SIN(rt_key(rt))->sin_addr),
209 			    (gate->sa_family != AF_LINK) ?
210 			    " (!AF_LINK)": "");
211 			break;
212 		}
213 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
214 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
215 		if (la != 0)
216 			break; /* This happens on a route change */
217 		/*
218 		 * Case 2:  This route may come from cloning, or a manual route
219 		 * add with a LL address.
220 		 */
221 		R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
222 		rt->rt_llinfo = (caddr_t)la;
223 		if (la == 0) {
224 			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
225 			break;
226 		}
227 		arp_allocated++;
228 		la->la_rt = rt;
229 		rt->rt_flags |= RTF_LLINFO;
230 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
231 		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
232 
233 #ifdef INET
234 		/*
235 		 * This keeps the multicast addresses from showing up
236 		 * in `arp -a' listings as unresolved.  It's not actually
237 		 * functional.  Then the same for broadcast.
238 		 */
239 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
240 		    rt->rt_ifp->if_type != IFT_ARCNET) {
241 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
242 					       LLADDR(SDL(gate)));
243 			SDL(gate)->sdl_alen = 6;
244 			rt->rt_expire = 0;
245 		}
246 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
247 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
248 			       rt->rt_ifp->if_addrlen);
249 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
250 			rt->rt_expire = 0;
251 		}
252 #endif
253 
254 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
255 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
256 		    /*
257 		     * This test used to be
258 		     *	if (loif.if_flags & IFF_UP)
259 		     * It allowed local traffic to be forced
260 		     * through the hardware by configuring the loopback down.
261 		     * However, it causes problems during network configuration
262 		     * for boards that can't receive packets they send.
263 		     * It is now necessary to clear "useloopback" and remove
264 		     * the route to force traffic out to the hardware.
265 		     */
266 			rt->rt_expire = 0;
267 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
268 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
269 			if (useloopback)
270 				rt->rt_ifp = loif;
271 
272 		}
273 		break;
274 
275 	case RTM_DELETE:
276 		if (la == 0)
277 			break;
278 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
279 		LIST_REMOVE(la, la_le);
280 		rt->rt_llinfo = 0;
281 		rt->rt_flags &= ~RTF_LLINFO;
282 		if (la->la_hold)
283 			m_freem(la->la_hold);
284 		Free((caddr_t)la);
285 	}
286 }
287 
288 /*
289  * Broadcast an ARP request. Caller specifies:
290  *	- arp header source ip address
291  *	- arp header target ip address
292  *	- arp header source ethernet address
293  */
294 static void
295 arprequest(ifp, sip, tip, enaddr)
296 	struct ifnet *ifp;
297 	struct in_addr *sip, *tip;
298 	u_char *enaddr;
299 {
300 	struct mbuf *m;
301 	struct arphdr *ah;
302 	struct sockaddr sa;
303 
304 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
305 		return;
306 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
307 		2*ifp->if_data.ifi_addrlen;
308 	m->m_pkthdr.len = m->m_len;
309 	MH_ALIGN(m, m->m_len);
310 	ah = mtod(m, struct arphdr *);
311 	bzero((caddr_t)ah, m->m_len);
312 #ifdef MAC
313 	mac_create_mbuf_linklayer(ifp, m);
314 #endif
315 	ah->ar_pro = htons(ETHERTYPE_IP);
316 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
317 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
318 	ah->ar_op = htons(ARPOP_REQUEST);
319 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
320 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
321 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
322 	sa.sa_family = AF_ARP;
323 	sa.sa_len = 2;
324 	m->m_flags |= M_BCAST;
325 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
326 
327 	return;
328 }
329 
330 /*
331  * Resolve an IP address into an ethernet address.
332  * On input:
333  *    ifp is the interface we use
334  *    dst is the next hop,
335  *    rt0 is the route to the final destination (possibly useless)
336  *    m is the mbuf
337  *    desten is where we want the address.
338  *
339  * On success, desten is filled in and the function returns 0;
340  * If the packet must be held pending resolution, we return EWOULDBLOCK
341  * On other errors, we return the corresponding error code.
342  */
343 int
344 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
345 	struct sockaddr *dst, u_char *desten)
346 {
347 	struct llinfo_arp *la = 0;
348 	struct sockaddr_dl *sdl;
349 	int error;
350 	struct rtentry *rt;
351 
352 	error = rt_check(&rt, &rt0, dst);
353 	if (error) {
354 		m_freem(m);
355 		return error;
356 	}
357 
358 	if (m->m_flags & M_BCAST) {	/* broadcast */
359 		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
360 		return (0);
361 	}
362 	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
363 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
364 		return (0);
365 	}
366 	if (rt)
367 		la = (struct llinfo_arp *)rt->rt_llinfo;
368 	if (la == 0) {
369 		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
370 		if (la)
371 			rt = la->la_rt;
372 	}
373 	if (la == 0 || rt == 0) {
374 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
375 			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
376 				rt ? "rt" : "");
377 		m_freem(m);
378 		return (EINVAL); /* XXX */
379 	}
380 	sdl = SDL(rt->rt_gateway);
381 	/*
382 	 * Check the address family and length is valid, the address
383 	 * is resolved; otherwise, try to resolve.
384 	 */
385 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
386 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
387 		/*
388 		 * If entry has an expiry time and it is approaching,
389 		 * see if we need to send an ARP request within this
390 		 * arpt_down interval.
391 		 */
392 		if ((rt->rt_expire != 0) &&
393 		    (time_second + la->la_preempt > rt->rt_expire)) {
394 			arprequest(ifp,
395 				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
396 				   &SIN(dst)->sin_addr,
397 				   IF_LLADDR(ifp));
398 			la->la_preempt--;
399 		}
400 
401 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
402 		return (0);
403 	}
404 	/*
405 	 * If ARP is disabled or static on this interface, stop.
406 	 * XXX
407 	 * Probably should not allocate empty llinfo struct if we are
408 	 * not going to be sending out an arp request.
409 	 */
410 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
411 		m_freem(m);
412 		return (EINVAL);
413 	}
414 	/*
415 	 * There is an arptab entry, but no ethernet address
416 	 * response yet.  Replace the held mbuf with this
417 	 * latest one.
418 	 */
419 	if (la->la_hold)
420 		m_freem(la->la_hold);
421 	la->la_hold = m;
422 	if (rt->rt_expire) {
423 		RT_LOCK(rt);
424 		rt->rt_flags &= ~RTF_REJECT;
425 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
426 			rt->rt_expire = time_second;
427 			if (la->la_asked++ < arp_maxtries) {
428 				arprequest(ifp,
429 					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
430 					   &SIN(dst)->sin_addr,
431 					   IF_LLADDR(ifp));
432 			} else {
433 				rt->rt_flags |= RTF_REJECT;
434 				rt->rt_expire += arpt_down;
435 				la->la_asked = 0;
436 				la->la_preempt = arp_maxtries;
437 			}
438 
439 		}
440 		RT_UNLOCK(rt);
441 	}
442 	return (EWOULDBLOCK);
443 }
444 
445 /*
446  * Common length and type checks are done here,
447  * then the protocol-specific routine is called.
448  */
449 static void
450 arpintr(struct mbuf *m)
451 {
452 	struct arphdr *ar;
453 
454 	if (m->m_len < sizeof(struct arphdr) &&
455 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
456 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
457 		return;
458 	}
459 	ar = mtod(m, struct arphdr *);
460 
461 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
462 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
463 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
464 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
465 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
466 		    (unsigned char *)&ar->ar_hrd, "");
467 		m_freem(m);
468 		return;
469 	}
470 
471 	if (m->m_len < arphdr_len(ar)) {
472 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
473 			log(LOG_ERR, "arp: runt packet\n");
474 			m_freem(m);
475 			return;
476 		}
477 		ar = mtod(m, struct arphdr *);
478 	}
479 
480 	switch (ntohs(ar->ar_pro)) {
481 #ifdef INET
482 	case ETHERTYPE_IP:
483 		in_arpinput(m);
484 		return;
485 #endif
486 	}
487 	m_freem(m);
488 }
489 
490 #ifdef INET
491 /*
492  * ARP for Internet protocols on 10 Mb/s Ethernet.
493  * Algorithm is that given in RFC 826.
494  * In addition, a sanity check is performed on the sender
495  * protocol address, to catch impersonators.
496  * We no longer handle negotiations for use of trailer protocol:
497  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
498  * along with IP replies if we wanted trailers sent to us,
499  * and also sent them in response to IP replies.
500  * This allowed either end to announce the desire to receive
501  * trailer packets.
502  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
503  * but formerly didn't normally send requests.
504  */
505 static int log_arp_wrong_iface = 1;
506 static int log_arp_movements = 1;
507 
508 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
509 	&log_arp_wrong_iface, 0,
510 	"log arp packets arriving on the wrong interface");
511 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
512         &log_arp_movements, 0,
513         "log arp replies from MACs different than the one in the cache");
514 
515 
516 static void
517 in_arpinput(m)
518 	struct mbuf *m;
519 {
520 	struct arphdr *ah;
521 	struct ifnet *ifp = m->m_pkthdr.rcvif;
522 	struct iso88025_header *th = (struct iso88025_header *)0;
523 	struct iso88025_sockaddr_dl_data *trld;
524 	struct llinfo_arp *la = 0;
525 	struct rtentry *rt;
526 	struct ifaddr *ifa;
527 	struct in_ifaddr *ia;
528 	struct sockaddr_dl *sdl;
529 	struct sockaddr sa;
530 	struct in_addr isaddr, itaddr, myaddr;
531 	int op, rif_len;
532 	int req_len;
533 
534 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
535 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
536 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
537 		return;
538 	}
539 
540 	ah = mtod(m, struct arphdr *);
541 	op = ntohs(ah->ar_op);
542 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
543 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
544 #ifdef BRIDGE
545 #define BRIDGE_TEST (do_bridge)
546 #else
547 #define BRIDGE_TEST (0) /* cc will optimise the test away */
548 #endif
549 	/*
550 	 * For a bridge, we want to check the address irrespective
551 	 * of the receive interface. (This will change slightly
552 	 * when we have clusters of interfaces).
553 	 */
554 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash)
555 		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
556 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
557 			goto match;
558 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
559 		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
560 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
561 			goto match;
562 	/*
563 	 * No match, use the first inet address on the receive interface
564 	 * as a dummy address for the rest of the function.
565 	 */
566 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
567 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
568 			ia = ifatoia(ifa);
569 			goto match;
570 		}
571 	/*
572 	 * If bridging, fall back to using any inet address.
573 	 */
574 	if (!BRIDGE_TEST || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
575 		goto drop;
576 match:
577 	myaddr = ia->ia_addr.sin_addr;
578 	if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen))
579 		goto drop;	/* it's from me, ignore it. */
580 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
581 		log(LOG_ERR,
582 		    "arp: link address is broadcast for IP address %s!\n",
583 		    inet_ntoa(isaddr));
584 		goto drop;
585 	}
586 	if (isaddr.s_addr == myaddr.s_addr) {
587 		log(LOG_ERR,
588 		   "arp: %*D is using my IP address %s!\n",
589 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
590 		   inet_ntoa(isaddr));
591 		itaddr = myaddr;
592 		goto reply;
593 	}
594 	if (ifp->if_flags & IFF_STATICARP)
595 		goto reply;
596 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
597 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
598 		/* the following is not an error when doing bridging */
599 		if (!BRIDGE_TEST && rt->rt_ifp != ifp) {
600 			if (log_arp_wrong_iface)
601 				log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
602 				    inet_ntoa(isaddr),
603 				    rt->rt_ifp->if_xname,
604 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
605 				    ifp->if_xname);
606 			goto reply;
607 		}
608 		if (sdl->sdl_alen &&
609 		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
610 			if (rt->rt_expire) {
611 			    if (log_arp_movements)
612 			        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
613 				    inet_ntoa(isaddr),
614 				    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
615 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
616 				    ifp->if_xname);
617 			} else {
618 			    log(LOG_ERR,
619 				"arp: %*D attempts to modify permanent entry for %s on %s\n",
620 				ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
621 				inet_ntoa(isaddr), ifp->if_xname);
622 			    goto reply;
623 			}
624 		}
625 		/*
626 		 * sanity check for the address length.
627 		 * XXX this does not work for protocols with variable address
628 		 * length. -is
629 		 */
630 		if (sdl->sdl_alen &&
631 		    sdl->sdl_alen != ah->ar_hln) {
632 			log(LOG_WARNING,
633 			    "arp from %*D: new addr len %d, was %d",
634 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
635 			    ah->ar_hln, sdl->sdl_alen);
636 		}
637 		if (ifp->if_addrlen != ah->ar_hln) {
638 			log(LOG_WARNING,
639 			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
640 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
641 			    ah->ar_hln, ifp->if_addrlen);
642 			goto reply;
643 		}
644 		(void)memcpy(LLADDR(sdl), ar_sha(ah),
645 		    sdl->sdl_alen = ah->ar_hln);
646 		/*
647 		 * If we receive an arp from a token-ring station over
648 		 * a token-ring nic then try to save the source
649 		 * routing info.
650 		 */
651 		if (ifp->if_type == IFT_ISO88025) {
652 			th = (struct iso88025_header *)m->m_pkthdr.header;
653 			trld = SDL_ISO88025(sdl);
654 			rif_len = TR_RCF_RIFLEN(th->rcf);
655 			if ((th->iso88025_shost[0] & TR_RII) &&
656 			    (rif_len > 2)) {
657 				trld->trld_rcf = th->rcf;
658 				trld->trld_rcf ^= htons(TR_RCF_DIR);
659 				memcpy(trld->trld_route, th->rd, rif_len - 2);
660 				trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
661 				/*
662 				 * Set up source routing information for
663 				 * reply packet (XXX)
664 				 */
665 				m->m_data -= rif_len;
666 				m->m_len  += rif_len;
667 				m->m_pkthdr.len += rif_len;
668 			} else {
669 				th->iso88025_shost[0] &= ~TR_RII;
670 				trld->trld_rcf = 0;
671 			}
672 			m->m_data -= 8;
673 			m->m_len  += 8;
674 			m->m_pkthdr.len += 8;
675 			th->rcf = trld->trld_rcf;
676 		}
677 		RT_LOCK(rt);
678 		if (rt->rt_expire)
679 			rt->rt_expire = time_second + arpt_keep;
680 		rt->rt_flags &= ~RTF_REJECT;
681 		RT_UNLOCK(rt);
682 		la->la_asked = 0;
683 		la->la_preempt = arp_maxtries;
684 		if (la->la_hold) {
685 			(*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
686 			la->la_hold = 0;
687 		}
688 	}
689 reply:
690 	if (op != ARPOP_REQUEST)
691 		goto drop;
692 	if (itaddr.s_addr == myaddr.s_addr) {
693 		/* I am the target */
694 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
695 		(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
696 	} else {
697 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
698 		if (la == NULL) {
699 			struct sockaddr_in sin;
700 
701 			if (!arp_proxyall)
702 				goto drop;
703 
704 			bzero(&sin, sizeof sin);
705 			sin.sin_family = AF_INET;
706 			sin.sin_len = sizeof sin;
707 			sin.sin_addr = itaddr;
708 
709 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
710 			if (!rt)
711 				goto drop;
712 			/*
713 			 * Don't send proxies for nodes on the same interface
714 			 * as this one came out of, or we'll get into a fight
715 			 * over who claims what Ether address.
716 			 */
717 			if (rt->rt_ifp == ifp) {
718 				rtfree(rt);
719 				goto drop;
720 			}
721 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
722 			(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
723 			rtfree(rt);
724 
725 			/*
726 			 * Also check that the node which sent the ARP packet
727 			 * is on the the interface we expect it to be on. This
728 			 * avoids ARP chaos if an interface is connected to the
729 			 * wrong network.
730 			 */
731 			sin.sin_addr = isaddr;
732 
733 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
734 			if (!rt)
735 				goto drop;
736 			if (rt->rt_ifp != ifp) {
737 				log(LOG_INFO, "arp_proxy: ignoring request"
738 				    " from %s via %s, expecting %s\n",
739 				    inet_ntoa(isaddr), ifp->if_xname,
740 				    rt->rt_ifp->if_xname);
741 				rtfree(rt);
742 				goto drop;
743 			}
744 			rtfree(rt);
745 
746 #ifdef DEBUG_PROXY
747 			printf("arp: proxying for %s\n",
748 			       inet_ntoa(itaddr));
749 #endif
750 		} else {
751 			rt = la->la_rt;
752 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
753 			sdl = SDL(rt->rt_gateway);
754 			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
755 		}
756 	}
757 
758 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
759 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
760 	ah->ar_op = htons(ARPOP_REPLY);
761 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
762 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
763 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
764 	m->m_pkthdr.len = m->m_len;
765 	sa.sa_family = AF_ARP;
766 	sa.sa_len = 2;
767 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
768 	return;
769 
770 drop:
771 	m_freem(m);
772 }
773 #endif
774 
775 /*
776  * Free an arp entry.
777  */
778 static void
779 arptfree(la)
780 	struct llinfo_arp *la;
781 {
782 	struct rtentry *rt = la->la_rt;
783 	struct sockaddr_dl *sdl;
784 
785 	if (rt == 0)
786 		panic("arptfree");
787 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
788 	    sdl->sdl_family == AF_LINK) {
789 		sdl->sdl_alen = 0;
790 		la->la_preempt = la->la_asked = 0;
791 		RT_LOCK(rt);		/* XXX needed or move higher? */
792 		rt->rt_flags &= ~RTF_REJECT;
793 		RT_UNLOCK(rt);
794 		return;
795 	}
796 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
797 			0, (struct rtentry **)0);
798 }
799 /*
800  * Lookup or enter a new address in arptab.
801  */
802 static struct llinfo_arp *
803 arplookup(addr, create, proxy)
804 	u_long addr;
805 	int create, proxy;
806 {
807 	struct rtentry *rt;
808 	struct sockaddr_inarp sin;
809 	const char *why = 0;
810 
811 	bzero(&sin, sizeof(sin));
812 	sin.sin_len = sizeof(sin);
813 	sin.sin_family = AF_INET;
814 	sin.sin_addr.s_addr = addr;
815 	if (proxy)
816 		sin.sin_other = SIN_PROXY;
817 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
818 	if (rt == 0)
819 		return (0);
820 
821 	if (rt->rt_flags & RTF_GATEWAY)
822 		why = "host is not on local network";
823 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
824 		why = "could not allocate llinfo";
825 	else if (rt->rt_gateway->sa_family != AF_LINK)
826 		why = "gateway route is not ours";
827 
828 	if (why) {
829 #define	ISDYNCLONE(_rt) \
830 	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
831 		if (create)
832 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
833 			    inet_ntoa(sin.sin_addr), why);
834 		/*
835 		 * If there are no references to this Layer 2 route,
836 		 * and it is a cloned route, and not static, and
837 		 * arplookup() is creating the route, then purge
838 		 * it from the routing table as it is probably bogus.
839 		 */
840 		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
841 			rtexpunge(rt);
842 		RTFREE_LOCKED(rt);
843 		return (0);
844 #undef ISDYNCLONE
845 	} else {
846 		RT_REMREF(rt);
847 		RT_UNLOCK(rt);
848 		return ((struct llinfo_arp *)rt->rt_llinfo);
849 	}
850 }
851 
852 void
853 arp_ifinit(ifp, ifa)
854 	struct ifnet *ifp;
855 	struct ifaddr *ifa;
856 {
857 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
858 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
859 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
860 	ifa->ifa_rtrequest = arp_rtrequest;
861 	ifa->ifa_flags |= RTF_CLONING;
862 }
863 
864 static void
865 arp_init(void)
866 {
867 
868 	arpintrq.ifq_maxlen = 50;
869 	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
870 	LIST_INIT(&llinfo_arp);
871 	callout_init(&arp_callout, CALLOUT_MPSAFE);
872 	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
873 	callout_reset(&arp_callout, hz, arptimer, NULL);
874 }
875 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
876