xref: /freebsd/sys/netinet/in.c (revision ccbb9c359dc332ae5e8c32bcfd114abad96364c9)
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 
3650bb1704SGleb Smirnoff #include "opt_carp.h"
3750bb1704SGleb Smirnoff 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
3926f9a767SRodney W. Grimes #include <sys/systm.h>
4051a53488SBruce Evans #include <sys/sockio.h>
41df8bae1dSRodney W. Grimes #include <sys/malloc.h>
42acd3428bSRobert Watson #include <sys/priv.h>
43df8bae1dSRodney W. Grimes #include <sys/socket.h>
445ce0eb7fSBjoern A. Zeeb #include <sys/jail.h>
45f6d24a78SPoul-Henning Kamp #include <sys/kernel.h>
465ce0eb7fSBjoern A. Zeeb #include <sys/proc.h>
47f6d24a78SPoul-Henning Kamp #include <sys/sysctl.h>
48ebc90701SQing Li #include <sys/syslog.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <net/if.h>
51df813b7eSQing Li #include <net/if_var.h>
52ebc90701SQing Li #include <net/if_dl.h>
536e6b3f7cSQing Li #include <net/if_llatbl.h>
546a800098SYoshinobu Inoue #include <net/if_types.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56ebc90701SQing Li #include <net/vnet.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
60e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h>
6171498f30SBruce M Simpson #include <netinet/ip_var.h>
62d10910e6SBruce M Simpson #include <netinet/igmp_var.h>
63eddfbb76SRobert Watson #include <netinet/udp.h>
64eddfbb76SRobert Watson #include <netinet/udp_var.h>
6555166637SPoul-Henning Kamp 
664d77a549SAlfred Perlstein static int in_mask2len(struct in_addr *);
674d77a549SAlfred Perlstein static void in_len2mask(struct in_addr *, int);
684d77a549SAlfred Perlstein static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
694d77a549SAlfred Perlstein 	struct ifnet *, struct thread *);
706a800098SYoshinobu Inoue 
7148321abeSMax Laier static int	in_addprefix(struct in_ifaddr *, int);
7248321abeSMax Laier static int	in_scrubprefix(struct in_ifaddr *);
734d77a549SAlfred Perlstein static void	in_socktrim(struct sockaddr_in *);
744d77a549SAlfred Perlstein static int	in_ifinit(struct ifnet *,
754d77a549SAlfred Perlstein 	    struct in_ifaddr *, struct sockaddr_in *, int);
76ec002feeSBruce M Simpson static void	in_purgemaddrs(struct ifnet *);
77df8bae1dSRodney W. Grimes 
78eddfbb76SRobert Watson static VNET_DEFINE(int, subnetsarelocal);
79eddfbb76SRobert Watson static VNET_DEFINE(int, sameprefixcarponly);
80eddfbb76SRobert Watson VNET_DECLARE(struct inpcbinfo, ripcbinfo);
8144e33a07SMarko Zec 
821e77c105SRobert Watson #define	V_subnetsarelocal		VNET(subnetsarelocal)
831e77c105SRobert Watson #define	V_sameprefixcarponly		VNET(sameprefixcarponly)
841e77c105SRobert Watson #define	V_ripcbinfo			VNET(ripcbinfo)
85eddfbb76SRobert Watson 
86eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
87eddfbb76SRobert Watson 	&VNET_NAME(subnetsarelocal), 0,
888b615593SMarko Zec 	"Treat all subnets as directly connected");
89eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
90eddfbb76SRobert Watson 	&VNET_NAME(sameprefixcarponly), 0,
911ae95409SGleb Smirnoff 	"Refuse to create same prefixes on different interfaces");
92477180fbSGarrett Wollman 
93df8bae1dSRodney W. Grimes /*
94df8bae1dSRodney W. Grimes  * Return 1 if an internet address is for a ``local'' host
95df8bae1dSRodney W. Grimes  * (one to which we have a connection).  If subnetsarelocal
96df8bae1dSRodney W. Grimes  * is true, this includes other subnets of the local net.
97df8bae1dSRodney W. Grimes  * Otherwise, it includes only the directly-connected (sub)nets.
98df8bae1dSRodney W. Grimes  */
9926f9a767SRodney W. Grimes int
100f2565d68SRobert Watson in_localaddr(struct in_addr in)
101df8bae1dSRodney W. Grimes {
102df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
103df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
104df8bae1dSRodney W. Grimes 
1052d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
106603724d3SBjoern A. Zeeb 	if (V_subnetsarelocal) {
1072d9cfabaSRobert Watson 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1082d9cfabaSRobert Watson 			if ((i & ia->ia_netmask) == ia->ia_net) {
1092d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
110df8bae1dSRodney W. Grimes 				return (1);
111df8bae1dSRodney W. Grimes 			}
1122d9cfabaSRobert Watson 		}
1132d9cfabaSRobert Watson 	} else {
1142d9cfabaSRobert Watson 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1152d9cfabaSRobert Watson 			if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
1162d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
1172d9cfabaSRobert Watson 				return (1);
1182d9cfabaSRobert Watson 			}
1192d9cfabaSRobert Watson 		}
1202d9cfabaSRobert Watson 	}
1212d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
122df8bae1dSRodney W. Grimes 	return (0);
123df8bae1dSRodney W. Grimes }
124df8bae1dSRodney W. Grimes 
125df8bae1dSRodney W. Grimes /*
1262eccc90bSAndre Oppermann  * Return 1 if an internet address is for the local host and configured
1272eccc90bSAndre Oppermann  * on one of its interfaces.
1282eccc90bSAndre Oppermann  */
1292eccc90bSAndre Oppermann int
130f2565d68SRobert Watson in_localip(struct in_addr in)
1312eccc90bSAndre Oppermann {
1322eccc90bSAndre Oppermann 	struct in_ifaddr *ia;
1332eccc90bSAndre Oppermann 
1342d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
1352eccc90bSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
1362d9cfabaSRobert Watson 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
1372d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
138460473a0SBjoern A. Zeeb 			return (1);
1392eccc90bSAndre Oppermann 		}
1402d9cfabaSRobert Watson 	}
1412d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
142460473a0SBjoern A. Zeeb 	return (0);
1432eccc90bSAndre Oppermann }
1442eccc90bSAndre Oppermann 
1452eccc90bSAndre Oppermann /*
146df8bae1dSRodney W. Grimes  * Determine whether an IP address is in a reserved set of addresses
147df8bae1dSRodney W. Grimes  * that may not be forwarded, or whether datagrams to that destination
148df8bae1dSRodney W. Grimes  * may be forwarded.
149df8bae1dSRodney W. Grimes  */
15026f9a767SRodney W. Grimes int
151f2565d68SRobert Watson in_canforward(struct in_addr in)
152df8bae1dSRodney W. Grimes {
153df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
154df8bae1dSRodney W. Grimes 	register u_long net;
155df8bae1dSRodney W. Grimes 
156f8429ca2SBruce M Simpson 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
157df8bae1dSRodney W. Grimes 		return (0);
158df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i)) {
159df8bae1dSRodney W. Grimes 		net = i & IN_CLASSA_NET;
160df8bae1dSRodney W. Grimes 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
161df8bae1dSRodney W. Grimes 			return (0);
162df8bae1dSRodney W. Grimes 	}
163df8bae1dSRodney W. Grimes 	return (1);
164df8bae1dSRodney W. Grimes }
165df8bae1dSRodney W. Grimes 
166df8bae1dSRodney W. Grimes /*
167df8bae1dSRodney W. Grimes  * Trim a mask in a sockaddr
168df8bae1dSRodney W. Grimes  */
1690312fbe9SPoul-Henning Kamp static void
170f2565d68SRobert Watson in_socktrim(struct sockaddr_in *ap)
171df8bae1dSRodney W. Grimes {
172df8bae1dSRodney W. Grimes     register char *cplim = (char *) &ap->sin_addr;
173df8bae1dSRodney W. Grimes     register char *cp = (char *) (&ap->sin_addr + 1);
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes     ap->sin_len = 0;
176df00058dSGarrett Wollman     while (--cp >= cplim)
177df8bae1dSRodney W. Grimes 	if (*cp) {
178df8bae1dSRodney W. Grimes 	    (ap)->sin_len = cp - (char *) (ap) + 1;
179df8bae1dSRodney W. Grimes 	    break;
180df8bae1dSRodney W. Grimes 	}
181df8bae1dSRodney W. Grimes }
182df8bae1dSRodney W. Grimes 
1836a800098SYoshinobu Inoue static int
1846a800098SYoshinobu Inoue in_mask2len(mask)
1856a800098SYoshinobu Inoue 	struct in_addr *mask;
1866a800098SYoshinobu Inoue {
1876a800098SYoshinobu Inoue 	int x, y;
1886a800098SYoshinobu Inoue 	u_char *p;
1896a800098SYoshinobu Inoue 
1906a800098SYoshinobu Inoue 	p = (u_char *)mask;
1916a800098SYoshinobu Inoue 	for (x = 0; x < sizeof(*mask); x++) {
1926a800098SYoshinobu Inoue 		if (p[x] != 0xff)
1936a800098SYoshinobu Inoue 			break;
1946a800098SYoshinobu Inoue 	}
1956a800098SYoshinobu Inoue 	y = 0;
1966a800098SYoshinobu Inoue 	if (x < sizeof(*mask)) {
1976a800098SYoshinobu Inoue 		for (y = 0; y < 8; y++) {
1986a800098SYoshinobu Inoue 			if ((p[x] & (0x80 >> y)) == 0)
1996a800098SYoshinobu Inoue 				break;
2006a800098SYoshinobu Inoue 		}
2016a800098SYoshinobu Inoue 	}
202460473a0SBjoern A. Zeeb 	return (x * 8 + y);
2036a800098SYoshinobu Inoue }
2046a800098SYoshinobu Inoue 
2056a800098SYoshinobu Inoue static void
206f2565d68SRobert Watson in_len2mask(struct in_addr *mask, int len)
2076a800098SYoshinobu Inoue {
2086a800098SYoshinobu Inoue 	int i;
2096a800098SYoshinobu Inoue 	u_char *p;
2106a800098SYoshinobu Inoue 
2116a800098SYoshinobu Inoue 	p = (u_char *)mask;
2126a800098SYoshinobu Inoue 	bzero(mask, sizeof(*mask));
2136a800098SYoshinobu Inoue 	for (i = 0; i < len / 8; i++)
2146a800098SYoshinobu Inoue 		p[i] = 0xff;
2156a800098SYoshinobu Inoue 	if (len % 8)
2166a800098SYoshinobu Inoue 		p[i] = (0xff00 >> (len % 8)) & 0xff;
2176a800098SYoshinobu Inoue }
2186a800098SYoshinobu Inoue 
219df8bae1dSRodney W. Grimes /*
220df8bae1dSRodney W. Grimes  * Generic internet control operations (ioctl's).
221bbb3fb61SRobert Watson  *
222bbb3fb61SRobert Watson  * ifp is NULL if not an interface-specific ioctl.
223df8bae1dSRodney W. Grimes  */
224df8bae1dSRodney W. Grimes /* ARGSUSED */
22526f9a767SRodney W. Grimes int
226f2565d68SRobert Watson in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
227f2565d68SRobert Watson     struct thread *td)
228df8bae1dSRodney W. Grimes {
229df8bae1dSRodney W. Grimes 	register struct ifreq *ifr = (struct ifreq *)data;
230460473a0SBjoern A. Zeeb 	register struct in_ifaddr *ia, *iap;
231df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
232f7e083afSBruce M Simpson 	struct in_addr allhosts_addr;
233ca925d9cSJonathan Lemon 	struct in_addr dst;
234d10910e6SBruce M Simpson 	struct in_ifinfo *ii;
235df8bae1dSRodney W. Grimes 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
236df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
2372d9cfabaSRobert Watson 	int error, hostIsNew, iaIsNew, maskIsNew;
238f7e083afSBruce M Simpson 	int iaIsFirst;
2390f02fdacSBrian Somers 
240460473a0SBjoern A. Zeeb 	ia = NULL;
241f7e083afSBruce M Simpson 	iaIsFirst = 0;
2420f02fdacSBrian Somers 	iaIsNew = 0;
243f7e083afSBruce M Simpson 	allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
244df8bae1dSRodney W. Grimes 
245bbb3fb61SRobert Watson 	/*
246bbb3fb61SRobert Watson 	 * Filter out ioctls we implement directly; forward the rest on to
247bbb3fb61SRobert Watson 	 * in_lifaddr_ioctl() and ifp->if_ioctl().
248bbb3fb61SRobert Watson 	 */
2496a800098SYoshinobu Inoue 	switch (cmd) {
250bbb3fb61SRobert Watson 	case SIOCAIFADDR:
251bbb3fb61SRobert Watson 	case SIOCDIFADDR:
252bbb3fb61SRobert Watson 	case SIOCGIFADDR:
253bbb3fb61SRobert Watson 	case SIOCGIFBRDADDR:
254bbb3fb61SRobert Watson 	case SIOCGIFDSTADDR:
255bbb3fb61SRobert Watson 	case SIOCGIFNETMASK:
256bbb3fb61SRobert Watson 	case SIOCSIFADDR:
257bbb3fb61SRobert Watson 	case SIOCSIFBRDADDR:
258bbb3fb61SRobert Watson 	case SIOCSIFDSTADDR:
259bbb3fb61SRobert Watson 	case SIOCSIFNETMASK:
260bbb3fb61SRobert Watson 		break;
261bbb3fb61SRobert Watson 
2626a800098SYoshinobu Inoue 	case SIOCALIFADDR:
263acd3428bSRobert Watson 		if (td != NULL) {
264acd3428bSRobert Watson 			error = priv_check(td, PRIV_NET_ADDIFADDR);
265acd3428bSRobert Watson 			if (error)
266acd3428bSRobert Watson 				return (error);
267acd3428bSRobert Watson 		}
268460473a0SBjoern A. Zeeb 		if (ifp == NULL)
269460473a0SBjoern A. Zeeb 			return (EINVAL);
270acd3428bSRobert Watson 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
271acd3428bSRobert Watson 
2726a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
273acd3428bSRobert Watson 		if (td != NULL) {
274acd3428bSRobert Watson 			error = priv_check(td, PRIV_NET_DELIFADDR);
275acd3428bSRobert Watson 			if (error)
276acd3428bSRobert Watson 				return (error);
277acd3428bSRobert Watson 		}
278460473a0SBjoern A. Zeeb 		if (ifp == NULL)
279460473a0SBjoern A. Zeeb 			return (EINVAL);
280acd3428bSRobert Watson 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
281acd3428bSRobert Watson 
2826a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
283460473a0SBjoern A. Zeeb 		if (ifp == NULL)
284460473a0SBjoern A. Zeeb 			return (EINVAL);
285b40ce416SJulian Elischer 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
286bbb3fb61SRobert Watson 
287bbb3fb61SRobert Watson 	default:
288bbb3fb61SRobert Watson 		if (ifp == NULL || ifp->if_ioctl == NULL)
289bbb3fb61SRobert Watson 			return (EOPNOTSUPP);
290bbb3fb61SRobert Watson 		return ((*ifp->if_ioctl)(ifp, cmd, data));
2916a800098SYoshinobu Inoue 	}
2926a800098SYoshinobu Inoue 
293bbb3fb61SRobert Watson 	if (ifp == NULL)
294bbb3fb61SRobert Watson 		return (EADDRNOTAVAIL);
295bbb3fb61SRobert Watson 
296df8bae1dSRodney W. Grimes 	/*
297cf7b18f1SRobert Watson 	 * Security checks before we get involved in any work.
298cf7b18f1SRobert Watson 	 */
299cf7b18f1SRobert Watson 	switch (cmd) {
300cf7b18f1SRobert Watson 	case SIOCAIFADDR:
301cf7b18f1SRobert Watson 	case SIOCSIFADDR:
302cf7b18f1SRobert Watson 	case SIOCSIFBRDADDR:
303cf7b18f1SRobert Watson 	case SIOCSIFNETMASK:
304cf7b18f1SRobert Watson 	case SIOCSIFDSTADDR:
305cf7b18f1SRobert Watson 		if (td != NULL) {
306cf7b18f1SRobert Watson 			error = priv_check(td, PRIV_NET_ADDIFADDR);
307cf7b18f1SRobert Watson 			if (error)
308cf7b18f1SRobert Watson 				return (error);
309cf7b18f1SRobert Watson 		}
310cf7b18f1SRobert Watson 		break;
311cf7b18f1SRobert Watson 
312cf7b18f1SRobert Watson 	case SIOCDIFADDR:
313cf7b18f1SRobert Watson 		if (td != NULL) {
314cf7b18f1SRobert Watson 			error = priv_check(td, PRIV_NET_DELIFADDR);
315cf7b18f1SRobert Watson 			if (error)
316cf7b18f1SRobert Watson 				return (error);
317cf7b18f1SRobert Watson 		}
318cf7b18f1SRobert Watson 		break;
319cf7b18f1SRobert Watson 	}
320cf7b18f1SRobert Watson 
321cf7b18f1SRobert Watson 	/*
322df8bae1dSRodney W. Grimes 	 * Find address for this interface, if it exists.
323ac0aa473SBill Fenner 	 *
324bbb3fb61SRobert Watson 	 * If an alias address was specified, find that one instead of the
325bbb3fb61SRobert Watson 	 * first one on the interface, if possible.
326df8bae1dSRodney W. Grimes 	 */
327ca925d9cSJonathan Lemon 	dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
3282d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
329bbb3fb61SRobert Watson 	LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
330ca925d9cSJonathan Lemon 		if (iap->ia_ifp == ifp &&
331ca925d9cSJonathan Lemon 		    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
332bbb3fb61SRobert Watson 			if (td == NULL || prison_check_ip4(td->td_ucred,
333bbb3fb61SRobert Watson 			    &dst) == 0)
334ac0aa473SBill Fenner 				ia = iap;
335df8bae1dSRodney W. Grimes 			break;
336ca925d9cSJonathan Lemon 		}
337bbb3fb61SRobert Watson 	}
3388c0fec80SRobert Watson 	if (ia != NULL)
3398c0fec80SRobert Watson 		ifa_ref(&ia->ia_ifa);
3402d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
341bbb3fb61SRobert Watson 	if (ia == NULL) {
3428c0fec80SRobert Watson 		IF_ADDR_LOCK(ifp);
343ca925d9cSJonathan Lemon 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
344ca925d9cSJonathan Lemon 			iap = ifatoia(ifa);
345ca925d9cSJonathan Lemon 			if (iap->ia_addr.sin_family == AF_INET) {
3465ce0eb7fSBjoern A. Zeeb 				if (td != NULL &&
347b89e82ddSJamie Gritton 				    prison_check_ip4(td->td_ucred,
348b89e82ddSJamie Gritton 				    &iap->ia_addr.sin_addr) != 0)
3495ce0eb7fSBjoern A. Zeeb 					continue;
350ac0aa473SBill Fenner 				ia = iap;
351ac0aa473SBill Fenner 				break;
352ac0aa473SBill Fenner 			}
353ac0aa473SBill Fenner 		}
3548c0fec80SRobert Watson 		if (ia != NULL)
3558c0fec80SRobert Watson 			ifa_ref(&ia->ia_ifa);
3568c0fec80SRobert Watson 		IF_ADDR_UNLOCK(ifp);
357bbb3fb61SRobert Watson 	}
358f7e083afSBruce M Simpson 	if (ia == NULL)
359f7e083afSBruce M Simpson 		iaIsFirst = 1;
360df8bae1dSRodney W. Grimes 
361588885f2SRobert Watson 	error = 0;
362df8bae1dSRodney W. Grimes 	switch (cmd) {
363df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
364df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
3651067217dSGarrett Wollman 		if (ifra->ifra_addr.sin_family == AF_INET) {
3668c0fec80SRobert Watson 			struct in_ifaddr *oia;
3678c0fec80SRobert Watson 
3682d9cfabaSRobert Watson 			IN_IFADDR_RLOCK();
369fc2ffbe6SPoul-Henning Kamp 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
370df8bae1dSRodney W. Grimes 				if (ia->ia_ifp == ifp  &&
371df8bae1dSRodney W. Grimes 				    ia->ia_addr.sin_addr.s_addr ==
372df8bae1dSRodney W. Grimes 				    ifra->ifra_addr.sin_addr.s_addr)
373df8bae1dSRodney W. Grimes 					break;
374df8bae1dSRodney W. Grimes 			}
3758c0fec80SRobert Watson 			if (ia != NULL && ia != oia)
3768c0fec80SRobert Watson 				ifa_ref(&ia->ia_ifa);
3778c0fec80SRobert Watson 			if (oia != NULL && ia != oia)
3788c0fec80SRobert Watson 				ifa_free(&oia->ia_ifa);
3792d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
3801067217dSGarrett Wollman 			if ((ifp->if_flags & IFF_POINTOPOINT)
3811067217dSGarrett Wollman 			    && (cmd == SIOCAIFADDR)
3821067217dSGarrett Wollman 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
3831067217dSGarrett Wollman 				== INADDR_ANY)) {
384588885f2SRobert Watson 				error = EDESTADDRREQ;
3858c0fec80SRobert Watson 				goto out;
3861067217dSGarrett Wollman 			}
3871067217dSGarrett Wollman 		}
388588885f2SRobert Watson 		if (cmd == SIOCDIFADDR && ia == NULL) {
389588885f2SRobert Watson 			error = EADDRNOTAVAIL;
3908c0fec80SRobert Watson 			goto out;
391588885f2SRobert Watson 		}
392df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
393df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
394df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
395df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
396460473a0SBjoern A. Zeeb 		if (ia == NULL) {
39759562606SGarrett Wollman 			ia = (struct in_ifaddr *)
398588885f2SRobert Watson 				malloc(sizeof *ia, M_IFADDR, M_NOWAIT |
399588885f2SRobert Watson 				    M_ZERO);
400588885f2SRobert Watson 			if (ia == NULL) {
401588885f2SRobert Watson 				error = ENOBUFS;
4028c0fec80SRobert Watson 				goto out;
403588885f2SRobert Watson 			}
404588885f2SRobert Watson 
40519fc74fbSJeffrey Hsu 			ifa = &ia->ia_ifa;
4061099f828SRobert Watson 			ifa_init(ifa);
40759562606SGarrett Wollman 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
40859562606SGarrett Wollman 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
40959562606SGarrett Wollman 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
41019fc74fbSJeffrey Hsu 
411df8bae1dSRodney W. Grimes 			ia->ia_sockmask.sin_len = 8;
412bc183b3fSDag-Erling Smørgrav 			ia->ia_sockmask.sin_family = AF_INET;
413df8bae1dSRodney W. Grimes 			if (ifp->if_flags & IFF_BROADCAST) {
414df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
415df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_family = AF_INET;
416df8bae1dSRodney W. Grimes 			}
417df8bae1dSRodney W. Grimes 			ia->ia_ifp = ifp;
418f3d30eb2SGleb Smirnoff 
4198c0fec80SRobert Watson 			ifa_ref(ifa);			/* if_addrhead */
4208c0fec80SRobert Watson 			IF_ADDR_LOCK(ifp);
4218021456aSRobert Watson 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
4228c0fec80SRobert Watson 			IF_ADDR_UNLOCK(ifp);
4238c0fec80SRobert Watson 			ifa_ref(ifa);			/* in_ifaddrhead */
4242d9cfabaSRobert Watson 			IN_IFADDR_WLOCK();
425603724d3SBjoern A. Zeeb 			TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
4262d9cfabaSRobert Watson 			IN_IFADDR_WUNLOCK();
4270f02fdacSBrian Somers 			iaIsNew = 1;
428df8bae1dSRodney W. Grimes 		}
429df8bae1dSRodney W. Grimes 		break;
430df8bae1dSRodney W. Grimes 
431df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
432df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
433df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
434df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
435df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
436588885f2SRobert Watson 		if (ia == NULL) {
437588885f2SRobert Watson 			error = EADDRNOTAVAIL;
4388c0fec80SRobert Watson 			goto out;
439588885f2SRobert Watson 		}
440df8bae1dSRodney W. Grimes 		break;
441df8bae1dSRodney W. Grimes 	}
442df8bae1dSRodney W. Grimes 
443588885f2SRobert Watson 	/*
4448c0fec80SRobert Watson 	 * Most paths in this switch return directly or via out.  Only paths
4458c0fec80SRobert Watson 	 * that remove the address break in order to hit common removal code.
446588885f2SRobert Watson 	 */
447588885f2SRobert Watson 	switch (cmd) {
448df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
449df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
4508c0fec80SRobert Watson 		goto out;
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
453588885f2SRobert Watson 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
454588885f2SRobert Watson 			error = EINVAL;
4558c0fec80SRobert Watson 			goto out;
456588885f2SRobert Watson 		}
457df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
4588c0fec80SRobert Watson 		goto out;
459df8bae1dSRodney W. Grimes 
460df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
461588885f2SRobert Watson 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
462588885f2SRobert Watson 			error = EINVAL;
4638c0fec80SRobert Watson 			goto out;
464588885f2SRobert Watson 		}
465df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
4668c0fec80SRobert Watson 		goto out;
467df8bae1dSRodney W. Grimes 
468df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
469df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
4708c0fec80SRobert Watson 		goto out;
471df8bae1dSRodney W. Grimes 
472df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
473588885f2SRobert Watson 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
474588885f2SRobert Watson 			error = EINVAL;
4758c0fec80SRobert Watson 			goto out;
476588885f2SRobert Watson 		}
477df8bae1dSRodney W. Grimes 		oldaddr = ia->ia_dstaddr;
478df8bae1dSRodney W. Grimes 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
479460473a0SBjoern A. Zeeb 		if (ifp->if_ioctl != NULL) {
480ba5da2a0SIan Dowse 			error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
481ba5da2a0SIan Dowse 			    (caddr_t)ia);
482ba5da2a0SIan Dowse 			if (error) {
483df8bae1dSRodney W. Grimes 				ia->ia_dstaddr = oldaddr;
4848c0fec80SRobert Watson 				goto out;
485df8bae1dSRodney W. Grimes 			}
486ba5da2a0SIan Dowse 		}
487df8bae1dSRodney W. Grimes 		if (ia->ia_flags & IFA_ROUTE) {
488df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
489df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
490df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr =
491df8bae1dSRodney W. Grimes 					(struct sockaddr *)&ia->ia_dstaddr;
492df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
493df8bae1dSRodney W. Grimes 		}
4948c0fec80SRobert Watson 		goto out;
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
497588885f2SRobert Watson 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
498588885f2SRobert Watson 			error = EINVAL;
4998c0fec80SRobert Watson 			goto out;
500588885f2SRobert Watson 		}
501df8bae1dSRodney W. Grimes 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
5028c0fec80SRobert Watson 		goto out;
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
5050f02fdacSBrian Somers 		error = in_ifinit(ifp, ia,
5060f02fdacSBrian Somers 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
5070f02fdacSBrian Somers 		if (error != 0 && iaIsNew)
5080f02fdacSBrian Somers 			break;
509f7e083afSBruce M Simpson 		if (error == 0) {
510c75aa354SBruce M Simpson 			ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
511d10910e6SBruce M Simpson 			if (iaIsFirst &&
512d10910e6SBruce M Simpson 			    (ifp->if_flags & IFF_MULTICAST) != 0) {
513d10910e6SBruce M Simpson 				error = in_joingroup(ifp, &allhosts_addr,
514d10910e6SBruce M Simpson 				    NULL, &ii->ii_allhosts);
515d10910e6SBruce M Simpson 			}
51625a4adceSMax Laier 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
517f7e083afSBruce M Simpson 		}
5188c0fec80SRobert Watson 		error = 0;
5198c0fec80SRobert Watson 		goto out;
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
522bc183b3fSDag-Erling Smørgrav 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
523bc183b3fSDag-Erling Smørgrav 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
5248c0fec80SRobert Watson 		goto out;
525df8bae1dSRodney W. Grimes 
526df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
527df8bae1dSRodney W. Grimes 		maskIsNew = 0;
528df8bae1dSRodney W. Grimes 		hostIsNew = 1;
529df8bae1dSRodney W. Grimes 		error = 0;
530df8bae1dSRodney W. Grimes 		if (ia->ia_addr.sin_family == AF_INET) {
531df8bae1dSRodney W. Grimes 			if (ifra->ifra_addr.sin_len == 0) {
532df8bae1dSRodney W. Grimes 				ifra->ifra_addr = ia->ia_addr;
533df8bae1dSRodney W. Grimes 				hostIsNew = 0;
534df8bae1dSRodney W. Grimes 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
535df8bae1dSRodney W. Grimes 					       ia->ia_addr.sin_addr.s_addr)
536df8bae1dSRodney W. Grimes 				hostIsNew = 0;
537df8bae1dSRodney W. Grimes 		}
538df8bae1dSRodney W. Grimes 		if (ifra->ifra_mask.sin_len) {
53996ed1732SQing Li 			/*
54096ed1732SQing Li 			 * QL: XXX
54196ed1732SQing Li 			 * Need to scrub the prefix here in case
54296ed1732SQing Li 			 * the issued command is SIOCAIFADDR with
54396ed1732SQing Li 			 * the same address, but with a different
54496ed1732SQing Li 			 * prefix length. And if the prefix length
54596ed1732SQing Li 			 * is the same as before, then the call is
54696ed1732SQing Li 			 * un-necessarily executed here.
54796ed1732SQing Li 			 */
54896ed1732SQing Li 			in_ifscrub(ifp, ia);
549df8bae1dSRodney W. Grimes 			ia->ia_sockmask = ifra->ifra_mask;
550bc183b3fSDag-Erling Smørgrav 			ia->ia_sockmask.sin_family = AF_INET;
551df8bae1dSRodney W. Grimes 			ia->ia_subnetmask =
552df8bae1dSRodney W. Grimes 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
553df8bae1dSRodney W. Grimes 			maskIsNew = 1;
554df8bae1dSRodney W. Grimes 		}
555df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
556df8bae1dSRodney W. Grimes 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
55796ed1732SQing Li 			in_ifscrub(ifp, ia);
558df8bae1dSRodney W. Grimes 			ia->ia_dstaddr = ifra->ifra_dstaddr;
559df8bae1dSRodney W. Grimes 			maskIsNew  = 1; /* We lie; but the effect's the same */
560df8bae1dSRodney W. Grimes 		}
561df8bae1dSRodney W. Grimes 		if (ifra->ifra_addr.sin_family == AF_INET &&
562df8bae1dSRodney W. Grimes 		    (hostIsNew || maskIsNew))
563df8bae1dSRodney W. Grimes 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
5640f02fdacSBrian Somers 		if (error != 0 && iaIsNew)
5658c0fec80SRobert Watson 			goto out;
5660f02fdacSBrian Somers 
567df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) &&
568df8bae1dSRodney W. Grimes 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
569df8bae1dSRodney W. Grimes 			ia->ia_broadaddr = ifra->ifra_broadaddr;
570f7e083afSBruce M Simpson 		if (error == 0) {
571c75aa354SBruce M Simpson 			ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
572d10910e6SBruce M Simpson 			if (iaIsFirst &&
573d10910e6SBruce M Simpson 			    (ifp->if_flags & IFF_MULTICAST) != 0) {
574d10910e6SBruce M Simpson 				error = in_joingroup(ifp, &allhosts_addr,
575d10910e6SBruce M Simpson 				    NULL, &ii->ii_allhosts);
576d10910e6SBruce M Simpson 			}
57725a4adceSMax Laier 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
578f7e083afSBruce M Simpson 		}
5798c0fec80SRobert Watson 		goto out;
580df8bae1dSRodney W. Grimes 
581df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
582089cdfadSRuslan Ermilov 		/*
583089cdfadSRuslan Ermilov 		 * in_ifscrub kills the interface route.
584089cdfadSRuslan Ermilov 		 */
585df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
586588885f2SRobert Watson 
587c655b7c4SDavid Greenman 		/*
588089cdfadSRuslan Ermilov 		 * in_ifadown gets rid of all the rest of
589089cdfadSRuslan Ermilov 		 * the routes.  This is not quite the right
590089cdfadSRuslan Ermilov 		 * thing to do, but at least if we are running
591089cdfadSRuslan Ermilov 		 * a routing process they will come back.
592089cdfadSRuslan Ermilov 		 */
59391854268SRuslan Ermilov 		in_ifadown(&ia->ia_ifa, 1);
59425a4adceSMax Laier 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
5950f02fdacSBrian Somers 		error = 0;
596df8bae1dSRodney W. Grimes 		break;
597df8bae1dSRodney W. Grimes 
598df8bae1dSRodney W. Grimes 	default:
599bbb3fb61SRobert Watson 		panic("in_control: unsupported ioctl");
600df8bae1dSRodney W. Grimes 	}
6010f02fdacSBrian Somers 
6028021456aSRobert Watson 	IF_ADDR_LOCK(ifp);
6030f02fdacSBrian Somers 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
6048021456aSRobert Watson 	IF_ADDR_UNLOCK(ifp);
6058c0fec80SRobert Watson 	ifa_free(&ia->ia_ifa);				/* if_addrhead */
6062d9cfabaSRobert Watson 
6072d9cfabaSRobert Watson 	IN_IFADDR_WLOCK();
608603724d3SBjoern A. Zeeb 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
609f7e083afSBruce M Simpson 	if (ia->ia_addr.sin_family == AF_INET) {
6108c0fec80SRobert Watson 		struct in_ifaddr *if_ia;
6118c0fec80SRobert Watson 
6120f02fdacSBrian Somers 		LIST_REMOVE(ia, ia_hash);
6132d9cfabaSRobert Watson 		IN_IFADDR_WUNLOCK();
614f7e083afSBruce M Simpson 		/*
615f7e083afSBruce M Simpson 		 * If this is the last IPv4 address configured on this
616f7e083afSBruce M Simpson 		 * interface, leave the all-hosts group.
617d10910e6SBruce M Simpson 		 * No state-change report need be transmitted.
618f7e083afSBruce M Simpson 		 */
6198c0fec80SRobert Watson 		if_ia = NULL;
6208c0fec80SRobert Watson 		IFP_TO_IA(ifp, if_ia);
6218c0fec80SRobert Watson 		if (if_ia == NULL) {
622c75aa354SBruce M Simpson 			ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
623f7e083afSBruce M Simpson 			IN_MULTI_LOCK();
624d10910e6SBruce M Simpson 			if (ii->ii_allhosts) {
625d10910e6SBruce M Simpson 				(void)in_leavegroup_locked(ii->ii_allhosts,
626d10910e6SBruce M Simpson 				    NULL);
627d10910e6SBruce M Simpson 				ii->ii_allhosts = NULL;
628d10910e6SBruce M Simpson 			}
629f7e083afSBruce M Simpson 			IN_MULTI_UNLOCK();
6308c0fec80SRobert Watson 		} else
6318c0fec80SRobert Watson 			ifa_free(&if_ia->ia_ifa);
6322d9cfabaSRobert Watson 	} else
6332d9cfabaSRobert Watson 		IN_IFADDR_WUNLOCK();
6342d9cfabaSRobert Watson 	ifa_free(&ia->ia_ifa);				/* in_ifaddrhead */
6358c0fec80SRobert Watson out:
6368c0fec80SRobert Watson 	if (ia != NULL)
6378c0fec80SRobert Watson 		ifa_free(&ia->ia_ifa);
638588885f2SRobert Watson 	return (error);
639df8bae1dSRodney W. Grimes }
640df8bae1dSRodney W. Grimes 
641df8bae1dSRodney W. Grimes /*
6426a800098SYoshinobu Inoue  * SIOC[GAD]LIFADDR.
6436a800098SYoshinobu Inoue  *	SIOCGLIFADDR: get first address. (?!?)
6446a800098SYoshinobu Inoue  *	SIOCGLIFADDR with IFLR_PREFIX:
6456a800098SYoshinobu Inoue  *		get first address that matches the specified prefix.
6466a800098SYoshinobu Inoue  *	SIOCALIFADDR: add the specified address.
6476a800098SYoshinobu Inoue  *	SIOCALIFADDR with IFLR_PREFIX:
6486a800098SYoshinobu Inoue  *		EINVAL since we can't deduce hostid part of the address.
6496a800098SYoshinobu Inoue  *	SIOCDLIFADDR: delete the specified address.
6506a800098SYoshinobu Inoue  *	SIOCDLIFADDR with IFLR_PREFIX:
6516a800098SYoshinobu Inoue  *		delete the first address that matches the specified prefix.
6526a800098SYoshinobu Inoue  * return values:
6536a800098SYoshinobu Inoue  *	EINVAL on invalid parameters
6546a800098SYoshinobu Inoue  *	EADDRNOTAVAIL on prefix match failed/specified address not found
6556a800098SYoshinobu Inoue  *	other values may be returned from in_ioctl()
6566a800098SYoshinobu Inoue  */
6576a800098SYoshinobu Inoue static int
658f2565d68SRobert Watson in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
659f2565d68SRobert Watson     struct ifnet *ifp, struct thread *td)
6606a800098SYoshinobu Inoue {
6616a800098SYoshinobu Inoue 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
6626a800098SYoshinobu Inoue 	struct ifaddr *ifa;
6636a800098SYoshinobu Inoue 
6646a800098SYoshinobu Inoue 	/* sanity checks */
665460473a0SBjoern A. Zeeb 	if (data == NULL || ifp == NULL) {
6666a800098SYoshinobu Inoue 		panic("invalid argument to in_lifaddr_ioctl");
6676a800098SYoshinobu Inoue 		/*NOTRECHED*/
6686a800098SYoshinobu Inoue 	}
6696a800098SYoshinobu Inoue 
6706a800098SYoshinobu Inoue 	switch (cmd) {
6716a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
6726a800098SYoshinobu Inoue 		/* address must be specified on GET with IFLR_PREFIX */
6736a800098SYoshinobu Inoue 		if ((iflr->flags & IFLR_PREFIX) == 0)
6746a800098SYoshinobu Inoue 			break;
6756a800098SYoshinobu Inoue 		/*FALLTHROUGH*/
6766a800098SYoshinobu Inoue 	case SIOCALIFADDR:
6776a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
6786a800098SYoshinobu Inoue 		/* address must be specified on ADD and DELETE */
6795d60ed0eSYoshinobu Inoue 		if (iflr->addr.ss_family != AF_INET)
680460473a0SBjoern A. Zeeb 			return (EINVAL);
6815d60ed0eSYoshinobu Inoue 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
682460473a0SBjoern A. Zeeb 			return (EINVAL);
6836a800098SYoshinobu Inoue 		/* XXX need improvement */
6845d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family
6855d60ed0eSYoshinobu Inoue 		 && iflr->dstaddr.ss_family != AF_INET)
686460473a0SBjoern A. Zeeb 			return (EINVAL);
6875d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family
6885d60ed0eSYoshinobu Inoue 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
689460473a0SBjoern A. Zeeb 			return (EINVAL);
6906a800098SYoshinobu Inoue 		break;
6916a800098SYoshinobu Inoue 	default: /*shouldn't happen*/
692460473a0SBjoern A. Zeeb 		return (EOPNOTSUPP);
6936a800098SYoshinobu Inoue 	}
6946a800098SYoshinobu Inoue 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
695460473a0SBjoern A. Zeeb 		return (EINVAL);
6966a800098SYoshinobu Inoue 
6976a800098SYoshinobu Inoue 	switch (cmd) {
6986a800098SYoshinobu Inoue 	case SIOCALIFADDR:
6996a800098SYoshinobu Inoue 	    {
7006a800098SYoshinobu Inoue 		struct in_aliasreq ifra;
7016a800098SYoshinobu Inoue 
7026a800098SYoshinobu Inoue 		if (iflr->flags & IFLR_PREFIX)
703460473a0SBjoern A. Zeeb 			return (EINVAL);
7046a800098SYoshinobu Inoue 
7056a800098SYoshinobu Inoue 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
7066a800098SYoshinobu Inoue 		bzero(&ifra, sizeof(ifra));
7076a800098SYoshinobu Inoue 		bcopy(iflr->iflr_name, ifra.ifra_name,
7086a800098SYoshinobu Inoue 			sizeof(ifra.ifra_name));
7096a800098SYoshinobu Inoue 
7105d60ed0eSYoshinobu Inoue 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
7116a800098SYoshinobu Inoue 
7125d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family) {	/*XXX*/
7136a800098SYoshinobu Inoue 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
7145d60ed0eSYoshinobu Inoue 				iflr->dstaddr.ss_len);
7156a800098SYoshinobu Inoue 		}
7166a800098SYoshinobu Inoue 
7176a800098SYoshinobu Inoue 		ifra.ifra_mask.sin_family = AF_INET;
7186a800098SYoshinobu Inoue 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
7196a800098SYoshinobu Inoue 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
7206a800098SYoshinobu Inoue 
721460473a0SBjoern A. Zeeb 		return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
7226a800098SYoshinobu Inoue 	    }
7236a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
7246a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
7256a800098SYoshinobu Inoue 	    {
7266a800098SYoshinobu Inoue 		struct in_ifaddr *ia;
7276a800098SYoshinobu Inoue 		struct in_addr mask, candidate, match;
7286a800098SYoshinobu Inoue 		struct sockaddr_in *sin;
7296a800098SYoshinobu Inoue 
7306a800098SYoshinobu Inoue 		bzero(&mask, sizeof(mask));
731fbdd20a1SMatt Jacob 		bzero(&match, sizeof(match));
7326a800098SYoshinobu Inoue 		if (iflr->flags & IFLR_PREFIX) {
7336a800098SYoshinobu Inoue 			/* lookup a prefix rather than address. */
7346a800098SYoshinobu Inoue 			in_len2mask(&mask, iflr->prefixlen);
7356a800098SYoshinobu Inoue 
7366a800098SYoshinobu Inoue 			sin = (struct sockaddr_in *)&iflr->addr;
7376a800098SYoshinobu Inoue 			match.s_addr = sin->sin_addr.s_addr;
7386a800098SYoshinobu Inoue 			match.s_addr &= mask.s_addr;
7396a800098SYoshinobu Inoue 
7406a800098SYoshinobu Inoue 			/* if you set extra bits, that's wrong */
7416a800098SYoshinobu Inoue 			if (match.s_addr != sin->sin_addr.s_addr)
742460473a0SBjoern A. Zeeb 				return (EINVAL);
7436a800098SYoshinobu Inoue 
7446a800098SYoshinobu Inoue 		} else {
7456a800098SYoshinobu Inoue 			/* on getting an address, take the 1st match */
7466a800098SYoshinobu Inoue 			/* on deleting an address, do exact match */
747fbdd20a1SMatt Jacob 			if (cmd != SIOCGLIFADDR) {
7486a800098SYoshinobu Inoue 				in_len2mask(&mask, 32);
7496a800098SYoshinobu Inoue 				sin = (struct sockaddr_in *)&iflr->addr;
7506a800098SYoshinobu Inoue 				match.s_addr = sin->sin_addr.s_addr;
7516a800098SYoshinobu Inoue 			}
7526a800098SYoshinobu Inoue 		}
7536a800098SYoshinobu Inoue 
7546a800098SYoshinobu Inoue 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
7556a800098SYoshinobu Inoue 			if (ifa->ifa_addr->sa_family != AF_INET6)
7566a800098SYoshinobu Inoue 				continue;
757fbdd20a1SMatt Jacob 			if (match.s_addr == 0)
7586a800098SYoshinobu Inoue 				break;
7596a800098SYoshinobu Inoue 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
7606a800098SYoshinobu Inoue 			candidate.s_addr &= mask.s_addr;
7616a800098SYoshinobu Inoue 			if (candidate.s_addr == match.s_addr)
7626a800098SYoshinobu Inoue 				break;
7636a800098SYoshinobu Inoue 		}
764460473a0SBjoern A. Zeeb 		if (ifa == NULL)
765460473a0SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
7666a800098SYoshinobu Inoue 		ia = (struct in_ifaddr *)ifa;
7676a800098SYoshinobu Inoue 
7686a800098SYoshinobu Inoue 		if (cmd == SIOCGLIFADDR) {
7696a800098SYoshinobu Inoue 			/* fill in the if_laddrreq structure */
7706a800098SYoshinobu Inoue 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
7716a800098SYoshinobu Inoue 
7726a800098SYoshinobu Inoue 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
7736a800098SYoshinobu Inoue 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
7746a800098SYoshinobu Inoue 					ia->ia_dstaddr.sin_len);
7756a800098SYoshinobu Inoue 			} else
7766a800098SYoshinobu Inoue 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
7776a800098SYoshinobu Inoue 
7786a800098SYoshinobu Inoue 			iflr->prefixlen =
7796a800098SYoshinobu Inoue 				in_mask2len(&ia->ia_sockmask.sin_addr);
7806a800098SYoshinobu Inoue 
7816a800098SYoshinobu Inoue 			iflr->flags = 0;	/*XXX*/
7826a800098SYoshinobu Inoue 
783460473a0SBjoern A. Zeeb 			return (0);
7846a800098SYoshinobu Inoue 		} else {
7856a800098SYoshinobu Inoue 			struct in_aliasreq ifra;
7866a800098SYoshinobu Inoue 
7876a800098SYoshinobu Inoue 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
7886a800098SYoshinobu Inoue 			bzero(&ifra, sizeof(ifra));
7896a800098SYoshinobu Inoue 			bcopy(iflr->iflr_name, ifra.ifra_name,
7906a800098SYoshinobu Inoue 				sizeof(ifra.ifra_name));
7916a800098SYoshinobu Inoue 
7926a800098SYoshinobu Inoue 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
7936a800098SYoshinobu Inoue 				ia->ia_addr.sin_len);
7946a800098SYoshinobu Inoue 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
7956a800098SYoshinobu Inoue 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
7966a800098SYoshinobu Inoue 					ia->ia_dstaddr.sin_len);
7976a800098SYoshinobu Inoue 			}
7986a800098SYoshinobu Inoue 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
7996a800098SYoshinobu Inoue 				ia->ia_sockmask.sin_len);
8006a800098SYoshinobu Inoue 
801460473a0SBjoern A. Zeeb 			return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
802460473a0SBjoern A. Zeeb 			    ifp, td));
8036a800098SYoshinobu Inoue 		}
8046a800098SYoshinobu Inoue 	    }
8056a800098SYoshinobu Inoue 	}
8066a800098SYoshinobu Inoue 
807460473a0SBjoern A. Zeeb 	return (EOPNOTSUPP);	/*just for safety*/
8086a800098SYoshinobu Inoue }
8096a800098SYoshinobu Inoue 
8106a800098SYoshinobu Inoue /*
811df8bae1dSRodney W. Grimes  * Delete any existing route for an interface.
812df8bae1dSRodney W. Grimes  */
81339191c8eSGarrett Wollman void
814f2565d68SRobert Watson in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
815df8bae1dSRodney W. Grimes {
816f2565d68SRobert Watson 
81748321abeSMax Laier 	in_scrubprefix(ia);
818df8bae1dSRodney W. Grimes }
819df8bae1dSRodney W. Grimes 
820df8bae1dSRodney W. Grimes /*
821df8bae1dSRodney W. Grimes  * Initialize an interface's internet address
822df8bae1dSRodney W. Grimes  * and routing table entry.
823df8bae1dSRodney W. Grimes  */
8240312fbe9SPoul-Henning Kamp static int
825f2565d68SRobert Watson in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
826f2565d68SRobert Watson     int scrub)
827df8bae1dSRodney W. Grimes {
828df8bae1dSRodney W. Grimes 	register u_long i = ntohl(sin->sin_addr.s_addr);
829df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
8305a43847dSBrian Somers 	int s = splimp(), flags = RTF_UP, error = 0;
831df8bae1dSRodney W. Grimes 
832df8bae1dSRodney W. Grimes 	oldaddr = ia->ia_addr;
8332754d95dSSUZUKI Shinsuke 	if (oldaddr.sin_family == AF_INET)
8342754d95dSSUZUKI Shinsuke 		LIST_REMOVE(ia, ia_hash);
835df8bae1dSRodney W. Grimes 	ia->ia_addr = *sin;
8362d9cfabaSRobert Watson 	if (ia->ia_addr.sin_family == AF_INET) {
8372d9cfabaSRobert Watson 		IN_IFADDR_WLOCK();
8382754d95dSSUZUKI Shinsuke 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
8392754d95dSSUZUKI Shinsuke 		    ia, ia_hash);
8402d9cfabaSRobert Watson 		IN_IFADDR_WUNLOCK();
8412d9cfabaSRobert Watson 	}
842df8bae1dSRodney W. Grimes 	/*
843df8bae1dSRodney W. Grimes 	 * Give the interface a chance to initialize
844df8bae1dSRodney W. Grimes 	 * if this is its first address,
845df8bae1dSRodney W. Grimes 	 * and to validate the address if necessary.
846df8bae1dSRodney W. Grimes 	 */
847460473a0SBjoern A. Zeeb 	if (ifp->if_ioctl != NULL) {
848ba5da2a0SIan Dowse 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
849ba5da2a0SIan Dowse 		if (error) {
850df8bae1dSRodney W. Grimes 			splx(s);
8512754d95dSSUZUKI Shinsuke 			/* LIST_REMOVE(ia, ia_hash) is done in in_control */
852df8bae1dSRodney W. Grimes 			ia->ia_addr = oldaddr;
8532d9cfabaSRobert Watson 			IN_IFADDR_WLOCK();
85422c819a7SJonathan Lemon 			if (ia->ia_addr.sin_family == AF_INET)
855ba5da2a0SIan Dowse 				LIST_INSERT_HEAD(INADDR_HASH(
856ba5da2a0SIan Dowse 				    ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
857cf77b848SOleksandr Tymoshenko 			else
858cf77b848SOleksandr Tymoshenko 				/*
859cf77b848SOleksandr Tymoshenko 				 * If oldaddr family is not AF_INET (e.g.
860cf77b848SOleksandr Tymoshenko 				 * interface has been just created) in_control
861cf77b848SOleksandr Tymoshenko 				 * does not call LIST_REMOVE, and we end up
862cf77b848SOleksandr Tymoshenko 				 * with bogus ia entries in hash
863cf77b848SOleksandr Tymoshenko 				 */
864cf77b848SOleksandr Tymoshenko 				LIST_REMOVE(ia, ia_hash);
8652d9cfabaSRobert Watson 			IN_IFADDR_WUNLOCK();
8662754d95dSSUZUKI Shinsuke 			return (error);
8672754d95dSSUZUKI Shinsuke 		}
868ba5da2a0SIan Dowse 	}
869df8bae1dSRodney W. Grimes 	splx(s);
870df8bae1dSRodney W. Grimes 	if (scrub) {
871df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
872df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
873df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
874df8bae1dSRodney W. Grimes 	}
875df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i))
876df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSA_NET;
877df8bae1dSRodney W. Grimes 	else if (IN_CLASSB(i))
878df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSB_NET;
879df8bae1dSRodney W. Grimes 	else
880df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSC_NET;
881df8bae1dSRodney W. Grimes 	/*
882df8bae1dSRodney W. Grimes 	 * The subnet mask usually includes at least the standard network part,
883df8bae1dSRodney W. Grimes 	 * but may may be smaller in the case of supernetting.
884df8bae1dSRodney W. Grimes 	 * If it is set, we believe it.
885df8bae1dSRodney W. Grimes 	 */
886df8bae1dSRodney W. Grimes 	if (ia->ia_subnetmask == 0) {
887df8bae1dSRodney W. Grimes 		ia->ia_subnetmask = ia->ia_netmask;
888df8bae1dSRodney W. Grimes 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
889df8bae1dSRodney W. Grimes 	} else
890df8bae1dSRodney W. Grimes 		ia->ia_netmask &= ia->ia_subnetmask;
891df8bae1dSRodney W. Grimes 	ia->ia_net = i & ia->ia_netmask;
892df8bae1dSRodney W. Grimes 	ia->ia_subnet = i & ia->ia_subnetmask;
893df8bae1dSRodney W. Grimes 	in_socktrim(&ia->ia_sockmask);
89450bb1704SGleb Smirnoff #ifdef DEV_CARP
89550bb1704SGleb Smirnoff 	/*
89650bb1704SGleb Smirnoff 	 * XXX: carp(4) does not have interface route
89750bb1704SGleb Smirnoff 	 */
89850bb1704SGleb Smirnoff 	if (ifp->if_type == IFT_CARP)
89950bb1704SGleb Smirnoff 		return (0);
90050bb1704SGleb Smirnoff #endif
901df8bae1dSRodney W. Grimes 	/*
902df8bae1dSRodney W. Grimes 	 * Add route for the network.
903df8bae1dSRodney W. Grimes 	 */
904df8bae1dSRodney W. Grimes 	ia->ia_ifa.ifa_metric = ifp->if_metric;
905df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_BROADCAST) {
906df8bae1dSRodney W. Grimes 		ia->ia_broadaddr.sin_addr.s_addr =
907df8bae1dSRodney W. Grimes 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
908df8bae1dSRodney W. Grimes 		ia->ia_netbroadcast.s_addr =
909df8bae1dSRodney W. Grimes 			htonl(ia->ia_net | ~ ia->ia_netmask);
910df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_LOOPBACK) {
9119a6a6eebSMax Laier 		ia->ia_dstaddr = ia->ia_addr;
912df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
913df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
914df8bae1dSRodney W. Grimes 		if (ia->ia_dstaddr.sin_family != AF_INET)
915df8bae1dSRodney W. Grimes 			return (0);
916df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
917df8bae1dSRodney W. Grimes 	}
91848321abeSMax Laier 	if ((error = in_addprefix(ia, flags)) != 0)
9190f02fdacSBrian Somers 		return (error);
9200f02fdacSBrian Somers 
92192fac994SQing Li 	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
92292fac994SQing Li 		return (0);
92392fac994SQing Li 
924ebc90701SQing Li 	/*
925ebc90701SQing Li 	 * add a loopback route to self
926ebc90701SQing Li 	 */
927c7ab6602SQing Li 	if (V_useloopback && !(ifp->if_flags & IFF_LOOPBACK)) {
928c7ab6602SQing Li 		struct route ia_ro;
929c7ab6602SQing Li 
930c7ab6602SQing Li 		bzero(&ia_ro, sizeof(ia_ro));
931c7ab6602SQing Li 		*((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr;
932c7ab6602SQing Li 		rtalloc_ign_fib(&ia_ro, 0, 0);
933c7ab6602SQing Li 		if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
934c7ab6602SQing Li 		    (ia_ro.ro_rt->rt_ifp == V_loif)) {
935c7ab6602SQing Li 			RT_LOCK(ia_ro.ro_rt);
936c7ab6602SQing Li 			RT_ADDREF(ia_ro.ro_rt);
937c7ab6602SQing Li 			RTFREE_LOCKED(ia_ro.ro_rt);
938c7ab6602SQing Li 		} else
9399bb7d0f4SQing Li 			error = ifa_add_loopback_route((struct ifaddr *)ia,
9409bb7d0f4SQing Li 				       (struct sockaddr *)&ia->ia_addr);
941c7ab6602SQing Li 		if (error == 0)
942c7ab6602SQing Li 			ia->ia_flags |= IFA_RTSELF;
943c7ab6602SQing Li 		if (ia_ro.ro_rt != NULL)
944c7ab6602SQing Li 			RTFREE(ia_ro.ro_rt);
945c7ab6602SQing Li 	}
946ebc90701SQing Li 
947df8bae1dSRodney W. Grimes 	return (error);
948df8bae1dSRodney W. Grimes }
949df8bae1dSRodney W. Grimes 
95048321abeSMax Laier #define rtinitflags(x) \
95148321abeSMax Laier 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
95248321abeSMax Laier 	    ? RTF_HOST : 0)
953ccbb9c35SQing Li 
954ccbb9c35SQing Li /*
955ccbb9c35SQing Li  * Generate a routing message when inserting or deleting
956ccbb9c35SQing Li  * an interface address alias.
957ccbb9c35SQing Li  */
958ccbb9c35SQing Li static void in_addralias_rtmsg(int cmd, struct in_addr *prefix,
959ccbb9c35SQing Li     struct in_ifaddr *target)
960ccbb9c35SQing Li {
961ccbb9c35SQing Li 	struct route pfx_ro;
962ccbb9c35SQing Li 	struct sockaddr_in *pfx_addr;
963ccbb9c35SQing Li 	struct rtentry msg_rt;
964ccbb9c35SQing Li 
965ccbb9c35SQing Li 	/* QL: XXX
966ccbb9c35SQing Li 	 * This is a bit questionable because there is no
967ccbb9c35SQing Li 	 * additional route entry added/deleted for an address
968ccbb9c35SQing Li 	 * alias. Therefore this route report is inaccurate.
969ccbb9c35SQing Li 	 */
970ccbb9c35SQing Li 	bzero(&pfx_ro, sizeof(pfx_ro));
971ccbb9c35SQing Li 	pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst);
972ccbb9c35SQing Li 	pfx_addr->sin_len = sizeof(*pfx_addr);
973ccbb9c35SQing Li 	pfx_addr->sin_family = AF_INET;
974ccbb9c35SQing Li 	pfx_addr->sin_addr = *prefix;
975ccbb9c35SQing Li 	rtalloc_ign_fib(&pfx_ro, 0, 0);
976ccbb9c35SQing Li 	if (pfx_ro.ro_rt != NULL) {
977ccbb9c35SQing Li 		msg_rt = *pfx_ro.ro_rt;
978ccbb9c35SQing Li 
979ccbb9c35SQing Li 		/* QL: XXX
980ccbb9c35SQing Li 		 * Point the gateway to the new interface
981ccbb9c35SQing Li 		 * address as if a new prefix route entry has
982ccbb9c35SQing Li 		 * been added through the new address alias.
983ccbb9c35SQing Li 		 * All other parts of the rtentry is accurate,
984ccbb9c35SQing Li 		 * e.g., rt_key, rt_mask, rt_ifp etc.
985ccbb9c35SQing Li 		 */
986ccbb9c35SQing Li 		msg_rt.rt_gateway =
987ccbb9c35SQing Li 			(struct sockaddr *)&target->ia_addr;
988ccbb9c35SQing Li 		rt_newaddrmsg(cmd,
989ccbb9c35SQing Li 			      (struct ifaddr *)target,
990ccbb9c35SQing Li 			      0, &msg_rt);
991ccbb9c35SQing Li 		RTFREE(pfx_ro.ro_rt);
992ccbb9c35SQing Li 	}
993ccbb9c35SQing Li 	return;
994ccbb9c35SQing Li }
995ccbb9c35SQing Li 
99648321abeSMax Laier /*
997fbdd20a1SMatt Jacob  * Check if we have a route for the given prefix already or add one accordingly.
99848321abeSMax Laier  */
99948321abeSMax Laier static int
1000f2565d68SRobert Watson in_addprefix(struct in_ifaddr *target, int flags)
100148321abeSMax Laier {
100248321abeSMax Laier 	struct in_ifaddr *ia;
1003bfb26eecSGleb Smirnoff 	struct in_addr prefix, mask, p, m;
100448321abeSMax Laier 	int error;
100548321abeSMax Laier 
1006fbdd20a1SMatt Jacob 	if ((flags & RTF_HOST) != 0) {
100748321abeSMax Laier 		prefix = target->ia_dstaddr.sin_addr;
1008fbdd20a1SMatt Jacob 		mask.s_addr = 0;
1009fbdd20a1SMatt Jacob 	} else {
101048321abeSMax Laier 		prefix = target->ia_addr.sin_addr;
101148321abeSMax Laier 		mask = target->ia_sockmask.sin_addr;
101248321abeSMax Laier 		prefix.s_addr &= mask.s_addr;
101348321abeSMax Laier 	}
101448321abeSMax Laier 
10152d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
1016603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1017bfb26eecSGleb Smirnoff 		if (rtinitflags(ia)) {
101848321abeSMax Laier 			p = ia->ia_addr.sin_addr;
101948321abeSMax Laier 
102048321abeSMax Laier 			if (prefix.s_addr != p.s_addr)
102148321abeSMax Laier 				continue;
1022bfb26eecSGleb Smirnoff 		} else {
1023bfb26eecSGleb Smirnoff 			p = ia->ia_addr.sin_addr;
1024bfb26eecSGleb Smirnoff 			m = ia->ia_sockmask.sin_addr;
1025bfb26eecSGleb Smirnoff 			p.s_addr &= m.s_addr;
1026bfb26eecSGleb Smirnoff 
1027bfb26eecSGleb Smirnoff 			if (prefix.s_addr != p.s_addr ||
1028bfb26eecSGleb Smirnoff 			    mask.s_addr != m.s_addr)
1029bfb26eecSGleb Smirnoff 				continue;
1030bfb26eecSGleb Smirnoff 		}
103148321abeSMax Laier 
103248321abeSMax Laier 		/*
103348321abeSMax Laier 		 * If we got a matching prefix route inserted by other
103448321abeSMax Laier 		 * interface address, we are done here.
103548321abeSMax Laier 		 */
10361ae95409SGleb Smirnoff 		if (ia->ia_flags & IFA_ROUTE) {
1037603724d3SBjoern A. Zeeb 			if (V_sameprefixcarponly &&
10381ae95409SGleb Smirnoff 			    target->ia_ifp->if_type != IFT_CARP &&
10392d9cfabaSRobert Watson 			    ia->ia_ifp->if_type != IFT_CARP) {
10402d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
10411ae95409SGleb Smirnoff 				return (EEXIST);
10422d9cfabaSRobert Watson 			} else {
1043ccbb9c35SQing Li 				in_addralias_rtmsg(RTM_ADD, &prefix, target);
10442d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
10451ae95409SGleb Smirnoff 				return (0);
10461ae95409SGleb Smirnoff 			}
104748321abeSMax Laier 		}
10482d9cfabaSRobert Watson 	}
10492d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
105048321abeSMax Laier 
105148321abeSMax Laier 	/*
105248321abeSMax Laier 	 * No-one seem to have this prefix route, so we try to insert it.
105348321abeSMax Laier 	 */
105448321abeSMax Laier 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
105548321abeSMax Laier 	if (!error)
105648321abeSMax Laier 		target->ia_flags |= IFA_ROUTE;
1057460473a0SBjoern A. Zeeb 	return (error);
105848321abeSMax Laier }
105948321abeSMax Laier 
10606e6b3f7cSQing Li extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
10616e6b3f7cSQing Li 
106248321abeSMax Laier /*
106348321abeSMax Laier  * If there is no other address in the system that can serve a route to the
106448321abeSMax Laier  * same prefix, remove the route.  Hand over the route to the new address
106548321abeSMax Laier  * otherwise.
106648321abeSMax Laier  */
106748321abeSMax Laier static int
1068f2565d68SRobert Watson in_scrubprefix(struct in_ifaddr *target)
106948321abeSMax Laier {
107048321abeSMax Laier 	struct in_ifaddr *ia;
107148321abeSMax Laier 	struct in_addr prefix, mask, p;
1072c7ab6602SQing Li 	int error = 0;
1073c9d763bfSQing Li 	struct sockaddr_in prefix0, mask0;
107448321abeSMax Laier 
1075df813b7eSQing Li 	/*
1076df813b7eSQing Li 	 * Remove the loopback route to the interface address.
1077df813b7eSQing Li 	 * The "useloopback" setting is not consulted because if the
1078df813b7eSQing Li 	 * user configures an interface address, turns off this
1079df813b7eSQing Li 	 * setting, and then tries to delete that interface address,
1080df813b7eSQing Li 	 * checking the current setting of "useloopback" would leave
1081df813b7eSQing Li 	 * that interface address loopback route untouched, which
1082df813b7eSQing Li 	 * would be wrong. Therefore the interface address loopback route
1083df813b7eSQing Li 	 * deletion is unconditional.
1084df813b7eSQing Li 	 */
108592fac994SQing Li 	if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
1086c7ab6602SQing Li 	    !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
1087c7ab6602SQing Li 	    (target->ia_flags & IFA_RTSELF)) {
1088c7ab6602SQing Li 		struct route ia_ro;
1089c7ab6602SQing Li 		int freeit = 0;
1090c7ab6602SQing Li 
1091c7ab6602SQing Li 		bzero(&ia_ro, sizeof(ia_ro));
1092c7ab6602SQing Li 		*((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr;
1093c7ab6602SQing Li 		rtalloc_ign_fib(&ia_ro, 0, 0);
1094c7ab6602SQing Li 		if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
1095c7ab6602SQing Li 		    (ia_ro.ro_rt->rt_ifp == V_loif)) {
1096c7ab6602SQing Li 			RT_LOCK(ia_ro.ro_rt);
1097c7ab6602SQing Li 			if (ia_ro.ro_rt->rt_refcnt <= 1)
1098c7ab6602SQing Li 				freeit = 1;
1099c7ab6602SQing Li 			else
1100c7ab6602SQing Li 				RT_REMREF(ia_ro.ro_rt);
1101c7ab6602SQing Li 			RTFREE_LOCKED(ia_ro.ro_rt);
1102c7ab6602SQing Li 		}
1103c7ab6602SQing Li 		if (freeit)
11049bb7d0f4SQing Li 			error = ifa_del_loopback_route((struct ifaddr *)target,
11059bb7d0f4SQing Li 				       (struct sockaddr *)&target->ia_addr);
1106c7ab6602SQing Li 		if (error == 0)
1107c7ab6602SQing Li 			target->ia_flags &= ~IFA_RTSELF;
1108fa3cfd39SQing Li 		/* remove arp cache */
1109fa3cfd39SQing Li 		arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
1110ebc90701SQing Li 	}
1111ebc90701SQing Li 
111248321abeSMax Laier 	if (rtinitflags(target))
111348321abeSMax Laier 		prefix = target->ia_dstaddr.sin_addr;
111448321abeSMax Laier 	else {
111548321abeSMax Laier 		prefix = target->ia_addr.sin_addr;
111648321abeSMax Laier 		mask = target->ia_sockmask.sin_addr;
111748321abeSMax Laier 		prefix.s_addr &= mask.s_addr;
111848321abeSMax Laier 	}
111948321abeSMax Laier 
1120ccbb9c35SQing Li 	if ((target->ia_flags & IFA_ROUTE) == 0) {
1121ccbb9c35SQing Li 		in_addralias_rtmsg(RTM_DELETE, &prefix, target);
1122ccbb9c35SQing Li 		return (0);
1123ccbb9c35SQing Li 	}
1124ccbb9c35SQing Li 
11252d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
1126603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
112748321abeSMax Laier 		if (rtinitflags(ia))
112848321abeSMax Laier 			p = ia->ia_dstaddr.sin_addr;
112948321abeSMax Laier 		else {
113048321abeSMax Laier 			p = ia->ia_addr.sin_addr;
113148321abeSMax Laier 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
113248321abeSMax Laier 		}
113348321abeSMax Laier 
113448321abeSMax Laier 		if (prefix.s_addr != p.s_addr)
113548321abeSMax Laier 			continue;
113648321abeSMax Laier 
113748321abeSMax Laier 		/*
113848321abeSMax Laier 		 * If we got a matching prefix address, move IFA_ROUTE and
113948321abeSMax Laier 		 * the route itself to it.  Make sure that routing daemons
114048321abeSMax Laier 		 * get a heads-up.
114150bb1704SGleb Smirnoff 		 *
114250bb1704SGleb Smirnoff 		 * XXX: a special case for carp(4) interface
114348321abeSMax Laier 		 */
114450bb1704SGleb Smirnoff 		if ((ia->ia_flags & IFA_ROUTE) == 0
114550bb1704SGleb Smirnoff #ifdef DEV_CARP
114650bb1704SGleb Smirnoff 		    && (ia->ia_ifp->if_type != IFT_CARP)
114750bb1704SGleb Smirnoff #endif
114850bb1704SGleb Smirnoff 							) {
11492d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
115048321abeSMax Laier 			rtinit(&(target->ia_ifa), (int)RTM_DELETE,
115148321abeSMax Laier 			    rtinitflags(target));
115248321abeSMax Laier 			target->ia_flags &= ~IFA_ROUTE;
115348321abeSMax Laier 
115448321abeSMax Laier 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
115548321abeSMax Laier 			    rtinitflags(ia) | RTF_UP);
115648321abeSMax Laier 			if (error == 0)
115748321abeSMax Laier 				ia->ia_flags |= IFA_ROUTE;
1158460473a0SBjoern A. Zeeb 			return (error);
115948321abeSMax Laier 		}
116048321abeSMax Laier 	}
11612d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
116248321abeSMax Laier 
116348321abeSMax Laier 	/*
1164c9d763bfSQing Li 	 * remove all L2 entries on the given prefix
1165c9d763bfSQing Li 	 */
1166c9d763bfSQing Li 	bzero(&prefix0, sizeof(prefix0));
1167c9d763bfSQing Li 	prefix0.sin_len = sizeof(prefix0);
1168c9d763bfSQing Li 	prefix0.sin_family = AF_INET;
1169c9d763bfSQing Li 	prefix0.sin_addr.s_addr = target->ia_subnet;
1170c9d763bfSQing Li 	bzero(&mask0, sizeof(mask0));
1171c9d763bfSQing Li 	mask0.sin_len = sizeof(mask0);
1172c9d763bfSQing Li 	mask0.sin_family = AF_INET;
1173c9d763bfSQing Li 	mask0.sin_addr.s_addr = target->ia_subnetmask;
1174c9d763bfSQing Li 	lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
1175c9d763bfSQing Li 			    (struct sockaddr *)&mask0);
1176c9d763bfSQing Li 
1177c9d763bfSQing Li 	/*
117848321abeSMax Laier 	 * As no-one seem to have this prefix, we can remove the route.
117948321abeSMax Laier 	 */
118048321abeSMax Laier 	rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
118148321abeSMax Laier 	target->ia_flags &= ~IFA_ROUTE;
1182460473a0SBjoern A. Zeeb 	return (0);
118348321abeSMax Laier }
118448321abeSMax Laier 
118548321abeSMax Laier #undef rtinitflags
1186df8bae1dSRodney W. Grimes 
1187df8bae1dSRodney W. Grimes /*
1188df8bae1dSRodney W. Grimes  * Return 1 if the address might be a local broadcast address.
1189df8bae1dSRodney W. Grimes  */
119026f9a767SRodney W. Grimes int
1191f2565d68SRobert Watson in_broadcast(struct in_addr in, struct ifnet *ifp)
1192df8bae1dSRodney W. Grimes {
1193df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
1194df8bae1dSRodney W. Grimes 	u_long t;
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes 	if (in.s_addr == INADDR_BROADCAST ||
1197df8bae1dSRodney W. Grimes 	    in.s_addr == INADDR_ANY)
1198460473a0SBjoern A. Zeeb 		return (1);
1199df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1200460473a0SBjoern A. Zeeb 		return (0);
1201df8bae1dSRodney W. Grimes 	t = ntohl(in.s_addr);
1202df8bae1dSRodney W. Grimes 	/*
1203df8bae1dSRodney W. Grimes 	 * Look through the list of addresses for a match
1204df8bae1dSRodney W. Grimes 	 * with a broadcast address.
1205df8bae1dSRodney W. Grimes 	 */
1206df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa)
1207462b86feSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1208df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == AF_INET &&
1209df8bae1dSRodney W. Grimes 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1210df8bae1dSRodney W. Grimes 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
1211df8bae1dSRodney W. Grimes 		     /*
1212df8bae1dSRodney W. Grimes 		      * Check for old-style (host 0) broadcast.
1213df8bae1dSRodney W. Grimes 		      */
12148dd27fd6SGuido van Rooij 		     t == ia->ia_subnet || t == ia->ia_net) &&
12158dd27fd6SGuido van Rooij 		     /*
12168dd27fd6SGuido van Rooij 		      * Check for an all one subnetmask. These
12178dd27fd6SGuido van Rooij 		      * only exist when an interface gets a secondary
12188dd27fd6SGuido van Rooij 		      * address.
12198dd27fd6SGuido van Rooij 		      */
12208dd27fd6SGuido van Rooij 		     ia->ia_subnetmask != (u_long)0xffffffff)
1221460473a0SBjoern A. Zeeb 			    return (1);
1222df8bae1dSRodney W. Grimes 	return (0);
1223df8bae1dSRodney W. Grimes #undef ia
1224df8bae1dSRodney W. Grimes }
1225ec002feeSBruce M Simpson 
1226df8bae1dSRodney W. Grimes /*
1227b1c53bc9SRobert Watson  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1228b1c53bc9SRobert Watson  */
1229b1c53bc9SRobert Watson void
1230f2565d68SRobert Watson in_ifdetach(struct ifnet *ifp)
1231b1c53bc9SRobert Watson {
1232b1c53bc9SRobert Watson 
1233603724d3SBjoern A. Zeeb 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
1234603724d3SBjoern A. Zeeb 	in_pcbpurgeif0(&V_udbinfo, ifp);
1235ec002feeSBruce M Simpson 	in_purgemaddrs(ifp);
1236b1c53bc9SRobert Watson }
12376e6b3f7cSQing Li 
1238d10910e6SBruce M Simpson /*
1239d10910e6SBruce M Simpson  * Delete all IPv4 multicast address records, and associated link-layer
1240d10910e6SBruce M Simpson  * multicast address records, associated with ifp.
1241d10910e6SBruce M Simpson  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
124256663a40SBruce M Simpson  * XXX This should not race with ifma_protospec being set during
124356663a40SBruce M Simpson  * a new allocation, if it does, we have bigger problems.
1244d10910e6SBruce M Simpson  */
1245d10910e6SBruce M Simpson static void
1246d10910e6SBruce M Simpson in_purgemaddrs(struct ifnet *ifp)
1247d10910e6SBruce M Simpson {
1248d10910e6SBruce M Simpson 	LIST_HEAD(,in_multi) purgeinms;
1249d10910e6SBruce M Simpson 	struct in_multi		*inm, *tinm;
1250d10910e6SBruce M Simpson 	struct ifmultiaddr	*ifma;
1251d10910e6SBruce M Simpson 
1252d10910e6SBruce M Simpson 	LIST_INIT(&purgeinms);
1253d10910e6SBruce M Simpson 	IN_MULTI_LOCK();
1254d10910e6SBruce M Simpson 
1255d10910e6SBruce M Simpson 	/*
1256d10910e6SBruce M Simpson 	 * Extract list of in_multi associated with the detaching ifp
1257d10910e6SBruce M Simpson 	 * which the PF_INET layer is about to release.
1258d10910e6SBruce M Simpson 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1259d10910e6SBruce M Simpson 	 * by code further down.
1260d10910e6SBruce M Simpson 	 */
1261d10910e6SBruce M Simpson 	IF_ADDR_LOCK(ifp);
1262d10910e6SBruce M Simpson 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
126356663a40SBruce M Simpson 		if (ifma->ifma_addr->sa_family != AF_INET ||
126456663a40SBruce M Simpson 		    ifma->ifma_protospec == NULL)
1265d10910e6SBruce M Simpson 			continue;
126656663a40SBruce M Simpson #if 0
126756663a40SBruce M Simpson 		KASSERT(ifma->ifma_protospec != NULL,
126856663a40SBruce M Simpson 		    ("%s: ifma_protospec is NULL", __func__));
126956663a40SBruce M Simpson #endif
1270d10910e6SBruce M Simpson 		inm = (struct in_multi *)ifma->ifma_protospec;
1271d10910e6SBruce M Simpson 		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1272d10910e6SBruce M Simpson 	}
1273d10910e6SBruce M Simpson 	IF_ADDR_UNLOCK(ifp);
1274d10910e6SBruce M Simpson 
1275d10910e6SBruce M Simpson 	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1276d10910e6SBruce M Simpson 		LIST_REMOVE(inm, inm_link);
127707cde5e9SRobert Watson 		inm_release_locked(inm);
1278d10910e6SBruce M Simpson 	}
1279d10910e6SBruce M Simpson 	igmp_ifdetach(ifp);
1280d10910e6SBruce M Simpson 
1281d10910e6SBruce M Simpson 	IN_MULTI_UNLOCK();
1282d10910e6SBruce M Simpson }
1283d10910e6SBruce M Simpson 
12846e6b3f7cSQing Li #include <net/if_dl.h>
12856e6b3f7cSQing Li #include <netinet/if_ether.h>
12866e6b3f7cSQing Li 
12876e6b3f7cSQing Li struct in_llentry {
12886e6b3f7cSQing Li 	struct llentry		base;
12896e6b3f7cSQing Li 	struct sockaddr_in	l3_addr4;
12906e6b3f7cSQing Li };
12916e6b3f7cSQing Li 
12926e6b3f7cSQing Li static struct llentry *
12936e6b3f7cSQing Li in_lltable_new(const struct sockaddr *l3addr, u_int flags)
12946e6b3f7cSQing Li {
12956e6b3f7cSQing Li 	struct in_llentry *lle;
12966e6b3f7cSQing Li 
12976e6b3f7cSQing Li 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
12986e6b3f7cSQing Li 	if (lle == NULL)		/* NB: caller generates msg */
12996e6b3f7cSQing Li 		return NULL;
13006e6b3f7cSQing Li 
13016e6b3f7cSQing Li 	callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
13026e6b3f7cSQing Li 	/*
13036e6b3f7cSQing Li 	 * For IPv4 this will trigger "arpresolve" to generate
13046e6b3f7cSQing Li 	 * an ARP request.
13056e6b3f7cSQing Li 	 */
13066e6b3f7cSQing Li 	lle->base.la_expire = time_second; /* mark expired */
13076e6b3f7cSQing Li 	lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
13086e6b3f7cSQing Li 	lle->base.lle_refcnt = 1;
13096e6b3f7cSQing Li 	LLE_LOCK_INIT(&lle->base);
13106e6b3f7cSQing Li 	return &lle->base;
13116e6b3f7cSQing Li }
13126e6b3f7cSQing Li 
13136e6b3f7cSQing Li /*
13146e6b3f7cSQing Li  * Deletes an address from the address table.
13156e6b3f7cSQing Li  * This function is called by the timer functions
13166e6b3f7cSQing Li  * such as arptimer() and nd6_llinfo_timer(), and
13176e6b3f7cSQing Li  * the caller does the locking.
13186e6b3f7cSQing Li  */
13196e6b3f7cSQing Li static void
13206e6b3f7cSQing Li in_lltable_free(struct lltable *llt, struct llentry *lle)
13216e6b3f7cSQing Li {
1322fbc2ca1bSKip Macy 	LLE_WUNLOCK(lle);
1323fbc2ca1bSKip Macy 	LLE_LOCK_DESTROY(lle);
13246e6b3f7cSQing Li 	free(lle, M_LLTABLE);
13256e6b3f7cSQing Li }
13266e6b3f7cSQing Li 
1327c9d763bfSQing Li 
1328c9d763bfSQing Li #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(			\
1329c9d763bfSQing Li 	    (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1330c9d763bfSQing Li 
1331c9d763bfSQing Li static void
1332c9d763bfSQing Li in_lltable_prefix_free(struct lltable *llt,
1333c9d763bfSQing Li 		       const struct sockaddr *prefix,
1334c9d763bfSQing Li 		       const struct sockaddr *mask)
1335c9d763bfSQing Li {
1336c9d763bfSQing Li 	const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1337c9d763bfSQing Li 	const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1338c9d763bfSQing Li 	struct llentry *lle, *next;
1339c9d763bfSQing Li 	register int i;
1340c9d763bfSQing Li 
1341c9d763bfSQing Li 	for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
1342c9d763bfSQing Li 		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1343c9d763bfSQing Li 
1344c9d763bfSQing Li 			if (IN_ARE_MASKED_ADDR_EQUAL((struct sockaddr_in *)L3_ADDR(lle),
1345c9d763bfSQing Li 						     pfx, msk)) {
1346c9d763bfSQing Li 				callout_drain(&lle->la_timer);
1347c9d763bfSQing Li 				LLE_WLOCK(lle);
1348c9d763bfSQing Li 				llentry_free(lle);
1349c9d763bfSQing Li 			}
1350c9d763bfSQing Li 		}
1351c9d763bfSQing Li 	}
1352c9d763bfSQing Li }
1353c9d763bfSQing Li 
1354c9d763bfSQing Li 
13556e6b3f7cSQing Li static int
1356c7ab6602SQing Li in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
13576e6b3f7cSQing Li {
13586e6b3f7cSQing Li 	struct rtentry *rt;
13596e6b3f7cSQing Li 
13606e6b3f7cSQing Li 	KASSERT(l3addr->sa_family == AF_INET,
13616e6b3f7cSQing Li 	    ("sin_family %d", l3addr->sa_family));
13626e6b3f7cSQing Li 
13636e6b3f7cSQing Li 	/* XXX rtalloc1 should take a const param */
13646e6b3f7cSQing Li 	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1365c7ab6602SQing Li 	if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) ||
1366c7ab6602SQing Li 	    ((rt->rt_ifp != ifp) && !(flags & LLE_PUB))) {
13676cb2b4e7SQing Li #ifdef DIAGNOSTIC
13686e6b3f7cSQing Li 		log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
13696e6b3f7cSQing Li 		    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1370b4a22c36SQing Li #endif
13716e6b3f7cSQing Li 		if (rt != NULL)
13726e6b3f7cSQing Li 			RTFREE_LOCKED(rt);
13736e6b3f7cSQing Li 		return (EINVAL);
13746e6b3f7cSQing Li 	}
13756e6b3f7cSQing Li 	RTFREE_LOCKED(rt);
13766e6b3f7cSQing Li 	return 0;
13776e6b3f7cSQing Li }
13786e6b3f7cSQing Li 
13796e6b3f7cSQing Li /*
13806e6b3f7cSQing Li  * Return NULL if not found or marked for deletion.
13816e6b3f7cSQing Li  * If found return lle read locked.
13826e6b3f7cSQing Li  */
13836e6b3f7cSQing Li static struct llentry *
13846e6b3f7cSQing Li in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
13856e6b3f7cSQing Li {
13866e6b3f7cSQing Li 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
13876e6b3f7cSQing Li 	struct ifnet *ifp = llt->llt_ifp;
13886e6b3f7cSQing Li 	struct llentry *lle;
13896e6b3f7cSQing Li 	struct llentries *lleh;
13906e6b3f7cSQing Li 	u_int hashkey;
13916e6b3f7cSQing Li 
13926e6b3f7cSQing Li 	IF_AFDATA_LOCK_ASSERT(ifp);
13936e6b3f7cSQing Li 	KASSERT(l3addr->sa_family == AF_INET,
13946e6b3f7cSQing Li 	    ("sin_family %d", l3addr->sa_family));
13956e6b3f7cSQing Li 
13966e6b3f7cSQing Li 	hashkey = sin->sin_addr.s_addr;
13976e6b3f7cSQing Li 	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
13986e6b3f7cSQing Li 	LIST_FOREACH(lle, lleh, lle_next) {
1399dc495497SQing Li 		struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle);
14006e6b3f7cSQing Li 		if (lle->la_flags & LLE_DELETED)
14016e6b3f7cSQing Li 			continue;
1402dc495497SQing Li 		if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
14036e6b3f7cSQing Li 			break;
14046e6b3f7cSQing Li 	}
14056e6b3f7cSQing Li 	if (lle == NULL) {
14066cb2b4e7SQing Li #ifdef DIAGNOSTIC
14076e6b3f7cSQing Li 		if (flags & LLE_DELETE)
14086e6b3f7cSQing Li 			log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
14096e6b3f7cSQing Li #endif
14106e6b3f7cSQing Li 		if (!(flags & LLE_CREATE))
14116e6b3f7cSQing Li 			return (NULL);
14126e6b3f7cSQing Li 		/*
14136e6b3f7cSQing Li 		 * A route that covers the given address must have
14146e6b3f7cSQing Li 		 * been installed 1st because we are doing a resolution,
14156e6b3f7cSQing Li 		 * verify this.
14166e6b3f7cSQing Li 		 */
14176e6b3f7cSQing Li 		if (!(flags & LLE_IFADDR) &&
1418c7ab6602SQing Li 		    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
14196e6b3f7cSQing Li 			goto done;
14206e6b3f7cSQing Li 
14216e6b3f7cSQing Li 		lle = in_lltable_new(l3addr, flags);
14226e6b3f7cSQing Li 		if (lle == NULL) {
14236e6b3f7cSQing Li 			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
14246e6b3f7cSQing Li 			goto done;
14256e6b3f7cSQing Li 		}
14266e6b3f7cSQing Li 		lle->la_flags = flags & ~LLE_CREATE;
14276e6b3f7cSQing Li 		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
14286e6b3f7cSQing Li 			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
14296e6b3f7cSQing Li 			lle->la_flags |= (LLE_VALID | LLE_STATIC);
14306e6b3f7cSQing Li 		}
14316e6b3f7cSQing Li 
14326e6b3f7cSQing Li 		lle->lle_tbl  = llt;
14336e6b3f7cSQing Li 		lle->lle_head = lleh;
14346e6b3f7cSQing Li 		LIST_INSERT_HEAD(lleh, lle, lle_next);
14356e6b3f7cSQing Li 	} else if (flags & LLE_DELETE) {
14366e6b3f7cSQing Li 		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
14376e6b3f7cSQing Li 			LLE_WLOCK(lle);
14386e6b3f7cSQing Li 			lle->la_flags = LLE_DELETED;
14399a311445SNavdeep Parhar 			EVENTHANDLER_INVOKE(arp_update_event, lle);
14406e6b3f7cSQing Li 			LLE_WUNLOCK(lle);
14416cb2b4e7SQing Li #ifdef DIAGNOSTIC
14426e6b3f7cSQing Li 			log(LOG_INFO, "ifaddr cache = %p  is deleted\n", lle);
14436e6b3f7cSQing Li #endif
14446e6b3f7cSQing Li 		}
14456e6b3f7cSQing Li 		lle = (void *)-1;
14466e6b3f7cSQing Li 
14476e6b3f7cSQing Li 	}
144842d866ddSBjoern A. Zeeb 	if (LLE_IS_VALID(lle)) {
14496e6b3f7cSQing Li 		if (flags & LLE_EXCLUSIVE)
14506e6b3f7cSQing Li 			LLE_WLOCK(lle);
14516e6b3f7cSQing Li 		else
14526e6b3f7cSQing Li 			LLE_RLOCK(lle);
14536e6b3f7cSQing Li 	}
14546e6b3f7cSQing Li done:
14556e6b3f7cSQing Li 	return (lle);
14566e6b3f7cSQing Li }
14576e6b3f7cSQing Li 
14586e6b3f7cSQing Li static int
14596e6b3f7cSQing Li in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
14606e6b3f7cSQing Li {
14616e6b3f7cSQing Li #define	SIN(lle)	((struct sockaddr_in *) L3_ADDR(lle))
14626e6b3f7cSQing Li 	struct ifnet *ifp = llt->llt_ifp;
14636e6b3f7cSQing Li 	struct llentry *lle;
14646e6b3f7cSQing Li 	/* XXX stack use */
14656e6b3f7cSQing Li 	struct {
14666e6b3f7cSQing Li 		struct rt_msghdr	rtm;
14676e6b3f7cSQing Li 		struct sockaddr_inarp	sin;
14686e6b3f7cSQing Li 		struct sockaddr_dl	sdl;
14696e6b3f7cSQing Li 	} arpc;
14706e6b3f7cSQing Li 	int error, i;
14716e6b3f7cSQing Li 
1472dc56e98fSRobert Watson 	LLTABLE_LOCK_ASSERT();
14736e6b3f7cSQing Li 
14746e6b3f7cSQing Li 	error = 0;
14756e6b3f7cSQing Li 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
14766e6b3f7cSQing Li 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
14776e6b3f7cSQing Li 			struct sockaddr_dl *sdl;
14786e6b3f7cSQing Li 
14796e6b3f7cSQing Li 			/* skip deleted entries */
148093704ac5SQing Li 			if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
14816e6b3f7cSQing Li 				continue;
1482813dd6aeSBjoern A. Zeeb 			/* Skip if jailed and not a valid IP of the prison. */
1483b89e82ddSJamie Gritton 			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1484813dd6aeSBjoern A. Zeeb 				continue;
14856e6b3f7cSQing Li 			/*
14866e6b3f7cSQing Li 			 * produce a msg made of:
14876e6b3f7cSQing Li 			 *  struct rt_msghdr;
14886e6b3f7cSQing Li 			 *  struct sockaddr_inarp; (IPv4)
14896e6b3f7cSQing Li 			 *  struct sockaddr_dl;
14906e6b3f7cSQing Li 			 */
14916e6b3f7cSQing Li 			bzero(&arpc, sizeof(arpc));
14926e6b3f7cSQing Li 			arpc.rtm.rtm_msglen = sizeof(arpc);
1493c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_version = RTM_VERSION;
1494c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_type = RTM_GET;
1495c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_flags = RTF_UP;
1496c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
14976e6b3f7cSQing Li 			arpc.sin.sin_family = AF_INET;
14986e6b3f7cSQing Li 			arpc.sin.sin_len = sizeof(arpc.sin);
14996e6b3f7cSQing Li 			arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
15006e6b3f7cSQing Li 
15016e6b3f7cSQing Li 			/* publish */
15026e6b3f7cSQing Li 			if (lle->la_flags & LLE_PUB) {
15036e6b3f7cSQing Li 				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
15046e6b3f7cSQing Li 				/* proxy only */
15056e6b3f7cSQing Li 				if (lle->la_flags & LLE_PROXY)
15066e6b3f7cSQing Li 					arpc.sin.sin_other = SIN_PROXY;
15076e6b3f7cSQing Li 			}
15086e6b3f7cSQing Li 
15096e6b3f7cSQing Li 			sdl = &arpc.sdl;
15106e6b3f7cSQing Li 			sdl->sdl_family = AF_LINK;
15116e6b3f7cSQing Li 			sdl->sdl_len = sizeof(*sdl);
15126e6b3f7cSQing Li 			sdl->sdl_index = ifp->if_index;
15136e6b3f7cSQing Li 			sdl->sdl_type = ifp->if_type;
151493704ac5SQing Li 			if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
151593704ac5SQing Li 				sdl->sdl_alen = ifp->if_addrlen;
15166e6b3f7cSQing Li 				bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
151793704ac5SQing Li 			} else {
151893704ac5SQing Li 				sdl->sdl_alen = 0;
151993704ac5SQing Li 				bzero(LLADDR(sdl), ifp->if_addrlen);
152093704ac5SQing Li 			}
15216e6b3f7cSQing Li 
15226e6b3f7cSQing Li 			arpc.rtm.rtm_rmx.rmx_expire =
15236e6b3f7cSQing Li 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
15248eca593cSQing Li 			arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
15256e6b3f7cSQing Li 			if (lle->la_flags & LLE_STATIC)
15266e6b3f7cSQing Li 				arpc.rtm.rtm_flags |= RTF_STATIC;
15276e6b3f7cSQing Li 			arpc.rtm.rtm_index = ifp->if_index;
15286e6b3f7cSQing Li 			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
15296e6b3f7cSQing Li 			if (error)
15306e6b3f7cSQing Li 				break;
15316e6b3f7cSQing Li 		}
15326e6b3f7cSQing Li 	}
15336e6b3f7cSQing Li 	return error;
15346e6b3f7cSQing Li #undef SIN
15356e6b3f7cSQing Li }
15366e6b3f7cSQing Li 
15376e6b3f7cSQing Li void *
15386e6b3f7cSQing Li in_domifattach(struct ifnet *ifp)
15396e6b3f7cSQing Li {
1540d10910e6SBruce M Simpson 	struct in_ifinfo *ii;
1541d10910e6SBruce M Simpson 	struct lltable *llt;
15426e6b3f7cSQing Li 
1543d10910e6SBruce M Simpson 	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1544d10910e6SBruce M Simpson 
1545d10910e6SBruce M Simpson 	llt = lltable_init(ifp, AF_INET);
15466e6b3f7cSQing Li 	if (llt != NULL) {
15476e6b3f7cSQing Li 		llt->llt_new = in_lltable_new;
15486e6b3f7cSQing Li 		llt->llt_free = in_lltable_free;
1549c9d763bfSQing Li 		llt->llt_prefix_free = in_lltable_prefix_free;
15506e6b3f7cSQing Li 		llt->llt_rtcheck = in_lltable_rtcheck;
15516e6b3f7cSQing Li 		llt->llt_lookup = in_lltable_lookup;
15526e6b3f7cSQing Li 		llt->llt_dump = in_lltable_dump;
15536e6b3f7cSQing Li 	}
1554d10910e6SBruce M Simpson 	ii->ii_llt = llt;
1555d10910e6SBruce M Simpson 
1556d10910e6SBruce M Simpson 	ii->ii_igmp = igmp_domifattach(ifp);
1557d10910e6SBruce M Simpson 
1558d10910e6SBruce M Simpson 	return ii;
15596e6b3f7cSQing Li }
15606e6b3f7cSQing Li 
15616e6b3f7cSQing Li void
1562d10910e6SBruce M Simpson in_domifdetach(struct ifnet *ifp, void *aux)
15636e6b3f7cSQing Li {
1564d10910e6SBruce M Simpson 	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
15656e6b3f7cSQing Li 
1566d10910e6SBruce M Simpson 	igmp_domifdetach(ifp);
1567d10910e6SBruce M Simpson 	lltable_free(ii->ii_llt);
1568d10910e6SBruce M Simpson 	free(ii, M_IFADDR);
15696e6b3f7cSQing Li }
1570