xref: /freebsd/sys/netinet/if_ether.c (revision 29910a5a77c2902cf80841a74a9446576dc19a17)
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
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32df8bae1dSRodney W. Grimes /*
33df8bae1dSRodney W. Grimes  * Ethernet address resolution protocol.
34df8bae1dSRodney W. Grimes  * TODO:
35df8bae1dSRodney W. Grimes  *	add "inuse/lock" bit (or ref. count) along with valid bit
36df8bae1dSRodney W. Grimes  */
37df8bae1dSRodney W. Grimes 
384b421e2dSMike Silbersack #include <sys/cdefs.h>
394b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
404b421e2dSMike Silbersack 
411d5e9e22SEivind Eklund #include "opt_inet.h"
4219527d3eSRobert Watson #include "opt_mac.h"
43a9771948SGleb Smirnoff #include "opt_carp.h"
441d5e9e22SEivind Eklund 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/kernel.h>
47ce02431fSDoug Rabson #include <sys/queue.h>
48885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h>
49885f1aa4SPoul-Henning Kamp #include <sys/systm.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>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <netinet/in.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
65df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
66df8bae1dSRodney W. Grimes 
67322dcb8dSMax Khon #include <net/if_arc.h>
68fda82fc2SJulian Elischer #include <net/iso88025.h>
69fda82fc2SJulian Elischer 
70a9771948SGleb Smirnoff #ifdef DEV_CARP
71a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
72a9771948SGleb Smirnoff #endif
73a9771948SGleb Smirnoff 
74aed55708SRobert Watson #include <security/mac/mac_framework.h>
75aed55708SRobert Watson 
76df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s)
77df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s)
78df8bae1dSRodney W. Grimes 
79ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether);
80602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes /* timer values */
83602d513cSGarrett Wollman static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
84885f1aa4SPoul-Henning Kamp 
85602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
86bd2b686fSEd Maste 	   &arpt_keep, 0, "ARP entry lifetime in seconds");
87885f1aa4SPoul-Henning Kamp 
88df8bae1dSRodney W. Grimes #define	rt_expire rt_rmx.rmx_expire
89df8bae1dSRodney W. Grimes 
9066800f57SGarrett Wollman struct llinfo_arp {
911daaa65dSGleb Smirnoff 	struct	callout la_timer;
9266800f57SGarrett Wollman 	struct	rtentry *la_rt;
9366800f57SGarrett Wollman 	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
94022695f8SOrion Hodson 	u_short	la_preempt;	/* countdown for pre-expiry arps */
95e1ff74c5SGleb Smirnoff 	u_short	la_asked;	/* # requests sent */
9666800f57SGarrett Wollman };
9766800f57SGarrett Wollman 
981cafed39SJonathan Lemon static struct	ifqueue arpintrq;
99d1dd20beSSam Leffler static int	arp_allocated;
100df8bae1dSRodney W. Grimes 
101885f1aa4SPoul-Henning Kamp static int	arp_maxtries = 5;
102602d513cSGarrett Wollman static int	useloopback = 1; /* use loopback interface for local traffic */
103885f1aa4SPoul-Henning Kamp static int	arp_proxyall = 0;
104602d513cSGarrett Wollman 
105602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
106bd2b686fSEd Maste 	   &arp_maxtries, 0, "ARP resolution attempts before returning error");
107602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
108bd2b686fSEd Maste 	   &useloopback, 0, "Use the loopback interface for local traffic");
109602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
110bd2b686fSEd Maste 	   &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
111885f1aa4SPoul-Henning Kamp 
1124d77a549SAlfred Perlstein static void	arp_init(void);
1134d77a549SAlfred Perlstein static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
1144d77a549SAlfred Perlstein static void	arprequest(struct ifnet *,
1154d77a549SAlfred Perlstein 			struct in_addr *, struct in_addr *, u_char *);
1161cafed39SJonathan Lemon static void	arpintr(struct mbuf *);
1174d77a549SAlfred Perlstein static void	arptimer(void *);
1181ed7bf1eSGleb Smirnoff static struct rtentry
1194d77a549SAlfred Perlstein 		*arplookup(u_long, int, int);
1201d5e9e22SEivind Eklund #ifdef INET
1214d77a549SAlfred Perlstein static void	in_arpinput(struct mbuf *);
1221d5e9e22SEivind Eklund #endif
12328e82295SGarrett Wollman 
124df8bae1dSRodney W. Grimes /*
1251daaa65dSGleb Smirnoff  * Timeout routine.
126df8bae1dSRodney W. Grimes  */
127df8bae1dSRodney W. Grimes static void
1281daaa65dSGleb Smirnoff arptimer(void *arg)
129df8bae1dSRodney W. Grimes {
1301daaa65dSGleb Smirnoff 	struct rtentry *rt = (struct rtentry *)arg;
131df8bae1dSRodney W. Grimes 
1321daaa65dSGleb Smirnoff 	RT_LOCK_ASSERT(rt);
1331ed7bf1eSGleb Smirnoff 	/*
1341daaa65dSGleb Smirnoff 	 * The lock is needed to close a theoretical race
1351daaa65dSGleb Smirnoff 	 * between spontaneous expiry and intentional removal.
1361daaa65dSGleb Smirnoff 	 * We still got an extra reference on rtentry, so can
1371daaa65dSGleb Smirnoff 	 * safely pass pointers to its contents.
1381ed7bf1eSGleb Smirnoff 	 */
1391ed7bf1eSGleb Smirnoff 	RT_UNLOCK(rt);
140d1dd20beSSam Leffler 
1411daaa65dSGleb Smirnoff 	rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
142df8bae1dSRodney W. Grimes }
143df8bae1dSRodney W. Grimes 
144df8bae1dSRodney W. Grimes /*
145df8bae1dSRodney W. Grimes  * Parallel to llc_rtrequest.
146df8bae1dSRodney W. Grimes  */
147b2774d00SGarrett Wollman static void
148f2565d68SRobert Watson arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
149df8bae1dSRodney W. Grimes {
150e952fa39SMatthew N. Dodd 	struct sockaddr *gate;
151e952fa39SMatthew N. Dodd 	struct llinfo_arp *la;
152df8bae1dSRodney W. Grimes 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
153067a8babSMax Laier 	struct in_ifaddr *ia;
154067a8babSMax Laier 	struct ifaddr *ifa;
155df8bae1dSRodney W. Grimes 
156d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
157d1dd20beSSam Leffler 
158df8bae1dSRodney W. Grimes 	if (rt->rt_flags & RTF_GATEWAY)
159df8bae1dSRodney W. Grimes 		return;
160d1dd20beSSam Leffler 	gate = rt->rt_gateway;
161d1dd20beSSam Leffler 	la = (struct llinfo_arp *)rt->rt_llinfo;
162df8bae1dSRodney W. Grimes 	switch (req) {
163df8bae1dSRodney W. Grimes 
164df8bae1dSRodney W. Grimes 	case RTM_ADD:
165df8bae1dSRodney W. Grimes 		/*
166df8bae1dSRodney W. Grimes 		 * XXX: If this is a manually added route to interface
167df8bae1dSRodney W. Grimes 		 * such as older version of routed or gated might provide,
168df8bae1dSRodney W. Grimes 		 * restore cloning bit.
169df8bae1dSRodney W. Grimes 		 */
170df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_HOST) == 0 &&
171d6fa5d28SBruce M Simpson 		    rt_mask(rt) != NULL &&
172df8bae1dSRodney W. Grimes 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
173df8bae1dSRodney W. Grimes 			rt->rt_flags |= RTF_CLONING;
174df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_CLONING) {
175df8bae1dSRodney W. Grimes 			/*
176df8bae1dSRodney W. Grimes 			 * Case 1: This route should come from a route to iface.
177df8bae1dSRodney W. Grimes 			 */
178df8bae1dSRodney W. Grimes 			rt_setgate(rt, rt_key(rt),
179df8bae1dSRodney W. Grimes 					(struct sockaddr *)&null_sdl);
180df8bae1dSRodney W. Grimes 			gate = rt->rt_gateway;
181df8bae1dSRodney W. Grimes 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
182df8bae1dSRodney W. Grimes 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
183fe53256dSAndre Oppermann 			rt->rt_expire = time_uptime;
184df8bae1dSRodney W. Grimes 			break;
185df8bae1dSRodney W. Grimes 		}
186df8bae1dSRodney W. Grimes 		/* Announce a new entry if requested. */
187df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_ANNOUNCE)
188322dcb8dSMax Khon 			arprequest(rt->rt_ifp,
189ed7509acSJulian Elischer 			    &SIN(rt_key(rt))->sin_addr,
190ed7509acSJulian Elischer 			    &SIN(rt_key(rt))->sin_addr,
191df8bae1dSRodney W. Grimes 			    (u_char *)LLADDR(SDL(gate)));
192df8bae1dSRodney W. Grimes 		/*FALLTHROUGH*/
193df8bae1dSRodney W. Grimes 	case RTM_RESOLVE:
194df8bae1dSRodney W. Grimes 		if (gate->sa_family != AF_LINK ||
195df8bae1dSRodney W. Grimes 		    gate->sa_len < sizeof(null_sdl)) {
196d1dd20beSSam Leffler 			log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
197beb2ced8SBruce M Simpson 			    inet_ntoa(SIN(rt_key(rt))->sin_addr),
198beb2ced8SBruce M Simpson 			    (gate->sa_family != AF_LINK) ?
199c3b52d64SBruce M Simpson 			    " (!AF_LINK)": "");
200df8bae1dSRodney W. Grimes 			break;
201df8bae1dSRodney W. Grimes 		}
202df8bae1dSRodney W. Grimes 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
203df8bae1dSRodney W. Grimes 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
204df8bae1dSRodney W. Grimes 		if (la != 0)
205df8bae1dSRodney W. Grimes 			break; /* This happens on a route change */
206df8bae1dSRodney W. Grimes 		/*
207df8bae1dSRodney W. Grimes 		 * Case 2:  This route may come from cloning, or a manual route
208df8bae1dSRodney W. Grimes 		 * add with a LL address.
209df8bae1dSRodney W. Grimes 		 */
210d1dd20beSSam Leffler 		R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
211df8bae1dSRodney W. Grimes 		rt->rt_llinfo = (caddr_t)la;
212df8bae1dSRodney W. Grimes 		if (la == 0) {
213d1dd20beSSam Leffler 			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
214df8bae1dSRodney W. Grimes 			break;
215df8bae1dSRodney W. Grimes 		}
216d1dd20beSSam Leffler 		arp_allocated++;
2171ed7bf1eSGleb Smirnoff 		/*
2181ed7bf1eSGleb Smirnoff 		 * We are storing a route entry outside of radix tree. So,
2191ed7bf1eSGleb Smirnoff 		 * it can be found and accessed by other means than radix
2201ed7bf1eSGleb Smirnoff 		 * lookup. The routing code assumes that any rtentry detached
2211ed7bf1eSGleb Smirnoff 		 * from radix can be destroyed safely. To prevent this, we
2221ed7bf1eSGleb Smirnoff 		 * add an additional reference.
2231ed7bf1eSGleb Smirnoff 		 */
2241ed7bf1eSGleb Smirnoff 		RT_ADDREF(rt);
225df8bae1dSRodney W. Grimes 		la->la_rt = rt;
226df8bae1dSRodney W. Grimes 		rt->rt_flags |= RTF_LLINFO;
2271daaa65dSGleb Smirnoff 		callout_init_mtx(&la->la_timer, &rt->rt_mtx,
2281daaa65dSGleb Smirnoff 		    CALLOUT_RETURNUNLOCKED);
229cbb0b46aSGarrett Wollman 
2301d5e9e22SEivind Eklund #ifdef INET
231cbb0b46aSGarrett Wollman 		/*
232cbb0b46aSGarrett Wollman 		 * This keeps the multicast addresses from showing up
233cbb0b46aSGarrett Wollman 		 * in `arp -a' listings as unresolved.  It's not actually
234cbb0b46aSGarrett Wollman 		 * functional.  Then the same for broadcast.
235cbb0b46aSGarrett Wollman 		 */
236c448c89cSJonathan Lemon 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
237c448c89cSJonathan Lemon 		    rt->rt_ifp->if_type != IFT_ARCNET) {
238cbb0b46aSGarrett Wollman 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
239cbb0b46aSGarrett Wollman 					       LLADDR(SDL(gate)));
240cbb0b46aSGarrett Wollman 			SDL(gate)->sdl_alen = 6;
24151a109a1SPeter Wemm 			rt->rt_expire = 0;
242cbb0b46aSGarrett Wollman 		}
243cbb0b46aSGarrett Wollman 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
244322dcb8dSMax Khon 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
245322dcb8dSMax Khon 			       rt->rt_ifp->if_addrlen);
246322dcb8dSMax Khon 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
24751a109a1SPeter Wemm 			rt->rt_expire = 0;
248cbb0b46aSGarrett Wollman 		}
2491d5e9e22SEivind Eklund #endif
250cbb0b46aSGarrett Wollman 
251067a8babSMax Laier 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
252067a8babSMax Laier 			if (ia->ia_ifp == rt->rt_ifp &&
253067a8babSMax Laier 			    SIN(rt_key(rt))->sin_addr.s_addr ==
254067a8babSMax Laier 			    (IA_SIN(ia))->sin_addr.s_addr)
255067a8babSMax Laier 				break;
256067a8babSMax Laier 		}
257067a8babSMax Laier 		if (ia) {
258df8bae1dSRodney W. Grimes 		    /*
259df8bae1dSRodney W. Grimes 		     * This test used to be
260df8bae1dSRodney W. Grimes 		     *	if (loif.if_flags & IFF_UP)
261df8bae1dSRodney W. Grimes 		     * It allowed local traffic to be forced
262df8bae1dSRodney W. Grimes 		     * through the hardware by configuring the loopback down.
263df8bae1dSRodney W. Grimes 		     * However, it causes problems during network configuration
264df8bae1dSRodney W. Grimes 		     * for boards that can't receive packets they send.
265df8bae1dSRodney W. Grimes 		     * It is now necessary to clear "useloopback" and remove
266df8bae1dSRodney W. Grimes 		     * the route to force traffic out to the hardware.
267df8bae1dSRodney W. Grimes 		     */
268df8bae1dSRodney W. Grimes 			rt->rt_expire = 0;
269ac912b2dSLuigi Rizzo 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
270322dcb8dSMax Khon 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
271402865f6SJohn-Mark Gurney 			if (useloopback) {
272f5fea3ddSPaul Traina 				rt->rt_ifp = loif;
273402865f6SJohn-Mark Gurney 				rt->rt_rmx.rmx_mtu = loif->if_mtu;
274402865f6SJohn-Mark Gurney 			}
275df8bae1dSRodney W. Grimes 
276067a8babSMax Laier 		    /*
277067a8babSMax Laier 		     * make sure to set rt->rt_ifa to the interface
278067a8babSMax Laier 		     * address we are using, otherwise we will have trouble
279067a8babSMax Laier 		     * with source address selection.
280067a8babSMax Laier 		     */
281067a8babSMax Laier 			ifa = &ia->ia_ifa;
282067a8babSMax Laier 			if (ifa != rt->rt_ifa) {
283067a8babSMax Laier 				IFAFREE(rt->rt_ifa);
284067a8babSMax Laier 				IFAREF(ifa);
285067a8babSMax Laier 				rt->rt_ifa = ifa;
286067a8babSMax Laier 			}
287df8bae1dSRodney W. Grimes 		}
288df8bae1dSRodney W. Grimes 		break;
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes 	case RTM_DELETE:
2911daaa65dSGleb Smirnoff 		if (la == NULL)	/* XXX: at least CARP does this. */
292df8bae1dSRodney W. Grimes 			break;
2931daaa65dSGleb Smirnoff 		callout_stop(&la->la_timer);
2941daaa65dSGleb Smirnoff 		rt->rt_llinfo = NULL;
295df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_LLINFO;
2961daaa65dSGleb Smirnoff 		RT_REMREF(rt);
297df8bae1dSRodney W. Grimes 		if (la->la_hold)
298df8bae1dSRodney W. Grimes 			m_freem(la->la_hold);
299df8bae1dSRodney W. Grimes 		Free((caddr_t)la);
300df8bae1dSRodney W. Grimes 	}
301df8bae1dSRodney W. Grimes }
302df8bae1dSRodney W. Grimes 
303df8bae1dSRodney W. Grimes /*
304df8bae1dSRodney W. Grimes  * Broadcast an ARP request. Caller specifies:
305df8bae1dSRodney W. Grimes  *	- arp header source ip address
306df8bae1dSRodney W. Grimes  *	- arp header target ip address
307df8bae1dSRodney W. Grimes  *	- arp header source ethernet address
308df8bae1dSRodney W. Grimes  */
309df8bae1dSRodney W. Grimes static void
310f2565d68SRobert Watson arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
311f2565d68SRobert Watson     u_char *enaddr)
312df8bae1dSRodney W. Grimes {
313e952fa39SMatthew N. Dodd 	struct mbuf *m;
314e952fa39SMatthew N. Dodd 	struct arphdr *ah;
315df8bae1dSRodney W. Grimes 	struct sockaddr sa;
316df8bae1dSRodney W. Grimes 
317a163d034SWarner Losh 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
318df8bae1dSRodney W. Grimes 		return;
31964bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
32064bf80ceSMatthew N. Dodd 		2*ifp->if_data.ifi_addrlen;
32164bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
32264bf80ceSMatthew N. Dodd 	MH_ALIGN(m, m->m_len);
32364bf80ceSMatthew N. Dodd 	ah = mtod(m, struct arphdr *);
32464bf80ceSMatthew N. Dodd 	bzero((caddr_t)ah, m->m_len);
32519527d3eSRobert Watson #ifdef MAC
326b9b0dac3SRobert Watson 	mac_netinet_arp_send(ifp, m);
32719527d3eSRobert Watson #endif
328322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP);
329322dcb8dSMax Khon 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
330322dcb8dSMax Khon 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
331322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REQUEST);
33264bf80ceSMatthew N. Dodd 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
33364bf80ceSMatthew N. Dodd 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
33464bf80ceSMatthew N. Dodd 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
33564bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
33664bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
33764bf80ceSMatthew N. Dodd 	m->m_flags |= M_BCAST;
338322dcb8dSMax Khon 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
33964bf80ceSMatthew N. Dodd 
34064bf80ceSMatthew N. Dodd 	return;
341df8bae1dSRodney W. Grimes }
342df8bae1dSRodney W. Grimes 
343df8bae1dSRodney W. Grimes /*
344cd46a114SLuigi Rizzo  * Resolve an IP address into an ethernet address.
345cd46a114SLuigi Rizzo  * On input:
346cd46a114SLuigi Rizzo  *    ifp is the interface we use
347cd46a114SLuigi Rizzo  *    dst is the next hop,
348cd46a114SLuigi Rizzo  *    rt0 is the route to the final destination (possibly useless)
349cd46a114SLuigi Rizzo  *    m is the mbuf
350cd46a114SLuigi Rizzo  *    desten is where we want the address.
351cd46a114SLuigi Rizzo  *
352cd46a114SLuigi Rizzo  * On success, desten is filled in and the function returns 0;
353cd46a114SLuigi Rizzo  * If the packet must be held pending resolution, we return EWOULDBLOCK
354cd46a114SLuigi Rizzo  * On other errors, we return the corresponding error code.
355df8bae1dSRodney W. Grimes  */
356df8bae1dSRodney W. Grimes int
357cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
358f7c5baa1SLuigi Rizzo     struct sockaddr *dst, u_char *desten)
359df8bae1dSRodney W. Grimes {
3601ed7bf1eSGleb Smirnoff 	struct llinfo_arp *la = NULL;
3611ed7bf1eSGleb Smirnoff 	struct rtentry *rt = NULL;
362df8bae1dSRodney W. Grimes 	struct sockaddr_dl *sdl;
363cd46a114SLuigi Rizzo 	int error;
364df8bae1dSRodney W. Grimes 
365df8bae1dSRodney W. Grimes 	if (m->m_flags & M_BCAST) {	/* broadcast */
366322dcb8dSMax Khon 		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
367cd46a114SLuigi Rizzo 		return (0);
368df8bae1dSRodney W. Grimes 	}
369322dcb8dSMax Khon 	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
370df8bae1dSRodney W. Grimes 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
371cd46a114SLuigi Rizzo 		return (0);
372df8bae1dSRodney W. Grimes 	}
3731ed7bf1eSGleb Smirnoff 
3741ed7bf1eSGleb Smirnoff 	if (rt0 != NULL) {
3751ed7bf1eSGleb Smirnoff 		error = rt_check(&rt, &rt0, dst);
3761ed7bf1eSGleb Smirnoff 		if (error) {
3771ed7bf1eSGleb Smirnoff 			m_freem(m);
3781ed7bf1eSGleb Smirnoff 			return error;
379df8bae1dSRodney W. Grimes 		}
3801ed7bf1eSGleb Smirnoff 		la = (struct llinfo_arp *)rt->rt_llinfo;
3811ed7bf1eSGleb Smirnoff 		if (la == NULL)
3821ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
3831ed7bf1eSGleb Smirnoff 	}
3841ed7bf1eSGleb Smirnoff 	if (la == NULL) {
3851ed7bf1eSGleb Smirnoff 		/*
3861ed7bf1eSGleb Smirnoff 		 * We enter this block in case if rt0 was NULL,
3871ed7bf1eSGleb Smirnoff 		 * or if rt found by rt_check() didn't have llinfo.
3881ed7bf1eSGleb Smirnoff 		 */
3891ed7bf1eSGleb Smirnoff 		rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
3901ed7bf1eSGleb Smirnoff 		if (rt == NULL) {
3911ed7bf1eSGleb Smirnoff 			log(LOG_DEBUG,
3921ed7bf1eSGleb Smirnoff 			    "arpresolve: can't allocate route for %s\n",
3931ed7bf1eSGleb Smirnoff 			    inet_ntoa(SIN(dst)->sin_addr));
394df8bae1dSRodney W. Grimes 			m_freem(m);
395cd46a114SLuigi Rizzo 			return (EINVAL); /* XXX */
396df8bae1dSRodney W. Grimes 		}
3971ed7bf1eSGleb Smirnoff 		la = (struct llinfo_arp *)rt->rt_llinfo;
3981ed7bf1eSGleb Smirnoff 		if (la == NULL) {
3991ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
4001ed7bf1eSGleb Smirnoff 			log(LOG_DEBUG,
4011ed7bf1eSGleb Smirnoff 			    "arpresolve: can't allocate llinfo for %s\n",
4021ed7bf1eSGleb Smirnoff 			    inet_ntoa(SIN(dst)->sin_addr));
4031ed7bf1eSGleb Smirnoff 			m_freem(m);
4041ed7bf1eSGleb Smirnoff 			return (EINVAL); /* XXX */
4051ed7bf1eSGleb Smirnoff 		}
4061ed7bf1eSGleb Smirnoff 	}
407df8bae1dSRodney W. Grimes 	sdl = SDL(rt->rt_gateway);
408df8bae1dSRodney W. Grimes 	/*
409df8bae1dSRodney W. Grimes 	 * Check the address family and length is valid, the address
410df8bae1dSRodney W. Grimes 	 * is resolved; otherwise, try to resolve.
411df8bae1dSRodney W. Grimes 	 */
412fe53256dSAndre Oppermann 	if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
413df8bae1dSRodney W. Grimes 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
414a20e2538SGleb Smirnoff 
415a20e2538SGleb Smirnoff 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
416a20e2538SGleb Smirnoff 
417f0f3379eSOrion Hodson 		/*
418f0f3379eSOrion Hodson 		 * If entry has an expiry time and it is approaching,
419e1ff74c5SGleb Smirnoff 		 * send an ARP request.
420f0f3379eSOrion Hodson 		 */
421f0f3379eSOrion Hodson 		if ((rt->rt_expire != 0) &&
422fe53256dSAndre Oppermann 		    (time_uptime + la->la_preempt > rt->rt_expire)) {
423a20e2538SGleb Smirnoff 			struct in_addr sin =
424a20e2538SGleb Smirnoff 			    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
425a20e2538SGleb Smirnoff 
426022695f8SOrion Hodson 			la->la_preempt--;
427a20e2538SGleb Smirnoff 			RT_UNLOCK(rt);
428a20e2538SGleb Smirnoff 			arprequest(ifp, &sin, &SIN(dst)->sin_addr,
429a20e2538SGleb Smirnoff 			    IF_LLADDR(ifp));
430a20e2538SGleb Smirnoff 			return (0);
431f0f3379eSOrion Hodson 		}
432f0f3379eSOrion Hodson 
4331ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
434cd46a114SLuigi Rizzo 		return (0);
435df8bae1dSRodney W. Grimes 	}
436df8bae1dSRodney W. Grimes 	/*
437deb62e28SRuslan Ermilov 	 * If ARP is disabled or static on this interface, stop.
43808aadfbbSJonathan Lemon 	 * XXX
43908aadfbbSJonathan Lemon 	 * Probably should not allocate empty llinfo struct if we are
44008aadfbbSJonathan Lemon 	 * not going to be sending out an arp request.
44108aadfbbSJonathan Lemon 	 */
442deb62e28SRuslan Ermilov 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
4431ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
44447891de1SRuslan Ermilov 		m_freem(m);
445cd46a114SLuigi Rizzo 		return (EINVAL);
44647891de1SRuslan Ermilov 	}
44708aadfbbSJonathan Lemon 	/*
448df8bae1dSRodney W. Grimes 	 * There is an arptab entry, but no ethernet address
449df8bae1dSRodney W. Grimes 	 * response yet.  Replace the held mbuf with this
450df8bae1dSRodney W. Grimes 	 * latest one.
451df8bae1dSRodney W. Grimes 	 */
452df8bae1dSRodney W. Grimes 	if (la->la_hold)
453df8bae1dSRodney W. Grimes 		m_freem(la->la_hold);
454df8bae1dSRodney W. Grimes 	la->la_hold = m;
45529910a5aSKip Macy 	KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
45629910a5aSKip Macy 
45729910a5aSKip Macy 	/*
45829910a5aSKip Macy 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
45929910a5aSKip Macy 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
46029910a5aSKip Macy 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
46129910a5aSKip Macy 	 * ARP request, but not faster than one request per second.
46229910a5aSKip Macy 	 */
46329910a5aSKip Macy 	if (la->la_asked < arp_maxtries)
46429910a5aSKip Macy 		error = EWOULDBLOCK;	/* First request. */
46529910a5aSKip Macy 	else
46629910a5aSKip Macy 		error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
46729910a5aSKip Macy 
46829910a5aSKip Macy 	if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
46929910a5aSKip Macy 		struct in_addr sin =
47029910a5aSKip Macy 		    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
47129910a5aSKip Macy 
47229910a5aSKip Macy 		rt->rt_expire = time_uptime;
47329910a5aSKip Macy 		callout_reset(&la->la_timer, hz, arptimer, rt);
47429910a5aSKip Macy 		la->la_asked++;
47529910a5aSKip Macy 		RT_UNLOCK(rt);
47629910a5aSKip Macy 
47729910a5aSKip Macy 		arprequest(ifp, &sin, &SIN(dst)->sin_addr,
47829910a5aSKip Macy 		    IF_LLADDR(ifp));
47929910a5aSKip Macy 	} else
48029910a5aSKip Macy 		RT_UNLOCK(rt);
48129910a5aSKip Macy 
48229910a5aSKip Macy 	return (error);
48329910a5aSKip Macy }
48429910a5aSKip Macy 
48529910a5aSKip Macy 
48629910a5aSKip Macy int
48729910a5aSKip Macy arpresolve2(struct ifnet *ifp, struct rtentry *rt0, struct sockaddr *dst,
48829910a5aSKip Macy     u_char *desten)
48929910a5aSKip Macy {
49029910a5aSKip Macy 	struct llinfo_arp *la = NULL;
49129910a5aSKip Macy 	struct rtentry *rt = NULL;
49229910a5aSKip Macy 	struct sockaddr_dl *sdl;
49329910a5aSKip Macy 	int error;
49429910a5aSKip Macy 
49529910a5aSKip Macy 	if (rt0 != NULL) {
49629910a5aSKip Macy 		error = rt_check(&rt, &rt0, dst);
49729910a5aSKip Macy 		if (error)
49829910a5aSKip Macy 			return (error);
49929910a5aSKip Macy 
50029910a5aSKip Macy 		la = (struct llinfo_arp *)rt->rt_llinfo;
50129910a5aSKip Macy 		if (la == NULL)
50229910a5aSKip Macy 			RT_UNLOCK(rt);
50329910a5aSKip Macy 	}
50429910a5aSKip Macy 	if (la == NULL) {
50529910a5aSKip Macy 		/*
50629910a5aSKip Macy 		 * We enter this block in case if rt0 was NULL,
50729910a5aSKip Macy 		 * or if rt found by rt_check() didn't have llinfo.
50829910a5aSKip Macy 		 */
50929910a5aSKip Macy 		rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
51029910a5aSKip Macy 		if (rt == NULL) {
51129910a5aSKip Macy 			log(LOG_DEBUG,
51229910a5aSKip Macy 			    "arpresolve: can't allocate route for %s\n",
51329910a5aSKip Macy 			    inet_ntoa(SIN(dst)->sin_addr));
51429910a5aSKip Macy 			return (EINVAL); /* XXX */
51529910a5aSKip Macy 		}
51629910a5aSKip Macy 		la = (struct llinfo_arp *)rt->rt_llinfo;
51729910a5aSKip Macy 		if (la == NULL) {
51829910a5aSKip Macy 			RT_UNLOCK(rt);
51929910a5aSKip Macy 			log(LOG_DEBUG,
52029910a5aSKip Macy 			    "arpresolve: can't allocate llinfo for %s\n",
52129910a5aSKip Macy 			    inet_ntoa(SIN(dst)->sin_addr));
52229910a5aSKip Macy 			return (EINVAL); /* XXX */
52329910a5aSKip Macy 		}
52429910a5aSKip Macy 	}
52529910a5aSKip Macy 	sdl = SDL(rt->rt_gateway);
52629910a5aSKip Macy 	/*
52729910a5aSKip Macy 	 * Check the address family and length is valid, the address
52829910a5aSKip Macy 	 * is resolved; otherwise, try to resolve.
52929910a5aSKip Macy 	 */
53029910a5aSKip Macy 	if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
53129910a5aSKip Macy 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
53229910a5aSKip Macy 
53329910a5aSKip Macy 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
53429910a5aSKip Macy 
53529910a5aSKip Macy 		/*
53629910a5aSKip Macy 		 * If entry has an expiry time and it is approaching,
53729910a5aSKip Macy 		 * send an ARP request.
53829910a5aSKip Macy 		 */
53929910a5aSKip Macy 		if ((rt->rt_expire != 0) &&
54029910a5aSKip Macy 		    (time_uptime + la->la_preempt > rt->rt_expire)) {
54129910a5aSKip Macy 			struct in_addr sin =
54229910a5aSKip Macy 			    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
54329910a5aSKip Macy 
54429910a5aSKip Macy 			la->la_preempt--;
54529910a5aSKip Macy 			RT_UNLOCK(rt);
54629910a5aSKip Macy 			arprequest(ifp, &sin, &SIN(dst)->sin_addr,
54729910a5aSKip Macy 			    IF_LLADDR(ifp));
54829910a5aSKip Macy 			return (0);
54929910a5aSKip Macy 		}
55029910a5aSKip Macy 
55129910a5aSKip Macy 		RT_UNLOCK(rt);
55229910a5aSKip Macy 		return (0);
55329910a5aSKip Macy 	}
55429910a5aSKip Macy 	/*
55529910a5aSKip Macy 	 * If ARP is disabled or static on this interface, stop.
55629910a5aSKip Macy 	 * XXX
55729910a5aSKip Macy 	 * Probably should not allocate empty llinfo struct if we are
55829910a5aSKip Macy 	 * not going to be sending out an arp request.
55929910a5aSKip Macy 	 */
56029910a5aSKip Macy 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
56129910a5aSKip Macy 		RT_UNLOCK(rt);
56229910a5aSKip Macy 		return (EINVAL);
56358505389SKip Macy 	}
564e1ff74c5SGleb Smirnoff 	KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
565e1ff74c5SGleb Smirnoff 
566e1ff74c5SGleb Smirnoff 	/*
567e1ff74c5SGleb Smirnoff 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
568e1ff74c5SGleb Smirnoff 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
569e1ff74c5SGleb Smirnoff 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
570e1ff74c5SGleb Smirnoff 	 * ARP request, but not faster than one request per second.
571e1ff74c5SGleb Smirnoff 	 */
572e1ff74c5SGleb Smirnoff 	if (la->la_asked < arp_maxtries)
573e1ff74c5SGleb Smirnoff 		error = EWOULDBLOCK;	/* First request. */
574e1ff74c5SGleb Smirnoff 	else
575e1ff74c5SGleb Smirnoff 		error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
576e1ff74c5SGleb Smirnoff 
57795ebcabeSMaxim Konovalov 	if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
578a20e2538SGleb Smirnoff 		struct in_addr sin =
579a20e2538SGleb Smirnoff 		    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
580a20e2538SGleb Smirnoff 
581e1ff74c5SGleb Smirnoff 		rt->rt_expire = time_uptime;
5821daaa65dSGleb Smirnoff 		callout_reset(&la->la_timer, hz, arptimer, rt);
58395ebcabeSMaxim Konovalov 		la->la_asked++;
584a20e2538SGleb Smirnoff 		RT_UNLOCK(rt);
585e1ff74c5SGleb Smirnoff 
586a20e2538SGleb Smirnoff 		arprequest(ifp, &sin, &SIN(dst)->sin_addr,
587322dcb8dSMax Khon 		    IF_LLADDR(ifp));
588e1ff74c5SGleb Smirnoff 	} else
5891ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
590e1ff74c5SGleb Smirnoff 
591e1ff74c5SGleb Smirnoff 	return (error);
592df8bae1dSRodney W. Grimes }
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes /*
595df8bae1dSRodney W. Grimes  * Common length and type checks are done here,
596df8bae1dSRodney W. Grimes  * then the protocol-specific routine is called.
597df8bae1dSRodney W. Grimes  */
598885f1aa4SPoul-Henning Kamp static void
5991cafed39SJonathan Lemon arpintr(struct mbuf *m)
600df8bae1dSRodney W. Grimes {
6011cafed39SJonathan Lemon 	struct arphdr *ar;
602df8bae1dSRodney W. Grimes 
60376ec7b2fSRobert Watson 	if (m->m_len < sizeof(struct arphdr) &&
60484365e2bSMatthew Dillon 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
605e44d6283SJoerg Wunsch 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
6061cafed39SJonathan Lemon 		return;
60776ec7b2fSRobert Watson 	}
60876ec7b2fSRobert Watson 	ar = mtod(m, struct arphdr *);
60976ec7b2fSRobert Watson 
6101cafed39SJonathan Lemon 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
6111cafed39SJonathan Lemon 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
612b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
613b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
6141cafed39SJonathan Lemon 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
61576ec7b2fSRobert Watson 		    (unsigned char *)&ar->ar_hrd, "");
61676ec7b2fSRobert Watson 		m_freem(m);
6171cafed39SJonathan Lemon 		return;
61876ec7b2fSRobert Watson 	}
61976ec7b2fSRobert Watson 
62031175791SRuslan Ermilov 	if (m->m_len < arphdr_len(ar)) {
62178e2d2bdSRuslan Ermilov 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
622f72d9d83SJoerg Wunsch 			log(LOG_ERR, "arp: runt packet\n");
62376ec7b2fSRobert Watson 			m_freem(m);
6241cafed39SJonathan Lemon 			return;
62576ec7b2fSRobert Watson 		}
62678e2d2bdSRuslan Ermilov 		ar = mtod(m, struct arphdr *);
62778e2d2bdSRuslan Ermilov 	}
628df8bae1dSRodney W. Grimes 
629df8bae1dSRodney W. Grimes 	switch (ntohs(ar->ar_pro)) {
6301d5e9e22SEivind Eklund #ifdef INET
631df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
632df8bae1dSRodney W. Grimes 		in_arpinput(m);
6331cafed39SJonathan Lemon 		return;
6341d5e9e22SEivind Eklund #endif
635df8bae1dSRodney W. Grimes 	}
636df8bae1dSRodney W. Grimes 	m_freem(m);
637df8bae1dSRodney W. Grimes }
638df8bae1dSRodney W. Grimes 
6391d5e9e22SEivind Eklund #ifdef INET
640df8bae1dSRodney W. Grimes /*
641df8bae1dSRodney W. Grimes  * ARP for Internet protocols on 10 Mb/s Ethernet.
642df8bae1dSRodney W. Grimes  * Algorithm is that given in RFC 826.
643df8bae1dSRodney W. Grimes  * In addition, a sanity check is performed on the sender
644df8bae1dSRodney W. Grimes  * protocol address, to catch impersonators.
645df8bae1dSRodney W. Grimes  * We no longer handle negotiations for use of trailer protocol:
646df8bae1dSRodney W. Grimes  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
647df8bae1dSRodney W. Grimes  * along with IP replies if we wanted trailers sent to us,
648df8bae1dSRodney W. Grimes  * and also sent them in response to IP replies.
649df8bae1dSRodney W. Grimes  * This allowed either end to announce the desire to receive
650df8bae1dSRodney W. Grimes  * trailer packets.
651df8bae1dSRodney W. Grimes  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
652df8bae1dSRodney W. Grimes  * but formerly didn't normally send requests.
653df8bae1dSRodney W. Grimes  */
6543269187dSAlfred Perlstein static int log_arp_wrong_iface = 1;
655e3d123d6SAlfred Perlstein static int log_arp_movements = 1;
65639393906SGleb Smirnoff static int log_arp_permanent_modify = 1;
6573269187dSAlfred Perlstein 
6583269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
6593269187dSAlfred Perlstein 	&log_arp_wrong_iface, 0,
6603269187dSAlfred Perlstein 	"log arp packets arriving on the wrong interface");
661e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
662e3d123d6SAlfred Perlstein         &log_arp_movements, 0,
66375ce3221SAlfred Perlstein         "log arp replies from MACs different than the one in the cache");
66439393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
66539393906SGleb Smirnoff         &log_arp_permanent_modify, 0,
66639393906SGleb Smirnoff         "log arp replies from MACs different than the one in the permanent arp entry");
667e3d123d6SAlfred Perlstein 
6683269187dSAlfred Perlstein 
669df8bae1dSRodney W. Grimes static void
670f2565d68SRobert Watson in_arpinput(struct mbuf *m)
671df8bae1dSRodney W. Grimes {
672e952fa39SMatthew N. Dodd 	struct arphdr *ah;
673e952fa39SMatthew N. Dodd 	struct ifnet *ifp = m->m_pkthdr.rcvif;
6741ed7bf1eSGleb Smirnoff 	struct llinfo_arp *la;
675e952fa39SMatthew N. Dodd 	struct rtentry *rt;
676ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
677ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
678df8bae1dSRodney W. Grimes 	struct sockaddr_dl *sdl;
679df8bae1dSRodney W. Grimes 	struct sockaddr sa;
680df8bae1dSRodney W. Grimes 	struct in_addr isaddr, itaddr, myaddr;
6811ed7bf1eSGleb Smirnoff 	struct mbuf *hold;
682a9771948SGleb Smirnoff 	u_int8_t *enaddr = NULL;
683b149dd6cSLarry Lile 	int op, rif_len;
684322dcb8dSMax Khon 	int req_len;
6858f867517SAndrew Thompson 	int bridged = 0;
686422a115aSGleb Smirnoff #ifdef DEV_CARP
6872ef4a436SGleb Smirnoff 	int carp_match = 0;
688422a115aSGleb Smirnoff #endif
6898e7e854cSKip Macy 	struct sockaddr_in sin;
6908e7e854cSKip Macy 	sin.sin_len = sizeof(struct sockaddr_in);
6918e7e854cSKip Macy 	sin.sin_family = AF_INET;
69229910a5aSKip Macy 	sin.sin_addr.s_addr = 0;
693df8bae1dSRodney W. Grimes 
69474948aa6SAndrew Thompson 	if (ifp->if_bridge)
6958f867517SAndrew Thompson 		bridged = 1;
6968f867517SAndrew Thompson 
697322dcb8dSMax Khon 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
698322dcb8dSMax Khon 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
6994cbc8ad1SYaroslav Tykhiy 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
7004cbc8ad1SYaroslav Tykhiy 		return;
7014cbc8ad1SYaroslav Tykhiy 	}
7024cbc8ad1SYaroslav Tykhiy 
703322dcb8dSMax Khon 	ah = mtod(m, struct arphdr *);
704322dcb8dSMax Khon 	op = ntohs(ah->ar_op);
705322dcb8dSMax Khon 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
706322dcb8dSMax Khon 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
70732439868SGleb Smirnoff 
708ca925d9cSJonathan Lemon 	/*
709ca925d9cSJonathan Lemon 	 * For a bridge, we want to check the address irrespective
710ca925d9cSJonathan Lemon 	 * of the receive interface. (This will change slightly
711ca925d9cSJonathan Lemon 	 * when we have clusters of interfaces).
712a9771948SGleb Smirnoff 	 * If the interface does not match, but the recieving interface
713a9771948SGleb Smirnoff 	 * is part of carp, we call carp_iamatch to see if this is a
714a9771948SGleb Smirnoff 	 * request for the virtual host ip.
715a9771948SGleb Smirnoff 	 * XXX: This is really ugly!
716ca925d9cSJonathan Lemon 	 */
7172ef4a436SGleb Smirnoff 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
7187f2d8767SAndrew Thompson 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
719235073f4SAndrew Thompson 		    (ia->ia_ifp == ifp)) &&
7202ef4a436SGleb Smirnoff 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
721ca925d9cSJonathan Lemon 			goto match;
7222ef4a436SGleb Smirnoff #ifdef DEV_CARP
7232ef4a436SGleb Smirnoff 		if (ifp->if_carp != NULL &&
7242ef4a436SGleb Smirnoff 		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
7252ef4a436SGleb Smirnoff 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
7262ef4a436SGleb Smirnoff 			carp_match = 1;
7272ef4a436SGleb Smirnoff 			goto match;
7282ef4a436SGleb Smirnoff 		}
7292ef4a436SGleb Smirnoff #endif
7302ef4a436SGleb Smirnoff 	}
731ca925d9cSJonathan Lemon 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
7327f2d8767SAndrew Thompson 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
733235073f4SAndrew Thompson 		    (ia->ia_ifp == ifp)) &&
734ca925d9cSJonathan Lemon 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
735ca925d9cSJonathan Lemon 			goto match;
736ca925d9cSJonathan Lemon 	/*
737d8b84d9eSJonathan Lemon 	 * No match, use the first inet address on the receive interface
738ca925d9cSJonathan Lemon 	 * as a dummy address for the rest of the function.
739ca925d9cSJonathan Lemon 	 */
740d8b84d9eSJonathan Lemon 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
7414b97d7afSYaroslav Tykhiy 		if (ifa->ifa_addr->sa_family == AF_INET) {
742ec691a10SJonathan Lemon 			ia = ifatoia(ifa);
743ec691a10SJonathan Lemon 			goto match;
744ec691a10SJonathan Lemon 		}
745ec691a10SJonathan Lemon 	/*
746ec691a10SJonathan Lemon 	 * If bridging, fall back to using any inet address.
747ec691a10SJonathan Lemon 	 */
7488f867517SAndrew Thompson 	if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
749b2a8ac7cSLuigi Rizzo 		goto drop;
750ca925d9cSJonathan Lemon match:
751a9771948SGleb Smirnoff 	if (!enaddr)
752a9771948SGleb Smirnoff 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
753ca925d9cSJonathan Lemon 	myaddr = ia->ia_addr.sin_addr;
754a9771948SGleb Smirnoff 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
755b2a8ac7cSLuigi Rizzo 		goto drop;	/* it's from me, ignore it. */
756322dcb8dSMax Khon 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
757df8bae1dSRodney W. Grimes 		log(LOG_ERR,
758322dcb8dSMax Khon 		    "arp: link address is broadcast for IP address %s!\n",
759ef0cdf33SGarrett Wollman 		    inet_ntoa(isaddr));
760b2a8ac7cSLuigi Rizzo 		goto drop;
761df8bae1dSRodney W. Grimes 	}
76200fcf9d1SRobert Watson 	/*
76300fcf9d1SRobert Watson 	 * Warn if another host is using the same IP address, but only if the
76400fcf9d1SRobert Watson 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
76500fcf9d1SRobert Watson 	 * case we suppress the warning to avoid false positive complaints of
76600fcf9d1SRobert Watson 	 * potential misconfiguration.
76700fcf9d1SRobert Watson 	 */
768f69453caSAndrew Thompson 	if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
769df8bae1dSRodney W. Grimes 		log(LOG_ERR,
7703affb6fbSYaroslav Tykhiy 		   "arp: %*D is using my IP address %s on %s!\n",
771322dcb8dSMax Khon 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
7723affb6fbSYaroslav Tykhiy 		   inet_ntoa(isaddr), ifp->if_xname);
773df8bae1dSRodney W. Grimes 		itaddr = myaddr;
774df8bae1dSRodney W. Grimes 		goto reply;
775df8bae1dSRodney W. Grimes 	}
776deb62e28SRuslan Ermilov 	if (ifp->if_flags & IFF_STATICARP)
777deb62e28SRuslan Ermilov 		goto reply;
7781ed7bf1eSGleb Smirnoff 	rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
7791ed7bf1eSGleb Smirnoff 	if (rt != NULL) {
780b3e761e5SKip Macy 		sin.sin_addr.s_addr = isaddr.s_addr;
78129910a5aSKip Macy 		EVENTHANDLER_INVOKE(route_arp_update_event, rt,
78229910a5aSKip Macy 		    ar_sha(ah), (struct sockaddr *)&sin);
783b3e761e5SKip Macy 
7841ed7bf1eSGleb Smirnoff 		la = (struct llinfo_arp *)rt->rt_llinfo;
7851ed7bf1eSGleb Smirnoff 		if (la == NULL) {
7861ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
7871ed7bf1eSGleb Smirnoff 			goto reply;
7881ed7bf1eSGleb Smirnoff 		}
7891ed7bf1eSGleb Smirnoff 	} else
7901ed7bf1eSGleb Smirnoff 		goto reply;
7911ed7bf1eSGleb Smirnoff 
7921ed7bf1eSGleb Smirnoff 	/* The following is not an error when doing bridging. */
7938f867517SAndrew Thompson 	if (!bridged && rt->rt_ifp != ifp
794422a115aSGleb Smirnoff #ifdef DEV_CARP
795422a115aSGleb Smirnoff 	    && (ifp->if_type != IFT_CARP || !carp_match)
796422a115aSGleb Smirnoff #endif
797422a115aSGleb Smirnoff 							) {
7983269187dSAlfred Perlstein 		if (log_arp_wrong_iface)
7999bf40edeSBrooks Davis 			log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
800dd9b6ddeSBill Fenner 			    inet_ntoa(isaddr),
8019bf40edeSBrooks Davis 			    rt->rt_ifp->if_xname,
802322dcb8dSMax Khon 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
8039bf40edeSBrooks Davis 			    ifp->if_xname);
8041ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
805dd9b6ddeSBill Fenner 		goto reply;
806dd9b6ddeSBill Fenner 	}
8071ed7bf1eSGleb Smirnoff 	sdl = SDL(rt->rt_gateway);
808df8bae1dSRodney W. Grimes 	if (sdl->sdl_alen &&
809322dcb8dSMax Khon 	    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
810e3d123d6SAlfred Perlstein 		if (rt->rt_expire) {
811e3d123d6SAlfred Perlstein 		    if (log_arp_movements)
8129bf40edeSBrooks Davis 		        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
813322dcb8dSMax Khon 			    inet_ntoa(isaddr),
814322dcb8dSMax Khon 			    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
815322dcb8dSMax Khon 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
8169bf40edeSBrooks Davis 			    ifp->if_xname);
817e3d123d6SAlfred Perlstein 		} else {
81839393906SGleb Smirnoff 			RT_UNLOCK(rt);
81939393906SGleb Smirnoff 			if (log_arp_permanent_modify)
82039393906SGleb Smirnoff 				log(LOG_ERR, "arp: %*D attempts to modify "
82139393906SGleb Smirnoff 				    "permanent entry for %s on %s\n",
822322dcb8dSMax Khon 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
8239bf40edeSBrooks Davis 				    inet_ntoa(isaddr), ifp->if_xname);
824dd9b6ddeSBill Fenner 			goto reply;
825dd9b6ddeSBill Fenner 		}
826dfd5dee1SPeter Wemm 	}
827322dcb8dSMax Khon 	/*
828322dcb8dSMax Khon 	 * sanity check for the address length.
829322dcb8dSMax Khon 	 * XXX this does not work for protocols with variable address
830322dcb8dSMax Khon 	 * length. -is
831322dcb8dSMax Khon 	 */
832322dcb8dSMax Khon 	if (sdl->sdl_alen &&
833322dcb8dSMax Khon 	    sdl->sdl_alen != ah->ar_hln) {
834322dcb8dSMax Khon 		log(LOG_WARNING,
835322dcb8dSMax Khon 		    "arp from %*D: new addr len %d, was %d",
836322dcb8dSMax Khon 		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
837322dcb8dSMax Khon 		    ah->ar_hln, sdl->sdl_alen);
838322dcb8dSMax Khon 	}
839322dcb8dSMax Khon 	if (ifp->if_addrlen != ah->ar_hln) {
840322dcb8dSMax Khon 		log(LOG_WARNING,
841322dcb8dSMax Khon 		    "arp from %*D: addr len: new %d, i/f %d (ignored)",
842322dcb8dSMax Khon 		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
843322dcb8dSMax Khon 		    ah->ar_hln, ifp->if_addrlen);
8441ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
845322dcb8dSMax Khon 		goto reply;
846322dcb8dSMax Khon 	}
847322dcb8dSMax Khon 	(void)memcpy(LLADDR(sdl), ar_sha(ah),
848322dcb8dSMax Khon 	    sdl->sdl_alen = ah->ar_hln);
849243c4c6dSEivind Eklund 	/*
850243c4c6dSEivind Eklund 	 * If we receive an arp from a token-ring station over
851243c4c6dSEivind Eklund 	 * a token-ring nic then try to save the source
852243c4c6dSEivind Eklund 	 * routing info.
853243c4c6dSEivind Eklund 	 */
854322dcb8dSMax Khon 	if (ifp->if_type == IFT_ISO88025) {
855f7a679b2SGleb Smirnoff 		struct iso88025_header *th = NULL;
856f7a679b2SGleb Smirnoff 		struct iso88025_sockaddr_dl_data *trld;
857f7a679b2SGleb Smirnoff 
858fda82fc2SJulian Elischer 		th = (struct iso88025_header *)m->m_pkthdr.header;
85942fdfc12SKelly Yancey 		trld = SDL_ISO88025(sdl);
860b149dd6cSLarry Lile 		rif_len = TR_RCF_RIFLEN(th->rcf);
861b149dd6cSLarry Lile 		if ((th->iso88025_shost[0] & TR_RII) &&
862b149dd6cSLarry Lile 		    (rif_len > 2)) {
86342fdfc12SKelly Yancey 			trld->trld_rcf = th->rcf;
86442fdfc12SKelly Yancey 			trld->trld_rcf ^= htons(TR_RCF_DIR);
86542fdfc12SKelly Yancey 			memcpy(trld->trld_route, th->rd, rif_len - 2);
86642fdfc12SKelly Yancey 			trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
867f9083fdbSLarry Lile 			/*
868f9083fdbSLarry Lile 			 * Set up source routing information for
869f9083fdbSLarry Lile 			 * reply packet (XXX)
870f9083fdbSLarry Lile 			 */
871b149dd6cSLarry Lile 			m->m_data -= rif_len;
872b149dd6cSLarry Lile 			m->m_len  += rif_len;
873b149dd6cSLarry Lile 			m->m_pkthdr.len += rif_len;
874fda82fc2SJulian Elischer 		} else {
875b149dd6cSLarry Lile 			th->iso88025_shost[0] &= ~TR_RII;
876c3a2190cSKelly Yancey 			trld->trld_rcf = 0;
877fcf11853SLarry Lile 		}
878fda82fc2SJulian Elischer 		m->m_data -= 8;
879fda82fc2SJulian Elischer 		m->m_len  += 8;
880fcf11853SLarry Lile 		m->m_pkthdr.len += 8;
88142fdfc12SKelly Yancey 		th->rcf = trld->trld_rcf;
882fda82fc2SJulian Elischer 	}
8831daaa65dSGleb Smirnoff 	if (rt->rt_expire) {
884fe53256dSAndre Oppermann 		rt->rt_expire = time_uptime + arpt_keep;
8851daaa65dSGleb Smirnoff 		callout_reset(&la->la_timer, hz * arpt_keep, arptimer, rt);
8861daaa65dSGleb Smirnoff 	}
887022695f8SOrion Hodson 	la->la_asked = 0;
888022695f8SOrion Hodson 	la->la_preempt = arp_maxtries;
8891ed7bf1eSGleb Smirnoff 	hold = la->la_hold;
8901ed7bf1eSGleb Smirnoff 	la->la_hold = NULL;
8911ed7bf1eSGleb Smirnoff 	RT_UNLOCK(rt);
8921ed7bf1eSGleb Smirnoff 	if (hold != NULL)
8931ed7bf1eSGleb Smirnoff 		(*ifp->if_output)(ifp, hold, rt_key(rt), rt);
8941ed7bf1eSGleb Smirnoff 
895df8bae1dSRodney W. Grimes reply:
896b2a8ac7cSLuigi Rizzo 	if (op != ARPOP_REQUEST)
897b2a8ac7cSLuigi Rizzo 		goto drop;
898df8bae1dSRodney W. Grimes 	if (itaddr.s_addr == myaddr.s_addr) {
899df8bae1dSRodney W. Grimes 		/* I am the target */
900322dcb8dSMax Khon 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
901a9771948SGleb Smirnoff 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
902df8bae1dSRodney W. Grimes 	} else {
9031ed7bf1eSGleb Smirnoff 		rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
9041ed7bf1eSGleb Smirnoff 		if (rt == NULL) {
90528e82295SGarrett Wollman 			struct sockaddr_in sin;
90628e82295SGarrett Wollman 
907b2a8ac7cSLuigi Rizzo 			if (!arp_proxyall)
908b2a8ac7cSLuigi Rizzo 				goto drop;
90928e82295SGarrett Wollman 
91028e82295SGarrett Wollman 			bzero(&sin, sizeof sin);
91128e82295SGarrett Wollman 			sin.sin_family = AF_INET;
91228e82295SGarrett Wollman 			sin.sin_len = sizeof sin;
91328e82295SGarrett Wollman 			sin.sin_addr = itaddr;
91428e82295SGarrett Wollman 
91531246bc2SGarrett Wollman 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
916b2a8ac7cSLuigi Rizzo 			if (!rt)
917b2a8ac7cSLuigi Rizzo 				goto drop;
91828e82295SGarrett Wollman 			/*
91928e82295SGarrett Wollman 			 * Don't send proxies for nodes on the same interface
92028e82295SGarrett Wollman 			 * as this one came out of, or we'll get into a fight
92128e82295SGarrett Wollman 			 * over who claims what Ether address.
92228e82295SGarrett Wollman 			 */
923322dcb8dSMax Khon 			if (rt->rt_ifp == ifp) {
92428e82295SGarrett Wollman 				rtfree(rt);
925b2a8ac7cSLuigi Rizzo 				goto drop;
92628e82295SGarrett Wollman 			}
927322dcb8dSMax Khon 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
928a9771948SGleb Smirnoff 			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
92928e82295SGarrett Wollman 			rtfree(rt);
930cc728227SDavid Malone 
931cc728227SDavid Malone 			/*
932cc728227SDavid Malone 			 * Also check that the node which sent the ARP packet
933cc728227SDavid Malone 			 * is on the the interface we expect it to be on. This
934cc728227SDavid Malone 			 * avoids ARP chaos if an interface is connected to the
935cc728227SDavid Malone 			 * wrong network.
936cc728227SDavid Malone 			 */
937cc728227SDavid Malone 			sin.sin_addr = isaddr;
938cc728227SDavid Malone 
939cc728227SDavid Malone 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
940b2a8ac7cSLuigi Rizzo 			if (!rt)
941b2a8ac7cSLuigi Rizzo 				goto drop;
942322dcb8dSMax Khon 			if (rt->rt_ifp != ifp) {
943cc728227SDavid Malone 				log(LOG_INFO, "arp_proxy: ignoring request"
9449bf40edeSBrooks Davis 				    " from %s via %s, expecting %s\n",
9459bf40edeSBrooks Davis 				    inet_ntoa(isaddr), ifp->if_xname,
9469bf40edeSBrooks Davis 				    rt->rt_ifp->if_xname);
947cc728227SDavid Malone 				rtfree(rt);
948b2a8ac7cSLuigi Rizzo 				goto drop;
949cc728227SDavid Malone 			}
950cc728227SDavid Malone 			rtfree(rt);
951cc728227SDavid Malone 
952ac234f93SGarrett Wollman #ifdef DEBUG_PROXY
953ac234f93SGarrett Wollman 			printf("arp: proxying for %s\n",
954ef0cdf33SGarrett Wollman 			       inet_ntoa(itaddr));
955ac234f93SGarrett Wollman #endif
95628e82295SGarrett Wollman 		} else {
957510b360fSGleb Smirnoff 			/*
958510b360fSGleb Smirnoff 			 * Return proxied ARP replies only on the interface
9595feebeebSAndrew Thompson 			 * or bridge cluster where this network resides.
9605feebeebSAndrew Thompson 			 * Otherwise we may conflict with the host we are
9615feebeebSAndrew Thompson 			 * proxying for.
962510b360fSGleb Smirnoff 			 */
9635feebeebSAndrew Thompson 			if (rt->rt_ifp != ifp &&
9645feebeebSAndrew Thompson 			    (rt->rt_ifp->if_bridge != ifp->if_bridge ||
9655feebeebSAndrew Thompson 			    ifp->if_bridge == NULL)) {
966510b360fSGleb Smirnoff 				RT_UNLOCK(rt);
967510b360fSGleb Smirnoff 				goto drop;
968510b360fSGleb Smirnoff 			}
969df8bae1dSRodney W. Grimes 			sdl = SDL(rt->rt_gateway);
9701ed7bf1eSGleb Smirnoff 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
971322dcb8dSMax Khon 			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
9721ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
97328e82295SGarrett Wollman 		}
974df8bae1dSRodney W. Grimes 	}
975df8bae1dSRodney W. Grimes 
976d0558157SBruce M Simpson 	if (itaddr.s_addr == myaddr.s_addr &&
977d0558157SBruce M Simpson 	    IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
978d0558157SBruce M Simpson 		/* RFC 3927 link-local IPv4; always reply by broadcast. */
979d0558157SBruce M Simpson #ifdef DEBUG_LINKLOCAL
980d0558157SBruce M Simpson 		printf("arp: sending reply for link-local addr %s\n",
981d0558157SBruce M Simpson 		    inet_ntoa(itaddr));
982d0558157SBruce M Simpson #endif
983d0558157SBruce M Simpson 		m->m_flags |= M_BCAST;
984d0558157SBruce M Simpson 		m->m_flags &= ~M_MCAST;
985d0558157SBruce M Simpson 	} else {
986d0558157SBruce M Simpson 		/* default behaviour; never reply by broadcast. */
987d0558157SBruce M Simpson 		m->m_flags &= ~(M_BCAST|M_MCAST);
988d0558157SBruce M Simpson 	}
989322dcb8dSMax Khon 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
990322dcb8dSMax Khon 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
991322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REPLY);
992322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
99364bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
99464bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
99564bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
99664bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
997322dcb8dSMax Khon 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
998df8bae1dSRodney W. Grimes 	return;
999b2a8ac7cSLuigi Rizzo 
1000b2a8ac7cSLuigi Rizzo drop:
1001b2a8ac7cSLuigi Rizzo 	m_freem(m);
1002df8bae1dSRodney W. Grimes }
10031d5e9e22SEivind Eklund #endif
1004df8bae1dSRodney W. Grimes 
1005df8bae1dSRodney W. Grimes /*
1006df8bae1dSRodney W. Grimes  * Lookup or enter a new address in arptab.
1007df8bae1dSRodney W. Grimes  */
10081ed7bf1eSGleb Smirnoff static struct rtentry *
1009f2565d68SRobert Watson arplookup(u_long addr, int create, int proxy)
1010df8bae1dSRodney W. Grimes {
1011e952fa39SMatthew N. Dodd 	struct rtentry *rt;
1012d1dd20beSSam Leffler 	struct sockaddr_inarp sin;
1013ac234f93SGarrett Wollman 	const char *why = 0;
1014df8bae1dSRodney W. Grimes 
1015d1dd20beSSam Leffler 	bzero(&sin, sizeof(sin));
1016d1dd20beSSam Leffler 	sin.sin_len = sizeof(sin);
1017d1dd20beSSam Leffler 	sin.sin_family = AF_INET;
1018df8bae1dSRodney W. Grimes 	sin.sin_addr.s_addr = addr;
1019d1dd20beSSam Leffler 	if (proxy)
1020d1dd20beSSam Leffler 		sin.sin_other = SIN_PROXY;
102131246bc2SGarrett Wollman 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
1022df8bae1dSRodney W. Grimes 	if (rt == 0)
1023df8bae1dSRodney W. Grimes 		return (0);
1024ac234f93SGarrett Wollman 
1025ac234f93SGarrett Wollman 	if (rt->rt_flags & RTF_GATEWAY)
1026ac234f93SGarrett Wollman 		why = "host is not on local network";
1027ac234f93SGarrett Wollman 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
1028ac234f93SGarrett Wollman 		why = "could not allocate llinfo";
1029ac234f93SGarrett Wollman 	else if (rt->rt_gateway->sa_family != AF_LINK)
1030ac234f93SGarrett Wollman 		why = "gateway route is not ours";
1031ac234f93SGarrett Wollman 
1032fedf1d01SBruce M Simpson 	if (why) {
1033d1dd20beSSam Leffler #define	ISDYNCLONE(_rt) \
1034d1dd20beSSam Leffler 	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
1035d1dd20beSSam Leffler 		if (create)
1036ac234f93SGarrett Wollman 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
1037ef0cdf33SGarrett Wollman 			    inet_ntoa(sin.sin_addr), why);
1038b75bead1SBruce M Simpson 		/*
1039b75bead1SBruce M Simpson 		 * If there are no references to this Layer 2 route,
1040b75bead1SBruce M Simpson 		 * and it is a cloned route, and not static, and
1041b75bead1SBruce M Simpson 		 * arplookup() is creating the route, then purge
1042b75bead1SBruce M Simpson 		 * it from the routing table as it is probably bogus.
1043b75bead1SBruce M Simpson 		 */
10449c63e9dbSSam Leffler 		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
10459c63e9dbSSam Leffler 			rtexpunge(rt);
10469c63e9dbSSam Leffler 		RTFREE_LOCKED(rt);
1047fedf1d01SBruce M Simpson 		return (0);
1048d1dd20beSSam Leffler #undef ISDYNCLONE
1049d1dd20beSSam Leffler 	} else {
10507138d65cSSam Leffler 		RT_REMREF(rt);
10511ed7bf1eSGleb Smirnoff 		return (rt);
1052df8bae1dSRodney W. Grimes 	}
1053d1dd20beSSam Leffler }
1054df8bae1dSRodney W. Grimes 
1055dd2e4102SGarrett Wollman void
1056f2565d68SRobert Watson arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1057dd2e4102SGarrett Wollman {
1058dd570d4dSTor Egge 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1059322dcb8dSMax Khon 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1060322dcb8dSMax Khon 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
1061dd2e4102SGarrett Wollman 	ifa->ifa_rtrequest = arp_rtrequest;
1062dd2e4102SGarrett Wollman 	ifa->ifa_flags |= RTF_CLONING;
1063dd2e4102SGarrett Wollman }
1064df5e1987SJonathan Lemon 
1065a9771948SGleb Smirnoff void
1066f2565d68SRobert Watson arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1067a9771948SGleb Smirnoff {
1068a9771948SGleb Smirnoff 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1069a9771948SGleb Smirnoff 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1070a9771948SGleb Smirnoff 				&IA_SIN(ifa)->sin_addr, enaddr);
1071a9771948SGleb Smirnoff 	ifa->ifa_rtrequest = arp_rtrequest;
1072a9771948SGleb Smirnoff 	ifa->ifa_flags |= RTF_CLONING;
1073a9771948SGleb Smirnoff }
1074a9771948SGleb Smirnoff 
1075df5e1987SJonathan Lemon static void
1076df5e1987SJonathan Lemon arp_init(void)
1077df5e1987SJonathan Lemon {
1078df5e1987SJonathan Lemon 
1079df5e1987SJonathan Lemon 	arpintrq.ifq_maxlen = 50;
10806008862bSJohn Baldwin 	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
10817902224cSSam Leffler 	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
1082df5e1987SJonathan Lemon }
1083df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
1084