Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.39
      1 /*	$NetBSD: in.c,v 1.39 1998/02/15 18:24:24 tls Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Public Access Networks Corporation ("Panix").  It was developed under
      9  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)in.c	8.4 (Berkeley) 1/9/95
     73  */
     74 
     75 #include "opt_mrouting.h"
     76 
     77 #include <sys/param.h>
     78 #include <sys/ioctl.h>
     79 #include <sys/errno.h>
     80 #include <sys/malloc.h>
     81 #include <sys/socket.h>
     82 #include <sys/socketvar.h>
     83 #include <sys/systm.h>
     84 #include <sys/proc.h>
     85 
     86 #include <net/if.h>
     87 #include <net/route.h>
     88 
     89 #include <net/if_ether.h>
     90 
     91 #include <netinet/in_systm.h>
     92 #include <netinet/in.h>
     93 #include <netinet/in_var.h>
     94 #include <netinet/if_inarp.h>
     95 #include <netinet/ip_mroute.h>
     96 #include <netinet/igmp_var.h>
     97 
     98 #include "ether.h"
     99 
    100 #ifdef INET
    101 
    102 #ifndef SUBNETSARELOCAL
    103 #define	SUBNETSARELOCAL	1
    104 #endif
    105 int subnetsarelocal = SUBNETSARELOCAL;
    106 
    107 /*
    108  * Return 1 if an internet address is for a ``local'' host
    109  * (one to which we have a connection).  If subnetsarelocal
    110  * is true, this includes other subnets of the local net.
    111  * Otherwise, it includes only the directly-connected (sub)nets.
    112  */
    113 int
    114 in_localaddr(in)
    115 	struct in_addr in;
    116 {
    117 	register struct in_ifaddr *ia;
    118 
    119 	if (subnetsarelocal) {
    120 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
    121 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
    122 				return (1);
    123 	} else {
    124 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
    125 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
    126 				return (1);
    127 	}
    128 	return (0);
    129 }
    130 
    131 /*
    132  * Determine whether an IP address is in a reserved set of addresses
    133  * that may not be forwarded, or whether datagrams to that destination
    134  * may be forwarded.
    135  */
    136 int
    137 in_canforward(in)
    138 	struct in_addr in;
    139 {
    140 	register u_int32_t net;
    141 
    142 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
    143 		return (0);
    144 	if (IN_CLASSA(in.s_addr)) {
    145 		net = in.s_addr & IN_CLASSA_NET;
    146 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
    147 			return (0);
    148 	}
    149 	return (1);
    150 }
    151 
    152 /*
    153  * Trim a mask in a sockaddr
    154  */
    155 void
    156 in_socktrim(ap)
    157 	struct sockaddr_in *ap;
    158 {
    159 	register char *cplim = (char *) &ap->sin_addr;
    160 	register char *cp = (char *) (&ap->sin_addr + 1);
    161 
    162 	ap->sin_len = 0;
    163 	while (--cp >= cplim)
    164 		if (*cp) {
    165 			(ap)->sin_len = cp - (char *) (ap) + 1;
    166 			break;
    167 		}
    168 }
    169 
    170 /*
    171  * Maintain the "in_maxmtu" variable, which is the largest
    172  * mtu for non-local interfaces with AF_INET addresses assigned
    173  * to them that are up.
    174  */
    175 unsigned long in_maxmtu;
    176 
    177 void
    178 in_setmaxmtu()
    179 {
    180 	register struct in_ifaddr *ia;
    181 	register struct ifnet *ifp;
    182 	unsigned long maxmtu = 0;
    183 
    184 	for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) {
    185 		if ((ifp = ia->ia_ifp) == 0)
    186 			continue;
    187 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
    188 			continue;
    189 		if (ifp->if_mtu > maxmtu)
    190 			maxmtu = ifp->if_mtu;
    191 	}
    192 	if (maxmtu)
    193 		in_maxmtu = maxmtu;
    194 }
    195 
    196 int	in_interfaces;		/* number of external internet interfaces */
    197 
    198 /*
    199  * Generic internet control operations (ioctl's).
    200  * Ifp is 0 if not an interface-specific ioctl.
    201  */
    202 /* ARGSUSED */
    203 int
    204 in_control(so, cmd, data, ifp, p)
    205 	struct socket *so;
    206 	u_long cmd;
    207 	caddr_t data;
    208 	register struct ifnet *ifp;
    209 	struct proc *p;
    210 {
    211 	register struct ifreq *ifr = (struct ifreq *)data;
    212 	register struct in_ifaddr *ia = 0;
    213 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
    214 	struct sockaddr_in oldaddr;
    215 	int error, hostIsNew, maskIsNew;
    216 
    217 	/*
    218 	 * Find address for this interface, if it exists.
    219 	 */
    220 	if (ifp)
    221 		IFP_TO_IA(ifp, ia);
    222 
    223 	switch (cmd) {
    224 
    225 	case SIOCAIFADDR:
    226 	case SIOCDIFADDR:
    227 		if (ifra->ifra_addr.sin_family == AF_INET)
    228 			for (ia = IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr).lh_first;
    229 			    ia != 0; ia = ia->ia_hash.le_next) {
    230 				if (ia->ia_ifp == ifp  &&
    231 				    in_hosteq(ia->ia_addr.sin_addr,
    232 				    ifra->ifra_addr.sin_addr))
    233 					break;
    234 			}
    235 		if (cmd == SIOCDIFADDR && ia == 0)
    236 			return (EADDRNOTAVAIL);
    237 		/* FALLTHROUGH */
    238 	case SIOCSIFADDR:
    239 	case SIOCSIFNETMASK:
    240 	case SIOCSIFDSTADDR:
    241 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    242 			return (EPERM);
    243 
    244 		if (ifp == 0)
    245 			panic("in_control");
    246 		if (ia == 0) {
    247 			MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
    248 			       M_IFADDR, M_WAITOK);
    249 			if (ia == 0)
    250 				return (ENOBUFS);
    251 			bzero((caddr_t)ia, sizeof *ia);
    252 			TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list);
    253 			TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia,
    254 			    ifa_list);
    255 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    256 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    257 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
    258 			ia->ia_sockmask.sin_len = 8;
    259 			if (ifp->if_flags & IFF_BROADCAST) {
    260 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
    261 				ia->ia_broadaddr.sin_family = AF_INET;
    262 			}
    263 			ia->ia_ifp = ifp;
    264 			LIST_INIT(&ia->ia_multiaddrs);
    265 			if ((ifp->if_flags & IFF_LOOPBACK) == 0)
    266 				in_interfaces++;
    267 		}
    268 		break;
    269 
    270 	case SIOCSIFBRDADDR:
    271 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    272 			return (EPERM);
    273 		/* FALLTHROUGH */
    274 
    275 	case SIOCGIFADDR:
    276 	case SIOCGIFNETMASK:
    277 	case SIOCGIFDSTADDR:
    278 	case SIOCGIFBRDADDR:
    279 		if (ia == 0)
    280 			return (EADDRNOTAVAIL);
    281 		break;
    282 	}
    283 	switch (cmd) {
    284 
    285 	case SIOCGIFADDR:
    286 		*satosin(&ifr->ifr_addr) = ia->ia_addr;
    287 		break;
    288 
    289 	case SIOCGIFBRDADDR:
    290 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    291 			return (EINVAL);
    292 		*satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
    293 		break;
    294 
    295 	case SIOCGIFDSTADDR:
    296 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    297 			return (EINVAL);
    298 		*satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
    299 		break;
    300 
    301 	case SIOCGIFNETMASK:
    302 		*satosin(&ifr->ifr_addr) = ia->ia_sockmask;
    303 		break;
    304 
    305 	case SIOCSIFDSTADDR:
    306 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    307 			return (EINVAL);
    308 		oldaddr = ia->ia_dstaddr;
    309 		ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
    310 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
    311 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
    312 			ia->ia_dstaddr = oldaddr;
    313 			return (error);
    314 		}
    315 		if (ia->ia_flags & IFA_ROUTE) {
    316 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
    317 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    318 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    319 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
    320 		}
    321 		break;
    322 
    323 	case SIOCSIFBRDADDR:
    324 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    325 			return (EINVAL);
    326 		ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
    327 		break;
    328 
    329 	case SIOCSIFADDR:
    330 		return (in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1));
    331 
    332 	case SIOCSIFNETMASK:
    333 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr =
    334 		    ifra->ifra_addr.sin_addr.s_addr;
    335 		break;
    336 
    337 	case SIOCAIFADDR:
    338 		maskIsNew = 0;
    339 		hostIsNew = 1;
    340 		error = 0;
    341 		if (ia->ia_addr.sin_family == AF_INET) {
    342 			if (ifra->ifra_addr.sin_len == 0) {
    343 				ifra->ifra_addr = ia->ia_addr;
    344 				hostIsNew = 0;
    345 			} else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr))
    346 				hostIsNew = 0;
    347 		}
    348 		if (ifra->ifra_mask.sin_len) {
    349 			in_ifscrub(ifp, ia);
    350 			ia->ia_sockmask = ifra->ifra_mask;
    351 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    352 			maskIsNew = 1;
    353 		}
    354 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    355 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
    356 			in_ifscrub(ifp, ia);
    357 			ia->ia_dstaddr = ifra->ifra_dstaddr;
    358 			maskIsNew  = 1; /* We lie; but the effect's the same */
    359 		}
    360 		if (ifra->ifra_addr.sin_family == AF_INET &&
    361 		    (hostIsNew || maskIsNew))
    362 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
    363 		if ((ifp->if_flags & IFF_BROADCAST) &&
    364 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
    365 			ia->ia_broadaddr = ifra->ifra_broadaddr;
    366 		return (error);
    367 
    368 	case SIOCDIFADDR:
    369 		in_ifscrub(ifp, ia);
    370 		LIST_REMOVE(ia, ia_hash);
    371 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    372 		TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
    373 		IFAFREE((&ia->ia_ifa));
    374 		in_setmaxmtu();
    375 		break;
    376 
    377 #ifdef MROUTING
    378 	case SIOCGETVIFCNT:
    379 	case SIOCGETSGCNT:
    380 		return (mrt_ioctl(so, cmd, data));
    381 #endif /* MROUTING */
    382 
    383 	default:
    384 		if (ifp == 0 || ifp->if_ioctl == 0)
    385 			return (EOPNOTSUPP);
    386 		error = (*ifp->if_ioctl)(ifp, cmd, data);
    387 		in_setmaxmtu();
    388 		return(error);
    389 	}
    390 	return (0);
    391 }
    392 
    393 /*
    394  * Delete any existing route for an interface.
    395  */
    396 void
    397 in_ifscrub(ifp, ia)
    398 	register struct ifnet *ifp;
    399 	register struct in_ifaddr *ia;
    400 {
    401 
    402 	if ((ia->ia_flags & IFA_ROUTE) == 0)
    403 		return;
    404 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
    405 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    406 	else
    407 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
    408 	ia->ia_flags &= ~IFA_ROUTE;
    409 }
    410 
    411 /*
    412  * Initialize an interface's internet address
    413  * and routing table entry.
    414  */
    415 int
    416 in_ifinit(ifp, ia, sin, scrub)
    417 	register struct ifnet *ifp;
    418 	register struct in_ifaddr *ia;
    419 	struct sockaddr_in *sin;
    420 	int scrub;
    421 {
    422 	register u_int32_t i = sin->sin_addr.s_addr;
    423 	struct sockaddr_in oldaddr;
    424 	int s = splimp(), flags = RTF_UP, error;
    425 
    426 	/*
    427 	 * Set up new addresses.
    428 	 */
    429 	oldaddr = ia->ia_addr;
    430 	if (ia->ia_addr.sin_family == AF_INET)
    431 		LIST_REMOVE(ia, ia_hash);
    432 	ia->ia_addr = *sin;
    433 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
    434 
    435 	/*
    436 	 * Give the interface a chance to initialize
    437 	 * if this is its first address,
    438 	 * and to validate the address if necessary.
    439 	 */
    440 	if (ifp->if_ioctl &&
    441 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
    442 		goto bad;
    443 	splx(s);
    444 	if (scrub) {
    445 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
    446 		in_ifscrub(ifp, ia);
    447 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    448 	}
    449 
    450 	if (IN_CLASSA(i))
    451 		ia->ia_netmask = IN_CLASSA_NET;
    452 	else if (IN_CLASSB(i))
    453 		ia->ia_netmask = IN_CLASSB_NET;
    454 	else
    455 		ia->ia_netmask = IN_CLASSC_NET;
    456 	/*
    457 	 * The subnet mask usually includes at least the standard network part,
    458 	 * but may may be smaller in the case of supernetting.
    459 	 * If it is set, we believe it.
    460 	 */
    461 	if (ia->ia_subnetmask == 0) {
    462 		ia->ia_subnetmask = ia->ia_netmask;
    463 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
    464 	} else
    465 		ia->ia_netmask &= ia->ia_subnetmask;
    466 
    467 	ia->ia_net = i & ia->ia_netmask;
    468 	ia->ia_subnet = i & ia->ia_subnetmask;
    469 	in_socktrim(&ia->ia_sockmask);
    470 	/* re-calculate the "in_maxmtu" value */
    471 	in_setmaxmtu();
    472 	/*
    473 	 * Add route for the network.
    474 	 */
    475 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    476 	if (ifp->if_flags & IFF_BROADCAST) {
    477 		ia->ia_broadaddr.sin_addr.s_addr =
    478 			ia->ia_subnet | ~ia->ia_subnetmask;
    479 		ia->ia_netbroadcast.s_addr =
    480 			ia->ia_net | ~ia->ia_netmask;
    481 	} else if (ifp->if_flags & IFF_LOOPBACK) {
    482 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
    483 		flags |= RTF_HOST;
    484 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
    485 		if (ia->ia_dstaddr.sin_family != AF_INET)
    486 			return (0);
    487 		flags |= RTF_HOST;
    488 	}
    489 	error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags);
    490 	if (!error)
    491 		ia->ia_flags |= IFA_ROUTE;
    492 	/*
    493 	 * If the interface supports multicast, join the "all hosts"
    494 	 * multicast group on that interface.
    495 	 */
    496 	if (ifp->if_flags & IFF_MULTICAST) {
    497 		struct in_addr addr;
    498 
    499 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
    500 		in_addmulti(&addr, ifp);
    501 	}
    502 	return (error);
    503 bad:
    504 	splx(s);
    505 	LIST_REMOVE(ia, ia_hash);
    506 	ia->ia_addr = oldaddr;
    507 	if (ia->ia_addr.sin_family == AF_INET)
    508 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
    509 		    ia, ia_hash);
    510 	return (error);
    511 }
    512 
    513 /*
    514  * Return 1 if the address might be a local broadcast address.
    515  */
    516 int
    517 in_broadcast(in, ifp)
    518 	struct in_addr in;
    519 	struct ifnet *ifp;
    520 {
    521 	register struct ifaddr *ifa;
    522 
    523 	if (in.s_addr == INADDR_BROADCAST ||
    524 	    in_nullhost(in))
    525 		return 1;
    526 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
    527 		return 0;
    528 	/*
    529 	 * Look through the list of addresses for a match
    530 	 * with a broadcast address.
    531 	 */
    532 #define ia (ifatoia(ifa))
    533 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
    534 		if (ifa->ifa_addr->sa_family == AF_INET &&
    535 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
    536 		     in_hosteq(in, ia->ia_netbroadcast) ||
    537 		     /*
    538 		      * Check for old-style (host 0) broadcast.
    539 		      */
    540 		     in.s_addr == ia->ia_subnet ||
    541 		     in.s_addr == ia->ia_net))
    542 			    return 1;
    543 	return (0);
    544 #undef ia
    545 }
    546 
    547 /*
    548  * Add an address to the list of IP multicast addresses for a given interface.
    549  */
    550 struct in_multi *
    551 in_addmulti(ap, ifp)
    552 	register struct in_addr *ap;
    553 	register struct ifnet *ifp;
    554 {
    555 	register struct in_multi *inm;
    556 	struct ifreq ifr;
    557 	struct in_ifaddr *ia;
    558 	int s = splsoftnet();
    559 
    560 	/*
    561 	 * See if address already in list.
    562 	 */
    563 	IN_LOOKUP_MULTI(*ap, ifp, inm);
    564 	if (inm != NULL) {
    565 		/*
    566 		 * Found it; just increment the reference count.
    567 		 */
    568 		++inm->inm_refcount;
    569 	} else {
    570 		/*
    571 		 * New address; allocate a new multicast record
    572 		 * and link it into the interface's multicast list.
    573 		 */
    574 		inm = (struct in_multi *)malloc(sizeof(*inm),
    575 		    M_IPMADDR, M_NOWAIT);
    576 		if (inm == NULL) {
    577 			splx(s);
    578 			return (NULL);
    579 		}
    580 		inm->inm_addr = *ap;
    581 		inm->inm_ifp = ifp;
    582 		inm->inm_refcount = 1;
    583 		IFP_TO_IA(ifp, ia);
    584 		if (ia == NULL) {
    585 			free(inm, M_IPMADDR);
    586 			splx(s);
    587 			return (NULL);
    588 		}
    589 		inm->inm_ia = ia;
    590 		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
    591 		/*
    592 		 * Ask the network driver to update its multicast reception
    593 		 * filter appropriately for the new address.
    594 		 */
    595 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
    596 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    597 		satosin(&ifr.ifr_addr)->sin_addr = *ap;
    598 		if ((ifp->if_ioctl == NULL) ||
    599 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
    600 			LIST_REMOVE(inm, inm_list);
    601 			free(inm, M_IPMADDR);
    602 			splx(s);
    603 			return (NULL);
    604 		}
    605 		/*
    606 		 * Let IGMP know that we have joined a new IP multicast group.
    607 		 */
    608 		igmp_joingroup(inm);
    609 	}
    610 	splx(s);
    611 	return (inm);
    612 }
    613 
    614 /*
    615  * Delete a multicast address record.
    616  */
    617 void
    618 in_delmulti(inm)
    619 	register struct in_multi *inm;
    620 {
    621 	struct ifreq ifr;
    622 	int s = splsoftnet();
    623 
    624 	if (--inm->inm_refcount == 0) {
    625 		/*
    626 		 * No remaining claims to this record; let IGMP know that
    627 		 * we are leaving the multicast group.
    628 		 */
    629 		igmp_leavegroup(inm);
    630 		/*
    631 		 * Unlink from list.
    632 		 */
    633 		LIST_REMOVE(inm, inm_list);
    634 		/*
    635 		 * Notify the network driver to update its multicast reception
    636 		 * filter.
    637 		 */
    638 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    639 		satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
    640 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
    641 							     (caddr_t)&ifr);
    642 		free(inm, M_IPMADDR);
    643 	}
    644 	splx(s);
    645 }
    646 #endif
    647