xref: /freebsd/sys/netinet/in.c (revision 2eccc90b612369b7384012f6d7e38865d87d0caf)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
292180b925SGarrett Wollman  *	@(#)in.c	8.4 (Berkeley) 1/9/95
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33df8bae1dSRodney W. Grimes #include <sys/param.h>
3426f9a767SRodney W. Grimes #include <sys/systm.h>
3551a53488SBruce Evans #include <sys/sockio.h>
36df8bae1dSRodney W. Grimes #include <sys/malloc.h>
37df8bae1dSRodney W. Grimes #include <sys/socket.h>
38f6d24a78SPoul-Henning Kamp #include <sys/kernel.h>
39f6d24a78SPoul-Henning Kamp #include <sys/sysctl.h>
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes #include <net/if.h>
426a800098SYoshinobu Inoue #include <net/if_types.h>
43df8bae1dSRodney W. Grimes #include <net/route.h>
44df8bae1dSRodney W. Grimes 
45df8bae1dSRodney W. Grimes #include <netinet/in.h>
46df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
47e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h>
48df8bae1dSRodney W. Grimes 
49c70f4510SPoul-Henning Kamp #include <netinet/igmp_var.h>
50c70f4510SPoul-Henning Kamp 
51a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
5255166637SPoul-Henning Kamp 
534d77a549SAlfred Perlstein static int in_mask2len(struct in_addr *);
544d77a549SAlfred Perlstein static void in_len2mask(struct in_addr *, int);
554d77a549SAlfred Perlstein static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
564d77a549SAlfred Perlstein 	struct ifnet *, struct thread *);
576a800098SYoshinobu Inoue 
584d77a549SAlfred Perlstein static void	in_socktrim(struct sockaddr_in *);
594d77a549SAlfred Perlstein static int	in_ifinit(struct ifnet *,
604d77a549SAlfred Perlstein 	    struct in_ifaddr *, struct sockaddr_in *, int);
61df8bae1dSRodney W. Grimes 
62f8731310SGarrett Wollman static int subnetsarelocal = 0;
63f6d24a78SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
643b95e134SRuslan Ermilov 	&subnetsarelocal, 0, "Treat all subnets as directly connected");
65477180fbSGarrett Wollman 
66477180fbSGarrett Wollman struct in_multihead in_multihead; /* XXX BSS initialization */
67477180fbSGarrett Wollman 
68e43cc4aeSHajimu UMEMOTO extern struct inpcbinfo ripcbinfo;
69e43cc4aeSHajimu UMEMOTO extern struct inpcbinfo udbinfo;
70e43cc4aeSHajimu UMEMOTO 
71df8bae1dSRodney W. Grimes /*
72df8bae1dSRodney W. Grimes  * Return 1 if an internet address is for a ``local'' host
73df8bae1dSRodney W. Grimes  * (one to which we have a connection).  If subnetsarelocal
74df8bae1dSRodney W. Grimes  * is true, this includes other subnets of the local net.
75df8bae1dSRodney W. Grimes  * Otherwise, it includes only the directly-connected (sub)nets.
76df8bae1dSRodney W. Grimes  */
7726f9a767SRodney W. Grimes int
78df8bae1dSRodney W. Grimes in_localaddr(in)
79df8bae1dSRodney W. Grimes 	struct in_addr in;
80df8bae1dSRodney W. Grimes {
81df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
82df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
83df8bae1dSRodney W. Grimes 
84df8bae1dSRodney W. Grimes 	if (subnetsarelocal) {
85462b86feSPoul-Henning Kamp 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
86df8bae1dSRodney W. Grimes 			if ((i & ia->ia_netmask) == ia->ia_net)
87df8bae1dSRodney W. Grimes 				return (1);
88df8bae1dSRodney W. Grimes 	} else {
8937d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
90df8bae1dSRodney W. Grimes 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
91df8bae1dSRodney W. Grimes 				return (1);
92df8bae1dSRodney W. Grimes 	}
93df8bae1dSRodney W. Grimes 	return (0);
94df8bae1dSRodney W. Grimes }
95df8bae1dSRodney W. Grimes 
96df8bae1dSRodney W. Grimes /*
972eccc90bSAndre Oppermann  * Return 1 if an internet address is for the local host and configured
982eccc90bSAndre Oppermann  * on one of its interfaces.
992eccc90bSAndre Oppermann  */
1002eccc90bSAndre Oppermann int
1012eccc90bSAndre Oppermann in_localip(in)
1022eccc90bSAndre Oppermann 	struct in_addr in;
1032eccc90bSAndre Oppermann {
1042eccc90bSAndre Oppermann 	struct in_ifaddr *ia;
1052eccc90bSAndre Oppermann 
1062eccc90bSAndre Oppermann 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
1072eccc90bSAndre Oppermann 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
1082eccc90bSAndre Oppermann 			return 1;
1092eccc90bSAndre Oppermann 	}
1102eccc90bSAndre Oppermann 	return 0;
1112eccc90bSAndre Oppermann }
1122eccc90bSAndre Oppermann 
1132eccc90bSAndre Oppermann /*
114df8bae1dSRodney W. Grimes  * Determine whether an IP address is in a reserved set of addresses
115df8bae1dSRodney W. Grimes  * that may not be forwarded, or whether datagrams to that destination
116df8bae1dSRodney W. Grimes  * may be forwarded.
117df8bae1dSRodney W. Grimes  */
11826f9a767SRodney W. Grimes int
119df8bae1dSRodney W. Grimes in_canforward(in)
120df8bae1dSRodney W. Grimes 	struct in_addr in;
121df8bae1dSRodney W. Grimes {
122df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
123df8bae1dSRodney W. Grimes 	register u_long net;
124df8bae1dSRodney W. Grimes 
125df8bae1dSRodney W. Grimes 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
126df8bae1dSRodney W. Grimes 		return (0);
127df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i)) {
128df8bae1dSRodney W. Grimes 		net = i & IN_CLASSA_NET;
129df8bae1dSRodney W. Grimes 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
130df8bae1dSRodney W. Grimes 			return (0);
131df8bae1dSRodney W. Grimes 	}
132df8bae1dSRodney W. Grimes 	return (1);
133df8bae1dSRodney W. Grimes }
134df8bae1dSRodney W. Grimes 
135df8bae1dSRodney W. Grimes /*
136df8bae1dSRodney W. Grimes  * Trim a mask in a sockaddr
137df8bae1dSRodney W. Grimes  */
1380312fbe9SPoul-Henning Kamp static void
139df8bae1dSRodney W. Grimes in_socktrim(ap)
140df8bae1dSRodney W. Grimes struct sockaddr_in *ap;
141df8bae1dSRodney W. Grimes {
142df8bae1dSRodney W. Grimes     register char *cplim = (char *) &ap->sin_addr;
143df8bae1dSRodney W. Grimes     register char *cp = (char *) (&ap->sin_addr + 1);
144df8bae1dSRodney W. Grimes 
145df8bae1dSRodney W. Grimes     ap->sin_len = 0;
146df00058dSGarrett Wollman     while (--cp >= cplim)
147df8bae1dSRodney W. Grimes         if (*cp) {
148df8bae1dSRodney W. Grimes 	    (ap)->sin_len = cp - (char *) (ap) + 1;
149df8bae1dSRodney W. Grimes 	    break;
150df8bae1dSRodney W. Grimes 	}
151df8bae1dSRodney W. Grimes }
152df8bae1dSRodney W. Grimes 
1536a800098SYoshinobu Inoue static int
1546a800098SYoshinobu Inoue in_mask2len(mask)
1556a800098SYoshinobu Inoue 	struct in_addr *mask;
1566a800098SYoshinobu Inoue {
1576a800098SYoshinobu Inoue 	int x, y;
1586a800098SYoshinobu Inoue 	u_char *p;
1596a800098SYoshinobu Inoue 
1606a800098SYoshinobu Inoue 	p = (u_char *)mask;
1616a800098SYoshinobu Inoue 	for (x = 0; x < sizeof(*mask); x++) {
1626a800098SYoshinobu Inoue 		if (p[x] != 0xff)
1636a800098SYoshinobu Inoue 			break;
1646a800098SYoshinobu Inoue 	}
1656a800098SYoshinobu Inoue 	y = 0;
1666a800098SYoshinobu Inoue 	if (x < sizeof(*mask)) {
1676a800098SYoshinobu Inoue 		for (y = 0; y < 8; y++) {
1686a800098SYoshinobu Inoue 			if ((p[x] & (0x80 >> y)) == 0)
1696a800098SYoshinobu Inoue 				break;
1706a800098SYoshinobu Inoue 		}
1716a800098SYoshinobu Inoue 	}
1726a800098SYoshinobu Inoue 	return x * 8 + y;
1736a800098SYoshinobu Inoue }
1746a800098SYoshinobu Inoue 
1756a800098SYoshinobu Inoue static void
1766a800098SYoshinobu Inoue in_len2mask(mask, len)
1776a800098SYoshinobu Inoue 	struct in_addr *mask;
1786a800098SYoshinobu Inoue 	int len;
1796a800098SYoshinobu Inoue {
1806a800098SYoshinobu Inoue 	int i;
1816a800098SYoshinobu Inoue 	u_char *p;
1826a800098SYoshinobu Inoue 
1836a800098SYoshinobu Inoue 	p = (u_char *)mask;
1846a800098SYoshinobu Inoue 	bzero(mask, sizeof(*mask));
1856a800098SYoshinobu Inoue 	for (i = 0; i < len / 8; i++)
1866a800098SYoshinobu Inoue 		p[i] = 0xff;
1876a800098SYoshinobu Inoue 	if (len % 8)
1886a800098SYoshinobu Inoue 		p[i] = (0xff00 >> (len % 8)) & 0xff;
1896a800098SYoshinobu Inoue }
1906a800098SYoshinobu Inoue 
191df8bae1dSRodney W. Grimes /*
192df8bae1dSRodney W. Grimes  * Generic internet control operations (ioctl's).
193df8bae1dSRodney W. Grimes  * Ifp is 0 if not an interface-specific ioctl.
194df8bae1dSRodney W. Grimes  */
195df8bae1dSRodney W. Grimes /* ARGSUSED */
19626f9a767SRodney W. Grimes int
197b40ce416SJulian Elischer in_control(so, cmd, data, ifp, td)
198df8bae1dSRodney W. Grimes 	struct socket *so;
199ecbb00a2SDoug Rabson 	u_long cmd;
200df8bae1dSRodney W. Grimes 	caddr_t data;
201df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
202b40ce416SJulian Elischer 	struct thread *td;
203df8bae1dSRodney W. Grimes {
204df8bae1dSRodney W. Grimes 	register struct ifreq *ifr = (struct ifreq *)data;
205ac0aa473SBill Fenner 	register struct in_ifaddr *ia = 0, *iap;
206df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
207ca925d9cSJonathan Lemon 	struct in_addr dst;
208df8bae1dSRodney W. Grimes 	struct in_ifaddr *oia;
209df8bae1dSRodney W. Grimes 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
210df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
2110f02fdacSBrian Somers 	int error, hostIsNew, iaIsNew, maskIsNew, s;
2120f02fdacSBrian Somers 
2130f02fdacSBrian Somers 	iaIsNew = 0;
214df8bae1dSRodney W. Grimes 
2156a800098SYoshinobu Inoue 	switch (cmd) {
2166a800098SYoshinobu Inoue 	case SIOCALIFADDR:
2176a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
21844731cabSJohn Baldwin 		if (td && (error = suser(td)) != 0)
2196a800098SYoshinobu Inoue 			return error;
2206a800098SYoshinobu Inoue 		/*fall through*/
2216a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
2226a800098SYoshinobu Inoue 		if (!ifp)
2236a800098SYoshinobu Inoue 			return EINVAL;
224b40ce416SJulian Elischer 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
2256a800098SYoshinobu Inoue 	}
2266a800098SYoshinobu Inoue 
227df8bae1dSRodney W. Grimes 	/*
228df8bae1dSRodney W. Grimes 	 * Find address for this interface, if it exists.
229ac0aa473SBill Fenner 	 *
230ac0aa473SBill Fenner 	 * If an alias address was specified, find that one instead of
231ca925d9cSJonathan Lemon 	 * the first one on the interface, if possible.
232df8bae1dSRodney W. Grimes 	 */
233ca925d9cSJonathan Lemon 	if (ifp) {
234ca925d9cSJonathan Lemon 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
235ca925d9cSJonathan Lemon 		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
236ca925d9cSJonathan Lemon 			if (iap->ia_ifp == ifp &&
237ca925d9cSJonathan Lemon 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
238ac0aa473SBill Fenner 				ia = iap;
239df8bae1dSRodney W. Grimes 				break;
240ca925d9cSJonathan Lemon 			}
241ca925d9cSJonathan Lemon 		if (ia == NULL)
242ca925d9cSJonathan Lemon 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
243ca925d9cSJonathan Lemon 				iap = ifatoia(ifa);
244ca925d9cSJonathan Lemon 				if (iap->ia_addr.sin_family == AF_INET) {
245ac0aa473SBill Fenner 					ia = iap;
246ac0aa473SBill Fenner 					break;
247ac0aa473SBill Fenner 				}
248ac0aa473SBill Fenner 			}
249ca925d9cSJonathan Lemon 	}
250df8bae1dSRodney W. Grimes 
251df8bae1dSRodney W. Grimes 	switch (cmd) {
252df8bae1dSRodney W. Grimes 
253df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
254df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
2556572231dSEivind Eklund 		if (ifp == 0)
2566572231dSEivind Eklund 			return (EADDRNOTAVAIL);
2571067217dSGarrett Wollman 		if (ifra->ifra_addr.sin_family == AF_INET) {
258fc2ffbe6SPoul-Henning Kamp 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
259df8bae1dSRodney W. Grimes 				if (ia->ia_ifp == ifp  &&
260df8bae1dSRodney W. Grimes 				    ia->ia_addr.sin_addr.s_addr ==
261df8bae1dSRodney W. Grimes 				    ifra->ifra_addr.sin_addr.s_addr)
262df8bae1dSRodney W. Grimes 					break;
263df8bae1dSRodney W. Grimes 			}
2641067217dSGarrett Wollman 			if ((ifp->if_flags & IFF_POINTOPOINT)
2651067217dSGarrett Wollman 			    && (cmd == SIOCAIFADDR)
2661067217dSGarrett Wollman 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
2671067217dSGarrett Wollman 				== INADDR_ANY)) {
268357b78a9SGarrett Wollman 				return EDESTADDRREQ;
2691067217dSGarrett Wollman 			}
2701067217dSGarrett Wollman 		}
271df8bae1dSRodney W. Grimes 		if (cmd == SIOCDIFADDR && ia == 0)
272df8bae1dSRodney W. Grimes 			return (EADDRNOTAVAIL);
273df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
274df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
275df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
276df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
27744731cabSJohn Baldwin 		if (td && (error = suser(td)) != 0)
278a29f300eSGarrett Wollman 			return error;
279df8bae1dSRodney W. Grimes 
280df8bae1dSRodney W. Grimes 		if (ifp == 0)
2816572231dSEivind Eklund 			return (EADDRNOTAVAIL);
282df8bae1dSRodney W. Grimes 		if (ia == (struct in_ifaddr *)0) {
28359562606SGarrett Wollman 			ia = (struct in_ifaddr *)
284a163d034SWarner Losh 				malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
28559562606SGarrett Wollman 			if (ia == (struct in_ifaddr *)NULL)
286df8bae1dSRodney W. Grimes 				return (ENOBUFS);
287c655b7c4SDavid Greenman 			/*
288c655b7c4SDavid Greenman 			 * Protect from ipintr() traversing address list
289c655b7c4SDavid Greenman 			 * while we're modifying it.
290c655b7c4SDavid Greenman 			 */
291c655b7c4SDavid Greenman 			s = splnet();
29259562606SGarrett Wollman 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
29359562606SGarrett Wollman 
29419fc74fbSJeffrey Hsu 			ifa = &ia->ia_ifa;
29519fc74fbSJeffrey Hsu 			IFA_LOCK_INIT(ifa);
29659562606SGarrett Wollman 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
29759562606SGarrett Wollman 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
29859562606SGarrett Wollman 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
29919fc74fbSJeffrey Hsu 			ifa->ifa_refcnt = 1;
30019fc74fbSJeffrey Hsu 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
30119fc74fbSJeffrey Hsu 
302df8bae1dSRodney W. Grimes 			ia->ia_sockmask.sin_len = 8;
303bc183b3fSDag-Erling Smørgrav 			ia->ia_sockmask.sin_family = AF_INET;
304df8bae1dSRodney W. Grimes 			if (ifp->if_flags & IFF_BROADCAST) {
305df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
306df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_family = AF_INET;
307df8bae1dSRodney W. Grimes 			}
308df8bae1dSRodney W. Grimes 			ia->ia_ifp = ifp;
309c655b7c4SDavid Greenman 			splx(s);
3100f02fdacSBrian Somers 			iaIsNew = 1;
311df8bae1dSRodney W. Grimes 		}
312df8bae1dSRodney W. Grimes 		break;
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
31544731cabSJohn Baldwin 		if (td && (error = suser(td)) != 0)
316a29f300eSGarrett Wollman 			return error;
317df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
320df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
321df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
322df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
323df8bae1dSRodney W. Grimes 		if (ia == (struct in_ifaddr *)0)
324df8bae1dSRodney W. Grimes 			return (EADDRNOTAVAIL);
325df8bae1dSRodney W. Grimes 		break;
326df8bae1dSRodney W. Grimes 	}
327df8bae1dSRodney W. Grimes 	switch (cmd) {
328df8bae1dSRodney W. Grimes 
329df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
330df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
3310f02fdacSBrian Somers 		return (0);
332df8bae1dSRodney W. Grimes 
333df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
334df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
335df8bae1dSRodney W. Grimes 			return (EINVAL);
336df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
3370f02fdacSBrian Somers 		return (0);
338df8bae1dSRodney W. Grimes 
339df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
340df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
341df8bae1dSRodney W. Grimes 			return (EINVAL);
342df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
3430f02fdacSBrian Somers 		return (0);
344df8bae1dSRodney W. Grimes 
345df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
346df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
3470f02fdacSBrian Somers 		return (0);
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
350df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
351df8bae1dSRodney W. Grimes 			return (EINVAL);
352df8bae1dSRodney W. Grimes 		oldaddr = ia->ia_dstaddr;
353df8bae1dSRodney W. Grimes 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
354df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
355df8bae1dSRodney W. Grimes 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
356df8bae1dSRodney W. Grimes 			ia->ia_dstaddr = oldaddr;
357df8bae1dSRodney W. Grimes 			return (error);
358df8bae1dSRodney W. Grimes 		}
359df8bae1dSRodney W. Grimes 		if (ia->ia_flags & IFA_ROUTE) {
360df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
361df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
362df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr =
363df8bae1dSRodney W. Grimes 					(struct sockaddr *)&ia->ia_dstaddr;
364df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
365df8bae1dSRodney W. Grimes 		}
3660f02fdacSBrian Somers 		return (0);
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
369df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
370df8bae1dSRodney W. Grimes 			return (EINVAL);
371df8bae1dSRodney W. Grimes 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
3720f02fdacSBrian Somers 		return (0);
373df8bae1dSRodney W. Grimes 
374df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
3750f02fdacSBrian Somers 		error = in_ifinit(ifp, ia,
3760f02fdacSBrian Somers 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
3770f02fdacSBrian Somers 		if (error != 0 && iaIsNew)
3780f02fdacSBrian Somers 			break;
37925a4adceSMax Laier 		if (error == 0)
38025a4adceSMax Laier 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
3810f02fdacSBrian Somers 		return (0);
382df8bae1dSRodney W. Grimes 
383df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
384bc183b3fSDag-Erling Smørgrav 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
385bc183b3fSDag-Erling Smørgrav 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
3860f02fdacSBrian Somers 		return (0);
387df8bae1dSRodney W. Grimes 
388df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
389df8bae1dSRodney W. Grimes 		maskIsNew = 0;
390df8bae1dSRodney W. Grimes 		hostIsNew = 1;
391df8bae1dSRodney W. Grimes 		error = 0;
392df8bae1dSRodney W. Grimes 		if (ia->ia_addr.sin_family == AF_INET) {
393df8bae1dSRodney W. Grimes 			if (ifra->ifra_addr.sin_len == 0) {
394df8bae1dSRodney W. Grimes 				ifra->ifra_addr = ia->ia_addr;
395df8bae1dSRodney W. Grimes 				hostIsNew = 0;
396df8bae1dSRodney W. Grimes 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
397df8bae1dSRodney W. Grimes 					       ia->ia_addr.sin_addr.s_addr)
398df8bae1dSRodney W. Grimes 				hostIsNew = 0;
399df8bae1dSRodney W. Grimes 		}
400df8bae1dSRodney W. Grimes 		if (ifra->ifra_mask.sin_len) {
401df8bae1dSRodney W. Grimes 			in_ifscrub(ifp, ia);
402df8bae1dSRodney W. Grimes 			ia->ia_sockmask = ifra->ifra_mask;
403bc183b3fSDag-Erling Smørgrav 			ia->ia_sockmask.sin_family = AF_INET;
404df8bae1dSRodney W. Grimes 			ia->ia_subnetmask =
405df8bae1dSRodney W. Grimes 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
406df8bae1dSRodney W. Grimes 			maskIsNew = 1;
407df8bae1dSRodney W. Grimes 		}
408df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
409df8bae1dSRodney W. Grimes 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
410df8bae1dSRodney W. Grimes 			in_ifscrub(ifp, ia);
411df8bae1dSRodney W. Grimes 			ia->ia_dstaddr = ifra->ifra_dstaddr;
412df8bae1dSRodney W. Grimes 			maskIsNew  = 1; /* We lie; but the effect's the same */
413df8bae1dSRodney W. Grimes 		}
414df8bae1dSRodney W. Grimes 		if (ifra->ifra_addr.sin_family == AF_INET &&
415df8bae1dSRodney W. Grimes 		    (hostIsNew || maskIsNew))
416df8bae1dSRodney W. Grimes 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
4170f02fdacSBrian Somers 		if (error != 0 && iaIsNew)
4180f02fdacSBrian Somers 			break;
4190f02fdacSBrian Somers 
420df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) &&
421df8bae1dSRodney W. Grimes 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
422df8bae1dSRodney W. Grimes 			ia->ia_broadaddr = ifra->ifra_broadaddr;
42325a4adceSMax Laier 		if (error == 0)
42425a4adceSMax Laier 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
425df8bae1dSRodney W. Grimes 		return (error);
426df8bae1dSRodney W. Grimes 
427df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
428089cdfadSRuslan Ermilov 		/*
429089cdfadSRuslan Ermilov 		 * in_ifscrub kills the interface route.
430089cdfadSRuslan Ermilov 		 */
431df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
432c655b7c4SDavid Greenman 		/*
433089cdfadSRuslan Ermilov 		 * in_ifadown gets rid of all the rest of
434089cdfadSRuslan Ermilov 		 * the routes.  This is not quite the right
435089cdfadSRuslan Ermilov 		 * thing to do, but at least if we are running
436089cdfadSRuslan Ermilov 		 * a routing process they will come back.
437089cdfadSRuslan Ermilov 		 */
43891854268SRuslan Ermilov 		in_ifadown(&ia->ia_ifa, 1);
439e43cc4aeSHajimu UMEMOTO 		/*
440e43cc4aeSHajimu UMEMOTO 		 * XXX horrible hack to detect that we are being called
441e43cc4aeSHajimu UMEMOTO 		 * from if_detach()
442e43cc4aeSHajimu UMEMOTO 		 */
4439ce78778SSam Leffler 		if (ifaddr_byindex(ifp->if_index) == NULL) {
444f76fcf6dSJeffrey Hsu 			in_pcbpurgeif0(&ripcbinfo, ifp);
445f76fcf6dSJeffrey Hsu 			in_pcbpurgeif0(&udbinfo, ifp);
446e43cc4aeSHajimu UMEMOTO 		}
44725a4adceSMax Laier 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
4480f02fdacSBrian Somers 		error = 0;
449df8bae1dSRodney W. Grimes 		break;
450df8bae1dSRodney W. Grimes 
451df8bae1dSRodney W. Grimes 	default:
452df8bae1dSRodney W. Grimes 		if (ifp == 0 || ifp->if_ioctl == 0)
453df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
454df8bae1dSRodney W. Grimes 		return ((*ifp->if_ioctl)(ifp, cmd, data));
455df8bae1dSRodney W. Grimes 	}
4560f02fdacSBrian Somers 
4570f02fdacSBrian Somers 	/*
4580f02fdacSBrian Somers 	 * Protect from ipintr() traversing address list while we're modifying
4590f02fdacSBrian Somers 	 * it.
4600f02fdacSBrian Somers 	 */
4610f02fdacSBrian Somers 	s = splnet();
4620f02fdacSBrian Somers 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
4630f02fdacSBrian Somers 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
4640f02fdacSBrian Somers 	LIST_REMOVE(ia, ia_hash);
4650f02fdacSBrian Somers 	IFAFREE(&ia->ia_ifa);
4660f02fdacSBrian Somers 	splx(s);
4670f02fdacSBrian Somers 
4680f02fdacSBrian Somers 	return (error);
469df8bae1dSRodney W. Grimes }
470df8bae1dSRodney W. Grimes 
471df8bae1dSRodney W. Grimes /*
4726a800098SYoshinobu Inoue  * SIOC[GAD]LIFADDR.
4736a800098SYoshinobu Inoue  *	SIOCGLIFADDR: get first address. (?!?)
4746a800098SYoshinobu Inoue  *	SIOCGLIFADDR with IFLR_PREFIX:
4756a800098SYoshinobu Inoue  *		get first address that matches the specified prefix.
4766a800098SYoshinobu Inoue  *	SIOCALIFADDR: add the specified address.
4776a800098SYoshinobu Inoue  *	SIOCALIFADDR with IFLR_PREFIX:
4786a800098SYoshinobu Inoue  *		EINVAL since we can't deduce hostid part of the address.
4796a800098SYoshinobu Inoue  *	SIOCDLIFADDR: delete the specified address.
4806a800098SYoshinobu Inoue  *	SIOCDLIFADDR with IFLR_PREFIX:
4816a800098SYoshinobu Inoue  *		delete the first address that matches the specified prefix.
4826a800098SYoshinobu Inoue  * return values:
4836a800098SYoshinobu Inoue  *	EINVAL on invalid parameters
4846a800098SYoshinobu Inoue  *	EADDRNOTAVAIL on prefix match failed/specified address not found
4856a800098SYoshinobu Inoue  *	other values may be returned from in_ioctl()
4866a800098SYoshinobu Inoue  */
4876a800098SYoshinobu Inoue static int
488b40ce416SJulian Elischer in_lifaddr_ioctl(so, cmd, data, ifp, td)
4896a800098SYoshinobu Inoue 	struct socket *so;
4906a800098SYoshinobu Inoue 	u_long cmd;
4916a800098SYoshinobu Inoue 	caddr_t	data;
4926a800098SYoshinobu Inoue 	struct ifnet *ifp;
493b40ce416SJulian Elischer 	struct thread *td;
4946a800098SYoshinobu Inoue {
4956a800098SYoshinobu Inoue 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
4966a800098SYoshinobu Inoue 	struct ifaddr *ifa;
4976a800098SYoshinobu Inoue 
4986a800098SYoshinobu Inoue 	/* sanity checks */
4996a800098SYoshinobu Inoue 	if (!data || !ifp) {
5006a800098SYoshinobu Inoue 		panic("invalid argument to in_lifaddr_ioctl");
5016a800098SYoshinobu Inoue 		/*NOTRECHED*/
5026a800098SYoshinobu Inoue 	}
5036a800098SYoshinobu Inoue 
5046a800098SYoshinobu Inoue 	switch (cmd) {
5056a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
5066a800098SYoshinobu Inoue 		/* address must be specified on GET with IFLR_PREFIX */
5076a800098SYoshinobu Inoue 		if ((iflr->flags & IFLR_PREFIX) == 0)
5086a800098SYoshinobu Inoue 			break;
5096a800098SYoshinobu Inoue 		/*FALLTHROUGH*/
5106a800098SYoshinobu Inoue 	case SIOCALIFADDR:
5116a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
5126a800098SYoshinobu Inoue 		/* address must be specified on ADD and DELETE */
5135d60ed0eSYoshinobu Inoue 		if (iflr->addr.ss_family != AF_INET)
5146a800098SYoshinobu Inoue 			return EINVAL;
5155d60ed0eSYoshinobu Inoue 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
5166a800098SYoshinobu Inoue 			return EINVAL;
5176a800098SYoshinobu Inoue 		/* XXX need improvement */
5185d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family
5195d60ed0eSYoshinobu Inoue 		 && iflr->dstaddr.ss_family != AF_INET)
5206a800098SYoshinobu Inoue 			return EINVAL;
5215d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family
5225d60ed0eSYoshinobu Inoue 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
5236a800098SYoshinobu Inoue 			return EINVAL;
5246a800098SYoshinobu Inoue 		break;
5256a800098SYoshinobu Inoue 	default: /*shouldn't happen*/
5266a800098SYoshinobu Inoue 		return EOPNOTSUPP;
5276a800098SYoshinobu Inoue 	}
5286a800098SYoshinobu Inoue 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
5296a800098SYoshinobu Inoue 		return EINVAL;
5306a800098SYoshinobu Inoue 
5316a800098SYoshinobu Inoue 	switch (cmd) {
5326a800098SYoshinobu Inoue 	case SIOCALIFADDR:
5336a800098SYoshinobu Inoue 	    {
5346a800098SYoshinobu Inoue 		struct in_aliasreq ifra;
5356a800098SYoshinobu Inoue 
5366a800098SYoshinobu Inoue 		if (iflr->flags & IFLR_PREFIX)
5376a800098SYoshinobu Inoue 			return EINVAL;
5386a800098SYoshinobu Inoue 
5396a800098SYoshinobu Inoue 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
5406a800098SYoshinobu Inoue 		bzero(&ifra, sizeof(ifra));
5416a800098SYoshinobu Inoue 		bcopy(iflr->iflr_name, ifra.ifra_name,
5426a800098SYoshinobu Inoue 			sizeof(ifra.ifra_name));
5436a800098SYoshinobu Inoue 
5445d60ed0eSYoshinobu Inoue 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
5456a800098SYoshinobu Inoue 
5465d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family) {	/*XXX*/
5476a800098SYoshinobu Inoue 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
5485d60ed0eSYoshinobu Inoue 				iflr->dstaddr.ss_len);
5496a800098SYoshinobu Inoue 		}
5506a800098SYoshinobu Inoue 
5516a800098SYoshinobu Inoue 		ifra.ifra_mask.sin_family = AF_INET;
5526a800098SYoshinobu Inoue 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
5536a800098SYoshinobu Inoue 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
5546a800098SYoshinobu Inoue 
555b40ce416SJulian Elischer 		return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
5566a800098SYoshinobu Inoue 	    }
5576a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
5586a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
5596a800098SYoshinobu Inoue 	    {
5606a800098SYoshinobu Inoue 		struct in_ifaddr *ia;
5616a800098SYoshinobu Inoue 		struct in_addr mask, candidate, match;
5626a800098SYoshinobu Inoue 		struct sockaddr_in *sin;
5636a800098SYoshinobu Inoue 		int cmp;
5646a800098SYoshinobu Inoue 
5656a800098SYoshinobu Inoue 		bzero(&mask, sizeof(mask));
5666a800098SYoshinobu Inoue 		if (iflr->flags & IFLR_PREFIX) {
5676a800098SYoshinobu Inoue 			/* lookup a prefix rather than address. */
5686a800098SYoshinobu Inoue 			in_len2mask(&mask, iflr->prefixlen);
5696a800098SYoshinobu Inoue 
5706a800098SYoshinobu Inoue 			sin = (struct sockaddr_in *)&iflr->addr;
5716a800098SYoshinobu Inoue 			match.s_addr = sin->sin_addr.s_addr;
5726a800098SYoshinobu Inoue 			match.s_addr &= mask.s_addr;
5736a800098SYoshinobu Inoue 
5746a800098SYoshinobu Inoue 			/* if you set extra bits, that's wrong */
5756a800098SYoshinobu Inoue 			if (match.s_addr != sin->sin_addr.s_addr)
5766a800098SYoshinobu Inoue 				return EINVAL;
5776a800098SYoshinobu Inoue 
5786a800098SYoshinobu Inoue 			cmp = 1;
5796a800098SYoshinobu Inoue 		} else {
5806a800098SYoshinobu Inoue 			if (cmd == SIOCGLIFADDR) {
5816a800098SYoshinobu Inoue 				/* on getting an address, take the 1st match */
5826a800098SYoshinobu Inoue 				cmp = 0;	/*XXX*/
5836a800098SYoshinobu Inoue 			} else {
5846a800098SYoshinobu Inoue 				/* on deleting an address, do exact match */
5856a800098SYoshinobu Inoue 				in_len2mask(&mask, 32);
5866a800098SYoshinobu Inoue 				sin = (struct sockaddr_in *)&iflr->addr;
5876a800098SYoshinobu Inoue 				match.s_addr = sin->sin_addr.s_addr;
5886a800098SYoshinobu Inoue 
5896a800098SYoshinobu Inoue 				cmp = 1;
5906a800098SYoshinobu Inoue 			}
5916a800098SYoshinobu Inoue 		}
5926a800098SYoshinobu Inoue 
5936a800098SYoshinobu Inoue 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
5946a800098SYoshinobu Inoue 			if (ifa->ifa_addr->sa_family != AF_INET6)
5956a800098SYoshinobu Inoue 				continue;
5966a800098SYoshinobu Inoue 			if (!cmp)
5976a800098SYoshinobu Inoue 				break;
5986a800098SYoshinobu Inoue 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
5996a800098SYoshinobu Inoue 			candidate.s_addr &= mask.s_addr;
6006a800098SYoshinobu Inoue 			if (candidate.s_addr == match.s_addr)
6016a800098SYoshinobu Inoue 				break;
6026a800098SYoshinobu Inoue 		}
6036a800098SYoshinobu Inoue 		if (!ifa)
6046a800098SYoshinobu Inoue 			return EADDRNOTAVAIL;
6056a800098SYoshinobu Inoue 		ia = (struct in_ifaddr *)ifa;
6066a800098SYoshinobu Inoue 
6076a800098SYoshinobu Inoue 		if (cmd == SIOCGLIFADDR) {
6086a800098SYoshinobu Inoue 			/* fill in the if_laddrreq structure */
6096a800098SYoshinobu Inoue 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
6106a800098SYoshinobu Inoue 
6116a800098SYoshinobu Inoue 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
6126a800098SYoshinobu Inoue 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
6136a800098SYoshinobu Inoue 					ia->ia_dstaddr.sin_len);
6146a800098SYoshinobu Inoue 			} else
6156a800098SYoshinobu Inoue 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
6166a800098SYoshinobu Inoue 
6176a800098SYoshinobu Inoue 			iflr->prefixlen =
6186a800098SYoshinobu Inoue 				in_mask2len(&ia->ia_sockmask.sin_addr);
6196a800098SYoshinobu Inoue 
6206a800098SYoshinobu Inoue 			iflr->flags = 0;	/*XXX*/
6216a800098SYoshinobu Inoue 
6226a800098SYoshinobu Inoue 			return 0;
6236a800098SYoshinobu Inoue 		} else {
6246a800098SYoshinobu Inoue 			struct in_aliasreq ifra;
6256a800098SYoshinobu Inoue 
6266a800098SYoshinobu Inoue 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
6276a800098SYoshinobu Inoue 			bzero(&ifra, sizeof(ifra));
6286a800098SYoshinobu Inoue 			bcopy(iflr->iflr_name, ifra.ifra_name,
6296a800098SYoshinobu Inoue 				sizeof(ifra.ifra_name));
6306a800098SYoshinobu Inoue 
6316a800098SYoshinobu Inoue 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
6326a800098SYoshinobu Inoue 				ia->ia_addr.sin_len);
6336a800098SYoshinobu Inoue 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
6346a800098SYoshinobu Inoue 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
6356a800098SYoshinobu Inoue 					ia->ia_dstaddr.sin_len);
6366a800098SYoshinobu Inoue 			}
6376a800098SYoshinobu Inoue 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
6386a800098SYoshinobu Inoue 				ia->ia_sockmask.sin_len);
6396a800098SYoshinobu Inoue 
6406a800098SYoshinobu Inoue 			return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
641b40ce416SJulian Elischer 					  ifp, td);
6426a800098SYoshinobu Inoue 		}
6436a800098SYoshinobu Inoue 	    }
6446a800098SYoshinobu Inoue 	}
6456a800098SYoshinobu Inoue 
6466a800098SYoshinobu Inoue 	return EOPNOTSUPP;	/*just for safety*/
6476a800098SYoshinobu Inoue }
6486a800098SYoshinobu Inoue 
6496a800098SYoshinobu Inoue /*
650df8bae1dSRodney W. Grimes  * Delete any existing route for an interface.
651df8bae1dSRodney W. Grimes  */
65239191c8eSGarrett Wollman void
653df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia)
654df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
655df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
656df8bae1dSRodney W. Grimes {
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes 	if ((ia->ia_flags & IFA_ROUTE) == 0)
659df8bae1dSRodney W. Grimes 		return;
660df8bae1dSRodney W. Grimes 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
661df8bae1dSRodney W. Grimes 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
662df8bae1dSRodney W. Grimes 	else
663df8bae1dSRodney W. Grimes 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
664df8bae1dSRodney W. Grimes 	ia->ia_flags &= ~IFA_ROUTE;
665df8bae1dSRodney W. Grimes }
666df8bae1dSRodney W. Grimes 
667df8bae1dSRodney W. Grimes /*
668df8bae1dSRodney W. Grimes  * Initialize an interface's internet address
669df8bae1dSRodney W. Grimes  * and routing table entry.
670df8bae1dSRodney W. Grimes  */
6710312fbe9SPoul-Henning Kamp static int
672df8bae1dSRodney W. Grimes in_ifinit(ifp, ia, sin, scrub)
673df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
674df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
675df8bae1dSRodney W. Grimes 	struct sockaddr_in *sin;
676df8bae1dSRodney W. Grimes 	int scrub;
677df8bae1dSRodney W. Grimes {
678df8bae1dSRodney W. Grimes 	register u_long i = ntohl(sin->sin_addr.s_addr);
679df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
6805a43847dSBrian Somers 	int s = splimp(), flags = RTF_UP, error = 0;
681df8bae1dSRodney W. Grimes 
682df8bae1dSRodney W. Grimes 	oldaddr = ia->ia_addr;
6832754d95dSSUZUKI Shinsuke 	if (oldaddr.sin_family == AF_INET)
6842754d95dSSUZUKI Shinsuke 		LIST_REMOVE(ia, ia_hash);
685df8bae1dSRodney W. Grimes 	ia->ia_addr = *sin;
6862754d95dSSUZUKI Shinsuke 	if (ia->ia_addr.sin_family == AF_INET)
6872754d95dSSUZUKI Shinsuke 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
6882754d95dSSUZUKI Shinsuke 		    ia, ia_hash);
689df8bae1dSRodney W. Grimes 	/*
690df8bae1dSRodney W. Grimes 	 * Give the interface a chance to initialize
691df8bae1dSRodney W. Grimes 	 * if this is its first address,
692df8bae1dSRodney W. Grimes 	 * and to validate the address if necessary.
693df8bae1dSRodney W. Grimes 	 */
694df8bae1dSRodney W. Grimes 	if (ifp->if_ioctl &&
695df8bae1dSRodney W. Grimes 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
696df8bae1dSRodney W. Grimes 		splx(s);
6972754d95dSSUZUKI Shinsuke 		/* LIST_REMOVE(ia, ia_hash) is done in in_control */
698df8bae1dSRodney W. Grimes 		ia->ia_addr = oldaddr;
69922c819a7SJonathan Lemon 		if (ia->ia_addr.sin_family == AF_INET)
70022c819a7SJonathan Lemon 			LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
70122c819a7SJonathan Lemon 			    ia, ia_hash);
7022754d95dSSUZUKI Shinsuke 		return (error);
7032754d95dSSUZUKI Shinsuke 	}
704df8bae1dSRodney W. Grimes 	splx(s);
705df8bae1dSRodney W. Grimes 	if (scrub) {
706df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
707df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
708df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
709df8bae1dSRodney W. Grimes 	}
710df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i))
711df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSA_NET;
712df8bae1dSRodney W. Grimes 	else if (IN_CLASSB(i))
713df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSB_NET;
714df8bae1dSRodney W. Grimes 	else
715df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSC_NET;
716df8bae1dSRodney W. Grimes 	/*
717df8bae1dSRodney W. Grimes 	 * The subnet mask usually includes at least the standard network part,
718df8bae1dSRodney W. Grimes 	 * but may may be smaller in the case of supernetting.
719df8bae1dSRodney W. Grimes 	 * If it is set, we believe it.
720df8bae1dSRodney W. Grimes 	 */
721df8bae1dSRodney W. Grimes 	if (ia->ia_subnetmask == 0) {
722df8bae1dSRodney W. Grimes 		ia->ia_subnetmask = ia->ia_netmask;
723df8bae1dSRodney W. Grimes 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
724df8bae1dSRodney W. Grimes 	} else
725df8bae1dSRodney W. Grimes 		ia->ia_netmask &= ia->ia_subnetmask;
726df8bae1dSRodney W. Grimes 	ia->ia_net = i & ia->ia_netmask;
727df8bae1dSRodney W. Grimes 	ia->ia_subnet = i & ia->ia_subnetmask;
728df8bae1dSRodney W. Grimes 	in_socktrim(&ia->ia_sockmask);
729df8bae1dSRodney W. Grimes 	/*
730df8bae1dSRodney W. Grimes 	 * Add route for the network.
731df8bae1dSRodney W. Grimes 	 */
732df8bae1dSRodney W. Grimes 	ia->ia_ifa.ifa_metric = ifp->if_metric;
733df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_BROADCAST) {
734df8bae1dSRodney W. Grimes 		ia->ia_broadaddr.sin_addr.s_addr =
735df8bae1dSRodney W. Grimes 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
736df8bae1dSRodney W. Grimes 		ia->ia_netbroadcast.s_addr =
737df8bae1dSRodney W. Grimes 			htonl(ia->ia_net | ~ ia->ia_netmask);
738df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_LOOPBACK) {
739df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
740df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
741df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
742df8bae1dSRodney W. Grimes 		if (ia->ia_dstaddr.sin_family != AF_INET)
743df8bae1dSRodney W. Grimes 			return (0);
744df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
745df8bae1dSRodney W. Grimes 	}
7460f02fdacSBrian Somers 
7475a43847dSBrian Somers 	/*-
7485a43847dSBrian Somers 	 * Don't add host routes for interface addresses of
7495a43847dSBrian Somers 	 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
7505a43847dSBrian Somers 	 * possible to assign several such address pairs with consistent
7515a43847dSBrian Somers 	 * results (no host route) and is required by BOOTP.
7525a43847dSBrian Somers 	 *
7535a43847dSBrian Somers 	 * XXX: This is ugly !  There should be a way for the caller to
7545a43847dSBrian Somers 	 *      say that they don't want a host route.
7555a43847dSBrian Somers 	 */
7565a43847dSBrian Somers 	if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
7575a43847dSBrian Somers 	    ia->ia_netmask != IN_CLASSA_NET ||
7585a43847dSBrian Somers 	    ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
7595a43847dSBrian Somers 		if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
7600f02fdacSBrian Somers 			ia->ia_addr = oldaddr;
7610f02fdacSBrian Somers 			return (error);
7620f02fdacSBrian Somers 		}
7635a43847dSBrian Somers 		ia->ia_flags |= IFA_ROUTE;
7645a43847dSBrian Somers 	}
7650f02fdacSBrian Somers 
766df8bae1dSRodney W. Grimes 	/*
767df8bae1dSRodney W. Grimes 	 * If the interface supports multicast, join the "all hosts"
768df8bae1dSRodney W. Grimes 	 * multicast group on that interface.
769df8bae1dSRodney W. Grimes 	 */
770df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_MULTICAST) {
771df8bae1dSRodney W. Grimes 		struct in_addr addr;
772df8bae1dSRodney W. Grimes 
773df8bae1dSRodney W. Grimes 		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
774df8bae1dSRodney W. Grimes 		in_addmulti(&addr, ifp);
775df8bae1dSRodney W. Grimes 	}
776df8bae1dSRodney W. Grimes 	return (error);
777df8bae1dSRodney W. Grimes }
778df8bae1dSRodney W. Grimes 
779df8bae1dSRodney W. Grimes 
780df8bae1dSRodney W. Grimes /*
781df8bae1dSRodney W. Grimes  * Return 1 if the address might be a local broadcast address.
782df8bae1dSRodney W. Grimes  */
78326f9a767SRodney W. Grimes int
784df8bae1dSRodney W. Grimes in_broadcast(in, ifp)
785df8bae1dSRodney W. Grimes 	struct in_addr in;
786df8bae1dSRodney W. Grimes         struct ifnet *ifp;
787df8bae1dSRodney W. Grimes {
788df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
789df8bae1dSRodney W. Grimes 	u_long t;
790df8bae1dSRodney W. Grimes 
791df8bae1dSRodney W. Grimes 	if (in.s_addr == INADDR_BROADCAST ||
792df8bae1dSRodney W. Grimes 	    in.s_addr == INADDR_ANY)
793df8bae1dSRodney W. Grimes 		return 1;
794df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
795df8bae1dSRodney W. Grimes 		return 0;
796df8bae1dSRodney W. Grimes 	t = ntohl(in.s_addr);
797df8bae1dSRodney W. Grimes 	/*
798df8bae1dSRodney W. Grimes 	 * Look through the list of addresses for a match
799df8bae1dSRodney W. Grimes 	 * with a broadcast address.
800df8bae1dSRodney W. Grimes 	 */
801df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa)
802462b86feSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
803df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == AF_INET &&
804df8bae1dSRodney W. Grimes 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
805df8bae1dSRodney W. Grimes 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
806df8bae1dSRodney W. Grimes 		     /*
807df8bae1dSRodney W. Grimes 		      * Check for old-style (host 0) broadcast.
808df8bae1dSRodney W. Grimes 		      */
8098dd27fd6SGuido van Rooij 		     t == ia->ia_subnet || t == ia->ia_net) &&
8108dd27fd6SGuido van Rooij 		     /*
8118dd27fd6SGuido van Rooij 		      * Check for an all one subnetmask. These
8128dd27fd6SGuido van Rooij 		      * only exist when an interface gets a secondary
8138dd27fd6SGuido van Rooij 		      * address.
8148dd27fd6SGuido van Rooij 		      */
8158dd27fd6SGuido van Rooij 		     ia->ia_subnetmask != (u_long)0xffffffff)
816df8bae1dSRodney W. Grimes 			    return 1;
817df8bae1dSRodney W. Grimes 	return (0);
818df8bae1dSRodney W. Grimes #undef ia
819df8bae1dSRodney W. Grimes }
820df8bae1dSRodney W. Grimes /*
821df8bae1dSRodney W. Grimes  * Add an address to the list of IP multicast addresses for a given interface.
822df8bae1dSRodney W. Grimes  */
823df8bae1dSRodney W. Grimes struct in_multi *
824df8bae1dSRodney W. Grimes in_addmulti(ap, ifp)
825df8bae1dSRodney W. Grimes 	register struct in_addr *ap;
826df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
827df8bae1dSRodney W. Grimes {
828df8bae1dSRodney W. Grimes 	register struct in_multi *inm;
829477180fbSGarrett Wollman 	int error;
830477180fbSGarrett Wollman 	struct sockaddr_in sin;
831477180fbSGarrett Wollman 	struct ifmultiaddr *ifma;
832df8bae1dSRodney W. Grimes 	int s = splnet();
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes 	/*
835477180fbSGarrett Wollman 	 * Call generic routine to add membership or increment
836477180fbSGarrett Wollman 	 * refcount.  It wants addresses in the form of a sockaddr,
837477180fbSGarrett Wollman 	 * so we build one here (being careful to zero the unused bytes).
838df8bae1dSRodney W. Grimes 	 */
839477180fbSGarrett Wollman 	bzero(&sin, sizeof sin);
840477180fbSGarrett Wollman 	sin.sin_family = AF_INET;
841477180fbSGarrett Wollman 	sin.sin_len = sizeof sin;
842477180fbSGarrett Wollman 	sin.sin_addr = *ap;
843477180fbSGarrett Wollman 	error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
844477180fbSGarrett Wollman 	if (error) {
845477180fbSGarrett Wollman 		splx(s);
846477180fbSGarrett Wollman 		return 0;
847df8bae1dSRodney W. Grimes 	}
848477180fbSGarrett Wollman 
849df8bae1dSRodney W. Grimes 	/*
850477180fbSGarrett Wollman 	 * If ifma->ifma_protospec is null, then if_addmulti() created
851477180fbSGarrett Wollman 	 * a new record.  Otherwise, we are done.
852df8bae1dSRodney W. Grimes 	 */
8534153a3a3SBruce Evans 	if (ifma->ifma_protospec != 0) {
8544153a3a3SBruce Evans 		splx(s);
855477180fbSGarrett Wollman 		return ifma->ifma_protospec;
8564153a3a3SBruce Evans 	}
857477180fbSGarrett Wollman 
858a163d034SWarner Losh 	/* XXX - if_addmulti uses M_WAITOK.  Can this really be called
859477180fbSGarrett Wollman 	   at interrupt time?  If so, need to fix if_addmulti. XXX */
8607cc0979fSDavid Malone 	inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
8617cc0979fSDavid Malone 	    M_NOWAIT | M_ZERO);
862df8bae1dSRodney W. Grimes 	if (inm == NULL) {
863df8bae1dSRodney W. Grimes 		splx(s);
864df8bae1dSRodney W. Grimes 		return (NULL);
865df8bae1dSRodney W. Grimes 	}
866477180fbSGarrett Wollman 
867df8bae1dSRodney W. Grimes 	inm->inm_addr = *ap;
868df8bae1dSRodney W. Grimes 	inm->inm_ifp = ifp;
869477180fbSGarrett Wollman 	inm->inm_ifma = ifma;
870477180fbSGarrett Wollman 	ifma->ifma_protospec = inm;
871477180fbSGarrett Wollman 	LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
872ffa5b11aSGarrett Wollman 
873df8bae1dSRodney W. Grimes 	/*
874df8bae1dSRodney W. Grimes 	 * Let IGMP know that we have joined a new IP multicast group.
875df8bae1dSRodney W. Grimes 	 */
876df8bae1dSRodney W. Grimes 	igmp_joingroup(inm);
877df8bae1dSRodney W. Grimes 	splx(s);
878df8bae1dSRodney W. Grimes 	return (inm);
879df8bae1dSRodney W. Grimes }
880df8bae1dSRodney W. Grimes 
881df8bae1dSRodney W. Grimes /*
882df8bae1dSRodney W. Grimes  * Delete a multicast address record.
883df8bae1dSRodney W. Grimes  */
88426f9a767SRodney W. Grimes void
885df8bae1dSRodney W. Grimes in_delmulti(inm)
886df8bae1dSRodney W. Grimes 	register struct in_multi *inm;
887df8bae1dSRodney W. Grimes {
888477180fbSGarrett Wollman 	struct ifmultiaddr *ifma = inm->inm_ifma;
88988a5354eSLuigi Rizzo 	struct in_multi my_inm;
890df8bae1dSRodney W. Grimes 	int s = splnet();
891df8bae1dSRodney W. Grimes 
89288a5354eSLuigi Rizzo 	my_inm.inm_ifp = NULL ; /* don't send the leave msg */
893477180fbSGarrett Wollman 	if (ifma->ifma_refcount == 1) {
894df8bae1dSRodney W. Grimes 		/*
895df8bae1dSRodney W. Grimes 		 * No remaining claims to this record; let IGMP know that
896df8bae1dSRodney W. Grimes 		 * we are leaving the multicast group.
89788a5354eSLuigi Rizzo 		 * But do it after the if_delmulti() which might reset
89888a5354eSLuigi Rizzo 		 * the interface and nuke the packet.
899df8bae1dSRodney W. Grimes 		 */
90088a5354eSLuigi Rizzo 		my_inm = *inm ;
901477180fbSGarrett Wollman 		ifma->ifma_protospec = 0;
902477180fbSGarrett Wollman 		LIST_REMOVE(inm, inm_link);
903df8bae1dSRodney W. Grimes 		free(inm, M_IPMADDR);
904df8bae1dSRodney W. Grimes 	}
905477180fbSGarrett Wollman 	/* XXX - should be separate API for when we have an ifma? */
906477180fbSGarrett Wollman 	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
90788a5354eSLuigi Rizzo 	if (my_inm.inm_ifp != NULL)
90888a5354eSLuigi Rizzo 		igmp_leavegroup(&my_inm);
909df8bae1dSRodney W. Grimes 	splx(s);
910df8bae1dSRodney W. Grimes }
911