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