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