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