Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.43
      1 /*	$NetBSD: in.c,v 1.43 1998/09/06 17:52:28 christos 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_inet.h"
     76 #include "opt_mrouting.h"
     77 
     78 #include <sys/param.h>
     79 #include <sys/ioctl.h>
     80 #include <sys/errno.h>
     81 #include <sys/malloc.h>
     82 #include <sys/socket.h>
     83 #include <sys/socketvar.h>
     84 #include <sys/systm.h>
     85 #include <sys/proc.h>
     86 
     87 #include <net/if.h>
     88 #include <net/route.h>
     89 
     90 #include <net/if_ether.h>
     91 
     92 #include <netinet/in_systm.h>
     93 #include <netinet/in.h>
     94 #include <netinet/in_var.h>
     95 #include <netinet/if_inarp.h>
     96 #include <netinet/ip_mroute.h>
     97 #include <netinet/igmp_var.h>
     98 
     99 #ifdef INET
    100 
    101 #ifndef SUBNETSARELOCAL
    102 #define	SUBNETSARELOCAL	1
    103 #endif
    104 int subnetsarelocal = SUBNETSARELOCAL;
    105 
    106 /*
    107  * Return 1 if an internet address is for a ``local'' host
    108  * (one to which we have a connection).  If subnetsarelocal
    109  * is true, this includes other subnets of the local net.
    110  * Otherwise, it includes only the directly-connected (sub)nets.
    111  */
    112 int
    113 in_localaddr(in)
    114 	struct in_addr in;
    115 {
    116 	register struct in_ifaddr *ia;
    117 
    118 	if (subnetsarelocal) {
    119 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
    120 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
    121 				return (1);
    122 	} else {
    123 		for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
    124 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
    125 				return (1);
    126 	}
    127 	return (0);
    128 }
    129 
    130 /*
    131  * Determine whether an IP address is in a reserved set of addresses
    132  * that may not be forwarded, or whether datagrams to that destination
    133  * may be forwarded.
    134  */
    135 int
    136 in_canforward(in)
    137 	struct in_addr in;
    138 {
    139 	register u_int32_t net;
    140 
    141 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
    142 		return (0);
    143 	if (IN_CLASSA(in.s_addr)) {
    144 		net = in.s_addr & IN_CLASSA_NET;
    145 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
    146 			return (0);
    147 	}
    148 	return (1);
    149 }
    150 
    151 /*
    152  * Trim a mask in a sockaddr
    153  */
    154 void
    155 in_socktrim(ap)
    156 	struct sockaddr_in *ap;
    157 {
    158 	register char *cplim = (char *) &ap->sin_addr;
    159 	register char *cp = (char *) (&ap->sin_addr + 1);
    160 
    161 	ap->sin_len = 0;
    162 	while (--cp >= cplim)
    163 		if (*cp) {
    164 			(ap)->sin_len = cp - (char *) (ap) + 1;
    165 			break;
    166 		}
    167 }
    168 
    169 /*
    170  *  Routine to take an Internet address and convert into a
    171  *  "dotted quad" representation for printing.
    172  */
    173 const char *
    174 in_fmtaddr(addr)
    175 	struct in_addr addr;
    176 {
    177 	static char buf[sizeof("123.456.789.123")];
    178 
    179 	addr.s_addr = ntohl(addr.s_addr);
    180 
    181 	sprintf(buf, "%d.%d.%d.%d",
    182 		(addr.s_addr >> 24) & 0xFF,
    183 		(addr.s_addr >> 16) & 0xFF,
    184 		(addr.s_addr >>  8) & 0xFF,
    185 		(addr.s_addr >>  0) & 0xFF);
    186 	return buf;
    187 }
    188 
    189 /*
    190  * Maintain the "in_maxmtu" variable, which is the largest
    191  * mtu for non-local interfaces with AF_INET addresses assigned
    192  * to them that are up.
    193  */
    194 unsigned long in_maxmtu;
    195 
    196 void
    197 in_setmaxmtu()
    198 {
    199 	register struct in_ifaddr *ia;
    200 	register struct ifnet *ifp;
    201 	unsigned long maxmtu = 0;
    202 
    203 	for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) {
    204 		if ((ifp = ia->ia_ifp) == 0)
    205 			continue;
    206 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
    207 			continue;
    208 		if (ifp->if_mtu > maxmtu)
    209 			maxmtu = ifp->if_mtu;
    210 	}
    211 	if (maxmtu)
    212 		in_maxmtu = maxmtu;
    213 }
    214 
    215 int	in_interfaces;		/* number of external internet interfaces */
    216 
    217 /*
    218  * Generic internet control operations (ioctl's).
    219  * Ifp is 0 if not an interface-specific ioctl.
    220  */
    221 /* ARGSUSED */
    222 int
    223 in_control(so, cmd, data, ifp, p)
    224 	struct socket *so;
    225 	u_long cmd;
    226 	caddr_t data;
    227 	register struct ifnet *ifp;
    228 	struct proc *p;
    229 {
    230 	register struct ifreq *ifr = (struct ifreq *)data;
    231 	register struct in_ifaddr *ia = 0;
    232 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
    233 	struct sockaddr_in oldaddr;
    234 	int error, hostIsNew, maskIsNew;
    235 
    236 	/*
    237 	 * Find address for this interface, if it exists.
    238 	 */
    239 	if (ifp)
    240 		IFP_TO_IA(ifp, ia);
    241 
    242 	switch (cmd) {
    243 
    244 	case SIOCAIFADDR:
    245 	case SIOCDIFADDR:
    246 	case SIOCGIFALIAS:
    247 		if (ifra->ifra_addr.sin_family == AF_INET)
    248 			for (ia = IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr).lh_first;
    249 			    ia != 0; ia = ia->ia_hash.le_next) {
    250 				if (ia->ia_ifp == ifp  &&
    251 				    in_hosteq(ia->ia_addr.sin_addr,
    252 				    ifra->ifra_addr.sin_addr))
    253 					break;
    254 			}
    255 		if (cmd == SIOCDIFADDR && ia == 0)
    256 			return (EADDRNOTAVAIL);
    257 		/* FALLTHROUGH */
    258 	case SIOCSIFADDR:
    259 	case SIOCSIFNETMASK:
    260 	case SIOCSIFDSTADDR:
    261 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    262 			return (EPERM);
    263 
    264 		if (ifp == 0)
    265 			panic("in_control");
    266 		if (ia == 0) {
    267 			MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
    268 			       M_IFADDR, M_WAITOK);
    269 			if (ia == 0)
    270 				return (ENOBUFS);
    271 			bzero((caddr_t)ia, sizeof *ia);
    272 			TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list);
    273 			TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia,
    274 			    ifa_list);
    275 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    276 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    277 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
    278 			ia->ia_sockmask.sin_len = 8;
    279 			if (ifp->if_flags & IFF_BROADCAST) {
    280 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
    281 				ia->ia_broadaddr.sin_family = AF_INET;
    282 			}
    283 			ia->ia_ifp = ifp;
    284 			LIST_INIT(&ia->ia_multiaddrs);
    285 			if ((ifp->if_flags & IFF_LOOPBACK) == 0)
    286 				in_interfaces++;
    287 		}
    288 		break;
    289 
    290 	case SIOCSIFBRDADDR:
    291 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    292 			return (EPERM);
    293 		/* FALLTHROUGH */
    294 
    295 	case SIOCGIFADDR:
    296 	case SIOCGIFNETMASK:
    297 	case SIOCGIFDSTADDR:
    298 	case SIOCGIFBRDADDR:
    299 		if (ia == 0)
    300 			return (EADDRNOTAVAIL);
    301 		break;
    302 	}
    303 	switch (cmd) {
    304 
    305 	case SIOCGIFADDR:
    306 		*satosin(&ifr->ifr_addr) = ia->ia_addr;
    307 		break;
    308 
    309 	case SIOCGIFBRDADDR:
    310 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    311 			return (EINVAL);
    312 		*satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
    313 		break;
    314 
    315 	case SIOCGIFDSTADDR:
    316 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    317 			return (EINVAL);
    318 		*satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
    319 		break;
    320 
    321 	case SIOCGIFNETMASK:
    322 		*satosin(&ifr->ifr_addr) = ia->ia_sockmask;
    323 		break;
    324 
    325 	case SIOCSIFDSTADDR:
    326 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    327 			return (EINVAL);
    328 		oldaddr = ia->ia_dstaddr;
    329 		ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
    330 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
    331 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
    332 			ia->ia_dstaddr = oldaddr;
    333 			return (error);
    334 		}
    335 		if (ia->ia_flags & IFA_ROUTE) {
    336 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
    337 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    338 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    339 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
    340 		}
    341 		break;
    342 
    343 	case SIOCSIFBRDADDR:
    344 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    345 			return (EINVAL);
    346 		ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
    347 		break;
    348 
    349 	case SIOCSIFADDR:
    350 		return (in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1));
    351 
    352 	case SIOCSIFNETMASK:
    353 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr =
    354 		    ifra->ifra_addr.sin_addr.s_addr;
    355 		break;
    356 
    357 	case SIOCAIFADDR:
    358 		maskIsNew = 0;
    359 		hostIsNew = 1;
    360 		error = 0;
    361 		if (ia->ia_addr.sin_family == AF_INET) {
    362 			if (ifra->ifra_addr.sin_len == 0) {
    363 				ifra->ifra_addr = ia->ia_addr;
    364 				hostIsNew = 0;
    365 			} else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr))
    366 				hostIsNew = 0;
    367 		}
    368 		if (ifra->ifra_mask.sin_len) {
    369 			in_ifscrub(ifp, ia);
    370 			ia->ia_sockmask = ifra->ifra_mask;
    371 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    372 			maskIsNew = 1;
    373 		}
    374 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    375 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
    376 			in_ifscrub(ifp, ia);
    377 			ia->ia_dstaddr = ifra->ifra_dstaddr;
    378 			maskIsNew  = 1; /* We lie; but the effect's the same */
    379 		}
    380 		if (ifra->ifra_addr.sin_family == AF_INET &&
    381 		    (hostIsNew || maskIsNew))
    382 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
    383 		if ((ifp->if_flags & IFF_BROADCAST) &&
    384 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
    385 			ia->ia_broadaddr = ifra->ifra_broadaddr;
    386 		return (error);
    387 
    388 	case SIOCGIFALIAS:
    389 		ifra->ifra_mask = ia->ia_sockmask;
    390 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    391 		    (ia->ia_dstaddr.sin_family == AF_INET))
    392 			ifra->ifra_dstaddr = ia->ia_dstaddr;
    393 		else if ((ifp->if_flags & IFF_BROADCAST) &&
    394 		    (ia->ia_broadaddr.sin_family == AF_INET))
    395 			ifra->ifra_broadaddr = ia->ia_broadaddr;
    396 		else
    397 			memset(&ifra->ifra_broadaddr, 0,
    398 			    sizeof(ifra->ifra_broadaddr));
    399 		return 0;
    400 
    401 	case SIOCDIFADDR:
    402 		in_ifscrub(ifp, ia);
    403 		LIST_REMOVE(ia, ia_hash);
    404 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    405 		TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
    406 		IFAFREE((&ia->ia_ifa));
    407 		in_setmaxmtu();
    408 		break;
    409 
    410 #ifdef MROUTING
    411 	case SIOCGETVIFCNT:
    412 	case SIOCGETSGCNT:
    413 		return (mrt_ioctl(so, cmd, data));
    414 #endif /* MROUTING */
    415 
    416 	default:
    417 		if (ifp == 0 || ifp->if_ioctl == 0)
    418 			return (EOPNOTSUPP);
    419 		error = (*ifp->if_ioctl)(ifp, cmd, data);
    420 		in_setmaxmtu();
    421 		return(error);
    422 	}
    423 	return (0);
    424 }
    425 
    426 /*
    427  * Delete any existing route for an interface.
    428  */
    429 void
    430 in_ifscrub(ifp, ia)
    431 	register struct ifnet *ifp;
    432 	register struct in_ifaddr *ia;
    433 {
    434 
    435 	if ((ia->ia_flags & IFA_ROUTE) == 0)
    436 		return;
    437 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
    438 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    439 	else
    440 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
    441 	ia->ia_flags &= ~IFA_ROUTE;
    442 }
    443 
    444 /*
    445  * Initialize an interface's internet address
    446  * and routing table entry.
    447  */
    448 int
    449 in_ifinit(ifp, ia, sin, scrub)
    450 	register struct ifnet *ifp;
    451 	register struct in_ifaddr *ia;
    452 	struct sockaddr_in *sin;
    453 	int scrub;
    454 {
    455 	register u_int32_t i = sin->sin_addr.s_addr;
    456 	struct sockaddr_in oldaddr;
    457 	int s = splimp(), flags = RTF_UP, error;
    458 
    459 	/*
    460 	 * Set up new addresses.
    461 	 */
    462 	oldaddr = ia->ia_addr;
    463 	if (ia->ia_addr.sin_family == AF_INET)
    464 		LIST_REMOVE(ia, ia_hash);
    465 	ia->ia_addr = *sin;
    466 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
    467 
    468 	/*
    469 	 * Give the interface a chance to initialize
    470 	 * if this is its first address,
    471 	 * and to validate the address if necessary.
    472 	 */
    473 	if (ifp->if_ioctl &&
    474 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
    475 		goto bad;
    476 	splx(s);
    477 	if (scrub) {
    478 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
    479 		in_ifscrub(ifp, ia);
    480 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    481 	}
    482 
    483 	if (IN_CLASSA(i))
    484 		ia->ia_netmask = IN_CLASSA_NET;
    485 	else if (IN_CLASSB(i))
    486 		ia->ia_netmask = IN_CLASSB_NET;
    487 	else
    488 		ia->ia_netmask = IN_CLASSC_NET;
    489 	/*
    490 	 * The subnet mask usually includes at least the standard network part,
    491 	 * but may may be smaller in the case of supernetting.
    492 	 * If it is set, we believe it.
    493 	 */
    494 	if (ia->ia_subnetmask == 0) {
    495 		ia->ia_subnetmask = ia->ia_netmask;
    496 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
    497 	} else
    498 		ia->ia_netmask &= ia->ia_subnetmask;
    499 
    500 	ia->ia_net = i & ia->ia_netmask;
    501 	ia->ia_subnet = i & ia->ia_subnetmask;
    502 	in_socktrim(&ia->ia_sockmask);
    503 	/* re-calculate the "in_maxmtu" value */
    504 	in_setmaxmtu();
    505 	/*
    506 	 * Add route for the network.
    507 	 */
    508 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    509 	if (ifp->if_flags & IFF_BROADCAST) {
    510 		ia->ia_broadaddr.sin_addr.s_addr =
    511 			ia->ia_subnet | ~ia->ia_subnetmask;
    512 		ia->ia_netbroadcast.s_addr =
    513 			ia->ia_net | ~ia->ia_netmask;
    514 	} else if (ifp->if_flags & IFF_LOOPBACK) {
    515 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
    516 		flags |= RTF_HOST;
    517 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
    518 		if (ia->ia_dstaddr.sin_family != AF_INET)
    519 			return (0);
    520 		flags |= RTF_HOST;
    521 	}
    522 	error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags);
    523 	if (!error)
    524 		ia->ia_flags |= IFA_ROUTE;
    525 	/*
    526 	 * If the interface supports multicast, join the "all hosts"
    527 	 * multicast group on that interface.
    528 	 */
    529 	if (ifp->if_flags & IFF_MULTICAST) {
    530 		struct in_addr addr;
    531 
    532 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
    533 		in_addmulti(&addr, ifp);
    534 	}
    535 	return (error);
    536 bad:
    537 	splx(s);
    538 	LIST_REMOVE(ia, ia_hash);
    539 	ia->ia_addr = oldaddr;
    540 	if (ia->ia_addr.sin_family == AF_INET)
    541 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
    542 		    ia, ia_hash);
    543 	return (error);
    544 }
    545 
    546 /*
    547  * Return 1 if the address might be a local broadcast address.
    548  */
    549 int
    550 in_broadcast(in, ifp)
    551 	struct in_addr in;
    552 	struct ifnet *ifp;
    553 {
    554 	register struct ifaddr *ifa;
    555 
    556 	if (in.s_addr == INADDR_BROADCAST ||
    557 	    in_nullhost(in))
    558 		return 1;
    559 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
    560 		return 0;
    561 	/*
    562 	 * Look through the list of addresses for a match
    563 	 * with a broadcast address.
    564 	 */
    565 #define ia (ifatoia(ifa))
    566 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
    567 		if (ifa->ifa_addr->sa_family == AF_INET &&
    568 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
    569 		     in_hosteq(in, ia->ia_netbroadcast) ||
    570 		     /*
    571 		      * Check for old-style (host 0) broadcast.
    572 		      */
    573 		     in.s_addr == ia->ia_subnet ||
    574 		     in.s_addr == ia->ia_net))
    575 			    return 1;
    576 	return (0);
    577 #undef ia
    578 }
    579 
    580 /*
    581  * Add an address to the list of IP multicast addresses for a given interface.
    582  */
    583 struct in_multi *
    584 in_addmulti(ap, ifp)
    585 	register struct in_addr *ap;
    586 	register struct ifnet *ifp;
    587 {
    588 	register struct in_multi *inm;
    589 	struct ifreq ifr;
    590 	struct in_ifaddr *ia;
    591 	int s = splsoftnet();
    592 
    593 	/*
    594 	 * See if address already in list.
    595 	 */
    596 	IN_LOOKUP_MULTI(*ap, ifp, inm);
    597 	if (inm != NULL) {
    598 		/*
    599 		 * Found it; just increment the reference count.
    600 		 */
    601 		++inm->inm_refcount;
    602 	} else {
    603 		/*
    604 		 * New address; allocate a new multicast record
    605 		 * and link it into the interface's multicast list.
    606 		 */
    607 		inm = (struct in_multi *)malloc(sizeof(*inm),
    608 		    M_IPMADDR, M_NOWAIT);
    609 		if (inm == NULL) {
    610 			splx(s);
    611 			return (NULL);
    612 		}
    613 		inm->inm_addr = *ap;
    614 		inm->inm_ifp = ifp;
    615 		inm->inm_refcount = 1;
    616 		IFP_TO_IA(ifp, ia);
    617 		if (ia == NULL) {
    618 			free(inm, M_IPMADDR);
    619 			splx(s);
    620 			return (NULL);
    621 		}
    622 		inm->inm_ia = ia;
    623 		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
    624 		/*
    625 		 * Ask the network driver to update its multicast reception
    626 		 * filter appropriately for the new address.
    627 		 */
    628 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
    629 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    630 		satosin(&ifr.ifr_addr)->sin_addr = *ap;
    631 		if ((ifp->if_ioctl == NULL) ||
    632 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
    633 			LIST_REMOVE(inm, inm_list);
    634 			free(inm, M_IPMADDR);
    635 			splx(s);
    636 			return (NULL);
    637 		}
    638 		/*
    639 		 * Let IGMP know that we have joined a new IP multicast group.
    640 		 */
    641 		igmp_joingroup(inm);
    642 	}
    643 	splx(s);
    644 	return (inm);
    645 }
    646 
    647 /*
    648  * Delete a multicast address record.
    649  */
    650 void
    651 in_delmulti(inm)
    652 	register struct in_multi *inm;
    653 {
    654 	struct ifreq ifr;
    655 	int s = splsoftnet();
    656 
    657 	if (--inm->inm_refcount == 0) {
    658 		/*
    659 		 * No remaining claims to this record; let IGMP know that
    660 		 * we are leaving the multicast group.
    661 		 */
    662 		igmp_leavegroup(inm);
    663 		/*
    664 		 * Unlink from list.
    665 		 */
    666 		LIST_REMOVE(inm, inm_list);
    667 		/*
    668 		 * Notify the network driver to update its multicast reception
    669 		 * filter.
    670 		 */
    671 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    672 		satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
    673 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
    674 							     (caddr_t)&ifr);
    675 		free(inm, M_IPMADDR);
    676 	}
    677 	splx(s);
    678 }
    679 #endif
    680