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