xref: /netbsd-src/sys/netinet/in.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: in.c,v 1.122 2007/12/06 00:28:36 dyoung Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Public Access Networks Corporation ("Panix").  It was developed under
38  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *	This product includes software developed by the NetBSD
51  *	Foundation, Inc. and its contributors.
52  * 4. Neither the name of The NetBSD Foundation nor the names of its
53  *    contributors may be used to endorse or promote products derived
54  *    from this software without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66  * POSSIBILITY OF SUCH DAMAGE.
67  */
68 
69 /*
70  * Copyright (c) 1982, 1986, 1991, 1993
71  *	The Regents of the University of California.  All rights reserved.
72  *
73  * Redistribution and use in source and binary forms, with or without
74  * modification, are permitted provided that the following conditions
75  * are met:
76  * 1. Redistributions of source code must retain the above copyright
77  *    notice, this list of conditions and the following disclaimer.
78  * 2. Redistributions in binary form must reproduce the above copyright
79  *    notice, this list of conditions and the following disclaimer in the
80  *    documentation and/or other materials provided with the distribution.
81  * 3. Neither the name of the University nor the names of its contributors
82  *    may be used to endorse or promote products derived from this software
83  *    without specific prior written permission.
84  *
85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95  * SUCH DAMAGE.
96  *
97  *	@(#)in.c	8.4 (Berkeley) 1/9/95
98  */
99 
100 #include <sys/cdefs.h>
101 __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.122 2007/12/06 00:28:36 dyoung Exp $");
102 
103 #include "opt_inet.h"
104 #include "opt_inet_conf.h"
105 #include "opt_mrouting.h"
106 #include "opt_pfil_hooks.h"
107 
108 #include <sys/param.h>
109 #include <sys/ioctl.h>
110 #include <sys/errno.h>
111 #include <sys/malloc.h>
112 #include <sys/socket.h>
113 #include <sys/socketvar.h>
114 #include <sys/sysctl.h>
115 #include <sys/systm.h>
116 #include <sys/proc.h>
117 #include <sys/syslog.h>
118 #include <sys/kauth.h>
119 
120 #include <net/if.h>
121 #include <net/route.h>
122 
123 #include <net/if_ether.h>
124 
125 #include <netinet/in_systm.h>
126 #include <netinet/in.h>
127 #include <netinet/in_var.h>
128 #include <netinet/ip.h>
129 #include <netinet/ip_var.h>
130 #include <netinet/in_ifattach.h>
131 #include <netinet/in_pcb.h>
132 #include <netinet/if_inarp.h>
133 #include <netinet/ip_mroute.h>
134 #include <netinet/igmp_var.h>
135 
136 #ifdef IPSELSRC
137 #include <netinet/in_selsrc.h>
138 #endif
139 
140 #ifdef PFIL_HOOKS
141 #include <net/pfil.h>
142 #endif
143 
144 #ifdef INET
145 static u_int in_mask2len(struct in_addr *);
146 static void in_len2mask(struct in_addr *, u_int);
147 static int in_lifaddr_ioctl(struct socket *, u_long, void *,
148 	struct ifnet *, struct lwp *);
149 
150 static int in_ifaddrpref_ioctl(struct socket *, u_long, void *,
151 	struct ifnet *);
152 static int in_addprefix(struct in_ifaddr *, int);
153 static int in_scrubprefix(struct in_ifaddr *);
154 
155 #ifndef SUBNETSARELOCAL
156 #define	SUBNETSARELOCAL	1
157 #endif
158 
159 #ifndef HOSTZEROBROADCAST
160 #define HOSTZEROBROADCAST 1
161 #endif
162 
163 int subnetsarelocal = SUBNETSARELOCAL;
164 int hostzeroisbroadcast = HOSTZEROBROADCAST;
165 
166 /*
167  * This list is used to keep track of in_multi chains which belong to
168  * deleted interface addresses.  We use in_ifaddr so that a chain head
169  * won't be deallocated until all multicast address record are deleted.
170  */
171 static TAILQ_HEAD(, in_ifaddr) in_mk = TAILQ_HEAD_INITIALIZER(in_mk);
172 
173 /*
174  * Return 1 if an internet address is for a ``local'' host
175  * (one to which we have a connection).  If subnetsarelocal
176  * is true, this includes other subnets of the local net.
177  * Otherwise, it includes only the directly-connected (sub)nets.
178  */
179 int
180 in_localaddr(struct in_addr in)
181 {
182 	struct in_ifaddr *ia;
183 
184 	if (subnetsarelocal) {
185 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
186 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
187 				return (1);
188 	} else {
189 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
190 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
191 				return (1);
192 	}
193 	return (0);
194 }
195 
196 /*
197  * Determine whether an IP address is in a reserved set of addresses
198  * that may not be forwarded, or whether datagrams to that destination
199  * may be forwarded.
200  */
201 int
202 in_canforward(struct in_addr in)
203 {
204 	u_int32_t net;
205 
206 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
207 		return (0);
208 	if (IN_CLASSA(in.s_addr)) {
209 		net = in.s_addr & IN_CLASSA_NET;
210 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
211 			return (0);
212 	}
213 	return (1);
214 }
215 
216 /*
217  * Trim a mask in a sockaddr
218  */
219 void
220 in_socktrim(struct sockaddr_in *ap)
221 {
222 	char *cplim = (char *) &ap->sin_addr;
223 	char *cp = (char *) (&ap->sin_addr + 1);
224 
225 	ap->sin_len = 0;
226 	while (--cp >= cplim)
227 		if (*cp) {
228 			(ap)->sin_len = cp - (char *) (ap) + 1;
229 			break;
230 		}
231 }
232 
233 /*
234  *  Routine to take an Internet address and convert into a
235  *  "dotted quad" representation for printing.
236  */
237 const char *
238 in_fmtaddr(struct in_addr addr)
239 {
240 	static char buf[sizeof("123.456.789.123")];
241 
242 	addr.s_addr = ntohl(addr.s_addr);
243 
244 	snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
245 		(addr.s_addr >> 24) & 0xFF,
246 		(addr.s_addr >> 16) & 0xFF,
247 		(addr.s_addr >>  8) & 0xFF,
248 		(addr.s_addr >>  0) & 0xFF);
249 	return buf;
250 }
251 
252 /*
253  * Maintain the "in_maxmtu" variable, which is the largest
254  * mtu for non-local interfaces with AF_INET addresses assigned
255  * to them that are up.
256  */
257 unsigned long in_maxmtu;
258 
259 void
260 in_setmaxmtu(void)
261 {
262 	struct in_ifaddr *ia;
263 	struct ifnet *ifp;
264 	unsigned long maxmtu = 0;
265 
266 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
267 		if ((ifp = ia->ia_ifp) == 0)
268 			continue;
269 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
270 			continue;
271 		if (ifp->if_mtu > maxmtu)
272 			maxmtu = ifp->if_mtu;
273 	}
274 	if (maxmtu)
275 		in_maxmtu = maxmtu;
276 }
277 
278 static u_int
279 in_mask2len(struct in_addr *mask)
280 {
281 	u_int x, y;
282 	u_char *p;
283 
284 	p = (u_char *)mask;
285 	for (x = 0; x < sizeof(*mask); x++) {
286 		if (p[x] != 0xff)
287 			break;
288 	}
289 	y = 0;
290 	if (x < sizeof(*mask)) {
291 		for (y = 0; y < 8; y++) {
292 			if ((p[x] & (0x80 >> y)) == 0)
293 				break;
294 		}
295 	}
296 	return x * 8 + y;
297 }
298 
299 static void
300 in_len2mask(struct in_addr *mask, u_int len)
301 {
302 	u_int i;
303 	u_char *p;
304 
305 	p = (u_char *)mask;
306 	bzero(mask, sizeof(*mask));
307 	for (i = 0; i < len / 8; i++)
308 		p[i] = 0xff;
309 	if (len % 8)
310 		p[i] = (0xff00 >> (len % 8)) & 0xff;
311 }
312 
313 /*
314  * Generic internet control operations (ioctl's).
315  * Ifp is 0 if not an interface-specific ioctl.
316  */
317 /* ARGSUSED */
318 int
319 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
320     struct lwp *l)
321 {
322 	struct ifreq *ifr = (struct ifreq *)data;
323 	struct in_ifaddr *ia = 0;
324 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
325 	struct sockaddr_in oldaddr;
326 	int error, hostIsNew, maskIsNew;
327 	int newifaddr = 0;
328 
329 	switch (cmd) {
330 	case SIOCALIFADDR:
331 	case SIOCDLIFADDR:
332 	case SIOCSIFADDRPREF:
333 		if (l == NULL)
334 			return (EPERM);
335 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
336 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
337 		    NULL) != 0)
338 			return (EPERM);
339 		/*FALLTHROUGH*/
340 	case SIOCGIFADDRPREF:
341 	case SIOCGLIFADDR:
342 		if (ifp == NULL)
343 			return EINVAL;
344 		if (cmd == SIOCGIFADDRPREF || cmd == SIOCSIFADDRPREF)
345 			return in_ifaddrpref_ioctl(so, cmd, data, ifp);
346 		else
347 			return in_lifaddr_ioctl(so, cmd, data, ifp, l);
348 	}
349 
350 	/*
351 	 * Find address for this interface, if it exists.
352 	 */
353 	if (ifp != NULL)
354 		IFP_TO_IA(ifp, ia);
355 
356 	switch (cmd) {
357 
358 	case SIOCAIFADDR:
359 	case SIOCDIFADDR:
360 	case SIOCGIFALIAS:
361 		if (ifra->ifra_addr.sin_family == AF_INET)
362 			LIST_FOREACH(ia,
363 			    &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
364 			    ia_hash) {
365 				if (ia->ia_ifp == ifp &&
366 				    in_hosteq(ia->ia_addr.sin_addr,
367 				    ifra->ifra_addr.sin_addr))
368 					break;
369 			}
370 		if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL)
371 			return (EADDRNOTAVAIL);
372 
373 #if 1 /*def COMPAT_43*/
374 		if (cmd == SIOCDIFADDR &&
375 		    ifra->ifra_addr.sin_family == AF_UNSPEC) {
376 			ifra->ifra_addr.sin_family = AF_INET;
377 		}
378 #endif
379 		/* FALLTHROUGH */
380 	case SIOCSIFADDR:
381 	case SIOCSIFDSTADDR:
382 		if (ifra->ifra_addr.sin_family != AF_INET)
383 			return (EAFNOSUPPORT);
384 		/* FALLTHROUGH */
385 	case SIOCSIFNETMASK:
386 		if (ifp == NULL)
387 			panic("in_control");
388 
389 		if (cmd == SIOCGIFALIAS)
390 			break;
391 
392 		if (ia == NULL &&
393 		    (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
394 			return (EADDRNOTAVAIL);
395 
396 		if (l == NULL)
397 			return (EPERM);
398 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
399 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
400 		    NULL) != 0)
401 			return (EPERM);
402 
403 		if (ia == 0) {
404 			MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
405 			       M_IFADDR, M_WAITOK);
406 			if (ia == 0)
407 				return (ENOBUFS);
408 			bzero((void *)ia, sizeof *ia);
409 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
410 			IFAREF(&ia->ia_ifa);
411 			ifa_insert(ifp, &ia->ia_ifa);
412 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
413 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
414 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
415 #ifdef IPSELSRC
416 			ia->ia_ifa.ifa_getifa = in_getifa;
417 #else /* IPSELSRC */
418 			ia->ia_ifa.ifa_getifa = NULL;
419 #endif /* IPSELSRC */
420 			ia->ia_sockmask.sin_len = 8;
421 			if (ifp->if_flags & IFF_BROADCAST) {
422 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
423 				ia->ia_broadaddr.sin_family = AF_INET;
424 			}
425 			ia->ia_ifp = ifp;
426 			LIST_INIT(&ia->ia_multiaddrs);
427 			newifaddr = 1;
428 		}
429 		break;
430 
431 	case SIOCSIFBRDADDR:
432 		if (l == NULL)
433 			return (EPERM);
434 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
435 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
436 		    NULL) != 0)
437 			return (EPERM);
438 		/* FALLTHROUGH */
439 
440 	case SIOCGIFADDR:
441 	case SIOCGIFNETMASK:
442 	case SIOCGIFDSTADDR:
443 	case SIOCGIFBRDADDR:
444 		if (ia == 0)
445 			return (EADDRNOTAVAIL);
446 		break;
447 	}
448 	error = 0;
449 	switch (cmd) {
450 
451 	case SIOCGIFADDR:
452 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
453 		break;
454 
455 	case SIOCGIFBRDADDR:
456 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
457 			return (EINVAL);
458 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
459 		break;
460 
461 	case SIOCGIFDSTADDR:
462 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
463 			return (EINVAL);
464 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
465 		break;
466 
467 	case SIOCGIFNETMASK:
468 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_sockmask));
469 		break;
470 
471 	case SIOCSIFDSTADDR:
472 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
473 			return (EINVAL);
474 		oldaddr = ia->ia_dstaddr;
475 		ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
476 		if (ifp->if_ioctl != NULL &&
477 		    (error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
478 		                              (void *)ia)) != 0) {
479 			ia->ia_dstaddr = oldaddr;
480 			return error;
481 		}
482 		if (ia->ia_flags & IFA_ROUTE) {
483 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
484 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
485 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
486 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
487 		}
488 		break;
489 
490 	case SIOCSIFBRDADDR:
491 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
492 			return EINVAL;
493 		ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
494 		break;
495 
496 	case SIOCSIFADDR:
497 		error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
498 		    1);
499 #ifdef PFIL_HOOKS
500 		if (error == 0)
501 			(void)pfil_run_hooks(&if_pfil,
502 			    (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
503 #endif
504 		break;
505 
506 	case SIOCSIFNETMASK:
507 		in_ifscrub(ifp, ia);
508 		ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
509 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
510 		error = in_ifinit(ifp, ia, NULL, 0);
511 		break;
512 
513 	case SIOCAIFADDR:
514 		maskIsNew = 0;
515 		hostIsNew = 1;
516 		if (ia->ia_addr.sin_family != AF_INET)
517 			;
518 		else if (ifra->ifra_addr.sin_len == 0) {
519 			ifra->ifra_addr = ia->ia_addr;
520 			hostIsNew = 0;
521 		} else if (in_hosteq(ia->ia_addr.sin_addr,
522 		           ifra->ifra_addr.sin_addr))
523 			hostIsNew = 0;
524 		if (ifra->ifra_mask.sin_len) {
525 			in_ifscrub(ifp, ia);
526 			ia->ia_sockmask = ifra->ifra_mask;
527 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
528 			maskIsNew = 1;
529 		}
530 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
531 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
532 			in_ifscrub(ifp, ia);
533 			ia->ia_dstaddr = ifra->ifra_dstaddr;
534 			maskIsNew  = 1; /* We lie; but the effect's the same */
535 		}
536 		if (ifra->ifra_addr.sin_family == AF_INET &&
537 		    (hostIsNew || maskIsNew)) {
538 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
539 		}
540 		if ((ifp->if_flags & IFF_BROADCAST) &&
541 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
542 			ia->ia_broadaddr = ifra->ifra_broadaddr;
543 #ifdef PFIL_HOOKS
544 		if (error == 0)
545 			(void)pfil_run_hooks(&if_pfil,
546 			    (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
547 #endif
548 		break;
549 
550 	case SIOCGIFALIAS:
551 		ifra->ifra_mask = ia->ia_sockmask;
552 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
553 		    (ia->ia_dstaddr.sin_family == AF_INET))
554 			ifra->ifra_dstaddr = ia->ia_dstaddr;
555 		else if ((ifp->if_flags & IFF_BROADCAST) &&
556 		    (ia->ia_broadaddr.sin_family == AF_INET))
557 			ifra->ifra_broadaddr = ia->ia_broadaddr;
558 		else
559 			memset(&ifra->ifra_broadaddr, 0,
560 			      sizeof(ifra->ifra_broadaddr));
561 		break;
562 
563 	case SIOCDIFADDR:
564 		in_purgeaddr(&ia->ia_ifa);
565 #ifdef PFIL_HOOKS
566 		(void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR,
567 		    ifp, PFIL_IFADDR);
568 #endif
569 		break;
570 
571 #ifdef MROUTING
572 	case SIOCGETVIFCNT:
573 	case SIOCGETSGCNT:
574 		error = mrt_ioctl(so, cmd, data);
575 		break;
576 #endif /* MROUTING */
577 
578 	default:
579 		if (ifp == NULL || ifp->if_ioctl == NULL)
580 			return EOPNOTSUPP;
581 		error = (*ifp->if_ioctl)(ifp, cmd, data);
582 		in_setmaxmtu();
583 		break;
584 	}
585 
586 	if (error != 0 && newifaddr) {
587 		KASSERT(ia != NULL);
588 		in_purgeaddr(&ia->ia_ifa);
589 	}
590 
591 	return error;
592 }
593 
594 void
595 in_purgeaddr(struct ifaddr *ifa)
596 {
597 	struct ifnet *ifp = ifa->ifa_ifp;
598 	struct in_ifaddr *ia = (void *) ifa;
599 
600 	in_ifscrub(ifp, ia);
601 	LIST_REMOVE(ia, ia_hash);
602 	ifa_remove(ifp, &ia->ia_ifa);
603 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
604 	if (ia->ia_allhosts != NULL)
605 		in_delmulti(ia->ia_allhosts);
606 	IFAFREE(&ia->ia_ifa);
607 	in_setmaxmtu();
608 }
609 
610 void
611 in_purgeif(struct ifnet *ifp)		/* MUST be called at splsoftnet() */
612 {
613 	if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
614 	igmp_purgeif(ifp);		/* manipulates pools */
615 #ifdef MROUTING
616 	ip_mrouter_detach(ifp);
617 #endif
618 }
619 
620 /*
621  * SIOC[GAD]LIFADDR.
622  *	SIOCGLIFADDR: get first address. (???)
623  *	SIOCGLIFADDR with IFLR_PREFIX:
624  *		get first address that matches the specified prefix.
625  *	SIOCALIFADDR: add the specified address.
626  *	SIOCALIFADDR with IFLR_PREFIX:
627  *		EINVAL since we can't deduce hostid part of the address.
628  *	SIOCDLIFADDR: delete the specified address.
629  *	SIOCDLIFADDR with IFLR_PREFIX:
630  *		delete the first address that matches the specified prefix.
631  * return values:
632  *	EINVAL on invalid parameters
633  *	EADDRNOTAVAIL on prefix match failed/specified address not found
634  *	other values may be returned from in_ioctl()
635  */
636 static int
637 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
638     struct ifnet *ifp, struct lwp *l)
639 {
640 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
641 	struct ifaddr *ifa;
642 	struct sockaddr *sa;
643 
644 	/* sanity checks */
645 	if (data == NULL || ifp == NULL) {
646 		panic("invalid argument to in_lifaddr_ioctl");
647 		/*NOTRECHED*/
648 	}
649 
650 	switch (cmd) {
651 	case SIOCGLIFADDR:
652 		/* address must be specified on GET with IFLR_PREFIX */
653 		if ((iflr->flags & IFLR_PREFIX) == 0)
654 			break;
655 		/*FALLTHROUGH*/
656 	case SIOCALIFADDR:
657 	case SIOCDLIFADDR:
658 		/* address must be specified on ADD and DELETE */
659 		sa = (struct sockaddr *)&iflr->addr;
660 		if (sa->sa_family != AF_INET)
661 			return EINVAL;
662 		if (sa->sa_len != sizeof(struct sockaddr_in))
663 			return EINVAL;
664 		/* XXX need improvement */
665 		sa = (struct sockaddr *)&iflr->dstaddr;
666 		if (sa->sa_family
667 		 && sa->sa_family != AF_INET)
668 			return EINVAL;
669 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in))
670 			return EINVAL;
671 		break;
672 	default: /*shouldn't happen*/
673 #if 0
674 		panic("invalid cmd to in_lifaddr_ioctl");
675 		/*NOTREACHED*/
676 #else
677 		return EOPNOTSUPP;
678 #endif
679 	}
680 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
681 		return EINVAL;
682 
683 	switch (cmd) {
684 	case SIOCALIFADDR:
685 	    {
686 		struct in_aliasreq ifra;
687 
688 		if (iflr->flags & IFLR_PREFIX)
689 			return EINVAL;
690 
691 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
692 		bzero(&ifra, sizeof(ifra));
693 		bcopy(iflr->iflr_name, ifra.ifra_name,
694 			sizeof(ifra.ifra_name));
695 
696 		bcopy(&iflr->addr, &ifra.ifra_addr,
697 			((struct sockaddr *)&iflr->addr)->sa_len);
698 
699 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
700 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
701 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
702 		}
703 
704 		ifra.ifra_mask.sin_family = AF_INET;
705 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
706 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
707 
708 		return in_control(so, SIOCAIFADDR, (void *)&ifra, ifp, l);
709 	    }
710 	case SIOCGLIFADDR:
711 	case SIOCDLIFADDR:
712 	    {
713 		struct in_ifaddr *ia;
714 		struct in_addr mask, candidate, match;
715 		struct sockaddr_in *sin;
716 		int cmp;
717 
718 		bzero(&mask, sizeof(mask));
719 		bzero(&match, sizeof(match));	/* XXX gcc */
720 		if (iflr->flags & IFLR_PREFIX) {
721 			/* lookup a prefix rather than address. */
722 			in_len2mask(&mask, iflr->prefixlen);
723 
724 			sin = (struct sockaddr_in *)&iflr->addr;
725 			match.s_addr = sin->sin_addr.s_addr;
726 			match.s_addr &= mask.s_addr;
727 
728 			/* if you set extra bits, that's wrong */
729 			if (match.s_addr != sin->sin_addr.s_addr)
730 				return EINVAL;
731 
732 			cmp = 1;
733 		} else {
734 			if (cmd == SIOCGLIFADDR) {
735 				/* on getting an address, take the 1st match */
736 				cmp = 0;	/*XXX*/
737 			} else {
738 				/* on deleting an address, do exact match */
739 				in_len2mask(&mask, 32);
740 				sin = (struct sockaddr_in *)&iflr->addr;
741 				match.s_addr = sin->sin_addr.s_addr;
742 
743 				cmp = 1;
744 			}
745 		}
746 
747 		IFADDR_FOREACH(ifa, ifp) {
748 			if (ifa->ifa_addr->sa_family != AF_INET)
749 				continue;
750 			if (cmp == 0)
751 				break;
752 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
753 			candidate.s_addr &= mask.s_addr;
754 			if (candidate.s_addr == match.s_addr)
755 				break;
756 		}
757 		if (ifa == NULL)
758 			return EADDRNOTAVAIL;
759 		ia = (struct in_ifaddr *)ifa;
760 
761 		if (cmd == SIOCGLIFADDR) {
762 			/* fill in the if_laddrreq structure */
763 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
764 
765 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
766 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
767 					ia->ia_dstaddr.sin_len);
768 			} else
769 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
770 
771 			iflr->prefixlen =
772 				in_mask2len(&ia->ia_sockmask.sin_addr);
773 
774 			iflr->flags = 0;	/*XXX*/
775 
776 			return 0;
777 		} else {
778 			struct in_aliasreq ifra;
779 
780 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
781 			bzero(&ifra, sizeof(ifra));
782 			bcopy(iflr->iflr_name, ifra.ifra_name,
783 				sizeof(ifra.ifra_name));
784 
785 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
786 				ia->ia_addr.sin_len);
787 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
788 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
789 					ia->ia_dstaddr.sin_len);
790 			}
791 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
792 				ia->ia_sockmask.sin_len);
793 
794 			return in_control(so, SIOCDIFADDR, (void *)&ifra,
795 				ifp, l);
796 		}
797 	    }
798 	}
799 
800 	return EOPNOTSUPP;	/*just for safety*/
801 }
802 
803 static int
804 in_ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data,
805     struct ifnet *ifp)
806 {
807 	struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
808 	struct ifaddr *ifa;
809 	struct sockaddr *sa;
810 	struct in_ifaddr *ia = NULL; /* appease gcc -Wuninitialized */
811 	struct in_addr match;
812 	struct sockaddr_in *sin;
813 
814 	/* sanity checks */
815 	if (data == NULL || ifp == NULL) {
816 		panic("invalid argument to %s", __func__);
817 		/*NOTREACHED*/
818 	}
819 
820 	/* address must be specified on ADD and DELETE */
821 	sa = (struct sockaddr *)&ifap->ifap_addr;
822 	if (sa->sa_family != AF_INET)
823 		return EINVAL;
824 	if (sa->sa_len != sizeof(struct sockaddr_in))
825 		return EINVAL;
826 
827 	switch (cmd) {
828 	case SIOCSIFADDRPREF:
829 	case SIOCGIFADDRPREF:
830 		break;
831 	default:
832 		return EOPNOTSUPP;
833 	}
834 
835 	sin = (struct sockaddr_in *)&ifap->ifap_addr;
836 	match.s_addr = sin->sin_addr.s_addr;
837 
838 	IFADDR_FOREACH(ifa, ifp) {
839 		ia = (struct in_ifaddr *)ifa;
840 		if (ia->ia_addr.sin_family != AF_INET)
841 			continue;
842 		if (ia->ia_addr.sin_addr.s_addr == match.s_addr)
843 			break;
844 	}
845 	if (ifa == NULL)
846 		return EADDRNOTAVAIL;
847 
848 	switch (cmd) {
849 	case SIOCSIFADDRPREF:
850 		ifa->ifa_preference = ifap->ifap_preference;
851 		return 0;
852 	case SIOCGIFADDRPREF:
853 		/* fill in the if_laddrreq structure */
854 		(void)memcpy(&ifap->ifap_addr, &ia->ia_addr,
855 		    ia->ia_addr.sin_len);
856 		ifap->ifap_preference = ifa->ifa_preference;
857 		return 0;
858 	default:
859 		return EOPNOTSUPP;
860 	}
861 }
862 
863 /*
864  * Delete any existing route for an interface.
865  */
866 void
867 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
868 {
869 
870 	in_scrubprefix(ia);
871 }
872 
873 /*
874  * Initialize an interface's internet address
875  * and routing table entry.
876  */
877 int
878 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
879     const struct sockaddr_in *sin, int scrub)
880 {
881 	u_int32_t i;
882 	struct sockaddr_in oldaddr;
883 	int s = splnet(), flags = RTF_UP, error;
884 
885 	if (sin == NULL)
886 		sin = &ia->ia_addr;
887 
888 	/*
889 	 * Set up new addresses.
890 	 */
891 	oldaddr = ia->ia_addr;
892 	if (ia->ia_addr.sin_family == AF_INET)
893 		LIST_REMOVE(ia, ia_hash);
894 	ia->ia_addr = *sin;
895 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
896 
897 	/*
898 	 * Give the interface a chance to initialize
899 	 * if this is its first address,
900 	 * and to validate the address if necessary.
901 	 */
902 	if (ifp->if_ioctl &&
903 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (void *)ia)))
904 		goto bad;
905 	splx(s);
906 	if (scrub) {
907 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
908 		in_ifscrub(ifp, ia);
909 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
910 	}
911 
912 	i = ia->ia_addr.sin_addr.s_addr;
913 	if (IN_CLASSA(i))
914 		ia->ia_netmask = IN_CLASSA_NET;
915 	else if (IN_CLASSB(i))
916 		ia->ia_netmask = IN_CLASSB_NET;
917 	else
918 		ia->ia_netmask = IN_CLASSC_NET;
919 	/*
920 	 * The subnet mask usually includes at least the standard network part,
921 	 * but may may be smaller in the case of supernetting.
922 	 * If it is set, we believe it.
923 	 */
924 	if (ia->ia_subnetmask == 0) {
925 		ia->ia_subnetmask = ia->ia_netmask;
926 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
927 	} else
928 		ia->ia_netmask &= ia->ia_subnetmask;
929 
930 	ia->ia_net = i & ia->ia_netmask;
931 	ia->ia_subnet = i & ia->ia_subnetmask;
932 	in_socktrim(&ia->ia_sockmask);
933 	/* re-calculate the "in_maxmtu" value */
934 	in_setmaxmtu();
935 	/*
936 	 * Add route for the network.
937 	 */
938 	ia->ia_ifa.ifa_metric = ifp->if_metric;
939 	if (ifp->if_flags & IFF_BROADCAST) {
940 		ia->ia_broadaddr.sin_addr.s_addr =
941 			ia->ia_subnet | ~ia->ia_subnetmask;
942 		ia->ia_netbroadcast.s_addr =
943 			ia->ia_net | ~ia->ia_netmask;
944 	} else if (ifp->if_flags & IFF_LOOPBACK) {
945 		ia->ia_dstaddr = ia->ia_addr;
946 		flags |= RTF_HOST;
947 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
948 		if (ia->ia_dstaddr.sin_family != AF_INET)
949 			return (0);
950 		flags |= RTF_HOST;
951 	}
952 	error = in_addprefix(ia, flags);
953 	/*
954 	 * If the interface supports multicast, join the "all hosts"
955 	 * multicast group on that interface.
956 	 */
957 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
958 		struct in_addr addr;
959 
960 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
961 		ia->ia_allhosts = in_addmulti(&addr, ifp);
962 	}
963 	return (error);
964 bad:
965 	splx(s);
966 	LIST_REMOVE(ia, ia_hash);
967 	ia->ia_addr = oldaddr;
968 	if (ia->ia_addr.sin_family == AF_INET)
969 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
970 		    ia, ia_hash);
971 	return (error);
972 }
973 
974 #define rtinitflags(x) \
975 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
976 	    ? RTF_HOST : 0)
977 
978 /*
979  * add a route to prefix ("connected route" in cisco terminology).
980  * does nothing if there's some interface address with the same prefix already.
981  */
982 static int
983 in_addprefix(struct in_ifaddr *target, int flags)
984 {
985 	struct in_ifaddr *ia;
986 	struct in_addr prefix, mask, p;
987 	int error;
988 
989 	if ((flags & RTF_HOST) != 0)
990 		prefix = target->ia_dstaddr.sin_addr;
991 	else {
992 		prefix = target->ia_addr.sin_addr;
993 		mask = target->ia_sockmask.sin_addr;
994 		prefix.s_addr &= mask.s_addr;
995 	}
996 
997 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
998 		if (rtinitflags(ia))
999 			p = ia->ia_dstaddr.sin_addr;
1000 		else {
1001 			p = ia->ia_addr.sin_addr;
1002 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1003 		}
1004 
1005 		if (prefix.s_addr != p.s_addr)
1006 			continue;
1007 
1008 		/*
1009 		 * if we got a matching prefix route inserted by other
1010 		 * interface address, we don't need to bother
1011 		 *
1012 		 * XXX RADIX_MPATH implications here? -dyoung
1013 		 */
1014 		if (ia->ia_flags & IFA_ROUTE)
1015 			return 0;
1016 	}
1017 
1018 	/*
1019 	 * noone seem to have prefix route.  insert it.
1020 	 */
1021 	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1022 	if (error == 0)
1023 		target->ia_flags |= IFA_ROUTE;
1024 	return error;
1025 }
1026 
1027 /*
1028  * remove a route to prefix ("connected route" in cisco terminology).
1029  * re-installs the route by using another interface address, if there's one
1030  * with the same prefix (otherwise we lose the route mistakenly).
1031  */
1032 static int
1033 in_scrubprefix(struct in_ifaddr *target)
1034 {
1035 	struct in_ifaddr *ia;
1036 	struct in_addr prefix, mask, p;
1037 	int error;
1038 
1039 	if ((target->ia_flags & IFA_ROUTE) == 0)
1040 		return 0;
1041 
1042 	if (rtinitflags(target))
1043 		prefix = target->ia_dstaddr.sin_addr;
1044 	else {
1045 		prefix = target->ia_addr.sin_addr;
1046 		mask = target->ia_sockmask.sin_addr;
1047 		prefix.s_addr &= mask.s_addr;
1048 	}
1049 
1050 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1051 		if (rtinitflags(ia))
1052 			p = ia->ia_dstaddr.sin_addr;
1053 		else {
1054 			p = ia->ia_addr.sin_addr;
1055 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1056 		}
1057 
1058 		if (prefix.s_addr != p.s_addr)
1059 			continue;
1060 
1061 		/*
1062 		 * if we got a matching prefix route, move IFA_ROUTE to him
1063 		 */
1064 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
1065 			rtinit(&target->ia_ifa, RTM_DELETE,
1066 			    rtinitflags(target));
1067 			target->ia_flags &= ~IFA_ROUTE;
1068 
1069 			error = rtinit(&ia->ia_ifa, RTM_ADD,
1070 			    rtinitflags(ia) | RTF_UP);
1071 			if (error == 0)
1072 				ia->ia_flags |= IFA_ROUTE;
1073 			return error;
1074 		}
1075 	}
1076 
1077 	/*
1078 	 * noone seem to have prefix route.  remove it.
1079 	 */
1080 	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1081 	target->ia_flags &= ~IFA_ROUTE;
1082 	return 0;
1083 }
1084 
1085 #undef rtinitflags
1086 
1087 /*
1088  * Return 1 if the address might be a local broadcast address.
1089  */
1090 int
1091 in_broadcast(struct in_addr in, struct ifnet *ifp)
1092 {
1093 	struct ifaddr *ifa;
1094 
1095 	if (in.s_addr == INADDR_BROADCAST ||
1096 	    in_nullhost(in))
1097 		return 1;
1098 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1099 		return 0;
1100 	/*
1101 	 * Look through the list of addresses for a match
1102 	 * with a broadcast address.
1103 	 */
1104 #define ia (ifatoia(ifa))
1105 	IFADDR_FOREACH(ifa, ifp)
1106 		if (ifa->ifa_addr->sa_family == AF_INET &&
1107 		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
1108 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1109 		     in_hosteq(in, ia->ia_netbroadcast) ||
1110 		     (hostzeroisbroadcast &&
1111 		      /*
1112 		       * Check for old-style (host 0) broadcast.
1113 		       */
1114 		      (in.s_addr == ia->ia_subnet ||
1115 		       in.s_addr == ia->ia_net))))
1116 			return 1;
1117 	return (0);
1118 #undef ia
1119 }
1120 
1121 /*
1122  * Add an address to the list of IP multicast addresses for a given interface.
1123  */
1124 struct in_multi *
1125 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1126 {
1127 	struct sockaddr_in sin;
1128 	struct in_multi *inm;
1129 	struct ifreq ifr;
1130 	int s = splsoftnet();
1131 
1132 	/*
1133 	 * See if address already in list.
1134 	 */
1135 	IN_LOOKUP_MULTI(*ap, ifp, inm);
1136 	if (inm != NULL) {
1137 		/*
1138 		 * Found it; just increment the reference count.
1139 		 */
1140 		++inm->inm_refcount;
1141 	} else {
1142 		/*
1143 		 * New address; allocate a new multicast record
1144 		 * and link it into the interface's multicast list.
1145 		 */
1146 		inm = pool_get(&inmulti_pool, PR_NOWAIT);
1147 		if (inm == NULL) {
1148 			splx(s);
1149 			return (NULL);
1150 		}
1151 		inm->inm_addr = *ap;
1152 		inm->inm_ifp = ifp;
1153 		inm->inm_refcount = 1;
1154 		LIST_INSERT_HEAD(
1155 		    &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1156 		    inm, inm_list);
1157 		/*
1158 		 * Ask the network driver to update its multicast reception
1159 		 * filter appropriately for the new address.
1160 		 */
1161 		sockaddr_in_init(&sin, ap, 0);
1162 		ifreq_setaddr(SIOCADDMULTI, &ifr, sintosa(&sin));
1163 		if ((ifp->if_ioctl == NULL) ||
1164 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(void *)&ifr) != 0) {
1165 			LIST_REMOVE(inm, inm_list);
1166 			pool_put(&inmulti_pool, inm);
1167 			splx(s);
1168 			return (NULL);
1169 		}
1170 		/*
1171 		 * Let IGMP know that we have joined a new IP multicast group.
1172 		 */
1173 		if (igmp_joingroup(inm) != 0) {
1174 			LIST_REMOVE(inm, inm_list);
1175 			pool_put(&inmulti_pool, inm);
1176 			splx(s);
1177 			return (NULL);
1178 		}
1179 		in_multientries++;
1180 	}
1181 	splx(s);
1182 	return (inm);
1183 }
1184 
1185 /*
1186  * Delete a multicast address record.
1187  */
1188 void
1189 in_delmulti(struct in_multi *inm)
1190 {
1191 	struct sockaddr_in sin;
1192 	struct ifreq ifr;
1193 	int s = splsoftnet();
1194 
1195 	if (--inm->inm_refcount == 0) {
1196 		/*
1197 		 * No remaining claims to this record; let IGMP know that
1198 		 * we are leaving the multicast group.
1199 		 */
1200 		igmp_leavegroup(inm);
1201 		/*
1202 		 * Unlink from list.
1203 		 */
1204 		LIST_REMOVE(inm, inm_list);
1205 		in_multientries--;
1206 		/*
1207 		 * Notify the network driver to update its multicast reception
1208 		 * filter.
1209 		 */
1210 		sockaddr_in_init(&sin, &inm->inm_addr, 0);
1211 		ifreq_setaddr(SIOCDELMULTI, &ifr, sintosa(&sin));
1212 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
1213 							     (void *)&ifr);
1214 		pool_put(&inmulti_pool, inm);
1215 	}
1216 	splx(s);
1217 }
1218 #endif /* INET */
1219