xref: /freebsd/sys/netinet/if_ether.c (revision 2ef4a436e02cf7ae21bd2bd86cf0ad407be7b6d9)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33df8bae1dSRodney W. Grimes /*
34df8bae1dSRodney W. Grimes  * Ethernet address resolution protocol.
35df8bae1dSRodney W. Grimes  * TODO:
36df8bae1dSRodney W. Grimes  *	add "inuse/lock" bit (or ref. count) along with valid bit
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
391d5e9e22SEivind Eklund #include "opt_inet.h"
40b715f178SLuigi Rizzo #include "opt_bdg.h"
4119527d3eSRobert Watson #include "opt_mac.h"
42a9771948SGleb Smirnoff #include "opt_carp.h"
431d5e9e22SEivind Eklund 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/kernel.h>
46ce02431fSDoug Rabson #include <sys/queue.h>
47885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h>
48885f1aa4SPoul-Henning Kamp #include <sys/systm.h>
4919527d3eSRobert Watson #include <sys/mac.h>
50885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h>
51885f1aa4SPoul-Henning Kamp #include <sys/malloc.h>
524458ac71SBruce Evans #include <sys/socket.h>
53885f1aa4SPoul-Henning Kamp #include <sys/syslog.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <net/if.h>
56df8bae1dSRodney W. Grimes #include <net/if_dl.h>
57722012ccSJulian Elischer #include <net/if_types.h>
58df8bae1dSRodney W. Grimes #include <net/route.h>
59748e0b0aSGarrett Wollman #include <net/netisr.h>
60b149dd6cSLarry Lile #include <net/if_llc.h>
61c8f8e9c1SJulian Elischer #include <net/ethernet.h>
62c8f8e9c1SJulian Elischer #include <net/bridge.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
67df8bae1dSRodney W. Grimes 
68322dcb8dSMax Khon #include <net/if_arc.h>
69fda82fc2SJulian Elischer #include <net/iso88025.h>
70fda82fc2SJulian Elischer 
71a9771948SGleb Smirnoff #ifdef DEV_CARP
72a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
73a9771948SGleb Smirnoff #endif
74a9771948SGleb Smirnoff 
75df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s)
76df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s)
77df8bae1dSRodney W. Grimes 
78ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether);
79602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes /* timer values */
82602d513cSGarrett Wollman static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
83602d513cSGarrett Wollman static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
84602d513cSGarrett Wollman static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
85885f1aa4SPoul-Henning Kamp 
86602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
87602d513cSGarrett Wollman 	   &arpt_prune, 0, "");
88602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
89602d513cSGarrett Wollman 	   &arpt_keep, 0, "");
90602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
91602d513cSGarrett Wollman 	   &arpt_down, 0, "");
92885f1aa4SPoul-Henning Kamp 
93df8bae1dSRodney W. Grimes #define	rt_expire rt_rmx.rmx_expire
94df8bae1dSRodney W. Grimes 
9566800f57SGarrett Wollman struct llinfo_arp {
96e3975643SJake Burkholder 	LIST_ENTRY(llinfo_arp) la_le;
9766800f57SGarrett Wollman 	struct	rtentry *la_rt;
9866800f57SGarrett Wollman 	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
99022695f8SOrion Hodson 	u_short	la_preempt;	/* countdown for pre-expiry arps */
10073224fb0SOrion Hodson 	u_short	la_asked;	/* #times we QUERIED following expiration */
10166800f57SGarrett Wollman #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
10266800f57SGarrett Wollman };
10366800f57SGarrett Wollman 
104e3975643SJake Burkholder static	LIST_HEAD(, llinfo_arp) llinfo_arp;
105df8bae1dSRodney W. Grimes 
1061cafed39SJonathan Lemon static struct	ifqueue arpintrq;
107d1dd20beSSam Leffler static int	arp_allocated;
108df8bae1dSRodney W. Grimes 
109885f1aa4SPoul-Henning Kamp static int	arp_maxtries = 5;
110602d513cSGarrett Wollman static int	useloopback = 1; /* use loopback interface for local traffic */
111885f1aa4SPoul-Henning Kamp static int	arp_proxyall = 0;
112d1dd20beSSam Leffler static struct callout arp_callout;
113602d513cSGarrett Wollman 
114602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
115602d513cSGarrett Wollman 	   &arp_maxtries, 0, "");
116602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
117602d513cSGarrett Wollman 	   &useloopback, 0, "");
118602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
119602d513cSGarrett Wollman 	   &arp_proxyall, 0, "");
120885f1aa4SPoul-Henning Kamp 
1214d77a549SAlfred Perlstein static void	arp_init(void);
1224d77a549SAlfred Perlstein static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
1234d77a549SAlfred Perlstein static void	arprequest(struct ifnet *,
1244d77a549SAlfred Perlstein 			struct in_addr *, struct in_addr *, u_char *);
1251cafed39SJonathan Lemon static void	arpintr(struct mbuf *);
1264d77a549SAlfred Perlstein static void	arptfree(struct llinfo_arp *);
1274d77a549SAlfred Perlstein static void	arptimer(void *);
128885f1aa4SPoul-Henning Kamp static struct llinfo_arp
1294d77a549SAlfred Perlstein 		*arplookup(u_long, int, int);
1301d5e9e22SEivind Eklund #ifdef INET
1314d77a549SAlfred Perlstein static void	in_arpinput(struct mbuf *);
1321d5e9e22SEivind Eklund #endif
13328e82295SGarrett Wollman 
134df8bae1dSRodney W. Grimes /*
135df8bae1dSRodney W. Grimes  * Timeout routine.  Age arp_tab entries periodically.
136df8bae1dSRodney W. Grimes  */
137df8bae1dSRodney W. Grimes /* ARGSUSED */
138df8bae1dSRodney W. Grimes static void
139df8bae1dSRodney W. Grimes arptimer(ignored_arg)
140df8bae1dSRodney W. Grimes 	void *ignored_arg;
141df8bae1dSRodney W. Grimes {
142c996428cSJeffrey Hsu 	struct llinfo_arp *la, *ola;
143df8bae1dSRodney W. Grimes 
14493f79889SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
145c996428cSJeffrey Hsu 	la = LIST_FIRST(&llinfo_arp);
146c996428cSJeffrey Hsu 	while (la != NULL) {
147c996428cSJeffrey Hsu 		struct rtentry *rt = la->la_rt;
148c996428cSJeffrey Hsu 		ola = la;
149fc2ffbe6SPoul-Henning Kamp 		la = LIST_NEXT(la, la_le);
150227ee8a1SPoul-Henning Kamp 		if (rt->rt_expire && rt->rt_expire <= time_second)
15166800f57SGarrett Wollman 			arptfree(ola);		/* timer has expired, clear */
152df8bae1dSRodney W. Grimes 	}
15393f79889SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
154d1dd20beSSam Leffler 
155d1dd20beSSam Leffler 	callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
156df8bae1dSRodney W. Grimes }
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes /*
159df8bae1dSRodney W. Grimes  * Parallel to llc_rtrequest.
160df8bae1dSRodney W. Grimes  */
161b2774d00SGarrett Wollman static void
1628071913dSRuslan Ermilov arp_rtrequest(req, rt, info)
163df8bae1dSRodney W. Grimes 	int req;
164e952fa39SMatthew N. Dodd 	struct rtentry *rt;
1658071913dSRuslan Ermilov 	struct rt_addrinfo *info;
166df8bae1dSRodney W. Grimes {
167e952fa39SMatthew N. Dodd 	struct sockaddr *gate;
168e952fa39SMatthew N. Dodd 	struct llinfo_arp *la;
169df8bae1dSRodney W. Grimes 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
170067a8babSMax Laier 	struct in_ifaddr *ia;
171067a8babSMax Laier 	struct ifaddr *ifa;
172df8bae1dSRodney W. Grimes 
173d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
174d1dd20beSSam Leffler 
175df8bae1dSRodney W. Grimes 	if (rt->rt_flags & RTF_GATEWAY)
176df8bae1dSRodney W. Grimes 		return;
177d1dd20beSSam Leffler 	gate = rt->rt_gateway;
178d1dd20beSSam Leffler 	la = (struct llinfo_arp *)rt->rt_llinfo;
179df8bae1dSRodney W. Grimes 	switch (req) {
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes 	case RTM_ADD:
182df8bae1dSRodney W. Grimes 		/*
183df8bae1dSRodney W. Grimes 		 * XXX: If this is a manually added route to interface
184df8bae1dSRodney W. Grimes 		 * such as older version of routed or gated might provide,
185df8bae1dSRodney W. Grimes 		 * restore cloning bit.
186df8bae1dSRodney W. Grimes 		 */
187df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_HOST) == 0 &&
188d6fa5d28SBruce M Simpson 		    rt_mask(rt) != NULL &&
189df8bae1dSRodney W. Grimes 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
190df8bae1dSRodney W. Grimes 			rt->rt_flags |= RTF_CLONING;
191df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_CLONING) {
192df8bae1dSRodney W. Grimes 			/*
193df8bae1dSRodney W. Grimes 			 * Case 1: This route should come from a route to iface.
194df8bae1dSRodney W. Grimes 			 */
195df8bae1dSRodney W. Grimes 			rt_setgate(rt, rt_key(rt),
196df8bae1dSRodney W. Grimes 					(struct sockaddr *)&null_sdl);
197df8bae1dSRodney W. Grimes 			gate = rt->rt_gateway;
198df8bae1dSRodney W. Grimes 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
199df8bae1dSRodney W. Grimes 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
200227ee8a1SPoul-Henning Kamp 			rt->rt_expire = time_second;
201df8bae1dSRodney W. Grimes 			break;
202df8bae1dSRodney W. Grimes 		}
203df8bae1dSRodney W. Grimes 		/* Announce a new entry if requested. */
204df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_ANNOUNCE)
205322dcb8dSMax Khon 			arprequest(rt->rt_ifp,
206ed7509acSJulian Elischer 			    &SIN(rt_key(rt))->sin_addr,
207ed7509acSJulian Elischer 			    &SIN(rt_key(rt))->sin_addr,
208df8bae1dSRodney W. Grimes 			    (u_char *)LLADDR(SDL(gate)));
209df8bae1dSRodney W. Grimes 		/*FALLTHROUGH*/
210df8bae1dSRodney W. Grimes 	case RTM_RESOLVE:
211df8bae1dSRodney W. Grimes 		if (gate->sa_family != AF_LINK ||
212df8bae1dSRodney W. Grimes 		    gate->sa_len < sizeof(null_sdl)) {
213d1dd20beSSam Leffler 			log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
214beb2ced8SBruce M Simpson 			    inet_ntoa(SIN(rt_key(rt))->sin_addr),
215beb2ced8SBruce M Simpson 			    (gate->sa_family != AF_LINK) ?
216c3b52d64SBruce M Simpson 			    " (!AF_LINK)": "");
217df8bae1dSRodney W. Grimes 			break;
218df8bae1dSRodney W. Grimes 		}
219df8bae1dSRodney W. Grimes 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
220df8bae1dSRodney W. Grimes 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
221df8bae1dSRodney W. Grimes 		if (la != 0)
222df8bae1dSRodney W. Grimes 			break; /* This happens on a route change */
223df8bae1dSRodney W. Grimes 		/*
224df8bae1dSRodney W. Grimes 		 * Case 2:  This route may come from cloning, or a manual route
225df8bae1dSRodney W. Grimes 		 * add with a LL address.
226df8bae1dSRodney W. Grimes 		 */
227d1dd20beSSam Leffler 		R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
228df8bae1dSRodney W. Grimes 		rt->rt_llinfo = (caddr_t)la;
229df8bae1dSRodney W. Grimes 		if (la == 0) {
230d1dd20beSSam Leffler 			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
231df8bae1dSRodney W. Grimes 			break;
232df8bae1dSRodney W. Grimes 		}
233d1dd20beSSam Leffler 		arp_allocated++;
234df8bae1dSRodney W. Grimes 		la->la_rt = rt;
235df8bae1dSRodney W. Grimes 		rt->rt_flags |= RTF_LLINFO;
23693f79889SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
23766800f57SGarrett Wollman 		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
238cbb0b46aSGarrett Wollman 
2391d5e9e22SEivind Eklund #ifdef INET
240cbb0b46aSGarrett Wollman 		/*
241cbb0b46aSGarrett Wollman 		 * This keeps the multicast addresses from showing up
242cbb0b46aSGarrett Wollman 		 * in `arp -a' listings as unresolved.  It's not actually
243cbb0b46aSGarrett Wollman 		 * functional.  Then the same for broadcast.
244cbb0b46aSGarrett Wollman 		 */
245c448c89cSJonathan Lemon 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
246c448c89cSJonathan Lemon 		    rt->rt_ifp->if_type != IFT_ARCNET) {
247cbb0b46aSGarrett Wollman 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
248cbb0b46aSGarrett Wollman 					       LLADDR(SDL(gate)));
249cbb0b46aSGarrett Wollman 			SDL(gate)->sdl_alen = 6;
25051a109a1SPeter Wemm 			rt->rt_expire = 0;
251cbb0b46aSGarrett Wollman 		}
252cbb0b46aSGarrett Wollman 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
253322dcb8dSMax Khon 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
254322dcb8dSMax Khon 			       rt->rt_ifp->if_addrlen);
255322dcb8dSMax Khon 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
25651a109a1SPeter Wemm 			rt->rt_expire = 0;
257cbb0b46aSGarrett Wollman 		}
2581d5e9e22SEivind Eklund #endif
259cbb0b46aSGarrett Wollman 
260067a8babSMax Laier 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
261067a8babSMax Laier 			if (ia->ia_ifp == rt->rt_ifp &&
262067a8babSMax Laier 			    SIN(rt_key(rt))->sin_addr.s_addr ==
263067a8babSMax Laier 			    (IA_SIN(ia))->sin_addr.s_addr)
264067a8babSMax Laier 				break;
265067a8babSMax Laier 		}
266067a8babSMax Laier 		if (ia) {
267df8bae1dSRodney W. Grimes 		    /*
268df8bae1dSRodney W. Grimes 		     * This test used to be
269df8bae1dSRodney W. Grimes 		     *	if (loif.if_flags & IFF_UP)
270df8bae1dSRodney W. Grimes 		     * It allowed local traffic to be forced
271df8bae1dSRodney W. Grimes 		     * through the hardware by configuring the loopback down.
272df8bae1dSRodney W. Grimes 		     * However, it causes problems during network configuration
273df8bae1dSRodney W. Grimes 		     * for boards that can't receive packets they send.
274df8bae1dSRodney W. Grimes 		     * It is now necessary to clear "useloopback" and remove
275df8bae1dSRodney W. Grimes 		     * the route to force traffic out to the hardware.
276df8bae1dSRodney W. Grimes 		     */
277df8bae1dSRodney W. Grimes 			rt->rt_expire = 0;
278ac912b2dSLuigi Rizzo 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
279322dcb8dSMax Khon 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
280df8bae1dSRodney W. Grimes 			if (useloopback)
281f5fea3ddSPaul Traina 				rt->rt_ifp = loif;
282df8bae1dSRodney W. Grimes 
283067a8babSMax Laier 		    /*
284067a8babSMax Laier 		     * make sure to set rt->rt_ifa to the interface
285067a8babSMax Laier 		     * address we are using, otherwise we will have trouble
286067a8babSMax Laier 		     * with source address selection.
287067a8babSMax Laier 		     */
288067a8babSMax Laier 			ifa = &ia->ia_ifa;
289067a8babSMax Laier 			if (ifa != rt->rt_ifa) {
290067a8babSMax Laier 				IFAFREE(rt->rt_ifa);
291067a8babSMax Laier 				IFAREF(ifa);
292067a8babSMax Laier 				rt->rt_ifa = ifa;
293067a8babSMax Laier 			}
294df8bae1dSRodney W. Grimes 		}
295df8bae1dSRodney W. Grimes 		break;
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes 	case RTM_DELETE:
298df8bae1dSRodney W. Grimes 		if (la == 0)
299df8bae1dSRodney W. Grimes 			break;
30093f79889SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
30166800f57SGarrett Wollman 		LIST_REMOVE(la, la_le);
302df8bae1dSRodney W. Grimes 		rt->rt_llinfo = 0;
303df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_LLINFO;
304df8bae1dSRodney W. Grimes 		if (la->la_hold)
305df8bae1dSRodney W. Grimes 			m_freem(la->la_hold);
306df8bae1dSRodney W. Grimes 		Free((caddr_t)la);
307df8bae1dSRodney W. Grimes 	}
308df8bae1dSRodney W. Grimes }
309df8bae1dSRodney W. Grimes 
310df8bae1dSRodney W. Grimes /*
311df8bae1dSRodney W. Grimes  * Broadcast an ARP request. Caller specifies:
312df8bae1dSRodney W. Grimes  *	- arp header source ip address
313df8bae1dSRodney W. Grimes  *	- arp header target ip address
314df8bae1dSRodney W. Grimes  *	- arp header source ethernet address
315df8bae1dSRodney W. Grimes  */
316df8bae1dSRodney W. Grimes static void
317322dcb8dSMax Khon arprequest(ifp, sip, tip, enaddr)
318e952fa39SMatthew N. Dodd 	struct ifnet *ifp;
319e952fa39SMatthew N. Dodd 	struct in_addr *sip, *tip;
320e952fa39SMatthew N. Dodd 	u_char *enaddr;
321df8bae1dSRodney W. Grimes {
322e952fa39SMatthew N. Dodd 	struct mbuf *m;
323e952fa39SMatthew N. Dodd 	struct arphdr *ah;
324df8bae1dSRodney W. Grimes 	struct sockaddr sa;
325df8bae1dSRodney W. Grimes 
326a163d034SWarner Losh 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
327df8bae1dSRodney W. Grimes 		return;
32864bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
32964bf80ceSMatthew N. Dodd 		2*ifp->if_data.ifi_addrlen;
33064bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
33164bf80ceSMatthew N. Dodd 	MH_ALIGN(m, m->m_len);
33264bf80ceSMatthew N. Dodd 	ah = mtod(m, struct arphdr *);
33364bf80ceSMatthew N. Dodd 	bzero((caddr_t)ah, m->m_len);
33419527d3eSRobert Watson #ifdef MAC
33519527d3eSRobert Watson 	mac_create_mbuf_linklayer(ifp, m);
33619527d3eSRobert Watson #endif
337322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP);
338322dcb8dSMax Khon 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
339322dcb8dSMax Khon 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
340322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REQUEST);
34164bf80ceSMatthew N. Dodd 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
34264bf80ceSMatthew N. Dodd 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
34364bf80ceSMatthew N. Dodd 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
34464bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
34564bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
34664bf80ceSMatthew N. Dodd 	m->m_flags |= M_BCAST;
347322dcb8dSMax Khon 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
34864bf80ceSMatthew N. Dodd 
34964bf80ceSMatthew N. Dodd 	return;
350df8bae1dSRodney W. Grimes }
351df8bae1dSRodney W. Grimes 
352df8bae1dSRodney W. Grimes /*
353cd46a114SLuigi Rizzo  * Resolve an IP address into an ethernet address.
354cd46a114SLuigi Rizzo  * On input:
355cd46a114SLuigi Rizzo  *    ifp is the interface we use
356cd46a114SLuigi Rizzo  *    dst is the next hop,
357cd46a114SLuigi Rizzo  *    rt0 is the route to the final destination (possibly useless)
358cd46a114SLuigi Rizzo  *    m is the mbuf
359cd46a114SLuigi Rizzo  *    desten is where we want the address.
360cd46a114SLuigi Rizzo  *
361cd46a114SLuigi Rizzo  * On success, desten is filled in and the function returns 0;
362cd46a114SLuigi Rizzo  * If the packet must be held pending resolution, we return EWOULDBLOCK
363cd46a114SLuigi Rizzo  * On other errors, we return the corresponding error code.
364df8bae1dSRodney W. Grimes  */
365df8bae1dSRodney W. Grimes int
366cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
367f7c5baa1SLuigi Rizzo 	struct sockaddr *dst, u_char *desten)
368df8bae1dSRodney W. Grimes {
36908aadfbbSJonathan Lemon 	struct llinfo_arp *la = 0;
370df8bae1dSRodney W. Grimes 	struct sockaddr_dl *sdl;
371cd46a114SLuigi Rizzo 	int error;
372cd46a114SLuigi Rizzo 	struct rtentry *rt;
373cd46a114SLuigi Rizzo 
374cd46a114SLuigi Rizzo 	error = rt_check(&rt, &rt0, dst);
375cd46a114SLuigi Rizzo 	if (error) {
376cd46a114SLuigi Rizzo 		m_freem(m);
377cd46a114SLuigi Rizzo 		return error;
378cd46a114SLuigi Rizzo 	}
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 	if (m->m_flags & M_BCAST) {	/* broadcast */
381322dcb8dSMax Khon 		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
382cd46a114SLuigi Rizzo 		return (0);
383df8bae1dSRodney W. Grimes 	}
384322dcb8dSMax Khon 	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
385df8bae1dSRodney W. Grimes 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
386cd46a114SLuigi Rizzo 		return (0);
387df8bae1dSRodney W. Grimes 	}
388df8bae1dSRodney W. Grimes 	if (rt)
389df8bae1dSRodney W. Grimes 		la = (struct llinfo_arp *)rt->rt_llinfo;
390e7bc5f27SBill Fenner 	if (la == 0) {
391623ae52eSPoul-Henning Kamp 		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
392623ae52eSPoul-Henning Kamp 		if (la)
393df8bae1dSRodney W. Grimes 			rt = la->la_rt;
394df8bae1dSRodney W. Grimes 	}
395df8bae1dSRodney W. Grimes 	if (la == 0 || rt == 0) {
396245086a0SPoul-Henning Kamp 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
397245086a0SPoul-Henning Kamp 			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
398245086a0SPoul-Henning Kamp 				rt ? "rt" : "");
399df8bae1dSRodney W. Grimes 		m_freem(m);
400cd46a114SLuigi Rizzo 		return (EINVAL); /* XXX */
401df8bae1dSRodney W. Grimes 	}
402df8bae1dSRodney W. Grimes 	sdl = SDL(rt->rt_gateway);
403df8bae1dSRodney W. Grimes 	/*
404df8bae1dSRodney W. Grimes 	 * Check the address family and length is valid, the address
405df8bae1dSRodney W. Grimes 	 * is resolved; otherwise, try to resolve.
406df8bae1dSRodney W. Grimes 	 */
407227ee8a1SPoul-Henning Kamp 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
408df8bae1dSRodney W. Grimes 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
409f0f3379eSOrion Hodson 		/*
410f0f3379eSOrion Hodson 		 * If entry has an expiry time and it is approaching,
411f0f3379eSOrion Hodson 		 * see if we need to send an ARP request within this
412f0f3379eSOrion Hodson 		 * arpt_down interval.
413f0f3379eSOrion Hodson 		 */
414f0f3379eSOrion Hodson 		if ((rt->rt_expire != 0) &&
415022695f8SOrion Hodson 		    (time_second + la->la_preempt > rt->rt_expire)) {
416f0f3379eSOrion Hodson 			arprequest(ifp,
417f0f3379eSOrion Hodson 				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
418f0f3379eSOrion Hodson 				   &SIN(dst)->sin_addr,
419f0f3379eSOrion Hodson 				   IF_LLADDR(ifp));
420022695f8SOrion Hodson 			la->la_preempt--;
421f0f3379eSOrion Hodson 		}
422f0f3379eSOrion Hodson 
4230453d3cbSBruce Evans 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
424cd46a114SLuigi Rizzo 		return (0);
425df8bae1dSRodney W. Grimes 	}
426df8bae1dSRodney W. Grimes 	/*
427deb62e28SRuslan Ermilov 	 * If ARP is disabled or static on this interface, stop.
42808aadfbbSJonathan Lemon 	 * XXX
42908aadfbbSJonathan Lemon 	 * Probably should not allocate empty llinfo struct if we are
43008aadfbbSJonathan Lemon 	 * not going to be sending out an arp request.
43108aadfbbSJonathan Lemon 	 */
432deb62e28SRuslan Ermilov 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
43347891de1SRuslan Ermilov 		m_freem(m);
434cd46a114SLuigi Rizzo 		return (EINVAL);
43547891de1SRuslan Ermilov 	}
43608aadfbbSJonathan Lemon 	/*
437df8bae1dSRodney W. Grimes 	 * There is an arptab entry, but no ethernet address
438df8bae1dSRodney W. Grimes 	 * response yet.  Replace the held mbuf with this
439df8bae1dSRodney W. Grimes 	 * latest one.
440df8bae1dSRodney W. Grimes 	 */
441df8bae1dSRodney W. Grimes 	if (la->la_hold)
442df8bae1dSRodney W. Grimes 		m_freem(la->la_hold);
443df8bae1dSRodney W. Grimes 	la->la_hold = m;
444df8bae1dSRodney W. Grimes 	if (rt->rt_expire) {
445d1dd20beSSam Leffler 		RT_LOCK(rt);
446df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_REJECT;
447227ee8a1SPoul-Henning Kamp 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
448227ee8a1SPoul-Henning Kamp 			rt->rt_expire = time_second;
44973224fb0SOrion Hodson 			if (la->la_asked++ < arp_maxtries) {
450322dcb8dSMax Khon 				arprequest(ifp,
451ed7509acSJulian Elischer 					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
452322dcb8dSMax Khon 					   &SIN(dst)->sin_addr,
453322dcb8dSMax Khon 					   IF_LLADDR(ifp));
45473224fb0SOrion Hodson 			} else {
455df8bae1dSRodney W. Grimes 				rt->rt_flags |= RTF_REJECT;
456df8bae1dSRodney W. Grimes 				rt->rt_expire += arpt_down;
457022695f8SOrion Hodson 				la->la_asked = 0;
458022695f8SOrion Hodson 				la->la_preempt = arp_maxtries;
459df8bae1dSRodney W. Grimes 			}
460df8bae1dSRodney W. Grimes 
461df8bae1dSRodney W. Grimes 		}
462d1dd20beSSam Leffler 		RT_UNLOCK(rt);
463df8bae1dSRodney W. Grimes 	}
464cd46a114SLuigi Rizzo 	return (EWOULDBLOCK);
465df8bae1dSRodney W. Grimes }
466df8bae1dSRodney W. Grimes 
467df8bae1dSRodney W. Grimes /*
468df8bae1dSRodney W. Grimes  * Common length and type checks are done here,
469df8bae1dSRodney W. Grimes  * then the protocol-specific routine is called.
470df8bae1dSRodney W. Grimes  */
471885f1aa4SPoul-Henning Kamp static void
4721cafed39SJonathan Lemon arpintr(struct mbuf *m)
473df8bae1dSRodney W. Grimes {
4741cafed39SJonathan Lemon 	struct arphdr *ar;
475df8bae1dSRodney W. Grimes 
47676ec7b2fSRobert Watson 	if (m->m_len < sizeof(struct arphdr) &&
47784365e2bSMatthew Dillon 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
478e44d6283SJoerg Wunsch 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
4791cafed39SJonathan Lemon 		return;
48076ec7b2fSRobert Watson 	}
48176ec7b2fSRobert Watson 	ar = mtod(m, struct arphdr *);
48276ec7b2fSRobert Watson 
4831cafed39SJonathan Lemon 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
4841cafed39SJonathan Lemon 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
485b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
486b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
4871cafed39SJonathan Lemon 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
48876ec7b2fSRobert Watson 		    (unsigned char *)&ar->ar_hrd, "");
48976ec7b2fSRobert Watson 		m_freem(m);
4901cafed39SJonathan Lemon 		return;
49176ec7b2fSRobert Watson 	}
49276ec7b2fSRobert Watson 
49331175791SRuslan Ermilov 	if (m->m_len < arphdr_len(ar)) {
49478e2d2bdSRuslan Ermilov 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
495f72d9d83SJoerg Wunsch 			log(LOG_ERR, "arp: runt packet\n");
49676ec7b2fSRobert Watson 			m_freem(m);
4971cafed39SJonathan Lemon 			return;
49876ec7b2fSRobert Watson 		}
49978e2d2bdSRuslan Ermilov 		ar = mtod(m, struct arphdr *);
50078e2d2bdSRuslan Ermilov 	}
501df8bae1dSRodney W. Grimes 
502df8bae1dSRodney W. Grimes 	switch (ntohs(ar->ar_pro)) {
5031d5e9e22SEivind Eklund #ifdef INET
504df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
505df8bae1dSRodney W. Grimes 		in_arpinput(m);
5061cafed39SJonathan Lemon 		return;
5071d5e9e22SEivind Eklund #endif
508df8bae1dSRodney W. Grimes 	}
509df8bae1dSRodney W. Grimes 	m_freem(m);
510df8bae1dSRodney W. Grimes }
511df8bae1dSRodney W. Grimes 
5121d5e9e22SEivind Eklund #ifdef INET
513df8bae1dSRodney W. Grimes /*
514df8bae1dSRodney W. Grimes  * ARP for Internet protocols on 10 Mb/s Ethernet.
515df8bae1dSRodney W. Grimes  * Algorithm is that given in RFC 826.
516df8bae1dSRodney W. Grimes  * In addition, a sanity check is performed on the sender
517df8bae1dSRodney W. Grimes  * protocol address, to catch impersonators.
518df8bae1dSRodney W. Grimes  * We no longer handle negotiations for use of trailer protocol:
519df8bae1dSRodney W. Grimes  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
520df8bae1dSRodney W. Grimes  * along with IP replies if we wanted trailers sent to us,
521df8bae1dSRodney W. Grimes  * and also sent them in response to IP replies.
522df8bae1dSRodney W. Grimes  * This allowed either end to announce the desire to receive
523df8bae1dSRodney W. Grimes  * trailer packets.
524df8bae1dSRodney W. Grimes  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
525df8bae1dSRodney W. Grimes  * but formerly didn't normally send requests.
526df8bae1dSRodney W. Grimes  */
5273269187dSAlfred Perlstein static int log_arp_wrong_iface = 1;
528e3d123d6SAlfred Perlstein static int log_arp_movements = 1;
5293269187dSAlfred Perlstein 
5303269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
5313269187dSAlfred Perlstein 	&log_arp_wrong_iface, 0,
5323269187dSAlfred Perlstein 	"log arp packets arriving on the wrong interface");
533e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
534e3d123d6SAlfred Perlstein         &log_arp_movements, 0,
53575ce3221SAlfred Perlstein         "log arp replies from MACs different than the one in the cache");
536e3d123d6SAlfred Perlstein 
5373269187dSAlfred Perlstein 
538df8bae1dSRodney W. Grimes static void
539df8bae1dSRodney W. Grimes in_arpinput(m)
540df8bae1dSRodney W. Grimes 	struct mbuf *m;
541df8bae1dSRodney W. Grimes {
542e952fa39SMatthew N. Dodd 	struct arphdr *ah;
543e952fa39SMatthew N. Dodd 	struct ifnet *ifp = m->m_pkthdr.rcvif;
544fda82fc2SJulian Elischer 	struct iso88025_header *th = (struct iso88025_header *)0;
54542fdfc12SKelly Yancey 	struct iso88025_sockaddr_dl_data *trld;
546e952fa39SMatthew N. Dodd 	struct llinfo_arp *la = 0;
547e952fa39SMatthew N. Dodd 	struct rtentry *rt;
548ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
549ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
550df8bae1dSRodney W. Grimes 	struct sockaddr_dl *sdl;
551df8bae1dSRodney W. Grimes 	struct sockaddr sa;
552df8bae1dSRodney W. Grimes 	struct in_addr isaddr, itaddr, myaddr;
553a9771948SGleb Smirnoff 	u_int8_t *enaddr = NULL;
554b149dd6cSLarry Lile 	int op, rif_len;
555322dcb8dSMax Khon 	int req_len;
5562ef4a436SGleb Smirnoff 	int carp_match = 0;
557df8bae1dSRodney W. Grimes 
558322dcb8dSMax Khon 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
559322dcb8dSMax Khon 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
5604cbc8ad1SYaroslav Tykhiy 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
5614cbc8ad1SYaroslav Tykhiy 		return;
5624cbc8ad1SYaroslav Tykhiy 	}
5634cbc8ad1SYaroslav Tykhiy 
564322dcb8dSMax Khon 	ah = mtod(m, struct arphdr *);
565322dcb8dSMax Khon 	op = ntohs(ah->ar_op);
566322dcb8dSMax Khon 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
567322dcb8dSMax Khon 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
56832439868SGleb Smirnoff 
569ca925d9cSJonathan Lemon 	/*
570ca925d9cSJonathan Lemon 	 * For a bridge, we want to check the address irrespective
571ca925d9cSJonathan Lemon 	 * of the receive interface. (This will change slightly
572ca925d9cSJonathan Lemon 	 * when we have clusters of interfaces).
573a9771948SGleb Smirnoff 	 * If the interface does not match, but the recieving interface
574a9771948SGleb Smirnoff 	 * is part of carp, we call carp_iamatch to see if this is a
575a9771948SGleb Smirnoff 	 * request for the virtual host ip.
576a9771948SGleb Smirnoff 	 * XXX: This is really ugly!
577ca925d9cSJonathan Lemon 	 */
5782ef4a436SGleb Smirnoff 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
5792ef4a436SGleb Smirnoff 		if ((do_bridge || (ia->ia_ifp == ifp)) &&
5802ef4a436SGleb Smirnoff 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
581ca925d9cSJonathan Lemon 			goto match;
5822ef4a436SGleb Smirnoff #ifdef DEV_CARP
5832ef4a436SGleb Smirnoff 		if (ifp->if_carp != NULL &&
5842ef4a436SGleb Smirnoff 		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
5852ef4a436SGleb Smirnoff 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
5862ef4a436SGleb Smirnoff 			carp_match = 1;
5872ef4a436SGleb Smirnoff 			goto match;
5882ef4a436SGleb Smirnoff 		}
5892ef4a436SGleb Smirnoff #endif
5902ef4a436SGleb Smirnoff 	}
591ca925d9cSJonathan Lemon 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
59232439868SGleb Smirnoff 		if ((do_bridge || (ia->ia_ifp == ifp)) &&
593ca925d9cSJonathan Lemon 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
594ca925d9cSJonathan Lemon 			goto match;
595ca925d9cSJonathan Lemon 	/*
596d8b84d9eSJonathan Lemon 	 * No match, use the first inet address on the receive interface
597ca925d9cSJonathan Lemon 	 * as a dummy address for the rest of the function.
598ca925d9cSJonathan Lemon 	 */
599d8b84d9eSJonathan Lemon 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
600ec691a10SJonathan Lemon 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
601ec691a10SJonathan Lemon 			ia = ifatoia(ifa);
602ec691a10SJonathan Lemon 			goto match;
603ec691a10SJonathan Lemon 		}
604ec691a10SJonathan Lemon 	/*
605ec691a10SJonathan Lemon 	 * If bridging, fall back to using any inet address.
606ec691a10SJonathan Lemon 	 */
60732439868SGleb Smirnoff 	if (!do_bridge || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
608b2a8ac7cSLuigi Rizzo 		goto drop;
609ca925d9cSJonathan Lemon match:
610a9771948SGleb Smirnoff 	if (!enaddr)
611a9771948SGleb Smirnoff 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
612ca925d9cSJonathan Lemon 	myaddr = ia->ia_addr.sin_addr;
613a9771948SGleb Smirnoff 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
614b2a8ac7cSLuigi Rizzo 		goto drop;	/* it's from me, ignore it. */
615322dcb8dSMax Khon 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
616df8bae1dSRodney W. Grimes 		log(LOG_ERR,
617322dcb8dSMax Khon 		    "arp: link address is broadcast for IP address %s!\n",
618ef0cdf33SGarrett Wollman 		    inet_ntoa(isaddr));
619b2a8ac7cSLuigi Rizzo 		goto drop;
620df8bae1dSRodney W. Grimes 	}
62100fcf9d1SRobert Watson 	/*
62200fcf9d1SRobert Watson 	 * Warn if another host is using the same IP address, but only if the
62300fcf9d1SRobert Watson 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
62400fcf9d1SRobert Watson 	 * case we suppress the warning to avoid false positive complaints of
62500fcf9d1SRobert Watson 	 * potential misconfiguration.
62600fcf9d1SRobert Watson 	 */
62700fcf9d1SRobert Watson 	if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
628df8bae1dSRodney W. Grimes 		log(LOG_ERR,
629322dcb8dSMax Khon 		   "arp: %*D is using my IP address %s!\n",
630322dcb8dSMax Khon 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
631322dcb8dSMax Khon 		   inet_ntoa(isaddr));
632df8bae1dSRodney W. Grimes 		itaddr = myaddr;
633df8bae1dSRodney W. Grimes 		goto reply;
634df8bae1dSRodney W. Grimes 	}
635deb62e28SRuslan Ermilov 	if (ifp->if_flags & IFF_STATICARP)
636deb62e28SRuslan Ermilov 		goto reply;
637df8bae1dSRodney W. Grimes 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
638df8bae1dSRodney W. Grimes 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
6397e1cd0d2SLuigi Rizzo 		/* the following is not an error when doing bridging */
6402ef4a436SGleb Smirnoff 		if (!do_bridge && rt->rt_ifp != ifp &&
6412ef4a436SGleb Smirnoff 		    !(ifp->if_type == IFT_CARP && carp_match)) {
6423269187dSAlfred Perlstein 			if (log_arp_wrong_iface)
6439bf40edeSBrooks Davis 				log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
644dd9b6ddeSBill Fenner 				    inet_ntoa(isaddr),
6459bf40edeSBrooks Davis 				    rt->rt_ifp->if_xname,
646322dcb8dSMax Khon 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
6479bf40edeSBrooks Davis 				    ifp->if_xname);
648dd9b6ddeSBill Fenner 			goto reply;
649dd9b6ddeSBill Fenner 		}
650df8bae1dSRodney W. Grimes 		if (sdl->sdl_alen &&
651322dcb8dSMax Khon 		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
652e3d123d6SAlfred Perlstein 			if (rt->rt_expire) {
653e3d123d6SAlfred Perlstein 			    if (log_arp_movements)
6549bf40edeSBrooks Davis 			        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
655322dcb8dSMax Khon 				    inet_ntoa(isaddr),
656322dcb8dSMax Khon 				    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
657322dcb8dSMax Khon 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
6589bf40edeSBrooks Davis 				    ifp->if_xname);
659e3d123d6SAlfred Perlstein 			} else {
660dd9b6ddeSBill Fenner 			    log(LOG_ERR,
6619bf40edeSBrooks Davis 				"arp: %*D attempts to modify permanent entry for %s on %s\n",
662322dcb8dSMax Khon 				ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
6639bf40edeSBrooks Davis 				inet_ntoa(isaddr), ifp->if_xname);
664dd9b6ddeSBill Fenner 			    goto reply;
665dd9b6ddeSBill Fenner 			}
666dfd5dee1SPeter Wemm 		}
667322dcb8dSMax Khon 		/*
668322dcb8dSMax Khon 		 * sanity check for the address length.
669322dcb8dSMax Khon 		 * XXX this does not work for protocols with variable address
670322dcb8dSMax Khon 		 * length. -is
671322dcb8dSMax Khon 		 */
672322dcb8dSMax Khon 		if (sdl->sdl_alen &&
673322dcb8dSMax Khon 		    sdl->sdl_alen != ah->ar_hln) {
674322dcb8dSMax Khon 			log(LOG_WARNING,
675322dcb8dSMax Khon 			    "arp from %*D: new addr len %d, was %d",
676322dcb8dSMax Khon 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
677322dcb8dSMax Khon 			    ah->ar_hln, sdl->sdl_alen);
678322dcb8dSMax Khon 		}
679322dcb8dSMax Khon 		if (ifp->if_addrlen != ah->ar_hln) {
680322dcb8dSMax Khon 			log(LOG_WARNING,
681322dcb8dSMax Khon 			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
682322dcb8dSMax Khon 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
683322dcb8dSMax Khon 			    ah->ar_hln, ifp->if_addrlen);
684322dcb8dSMax Khon 			goto reply;
685322dcb8dSMax Khon 		}
686322dcb8dSMax Khon 		(void)memcpy(LLADDR(sdl), ar_sha(ah),
687322dcb8dSMax Khon 		    sdl->sdl_alen = ah->ar_hln);
688243c4c6dSEivind Eklund 		/*
689243c4c6dSEivind Eklund 		 * If we receive an arp from a token-ring station over
690243c4c6dSEivind Eklund 		 * a token-ring nic then try to save the source
691243c4c6dSEivind Eklund 		 * routing info.
692243c4c6dSEivind Eklund 		 */
693322dcb8dSMax Khon 		if (ifp->if_type == IFT_ISO88025) {
694fda82fc2SJulian Elischer 			th = (struct iso88025_header *)m->m_pkthdr.header;
69542fdfc12SKelly Yancey 			trld = SDL_ISO88025(sdl);
696b149dd6cSLarry Lile 			rif_len = TR_RCF_RIFLEN(th->rcf);
697b149dd6cSLarry Lile 			if ((th->iso88025_shost[0] & TR_RII) &&
698b149dd6cSLarry Lile 			    (rif_len > 2)) {
69942fdfc12SKelly Yancey 				trld->trld_rcf = th->rcf;
70042fdfc12SKelly Yancey 				trld->trld_rcf ^= htons(TR_RCF_DIR);
70142fdfc12SKelly Yancey 				memcpy(trld->trld_route, th->rd, rif_len - 2);
70242fdfc12SKelly Yancey 				trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
703f9083fdbSLarry Lile 				/*
704f9083fdbSLarry Lile 				 * Set up source routing information for
705f9083fdbSLarry Lile 				 * reply packet (XXX)
706f9083fdbSLarry Lile 				 */
707b149dd6cSLarry Lile 				m->m_data -= rif_len;
708b149dd6cSLarry Lile 				m->m_len  += rif_len;
709b149dd6cSLarry Lile 				m->m_pkthdr.len += rif_len;
710fda82fc2SJulian Elischer 			} else {
711b149dd6cSLarry Lile 				th->iso88025_shost[0] &= ~TR_RII;
712c3a2190cSKelly Yancey 				trld->trld_rcf = 0;
713fcf11853SLarry Lile 			}
714fda82fc2SJulian Elischer 			m->m_data -= 8;
715fda82fc2SJulian Elischer 			m->m_len  += 8;
716fcf11853SLarry Lile 			m->m_pkthdr.len += 8;
71742fdfc12SKelly Yancey 			th->rcf = trld->trld_rcf;
718fda82fc2SJulian Elischer 		}
719d1dd20beSSam Leffler 		RT_LOCK(rt);
720df8bae1dSRodney W. Grimes 		if (rt->rt_expire)
721227ee8a1SPoul-Henning Kamp 			rt->rt_expire = time_second + arpt_keep;
722df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_REJECT;
723d1dd20beSSam Leffler 		RT_UNLOCK(rt);
724022695f8SOrion Hodson 		la->la_asked = 0;
725022695f8SOrion Hodson 		la->la_preempt = arp_maxtries;
726df8bae1dSRodney W. Grimes 		if (la->la_hold) {
7272964fb65SMatthew N. Dodd 			(*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
728df8bae1dSRodney W. Grimes 			la->la_hold = 0;
729df8bae1dSRodney W. Grimes 		}
730df8bae1dSRodney W. Grimes 	}
731df8bae1dSRodney W. Grimes reply:
732b2a8ac7cSLuigi Rizzo 	if (op != ARPOP_REQUEST)
733b2a8ac7cSLuigi Rizzo 		goto drop;
734df8bae1dSRodney W. Grimes 	if (itaddr.s_addr == myaddr.s_addr) {
735df8bae1dSRodney W. Grimes 		/* I am the target */
736322dcb8dSMax Khon 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
737a9771948SGleb Smirnoff 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
738df8bae1dSRodney W. Grimes 	} else {
739df8bae1dSRodney W. Grimes 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
74028e82295SGarrett Wollman 		if (la == NULL) {
74128e82295SGarrett Wollman 			struct sockaddr_in sin;
74228e82295SGarrett Wollman 
743b2a8ac7cSLuigi Rizzo 			if (!arp_proxyall)
744b2a8ac7cSLuigi Rizzo 				goto drop;
74528e82295SGarrett Wollman 
74628e82295SGarrett Wollman 			bzero(&sin, sizeof sin);
74728e82295SGarrett Wollman 			sin.sin_family = AF_INET;
74828e82295SGarrett Wollman 			sin.sin_len = sizeof sin;
74928e82295SGarrett Wollman 			sin.sin_addr = itaddr;
75028e82295SGarrett Wollman 
75131246bc2SGarrett Wollman 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
752b2a8ac7cSLuigi Rizzo 			if (!rt)
753b2a8ac7cSLuigi Rizzo 				goto drop;
75428e82295SGarrett Wollman 			/*
75528e82295SGarrett Wollman 			 * Don't send proxies for nodes on the same interface
75628e82295SGarrett Wollman 			 * as this one came out of, or we'll get into a fight
75728e82295SGarrett Wollman 			 * over who claims what Ether address.
75828e82295SGarrett Wollman 			 */
759322dcb8dSMax Khon 			if (rt->rt_ifp == ifp) {
76028e82295SGarrett Wollman 				rtfree(rt);
761b2a8ac7cSLuigi Rizzo 				goto drop;
76228e82295SGarrett Wollman 			}
763322dcb8dSMax Khon 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
764a9771948SGleb Smirnoff 			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
76528e82295SGarrett Wollman 			rtfree(rt);
766cc728227SDavid Malone 
767cc728227SDavid Malone 			/*
768cc728227SDavid Malone 			 * Also check that the node which sent the ARP packet
769cc728227SDavid Malone 			 * is on the the interface we expect it to be on. This
770cc728227SDavid Malone 			 * avoids ARP chaos if an interface is connected to the
771cc728227SDavid Malone 			 * wrong network.
772cc728227SDavid Malone 			 */
773cc728227SDavid Malone 			sin.sin_addr = isaddr;
774cc728227SDavid Malone 
775cc728227SDavid Malone 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
776b2a8ac7cSLuigi Rizzo 			if (!rt)
777b2a8ac7cSLuigi Rizzo 				goto drop;
778322dcb8dSMax Khon 			if (rt->rt_ifp != ifp) {
779cc728227SDavid Malone 				log(LOG_INFO, "arp_proxy: ignoring request"
7809bf40edeSBrooks Davis 				    " from %s via %s, expecting %s\n",
7819bf40edeSBrooks Davis 				    inet_ntoa(isaddr), ifp->if_xname,
7829bf40edeSBrooks Davis 				    rt->rt_ifp->if_xname);
783cc728227SDavid Malone 				rtfree(rt);
784b2a8ac7cSLuigi Rizzo 				goto drop;
785cc728227SDavid Malone 			}
786cc728227SDavid Malone 			rtfree(rt);
787cc728227SDavid Malone 
788ac234f93SGarrett Wollman #ifdef DEBUG_PROXY
789ac234f93SGarrett Wollman 			printf("arp: proxying for %s\n",
790ef0cdf33SGarrett Wollman 			       inet_ntoa(itaddr));
791ac234f93SGarrett Wollman #endif
79228e82295SGarrett Wollman 		} else {
793df8bae1dSRodney W. Grimes 			rt = la->la_rt;
794322dcb8dSMax Khon 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
795df8bae1dSRodney W. Grimes 			sdl = SDL(rt->rt_gateway);
796322dcb8dSMax Khon 			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
79728e82295SGarrett Wollman 		}
798df8bae1dSRodney W. Grimes 	}
799df8bae1dSRodney W. Grimes 
800322dcb8dSMax Khon 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
801322dcb8dSMax Khon 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
802322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REPLY);
803322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
80464bf80ceSMatthew N. Dodd 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
80564bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
80664bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
80764bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
80864bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
809322dcb8dSMax Khon 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
810df8bae1dSRodney W. Grimes 	return;
811b2a8ac7cSLuigi Rizzo 
812b2a8ac7cSLuigi Rizzo drop:
813b2a8ac7cSLuigi Rizzo 	m_freem(m);
814df8bae1dSRodney W. Grimes }
8151d5e9e22SEivind Eklund #endif
816df8bae1dSRodney W. Grimes 
817df8bae1dSRodney W. Grimes /*
818df8bae1dSRodney W. Grimes  * Free an arp entry.
819df8bae1dSRodney W. Grimes  */
820df8bae1dSRodney W. Grimes static void
821df8bae1dSRodney W. Grimes arptfree(la)
822e952fa39SMatthew N. Dodd 	struct llinfo_arp *la;
823df8bae1dSRodney W. Grimes {
824e952fa39SMatthew N. Dodd 	struct rtentry *rt = la->la_rt;
825e952fa39SMatthew N. Dodd 	struct sockaddr_dl *sdl;
826d1dd20beSSam Leffler 
827df8bae1dSRodney W. Grimes 	if (rt == 0)
828df8bae1dSRodney W. Grimes 		panic("arptfree");
829df8bae1dSRodney W. Grimes 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
830df8bae1dSRodney W. Grimes 	    sdl->sdl_family == AF_LINK) {
831df8bae1dSRodney W. Grimes 		sdl->sdl_alen = 0;
83273224fb0SOrion Hodson 		la->la_preempt = la->la_asked = 0;
833d1dd20beSSam Leffler 		RT_LOCK(rt);		/* XXX needed or move higher? */
834df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_REJECT;
835d1dd20beSSam Leffler 		RT_UNLOCK(rt);
836df8bae1dSRodney W. Grimes 		return;
837df8bae1dSRodney W. Grimes 	}
838df8bae1dSRodney W. Grimes 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
839df8bae1dSRodney W. Grimes 			0, (struct rtentry **)0);
840df8bae1dSRodney W. Grimes }
841df8bae1dSRodney W. Grimes /*
842df8bae1dSRodney W. Grimes  * Lookup or enter a new address in arptab.
843df8bae1dSRodney W. Grimes  */
844df8bae1dSRodney W. Grimes static struct llinfo_arp *
845df8bae1dSRodney W. Grimes arplookup(addr, create, proxy)
846df8bae1dSRodney W. Grimes 	u_long addr;
847df8bae1dSRodney W. Grimes 	int create, proxy;
848df8bae1dSRodney W. Grimes {
849e952fa39SMatthew N. Dodd 	struct rtentry *rt;
850d1dd20beSSam Leffler 	struct sockaddr_inarp sin;
851ac234f93SGarrett Wollman 	const char *why = 0;
852df8bae1dSRodney W. Grimes 
853d1dd20beSSam Leffler 	bzero(&sin, sizeof(sin));
854d1dd20beSSam Leffler 	sin.sin_len = sizeof(sin);
855d1dd20beSSam Leffler 	sin.sin_family = AF_INET;
856df8bae1dSRodney W. Grimes 	sin.sin_addr.s_addr = addr;
857d1dd20beSSam Leffler 	if (proxy)
858d1dd20beSSam Leffler 		sin.sin_other = SIN_PROXY;
85931246bc2SGarrett Wollman 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
860df8bae1dSRodney W. Grimes 	if (rt == 0)
861df8bae1dSRodney W. Grimes 		return (0);
862ac234f93SGarrett Wollman 
863ac234f93SGarrett Wollman 	if (rt->rt_flags & RTF_GATEWAY)
864ac234f93SGarrett Wollman 		why = "host is not on local network";
865ac234f93SGarrett Wollman 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
866ac234f93SGarrett Wollman 		why = "could not allocate llinfo";
867ac234f93SGarrett Wollman 	else if (rt->rt_gateway->sa_family != AF_LINK)
868ac234f93SGarrett Wollman 		why = "gateway route is not ours";
869ac234f93SGarrett Wollman 
870fedf1d01SBruce M Simpson 	if (why) {
871d1dd20beSSam Leffler #define	ISDYNCLONE(_rt) \
872d1dd20beSSam Leffler 	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
873d1dd20beSSam Leffler 		if (create)
874ac234f93SGarrett Wollman 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
875ef0cdf33SGarrett Wollman 			    inet_ntoa(sin.sin_addr), why);
876b75bead1SBruce M Simpson 		/*
877b75bead1SBruce M Simpson 		 * If there are no references to this Layer 2 route,
878b75bead1SBruce M Simpson 		 * and it is a cloned route, and not static, and
879b75bead1SBruce M Simpson 		 * arplookup() is creating the route, then purge
880b75bead1SBruce M Simpson 		 * it from the routing table as it is probably bogus.
881b75bead1SBruce M Simpson 		 */
8829c63e9dbSSam Leffler 		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
8839c63e9dbSSam Leffler 			rtexpunge(rt);
8849c63e9dbSSam Leffler 		RTFREE_LOCKED(rt);
885fedf1d01SBruce M Simpson 		return (0);
886d1dd20beSSam Leffler #undef ISDYNCLONE
887d1dd20beSSam Leffler 	} else {
8887138d65cSSam Leffler 		RT_REMREF(rt);
889d1dd20beSSam Leffler 		RT_UNLOCK(rt);
890df8bae1dSRodney W. Grimes 		return ((struct llinfo_arp *)rt->rt_llinfo);
891df8bae1dSRodney W. Grimes 	}
892d1dd20beSSam Leffler }
893df8bae1dSRodney W. Grimes 
894dd2e4102SGarrett Wollman void
895322dcb8dSMax Khon arp_ifinit(ifp, ifa)
896322dcb8dSMax Khon 	struct ifnet *ifp;
897dd2e4102SGarrett Wollman 	struct ifaddr *ifa;
898dd2e4102SGarrett Wollman {
899dd570d4dSTor Egge 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
900322dcb8dSMax Khon 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
901322dcb8dSMax Khon 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
902dd2e4102SGarrett Wollman 	ifa->ifa_rtrequest = arp_rtrequest;
903dd2e4102SGarrett Wollman 	ifa->ifa_flags |= RTF_CLONING;
904dd2e4102SGarrett Wollman }
905df5e1987SJonathan Lemon 
906a9771948SGleb Smirnoff void
907a9771948SGleb Smirnoff arp_ifinit2(ifp, ifa, enaddr)
908a9771948SGleb Smirnoff 	struct ifnet *ifp;
909a9771948SGleb Smirnoff 	struct ifaddr *ifa;
910a9771948SGleb Smirnoff 	u_char *enaddr;
911a9771948SGleb Smirnoff {
912a9771948SGleb Smirnoff 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
913a9771948SGleb Smirnoff 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
914a9771948SGleb Smirnoff 				&IA_SIN(ifa)->sin_addr, enaddr);
915a9771948SGleb Smirnoff 	ifa->ifa_rtrequest = arp_rtrequest;
916a9771948SGleb Smirnoff 	ifa->ifa_flags |= RTF_CLONING;
917a9771948SGleb Smirnoff }
918a9771948SGleb Smirnoff 
919df5e1987SJonathan Lemon static void
920df5e1987SJonathan Lemon arp_init(void)
921df5e1987SJonathan Lemon {
922df5e1987SJonathan Lemon 
923df5e1987SJonathan Lemon 	arpintrq.ifq_maxlen = 50;
9246008862bSJohn Baldwin 	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
925532cf61bSPeter Wemm 	LIST_INIT(&llinfo_arp);
926d1dd20beSSam Leffler 	callout_init(&arp_callout, CALLOUT_MPSAFE);
9277902224cSSam Leffler 	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
928cfff63f1SLuigi Rizzo 	callout_reset(&arp_callout, hz, arptimer, NULL);
929df5e1987SJonathan Lemon }
930df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
931