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