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