Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.75
      1 /*	$NetBSD: in.c,v 1.75 2002/03/30 00:40:32 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Public Access Networks Corporation ("Panix").  It was developed under
     38  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1991, 1993
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *	This product includes software developed by the University of
     84  *	California, Berkeley and its contributors.
     85  * 4. Neither the name of the University nor the names of its contributors
     86  *    may be used to endorse or promote products derived from this software
     87  *    without specific prior written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     91  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     92  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     93  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     94  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     95  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     96  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     97  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     98  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     99  * SUCH DAMAGE.
    100  *
    101  *	@(#)in.c	8.4 (Berkeley) 1/9/95
    102  */
    103 
    104 #include <sys/cdefs.h>
    105 __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.75 2002/03/30 00:40:32 itojun Exp $");
    106 
    107 #include "opt_inet.h"
    108 #include "opt_inet_conf.h"
    109 #include "opt_mrouting.h"
    110 
    111 #include <sys/param.h>
    112 #include <sys/ioctl.h>
    113 #include <sys/errno.h>
    114 #include <sys/malloc.h>
    115 #include <sys/socket.h>
    116 #include <sys/socketvar.h>
    117 #include <sys/systm.h>
    118 #include <sys/proc.h>
    119 #include <sys/syslog.h>
    120 
    121 #include <net/if.h>
    122 #include <net/route.h>
    123 
    124 #include <net/if_ether.h>
    125 
    126 #include <netinet/in_systm.h>
    127 #include <netinet/in.h>
    128 #include <netinet/in_var.h>
    129 #include <netinet/if_inarp.h>
    130 #include <netinet/ip_mroute.h>
    131 #include <netinet/igmp_var.h>
    132 
    133 #ifdef INET
    134 
    135 static int in_mask2len __P((struct in_addr *));
    136 static void in_len2mask __P((struct in_addr *, int));
    137 static int in_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
    138 	struct ifnet *, struct proc *));
    139 static int in_rt_walktree __P((struct radix_node *, void *));
    140 
    141 static int in_addprefix __P((struct in_ifaddr *, int));
    142 static int in_scrubprefix __P((struct in_ifaddr *));
    143 
    144 #ifndef SUBNETSARELOCAL
    145 #define	SUBNETSARELOCAL	1
    146 #endif
    147 
    148 #ifndef HOSTZEROBROADCAST
    149 #define HOSTZEROBROADCAST 1
    150 #endif
    151 
    152 int subnetsarelocal = SUBNETSARELOCAL;
    153 int hostzeroisbroadcast = HOSTZEROBROADCAST;
    154 
    155 /*
    156  * This list is used to keep track of in_multi chains which belong to
    157  * deleted interface addresses.  We use in_ifaddr so that a chain head
    158  * won't be deallocated until all multicast address record are deleted.
    159  */
    160 static TAILQ_HEAD(, in_ifaddr) in_mk = TAILQ_HEAD_INITIALIZER(in_mk);
    161 
    162 /*
    163  * Return 1 if an internet address is for a ``local'' host
    164  * (one to which we have a connection).  If subnetsarelocal
    165  * is true, this includes other subnets of the local net.
    166  * Otherwise, it includes only the directly-connected (sub)nets.
    167  */
    168 int
    169 in_localaddr(in)
    170 	struct in_addr in;
    171 {
    172 	struct in_ifaddr *ia;
    173 
    174 	if (subnetsarelocal) {
    175 		TAILQ_FOREACH(ia, &in_ifaddr, ia_list)
    176 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
    177 				return (1);
    178 	} else {
    179 		TAILQ_FOREACH(ia, &in_ifaddr, ia_list)
    180 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
    181 				return (1);
    182 	}
    183 	return (0);
    184 }
    185 
    186 /*
    187  * Determine whether an IP address is in a reserved set of addresses
    188  * that may not be forwarded, or whether datagrams to that destination
    189  * may be forwarded.
    190  */
    191 int
    192 in_canforward(in)
    193 	struct in_addr in;
    194 {
    195 	u_int32_t net;
    196 
    197 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
    198 		return (0);
    199 	if (IN_CLASSA(in.s_addr)) {
    200 		net = in.s_addr & IN_CLASSA_NET;
    201 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
    202 			return (0);
    203 	}
    204 	return (1);
    205 }
    206 
    207 /*
    208  * Trim a mask in a sockaddr
    209  */
    210 void
    211 in_socktrim(ap)
    212 	struct sockaddr_in *ap;
    213 {
    214 	char *cplim = (char *) &ap->sin_addr;
    215 	char *cp = (char *) (&ap->sin_addr + 1);
    216 
    217 	ap->sin_len = 0;
    218 	while (--cp >= cplim)
    219 		if (*cp) {
    220 			(ap)->sin_len = cp - (char *) (ap) + 1;
    221 			break;
    222 		}
    223 }
    224 
    225 /*
    226  *  Routine to take an Internet address and convert into a
    227  *  "dotted quad" representation for printing.
    228  */
    229 const char *
    230 in_fmtaddr(addr)
    231 	struct in_addr addr;
    232 {
    233 	static char buf[sizeof("123.456.789.123")];
    234 
    235 	addr.s_addr = ntohl(addr.s_addr);
    236 
    237 	sprintf(buf, "%d.%d.%d.%d",
    238 		(addr.s_addr >> 24) & 0xFF,
    239 		(addr.s_addr >> 16) & 0xFF,
    240 		(addr.s_addr >>  8) & 0xFF,
    241 		(addr.s_addr >>  0) & 0xFF);
    242 	return buf;
    243 }
    244 
    245 /*
    246  * Maintain the "in_maxmtu" variable, which is the largest
    247  * mtu for non-local interfaces with AF_INET addresses assigned
    248  * to them that are up.
    249  */
    250 unsigned long in_maxmtu;
    251 
    252 void
    253 in_setmaxmtu()
    254 {
    255 	struct in_ifaddr *ia;
    256 	struct ifnet *ifp;
    257 	unsigned long maxmtu = 0;
    258 
    259 	TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
    260 		if ((ifp = ia->ia_ifp) == 0)
    261 			continue;
    262 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
    263 			continue;
    264 		if (ifp->if_mtu > maxmtu)
    265 			maxmtu = ifp->if_mtu;
    266 	}
    267 	if (maxmtu)
    268 		in_maxmtu = maxmtu;
    269 }
    270 
    271 static int
    272 in_mask2len(mask)
    273 	struct in_addr *mask;
    274 {
    275 	int x, y;
    276 	u_char *p;
    277 
    278 	p = (u_char *)mask;
    279 	for (x = 0; x < sizeof(*mask); x++) {
    280 		if (p[x] != 0xff)
    281 			break;
    282 	}
    283 	y = 0;
    284 	if (x < sizeof(*mask)) {
    285 		for (y = 0; y < 8; y++) {
    286 			if ((p[x] & (0x80 >> y)) == 0)
    287 				break;
    288 		}
    289 	}
    290 	return x * 8 + y;
    291 }
    292 
    293 static void
    294 in_len2mask(mask, len)
    295 	struct in_addr *mask;
    296 	int len;
    297 {
    298 	int i;
    299 	u_char *p;
    300 
    301 	p = (u_char *)mask;
    302 	bzero(mask, sizeof(*mask));
    303 	for (i = 0; i < len / 8; i++)
    304 		p[i] = 0xff;
    305 	if (len % 8)
    306 		p[i] = (0xff00 >> (len % 8)) & 0xff;
    307 }
    308 
    309 /*
    310  * Generic internet control operations (ioctl's).
    311  * Ifp is 0 if not an interface-specific ioctl.
    312  */
    313 /* ARGSUSED */
    314 int
    315 in_control(so, cmd, data, ifp, p)
    316 	struct socket *so;
    317 	u_long cmd;
    318 	caddr_t data;
    319 	struct ifnet *ifp;
    320 	struct proc *p;
    321 {
    322 	struct ifreq *ifr = (struct ifreq *)data;
    323 	struct in_ifaddr *ia = 0;
    324 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
    325 	struct sockaddr_in oldaddr;
    326 	int error, hostIsNew, maskIsNew;
    327 	int newifaddr;
    328 
    329 	switch (cmd) {
    330 	case SIOCALIFADDR:
    331 	case SIOCDLIFADDR:
    332 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    333 			return(EPERM);
    334 		/*fall through*/
    335 	case SIOCGLIFADDR:
    336 		if (!ifp)
    337 			return EINVAL;
    338 		return in_lifaddr_ioctl(so, cmd, data, ifp, p);
    339 	}
    340 
    341 	/*
    342 	 * Find address for this interface, if it exists.
    343 	 */
    344 	if (ifp)
    345 		IFP_TO_IA(ifp, ia);
    346 
    347 	switch (cmd) {
    348 
    349 	case SIOCAIFADDR:
    350 	case SIOCDIFADDR:
    351 	case SIOCGIFALIAS:
    352 		if (ifra->ifra_addr.sin_family == AF_INET)
    353 			LIST_FOREACH(ia,
    354 			    &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
    355 			    ia_hash) {
    356 				if (ia->ia_ifp == ifp  &&
    357 				    in_hosteq(ia->ia_addr.sin_addr,
    358 				    ifra->ifra_addr.sin_addr))
    359 					break;
    360 			}
    361 		if (cmd == SIOCDIFADDR) {
    362 			if (ia == 0)
    363 				return (EADDRNOTAVAIL);
    364 #if 1 /*def COMPAT_43*/
    365 			if (ifra->ifra_addr.sin_family == AF_UNSPEC)
    366 				ifra->ifra_addr.sin_family = AF_INET;
    367 #endif
    368 		}
    369 		/* FALLTHROUGH */
    370 	case SIOCSIFADDR:
    371 	case SIOCSIFDSTADDR:
    372 		if (ifra->ifra_addr.sin_family != AF_INET)
    373 			return (EAFNOSUPPORT);
    374 		/* FALLTHROUGH */
    375 	case SIOCSIFNETMASK:
    376 		if (ifp == 0)
    377 			panic("in_control");
    378 
    379 		if (cmd == SIOCGIFALIAS)
    380 			break;
    381 
    382 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    383 			return (EPERM);
    384 
    385 		if (ia == 0) {
    386 			MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
    387 			       M_IFADDR, M_WAITOK);
    388 			if (ia == 0)
    389 				return (ENOBUFS);
    390 			bzero((caddr_t)ia, sizeof *ia);
    391 			TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list);
    392 			IFAREF(&ia->ia_ifa);
    393 			TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
    394 			    ifa_list);
    395 			IFAREF(&ia->ia_ifa);
    396 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    397 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    398 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
    399 			ia->ia_sockmask.sin_len = 8;
    400 			if (ifp->if_flags & IFF_BROADCAST) {
    401 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
    402 				ia->ia_broadaddr.sin_family = AF_INET;
    403 			}
    404 			ia->ia_ifp = ifp;
    405 			LIST_INIT(&ia->ia_multiaddrs);
    406 			newifaddr = 1;
    407 		} else
    408 			newifaddr = 0;
    409 		break;
    410 
    411 	case SIOCSIFBRDADDR:
    412 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    413 			return (EPERM);
    414 		/* FALLTHROUGH */
    415 
    416 	case SIOCGIFADDR:
    417 	case SIOCGIFNETMASK:
    418 	case SIOCGIFDSTADDR:
    419 	case SIOCGIFBRDADDR:
    420 		if (ia == 0)
    421 			return (EADDRNOTAVAIL);
    422 		break;
    423 	}
    424 	switch (cmd) {
    425 
    426 	case SIOCGIFADDR:
    427 		*satosin(&ifr->ifr_addr) = ia->ia_addr;
    428 		break;
    429 
    430 	case SIOCGIFBRDADDR:
    431 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    432 			return (EINVAL);
    433 		*satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
    434 		break;
    435 
    436 	case SIOCGIFDSTADDR:
    437 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    438 			return (EINVAL);
    439 		*satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
    440 		break;
    441 
    442 	case SIOCGIFNETMASK:
    443 		*satosin(&ifr->ifr_addr) = ia->ia_sockmask;
    444 		break;
    445 
    446 	case SIOCSIFDSTADDR:
    447 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    448 			return (EINVAL);
    449 		oldaddr = ia->ia_dstaddr;
    450 		ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
    451 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
    452 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
    453 			ia->ia_dstaddr = oldaddr;
    454 			return (error);
    455 		}
    456 		if (ia->ia_flags & IFA_ROUTE) {
    457 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
    458 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
    459 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    460 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
    461 		}
    462 		break;
    463 
    464 	case SIOCSIFBRDADDR:
    465 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    466 			return (EINVAL);
    467 		ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
    468 		break;
    469 
    470 	case SIOCSIFADDR:
    471 		error = in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1);
    472 		return error;
    473 
    474 	case SIOCSIFNETMASK:
    475 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr =
    476 		    ifra->ifra_addr.sin_addr.s_addr;
    477 		break;
    478 
    479 	case SIOCAIFADDR:
    480 		maskIsNew = 0;
    481 		hostIsNew = 1;
    482 		error = 0;
    483 		if (ia->ia_addr.sin_family == AF_INET) {
    484 			if (ifra->ifra_addr.sin_len == 0) {
    485 				ifra->ifra_addr = ia->ia_addr;
    486 				hostIsNew = 0;
    487 			} else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr))
    488 				hostIsNew = 0;
    489 		}
    490 		if (ifra->ifra_mask.sin_len) {
    491 			in_ifscrub(ifp, ia);
    492 			ia->ia_sockmask = ifra->ifra_mask;
    493 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    494 			maskIsNew = 1;
    495 		}
    496 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    497 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
    498 			in_ifscrub(ifp, ia);
    499 			ia->ia_dstaddr = ifra->ifra_dstaddr;
    500 			maskIsNew  = 1; /* We lie; but the effect's the same */
    501 		}
    502 		if (ifra->ifra_addr.sin_family == AF_INET &&
    503 		    (hostIsNew || maskIsNew)) {
    504 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
    505 		}
    506 		if ((ifp->if_flags & IFF_BROADCAST) &&
    507 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
    508 			ia->ia_broadaddr = ifra->ifra_broadaddr;
    509 		return (error);
    510 
    511 	case SIOCGIFALIAS:
    512 		ifra->ifra_mask = ia->ia_sockmask;
    513 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    514 		    (ia->ia_dstaddr.sin_family == AF_INET))
    515 			ifra->ifra_dstaddr = ia->ia_dstaddr;
    516 		else if ((ifp->if_flags & IFF_BROADCAST) &&
    517 		    (ia->ia_broadaddr.sin_family == AF_INET))
    518 			ifra->ifra_broadaddr = ia->ia_broadaddr;
    519 		else
    520 			bzero(&ifra->ifra_broadaddr,
    521 			      sizeof(ifra->ifra_broadaddr));
    522 		return 0;
    523 
    524 	case SIOCDIFADDR:
    525 		in_purgeaddr(&ia->ia_ifa, ifp);
    526 		break;
    527 
    528 #ifdef MROUTING
    529 	case SIOCGETVIFCNT:
    530 	case SIOCGETSGCNT:
    531 		return (mrt_ioctl(so, cmd, data));
    532 #endif /* MROUTING */
    533 
    534 	default:
    535 		if (ifp == 0 || ifp->if_ioctl == 0)
    536 			return (EOPNOTSUPP);
    537 		error = (*ifp->if_ioctl)(ifp, cmd, data);
    538 		in_setmaxmtu();
    539 		return(error);
    540 	}
    541 	return (0);
    542 }
    543 
    544 static int
    545 in_rt_walktree(rn, v)
    546 	struct radix_node *rn;
    547 	void *v;
    548 {
    549 	struct in_ifaddr *ia = (struct in_ifaddr *)v;
    550 	struct rtentry *rt = (struct rtentry *)rn;
    551 	int error;
    552 
    553 	if (rt->rt_ifa == &ia->ia_ifa) {
    554 		if ((error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
    555 		    rt_mask(rt), rt->rt_flags, NULL)) != 0) {
    556 			log(LOG_WARNING, "ifa_rt_walktree: unable to delete "
    557 			    "rtentry. error= %d", error);
    558 		}
    559 	}
    560 	return 0;
    561 }
    562 
    563 void
    564 in_purgeaddr(ifa, ifp)
    565 	struct ifaddr *ifa;
    566 	struct ifnet *ifp;
    567 {
    568 	struct in_ifaddr *ia = (void *) ifa;
    569 	struct radix_node_head *rnh;
    570 
    571 	in_ifscrub(ifp, ia);
    572 	LIST_REMOVE(ia, ia_hash);
    573 	TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
    574 	IFAFREE(&ia->ia_ifa);
    575 	TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
    576 	if (ia->ia_allhosts != NULL)
    577 		in_delmulti(ia->ia_allhosts);
    578 	if (LIST_FIRST(&ia->ia_multiaddrs) != NULL &&
    579 	    /*
    580 	     * If the interface is going away, don't bother to save
    581 	     * the multicast entries.
    582 	     */
    583 	    ifp->if_output != if_nulloutput)
    584 		in_savemkludge(ia);
    585 	IFAFREE(&ia->ia_ifa);
    586 
    587 	if ((rnh = rt_tables[AF_INET]) != NULL)
    588 		(*rnh->rnh_walktree)(rnh, in_rt_walktree, ifa);
    589 
    590 	in_setmaxmtu();
    591 }
    592 
    593 void
    594 in_purgeif(ifp)
    595 	struct ifnet *ifp;
    596 {
    597 	struct ifaddr *ifa, *nifa;
    598 
    599 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
    600 		nifa = TAILQ_NEXT(ifa, ifa_list);
    601 		if (ifa->ifa_addr->sa_family != AF_INET)
    602 			continue;
    603 		in_purgeaddr(ifa, ifp);
    604 	}
    605 	in_purgemkludge(ifp);
    606 }
    607 
    608 /*
    609  * SIOC[GAD]LIFADDR.
    610  *	SIOCGLIFADDR: get first address. (???)
    611  *	SIOCGLIFADDR with IFLR_PREFIX:
    612  *		get first address that matches the specified prefix.
    613  *	SIOCALIFADDR: add the specified address.
    614  *	SIOCALIFADDR with IFLR_PREFIX:
    615  *		EINVAL since we can't deduce hostid part of the address.
    616  *	SIOCDLIFADDR: delete the specified address.
    617  *	SIOCDLIFADDR with IFLR_PREFIX:
    618  *		delete the first address that matches the specified prefix.
    619  * return values:
    620  *	EINVAL on invalid parameters
    621  *	EADDRNOTAVAIL on prefix match failed/specified address not found
    622  *	other values may be returned from in_ioctl()
    623  */
    624 static int
    625 in_lifaddr_ioctl(so, cmd, data, ifp, p)
    626 	struct socket *so;
    627 	u_long cmd;
    628 	caddr_t	data;
    629 	struct ifnet *ifp;
    630 	struct proc *p;
    631 {
    632 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
    633 	struct ifaddr *ifa;
    634 	struct sockaddr *sa;
    635 
    636 	/* sanity checks */
    637 	if (!data || !ifp) {
    638 		panic("invalid argument to in_lifaddr_ioctl");
    639 		/*NOTRECHED*/
    640 	}
    641 
    642 	switch (cmd) {
    643 	case SIOCGLIFADDR:
    644 		/* address must be specified on GET with IFLR_PREFIX */
    645 		if ((iflr->flags & IFLR_PREFIX) == 0)
    646 			break;
    647 		/*FALLTHROUGH*/
    648 	case SIOCALIFADDR:
    649 	case SIOCDLIFADDR:
    650 		/* address must be specified on ADD and DELETE */
    651 		sa = (struct sockaddr *)&iflr->addr;
    652 		if (sa->sa_family != AF_INET)
    653 			return EINVAL;
    654 		if (sa->sa_len != sizeof(struct sockaddr_in))
    655 			return EINVAL;
    656 		/* XXX need improvement */
    657 		sa = (struct sockaddr *)&iflr->dstaddr;
    658 		if (sa->sa_family
    659 		 && sa->sa_family != AF_INET)
    660 			return EINVAL;
    661 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in))
    662 			return EINVAL;
    663 		break;
    664 	default: /*shouldn't happen*/
    665 #if 0
    666 		panic("invalid cmd to in_lifaddr_ioctl");
    667 		/*NOTREACHED*/
    668 #else
    669 		return EOPNOTSUPP;
    670 #endif
    671 	}
    672 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
    673 		return EINVAL;
    674 
    675 	switch (cmd) {
    676 	case SIOCALIFADDR:
    677 	    {
    678 		struct in_aliasreq ifra;
    679 
    680 		if (iflr->flags & IFLR_PREFIX)
    681 			return EINVAL;
    682 
    683 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
    684 		bzero(&ifra, sizeof(ifra));
    685 		bcopy(iflr->iflr_name, ifra.ifra_name,
    686 			sizeof(ifra.ifra_name));
    687 
    688 		bcopy(&iflr->addr, &ifra.ifra_addr,
    689 			((struct sockaddr *)&iflr->addr)->sa_len);
    690 
    691 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
    692 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
    693 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
    694 		}
    695 
    696 		ifra.ifra_mask.sin_family = AF_INET;
    697 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
    698 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
    699 
    700 		return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, p);
    701 	    }
    702 	case SIOCGLIFADDR:
    703 	case SIOCDLIFADDR:
    704 	    {
    705 		struct in_ifaddr *ia;
    706 		struct in_addr mask, candidate, match;
    707 		struct sockaddr_in *sin;
    708 		int cmp;
    709 
    710 		bzero(&mask, sizeof(mask));
    711 		if (iflr->flags & IFLR_PREFIX) {
    712 			/* lookup a prefix rather than address. */
    713 			in_len2mask(&mask, iflr->prefixlen);
    714 
    715 			sin = (struct sockaddr_in *)&iflr->addr;
    716 			match.s_addr = sin->sin_addr.s_addr;
    717 			match.s_addr &= mask.s_addr;
    718 
    719 			/* if you set extra bits, that's wrong */
    720 			if (match.s_addr != sin->sin_addr.s_addr)
    721 				return EINVAL;
    722 
    723 			cmp = 1;
    724 		} else {
    725 			if (cmd == SIOCGLIFADDR) {
    726 				/* on getting an address, take the 1st match */
    727 				cmp = 0;	/*XXX*/
    728 			} else {
    729 				/* on deleting an address, do exact match */
    730 				in_len2mask(&mask, 32);
    731 				sin = (struct sockaddr_in *)&iflr->addr;
    732 				match.s_addr = sin->sin_addr.s_addr;
    733 
    734 				cmp = 1;
    735 			}
    736 		}
    737 
    738 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    739 			if (ifa->ifa_addr->sa_family != AF_INET6)
    740 				continue;
    741 			if (!cmp)
    742 				break;
    743 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
    744 			candidate.s_addr &= mask.s_addr;
    745 			if (candidate.s_addr == match.s_addr)
    746 				break;
    747 		}
    748 		if (!ifa)
    749 			return EADDRNOTAVAIL;
    750 		ia = (struct in_ifaddr *)ifa;
    751 
    752 		if (cmd == SIOCGLIFADDR) {
    753 			/* fill in the if_laddrreq structure */
    754 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
    755 
    756 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
    757 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
    758 					ia->ia_dstaddr.sin_len);
    759 			} else
    760 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
    761 
    762 			iflr->prefixlen =
    763 				in_mask2len(&ia->ia_sockmask.sin_addr);
    764 
    765 			iflr->flags = 0;	/*XXX*/
    766 
    767 			return 0;
    768 		} else {
    769 			struct in_aliasreq ifra;
    770 
    771 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
    772 			bzero(&ifra, sizeof(ifra));
    773 			bcopy(iflr->iflr_name, ifra.ifra_name,
    774 				sizeof(ifra.ifra_name));
    775 
    776 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
    777 				ia->ia_addr.sin_len);
    778 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
    779 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
    780 					ia->ia_dstaddr.sin_len);
    781 			}
    782 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
    783 				ia->ia_sockmask.sin_len);
    784 
    785 			return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
    786 				ifp, p);
    787 		}
    788 	    }
    789 	}
    790 
    791 	return EOPNOTSUPP;	/*just for safety*/
    792 }
    793 
    794 /*
    795  * Delete any existing route for an interface.
    796  */
    797 void
    798 in_ifscrub(ifp, ia)
    799 	struct ifnet *ifp;
    800 	struct in_ifaddr *ia;
    801 {
    802 
    803 	in_scrubprefix(ia);
    804 }
    805 
    806 /*
    807  * Initialize an interface's internet address
    808  * and routing table entry.
    809  */
    810 int
    811 in_ifinit(ifp, ia, sin, scrub)
    812 	struct ifnet *ifp;
    813 	struct in_ifaddr *ia;
    814 	struct sockaddr_in *sin;
    815 	int scrub;
    816 {
    817 	u_int32_t i = sin->sin_addr.s_addr;
    818 	struct sockaddr_in oldaddr;
    819 	int s = splnet(), flags = RTF_UP, error;
    820 
    821 	/*
    822 	 * Set up new addresses.
    823 	 */
    824 	oldaddr = ia->ia_addr;
    825 	if (ia->ia_addr.sin_family == AF_INET)
    826 		LIST_REMOVE(ia, ia_hash);
    827 	ia->ia_addr = *sin;
    828 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
    829 
    830 	/*
    831 	 * Give the interface a chance to initialize
    832 	 * if this is its first address,
    833 	 * and to validate the address if necessary.
    834 	 */
    835 	if (ifp->if_ioctl &&
    836 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
    837 		goto bad;
    838 	splx(s);
    839 	if (scrub) {
    840 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
    841 		in_ifscrub(ifp, ia);
    842 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    843 	}
    844 
    845 	if (IN_CLASSA(i))
    846 		ia->ia_netmask = IN_CLASSA_NET;
    847 	else if (IN_CLASSB(i))
    848 		ia->ia_netmask = IN_CLASSB_NET;
    849 	else
    850 		ia->ia_netmask = IN_CLASSC_NET;
    851 	/*
    852 	 * The subnet mask usually includes at least the standard network part,
    853 	 * but may may be smaller in the case of supernetting.
    854 	 * If it is set, we believe it.
    855 	 */
    856 	if (ia->ia_subnetmask == 0) {
    857 		ia->ia_subnetmask = ia->ia_netmask;
    858 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
    859 	} else
    860 		ia->ia_netmask &= ia->ia_subnetmask;
    861 
    862 	ia->ia_net = i & ia->ia_netmask;
    863 	ia->ia_subnet = i & ia->ia_subnetmask;
    864 	in_socktrim(&ia->ia_sockmask);
    865 	/* re-calculate the "in_maxmtu" value */
    866 	in_setmaxmtu();
    867 	/*
    868 	 * Add route for the network.
    869 	 */
    870 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    871 	if (ifp->if_flags & IFF_BROADCAST) {
    872 		ia->ia_broadaddr.sin_addr.s_addr =
    873 			ia->ia_subnet | ~ia->ia_subnetmask;
    874 		ia->ia_netbroadcast.s_addr =
    875 			ia->ia_net | ~ia->ia_netmask;
    876 	} else if (ifp->if_flags & IFF_LOOPBACK) {
    877 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
    878 		flags |= RTF_HOST;
    879 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
    880 		if (ia->ia_dstaddr.sin_family != AF_INET)
    881 			return (0);
    882 		flags |= RTF_HOST;
    883 	}
    884 	error = in_addprefix(ia, flags);
    885 	/*
    886 	 * recover multicast kludge entry, if there is.
    887 	 */
    888 	if (ifp->if_flags & IFF_MULTICAST)
    889 		in_restoremkludge(ia, ifp);
    890 	/*
    891 	 * If the interface supports multicast, join the "all hosts"
    892 	 * multicast group on that interface.
    893 	 */
    894 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
    895 		struct in_addr addr;
    896 
    897 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
    898 		ia->ia_allhosts = in_addmulti(&addr, ifp);
    899 	}
    900 	return (error);
    901 bad:
    902 	splx(s);
    903 	LIST_REMOVE(ia, ia_hash);
    904 	ia->ia_addr = oldaddr;
    905 	if (ia->ia_addr.sin_family == AF_INET)
    906 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
    907 		    ia, ia_hash);
    908 	return (error);
    909 }
    910 
    911 #define rtinitflags(x) \
    912 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
    913 	    ? RTF_HOST : 0)
    914 
    915 /*
    916  * add a route to prefix ("connected route" in cisco terminology).
    917  * does nothing if there's some interface address with the same prefix already.
    918  */
    919 static int
    920 in_addprefix(target, flags)
    921 	struct in_ifaddr *target;
    922 	int flags;
    923 {
    924 	struct in_ifaddr *ia;
    925 	struct in_addr prefix, mask, p;
    926 	int error;
    927 
    928 	if ((flags & RTF_HOST) != 0)
    929 		prefix = target->ia_dstaddr.sin_addr;
    930 	else
    931 		prefix = target->ia_addr.sin_addr;
    932 	mask = target->ia_sockmask.sin_addr;
    933 	prefix.s_addr &= mask.s_addr;
    934 
    935 	TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
    936 		/* easy one first */
    937 		if (mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
    938 			continue;
    939 
    940 		if (rtinitflags(ia))
    941 			p = ia->ia_dstaddr.sin_addr;
    942 		else
    943 			p = ia->ia_addr.sin_addr;
    944 		p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
    945 		if (prefix.s_addr != p.s_addr)
    946 			continue;
    947 
    948 		/*
    949 		 * if we got a matching prefix route inserted by other
    950 		 * interface address, we don't need to bother
    951 		 */
    952 		if (ia->ia_flags & IFA_ROUTE)
    953 			return 0;
    954 	}
    955 
    956 	/*
    957 	 * noone seem to have prefix route.  insert it.
    958 	 */
    959 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
    960 	if (!error)
    961 		target->ia_flags |= IFA_ROUTE;
    962 	return error;
    963 }
    964 
    965 /*
    966  * remove a route to prefix ("connected route" in cisco terminology).
    967  * re-installs the route by using another interface address, if there's one
    968  * with the same prefix (otherwise we lose the route mistakenly).
    969  */
    970 static int
    971 in_scrubprefix(target)
    972 	struct in_ifaddr *target;
    973 {
    974 	struct in_ifaddr *ia;
    975 	struct in_addr prefix, mask, p;
    976 	int error;
    977 
    978 	if ((target->ia_flags & IFA_ROUTE) == 0)
    979 		return 0;
    980 
    981 	if (rtinitflags(target))
    982 		prefix = target->ia_dstaddr.sin_addr;
    983 	else
    984 		prefix = target->ia_addr.sin_addr;
    985 	mask = target->ia_sockmask.sin_addr;
    986 	prefix.s_addr &= mask.s_addr;
    987 
    988 	TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
    989 		/* easy one first */
    990 		if (mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
    991 			continue;
    992 
    993 		if (rtinitflags(ia))
    994 			p = ia->ia_dstaddr.sin_addr;
    995 		else
    996 			p = ia->ia_addr.sin_addr;
    997 		p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
    998 		if (prefix.s_addr != p.s_addr)
    999 			continue;
   1000 
   1001 		/*
   1002 		 * if we got a matching prefix route, move IFA_ROUTE to him
   1003 		 */
   1004 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
   1005 			rtinit(&(target->ia_ifa), (int)RTM_DELETE,
   1006 			    rtinitflags(target));
   1007 			target->ia_flags &= ~IFA_ROUTE;
   1008 
   1009 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
   1010 			    rtinitflags(ia) | RTF_UP);
   1011 			if (error == 0)
   1012 				ia->ia_flags |= IFA_ROUTE;
   1013 			return error;
   1014 		}
   1015 	}
   1016 
   1017 	/*
   1018 	 * noone seem to have prefix route.  remove it.
   1019 	 */
   1020 	rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
   1021 	target->ia_flags &= ~IFA_ROUTE;
   1022 	return 0;
   1023 }
   1024 
   1025 #undef rtinitflags
   1026 
   1027 /*
   1028  * Return 1 if the address might be a local broadcast address.
   1029  */
   1030 int
   1031 in_broadcast(in, ifp)
   1032 	struct in_addr in;
   1033 	struct ifnet *ifp;
   1034 {
   1035 	struct ifaddr *ifa;
   1036 
   1037 	if (in.s_addr == INADDR_BROADCAST ||
   1038 	    in_nullhost(in))
   1039 		return 1;
   1040 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
   1041 		return 0;
   1042 	/*
   1043 	 * Look through the list of addresses for a match
   1044 	 * with a broadcast address.
   1045 	 */
   1046 #define ia (ifatoia(ifa))
   1047 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
   1048 		if (ifa->ifa_addr->sa_family == AF_INET &&
   1049 		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
   1050 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
   1051 		     in_hosteq(in, ia->ia_netbroadcast) ||
   1052 		     (hostzeroisbroadcast &&
   1053 		      /*
   1054 		       * Check for old-style (host 0) broadcast.
   1055 		       */
   1056 		      (in.s_addr == ia->ia_subnet ||
   1057 		       in.s_addr == ia->ia_net))))
   1058 			return 1;
   1059 	return (0);
   1060 #undef ia
   1061 }
   1062 
   1063 /*
   1064  * Multicast address kludge:
   1065  * If there were any multicast addresses attached to this interface address,
   1066  * either move them to another address on this interface, or save them until
   1067  * such time as this interface is reconfigured for IPv4.
   1068  */
   1069 void
   1070 in_savemkludge(oia)
   1071 	struct in_ifaddr *oia;
   1072 {
   1073 	struct in_ifaddr *ia;
   1074 	struct in_multi *inm, *next;
   1075 
   1076 	IFP_TO_IA(oia->ia_ifp, ia);
   1077 	if (ia) {	/* there is another address */
   1078 		for (inm = LIST_FIRST(&oia->ia_multiaddrs); inm; inm = next){
   1079 			next = LIST_NEXT(inm, inm_list);
   1080 			LIST_REMOVE(inm, inm_list);
   1081 			IFAFREE(&inm->inm_ia->ia_ifa);
   1082 			IFAREF(&ia->ia_ifa);
   1083 			inm->inm_ia = ia;
   1084 			LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
   1085 		}
   1086 	} else {	/* last address on this if deleted, save */
   1087 		TAILQ_INSERT_TAIL(&in_mk, oia, ia_list);
   1088 		IFAREF(&oia->ia_ifa);
   1089 	}
   1090 }
   1091 
   1092 /*
   1093  * Continuation of multicast address hack:
   1094  * If there was a multicast group list previously saved for this interface,
   1095  * then we re-attach it to the first address configured on the i/f.
   1096  */
   1097 void
   1098 in_restoremkludge(ia, ifp)
   1099 	struct in_ifaddr *ia;
   1100 	struct ifnet *ifp;
   1101 {
   1102 	struct in_ifaddr *oia;
   1103 
   1104 	for (oia = TAILQ_FIRST(&in_mk); oia != NULL;
   1105 	    oia = TAILQ_NEXT(oia, ia_list)) {
   1106 		if (oia->ia_ifp == ifp) {
   1107 			struct in_multi *inm, *next;
   1108 
   1109 			for (inm = LIST_FIRST(&oia->ia_multiaddrs);
   1110 			    inm != NULL; inm = next) {
   1111 				next = LIST_NEXT(inm, inm_list);
   1112 				LIST_REMOVE(inm, inm_list);
   1113 				IFAFREE(&inm->inm_ia->ia_ifa);
   1114 				IFAREF(&ia->ia_ifa);
   1115 				inm->inm_ia = ia;
   1116 				LIST_INSERT_HEAD(&ia->ia_multiaddrs,
   1117 				    inm, inm_list);
   1118 			}
   1119 	    		TAILQ_REMOVE(&in_mk, oia, ia_list);
   1120 			IFAFREE(&oia->ia_ifa);
   1121 			break;
   1122 		}
   1123 	}
   1124 }
   1125 
   1126 void
   1127 in_purgemkludge(ifp)
   1128 	struct ifnet *ifp;
   1129 {
   1130 	struct in_ifaddr *oia;
   1131 
   1132 	for (oia = TAILQ_FIRST(&in_mk); oia != NULL;
   1133 	    oia = TAILQ_NEXT(oia, ia_list)) {
   1134 		if (oia->ia_ifp != ifp)
   1135 			continue;
   1136 
   1137 		/*
   1138 		 * Leaving from all multicast groups joined through
   1139 		 * this interface is done via in_pcbpurgeif().
   1140 		 */
   1141 
   1142 	    	TAILQ_REMOVE(&in_mk, oia, ia_list);
   1143 		IFAFREE(&oia->ia_ifa);
   1144 		break;
   1145 	}
   1146 }
   1147 
   1148 /*
   1149  * Add an address to the list of IP multicast addresses for a given interface.
   1150  */
   1151 struct in_multi *
   1152 in_addmulti(ap, ifp)
   1153 	struct in_addr *ap;
   1154 	struct ifnet *ifp;
   1155 {
   1156 	struct in_multi *inm;
   1157 	struct ifreq ifr;
   1158 	struct in_ifaddr *ia;
   1159 	int s = splsoftnet();
   1160 
   1161 	/*
   1162 	 * See if address already in list.
   1163 	 */
   1164 	IN_LOOKUP_MULTI(*ap, ifp, inm);
   1165 	if (inm != NULL) {
   1166 		/*
   1167 		 * Found it; just increment the reference count.
   1168 		 */
   1169 		++inm->inm_refcount;
   1170 	} else {
   1171 		/*
   1172 		 * New address; allocate a new multicast record
   1173 		 * and link it into the interface's multicast list.
   1174 		 */
   1175 		inm = (struct in_multi *)malloc(sizeof(*inm),
   1176 		    M_IPMADDR, M_NOWAIT);
   1177 		if (inm == NULL) {
   1178 			splx(s);
   1179 			return (NULL);
   1180 		}
   1181 		inm->inm_addr = *ap;
   1182 		inm->inm_ifp = ifp;
   1183 		inm->inm_refcount = 1;
   1184 		IFP_TO_IA(ifp, ia);
   1185 		if (ia == NULL) {
   1186 			free(inm, M_IPMADDR);
   1187 			splx(s);
   1188 			return (NULL);
   1189 		}
   1190 		inm->inm_ia = ia;
   1191 		IFAREF(&inm->inm_ia->ia_ifa);
   1192 		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
   1193 		/*
   1194 		 * Ask the network driver to update its multicast reception
   1195 		 * filter appropriately for the new address.
   1196 		 */
   1197 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
   1198 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
   1199 		satosin(&ifr.ifr_addr)->sin_addr = *ap;
   1200 		if ((ifp->if_ioctl == NULL) ||
   1201 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
   1202 			LIST_REMOVE(inm, inm_list);
   1203 			free(inm, M_IPMADDR);
   1204 			splx(s);
   1205 			return (NULL);
   1206 		}
   1207 		/*
   1208 		 * Let IGMP know that we have joined a new IP multicast group.
   1209 		 */
   1210 		igmp_joingroup(inm);
   1211 	}
   1212 	splx(s);
   1213 	return (inm);
   1214 }
   1215 
   1216 /*
   1217  * Delete a multicast address record.
   1218  */
   1219 void
   1220 in_delmulti(inm)
   1221 	struct in_multi *inm;
   1222 {
   1223 	struct ifreq ifr;
   1224 	int s = splsoftnet();
   1225 
   1226 	if (--inm->inm_refcount == 0) {
   1227 		/*
   1228 		 * No remaining claims to this record; let IGMP know that
   1229 		 * we are leaving the multicast group.
   1230 		 */
   1231 		igmp_leavegroup(inm);
   1232 		/*
   1233 		 * Unlink from list.
   1234 		 */
   1235 		LIST_REMOVE(inm, inm_list);
   1236 		IFAFREE(&inm->inm_ia->ia_ifa);
   1237 		/*
   1238 		 * Notify the network driver to update its multicast reception
   1239 		 * filter.
   1240 		 */
   1241 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
   1242 		satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
   1243 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
   1244 							     (caddr_t)&ifr);
   1245 		free(inm, M_IPMADDR);
   1246 	}
   1247 	splx(s);
   1248 }
   1249 #endif
   1250