xref: /freebsd/sys/netinet/in.c (revision d10910e6cee0dca8be3d5a7fdbe404a15d9e7b52)
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>
48603724d3SBjoern A. Zeeb #include <sys/vimage.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <net/if.h>
516e6b3f7cSQing Li #include <net/if_llatbl.h>
526a800098SYoshinobu Inoue #include <net/if_types.h>
53df8bae1dSRodney W. Grimes #include <net/route.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <netinet/in.h>
56df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
57e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h>
5871498f30SBruce M Simpson #include <netinet/ip_var.h>
594b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
60d10910e6SBruce M Simpson #include <netinet/igmp_var.h>
6155166637SPoul-Henning Kamp 
624d77a549SAlfred Perlstein static int in_mask2len(struct in_addr *);
634d77a549SAlfred Perlstein static void in_len2mask(struct in_addr *, int);
644d77a549SAlfred Perlstein static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
654d77a549SAlfred Perlstein 	struct ifnet *, struct thread *);
666a800098SYoshinobu Inoue 
6748321abeSMax Laier static int	in_addprefix(struct in_ifaddr *, int);
6848321abeSMax Laier static int	in_scrubprefix(struct in_ifaddr *);
694d77a549SAlfred Perlstein static void	in_socktrim(struct sockaddr_in *);
704d77a549SAlfred Perlstein static int	in_ifinit(struct ifnet *,
714d77a549SAlfred Perlstein 	    struct in_ifaddr *, struct sockaddr_in *, int);
72ec002feeSBruce M Simpson static void	in_purgemaddrs(struct ifnet *);
73df8bae1dSRodney W. Grimes 
7444e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
7544e33a07SMarko Zec static int subnetsarelocal;
7644e33a07SMarko Zec static int sameprefixcarponly;
7744e33a07SMarko Zec extern struct inpcbinfo ripcbinfo;
7844e33a07SMarko Zec #endif
7944e33a07SMarko Zec 
808b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, subnets_are_local,
818b615593SMarko Zec 	CTLFLAG_RW, subnetsarelocal, 0,
828b615593SMarko Zec 	"Treat all subnets as directly connected");
838b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, same_prefix_carp_only,
848b615593SMarko Zec 	CTLFLAG_RW, sameprefixcarponly, 0,
851ae95409SGleb Smirnoff 	"Refuse to create same prefixes on different interfaces");
86477180fbSGarrett Wollman 
87df8bae1dSRodney W. Grimes /*
88df8bae1dSRodney W. Grimes  * Return 1 if an internet address is for a ``local'' host
89df8bae1dSRodney W. Grimes  * (one to which we have a connection).  If subnetsarelocal
90df8bae1dSRodney W. Grimes  * is true, this includes other subnets of the local net.
91df8bae1dSRodney W. Grimes  * Otherwise, it includes only the directly-connected (sub)nets.
92df8bae1dSRodney W. Grimes  */
9326f9a767SRodney W. Grimes int
94f2565d68SRobert Watson in_localaddr(struct in_addr in)
95df8bae1dSRodney W. Grimes {
968b615593SMarko Zec 	INIT_VNET_INET(curvnet);
97df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
98df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
99df8bae1dSRodney W. Grimes 
100603724d3SBjoern A. Zeeb 	if (V_subnetsarelocal) {
101603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
102df8bae1dSRodney W. Grimes 			if ((i & ia->ia_netmask) == ia->ia_net)
103df8bae1dSRodney W. Grimes 				return (1);
104df8bae1dSRodney W. Grimes 	} else {
105603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
106df8bae1dSRodney W. Grimes 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
107df8bae1dSRodney W. Grimes 				return (1);
108df8bae1dSRodney W. Grimes 	}
109df8bae1dSRodney W. Grimes 	return (0);
110df8bae1dSRodney W. Grimes }
111df8bae1dSRodney W. Grimes 
112df8bae1dSRodney W. Grimes /*
1132eccc90bSAndre Oppermann  * Return 1 if an internet address is for the local host and configured
1142eccc90bSAndre Oppermann  * on one of its interfaces.
1152eccc90bSAndre Oppermann  */
1162eccc90bSAndre Oppermann int
117f2565d68SRobert Watson in_localip(struct in_addr in)
1182eccc90bSAndre Oppermann {
1198b615593SMarko Zec 	INIT_VNET_INET(curvnet);
1202eccc90bSAndre Oppermann 	struct in_ifaddr *ia;
1212eccc90bSAndre Oppermann 
1222eccc90bSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
1232eccc90bSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
124460473a0SBjoern A. Zeeb 			return (1);
1252eccc90bSAndre Oppermann 	}
126460473a0SBjoern A. Zeeb 	return (0);
1272eccc90bSAndre Oppermann }
1282eccc90bSAndre Oppermann 
1292eccc90bSAndre Oppermann /*
130df8bae1dSRodney W. Grimes  * Determine whether an IP address is in a reserved set of addresses
131df8bae1dSRodney W. Grimes  * that may not be forwarded, or whether datagrams to that destination
132df8bae1dSRodney W. Grimes  * may be forwarded.
133df8bae1dSRodney W. Grimes  */
13426f9a767SRodney W. Grimes int
135f2565d68SRobert Watson in_canforward(struct in_addr in)
136df8bae1dSRodney W. Grimes {
137df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
138df8bae1dSRodney W. Grimes 	register u_long net;
139df8bae1dSRodney W. Grimes 
140f8429ca2SBruce M Simpson 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
141df8bae1dSRodney W. Grimes 		return (0);
142df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i)) {
143df8bae1dSRodney W. Grimes 		net = i & IN_CLASSA_NET;
144df8bae1dSRodney W. Grimes 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
145df8bae1dSRodney W. Grimes 			return (0);
146df8bae1dSRodney W. Grimes 	}
147df8bae1dSRodney W. Grimes 	return (1);
148df8bae1dSRodney W. Grimes }
149df8bae1dSRodney W. Grimes 
150df8bae1dSRodney W. Grimes /*
151df8bae1dSRodney W. Grimes  * Trim a mask in a sockaddr
152df8bae1dSRodney W. Grimes  */
1530312fbe9SPoul-Henning Kamp static void
154f2565d68SRobert Watson in_socktrim(struct sockaddr_in *ap)
155df8bae1dSRodney W. Grimes {
156df8bae1dSRodney W. Grimes     register char *cplim = (char *) &ap->sin_addr;
157df8bae1dSRodney W. Grimes     register char *cp = (char *) (&ap->sin_addr + 1);
158df8bae1dSRodney W. Grimes 
159df8bae1dSRodney W. Grimes     ap->sin_len = 0;
160df00058dSGarrett Wollman     while (--cp >= cplim)
161df8bae1dSRodney W. Grimes 	if (*cp) {
162df8bae1dSRodney W. Grimes 	    (ap)->sin_len = cp - (char *) (ap) + 1;
163df8bae1dSRodney W. Grimes 	    break;
164df8bae1dSRodney W. Grimes 	}
165df8bae1dSRodney W. Grimes }
166df8bae1dSRodney W. Grimes 
1676a800098SYoshinobu Inoue static int
1686a800098SYoshinobu Inoue in_mask2len(mask)
1696a800098SYoshinobu Inoue 	struct in_addr *mask;
1706a800098SYoshinobu Inoue {
1716a800098SYoshinobu Inoue 	int x, y;
1726a800098SYoshinobu Inoue 	u_char *p;
1736a800098SYoshinobu Inoue 
1746a800098SYoshinobu Inoue 	p = (u_char *)mask;
1756a800098SYoshinobu Inoue 	for (x = 0; x < sizeof(*mask); x++) {
1766a800098SYoshinobu Inoue 		if (p[x] != 0xff)
1776a800098SYoshinobu Inoue 			break;
1786a800098SYoshinobu Inoue 	}
1796a800098SYoshinobu Inoue 	y = 0;
1806a800098SYoshinobu Inoue 	if (x < sizeof(*mask)) {
1816a800098SYoshinobu Inoue 		for (y = 0; y < 8; y++) {
1826a800098SYoshinobu Inoue 			if ((p[x] & (0x80 >> y)) == 0)
1836a800098SYoshinobu Inoue 				break;
1846a800098SYoshinobu Inoue 		}
1856a800098SYoshinobu Inoue 	}
186460473a0SBjoern A. Zeeb 	return (x * 8 + y);
1876a800098SYoshinobu Inoue }
1886a800098SYoshinobu Inoue 
1896a800098SYoshinobu Inoue static void
190f2565d68SRobert Watson in_len2mask(struct in_addr *mask, int len)
1916a800098SYoshinobu Inoue {
1926a800098SYoshinobu Inoue 	int i;
1936a800098SYoshinobu Inoue 	u_char *p;
1946a800098SYoshinobu Inoue 
1956a800098SYoshinobu Inoue 	p = (u_char *)mask;
1966a800098SYoshinobu Inoue 	bzero(mask, sizeof(*mask));
1976a800098SYoshinobu Inoue 	for (i = 0; i < len / 8; i++)
1986a800098SYoshinobu Inoue 		p[i] = 0xff;
1996a800098SYoshinobu Inoue 	if (len % 8)
2006a800098SYoshinobu Inoue 		p[i] = (0xff00 >> (len % 8)) & 0xff;
2016a800098SYoshinobu Inoue }
2026a800098SYoshinobu Inoue 
203df8bae1dSRodney W. Grimes /*
204df8bae1dSRodney W. Grimes  * Generic internet control operations (ioctl's).
205df8bae1dSRodney W. Grimes  * Ifp is 0 if not an interface-specific ioctl.
206df8bae1dSRodney W. Grimes  */
207df8bae1dSRodney W. Grimes /* ARGSUSED */
20826f9a767SRodney W. Grimes int
209f2565d68SRobert Watson in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
210f2565d68SRobert Watson     struct thread *td)
211df8bae1dSRodney W. Grimes {
2128b615593SMarko Zec 	INIT_VNET_INET(curvnet); /* both so and ifp can be NULL here! */
213df8bae1dSRodney W. Grimes 	register struct ifreq *ifr = (struct ifreq *)data;
214460473a0SBjoern A. Zeeb 	register struct in_ifaddr *ia, *iap;
215df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
216f7e083afSBruce M Simpson 	struct in_addr allhosts_addr;
217ca925d9cSJonathan Lemon 	struct in_addr dst;
218df8bae1dSRodney W. Grimes 	struct in_ifaddr *oia;
219d10910e6SBruce M Simpson 	struct in_ifinfo *ii;
220df8bae1dSRodney W. Grimes 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
221df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
2220f02fdacSBrian Somers 	int error, hostIsNew, iaIsNew, maskIsNew, s;
223f7e083afSBruce M Simpson 	int iaIsFirst;
2240f02fdacSBrian Somers 
225460473a0SBjoern A. Zeeb 	ia = NULL;
226d10910e6SBruce M Simpson 	ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
227f7e083afSBruce M Simpson 	iaIsFirst = 0;
2280f02fdacSBrian Somers 	iaIsNew = 0;
229f7e083afSBruce M Simpson 	allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
230df8bae1dSRodney W. Grimes 
2316a800098SYoshinobu Inoue 	switch (cmd) {
2326a800098SYoshinobu Inoue 	case SIOCALIFADDR:
233acd3428bSRobert Watson 		if (td != NULL) {
234acd3428bSRobert Watson 			error = priv_check(td, PRIV_NET_ADDIFADDR);
235acd3428bSRobert Watson 			if (error)
236acd3428bSRobert Watson 				return (error);
237acd3428bSRobert Watson 		}
238460473a0SBjoern A. Zeeb 		if (ifp == NULL)
239460473a0SBjoern A. Zeeb 			return (EINVAL);
240acd3428bSRobert Watson 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
241acd3428bSRobert Watson 
2426a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
243acd3428bSRobert Watson 		if (td != NULL) {
244acd3428bSRobert Watson 			error = priv_check(td, PRIV_NET_DELIFADDR);
245acd3428bSRobert Watson 			if (error)
246acd3428bSRobert Watson 				return (error);
247acd3428bSRobert Watson 		}
248460473a0SBjoern A. Zeeb 		if (ifp == NULL)
249460473a0SBjoern A. Zeeb 			return (EINVAL);
250acd3428bSRobert Watson 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
251acd3428bSRobert Watson 
2526a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
253460473a0SBjoern A. Zeeb 		if (ifp == NULL)
254460473a0SBjoern A. Zeeb 			return (EINVAL);
255b40ce416SJulian Elischer 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
2566a800098SYoshinobu Inoue 	}
2576a800098SYoshinobu Inoue 
258df8bae1dSRodney W. Grimes 	/*
259df8bae1dSRodney W. Grimes 	 * Find address for this interface, if it exists.
260ac0aa473SBill Fenner 	 *
261ac0aa473SBill Fenner 	 * If an alias address was specified, find that one instead of
262ca925d9cSJonathan Lemon 	 * the first one on the interface, if possible.
263df8bae1dSRodney W. Grimes 	 */
264460473a0SBjoern A. Zeeb 	if (ifp != NULL) {
265ca925d9cSJonathan Lemon 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
266ca925d9cSJonathan Lemon 		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
267ca925d9cSJonathan Lemon 			if (iap->ia_ifp == ifp &&
268ca925d9cSJonathan Lemon 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
2695ce0eb7fSBjoern A. Zeeb 				if (td == NULL || prison_check_ip4(
270b89e82ddSJamie Gritton 				    td->td_ucred, &dst) == 0)
271ac0aa473SBill Fenner 					ia = iap;
272df8bae1dSRodney W. Grimes 				break;
273ca925d9cSJonathan Lemon 			}
274ca925d9cSJonathan Lemon 		if (ia == NULL)
275ca925d9cSJonathan Lemon 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
276ca925d9cSJonathan Lemon 				iap = ifatoia(ifa);
277ca925d9cSJonathan Lemon 				if (iap->ia_addr.sin_family == AF_INET) {
2785ce0eb7fSBjoern A. Zeeb 					if (td != NULL &&
279b89e82ddSJamie Gritton 					    prison_check_ip4(td->td_ucred,
280b89e82ddSJamie Gritton 					    &iap->ia_addr.sin_addr) != 0)
2815ce0eb7fSBjoern A. Zeeb 						continue;
282ac0aa473SBill Fenner 					ia = iap;
283ac0aa473SBill Fenner 					break;
284ac0aa473SBill Fenner 				}
285ac0aa473SBill Fenner 			}
286f7e083afSBruce M Simpson 		if (ia == NULL)
287f7e083afSBruce M Simpson 			iaIsFirst = 1;
288ca925d9cSJonathan Lemon 	}
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes 	switch (cmd) {
291df8bae1dSRodney W. Grimes 
292df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
293df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
294460473a0SBjoern A. Zeeb 		if (ifp == NULL)
2956572231dSEivind Eklund 			return (EADDRNOTAVAIL);
2961067217dSGarrett Wollman 		if (ifra->ifra_addr.sin_family == AF_INET) {
297fc2ffbe6SPoul-Henning Kamp 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
298df8bae1dSRodney W. Grimes 				if (ia->ia_ifp == ifp  &&
299df8bae1dSRodney W. Grimes 				    ia->ia_addr.sin_addr.s_addr ==
300df8bae1dSRodney W. Grimes 				    ifra->ifra_addr.sin_addr.s_addr)
301df8bae1dSRodney W. Grimes 					break;
302df8bae1dSRodney W. Grimes 			}
3031067217dSGarrett Wollman 			if ((ifp->if_flags & IFF_POINTOPOINT)
3041067217dSGarrett Wollman 			    && (cmd == SIOCAIFADDR)
3051067217dSGarrett Wollman 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
3061067217dSGarrett Wollman 				== INADDR_ANY)) {
307460473a0SBjoern A. Zeeb 				return (EDESTADDRREQ);
3081067217dSGarrett Wollman 			}
3091067217dSGarrett Wollman 		}
310460473a0SBjoern A. Zeeb 		if (cmd == SIOCDIFADDR && ia == NULL)
311df8bae1dSRodney W. Grimes 			return (EADDRNOTAVAIL);
312df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
313df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
314df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
315df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
316acd3428bSRobert Watson 		if (td != NULL) {
317107d1244SBjoern A. Zeeb 			error = priv_check(td, (cmd == SIOCDIFADDR) ?
318107d1244SBjoern A. Zeeb 			    PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
319acd3428bSRobert Watson 			if (error)
320acd3428bSRobert Watson 				return (error);
321acd3428bSRobert Watson 		}
322df8bae1dSRodney W. Grimes 
323460473a0SBjoern A. Zeeb 		if (ifp == NULL)
3246572231dSEivind Eklund 			return (EADDRNOTAVAIL);
325460473a0SBjoern A. Zeeb 		if (ia == NULL) {
32659562606SGarrett Wollman 			ia = (struct in_ifaddr *)
327a163d034SWarner Losh 				malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
328460473a0SBjoern A. Zeeb 			if (ia == NULL)
329df8bae1dSRodney W. Grimes 				return (ENOBUFS);
330c655b7c4SDavid Greenman 			/*
331c655b7c4SDavid Greenman 			 * Protect from ipintr() traversing address list
332c655b7c4SDavid Greenman 			 * while we're modifying it.
333c655b7c4SDavid Greenman 			 */
334c655b7c4SDavid Greenman 			s = splnet();
33519fc74fbSJeffrey Hsu 			ifa = &ia->ia_ifa;
33619fc74fbSJeffrey Hsu 			IFA_LOCK_INIT(ifa);
33759562606SGarrett Wollman 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
33859562606SGarrett Wollman 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
33959562606SGarrett Wollman 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
34019fc74fbSJeffrey Hsu 			ifa->ifa_refcnt = 1;
34119fc74fbSJeffrey Hsu 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
34219fc74fbSJeffrey Hsu 
343df8bae1dSRodney W. Grimes 			ia->ia_sockmask.sin_len = 8;
344bc183b3fSDag-Erling Smørgrav 			ia->ia_sockmask.sin_family = AF_INET;
345df8bae1dSRodney W. Grimes 			if (ifp->if_flags & IFF_BROADCAST) {
346df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
347df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_family = AF_INET;
348df8bae1dSRodney W. Grimes 			}
349df8bae1dSRodney W. Grimes 			ia->ia_ifp = ifp;
350f3d30eb2SGleb Smirnoff 
351603724d3SBjoern A. Zeeb 			TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
352c655b7c4SDavid Greenman 			splx(s);
3530f02fdacSBrian Somers 			iaIsNew = 1;
354df8bae1dSRodney W. Grimes 		}
355df8bae1dSRodney W. Grimes 		break;
356df8bae1dSRodney W. Grimes 
357df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
358acd3428bSRobert Watson 		if (td != NULL) {
359acd3428bSRobert Watson 			error = priv_check(td, PRIV_NET_ADDIFADDR);
360acd3428bSRobert Watson 			if (error)
361acd3428bSRobert Watson 				return (error);
362acd3428bSRobert Watson 		}
363df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
364df8bae1dSRodney W. Grimes 
365df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
366df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
367df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
368df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
369460473a0SBjoern A. Zeeb 		if (ia == NULL)
370df8bae1dSRodney W. Grimes 			return (EADDRNOTAVAIL);
371df8bae1dSRodney W. Grimes 		break;
372df8bae1dSRodney W. Grimes 	}
373df8bae1dSRodney W. Grimes 	switch (cmd) {
374df8bae1dSRodney W. Grimes 
375df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
376df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
3770f02fdacSBrian Somers 		return (0);
378df8bae1dSRodney W. Grimes 
379df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
380df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
381df8bae1dSRodney W. Grimes 			return (EINVAL);
382df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
3830f02fdacSBrian Somers 		return (0);
384df8bae1dSRodney W. Grimes 
385df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
386df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
387df8bae1dSRodney W. Grimes 			return (EINVAL);
388df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
3890f02fdacSBrian Somers 		return (0);
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
392df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
3930f02fdacSBrian Somers 		return (0);
394df8bae1dSRodney W. Grimes 
395df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
396df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
397df8bae1dSRodney W. Grimes 			return (EINVAL);
398df8bae1dSRodney W. Grimes 		oldaddr = ia->ia_dstaddr;
399df8bae1dSRodney W. Grimes 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
400460473a0SBjoern A. Zeeb 		if (ifp->if_ioctl != NULL) {
401ba5da2a0SIan Dowse 			IFF_LOCKGIANT(ifp);
402ba5da2a0SIan Dowse 			error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
403ba5da2a0SIan Dowse 			    (caddr_t)ia);
404ba5da2a0SIan Dowse 			IFF_UNLOCKGIANT(ifp);
405ba5da2a0SIan Dowse 			if (error) {
406df8bae1dSRodney W. Grimes 				ia->ia_dstaddr = oldaddr;
407df8bae1dSRodney W. Grimes 				return (error);
408df8bae1dSRodney W. Grimes 			}
409ba5da2a0SIan Dowse 		}
410df8bae1dSRodney W. Grimes 		if (ia->ia_flags & IFA_ROUTE) {
411df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
412df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
413df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr =
414df8bae1dSRodney W. Grimes 					(struct sockaddr *)&ia->ia_dstaddr;
415df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
416df8bae1dSRodney W. Grimes 		}
4170f02fdacSBrian Somers 		return (0);
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
420df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
421df8bae1dSRodney W. Grimes 			return (EINVAL);
422df8bae1dSRodney W. Grimes 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
4230f02fdacSBrian Somers 		return (0);
424df8bae1dSRodney W. Grimes 
425df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
4260f02fdacSBrian Somers 		error = in_ifinit(ifp, ia,
4270f02fdacSBrian Somers 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
4280f02fdacSBrian Somers 		if (error != 0 && iaIsNew)
4290f02fdacSBrian Somers 			break;
430f7e083afSBruce M Simpson 		if (error == 0) {
431d10910e6SBruce M Simpson 			if (iaIsFirst &&
432d10910e6SBruce M Simpson 			    (ifp->if_flags & IFF_MULTICAST) != 0) {
433d10910e6SBruce M Simpson 				error = in_joingroup(ifp, &allhosts_addr,
434d10910e6SBruce M Simpson 				    NULL, &ii->ii_allhosts);
435d10910e6SBruce M Simpson 			}
43625a4adceSMax Laier 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
437f7e083afSBruce M Simpson 		}
4380f02fdacSBrian Somers 		return (0);
439df8bae1dSRodney W. Grimes 
440df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
441bc183b3fSDag-Erling Smørgrav 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
442bc183b3fSDag-Erling Smørgrav 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
4430f02fdacSBrian Somers 		return (0);
444df8bae1dSRodney W. Grimes 
445df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
446df8bae1dSRodney W. Grimes 		maskIsNew = 0;
447df8bae1dSRodney W. Grimes 		hostIsNew = 1;
448df8bae1dSRodney W. Grimes 		error = 0;
449df8bae1dSRodney W. Grimes 		if (ia->ia_addr.sin_family == AF_INET) {
450df8bae1dSRodney W. Grimes 			if (ifra->ifra_addr.sin_len == 0) {
451df8bae1dSRodney W. Grimes 				ifra->ifra_addr = ia->ia_addr;
452df8bae1dSRodney W. Grimes 				hostIsNew = 0;
453df8bae1dSRodney W. Grimes 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
454df8bae1dSRodney W. Grimes 					       ia->ia_addr.sin_addr.s_addr)
455df8bae1dSRodney W. Grimes 				hostIsNew = 0;
456df8bae1dSRodney W. Grimes 		}
457df8bae1dSRodney W. Grimes 		if (ifra->ifra_mask.sin_len) {
458df8bae1dSRodney W. Grimes 			in_ifscrub(ifp, ia);
459df8bae1dSRodney W. Grimes 			ia->ia_sockmask = ifra->ifra_mask;
460bc183b3fSDag-Erling Smørgrav 			ia->ia_sockmask.sin_family = AF_INET;
461df8bae1dSRodney W. Grimes 			ia->ia_subnetmask =
462df8bae1dSRodney W. Grimes 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
463df8bae1dSRodney W. Grimes 			maskIsNew = 1;
464df8bae1dSRodney W. Grimes 		}
465df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
466df8bae1dSRodney W. Grimes 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
467df8bae1dSRodney W. Grimes 			in_ifscrub(ifp, ia);
468df8bae1dSRodney W. Grimes 			ia->ia_dstaddr = ifra->ifra_dstaddr;
469df8bae1dSRodney W. Grimes 			maskIsNew  = 1; /* We lie; but the effect's the same */
470df8bae1dSRodney W. Grimes 		}
471df8bae1dSRodney W. Grimes 		if (ifra->ifra_addr.sin_family == AF_INET &&
472df8bae1dSRodney W. Grimes 		    (hostIsNew || maskIsNew))
473df8bae1dSRodney W. Grimes 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
4740f02fdacSBrian Somers 		if (error != 0 && iaIsNew)
4750f02fdacSBrian Somers 			break;
4760f02fdacSBrian Somers 
477df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) &&
478df8bae1dSRodney W. Grimes 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
479df8bae1dSRodney W. Grimes 			ia->ia_broadaddr = ifra->ifra_broadaddr;
480f7e083afSBruce M Simpson 		if (error == 0) {
481d10910e6SBruce M Simpson 			if (iaIsFirst &&
482d10910e6SBruce M Simpson 			    (ifp->if_flags & IFF_MULTICAST) != 0) {
483d10910e6SBruce M Simpson 				error = in_joingroup(ifp, &allhosts_addr,
484d10910e6SBruce M Simpson 				    NULL, &ii->ii_allhosts);
485d10910e6SBruce M Simpson 			}
48625a4adceSMax Laier 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
487f7e083afSBruce M Simpson 		}
488df8bae1dSRodney W. Grimes 		return (error);
489df8bae1dSRodney W. Grimes 
490df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
491089cdfadSRuslan Ermilov 		/*
492089cdfadSRuslan Ermilov 		 * in_ifscrub kills the interface route.
493089cdfadSRuslan Ermilov 		 */
494df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
495c655b7c4SDavid Greenman 		/*
496089cdfadSRuslan Ermilov 		 * in_ifadown gets rid of all the rest of
497089cdfadSRuslan Ermilov 		 * the routes.  This is not quite the right
498089cdfadSRuslan Ermilov 		 * thing to do, but at least if we are running
499089cdfadSRuslan Ermilov 		 * a routing process they will come back.
500089cdfadSRuslan Ermilov 		 */
50191854268SRuslan Ermilov 		in_ifadown(&ia->ia_ifa, 1);
50225a4adceSMax Laier 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
5030f02fdacSBrian Somers 		error = 0;
504df8bae1dSRodney W. Grimes 		break;
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes 	default:
507460473a0SBjoern A. Zeeb 		if (ifp == NULL || ifp->if_ioctl == NULL)
508df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
509ba5da2a0SIan Dowse 		IFF_LOCKGIANT(ifp);
510ba5da2a0SIan Dowse 		error = (*ifp->if_ioctl)(ifp, cmd, data);
511ba5da2a0SIan Dowse 		IFF_UNLOCKGIANT(ifp);
512ba5da2a0SIan Dowse 		return (error);
513df8bae1dSRodney W. Grimes 	}
5140f02fdacSBrian Somers 
5150f02fdacSBrian Somers 	/*
5160f02fdacSBrian Somers 	 * Protect from ipintr() traversing address list while we're modifying
5170f02fdacSBrian Somers 	 * it.
5180f02fdacSBrian Somers 	 */
5190f02fdacSBrian Somers 	s = splnet();
5200f02fdacSBrian Somers 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
521603724d3SBjoern A. Zeeb 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
522f7e083afSBruce M Simpson 	if (ia->ia_addr.sin_family == AF_INET) {
5230f02fdacSBrian Somers 		LIST_REMOVE(ia, ia_hash);
524f7e083afSBruce M Simpson 		/*
525f7e083afSBruce M Simpson 		 * If this is the last IPv4 address configured on this
526f7e083afSBruce M Simpson 		 * interface, leave the all-hosts group.
527d10910e6SBruce M Simpson 		 * No state-change report need be transmitted.
528f7e083afSBruce M Simpson 		 */
529f7e083afSBruce M Simpson 		oia = NULL;
530f7e083afSBruce M Simpson 		IFP_TO_IA(ifp, oia);
531f7e083afSBruce M Simpson 		if (oia == NULL) {
532f7e083afSBruce M Simpson 			IFF_LOCKGIANT(ifp);
533f7e083afSBruce M Simpson 			IN_MULTI_LOCK();
534d10910e6SBruce M Simpson 			if (ii->ii_allhosts) {
535d10910e6SBruce M Simpson 				(void)in_leavegroup_locked(ii->ii_allhosts,
536d10910e6SBruce M Simpson 				    NULL);
537d10910e6SBruce M Simpson 				ii->ii_allhosts = NULL;
538d10910e6SBruce M Simpson 			}
539f7e083afSBruce M Simpson 			IN_MULTI_UNLOCK();
540f7e083afSBruce M Simpson 			IFF_UNLOCKGIANT(ifp);
541f7e083afSBruce M Simpson 		}
542f7e083afSBruce M Simpson 	}
5430f02fdacSBrian Somers 	IFAFREE(&ia->ia_ifa);
5440f02fdacSBrian Somers 	splx(s);
5450f02fdacSBrian Somers 
5460f02fdacSBrian Somers 	return (error);
547df8bae1dSRodney W. Grimes }
548df8bae1dSRodney W. Grimes 
549df8bae1dSRodney W. Grimes /*
5506a800098SYoshinobu Inoue  * SIOC[GAD]LIFADDR.
5516a800098SYoshinobu Inoue  *	SIOCGLIFADDR: get first address. (?!?)
5526a800098SYoshinobu Inoue  *	SIOCGLIFADDR with IFLR_PREFIX:
5536a800098SYoshinobu Inoue  *		get first address that matches the specified prefix.
5546a800098SYoshinobu Inoue  *	SIOCALIFADDR: add the specified address.
5556a800098SYoshinobu Inoue  *	SIOCALIFADDR with IFLR_PREFIX:
5566a800098SYoshinobu Inoue  *		EINVAL since we can't deduce hostid part of the address.
5576a800098SYoshinobu Inoue  *	SIOCDLIFADDR: delete the specified address.
5586a800098SYoshinobu Inoue  *	SIOCDLIFADDR with IFLR_PREFIX:
5596a800098SYoshinobu Inoue  *		delete the first address that matches the specified prefix.
5606a800098SYoshinobu Inoue  * return values:
5616a800098SYoshinobu Inoue  *	EINVAL on invalid parameters
5626a800098SYoshinobu Inoue  *	EADDRNOTAVAIL on prefix match failed/specified address not found
5636a800098SYoshinobu Inoue  *	other values may be returned from in_ioctl()
5646a800098SYoshinobu Inoue  */
5656a800098SYoshinobu Inoue static int
566f2565d68SRobert Watson in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
567f2565d68SRobert Watson     struct ifnet *ifp, struct thread *td)
5686a800098SYoshinobu Inoue {
5696a800098SYoshinobu Inoue 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
5706a800098SYoshinobu Inoue 	struct ifaddr *ifa;
5716a800098SYoshinobu Inoue 
5726a800098SYoshinobu Inoue 	/* sanity checks */
573460473a0SBjoern A. Zeeb 	if (data == NULL || ifp == NULL) {
5746a800098SYoshinobu Inoue 		panic("invalid argument to in_lifaddr_ioctl");
5756a800098SYoshinobu Inoue 		/*NOTRECHED*/
5766a800098SYoshinobu Inoue 	}
5776a800098SYoshinobu Inoue 
5786a800098SYoshinobu Inoue 	switch (cmd) {
5796a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
5806a800098SYoshinobu Inoue 		/* address must be specified on GET with IFLR_PREFIX */
5816a800098SYoshinobu Inoue 		if ((iflr->flags & IFLR_PREFIX) == 0)
5826a800098SYoshinobu Inoue 			break;
5836a800098SYoshinobu Inoue 		/*FALLTHROUGH*/
5846a800098SYoshinobu Inoue 	case SIOCALIFADDR:
5856a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
5866a800098SYoshinobu Inoue 		/* address must be specified on ADD and DELETE */
5875d60ed0eSYoshinobu Inoue 		if (iflr->addr.ss_family != AF_INET)
588460473a0SBjoern A. Zeeb 			return (EINVAL);
5895d60ed0eSYoshinobu Inoue 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
590460473a0SBjoern A. Zeeb 			return (EINVAL);
5916a800098SYoshinobu Inoue 		/* XXX need improvement */
5925d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family
5935d60ed0eSYoshinobu Inoue 		 && iflr->dstaddr.ss_family != AF_INET)
594460473a0SBjoern A. Zeeb 			return (EINVAL);
5955d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family
5965d60ed0eSYoshinobu Inoue 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
597460473a0SBjoern A. Zeeb 			return (EINVAL);
5986a800098SYoshinobu Inoue 		break;
5996a800098SYoshinobu Inoue 	default: /*shouldn't happen*/
600460473a0SBjoern A. Zeeb 		return (EOPNOTSUPP);
6016a800098SYoshinobu Inoue 	}
6026a800098SYoshinobu Inoue 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
603460473a0SBjoern A. Zeeb 		return (EINVAL);
6046a800098SYoshinobu Inoue 
6056a800098SYoshinobu Inoue 	switch (cmd) {
6066a800098SYoshinobu Inoue 	case SIOCALIFADDR:
6076a800098SYoshinobu Inoue 	    {
6086a800098SYoshinobu Inoue 		struct in_aliasreq ifra;
6096a800098SYoshinobu Inoue 
6106a800098SYoshinobu Inoue 		if (iflr->flags & IFLR_PREFIX)
611460473a0SBjoern A. Zeeb 			return (EINVAL);
6126a800098SYoshinobu Inoue 
6136a800098SYoshinobu Inoue 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
6146a800098SYoshinobu Inoue 		bzero(&ifra, sizeof(ifra));
6156a800098SYoshinobu Inoue 		bcopy(iflr->iflr_name, ifra.ifra_name,
6166a800098SYoshinobu Inoue 			sizeof(ifra.ifra_name));
6176a800098SYoshinobu Inoue 
6185d60ed0eSYoshinobu Inoue 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
6196a800098SYoshinobu Inoue 
6205d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family) {	/*XXX*/
6216a800098SYoshinobu Inoue 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
6225d60ed0eSYoshinobu Inoue 				iflr->dstaddr.ss_len);
6236a800098SYoshinobu Inoue 		}
6246a800098SYoshinobu Inoue 
6256a800098SYoshinobu Inoue 		ifra.ifra_mask.sin_family = AF_INET;
6266a800098SYoshinobu Inoue 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
6276a800098SYoshinobu Inoue 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
6286a800098SYoshinobu Inoue 
629460473a0SBjoern A. Zeeb 		return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
6306a800098SYoshinobu Inoue 	    }
6316a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
6326a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
6336a800098SYoshinobu Inoue 	    {
6346a800098SYoshinobu Inoue 		struct in_ifaddr *ia;
6356a800098SYoshinobu Inoue 		struct in_addr mask, candidate, match;
6366a800098SYoshinobu Inoue 		struct sockaddr_in *sin;
6376a800098SYoshinobu Inoue 
6386a800098SYoshinobu Inoue 		bzero(&mask, sizeof(mask));
639fbdd20a1SMatt Jacob 		bzero(&match, sizeof(match));
6406a800098SYoshinobu Inoue 		if (iflr->flags & IFLR_PREFIX) {
6416a800098SYoshinobu Inoue 			/* lookup a prefix rather than address. */
6426a800098SYoshinobu Inoue 			in_len2mask(&mask, iflr->prefixlen);
6436a800098SYoshinobu Inoue 
6446a800098SYoshinobu Inoue 			sin = (struct sockaddr_in *)&iflr->addr;
6456a800098SYoshinobu Inoue 			match.s_addr = sin->sin_addr.s_addr;
6466a800098SYoshinobu Inoue 			match.s_addr &= mask.s_addr;
6476a800098SYoshinobu Inoue 
6486a800098SYoshinobu Inoue 			/* if you set extra bits, that's wrong */
6496a800098SYoshinobu Inoue 			if (match.s_addr != sin->sin_addr.s_addr)
650460473a0SBjoern A. Zeeb 				return (EINVAL);
6516a800098SYoshinobu Inoue 
6526a800098SYoshinobu Inoue 		} else {
6536a800098SYoshinobu Inoue 			/* on getting an address, take the 1st match */
6546a800098SYoshinobu Inoue 			/* on deleting an address, do exact match */
655fbdd20a1SMatt Jacob 			if (cmd != SIOCGLIFADDR) {
6566a800098SYoshinobu Inoue 				in_len2mask(&mask, 32);
6576a800098SYoshinobu Inoue 				sin = (struct sockaddr_in *)&iflr->addr;
6586a800098SYoshinobu Inoue 				match.s_addr = sin->sin_addr.s_addr;
6596a800098SYoshinobu Inoue 			}
6606a800098SYoshinobu Inoue 		}
6616a800098SYoshinobu Inoue 
6626a800098SYoshinobu Inoue 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
6636a800098SYoshinobu Inoue 			if (ifa->ifa_addr->sa_family != AF_INET6)
6646a800098SYoshinobu Inoue 				continue;
665fbdd20a1SMatt Jacob 			if (match.s_addr == 0)
6666a800098SYoshinobu Inoue 				break;
6676a800098SYoshinobu Inoue 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
6686a800098SYoshinobu Inoue 			candidate.s_addr &= mask.s_addr;
6696a800098SYoshinobu Inoue 			if (candidate.s_addr == match.s_addr)
6706a800098SYoshinobu Inoue 				break;
6716a800098SYoshinobu Inoue 		}
672460473a0SBjoern A. Zeeb 		if (ifa == NULL)
673460473a0SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
6746a800098SYoshinobu Inoue 		ia = (struct in_ifaddr *)ifa;
6756a800098SYoshinobu Inoue 
6766a800098SYoshinobu Inoue 		if (cmd == SIOCGLIFADDR) {
6776a800098SYoshinobu Inoue 			/* fill in the if_laddrreq structure */
6786a800098SYoshinobu Inoue 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
6796a800098SYoshinobu Inoue 
6806a800098SYoshinobu Inoue 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
6816a800098SYoshinobu Inoue 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
6826a800098SYoshinobu Inoue 					ia->ia_dstaddr.sin_len);
6836a800098SYoshinobu Inoue 			} else
6846a800098SYoshinobu Inoue 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
6856a800098SYoshinobu Inoue 
6866a800098SYoshinobu Inoue 			iflr->prefixlen =
6876a800098SYoshinobu Inoue 				in_mask2len(&ia->ia_sockmask.sin_addr);
6886a800098SYoshinobu Inoue 
6896a800098SYoshinobu Inoue 			iflr->flags = 0;	/*XXX*/
6906a800098SYoshinobu Inoue 
691460473a0SBjoern A. Zeeb 			return (0);
6926a800098SYoshinobu Inoue 		} else {
6936a800098SYoshinobu Inoue 			struct in_aliasreq ifra;
6946a800098SYoshinobu Inoue 
6956a800098SYoshinobu Inoue 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
6966a800098SYoshinobu Inoue 			bzero(&ifra, sizeof(ifra));
6976a800098SYoshinobu Inoue 			bcopy(iflr->iflr_name, ifra.ifra_name,
6986a800098SYoshinobu Inoue 				sizeof(ifra.ifra_name));
6996a800098SYoshinobu Inoue 
7006a800098SYoshinobu Inoue 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
7016a800098SYoshinobu Inoue 				ia->ia_addr.sin_len);
7026a800098SYoshinobu Inoue 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
7036a800098SYoshinobu Inoue 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
7046a800098SYoshinobu Inoue 					ia->ia_dstaddr.sin_len);
7056a800098SYoshinobu Inoue 			}
7066a800098SYoshinobu Inoue 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
7076a800098SYoshinobu Inoue 				ia->ia_sockmask.sin_len);
7086a800098SYoshinobu Inoue 
709460473a0SBjoern A. Zeeb 			return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
710460473a0SBjoern A. Zeeb 			    ifp, td));
7116a800098SYoshinobu Inoue 		}
7126a800098SYoshinobu Inoue 	    }
7136a800098SYoshinobu Inoue 	}
7146a800098SYoshinobu Inoue 
715460473a0SBjoern A. Zeeb 	return (EOPNOTSUPP);	/*just for safety*/
7166a800098SYoshinobu Inoue }
7176a800098SYoshinobu Inoue 
7186a800098SYoshinobu Inoue /*
719df8bae1dSRodney W. Grimes  * Delete any existing route for an interface.
720df8bae1dSRodney W. Grimes  */
72139191c8eSGarrett Wollman void
722f2565d68SRobert Watson in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
723df8bae1dSRodney W. Grimes {
724f2565d68SRobert Watson 
72548321abeSMax Laier 	in_scrubprefix(ia);
726df8bae1dSRodney W. Grimes }
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes /*
729df8bae1dSRodney W. Grimes  * Initialize an interface's internet address
730df8bae1dSRodney W. Grimes  * and routing table entry.
731df8bae1dSRodney W. Grimes  */
7320312fbe9SPoul-Henning Kamp static int
733f2565d68SRobert Watson in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
734f2565d68SRobert Watson     int scrub)
735df8bae1dSRodney W. Grimes {
7368b615593SMarko Zec 	INIT_VNET_INET(ifp->if_vnet);
737df8bae1dSRodney W. Grimes 	register u_long i = ntohl(sin->sin_addr.s_addr);
738df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
7395a43847dSBrian Somers 	int s = splimp(), flags = RTF_UP, error = 0;
740df8bae1dSRodney W. Grimes 
741df8bae1dSRodney W. Grimes 	oldaddr = ia->ia_addr;
7422754d95dSSUZUKI Shinsuke 	if (oldaddr.sin_family == AF_INET)
7432754d95dSSUZUKI Shinsuke 		LIST_REMOVE(ia, ia_hash);
744df8bae1dSRodney W. Grimes 	ia->ia_addr = *sin;
7452754d95dSSUZUKI Shinsuke 	if (ia->ia_addr.sin_family == AF_INET)
7462754d95dSSUZUKI Shinsuke 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
7472754d95dSSUZUKI Shinsuke 		    ia, ia_hash);
748df8bae1dSRodney W. Grimes 	/*
749df8bae1dSRodney W. Grimes 	 * Give the interface a chance to initialize
750df8bae1dSRodney W. Grimes 	 * if this is its first address,
751df8bae1dSRodney W. Grimes 	 * and to validate the address if necessary.
752df8bae1dSRodney W. Grimes 	 */
753460473a0SBjoern A. Zeeb 	if (ifp->if_ioctl != NULL) {
754ba5da2a0SIan Dowse 		IFF_LOCKGIANT(ifp);
755ba5da2a0SIan Dowse 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
756ba5da2a0SIan Dowse 		IFF_UNLOCKGIANT(ifp);
757ba5da2a0SIan Dowse 		if (error) {
758df8bae1dSRodney W. Grimes 			splx(s);
7592754d95dSSUZUKI Shinsuke 			/* LIST_REMOVE(ia, ia_hash) is done in in_control */
760df8bae1dSRodney W. Grimes 			ia->ia_addr = oldaddr;
76122c819a7SJonathan Lemon 			if (ia->ia_addr.sin_family == AF_INET)
762ba5da2a0SIan Dowse 				LIST_INSERT_HEAD(INADDR_HASH(
763ba5da2a0SIan Dowse 				    ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
764cf77b848SOleksandr Tymoshenko 			else
765cf77b848SOleksandr Tymoshenko 				/*
766cf77b848SOleksandr Tymoshenko 				 * If oldaddr family is not AF_INET (e.g.
767cf77b848SOleksandr Tymoshenko 				 * interface has been just created) in_control
768cf77b848SOleksandr Tymoshenko 				 * does not call LIST_REMOVE, and we end up
769cf77b848SOleksandr Tymoshenko 				 * with bogus ia entries in hash
770cf77b848SOleksandr Tymoshenko 				 */
771cf77b848SOleksandr Tymoshenko 				LIST_REMOVE(ia, ia_hash);
7722754d95dSSUZUKI Shinsuke 			return (error);
7732754d95dSSUZUKI Shinsuke 		}
774ba5da2a0SIan Dowse 	}
775df8bae1dSRodney W. Grimes 	splx(s);
776df8bae1dSRodney W. Grimes 	if (scrub) {
777df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
778df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
779df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
780df8bae1dSRodney W. Grimes 	}
781df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i))
782df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSA_NET;
783df8bae1dSRodney W. Grimes 	else if (IN_CLASSB(i))
784df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSB_NET;
785df8bae1dSRodney W. Grimes 	else
786df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSC_NET;
787df8bae1dSRodney W. Grimes 	/*
788df8bae1dSRodney W. Grimes 	 * The subnet mask usually includes at least the standard network part,
789df8bae1dSRodney W. Grimes 	 * but may may be smaller in the case of supernetting.
790df8bae1dSRodney W. Grimes 	 * If it is set, we believe it.
791df8bae1dSRodney W. Grimes 	 */
792df8bae1dSRodney W. Grimes 	if (ia->ia_subnetmask == 0) {
793df8bae1dSRodney W. Grimes 		ia->ia_subnetmask = ia->ia_netmask;
794df8bae1dSRodney W. Grimes 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
795df8bae1dSRodney W. Grimes 	} else
796df8bae1dSRodney W. Grimes 		ia->ia_netmask &= ia->ia_subnetmask;
797df8bae1dSRodney W. Grimes 	ia->ia_net = i & ia->ia_netmask;
798df8bae1dSRodney W. Grimes 	ia->ia_subnet = i & ia->ia_subnetmask;
799df8bae1dSRodney W. Grimes 	in_socktrim(&ia->ia_sockmask);
80050bb1704SGleb Smirnoff #ifdef DEV_CARP
80150bb1704SGleb Smirnoff 	/*
80250bb1704SGleb Smirnoff 	 * XXX: carp(4) does not have interface route
80350bb1704SGleb Smirnoff 	 */
80450bb1704SGleb Smirnoff 	if (ifp->if_type == IFT_CARP)
80550bb1704SGleb Smirnoff 		return (0);
80650bb1704SGleb Smirnoff #endif
807df8bae1dSRodney W. Grimes 	/*
808df8bae1dSRodney W. Grimes 	 * Add route for the network.
809df8bae1dSRodney W. Grimes 	 */
810df8bae1dSRodney W. Grimes 	ia->ia_ifa.ifa_metric = ifp->if_metric;
811df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_BROADCAST) {
812df8bae1dSRodney W. Grimes 		ia->ia_broadaddr.sin_addr.s_addr =
813df8bae1dSRodney W. Grimes 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
814df8bae1dSRodney W. Grimes 		ia->ia_netbroadcast.s_addr =
815df8bae1dSRodney W. Grimes 			htonl(ia->ia_net | ~ ia->ia_netmask);
816df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_LOOPBACK) {
8179a6a6eebSMax Laier 		ia->ia_dstaddr = ia->ia_addr;
818df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
819df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
820df8bae1dSRodney W. Grimes 		if (ia->ia_dstaddr.sin_family != AF_INET)
821df8bae1dSRodney W. Grimes 			return (0);
822df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
823df8bae1dSRodney W. Grimes 	}
82448321abeSMax Laier 	if ((error = in_addprefix(ia, flags)) != 0)
8250f02fdacSBrian Somers 		return (error);
8260f02fdacSBrian Somers 
827df8bae1dSRodney W. Grimes 	return (error);
828df8bae1dSRodney W. Grimes }
829df8bae1dSRodney W. Grimes 
83048321abeSMax Laier #define rtinitflags(x) \
83148321abeSMax Laier 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
83248321abeSMax Laier 	    ? RTF_HOST : 0)
83348321abeSMax Laier /*
834fbdd20a1SMatt Jacob  * Check if we have a route for the given prefix already or add one accordingly.
83548321abeSMax Laier  */
83648321abeSMax Laier static int
837f2565d68SRobert Watson in_addprefix(struct in_ifaddr *target, int flags)
83848321abeSMax Laier {
8398b615593SMarko Zec 	INIT_VNET_INET(curvnet);
84048321abeSMax Laier 	struct in_ifaddr *ia;
841bfb26eecSGleb Smirnoff 	struct in_addr prefix, mask, p, m;
84248321abeSMax Laier 	int error;
84348321abeSMax Laier 
844fbdd20a1SMatt Jacob 	if ((flags & RTF_HOST) != 0) {
84548321abeSMax Laier 		prefix = target->ia_dstaddr.sin_addr;
846fbdd20a1SMatt Jacob 		mask.s_addr = 0;
847fbdd20a1SMatt Jacob 	} else {
84848321abeSMax Laier 		prefix = target->ia_addr.sin_addr;
84948321abeSMax Laier 		mask = target->ia_sockmask.sin_addr;
85048321abeSMax Laier 		prefix.s_addr &= mask.s_addr;
85148321abeSMax Laier 	}
85248321abeSMax Laier 
853603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
854bfb26eecSGleb Smirnoff 		if (rtinitflags(ia)) {
85548321abeSMax Laier 			p = ia->ia_addr.sin_addr;
85648321abeSMax Laier 
85748321abeSMax Laier 			if (prefix.s_addr != p.s_addr)
85848321abeSMax Laier 				continue;
859bfb26eecSGleb Smirnoff 		} else {
860bfb26eecSGleb Smirnoff 			p = ia->ia_addr.sin_addr;
861bfb26eecSGleb Smirnoff 			m = ia->ia_sockmask.sin_addr;
862bfb26eecSGleb Smirnoff 			p.s_addr &= m.s_addr;
863bfb26eecSGleb Smirnoff 
864bfb26eecSGleb Smirnoff 			if (prefix.s_addr != p.s_addr ||
865bfb26eecSGleb Smirnoff 			    mask.s_addr != m.s_addr)
866bfb26eecSGleb Smirnoff 				continue;
867bfb26eecSGleb Smirnoff 		}
86848321abeSMax Laier 
86948321abeSMax Laier 		/*
87048321abeSMax Laier 		 * If we got a matching prefix route inserted by other
87148321abeSMax Laier 		 * interface address, we are done here.
87248321abeSMax Laier 		 */
8731ae95409SGleb Smirnoff 		if (ia->ia_flags & IFA_ROUTE) {
874603724d3SBjoern A. Zeeb 			if (V_sameprefixcarponly &&
8751ae95409SGleb Smirnoff 			    target->ia_ifp->if_type != IFT_CARP &&
8761ae95409SGleb Smirnoff 			    ia->ia_ifp->if_type != IFT_CARP)
8771ae95409SGleb Smirnoff 				return (EEXIST);
8781ae95409SGleb Smirnoff 			else
8791ae95409SGleb Smirnoff 				return (0);
8801ae95409SGleb Smirnoff 		}
88148321abeSMax Laier 	}
88248321abeSMax Laier 
88348321abeSMax Laier 	/*
88448321abeSMax Laier 	 * No-one seem to have this prefix route, so we try to insert it.
88548321abeSMax Laier 	 */
88648321abeSMax Laier 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
88748321abeSMax Laier 	if (!error)
88848321abeSMax Laier 		target->ia_flags |= IFA_ROUTE;
889460473a0SBjoern A. Zeeb 	return (error);
89048321abeSMax Laier }
89148321abeSMax Laier 
8926e6b3f7cSQing Li extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
8936e6b3f7cSQing Li 
89448321abeSMax Laier /*
89548321abeSMax Laier  * If there is no other address in the system that can serve a route to the
89648321abeSMax Laier  * same prefix, remove the route.  Hand over the route to the new address
89748321abeSMax Laier  * otherwise.
89848321abeSMax Laier  */
89948321abeSMax Laier static int
900f2565d68SRobert Watson in_scrubprefix(struct in_ifaddr *target)
90148321abeSMax Laier {
9028b615593SMarko Zec 	INIT_VNET_INET(curvnet);
90348321abeSMax Laier 	struct in_ifaddr *ia;
90448321abeSMax Laier 	struct in_addr prefix, mask, p;
90548321abeSMax Laier 	int error;
90648321abeSMax Laier 
90748321abeSMax Laier 	if ((target->ia_flags & IFA_ROUTE) == 0)
908460473a0SBjoern A. Zeeb 		return (0);
90948321abeSMax Laier 
91048321abeSMax Laier 	if (rtinitflags(target))
91148321abeSMax Laier 		prefix = target->ia_dstaddr.sin_addr;
91248321abeSMax Laier 	else {
91348321abeSMax Laier 		prefix = target->ia_addr.sin_addr;
91448321abeSMax Laier 		mask = target->ia_sockmask.sin_addr;
91548321abeSMax Laier 		prefix.s_addr &= mask.s_addr;
9166e6b3f7cSQing Li 		/* remove arp cache */
9176e6b3f7cSQing Li 		arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
91848321abeSMax Laier 	}
91948321abeSMax Laier 
920603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
92148321abeSMax Laier 		if (rtinitflags(ia))
92248321abeSMax Laier 			p = ia->ia_dstaddr.sin_addr;
92348321abeSMax Laier 		else {
92448321abeSMax Laier 			p = ia->ia_addr.sin_addr;
92548321abeSMax Laier 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
92648321abeSMax Laier 		}
92748321abeSMax Laier 
92848321abeSMax Laier 		if (prefix.s_addr != p.s_addr)
92948321abeSMax Laier 			continue;
93048321abeSMax Laier 
93148321abeSMax Laier 		/*
93248321abeSMax Laier 		 * If we got a matching prefix address, move IFA_ROUTE and
93348321abeSMax Laier 		 * the route itself to it.  Make sure that routing daemons
93448321abeSMax Laier 		 * get a heads-up.
93550bb1704SGleb Smirnoff 		 *
93650bb1704SGleb Smirnoff 		 * XXX: a special case for carp(4) interface
93748321abeSMax Laier 		 */
93850bb1704SGleb Smirnoff 		if ((ia->ia_flags & IFA_ROUTE) == 0
93950bb1704SGleb Smirnoff #ifdef DEV_CARP
94050bb1704SGleb Smirnoff 		    && (ia->ia_ifp->if_type != IFT_CARP)
94150bb1704SGleb Smirnoff #endif
94250bb1704SGleb Smirnoff 							) {
94348321abeSMax Laier 			rtinit(&(target->ia_ifa), (int)RTM_DELETE,
94448321abeSMax Laier 			    rtinitflags(target));
94548321abeSMax Laier 			target->ia_flags &= ~IFA_ROUTE;
94648321abeSMax Laier 
94748321abeSMax Laier 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
94848321abeSMax Laier 			    rtinitflags(ia) | RTF_UP);
94948321abeSMax Laier 			if (error == 0)
95048321abeSMax Laier 				ia->ia_flags |= IFA_ROUTE;
951460473a0SBjoern A. Zeeb 			return (error);
95248321abeSMax Laier 		}
95348321abeSMax Laier 	}
95448321abeSMax Laier 
95548321abeSMax Laier 	/*
95648321abeSMax Laier 	 * As no-one seem to have this prefix, we can remove the route.
95748321abeSMax Laier 	 */
95848321abeSMax Laier 	rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
95948321abeSMax Laier 	target->ia_flags &= ~IFA_ROUTE;
960460473a0SBjoern A. Zeeb 	return (0);
96148321abeSMax Laier }
96248321abeSMax Laier 
96348321abeSMax Laier #undef rtinitflags
964df8bae1dSRodney W. Grimes 
965df8bae1dSRodney W. Grimes /*
966df8bae1dSRodney W. Grimes  * Return 1 if the address might be a local broadcast address.
967df8bae1dSRodney W. Grimes  */
96826f9a767SRodney W. Grimes int
969f2565d68SRobert Watson in_broadcast(struct in_addr in, struct ifnet *ifp)
970df8bae1dSRodney W. Grimes {
971df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
972df8bae1dSRodney W. Grimes 	u_long t;
973df8bae1dSRodney W. Grimes 
974df8bae1dSRodney W. Grimes 	if (in.s_addr == INADDR_BROADCAST ||
975df8bae1dSRodney W. Grimes 	    in.s_addr == INADDR_ANY)
976460473a0SBjoern A. Zeeb 		return (1);
977df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
978460473a0SBjoern A. Zeeb 		return (0);
979df8bae1dSRodney W. Grimes 	t = ntohl(in.s_addr);
980df8bae1dSRodney W. Grimes 	/*
981df8bae1dSRodney W. Grimes 	 * Look through the list of addresses for a match
982df8bae1dSRodney W. Grimes 	 * with a broadcast address.
983df8bae1dSRodney W. Grimes 	 */
984df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa)
985462b86feSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
986df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == AF_INET &&
987df8bae1dSRodney W. Grimes 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
988df8bae1dSRodney W. Grimes 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
989df8bae1dSRodney W. Grimes 		     /*
990df8bae1dSRodney W. Grimes 		      * Check for old-style (host 0) broadcast.
991df8bae1dSRodney W. Grimes 		      */
9928dd27fd6SGuido van Rooij 		     t == ia->ia_subnet || t == ia->ia_net) &&
9938dd27fd6SGuido van Rooij 		     /*
9948dd27fd6SGuido van Rooij 		      * Check for an all one subnetmask. These
9958dd27fd6SGuido van Rooij 		      * only exist when an interface gets a secondary
9968dd27fd6SGuido van Rooij 		      * address.
9978dd27fd6SGuido van Rooij 		      */
9988dd27fd6SGuido van Rooij 		     ia->ia_subnetmask != (u_long)0xffffffff)
999460473a0SBjoern A. Zeeb 			    return (1);
1000df8bae1dSRodney W. Grimes 	return (0);
1001df8bae1dSRodney W. Grimes #undef ia
1002df8bae1dSRodney W. Grimes }
1003ec002feeSBruce M Simpson 
1004df8bae1dSRodney W. Grimes /*
1005b1c53bc9SRobert Watson  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1006b1c53bc9SRobert Watson  */
1007b1c53bc9SRobert Watson void
1008f2565d68SRobert Watson in_ifdetach(struct ifnet *ifp)
1009b1c53bc9SRobert Watson {
10108b615593SMarko Zec 	INIT_VNET_INET(ifp->if_vnet);
1011b1c53bc9SRobert Watson 
1012603724d3SBjoern A. Zeeb 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
1013603724d3SBjoern A. Zeeb 	in_pcbpurgeif0(&V_udbinfo, ifp);
1014ec002feeSBruce M Simpson 	in_purgemaddrs(ifp);
1015b1c53bc9SRobert Watson }
10166e6b3f7cSQing Li 
1017d10910e6SBruce M Simpson /*
1018d10910e6SBruce M Simpson  * Delete all IPv4 multicast address records, and associated link-layer
1019d10910e6SBruce M Simpson  * multicast address records, associated with ifp.
1020d10910e6SBruce M Simpson  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1021d10910e6SBruce M Simpson  */
1022d10910e6SBruce M Simpson static void
1023d10910e6SBruce M Simpson in_purgemaddrs(struct ifnet *ifp)
1024d10910e6SBruce M Simpson {
1025d10910e6SBruce M Simpson 	INIT_VNET_INET(ifp->if_vnet);
1026d10910e6SBruce M Simpson 	LIST_HEAD(,in_multi) purgeinms;
1027d10910e6SBruce M Simpson 	struct in_multi		*inm, *tinm;
1028d10910e6SBruce M Simpson 	struct ifmultiaddr	*ifma;
1029d10910e6SBruce M Simpson 
1030d10910e6SBruce M Simpson 	LIST_INIT(&purgeinms);
1031d10910e6SBruce M Simpson 	IN_MULTI_LOCK();
1032d10910e6SBruce M Simpson 
1033d10910e6SBruce M Simpson 	/*
1034d10910e6SBruce M Simpson 	 * Extract list of in_multi associated with the detaching ifp
1035d10910e6SBruce M Simpson 	 * which the PF_INET layer is about to release.
1036d10910e6SBruce M Simpson 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1037d10910e6SBruce M Simpson 	 * by code further down.
1038d10910e6SBruce M Simpson 	 */
1039d10910e6SBruce M Simpson 	IF_ADDR_LOCK(ifp);
1040d10910e6SBruce M Simpson 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1041d10910e6SBruce M Simpson 		if (ifma->ifma_addr->sa_family != AF_INET)
1042d10910e6SBruce M Simpson 			continue;
1043d10910e6SBruce M Simpson 		inm = (struct in_multi *)ifma->ifma_protospec;
1044d10910e6SBruce M Simpson 		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1045d10910e6SBruce M Simpson 	}
1046d10910e6SBruce M Simpson 	IF_ADDR_UNLOCK(ifp);
1047d10910e6SBruce M Simpson 
1048d10910e6SBruce M Simpson 	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1049d10910e6SBruce M Simpson 		inm_release_locked(inm);
1050d10910e6SBruce M Simpson 		LIST_REMOVE(inm, inm_link);
1051d10910e6SBruce M Simpson 	}
1052d10910e6SBruce M Simpson 	igmp_ifdetach(ifp);
1053d10910e6SBruce M Simpson 
1054d10910e6SBruce M Simpson 	IN_MULTI_UNLOCK();
1055d10910e6SBruce M Simpson }
1056d10910e6SBruce M Simpson 
10576e6b3f7cSQing Li #include <sys/syslog.h>
10586e6b3f7cSQing Li #include <net/if_dl.h>
10596e6b3f7cSQing Li #include <netinet/if_ether.h>
10606e6b3f7cSQing Li 
10616e6b3f7cSQing Li struct in_llentry {
10626e6b3f7cSQing Li 	struct llentry		base;
10636e6b3f7cSQing Li 	struct sockaddr_in	l3_addr4;
10646e6b3f7cSQing Li };
10656e6b3f7cSQing Li 
10666e6b3f7cSQing Li static struct llentry *
10676e6b3f7cSQing Li in_lltable_new(const struct sockaddr *l3addr, u_int flags)
10686e6b3f7cSQing Li {
10696e6b3f7cSQing Li 	struct in_llentry *lle;
10706e6b3f7cSQing Li 
10716e6b3f7cSQing Li 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
10726e6b3f7cSQing Li 	if (lle == NULL)		/* NB: caller generates msg */
10736e6b3f7cSQing Li 		return NULL;
10746e6b3f7cSQing Li 
10756e6b3f7cSQing Li 	callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
10766e6b3f7cSQing Li 	/*
10776e6b3f7cSQing Li 	 * For IPv4 this will trigger "arpresolve" to generate
10786e6b3f7cSQing Li 	 * an ARP request.
10796e6b3f7cSQing Li 	 */
10806e6b3f7cSQing Li 	lle->base.la_expire = time_second; /* mark expired */
10816e6b3f7cSQing Li 	lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
10826e6b3f7cSQing Li 	lle->base.lle_refcnt = 1;
10836e6b3f7cSQing Li 	LLE_LOCK_INIT(&lle->base);
10846e6b3f7cSQing Li 	return &lle->base;
10856e6b3f7cSQing Li }
10866e6b3f7cSQing Li 
10876e6b3f7cSQing Li /*
10886e6b3f7cSQing Li  * Deletes an address from the address table.
10896e6b3f7cSQing Li  * This function is called by the timer functions
10906e6b3f7cSQing Li  * such as arptimer() and nd6_llinfo_timer(), and
10916e6b3f7cSQing Li  * the caller does the locking.
10926e6b3f7cSQing Li  */
10936e6b3f7cSQing Li static void
10946e6b3f7cSQing Li in_lltable_free(struct lltable *llt, struct llentry *lle)
10956e6b3f7cSQing Li {
1096fbc2ca1bSKip Macy 	LLE_WUNLOCK(lle);
1097fbc2ca1bSKip Macy 	LLE_LOCK_DESTROY(lle);
10986e6b3f7cSQing Li 	free(lle, M_LLTABLE);
10996e6b3f7cSQing Li }
11006e6b3f7cSQing Li 
11016e6b3f7cSQing Li static int
11026e6b3f7cSQing Li in_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr)
11036e6b3f7cSQing Li {
11046e6b3f7cSQing Li 	struct rtentry *rt;
11056e6b3f7cSQing Li 
11066e6b3f7cSQing Li 	KASSERT(l3addr->sa_family == AF_INET,
11076e6b3f7cSQing Li 	    ("sin_family %d", l3addr->sa_family));
11086e6b3f7cSQing Li 
11096e6b3f7cSQing Li 	/* XXX rtalloc1 should take a const param */
11106e6b3f7cSQing Li 	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
11116e6b3f7cSQing Li 	if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
11126e6b3f7cSQing Li 		log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
11136e6b3f7cSQing Li 		    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
11146e6b3f7cSQing Li 		if (rt != NULL)
11156e6b3f7cSQing Li 			RTFREE_LOCKED(rt);
11166e6b3f7cSQing Li 		return (EINVAL);
11176e6b3f7cSQing Li 	}
11186e6b3f7cSQing Li 	RTFREE_LOCKED(rt);
11196e6b3f7cSQing Li 	return 0;
11206e6b3f7cSQing Li }
11216e6b3f7cSQing Li 
11226e6b3f7cSQing Li /*
11236e6b3f7cSQing Li  * Return NULL if not found or marked for deletion.
11246e6b3f7cSQing Li  * If found return lle read locked.
11256e6b3f7cSQing Li  */
11266e6b3f7cSQing Li static struct llentry *
11276e6b3f7cSQing Li in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
11286e6b3f7cSQing Li {
11296e6b3f7cSQing Li 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
11306e6b3f7cSQing Li 	struct ifnet *ifp = llt->llt_ifp;
11316e6b3f7cSQing Li 	struct llentry *lle;
11326e6b3f7cSQing Li 	struct llentries *lleh;
11336e6b3f7cSQing Li 	u_int hashkey;
11346e6b3f7cSQing Li 
11356e6b3f7cSQing Li 	IF_AFDATA_LOCK_ASSERT(ifp);
11366e6b3f7cSQing Li 	KASSERT(l3addr->sa_family == AF_INET,
11376e6b3f7cSQing Li 	    ("sin_family %d", l3addr->sa_family));
11386e6b3f7cSQing Li 
11396e6b3f7cSQing Li 	hashkey = sin->sin_addr.s_addr;
11406e6b3f7cSQing Li 	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
11416e6b3f7cSQing Li 	LIST_FOREACH(lle, lleh, lle_next) {
1142dc495497SQing Li 		struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle);
11436e6b3f7cSQing Li 		if (lle->la_flags & LLE_DELETED)
11446e6b3f7cSQing Li 			continue;
1145dc495497SQing Li 		if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
11466e6b3f7cSQing Li 			break;
11476e6b3f7cSQing Li 	}
11486e6b3f7cSQing Li 	if (lle == NULL) {
11496e6b3f7cSQing Li #ifdef DIAGNOSTICS
11506e6b3f7cSQing Li 		if (flags & LLE_DELETE)
11516e6b3f7cSQing Li 			log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
11526e6b3f7cSQing Li #endif
11536e6b3f7cSQing Li 		if (!(flags & LLE_CREATE))
11546e6b3f7cSQing Li 			return (NULL);
11556e6b3f7cSQing Li 		/*
11566e6b3f7cSQing Li 		 * A route that covers the given address must have
11576e6b3f7cSQing Li 		 * been installed 1st because we are doing a resolution,
11586e6b3f7cSQing Li 		 * verify this.
11596e6b3f7cSQing Li 		 */
11606e6b3f7cSQing Li 		if (!(flags & LLE_IFADDR) &&
11616e6b3f7cSQing Li 		    in_lltable_rtcheck(ifp, l3addr) != 0)
11626e6b3f7cSQing Li 			goto done;
11636e6b3f7cSQing Li 
11646e6b3f7cSQing Li 		lle = in_lltable_new(l3addr, flags);
11656e6b3f7cSQing Li 		if (lle == NULL) {
11666e6b3f7cSQing Li 			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
11676e6b3f7cSQing Li 			goto done;
11686e6b3f7cSQing Li 		}
11696e6b3f7cSQing Li 		lle->la_flags = flags & ~LLE_CREATE;
11706e6b3f7cSQing Li 		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
11716e6b3f7cSQing Li 			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
11726e6b3f7cSQing Li 			lle->la_flags |= (LLE_VALID | LLE_STATIC);
11736e6b3f7cSQing Li 		}
11746e6b3f7cSQing Li 
11756e6b3f7cSQing Li 		lle->lle_tbl  = llt;
11766e6b3f7cSQing Li 		lle->lle_head = lleh;
11776e6b3f7cSQing Li 		LIST_INSERT_HEAD(lleh, lle, lle_next);
11786e6b3f7cSQing Li 	} else if (flags & LLE_DELETE) {
11796e6b3f7cSQing Li 		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
11806e6b3f7cSQing Li 			LLE_WLOCK(lle);
11816e6b3f7cSQing Li 			lle->la_flags = LLE_DELETED;
11826e6b3f7cSQing Li 			LLE_WUNLOCK(lle);
11836e6b3f7cSQing Li #ifdef DIAGNOSTICS
11846e6b3f7cSQing Li 			log(LOG_INFO, "ifaddr cache = %p  is deleted\n", lle);
11856e6b3f7cSQing Li #endif
11866e6b3f7cSQing Li 		}
11876e6b3f7cSQing Li 		lle = (void *)-1;
11886e6b3f7cSQing Li 
11896e6b3f7cSQing Li 	}
119042d866ddSBjoern A. Zeeb 	if (LLE_IS_VALID(lle)) {
11916e6b3f7cSQing Li 		if (flags & LLE_EXCLUSIVE)
11926e6b3f7cSQing Li 			LLE_WLOCK(lle);
11936e6b3f7cSQing Li 		else
11946e6b3f7cSQing Li 			LLE_RLOCK(lle);
11956e6b3f7cSQing Li 	}
11966e6b3f7cSQing Li done:
11976e6b3f7cSQing Li 	return (lle);
11986e6b3f7cSQing Li }
11996e6b3f7cSQing Li 
12006e6b3f7cSQing Li static int
12016e6b3f7cSQing Li in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
12026e6b3f7cSQing Li {
12036e6b3f7cSQing Li #define	SIN(lle)	((struct sockaddr_in *) L3_ADDR(lle))
12046e6b3f7cSQing Li 	struct ifnet *ifp = llt->llt_ifp;
12056e6b3f7cSQing Li 	struct llentry *lle;
12066e6b3f7cSQing Li 	/* XXX stack use */
12076e6b3f7cSQing Li 	struct {
12086e6b3f7cSQing Li 		struct rt_msghdr	rtm;
12096e6b3f7cSQing Li 		struct sockaddr_inarp	sin;
12106e6b3f7cSQing Li 		struct sockaddr_dl	sdl;
12116e6b3f7cSQing Li 	} arpc;
12126e6b3f7cSQing Li 	int error, i;
12136e6b3f7cSQing Li 
12146e6b3f7cSQing Li 	/* XXXXX
12156e6b3f7cSQing Li 	 * current IFNET_RLOCK() is mapped to IFNET_WLOCK()
12166e6b3f7cSQing Li 	 * so it is okay to use this ASSERT, change it when
12176e6b3f7cSQing Li 	 * IFNET lock is finalized
12186e6b3f7cSQing Li 	 */
12196e6b3f7cSQing Li 	IFNET_WLOCK_ASSERT();
12206e6b3f7cSQing Li 
12216e6b3f7cSQing Li 	error = 0;
12226e6b3f7cSQing Li 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
12236e6b3f7cSQing Li 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
12246e6b3f7cSQing Li 			struct sockaddr_dl *sdl;
12256e6b3f7cSQing Li 
12266e6b3f7cSQing Li 			/* skip deleted entries */
12276e6b3f7cSQing Li 			if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
12286e6b3f7cSQing Li 				continue;
1229813dd6aeSBjoern A. Zeeb 			/* Skip if jailed and not a valid IP of the prison. */
1230b89e82ddSJamie Gritton 			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1231813dd6aeSBjoern A. Zeeb 				continue;
12326e6b3f7cSQing Li 			/*
12336e6b3f7cSQing Li 			 * produce a msg made of:
12346e6b3f7cSQing Li 			 *  struct rt_msghdr;
12356e6b3f7cSQing Li 			 *  struct sockaddr_inarp; (IPv4)
12366e6b3f7cSQing Li 			 *  struct sockaddr_dl;
12376e6b3f7cSQing Li 			 */
12386e6b3f7cSQing Li 			bzero(&arpc, sizeof(arpc));
12396e6b3f7cSQing Li 			arpc.rtm.rtm_msglen = sizeof(arpc);
1240c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_version = RTM_VERSION;
1241c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_type = RTM_GET;
1242c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_flags = RTF_UP;
1243c0e9a8a1SHartmut Brandt 			arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
12446e6b3f7cSQing Li 			arpc.sin.sin_family = AF_INET;
12456e6b3f7cSQing Li 			arpc.sin.sin_len = sizeof(arpc.sin);
12466e6b3f7cSQing Li 			arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
12476e6b3f7cSQing Li 
12486e6b3f7cSQing Li 			/* publish */
12496e6b3f7cSQing Li 			if (lle->la_flags & LLE_PUB) {
12506e6b3f7cSQing Li 				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
12516e6b3f7cSQing Li 				/* proxy only */
12526e6b3f7cSQing Li 				if (lle->la_flags & LLE_PROXY)
12536e6b3f7cSQing Li 					arpc.sin.sin_other = SIN_PROXY;
12546e6b3f7cSQing Li 			}
12556e6b3f7cSQing Li 
12566e6b3f7cSQing Li 			sdl = &arpc.sdl;
12576e6b3f7cSQing Li 			sdl->sdl_family = AF_LINK;
12586e6b3f7cSQing Li 			sdl->sdl_len = sizeof(*sdl);
12596e6b3f7cSQing Li 			sdl->sdl_alen = ifp->if_addrlen;
12606e6b3f7cSQing Li 			sdl->sdl_index = ifp->if_index;
12616e6b3f7cSQing Li 			sdl->sdl_type = ifp->if_type;
12626e6b3f7cSQing Li 			bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
12636e6b3f7cSQing Li 
12646e6b3f7cSQing Li 			arpc.rtm.rtm_rmx.rmx_expire =
12656e6b3f7cSQing Li 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
12668eca593cSQing Li 			arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
12676e6b3f7cSQing Li 			if (lle->la_flags & LLE_STATIC)
12686e6b3f7cSQing Li 				arpc.rtm.rtm_flags |= RTF_STATIC;
12696e6b3f7cSQing Li 			arpc.rtm.rtm_index = ifp->if_index;
12706e6b3f7cSQing Li 			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
12716e6b3f7cSQing Li 			if (error)
12726e6b3f7cSQing Li 				break;
12736e6b3f7cSQing Li 		}
12746e6b3f7cSQing Li 	}
12756e6b3f7cSQing Li 	return error;
12766e6b3f7cSQing Li #undef SIN
12776e6b3f7cSQing Li }
12786e6b3f7cSQing Li 
12796e6b3f7cSQing Li void *
12806e6b3f7cSQing Li in_domifattach(struct ifnet *ifp)
12816e6b3f7cSQing Li {
1282d10910e6SBruce M Simpson 	struct in_ifinfo *ii;
1283d10910e6SBruce M Simpson 	struct lltable *llt;
12846e6b3f7cSQing Li 
1285d10910e6SBruce M Simpson 	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1286d10910e6SBruce M Simpson 
1287d10910e6SBruce M Simpson 	llt = lltable_init(ifp, AF_INET);
12886e6b3f7cSQing Li 	if (llt != NULL) {
12896e6b3f7cSQing Li 		llt->llt_new = in_lltable_new;
12906e6b3f7cSQing Li 		llt->llt_free = in_lltable_free;
12916e6b3f7cSQing Li 		llt->llt_rtcheck = in_lltable_rtcheck;
12926e6b3f7cSQing Li 		llt->llt_lookup = in_lltable_lookup;
12936e6b3f7cSQing Li 		llt->llt_dump = in_lltable_dump;
12946e6b3f7cSQing Li 	}
1295d10910e6SBruce M Simpson 	ii->ii_llt = llt;
1296d10910e6SBruce M Simpson 
1297d10910e6SBruce M Simpson 	ii->ii_igmp = igmp_domifattach(ifp);
1298d10910e6SBruce M Simpson 
1299d10910e6SBruce M Simpson 	return ii;
13006e6b3f7cSQing Li }
13016e6b3f7cSQing Li 
13026e6b3f7cSQing Li void
1303d10910e6SBruce M Simpson in_domifdetach(struct ifnet *ifp, void *aux)
13046e6b3f7cSQing Li {
1305d10910e6SBruce M Simpson 	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
13066e6b3f7cSQing Li 
1307d10910e6SBruce M Simpson 	igmp_domifdetach(ifp);
1308d10910e6SBruce M Simpson 	lltable_free(ii->ii_llt);
1309d10910e6SBruce M Simpson 	free(ii, M_IFADDR);
13106e6b3f7cSQing Li }
1311