Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.121
      1 /*	$NetBSD: in.c,v 1.121 2007/12/05 23:47:18 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.121 2007/12/05 23:47:18 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 			TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
    412 			    ifa_list);
    413 			IFAREF(&ia->ia_ifa);
    414 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    415 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    416 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
    417 #ifdef IPSELSRC
    418 			ia->ia_ifa.ifa_getifa = in_getifa;
    419 #else /* IPSELSRC */
    420 			ia->ia_ifa.ifa_getifa = NULL;
    421 #endif /* IPSELSRC */
    422 			ia->ia_sockmask.sin_len = 8;
    423 			if (ifp->if_flags & IFF_BROADCAST) {
    424 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
    425 				ia->ia_broadaddr.sin_family = AF_INET;
    426 			}
    427 			ia->ia_ifp = ifp;
    428 			LIST_INIT(&ia->ia_multiaddrs);
    429 			newifaddr = 1;
    430 		}
    431 		break;
    432 
    433 	case SIOCSIFBRDADDR:
    434 		if (l == NULL)
    435 			return (EPERM);
    436 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
    437 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    438 		    NULL) != 0)
    439 			return (EPERM);
    440 		/* FALLTHROUGH */
    441 
    442 	case SIOCGIFADDR:
    443 	case SIOCGIFNETMASK:
    444 	case SIOCGIFDSTADDR:
    445 	case SIOCGIFBRDADDR:
    446 		if (ia == 0)
    447 			return (EADDRNOTAVAIL);
    448 		break;
    449 	}
    450 	error = 0;
    451 	switch (cmd) {
    452 
    453 	case SIOCGIFADDR:
    454 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
    455 		break;
    456 
    457 	case SIOCGIFBRDADDR:
    458 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    459 			return (EINVAL);
    460 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
    461 		break;
    462 
    463 	case SIOCGIFDSTADDR:
    464 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    465 			return (EINVAL);
    466 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
    467 		break;
    468 
    469 	case SIOCGIFNETMASK:
    470 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_sockmask));
    471 		break;
    472 
    473 	case SIOCSIFDSTADDR:
    474 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    475 			return (EINVAL);
    476 		oldaddr = ia->ia_dstaddr;
    477 		ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
    478 		if (ifp->if_ioctl != NULL &&
    479 		    (error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
    480 		                              (void *)ia)) != 0) {
    481 			ia->ia_dstaddr = oldaddr;
    482 			return error;
    483 		}
    484 		if (ia->ia_flags & IFA_ROUTE) {
    485 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
    486 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
    487 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    488 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
    489 		}
    490 		break;
    491 
    492 	case SIOCSIFBRDADDR:
    493 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    494 			return EINVAL;
    495 		ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
    496 		break;
    497 
    498 	case SIOCSIFADDR:
    499 		error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
    500 		    1);
    501 #ifdef PFIL_HOOKS
    502 		if (error == 0)
    503 			(void)pfil_run_hooks(&if_pfil,
    504 			    (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
    505 #endif
    506 		break;
    507 
    508 	case SIOCSIFNETMASK:
    509 		in_ifscrub(ifp, ia);
    510 		ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
    511 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    512 		error = in_ifinit(ifp, ia, NULL, 0);
    513 		break;
    514 
    515 	case SIOCAIFADDR:
    516 		maskIsNew = 0;
    517 		hostIsNew = 1;
    518 		if (ia->ia_addr.sin_family != AF_INET)
    519 			;
    520 		else if (ifra->ifra_addr.sin_len == 0) {
    521 			ifra->ifra_addr = ia->ia_addr;
    522 			hostIsNew = 0;
    523 		} else if (in_hosteq(ia->ia_addr.sin_addr,
    524 		           ifra->ifra_addr.sin_addr))
    525 			hostIsNew = 0;
    526 		if (ifra->ifra_mask.sin_len) {
    527 			in_ifscrub(ifp, ia);
    528 			ia->ia_sockmask = ifra->ifra_mask;
    529 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    530 			maskIsNew = 1;
    531 		}
    532 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    533 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
    534 			in_ifscrub(ifp, ia);
    535 			ia->ia_dstaddr = ifra->ifra_dstaddr;
    536 			maskIsNew  = 1; /* We lie; but the effect's the same */
    537 		}
    538 		if (ifra->ifra_addr.sin_family == AF_INET &&
    539 		    (hostIsNew || maskIsNew)) {
    540 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
    541 		}
    542 		if ((ifp->if_flags & IFF_BROADCAST) &&
    543 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
    544 			ia->ia_broadaddr = ifra->ifra_broadaddr;
    545 #ifdef PFIL_HOOKS
    546 		if (error == 0)
    547 			(void)pfil_run_hooks(&if_pfil,
    548 			    (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
    549 #endif
    550 		break;
    551 
    552 	case SIOCGIFALIAS:
    553 		ifra->ifra_mask = ia->ia_sockmask;
    554 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    555 		    (ia->ia_dstaddr.sin_family == AF_INET))
    556 			ifra->ifra_dstaddr = ia->ia_dstaddr;
    557 		else if ((ifp->if_flags & IFF_BROADCAST) &&
    558 		    (ia->ia_broadaddr.sin_family == AF_INET))
    559 			ifra->ifra_broadaddr = ia->ia_broadaddr;
    560 		else
    561 			memset(&ifra->ifra_broadaddr, 0,
    562 			      sizeof(ifra->ifra_broadaddr));
    563 		break;
    564 
    565 	case SIOCDIFADDR:
    566 		in_purgeaddr(&ia->ia_ifa);
    567 #ifdef PFIL_HOOKS
    568 		(void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR,
    569 		    ifp, PFIL_IFADDR);
    570 #endif
    571 		break;
    572 
    573 #ifdef MROUTING
    574 	case SIOCGETVIFCNT:
    575 	case SIOCGETSGCNT:
    576 		error = mrt_ioctl(so, cmd, data);
    577 		break;
    578 #endif /* MROUTING */
    579 
    580 	default:
    581 		if (ifp == NULL || ifp->if_ioctl == NULL)
    582 			return EOPNOTSUPP;
    583 		error = (*ifp->if_ioctl)(ifp, cmd, data);
    584 		in_setmaxmtu();
    585 		break;
    586 	}
    587 
    588 	if (error != 0 && newifaddr) {
    589 		KASSERT(ia != NULL);
    590 		in_purgeaddr(&ia->ia_ifa);
    591 	}
    592 
    593 	return error;
    594 }
    595 
    596 void
    597 in_purgeaddr(struct ifaddr *ifa)
    598 {
    599 	struct ifnet *ifp = ifa->ifa_ifp;
    600 	struct in_ifaddr *ia = (void *) ifa;
    601 
    602 	in_ifscrub(ifp, ia);
    603 	LIST_REMOVE(ia, ia_hash);
    604 	TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
    605 	IFAFREE(&ia->ia_ifa);
    606 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
    607 	if (ia->ia_allhosts != NULL)
    608 		in_delmulti(ia->ia_allhosts);
    609 	IFAFREE(&ia->ia_ifa);
    610 	in_setmaxmtu();
    611 }
    612 
    613 void
    614 in_purgeif(struct ifnet *ifp)		/* MUST be called at splsoftnet() */
    615 {
    616 	if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
    617 	igmp_purgeif(ifp);		/* manipulates pools */
    618 #ifdef MROUTING
    619 	ip_mrouter_detach(ifp);
    620 #endif
    621 }
    622 
    623 /*
    624  * SIOC[GAD]LIFADDR.
    625  *	SIOCGLIFADDR: get first address. (???)
    626  *	SIOCGLIFADDR with IFLR_PREFIX:
    627  *		get first address that matches the specified prefix.
    628  *	SIOCALIFADDR: add the specified address.
    629  *	SIOCALIFADDR with IFLR_PREFIX:
    630  *		EINVAL since we can't deduce hostid part of the address.
    631  *	SIOCDLIFADDR: delete the specified address.
    632  *	SIOCDLIFADDR with IFLR_PREFIX:
    633  *		delete the first address that matches the specified prefix.
    634  * return values:
    635  *	EINVAL on invalid parameters
    636  *	EADDRNOTAVAIL on prefix match failed/specified address not found
    637  *	other values may be returned from in_ioctl()
    638  */
    639 static int
    640 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
    641     struct ifnet *ifp, struct lwp *l)
    642 {
    643 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
    644 	struct ifaddr *ifa;
    645 	struct sockaddr *sa;
    646 
    647 	/* sanity checks */
    648 	if (data == NULL || ifp == NULL) {
    649 		panic("invalid argument to in_lifaddr_ioctl");
    650 		/*NOTRECHED*/
    651 	}
    652 
    653 	switch (cmd) {
    654 	case SIOCGLIFADDR:
    655 		/* address must be specified on GET with IFLR_PREFIX */
    656 		if ((iflr->flags & IFLR_PREFIX) == 0)
    657 			break;
    658 		/*FALLTHROUGH*/
    659 	case SIOCALIFADDR:
    660 	case SIOCDLIFADDR:
    661 		/* address must be specified on ADD and DELETE */
    662 		sa = (struct sockaddr *)&iflr->addr;
    663 		if (sa->sa_family != AF_INET)
    664 			return EINVAL;
    665 		if (sa->sa_len != sizeof(struct sockaddr_in))
    666 			return EINVAL;
    667 		/* XXX need improvement */
    668 		sa = (struct sockaddr *)&iflr->dstaddr;
    669 		if (sa->sa_family
    670 		 && sa->sa_family != AF_INET)
    671 			return EINVAL;
    672 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in))
    673 			return EINVAL;
    674 		break;
    675 	default: /*shouldn't happen*/
    676 #if 0
    677 		panic("invalid cmd to in_lifaddr_ioctl");
    678 		/*NOTREACHED*/
    679 #else
    680 		return EOPNOTSUPP;
    681 #endif
    682 	}
    683 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
    684 		return EINVAL;
    685 
    686 	switch (cmd) {
    687 	case SIOCALIFADDR:
    688 	    {
    689 		struct in_aliasreq ifra;
    690 
    691 		if (iflr->flags & IFLR_PREFIX)
    692 			return EINVAL;
    693 
    694 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
    695 		bzero(&ifra, sizeof(ifra));
    696 		bcopy(iflr->iflr_name, ifra.ifra_name,
    697 			sizeof(ifra.ifra_name));
    698 
    699 		bcopy(&iflr->addr, &ifra.ifra_addr,
    700 			((struct sockaddr *)&iflr->addr)->sa_len);
    701 
    702 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
    703 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
    704 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
    705 		}
    706 
    707 		ifra.ifra_mask.sin_family = AF_INET;
    708 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
    709 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
    710 
    711 		return in_control(so, SIOCAIFADDR, (void *)&ifra, ifp, l);
    712 	    }
    713 	case SIOCGLIFADDR:
    714 	case SIOCDLIFADDR:
    715 	    {
    716 		struct in_ifaddr *ia;
    717 		struct in_addr mask, candidate, match;
    718 		struct sockaddr_in *sin;
    719 		int cmp;
    720 
    721 		bzero(&mask, sizeof(mask));
    722 		bzero(&match, sizeof(match));	/* XXX gcc */
    723 		if (iflr->flags & IFLR_PREFIX) {
    724 			/* lookup a prefix rather than address. */
    725 			in_len2mask(&mask, iflr->prefixlen);
    726 
    727 			sin = (struct sockaddr_in *)&iflr->addr;
    728 			match.s_addr = sin->sin_addr.s_addr;
    729 			match.s_addr &= mask.s_addr;
    730 
    731 			/* if you set extra bits, that's wrong */
    732 			if (match.s_addr != sin->sin_addr.s_addr)
    733 				return EINVAL;
    734 
    735 			cmp = 1;
    736 		} else {
    737 			if (cmd == SIOCGLIFADDR) {
    738 				/* on getting an address, take the 1st match */
    739 				cmp = 0;	/*XXX*/
    740 			} else {
    741 				/* on deleting an address, do exact match */
    742 				in_len2mask(&mask, 32);
    743 				sin = (struct sockaddr_in *)&iflr->addr;
    744 				match.s_addr = sin->sin_addr.s_addr;
    745 
    746 				cmp = 1;
    747 			}
    748 		}
    749 
    750 		IFADDR_FOREACH(ifa, ifp) {
    751 			if (ifa->ifa_addr->sa_family != AF_INET)
    752 				continue;
    753 			if (cmp == 0)
    754 				break;
    755 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
    756 			candidate.s_addr &= mask.s_addr;
    757 			if (candidate.s_addr == match.s_addr)
    758 				break;
    759 		}
    760 		if (ifa == NULL)
    761 			return EADDRNOTAVAIL;
    762 		ia = (struct in_ifaddr *)ifa;
    763 
    764 		if (cmd == SIOCGLIFADDR) {
    765 			/* fill in the if_laddrreq structure */
    766 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
    767 
    768 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
    769 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
    770 					ia->ia_dstaddr.sin_len);
    771 			} else
    772 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
    773 
    774 			iflr->prefixlen =
    775 				in_mask2len(&ia->ia_sockmask.sin_addr);
    776 
    777 			iflr->flags = 0;	/*XXX*/
    778 
    779 			return 0;
    780 		} else {
    781 			struct in_aliasreq ifra;
    782 
    783 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
    784 			bzero(&ifra, sizeof(ifra));
    785 			bcopy(iflr->iflr_name, ifra.ifra_name,
    786 				sizeof(ifra.ifra_name));
    787 
    788 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
    789 				ia->ia_addr.sin_len);
    790 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
    791 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
    792 					ia->ia_dstaddr.sin_len);
    793 			}
    794 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
    795 				ia->ia_sockmask.sin_len);
    796 
    797 			return in_control(so, SIOCDIFADDR, (void *)&ifra,
    798 				ifp, l);
    799 		}
    800 	    }
    801 	}
    802 
    803 	return EOPNOTSUPP;	/*just for safety*/
    804 }
    805 
    806 static int
    807 in_ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data,
    808     struct ifnet *ifp)
    809 {
    810 	struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
    811 	struct ifaddr *ifa;
    812 	struct sockaddr *sa;
    813 	struct in_ifaddr *ia = NULL; /* appease gcc -Wuninitialized */
    814 	struct in_addr match;
    815 	struct sockaddr_in *sin;
    816 
    817 	/* sanity checks */
    818 	if (data == NULL || ifp == NULL) {
    819 		panic("invalid argument to %s", __func__);
    820 		/*NOTREACHED*/
    821 	}
    822 
    823 	/* address must be specified on ADD and DELETE */
    824 	sa = (struct sockaddr *)&ifap->ifap_addr;
    825 	if (sa->sa_family != AF_INET)
    826 		return EINVAL;
    827 	if (sa->sa_len != sizeof(struct sockaddr_in))
    828 		return EINVAL;
    829 
    830 	switch (cmd) {
    831 	case SIOCSIFADDRPREF:
    832 	case SIOCGIFADDRPREF:
    833 		break;
    834 	default:
    835 		return EOPNOTSUPP;
    836 	}
    837 
    838 	sin = (struct sockaddr_in *)&ifap->ifap_addr;
    839 	match.s_addr = sin->sin_addr.s_addr;
    840 
    841 	IFADDR_FOREACH(ifa, ifp) {
    842 		ia = (struct in_ifaddr *)ifa;
    843 		if (ia->ia_addr.sin_family != AF_INET)
    844 			continue;
    845 		if (ia->ia_addr.sin_addr.s_addr == match.s_addr)
    846 			break;
    847 	}
    848 	if (ifa == NULL)
    849 		return EADDRNOTAVAIL;
    850 
    851 	switch (cmd) {
    852 	case SIOCSIFADDRPREF:
    853 		ifa->ifa_preference = ifap->ifap_preference;
    854 		return 0;
    855 	case SIOCGIFADDRPREF:
    856 		/* fill in the if_laddrreq structure */
    857 		(void)memcpy(&ifap->ifap_addr, &ia->ia_addr,
    858 		    ia->ia_addr.sin_len);
    859 		ifap->ifap_preference = ifa->ifa_preference;
    860 		return 0;
    861 	default:
    862 		return EOPNOTSUPP;
    863 	}
    864 }
    865 
    866 /*
    867  * Delete any existing route for an interface.
    868  */
    869 void
    870 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
    871 {
    872 
    873 	in_scrubprefix(ia);
    874 }
    875 
    876 /*
    877  * Initialize an interface's internet address
    878  * and routing table entry.
    879  */
    880 int
    881 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
    882     const struct sockaddr_in *sin, int scrub)
    883 {
    884 	u_int32_t i;
    885 	struct sockaddr_in oldaddr;
    886 	int s = splnet(), flags = RTF_UP, error;
    887 
    888 	if (sin == NULL)
    889 		sin = &ia->ia_addr;
    890 
    891 	/*
    892 	 * Set up new addresses.
    893 	 */
    894 	oldaddr = ia->ia_addr;
    895 	if (ia->ia_addr.sin_family == AF_INET)
    896 		LIST_REMOVE(ia, ia_hash);
    897 	ia->ia_addr = *sin;
    898 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
    899 
    900 	/*
    901 	 * Give the interface a chance to initialize
    902 	 * if this is its first address,
    903 	 * and to validate the address if necessary.
    904 	 */
    905 	if (ifp->if_ioctl &&
    906 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (void *)ia)))
    907 		goto bad;
    908 	splx(s);
    909 	if (scrub) {
    910 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
    911 		in_ifscrub(ifp, ia);
    912 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    913 	}
    914 
    915 	i = ia->ia_addr.sin_addr.s_addr;
    916 	if (IN_CLASSA(i))
    917 		ia->ia_netmask = IN_CLASSA_NET;
    918 	else if (IN_CLASSB(i))
    919 		ia->ia_netmask = IN_CLASSB_NET;
    920 	else
    921 		ia->ia_netmask = IN_CLASSC_NET;
    922 	/*
    923 	 * The subnet mask usually includes at least the standard network part,
    924 	 * but may may be smaller in the case of supernetting.
    925 	 * If it is set, we believe it.
    926 	 */
    927 	if (ia->ia_subnetmask == 0) {
    928 		ia->ia_subnetmask = ia->ia_netmask;
    929 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
    930 	} else
    931 		ia->ia_netmask &= ia->ia_subnetmask;
    932 
    933 	ia->ia_net = i & ia->ia_netmask;
    934 	ia->ia_subnet = i & ia->ia_subnetmask;
    935 	in_socktrim(&ia->ia_sockmask);
    936 	/* re-calculate the "in_maxmtu" value */
    937 	in_setmaxmtu();
    938 	/*
    939 	 * Add route for the network.
    940 	 */
    941 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    942 	if (ifp->if_flags & IFF_BROADCAST) {
    943 		ia->ia_broadaddr.sin_addr.s_addr =
    944 			ia->ia_subnet | ~ia->ia_subnetmask;
    945 		ia->ia_netbroadcast.s_addr =
    946 			ia->ia_net | ~ia->ia_netmask;
    947 	} else if (ifp->if_flags & IFF_LOOPBACK) {
    948 		ia->ia_dstaddr = ia->ia_addr;
    949 		flags |= RTF_HOST;
    950 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
    951 		if (ia->ia_dstaddr.sin_family != AF_INET)
    952 			return (0);
    953 		flags |= RTF_HOST;
    954 	}
    955 	error = in_addprefix(ia, flags);
    956 	/*
    957 	 * If the interface supports multicast, join the "all hosts"
    958 	 * multicast group on that interface.
    959 	 */
    960 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
    961 		struct in_addr addr;
    962 
    963 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
    964 		ia->ia_allhosts = in_addmulti(&addr, ifp);
    965 	}
    966 	return (error);
    967 bad:
    968 	splx(s);
    969 	LIST_REMOVE(ia, ia_hash);
    970 	ia->ia_addr = oldaddr;
    971 	if (ia->ia_addr.sin_family == AF_INET)
    972 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
    973 		    ia, ia_hash);
    974 	return (error);
    975 }
    976 
    977 #define rtinitflags(x) \
    978 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
    979 	    ? RTF_HOST : 0)
    980 
    981 /*
    982  * add a route to prefix ("connected route" in cisco terminology).
    983  * does nothing if there's some interface address with the same prefix already.
    984  */
    985 static int
    986 in_addprefix(struct in_ifaddr *target, int flags)
    987 {
    988 	struct in_ifaddr *ia;
    989 	struct in_addr prefix, mask, p;
    990 	int error;
    991 
    992 	if ((flags & RTF_HOST) != 0)
    993 		prefix = target->ia_dstaddr.sin_addr;
    994 	else {
    995 		prefix = target->ia_addr.sin_addr;
    996 		mask = target->ia_sockmask.sin_addr;
    997 		prefix.s_addr &= mask.s_addr;
    998 	}
    999 
   1000 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
   1001 		if (rtinitflags(ia))
   1002 			p = ia->ia_dstaddr.sin_addr;
   1003 		else {
   1004 			p = ia->ia_addr.sin_addr;
   1005 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
   1006 		}
   1007 
   1008 		if (prefix.s_addr != p.s_addr)
   1009 			continue;
   1010 
   1011 		/*
   1012 		 * if we got a matching prefix route inserted by other
   1013 		 * interface address, we don't need to bother
   1014 		 *
   1015 		 * XXX RADIX_MPATH implications here? -dyoung
   1016 		 */
   1017 		if (ia->ia_flags & IFA_ROUTE)
   1018 			return 0;
   1019 	}
   1020 
   1021 	/*
   1022 	 * noone seem to have prefix route.  insert it.
   1023 	 */
   1024 	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
   1025 	if (error == 0)
   1026 		target->ia_flags |= IFA_ROUTE;
   1027 	return error;
   1028 }
   1029 
   1030 /*
   1031  * remove a route to prefix ("connected route" in cisco terminology).
   1032  * re-installs the route by using another interface address, if there's one
   1033  * with the same prefix (otherwise we lose the route mistakenly).
   1034  */
   1035 static int
   1036 in_scrubprefix(struct in_ifaddr *target)
   1037 {
   1038 	struct in_ifaddr *ia;
   1039 	struct in_addr prefix, mask, p;
   1040 	int error;
   1041 
   1042 	if ((target->ia_flags & IFA_ROUTE) == 0)
   1043 		return 0;
   1044 
   1045 	if (rtinitflags(target))
   1046 		prefix = target->ia_dstaddr.sin_addr;
   1047 	else {
   1048 		prefix = target->ia_addr.sin_addr;
   1049 		mask = target->ia_sockmask.sin_addr;
   1050 		prefix.s_addr &= mask.s_addr;
   1051 	}
   1052 
   1053 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
   1054 		if (rtinitflags(ia))
   1055 			p = ia->ia_dstaddr.sin_addr;
   1056 		else {
   1057 			p = ia->ia_addr.sin_addr;
   1058 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
   1059 		}
   1060 
   1061 		if (prefix.s_addr != p.s_addr)
   1062 			continue;
   1063 
   1064 		/*
   1065 		 * if we got a matching prefix route, move IFA_ROUTE to him
   1066 		 */
   1067 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
   1068 			rtinit(&target->ia_ifa, RTM_DELETE,
   1069 			    rtinitflags(target));
   1070 			target->ia_flags &= ~IFA_ROUTE;
   1071 
   1072 			error = rtinit(&ia->ia_ifa, RTM_ADD,
   1073 			    rtinitflags(ia) | RTF_UP);
   1074 			if (error == 0)
   1075 				ia->ia_flags |= IFA_ROUTE;
   1076 			return error;
   1077 		}
   1078 	}
   1079 
   1080 	/*
   1081 	 * noone seem to have prefix route.  remove it.
   1082 	 */
   1083 	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
   1084 	target->ia_flags &= ~IFA_ROUTE;
   1085 	return 0;
   1086 }
   1087 
   1088 #undef rtinitflags
   1089 
   1090 /*
   1091  * Return 1 if the address might be a local broadcast address.
   1092  */
   1093 int
   1094 in_broadcast(struct in_addr in, struct ifnet *ifp)
   1095 {
   1096 	struct ifaddr *ifa;
   1097 
   1098 	if (in.s_addr == INADDR_BROADCAST ||
   1099 	    in_nullhost(in))
   1100 		return 1;
   1101 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
   1102 		return 0;
   1103 	/*
   1104 	 * Look through the list of addresses for a match
   1105 	 * with a broadcast address.
   1106 	 */
   1107 #define ia (ifatoia(ifa))
   1108 	IFADDR_FOREACH(ifa, ifp)
   1109 		if (ifa->ifa_addr->sa_family == AF_INET &&
   1110 		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
   1111 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
   1112 		     in_hosteq(in, ia->ia_netbroadcast) ||
   1113 		     (hostzeroisbroadcast &&
   1114 		      /*
   1115 		       * Check for old-style (host 0) broadcast.
   1116 		       */
   1117 		      (in.s_addr == ia->ia_subnet ||
   1118 		       in.s_addr == ia->ia_net))))
   1119 			return 1;
   1120 	return (0);
   1121 #undef ia
   1122 }
   1123 
   1124 /*
   1125  * Add an address to the list of IP multicast addresses for a given interface.
   1126  */
   1127 struct in_multi *
   1128 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
   1129 {
   1130 	struct sockaddr_in sin;
   1131 	struct in_multi *inm;
   1132 	struct ifreq ifr;
   1133 	int s = splsoftnet();
   1134 
   1135 	/*
   1136 	 * See if address already in list.
   1137 	 */
   1138 	IN_LOOKUP_MULTI(*ap, ifp, inm);
   1139 	if (inm != NULL) {
   1140 		/*
   1141 		 * Found it; just increment the reference count.
   1142 		 */
   1143 		++inm->inm_refcount;
   1144 	} else {
   1145 		/*
   1146 		 * New address; allocate a new multicast record
   1147 		 * and link it into the interface's multicast list.
   1148 		 */
   1149 		inm = pool_get(&inmulti_pool, PR_NOWAIT);
   1150 		if (inm == NULL) {
   1151 			splx(s);
   1152 			return (NULL);
   1153 		}
   1154 		inm->inm_addr = *ap;
   1155 		inm->inm_ifp = ifp;
   1156 		inm->inm_refcount = 1;
   1157 		LIST_INSERT_HEAD(
   1158 		    &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
   1159 		    inm, inm_list);
   1160 		/*
   1161 		 * Ask the network driver to update its multicast reception
   1162 		 * filter appropriately for the new address.
   1163 		 */
   1164 		sockaddr_in_init(&sin, ap, 0);
   1165 		ifreq_setaddr(SIOCADDMULTI, &ifr, sintosa(&sin));
   1166 		if ((ifp->if_ioctl == NULL) ||
   1167 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(void *)&ifr) != 0) {
   1168 			LIST_REMOVE(inm, inm_list);
   1169 			pool_put(&inmulti_pool, inm);
   1170 			splx(s);
   1171 			return (NULL);
   1172 		}
   1173 		/*
   1174 		 * Let IGMP know that we have joined a new IP multicast group.
   1175 		 */
   1176 		if (igmp_joingroup(inm) != 0) {
   1177 			LIST_REMOVE(inm, inm_list);
   1178 			pool_put(&inmulti_pool, inm);
   1179 			splx(s);
   1180 			return (NULL);
   1181 		}
   1182 		in_multientries++;
   1183 	}
   1184 	splx(s);
   1185 	return (inm);
   1186 }
   1187 
   1188 /*
   1189  * Delete a multicast address record.
   1190  */
   1191 void
   1192 in_delmulti(struct in_multi *inm)
   1193 {
   1194 	struct sockaddr_in sin;
   1195 	struct ifreq ifr;
   1196 	int s = splsoftnet();
   1197 
   1198 	if (--inm->inm_refcount == 0) {
   1199 		/*
   1200 		 * No remaining claims to this record; let IGMP know that
   1201 		 * we are leaving the multicast group.
   1202 		 */
   1203 		igmp_leavegroup(inm);
   1204 		/*
   1205 		 * Unlink from list.
   1206 		 */
   1207 		LIST_REMOVE(inm, inm_list);
   1208 		in_multientries--;
   1209 		/*
   1210 		 * Notify the network driver to update its multicast reception
   1211 		 * filter.
   1212 		 */
   1213 		sockaddr_in_init(&sin, &inm->inm_addr, 0);
   1214 		ifreq_setaddr(SIOCDELMULTI, &ifr, sintosa(&sin));
   1215 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
   1216 							     (void *)&ifr);
   1217 		pool_put(&inmulti_pool, inm);
   1218 	}
   1219 	splx(s);
   1220 }
   1221 #endif /* INET */
   1222