xref: /freebsd/sys/netinet/in.c (revision 19fc74fb60b2ccd4f1c64f561cde5d0c9835e457)
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
332180b925SGarrett Wollman  *	@(#)in.c	8.4 (Berkeley) 1/9/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
3826f9a767SRodney W. Grimes #include <sys/systm.h>
3951a53488SBruce Evans #include <sys/sockio.h>
40df8bae1dSRodney W. Grimes #include <sys/malloc.h>
41df8bae1dSRodney W. Grimes #include <sys/socket.h>
42f6d24a78SPoul-Henning Kamp #include <sys/kernel.h>
43f6d24a78SPoul-Henning Kamp #include <sys/sysctl.h>
44df8bae1dSRodney W. Grimes 
45df8bae1dSRodney W. Grimes #include <net/if.h>
466a800098SYoshinobu Inoue #include <net/if_types.h>
47df8bae1dSRodney W. Grimes #include <net/route.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <netinet/in.h>
50df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
51e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h>
52df8bae1dSRodney W. Grimes 
53c70f4510SPoul-Henning Kamp #include <netinet/igmp_var.h>
54c70f4510SPoul-Henning Kamp 
55a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
5655166637SPoul-Henning Kamp 
574d77a549SAlfred Perlstein static int in_mask2len(struct in_addr *);
584d77a549SAlfred Perlstein static void in_len2mask(struct in_addr *, int);
594d77a549SAlfred Perlstein static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
604d77a549SAlfred Perlstein 	struct ifnet *, struct thread *);
616a800098SYoshinobu Inoue 
624d77a549SAlfred Perlstein static void	in_socktrim(struct sockaddr_in *);
634d77a549SAlfred Perlstein static int	in_ifinit(struct ifnet *,
644d77a549SAlfred Perlstein 	    struct in_ifaddr *, struct sockaddr_in *, int);
65df8bae1dSRodney W. Grimes 
66f8731310SGarrett Wollman static int subnetsarelocal = 0;
67f6d24a78SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
68f6d24a78SPoul-Henning Kamp 	&subnetsarelocal, 0, "");
69477180fbSGarrett Wollman 
70477180fbSGarrett Wollman struct in_multihead in_multihead; /* XXX BSS initialization */
71477180fbSGarrett Wollman 
72e43cc4aeSHajimu UMEMOTO extern struct inpcbinfo ripcbinfo;
73e43cc4aeSHajimu UMEMOTO extern struct inpcbinfo udbinfo;
74e43cc4aeSHajimu UMEMOTO 
75df8bae1dSRodney W. Grimes /*
76df8bae1dSRodney W. Grimes  * Return 1 if an internet address is for a ``local'' host
77df8bae1dSRodney W. Grimes  * (one to which we have a connection).  If subnetsarelocal
78df8bae1dSRodney W. Grimes  * is true, this includes other subnets of the local net.
79df8bae1dSRodney W. Grimes  * Otherwise, it includes only the directly-connected (sub)nets.
80df8bae1dSRodney W. Grimes  */
8126f9a767SRodney W. Grimes int
82df8bae1dSRodney W. Grimes in_localaddr(in)
83df8bae1dSRodney W. Grimes 	struct in_addr in;
84df8bae1dSRodney W. Grimes {
85df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
86df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes 	if (subnetsarelocal) {
89462b86feSPoul-Henning Kamp 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
90df8bae1dSRodney W. Grimes 			if ((i & ia->ia_netmask) == ia->ia_net)
91df8bae1dSRodney W. Grimes 				return (1);
92df8bae1dSRodney W. Grimes 	} else {
9337d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
94df8bae1dSRodney W. Grimes 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
95df8bae1dSRodney W. Grimes 				return (1);
96df8bae1dSRodney W. Grimes 	}
97df8bae1dSRodney W. Grimes 	return (0);
98df8bae1dSRodney W. Grimes }
99df8bae1dSRodney W. Grimes 
100df8bae1dSRodney W. Grimes /*
101df8bae1dSRodney W. Grimes  * Determine whether an IP address is in a reserved set of addresses
102df8bae1dSRodney W. Grimes  * that may not be forwarded, or whether datagrams to that destination
103df8bae1dSRodney W. Grimes  * may be forwarded.
104df8bae1dSRodney W. Grimes  */
10526f9a767SRodney W. Grimes int
106df8bae1dSRodney W. Grimes in_canforward(in)
107df8bae1dSRodney W. Grimes 	struct in_addr in;
108df8bae1dSRodney W. Grimes {
109df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
110df8bae1dSRodney W. Grimes 	register u_long net;
111df8bae1dSRodney W. Grimes 
112df8bae1dSRodney W. Grimes 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
113df8bae1dSRodney W. Grimes 		return (0);
114df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i)) {
115df8bae1dSRodney W. Grimes 		net = i & IN_CLASSA_NET;
116df8bae1dSRodney W. Grimes 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
117df8bae1dSRodney W. Grimes 			return (0);
118df8bae1dSRodney W. Grimes 	}
119df8bae1dSRodney W. Grimes 	return (1);
120df8bae1dSRodney W. Grimes }
121df8bae1dSRodney W. Grimes 
122df8bae1dSRodney W. Grimes /*
123df8bae1dSRodney W. Grimes  * Trim a mask in a sockaddr
124df8bae1dSRodney W. Grimes  */
1250312fbe9SPoul-Henning Kamp static void
126df8bae1dSRodney W. Grimes in_socktrim(ap)
127df8bae1dSRodney W. Grimes struct sockaddr_in *ap;
128df8bae1dSRodney W. Grimes {
129df8bae1dSRodney W. Grimes     register char *cplim = (char *) &ap->sin_addr;
130df8bae1dSRodney W. Grimes     register char *cp = (char *) (&ap->sin_addr + 1);
131df8bae1dSRodney W. Grimes 
132df8bae1dSRodney W. Grimes     ap->sin_len = 0;
133df00058dSGarrett Wollman     while (--cp >= cplim)
134df8bae1dSRodney W. Grimes         if (*cp) {
135df8bae1dSRodney W. Grimes 	    (ap)->sin_len = cp - (char *) (ap) + 1;
136df8bae1dSRodney W. Grimes 	    break;
137df8bae1dSRodney W. Grimes 	}
138df8bae1dSRodney W. Grimes }
139df8bae1dSRodney W. Grimes 
1406a800098SYoshinobu Inoue static int
1416a800098SYoshinobu Inoue in_mask2len(mask)
1426a800098SYoshinobu Inoue 	struct in_addr *mask;
1436a800098SYoshinobu Inoue {
1446a800098SYoshinobu Inoue 	int x, y;
1456a800098SYoshinobu Inoue 	u_char *p;
1466a800098SYoshinobu Inoue 
1476a800098SYoshinobu Inoue 	p = (u_char *)mask;
1486a800098SYoshinobu Inoue 	for (x = 0; x < sizeof(*mask); x++) {
1496a800098SYoshinobu Inoue 		if (p[x] != 0xff)
1506a800098SYoshinobu Inoue 			break;
1516a800098SYoshinobu Inoue 	}
1526a800098SYoshinobu Inoue 	y = 0;
1536a800098SYoshinobu Inoue 	if (x < sizeof(*mask)) {
1546a800098SYoshinobu Inoue 		for (y = 0; y < 8; y++) {
1556a800098SYoshinobu Inoue 			if ((p[x] & (0x80 >> y)) == 0)
1566a800098SYoshinobu Inoue 				break;
1576a800098SYoshinobu Inoue 		}
1586a800098SYoshinobu Inoue 	}
1596a800098SYoshinobu Inoue 	return x * 8 + y;
1606a800098SYoshinobu Inoue }
1616a800098SYoshinobu Inoue 
1626a800098SYoshinobu Inoue static void
1636a800098SYoshinobu Inoue in_len2mask(mask, len)
1646a800098SYoshinobu Inoue 	struct in_addr *mask;
1656a800098SYoshinobu Inoue 	int len;
1666a800098SYoshinobu Inoue {
1676a800098SYoshinobu Inoue 	int i;
1686a800098SYoshinobu Inoue 	u_char *p;
1696a800098SYoshinobu Inoue 
1706a800098SYoshinobu Inoue 	p = (u_char *)mask;
1716a800098SYoshinobu Inoue 	bzero(mask, sizeof(*mask));
1726a800098SYoshinobu Inoue 	for (i = 0; i < len / 8; i++)
1736a800098SYoshinobu Inoue 		p[i] = 0xff;
1746a800098SYoshinobu Inoue 	if (len % 8)
1756a800098SYoshinobu Inoue 		p[i] = (0xff00 >> (len % 8)) & 0xff;
1766a800098SYoshinobu Inoue }
1776a800098SYoshinobu Inoue 
178df8bae1dSRodney W. Grimes /*
179df8bae1dSRodney W. Grimes  * Generic internet control operations (ioctl's).
180df8bae1dSRodney W. Grimes  * Ifp is 0 if not an interface-specific ioctl.
181df8bae1dSRodney W. Grimes  */
182df8bae1dSRodney W. Grimes /* ARGSUSED */
18326f9a767SRodney W. Grimes int
184b40ce416SJulian Elischer in_control(so, cmd, data, ifp, td)
185df8bae1dSRodney W. Grimes 	struct socket *so;
186ecbb00a2SDoug Rabson 	u_long cmd;
187df8bae1dSRodney W. Grimes 	caddr_t data;
188df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
189b40ce416SJulian Elischer 	struct thread *td;
190df8bae1dSRodney W. Grimes {
191df8bae1dSRodney W. Grimes 	register struct ifreq *ifr = (struct ifreq *)data;
192ac0aa473SBill Fenner 	register struct in_ifaddr *ia = 0, *iap;
193df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
194ca925d9cSJonathan Lemon 	struct in_addr dst;
195df8bae1dSRodney W. Grimes 	struct in_ifaddr *oia;
196df8bae1dSRodney W. Grimes 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
197df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
1980f02fdacSBrian Somers 	int error, hostIsNew, iaIsNew, maskIsNew, s;
1990f02fdacSBrian Somers 
2000f02fdacSBrian Somers 	iaIsNew = 0;
201df8bae1dSRodney W. Grimes 
2026a800098SYoshinobu Inoue 	switch (cmd) {
2036a800098SYoshinobu Inoue 	case SIOCALIFADDR:
2046a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
20544731cabSJohn Baldwin 		if (td && (error = suser(td)) != 0)
2066a800098SYoshinobu Inoue 			return error;
2076a800098SYoshinobu Inoue 		/*fall through*/
2086a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
2096a800098SYoshinobu Inoue 		if (!ifp)
2106a800098SYoshinobu Inoue 			return EINVAL;
211b40ce416SJulian Elischer 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
2126a800098SYoshinobu Inoue 	}
2136a800098SYoshinobu Inoue 
214df8bae1dSRodney W. Grimes 	/*
215df8bae1dSRodney W. Grimes 	 * Find address for this interface, if it exists.
216ac0aa473SBill Fenner 	 *
217ac0aa473SBill Fenner 	 * If an alias address was specified, find that one instead of
218ca925d9cSJonathan Lemon 	 * the first one on the interface, if possible.
219df8bae1dSRodney W. Grimes 	 */
220ca925d9cSJonathan Lemon 	if (ifp) {
221ca925d9cSJonathan Lemon 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
222ca925d9cSJonathan Lemon 		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
223ca925d9cSJonathan Lemon 			if (iap->ia_ifp == ifp &&
224ca925d9cSJonathan Lemon 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
225ac0aa473SBill Fenner 				ia = iap;
226df8bae1dSRodney W. Grimes 				break;
227ca925d9cSJonathan Lemon 			}
228ca925d9cSJonathan Lemon 		if (ia == NULL)
229ca925d9cSJonathan Lemon 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
230ca925d9cSJonathan Lemon 				iap = ifatoia(ifa);
231ca925d9cSJonathan Lemon 				if (iap->ia_addr.sin_family == AF_INET) {
232ac0aa473SBill Fenner 					ia = iap;
233ac0aa473SBill Fenner 					break;
234ac0aa473SBill Fenner 				}
235ac0aa473SBill Fenner 			}
236ca925d9cSJonathan Lemon 	}
237df8bae1dSRodney W. Grimes 
238df8bae1dSRodney W. Grimes 	switch (cmd) {
239df8bae1dSRodney W. Grimes 
240df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
241df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
2426572231dSEivind Eklund 		if (ifp == 0)
2436572231dSEivind Eklund 			return (EADDRNOTAVAIL);
2441067217dSGarrett Wollman 		if (ifra->ifra_addr.sin_family == AF_INET) {
245fc2ffbe6SPoul-Henning Kamp 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
246df8bae1dSRodney W. Grimes 				if (ia->ia_ifp == ifp  &&
247df8bae1dSRodney W. Grimes 				    ia->ia_addr.sin_addr.s_addr ==
248df8bae1dSRodney W. Grimes 				    ifra->ifra_addr.sin_addr.s_addr)
249df8bae1dSRodney W. Grimes 					break;
250df8bae1dSRodney W. Grimes 			}
2511067217dSGarrett Wollman 			if ((ifp->if_flags & IFF_POINTOPOINT)
2521067217dSGarrett Wollman 			    && (cmd == SIOCAIFADDR)
2531067217dSGarrett Wollman 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
2541067217dSGarrett Wollman 				== INADDR_ANY)) {
255357b78a9SGarrett Wollman 				return EDESTADDRREQ;
2561067217dSGarrett Wollman 			}
2571067217dSGarrett Wollman 		}
258df8bae1dSRodney W. Grimes 		if (cmd == SIOCDIFADDR && ia == 0)
259df8bae1dSRodney W. Grimes 			return (EADDRNOTAVAIL);
260df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
261df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
262df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
263df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
26444731cabSJohn Baldwin 		if (td && (error = suser(td)) != 0)
265a29f300eSGarrett Wollman 			return error;
266df8bae1dSRodney W. Grimes 
267df8bae1dSRodney W. Grimes 		if (ifp == 0)
2686572231dSEivind Eklund 			return (EADDRNOTAVAIL);
269df8bae1dSRodney W. Grimes 		if (ia == (struct in_ifaddr *)0) {
27059562606SGarrett Wollman 			ia = (struct in_ifaddr *)
2717cc0979fSDavid Malone 				malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
27259562606SGarrett Wollman 			if (ia == (struct in_ifaddr *)NULL)
273df8bae1dSRodney W. Grimes 				return (ENOBUFS);
274c655b7c4SDavid Greenman 			/*
275c655b7c4SDavid Greenman 			 * Protect from ipintr() traversing address list
276c655b7c4SDavid Greenman 			 * while we're modifying it.
277c655b7c4SDavid Greenman 			 */
278c655b7c4SDavid Greenman 			s = splnet();
27959562606SGarrett Wollman 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
28059562606SGarrett Wollman 
28119fc74fbSJeffrey Hsu 			ifa = &ia->ia_ifa;
28219fc74fbSJeffrey Hsu 			IFA_LOCK_INIT(ifa);
28359562606SGarrett Wollman 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
28459562606SGarrett Wollman 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
28559562606SGarrett Wollman 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
28619fc74fbSJeffrey Hsu 			ifa->ifa_refcnt = 1;
28719fc74fbSJeffrey Hsu 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
28819fc74fbSJeffrey Hsu 
289df8bae1dSRodney W. Grimes 			ia->ia_sockmask.sin_len = 8;
290bc183b3fSDag-Erling Smørgrav 			ia->ia_sockmask.sin_family = AF_INET;
291df8bae1dSRodney W. Grimes 			if (ifp->if_flags & IFF_BROADCAST) {
292df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
293df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_family = AF_INET;
294df8bae1dSRodney W. Grimes 			}
295df8bae1dSRodney W. Grimes 			ia->ia_ifp = ifp;
296c655b7c4SDavid Greenman 			splx(s);
2970f02fdacSBrian Somers 			iaIsNew = 1;
298df8bae1dSRodney W. Grimes 		}
299df8bae1dSRodney W. Grimes 		break;
300df8bae1dSRodney W. Grimes 
301df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
30244731cabSJohn Baldwin 		if (td && (error = suser(td)) != 0)
303a29f300eSGarrett Wollman 			return error;
304df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
305df8bae1dSRodney W. Grimes 
306df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
307df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
308df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
309df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
310df8bae1dSRodney W. Grimes 		if (ia == (struct in_ifaddr *)0)
311df8bae1dSRodney W. Grimes 			return (EADDRNOTAVAIL);
312df8bae1dSRodney W. Grimes 		break;
313df8bae1dSRodney W. Grimes 	}
314df8bae1dSRodney W. Grimes 	switch (cmd) {
315df8bae1dSRodney W. Grimes 
316df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
317df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
3180f02fdacSBrian Somers 		return (0);
319df8bae1dSRodney W. Grimes 
320df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
321df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
322df8bae1dSRodney W. Grimes 			return (EINVAL);
323df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
3240f02fdacSBrian Somers 		return (0);
325df8bae1dSRodney W. Grimes 
326df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
327df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
328df8bae1dSRodney W. Grimes 			return (EINVAL);
329df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
3300f02fdacSBrian Somers 		return (0);
331df8bae1dSRodney W. Grimes 
332df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
333df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
3340f02fdacSBrian Somers 		return (0);
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
337df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
338df8bae1dSRodney W. Grimes 			return (EINVAL);
339df8bae1dSRodney W. Grimes 		oldaddr = ia->ia_dstaddr;
340df8bae1dSRodney W. Grimes 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
341df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
342df8bae1dSRodney W. Grimes 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
343df8bae1dSRodney W. Grimes 			ia->ia_dstaddr = oldaddr;
344df8bae1dSRodney W. Grimes 			return (error);
345df8bae1dSRodney W. Grimes 		}
346df8bae1dSRodney W. Grimes 		if (ia->ia_flags & IFA_ROUTE) {
347df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
348df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
349df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr =
350df8bae1dSRodney W. Grimes 					(struct sockaddr *)&ia->ia_dstaddr;
351df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
352df8bae1dSRodney W. Grimes 		}
3530f02fdacSBrian Somers 		return (0);
354df8bae1dSRodney W. Grimes 
355df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
356df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
357df8bae1dSRodney W. Grimes 			return (EINVAL);
358df8bae1dSRodney W. Grimes 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
3590f02fdacSBrian Somers 		return (0);
360df8bae1dSRodney W. Grimes 
361df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
3620f02fdacSBrian Somers 		error = in_ifinit(ifp, ia,
3630f02fdacSBrian Somers 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
3640f02fdacSBrian Somers 		if (error != 0 && iaIsNew)
3650f02fdacSBrian Somers 			break;
3660f02fdacSBrian Somers 		return (0);
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
369bc183b3fSDag-Erling Smørgrav 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
370bc183b3fSDag-Erling Smørgrav 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
3710f02fdacSBrian Somers 		return (0);
372df8bae1dSRodney W. Grimes 
373df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
374df8bae1dSRodney W. Grimes 		maskIsNew = 0;
375df8bae1dSRodney W. Grimes 		hostIsNew = 1;
376df8bae1dSRodney W. Grimes 		error = 0;
377df8bae1dSRodney W. Grimes 		if (ia->ia_addr.sin_family == AF_INET) {
378df8bae1dSRodney W. Grimes 			if (ifra->ifra_addr.sin_len == 0) {
379df8bae1dSRodney W. Grimes 				ifra->ifra_addr = ia->ia_addr;
380df8bae1dSRodney W. Grimes 				hostIsNew = 0;
381df8bae1dSRodney W. Grimes 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
382df8bae1dSRodney W. Grimes 					       ia->ia_addr.sin_addr.s_addr)
383df8bae1dSRodney W. Grimes 				hostIsNew = 0;
384df8bae1dSRodney W. Grimes 		}
385df8bae1dSRodney W. Grimes 		if (ifra->ifra_mask.sin_len) {
386df8bae1dSRodney W. Grimes 			in_ifscrub(ifp, ia);
387df8bae1dSRodney W. Grimes 			ia->ia_sockmask = ifra->ifra_mask;
388bc183b3fSDag-Erling Smørgrav 			ia->ia_sockmask.sin_family = AF_INET;
389df8bae1dSRodney W. Grimes 			ia->ia_subnetmask =
390df8bae1dSRodney W. Grimes 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
391df8bae1dSRodney W. Grimes 			maskIsNew = 1;
392df8bae1dSRodney W. Grimes 		}
393df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
394df8bae1dSRodney W. Grimes 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
395df8bae1dSRodney W. Grimes 			in_ifscrub(ifp, ia);
396df8bae1dSRodney W. Grimes 			ia->ia_dstaddr = ifra->ifra_dstaddr;
397df8bae1dSRodney W. Grimes 			maskIsNew  = 1; /* We lie; but the effect's the same */
398df8bae1dSRodney W. Grimes 		}
399df8bae1dSRodney W. Grimes 		if (ifra->ifra_addr.sin_family == AF_INET &&
400df8bae1dSRodney W. Grimes 		    (hostIsNew || maskIsNew))
401df8bae1dSRodney W. Grimes 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
4020f02fdacSBrian Somers 		if (error != 0 && iaIsNew)
4030f02fdacSBrian Somers 			break;
4040f02fdacSBrian Somers 
405df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) &&
406df8bae1dSRodney W. Grimes 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
407df8bae1dSRodney W. Grimes 			ia->ia_broadaddr = ifra->ifra_broadaddr;
408df8bae1dSRodney W. Grimes 		return (error);
409df8bae1dSRodney W. Grimes 
410df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
411089cdfadSRuslan Ermilov 		/*
412089cdfadSRuslan Ermilov 		 * in_ifscrub kills the interface route.
413089cdfadSRuslan Ermilov 		 */
414df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
415c655b7c4SDavid Greenman 		/*
416089cdfadSRuslan Ermilov 		 * in_ifadown gets rid of all the rest of
417089cdfadSRuslan Ermilov 		 * the routes.  This is not quite the right
418089cdfadSRuslan Ermilov 		 * thing to do, but at least if we are running
419089cdfadSRuslan Ermilov 		 * a routing process they will come back.
420089cdfadSRuslan Ermilov 		 */
42191854268SRuslan Ermilov 		in_ifadown(&ia->ia_ifa, 1);
422e43cc4aeSHajimu UMEMOTO 		/*
423e43cc4aeSHajimu UMEMOTO 		 * XXX horrible hack to detect that we are being called
424e43cc4aeSHajimu UMEMOTO 		 * from if_detach()
425e43cc4aeSHajimu UMEMOTO 		 */
426f9132cebSJonathan Lemon 		if (ifaddr_byindex(ifp->if_index) != NULL) {
427f76fcf6dSJeffrey Hsu 			in_pcbpurgeif0(&ripcbinfo, ifp);
428f76fcf6dSJeffrey Hsu 			in_pcbpurgeif0(&udbinfo, ifp);
429e43cc4aeSHajimu UMEMOTO 		}
4300f02fdacSBrian Somers 		error = 0;
431df8bae1dSRodney W. Grimes 		break;
432df8bae1dSRodney W. Grimes 
433df8bae1dSRodney W. Grimes 	default:
434df8bae1dSRodney W. Grimes 		if (ifp == 0 || ifp->if_ioctl == 0)
435df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
436df8bae1dSRodney W. Grimes 		return ((*ifp->if_ioctl)(ifp, cmd, data));
437df8bae1dSRodney W. Grimes 	}
4380f02fdacSBrian Somers 
4390f02fdacSBrian Somers 	/*
4400f02fdacSBrian Somers 	 * Protect from ipintr() traversing address list while we're modifying
4410f02fdacSBrian Somers 	 * it.
4420f02fdacSBrian Somers 	 */
4430f02fdacSBrian Somers 	s = splnet();
4440f02fdacSBrian Somers 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
4450f02fdacSBrian Somers 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
4460f02fdacSBrian Somers 	LIST_REMOVE(ia, ia_hash);
4470f02fdacSBrian Somers 	IFAFREE(&ia->ia_ifa);
4480f02fdacSBrian Somers 	splx(s);
4490f02fdacSBrian Somers 
4500f02fdacSBrian Somers 	return (error);
451df8bae1dSRodney W. Grimes }
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes /*
4546a800098SYoshinobu Inoue  * SIOC[GAD]LIFADDR.
4556a800098SYoshinobu Inoue  *	SIOCGLIFADDR: get first address. (?!?)
4566a800098SYoshinobu Inoue  *	SIOCGLIFADDR with IFLR_PREFIX:
4576a800098SYoshinobu Inoue  *		get first address that matches the specified prefix.
4586a800098SYoshinobu Inoue  *	SIOCALIFADDR: add the specified address.
4596a800098SYoshinobu Inoue  *	SIOCALIFADDR with IFLR_PREFIX:
4606a800098SYoshinobu Inoue  *		EINVAL since we can't deduce hostid part of the address.
4616a800098SYoshinobu Inoue  *	SIOCDLIFADDR: delete the specified address.
4626a800098SYoshinobu Inoue  *	SIOCDLIFADDR with IFLR_PREFIX:
4636a800098SYoshinobu Inoue  *		delete the first address that matches the specified prefix.
4646a800098SYoshinobu Inoue  * return values:
4656a800098SYoshinobu Inoue  *	EINVAL on invalid parameters
4666a800098SYoshinobu Inoue  *	EADDRNOTAVAIL on prefix match failed/specified address not found
4676a800098SYoshinobu Inoue  *	other values may be returned from in_ioctl()
4686a800098SYoshinobu Inoue  */
4696a800098SYoshinobu Inoue static int
470b40ce416SJulian Elischer in_lifaddr_ioctl(so, cmd, data, ifp, td)
4716a800098SYoshinobu Inoue 	struct socket *so;
4726a800098SYoshinobu Inoue 	u_long cmd;
4736a800098SYoshinobu Inoue 	caddr_t	data;
4746a800098SYoshinobu Inoue 	struct ifnet *ifp;
475b40ce416SJulian Elischer 	struct thread *td;
4766a800098SYoshinobu Inoue {
4776a800098SYoshinobu Inoue 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
4786a800098SYoshinobu Inoue 	struct ifaddr *ifa;
4796a800098SYoshinobu Inoue 
4806a800098SYoshinobu Inoue 	/* sanity checks */
4816a800098SYoshinobu Inoue 	if (!data || !ifp) {
4826a800098SYoshinobu Inoue 		panic("invalid argument to in_lifaddr_ioctl");
4836a800098SYoshinobu Inoue 		/*NOTRECHED*/
4846a800098SYoshinobu Inoue 	}
4856a800098SYoshinobu Inoue 
4866a800098SYoshinobu Inoue 	switch (cmd) {
4876a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
4886a800098SYoshinobu Inoue 		/* address must be specified on GET with IFLR_PREFIX */
4896a800098SYoshinobu Inoue 		if ((iflr->flags & IFLR_PREFIX) == 0)
4906a800098SYoshinobu Inoue 			break;
4916a800098SYoshinobu Inoue 		/*FALLTHROUGH*/
4926a800098SYoshinobu Inoue 	case SIOCALIFADDR:
4936a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
4946a800098SYoshinobu Inoue 		/* address must be specified on ADD and DELETE */
4955d60ed0eSYoshinobu Inoue 		if (iflr->addr.ss_family != AF_INET)
4966a800098SYoshinobu Inoue 			return EINVAL;
4975d60ed0eSYoshinobu Inoue 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
4986a800098SYoshinobu Inoue 			return EINVAL;
4996a800098SYoshinobu Inoue 		/* XXX need improvement */
5005d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family
5015d60ed0eSYoshinobu Inoue 		 && iflr->dstaddr.ss_family != AF_INET)
5026a800098SYoshinobu Inoue 			return EINVAL;
5035d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family
5045d60ed0eSYoshinobu Inoue 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
5056a800098SYoshinobu Inoue 			return EINVAL;
5066a800098SYoshinobu Inoue 		break;
5076a800098SYoshinobu Inoue 	default: /*shouldn't happen*/
5086a800098SYoshinobu Inoue 		return EOPNOTSUPP;
5096a800098SYoshinobu Inoue 	}
5106a800098SYoshinobu Inoue 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
5116a800098SYoshinobu Inoue 		return EINVAL;
5126a800098SYoshinobu Inoue 
5136a800098SYoshinobu Inoue 	switch (cmd) {
5146a800098SYoshinobu Inoue 	case SIOCALIFADDR:
5156a800098SYoshinobu Inoue 	    {
5166a800098SYoshinobu Inoue 		struct in_aliasreq ifra;
5176a800098SYoshinobu Inoue 
5186a800098SYoshinobu Inoue 		if (iflr->flags & IFLR_PREFIX)
5196a800098SYoshinobu Inoue 			return EINVAL;
5206a800098SYoshinobu Inoue 
5216a800098SYoshinobu Inoue 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
5226a800098SYoshinobu Inoue 		bzero(&ifra, sizeof(ifra));
5236a800098SYoshinobu Inoue 		bcopy(iflr->iflr_name, ifra.ifra_name,
5246a800098SYoshinobu Inoue 			sizeof(ifra.ifra_name));
5256a800098SYoshinobu Inoue 
5265d60ed0eSYoshinobu Inoue 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
5276a800098SYoshinobu Inoue 
5285d60ed0eSYoshinobu Inoue 		if (iflr->dstaddr.ss_family) {	/*XXX*/
5296a800098SYoshinobu Inoue 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
5305d60ed0eSYoshinobu Inoue 				iflr->dstaddr.ss_len);
5316a800098SYoshinobu Inoue 		}
5326a800098SYoshinobu Inoue 
5336a800098SYoshinobu Inoue 		ifra.ifra_mask.sin_family = AF_INET;
5346a800098SYoshinobu Inoue 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
5356a800098SYoshinobu Inoue 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
5366a800098SYoshinobu Inoue 
537b40ce416SJulian Elischer 		return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
5386a800098SYoshinobu Inoue 	    }
5396a800098SYoshinobu Inoue 	case SIOCGLIFADDR:
5406a800098SYoshinobu Inoue 	case SIOCDLIFADDR:
5416a800098SYoshinobu Inoue 	    {
5426a800098SYoshinobu Inoue 		struct in_ifaddr *ia;
5436a800098SYoshinobu Inoue 		struct in_addr mask, candidate, match;
5446a800098SYoshinobu Inoue 		struct sockaddr_in *sin;
5456a800098SYoshinobu Inoue 		int cmp;
5466a800098SYoshinobu Inoue 
5476a800098SYoshinobu Inoue 		bzero(&mask, sizeof(mask));
5486a800098SYoshinobu Inoue 		if (iflr->flags & IFLR_PREFIX) {
5496a800098SYoshinobu Inoue 			/* lookup a prefix rather than address. */
5506a800098SYoshinobu Inoue 			in_len2mask(&mask, iflr->prefixlen);
5516a800098SYoshinobu Inoue 
5526a800098SYoshinobu Inoue 			sin = (struct sockaddr_in *)&iflr->addr;
5536a800098SYoshinobu Inoue 			match.s_addr = sin->sin_addr.s_addr;
5546a800098SYoshinobu Inoue 			match.s_addr &= mask.s_addr;
5556a800098SYoshinobu Inoue 
5566a800098SYoshinobu Inoue 			/* if you set extra bits, that's wrong */
5576a800098SYoshinobu Inoue 			if (match.s_addr != sin->sin_addr.s_addr)
5586a800098SYoshinobu Inoue 				return EINVAL;
5596a800098SYoshinobu Inoue 
5606a800098SYoshinobu Inoue 			cmp = 1;
5616a800098SYoshinobu Inoue 		} else {
5626a800098SYoshinobu Inoue 			if (cmd == SIOCGLIFADDR) {
5636a800098SYoshinobu Inoue 				/* on getting an address, take the 1st match */
5646a800098SYoshinobu Inoue 				cmp = 0;	/*XXX*/
5656a800098SYoshinobu Inoue 			} else {
5666a800098SYoshinobu Inoue 				/* on deleting an address, do exact match */
5676a800098SYoshinobu Inoue 				in_len2mask(&mask, 32);
5686a800098SYoshinobu Inoue 				sin = (struct sockaddr_in *)&iflr->addr;
5696a800098SYoshinobu Inoue 				match.s_addr = sin->sin_addr.s_addr;
5706a800098SYoshinobu Inoue 
5716a800098SYoshinobu Inoue 				cmp = 1;
5726a800098SYoshinobu Inoue 			}
5736a800098SYoshinobu Inoue 		}
5746a800098SYoshinobu Inoue 
5756a800098SYoshinobu Inoue 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
5766a800098SYoshinobu Inoue 			if (ifa->ifa_addr->sa_family != AF_INET6)
5776a800098SYoshinobu Inoue 				continue;
5786a800098SYoshinobu Inoue 			if (!cmp)
5796a800098SYoshinobu Inoue 				break;
5806a800098SYoshinobu Inoue 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
5816a800098SYoshinobu Inoue 			candidate.s_addr &= mask.s_addr;
5826a800098SYoshinobu Inoue 			if (candidate.s_addr == match.s_addr)
5836a800098SYoshinobu Inoue 				break;
5846a800098SYoshinobu Inoue 		}
5856a800098SYoshinobu Inoue 		if (!ifa)
5866a800098SYoshinobu Inoue 			return EADDRNOTAVAIL;
5876a800098SYoshinobu Inoue 		ia = (struct in_ifaddr *)ifa;
5886a800098SYoshinobu Inoue 
5896a800098SYoshinobu Inoue 		if (cmd == SIOCGLIFADDR) {
5906a800098SYoshinobu Inoue 			/* fill in the if_laddrreq structure */
5916a800098SYoshinobu Inoue 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
5926a800098SYoshinobu Inoue 
5936a800098SYoshinobu Inoue 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
5946a800098SYoshinobu Inoue 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
5956a800098SYoshinobu Inoue 					ia->ia_dstaddr.sin_len);
5966a800098SYoshinobu Inoue 			} else
5976a800098SYoshinobu Inoue 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
5986a800098SYoshinobu Inoue 
5996a800098SYoshinobu Inoue 			iflr->prefixlen =
6006a800098SYoshinobu Inoue 				in_mask2len(&ia->ia_sockmask.sin_addr);
6016a800098SYoshinobu Inoue 
6026a800098SYoshinobu Inoue 			iflr->flags = 0;	/*XXX*/
6036a800098SYoshinobu Inoue 
6046a800098SYoshinobu Inoue 			return 0;
6056a800098SYoshinobu Inoue 		} else {
6066a800098SYoshinobu Inoue 			struct in_aliasreq ifra;
6076a800098SYoshinobu Inoue 
6086a800098SYoshinobu Inoue 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
6096a800098SYoshinobu Inoue 			bzero(&ifra, sizeof(ifra));
6106a800098SYoshinobu Inoue 			bcopy(iflr->iflr_name, ifra.ifra_name,
6116a800098SYoshinobu Inoue 				sizeof(ifra.ifra_name));
6126a800098SYoshinobu Inoue 
6136a800098SYoshinobu Inoue 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
6146a800098SYoshinobu Inoue 				ia->ia_addr.sin_len);
6156a800098SYoshinobu Inoue 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
6166a800098SYoshinobu Inoue 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
6176a800098SYoshinobu Inoue 					ia->ia_dstaddr.sin_len);
6186a800098SYoshinobu Inoue 			}
6196a800098SYoshinobu Inoue 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
6206a800098SYoshinobu Inoue 				ia->ia_sockmask.sin_len);
6216a800098SYoshinobu Inoue 
6226a800098SYoshinobu Inoue 			return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
623b40ce416SJulian Elischer 					  ifp, td);
6246a800098SYoshinobu Inoue 		}
6256a800098SYoshinobu Inoue 	    }
6266a800098SYoshinobu Inoue 	}
6276a800098SYoshinobu Inoue 
6286a800098SYoshinobu Inoue 	return EOPNOTSUPP;	/*just for safety*/
6296a800098SYoshinobu Inoue }
6306a800098SYoshinobu Inoue 
6316a800098SYoshinobu Inoue /*
632df8bae1dSRodney W. Grimes  * Delete any existing route for an interface.
633df8bae1dSRodney W. Grimes  */
63439191c8eSGarrett Wollman void
635df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia)
636df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
637df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
638df8bae1dSRodney W. Grimes {
639df8bae1dSRodney W. Grimes 
640df8bae1dSRodney W. Grimes 	if ((ia->ia_flags & IFA_ROUTE) == 0)
641df8bae1dSRodney W. Grimes 		return;
642df8bae1dSRodney W. Grimes 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
643df8bae1dSRodney W. Grimes 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
644df8bae1dSRodney W. Grimes 	else
645df8bae1dSRodney W. Grimes 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
646df8bae1dSRodney W. Grimes 	ia->ia_flags &= ~IFA_ROUTE;
647df8bae1dSRodney W. Grimes }
648df8bae1dSRodney W. Grimes 
649df8bae1dSRodney W. Grimes /*
650df8bae1dSRodney W. Grimes  * Initialize an interface's internet address
651df8bae1dSRodney W. Grimes  * and routing table entry.
652df8bae1dSRodney W. Grimes  */
6530312fbe9SPoul-Henning Kamp static int
654df8bae1dSRodney W. Grimes in_ifinit(ifp, ia, sin, scrub)
655df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
656df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
657df8bae1dSRodney W. Grimes 	struct sockaddr_in *sin;
658df8bae1dSRodney W. Grimes 	int scrub;
659df8bae1dSRodney W. Grimes {
660df8bae1dSRodney W. Grimes 	register u_long i = ntohl(sin->sin_addr.s_addr);
661df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
6625a43847dSBrian Somers 	int s = splimp(), flags = RTF_UP, error = 0;
663df8bae1dSRodney W. Grimes 
664df8bae1dSRodney W. Grimes 	oldaddr = ia->ia_addr;
6652754d95dSSUZUKI Shinsuke 	if (oldaddr.sin_family == AF_INET)
6662754d95dSSUZUKI Shinsuke 		LIST_REMOVE(ia, ia_hash);
667df8bae1dSRodney W. Grimes 	ia->ia_addr = *sin;
6682754d95dSSUZUKI Shinsuke 	if (ia->ia_addr.sin_family == AF_INET)
6692754d95dSSUZUKI Shinsuke 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
6702754d95dSSUZUKI Shinsuke 		    ia, ia_hash);
671df8bae1dSRodney W. Grimes 	/*
672df8bae1dSRodney W. Grimes 	 * Give the interface a chance to initialize
673df8bae1dSRodney W. Grimes 	 * if this is its first address,
674df8bae1dSRodney W. Grimes 	 * and to validate the address if necessary.
675df8bae1dSRodney W. Grimes 	 */
676df8bae1dSRodney W. Grimes 	if (ifp->if_ioctl &&
677df8bae1dSRodney W. Grimes 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
678df8bae1dSRodney W. Grimes 		splx(s);
6792754d95dSSUZUKI Shinsuke 		/* LIST_REMOVE(ia, ia_hash) is done in in_control */
680df8bae1dSRodney W. Grimes 		ia->ia_addr = oldaddr;
68122c819a7SJonathan Lemon 		if (ia->ia_addr.sin_family == AF_INET)
68222c819a7SJonathan Lemon 			LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
68322c819a7SJonathan Lemon 			    ia, ia_hash);
6842754d95dSSUZUKI Shinsuke 		return (error);
6852754d95dSSUZUKI Shinsuke 	}
686df8bae1dSRodney W. Grimes 	splx(s);
687df8bae1dSRodney W. Grimes 	if (scrub) {
688df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
689df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
690df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
691df8bae1dSRodney W. Grimes 	}
692df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i))
693df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSA_NET;
694df8bae1dSRodney W. Grimes 	else if (IN_CLASSB(i))
695df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSB_NET;
696df8bae1dSRodney W. Grimes 	else
697df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSC_NET;
698df8bae1dSRodney W. Grimes 	/*
699df8bae1dSRodney W. Grimes 	 * The subnet mask usually includes at least the standard network part,
700df8bae1dSRodney W. Grimes 	 * but may may be smaller in the case of supernetting.
701df8bae1dSRodney W. Grimes 	 * If it is set, we believe it.
702df8bae1dSRodney W. Grimes 	 */
703df8bae1dSRodney W. Grimes 	if (ia->ia_subnetmask == 0) {
704df8bae1dSRodney W. Grimes 		ia->ia_subnetmask = ia->ia_netmask;
705df8bae1dSRodney W. Grimes 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
706df8bae1dSRodney W. Grimes 	} else
707df8bae1dSRodney W. Grimes 		ia->ia_netmask &= ia->ia_subnetmask;
708df8bae1dSRodney W. Grimes 	ia->ia_net = i & ia->ia_netmask;
709df8bae1dSRodney W. Grimes 	ia->ia_subnet = i & ia->ia_subnetmask;
710df8bae1dSRodney W. Grimes 	in_socktrim(&ia->ia_sockmask);
711df8bae1dSRodney W. Grimes 	/*
712df8bae1dSRodney W. Grimes 	 * Add route for the network.
713df8bae1dSRodney W. Grimes 	 */
714df8bae1dSRodney W. Grimes 	ia->ia_ifa.ifa_metric = ifp->if_metric;
715df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_BROADCAST) {
716df8bae1dSRodney W. Grimes 		ia->ia_broadaddr.sin_addr.s_addr =
717df8bae1dSRodney W. Grimes 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
718df8bae1dSRodney W. Grimes 		ia->ia_netbroadcast.s_addr =
719df8bae1dSRodney W. Grimes 			htonl(ia->ia_net | ~ ia->ia_netmask);
720df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_LOOPBACK) {
721df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
722df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
723df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
724df8bae1dSRodney W. Grimes 		if (ia->ia_dstaddr.sin_family != AF_INET)
725df8bae1dSRodney W. Grimes 			return (0);
726df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
727df8bae1dSRodney W. Grimes 	}
7280f02fdacSBrian Somers 
7295a43847dSBrian Somers 	/*-
7305a43847dSBrian Somers 	 * Don't add host routes for interface addresses of
7315a43847dSBrian Somers 	 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
7325a43847dSBrian Somers 	 * possible to assign several such address pairs with consistent
7335a43847dSBrian Somers 	 * results (no host route) and is required by BOOTP.
7345a43847dSBrian Somers 	 *
7355a43847dSBrian Somers 	 * XXX: This is ugly !  There should be a way for the caller to
7365a43847dSBrian Somers 	 *      say that they don't want a host route.
7375a43847dSBrian Somers 	 */
7385a43847dSBrian Somers 	if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
7395a43847dSBrian Somers 	    ia->ia_netmask != IN_CLASSA_NET ||
7405a43847dSBrian Somers 	    ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
7415a43847dSBrian Somers 		if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
7420f02fdacSBrian Somers 			ia->ia_addr = oldaddr;
7430f02fdacSBrian Somers 			return (error);
7440f02fdacSBrian Somers 		}
7455a43847dSBrian Somers 		ia->ia_flags |= IFA_ROUTE;
7465a43847dSBrian Somers 	}
7470f02fdacSBrian Somers 
748df8bae1dSRodney W. Grimes 	/*
749df8bae1dSRodney W. Grimes 	 * If the interface supports multicast, join the "all hosts"
750df8bae1dSRodney W. Grimes 	 * multicast group on that interface.
751df8bae1dSRodney W. Grimes 	 */
752df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_MULTICAST) {
753df8bae1dSRodney W. Grimes 		struct in_addr addr;
754df8bae1dSRodney W. Grimes 
755df8bae1dSRodney W. Grimes 		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
756df8bae1dSRodney W. Grimes 		in_addmulti(&addr, ifp);
757df8bae1dSRodney W. Grimes 	}
758df8bae1dSRodney W. Grimes 	return (error);
759df8bae1dSRodney W. Grimes }
760df8bae1dSRodney W. Grimes 
761df8bae1dSRodney W. Grimes 
762df8bae1dSRodney W. Grimes /*
763df8bae1dSRodney W. Grimes  * Return 1 if the address might be a local broadcast address.
764df8bae1dSRodney W. Grimes  */
76526f9a767SRodney W. Grimes int
766df8bae1dSRodney W. Grimes in_broadcast(in, ifp)
767df8bae1dSRodney W. Grimes 	struct in_addr in;
768df8bae1dSRodney W. Grimes         struct ifnet *ifp;
769df8bae1dSRodney W. Grimes {
770df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
771df8bae1dSRodney W. Grimes 	u_long t;
772df8bae1dSRodney W. Grimes 
773df8bae1dSRodney W. Grimes 	if (in.s_addr == INADDR_BROADCAST ||
774df8bae1dSRodney W. Grimes 	    in.s_addr == INADDR_ANY)
775df8bae1dSRodney W. Grimes 		return 1;
776df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
777df8bae1dSRodney W. Grimes 		return 0;
778df8bae1dSRodney W. Grimes 	t = ntohl(in.s_addr);
779df8bae1dSRodney W. Grimes 	/*
780df8bae1dSRodney W. Grimes 	 * Look through the list of addresses for a match
781df8bae1dSRodney W. Grimes 	 * with a broadcast address.
782df8bae1dSRodney W. Grimes 	 */
783df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa)
784462b86feSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
785df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == AF_INET &&
786df8bae1dSRodney W. Grimes 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
787df8bae1dSRodney W. Grimes 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
788df8bae1dSRodney W. Grimes 		     /*
789df8bae1dSRodney W. Grimes 		      * Check for old-style (host 0) broadcast.
790df8bae1dSRodney W. Grimes 		      */
7918dd27fd6SGuido van Rooij 		     t == ia->ia_subnet || t == ia->ia_net) &&
7928dd27fd6SGuido van Rooij 		     /*
7938dd27fd6SGuido van Rooij 		      * Check for an all one subnetmask. These
7948dd27fd6SGuido van Rooij 		      * only exist when an interface gets a secondary
7958dd27fd6SGuido van Rooij 		      * address.
7968dd27fd6SGuido van Rooij 		      */
7978dd27fd6SGuido van Rooij 		     ia->ia_subnetmask != (u_long)0xffffffff)
798df8bae1dSRodney W. Grimes 			    return 1;
799df8bae1dSRodney W. Grimes 	return (0);
800df8bae1dSRodney W. Grimes #undef ia
801df8bae1dSRodney W. Grimes }
802df8bae1dSRodney W. Grimes /*
803df8bae1dSRodney W. Grimes  * Add an address to the list of IP multicast addresses for a given interface.
804df8bae1dSRodney W. Grimes  */
805df8bae1dSRodney W. Grimes struct in_multi *
806df8bae1dSRodney W. Grimes in_addmulti(ap, ifp)
807df8bae1dSRodney W. Grimes 	register struct in_addr *ap;
808df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
809df8bae1dSRodney W. Grimes {
810df8bae1dSRodney W. Grimes 	register struct in_multi *inm;
811477180fbSGarrett Wollman 	int error;
812477180fbSGarrett Wollman 	struct sockaddr_in sin;
813477180fbSGarrett Wollman 	struct ifmultiaddr *ifma;
814df8bae1dSRodney W. Grimes 	int s = splnet();
815df8bae1dSRodney W. Grimes 
816df8bae1dSRodney W. Grimes 	/*
817477180fbSGarrett Wollman 	 * Call generic routine to add membership or increment
818477180fbSGarrett Wollman 	 * refcount.  It wants addresses in the form of a sockaddr,
819477180fbSGarrett Wollman 	 * so we build one here (being careful to zero the unused bytes).
820df8bae1dSRodney W. Grimes 	 */
821477180fbSGarrett Wollman 	bzero(&sin, sizeof sin);
822477180fbSGarrett Wollman 	sin.sin_family = AF_INET;
823477180fbSGarrett Wollman 	sin.sin_len = sizeof sin;
824477180fbSGarrett Wollman 	sin.sin_addr = *ap;
825477180fbSGarrett Wollman 	error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
826477180fbSGarrett Wollman 	if (error) {
827477180fbSGarrett Wollman 		splx(s);
828477180fbSGarrett Wollman 		return 0;
829df8bae1dSRodney W. Grimes 	}
830477180fbSGarrett Wollman 
831df8bae1dSRodney W. Grimes 	/*
832477180fbSGarrett Wollman 	 * If ifma->ifma_protospec is null, then if_addmulti() created
833477180fbSGarrett Wollman 	 * a new record.  Otherwise, we are done.
834df8bae1dSRodney W. Grimes 	 */
8354153a3a3SBruce Evans 	if (ifma->ifma_protospec != 0) {
8364153a3a3SBruce Evans 		splx(s);
837477180fbSGarrett Wollman 		return ifma->ifma_protospec;
8384153a3a3SBruce Evans 	}
839477180fbSGarrett Wollman 
840477180fbSGarrett Wollman 	/* XXX - if_addmulti uses M_WAITOK.  Can this really be called
841477180fbSGarrett Wollman 	   at interrupt time?  If so, need to fix if_addmulti. XXX */
8427cc0979fSDavid Malone 	inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
8437cc0979fSDavid Malone 	    M_NOWAIT | M_ZERO);
844df8bae1dSRodney W. Grimes 	if (inm == NULL) {
845df8bae1dSRodney W. Grimes 		splx(s);
846df8bae1dSRodney W. Grimes 		return (NULL);
847df8bae1dSRodney W. Grimes 	}
848477180fbSGarrett Wollman 
849df8bae1dSRodney W. Grimes 	inm->inm_addr = *ap;
850df8bae1dSRodney W. Grimes 	inm->inm_ifp = ifp;
851477180fbSGarrett Wollman 	inm->inm_ifma = ifma;
852477180fbSGarrett Wollman 	ifma->ifma_protospec = inm;
853477180fbSGarrett Wollman 	LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
854ffa5b11aSGarrett Wollman 
855df8bae1dSRodney W. Grimes 	/*
856df8bae1dSRodney W. Grimes 	 * Let IGMP know that we have joined a new IP multicast group.
857df8bae1dSRodney W. Grimes 	 */
858df8bae1dSRodney W. Grimes 	igmp_joingroup(inm);
859df8bae1dSRodney W. Grimes 	splx(s);
860df8bae1dSRodney W. Grimes 	return (inm);
861df8bae1dSRodney W. Grimes }
862df8bae1dSRodney W. Grimes 
863df8bae1dSRodney W. Grimes /*
864df8bae1dSRodney W. Grimes  * Delete a multicast address record.
865df8bae1dSRodney W. Grimes  */
86626f9a767SRodney W. Grimes void
867df8bae1dSRodney W. Grimes in_delmulti(inm)
868df8bae1dSRodney W. Grimes 	register struct in_multi *inm;
869df8bae1dSRodney W. Grimes {
870477180fbSGarrett Wollman 	struct ifmultiaddr *ifma = inm->inm_ifma;
87188a5354eSLuigi Rizzo 	struct in_multi my_inm;
872df8bae1dSRodney W. Grimes 	int s = splnet();
873df8bae1dSRodney W. Grimes 
87488a5354eSLuigi Rizzo 	my_inm.inm_ifp = NULL ; /* don't send the leave msg */
875477180fbSGarrett Wollman 	if (ifma->ifma_refcount == 1) {
876df8bae1dSRodney W. Grimes 		/*
877df8bae1dSRodney W. Grimes 		 * No remaining claims to this record; let IGMP know that
878df8bae1dSRodney W. Grimes 		 * we are leaving the multicast group.
87988a5354eSLuigi Rizzo 		 * But do it after the if_delmulti() which might reset
88088a5354eSLuigi Rizzo 		 * the interface and nuke the packet.
881df8bae1dSRodney W. Grimes 		 */
88288a5354eSLuigi Rizzo 		my_inm = *inm ;
883477180fbSGarrett Wollman 		ifma->ifma_protospec = 0;
884477180fbSGarrett Wollman 		LIST_REMOVE(inm, inm_link);
885df8bae1dSRodney W. Grimes 		free(inm, M_IPMADDR);
886df8bae1dSRodney W. Grimes 	}
887477180fbSGarrett Wollman 	/* XXX - should be separate API for when we have an ifma? */
888477180fbSGarrett Wollman 	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
88988a5354eSLuigi Rizzo 	if (my_inm.inm_ifp != NULL)
89088a5354eSLuigi Rizzo 		igmp_leavegroup(&my_inm);
891df8bae1dSRodney W. Grimes 	splx(s);
892df8bae1dSRodney W. Grimes }
893