xref: /freebsd/sys/netinet/in.c (revision 6e6b3f7cbc89cf29eafdfa6d1613c6b1a37c364b)
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>
44f6d24a78SPoul-Henning Kamp #include <sys/kernel.h>
45f6d24a78SPoul-Henning Kamp #include <sys/sysctl.h>
46603724d3SBjoern A. Zeeb #include <sys/vimage.h>
47df8bae1dSRodney W. Grimes 
48df8bae1dSRodney W. Grimes #include <net/if.h>
496e6b3f7cSQing Li #include <net/if_llatbl.h>
506a800098SYoshinobu Inoue #include <net/if_types.h>
51df8bae1dSRodney W. Grimes #include <net/route.h>
52df8bae1dSRodney W. Grimes 
53df8bae1dSRodney W. Grimes #include <netinet/in.h>
54df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
55e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h>
5671498f30SBruce M Simpson #include <netinet/ip_var.h>
574b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
5855166637SPoul-Henning Kamp 
594d77a549SAlfred Perlstein static int in_mask2len(struct in_addr *);
604d77a549SAlfred Perlstein static void in_len2mask(struct in_addr *, int);
614d77a549SAlfred Perlstein static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
624d77a549SAlfred Perlstein 	struct ifnet *, struct thread *);
636a800098SYoshinobu Inoue 
6448321abeSMax Laier static int	in_addprefix(struct in_ifaddr *, int);
6548321abeSMax Laier static int	in_scrubprefix(struct in_ifaddr *);
664d77a549SAlfred Perlstein static void	in_socktrim(struct sockaddr_in *);
674d77a549SAlfred Perlstein static int	in_ifinit(struct ifnet *,
684d77a549SAlfred Perlstein 	    struct in_ifaddr *, struct sockaddr_in *, int);
69ec002feeSBruce M Simpson static void	in_purgemaddrs(struct ifnet *);
70df8bae1dSRodney W. Grimes 
7144e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
7244e33a07SMarko Zec static int subnetsarelocal;
7344e33a07SMarko Zec static int sameprefixcarponly;
7444e33a07SMarko Zec extern struct inpcbinfo ripcbinfo;
7544e33a07SMarko Zec #endif
7644e33a07SMarko Zec 
778b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, subnets_are_local,
788b615593SMarko Zec 	CTLFLAG_RW, subnetsarelocal, 0,
798b615593SMarko Zec 	"Treat all subnets as directly connected");
808b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, same_prefix_carp_only,
818b615593SMarko Zec 	CTLFLAG_RW, sameprefixcarponly, 0,
821ae95409SGleb Smirnoff 	"Refuse to create same prefixes on different interfaces");
83477180fbSGarrett Wollman 
84df8bae1dSRodney W. Grimes /*
85df8bae1dSRodney W. Grimes  * Return 1 if an internet address is for a ``local'' host
86df8bae1dSRodney W. Grimes  * (one to which we have a connection).  If subnetsarelocal
87df8bae1dSRodney W. Grimes  * is true, this includes other subnets of the local net.
88df8bae1dSRodney W. Grimes  * Otherwise, it includes only the directly-connected (sub)nets.
89df8bae1dSRodney W. Grimes  */
9026f9a767SRodney W. Grimes int
91f2565d68SRobert Watson in_localaddr(struct in_addr in)
92df8bae1dSRodney W. Grimes {
938b615593SMarko Zec 	INIT_VNET_INET(curvnet);
94df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
95df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
96df8bae1dSRodney W. Grimes 
97603724d3SBjoern A. Zeeb 	if (V_subnetsarelocal) {
98603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
99df8bae1dSRodney W. Grimes 			if ((i & ia->ia_netmask) == ia->ia_net)
100df8bae1dSRodney W. Grimes 				return (1);
101df8bae1dSRodney W. Grimes 	} else {
102603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
103df8bae1dSRodney W. Grimes 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
104df8bae1dSRodney W. Grimes 				return (1);
105df8bae1dSRodney W. Grimes 	}
106df8bae1dSRodney W. Grimes 	return (0);
107df8bae1dSRodney W. Grimes }
108df8bae1dSRodney W. Grimes 
109df8bae1dSRodney W. Grimes /*
1102eccc90bSAndre Oppermann  * Return 1 if an internet address is for the local host and configured
1112eccc90bSAndre Oppermann  * on one of its interfaces.
1122eccc90bSAndre Oppermann  */
1132eccc90bSAndre Oppermann int
114f2565d68SRobert Watson in_localip(struct in_addr in)
1152eccc90bSAndre Oppermann {
1168b615593SMarko Zec 	INIT_VNET_INET(curvnet);
1172eccc90bSAndre Oppermann 	struct in_ifaddr *ia;
1182eccc90bSAndre Oppermann 
1192eccc90bSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
1202eccc90bSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
121460473a0SBjoern A. Zeeb 			return (1);
1222eccc90bSAndre Oppermann 	}
123460473a0SBjoern A. Zeeb 	return (0);
1242eccc90bSAndre Oppermann }
1252eccc90bSAndre Oppermann 
1262eccc90bSAndre Oppermann /*
127df8bae1dSRodney W. Grimes  * Determine whether an IP address is in a reserved set of addresses
128df8bae1dSRodney W. Grimes  * that may not be forwarded, or whether datagrams to that destination
129df8bae1dSRodney W. Grimes  * may be forwarded.
130df8bae1dSRodney W. Grimes  */
13126f9a767SRodney W. Grimes int
132f2565d68SRobert Watson in_canforward(struct in_addr in)
133df8bae1dSRodney W. Grimes {
134df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
135df8bae1dSRodney W. Grimes 	register u_long net;
136df8bae1dSRodney W. Grimes 
137f8429ca2SBruce M Simpson 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
138df8bae1dSRodney W. Grimes 		return (0);
139df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i)) {
140df8bae1dSRodney W. Grimes 		net = i & IN_CLASSA_NET;
141df8bae1dSRodney W. Grimes 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
142df8bae1dSRodney W. Grimes 			return (0);
143df8bae1dSRodney W. Grimes 	}
144df8bae1dSRodney W. Grimes 	return (1);
145df8bae1dSRodney W. Grimes }
146df8bae1dSRodney W. Grimes 
147df8bae1dSRodney W. Grimes /*
148df8bae1dSRodney W. Grimes  * Trim a mask in a sockaddr
149df8bae1dSRodney W. Grimes  */
1500312fbe9SPoul-Henning Kamp static void
151f2565d68SRobert Watson in_socktrim(struct sockaddr_in *ap)
152df8bae1dSRodney W. Grimes {
153df8bae1dSRodney W. Grimes     register char *cplim = (char *) &ap->sin_addr;
154df8bae1dSRodney W. Grimes     register char *cp = (char *) (&ap->sin_addr + 1);
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes     ap->sin_len = 0;
157df00058dSGarrett Wollman     while (--cp >= cplim)
158df8bae1dSRodney W. Grimes 	if (*cp) {
159df8bae1dSRodney W. Grimes 	    (ap)->sin_len = cp - (char *) (ap) + 1;
160df8bae1dSRodney W. Grimes 	    break;
161df8bae1dSRodney W. Grimes 	}
162df8bae1dSRodney W. Grimes }
163df8bae1dSRodney W. Grimes 
1646a800098SYoshinobu Inoue static int
1656a800098SYoshinobu Inoue in_mask2len(mask)
1666a800098SYoshinobu Inoue 	struct in_addr *mask;
1676a800098SYoshinobu Inoue {
1686a800098SYoshinobu Inoue 	int x, y;
1696a800098SYoshinobu Inoue 	u_char *p;
1706a800098SYoshinobu Inoue 
1716a800098SYoshinobu Inoue 	p = (u_char *)mask;
1726a800098SYoshinobu Inoue 	for (x = 0; x < sizeof(*mask); x++) {
1736a800098SYoshinobu Inoue 		if (p[x] != 0xff)
1746a800098SYoshinobu Inoue 			break;
1756a800098SYoshinobu Inoue 	}
1766a800098SYoshinobu Inoue 	y = 0;
1776a800098SYoshinobu Inoue 	if (x < sizeof(*mask)) {
1786a800098SYoshinobu Inoue 		for (y = 0; y < 8; y++) {
1796a800098SYoshinobu Inoue 			if ((p[x] & (0x80 >> y)) == 0)
1806a800098SYoshinobu Inoue 				break;
1816a800098SYoshinobu Inoue 		}
1826a800098SYoshinobu Inoue 	}
183460473a0SBjoern A. Zeeb 	return (x * 8 + y);
1846a800098SYoshinobu Inoue }
1856a800098SYoshinobu Inoue 
1866a800098SYoshinobu Inoue static void
187f2565d68SRobert Watson in_len2mask(struct in_addr *mask, int len)
1886a800098SYoshinobu Inoue {
1896a800098SYoshinobu Inoue 	int i;
1906a800098SYoshinobu Inoue 	u_char *p;
1916a800098SYoshinobu Inoue 
1926a800098SYoshinobu Inoue 	p = (u_char *)mask;
1936a800098SYoshinobu Inoue 	bzero(mask, sizeof(*mask));
1946a800098SYoshinobu Inoue 	for (i = 0; i < len / 8; i++)
1956a800098SYoshinobu Inoue 		p[i] = 0xff;
1966a800098SYoshinobu Inoue 	if (len % 8)
1976a800098SYoshinobu Inoue 		p[i] = (0xff00 >> (len % 8)) & 0xff;
1986a800098SYoshinobu Inoue }
1996a800098SYoshinobu Inoue 
200df8bae1dSRodney W. Grimes /*
201df8bae1dSRodney W. Grimes  * Generic internet control operations (ioctl's).
202df8bae1dSRodney W. Grimes  * Ifp is 0 if not an interface-specific ioctl.
203df8bae1dSRodney W. Grimes  */
204df8bae1dSRodney W. Grimes /* ARGSUSED */
20526f9a767SRodney W. Grimes int
206f2565d68SRobert Watson in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
207f2565d68SRobert Watson     struct thread *td)
208df8bae1dSRodney W. Grimes {
2098b615593SMarko Zec 	INIT_VNET_INET(curvnet); /* both so and ifp can be NULL here! */
210df8bae1dSRodney W. Grimes 	register struct ifreq *ifr = (struct ifreq *)data;
211460473a0SBjoern A. Zeeb 	register struct in_ifaddr *ia, *iap;
212df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
213f7e083afSBruce M Simpson 	struct in_addr allhosts_addr;
214ca925d9cSJonathan Lemon 	struct in_addr dst;
215df8bae1dSRodney W. Grimes 	struct in_ifaddr *oia;
216df8bae1dSRodney W. Grimes 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
217df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
2180f02fdacSBrian Somers 	int error, hostIsNew, iaIsNew, maskIsNew, s;
219f7e083afSBruce M Simpson 	int iaIsFirst;
2200f02fdacSBrian Somers 
221460473a0SBjoern A. Zeeb 	ia = NULL;
222f7e083afSBruce M Simpson 	iaIsFirst = 0;
2230f02fdacSBrian Somers 	iaIsNew = 0;
224f7e083afSBruce M Simpson 	allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
225df8bae1dSRodney W. Grimes 
2266a800098SYoshinobu Inoue 	switch (cmd) {
2276a800098SYoshinobu Inoue 	case SIOCALIFADDR:
228acd3428bSRobert Watson 		if (td != NULL) {
229acd3428bSRobert Watson 			error = priv_check(td, PRIV_NET_ADDIFADDR);
230acd3428bSRobert Watson 			if (error)
231acd3428bSRobert Watson 				return (error);
232acd3428bSRobert Watson 		}
233460473a0SBjoern A. Zeeb 		if (ifp == NULL)
234460473a0SBjoern A. Zeeb 			return (EINVAL);
235acd3428bSRobert Watson 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
236acd3428bSRobert Watson 
2376a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
238acd3428bSRobert Watson 		if (td != NULL) {
239acd3428bSRobert Watson 			error = priv_check(td, PRIV_NET_DELIFADDR);
240acd3428bSRobert Watson 			if (error)
241acd3428bSRobert Watson 				return (error);
242acd3428bSRobert Watson 		}
243460473a0SBjoern A. Zeeb 		if (ifp == NULL)
244460473a0SBjoern A. Zeeb 			return (EINVAL);
245acd3428bSRobert Watson 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
246acd3428bSRobert Watson 
2476a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
248460473a0SBjoern A. Zeeb 		if (ifp == NULL)
249460473a0SBjoern A. Zeeb 			return (EINVAL);
250b40ce416SJulian Elischer 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
2516a800098SYoshinobu Inoue 	}
2526a800098SYoshinobu Inoue 
253df8bae1dSRodney W. Grimes 	/*
254df8bae1dSRodney W. Grimes 	 * Find address for this interface, if it exists.
255ac0aa473SBill Fenner 	 *
256ac0aa473SBill Fenner 	 * If an alias address was specified, find that one instead of
257ca925d9cSJonathan Lemon 	 * the first one on the interface, if possible.
258df8bae1dSRodney W. Grimes 	 */
259460473a0SBjoern A. Zeeb 	if (ifp != NULL) {
260ca925d9cSJonathan Lemon 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
261ca925d9cSJonathan Lemon 		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
262ca925d9cSJonathan Lemon 			if (iap->ia_ifp == ifp &&
263ca925d9cSJonathan Lemon 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
264ac0aa473SBill Fenner 				ia = iap;
265df8bae1dSRodney W. Grimes 				break;
266ca925d9cSJonathan Lemon 			}
267ca925d9cSJonathan Lemon 		if (ia == NULL)
268ca925d9cSJonathan Lemon 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
269ca925d9cSJonathan Lemon 				iap = ifatoia(ifa);
270ca925d9cSJonathan Lemon 				if (iap->ia_addr.sin_family == AF_INET) {
271ac0aa473SBill Fenner 					ia = iap;
272ac0aa473SBill Fenner 					break;
273ac0aa473SBill Fenner 				}
274ac0aa473SBill Fenner 			}
275f7e083afSBruce M Simpson 		if (ia == NULL)
276f7e083afSBruce M Simpson 			iaIsFirst = 1;
277ca925d9cSJonathan Lemon 	}
278df8bae1dSRodney W. Grimes 
279df8bae1dSRodney W. Grimes 	switch (cmd) {
280df8bae1dSRodney W. Grimes 
281df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
282df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
283460473a0SBjoern A. Zeeb 		if (ifp == NULL)
2846572231dSEivind Eklund 			return (EADDRNOTAVAIL);
2851067217dSGarrett Wollman 		if (ifra->ifra_addr.sin_family == AF_INET) {
286fc2ffbe6SPoul-Henning Kamp 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
287df8bae1dSRodney W. Grimes 				if (ia->ia_ifp == ifp  &&
288df8bae1dSRodney W. Grimes 				    ia->ia_addr.sin_addr.s_addr ==
289df8bae1dSRodney W. Grimes 				    ifra->ifra_addr.sin_addr.s_addr)
290df8bae1dSRodney W. Grimes 					break;
291df8bae1dSRodney W. Grimes 			}
2921067217dSGarrett Wollman 			if ((ifp->if_flags & IFF_POINTOPOINT)
2931067217dSGarrett Wollman 			    && (cmd == SIOCAIFADDR)
2941067217dSGarrett Wollman 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
2951067217dSGarrett Wollman 				== INADDR_ANY)) {
296460473a0SBjoern A. Zeeb 				return (EDESTADDRREQ);
2971067217dSGarrett Wollman 			}
2981067217dSGarrett Wollman 		}
299460473a0SBjoern A. Zeeb 		if (cmd == SIOCDIFADDR && ia == NULL)
300df8bae1dSRodney W. Grimes 			return (EADDRNOTAVAIL);
301df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
302df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
303df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
304df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
305acd3428bSRobert Watson 		if (td != NULL) {
306107d1244SBjoern A. Zeeb 			error = priv_check(td, (cmd == SIOCDIFADDR) ?
307107d1244SBjoern A. Zeeb 			    PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
308acd3428bSRobert Watson 			if (error)
309acd3428bSRobert Watson 				return (error);
310acd3428bSRobert Watson 		}
311df8bae1dSRodney W. Grimes 
312460473a0SBjoern A. Zeeb 		if (ifp == NULL)
3136572231dSEivind Eklund 			return (EADDRNOTAVAIL);
314460473a0SBjoern A. Zeeb 		if (ia == NULL) {
31559562606SGarrett Wollman 			ia = (struct in_ifaddr *)
316a163d034SWarner Losh 				malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
317460473a0SBjoern A. Zeeb 			if (ia == NULL)
318df8bae1dSRodney W. Grimes 				return (ENOBUFS);
319c655b7c4SDavid Greenman 			/*
320c655b7c4SDavid Greenman 			 * Protect from ipintr() traversing address list
321c655b7c4SDavid Greenman 			 * while we're modifying it.
322c655b7c4SDavid Greenman 			 */
323c655b7c4SDavid Greenman 			s = splnet();
32419fc74fbSJeffrey Hsu 			ifa = &ia->ia_ifa;
32519fc74fbSJeffrey Hsu 			IFA_LOCK_INIT(ifa);
32659562606SGarrett Wollman 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
32759562606SGarrett Wollman 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
32859562606SGarrett Wollman 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
32919fc74fbSJeffrey Hsu 			ifa->ifa_refcnt = 1;
33019fc74fbSJeffrey Hsu 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
33119fc74fbSJeffrey Hsu 
332df8bae1dSRodney W. Grimes 			ia->ia_sockmask.sin_len = 8;
333bc183b3fSDag-Erling Smørgrav 			ia->ia_sockmask.sin_family = AF_INET;
334df8bae1dSRodney W. Grimes 			if (ifp->if_flags & IFF_BROADCAST) {
335df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
336df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_family = AF_INET;
337df8bae1dSRodney W. Grimes 			}
338df8bae1dSRodney W. Grimes 			ia->ia_ifp = ifp;
339f3d30eb2SGleb Smirnoff 
340603724d3SBjoern A. Zeeb 			TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
341c655b7c4SDavid Greenman 			splx(s);
3420f02fdacSBrian Somers 			iaIsNew = 1;
343df8bae1dSRodney W. Grimes 		}
344df8bae1dSRodney W. Grimes 		break;
345df8bae1dSRodney W. Grimes 
346df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
347acd3428bSRobert Watson 		if (td != NULL) {
348acd3428bSRobert Watson 			error = priv_check(td, PRIV_NET_ADDIFADDR);
349acd3428bSRobert Watson 			if (error)
350acd3428bSRobert Watson 				return (error);
351acd3428bSRobert Watson 		}
352df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
353df8bae1dSRodney W. Grimes 
354df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
355df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
356df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
357df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
358460473a0SBjoern A. Zeeb 		if (ia == NULL)
359df8bae1dSRodney W. Grimes 			return (EADDRNOTAVAIL);
360df8bae1dSRodney W. Grimes 		break;
361df8bae1dSRodney W. Grimes 	}
362df8bae1dSRodney W. Grimes 	switch (cmd) {
363df8bae1dSRodney W. Grimes 
364df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
365df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
3660f02fdacSBrian Somers 		return (0);
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
369df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
370df8bae1dSRodney W. Grimes 			return (EINVAL);
371df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
3720f02fdacSBrian Somers 		return (0);
373df8bae1dSRodney W. Grimes 
374df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
375df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
376df8bae1dSRodney W. Grimes 			return (EINVAL);
377df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
3780f02fdacSBrian Somers 		return (0);
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
381df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
3820f02fdacSBrian Somers 		return (0);
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
385df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
386df8bae1dSRodney W. Grimes 			return (EINVAL);
387df8bae1dSRodney W. Grimes 		oldaddr = ia->ia_dstaddr;
388df8bae1dSRodney W. Grimes 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
389460473a0SBjoern A. Zeeb 		if (ifp->if_ioctl != NULL) {
390ba5da2a0SIan Dowse 			IFF_LOCKGIANT(ifp);
391ba5da2a0SIan Dowse 			error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
392ba5da2a0SIan Dowse 			    (caddr_t)ia);
393ba5da2a0SIan Dowse 			IFF_UNLOCKGIANT(ifp);
394ba5da2a0SIan Dowse 			if (error) {
395df8bae1dSRodney W. Grimes 				ia->ia_dstaddr = oldaddr;
396df8bae1dSRodney W. Grimes 				return (error);
397df8bae1dSRodney W. Grimes 			}
398ba5da2a0SIan Dowse 		}
399df8bae1dSRodney W. Grimes 		if (ia->ia_flags & IFA_ROUTE) {
400df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
401df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
402df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr =
403df8bae1dSRodney W. Grimes 					(struct sockaddr *)&ia->ia_dstaddr;
404df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
405df8bae1dSRodney W. Grimes 		}
4060f02fdacSBrian Somers 		return (0);
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
409df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
410df8bae1dSRodney W. Grimes 			return (EINVAL);
411df8bae1dSRodney W. Grimes 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
4120f02fdacSBrian Somers 		return (0);
413df8bae1dSRodney W. Grimes 
414df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
4150f02fdacSBrian Somers 		error = in_ifinit(ifp, ia,
4160f02fdacSBrian Somers 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
4170f02fdacSBrian Somers 		if (error != 0 && iaIsNew)
4180f02fdacSBrian Somers 			break;
419f7e083afSBruce M Simpson 		if (error == 0) {
420f7e083afSBruce M Simpson 			if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
421f7e083afSBruce M Simpson 				in_addmulti(&allhosts_addr, ifp);
42225a4adceSMax Laier 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
423f7e083afSBruce M Simpson 		}
4240f02fdacSBrian Somers 		return (0);
425df8bae1dSRodney W. Grimes 
426df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
427bc183b3fSDag-Erling Smørgrav 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
428bc183b3fSDag-Erling Smørgrav 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
4290f02fdacSBrian Somers 		return (0);
430df8bae1dSRodney W. Grimes 
431df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
432df8bae1dSRodney W. Grimes 		maskIsNew = 0;
433df8bae1dSRodney W. Grimes 		hostIsNew = 1;
434df8bae1dSRodney W. Grimes 		error = 0;
435df8bae1dSRodney W. Grimes 		if (ia->ia_addr.sin_family == AF_INET) {
436df8bae1dSRodney W. Grimes 			if (ifra->ifra_addr.sin_len == 0) {
437df8bae1dSRodney W. Grimes 				ifra->ifra_addr = ia->ia_addr;
438df8bae1dSRodney W. Grimes 				hostIsNew = 0;
439df8bae1dSRodney W. Grimes 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
440df8bae1dSRodney W. Grimes 					       ia->ia_addr.sin_addr.s_addr)
441df8bae1dSRodney W. Grimes 				hostIsNew = 0;
442df8bae1dSRodney W. Grimes 		}
443df8bae1dSRodney W. Grimes 		if (ifra->ifra_mask.sin_len) {
444df8bae1dSRodney W. Grimes 			in_ifscrub(ifp, ia);
445df8bae1dSRodney W. Grimes 			ia->ia_sockmask = ifra->ifra_mask;
446bc183b3fSDag-Erling Smørgrav 			ia->ia_sockmask.sin_family = AF_INET;
447df8bae1dSRodney W. Grimes 			ia->ia_subnetmask =
448df8bae1dSRodney W. Grimes 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
449df8bae1dSRodney W. Grimes 			maskIsNew = 1;
450df8bae1dSRodney W. Grimes 		}
451df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
452df8bae1dSRodney W. Grimes 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
453df8bae1dSRodney W. Grimes 			in_ifscrub(ifp, ia);
454df8bae1dSRodney W. Grimes 			ia->ia_dstaddr = ifra->ifra_dstaddr;
455df8bae1dSRodney W. Grimes 			maskIsNew  = 1; /* We lie; but the effect's the same */
456df8bae1dSRodney W. Grimes 		}
457df8bae1dSRodney W. Grimes 		if (ifra->ifra_addr.sin_family == AF_INET &&
458df8bae1dSRodney W. Grimes 		    (hostIsNew || maskIsNew))
459df8bae1dSRodney W. Grimes 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
4600f02fdacSBrian Somers 		if (error != 0 && iaIsNew)
4610f02fdacSBrian Somers 			break;
4620f02fdacSBrian Somers 
463df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) &&
464df8bae1dSRodney W. Grimes 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
465df8bae1dSRodney W. Grimes 			ia->ia_broadaddr = ifra->ifra_broadaddr;
466f7e083afSBruce M Simpson 		if (error == 0) {
467f7e083afSBruce M Simpson 			if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
468f7e083afSBruce M Simpson 				in_addmulti(&allhosts_addr, ifp);
46925a4adceSMax Laier 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
470f7e083afSBruce M Simpson 		}
471df8bae1dSRodney W. Grimes 		return (error);
472df8bae1dSRodney W. Grimes 
473df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
474089cdfadSRuslan Ermilov 		/*
475089cdfadSRuslan Ermilov 		 * in_ifscrub kills the interface route.
476089cdfadSRuslan Ermilov 		 */
477df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
478c655b7c4SDavid Greenman 		/*
479089cdfadSRuslan Ermilov 		 * in_ifadown gets rid of all the rest of
480089cdfadSRuslan Ermilov 		 * the routes.  This is not quite the right
481089cdfadSRuslan Ermilov 		 * thing to do, but at least if we are running
482089cdfadSRuslan Ermilov 		 * a routing process they will come back.
483089cdfadSRuslan Ermilov 		 */
48491854268SRuslan Ermilov 		in_ifadown(&ia->ia_ifa, 1);
48525a4adceSMax Laier 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
4860f02fdacSBrian Somers 		error = 0;
487df8bae1dSRodney W. Grimes 		break;
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes 	default:
490460473a0SBjoern A. Zeeb 		if (ifp == NULL || ifp->if_ioctl == NULL)
491df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
492ba5da2a0SIan Dowse 		IFF_LOCKGIANT(ifp);
493ba5da2a0SIan Dowse 		error = (*ifp->if_ioctl)(ifp, cmd, data);
494ba5da2a0SIan Dowse 		IFF_UNLOCKGIANT(ifp);
495ba5da2a0SIan Dowse 		return (error);
496df8bae1dSRodney W. Grimes 	}
4970f02fdacSBrian Somers 
4980f02fdacSBrian Somers 	/*
4990f02fdacSBrian Somers 	 * Protect from ipintr() traversing address list while we're modifying
5000f02fdacSBrian Somers 	 * it.
5010f02fdacSBrian Somers 	 */
5020f02fdacSBrian Somers 	s = splnet();
5030f02fdacSBrian Somers 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
504603724d3SBjoern A. Zeeb 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
505f7e083afSBruce M Simpson 	if (ia->ia_addr.sin_family == AF_INET) {
5060f02fdacSBrian Somers 		LIST_REMOVE(ia, ia_hash);
507f7e083afSBruce M Simpson 		/*
508f7e083afSBruce M Simpson 		 * If this is the last IPv4 address configured on this
509f7e083afSBruce M Simpson 		 * interface, leave the all-hosts group.
510f7e083afSBruce M Simpson 		 * XXX: This is quite ugly because of locking and structure.
511f7e083afSBruce M Simpson 		 */
512f7e083afSBruce M Simpson 		oia = NULL;
513f7e083afSBruce M Simpson 		IFP_TO_IA(ifp, oia);
514f7e083afSBruce M Simpson 		if (oia == NULL) {
515f7e083afSBruce M Simpson 			struct in_multi *inm;
516f7e083afSBruce M Simpson 
517f7e083afSBruce M Simpson 			IFF_LOCKGIANT(ifp);
518f7e083afSBruce M Simpson 			IN_MULTI_LOCK();
519f7e083afSBruce M Simpson 			IN_LOOKUP_MULTI(allhosts_addr, ifp, inm);
520f7e083afSBruce M Simpson 			if (inm != NULL)
521f7e083afSBruce M Simpson 				in_delmulti_locked(inm);
522f7e083afSBruce M Simpson 			IN_MULTI_UNLOCK();
523f7e083afSBruce M Simpson 			IFF_UNLOCKGIANT(ifp);
524f7e083afSBruce M Simpson 		}
525f7e083afSBruce M Simpson 	}
5260f02fdacSBrian Somers 	IFAFREE(&ia->ia_ifa);
5270f02fdacSBrian Somers 	splx(s);
5280f02fdacSBrian Somers 
5290f02fdacSBrian Somers 	return (error);
530df8bae1dSRodney W. Grimes }
531df8bae1dSRodney W. Grimes 
532df8bae1dSRodney W. Grimes /*
5336a800098SYoshinobu Inoue  * SIOC[GAD]LIFADDR.
5346a800098SYoshinobu Inoue  *	SIOCGLIFADDR: get first address. (?!?)
5356a800098SYoshinobu Inoue  *	SIOCGLIFADDR with IFLR_PREFIX:
5366a800098SYoshinobu Inoue  *		get first address that matches the specified prefix.
5376a800098SYoshinobu Inoue  *	SIOCALIFADDR: add the specified address.
5386a800098SYoshinobu Inoue  *	SIOCALIFADDR with IFLR_PREFIX:
5396a800098SYoshinobu Inoue  *		EINVAL since we can't deduce hostid part of the address.
5406a800098SYoshinobu Inoue  *	SIOCDLIFADDR: delete the specified address.
5416a800098SYoshinobu Inoue  *	SIOCDLIFADDR with IFLR_PREFIX:
5426a800098SYoshinobu Inoue  *		delete the first address that matches the specified prefix.
5436a800098SYoshinobu Inoue  * return values:
5446a800098SYoshinobu Inoue  *	EINVAL on invalid parameters
5456a800098SYoshinobu Inoue  *	EADDRNOTAVAIL on prefix match failed/specified address not found
5466a800098SYoshinobu Inoue  *	other values may be returned from in_ioctl()
5476a800098SYoshinobu Inoue  */
5486a800098SYoshinobu Inoue static int
549f2565d68SRobert Watson in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
550f2565d68SRobert Watson     struct ifnet *ifp, struct thread *td)
5516a800098SYoshinobu Inoue {
5526a800098SYoshinobu Inoue 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
5536a800098SYoshinobu Inoue 	struct ifaddr *ifa;
5546a800098SYoshinobu Inoue 
5556a800098SYoshinobu Inoue 	/* sanity checks */
556460473a0SBjoern A. Zeeb 	if (data == NULL || ifp == NULL) {
5576a800098SYoshinobu Inoue 		panic("invalid argument to in_lifaddr_ioctl");
5586a800098SYoshinobu Inoue 		/*NOTRECHED*/
5596a800098SYoshinobu Inoue 	}
5606a800098SYoshinobu Inoue 
5616a800098SYoshinobu Inoue 	switch (cmd) {
5626a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
5636a800098SYoshinobu Inoue 		/* address must be specified on GET with IFLR_PREFIX */
5646a800098SYoshinobu Inoue 		if ((iflr->flags & IFLR_PREFIX) == 0)
5656a800098SYoshinobu Inoue 			break;
5666a800098SYoshinobu Inoue 		/*FALLTHROUGH*/
5676a800098SYoshinobu Inoue 	case SIOCALIFADDR:
5686a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
5696a800098SYoshinobu Inoue 		/* address must be specified on ADD and DELETE */
5705d60ed0eSYoshinobu Inoue 		if (iflr->addr.ss_family != AF_INET)
571460473a0SBjoern A. Zeeb 			return (EINVAL);
5725d60ed0eSYoshinobu Inoue 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
573460473a0SBjoern A. Zeeb 			return (EINVAL);
5746a800098SYoshinobu Inoue 		/* XXX need improvement */
5755d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family
5765d60ed0eSYoshinobu Inoue 		 && iflr->dstaddr.ss_family != AF_INET)
577460473a0SBjoern A. Zeeb 			return (EINVAL);
5785d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family
5795d60ed0eSYoshinobu Inoue 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
580460473a0SBjoern A. Zeeb 			return (EINVAL);
5816a800098SYoshinobu Inoue 		break;
5826a800098SYoshinobu Inoue 	default: /*shouldn't happen*/
583460473a0SBjoern A. Zeeb 		return (EOPNOTSUPP);
5846a800098SYoshinobu Inoue 	}
5856a800098SYoshinobu Inoue 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
586460473a0SBjoern A. Zeeb 		return (EINVAL);
5876a800098SYoshinobu Inoue 
5886a800098SYoshinobu Inoue 	switch (cmd) {
5896a800098SYoshinobu Inoue 	case SIOCALIFADDR:
5906a800098SYoshinobu Inoue 	    {
5916a800098SYoshinobu Inoue 		struct in_aliasreq ifra;
5926a800098SYoshinobu Inoue 
5936a800098SYoshinobu Inoue 		if (iflr->flags & IFLR_PREFIX)
594460473a0SBjoern A. Zeeb 			return (EINVAL);
5956a800098SYoshinobu Inoue 
5966a800098SYoshinobu Inoue 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
5976a800098SYoshinobu Inoue 		bzero(&ifra, sizeof(ifra));
5986a800098SYoshinobu Inoue 		bcopy(iflr->iflr_name, ifra.ifra_name,
5996a800098SYoshinobu Inoue 			sizeof(ifra.ifra_name));
6006a800098SYoshinobu Inoue 
6015d60ed0eSYoshinobu Inoue 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
6026a800098SYoshinobu Inoue 
6035d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family) {	/*XXX*/
6046a800098SYoshinobu Inoue 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
6055d60ed0eSYoshinobu Inoue 				iflr->dstaddr.ss_len);
6066a800098SYoshinobu Inoue 		}
6076a800098SYoshinobu Inoue 
6086a800098SYoshinobu Inoue 		ifra.ifra_mask.sin_family = AF_INET;
6096a800098SYoshinobu Inoue 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
6106a800098SYoshinobu Inoue 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
6116a800098SYoshinobu Inoue 
612460473a0SBjoern A. Zeeb 		return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
6136a800098SYoshinobu Inoue 	    }
6146a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
6156a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
6166a800098SYoshinobu Inoue 	    {
6176a800098SYoshinobu Inoue 		struct in_ifaddr *ia;
6186a800098SYoshinobu Inoue 		struct in_addr mask, candidate, match;
6196a800098SYoshinobu Inoue 		struct sockaddr_in *sin;
6206a800098SYoshinobu Inoue 
6216a800098SYoshinobu Inoue 		bzero(&mask, sizeof(mask));
622fbdd20a1SMatt Jacob 		bzero(&match, sizeof(match));
6236a800098SYoshinobu Inoue 		if (iflr->flags & IFLR_PREFIX) {
6246a800098SYoshinobu Inoue 			/* lookup a prefix rather than address. */
6256a800098SYoshinobu Inoue 			in_len2mask(&mask, iflr->prefixlen);
6266a800098SYoshinobu Inoue 
6276a800098SYoshinobu Inoue 			sin = (struct sockaddr_in *)&iflr->addr;
6286a800098SYoshinobu Inoue 			match.s_addr = sin->sin_addr.s_addr;
6296a800098SYoshinobu Inoue 			match.s_addr &= mask.s_addr;
6306a800098SYoshinobu Inoue 
6316a800098SYoshinobu Inoue 			/* if you set extra bits, that's wrong */
6326a800098SYoshinobu Inoue 			if (match.s_addr != sin->sin_addr.s_addr)
633460473a0SBjoern A. Zeeb 				return (EINVAL);
6346a800098SYoshinobu Inoue 
6356a800098SYoshinobu Inoue 		} else {
6366a800098SYoshinobu Inoue 			/* on getting an address, take the 1st match */
6376a800098SYoshinobu Inoue 			/* on deleting an address, do exact match */
638fbdd20a1SMatt Jacob 			if (cmd != SIOCGLIFADDR) {
6396a800098SYoshinobu Inoue 				in_len2mask(&mask, 32);
6406a800098SYoshinobu Inoue 				sin = (struct sockaddr_in *)&iflr->addr;
6416a800098SYoshinobu Inoue 				match.s_addr = sin->sin_addr.s_addr;
6426a800098SYoshinobu Inoue 			}
6436a800098SYoshinobu Inoue 		}
6446a800098SYoshinobu Inoue 
6456a800098SYoshinobu Inoue 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
6466a800098SYoshinobu Inoue 			if (ifa->ifa_addr->sa_family != AF_INET6)
6476a800098SYoshinobu Inoue 				continue;
648fbdd20a1SMatt Jacob 			if (match.s_addr == 0)
6496a800098SYoshinobu Inoue 				break;
6506a800098SYoshinobu Inoue 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
6516a800098SYoshinobu Inoue 			candidate.s_addr &= mask.s_addr;
6526a800098SYoshinobu Inoue 			if (candidate.s_addr == match.s_addr)
6536a800098SYoshinobu Inoue 				break;
6546a800098SYoshinobu Inoue 		}
655460473a0SBjoern A. Zeeb 		if (ifa == NULL)
656460473a0SBjoern A. Zeeb 			return (EADDRNOTAVAIL);
6576a800098SYoshinobu Inoue 		ia = (struct in_ifaddr *)ifa;
6586a800098SYoshinobu Inoue 
6596a800098SYoshinobu Inoue 		if (cmd == SIOCGLIFADDR) {
6606a800098SYoshinobu Inoue 			/* fill in the if_laddrreq structure */
6616a800098SYoshinobu Inoue 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
6626a800098SYoshinobu Inoue 
6636a800098SYoshinobu Inoue 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
6646a800098SYoshinobu Inoue 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
6656a800098SYoshinobu Inoue 					ia->ia_dstaddr.sin_len);
6666a800098SYoshinobu Inoue 			} else
6676a800098SYoshinobu Inoue 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
6686a800098SYoshinobu Inoue 
6696a800098SYoshinobu Inoue 			iflr->prefixlen =
6706a800098SYoshinobu Inoue 				in_mask2len(&ia->ia_sockmask.sin_addr);
6716a800098SYoshinobu Inoue 
6726a800098SYoshinobu Inoue 			iflr->flags = 0;	/*XXX*/
6736a800098SYoshinobu Inoue 
674460473a0SBjoern A. Zeeb 			return (0);
6756a800098SYoshinobu Inoue 		} else {
6766a800098SYoshinobu Inoue 			struct in_aliasreq ifra;
6776a800098SYoshinobu Inoue 
6786a800098SYoshinobu Inoue 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
6796a800098SYoshinobu Inoue 			bzero(&ifra, sizeof(ifra));
6806a800098SYoshinobu Inoue 			bcopy(iflr->iflr_name, ifra.ifra_name,
6816a800098SYoshinobu Inoue 				sizeof(ifra.ifra_name));
6826a800098SYoshinobu Inoue 
6836a800098SYoshinobu Inoue 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
6846a800098SYoshinobu Inoue 				ia->ia_addr.sin_len);
6856a800098SYoshinobu Inoue 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
6866a800098SYoshinobu Inoue 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
6876a800098SYoshinobu Inoue 					ia->ia_dstaddr.sin_len);
6886a800098SYoshinobu Inoue 			}
6896a800098SYoshinobu Inoue 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
6906a800098SYoshinobu Inoue 				ia->ia_sockmask.sin_len);
6916a800098SYoshinobu Inoue 
692460473a0SBjoern A. Zeeb 			return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
693460473a0SBjoern A. Zeeb 			    ifp, td));
6946a800098SYoshinobu Inoue 		}
6956a800098SYoshinobu Inoue 	    }
6966a800098SYoshinobu Inoue 	}
6976a800098SYoshinobu Inoue 
698460473a0SBjoern A. Zeeb 	return (EOPNOTSUPP);	/*just for safety*/
6996a800098SYoshinobu Inoue }
7006a800098SYoshinobu Inoue 
7016a800098SYoshinobu Inoue /*
702df8bae1dSRodney W. Grimes  * Delete any existing route for an interface.
703df8bae1dSRodney W. Grimes  */
70439191c8eSGarrett Wollman void
705f2565d68SRobert Watson in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
706df8bae1dSRodney W. Grimes {
707f2565d68SRobert Watson 
70848321abeSMax Laier 	in_scrubprefix(ia);
709df8bae1dSRodney W. Grimes }
710df8bae1dSRodney W. Grimes 
711df8bae1dSRodney W. Grimes /*
712df8bae1dSRodney W. Grimes  * Initialize an interface's internet address
713df8bae1dSRodney W. Grimes  * and routing table entry.
714df8bae1dSRodney W. Grimes  */
7150312fbe9SPoul-Henning Kamp static int
716f2565d68SRobert Watson in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
717f2565d68SRobert Watson     int scrub)
718df8bae1dSRodney W. Grimes {
7198b615593SMarko Zec 	INIT_VNET_INET(ifp->if_vnet);
720df8bae1dSRodney W. Grimes 	register u_long i = ntohl(sin->sin_addr.s_addr);
721df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
7225a43847dSBrian Somers 	int s = splimp(), flags = RTF_UP, error = 0;
723df8bae1dSRodney W. Grimes 
724df8bae1dSRodney W. Grimes 	oldaddr = ia->ia_addr;
7252754d95dSSUZUKI Shinsuke 	if (oldaddr.sin_family == AF_INET)
7262754d95dSSUZUKI Shinsuke 		LIST_REMOVE(ia, ia_hash);
727df8bae1dSRodney W. Grimes 	ia->ia_addr = *sin;
7282754d95dSSUZUKI Shinsuke 	if (ia->ia_addr.sin_family == AF_INET)
7292754d95dSSUZUKI Shinsuke 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
7302754d95dSSUZUKI Shinsuke 		    ia, ia_hash);
731df8bae1dSRodney W. Grimes 	/*
732df8bae1dSRodney W. Grimes 	 * Give the interface a chance to initialize
733df8bae1dSRodney W. Grimes 	 * if this is its first address,
734df8bae1dSRodney W. Grimes 	 * and to validate the address if necessary.
735df8bae1dSRodney W. Grimes 	 */
736460473a0SBjoern A. Zeeb 	if (ifp->if_ioctl != NULL) {
737ba5da2a0SIan Dowse 		IFF_LOCKGIANT(ifp);
738ba5da2a0SIan Dowse 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
739ba5da2a0SIan Dowse 		IFF_UNLOCKGIANT(ifp);
740ba5da2a0SIan Dowse 		if (error) {
741df8bae1dSRodney W. Grimes 			splx(s);
7422754d95dSSUZUKI Shinsuke 			/* LIST_REMOVE(ia, ia_hash) is done in in_control */
743df8bae1dSRodney W. Grimes 			ia->ia_addr = oldaddr;
74422c819a7SJonathan Lemon 			if (ia->ia_addr.sin_family == AF_INET)
745ba5da2a0SIan Dowse 				LIST_INSERT_HEAD(INADDR_HASH(
746ba5da2a0SIan Dowse 				    ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
747cf77b848SOleksandr Tymoshenko 			else
748cf77b848SOleksandr Tymoshenko 				/*
749cf77b848SOleksandr Tymoshenko 				 * If oldaddr family is not AF_INET (e.g.
750cf77b848SOleksandr Tymoshenko 				 * interface has been just created) in_control
751cf77b848SOleksandr Tymoshenko 				 * does not call LIST_REMOVE, and we end up
752cf77b848SOleksandr Tymoshenko 				 * with bogus ia entries in hash
753cf77b848SOleksandr Tymoshenko 				 */
754cf77b848SOleksandr Tymoshenko 				LIST_REMOVE(ia, ia_hash);
7552754d95dSSUZUKI Shinsuke 			return (error);
7562754d95dSSUZUKI Shinsuke 		}
757ba5da2a0SIan Dowse 	}
758df8bae1dSRodney W. Grimes 	splx(s);
759df8bae1dSRodney W. Grimes 	if (scrub) {
760df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
761df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
762df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
763df8bae1dSRodney W. Grimes 	}
764df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i))
765df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSA_NET;
766df8bae1dSRodney W. Grimes 	else if (IN_CLASSB(i))
767df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSB_NET;
768df8bae1dSRodney W. Grimes 	else
769df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSC_NET;
770df8bae1dSRodney W. Grimes 	/*
771df8bae1dSRodney W. Grimes 	 * The subnet mask usually includes at least the standard network part,
772df8bae1dSRodney W. Grimes 	 * but may may be smaller in the case of supernetting.
773df8bae1dSRodney W. Grimes 	 * If it is set, we believe it.
774df8bae1dSRodney W. Grimes 	 */
775df8bae1dSRodney W. Grimes 	if (ia->ia_subnetmask == 0) {
776df8bae1dSRodney W. Grimes 		ia->ia_subnetmask = ia->ia_netmask;
777df8bae1dSRodney W. Grimes 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
778df8bae1dSRodney W. Grimes 	} else
779df8bae1dSRodney W. Grimes 		ia->ia_netmask &= ia->ia_subnetmask;
780df8bae1dSRodney W. Grimes 	ia->ia_net = i & ia->ia_netmask;
781df8bae1dSRodney W. Grimes 	ia->ia_subnet = i & ia->ia_subnetmask;
782df8bae1dSRodney W. Grimes 	in_socktrim(&ia->ia_sockmask);
78350bb1704SGleb Smirnoff #ifdef DEV_CARP
78450bb1704SGleb Smirnoff 	/*
78550bb1704SGleb Smirnoff 	 * XXX: carp(4) does not have interface route
78650bb1704SGleb Smirnoff 	 */
78750bb1704SGleb Smirnoff 	if (ifp->if_type == IFT_CARP)
78850bb1704SGleb Smirnoff 		return (0);
78950bb1704SGleb Smirnoff #endif
790df8bae1dSRodney W. Grimes 	/*
791df8bae1dSRodney W. Grimes 	 * Add route for the network.
792df8bae1dSRodney W. Grimes 	 */
793df8bae1dSRodney W. Grimes 	ia->ia_ifa.ifa_metric = ifp->if_metric;
794df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_BROADCAST) {
795df8bae1dSRodney W. Grimes 		ia->ia_broadaddr.sin_addr.s_addr =
796df8bae1dSRodney W. Grimes 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
797df8bae1dSRodney W. Grimes 		ia->ia_netbroadcast.s_addr =
798df8bae1dSRodney W. Grimes 			htonl(ia->ia_net | ~ ia->ia_netmask);
799df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_LOOPBACK) {
8009a6a6eebSMax Laier 		ia->ia_dstaddr = ia->ia_addr;
801df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
802df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
803df8bae1dSRodney W. Grimes 		if (ia->ia_dstaddr.sin_family != AF_INET)
804df8bae1dSRodney W. Grimes 			return (0);
805df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
806df8bae1dSRodney W. Grimes 	}
80748321abeSMax Laier 	if ((error = in_addprefix(ia, flags)) != 0)
8080f02fdacSBrian Somers 		return (error);
8090f02fdacSBrian Somers 
810df8bae1dSRodney W. Grimes 	return (error);
811df8bae1dSRodney W. Grimes }
812df8bae1dSRodney W. Grimes 
81348321abeSMax Laier #define rtinitflags(x) \
81448321abeSMax Laier 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
81548321abeSMax Laier 	    ? RTF_HOST : 0)
81648321abeSMax Laier /*
817fbdd20a1SMatt Jacob  * Check if we have a route for the given prefix already or add one accordingly.
81848321abeSMax Laier  */
81948321abeSMax Laier static int
820f2565d68SRobert Watson in_addprefix(struct in_ifaddr *target, int flags)
82148321abeSMax Laier {
8228b615593SMarko Zec 	INIT_VNET_INET(curvnet);
82348321abeSMax Laier 	struct in_ifaddr *ia;
824bfb26eecSGleb Smirnoff 	struct in_addr prefix, mask, p, m;
82548321abeSMax Laier 	int error;
82648321abeSMax Laier 
827fbdd20a1SMatt Jacob 	if ((flags & RTF_HOST) != 0) {
82848321abeSMax Laier 		prefix = target->ia_dstaddr.sin_addr;
829fbdd20a1SMatt Jacob 		mask.s_addr = 0;
830fbdd20a1SMatt Jacob 	} else {
83148321abeSMax Laier 		prefix = target->ia_addr.sin_addr;
83248321abeSMax Laier 		mask = target->ia_sockmask.sin_addr;
83348321abeSMax Laier 		prefix.s_addr &= mask.s_addr;
83448321abeSMax Laier 	}
83548321abeSMax Laier 
836603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
837bfb26eecSGleb Smirnoff 		if (rtinitflags(ia)) {
83848321abeSMax Laier 			p = ia->ia_addr.sin_addr;
83948321abeSMax Laier 
84048321abeSMax Laier 			if (prefix.s_addr != p.s_addr)
84148321abeSMax Laier 				continue;
842bfb26eecSGleb Smirnoff 		} else {
843bfb26eecSGleb Smirnoff 			p = ia->ia_addr.sin_addr;
844bfb26eecSGleb Smirnoff 			m = ia->ia_sockmask.sin_addr;
845bfb26eecSGleb Smirnoff 			p.s_addr &= m.s_addr;
846bfb26eecSGleb Smirnoff 
847bfb26eecSGleb Smirnoff 			if (prefix.s_addr != p.s_addr ||
848bfb26eecSGleb Smirnoff 			    mask.s_addr != m.s_addr)
849bfb26eecSGleb Smirnoff 				continue;
850bfb26eecSGleb Smirnoff 		}
85148321abeSMax Laier 
85248321abeSMax Laier 		/*
85348321abeSMax Laier 		 * If we got a matching prefix route inserted by other
85448321abeSMax Laier 		 * interface address, we are done here.
85548321abeSMax Laier 		 */
8561ae95409SGleb Smirnoff 		if (ia->ia_flags & IFA_ROUTE) {
857603724d3SBjoern A. Zeeb 			if (V_sameprefixcarponly &&
8581ae95409SGleb Smirnoff 			    target->ia_ifp->if_type != IFT_CARP &&
8591ae95409SGleb Smirnoff 			    ia->ia_ifp->if_type != IFT_CARP)
8601ae95409SGleb Smirnoff 				return (EEXIST);
8611ae95409SGleb Smirnoff 			else
8621ae95409SGleb Smirnoff 				return (0);
8631ae95409SGleb Smirnoff 		}
86448321abeSMax Laier 	}
86548321abeSMax Laier 
86648321abeSMax Laier 	/*
86748321abeSMax Laier 	 * No-one seem to have this prefix route, so we try to insert it.
86848321abeSMax Laier 	 */
86948321abeSMax Laier 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
87048321abeSMax Laier 	if (!error)
87148321abeSMax Laier 		target->ia_flags |= IFA_ROUTE;
872460473a0SBjoern A. Zeeb 	return (error);
87348321abeSMax Laier }
87448321abeSMax Laier 
8756e6b3f7cSQing Li extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
8766e6b3f7cSQing Li 
87748321abeSMax Laier /*
87848321abeSMax Laier  * If there is no other address in the system that can serve a route to the
87948321abeSMax Laier  * same prefix, remove the route.  Hand over the route to the new address
88048321abeSMax Laier  * otherwise.
88148321abeSMax Laier  */
88248321abeSMax Laier static int
883f2565d68SRobert Watson in_scrubprefix(struct in_ifaddr *target)
88448321abeSMax Laier {
8858b615593SMarko Zec 	INIT_VNET_INET(curvnet);
88648321abeSMax Laier 	struct in_ifaddr *ia;
88748321abeSMax Laier 	struct in_addr prefix, mask, p;
88848321abeSMax Laier 	int error;
88948321abeSMax Laier 
89048321abeSMax Laier 	if ((target->ia_flags & IFA_ROUTE) == 0)
891460473a0SBjoern A. Zeeb 		return (0);
89248321abeSMax Laier 
89348321abeSMax Laier 	if (rtinitflags(target))
89448321abeSMax Laier 		prefix = target->ia_dstaddr.sin_addr;
89548321abeSMax Laier 	else {
89648321abeSMax Laier 		prefix = target->ia_addr.sin_addr;
89748321abeSMax Laier 		mask = target->ia_sockmask.sin_addr;
89848321abeSMax Laier 		prefix.s_addr &= mask.s_addr;
8996e6b3f7cSQing Li 		/* remove arp cache */
9006e6b3f7cSQing Li 		arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
90148321abeSMax Laier 	}
90248321abeSMax Laier 
903603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
90448321abeSMax Laier 		if (rtinitflags(ia))
90548321abeSMax Laier 			p = ia->ia_dstaddr.sin_addr;
90648321abeSMax Laier 		else {
90748321abeSMax Laier 			p = ia->ia_addr.sin_addr;
90848321abeSMax Laier 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
90948321abeSMax Laier 		}
91048321abeSMax Laier 
91148321abeSMax Laier 		if (prefix.s_addr != p.s_addr)
91248321abeSMax Laier 			continue;
91348321abeSMax Laier 
91448321abeSMax Laier 		/*
91548321abeSMax Laier 		 * If we got a matching prefix address, move IFA_ROUTE and
91648321abeSMax Laier 		 * the route itself to it.  Make sure that routing daemons
91748321abeSMax Laier 		 * get a heads-up.
91850bb1704SGleb Smirnoff 		 *
91950bb1704SGleb Smirnoff 		 * XXX: a special case for carp(4) interface
92048321abeSMax Laier 		 */
92150bb1704SGleb Smirnoff 		if ((ia->ia_flags & IFA_ROUTE) == 0
92250bb1704SGleb Smirnoff #ifdef DEV_CARP
92350bb1704SGleb Smirnoff 		    && (ia->ia_ifp->if_type != IFT_CARP)
92450bb1704SGleb Smirnoff #endif
92550bb1704SGleb Smirnoff 							) {
92648321abeSMax Laier 			rtinit(&(target->ia_ifa), (int)RTM_DELETE,
92748321abeSMax Laier 			    rtinitflags(target));
92848321abeSMax Laier 			target->ia_flags &= ~IFA_ROUTE;
92948321abeSMax Laier 
93048321abeSMax Laier 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
93148321abeSMax Laier 			    rtinitflags(ia) | RTF_UP);
93248321abeSMax Laier 			if (error == 0)
93348321abeSMax Laier 				ia->ia_flags |= IFA_ROUTE;
934460473a0SBjoern A. Zeeb 			return (error);
93548321abeSMax Laier 		}
93648321abeSMax Laier 	}
93748321abeSMax Laier 
93848321abeSMax Laier 	/*
93948321abeSMax Laier 	 * As no-one seem to have this prefix, we can remove the route.
94048321abeSMax Laier 	 */
94148321abeSMax Laier 	rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
94248321abeSMax Laier 	target->ia_flags &= ~IFA_ROUTE;
943460473a0SBjoern A. Zeeb 	return (0);
94448321abeSMax Laier }
94548321abeSMax Laier 
94648321abeSMax Laier #undef rtinitflags
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes /*
949df8bae1dSRodney W. Grimes  * Return 1 if the address might be a local broadcast address.
950df8bae1dSRodney W. Grimes  */
95126f9a767SRodney W. Grimes int
952f2565d68SRobert Watson in_broadcast(struct in_addr in, struct ifnet *ifp)
953df8bae1dSRodney W. Grimes {
954df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
955df8bae1dSRodney W. Grimes 	u_long t;
956df8bae1dSRodney W. Grimes 
957df8bae1dSRodney W. Grimes 	if (in.s_addr == INADDR_BROADCAST ||
958df8bae1dSRodney W. Grimes 	    in.s_addr == INADDR_ANY)
959460473a0SBjoern A. Zeeb 		return (1);
960df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
961460473a0SBjoern A. Zeeb 		return (0);
962df8bae1dSRodney W. Grimes 	t = ntohl(in.s_addr);
963df8bae1dSRodney W. Grimes 	/*
964df8bae1dSRodney W. Grimes 	 * Look through the list of addresses for a match
965df8bae1dSRodney W. Grimes 	 * with a broadcast address.
966df8bae1dSRodney W. Grimes 	 */
967df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa)
968462b86feSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
969df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == AF_INET &&
970df8bae1dSRodney W. Grimes 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
971df8bae1dSRodney W. Grimes 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
972df8bae1dSRodney W. Grimes 		     /*
973df8bae1dSRodney W. Grimes 		      * Check for old-style (host 0) broadcast.
974df8bae1dSRodney W. Grimes 		      */
9758dd27fd6SGuido van Rooij 		     t == ia->ia_subnet || t == ia->ia_net) &&
9768dd27fd6SGuido van Rooij 		     /*
9778dd27fd6SGuido van Rooij 		      * Check for an all one subnetmask. These
9788dd27fd6SGuido van Rooij 		      * only exist when an interface gets a secondary
9798dd27fd6SGuido van Rooij 		      * address.
9808dd27fd6SGuido van Rooij 		      */
9818dd27fd6SGuido van Rooij 		     ia->ia_subnetmask != (u_long)0xffffffff)
982460473a0SBjoern A. Zeeb 			    return (1);
983df8bae1dSRodney W. Grimes 	return (0);
984df8bae1dSRodney W. Grimes #undef ia
985df8bae1dSRodney W. Grimes }
986ec002feeSBruce M Simpson 
987df8bae1dSRodney W. Grimes /*
988ec002feeSBruce M Simpson  * Delete all IPv4 multicast address records, and associated link-layer
989ec002feeSBruce M Simpson  * multicast address records, associated with ifp.
990d9668414SBruce M Simpson  */
991ec002feeSBruce M Simpson static void
992ec002feeSBruce M Simpson in_purgemaddrs(struct ifnet *ifp)
993d9668414SBruce M Simpson {
9948b615593SMarko Zec 	INIT_VNET_INET(ifp->if_vnet);
995d9668414SBruce M Simpson 	struct in_multi *inm;
996d9668414SBruce M Simpson 	struct in_multi *oinm;
997d9668414SBruce M Simpson 
998f7e083afSBruce M Simpson #ifdef DIAGNOSTIC
999f7e083afSBruce M Simpson 	printf("%s: purging ifp %p\n", __func__, ifp);
1000f7e083afSBruce M Simpson #endif
1001d9668414SBruce M Simpson 	IFF_LOCKGIANT(ifp);
1002d9668414SBruce M Simpson 	IN_MULTI_LOCK();
1003603724d3SBjoern A. Zeeb 	LIST_FOREACH_SAFE(inm, &V_in_multihead, inm_link, oinm) {
1004d9668414SBruce M Simpson 		if (inm->inm_ifp == ifp)
1005d9668414SBruce M Simpson 			in_delmulti_locked(inm);
1006d9668414SBruce M Simpson 	}
1007dd5a318bSRobert Watson 	IN_MULTI_UNLOCK();
1008c48b03fbSRobert Watson 	IFF_UNLOCKGIANT(ifp);
1009df8bae1dSRodney W. Grimes }
1010b1c53bc9SRobert Watson 
1011b1c53bc9SRobert Watson /*
1012b1c53bc9SRobert Watson  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1013b1c53bc9SRobert Watson  */
1014b1c53bc9SRobert Watson void
1015f2565d68SRobert Watson in_ifdetach(struct ifnet *ifp)
1016b1c53bc9SRobert Watson {
10178b615593SMarko Zec 	INIT_VNET_INET(ifp->if_vnet);
1018b1c53bc9SRobert Watson 
1019603724d3SBjoern A. Zeeb 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
1020603724d3SBjoern A. Zeeb 	in_pcbpurgeif0(&V_udbinfo, ifp);
1021ec002feeSBruce M Simpson 	in_purgemaddrs(ifp);
1022b1c53bc9SRobert Watson }
10236e6b3f7cSQing Li 
10246e6b3f7cSQing Li #include <sys/syslog.h>
10256e6b3f7cSQing Li #include <net/if_dl.h>
10266e6b3f7cSQing Li #include <netinet/if_ether.h>
10276e6b3f7cSQing Li 
10286e6b3f7cSQing Li struct in_llentry {
10296e6b3f7cSQing Li 	struct llentry		base;
10306e6b3f7cSQing Li 	struct sockaddr_in	l3_addr4;
10316e6b3f7cSQing Li };
10326e6b3f7cSQing Li 
10336e6b3f7cSQing Li static struct llentry *
10346e6b3f7cSQing Li in_lltable_new(const struct sockaddr *l3addr, u_int flags)
10356e6b3f7cSQing Li {
10366e6b3f7cSQing Li 	struct in_llentry *lle;
10376e6b3f7cSQing Li 
10386e6b3f7cSQing Li 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
10396e6b3f7cSQing Li 	if (lle == NULL)		/* NB: caller generates msg */
10406e6b3f7cSQing Li 		return NULL;
10416e6b3f7cSQing Li 
10426e6b3f7cSQing Li 	callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
10436e6b3f7cSQing Li 	/*
10446e6b3f7cSQing Li 	 * For IPv4 this will trigger "arpresolve" to generate
10456e6b3f7cSQing Li 	 * an ARP request.
10466e6b3f7cSQing Li 	 */
10476e6b3f7cSQing Li 	lle->base.la_expire = time_second; /* mark expired */
10486e6b3f7cSQing Li 	lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
10496e6b3f7cSQing Li 	lle->base.lle_refcnt = 1;
10506e6b3f7cSQing Li 	LLE_LOCK_INIT(&lle->base);
10516e6b3f7cSQing Li 	return &lle->base;
10526e6b3f7cSQing Li }
10536e6b3f7cSQing Li 
10546e6b3f7cSQing Li /*
10556e6b3f7cSQing Li  * Deletes an address from the address table.
10566e6b3f7cSQing Li  * This function is called by the timer functions
10576e6b3f7cSQing Li  * such as arptimer() and nd6_llinfo_timer(), and
10586e6b3f7cSQing Li  * the caller does the locking.
10596e6b3f7cSQing Li  */
10606e6b3f7cSQing Li static void
10616e6b3f7cSQing Li in_lltable_free(struct lltable *llt, struct llentry *lle)
10626e6b3f7cSQing Li {
10636e6b3f7cSQing Li 	free(lle, M_LLTABLE);
10646e6b3f7cSQing Li }
10656e6b3f7cSQing Li 
10666e6b3f7cSQing Li static int
10676e6b3f7cSQing Li in_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr)
10686e6b3f7cSQing Li {
10696e6b3f7cSQing Li 	struct rtentry *rt;
10706e6b3f7cSQing Li 
10716e6b3f7cSQing Li 	KASSERT(l3addr->sa_family == AF_INET,
10726e6b3f7cSQing Li 	    ("sin_family %d", l3addr->sa_family));
10736e6b3f7cSQing Li 
10746e6b3f7cSQing Li 	/* XXX rtalloc1 should take a const param */
10756e6b3f7cSQing Li 	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
10766e6b3f7cSQing Li 	if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
10776e6b3f7cSQing Li 		log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
10786e6b3f7cSQing Li 		    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
10796e6b3f7cSQing Li 		if (rt != NULL)
10806e6b3f7cSQing Li 			RTFREE_LOCKED(rt);
10816e6b3f7cSQing Li 		return (EINVAL);
10826e6b3f7cSQing Li 	}
10836e6b3f7cSQing Li 	RTFREE_LOCKED(rt);
10846e6b3f7cSQing Li 	return 0;
10856e6b3f7cSQing Li }
10866e6b3f7cSQing Li 
10876e6b3f7cSQing Li /*
10886e6b3f7cSQing Li  * Return NULL if not found or marked for deletion.
10896e6b3f7cSQing Li  * If found return lle read locked.
10906e6b3f7cSQing Li  */
10916e6b3f7cSQing Li static struct llentry *
10926e6b3f7cSQing Li in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
10936e6b3f7cSQing Li {
10946e6b3f7cSQing Li 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
10956e6b3f7cSQing Li 	struct ifnet *ifp = llt->llt_ifp;
10966e6b3f7cSQing Li 	struct llentry *lle;
10976e6b3f7cSQing Li 	struct llentries *lleh;
10986e6b3f7cSQing Li 	u_int hashkey;
10996e6b3f7cSQing Li 
11006e6b3f7cSQing Li 	IF_AFDATA_LOCK_ASSERT(ifp);
11016e6b3f7cSQing Li 	KASSERT(l3addr->sa_family == AF_INET,
11026e6b3f7cSQing Li 	    ("sin_family %d", l3addr->sa_family));
11036e6b3f7cSQing Li 
11046e6b3f7cSQing Li 	hashkey = sin->sin_addr.s_addr;
11056e6b3f7cSQing Li 	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
11066e6b3f7cSQing Li 	LIST_FOREACH(lle, lleh, lle_next) {
11076e6b3f7cSQing Li 		if (lle->la_flags & LLE_DELETED)
11086e6b3f7cSQing Li 			continue;
11096e6b3f7cSQing Li 		if (bcmp(L3_ADDR(lle), l3addr, sizeof(struct sockaddr_in)) == 0)
11106e6b3f7cSQing Li 			break;
11116e6b3f7cSQing Li 	}
11126e6b3f7cSQing Li 	if (lle == NULL) {
11136e6b3f7cSQing Li #ifdef DIAGNOSTICS
11146e6b3f7cSQing Li 		if (flags & LLE_DELETE)
11156e6b3f7cSQing Li 			log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
11166e6b3f7cSQing Li #endif
11176e6b3f7cSQing Li 		if (!(flags & LLE_CREATE))
11186e6b3f7cSQing Li 			return (NULL);
11196e6b3f7cSQing Li 		/*
11206e6b3f7cSQing Li 		 * A route that covers the given address must have
11216e6b3f7cSQing Li 		 * been installed 1st because we are doing a resolution,
11226e6b3f7cSQing Li 		 * verify this.
11236e6b3f7cSQing Li 		 */
11246e6b3f7cSQing Li 		if (!(flags & LLE_IFADDR) &&
11256e6b3f7cSQing Li 		    in_lltable_rtcheck(ifp, l3addr) != 0)
11266e6b3f7cSQing Li 			goto done;
11276e6b3f7cSQing Li 
11286e6b3f7cSQing Li 		lle = in_lltable_new(l3addr, flags);
11296e6b3f7cSQing Li 		if (lle == NULL) {
11306e6b3f7cSQing Li 			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
11316e6b3f7cSQing Li 			goto done;
11326e6b3f7cSQing Li 		}
11336e6b3f7cSQing Li 		lle->la_flags = flags & ~LLE_CREATE;
11346e6b3f7cSQing Li 		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
11356e6b3f7cSQing Li 			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
11366e6b3f7cSQing Li 			lle->la_flags |= (LLE_VALID | LLE_STATIC);
11376e6b3f7cSQing Li 		}
11386e6b3f7cSQing Li 
11396e6b3f7cSQing Li 		lle->lle_tbl  = llt;
11406e6b3f7cSQing Li 		lle->lle_head = lleh;
11416e6b3f7cSQing Li 		LIST_INSERT_HEAD(lleh, lle, lle_next);
11426e6b3f7cSQing Li 	} else if (flags & LLE_DELETE) {
11436e6b3f7cSQing Li 		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
11446e6b3f7cSQing Li 			LLE_WLOCK(lle);
11456e6b3f7cSQing Li 			lle->la_flags = LLE_DELETED;
11466e6b3f7cSQing Li 			LLE_WUNLOCK(lle);
11476e6b3f7cSQing Li #ifdef DIAGNOSTICS
11486e6b3f7cSQing Li 			log(LOG_INFO, "ifaddr cache = %p  is deleted\n", lle);
11496e6b3f7cSQing Li #endif
11506e6b3f7cSQing Li 		}
11516e6b3f7cSQing Li 		lle = (void *)-1;
11526e6b3f7cSQing Li 
11536e6b3f7cSQing Li 	}
11546e6b3f7cSQing Li 	if (lle != NULL && lle != (void *)-1) {
11556e6b3f7cSQing Li 		if (flags & LLE_EXCLUSIVE)
11566e6b3f7cSQing Li 			LLE_WLOCK(lle);
11576e6b3f7cSQing Li 		else
11586e6b3f7cSQing Li 			LLE_RLOCK(lle);
11596e6b3f7cSQing Li 	}
11606e6b3f7cSQing Li done:
11616e6b3f7cSQing Li 	return (lle);
11626e6b3f7cSQing Li }
11636e6b3f7cSQing Li 
11646e6b3f7cSQing Li static int
11656e6b3f7cSQing Li in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
11666e6b3f7cSQing Li {
11676e6b3f7cSQing Li #define	SIN(lle)	((struct sockaddr_in *) L3_ADDR(lle))
11686e6b3f7cSQing Li 	struct ifnet *ifp = llt->llt_ifp;
11696e6b3f7cSQing Li 	struct llentry *lle;
11706e6b3f7cSQing Li 	/* XXX stack use */
11716e6b3f7cSQing Li 	struct {
11726e6b3f7cSQing Li 		struct rt_msghdr	rtm;
11736e6b3f7cSQing Li 		struct sockaddr_inarp	sin;
11746e6b3f7cSQing Li 		struct sockaddr_dl	sdl;
11756e6b3f7cSQing Li 	} arpc;
11766e6b3f7cSQing Li 	int error, i;
11776e6b3f7cSQing Li 
11786e6b3f7cSQing Li 	/* XXXXX
11796e6b3f7cSQing Li 	 * current IFNET_RLOCK() is mapped to IFNET_WLOCK()
11806e6b3f7cSQing Li 	 * so it is okay to use this ASSERT, change it when
11816e6b3f7cSQing Li 	 * IFNET lock is finalized
11826e6b3f7cSQing Li 	 */
11836e6b3f7cSQing Li 	IFNET_WLOCK_ASSERT();
11846e6b3f7cSQing Li 
11856e6b3f7cSQing Li 	error = 0;
11866e6b3f7cSQing Li 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
11876e6b3f7cSQing Li 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
11886e6b3f7cSQing Li 			struct sockaddr_dl *sdl;
11896e6b3f7cSQing Li 
11906e6b3f7cSQing Li 			/* skip deleted entries */
11916e6b3f7cSQing Li 			if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
11926e6b3f7cSQing Li 				continue;
11936e6b3f7cSQing Li 			/*
11946e6b3f7cSQing Li 			 * produce a msg made of:
11956e6b3f7cSQing Li 			 *  struct rt_msghdr;
11966e6b3f7cSQing Li 			 *  struct sockaddr_inarp; (IPv4)
11976e6b3f7cSQing Li 			 *  struct sockaddr_dl;
11986e6b3f7cSQing Li 			 */
11996e6b3f7cSQing Li 			bzero(&arpc, sizeof(arpc));
12006e6b3f7cSQing Li 			arpc.rtm.rtm_msglen = sizeof(arpc);
12016e6b3f7cSQing Li 			arpc.sin.sin_family = AF_INET;
12026e6b3f7cSQing Li 			arpc.sin.sin_len = sizeof(arpc.sin);
12036e6b3f7cSQing Li 			arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
12046e6b3f7cSQing Li 
12056e6b3f7cSQing Li 			/* publish */
12066e6b3f7cSQing Li 			if (lle->la_flags & LLE_PUB) {
12076e6b3f7cSQing Li 				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
12086e6b3f7cSQing Li 				/* proxy only */
12096e6b3f7cSQing Li 				if (lle->la_flags & LLE_PROXY)
12106e6b3f7cSQing Li 					arpc.sin.sin_other = SIN_PROXY;
12116e6b3f7cSQing Li 			}
12126e6b3f7cSQing Li 
12136e6b3f7cSQing Li 			sdl = &arpc.sdl;
12146e6b3f7cSQing Li 			sdl->sdl_family = AF_LINK;
12156e6b3f7cSQing Li 			sdl->sdl_len = sizeof(*sdl);
12166e6b3f7cSQing Li 			sdl->sdl_alen = ifp->if_addrlen;
12176e6b3f7cSQing Li 			sdl->sdl_index = ifp->if_index;
12186e6b3f7cSQing Li 			sdl->sdl_type = ifp->if_type;
12196e6b3f7cSQing Li 			bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
12206e6b3f7cSQing Li 
12216e6b3f7cSQing Li 			arpc.rtm.rtm_rmx.rmx_expire =
12226e6b3f7cSQing Li 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
12236e6b3f7cSQing Li 			arpc.rtm.rtm_flags |= RTF_HOST;
12246e6b3f7cSQing Li 			if (lle->la_flags & LLE_STATIC)
12256e6b3f7cSQing Li 				arpc.rtm.rtm_flags |= RTF_STATIC;
12266e6b3f7cSQing Li 			arpc.rtm.rtm_index = ifp->if_index;
12276e6b3f7cSQing Li 			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
12286e6b3f7cSQing Li 			if (error)
12296e6b3f7cSQing Li 				break;
12306e6b3f7cSQing Li 		}
12316e6b3f7cSQing Li 	}
12326e6b3f7cSQing Li 	return error;
12336e6b3f7cSQing Li #undef SIN
12346e6b3f7cSQing Li }
12356e6b3f7cSQing Li 
12366e6b3f7cSQing Li void *
12376e6b3f7cSQing Li in_domifattach(struct ifnet *ifp)
12386e6b3f7cSQing Li {
12396e6b3f7cSQing Li 	struct lltable *llt = lltable_init(ifp, AF_INET);
12406e6b3f7cSQing Li 
12416e6b3f7cSQing Li 	if (llt != NULL) {
12426e6b3f7cSQing Li 		llt->llt_new = in_lltable_new;
12436e6b3f7cSQing Li 		llt->llt_free = in_lltable_free;
12446e6b3f7cSQing Li 		llt->llt_rtcheck = in_lltable_rtcheck;
12456e6b3f7cSQing Li 		llt->llt_lookup = in_lltable_lookup;
12466e6b3f7cSQing Li 		llt->llt_dump = in_lltable_dump;
12476e6b3f7cSQing Li 	}
12486e6b3f7cSQing Li 	return (llt);
12496e6b3f7cSQing Li }
12506e6b3f7cSQing Li 
12516e6b3f7cSQing Li void
12526e6b3f7cSQing Li in_domifdetach(struct ifnet *ifp __unused, void *aux)
12536e6b3f7cSQing Li {
12546e6b3f7cSQing Li 	struct lltable *llt = (struct lltable *)aux;
12556e6b3f7cSQing Li 
12566e6b3f7cSQing Li 	lltable_free(llt);
12576e6b3f7cSQing Li }
1258