xref: /freebsd/sys/netinet/in.c (revision a49b317c4102b1ba27da259b49fb1eaa18a86b02)
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 	/*
245821b5cafSGleb Smirnoff 	 * For SIOCGIFADDR, pick the first address.  For the rest of
246821b5cafSGleb Smirnoff 	 * ioctls, try to find specified address.
247df8bae1dSRodney W. Grimes 	 */
248821b5cafSGleb Smirnoff 	IF_ADDR_RLOCK(ifp);
249821b5cafSGleb Smirnoff 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2509706c950SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET)
2519706c950SGleb Smirnoff 			continue;
252821b5cafSGleb Smirnoff 		ia = (struct in_ifaddr *)ifa;
253821b5cafSGleb Smirnoff 		if (cmd == SIOCGIFADDR || addr->sin_addr.s_addr == INADDR_ANY)
254821b5cafSGleb Smirnoff 			break;
255821b5cafSGleb Smirnoff 		if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
256df8bae1dSRodney W. Grimes 			break;
257ca925d9cSJonathan Lemon 	}
258f7a39160SGleb Smirnoff 
259821b5cafSGleb Smirnoff 	if (ifa == NULL) {
260821b5cafSGleb Smirnoff 		IF_ADDR_RUNLOCK(ifp);
261f7a39160SGleb Smirnoff 		return (EADDRNOTAVAIL);
262ac0aa473SBill Fenner 	}
263df8bae1dSRodney W. Grimes 
264588885f2SRobert Watson 	error = 0;
265df8bae1dSRodney W. Grimes 	switch (cmd) {
266f7a39160SGleb Smirnoff 	case SIOCGIFADDR:
267f7a39160SGleb Smirnoff 		*addr = ia->ia_addr;
268f7a39160SGleb Smirnoff 		break;
2698c0fec80SRobert Watson 
270f7a39160SGleb Smirnoff 	case SIOCGIFBRDADDR:
271f7a39160SGleb Smirnoff 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
272f7a39160SGleb Smirnoff 			error = EINVAL;
273df8bae1dSRodney W. Grimes 			break;
274df8bae1dSRodney W. Grimes 		}
275f7a39160SGleb Smirnoff 		*addr = ia->ia_broadaddr;
276f7a39160SGleb Smirnoff 		break;
277f7a39160SGleb Smirnoff 
278f7a39160SGleb Smirnoff 	case SIOCGIFDSTADDR:
279f7a39160SGleb Smirnoff 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
280f7a39160SGleb Smirnoff 			error = EINVAL;
281f7a39160SGleb Smirnoff 			break;
282f7a39160SGleb Smirnoff 		}
283f7a39160SGleb Smirnoff 		*addr = ia->ia_dstaddr;
284f7a39160SGleb Smirnoff 		break;
285f7a39160SGleb Smirnoff 
286f7a39160SGleb Smirnoff 	case SIOCGIFNETMASK:
287f7a39160SGleb Smirnoff 		*addr = ia->ia_sockmask;
288f7a39160SGleb Smirnoff 		break;
289f7a39160SGleb Smirnoff 	}
290f7a39160SGleb Smirnoff 
291821b5cafSGleb Smirnoff 	IF_ADDR_RUNLOCK(ifp);
292f7a39160SGleb Smirnoff 
293f7a39160SGleb Smirnoff 	return (error);
2941067217dSGarrett Wollman }
295f7a39160SGleb Smirnoff 
296f7a39160SGleb Smirnoff static int
29777b89ad8SGleb Smirnoff in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
298f7a39160SGleb Smirnoff {
299f7a39160SGleb Smirnoff 	const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
300f7a39160SGleb Smirnoff 	const struct sockaddr_in *addr = &ifra->ifra_addr;
301f7a39160SGleb Smirnoff 	const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
302f7a39160SGleb Smirnoff 	const struct sockaddr_in *mask = &ifra->ifra_mask;
303f7a39160SGleb Smirnoff 	const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
30477b89ad8SGleb Smirnoff 	const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
305f7a39160SGleb Smirnoff 	struct ifaddr *ifa;
306f7a39160SGleb Smirnoff 	struct in_ifaddr *ia;
307f7a39160SGleb Smirnoff 	bool iaIsFirst;
308f7a39160SGleb Smirnoff 	int error = 0;
309f7a39160SGleb Smirnoff 
310f7a39160SGleb Smirnoff 	error = priv_check(td, PRIV_NET_ADDIFADDR);
311f7a39160SGleb Smirnoff 	if (error)
312f7a39160SGleb Smirnoff 		return (error);
313f7a39160SGleb Smirnoff 
314f7a39160SGleb Smirnoff 	/*
315f7a39160SGleb Smirnoff 	 * ifra_addr must be present and be of INET family.
316f7a39160SGleb Smirnoff 	 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
317f7a39160SGleb Smirnoff 	 */
318f7a39160SGleb Smirnoff 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
319f7a39160SGleb Smirnoff 	    addr->sin_family != AF_INET)
320f7a39160SGleb Smirnoff 		return (EINVAL);
321f7a39160SGleb Smirnoff 	if (broadaddr->sin_len != 0 &&
322f7a39160SGleb Smirnoff 	    (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
323f7a39160SGleb Smirnoff 	    broadaddr->sin_family != AF_INET))
324f7a39160SGleb Smirnoff 		return (EINVAL);
325f7a39160SGleb Smirnoff 	if (mask->sin_len != 0 &&
326f7a39160SGleb Smirnoff 	    (mask->sin_len != sizeof(struct sockaddr_in) ||
327f7a39160SGleb Smirnoff 	    mask->sin_family != AF_INET))
328f7a39160SGleb Smirnoff 		return (EINVAL);
329f7a39160SGleb Smirnoff 	if ((ifp->if_flags & IFF_POINTOPOINT) &&
330f7a39160SGleb Smirnoff 	    (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
331f7a39160SGleb Smirnoff 	     dstaddr->sin_addr.s_addr == INADDR_ANY))
332f7a39160SGleb Smirnoff 		return (EDESTADDRREQ);
333f7a39160SGleb Smirnoff 	if (vhid > 0 && carp_attach_p == NULL)
334f7a39160SGleb Smirnoff 		return (EPROTONOSUPPORT);
335f7a39160SGleb Smirnoff 
336f7a39160SGleb Smirnoff 	/*
337f7a39160SGleb Smirnoff 	 * See whether address already exist.
338f7a39160SGleb Smirnoff 	 */
339f7a39160SGleb Smirnoff 	iaIsFirst = true;
340f7a39160SGleb Smirnoff 	ia = NULL;
341f7a39160SGleb Smirnoff 	IF_ADDR_RLOCK(ifp);
342f7a39160SGleb Smirnoff 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3439706c950SGleb Smirnoff 		struct in_ifaddr *it;
344f7a39160SGleb Smirnoff 
3459706c950SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET)
346f7a39160SGleb Smirnoff 			continue;
347f7a39160SGleb Smirnoff 
3489706c950SGleb Smirnoff 		it = (struct in_ifaddr *)ifa;
349f7a39160SGleb Smirnoff 		iaIsFirst = false;
350f7a39160SGleb Smirnoff 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
351f7a39160SGleb Smirnoff 		    prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
352f7a39160SGleb Smirnoff 			ia = it;
3531067217dSGarrett Wollman 	}
354f7a39160SGleb Smirnoff 	IF_ADDR_RUNLOCK(ifp);
355f7a39160SGleb Smirnoff 
356f7a39160SGleb Smirnoff 	if (ia != NULL)
357f7a39160SGleb Smirnoff 		(void )in_difaddr_ioctl(data, ifp, td);
358f7a39160SGleb Smirnoff 
35946758960SGleb Smirnoff 	ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
36046758960SGleb Smirnoff 	ia = (struct in_ifaddr *)ifa;
36159562606SGarrett Wollman 	ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
36259562606SGarrett Wollman 	ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
36359562606SGarrett Wollman 	ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
36419fc74fbSJeffrey Hsu 
365f7a39160SGleb Smirnoff 	ia->ia_ifp = ifp;
366f7a39160SGleb Smirnoff 	ia->ia_ifa.ifa_metric = ifp->if_metric;
367f7a39160SGleb Smirnoff 	ia->ia_addr = *addr;
368f7a39160SGleb Smirnoff 	if (mask->sin_len != 0) {
369f7a39160SGleb Smirnoff 		ia->ia_sockmask = *mask;
370f7a39160SGleb Smirnoff 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
371f7a39160SGleb Smirnoff 	} else {
372f7a39160SGleb Smirnoff 		in_addr_t i = ntohl(addr->sin_addr.s_addr);
373f7a39160SGleb Smirnoff 
374f7a39160SGleb Smirnoff 		/*
375f7a39160SGleb Smirnoff 	 	 * Be compatible with network classes, if netmask isn't
376f7a39160SGleb Smirnoff 		 * supplied, guess it based on classes.
377f7a39160SGleb Smirnoff 	 	 */
378f7a39160SGleb Smirnoff 		if (IN_CLASSA(i))
379f7a39160SGleb Smirnoff 			ia->ia_subnetmask = IN_CLASSA_NET;
380f7a39160SGleb Smirnoff 		else if (IN_CLASSB(i))
381f7a39160SGleb Smirnoff 			ia->ia_subnetmask = IN_CLASSB_NET;
382f7a39160SGleb Smirnoff 		else
383f7a39160SGleb Smirnoff 			ia->ia_subnetmask = IN_CLASSC_NET;
384f7a39160SGleb Smirnoff 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
385f7a39160SGleb Smirnoff 	}
386f7a39160SGleb Smirnoff 	ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
387f7a39160SGleb Smirnoff 	in_socktrim(&ia->ia_sockmask);
388f7a39160SGleb Smirnoff 
389df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_BROADCAST) {
390f7a39160SGleb Smirnoff 		if (broadaddr->sin_len != 0) {
391f7a39160SGleb Smirnoff 			ia->ia_broadaddr = *broadaddr;
392f7a39160SGleb Smirnoff 		} else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
393f7a39160SGleb Smirnoff 			ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
394f7a39160SGleb Smirnoff 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
395f7a39160SGleb Smirnoff 			ia->ia_broadaddr.sin_family = AF_INET;
396f7a39160SGleb Smirnoff 		} else {
397f7a39160SGleb Smirnoff 			ia->ia_broadaddr.sin_addr.s_addr =
398f7a39160SGleb Smirnoff 			    htonl(ia->ia_subnet | ~ia->ia_subnetmask);
399f7a39160SGleb Smirnoff 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
400df8bae1dSRodney W. Grimes 			ia->ia_broadaddr.sin_family = AF_INET;
401df8bae1dSRodney W. Grimes 		}
402f7a39160SGleb Smirnoff 	}
403f7a39160SGleb Smirnoff 
404f7a39160SGleb Smirnoff 	if (ifp->if_flags & IFF_POINTOPOINT)
405f7a39160SGleb Smirnoff 		ia->ia_dstaddr = *dstaddr;
406f7a39160SGleb Smirnoff 
407f7a39160SGleb Smirnoff 	/* XXXGL: rtinit() needs this strange assignment. */
408f7a39160SGleb Smirnoff 	if (ifp->if_flags & IFF_LOOPBACK)
409f7a39160SGleb Smirnoff                 ia->ia_dstaddr = ia->ia_addr;
410f3d30eb2SGleb Smirnoff 
411*a49b317cSAlexander V. Chernikov 	/* if_addrhead is already referenced by ifa_alloc() */
412137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
4138021456aSRobert Watson 	TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
414137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
415f7a39160SGleb Smirnoff 
4168c0fec80SRobert Watson 	ifa_ref(ifa);			/* in_ifaddrhead */
4172d9cfabaSRobert Watson 	IN_IFADDR_WLOCK();
418603724d3SBjoern A. Zeeb 	TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
419f7a39160SGleb Smirnoff 	LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
4202d9cfabaSRobert Watson 	IN_IFADDR_WUNLOCK();
421df8bae1dSRodney W. Grimes 
422f7a39160SGleb Smirnoff 	if (vhid != 0)
423f7a39160SGleb Smirnoff 		error = (*carp_attach_p)(&ia->ia_ifa, vhid);
424f7a39160SGleb Smirnoff 	if (error)
425f7a39160SGleb Smirnoff 		goto fail1;
426f7a39160SGleb Smirnoff 
427f7a39160SGleb Smirnoff 	/*
428f7a39160SGleb Smirnoff 	 * Give the interface a chance to initialize
429f7a39160SGleb Smirnoff 	 * if this is its first address,
430f7a39160SGleb Smirnoff 	 * and to validate the address if necessary.
431f7a39160SGleb Smirnoff 	 */
432f7a39160SGleb Smirnoff 	if (ifp->if_ioctl != NULL)
433f7a39160SGleb Smirnoff 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
434f7a39160SGleb Smirnoff 	if (error)
435f7a39160SGleb Smirnoff 		goto fail2;
436f7a39160SGleb Smirnoff 
437f7a39160SGleb Smirnoff 	/*
438f7a39160SGleb Smirnoff 	 * Add route for the network.
439f7a39160SGleb Smirnoff 	 */
440f7a39160SGleb Smirnoff 	if (vhid == 0) {
441f7a39160SGleb Smirnoff 		int flags = RTF_UP;
442f7a39160SGleb Smirnoff 
443f7a39160SGleb Smirnoff 		if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
444f7a39160SGleb Smirnoff 			flags |= RTF_HOST;
445f7a39160SGleb Smirnoff 
446f7a39160SGleb Smirnoff 		error = in_addprefix(ia, flags);
447f7a39160SGleb Smirnoff 		if (error)
448f7a39160SGleb Smirnoff 			goto fail2;
449df8bae1dSRodney W. Grimes 	}
450df8bae1dSRodney W. Grimes 
451588885f2SRobert Watson 	/*
452f7a39160SGleb Smirnoff 	 * Add a loopback route to self.
453588885f2SRobert Watson 	 */
454f7a39160SGleb Smirnoff 	if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 &&
455c1f7c3f5SGleb Smirnoff 	    ia->ia_addr.sin_addr.s_addr != INADDR_ANY &&
456c1f7c3f5SGleb Smirnoff 	    !((ifp->if_flags & IFF_POINTOPOINT) &&
457c1f7c3f5SGleb Smirnoff 	     ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
458f7a39160SGleb Smirnoff 		struct in_ifaddr *eia;
459df8bae1dSRodney W. Grimes 
460f7a39160SGleb Smirnoff 		eia = in_localip_more(ia);
461f7a39160SGleb Smirnoff 
462f7a39160SGleb Smirnoff 		if (eia == NULL) {
463f7a39160SGleb Smirnoff 			error = ifa_add_loopback_route((struct ifaddr *)ia,
464f7a39160SGleb Smirnoff 			    (struct sockaddr *)&ia->ia_addr);
465f7a39160SGleb Smirnoff 			if (error)
466f7a39160SGleb Smirnoff 				goto fail3;
467f7a39160SGleb Smirnoff 		} else
468f7a39160SGleb Smirnoff 			ifa_free(&eia->ia_ifa);
469588885f2SRobert Watson 	}
470df8bae1dSRodney W. Grimes 
471f7a39160SGleb Smirnoff 	if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
472f7a39160SGleb Smirnoff 		struct in_addr allhosts_addr;
473f7a39160SGleb Smirnoff 		struct in_ifinfo *ii;
474df8bae1dSRodney W. Grimes 
475c75aa354SBruce M Simpson 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
476f7a39160SGleb Smirnoff 		allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
477df8bae1dSRodney W. Grimes 
478f7a39160SGleb Smirnoff 		error = in_joingroup(ifp, &allhosts_addr, NULL,
479f7a39160SGleb Smirnoff 			&ii->ii_allhosts);
480f7a39160SGleb Smirnoff 	}
481f7a39160SGleb Smirnoff 
482f7a39160SGleb Smirnoff 	EVENTHANDLER_INVOKE(ifaddr_event, ifp);
483f7a39160SGleb Smirnoff 
484f7a39160SGleb Smirnoff 	return (error);
485f7a39160SGleb Smirnoff 
486f7a39160SGleb Smirnoff fail3:
487f7a39160SGleb Smirnoff 	if (vhid == 0)
488f7a39160SGleb Smirnoff 		(void )in_scrubprefix(ia, LLE_STATIC);
489f7a39160SGleb Smirnoff 
490f7a39160SGleb Smirnoff fail2:
491f7a39160SGleb Smirnoff 	if (ia->ia_ifa.ifa_carp)
492f7a39160SGleb Smirnoff 		(*carp_detach_p)(&ia->ia_ifa);
493f7a39160SGleb Smirnoff 
494f7a39160SGleb Smirnoff fail1:
495f7a39160SGleb Smirnoff 	IF_ADDR_WLOCK(ifp);
496f7a39160SGleb Smirnoff 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
497f7a39160SGleb Smirnoff 	IF_ADDR_WUNLOCK(ifp);
498*a49b317cSAlexander V. Chernikov 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
499f7a39160SGleb Smirnoff 
500f7a39160SGleb Smirnoff 	IN_IFADDR_WLOCK();
501f7a39160SGleb Smirnoff 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
502f7a39160SGleb Smirnoff 	LIST_REMOVE(ia, ia_hash);
503f7a39160SGleb Smirnoff 	IN_IFADDR_WUNLOCK();
504*a49b317cSAlexander V. Chernikov 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
505f7a39160SGleb Smirnoff 
506f7a39160SGleb Smirnoff 	return (error);
507f7a39160SGleb Smirnoff }
508f7a39160SGleb Smirnoff 
509f7a39160SGleb Smirnoff static int
510f7a39160SGleb Smirnoff in_difaddr_ioctl(caddr_t data, struct ifnet *ifp, struct thread *td)
511f7a39160SGleb Smirnoff {
512f7a39160SGleb Smirnoff 	const struct ifreq *ifr = (struct ifreq *)data;
5136224cd89SNathan Whitehorn 	const struct sockaddr_in *addr = (const struct sockaddr_in *)
5146224cd89SNathan Whitehorn 	    &ifr->ifr_addr;
515f7a39160SGleb Smirnoff 	struct ifaddr *ifa;
516f7a39160SGleb Smirnoff 	struct in_ifaddr *ia;
517f7a39160SGleb Smirnoff 	bool deleteAny, iaIsLast;
518f7a39160SGleb Smirnoff 	int error;
519f7a39160SGleb Smirnoff 
520f7a39160SGleb Smirnoff 	if (td != NULL) {
521f7a39160SGleb Smirnoff 		error = priv_check(td, PRIV_NET_DELIFADDR);
522f7a39160SGleb Smirnoff 		if (error)
523f7a39160SGleb Smirnoff 			return (error);
524f7a39160SGleb Smirnoff 	}
525f7a39160SGleb Smirnoff 
526f7a39160SGleb Smirnoff 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
527f7a39160SGleb Smirnoff 	    addr->sin_family != AF_INET)
528f7a39160SGleb Smirnoff 		deleteAny = true;
529f7a39160SGleb Smirnoff 	else
530f7a39160SGleb Smirnoff 		deleteAny = false;
531f7a39160SGleb Smirnoff 
532f7a39160SGleb Smirnoff 	iaIsLast = true;
533f7a39160SGleb Smirnoff 	ia = NULL;
534f7a39160SGleb Smirnoff 	IF_ADDR_WLOCK(ifp);
535f7a39160SGleb Smirnoff 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
5369706c950SGleb Smirnoff 		struct in_ifaddr *it;
537f7a39160SGleb Smirnoff 
5389706c950SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET)
539f7a39160SGleb Smirnoff 			continue;
540f7a39160SGleb Smirnoff 
5419706c950SGleb Smirnoff 		it = (struct in_ifaddr *)ifa;
542f7a39160SGleb Smirnoff 		if (deleteAny && ia == NULL && (td == NULL ||
543f7a39160SGleb Smirnoff 		    prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
544f7a39160SGleb Smirnoff 			ia = it;
545f7a39160SGleb Smirnoff 
546f7a39160SGleb Smirnoff 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
547f7a39160SGleb Smirnoff 		    (td == NULL || prison_check_ip4(td->td_ucred,
548f7a39160SGleb Smirnoff 		    &addr->sin_addr) == 0))
549f7a39160SGleb Smirnoff 			ia = it;
550f7a39160SGleb Smirnoff 
551f7a39160SGleb Smirnoff 		if (it != ia)
552f7a39160SGleb Smirnoff 			iaIsLast = false;
553f7a39160SGleb Smirnoff 	}
554f7a39160SGleb Smirnoff 
555f7a39160SGleb Smirnoff 	if (ia == NULL) {
556f7a39160SGleb Smirnoff 		IF_ADDR_WUNLOCK(ifp);
557f7a39160SGleb Smirnoff 		return (EADDRNOTAVAIL);
558f7a39160SGleb Smirnoff 	}
559f7a39160SGleb Smirnoff 
560f7a39160SGleb Smirnoff 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
561f7a39160SGleb Smirnoff 	IF_ADDR_WUNLOCK(ifp);
562f7a39160SGleb Smirnoff 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
563f7a39160SGleb Smirnoff 
564f7a39160SGleb Smirnoff 	IN_IFADDR_WLOCK();
565f7a39160SGleb Smirnoff 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
566f7a39160SGleb Smirnoff 	LIST_REMOVE(ia, ia_hash);
567f7a39160SGleb Smirnoff 	IN_IFADDR_WUNLOCK();
568f7a39160SGleb Smirnoff 
569089cdfadSRuslan Ermilov 	/*
570237bf7f7SGleb Smirnoff 	 * in_scrubprefix() kills the interface route.
571089cdfadSRuslan Ermilov 	 */
572237bf7f7SGleb Smirnoff 	in_scrubprefix(ia, LLE_STATIC);
573588885f2SRobert Watson 
574c655b7c4SDavid Greenman 	/*
575089cdfadSRuslan Ermilov 	 * in_ifadown gets rid of all the rest of
576089cdfadSRuslan Ermilov 	 * the routes.  This is not quite the right
577089cdfadSRuslan Ermilov 	 * thing to do, but at least if we are running
578089cdfadSRuslan Ermilov 	 * a routing process they will come back.
579089cdfadSRuslan Ermilov 	 */
58091854268SRuslan Ermilov 	in_ifadown(&ia->ia_ifa, 1);
5810f02fdacSBrian Somers 
58208b68b0eSGleb Smirnoff 	if (ia->ia_ifa.ifa_carp)
58308b68b0eSGleb Smirnoff 		(*carp_detach_p)(&ia->ia_ifa);
58408b68b0eSGleb Smirnoff 
585f7e083afSBruce M Simpson 	/*
586f7e083afSBruce M Simpson 	 * If this is the last IPv4 address configured on this
587f7e083afSBruce M Simpson 	 * interface, leave the all-hosts group.
588d10910e6SBruce M Simpson 	 * No state-change report need be transmitted.
589f7e083afSBruce M Simpson 	 */
590f7a39160SGleb Smirnoff 	if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
591f7a39160SGleb Smirnoff 		struct in_ifinfo *ii;
592f7a39160SGleb Smirnoff 
593c75aa354SBruce M Simpson 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
594f7e083afSBruce M Simpson 		IN_MULTI_LOCK();
595d10910e6SBruce M Simpson 		if (ii->ii_allhosts) {
5966d00fd9cSGleb Smirnoff 			(void)in_leavegroup_locked(ii->ii_allhosts, NULL);
597d10910e6SBruce M Simpson 			ii->ii_allhosts = NULL;
598d10910e6SBruce M Simpson 		}
599f7e083afSBruce M Simpson 		IN_MULTI_UNLOCK();
600f7a39160SGleb Smirnoff 	}
6016d00fd9cSGleb Smirnoff 
602f7a39160SGleb Smirnoff 	EVENTHANDLER_INVOKE(ifaddr_event, ifp);
603*a49b317cSAlexander V. Chernikov 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
604f7a39160SGleb Smirnoff 
605f7a39160SGleb Smirnoff 	return (0);
606df8bae1dSRodney W. Grimes }
607df8bae1dSRodney W. Grimes 
60848321abeSMax Laier #define rtinitflags(x) \
60948321abeSMax Laier 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
61048321abeSMax Laier 	    ? RTF_HOST : 0)
611ccbb9c35SQing Li 
612ccbb9c35SQing Li /*
613fbdd20a1SMatt Jacob  * Check if we have a route for the given prefix already or add one accordingly.
61448321abeSMax Laier  */
61508b68b0eSGleb Smirnoff int
616f2565d68SRobert Watson in_addprefix(struct in_ifaddr *target, int flags)
61748321abeSMax Laier {
61848321abeSMax Laier 	struct in_ifaddr *ia;
619bfb26eecSGleb Smirnoff 	struct in_addr prefix, mask, p, m;
620d375edc9SAlexander V. Chernikov 	int error, fibnum;
62148321abeSMax Laier 
622fbdd20a1SMatt Jacob 	if ((flags & RTF_HOST) != 0) {
62348321abeSMax Laier 		prefix = target->ia_dstaddr.sin_addr;
624fbdd20a1SMatt Jacob 		mask.s_addr = 0;
625fbdd20a1SMatt Jacob 	} else {
62648321abeSMax Laier 		prefix = target->ia_addr.sin_addr;
62748321abeSMax Laier 		mask = target->ia_sockmask.sin_addr;
62848321abeSMax Laier 		prefix.s_addr &= mask.s_addr;
62948321abeSMax Laier 	}
63048321abeSMax Laier 
631d375edc9SAlexander V. Chernikov 	fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS : target->ia_ifp->if_fib;
632d375edc9SAlexander V. Chernikov 
6332d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
634603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
635bfb26eecSGleb Smirnoff 		if (rtinitflags(ia)) {
63672366606SKevin Lo 			p = ia->ia_dstaddr.sin_addr;
63748321abeSMax Laier 
63848321abeSMax Laier 			if (prefix.s_addr != p.s_addr)
63948321abeSMax Laier 				continue;
640bfb26eecSGleb Smirnoff 		} else {
641bfb26eecSGleb Smirnoff 			p = ia->ia_addr.sin_addr;
642bfb26eecSGleb Smirnoff 			m = ia->ia_sockmask.sin_addr;
643bfb26eecSGleb Smirnoff 			p.s_addr &= m.s_addr;
644bfb26eecSGleb Smirnoff 
645bfb26eecSGleb Smirnoff 			if (prefix.s_addr != p.s_addr ||
646bfb26eecSGleb Smirnoff 			    mask.s_addr != m.s_addr)
647bfb26eecSGleb Smirnoff 				continue;
648bfb26eecSGleb Smirnoff 		}
64948321abeSMax Laier 
65048321abeSMax Laier 		/*
65148321abeSMax Laier 		 * If we got a matching prefix route inserted by other
65248321abeSMax Laier 		 * interface address, we are done here.
65348321abeSMax Laier 		 */
6541ae95409SGleb Smirnoff 		if (ia->ia_flags & IFA_ROUTE) {
655c7ea0aa6SQing Li #ifdef RADIX_MPATH
656c7ea0aa6SQing Li 			if (ia->ia_addr.sin_addr.s_addr ==
65742db1b87SBjoern A. Zeeb 			    target->ia_addr.sin_addr.s_addr) {
65842db1b87SBjoern A. Zeeb 				IN_IFADDR_RUNLOCK();
659c7ea0aa6SQing Li 				return (EEXIST);
66042db1b87SBjoern A. Zeeb 			} else
661c7ea0aa6SQing Li 				break;
662c7ea0aa6SQing Li #endif
66308b68b0eSGleb Smirnoff 			if (V_nosameprefix) {
6642d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
6651ae95409SGleb Smirnoff 				return (EEXIST);
6662d9cfabaSRobert Watson 			} else {
667d375edc9SAlexander V. Chernikov 				rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
6682d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
6691ae95409SGleb Smirnoff 				return (0);
6701ae95409SGleb Smirnoff 			}
67148321abeSMax Laier 		}
6722d9cfabaSRobert Watson 	}
6732d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
67448321abeSMax Laier 
67548321abeSMax Laier 	/*
67648321abeSMax Laier 	 * No-one seem to have this prefix route, so we try to insert it.
67748321abeSMax Laier 	 */
67848321abeSMax Laier 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
67948321abeSMax Laier 	if (!error)
68048321abeSMax Laier 		target->ia_flags |= IFA_ROUTE;
681460473a0SBjoern A. Zeeb 	return (error);
68248321abeSMax Laier }
68348321abeSMax Laier 
68448321abeSMax Laier /*
68548321abeSMax Laier  * If there is no other address in the system that can serve a route to the
68648321abeSMax Laier  * same prefix, remove the route.  Hand over the route to the new address
68748321abeSMax Laier  * otherwise.
68848321abeSMax Laier  */
68908b68b0eSGleb Smirnoff int
6905b84dc78SQing Li in_scrubprefix(struct in_ifaddr *target, u_int flags)
69148321abeSMax Laier {
69248321abeSMax Laier 	struct in_ifaddr *ia;
69355174c34SGleb Smirnoff 	struct in_addr prefix, mask, p, m;
694d375edc9SAlexander V. Chernikov 	int error = 0, fibnum;
695c9d763bfSQing Li 	struct sockaddr_in prefix0, mask0;
69648321abeSMax Laier 
697d375edc9SAlexander V. Chernikov 	fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS : target->ia_ifp->if_fib;
698d375edc9SAlexander V. Chernikov 
699df813b7eSQing Li 	/*
700df813b7eSQing Li 	 * Remove the loopback route to the interface address.
701df813b7eSQing Li 	 */
70292fac994SQing Li 	if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
703c7ab6602SQing Li 	    !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
704f7a39160SGleb Smirnoff 	    (flags & LLE_STATIC)) {
705f7a39160SGleb Smirnoff 		struct in_ifaddr *eia;
706c7ab6602SQing Li 
707f7a39160SGleb Smirnoff 		eia = in_localip_more(target);
708f7a39160SGleb Smirnoff 
709f7a39160SGleb Smirnoff 		if (eia != NULL) {
710f7a39160SGleb Smirnoff 			error = ifa_switch_loopback_route((struct ifaddr *)eia,
711f7a39160SGleb Smirnoff 			    (struct sockaddr *)&target->ia_addr);
712f7a39160SGleb Smirnoff 			ifa_free(&eia->ia_ifa);
713f7a39160SGleb Smirnoff 		} else {
7149bb7d0f4SQing Li 			error = ifa_del_loopback_route((struct ifaddr *)target,
7159bb7d0f4SQing Li 			    (struct sockaddr *)&target->ia_addr);
7165b84dc78SQing Li 		}
717f7a39160SGleb Smirnoff 
718f7a39160SGleb Smirnoff 		if (!(target->ia_ifp->if_flags & IFF_NOARP))
719fa3cfd39SQing Li 			/* remove arp cache */
720f7a39160SGleb Smirnoff 			arp_ifscrub(target->ia_ifp,
721f7a39160SGleb Smirnoff 			    IA_SIN(target)->sin_addr.s_addr);
722ebc90701SQing Li 	}
723ebc90701SQing Li 
72455174c34SGleb Smirnoff 	if (rtinitflags(target)) {
72548321abeSMax Laier 		prefix = target->ia_dstaddr.sin_addr;
72655174c34SGleb Smirnoff 		mask.s_addr = 0;
72755174c34SGleb Smirnoff 	} else {
72848321abeSMax Laier 		prefix = target->ia_addr.sin_addr;
72948321abeSMax Laier 		mask = target->ia_sockmask.sin_addr;
73048321abeSMax Laier 		prefix.s_addr &= mask.s_addr;
73148321abeSMax Laier 	}
73248321abeSMax Laier 
733ccbb9c35SQing Li 	if ((target->ia_flags & IFA_ROUTE) == 0) {
734d375edc9SAlexander V. Chernikov 		rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
735ccbb9c35SQing Li 		return (0);
736ccbb9c35SQing Li 	}
737ccbb9c35SQing Li 
7382d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
739603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
74055174c34SGleb Smirnoff 		if (rtinitflags(ia)) {
74148321abeSMax Laier 			p = ia->ia_dstaddr.sin_addr;
74255174c34SGleb Smirnoff 
74355174c34SGleb Smirnoff 			if (prefix.s_addr != p.s_addr)
74455174c34SGleb Smirnoff 				continue;
74555174c34SGleb Smirnoff 		} else {
74648321abeSMax Laier 			p = ia->ia_addr.sin_addr;
74755174c34SGleb Smirnoff 			m = ia->ia_sockmask.sin_addr;
74855174c34SGleb Smirnoff 			p.s_addr &= m.s_addr;
74955174c34SGleb Smirnoff 
75055174c34SGleb Smirnoff 			if (prefix.s_addr != p.s_addr ||
75155174c34SGleb Smirnoff 			    mask.s_addr != m.s_addr)
75255174c34SGleb Smirnoff 				continue;
75348321abeSMax Laier 		}
75448321abeSMax Laier 
75555174c34SGleb Smirnoff 		if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
75648321abeSMax Laier 			continue;
75748321abeSMax Laier 
75848321abeSMax Laier 		/*
75948321abeSMax Laier 		 * If we got a matching prefix address, move IFA_ROUTE and
76048321abeSMax Laier 		 * the route itself to it.  Make sure that routing daemons
76148321abeSMax Laier 		 * get a heads-up.
76248321abeSMax Laier 		 */
76308b68b0eSGleb Smirnoff 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
76479d51435SSergey Kandaurov 			ifa_ref(&ia->ia_ifa);
7652d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
76692322284SQing Li 			error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
76748321abeSMax Laier 			    rtinitflags(target));
76892322284SQing Li 			if (error == 0)
76948321abeSMax Laier 				target->ia_flags &= ~IFA_ROUTE;
77092322284SQing Li 			else
77192322284SQing Li 				log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
77292322284SQing Li 					error);
77348321abeSMax Laier 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
77448321abeSMax Laier 			    rtinitflags(ia) | RTF_UP);
77548321abeSMax Laier 			if (error == 0)
77648321abeSMax Laier 				ia->ia_flags |= IFA_ROUTE;
77792322284SQing Li 			else
77892322284SQing Li 				log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
77992322284SQing Li 					error);
78079d51435SSergey Kandaurov 			ifa_free(&ia->ia_ifa);
781460473a0SBjoern A. Zeeb 			return (error);
78248321abeSMax Laier 		}
78348321abeSMax Laier 	}
7842d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
78548321abeSMax Laier 
78648321abeSMax Laier 	/*
787c9d763bfSQing Li 	 * remove all L2 entries on the given prefix
788c9d763bfSQing Li 	 */
789c9d763bfSQing Li 	bzero(&prefix0, sizeof(prefix0));
790c9d763bfSQing Li 	prefix0.sin_len = sizeof(prefix0);
791c9d763bfSQing Li 	prefix0.sin_family = AF_INET;
792c9d763bfSQing Li 	prefix0.sin_addr.s_addr = target->ia_subnet;
793c9d763bfSQing Li 	bzero(&mask0, sizeof(mask0));
794c9d763bfSQing Li 	mask0.sin_len = sizeof(mask0);
795c9d763bfSQing Li 	mask0.sin_family = AF_INET;
796c9d763bfSQing Li 	mask0.sin_addr.s_addr = target->ia_subnetmask;
797c9d763bfSQing Li 	lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
7985b84dc78SQing Li 	    (struct sockaddr *)&mask0, flags);
799c9d763bfSQing Li 
800c9d763bfSQing Li 	/*
80148321abeSMax Laier 	 * As no-one seem to have this prefix, we can remove the route.
80248321abeSMax Laier 	 */
80392322284SQing Li 	error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
80492322284SQing Li 	if (error == 0)
80548321abeSMax Laier 		target->ia_flags &= ~IFA_ROUTE;
80692322284SQing Li 	else
80792322284SQing Li 		log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
80892322284SQing Li 	return (error);
80948321abeSMax Laier }
81048321abeSMax Laier 
81148321abeSMax Laier #undef rtinitflags
812df8bae1dSRodney W. Grimes 
813df8bae1dSRodney W. Grimes /*
814df8bae1dSRodney W. Grimes  * Return 1 if the address might be a local broadcast address.
815df8bae1dSRodney W. Grimes  */
81626f9a767SRodney W. Grimes int
817f2565d68SRobert Watson in_broadcast(struct in_addr in, struct ifnet *ifp)
818df8bae1dSRodney W. Grimes {
819df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
820df8bae1dSRodney W. Grimes 	u_long t;
821df8bae1dSRodney W. Grimes 
822df8bae1dSRodney W. Grimes 	if (in.s_addr == INADDR_BROADCAST ||
823df8bae1dSRodney W. Grimes 	    in.s_addr == INADDR_ANY)
824460473a0SBjoern A. Zeeb 		return (1);
825df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
826460473a0SBjoern A. Zeeb 		return (0);
827df8bae1dSRodney W. Grimes 	t = ntohl(in.s_addr);
828df8bae1dSRodney W. Grimes 	/*
829df8bae1dSRodney W. Grimes 	 * Look through the list of addresses for a match
830df8bae1dSRodney W. Grimes 	 * with a broadcast address.
831df8bae1dSRodney W. Grimes 	 */
832df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa)
833462b86feSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
834df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == AF_INET &&
835df8bae1dSRodney W. Grimes 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
836df8bae1dSRodney W. Grimes 		     /*
83753883e0cSGleb Smirnoff 		      * Check for old-style (host 0) broadcast, but
83853883e0cSGleb Smirnoff 		      * taking into account that RFC 3021 obsoletes it.
839df8bae1dSRodney W. Grimes 		      */
84053883e0cSGleb Smirnoff 		    (ia->ia_subnetmask != IN_RFC3021_MASK &&
84153883e0cSGleb Smirnoff 		    t == ia->ia_subnet)) &&
8428dd27fd6SGuido van Rooij 		     /*
8438dd27fd6SGuido van Rooij 		      * Check for an all one subnetmask. These
8448dd27fd6SGuido van Rooij 		      * only exist when an interface gets a secondary
8458dd27fd6SGuido van Rooij 		      * address.
8468dd27fd6SGuido van Rooij 		      */
8478dd27fd6SGuido van Rooij 		    ia->ia_subnetmask != (u_long)0xffffffff)
848460473a0SBjoern A. Zeeb 			    return (1);
849df8bae1dSRodney W. Grimes 	return (0);
850df8bae1dSRodney W. Grimes #undef ia
851df8bae1dSRodney W. Grimes }
852ec002feeSBruce M Simpson 
853df8bae1dSRodney W. Grimes /*
854b1c53bc9SRobert Watson  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
855b1c53bc9SRobert Watson  */
856b1c53bc9SRobert Watson void
857f2565d68SRobert Watson in_ifdetach(struct ifnet *ifp)
858b1c53bc9SRobert Watson {
859b1c53bc9SRobert Watson 
860603724d3SBjoern A. Zeeb 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
861603724d3SBjoern A. Zeeb 	in_pcbpurgeif0(&V_udbinfo, ifp);
862ec002feeSBruce M Simpson 	in_purgemaddrs(ifp);
863b1c53bc9SRobert Watson }
8646e6b3f7cSQing Li 
865d10910e6SBruce M Simpson /*
866d10910e6SBruce M Simpson  * Delete all IPv4 multicast address records, and associated link-layer
867d10910e6SBruce M Simpson  * multicast address records, associated with ifp.
868d10910e6SBruce M Simpson  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
86956663a40SBruce M Simpson  * XXX This should not race with ifma_protospec being set during
87056663a40SBruce M Simpson  * a new allocation, if it does, we have bigger problems.
871d10910e6SBruce M Simpson  */
872d10910e6SBruce M Simpson static void
873d10910e6SBruce M Simpson in_purgemaddrs(struct ifnet *ifp)
874d10910e6SBruce M Simpson {
875d10910e6SBruce M Simpson 	LIST_HEAD(,in_multi) purgeinms;
876d10910e6SBruce M Simpson 	struct in_multi		*inm, *tinm;
877d10910e6SBruce M Simpson 	struct ifmultiaddr	*ifma;
878d10910e6SBruce M Simpson 
879d10910e6SBruce M Simpson 	LIST_INIT(&purgeinms);
880d10910e6SBruce M Simpson 	IN_MULTI_LOCK();
881d10910e6SBruce M Simpson 
882d10910e6SBruce M Simpson 	/*
883d10910e6SBruce M Simpson 	 * Extract list of in_multi associated with the detaching ifp
884d10910e6SBruce M Simpson 	 * which the PF_INET layer is about to release.
885d10910e6SBruce M Simpson 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
886d10910e6SBruce M Simpson 	 * by code further down.
887d10910e6SBruce M Simpson 	 */
888137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
889d10910e6SBruce M Simpson 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
89056663a40SBruce M Simpson 		if (ifma->ifma_addr->sa_family != AF_INET ||
89156663a40SBruce M Simpson 		    ifma->ifma_protospec == NULL)
892d10910e6SBruce M Simpson 			continue;
89356663a40SBruce M Simpson #if 0
89456663a40SBruce M Simpson 		KASSERT(ifma->ifma_protospec != NULL,
89556663a40SBruce M Simpson 		    ("%s: ifma_protospec is NULL", __func__));
89656663a40SBruce M Simpson #endif
897d10910e6SBruce M Simpson 		inm = (struct in_multi *)ifma->ifma_protospec;
898d10910e6SBruce M Simpson 		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
899d10910e6SBruce M Simpson 	}
900137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
901d10910e6SBruce M Simpson 
902d10910e6SBruce M Simpson 	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
903d10910e6SBruce M Simpson 		LIST_REMOVE(inm, inm_link);
90407cde5e9SRobert Watson 		inm_release_locked(inm);
905d10910e6SBruce M Simpson 	}
906d10910e6SBruce M Simpson 	igmp_ifdetach(ifp);
907d10910e6SBruce M Simpson 
908d10910e6SBruce M Simpson 	IN_MULTI_UNLOCK();
909d10910e6SBruce M Simpson }
910d10910e6SBruce M Simpson 
9116e6b3f7cSQing Li struct in_llentry {
9126e6b3f7cSQing Li 	struct llentry		base;
9136e6b3f7cSQing Li 	struct sockaddr_in	l3_addr4;
9146e6b3f7cSQing Li };
9156e6b3f7cSQing Li 
916a93cda78SKip Macy /*
917a93cda78SKip Macy  * Deletes an address from the address table.
918a93cda78SKip Macy  * This function is called by the timer functions
919a93cda78SKip Macy  * such as arptimer() and nd6_llinfo_timer(), and
920a93cda78SKip Macy  * the caller does the locking.
921a93cda78SKip Macy  */
922a93cda78SKip Macy static void
923a93cda78SKip Macy in_lltable_free(struct lltable *llt, struct llentry *lle)
924a93cda78SKip Macy {
925a93cda78SKip Macy 	LLE_WUNLOCK(lle);
926a93cda78SKip Macy 	LLE_LOCK_DESTROY(lle);
927a93cda78SKip Macy 	free(lle, M_LLTABLE);
928a93cda78SKip Macy }
929a93cda78SKip Macy 
9306e6b3f7cSQing Li static struct llentry *
9316e6b3f7cSQing Li in_lltable_new(const struct sockaddr *l3addr, u_int flags)
9326e6b3f7cSQing Li {
9336e6b3f7cSQing Li 	struct in_llentry *lle;
9346e6b3f7cSQing Li 
93590b357f6SGleb Smirnoff 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
9366e6b3f7cSQing Li 	if (lle == NULL)		/* NB: caller generates msg */
9376e6b3f7cSQing Li 		return NULL;
9386e6b3f7cSQing Li 
9396e6b3f7cSQing Li 	/*
9406e6b3f7cSQing Li 	 * For IPv4 this will trigger "arpresolve" to generate
9416e6b3f7cSQing Li 	 * an ARP request.
9426e6b3f7cSQing Li 	 */
943a98c06f1SGleb Smirnoff 	lle->base.la_expire = time_uptime; /* mark expired */
9446e6b3f7cSQing Li 	lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
9456e6b3f7cSQing Li 	lle->base.lle_refcnt = 1;
946a93cda78SKip Macy 	lle->base.lle_free = in_lltable_free;
9476e6b3f7cSQing Li 	LLE_LOCK_INIT(&lle->base);
948ea537929SGleb Smirnoff 	callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock,
949ea537929SGleb Smirnoff 	    CALLOUT_RETURNUNLOCKED);
950ea537929SGleb Smirnoff 
951ea537929SGleb Smirnoff 	return (&lle->base);
9526e6b3f7cSQing Li }
9536e6b3f7cSQing Li 
954c9d763bfSQing Li #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(			\
955c9d763bfSQing Li 	    (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
956c9d763bfSQing Li 
957c9d763bfSQing Li static void
958ea50c13eSGleb Smirnoff in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
959ea50c13eSGleb Smirnoff     const struct sockaddr *mask, u_int flags)
960c9d763bfSQing Li {
961c9d763bfSQing Li 	const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
962c9d763bfSQing Li 	const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
963c9d763bfSQing Li 	struct llentry *lle, *next;
964ea50c13eSGleb Smirnoff 	int i;
965e162ea60SGeorge V. Neville-Neil 	size_t pkts_dropped;
966c9d763bfSQing Li 
967ea537929SGleb Smirnoff 	IF_AFDATA_WLOCK(llt->llt_ifp);
968c9d763bfSQing Li 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
969c9d763bfSQing Li 		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
9705b84dc78SQing Li 			/*
9715b84dc78SQing Li 			 * (flags & LLE_STATIC) means deleting all entries
972ea50c13eSGleb Smirnoff 			 * including static ARP entries.
9735b84dc78SQing Li 			 */
974ea50c13eSGleb Smirnoff 			if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)),
975ea50c13eSGleb Smirnoff 			    pfx, msk) && ((flags & LLE_STATIC) ||
976ea50c13eSGleb Smirnoff 			    !(lle->la_flags & LLE_STATIC))) {
977c9d763bfSQing Li 				LLE_WLOCK(lle);
978ea537929SGleb Smirnoff 				if (callout_stop(&lle->la_timer))
979becba438SBjoern A. Zeeb 					LLE_REMREF(lle);
980e162ea60SGeorge V. Neville-Neil 				pkts_dropped = llentry_free(lle);
981e162ea60SGeorge V. Neville-Neil 				ARPSTAT_ADD(dropped, pkts_dropped);
982c9d763bfSQing Li 			}
983c9d763bfSQing Li 		}
984c9d763bfSQing Li 	}
985ea537929SGleb Smirnoff 	IF_AFDATA_WUNLOCK(llt->llt_ifp);
986c9d763bfSQing Li }
987c9d763bfSQing Li 
988c9d763bfSQing Li 
9896e6b3f7cSQing Li static int
990c7ab6602SQing Li in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
9916e6b3f7cSQing Li {
9926e6b3f7cSQing Li 	struct rtentry *rt;
9936e6b3f7cSQing Li 
9946e6b3f7cSQing Li 	KASSERT(l3addr->sa_family == AF_INET,
9956e6b3f7cSQing Li 	    ("sin_family %d", l3addr->sa_family));
9966e6b3f7cSQing Li 
9976e6b3f7cSQing Li 	/* XXX rtalloc1 should take a const param */
9986e6b3f7cSQing Li 	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
99913e255faSMarko Zec 
10006cf8e330SQing Li 	if (rt == NULL)
10016cf8e330SQing Li 		return (EINVAL);
10026cf8e330SQing Li 
100313e255faSMarko Zec 	/*
100413e255faSMarko Zec 	 * If the gateway for an existing host route matches the target L3
10056cf8e330SQing Li 	 * address, which is a special route inserted by some implementation
10066cf8e330SQing Li 	 * such as MANET, and the interface is of the correct type, then
10076cf8e330SQing Li 	 * allow for ARP to proceed.
100813e255faSMarko Zec 	 */
1009db92413eSQing Li 	if (rt->rt_flags & RTF_GATEWAY) {
101015d25219SQing Li 		if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
101115d25219SQing Li 		    rt->rt_ifp->if_type != IFT_ETHER ||
1012ea50c13eSGleb Smirnoff 		    (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
101315d25219SQing Li 		    memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
101415d25219SQing Li 		    sizeof(in_addr_t)) != 0) {
1015db92413eSQing Li 			RTFREE_LOCKED(rt);
1016db92413eSQing Li 			return (EINVAL);
1017db92413eSQing Li 		}
101815d25219SQing Li 	}
1019db92413eSQing Li 
1020db92413eSQing Li 	/*
1021db92413eSQing Li 	 * Make sure that at least the destination address is covered
1022db92413eSQing Li 	 * by the route. This is for handling the case where 2 or more
1023db92413eSQing Li 	 * interfaces have the same prefix. An incoming packet arrives
1024db92413eSQing Li 	 * on one interface and the corresponding outgoing packet leaves
1025db92413eSQing Li 	 * another interface.
1026db92413eSQing Li 	 */
1027b3664a14SQing Li 	if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
102815d25219SQing Li 		const char *sa, *mask, *addr, *lim;
1029db92413eSQing Li 		int len;
1030db92413eSQing Li 
103115d25219SQing Li 		mask = (const char *)rt_mask(rt);
1032b3664a14SQing Li 		/*
1033b3664a14SQing Li 		 * Just being extra cautious to avoid some custom
1034b3664a14SQing Li 		 * code getting into trouble.
1035b3664a14SQing Li 		 */
1036b3664a14SQing Li 		if (mask == NULL) {
1037b3664a14SQing Li 			RTFREE_LOCKED(rt);
1038b3664a14SQing Li 			return (EINVAL);
1039b3664a14SQing Li 		}
1040b3664a14SQing Li 
1041b3664a14SQing Li 		sa = (const char *)rt_key(rt);
104215d25219SQing Li 		addr = (const char *)l3addr;
104315d25219SQing Li 		len = ((const struct sockaddr_in *)l3addr)->sin_len;
1044db92413eSQing Li 		lim = addr + len;
1045db92413eSQing Li 
1046db92413eSQing Li 		for ( ; addr < lim; sa++, mask++, addr++) {
1047db92413eSQing Li 			if ((*sa ^ *addr) & *mask) {
10486cb2b4e7SQing Li #ifdef DIAGNOSTIC
10496e6b3f7cSQing Li 				log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
10506e6b3f7cSQing Li 				    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1051b4a22c36SQing Li #endif
105215d25219SQing Li 				RTFREE_LOCKED(rt);
105315d25219SQing Li 				return (EINVAL);
10546e6b3f7cSQing Li 			}
1055db92413eSQing Li 		}
1056db92413eSQing Li 	}
1057db92413eSQing Li 
10586e6b3f7cSQing Li 	RTFREE_LOCKED(rt);
105915d25219SQing Li 	return (0);
10606e6b3f7cSQing Li }
10616e6b3f7cSQing Li 
10626e6b3f7cSQing Li /*
10636e6b3f7cSQing Li  * Return NULL if not found or marked for deletion.
10646e6b3f7cSQing Li  * If found return lle read locked.
10656e6b3f7cSQing Li  */
10666e6b3f7cSQing Li static struct llentry *
10676e6b3f7cSQing Li in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
10686e6b3f7cSQing Li {
10696e6b3f7cSQing Li 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
10706e6b3f7cSQing Li 	struct ifnet *ifp = llt->llt_ifp;
10716e6b3f7cSQing Li 	struct llentry *lle;
10726e6b3f7cSQing Li 	struct llentries *lleh;
10736e6b3f7cSQing Li 	u_int hashkey;
10746e6b3f7cSQing Li 
10756e6b3f7cSQing Li 	IF_AFDATA_LOCK_ASSERT(ifp);
10766e6b3f7cSQing Li 	KASSERT(l3addr->sa_family == AF_INET,
10776e6b3f7cSQing Li 	    ("sin_family %d", l3addr->sa_family));
10786e6b3f7cSQing Li 
10796e6b3f7cSQing Li 	hashkey = sin->sin_addr.s_addr;
10806e6b3f7cSQing Li 	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
10816e6b3f7cSQing Li 	LIST_FOREACH(lle, lleh, lle_next) {
1082ea50c13eSGleb Smirnoff 		struct sockaddr_in *sa2 = satosin(L3_ADDR(lle));
10836e6b3f7cSQing Li 		if (lle->la_flags & LLE_DELETED)
10846e6b3f7cSQing Li 			continue;
1085dc495497SQing Li 		if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
10866e6b3f7cSQing Li 			break;
10876e6b3f7cSQing Li 	}
10886e6b3f7cSQing Li 	if (lle == NULL) {
10896cb2b4e7SQing Li #ifdef DIAGNOSTIC
10906e6b3f7cSQing Li 		if (flags & LLE_DELETE)
10916e6b3f7cSQing Li 			log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
10926e6b3f7cSQing Li #endif
10936e6b3f7cSQing Li 		if (!(flags & LLE_CREATE))
10946e6b3f7cSQing Li 			return (NULL);
1095e2d14d93SAndrey V. Elsukov 		IF_AFDATA_WLOCK_ASSERT(ifp);
10966e6b3f7cSQing Li 		/*
10976e6b3f7cSQing Li 		 * A route that covers the given address must have
10986e6b3f7cSQing Li 		 * been installed 1st because we are doing a resolution,
10996e6b3f7cSQing Li 		 * verify this.
11006e6b3f7cSQing Li 		 */
11016e6b3f7cSQing Li 		if (!(flags & LLE_IFADDR) &&
1102c7ab6602SQing Li 		    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
11036e6b3f7cSQing Li 			goto done;
11046e6b3f7cSQing Li 
11056e6b3f7cSQing Li 		lle = in_lltable_new(l3addr, flags);
11066e6b3f7cSQing Li 		if (lle == NULL) {
11076e6b3f7cSQing Li 			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
11086e6b3f7cSQing Li 			goto done;
11096e6b3f7cSQing Li 		}
11106e6b3f7cSQing Li 		lle->la_flags = flags & ~LLE_CREATE;
11116e6b3f7cSQing Li 		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
11126e6b3f7cSQing Li 			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
11136e6b3f7cSQing Li 			lle->la_flags |= (LLE_VALID | LLE_STATIC);
11146e6b3f7cSQing Li 		}
11156e6b3f7cSQing Li 
11166e6b3f7cSQing Li 		lle->lle_tbl  = llt;
11176e6b3f7cSQing Li 		lle->lle_head = lleh;
1118ea537929SGleb Smirnoff 		lle->la_flags |= LLE_LINKED;
11196e6b3f7cSQing Li 		LIST_INSERT_HEAD(lleh, lle, lle_next);
11206e6b3f7cSQing Li 	} else if (flags & LLE_DELETE) {
11216e6b3f7cSQing Li 		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
11226e6b3f7cSQing Li 			LLE_WLOCK(lle);
1123ea537929SGleb Smirnoff 			lle->la_flags |= LLE_DELETED;
112409fe6320SNavdeep Parhar 			EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
11256cb2b4e7SQing Li #ifdef DIAGNOSTIC
11266e6b3f7cSQing Li 			log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
11276e6b3f7cSQing Li #endif
11281571132fSOleg Bulyzhin 			if ((lle->la_flags &
11291571132fSOleg Bulyzhin 			    (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
11301571132fSOleg Bulyzhin 				llentry_free(lle);
11311571132fSOleg Bulyzhin 			else
11321571132fSOleg Bulyzhin 				LLE_WUNLOCK(lle);
11336e6b3f7cSQing Li 		}
11346e6b3f7cSQing Li 		lle = (void *)-1;
11356e6b3f7cSQing Li 
11366e6b3f7cSQing Li 	}
113742d866ddSBjoern A. Zeeb 	if (LLE_IS_VALID(lle)) {
11386e6b3f7cSQing Li 		if (flags & LLE_EXCLUSIVE)
11396e6b3f7cSQing Li 			LLE_WLOCK(lle);
11406e6b3f7cSQing Li 		else
11416e6b3f7cSQing Li 			LLE_RLOCK(lle);
11426e6b3f7cSQing Li 	}
11436e6b3f7cSQing Li done:
11446e6b3f7cSQing Li 	return (lle);
11456e6b3f7cSQing Li }
11466e6b3f7cSQing Li 
11476e6b3f7cSQing Li static int
11486e6b3f7cSQing Li in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
11496e6b3f7cSQing Li {
11506e6b3f7cSQing Li #define	SIN(lle)	((struct sockaddr_in *) L3_ADDR(lle))
11516e6b3f7cSQing Li 	struct ifnet *ifp = llt->llt_ifp;
11526e6b3f7cSQing Li 	struct llentry *lle;
11536e6b3f7cSQing Li 	/* XXX stack use */
11546e6b3f7cSQing Li 	struct {
11556e6b3f7cSQing Li 		struct rt_msghdr	rtm;
11569711a168SGleb Smirnoff 		struct sockaddr_in	sin;
11576e6b3f7cSQing Li 		struct sockaddr_dl	sdl;
11586e6b3f7cSQing Li 	} arpc;
11596e6b3f7cSQing Li 	int error, i;
11606e6b3f7cSQing Li 
1161dc56e98fSRobert Watson 	LLTABLE_LOCK_ASSERT();
11626e6b3f7cSQing Li 
11636e6b3f7cSQing Li 	error = 0;
11646e6b3f7cSQing Li 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
11656e6b3f7cSQing Li 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
11666e6b3f7cSQing Li 			struct sockaddr_dl *sdl;
11676e6b3f7cSQing Li 
11686e6b3f7cSQing Li 			/* skip deleted entries */
116993704ac5SQing Li 			if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
11706e6b3f7cSQing Li 				continue;
1171813dd6aeSBjoern A. Zeeb 			/* Skip if jailed and not a valid IP of the prison. */
1172b89e82ddSJamie Gritton 			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1173813dd6aeSBjoern A. Zeeb 				continue;
11746e6b3f7cSQing Li 			/*
11756e6b3f7cSQing Li 			 * produce a msg made of:
11766e6b3f7cSQing Li 			 *  struct rt_msghdr;
11779711a168SGleb Smirnoff 			 *  struct sockaddr_in; (IPv4)
11786e6b3f7cSQing Li 			 *  struct sockaddr_dl;
11796e6b3f7cSQing Li 			 */
11806e6b3f7cSQing Li 			bzero(&arpc, sizeof(arpc));
11816e6b3f7cSQing Li 			arpc.rtm.rtm_msglen = sizeof(arpc);
1182c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_version = RTM_VERSION;
1183c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_type = RTM_GET;
1184c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_flags = RTF_UP;
1185c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
11866e6b3f7cSQing Li 			arpc.sin.sin_family = AF_INET;
11876e6b3f7cSQing Li 			arpc.sin.sin_len = sizeof(arpc.sin);
11886e6b3f7cSQing Li 			arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
11896e6b3f7cSQing Li 
11906e6b3f7cSQing Li 			/* publish */
11919711a168SGleb Smirnoff 			if (lle->la_flags & LLE_PUB)
11926e6b3f7cSQing Li 				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
11936e6b3f7cSQing Li 
11946e6b3f7cSQing Li 			sdl = &arpc.sdl;
11956e6b3f7cSQing Li 			sdl->sdl_family = AF_LINK;
11966e6b3f7cSQing Li 			sdl->sdl_len = sizeof(*sdl);
11976e6b3f7cSQing Li 			sdl->sdl_index = ifp->if_index;
11986e6b3f7cSQing Li 			sdl->sdl_type = ifp->if_type;
119993704ac5SQing Li 			if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
120093704ac5SQing Li 				sdl->sdl_alen = ifp->if_addrlen;
12016e6b3f7cSQing Li 				bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
120293704ac5SQing Li 			} else {
120393704ac5SQing Li 				sdl->sdl_alen = 0;
120493704ac5SQing Li 				bzero(LLADDR(sdl), ifp->if_addrlen);
120593704ac5SQing Li 			}
12066e6b3f7cSQing Li 
12076e6b3f7cSQing Li 			arpc.rtm.rtm_rmx.rmx_expire =
12086e6b3f7cSQing Li 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
12098eca593cSQing Li 			arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
12106e6b3f7cSQing Li 			if (lle->la_flags & LLE_STATIC)
12116e6b3f7cSQing Li 				arpc.rtm.rtm_flags |= RTF_STATIC;
12126e6b3f7cSQing Li 			arpc.rtm.rtm_index = ifp->if_index;
12136e6b3f7cSQing Li 			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
12146e6b3f7cSQing Li 			if (error)
12156e6b3f7cSQing Li 				break;
12166e6b3f7cSQing Li 		}
12176e6b3f7cSQing Li 	}
12186e6b3f7cSQing Li 	return error;
12196e6b3f7cSQing Li #undef SIN
12206e6b3f7cSQing Li }
12216e6b3f7cSQing Li 
12226e6b3f7cSQing Li void *
12236e6b3f7cSQing Li in_domifattach(struct ifnet *ifp)
12246e6b3f7cSQing Li {
1225d10910e6SBruce M Simpson 	struct in_ifinfo *ii;
1226d10910e6SBruce M Simpson 	struct lltable *llt;
12276e6b3f7cSQing Li 
1228d10910e6SBruce M Simpson 	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1229d10910e6SBruce M Simpson 
1230d10910e6SBruce M Simpson 	llt = lltable_init(ifp, AF_INET);
12316e6b3f7cSQing Li 	if (llt != NULL) {
1232c9d763bfSQing Li 		llt->llt_prefix_free = in_lltable_prefix_free;
12336e6b3f7cSQing Li 		llt->llt_lookup = in_lltable_lookup;
12346e6b3f7cSQing Li 		llt->llt_dump = in_lltable_dump;
12356e6b3f7cSQing Li 	}
1236d10910e6SBruce M Simpson 	ii->ii_llt = llt;
1237d10910e6SBruce M Simpson 
1238d10910e6SBruce M Simpson 	ii->ii_igmp = igmp_domifattach(ifp);
1239d10910e6SBruce M Simpson 
1240d10910e6SBruce M Simpson 	return ii;
12416e6b3f7cSQing Li }
12426e6b3f7cSQing Li 
12436e6b3f7cSQing Li void
1244d10910e6SBruce M Simpson in_domifdetach(struct ifnet *ifp, void *aux)
12456e6b3f7cSQing Li {
1246d10910e6SBruce M Simpson 	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
12476e6b3f7cSQing Li 
1248d10910e6SBruce M Simpson 	igmp_domifdetach(ifp);
1249d10910e6SBruce M Simpson 	lltable_free(ii->ii_llt);
1250d10910e6SBruce M Simpson 	free(ii, M_IFADDR);
12516e6b3f7cSQing Li }
1252