xref: /freebsd/sys/netinet/in.c (revision a7f77a395017568a3c94ab5e89c01144880e172a)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
481d96ce8SMax Laier  * Copyright (C) 2001 WIDE Project.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
7df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
8df8bae1dSRodney W. Grimes  * are met:
9df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
10df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
11df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
13df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
14df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
15df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
16df8bae1dSRodney W. Grimes  *    without specific prior written permission.
17df8bae1dSRodney W. Grimes  *
18df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
29df8bae1dSRodney W. Grimes  *
302180b925SGarrett Wollman  *	@(#)in.c	8.4 (Berkeley) 1/9/95
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
334b421e2dSMike Silbersack #include <sys/cdefs.h>
344b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
354b421e2dSMike Silbersack 
36c7ea0aa6SQing Li #include "opt_mpath.h"
3750bb1704SGleb Smirnoff 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
4026f9a767SRodney W. Grimes #include <sys/systm.h>
4151a53488SBruce Evans #include <sys/sockio.h>
42df8bae1dSRodney W. Grimes #include <sys/malloc.h>
43acd3428bSRobert Watson #include <sys/priv.h>
44df8bae1dSRodney W. Grimes #include <sys/socket.h>
455ce0eb7fSBjoern A. Zeeb #include <sys/jail.h>
46f6d24a78SPoul-Henning Kamp #include <sys/kernel.h>
475ce0eb7fSBjoern A. Zeeb #include <sys/proc.h>
48f6d24a78SPoul-Henning Kamp #include <sys/sysctl.h>
49ebc90701SQing Li #include <sys/syslog.h>
50f7a39160SGleb Smirnoff #include <sys/sx.h>
51df8bae1dSRodney W. Grimes 
52df8bae1dSRodney W. Grimes #include <net/if.h>
53df813b7eSQing Li #include <net/if_var.h>
54e162ea60SGeorge V. Neville-Neil #include <net/if_arp.h>
55ebc90701SQing Li #include <net/if_dl.h>
566e6b3f7cSQing Li #include <net/if_llatbl.h>
576a800098SYoshinobu Inoue #include <net/if_types.h>
58df8bae1dSRodney W. Grimes #include <net/route.h>
59ebc90701SQing Li #include <net/vnet.h>
60df8bae1dSRodney W. Grimes 
6108b68b0eSGleb Smirnoff #include <netinet/if_ether.h>
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
64e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h>
6571498f30SBruce M Simpson #include <netinet/ip_var.h>
6608b68b0eSGleb Smirnoff #include <netinet/ip_carp.h>
67d10910e6SBruce M Simpson #include <netinet/igmp_var.h>
68eddfbb76SRobert Watson #include <netinet/udp.h>
69eddfbb76SRobert Watson #include <netinet/udp_var.h>
7055166637SPoul-Henning Kamp 
7177b89ad8SGleb Smirnoff static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
72f7a39160SGleb Smirnoff static int in_difaddr_ioctl(caddr_t, struct ifnet *, struct thread *);
736a800098SYoshinobu Inoue 
744d77a549SAlfred Perlstein static void	in_socktrim(struct sockaddr_in *);
75ec002feeSBruce M Simpson static void	in_purgemaddrs(struct ifnet *);
76df8bae1dSRodney W. Grimes 
7708b68b0eSGleb Smirnoff static VNET_DEFINE(int, nosameprefix);
7808b68b0eSGleb Smirnoff #define	V_nosameprefix			VNET(nosameprefix)
7908b68b0eSGleb Smirnoff SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_RW,
8008b68b0eSGleb Smirnoff 	&VNET_NAME(nosameprefix), 0,
811ae95409SGleb Smirnoff 	"Refuse to create same prefixes on different interfaces");
82477180fbSGarrett Wollman 
8382cea7e6SBjoern A. Zeeb VNET_DECLARE(struct inpcbinfo, ripcbinfo);
8482cea7e6SBjoern A. Zeeb #define	V_ripcbinfo			VNET(ripcbinfo)
8582cea7e6SBjoern A. Zeeb 
86f7a39160SGleb Smirnoff static struct sx in_control_sx;
87f7a39160SGleb Smirnoff SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
88f7a39160SGleb Smirnoff 
89df8bae1dSRodney W. Grimes /*
90df8bae1dSRodney W. Grimes  * Return 1 if an internet address is for a ``local'' host
91b365d954SGleb Smirnoff  * (one to which we have a connection).
92df8bae1dSRodney W. Grimes  */
9326f9a767SRodney W. Grimes int
94f2565d68SRobert Watson in_localaddr(struct in_addr in)
95df8bae1dSRodney W. Grimes {
96df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
97df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
98df8bae1dSRodney W. Grimes 
992d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
1002d9cfabaSRobert Watson 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1012d9cfabaSRobert Watson 		if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
1022d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
1032d9cfabaSRobert Watson 			return (1);
1042d9cfabaSRobert Watson 		}
1052d9cfabaSRobert Watson 	}
1062d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
107df8bae1dSRodney W. Grimes 	return (0);
108df8bae1dSRodney W. Grimes }
109df8bae1dSRodney W. Grimes 
110df8bae1dSRodney W. Grimes /*
1112eccc90bSAndre Oppermann  * Return 1 if an internet address is for the local host and configured
1122eccc90bSAndre Oppermann  * on one of its interfaces.
1132eccc90bSAndre Oppermann  */
1142eccc90bSAndre Oppermann int
115f2565d68SRobert Watson in_localip(struct in_addr in)
1162eccc90bSAndre Oppermann {
1172eccc90bSAndre Oppermann 	struct in_ifaddr *ia;
1182eccc90bSAndre Oppermann 
1192d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
1202eccc90bSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
1212d9cfabaSRobert Watson 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
1222d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
123460473a0SBjoern A. Zeeb 			return (1);
1242eccc90bSAndre Oppermann 		}
1252d9cfabaSRobert Watson 	}
1262d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
127460473a0SBjoern A. Zeeb 	return (0);
1282eccc90bSAndre Oppermann }
1292eccc90bSAndre Oppermann 
1302eccc90bSAndre Oppermann /*
131f7a39160SGleb Smirnoff  * Return a reference to the interface address which is different to
132f7a39160SGleb Smirnoff  * the supplied one but with same IP address value.
133f7a39160SGleb Smirnoff  */
134f7a39160SGleb Smirnoff static struct in_ifaddr *
135f7a39160SGleb Smirnoff in_localip_more(struct in_ifaddr *ia)
136f7a39160SGleb Smirnoff {
137f7a39160SGleb Smirnoff 	in_addr_t in = IA_SIN(ia)->sin_addr.s_addr;
138f7a39160SGleb Smirnoff 	struct in_ifaddr *it;
139f7a39160SGleb Smirnoff 
140f7a39160SGleb Smirnoff 	IN_IFADDR_RLOCK();
141f7a39160SGleb Smirnoff 	LIST_FOREACH(it, INADDR_HASH(in), ia_hash) {
142f7a39160SGleb Smirnoff 		if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) {
143f7a39160SGleb Smirnoff 			ifa_ref(&it->ia_ifa);
144f7a39160SGleb Smirnoff 			IN_IFADDR_RUNLOCK();
145f7a39160SGleb Smirnoff 			return (it);
146f7a39160SGleb Smirnoff 		}
147f7a39160SGleb Smirnoff 	}
148f7a39160SGleb Smirnoff 	IN_IFADDR_RUNLOCK();
149f7a39160SGleb Smirnoff 
150f7a39160SGleb Smirnoff 	return (NULL);
151f7a39160SGleb Smirnoff }
152f7a39160SGleb Smirnoff 
153f7a39160SGleb Smirnoff /*
154df8bae1dSRodney W. Grimes  * Determine whether an IP address is in a reserved set of addresses
155df8bae1dSRodney W. Grimes  * that may not be forwarded, or whether datagrams to that destination
156df8bae1dSRodney W. Grimes  * may be forwarded.
157df8bae1dSRodney W. Grimes  */
15826f9a767SRodney W. Grimes int
159f2565d68SRobert Watson in_canforward(struct in_addr in)
160df8bae1dSRodney W. Grimes {
161df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
162df8bae1dSRodney W. Grimes 	register u_long net;
163df8bae1dSRodney W. Grimes 
164f8429ca2SBruce M Simpson 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
165df8bae1dSRodney W. Grimes 		return (0);
166df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i)) {
167df8bae1dSRodney W. Grimes 		net = i & IN_CLASSA_NET;
168df8bae1dSRodney W. Grimes 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
169df8bae1dSRodney W. Grimes 			return (0);
170df8bae1dSRodney W. Grimes 	}
171df8bae1dSRodney W. Grimes 	return (1);
172df8bae1dSRodney W. Grimes }
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes /*
175df8bae1dSRodney W. Grimes  * Trim a mask in a sockaddr
176df8bae1dSRodney W. Grimes  */
1770312fbe9SPoul-Henning Kamp static void
178f2565d68SRobert Watson in_socktrim(struct sockaddr_in *ap)
179df8bae1dSRodney W. Grimes {
180df8bae1dSRodney W. Grimes     register char *cplim = (char *) &ap->sin_addr;
181df8bae1dSRodney W. Grimes     register char *cp = (char *) (&ap->sin_addr + 1);
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes     ap->sin_len = 0;
184df00058dSGarrett Wollman     while (--cp >= cplim)
185df8bae1dSRodney W. Grimes 	if (*cp) {
186df8bae1dSRodney W. Grimes 	    (ap)->sin_len = cp - (char *) (ap) + 1;
187df8bae1dSRodney W. Grimes 	    break;
188df8bae1dSRodney W. Grimes 	}
189df8bae1dSRodney W. Grimes }
190df8bae1dSRodney W. Grimes 
191df8bae1dSRodney W. Grimes /*
192df8bae1dSRodney W. Grimes  * Generic internet control operations (ioctl's).
193df8bae1dSRodney W. Grimes  */
19426f9a767SRodney W. Grimes int
195f2565d68SRobert Watson in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
196f2565d68SRobert Watson     struct thread *td)
197df8bae1dSRodney W. Grimes {
198f7a39160SGleb Smirnoff 	struct ifreq *ifr = (struct ifreq *)data;
199f7a39160SGleb Smirnoff 	struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
200821b5cafSGleb Smirnoff 	struct ifaddr *ifa;
201f7a39160SGleb Smirnoff 	struct in_ifaddr *ia;
202f7a39160SGleb Smirnoff 	int error;
203f7a39160SGleb Smirnoff 
204f7a39160SGleb Smirnoff 	if (ifp == NULL)
205f7a39160SGleb Smirnoff 		return (EADDRNOTAVAIL);
20671212473SGleb Smirnoff 
20771212473SGleb Smirnoff 	/*
208f7a39160SGleb Smirnoff 	 * Filter out 4 ioctls we implement directly.  Forward the rest
209f7a39160SGleb Smirnoff 	 * to specific functions and ifp->if_ioctl().
210bbb3fb61SRobert Watson 	 */
2116a800098SYoshinobu Inoue 	switch (cmd) {
212bbb3fb61SRobert Watson 	case SIOCGIFADDR:
213bbb3fb61SRobert Watson 	case SIOCGIFBRDADDR:
214bbb3fb61SRobert Watson 	case SIOCGIFDSTADDR:
215bbb3fb61SRobert Watson 	case SIOCGIFNETMASK:
216f7a39160SGleb Smirnoff 		break;
2176d00fd9cSGleb Smirnoff 	case SIOCDIFADDR:
218f7a39160SGleb Smirnoff 		sx_xlock(&in_control_sx);
219f7a39160SGleb Smirnoff 		error = in_difaddr_ioctl(data, ifp, td);
220f7a39160SGleb Smirnoff 		sx_xunlock(&in_control_sx);
221f7a39160SGleb Smirnoff 		return (error);
22277b89ad8SGleb Smirnoff 	case OSIOCAIFADDR:	/* 9.x compat */
2236d00fd9cSGleb Smirnoff 	case SIOCAIFADDR:
224f7a39160SGleb Smirnoff 		sx_xlock(&in_control_sx);
22577b89ad8SGleb Smirnoff 		error = in_aifaddr_ioctl(cmd, data, ifp, td);
226f7a39160SGleb Smirnoff 		sx_xunlock(&in_control_sx);
227f7a39160SGleb Smirnoff 		return (error);
228bbb3fb61SRobert Watson 	case SIOCSIFADDR:
229bbb3fb61SRobert Watson 	case SIOCSIFBRDADDR:
230bbb3fb61SRobert Watson 	case SIOCSIFDSTADDR:
231bbb3fb61SRobert Watson 	case SIOCSIFNETMASK:
23256cf9dc1SGleb Smirnoff 		/* We no longer support that old commands. */
2336d00fd9cSGleb Smirnoff 		return (EINVAL);
234bbb3fb61SRobert Watson 	default:
235f7a39160SGleb Smirnoff 		if (ifp->if_ioctl == NULL)
236bbb3fb61SRobert Watson 			return (EOPNOTSUPP);
237bbb3fb61SRobert Watson 		return ((*ifp->if_ioctl)(ifp, cmd, data));
2386a800098SYoshinobu Inoue 	}
2396a800098SYoshinobu Inoue 
240821b5cafSGleb Smirnoff 	if (addr->sin_addr.s_addr != INADDR_ANY &&
241821b5cafSGleb Smirnoff 	    prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
242821b5cafSGleb Smirnoff 		return (EADDRNOTAVAIL);
243821b5cafSGleb Smirnoff 
244cf7b18f1SRobert Watson 	/*
245*a7f77a39SXin LI 	 * Find address for this interface, if it exists.  If an
246*a7f77a39SXin LI 	 * address was specified, find that one instead of the
247*a7f77a39SXin LI 	 * first one on the interface, if possible.
248df8bae1dSRodney W. Grimes 	 */
249821b5cafSGleb Smirnoff 	IF_ADDR_RLOCK(ifp);
250821b5cafSGleb Smirnoff 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2519706c950SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET)
2529706c950SGleb Smirnoff 			continue;
253821b5cafSGleb Smirnoff 		ia = (struct in_ifaddr *)ifa;
254821b5cafSGleb Smirnoff 		if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
255df8bae1dSRodney W. Grimes 			break;
256ca925d9cSJonathan Lemon 	}
257*a7f77a39SXin LI 	if (ifa == NULL)
258*a7f77a39SXin LI 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
259*a7f77a39SXin LI 			if (ifa->ifa_addr->sa_family == AF_INET) {
260*a7f77a39SXin LI 				ia = (struct in_ifaddr *)ifa;
261*a7f77a39SXin LI 				if (prison_check_ip4(td->td_ucred,
262*a7f77a39SXin LI 				    &ia->ia_addr.sin_addr) == 0)
263*a7f77a39SXin LI 					break;
264*a7f77a39SXin LI 			}
265f7a39160SGleb Smirnoff 
266821b5cafSGleb Smirnoff 	if (ifa == NULL) {
267821b5cafSGleb Smirnoff 		IF_ADDR_RUNLOCK(ifp);
268f7a39160SGleb Smirnoff 		return (EADDRNOTAVAIL);
269ac0aa473SBill Fenner 	}
270df8bae1dSRodney W. Grimes 
271588885f2SRobert Watson 	error = 0;
272df8bae1dSRodney W. Grimes 	switch (cmd) {
273f7a39160SGleb Smirnoff 	case SIOCGIFADDR:
274f7a39160SGleb Smirnoff 		*addr = ia->ia_addr;
275f7a39160SGleb Smirnoff 		break;
2768c0fec80SRobert Watson 
277f7a39160SGleb Smirnoff 	case SIOCGIFBRDADDR:
278f7a39160SGleb Smirnoff 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
279f7a39160SGleb Smirnoff 			error = EINVAL;
280df8bae1dSRodney W. Grimes 			break;
281df8bae1dSRodney W. Grimes 		}
282f7a39160SGleb Smirnoff 		*addr = ia->ia_broadaddr;
283f7a39160SGleb Smirnoff 		break;
284f7a39160SGleb Smirnoff 
285f7a39160SGleb Smirnoff 	case SIOCGIFDSTADDR:
286f7a39160SGleb Smirnoff 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
287f7a39160SGleb Smirnoff 			error = EINVAL;
288f7a39160SGleb Smirnoff 			break;
289f7a39160SGleb Smirnoff 		}
290f7a39160SGleb Smirnoff 		*addr = ia->ia_dstaddr;
291f7a39160SGleb Smirnoff 		break;
292f7a39160SGleb Smirnoff 
293f7a39160SGleb Smirnoff 	case SIOCGIFNETMASK:
294f7a39160SGleb Smirnoff 		*addr = ia->ia_sockmask;
295f7a39160SGleb Smirnoff 		break;
296f7a39160SGleb Smirnoff 	}
297f7a39160SGleb Smirnoff 
298821b5cafSGleb Smirnoff 	IF_ADDR_RUNLOCK(ifp);
299f7a39160SGleb Smirnoff 
300f7a39160SGleb Smirnoff 	return (error);
3011067217dSGarrett Wollman }
302f7a39160SGleb Smirnoff 
303f7a39160SGleb Smirnoff static int
30477b89ad8SGleb Smirnoff in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
305f7a39160SGleb Smirnoff {
306f7a39160SGleb Smirnoff 	const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
307f7a39160SGleb Smirnoff 	const struct sockaddr_in *addr = &ifra->ifra_addr;
308f7a39160SGleb Smirnoff 	const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
309f7a39160SGleb Smirnoff 	const struct sockaddr_in *mask = &ifra->ifra_mask;
310f7a39160SGleb Smirnoff 	const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
31177b89ad8SGleb Smirnoff 	const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
312f7a39160SGleb Smirnoff 	struct ifaddr *ifa;
313f7a39160SGleb Smirnoff 	struct in_ifaddr *ia;
314f7a39160SGleb Smirnoff 	bool iaIsFirst;
315f7a39160SGleb Smirnoff 	int error = 0;
316f7a39160SGleb Smirnoff 
317f7a39160SGleb Smirnoff 	error = priv_check(td, PRIV_NET_ADDIFADDR);
318f7a39160SGleb Smirnoff 	if (error)
319f7a39160SGleb Smirnoff 		return (error);
320f7a39160SGleb Smirnoff 
321f7a39160SGleb Smirnoff 	/*
322f7a39160SGleb Smirnoff 	 * ifra_addr must be present and be of INET family.
323f7a39160SGleb Smirnoff 	 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
324f7a39160SGleb Smirnoff 	 */
325f7a39160SGleb Smirnoff 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
326f7a39160SGleb Smirnoff 	    addr->sin_family != AF_INET)
327f7a39160SGleb Smirnoff 		return (EINVAL);
328f7a39160SGleb Smirnoff 	if (broadaddr->sin_len != 0 &&
329f7a39160SGleb Smirnoff 	    (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
330f7a39160SGleb Smirnoff 	    broadaddr->sin_family != AF_INET))
331f7a39160SGleb Smirnoff 		return (EINVAL);
332f7a39160SGleb Smirnoff 	if (mask->sin_len != 0 &&
333f7a39160SGleb Smirnoff 	    (mask->sin_len != sizeof(struct sockaddr_in) ||
334f7a39160SGleb Smirnoff 	    mask->sin_family != AF_INET))
335f7a39160SGleb Smirnoff 		return (EINVAL);
336f7a39160SGleb Smirnoff 	if ((ifp->if_flags & IFF_POINTOPOINT) &&
337f7a39160SGleb Smirnoff 	    (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
338f7a39160SGleb Smirnoff 	     dstaddr->sin_addr.s_addr == INADDR_ANY))
339f7a39160SGleb Smirnoff 		return (EDESTADDRREQ);
340f7a39160SGleb Smirnoff 	if (vhid > 0 && carp_attach_p == NULL)
341f7a39160SGleb Smirnoff 		return (EPROTONOSUPPORT);
342f7a39160SGleb Smirnoff 
343f7a39160SGleb Smirnoff 	/*
344f7a39160SGleb Smirnoff 	 * See whether address already exist.
345f7a39160SGleb Smirnoff 	 */
346f7a39160SGleb Smirnoff 	iaIsFirst = true;
347f7a39160SGleb Smirnoff 	ia = NULL;
348f7a39160SGleb Smirnoff 	IF_ADDR_RLOCK(ifp);
349f7a39160SGleb Smirnoff 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3509706c950SGleb Smirnoff 		struct in_ifaddr *it;
351f7a39160SGleb Smirnoff 
3529706c950SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET)
353f7a39160SGleb Smirnoff 			continue;
354f7a39160SGleb Smirnoff 
3559706c950SGleb Smirnoff 		it = (struct in_ifaddr *)ifa;
356f7a39160SGleb Smirnoff 		iaIsFirst = false;
357f7a39160SGleb Smirnoff 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
358f7a39160SGleb Smirnoff 		    prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
359f7a39160SGleb Smirnoff 			ia = it;
3601067217dSGarrett Wollman 	}
361f7a39160SGleb Smirnoff 	IF_ADDR_RUNLOCK(ifp);
362f7a39160SGleb Smirnoff 
363f7a39160SGleb Smirnoff 	if (ia != NULL)
364f7a39160SGleb Smirnoff 		(void )in_difaddr_ioctl(data, ifp, td);
365f7a39160SGleb Smirnoff 
36646758960SGleb Smirnoff 	ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
36746758960SGleb Smirnoff 	ia = (struct in_ifaddr *)ifa;
36859562606SGarrett Wollman 	ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
36959562606SGarrett Wollman 	ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
37059562606SGarrett Wollman 	ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
37119fc74fbSJeffrey Hsu 
372f7a39160SGleb Smirnoff 	ia->ia_ifp = ifp;
373f7a39160SGleb Smirnoff 	ia->ia_addr = *addr;
374f7a39160SGleb Smirnoff 	if (mask->sin_len != 0) {
375f7a39160SGleb Smirnoff 		ia->ia_sockmask = *mask;
376f7a39160SGleb Smirnoff 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
377f7a39160SGleb Smirnoff 	} else {
378f7a39160SGleb Smirnoff 		in_addr_t i = ntohl(addr->sin_addr.s_addr);
379f7a39160SGleb Smirnoff 
380f7a39160SGleb Smirnoff 		/*
381f7a39160SGleb Smirnoff 	 	 * Be compatible with network classes, if netmask isn't
382f7a39160SGleb Smirnoff 		 * supplied, guess it based on classes.
383f7a39160SGleb Smirnoff 	 	 */
384f7a39160SGleb Smirnoff 		if (IN_CLASSA(i))
385f7a39160SGleb Smirnoff 			ia->ia_subnetmask = IN_CLASSA_NET;
386f7a39160SGleb Smirnoff 		else if (IN_CLASSB(i))
387f7a39160SGleb Smirnoff 			ia->ia_subnetmask = IN_CLASSB_NET;
388f7a39160SGleb Smirnoff 		else
389f7a39160SGleb Smirnoff 			ia->ia_subnetmask = IN_CLASSC_NET;
390f7a39160SGleb Smirnoff 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
391f7a39160SGleb Smirnoff 	}
392f7a39160SGleb Smirnoff 	ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
393f7a39160SGleb Smirnoff 	in_socktrim(&ia->ia_sockmask);
394f7a39160SGleb Smirnoff 
395df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_BROADCAST) {
396f7a39160SGleb Smirnoff 		if (broadaddr->sin_len != 0) {
397f7a39160SGleb Smirnoff 			ia->ia_broadaddr = *broadaddr;
398f7a39160SGleb Smirnoff 		} else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
399f7a39160SGleb Smirnoff 			ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
400f7a39160SGleb Smirnoff 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
401f7a39160SGleb Smirnoff 			ia->ia_broadaddr.sin_family = AF_INET;
402f7a39160SGleb Smirnoff 		} else {
403f7a39160SGleb Smirnoff 			ia->ia_broadaddr.sin_addr.s_addr =
404f7a39160SGleb Smirnoff 			    htonl(ia->ia_subnet | ~ia->ia_subnetmask);
405f7a39160SGleb Smirnoff 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
406df8bae1dSRodney W. Grimes 			ia->ia_broadaddr.sin_family = AF_INET;
407df8bae1dSRodney W. Grimes 		}
408f7a39160SGleb Smirnoff 	}
409f7a39160SGleb Smirnoff 
410f7a39160SGleb Smirnoff 	if (ifp->if_flags & IFF_POINTOPOINT)
411f7a39160SGleb Smirnoff 		ia->ia_dstaddr = *dstaddr;
412f7a39160SGleb Smirnoff 
413f7a39160SGleb Smirnoff 	/* XXXGL: rtinit() needs this strange assignment. */
414f7a39160SGleb Smirnoff 	if (ifp->if_flags & IFF_LOOPBACK)
415f7a39160SGleb Smirnoff                 ia->ia_dstaddr = ia->ia_addr;
416f3d30eb2SGleb Smirnoff 
4175af464bbSSteven Hartland 	if (vhid != 0) {
4185af464bbSSteven Hartland 		error = (*carp_attach_p)(&ia->ia_ifa, vhid);
4195af464bbSSteven Hartland 		if (error)
4205af464bbSSteven Hartland 			return (error);
4215af464bbSSteven Hartland 	}
4225af464bbSSteven Hartland 
423a49b317cSAlexander V. Chernikov 	/* if_addrhead is already referenced by ifa_alloc() */
424137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
4258021456aSRobert Watson 	TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
426137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
427f7a39160SGleb Smirnoff 
4288c0fec80SRobert Watson 	ifa_ref(ifa);			/* in_ifaddrhead */
4292d9cfabaSRobert Watson 	IN_IFADDR_WLOCK();
430603724d3SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
431f7a39160SGleb Smirnoff 	LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
4322d9cfabaSRobert Watson 	IN_IFADDR_WUNLOCK();
433df8bae1dSRodney W. Grimes 
434f7a39160SGleb Smirnoff 	/*
435f7a39160SGleb Smirnoff 	 * Give the interface a chance to initialize
436f7a39160SGleb Smirnoff 	 * if this is its first address,
437f7a39160SGleb Smirnoff 	 * and to validate the address if necessary.
438f7a39160SGleb Smirnoff 	 */
439d34165f7SSteven Hartland 	if (ifp->if_ioctl != NULL) {
440f7a39160SGleb Smirnoff 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
441f7a39160SGleb Smirnoff 		if (error)
4425af464bbSSteven Hartland 			goto fail1;
443d34165f7SSteven Hartland 	}
444f7a39160SGleb Smirnoff 
445f7a39160SGleb Smirnoff 	/*
446f7a39160SGleb Smirnoff 	 * Add route for the network.
447f7a39160SGleb Smirnoff 	 */
448f7a39160SGleb Smirnoff 	if (vhid == 0) {
449f7a39160SGleb Smirnoff 		int flags = RTF_UP;
450f7a39160SGleb Smirnoff 
451f7a39160SGleb Smirnoff 		if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
452f7a39160SGleb Smirnoff 			flags |= RTF_HOST;
453f7a39160SGleb Smirnoff 
454f7a39160SGleb Smirnoff 		error = in_addprefix(ia, flags);
455f7a39160SGleb Smirnoff 		if (error)
4565af464bbSSteven Hartland 			goto fail1;
457df8bae1dSRodney W. Grimes 	}
458df8bae1dSRodney W. Grimes 
459588885f2SRobert Watson 	/*
460f7a39160SGleb Smirnoff 	 * Add a loopback route to self.
461588885f2SRobert Watson 	 */
462f7a39160SGleb Smirnoff 	if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 &&
463c1f7c3f5SGleb Smirnoff 	    ia->ia_addr.sin_addr.s_addr != INADDR_ANY &&
464c1f7c3f5SGleb Smirnoff 	    !((ifp->if_flags & IFF_POINTOPOINT) &&
465c1f7c3f5SGleb Smirnoff 	     ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
466f7a39160SGleb Smirnoff 		struct in_ifaddr *eia;
467df8bae1dSRodney W. Grimes 
468f7a39160SGleb Smirnoff 		eia = in_localip_more(ia);
469f7a39160SGleb Smirnoff 
470f7a39160SGleb Smirnoff 		if (eia == NULL) {
471f7a39160SGleb Smirnoff 			error = ifa_add_loopback_route((struct ifaddr *)ia,
472f7a39160SGleb Smirnoff 			    (struct sockaddr *)&ia->ia_addr);
473f7a39160SGleb Smirnoff 			if (error)
4745af464bbSSteven Hartland 				goto fail2;
475f7a39160SGleb Smirnoff 		} else
476f7a39160SGleb Smirnoff 			ifa_free(&eia->ia_ifa);
477588885f2SRobert Watson 	}
478df8bae1dSRodney W. Grimes 
479f7a39160SGleb Smirnoff 	if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
480f7a39160SGleb Smirnoff 		struct in_addr allhosts_addr;
481f7a39160SGleb Smirnoff 		struct in_ifinfo *ii;
482df8bae1dSRodney W. Grimes 
483c75aa354SBruce M Simpson 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
484f7a39160SGleb Smirnoff 		allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
485df8bae1dSRodney W. Grimes 
486f7a39160SGleb Smirnoff 		error = in_joingroup(ifp, &allhosts_addr, NULL,
487f7a39160SGleb Smirnoff 			&ii->ii_allhosts);
488f7a39160SGleb Smirnoff 	}
489f7a39160SGleb Smirnoff 
490f7a39160SGleb Smirnoff 	EVENTHANDLER_INVOKE(ifaddr_event, ifp);
491f7a39160SGleb Smirnoff 
492f7a39160SGleb Smirnoff 	return (error);
493f7a39160SGleb Smirnoff 
4945af464bbSSteven Hartland fail2:
495f7a39160SGleb Smirnoff 	if (vhid == 0)
496f7a39160SGleb Smirnoff 		(void )in_scrubprefix(ia, LLE_STATIC);
497f7a39160SGleb Smirnoff 
4985af464bbSSteven Hartland fail1:
499f7a39160SGleb Smirnoff 	if (ia->ia_ifa.ifa_carp)
500f7a39160SGleb Smirnoff 		(*carp_detach_p)(&ia->ia_ifa);
501f7a39160SGleb Smirnoff 
502f7a39160SGleb Smirnoff 	IF_ADDR_WLOCK(ifp);
503f7a39160SGleb Smirnoff 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
504f7a39160SGleb Smirnoff 	IF_ADDR_WUNLOCK(ifp);
505a49b317cSAlexander V. Chernikov 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
506f7a39160SGleb Smirnoff 
507f7a39160SGleb Smirnoff 	IN_IFADDR_WLOCK();
508f7a39160SGleb Smirnoff 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
509f7a39160SGleb Smirnoff 	LIST_REMOVE(ia, ia_hash);
510f7a39160SGleb Smirnoff 	IN_IFADDR_WUNLOCK();
511a49b317cSAlexander V. Chernikov 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
512f7a39160SGleb Smirnoff 
513f7a39160SGleb Smirnoff 	return (error);
514f7a39160SGleb Smirnoff }
515f7a39160SGleb Smirnoff 
516f7a39160SGleb Smirnoff static int
517f7a39160SGleb Smirnoff in_difaddr_ioctl(caddr_t data, struct ifnet *ifp, struct thread *td)
518f7a39160SGleb Smirnoff {
519f7a39160SGleb Smirnoff 	const struct ifreq *ifr = (struct ifreq *)data;
5206224cd89SNathan Whitehorn 	const struct sockaddr_in *addr = (const struct sockaddr_in *)
5216224cd89SNathan Whitehorn 	    &ifr->ifr_addr;
522f7a39160SGleb Smirnoff 	struct ifaddr *ifa;
523f7a39160SGleb Smirnoff 	struct in_ifaddr *ia;
524f7a39160SGleb Smirnoff 	bool deleteAny, iaIsLast;
525f7a39160SGleb Smirnoff 	int error;
526f7a39160SGleb Smirnoff 
527f7a39160SGleb Smirnoff 	if (td != NULL) {
528f7a39160SGleb Smirnoff 		error = priv_check(td, PRIV_NET_DELIFADDR);
529f7a39160SGleb Smirnoff 		if (error)
530f7a39160SGleb Smirnoff 			return (error);
531f7a39160SGleb Smirnoff 	}
532f7a39160SGleb Smirnoff 
533f7a39160SGleb Smirnoff 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
534f7a39160SGleb Smirnoff 	    addr->sin_family != AF_INET)
535f7a39160SGleb Smirnoff 		deleteAny = true;
536f7a39160SGleb Smirnoff 	else
537f7a39160SGleb Smirnoff 		deleteAny = false;
538f7a39160SGleb Smirnoff 
539f7a39160SGleb Smirnoff 	iaIsLast = true;
540f7a39160SGleb Smirnoff 	ia = NULL;
541f7a39160SGleb Smirnoff 	IF_ADDR_WLOCK(ifp);
542f7a39160SGleb Smirnoff 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
5439706c950SGleb Smirnoff 		struct in_ifaddr *it;
544f7a39160SGleb Smirnoff 
5459706c950SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET)
546f7a39160SGleb Smirnoff 			continue;
547f7a39160SGleb Smirnoff 
5489706c950SGleb Smirnoff 		it = (struct in_ifaddr *)ifa;
549f7a39160SGleb Smirnoff 		if (deleteAny && ia == NULL && (td == NULL ||
550f7a39160SGleb Smirnoff 		    prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
551f7a39160SGleb Smirnoff 			ia = it;
552f7a39160SGleb Smirnoff 
553f7a39160SGleb Smirnoff 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
554f7a39160SGleb Smirnoff 		    (td == NULL || prison_check_ip4(td->td_ucred,
555f7a39160SGleb Smirnoff 		    &addr->sin_addr) == 0))
556f7a39160SGleb Smirnoff 			ia = it;
557f7a39160SGleb Smirnoff 
558f7a39160SGleb Smirnoff 		if (it != ia)
559f7a39160SGleb Smirnoff 			iaIsLast = false;
560f7a39160SGleb Smirnoff 	}
561f7a39160SGleb Smirnoff 
562f7a39160SGleb Smirnoff 	if (ia == NULL) {
563f7a39160SGleb Smirnoff 		IF_ADDR_WUNLOCK(ifp);
564f7a39160SGleb Smirnoff 		return (EADDRNOTAVAIL);
565f7a39160SGleb Smirnoff 	}
566f7a39160SGleb Smirnoff 
567f7a39160SGleb Smirnoff 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
568f7a39160SGleb Smirnoff 	IF_ADDR_WUNLOCK(ifp);
569f7a39160SGleb Smirnoff 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
570f7a39160SGleb Smirnoff 
571f7a39160SGleb Smirnoff 	IN_IFADDR_WLOCK();
572f7a39160SGleb Smirnoff 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
573f7a39160SGleb Smirnoff 	LIST_REMOVE(ia, ia_hash);
574f7a39160SGleb Smirnoff 	IN_IFADDR_WUNLOCK();
575f7a39160SGleb Smirnoff 
576089cdfadSRuslan Ermilov 	/*
577237bf7f7SGleb Smirnoff 	 * in_scrubprefix() kills the interface route.
578089cdfadSRuslan Ermilov 	 */
579237bf7f7SGleb Smirnoff 	in_scrubprefix(ia, LLE_STATIC);
580588885f2SRobert Watson 
581c655b7c4SDavid Greenman 	/*
582089cdfadSRuslan Ermilov 	 * in_ifadown gets rid of all the rest of
583089cdfadSRuslan Ermilov 	 * the routes.  This is not quite the right
584089cdfadSRuslan Ermilov 	 * thing to do, but at least if we are running
585089cdfadSRuslan Ermilov 	 * a routing process they will come back.
586089cdfadSRuslan Ermilov 	 */
58791854268SRuslan Ermilov 	in_ifadown(&ia->ia_ifa, 1);
5880f02fdacSBrian Somers 
58908b68b0eSGleb Smirnoff 	if (ia->ia_ifa.ifa_carp)
59008b68b0eSGleb Smirnoff 		(*carp_detach_p)(&ia->ia_ifa);
59108b68b0eSGleb Smirnoff 
592f7e083afSBruce M Simpson 	/*
593f7e083afSBruce M Simpson 	 * If this is the last IPv4 address configured on this
594f7e083afSBruce M Simpson 	 * interface, leave the all-hosts group.
595d10910e6SBruce M Simpson 	 * No state-change report need be transmitted.
596f7e083afSBruce M Simpson 	 */
597f7a39160SGleb Smirnoff 	if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
598f7a39160SGleb Smirnoff 		struct in_ifinfo *ii;
599f7a39160SGleb Smirnoff 
600c75aa354SBruce M Simpson 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
601f7e083afSBruce M Simpson 		IN_MULTI_LOCK();
602d10910e6SBruce M Simpson 		if (ii->ii_allhosts) {
6036d00fd9cSGleb Smirnoff 			(void)in_leavegroup_locked(ii->ii_allhosts, NULL);
604d10910e6SBruce M Simpson 			ii->ii_allhosts = NULL;
605d10910e6SBruce M Simpson 		}
606f7e083afSBruce M Simpson 		IN_MULTI_UNLOCK();
607f7a39160SGleb Smirnoff 	}
6086d00fd9cSGleb Smirnoff 
609f7a39160SGleb Smirnoff 	EVENTHANDLER_INVOKE(ifaddr_event, ifp);
610a49b317cSAlexander V. Chernikov 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
611f7a39160SGleb Smirnoff 
612f7a39160SGleb Smirnoff 	return (0);
613df8bae1dSRodney W. Grimes }
614df8bae1dSRodney W. Grimes 
61548321abeSMax Laier #define rtinitflags(x) \
61648321abeSMax Laier 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
61748321abeSMax Laier 	    ? RTF_HOST : 0)
618ccbb9c35SQing Li 
619ccbb9c35SQing Li /*
620fbdd20a1SMatt Jacob  * Check if we have a route for the given prefix already or add one accordingly.
62148321abeSMax Laier  */
62208b68b0eSGleb Smirnoff int
623f2565d68SRobert Watson in_addprefix(struct in_ifaddr *target, int flags)
62448321abeSMax Laier {
62548321abeSMax Laier 	struct in_ifaddr *ia;
626bfb26eecSGleb Smirnoff 	struct in_addr prefix, mask, p, m;
6270cfee0c2SAlan Somers 	int error;
62848321abeSMax Laier 
629fbdd20a1SMatt Jacob 	if ((flags & RTF_HOST) != 0) {
63048321abeSMax Laier 		prefix = target->ia_dstaddr.sin_addr;
631fbdd20a1SMatt Jacob 		mask.s_addr = 0;
632fbdd20a1SMatt Jacob 	} else {
63348321abeSMax Laier 		prefix = target->ia_addr.sin_addr;
63448321abeSMax Laier 		mask = target->ia_sockmask.sin_addr;
63548321abeSMax Laier 		prefix.s_addr &= mask.s_addr;
63648321abeSMax Laier 	}
63748321abeSMax Laier 
6382d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
6390cfee0c2SAlan Somers 	/* Look for an existing address with the same prefix, mask, and fib */
640603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
641bfb26eecSGleb Smirnoff 		if (rtinitflags(ia)) {
64272366606SKevin Lo 			p = ia->ia_dstaddr.sin_addr;
64348321abeSMax Laier 
64448321abeSMax Laier 			if (prefix.s_addr != p.s_addr)
64548321abeSMax Laier 				continue;
646bfb26eecSGleb Smirnoff 		} else {
647bfb26eecSGleb Smirnoff 			p = ia->ia_addr.sin_addr;
648bfb26eecSGleb Smirnoff 			m = ia->ia_sockmask.sin_addr;
649bfb26eecSGleb Smirnoff 			p.s_addr &= m.s_addr;
650bfb26eecSGleb Smirnoff 
651bfb26eecSGleb Smirnoff 			if (prefix.s_addr != p.s_addr ||
652bfb26eecSGleb Smirnoff 			    mask.s_addr != m.s_addr)
653bfb26eecSGleb Smirnoff 				continue;
654bfb26eecSGleb Smirnoff 		}
6550cfee0c2SAlan Somers 		if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
6560cfee0c2SAlan Somers 			continue;
65748321abeSMax Laier 
65848321abeSMax Laier 		/*
65948321abeSMax Laier 		 * If we got a matching prefix route inserted by other
66048321abeSMax Laier 		 * interface address, we are done here.
66148321abeSMax Laier 		 */
6621ae95409SGleb Smirnoff 		if (ia->ia_flags & IFA_ROUTE) {
663c7ea0aa6SQing Li #ifdef RADIX_MPATH
664c7ea0aa6SQing Li 			if (ia->ia_addr.sin_addr.s_addr ==
66542db1b87SBjoern A. Zeeb 			    target->ia_addr.sin_addr.s_addr) {
66642db1b87SBjoern A. Zeeb 				IN_IFADDR_RUNLOCK();
667c7ea0aa6SQing Li 				return (EEXIST);
66842db1b87SBjoern A. Zeeb 			} else
669c7ea0aa6SQing Li 				break;
670c7ea0aa6SQing Li #endif
67108b68b0eSGleb Smirnoff 			if (V_nosameprefix) {
6722d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
6731ae95409SGleb Smirnoff 				return (EEXIST);
6742d9cfabaSRobert Watson 			} else {
6750cfee0c2SAlan Somers 				int fibnum;
6760cfee0c2SAlan Somers 
6770cfee0c2SAlan Somers 				fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS :
6780cfee0c2SAlan Somers 					target->ia_ifp->if_fib;
679d375edc9SAlexander V. Chernikov 				rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
6802d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
6811ae95409SGleb Smirnoff 				return (0);
6821ae95409SGleb Smirnoff 			}
68348321abeSMax Laier 		}
6842d9cfabaSRobert Watson 	}
6852d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
68648321abeSMax Laier 
68748321abeSMax Laier 	/*
68848321abeSMax Laier 	 * No-one seem to have this prefix route, so we try to insert it.
68948321abeSMax Laier 	 */
69048321abeSMax Laier 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
69148321abeSMax Laier 	if (!error)
69248321abeSMax Laier 		target->ia_flags |= IFA_ROUTE;
693460473a0SBjoern A. Zeeb 	return (error);
69448321abeSMax Laier }
69548321abeSMax Laier 
69648321abeSMax Laier /*
69748321abeSMax Laier  * If there is no other address in the system that can serve a route to the
69848321abeSMax Laier  * same prefix, remove the route.  Hand over the route to the new address
69948321abeSMax Laier  * otherwise.
70048321abeSMax Laier  */
70108b68b0eSGleb Smirnoff int
7025b84dc78SQing Li in_scrubprefix(struct in_ifaddr *target, u_int flags)
70348321abeSMax Laier {
70448321abeSMax Laier 	struct in_ifaddr *ia;
70555174c34SGleb Smirnoff 	struct in_addr prefix, mask, p, m;
7067278b62aSAlan Somers 	int error = 0;
707c9d763bfSQing Li 	struct sockaddr_in prefix0, mask0;
70848321abeSMax Laier 
709df813b7eSQing Li 	/*
710df813b7eSQing Li 	 * Remove the loopback route to the interface address.
711df813b7eSQing Li 	 */
71292fac994SQing Li 	if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
713c7ab6602SQing Li 	    !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
714f7a39160SGleb Smirnoff 	    (flags & LLE_STATIC)) {
715f7a39160SGleb Smirnoff 		struct in_ifaddr *eia;
716c7ab6602SQing Li 
717f7a39160SGleb Smirnoff 		eia = in_localip_more(target);
718f7a39160SGleb Smirnoff 
719f7a39160SGleb Smirnoff 		if (eia != NULL) {
7207278b62aSAlan Somers 			int fibnum = target->ia_ifp->if_fib;
7217278b62aSAlan Somers 
722f7a39160SGleb Smirnoff 			error = ifa_switch_loopback_route((struct ifaddr *)eia,
7230489b891SAlan Somers 			    (struct sockaddr *)&target->ia_addr, fibnum);
724f7a39160SGleb Smirnoff 			ifa_free(&eia->ia_ifa);
725f7a39160SGleb Smirnoff 		} else {
7269bb7d0f4SQing Li 			error = ifa_del_loopback_route((struct ifaddr *)target,
7279bb7d0f4SQing Li 			    (struct sockaddr *)&target->ia_addr);
7285b84dc78SQing Li 		}
729f7a39160SGleb Smirnoff 
730f7a39160SGleb Smirnoff 		if (!(target->ia_ifp->if_flags & IFF_NOARP))
731fa3cfd39SQing Li 			/* remove arp cache */
732f7a39160SGleb Smirnoff 			arp_ifscrub(target->ia_ifp,
733f7a39160SGleb Smirnoff 			    IA_SIN(target)->sin_addr.s_addr);
734ebc90701SQing Li 	}
735ebc90701SQing Li 
73655174c34SGleb Smirnoff 	if (rtinitflags(target)) {
73748321abeSMax Laier 		prefix = target->ia_dstaddr.sin_addr;
73855174c34SGleb Smirnoff 		mask.s_addr = 0;
73955174c34SGleb Smirnoff 	} else {
74048321abeSMax Laier 		prefix = target->ia_addr.sin_addr;
74148321abeSMax Laier 		mask = target->ia_sockmask.sin_addr;
74248321abeSMax Laier 		prefix.s_addr &= mask.s_addr;
74348321abeSMax Laier 	}
74448321abeSMax Laier 
745ccbb9c35SQing Li 	if ((target->ia_flags & IFA_ROUTE) == 0) {
7467278b62aSAlan Somers 		int fibnum;
7477278b62aSAlan Somers 
7487278b62aSAlan Somers 		fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS :
7497278b62aSAlan Somers 			target->ia_ifp->if_fib;
750d375edc9SAlexander V. Chernikov 		rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
751ccbb9c35SQing Li 		return (0);
752ccbb9c35SQing Li 	}
753ccbb9c35SQing Li 
7542d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
755603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
75655174c34SGleb Smirnoff 		if (rtinitflags(ia)) {
75748321abeSMax Laier 			p = ia->ia_dstaddr.sin_addr;
75855174c34SGleb Smirnoff 
75955174c34SGleb Smirnoff 			if (prefix.s_addr != p.s_addr)
76055174c34SGleb Smirnoff 				continue;
76155174c34SGleb Smirnoff 		} else {
76248321abeSMax Laier 			p = ia->ia_addr.sin_addr;
76355174c34SGleb Smirnoff 			m = ia->ia_sockmask.sin_addr;
76455174c34SGleb Smirnoff 			p.s_addr &= m.s_addr;
76555174c34SGleb Smirnoff 
76655174c34SGleb Smirnoff 			if (prefix.s_addr != p.s_addr ||
76755174c34SGleb Smirnoff 			    mask.s_addr != m.s_addr)
76855174c34SGleb Smirnoff 				continue;
76948321abeSMax Laier 		}
77048321abeSMax Laier 
77155174c34SGleb Smirnoff 		if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
77248321abeSMax Laier 			continue;
77348321abeSMax Laier 
77448321abeSMax Laier 		/*
77548321abeSMax Laier 		 * If we got a matching prefix address, move IFA_ROUTE and
77648321abeSMax Laier 		 * the route itself to it.  Make sure that routing daemons
77748321abeSMax Laier 		 * get a heads-up.
77848321abeSMax Laier 		 */
77908b68b0eSGleb Smirnoff 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
78079d51435SSergey Kandaurov 			ifa_ref(&ia->ia_ifa);
7812d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
78292322284SQing Li 			error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
78348321abeSMax Laier 			    rtinitflags(target));
78492322284SQing Li 			if (error == 0)
78548321abeSMax Laier 				target->ia_flags &= ~IFA_ROUTE;
78692322284SQing Li 			else
78792322284SQing Li 				log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
78892322284SQing Li 					error);
78948321abeSMax Laier 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
79048321abeSMax Laier 			    rtinitflags(ia) | RTF_UP);
79148321abeSMax Laier 			if (error == 0)
79248321abeSMax Laier 				ia->ia_flags |= IFA_ROUTE;
79392322284SQing Li 			else
79492322284SQing Li 				log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
79592322284SQing Li 					error);
79679d51435SSergey Kandaurov 			ifa_free(&ia->ia_ifa);
797460473a0SBjoern A. Zeeb 			return (error);
79848321abeSMax Laier 		}
79948321abeSMax Laier 	}
8002d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
80148321abeSMax Laier 
80248321abeSMax Laier 	/*
803c9d763bfSQing Li 	 * remove all L2 entries on the given prefix
804c9d763bfSQing Li 	 */
805c9d763bfSQing Li 	bzero(&prefix0, sizeof(prefix0));
806c9d763bfSQing Li 	prefix0.sin_len = sizeof(prefix0);
807c9d763bfSQing Li 	prefix0.sin_family = AF_INET;
808c9d763bfSQing Li 	prefix0.sin_addr.s_addr = target->ia_subnet;
809c9d763bfSQing Li 	bzero(&mask0, sizeof(mask0));
810c9d763bfSQing Li 	mask0.sin_len = sizeof(mask0);
811c9d763bfSQing Li 	mask0.sin_family = AF_INET;
812c9d763bfSQing Li 	mask0.sin_addr.s_addr = target->ia_subnetmask;
813c9d763bfSQing Li 	lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
8145b84dc78SQing Li 	    (struct sockaddr *)&mask0, flags);
815c9d763bfSQing Li 
816c9d763bfSQing Li 	/*
81748321abeSMax Laier 	 * As no-one seem to have this prefix, we can remove the route.
81848321abeSMax Laier 	 */
81992322284SQing Li 	error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
82092322284SQing Li 	if (error == 0)
82148321abeSMax Laier 		target->ia_flags &= ~IFA_ROUTE;
82292322284SQing Li 	else
82392322284SQing Li 		log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
82492322284SQing Li 	return (error);
82548321abeSMax Laier }
82648321abeSMax Laier 
82748321abeSMax Laier #undef rtinitflags
828df8bae1dSRodney W. Grimes 
829df8bae1dSRodney W. Grimes /*
830df8bae1dSRodney W. Grimes  * Return 1 if the address might be a local broadcast address.
831df8bae1dSRodney W. Grimes  */
83226f9a767SRodney W. Grimes int
833f2565d68SRobert Watson in_broadcast(struct in_addr in, struct ifnet *ifp)
834df8bae1dSRodney W. Grimes {
835df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
836df8bae1dSRodney W. Grimes 	u_long t;
837df8bae1dSRodney W. Grimes 
838df8bae1dSRodney W. Grimes 	if (in.s_addr == INADDR_BROADCAST ||
839df8bae1dSRodney W. Grimes 	    in.s_addr == INADDR_ANY)
840460473a0SBjoern A. Zeeb 		return (1);
841df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
842460473a0SBjoern A. Zeeb 		return (0);
843df8bae1dSRodney W. Grimes 	t = ntohl(in.s_addr);
844df8bae1dSRodney W. Grimes 	/*
845df8bae1dSRodney W. Grimes 	 * Look through the list of addresses for a match
846df8bae1dSRodney W. Grimes 	 * with a broadcast address.
847df8bae1dSRodney W. Grimes 	 */
848df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa)
849462b86feSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
850df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == AF_INET &&
851df8bae1dSRodney W. Grimes 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
852df8bae1dSRodney W. Grimes 		     /*
85353883e0cSGleb Smirnoff 		      * Check for old-style (host 0) broadcast, but
85453883e0cSGleb Smirnoff 		      * taking into account that RFC 3021 obsoletes it.
855df8bae1dSRodney W. Grimes 		      */
85653883e0cSGleb Smirnoff 		    (ia->ia_subnetmask != IN_RFC3021_MASK &&
85753883e0cSGleb Smirnoff 		    t == ia->ia_subnet)) &&
8588dd27fd6SGuido van Rooij 		     /*
8598dd27fd6SGuido van Rooij 		      * Check for an all one subnetmask. These
8608dd27fd6SGuido van Rooij 		      * only exist when an interface gets a secondary
8618dd27fd6SGuido van Rooij 		      * address.
8628dd27fd6SGuido van Rooij 		      */
8638dd27fd6SGuido van Rooij 		    ia->ia_subnetmask != (u_long)0xffffffff)
864460473a0SBjoern A. Zeeb 			    return (1);
865df8bae1dSRodney W. Grimes 	return (0);
866df8bae1dSRodney W. Grimes #undef ia
867df8bae1dSRodney W. Grimes }
868ec002feeSBruce M Simpson 
869df8bae1dSRodney W. Grimes /*
870b1c53bc9SRobert Watson  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
871b1c53bc9SRobert Watson  */
872b1c53bc9SRobert Watson void
873f2565d68SRobert Watson in_ifdetach(struct ifnet *ifp)
874b1c53bc9SRobert Watson {
875b1c53bc9SRobert Watson 
876603724d3SBjoern A. Zeeb 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
877603724d3SBjoern A. Zeeb 	in_pcbpurgeif0(&V_udbinfo, ifp);
878e06e816fSKevin Lo 	in_pcbpurgeif0(&V_ulitecbinfo, ifp);
879ec002feeSBruce M Simpson 	in_purgemaddrs(ifp);
880b1c53bc9SRobert Watson }
8816e6b3f7cSQing Li 
882d10910e6SBruce M Simpson /*
883d10910e6SBruce M Simpson  * Delete all IPv4 multicast address records, and associated link-layer
884d10910e6SBruce M Simpson  * multicast address records, associated with ifp.
885d10910e6SBruce M Simpson  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
88656663a40SBruce M Simpson  * XXX This should not race with ifma_protospec being set during
88756663a40SBruce M Simpson  * a new allocation, if it does, we have bigger problems.
888d10910e6SBruce M Simpson  */
889d10910e6SBruce M Simpson static void
890d10910e6SBruce M Simpson in_purgemaddrs(struct ifnet *ifp)
891d10910e6SBruce M Simpson {
892d10910e6SBruce M Simpson 	LIST_HEAD(,in_multi) purgeinms;
893d10910e6SBruce M Simpson 	struct in_multi		*inm, *tinm;
894d10910e6SBruce M Simpson 	struct ifmultiaddr	*ifma;
895d10910e6SBruce M Simpson 
896d10910e6SBruce M Simpson 	LIST_INIT(&purgeinms);
897d10910e6SBruce M Simpson 	IN_MULTI_LOCK();
898d10910e6SBruce M Simpson 
899d10910e6SBruce M Simpson 	/*
900d10910e6SBruce M Simpson 	 * Extract list of in_multi associated with the detaching ifp
901d10910e6SBruce M Simpson 	 * which the PF_INET layer is about to release.
902d10910e6SBruce M Simpson 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
903d10910e6SBruce M Simpson 	 * by code further down.
904d10910e6SBruce M Simpson 	 */
905137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
906d10910e6SBruce M Simpson 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
90756663a40SBruce M Simpson 		if (ifma->ifma_addr->sa_family != AF_INET ||
90856663a40SBruce M Simpson 		    ifma->ifma_protospec == NULL)
909d10910e6SBruce M Simpson 			continue;
91056663a40SBruce M Simpson #if 0
91156663a40SBruce M Simpson 		KASSERT(ifma->ifma_protospec != NULL,
91256663a40SBruce M Simpson 		    ("%s: ifma_protospec is NULL", __func__));
91356663a40SBruce M Simpson #endif
914d10910e6SBruce M Simpson 		inm = (struct in_multi *)ifma->ifma_protospec;
915d10910e6SBruce M Simpson 		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
916d10910e6SBruce M Simpson 	}
917137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
918d10910e6SBruce M Simpson 
919d10910e6SBruce M Simpson 	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
920d10910e6SBruce M Simpson 		LIST_REMOVE(inm, inm_link);
92107cde5e9SRobert Watson 		inm_release_locked(inm);
922d10910e6SBruce M Simpson 	}
923d10910e6SBruce M Simpson 	igmp_ifdetach(ifp);
924d10910e6SBruce M Simpson 
925d10910e6SBruce M Simpson 	IN_MULTI_UNLOCK();
926d10910e6SBruce M Simpson }
927d10910e6SBruce M Simpson 
9286e6b3f7cSQing Li struct in_llentry {
9296e6b3f7cSQing Li 	struct llentry		base;
9306e6b3f7cSQing Li 	struct sockaddr_in	l3_addr4;
9316e6b3f7cSQing Li };
9326e6b3f7cSQing Li 
933a93cda78SKip Macy /*
934a93cda78SKip Macy  * Deletes an address from the address table.
935a93cda78SKip Macy  * This function is called by the timer functions
936a93cda78SKip Macy  * such as arptimer() and nd6_llinfo_timer(), and
937a93cda78SKip Macy  * the caller does the locking.
938a93cda78SKip Macy  */
939a93cda78SKip Macy static void
940a93cda78SKip Macy in_lltable_free(struct lltable *llt, struct llentry *lle)
941a93cda78SKip Macy {
942a93cda78SKip Macy 	LLE_WUNLOCK(lle);
943a93cda78SKip Macy 	LLE_LOCK_DESTROY(lle);
944a93cda78SKip Macy 	free(lle, M_LLTABLE);
945a93cda78SKip Macy }
946a93cda78SKip Macy 
9476e6b3f7cSQing Li static struct llentry *
9486e6b3f7cSQing Li in_lltable_new(const struct sockaddr *l3addr, u_int flags)
9496e6b3f7cSQing Li {
9506e6b3f7cSQing Li 	struct in_llentry *lle;
9516e6b3f7cSQing Li 
95290b357f6SGleb Smirnoff 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
9536e6b3f7cSQing Li 	if (lle == NULL)		/* NB: caller generates msg */
9546e6b3f7cSQing Li 		return NULL;
9556e6b3f7cSQing Li 
9566e6b3f7cSQing Li 	/*
9576e6b3f7cSQing Li 	 * For IPv4 this will trigger "arpresolve" to generate
9586e6b3f7cSQing Li 	 * an ARP request.
9596e6b3f7cSQing Li 	 */
960a98c06f1SGleb Smirnoff 	lle->base.la_expire = time_uptime; /* mark expired */
9616e6b3f7cSQing Li 	lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
9626e6b3f7cSQing Li 	lle->base.lle_refcnt = 1;
963a93cda78SKip Macy 	lle->base.lle_free = in_lltable_free;
9646e6b3f7cSQing Li 	LLE_LOCK_INIT(&lle->base);
965ea537929SGleb Smirnoff 	callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock,
966ea537929SGleb Smirnoff 	    CALLOUT_RETURNUNLOCKED);
967ea537929SGleb Smirnoff 
968ea537929SGleb Smirnoff 	return (&lle->base);
9696e6b3f7cSQing Li }
9706e6b3f7cSQing Li 
971c9d763bfSQing Li #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(			\
972c9d763bfSQing Li 	    (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
973c9d763bfSQing Li 
974c9d763bfSQing Li static void
975ea50c13eSGleb Smirnoff in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
976ea50c13eSGleb Smirnoff     const struct sockaddr *mask, u_int flags)
977c9d763bfSQing Li {
978c9d763bfSQing Li 	const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
979c9d763bfSQing Li 	const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
980c9d763bfSQing Li 	struct llentry *lle, *next;
981ea50c13eSGleb Smirnoff 	int i;
982e162ea60SGeorge V. Neville-Neil 	size_t pkts_dropped;
983c9d763bfSQing Li 
984ea537929SGleb Smirnoff 	IF_AFDATA_WLOCK(llt->llt_ifp);
985c9d763bfSQing Li 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
986c9d763bfSQing Li 		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
9875b84dc78SQing Li 			/*
9885b84dc78SQing Li 			 * (flags & LLE_STATIC) means deleting all entries
989ea50c13eSGleb Smirnoff 			 * including static ARP entries.
9905b84dc78SQing Li 			 */
991ea50c13eSGleb Smirnoff 			if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)),
992ea50c13eSGleb Smirnoff 			    pfx, msk) && ((flags & LLE_STATIC) ||
993ea50c13eSGleb Smirnoff 			    !(lle->la_flags & LLE_STATIC))) {
994c9d763bfSQing Li 				LLE_WLOCK(lle);
995ea537929SGleb Smirnoff 				if (callout_stop(&lle->la_timer))
996becba438SBjoern A. Zeeb 					LLE_REMREF(lle);
997e162ea60SGeorge V. Neville-Neil 				pkts_dropped = llentry_free(lle);
998e162ea60SGeorge V. Neville-Neil 				ARPSTAT_ADD(dropped, pkts_dropped);
999c9d763bfSQing Li 			}
1000c9d763bfSQing Li 		}
1001c9d763bfSQing Li 	}
1002ea537929SGleb Smirnoff 	IF_AFDATA_WUNLOCK(llt->llt_ifp);
1003c9d763bfSQing Li }
1004c9d763bfSQing Li 
1005c9d763bfSQing Li 
10066e6b3f7cSQing Li static int
1007c7ab6602SQing Li in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
10086e6b3f7cSQing Li {
10096e6b3f7cSQing Li 	struct rtentry *rt;
10106e6b3f7cSQing Li 
10116e6b3f7cSQing Li 	KASSERT(l3addr->sa_family == AF_INET,
10126e6b3f7cSQing Li 	    ("sin_family %d", l3addr->sa_family));
10136e6b3f7cSQing Li 
1014743c072aSAlan Somers 	/* XXX rtalloc1_fib should take a const param */
1015743c072aSAlan Somers 	rt = rtalloc1_fib(__DECONST(struct sockaddr *, l3addr), 0, 0,
1016743c072aSAlan Somers 	    ifp->if_fib);
101713e255faSMarko Zec 
10186cf8e330SQing Li 	if (rt == NULL)
10196cf8e330SQing Li 		return (EINVAL);
10206cf8e330SQing Li 
102113e255faSMarko Zec 	/*
102213e255faSMarko Zec 	 * If the gateway for an existing host route matches the target L3
10236cf8e330SQing Li 	 * address, which is a special route inserted by some implementation
10246cf8e330SQing Li 	 * such as MANET, and the interface is of the correct type, then
10256cf8e330SQing Li 	 * allow for ARP to proceed.
102613e255faSMarko Zec 	 */
1027db92413eSQing Li 	if (rt->rt_flags & RTF_GATEWAY) {
102815d25219SQing Li 		if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
102915d25219SQing Li 		    rt->rt_ifp->if_type != IFT_ETHER ||
1030ea50c13eSGleb Smirnoff 		    (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
103115d25219SQing Li 		    memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
103215d25219SQing Li 		    sizeof(in_addr_t)) != 0) {
1033db92413eSQing Li 			RTFREE_LOCKED(rt);
1034db92413eSQing Li 			return (EINVAL);
1035db92413eSQing Li 		}
103615d25219SQing Li 	}
1037db92413eSQing Li 
1038db92413eSQing Li 	/*
1039db92413eSQing Li 	 * Make sure that at least the destination address is covered
1040db92413eSQing Li 	 * by the route. This is for handling the case where 2 or more
1041db92413eSQing Li 	 * interfaces have the same prefix. An incoming packet arrives
1042db92413eSQing Li 	 * on one interface and the corresponding outgoing packet leaves
1043db92413eSQing Li 	 * another interface.
1044db92413eSQing Li 	 */
1045b3664a14SQing Li 	if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
104615d25219SQing Li 		const char *sa, *mask, *addr, *lim;
1047db92413eSQing Li 		int len;
1048db92413eSQing Li 
104915d25219SQing Li 		mask = (const char *)rt_mask(rt);
1050b3664a14SQing Li 		/*
1051b3664a14SQing Li 		 * Just being extra cautious to avoid some custom
1052b3664a14SQing Li 		 * code getting into trouble.
1053b3664a14SQing Li 		 */
1054b3664a14SQing Li 		if (mask == NULL) {
1055b3664a14SQing Li 			RTFREE_LOCKED(rt);
1056b3664a14SQing Li 			return (EINVAL);
1057b3664a14SQing Li 		}
1058b3664a14SQing Li 
1059b3664a14SQing Li 		sa = (const char *)rt_key(rt);
106015d25219SQing Li 		addr = (const char *)l3addr;
106115d25219SQing Li 		len = ((const struct sockaddr_in *)l3addr)->sin_len;
1062db92413eSQing Li 		lim = addr + len;
1063db92413eSQing Li 
1064db92413eSQing Li 		for ( ; addr < lim; sa++, mask++, addr++) {
1065db92413eSQing Li 			if ((*sa ^ *addr) & *mask) {
10666cb2b4e7SQing Li #ifdef DIAGNOSTIC
10676e6b3f7cSQing Li 				log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
10686e6b3f7cSQing Li 				    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1069b4a22c36SQing Li #endif
107015d25219SQing Li 				RTFREE_LOCKED(rt);
107115d25219SQing Li 				return (EINVAL);
10726e6b3f7cSQing Li 			}
1073db92413eSQing Li 		}
1074db92413eSQing Li 	}
1075db92413eSQing Li 
10766e6b3f7cSQing Li 	RTFREE_LOCKED(rt);
107715d25219SQing Li 	return (0);
10786e6b3f7cSQing Li }
10796e6b3f7cSQing Li 
10806e6b3f7cSQing Li /*
10816e6b3f7cSQing Li  * Return NULL if not found or marked for deletion.
10826e6b3f7cSQing Li  * If found return lle read locked.
10836e6b3f7cSQing Li  */
10846e6b3f7cSQing Li static struct llentry *
10856e6b3f7cSQing Li in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
10866e6b3f7cSQing Li {
10876e6b3f7cSQing Li 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
10886e6b3f7cSQing Li 	struct ifnet *ifp = llt->llt_ifp;
10896e6b3f7cSQing Li 	struct llentry *lle;
10906e6b3f7cSQing Li 	struct llentries *lleh;
10916e6b3f7cSQing Li 	u_int hashkey;
10926e6b3f7cSQing Li 
10936e6b3f7cSQing Li 	IF_AFDATA_LOCK_ASSERT(ifp);
10946e6b3f7cSQing Li 	KASSERT(l3addr->sa_family == AF_INET,
10956e6b3f7cSQing Li 	    ("sin_family %d", l3addr->sa_family));
10966e6b3f7cSQing Li 
10976e6b3f7cSQing Li 	hashkey = sin->sin_addr.s_addr;
10986e6b3f7cSQing Li 	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
10996e6b3f7cSQing Li 	LIST_FOREACH(lle, lleh, lle_next) {
1100ea50c13eSGleb Smirnoff 		struct sockaddr_in *sa2 = satosin(L3_ADDR(lle));
11016e6b3f7cSQing Li 		if (lle->la_flags & LLE_DELETED)
11026e6b3f7cSQing Li 			continue;
1103dc495497SQing Li 		if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
11046e6b3f7cSQing Li 			break;
11056e6b3f7cSQing Li 	}
11066e6b3f7cSQing Li 	if (lle == NULL) {
11076cb2b4e7SQing Li #ifdef DIAGNOSTIC
11086e6b3f7cSQing Li 		if (flags & LLE_DELETE)
11096e6b3f7cSQing Li 			log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
11106e6b3f7cSQing Li #endif
11116e6b3f7cSQing Li 		if (!(flags & LLE_CREATE))
11126e6b3f7cSQing Li 			return (NULL);
1113e2d14d93SAndrey V. Elsukov 		IF_AFDATA_WLOCK_ASSERT(ifp);
11146e6b3f7cSQing Li 		/*
11156e6b3f7cSQing Li 		 * A route that covers the given address must have
11166e6b3f7cSQing Li 		 * been installed 1st because we are doing a resolution,
11176e6b3f7cSQing Li 		 * verify this.
11186e6b3f7cSQing Li 		 */
11196e6b3f7cSQing Li 		if (!(flags & LLE_IFADDR) &&
1120c7ab6602SQing Li 		    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
11216e6b3f7cSQing Li 			goto done;
11226e6b3f7cSQing Li 
11236e6b3f7cSQing Li 		lle = in_lltable_new(l3addr, flags);
11246e6b3f7cSQing Li 		if (lle == NULL) {
11256e6b3f7cSQing Li 			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
11266e6b3f7cSQing Li 			goto done;
11276e6b3f7cSQing Li 		}
11286e6b3f7cSQing Li 		lle->la_flags = flags & ~LLE_CREATE;
11296e6b3f7cSQing Li 		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
11306e6b3f7cSQing Li 			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
11316e6b3f7cSQing Li 			lle->la_flags |= (LLE_VALID | LLE_STATIC);
11326e6b3f7cSQing Li 		}
11336e6b3f7cSQing Li 
11346e6b3f7cSQing Li 		lle->lle_tbl  = llt;
11356e6b3f7cSQing Li 		lle->lle_head = lleh;
1136ea537929SGleb Smirnoff 		lle->la_flags |= LLE_LINKED;
11376e6b3f7cSQing Li 		LIST_INSERT_HEAD(lleh, lle, lle_next);
11386e6b3f7cSQing Li 	} else if (flags & LLE_DELETE) {
11396e6b3f7cSQing Li 		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
11406e6b3f7cSQing Li 			LLE_WLOCK(lle);
1141ea537929SGleb Smirnoff 			lle->la_flags |= LLE_DELETED;
114209fe6320SNavdeep Parhar 			EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
11436cb2b4e7SQing Li #ifdef DIAGNOSTIC
11446e6b3f7cSQing Li 			log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
11456e6b3f7cSQing Li #endif
11461571132fSOleg Bulyzhin 			if ((lle->la_flags &
11471571132fSOleg Bulyzhin 			    (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
11481571132fSOleg Bulyzhin 				llentry_free(lle);
11491571132fSOleg Bulyzhin 			else
11501571132fSOleg Bulyzhin 				LLE_WUNLOCK(lle);
11516e6b3f7cSQing Li 		}
11526e6b3f7cSQing Li 		lle = (void *)-1;
11536e6b3f7cSQing Li 
11546e6b3f7cSQing Li 	}
115542d866ddSBjoern A. Zeeb 	if (LLE_IS_VALID(lle)) {
11566e6b3f7cSQing Li 		if (flags & LLE_EXCLUSIVE)
11576e6b3f7cSQing Li 			LLE_WLOCK(lle);
11586e6b3f7cSQing Li 		else
11596e6b3f7cSQing Li 			LLE_RLOCK(lle);
11606e6b3f7cSQing Li 	}
11616e6b3f7cSQing Li done:
11626e6b3f7cSQing Li 	return (lle);
11636e6b3f7cSQing Li }
11646e6b3f7cSQing Li 
11656e6b3f7cSQing Li static int
11666e6b3f7cSQing Li in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
11676e6b3f7cSQing Li {
11686e6b3f7cSQing Li #define	SIN(lle)	((struct sockaddr_in *) L3_ADDR(lle))
11696e6b3f7cSQing Li 	struct ifnet *ifp = llt->llt_ifp;
11706e6b3f7cSQing Li 	struct llentry *lle;
11716e6b3f7cSQing Li 	/* XXX stack use */
11726e6b3f7cSQing Li 	struct {
11736e6b3f7cSQing Li 		struct rt_msghdr	rtm;
11749711a168SGleb Smirnoff 		struct sockaddr_in	sin;
11756e6b3f7cSQing Li 		struct sockaddr_dl	sdl;
11766e6b3f7cSQing Li 	} arpc;
11776e6b3f7cSQing Li 	int error, i;
11786e6b3f7cSQing Li 
1179dc56e98fSRobert Watson 	LLTABLE_LOCK_ASSERT();
11806e6b3f7cSQing Li 
11816e6b3f7cSQing Li 	error = 0;
11826e6b3f7cSQing Li 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
11836e6b3f7cSQing Li 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
11846e6b3f7cSQing Li 			struct sockaddr_dl *sdl;
11856e6b3f7cSQing Li 
11866e6b3f7cSQing Li 			/* skip deleted entries */
118793704ac5SQing Li 			if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
11886e6b3f7cSQing Li 				continue;
1189813dd6aeSBjoern A. Zeeb 			/* Skip if jailed and not a valid IP of the prison. */
1190b89e82ddSJamie Gritton 			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1191813dd6aeSBjoern A. Zeeb 				continue;
11926e6b3f7cSQing Li 			/*
11936e6b3f7cSQing Li 			 * produce a msg made of:
11946e6b3f7cSQing Li 			 *  struct rt_msghdr;
11959711a168SGleb Smirnoff 			 *  struct sockaddr_in; (IPv4)
11966e6b3f7cSQing Li 			 *  struct sockaddr_dl;
11976e6b3f7cSQing Li 			 */
11986e6b3f7cSQing Li 			bzero(&arpc, sizeof(arpc));
11996e6b3f7cSQing Li 			arpc.rtm.rtm_msglen = sizeof(arpc);
1200c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_version = RTM_VERSION;
1201c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_type = RTM_GET;
1202c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_flags = RTF_UP;
1203c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
12046e6b3f7cSQing Li 			arpc.sin.sin_family = AF_INET;
12056e6b3f7cSQing Li 			arpc.sin.sin_len = sizeof(arpc.sin);
12066e6b3f7cSQing Li 			arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
12076e6b3f7cSQing Li 
12086e6b3f7cSQing Li 			/* publish */
12099711a168SGleb Smirnoff 			if (lle->la_flags & LLE_PUB)
12106e6b3f7cSQing Li 				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
12116e6b3f7cSQing Li 
12126e6b3f7cSQing Li 			sdl = &arpc.sdl;
12136e6b3f7cSQing Li 			sdl->sdl_family = AF_LINK;
12146e6b3f7cSQing Li 			sdl->sdl_len = sizeof(*sdl);
12156e6b3f7cSQing Li 			sdl->sdl_index = ifp->if_index;
12166e6b3f7cSQing Li 			sdl->sdl_type = ifp->if_type;
121793704ac5SQing Li 			if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
121893704ac5SQing Li 				sdl->sdl_alen = ifp->if_addrlen;
12196e6b3f7cSQing Li 				bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
122093704ac5SQing Li 			} else {
122193704ac5SQing Li 				sdl->sdl_alen = 0;
122293704ac5SQing Li 				bzero(LLADDR(sdl), ifp->if_addrlen);
122393704ac5SQing Li 			}
12246e6b3f7cSQing Li 
12256e6b3f7cSQing Li 			arpc.rtm.rtm_rmx.rmx_expire =
12266e6b3f7cSQing Li 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
12278eca593cSQing Li 			arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
12286e6b3f7cSQing Li 			if (lle->la_flags & LLE_STATIC)
12296e6b3f7cSQing Li 				arpc.rtm.rtm_flags |= RTF_STATIC;
12306e6b3f7cSQing Li 			arpc.rtm.rtm_index = ifp->if_index;
12316e6b3f7cSQing Li 			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
12326e6b3f7cSQing Li 			if (error)
12336e6b3f7cSQing Li 				break;
12346e6b3f7cSQing Li 		}
12356e6b3f7cSQing Li 	}
12366e6b3f7cSQing Li 	return error;
12376e6b3f7cSQing Li #undef SIN
12386e6b3f7cSQing Li }
12396e6b3f7cSQing Li 
12406e6b3f7cSQing Li void *
12416e6b3f7cSQing Li in_domifattach(struct ifnet *ifp)
12426e6b3f7cSQing Li {
1243d10910e6SBruce M Simpson 	struct in_ifinfo *ii;
1244d10910e6SBruce M Simpson 	struct lltable *llt;
12456e6b3f7cSQing Li 
1246d10910e6SBruce M Simpson 	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1247d10910e6SBruce M Simpson 
1248d10910e6SBruce M Simpson 	llt = lltable_init(ifp, AF_INET);
12496e6b3f7cSQing Li 	if (llt != NULL) {
1250c9d763bfSQing Li 		llt->llt_prefix_free = in_lltable_prefix_free;
12516e6b3f7cSQing Li 		llt->llt_lookup = in_lltable_lookup;
12526e6b3f7cSQing Li 		llt->llt_dump = in_lltable_dump;
12536e6b3f7cSQing Li 	}
1254d10910e6SBruce M Simpson 	ii->ii_llt = llt;
1255d10910e6SBruce M Simpson 
1256d10910e6SBruce M Simpson 	ii->ii_igmp = igmp_domifattach(ifp);
1257d10910e6SBruce M Simpson 
1258d10910e6SBruce M Simpson 	return ii;
12596e6b3f7cSQing Li }
12606e6b3f7cSQing Li 
12616e6b3f7cSQing Li void
1262d10910e6SBruce M Simpson in_domifdetach(struct ifnet *ifp, void *aux)
12636e6b3f7cSQing Li {
1264d10910e6SBruce M Simpson 	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
12656e6b3f7cSQing Li 
1266d10910e6SBruce M Simpson 	igmp_domifdetach(ifp);
1267d10910e6SBruce M Simpson 	lltable_free(ii->ii_llt);
1268d10910e6SBruce M Simpson 	free(ii, M_IFADDR);
12696e6b3f7cSQing Li }
1270