Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.40
      1 /*	$NetBSD: in.c,v 1.40 1998/05/29 15:34:24 matt 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  *  Routine to take an Internet address and convert into a
    172  *  "dotted quad" representation for printing.
    173  */
    174 const char *
    175 in_fmtaddr(addr)
    176 	struct in_addr addr;
    177 {
    178 	static char buf[sizeof("123.456.789.123")];
    179 
    180 	addr.s_addr = ntohl(addr.s_addr);
    181 
    182 	sprintf(buf, "%d.%d.%d.%d",
    183 		(addr.s_addr >> 24) & 0xFF,
    184 		(addr.s_addr >> 16) & 0xFF,
    185 		(addr.s_addr >>  8) & 0xFF,
    186 		(addr.s_addr >>  0) & 0xFF);
    187 	return buf;
    188 }
    189 
    190 /*
    191  * Maintain the "in_maxmtu" variable, which is the largest
    192  * mtu for non-local interfaces with AF_INET addresses assigned
    193  * to them that are up.
    194  */
    195 unsigned long in_maxmtu;
    196 
    197 void
    198 in_setmaxmtu()
    199 {
    200 	register struct in_ifaddr *ia;
    201 	register struct ifnet *ifp;
    202 	unsigned long maxmtu = 0;
    203 
    204 	for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) {
    205 		if ((ifp = ia->ia_ifp) == 0)
    206 			continue;
    207 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
    208 			continue;
    209 		if (ifp->if_mtu > maxmtu)
    210 			maxmtu = ifp->if_mtu;
    211 	}
    212 	if (maxmtu)
    213 		in_maxmtu = maxmtu;
    214 }
    215 
    216 int	in_interfaces;		/* number of external internet interfaces */
    217 
    218 /*
    219  * Generic internet control operations (ioctl's).
    220  * Ifp is 0 if not an interface-specific ioctl.
    221  */
    222 /* ARGSUSED */
    223 int
    224 in_control(so, cmd, data, ifp, p)
    225 	struct socket *so;
    226 	u_long cmd;
    227 	caddr_t data;
    228 	register struct ifnet *ifp;
    229 	struct proc *p;
    230 {
    231 	register struct ifreq *ifr = (struct ifreq *)data;
    232 	register struct in_ifaddr *ia = 0;
    233 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
    234 	struct sockaddr_in oldaddr;
    235 	int error, hostIsNew, maskIsNew;
    236 
    237 	/*
    238 	 * Find address for this interface, if it exists.
    239 	 */
    240 	if (ifp)
    241 		IFP_TO_IA(ifp, ia);
    242 
    243 	switch (cmd) {
    244 
    245 	case SIOCAIFADDR:
    246 	case SIOCDIFADDR:
    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 SIOCDIFADDR:
    389 		in_ifscrub(ifp, ia);
    390 		LIST_REMOVE(ia, ia_hash);
    391 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
    392 		TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
    393 		IFAFREE((&ia->ia_ifa));
    394 		in_setmaxmtu();
    395 		break;
    396 
    397 #ifdef MROUTING
    398 	case SIOCGETVIFCNT:
    399 	case SIOCGETSGCNT:
    400 		return (mrt_ioctl(so, cmd, data));
    401 #endif /* MROUTING */
    402 
    403 	default:
    404 		if (ifp == 0 || ifp->if_ioctl == 0)
    405 			return (EOPNOTSUPP);
    406 		error = (*ifp->if_ioctl)(ifp, cmd, data);
    407 		in_setmaxmtu();
    408 		return(error);
    409 	}
    410 	return (0);
    411 }
    412 
    413 /*
    414  * Delete any existing route for an interface.
    415  */
    416 void
    417 in_ifscrub(ifp, ia)
    418 	register struct ifnet *ifp;
    419 	register struct in_ifaddr *ia;
    420 {
    421 
    422 	if ((ia->ia_flags & IFA_ROUTE) == 0)
    423 		return;
    424 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
    425 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    426 	else
    427 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
    428 	ia->ia_flags &= ~IFA_ROUTE;
    429 }
    430 
    431 /*
    432  * Initialize an interface's internet address
    433  * and routing table entry.
    434  */
    435 int
    436 in_ifinit(ifp, ia, sin, scrub)
    437 	register struct ifnet *ifp;
    438 	register struct in_ifaddr *ia;
    439 	struct sockaddr_in *sin;
    440 	int scrub;
    441 {
    442 	register u_int32_t i = sin->sin_addr.s_addr;
    443 	struct sockaddr_in oldaddr;
    444 	int s = splimp(), flags = RTF_UP, error;
    445 
    446 	/*
    447 	 * Set up new addresses.
    448 	 */
    449 	oldaddr = ia->ia_addr;
    450 	if (ia->ia_addr.sin_family == AF_INET)
    451 		LIST_REMOVE(ia, ia_hash);
    452 	ia->ia_addr = *sin;
    453 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
    454 
    455 	/*
    456 	 * Give the interface a chance to initialize
    457 	 * if this is its first address,
    458 	 * and to validate the address if necessary.
    459 	 */
    460 	if (ifp->if_ioctl &&
    461 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
    462 		goto bad;
    463 	splx(s);
    464 	if (scrub) {
    465 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
    466 		in_ifscrub(ifp, ia);
    467 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    468 	}
    469 
    470 	if (IN_CLASSA(i))
    471 		ia->ia_netmask = IN_CLASSA_NET;
    472 	else if (IN_CLASSB(i))
    473 		ia->ia_netmask = IN_CLASSB_NET;
    474 	else
    475 		ia->ia_netmask = IN_CLASSC_NET;
    476 	/*
    477 	 * The subnet mask usually includes at least the standard network part,
    478 	 * but may may be smaller in the case of supernetting.
    479 	 * If it is set, we believe it.
    480 	 */
    481 	if (ia->ia_subnetmask == 0) {
    482 		ia->ia_subnetmask = ia->ia_netmask;
    483 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
    484 	} else
    485 		ia->ia_netmask &= ia->ia_subnetmask;
    486 
    487 	ia->ia_net = i & ia->ia_netmask;
    488 	ia->ia_subnet = i & ia->ia_subnetmask;
    489 	in_socktrim(&ia->ia_sockmask);
    490 	/* re-calculate the "in_maxmtu" value */
    491 	in_setmaxmtu();
    492 	/*
    493 	 * Add route for the network.
    494 	 */
    495 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    496 	if (ifp->if_flags & IFF_BROADCAST) {
    497 		ia->ia_broadaddr.sin_addr.s_addr =
    498 			ia->ia_subnet | ~ia->ia_subnetmask;
    499 		ia->ia_netbroadcast.s_addr =
    500 			ia->ia_net | ~ia->ia_netmask;
    501 	} else if (ifp->if_flags & IFF_LOOPBACK) {
    502 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
    503 		flags |= RTF_HOST;
    504 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
    505 		if (ia->ia_dstaddr.sin_family != AF_INET)
    506 			return (0);
    507 		flags |= RTF_HOST;
    508 	}
    509 	error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags);
    510 	if (!error)
    511 		ia->ia_flags |= IFA_ROUTE;
    512 	/*
    513 	 * If the interface supports multicast, join the "all hosts"
    514 	 * multicast group on that interface.
    515 	 */
    516 	if (ifp->if_flags & IFF_MULTICAST) {
    517 		struct in_addr addr;
    518 
    519 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
    520 		in_addmulti(&addr, ifp);
    521 	}
    522 	return (error);
    523 bad:
    524 	splx(s);
    525 	LIST_REMOVE(ia, ia_hash);
    526 	ia->ia_addr = oldaddr;
    527 	if (ia->ia_addr.sin_family == AF_INET)
    528 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
    529 		    ia, ia_hash);
    530 	return (error);
    531 }
    532 
    533 /*
    534  * Return 1 if the address might be a local broadcast address.
    535  */
    536 int
    537 in_broadcast(in, ifp)
    538 	struct in_addr in;
    539 	struct ifnet *ifp;
    540 {
    541 	register struct ifaddr *ifa;
    542 
    543 	if (in.s_addr == INADDR_BROADCAST ||
    544 	    in_nullhost(in))
    545 		return 1;
    546 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
    547 		return 0;
    548 	/*
    549 	 * Look through the list of addresses for a match
    550 	 * with a broadcast address.
    551 	 */
    552 #define ia (ifatoia(ifa))
    553 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
    554 		if (ifa->ifa_addr->sa_family == AF_INET &&
    555 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
    556 		     in_hosteq(in, ia->ia_netbroadcast) ||
    557 		     /*
    558 		      * Check for old-style (host 0) broadcast.
    559 		      */
    560 		     in.s_addr == ia->ia_subnet ||
    561 		     in.s_addr == ia->ia_net))
    562 			    return 1;
    563 	return (0);
    564 #undef ia
    565 }
    566 
    567 /*
    568  * Add an address to the list of IP multicast addresses for a given interface.
    569  */
    570 struct in_multi *
    571 in_addmulti(ap, ifp)
    572 	register struct in_addr *ap;
    573 	register struct ifnet *ifp;
    574 {
    575 	register struct in_multi *inm;
    576 	struct ifreq ifr;
    577 	struct in_ifaddr *ia;
    578 	int s = splsoftnet();
    579 
    580 	/*
    581 	 * See if address already in list.
    582 	 */
    583 	IN_LOOKUP_MULTI(*ap, ifp, inm);
    584 	if (inm != NULL) {
    585 		/*
    586 		 * Found it; just increment the reference count.
    587 		 */
    588 		++inm->inm_refcount;
    589 	} else {
    590 		/*
    591 		 * New address; allocate a new multicast record
    592 		 * and link it into the interface's multicast list.
    593 		 */
    594 		inm = (struct in_multi *)malloc(sizeof(*inm),
    595 		    M_IPMADDR, M_NOWAIT);
    596 		if (inm == NULL) {
    597 			splx(s);
    598 			return (NULL);
    599 		}
    600 		inm->inm_addr = *ap;
    601 		inm->inm_ifp = ifp;
    602 		inm->inm_refcount = 1;
    603 		IFP_TO_IA(ifp, ia);
    604 		if (ia == NULL) {
    605 			free(inm, M_IPMADDR);
    606 			splx(s);
    607 			return (NULL);
    608 		}
    609 		inm->inm_ia = ia;
    610 		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
    611 		/*
    612 		 * Ask the network driver to update its multicast reception
    613 		 * filter appropriately for the new address.
    614 		 */
    615 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
    616 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    617 		satosin(&ifr.ifr_addr)->sin_addr = *ap;
    618 		if ((ifp->if_ioctl == NULL) ||
    619 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
    620 			LIST_REMOVE(inm, inm_list);
    621 			free(inm, M_IPMADDR);
    622 			splx(s);
    623 			return (NULL);
    624 		}
    625 		/*
    626 		 * Let IGMP know that we have joined a new IP multicast group.
    627 		 */
    628 		igmp_joingroup(inm);
    629 	}
    630 	splx(s);
    631 	return (inm);
    632 }
    633 
    634 /*
    635  * Delete a multicast address record.
    636  */
    637 void
    638 in_delmulti(inm)
    639 	register struct in_multi *inm;
    640 {
    641 	struct ifreq ifr;
    642 	int s = splsoftnet();
    643 
    644 	if (--inm->inm_refcount == 0) {
    645 		/*
    646 		 * No remaining claims to this record; let IGMP know that
    647 		 * we are leaving the multicast group.
    648 		 */
    649 		igmp_leavegroup(inm);
    650 		/*
    651 		 * Unlink from list.
    652 		 */
    653 		LIST_REMOVE(inm, inm_list);
    654 		/*
    655 		 * Notify the network driver to update its multicast reception
    656 		 * filter.
    657 		 */
    658 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    659 		satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
    660 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
    661 							     (caddr_t)&ifr);
    662 		free(inm, M_IPMADDR);
    663 	}
    664 	splx(s);
    665 }
    666 #endif
    667