Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.72
      1 /*	$NetBSD: in.c,v 1.72 2002/02/21 21:59:16 christos 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.72 2002/02/21 21:59:16 christos 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 		} else {
    559 			IFAFREE(&ia->ia_ifa);
    560 		}
    561 	}
    562 	return 0;
    563 }
    564 
    565 void
    566 in_purgeaddr(ifa, ifp)
    567 	struct ifaddr *ifa;
    568 	struct ifnet *ifp;
    569 {
    570 	struct in_ifaddr *ia = (void *) ifa;
    571 	struct radix_node_head *rnh;
    572 
    573 	in_ifscrub(ifp, ia);
    574 	LIST_REMOVE(ia, ia_hash);
    575 	TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
    576 	IFAFREE(&ia->ia_ifa);
    577 	TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
    578 	if (ia->ia_allhosts != NULL)
    579 		in_delmulti(ia->ia_allhosts);
    580 	if (LIST_FIRST(&ia->ia_multiaddrs) != NULL &&
    581 	    /*
    582 	     * If the interface is going away, don't bother to save
    583 	     * the multicast entries.
    584 	     */
    585 	    ifp->if_output != if_nulloutput)
    586 		in_savemkludge(ia);
    587 	IFAFREE(&ia->ia_ifa);
    588 
    589 	if ((rnh = rt_tables[AF_INET]) != NULL)
    590 		(*rnh->rnh_walktree)(rnh, in_rt_walktree, ifa);
    591 
    592 	in_setmaxmtu();
    593 }
    594 
    595 void
    596 in_purgeif(ifp)
    597 	struct ifnet *ifp;
    598 {
    599 	struct ifaddr *ifa, *nifa;
    600 
    601 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
    602 		nifa = TAILQ_NEXT(ifa, ifa_list);
    603 		if (ifa->ifa_addr->sa_family != AF_INET)
    604 			continue;
    605 		in_purgeaddr(ifa, ifp);
    606 	}
    607 	in_purgemkludge(ifp);
    608 }
    609 
    610 /*
    611  * SIOC[GAD]LIFADDR.
    612  *	SIOCGLIFADDR: get first address. (???)
    613  *	SIOCGLIFADDR with IFLR_PREFIX:
    614  *		get first address that matches the specified prefix.
    615  *	SIOCALIFADDR: add the specified address.
    616  *	SIOCALIFADDR with IFLR_PREFIX:
    617  *		EINVAL since we can't deduce hostid part of the address.
    618  *	SIOCDLIFADDR: delete the specified address.
    619  *	SIOCDLIFADDR with IFLR_PREFIX:
    620  *		delete the first address that matches the specified prefix.
    621  * return values:
    622  *	EINVAL on invalid parameters
    623  *	EADDRNOTAVAIL on prefix match failed/specified address not found
    624  *	other values may be returned from in_ioctl()
    625  */
    626 static int
    627 in_lifaddr_ioctl(so, cmd, data, ifp, p)
    628 	struct socket *so;
    629 	u_long cmd;
    630 	caddr_t	data;
    631 	struct ifnet *ifp;
    632 	struct proc *p;
    633 {
    634 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
    635 	struct ifaddr *ifa;
    636 	struct sockaddr *sa;
    637 
    638 	/* sanity checks */
    639 	if (!data || !ifp) {
    640 		panic("invalid argument to in_lifaddr_ioctl");
    641 		/*NOTRECHED*/
    642 	}
    643 
    644 	switch (cmd) {
    645 	case SIOCGLIFADDR:
    646 		/* address must be specified on GET with IFLR_PREFIX */
    647 		if ((iflr->flags & IFLR_PREFIX) == 0)
    648 			break;
    649 		/*FALLTHROUGH*/
    650 	case SIOCALIFADDR:
    651 	case SIOCDLIFADDR:
    652 		/* address must be specified on ADD and DELETE */
    653 		sa = (struct sockaddr *)&iflr->addr;
    654 		if (sa->sa_family != AF_INET)
    655 			return EINVAL;
    656 		if (sa->sa_len != sizeof(struct sockaddr_in))
    657 			return EINVAL;
    658 		/* XXX need improvement */
    659 		sa = (struct sockaddr *)&iflr->dstaddr;
    660 		if (sa->sa_family
    661 		 && sa->sa_family != AF_INET)
    662 			return EINVAL;
    663 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in))
    664 			return EINVAL;
    665 		break;
    666 	default: /*shouldn't happen*/
    667 #if 0
    668 		panic("invalid cmd to in_lifaddr_ioctl");
    669 		/*NOTREACHED*/
    670 #else
    671 		return EOPNOTSUPP;
    672 #endif
    673 	}
    674 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
    675 		return EINVAL;
    676 
    677 	switch (cmd) {
    678 	case SIOCALIFADDR:
    679 	    {
    680 		struct in_aliasreq ifra;
    681 
    682 		if (iflr->flags & IFLR_PREFIX)
    683 			return EINVAL;
    684 
    685 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
    686 		bzero(&ifra, sizeof(ifra));
    687 		bcopy(iflr->iflr_name, ifra.ifra_name,
    688 			sizeof(ifra.ifra_name));
    689 
    690 		bcopy(&iflr->addr, &ifra.ifra_addr,
    691 			((struct sockaddr *)&iflr->addr)->sa_len);
    692 
    693 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
    694 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
    695 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
    696 		}
    697 
    698 		ifra.ifra_mask.sin_family = AF_INET;
    699 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
    700 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
    701 
    702 		return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, p);
    703 	    }
    704 	case SIOCGLIFADDR:
    705 	case SIOCDLIFADDR:
    706 	    {
    707 		struct in_ifaddr *ia;
    708 		struct in_addr mask, candidate, match;
    709 		struct sockaddr_in *sin;
    710 		int cmp;
    711 
    712 		bzero(&mask, sizeof(mask));
    713 		if (iflr->flags & IFLR_PREFIX) {
    714 			/* lookup a prefix rather than address. */
    715 			in_len2mask(&mask, iflr->prefixlen);
    716 
    717 			sin = (struct sockaddr_in *)&iflr->addr;
    718 			match.s_addr = sin->sin_addr.s_addr;
    719 			match.s_addr &= mask.s_addr;
    720 
    721 			/* if you set extra bits, that's wrong */
    722 			if (match.s_addr != sin->sin_addr.s_addr)
    723 				return EINVAL;
    724 
    725 			cmp = 1;
    726 		} else {
    727 			if (cmd == SIOCGLIFADDR) {
    728 				/* on getting an address, take the 1st match */
    729 				cmp = 0;	/*XXX*/
    730 			} else {
    731 				/* on deleting an address, do exact match */
    732 				in_len2mask(&mask, 32);
    733 				sin = (struct sockaddr_in *)&iflr->addr;
    734 				match.s_addr = sin->sin_addr.s_addr;
    735 
    736 				cmp = 1;
    737 			}
    738 		}
    739 
    740 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
    741 			if (ifa->ifa_addr->sa_family != AF_INET6)
    742 				continue;
    743 			if (!cmp)
    744 				break;
    745 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
    746 			candidate.s_addr &= mask.s_addr;
    747 			if (candidate.s_addr == match.s_addr)
    748 				break;
    749 		}
    750 		if (!ifa)
    751 			return EADDRNOTAVAIL;
    752 		ia = (struct in_ifaddr *)ifa;
    753 
    754 		if (cmd == SIOCGLIFADDR) {
    755 			/* fill in the if_laddrreq structure */
    756 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
    757 
    758 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
    759 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
    760 					ia->ia_dstaddr.sin_len);
    761 			} else
    762 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
    763 
    764 			iflr->prefixlen =
    765 				in_mask2len(&ia->ia_sockmask.sin_addr);
    766 
    767 			iflr->flags = 0;	/*XXX*/
    768 
    769 			return 0;
    770 		} else {
    771 			struct in_aliasreq ifra;
    772 
    773 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
    774 			bzero(&ifra, sizeof(ifra));
    775 			bcopy(iflr->iflr_name, ifra.ifra_name,
    776 				sizeof(ifra.ifra_name));
    777 
    778 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
    779 				ia->ia_addr.sin_len);
    780 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
    781 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
    782 					ia->ia_dstaddr.sin_len);
    783 			}
    784 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
    785 				ia->ia_sockmask.sin_len);
    786 
    787 			return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
    788 				ifp, p);
    789 		}
    790 	    }
    791 	}
    792 
    793 	return EOPNOTSUPP;	/*just for safety*/
    794 }
    795 
    796 /*
    797  * Delete any existing route for an interface.
    798  */
    799 void
    800 in_ifscrub(ifp, ia)
    801 	struct ifnet *ifp;
    802 	struct in_ifaddr *ia;
    803 {
    804 
    805 	in_scrubprefix(ia);
    806 }
    807 
    808 /*
    809  * Initialize an interface's internet address
    810  * and routing table entry.
    811  */
    812 int
    813 in_ifinit(ifp, ia, sin, scrub)
    814 	struct ifnet *ifp;
    815 	struct in_ifaddr *ia;
    816 	struct sockaddr_in *sin;
    817 	int scrub;
    818 {
    819 	u_int32_t i = sin->sin_addr.s_addr;
    820 	struct sockaddr_in oldaddr;
    821 	int s = splnet(), flags = RTF_UP, error;
    822 
    823 	/*
    824 	 * Set up new addresses.
    825 	 */
    826 	oldaddr = ia->ia_addr;
    827 	if (ia->ia_addr.sin_family == AF_INET)
    828 		LIST_REMOVE(ia, ia_hash);
    829 	ia->ia_addr = *sin;
    830 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
    831 
    832 	/*
    833 	 * Give the interface a chance to initialize
    834 	 * if this is its first address,
    835 	 * and to validate the address if necessary.
    836 	 */
    837 	if (ifp->if_ioctl &&
    838 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
    839 		goto bad;
    840 	splx(s);
    841 	if (scrub) {
    842 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
    843 		in_ifscrub(ifp, ia);
    844 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    845 	}
    846 
    847 	if (IN_CLASSA(i))
    848 		ia->ia_netmask = IN_CLASSA_NET;
    849 	else if (IN_CLASSB(i))
    850 		ia->ia_netmask = IN_CLASSB_NET;
    851 	else
    852 		ia->ia_netmask = IN_CLASSC_NET;
    853 	/*
    854 	 * The subnet mask usually includes at least the standard network part,
    855 	 * but may may be smaller in the case of supernetting.
    856 	 * If it is set, we believe it.
    857 	 */
    858 	if (ia->ia_subnetmask == 0) {
    859 		ia->ia_subnetmask = ia->ia_netmask;
    860 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
    861 	} else
    862 		ia->ia_netmask &= ia->ia_subnetmask;
    863 
    864 	ia->ia_net = i & ia->ia_netmask;
    865 	ia->ia_subnet = i & ia->ia_subnetmask;
    866 	in_socktrim(&ia->ia_sockmask);
    867 	/* re-calculate the "in_maxmtu" value */
    868 	in_setmaxmtu();
    869 	/*
    870 	 * Add route for the network.
    871 	 */
    872 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    873 	if (ifp->if_flags & IFF_BROADCAST) {
    874 		ia->ia_broadaddr.sin_addr.s_addr =
    875 			ia->ia_subnet | ~ia->ia_subnetmask;
    876 		ia->ia_netbroadcast.s_addr =
    877 			ia->ia_net | ~ia->ia_netmask;
    878 	} else if (ifp->if_flags & IFF_LOOPBACK) {
    879 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
    880 		flags |= RTF_HOST;
    881 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
    882 		if (ia->ia_dstaddr.sin_family != AF_INET)
    883 			return (0);
    884 		flags |= RTF_HOST;
    885 	}
    886 	error = in_addprefix(ia, flags);
    887 	/*
    888 	 * recover multicast kludge entry, if there is.
    889 	 */
    890 	if (ifp->if_flags & IFF_MULTICAST)
    891 		in_restoremkludge(ia, ifp);
    892 	/*
    893 	 * If the interface supports multicast, join the "all hosts"
    894 	 * multicast group on that interface.
    895 	 */
    896 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
    897 		struct in_addr addr;
    898 
    899 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
    900 		ia->ia_allhosts = in_addmulti(&addr, ifp);
    901 	}
    902 	return (error);
    903 bad:
    904 	splx(s);
    905 	LIST_REMOVE(ia, ia_hash);
    906 	ia->ia_addr = oldaddr;
    907 	if (ia->ia_addr.sin_family == AF_INET)
    908 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
    909 		    ia, ia_hash);
    910 	return (error);
    911 }
    912 
    913 #define rtinitflags(x) \
    914 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
    915 	    ? RTF_HOST : 0)
    916 
    917 /*
    918  * add a route to prefix ("connected route" in cisco terminology).
    919  * does nothing if there's some interface address with the same prefix already.
    920  */
    921 static int
    922 in_addprefix(target, flags)
    923 	struct in_ifaddr *target;
    924 	int flags;
    925 {
    926 	struct in_ifaddr *ia;
    927 	struct in_addr prefix, mask, p;
    928 	int error;
    929 
    930 	if ((flags & RTF_HOST) != 0)
    931 		prefix = target->ia_dstaddr.sin_addr;
    932 	else
    933 		prefix = target->ia_addr.sin_addr;
    934 	mask = target->ia_sockmask.sin_addr;
    935 	prefix.s_addr &= mask.s_addr;
    936 
    937 	TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
    938 		/* easy one first */
    939 		if (mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
    940 			continue;
    941 
    942 		if (rtinitflags(ia))
    943 			p = ia->ia_dstaddr.sin_addr;
    944 		else
    945 			p = ia->ia_addr.sin_addr;
    946 		p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
    947 		if (prefix.s_addr != p.s_addr)
    948 			continue;
    949 
    950 		/*
    951 		 * if we got a matching prefix route inserted by other
    952 		 * interface address, we don't need to bother
    953 		 */
    954 		if (ia->ia_flags & IFA_ROUTE)
    955 			return 0;
    956 	}
    957 
    958 	/*
    959 	 * noone seem to have prefix route.  insert it.
    960 	 */
    961 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
    962 	if (!error)
    963 		target->ia_flags |= IFA_ROUTE;
    964 	return error;
    965 }
    966 
    967 /*
    968  * remove a route to prefix ("connected route" in cisco terminology).
    969  * re-installs the route by using another interface address, if there's one
    970  * with the same prefix (otherwise we lose the route mistakenly).
    971  */
    972 static int
    973 in_scrubprefix(target)
    974 	struct in_ifaddr *target;
    975 {
    976 	struct in_ifaddr *ia;
    977 	struct in_addr prefix, mask, p;
    978 	int error;
    979 
    980 	if ((target->ia_flags & IFA_ROUTE) == 0)
    981 		return 0;
    982 
    983 	if (rtinitflags(target))
    984 		prefix = target->ia_dstaddr.sin_addr;
    985 	else
    986 		prefix = target->ia_addr.sin_addr;
    987 	mask = target->ia_sockmask.sin_addr;
    988 	prefix.s_addr &= mask.s_addr;
    989 
    990 	TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
    991 		/* easy one first */
    992 		if (mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
    993 			continue;
    994 
    995 		if (rtinitflags(ia))
    996 			p = ia->ia_dstaddr.sin_addr;
    997 		else
    998 			p = ia->ia_addr.sin_addr;
    999 		p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
   1000 		if (prefix.s_addr != p.s_addr)
   1001 			continue;
   1002 
   1003 		/*
   1004 		 * if we got a matching prefix route, move IFA_ROUTE to him
   1005 		 */
   1006 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
   1007 			rtinit(&(target->ia_ifa), (int)RTM_DELETE,
   1008 			    rtinitflags(target));
   1009 			target->ia_flags &= ~IFA_ROUTE;
   1010 
   1011 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
   1012 			    rtinitflags(ia) | RTF_UP);
   1013 			if (error == 0)
   1014 				ia->ia_flags |= IFA_ROUTE;
   1015 			return error;
   1016 		}
   1017 	}
   1018 
   1019 	/*
   1020 	 * noone seem to have prefix route.  remove it.
   1021 	 */
   1022 	rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
   1023 	target->ia_flags &= ~IFA_ROUTE;
   1024 	return 0;
   1025 }
   1026 
   1027 #undef rtinitflags
   1028 
   1029 /*
   1030  * Return 1 if the address might be a local broadcast address.
   1031  */
   1032 int
   1033 in_broadcast(in, ifp)
   1034 	struct in_addr in;
   1035 	struct ifnet *ifp;
   1036 {
   1037 	struct ifaddr *ifa;
   1038 
   1039 	if (in.s_addr == INADDR_BROADCAST ||
   1040 	    in_nullhost(in))
   1041 		return 1;
   1042 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
   1043 		return 0;
   1044 	/*
   1045 	 * Look through the list of addresses for a match
   1046 	 * with a broadcast address.
   1047 	 */
   1048 #define ia (ifatoia(ifa))
   1049 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
   1050 		if (ifa->ifa_addr->sa_family == AF_INET &&
   1051 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
   1052 		     in_hosteq(in, ia->ia_netbroadcast) ||
   1053 		     (hostzeroisbroadcast &&
   1054 		      /*
   1055 		       * Check for old-style (host 0) broadcast.
   1056 		       */
   1057 		      (in.s_addr == ia->ia_subnet ||
   1058 		       in.s_addr == ia->ia_net))))
   1059 			return 1;
   1060 	return (0);
   1061 #undef ia
   1062 }
   1063 
   1064 /*
   1065  * Multicast address kludge:
   1066  * If there were any multicast addresses attached to this interface address,
   1067  * either move them to another address on this interface, or save them until
   1068  * such time as this interface is reconfigured for IPv4.
   1069  */
   1070 void
   1071 in_savemkludge(oia)
   1072 	struct in_ifaddr *oia;
   1073 {
   1074 	struct in_ifaddr *ia;
   1075 	struct in_multi *inm, *next;
   1076 
   1077 	IFP_TO_IA(oia->ia_ifp, ia);
   1078 	if (ia) {	/* there is another address */
   1079 		for (inm = LIST_FIRST(&oia->ia_multiaddrs); inm; inm = next){
   1080 			next = LIST_NEXT(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 				IFAFREE(&inm->inm_ia->ia_ifa);
   1113 				IFAREF(&ia->ia_ifa);
   1114 				inm->inm_ia = ia;
   1115 				LIST_INSERT_HEAD(&ia->ia_multiaddrs,
   1116 				    inm, inm_list);
   1117 			}
   1118 	    		TAILQ_REMOVE(&in_mk, oia, ia_list);
   1119 			IFAFREE(&oia->ia_ifa);
   1120 			break;
   1121 		}
   1122 	}
   1123 }
   1124 
   1125 void
   1126 in_purgemkludge(ifp)
   1127 	struct ifnet *ifp;
   1128 {
   1129 	struct in_ifaddr *oia;
   1130 
   1131 	for (oia = TAILQ_FIRST(&in_mk); oia != NULL;
   1132 	    oia = TAILQ_NEXT(oia, ia_list)) {
   1133 		if (oia->ia_ifp != ifp)
   1134 			continue;
   1135 
   1136 		/*
   1137 		 * Leaving from all multicast groups joined through
   1138 		 * this interface is done via in_pcbpurgeif().
   1139 		 */
   1140 
   1141 	    	TAILQ_REMOVE(&in_mk, oia, ia_list);
   1142 		IFAFREE(&oia->ia_ifa);
   1143 		break;
   1144 	}
   1145 }
   1146 
   1147 /*
   1148  * Add an address to the list of IP multicast addresses for a given interface.
   1149  */
   1150 struct in_multi *
   1151 in_addmulti(ap, ifp)
   1152 	struct in_addr *ap;
   1153 	struct ifnet *ifp;
   1154 {
   1155 	struct in_multi *inm;
   1156 	struct ifreq ifr;
   1157 	struct in_ifaddr *ia;
   1158 	int s = splsoftnet();
   1159 
   1160 	/*
   1161 	 * See if address already in list.
   1162 	 */
   1163 	IN_LOOKUP_MULTI(*ap, ifp, inm);
   1164 	if (inm != NULL) {
   1165 		/*
   1166 		 * Found it; just increment the reference count.
   1167 		 */
   1168 		++inm->inm_refcount;
   1169 	} else {
   1170 		/*
   1171 		 * New address; allocate a new multicast record
   1172 		 * and link it into the interface's multicast list.
   1173 		 */
   1174 		inm = (struct in_multi *)malloc(sizeof(*inm),
   1175 		    M_IPMADDR, M_NOWAIT);
   1176 		if (inm == NULL) {
   1177 			splx(s);
   1178 			return (NULL);
   1179 		}
   1180 		inm->inm_addr = *ap;
   1181 		inm->inm_ifp = ifp;
   1182 		inm->inm_refcount = 1;
   1183 		IFP_TO_IA(ifp, ia);
   1184 		if (ia == NULL) {
   1185 			free(inm, M_IPMADDR);
   1186 			splx(s);
   1187 			return (NULL);
   1188 		}
   1189 		inm->inm_ia = ia;
   1190 		IFAREF(&inm->inm_ia->ia_ifa);
   1191 		LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
   1192 		/*
   1193 		 * Ask the network driver to update its multicast reception
   1194 		 * filter appropriately for the new address.
   1195 		 */
   1196 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
   1197 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
   1198 		satosin(&ifr.ifr_addr)->sin_addr = *ap;
   1199 		if ((ifp->if_ioctl == NULL) ||
   1200 		    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
   1201 			LIST_REMOVE(inm, inm_list);
   1202 			free(inm, M_IPMADDR);
   1203 			splx(s);
   1204 			return (NULL);
   1205 		}
   1206 		/*
   1207 		 * Let IGMP know that we have joined a new IP multicast group.
   1208 		 */
   1209 		igmp_joingroup(inm);
   1210 	}
   1211 	splx(s);
   1212 	return (inm);
   1213 }
   1214 
   1215 /*
   1216  * Delete a multicast address record.
   1217  */
   1218 void
   1219 in_delmulti(inm)
   1220 	struct in_multi *inm;
   1221 {
   1222 	struct ifreq ifr;
   1223 	int s = splsoftnet();
   1224 
   1225 	if (--inm->inm_refcount == 0) {
   1226 		/*
   1227 		 * No remaining claims to this record; let IGMP know that
   1228 		 * we are leaving the multicast group.
   1229 		 */
   1230 		igmp_leavegroup(inm);
   1231 		/*
   1232 		 * Unlink from list.
   1233 		 */
   1234 		LIST_REMOVE(inm, inm_list);
   1235 		IFAFREE(&inm->inm_ia->ia_ifa);
   1236 		/*
   1237 		 * Notify the network driver to update its multicast reception
   1238 		 * filter.
   1239 		 */
   1240 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
   1241 		satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
   1242 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
   1243 							     (caddr_t)&ifr);
   1244 		free(inm, M_IPMADDR);
   1245 	}
   1246 	splx(s);
   1247 }
   1248 #endif
   1249