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