xref: /freebsd/sys/netinet/in.c (revision f6d24a780bcb56bf0ea5ff3f195b9d4d4a442d97)
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
34f6d24a78SPoul-Henning Kamp  *	$Id: in.c,v 1.19 1995/11/20 12:28:21 phk Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
3826f9a767SRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/ioctl.h>
40df8bae1dSRodney W. Grimes #include <sys/errno.h>
41df8bae1dSRodney W. Grimes #include <sys/malloc.h>
42df8bae1dSRodney W. Grimes #include <sys/socket.h>
43df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
44ffa5b11aSGarrett Wollman #include <sys/queue.h>
45f6d24a78SPoul-Henning Kamp #include <sys/kernel.h>
46f6d24a78SPoul-Henning Kamp #include <sys/sysctl.h>
47df8bae1dSRodney W. Grimes 
48df8bae1dSRodney W. Grimes #include <net/if.h>
49df8bae1dSRodney W. Grimes #include <net/route.h>
50df8bae1dSRodney W. Grimes 
51df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
52df8bae1dSRodney W. Grimes #include <netinet/in.h>
53df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
54df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
55df8bae1dSRodney W. Grimes 
56c70f4510SPoul-Henning Kamp #include <netinet/igmp_var.h>
57c70f4510SPoul-Henning Kamp 
58df8bae1dSRodney W. Grimes /*
59ffa5b11aSGarrett Wollman  * This structure is used to keep track of in_multi chains which belong to
60ffa5b11aSGarrett Wollman  * deleted interface addresses.
61ffa5b11aSGarrett Wollman  */
62ffa5b11aSGarrett Wollman static LIST_HEAD(, multi_kludge) in_mk; /* XXX BSS initialization */
63ffa5b11aSGarrett Wollman 
64ffa5b11aSGarrett Wollman struct multi_kludge {
65ffa5b11aSGarrett Wollman 	LIST_ENTRY(multi_kludge) mk_entry;
66ffa5b11aSGarrett Wollman 	struct ifnet *mk_ifp;
67ffa5b11aSGarrett Wollman 	struct in_multihead mk_head;
68ffa5b11aSGarrett Wollman };
69ffa5b11aSGarrett Wollman 
700312fbe9SPoul-Henning Kamp static void	in_socktrim __P((struct sockaddr_in *));
710312fbe9SPoul-Henning Kamp static int	in_ifinit __P((struct ifnet *,
720312fbe9SPoul-Henning Kamp 	    struct in_ifaddr *, struct sockaddr_in *, int));
730312fbe9SPoul-Henning Kamp static void	in_ifscrub __P((struct ifnet *, struct in_ifaddr *));
74df8bae1dSRodney W. Grimes 
75f6d24a78SPoul-Henning Kamp static int subnetsarelocal = 1;
76f6d24a78SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
77f6d24a78SPoul-Henning Kamp 	&subnetsarelocal, 0, "");
78df8bae1dSRodney W. Grimes /*
79df8bae1dSRodney W. Grimes  * Return 1 if an internet address is for a ``local'' host
80df8bae1dSRodney W. Grimes  * (one to which we have a connection).  If subnetsarelocal
81df8bae1dSRodney W. Grimes  * is true, this includes other subnets of the local net.
82df8bae1dSRodney W. Grimes  * Otherwise, it includes only the directly-connected (sub)nets.
83df8bae1dSRodney W. Grimes  */
8426f9a767SRodney W. Grimes int
85df8bae1dSRodney W. Grimes in_localaddr(in)
86df8bae1dSRodney W. Grimes 	struct in_addr in;
87df8bae1dSRodney W. Grimes {
88df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
89df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes 	if (subnetsarelocal) {
92df8bae1dSRodney W. Grimes 		for (ia = in_ifaddr; ia; ia = ia->ia_next)
93df8bae1dSRodney W. Grimes 			if ((i & ia->ia_netmask) == ia->ia_net)
94df8bae1dSRodney W. Grimes 				return (1);
95df8bae1dSRodney W. Grimes 	} else {
96df8bae1dSRodney W. Grimes 		for (ia = in_ifaddr; ia; ia = ia->ia_next)
97df8bae1dSRodney W. Grimes 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
98df8bae1dSRodney W. Grimes 				return (1);
99df8bae1dSRodney W. Grimes 	}
100df8bae1dSRodney W. Grimes 	return (0);
101df8bae1dSRodney W. Grimes }
102df8bae1dSRodney W. Grimes 
103df8bae1dSRodney W. Grimes /*
104df8bae1dSRodney W. Grimes  * Determine whether an IP address is in a reserved set of addresses
105df8bae1dSRodney W. Grimes  * that may not be forwarded, or whether datagrams to that destination
106df8bae1dSRodney W. Grimes  * may be forwarded.
107df8bae1dSRodney W. Grimes  */
10826f9a767SRodney W. Grimes int
109df8bae1dSRodney W. Grimes in_canforward(in)
110df8bae1dSRodney W. Grimes 	struct in_addr in;
111df8bae1dSRodney W. Grimes {
112df8bae1dSRodney W. Grimes 	register u_long i = ntohl(in.s_addr);
113df8bae1dSRodney W. Grimes 	register u_long net;
114df8bae1dSRodney W. Grimes 
115df8bae1dSRodney W. Grimes 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
116df8bae1dSRodney W. Grimes 		return (0);
117df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i)) {
118df8bae1dSRodney W. Grimes 		net = i & IN_CLASSA_NET;
119df8bae1dSRodney W. Grimes 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
120df8bae1dSRodney W. Grimes 			return (0);
121df8bae1dSRodney W. Grimes 	}
122df8bae1dSRodney W. Grimes 	return (1);
123df8bae1dSRodney W. Grimes }
124df8bae1dSRodney W. Grimes 
125df8bae1dSRodney W. Grimes /*
126df8bae1dSRodney W. Grimes  * Trim a mask in a sockaddr
127df8bae1dSRodney W. Grimes  */
1280312fbe9SPoul-Henning Kamp static void
129df8bae1dSRodney W. Grimes in_socktrim(ap)
130df8bae1dSRodney W. Grimes struct sockaddr_in *ap;
131df8bae1dSRodney W. Grimes {
132df8bae1dSRodney W. Grimes     register char *cplim = (char *) &ap->sin_addr;
133df8bae1dSRodney W. Grimes     register char *cp = (char *) (&ap->sin_addr + 1);
134df8bae1dSRodney W. Grimes 
135df8bae1dSRodney W. Grimes     ap->sin_len = 0;
136df00058dSGarrett Wollman     while (--cp >= cplim)
137df8bae1dSRodney W. Grimes         if (*cp) {
138df8bae1dSRodney W. Grimes 	    (ap)->sin_len = cp - (char *) (ap) + 1;
139df8bae1dSRodney W. Grimes 	    break;
140df8bae1dSRodney W. Grimes 	}
141df8bae1dSRodney W. Grimes }
142df8bae1dSRodney W. Grimes 
143f6d24a78SPoul-Henning Kamp static int in_interfaces;	/* number of external internet interfaces */
144df8bae1dSRodney W. Grimes 
145df8bae1dSRodney W. Grimes /*
146df8bae1dSRodney W. Grimes  * Generic internet control operations (ioctl's).
147df8bae1dSRodney W. Grimes  * Ifp is 0 if not an interface-specific ioctl.
148df8bae1dSRodney W. Grimes  */
149df8bae1dSRodney W. Grimes /* ARGSUSED */
15026f9a767SRodney W. Grimes int
151df8bae1dSRodney W. Grimes in_control(so, cmd, data, ifp)
152df8bae1dSRodney W. Grimes 	struct socket *so;
1532180b925SGarrett Wollman 	u_long cmd;
154df8bae1dSRodney W. Grimes 	caddr_t data;
155df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
156df8bae1dSRodney W. Grimes {
157df8bae1dSRodney W. Grimes 	register struct ifreq *ifr = (struct ifreq *)data;
158df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia = 0;
159df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
160df8bae1dSRodney W. Grimes 	struct in_ifaddr *oia;
161df8bae1dSRodney W. Grimes 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
162df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
163df8bae1dSRodney W. Grimes 	int error, hostIsNew, maskIsNew;
164df8bae1dSRodney W. Grimes 	u_long i;
165ffa5b11aSGarrett Wollman 	struct multi_kludge *mk;
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes 	/*
168df8bae1dSRodney W. Grimes 	 * Find address for this interface, if it exists.
169df8bae1dSRodney W. Grimes 	 */
170df8bae1dSRodney W. Grimes 	if (ifp)
171df8bae1dSRodney W. Grimes 		for (ia = in_ifaddr; ia; ia = ia->ia_next)
172df8bae1dSRodney W. Grimes 			if (ia->ia_ifp == ifp)
173df8bae1dSRodney W. Grimes 				break;
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes 	switch (cmd) {
176df8bae1dSRodney W. Grimes 
177df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
178df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
1791067217dSGarrett Wollman 		if (ifra->ifra_addr.sin_family == AF_INET) {
180df8bae1dSRodney W. Grimes 			for (oia = ia; ia; ia = ia->ia_next) {
181df8bae1dSRodney W. Grimes 				if (ia->ia_ifp == ifp  &&
182df8bae1dSRodney W. Grimes 				    ia->ia_addr.sin_addr.s_addr ==
183df8bae1dSRodney W. Grimes 				    ifra->ifra_addr.sin_addr.s_addr)
184df8bae1dSRodney W. Grimes 					break;
185df8bae1dSRodney W. Grimes 			}
1861067217dSGarrett Wollman 			if ((ifp->if_flags & IFF_POINTOPOINT)
1871067217dSGarrett Wollman 			    && (cmd == SIOCAIFADDR)
1881067217dSGarrett Wollman 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
1891067217dSGarrett Wollman 				== INADDR_ANY)) {
190357b78a9SGarrett Wollman 				return EDESTADDRREQ;
1911067217dSGarrett Wollman 			}
1921067217dSGarrett Wollman 		}
193df8bae1dSRodney W. Grimes 		if (cmd == SIOCDIFADDR && ia == 0)
194df8bae1dSRodney W. Grimes 			return (EADDRNOTAVAIL);
195df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
196df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
197df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
198df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
199df8bae1dSRodney W. Grimes 		if ((so->so_state & SS_PRIV) == 0)
200df8bae1dSRodney W. Grimes 			return (EPERM);
201df8bae1dSRodney W. Grimes 
202df8bae1dSRodney W. Grimes 		if (ifp == 0)
203df8bae1dSRodney W. Grimes 			panic("in_control");
204df8bae1dSRodney W. Grimes 		if (ia == (struct in_ifaddr *)0) {
205df8bae1dSRodney W. Grimes 			oia = (struct in_ifaddr *)
206df8bae1dSRodney W. Grimes 				malloc(sizeof *oia, M_IFADDR, M_WAITOK);
207df8bae1dSRodney W. Grimes 			if (oia == (struct in_ifaddr *)NULL)
208df8bae1dSRodney W. Grimes 				return (ENOBUFS);
209df8bae1dSRodney W. Grimes 			bzero((caddr_t)oia, sizeof *oia);
210623ae52eSPoul-Henning Kamp 			ia = in_ifaddr;
211623ae52eSPoul-Henning Kamp 			if (ia) {
212df8bae1dSRodney W. Grimes 				for ( ; ia->ia_next; ia = ia->ia_next)
213df8bae1dSRodney W. Grimes 					continue;
214df8bae1dSRodney W. Grimes 				ia->ia_next = oia;
215df8bae1dSRodney W. Grimes 			} else
216df8bae1dSRodney W. Grimes 				in_ifaddr = oia;
217df8bae1dSRodney W. Grimes 			ia = oia;
218623ae52eSPoul-Henning Kamp 			ifa = ifp->if_addrlist;
219623ae52eSPoul-Henning Kamp 			if (ifa) {
220df8bae1dSRodney W. Grimes 				for ( ; ifa->ifa_next; ifa = ifa->ifa_next)
221df8bae1dSRodney W. Grimes 					continue;
222df8bae1dSRodney W. Grimes 				ifa->ifa_next = (struct ifaddr *) ia;
223df8bae1dSRodney W. Grimes 			} else
224df8bae1dSRodney W. Grimes 				ifp->if_addrlist = (struct ifaddr *) ia;
225df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
226df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr
227df8bae1dSRodney W. Grimes 					= (struct sockaddr *)&ia->ia_dstaddr;
228df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_netmask
229df8bae1dSRodney W. Grimes 					= (struct sockaddr *)&ia->ia_sockmask;
230df8bae1dSRodney W. Grimes 			ia->ia_sockmask.sin_len = 8;
231df8bae1dSRodney W. Grimes 			if (ifp->if_flags & IFF_BROADCAST) {
232df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
233df8bae1dSRodney W. Grimes 				ia->ia_broadaddr.sin_family = AF_INET;
234df8bae1dSRodney W. Grimes 			}
235df8bae1dSRodney W. Grimes 			ia->ia_ifp = ifp;
236f5fea3ddSPaul Traina 			if (!(ifp->if_flags & IFF_LOOPBACK))
237df8bae1dSRodney W. Grimes 				in_interfaces++;
238df8bae1dSRodney W. Grimes 		}
239df8bae1dSRodney W. Grimes 		break;
240df8bae1dSRodney W. Grimes 
241df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
242df8bae1dSRodney W. Grimes 		if ((so->so_state & SS_PRIV) == 0)
243df8bae1dSRodney W. Grimes 			return (EPERM);
244df8bae1dSRodney W. Grimes 		/* FALLTHROUGH */
245df8bae1dSRodney W. Grimes 
246df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
247df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
248df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
249df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
250df8bae1dSRodney W. Grimes 		if (ia == (struct in_ifaddr *)0)
251df8bae1dSRodney W. Grimes 			return (EADDRNOTAVAIL);
252df8bae1dSRodney W. Grimes 		break;
253df8bae1dSRodney W. Grimes 	}
254df8bae1dSRodney W. Grimes 	switch (cmd) {
255df8bae1dSRodney W. Grimes 
256df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
257df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
258df8bae1dSRodney W. Grimes 		break;
259df8bae1dSRodney W. Grimes 
260df8bae1dSRodney W. Grimes 	case SIOCGIFBRDADDR:
261df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
262df8bae1dSRodney W. Grimes 			return (EINVAL);
263df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
264df8bae1dSRodney W. Grimes 		break;
265df8bae1dSRodney W. Grimes 
266df8bae1dSRodney W. Grimes 	case SIOCGIFDSTADDR:
267df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
268df8bae1dSRodney W. Grimes 			return (EINVAL);
269df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
270df8bae1dSRodney W. Grimes 		break;
271df8bae1dSRodney W. Grimes 
272df8bae1dSRodney W. Grimes 	case SIOCGIFNETMASK:
273df8bae1dSRodney W. Grimes 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
274df8bae1dSRodney W. Grimes 		break;
275df8bae1dSRodney W. Grimes 
276df8bae1dSRodney W. Grimes 	case SIOCSIFDSTADDR:
277df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
278df8bae1dSRodney W. Grimes 			return (EINVAL);
279df8bae1dSRodney W. Grimes 		oldaddr = ia->ia_dstaddr;
280df8bae1dSRodney W. Grimes 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
281df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
282df8bae1dSRodney W. Grimes 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
283df8bae1dSRodney W. Grimes 			ia->ia_dstaddr = oldaddr;
284df8bae1dSRodney W. Grimes 			return (error);
285df8bae1dSRodney W. Grimes 		}
286df8bae1dSRodney W. Grimes 		if (ia->ia_flags & IFA_ROUTE) {
287df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
288df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
289df8bae1dSRodney W. Grimes 			ia->ia_ifa.ifa_dstaddr =
290df8bae1dSRodney W. Grimes 					(struct sockaddr *)&ia->ia_dstaddr;
291df8bae1dSRodney W. Grimes 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
292df8bae1dSRodney W. Grimes 		}
293df8bae1dSRodney W. Grimes 		break;
294df8bae1dSRodney W. Grimes 
295df8bae1dSRodney W. Grimes 	case SIOCSIFBRDADDR:
296df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
297df8bae1dSRodney W. Grimes 			return (EINVAL);
298df8bae1dSRodney W. Grimes 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
299df8bae1dSRodney W. Grimes 		break;
300df8bae1dSRodney W. Grimes 
301df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
302df8bae1dSRodney W. Grimes 		return (in_ifinit(ifp, ia,
303df8bae1dSRodney W. Grimes 		    (struct sockaddr_in *) &ifr->ifr_addr, 1));
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes 	case SIOCSIFNETMASK:
306df8bae1dSRodney W. Grimes 		i = ifra->ifra_addr.sin_addr.s_addr;
307df8bae1dSRodney W. Grimes 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
308df8bae1dSRodney W. Grimes 		break;
309df8bae1dSRodney W. Grimes 
310df8bae1dSRodney W. Grimes 	case SIOCAIFADDR:
311df8bae1dSRodney W. Grimes 		maskIsNew = 0;
312df8bae1dSRodney W. Grimes 		hostIsNew = 1;
313df8bae1dSRodney W. Grimes 		error = 0;
314df8bae1dSRodney W. Grimes 		if (ia->ia_addr.sin_family == AF_INET) {
315df8bae1dSRodney W. Grimes 			if (ifra->ifra_addr.sin_len == 0) {
316df8bae1dSRodney W. Grimes 				ifra->ifra_addr = ia->ia_addr;
317df8bae1dSRodney W. Grimes 				hostIsNew = 0;
318df8bae1dSRodney W. Grimes 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
319df8bae1dSRodney W. Grimes 					       ia->ia_addr.sin_addr.s_addr)
320df8bae1dSRodney W. Grimes 				hostIsNew = 0;
321df8bae1dSRodney W. Grimes 		}
322df8bae1dSRodney W. Grimes 		if (ifra->ifra_mask.sin_len) {
323df8bae1dSRodney W. Grimes 			in_ifscrub(ifp, ia);
324df8bae1dSRodney W. Grimes 			ia->ia_sockmask = ifra->ifra_mask;
325df8bae1dSRodney W. Grimes 			ia->ia_subnetmask =
326df8bae1dSRodney W. Grimes 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
327df8bae1dSRodney W. Grimes 			maskIsNew = 1;
328df8bae1dSRodney W. Grimes 		}
329df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
330df8bae1dSRodney W. Grimes 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
331df8bae1dSRodney W. Grimes 			in_ifscrub(ifp, ia);
332df8bae1dSRodney W. Grimes 			ia->ia_dstaddr = ifra->ifra_dstaddr;
333df8bae1dSRodney W. Grimes 			maskIsNew  = 1; /* We lie; but the effect's the same */
334df8bae1dSRodney W. Grimes 		}
335df8bae1dSRodney W. Grimes 		if (ifra->ifra_addr.sin_family == AF_INET &&
336df8bae1dSRodney W. Grimes 		    (hostIsNew || maskIsNew))
337df8bae1dSRodney W. Grimes 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
338df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) &&
339df8bae1dSRodney W. Grimes 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
340df8bae1dSRodney W. Grimes 			ia->ia_broadaddr = ifra->ifra_broadaddr;
341df8bae1dSRodney W. Grimes 		return (error);
342df8bae1dSRodney W. Grimes 
343df8bae1dSRodney W. Grimes 	case SIOCDIFADDR:
344ffa5b11aSGarrett Wollman 		mk = malloc(sizeof *mk, M_IPMADDR, M_WAITOK);
345ffa5b11aSGarrett Wollman 		if (!mk)
346ffa5b11aSGarrett Wollman 			return ENOBUFS;
347ffa5b11aSGarrett Wollman 
348df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
349df8bae1dSRodney W. Grimes 		if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia)
350df8bae1dSRodney W. Grimes 			ifp->if_addrlist = ifa->ifa_next;
351df8bae1dSRodney W. Grimes 		else {
352df8bae1dSRodney W. Grimes 			while (ifa->ifa_next &&
353df8bae1dSRodney W. Grimes 			       (ifa->ifa_next != (struct ifaddr *)ia))
354df8bae1dSRodney W. Grimes 				    ifa = ifa->ifa_next;
355df8bae1dSRodney W. Grimes 			if (ifa->ifa_next)
356df8bae1dSRodney W. Grimes 				ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next;
357df8bae1dSRodney W. Grimes 			else
358df8bae1dSRodney W. Grimes 				printf("Couldn't unlink inifaddr from ifp\n");
359df8bae1dSRodney W. Grimes 		}
360df8bae1dSRodney W. Grimes 		oia = ia;
361df8bae1dSRodney W. Grimes 		if (oia == (ia = in_ifaddr))
362df8bae1dSRodney W. Grimes 			in_ifaddr = ia->ia_next;
363df8bae1dSRodney W. Grimes 		else {
364df8bae1dSRodney W. Grimes 			while (ia->ia_next && (ia->ia_next != oia))
365df8bae1dSRodney W. Grimes 				ia = ia->ia_next;
366df8bae1dSRodney W. Grimes 			if (ia->ia_next)
367df8bae1dSRodney W. Grimes 				ia->ia_next = oia->ia_next;
368df8bae1dSRodney W. Grimes 			else
369df8bae1dSRodney W. Grimes 				printf("Didn't unlink inifadr from list\n");
370df8bae1dSRodney W. Grimes 		}
371ffa5b11aSGarrett Wollman 
372ffa5b11aSGarrett Wollman 		if (!oia->ia_multiaddrs.lh_first) {
373ffa5b11aSGarrett Wollman 			IFAFREE(&oia->ia_ifa);
374ffa5b11aSGarrett Wollman 			FREE(mk, M_IPMADDR);
375ffa5b11aSGarrett Wollman 			break;
376ffa5b11aSGarrett Wollman 		}
377ffa5b11aSGarrett Wollman 
378ffa5b11aSGarrett Wollman 		/*
379ffa5b11aSGarrett Wollman 		 * Multicast address kludge:
380ffa5b11aSGarrett Wollman 		 * If there were any multicast addresses attached to this
381ffa5b11aSGarrett Wollman 		 * interface address, either move them to another address
382ffa5b11aSGarrett Wollman 		 * on this interface, or save them until such time as this
383ffa5b11aSGarrett Wollman 		 * interface is reconfigured for IP.
384ffa5b11aSGarrett Wollman 		 */
385ffa5b11aSGarrett Wollman 		IFP_TO_IA(oia->ia_ifp, ia);
386ffa5b11aSGarrett Wollman 		if (ia) {	/* there is another address */
387ffa5b11aSGarrett Wollman 			struct in_multi *inm;
388ffa5b11aSGarrett Wollman 			for(inm = oia->ia_multiaddrs.lh_first; inm;
389ffa5b11aSGarrett Wollman 			    inm = inm->inm_entry.le_next) {
390ffa5b11aSGarrett Wollman 				IFAFREE(&inm->inm_ia->ia_ifa);
391ffa5b11aSGarrett Wollman 				ia->ia_ifa.ifa_refcnt++;
392ffa5b11aSGarrett Wollman 				inm->inm_ia = ia;
393ffa5b11aSGarrett Wollman 				LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm,
394ffa5b11aSGarrett Wollman 						 inm_entry);
395ffa5b11aSGarrett Wollman 			}
396ffa5b11aSGarrett Wollman 			FREE(mk, M_IPMADDR);
397ffa5b11aSGarrett Wollman 		} else {	/* last address on this if deleted, save */
398ffa5b11aSGarrett Wollman 			struct in_multi *inm;
399ffa5b11aSGarrett Wollman 
400ffa5b11aSGarrett Wollman 			LIST_INIT(&mk->mk_head);
401ffa5b11aSGarrett Wollman 			mk->mk_ifp = ifp;
402ffa5b11aSGarrett Wollman 
403ffa5b11aSGarrett Wollman 			for(inm = oia->ia_multiaddrs.lh_first; inm;
404ffa5b11aSGarrett Wollman 			    inm = inm->inm_entry.le_next) {
405ffa5b11aSGarrett Wollman 				LIST_INSERT_HEAD(&mk->mk_head, inm, inm_entry);
406ffa5b11aSGarrett Wollman 			}
407ffa5b11aSGarrett Wollman 
408ffa5b11aSGarrett Wollman 			if (mk->mk_head.lh_first) {
409ffa5b11aSGarrett Wollman 				LIST_INSERT_HEAD(&in_mk, mk, mk_entry);
410ffa5b11aSGarrett Wollman 			} else {
411ffa5b11aSGarrett Wollman 				FREE(mk, M_IPMADDR);
412ffa5b11aSGarrett Wollman 			}
413ffa5b11aSGarrett Wollman 		}
414ffa5b11aSGarrett Wollman 
415df8bae1dSRodney W. Grimes 		IFAFREE((&oia->ia_ifa));
416df8bae1dSRodney W. Grimes 		break;
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes 	default:
419df8bae1dSRodney W. Grimes 		if (ifp == 0 || ifp->if_ioctl == 0)
420df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
421df8bae1dSRodney W. Grimes 		return ((*ifp->if_ioctl)(ifp, cmd, data));
422df8bae1dSRodney W. Grimes 	}
423df8bae1dSRodney W. Grimes 	return (0);
424df8bae1dSRodney W. Grimes }
425df8bae1dSRodney W. Grimes 
426df8bae1dSRodney W. Grimes /*
427df8bae1dSRodney W. Grimes  * Delete any existing route for an interface.
428df8bae1dSRodney W. Grimes  */
4290312fbe9SPoul-Henning Kamp static void
430df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia)
431df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
432df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
433df8bae1dSRodney W. Grimes {
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes 	if ((ia->ia_flags & IFA_ROUTE) == 0)
436df8bae1dSRodney W. Grimes 		return;
437df8bae1dSRodney W. Grimes 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
438df8bae1dSRodney W. Grimes 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
439df8bae1dSRodney W. Grimes 	else
440df8bae1dSRodney W. Grimes 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
441df8bae1dSRodney W. Grimes 	ia->ia_flags &= ~IFA_ROUTE;
442df8bae1dSRodney W. Grimes }
443df8bae1dSRodney W. Grimes 
444df8bae1dSRodney W. Grimes /*
445df8bae1dSRodney W. Grimes  * Initialize an interface's internet address
446df8bae1dSRodney W. Grimes  * and routing table entry.
447df8bae1dSRodney W. Grimes  */
4480312fbe9SPoul-Henning Kamp static int
449df8bae1dSRodney W. Grimes in_ifinit(ifp, ia, sin, scrub)
450df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
451df8bae1dSRodney W. Grimes 	register struct in_ifaddr *ia;
452df8bae1dSRodney W. Grimes 	struct sockaddr_in *sin;
453df8bae1dSRodney W. Grimes 	int scrub;
454df8bae1dSRodney W. Grimes {
455df8bae1dSRodney W. Grimes 	register u_long i = ntohl(sin->sin_addr.s_addr);
456df8bae1dSRodney W. Grimes 	struct sockaddr_in oldaddr;
457f23b4c91SGarrett Wollman 	int s = splimp(), flags = RTF_UP, error;
458ffa5b11aSGarrett Wollman 	struct multi_kludge *mk;
459df8bae1dSRodney W. Grimes 
460df8bae1dSRodney W. Grimes 	oldaddr = ia->ia_addr;
461df8bae1dSRodney W. Grimes 	ia->ia_addr = *sin;
462df8bae1dSRodney W. Grimes 	/*
463df8bae1dSRodney W. Grimes 	 * Give the interface a chance to initialize
464df8bae1dSRodney W. Grimes 	 * if this is its first address,
465df8bae1dSRodney W. Grimes 	 * and to validate the address if necessary.
466df8bae1dSRodney W. Grimes 	 */
467df8bae1dSRodney W. Grimes 	if (ifp->if_ioctl &&
468df8bae1dSRodney W. Grimes 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
469df8bae1dSRodney W. Grimes 		splx(s);
470df8bae1dSRodney W. Grimes 		ia->ia_addr = oldaddr;
471df8bae1dSRodney W. Grimes 		return (error);
472df8bae1dSRodney W. Grimes 	}
473df8bae1dSRodney W. Grimes 	splx(s);
474df8bae1dSRodney W. Grimes 	if (scrub) {
475df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
476df8bae1dSRodney W. Grimes 		in_ifscrub(ifp, ia);
477df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
478df8bae1dSRodney W. Grimes 	}
479df8bae1dSRodney W. Grimes 	if (IN_CLASSA(i))
480df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSA_NET;
481df8bae1dSRodney W. Grimes 	else if (IN_CLASSB(i))
482df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSB_NET;
483df8bae1dSRodney W. Grimes 	else
484df8bae1dSRodney W. Grimes 		ia->ia_netmask = IN_CLASSC_NET;
485df8bae1dSRodney W. Grimes 	/*
486df8bae1dSRodney W. Grimes 	 * The subnet mask usually includes at least the standard network part,
487df8bae1dSRodney W. Grimes 	 * but may may be smaller in the case of supernetting.
488df8bae1dSRodney W. Grimes 	 * If it is set, we believe it.
489df8bae1dSRodney W. Grimes 	 */
490df8bae1dSRodney W. Grimes 	if (ia->ia_subnetmask == 0) {
491df8bae1dSRodney W. Grimes 		ia->ia_subnetmask = ia->ia_netmask;
492df8bae1dSRodney W. Grimes 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
493df8bae1dSRodney W. Grimes 	} else
494df8bae1dSRodney W. Grimes 		ia->ia_netmask &= ia->ia_subnetmask;
495df8bae1dSRodney W. Grimes 	ia->ia_net = i & ia->ia_netmask;
496df8bae1dSRodney W. Grimes 	ia->ia_subnet = i & ia->ia_subnetmask;
497df8bae1dSRodney W. Grimes 	in_socktrim(&ia->ia_sockmask);
498df8bae1dSRodney W. Grimes 	/*
499df8bae1dSRodney W. Grimes 	 * Add route for the network.
500df8bae1dSRodney W. Grimes 	 */
501df8bae1dSRodney W. Grimes 	ia->ia_ifa.ifa_metric = ifp->if_metric;
502df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_BROADCAST) {
503df8bae1dSRodney W. Grimes 		ia->ia_broadaddr.sin_addr.s_addr =
504df8bae1dSRodney W. Grimes 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
505df8bae1dSRodney W. Grimes 		ia->ia_netbroadcast.s_addr =
506df8bae1dSRodney W. Grimes 			htonl(ia->ia_net | ~ ia->ia_netmask);
507df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_LOOPBACK) {
508df8bae1dSRodney W. Grimes 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
509df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
510df8bae1dSRodney W. Grimes 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
511df8bae1dSRodney W. Grimes 		if (ia->ia_dstaddr.sin_family != AF_INET)
512df8bae1dSRodney W. Grimes 			return (0);
513df8bae1dSRodney W. Grimes 		flags |= RTF_HOST;
514df8bae1dSRodney W. Grimes 	}
515df8bae1dSRodney W. Grimes 	if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
516df8bae1dSRodney W. Grimes 		ia->ia_flags |= IFA_ROUTE;
517ffa5b11aSGarrett Wollman 
518ffa5b11aSGarrett Wollman 	LIST_INIT(&ia->ia_multiaddrs);
519df8bae1dSRodney W. Grimes 	/*
520df8bae1dSRodney W. Grimes 	 * If the interface supports multicast, join the "all hosts"
521df8bae1dSRodney W. Grimes 	 * multicast group on that interface.
522df8bae1dSRodney W. Grimes 	 */
523df8bae1dSRodney W. Grimes 	if (ifp->if_flags & IFF_MULTICAST) {
524df8bae1dSRodney W. Grimes 		struct in_addr addr;
525df8bae1dSRodney W. Grimes 
526ffa5b11aSGarrett Wollman 		/*
527ffa5b11aSGarrett Wollman 		 * Continuation of multicast address hack:
528ffa5b11aSGarrett Wollman 		 * If there was a multicast group list previously saved
529ffa5b11aSGarrett Wollman 		 * for this interface, then we re-attach it to the first
530ffa5b11aSGarrett Wollman 		 * address configured on the i/f.
531ffa5b11aSGarrett Wollman 		 */
532ffa5b11aSGarrett Wollman 		for(mk = in_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
533ffa5b11aSGarrett Wollman 			if(mk->mk_ifp == ifp) {
534ffa5b11aSGarrett Wollman 				struct in_multi *inm;
535ffa5b11aSGarrett Wollman 
536ffa5b11aSGarrett Wollman 				for(inm = mk->mk_head.lh_first; inm;
537ffa5b11aSGarrett Wollman 				    inm = inm->inm_entry.le_next) {
538ffa5b11aSGarrett Wollman 					IFAFREE(&inm->inm_ia->ia_ifa);
539ffa5b11aSGarrett Wollman 					ia->ia_ifa.ifa_refcnt++;
540ffa5b11aSGarrett Wollman 					inm->inm_ia = ia;
541ffa5b11aSGarrett Wollman 					LIST_INSERT_HEAD(&ia->ia_multiaddrs,
542ffa5b11aSGarrett Wollman 							 inm, inm_entry);
543ffa5b11aSGarrett Wollman 				}
544ffa5b11aSGarrett Wollman 				LIST_REMOVE(mk, mk_entry);
545ffa5b11aSGarrett Wollman 				free(mk, M_IPMADDR);
546ffa5b11aSGarrett Wollman 				break;
547ffa5b11aSGarrett Wollman 			}
548ffa5b11aSGarrett Wollman 		}
549ffa5b11aSGarrett Wollman 
550df8bae1dSRodney W. Grimes 		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
551df8bae1dSRodney W. Grimes 		in_addmulti(&addr, ifp);
552df8bae1dSRodney W. Grimes 	}
553df8bae1dSRodney W. Grimes 	return (error);
554df8bae1dSRodney W. Grimes }
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes /*
558df8bae1dSRodney W. Grimes  * Return 1 if the address might be a local broadcast address.
559df8bae1dSRodney W. Grimes  */
56026f9a767SRodney W. Grimes int
561df8bae1dSRodney W. Grimes in_broadcast(in, ifp)
562df8bae1dSRodney W. Grimes 	struct in_addr in;
563df8bae1dSRodney W. Grimes         struct ifnet *ifp;
564df8bae1dSRodney W. Grimes {
565df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
566df8bae1dSRodney W. Grimes 	u_long t;
567df8bae1dSRodney W. Grimes 
568df8bae1dSRodney W. Grimes 	if (in.s_addr == INADDR_BROADCAST ||
569df8bae1dSRodney W. Grimes 	    in.s_addr == INADDR_ANY)
570df8bae1dSRodney W. Grimes 		return 1;
571df8bae1dSRodney W. Grimes 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
572df8bae1dSRodney W. Grimes 		return 0;
573df8bae1dSRodney W. Grimes 	t = ntohl(in.s_addr);
574df8bae1dSRodney W. Grimes 	/*
575df8bae1dSRodney W. Grimes 	 * Look through the list of addresses for a match
576df8bae1dSRodney W. Grimes 	 * with a broadcast address.
577df8bae1dSRodney W. Grimes 	 */
578df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa)
579df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
580df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == AF_INET &&
581df8bae1dSRodney W. Grimes 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
582df8bae1dSRodney W. Grimes 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
583df8bae1dSRodney W. Grimes 		     /*
584df8bae1dSRodney W. Grimes 		      * Check for old-style (host 0) broadcast.
585df8bae1dSRodney W. Grimes 		      */
586df8bae1dSRodney W. Grimes 		     t == ia->ia_subnet || t == ia->ia_net))
587df8bae1dSRodney W. Grimes 			    return 1;
588df8bae1dSRodney W. Grimes 	return (0);
589df8bae1dSRodney W. Grimes #undef ia
590df8bae1dSRodney W. Grimes }
591df8bae1dSRodney W. Grimes /*
592df8bae1dSRodney W. Grimes  * Add an address to the list of IP multicast addresses for a given interface.
593df8bae1dSRodney W. Grimes  */
594df8bae1dSRodney W. Grimes struct in_multi *
595df8bae1dSRodney W. Grimes in_addmulti(ap, ifp)
596df8bae1dSRodney W. Grimes 	register struct in_addr *ap;
597df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
598df8bae1dSRodney W. Grimes {
599df8bae1dSRodney W. Grimes 	register struct in_multi *inm;
600df8bae1dSRodney W. Grimes 	struct ifreq ifr;
601df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
602df8bae1dSRodney W. Grimes 	int s = splnet();
603df8bae1dSRodney W. Grimes 
604df8bae1dSRodney W. Grimes 	/*
605df8bae1dSRodney W. Grimes 	 * See if address already in list.
606df8bae1dSRodney W. Grimes 	 */
607df8bae1dSRodney W. Grimes 	IN_LOOKUP_MULTI(*ap, ifp, inm);
608df8bae1dSRodney W. Grimes 	if (inm != NULL) {
609df8bae1dSRodney W. Grimes 		/*
610df8bae1dSRodney W. Grimes 		 * Found it; just increment the reference count.
611df8bae1dSRodney W. Grimes 		 */
612df8bae1dSRodney W. Grimes 		++inm->inm_refcount;
613df8bae1dSRodney W. Grimes 	}
614df8bae1dSRodney W. Grimes 	else {
615df8bae1dSRodney W. Grimes 		/*
616df8bae1dSRodney W. Grimes 		 * New address; allocate a new multicast record
617df8bae1dSRodney W. Grimes 		 * and link it into the interface's multicast list.
618df8bae1dSRodney W. Grimes 		 */
619df8bae1dSRodney W. Grimes 		inm = (struct in_multi *)malloc(sizeof(*inm),
620df8bae1dSRodney W. Grimes 		    M_IPMADDR, M_NOWAIT);
621df8bae1dSRodney W. Grimes 		if (inm == NULL) {
622df8bae1dSRodney W. Grimes 			splx(s);
623df8bae1dSRodney W. Grimes 			return (NULL);
624df8bae1dSRodney W. Grimes 		}
625df8bae1dSRodney W. Grimes 		inm->inm_addr = *ap;
626df8bae1dSRodney W. Grimes 		inm->inm_ifp = ifp;
627df8bae1dSRodney W. Grimes 		inm->inm_refcount = 1;
628df8bae1dSRodney W. Grimes 		IFP_TO_IA(ifp, ia);
629df8bae1dSRodney W. Grimes 		if (ia == NULL) {
630df8bae1dSRodney W. Grimes 			free(inm, M_IPMADDR);
631df8bae1dSRodney W. Grimes 			splx(s);
632df8bae1dSRodney W. Grimes 			return (NULL);
633df8bae1dSRodney W. Grimes 		}
634df8bae1dSRodney W. Grimes 		inm->inm_ia = ia;
635ffa5b11aSGarrett Wollman 		ia->ia_ifa.ifa_refcnt++; /* gain a reference */
636ffa5b11aSGarrett Wollman 		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_entry);
637ffa5b11aSGarrett Wollman 
638df8bae1dSRodney W. Grimes 		/*
639df8bae1dSRodney W. Grimes 		 * Ask the network driver to update its multicast reception
640df8bae1dSRodney W. Grimes 		 * filter appropriately for the new address.
641df8bae1dSRodney W. Grimes 		 */
642df8bae1dSRodney W. Grimes 		((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET;
643df8bae1dSRodney W. Grimes 		((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr = *ap;
644df8bae1dSRodney W. Grimes 		if ((ifp->if_ioctl == NULL) ||
645df8bae1dSRodney W. Grimes 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
646ffa5b11aSGarrett Wollman 			LIST_REMOVE(inm, inm_entry);
647ffa5b11aSGarrett Wollman 			IFAFREE(&ia->ia_ifa); /* release reference */
648df8bae1dSRodney W. Grimes 			free(inm, M_IPMADDR);
649df8bae1dSRodney W. Grimes 			splx(s);
650df8bae1dSRodney W. Grimes 			return (NULL);
651df8bae1dSRodney W. Grimes 		}
652df8bae1dSRodney W. Grimes 		/*
653df8bae1dSRodney W. Grimes 		 * Let IGMP know that we have joined a new IP multicast group.
654df8bae1dSRodney W. Grimes 		 */
655df8bae1dSRodney W. Grimes 		igmp_joingroup(inm);
656df8bae1dSRodney W. Grimes 	}
657df8bae1dSRodney W. Grimes 	splx(s);
658df8bae1dSRodney W. Grimes 	return (inm);
659df8bae1dSRodney W. Grimes }
660df8bae1dSRodney W. Grimes 
661df8bae1dSRodney W. Grimes /*
662df8bae1dSRodney W. Grimes  * Delete a multicast address record.
663df8bae1dSRodney W. Grimes  */
66426f9a767SRodney W. Grimes void
665df8bae1dSRodney W. Grimes in_delmulti(inm)
666df8bae1dSRodney W. Grimes 	register struct in_multi *inm;
667df8bae1dSRodney W. Grimes {
668df8bae1dSRodney W. Grimes 	struct ifreq ifr;
669df8bae1dSRodney W. Grimes 	int s = splnet();
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 	if (--inm->inm_refcount == 0) {
672df8bae1dSRodney W. Grimes 		/*
673df8bae1dSRodney W. Grimes 		 * No remaining claims to this record; let IGMP know that
674df8bae1dSRodney W. Grimes 		 * we are leaving the multicast group.
675df8bae1dSRodney W. Grimes 		 */
676df8bae1dSRodney W. Grimes 		igmp_leavegroup(inm);
677df8bae1dSRodney W. Grimes 		/*
678df8bae1dSRodney W. Grimes 		 * Unlink from list.
679df8bae1dSRodney W. Grimes 		 */
680ffa5b11aSGarrett Wollman 		LIST_REMOVE(inm, inm_entry);
681ffa5b11aSGarrett Wollman 		IFAFREE(&inm->inm_ia->ia_ifa); /* release reference */
682ffa5b11aSGarrett Wollman 
683df8bae1dSRodney W. Grimes 		/*
684df8bae1dSRodney W. Grimes 		 * Notify the network driver to update its multicast reception
685df8bae1dSRodney W. Grimes 		 * filter.
686df8bae1dSRodney W. Grimes 		 */
687df8bae1dSRodney W. Grimes 		((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
688df8bae1dSRodney W. Grimes 		((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr =
689df8bae1dSRodney W. Grimes 								inm->inm_addr;
690df8bae1dSRodney W. Grimes 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
691df8bae1dSRodney W. Grimes 							     (caddr_t)&ifr);
692df8bae1dSRodney W. Grimes 		free(inm, M_IPMADDR);
693df8bae1dSRodney W. Grimes 	}
694df8bae1dSRodney W. Grimes 	splx(s);
695df8bae1dSRodney W. Grimes }
696