xref: /freebsd/sys/netinet/if_ether.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
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 	u_short	la_preempt;	/* #times we QUERIED before entry expiration */
101 	u_short	la_asked;	/* #times we QUERIED following expiration */
102 #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
103 };
104 
105 static	LIST_HEAD(, llinfo_arp) llinfo_arp;
106 
107 struct	ifqueue arpintrq;
108 static int	arp_inuse, arp_allocated, arpinit_done;
109 
110 static int	arp_maxtries = 5;
111 static int	useloopback = 1; /* use loopback interface for local traffic */
112 static int	arp_proxyall = 0;
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(void);
126 static void	arptfree(struct llinfo_arp *);
127 static void	arptimer(void *);
128 static struct llinfo_arp
129 		*arplookup(u_long, int, int);
130 #ifdef INET
131 static void	in_arpinput(struct mbuf *);
132 #endif
133 
134 /*
135  * Timeout routine.  Age arp_tab entries periodically.
136  */
137 /* ARGSUSED */
138 static void
139 arptimer(ignored_arg)
140 	void *ignored_arg;
141 {
142 	struct llinfo_arp *la, *ola;
143 	int s = splnet();
144 
145 	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
146 	la = LIST_FIRST(&llinfo_arp);
147 	while (la != NULL) {
148 		struct rtentry *rt = la->la_rt;
149 		ola = la;
150 		la = LIST_NEXT(la, la_le);
151 		if (rt->rt_expire && rt->rt_expire <= time_second)
152 			arptfree(ola);		/* timer has expired, clear */
153 	}
154 	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
155 	splx(s);
156 	timeout(arptimer, NULL, arpt_prune * hz);
157 }
158 
159 /*
160  * Parallel to llc_rtrequest.
161  */
162 static void
163 arp_rtrequest(req, rt, info)
164 	int req;
165 	register struct rtentry *rt;
166 	struct rt_addrinfo *info;
167 {
168 	register struct sockaddr *gate = rt->rt_gateway;
169 	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
170 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
171 
172 	if (!arpinit_done) {
173 		arpinit_done = 1;
174 		timeout(arptimer, (caddr_t)0, hz);
175 	}
176 	if (rt->rt_flags & RTF_GATEWAY)
177 		return;
178 	switch (req) {
179 
180 	case RTM_ADD:
181 		/*
182 		 * XXX: If this is a manually added route to interface
183 		 * such as older version of routed or gated might provide,
184 		 * restore cloning bit.
185 		 */
186 		if ((rt->rt_flags & RTF_HOST) == 0 &&
187 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
188 			rt->rt_flags |= RTF_CLONING;
189 		if (rt->rt_flags & RTF_CLONING) {
190 			/*
191 			 * Case 1: This route should come from a route to iface.
192 			 */
193 			rt_setgate(rt, rt_key(rt),
194 					(struct sockaddr *)&null_sdl);
195 			gate = rt->rt_gateway;
196 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
197 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
198 			rt->rt_expire = time_second;
199 			break;
200 		}
201 		/* Announce a new entry if requested. */
202 		if (rt->rt_flags & RTF_ANNOUNCE)
203 			arprequest(rt->rt_ifp,
204 			    &SIN(rt_key(rt))->sin_addr,
205 			    &SIN(rt_key(rt))->sin_addr,
206 			    (u_char *)LLADDR(SDL(gate)));
207 		/*FALLTHROUGH*/
208 	case RTM_RESOLVE:
209 		if (gate->sa_family != AF_LINK ||
210 		    gate->sa_len < sizeof(null_sdl)) {
211 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
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_Malloc(la, struct llinfo_arp *, sizeof(*la));
223 		rt->rt_llinfo = (caddr_t)la;
224 		if (la == 0) {
225 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
226 			break;
227 		}
228 		arp_inuse++, arp_allocated++;
229 		Bzero(la, sizeof(*la));
230 		la->la_rt = rt;
231 		rt->rt_flags |= RTF_LLINFO;
232 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
233 		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
234 
235 #ifdef INET
236 		/*
237 		 * This keeps the multicast addresses from showing up
238 		 * in `arp -a' listings as unresolved.  It's not actually
239 		 * functional.  Then the same for broadcast.
240 		 */
241 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
242 		    rt->rt_ifp->if_type != IFT_ARCNET) {
243 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
244 					       LLADDR(SDL(gate)));
245 			SDL(gate)->sdl_alen = 6;
246 			rt->rt_expire = 0;
247 		}
248 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
249 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
250 			       rt->rt_ifp->if_addrlen);
251 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
252 			rt->rt_expire = 0;
253 		}
254 #endif
255 
256 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
257 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
258 		    /*
259 		     * This test used to be
260 		     *	if (loif.if_flags & IFF_UP)
261 		     * It allowed local traffic to be forced
262 		     * through the hardware by configuring the loopback down.
263 		     * However, it causes problems during network configuration
264 		     * for boards that can't receive packets they send.
265 		     * It is now necessary to clear "useloopback" and remove
266 		     * the route to force traffic out to the hardware.
267 		     */
268 			rt->rt_expire = 0;
269 			Bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
270 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
271 			if (useloopback)
272 				rt->rt_ifp = loif;
273 
274 		}
275 		break;
276 
277 	case RTM_DELETE:
278 		if (la == 0)
279 			break;
280 		arp_inuse--;
281 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
282 		LIST_REMOVE(la, la_le);
283 		rt->rt_llinfo = 0;
284 		rt->rt_flags &= ~RTF_LLINFO;
285 		if (la->la_hold)
286 			m_freem(la->la_hold);
287 		Free((caddr_t)la);
288 	}
289 }
290 
291 /*
292  * Broadcast an ARP request. Caller specifies:
293  *	- arp header source ip address
294  *	- arp header target ip address
295  *	- arp header source ethernet address
296  */
297 static void
298 arprequest(ifp, sip, tip, enaddr)
299 	register struct ifnet *ifp;
300 	register struct in_addr *sip, *tip;
301 	register u_char *enaddr;
302 {
303 	register struct mbuf *m;
304 	register struct ether_header *eh;
305 	register struct arc_header *arh;
306 	register struct arphdr *ah;
307 	struct sockaddr sa;
308 	static u_char	llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
309 				   LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
310 	u_short ar_hrd;
311 
312 	if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
313 		return;
314 	m->m_pkthdr.rcvif = (struct ifnet *)0;
315 #ifdef MAC
316 	mac_create_mbuf_linklayer(ifp, m);
317 #endif
318 	switch (ifp->if_type) {
319 	case IFT_ARCNET:
320 		ar_hrd = htons(ARPHRD_ARCNET);
321 
322 		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
323 		m->m_pkthdr.len = m->m_len;
324 		MH_ALIGN(m, m->m_len);
325 
326 		arh = (struct arc_header *)sa.sa_data;
327 		arh->arc_dhost = *ifp->if_broadcastaddr;
328 		arh->arc_type = ARCTYPE_ARP;
329 
330 		ah = mtod(m, struct arphdr *);
331 		break;
332 
333 	case IFT_ISO88025:
334 		ar_hrd = htons(ARPHRD_IEEE802);
335 
336 		m->m_len = sizeof(llcx) +
337 		    arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
338 		m->m_pkthdr.len = m->m_len;
339 		MH_ALIGN(m, m->m_len);
340 
341 		(void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx));
342 		(void)memcpy(sa.sa_data, ifp->if_broadcastaddr, 6);
343 		(void)memcpy(sa.sa_data + 6, enaddr, 6);
344 		sa.sa_data[6] |= TR_RII;
345 		sa.sa_data[12] = TR_AC;
346 		sa.sa_data[13] = TR_LLC_FRAME;
347 
348 		ah = (struct arphdr *)(mtod(m, char *) + sizeof(llcx));
349 		break;
350 	case IFT_FDDI:
351 	case IFT_ETHER:
352 		/*
353 		 * This may not be correct for types not explicitly
354 		 * listed, but this is our best guess
355 		 */
356 	default:
357 		ar_hrd = htons(ARPHRD_ETHER);
358 
359 		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
360 		m->m_pkthdr.len = m->m_len;
361 		MH_ALIGN(m, m->m_len);
362 
363 		eh = (struct ether_header *)sa.sa_data;
364 		/* if_output will not swap */
365 		eh->ether_type = htons(ETHERTYPE_ARP);
366 		(void)memcpy(eh->ether_dhost, ifp->if_broadcastaddr,
367 		    sizeof(eh->ether_dhost));
368 
369 		ah = mtod(m, struct arphdr *);
370 		break;
371 	}
372 
373 	ah->ar_hrd = ar_hrd;
374 	ah->ar_pro = htons(ETHERTYPE_IP);
375 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
376 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
377 	ah->ar_op = htons(ARPOP_REQUEST);
378 	(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
379 	memset(ar_tha(ah), 0, ah->ar_hln);
380 	(void)memcpy(ar_spa(ah), sip, ah->ar_pln);
381 	(void)memcpy(ar_tpa(ah), tip, ah->ar_pln);
382 
383 	sa.sa_family = AF_UNSPEC;
384 	sa.sa_len = sizeof(sa);
385 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
386 }
387 
388 /*
389  * Resolve an IP address into an ethernet address.  If success,
390  * desten is filled in.  If there is no entry in arptab,
391  * set one up and broadcast a request for the IP address.
392  * Hold onto this mbuf and resend it once the address
393  * is finally resolved.  A return value of 1 indicates
394  * that desten has been filled in and the packet should be sent
395  * normally; a 0 return indicates that the packet has been
396  * taken over here, either now or for later transmission.
397  */
398 int
399 arpresolve(ifp, rt, m, dst, desten, rt0)
400 	register struct ifnet *ifp;
401 	register struct rtentry *rt;
402 	struct mbuf *m;
403 	register struct sockaddr *dst;
404 	register u_char *desten;
405 	struct rtentry *rt0;
406 {
407 	struct llinfo_arp *la = 0;
408 	struct sockaddr_dl *sdl;
409 
410 	if (m->m_flags & M_BCAST) {	/* broadcast */
411 		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
412 		return (1);
413 	}
414 	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
415 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
416 		return(1);
417 	}
418 	if (rt)
419 		la = (struct llinfo_arp *)rt->rt_llinfo;
420 	if (la == 0) {
421 		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
422 		if (la)
423 			rt = la->la_rt;
424 	}
425 	if (la == 0 || rt == 0) {
426 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
427 			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
428 				rt ? "rt" : "");
429 		m_freem(m);
430 		return (0);
431 	}
432 	sdl = SDL(rt->rt_gateway);
433 	/*
434 	 * Check the address family and length is valid, the address
435 	 * is resolved; otherwise, try to resolve.
436 	 */
437 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
438 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
439 		/*
440 		 * If entry has an expiry time and it is approaching,
441 		 * see if we need to send an ARP request within this
442 		 * arpt_down interval.
443 		 */
444 		if ((rt->rt_expire != 0) &&
445 		    (time_second + (arp_maxtries - la->la_preempt) * arpt_down
446 		     > rt->rt_expire)) {
447 			arprequest(ifp,
448 				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
449 				   &SIN(dst)->sin_addr,
450 				   IF_LLADDR(ifp));
451 			la->la_preempt++;
452 		}
453 
454 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
455 		return 1;
456 	}
457 	/*
458 	 * If ARP is disabled on this interface, stop.
459 	 * XXX
460 	 * Probably should not allocate empty llinfo struct if we are
461 	 * not going to be sending out an arp request.
462 	 */
463 	if (ifp->if_flags & IFF_NOARP) {
464 		m_freem(m);
465 		return (0);
466 	}
467 	/*
468 	 * There is an arptab entry, but no ethernet address
469 	 * response yet.  Replace the held mbuf with this
470 	 * latest one.
471 	 */
472 	if (la->la_hold)
473 		m_freem(la->la_hold);
474 	la->la_hold = m;
475 	if (rt->rt_expire) {
476 		rt->rt_flags &= ~RTF_REJECT;
477 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
478 			rt->rt_expire = time_second;
479 			if (la->la_asked++ < arp_maxtries) {
480 				arprequest(ifp,
481 					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
482 					   &SIN(dst)->sin_addr,
483 					   IF_LLADDR(ifp));
484 			} else {
485 				rt->rt_flags |= RTF_REJECT;
486 				rt->rt_expire += arpt_down;
487 				la->la_preempt = la->la_asked = 0;
488 			}
489 
490 		}
491 	}
492 	return (0);
493 }
494 
495 /*
496  * Common length and type checks are done here,
497  * then the protocol-specific routine is called.
498  */
499 static void
500 arpintr()
501 {
502 	register struct mbuf *m;
503 	register struct arphdr *ar;
504 	int s;
505 
506 	if (!arpinit_done) {
507 		arpinit_done = 1;
508 		timeout(arptimer, (caddr_t)0, hz);
509 	}
510 	while (arpintrq.ifq_head) {
511 		s = splimp();
512 		IF_DEQUEUE(&arpintrq, m);
513 		splx(s);
514 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
515 			panic("arpintr");
516 
517                 if (m->m_len < sizeof(struct arphdr) &&
518                     ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
519 			log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
520 			continue;
521 		}
522 		ar = mtod(m, struct arphdr *);
523 
524 		if (ntohs(ar->ar_hrd) != ARPHRD_ETHER
525 		    && ntohs(ar->ar_hrd) != ARPHRD_IEEE802
526 		    && ntohs(ar->ar_hrd) != ARPHRD_ARCNET) {
527 			log(LOG_ERR,
528 			    "arp: unknown hardware address format (0x%2D)\n",
529 			    (unsigned char *)&ar->ar_hrd, "");
530 			m_freem(m);
531 			continue;
532 		}
533 
534 		if (m->m_pkthdr.len < arphdr_len(ar) &&
535 		    (m = m_pullup(m, arphdr_len(ar))) == NULL) {
536 			log(LOG_ERR, "arp: runt packet\n");
537 			m_freem(m);
538 			continue;
539 		}
540 
541 		switch (ntohs(ar->ar_pro)) {
542 #ifdef INET
543 			case ETHERTYPE_IP:
544 				in_arpinput(m);
545 				continue;
546 #endif
547 		}
548 		m_freem(m);
549 	}
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 	register struct arphdr *ah;
583 	register struct ifnet *ifp = m->m_pkthdr.rcvif;
584 	struct ether_header *eh;
585 	struct arc_header *arh;
586 	struct iso88025_header *th = (struct iso88025_header *)0;
587 	struct iso88025_sockaddr_dl_data *trld;
588 	register struct llinfo_arp *la = 0;
589 	register struct rtentry *rt;
590 	struct ifaddr *ifa;
591 	struct in_ifaddr *ia;
592 	struct sockaddr_dl *sdl;
593 	struct sockaddr sa;
594 	struct in_addr isaddr, itaddr, myaddr;
595 	int op, rif_len;
596 	int req_len;
597 
598 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
599 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
600 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
601 		return;
602 	}
603 
604 	ah = mtod(m, struct arphdr *);
605 	op = ntohs(ah->ar_op);
606 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
607 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
608 #ifdef BRIDGE
609 #define BRIDGE_TEST (do_bridge)
610 #else
611 #define BRIDGE_TEST (0) /* cc will optimise the test away */
612 #endif
613 	/*
614 	 * For a bridge, we want to check the address irrespective
615 	 * of the receive interface. (This will change slightly
616 	 * when we have clusters of interfaces).
617 	 */
618 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash)
619 		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
620 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
621 			goto match;
622 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
623 		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
624 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
625 			goto match;
626 	/*
627 	 * No match, use the first inet address on the receive interface
628 	 * as a dummy address for the rest of the function.
629 	 */
630 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
631 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
632 			ia = ifatoia(ifa);
633 			goto match;
634 		}
635 	/*
636 	 * If bridging, fall back to using any inet address.
637 	 */
638 	if (!BRIDGE_TEST ||
639 	    (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) {
640 		m_freem(m);
641 		return;
642 	}
643 match:
644 	myaddr = ia->ia_addr.sin_addr;
645 	if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) {
646 		m_freem(m);	/* it's from me, ignore it. */
647 		return;
648 	}
649 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
650 		log(LOG_ERR,
651 		    "arp: link address is broadcast for IP address %s!\n",
652 		    inet_ntoa(isaddr));
653 		m_freem(m);
654 		return;
655 	}
656 	if (isaddr.s_addr == myaddr.s_addr) {
657 		log(LOG_ERR,
658 		   "arp: %*D is using my IP address %s!\n",
659 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
660 		   inet_ntoa(isaddr));
661 		itaddr = myaddr;
662 		goto reply;
663 	}
664 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
665 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
666 		/* the following is not an error when doing bridging */
667 		if (!BRIDGE_TEST && rt->rt_ifp != ifp) {
668 			if (log_arp_wrong_iface)
669 				log(LOG_ERR, "arp: %s is on %s%d but got reply from %*D on %s%d\n",
670 				    inet_ntoa(isaddr),
671 				    rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
672 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
673 				    ifp->if_name, ifp->if_unit);
674 			goto reply;
675 		}
676 		if (sdl->sdl_alen &&
677 		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
678 			if (rt->rt_expire) {
679 			    if (log_arp_movements)
680 			        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s%d\n",
681 				    inet_ntoa(isaddr),
682 				    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
683 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
684 				    ifp->if_name, ifp->if_unit);
685 			} else {
686 			    log(LOG_ERR,
687 				"arp: %*D attempts to modify permanent entry for %s on %s%d\n",
688 				ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
689 				inet_ntoa(isaddr), ifp->if_name, ifp->if_unit);
690 			    goto reply;
691 			}
692 		}
693 		/*
694 		 * sanity check for the address length.
695 		 * XXX this does not work for protocols with variable address
696 		 * length. -is
697 		 */
698 		if (sdl->sdl_alen &&
699 		    sdl->sdl_alen != ah->ar_hln) {
700 			log(LOG_WARNING,
701 			    "arp from %*D: new addr len %d, was %d",
702 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
703 			    ah->ar_hln, sdl->sdl_alen);
704 		}
705 		if (ifp->if_addrlen != ah->ar_hln) {
706 			log(LOG_WARNING,
707 			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
708 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
709 			    ah->ar_hln, ifp->if_addrlen);
710 			goto reply;
711 		}
712 		(void)memcpy(LLADDR(sdl), ar_sha(ah),
713 		    sdl->sdl_alen = ah->ar_hln);
714 		/*
715 		 * If we receive an arp from a token-ring station over
716 		 * a token-ring nic then try to save the source
717 		 * routing info.
718 		 */
719 		if (ifp->if_type == IFT_ISO88025) {
720 			th = (struct iso88025_header *)m->m_pkthdr.header;
721 			trld = SDL_ISO88025(sdl);
722 			rif_len = TR_RCF_RIFLEN(th->rcf);
723 			if ((th->iso88025_shost[0] & TR_RII) &&
724 			    (rif_len > 2)) {
725 				trld->trld_rcf = th->rcf;
726 				trld->trld_rcf ^= htons(TR_RCF_DIR);
727 				memcpy(trld->trld_route, th->rd, rif_len - 2);
728 				trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
729 				/*
730 				 * Set up source routing information for
731 				 * reply packet (XXX)
732 				 */
733 				m->m_data -= rif_len;
734 				m->m_len  += rif_len;
735 				m->m_pkthdr.len += rif_len;
736 			} else {
737 				th->iso88025_shost[0] &= ~TR_RII;
738 				trld->trld_rcf = 0;
739 			}
740 			m->m_data -= 8;
741 			m->m_len  += 8;
742 			m->m_pkthdr.len += 8;
743 			th->rcf = trld->trld_rcf;
744 		}
745 		if (rt->rt_expire)
746 			rt->rt_expire = time_second + arpt_keep;
747 		rt->rt_flags &= ~RTF_REJECT;
748 		la->la_preempt = la->la_asked = 0;
749 		if (la->la_hold) {
750 			(*ifp->if_output)(ifp, la->la_hold,
751 				rt_key(rt), rt);
752 			la->la_hold = 0;
753 		}
754 	}
755 reply:
756 	if (op != ARPOP_REQUEST) {
757 		m_freem(m);
758 		return;
759 	}
760 	if (itaddr.s_addr == myaddr.s_addr) {
761 		/* I am the target */
762 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
763 		(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
764 	} else {
765 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
766 		if (la == NULL) {
767 			struct sockaddr_in sin;
768 
769 			if (!arp_proxyall) {
770 				m_freem(m);
771 				return;
772 			}
773 
774 			bzero(&sin, sizeof sin);
775 			sin.sin_family = AF_INET;
776 			sin.sin_len = sizeof sin;
777 			sin.sin_addr = itaddr;
778 
779 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
780 			if (!rt) {
781 				m_freem(m);
782 				return;
783 			}
784 			/*
785 			 * Don't send proxies for nodes on the same interface
786 			 * as this one came out of, or we'll get into a fight
787 			 * over who claims what Ether address.
788 			 */
789 			if (rt->rt_ifp == ifp) {
790 				rtfree(rt);
791 				m_freem(m);
792 				return;
793 			}
794 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
795 			(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
796 			rtfree(rt);
797 
798 			/*
799 			 * Also check that the node which sent the ARP packet
800 			 * is on the the interface we expect it to be on. This
801 			 * avoids ARP chaos if an interface is connected to the
802 			 * wrong network.
803 			 */
804 			sin.sin_addr = isaddr;
805 
806 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
807 			if (!rt) {
808 				m_freem(m);
809 				return;
810 			}
811 			if (rt->rt_ifp != ifp) {
812 				log(LOG_INFO, "arp_proxy: ignoring request"
813 				    " from %s via %s%d, expecting %s%d\n",
814 				    inet_ntoa(isaddr), ifp->if_name,
815 				    ifp->if_unit, rt->rt_ifp->if_name,
816 				    rt->rt_ifp->if_unit);
817 				rtfree(rt);
818 				m_freem(m);
819 				return;
820 			}
821 			rtfree(rt);
822 
823 #ifdef DEBUG_PROXY
824 			printf("arp: proxying for %s\n",
825 			       inet_ntoa(itaddr));
826 #endif
827 		} else {
828 			rt = la->la_rt;
829 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
830 			sdl = SDL(rt->rt_gateway);
831 			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
832 		}
833 	}
834 
835 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
836 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
837 	ah->ar_op = htons(ARPOP_REPLY);
838 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
839 	switch (ifp->if_type) {
840 	case IFT_ARCNET:
841 		arh = (struct arc_header *)sa.sa_data;
842 		arh->arc_dhost = *ar_tha(ah);
843 		arh->arc_type = ARCTYPE_ARP;
844 		break;
845 
846 	case IFT_ISO88025:
847 		/* Re-arrange the source/dest address */
848 		memcpy(th->iso88025_dhost, th->iso88025_shost,
849 		    sizeof(th->iso88025_dhost));
850 		memcpy(th->iso88025_shost, IF_LLADDR(ifp),
851 		    sizeof(th->iso88025_shost));
852 		/* Set the source routing bit if neccesary */
853 		if (th->iso88025_dhost[0] & TR_RII) {
854 			th->iso88025_dhost[0] &= ~TR_RII;
855 			if (TR_RCF_RIFLEN(th->rcf) > 2)
856 				th->iso88025_shost[0] |= TR_RII;
857 		}
858 		/* Copy the addresses, ac and fc into sa_data */
859 		memcpy(sa.sa_data, th->iso88025_dhost,
860 		    sizeof(th->iso88025_dhost) * 2);
861 		sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC;
862 		sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME;
863 		break;
864 	case IFT_ETHER:
865 	case IFT_FDDI:
866 	/*
867 	 * May not be correct for types not explictly
868 	 * listed, but it is our best guess.
869 	 */
870 	default:
871 		eh = (struct ether_header *)sa.sa_data;
872 		(void)memcpy(eh->ether_dhost, ar_tha(ah),
873 		    sizeof(eh->ether_dhost));
874 		eh->ether_type = htons(ETHERTYPE_ARP);
875 		break;
876 	}
877 	sa.sa_family = AF_UNSPEC;
878 	sa.sa_len = sizeof(sa);
879 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
880 	return;
881 }
882 #endif
883 
884 /*
885  * Free an arp entry.
886  */
887 static void
888 arptfree(la)
889 	register struct llinfo_arp *la;
890 {
891 	register struct rtentry *rt = la->la_rt;
892 	register struct sockaddr_dl *sdl;
893 	if (rt == 0)
894 		panic("arptfree");
895 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
896 	    sdl->sdl_family == AF_LINK) {
897 		sdl->sdl_alen = 0;
898 		la->la_preempt = la->la_asked = 0;
899 		rt->rt_flags &= ~RTF_REJECT;
900 		return;
901 	}
902 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
903 			0, (struct rtentry **)0);
904 }
905 /*
906  * Lookup or enter a new address in arptab.
907  */
908 static struct llinfo_arp *
909 arplookup(addr, create, proxy)
910 	u_long addr;
911 	int create, proxy;
912 {
913 	register struct rtentry *rt;
914 	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
915 	const char *why = 0;
916 
917 	sin.sin_addr.s_addr = addr;
918 	sin.sin_other = proxy ? SIN_PROXY : 0;
919 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
920 	if (rt == 0)
921 		return (0);
922 	rt->rt_refcnt--;
923 
924 	if (rt->rt_flags & RTF_GATEWAY)
925 		why = "host is not on local network";
926 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
927 		why = "could not allocate llinfo";
928 	else if (rt->rt_gateway->sa_family != AF_LINK)
929 		why = "gateway route is not ours";
930 
931 	if (why && create) {
932 		log(LOG_DEBUG, "arplookup %s failed: %s\n",
933 		    inet_ntoa(sin.sin_addr), why);
934 		return 0;
935 	} else if (why) {
936 		return 0;
937 	}
938 	return ((struct llinfo_arp *)rt->rt_llinfo);
939 }
940 
941 void
942 arp_ifinit(ifp, ifa)
943 	struct ifnet *ifp;
944 	struct ifaddr *ifa;
945 {
946 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
947 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
948 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
949 	ifa->ifa_rtrequest = arp_rtrequest;
950 	ifa->ifa_flags |= RTF_CLONING;
951 }
952 
953 static void
954 arp_init(void)
955 {
956 
957 	arpintrq.ifq_maxlen = 50;
958 	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
959 	LIST_INIT(&llinfo_arp);
960 	register_netisr(NETISR_ARP, arpintr);
961 }
962 
963 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
964