Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.185
      1  1.185       roy /*	$NetBSD: in.c,v 1.185 2016/09/29 15:18:18 roy Exp $	*/
      2   1.48    itojun 
      3   1.48    itojun /*
      4   1.48    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5   1.48    itojun  * All rights reserved.
      6   1.77    itojun  *
      7   1.48    itojun  * Redistribution and use in source and binary forms, with or without
      8   1.48    itojun  * modification, are permitted provided that the following conditions
      9   1.48    itojun  * are met:
     10   1.48    itojun  * 1. Redistributions of source code must retain the above copyright
     11   1.48    itojun  *    notice, this list of conditions and the following disclaimer.
     12   1.48    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.48    itojun  *    notice, this list of conditions and the following disclaimer in the
     14   1.48    itojun  *    documentation and/or other materials provided with the distribution.
     15   1.48    itojun  * 3. Neither the name of the project nor the names of its contributors
     16   1.48    itojun  *    may be used to endorse or promote products derived from this software
     17   1.48    itojun  *    without specific prior written permission.
     18   1.77    itojun  *
     19   1.48    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20   1.48    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.48    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.48    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23   1.48    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.48    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.48    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.48    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.48    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.48    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.48    itojun  * SUCH DAMAGE.
     30   1.48    itojun  */
     31   1.14       cgd 
     32   1.46   thorpej /*-
     33   1.46   thorpej  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     34   1.46   thorpej  * All rights reserved.
     35   1.46   thorpej  *
     36   1.46   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     37   1.46   thorpej  * by Public Access Networks Corporation ("Panix").  It was developed under
     38   1.46   thorpej  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     39   1.46   thorpej  *
     40   1.46   thorpej  * Redistribution and use in source and binary forms, with or without
     41   1.46   thorpej  * modification, are permitted provided that the following conditions
     42   1.46   thorpej  * are met:
     43   1.46   thorpej  * 1. Redistributions of source code must retain the above copyright
     44   1.46   thorpej  *    notice, this list of conditions and the following disclaimer.
     45   1.46   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     46   1.46   thorpej  *    notice, this list of conditions and the following disclaimer in the
     47   1.46   thorpej  *    documentation and/or other materials provided with the distribution.
     48   1.46   thorpej  *
     49   1.46   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     50   1.46   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     51   1.46   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     52   1.46   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     53   1.46   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     54   1.46   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     55   1.46   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     56   1.46   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     57   1.46   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     58   1.46   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     59   1.46   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     60   1.46   thorpej  */
     61   1.46   thorpej 
     62    1.1       cgd /*
     63   1.12   mycroft  * Copyright (c) 1982, 1986, 1991, 1993
     64   1.12   mycroft  *	The Regents of the University of California.  All rights reserved.
     65    1.1       cgd  *
     66    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     67    1.1       cgd  * modification, are permitted provided that the following conditions
     68    1.1       cgd  * are met:
     69    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     70    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     71    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     72    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     73    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     74   1.90       agc  * 3. Neither the name of the University nor the names of its contributors
     75    1.1       cgd  *    may be used to endorse or promote products derived from this software
     76    1.1       cgd  *    without specific prior written permission.
     77    1.1       cgd  *
     78    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     79    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     80    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     81    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     82    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     83    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     84    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     85    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     86    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     87    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     88    1.1       cgd  * SUCH DAMAGE.
     89    1.1       cgd  *
     90   1.36   thorpej  *	@(#)in.c	8.4 (Berkeley) 1/9/95
     91    1.1       cgd  */
     92   1.71     lukem 
     93   1.71     lukem #include <sys/cdefs.h>
     94  1.185       roy __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.185 2016/09/29 15:18:18 roy Exp $");
     95   1.37    scottr 
     96  1.154     joerg #include "arp.h"
     97  1.157     pooka 
     98  1.157     pooka #ifdef _KERNEL_OPT
     99   1.41  jonathan #include "opt_inet.h"
    100   1.47  sommerfe #include "opt_inet_conf.h"
    101   1.37    scottr #include "opt_mrouting.h"
    102  1.157     pooka #endif
    103    1.1       cgd 
    104    1.6   mycroft #include <sys/param.h>
    105    1.6   mycroft #include <sys/ioctl.h>
    106   1.12   mycroft #include <sys/errno.h>
    107  1.152       roy #include <sys/kernel.h>
    108   1.12   mycroft #include <sys/malloc.h>
    109    1.6   mycroft #include <sys/socket.h>
    110    1.6   mycroft #include <sys/socketvar.h>
    111  1.114    dyoung #include <sys/sysctl.h>
    112   1.26  christos #include <sys/systm.h>
    113   1.27   mycroft #include <sys/proc.h>
    114   1.72  christos #include <sys/syslog.h>
    115  1.108      elad #include <sys/kauth.h>
    116  1.158     ozaki #include <sys/kmem.h>
    117    1.6   mycroft 
    118  1.141       tls #include <sys/cprng.h>
    119  1.141       tls 
    120    1.6   mycroft #include <net/if.h>
    121    1.6   mycroft #include <net/route.h>
    122  1.144     rmind #include <net/pfil.h>
    123    1.6   mycroft 
    124  1.159     ozaki #include <net/if_arp.h>
    125   1.34        is #include <net/if_ether.h>
    126  1.158     ozaki #include <net/if_types.h>
    127  1.158     ozaki #include <net/if_llatbl.h>
    128  1.158     ozaki #include <net/if_dl.h>
    129   1.34        is 
    130   1.12   mycroft #include <netinet/in_systm.h>
    131    1.6   mycroft #include <netinet/in.h>
    132    1.6   mycroft #include <netinet/in_var.h>
    133   1.84      matt #include <netinet/ip.h>
    134   1.84      matt #include <netinet/ip_var.h>
    135  1.114    dyoung #include <netinet/in_ifattach.h>
    136   1.84      matt #include <netinet/in_pcb.h>
    137  1.158     ozaki #include <netinet/in_selsrc.h>
    138   1.34        is #include <netinet/if_inarp.h>
    139   1.19   mycroft #include <netinet/ip_mroute.h>
    140   1.26  christos #include <netinet/igmp_var.h>
    141   1.13    chopps 
    142  1.114    dyoung #ifdef IPSELSRC
    143  1.114    dyoung #include <netinet/in_selsrc.h>
    144  1.114    dyoung #endif
    145  1.114    dyoung 
    146  1.145     rmind static u_int	in_mask2len(struct in_addr *);
    147  1.145     rmind static void	in_len2mask(struct in_addr *, u_int);
    148  1.145     rmind static int	in_lifaddr_ioctl(struct socket *, u_long, void *,
    149  1.147       rtr 	struct ifnet *);
    150   1.48    itojun 
    151  1.145     rmind static int	in_addprefix(struct in_ifaddr *, int);
    152  1.183       roy static void	in_scrubaddr(struct in_ifaddr *);
    153  1.145     rmind static int	in_scrubprefix(struct in_ifaddr *);
    154  1.145     rmind static void	in_sysctl_init(struct sysctllog **);
    155   1.67    itojun 
    156    1.1       cgd #ifndef SUBNETSARELOCAL
    157    1.1       cgd #define	SUBNETSARELOCAL	1
    158    1.1       cgd #endif
    159   1.47  sommerfe 
    160   1.47  sommerfe #ifndef HOSTZEROBROADCAST
    161  1.166  christos #define HOSTZEROBROADCAST 0
    162   1.47  sommerfe #endif
    163   1.47  sommerfe 
    164  1.146     rmind /* Note: 61, 127, 251, 509, 1021, 2039 are good. */
    165  1.146     rmind #ifndef IN_MULTI_HASH_SIZE
    166  1.146     rmind #define IN_MULTI_HASH_SIZE	509
    167  1.146     rmind #endif
    168  1.146     rmind 
    169  1.145     rmind static int			subnetsarelocal = SUBNETSARELOCAL;
    170  1.145     rmind static int			hostzeroisbroadcast = HOSTZEROBROADCAST;
    171   1.30       mrg 
    172    1.1       cgd /*
    173   1.65     enami  * This list is used to keep track of in_multi chains which belong to
    174   1.65     enami  * deleted interface addresses.  We use in_ifaddr so that a chain head
    175   1.65     enami  * won't be deallocated until all multicast address record are deleted.
    176   1.64    itojun  */
    177  1.146     rmind 
    178  1.146     rmind LIST_HEAD(in_multihashhead, in_multi);		/* Type of the hash head */
    179  1.145     rmind 
    180  1.145     rmind static struct pool		inmulti_pool;
    181  1.145     rmind static u_int			in_multientries;
    182  1.146     rmind static struct in_multihashhead *in_multihashtbl;
    183  1.146     rmind static u_long			in_multihash;
    184  1.146     rmind static krwlock_t		in_multilock;
    185  1.146     rmind 
    186  1.146     rmind #define IN_MULTI_HASH(x, ifp) \
    187  1.146     rmind     (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE])
    188  1.145     rmind 
    189  1.171     ozaki /* XXX DEPRECATED. Keep them to avoid breaking kvm(3) users. */
    190  1.145     rmind struct in_ifaddrhashhead *	in_ifaddrhashtbl;
    191  1.145     rmind u_long				in_ifaddrhash;
    192  1.145     rmind struct in_ifaddrhead		in_ifaddrhead;
    193  1.178     ozaki static kmutex_t			in_ifaddr_lock;
    194  1.145     rmind 
    195  1.170     ozaki struct pslist_head *		in_ifaddrhashtbl_pslist;
    196  1.170     ozaki u_long				in_ifaddrhash_pslist;
    197  1.171     ozaki struct pslist_head		in_ifaddrhead_pslist;
    198  1.170     ozaki 
    199  1.145     rmind void
    200  1.145     rmind in_init(void)
    201  1.145     rmind {
    202  1.145     rmind 	pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
    203  1.145     rmind 	    NULL, IPL_SOFTNET);
    204  1.145     rmind 	TAILQ_INIT(&in_ifaddrhead);
    205  1.171     ozaki 	PSLIST_INIT(&in_ifaddrhead_pslist);
    206  1.145     rmind 
    207  1.145     rmind 	in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
    208  1.145     rmind 	    &in_ifaddrhash);
    209  1.178     ozaki 
    210  1.170     ozaki 	in_ifaddrhashtbl_pslist = hashinit(IN_IFADDR_HASH_SIZE, HASH_PSLIST,
    211  1.170     ozaki 	    true, &in_ifaddrhash_pslist);
    212  1.178     ozaki 	mutex_init(&in_ifaddr_lock, MUTEX_DEFAULT, IPL_NONE);
    213  1.178     ozaki 
    214  1.145     rmind 	in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
    215  1.145     rmind 	    &in_multihash);
    216  1.146     rmind 	rw_init(&in_multilock);
    217  1.145     rmind 
    218  1.145     rmind 	in_sysctl_init(NULL);
    219  1.145     rmind }
    220   1.64    itojun 
    221   1.64    itojun /*
    222    1.1       cgd  * Return 1 if an internet address is for a ``local'' host
    223    1.1       cgd  * (one to which we have a connection).  If subnetsarelocal
    224    1.1       cgd  * is true, this includes other subnets of the local net.
    225    1.1       cgd  * Otherwise, it includes only the directly-connected (sub)nets.
    226    1.1       cgd  */
    227    1.8   mycroft int
    228  1.103     perry in_localaddr(struct in_addr in)
    229    1.1       cgd {
    230   1.59  augustss 	struct in_ifaddr *ia;
    231  1.178     ozaki 	int localaddr = 0;
    232  1.178     ozaki 	int s = pserialize_read_enter();
    233    1.1       cgd 
    234    1.1       cgd 	if (subnetsarelocal) {
    235  1.171     ozaki 		IN_ADDRLIST_READER_FOREACH(ia) {
    236  1.178     ozaki 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net) {
    237  1.178     ozaki 				localaddr = 1;
    238  1.178     ozaki 				break;
    239  1.178     ozaki 			}
    240  1.171     ozaki 		}
    241    1.1       cgd 	} else {
    242  1.171     ozaki 		IN_ADDRLIST_READER_FOREACH(ia) {
    243  1.178     ozaki 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) {
    244  1.178     ozaki 				localaddr = 1;
    245  1.178     ozaki 				break;
    246  1.178     ozaki 			}
    247  1.171     ozaki 		}
    248    1.1       cgd 	}
    249  1.178     ozaki 	pserialize_read_exit(s);
    250  1.178     ozaki 
    251  1.178     ozaki 	return localaddr;
    252    1.1       cgd }
    253    1.1       cgd 
    254    1.1       cgd /*
    255    1.1       cgd  * Determine whether an IP address is in a reserved set of addresses
    256    1.1       cgd  * that may not be forwarded, or whether datagrams to that destination
    257    1.1       cgd  * may be forwarded.
    258    1.1       cgd  */
    259    1.8   mycroft int
    260  1.103     perry in_canforward(struct in_addr in)
    261    1.1       cgd {
    262   1.59  augustss 	u_int32_t net;
    263    1.1       cgd 
    264   1.20   mycroft 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
    265    1.1       cgd 		return (0);
    266   1.20   mycroft 	if (IN_CLASSA(in.s_addr)) {
    267   1.20   mycroft 		net = in.s_addr & IN_CLASSA_NET;
    268   1.20   mycroft 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
    269    1.1       cgd 			return (0);
    270    1.1       cgd 	}
    271    1.1       cgd 	return (1);
    272    1.1       cgd }
    273    1.1       cgd 
    274   1.12   mycroft /*
    275   1.12   mycroft  * Trim a mask in a sockaddr
    276   1.12   mycroft  */
    277   1.12   mycroft void
    278  1.103     perry in_socktrim(struct sockaddr_in *ap)
    279   1.12   mycroft {
    280   1.59  augustss 	char *cplim = (char *) &ap->sin_addr;
    281   1.59  augustss 	char *cp = (char *) (&ap->sin_addr + 1);
    282   1.12   mycroft 
    283   1.12   mycroft 	ap->sin_len = 0;
    284   1.15   mycroft 	while (--cp >= cplim)
    285   1.12   mycroft 		if (*cp) {
    286   1.12   mycroft 			(ap)->sin_len = cp - (char *) (ap) + 1;
    287   1.12   mycroft 			break;
    288   1.12   mycroft 		}
    289   1.40      matt }
    290   1.40      matt 
    291   1.40      matt /*
    292   1.40      matt  *  Routine to take an Internet address and convert into a
    293   1.40      matt  *  "dotted quad" representation for printing.
    294   1.40      matt  */
    295   1.40      matt const char *
    296  1.103     perry in_fmtaddr(struct in_addr addr)
    297   1.40      matt {
    298   1.40      matt 	static char buf[sizeof("123.456.789.123")];
    299   1.40      matt 
    300   1.40      matt 	addr.s_addr = ntohl(addr.s_addr);
    301   1.40      matt 
    302   1.94    itojun 	snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
    303   1.40      matt 		(addr.s_addr >> 24) & 0xFF,
    304   1.40      matt 		(addr.s_addr >> 16) & 0xFF,
    305   1.40      matt 		(addr.s_addr >>  8) & 0xFF,
    306   1.40      matt 		(addr.s_addr >>  0) & 0xFF);
    307   1.40      matt 	return buf;
    308   1.12   mycroft }
    309   1.12   mycroft 
    310   1.35   thorpej /*
    311   1.35   thorpej  * Maintain the "in_maxmtu" variable, which is the largest
    312   1.35   thorpej  * mtu for non-local interfaces with AF_INET addresses assigned
    313   1.35   thorpej  * to them that are up.
    314   1.35   thorpej  */
    315   1.35   thorpej unsigned long in_maxmtu;
    316   1.35   thorpej 
    317   1.35   thorpej void
    318  1.103     perry in_setmaxmtu(void)
    319   1.35   thorpej {
    320   1.59  augustss 	struct in_ifaddr *ia;
    321   1.59  augustss 	struct ifnet *ifp;
    322   1.35   thorpej 	unsigned long maxmtu = 0;
    323  1.178     ozaki 	int s = pserialize_read_enter();
    324   1.35   thorpej 
    325  1.171     ozaki 	IN_ADDRLIST_READER_FOREACH(ia) {
    326   1.35   thorpej 		if ((ifp = ia->ia_ifp) == 0)
    327   1.35   thorpej 			continue;
    328   1.35   thorpej 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
    329   1.35   thorpej 			continue;
    330   1.35   thorpej 		if (ifp->if_mtu > maxmtu)
    331   1.38       tls 			maxmtu = ifp->if_mtu;
    332   1.35   thorpej 	}
    333   1.35   thorpej 	if (maxmtu)
    334   1.35   thorpej 		in_maxmtu = maxmtu;
    335  1.178     ozaki 	pserialize_read_exit(s);
    336   1.35   thorpej }
    337   1.35   thorpej 
    338   1.82   thorpej static u_int
    339  1.103     perry in_mask2len(struct in_addr *mask)
    340   1.48    itojun {
    341   1.82   thorpej 	u_int x, y;
    342   1.48    itojun 	u_char *p;
    343   1.48    itojun 
    344   1.48    itojun 	p = (u_char *)mask;
    345   1.48    itojun 	for (x = 0; x < sizeof(*mask); x++) {
    346   1.48    itojun 		if (p[x] != 0xff)
    347   1.48    itojun 			break;
    348   1.48    itojun 	}
    349   1.48    itojun 	y = 0;
    350   1.48    itojun 	if (x < sizeof(*mask)) {
    351  1.124    dyoung 		for (y = 0; y < NBBY; y++) {
    352   1.48    itojun 			if ((p[x] & (0x80 >> y)) == 0)
    353   1.48    itojun 				break;
    354   1.48    itojun 		}
    355   1.48    itojun 	}
    356  1.124    dyoung 	return x * NBBY + y;
    357   1.48    itojun }
    358   1.48    itojun 
    359   1.48    itojun static void
    360  1.103     perry in_len2mask(struct in_addr *mask, u_int len)
    361   1.48    itojun {
    362   1.82   thorpej 	u_int i;
    363   1.48    itojun 	u_char *p;
    364   1.48    itojun 
    365   1.48    itojun 	p = (u_char *)mask;
    366  1.132    cegger 	memset(mask, 0, sizeof(*mask));
    367  1.124    dyoung 	for (i = 0; i < len / NBBY; i++)
    368   1.48    itojun 		p[i] = 0xff;
    369  1.124    dyoung 	if (len % NBBY)
    370  1.124    dyoung 		p[i] = (0xff00 >> (len % NBBY)) & 0xff;
    371   1.48    itojun }
    372   1.48    itojun 
    373    1.1       cgd /*
    374    1.1       cgd  * Generic internet control operations (ioctl's).
    375    1.1       cgd  * Ifp is 0 if not an interface-specific ioctl.
    376    1.1       cgd  */
    377    1.1       cgd /* ARGSUSED */
    378  1.177     ozaki static int
    379  1.177     ozaki in_control0(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
    380    1.1       cgd {
    381   1.59  augustss 	struct ifreq *ifr = (struct ifreq *)data;
    382  1.136    dyoung 	struct in_ifaddr *ia = NULL;
    383    1.1       cgd 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
    384  1.184       roy 	struct sockaddr_in oldaddr, *new_dstaddr;
    385    1.1       cgd 	int error, hostIsNew, maskIsNew;
    386  1.100      yamt 	int newifaddr = 0;
    387  1.169     ozaki 	bool run_hook = false;
    388  1.169     ozaki 	bool need_reinsert = false;
    389  1.178     ozaki 	struct psref psref;
    390  1.178     ozaki 	int bound;
    391   1.48    itojun 
    392   1.48    itojun 	switch (cmd) {
    393   1.48    itojun 	case SIOCALIFADDR:
    394   1.48    itojun 	case SIOCDLIFADDR:
    395  1.135    dyoung 	case SIOCGLIFADDR:
    396  1.135    dyoung 		if (ifp == NULL)
    397  1.135    dyoung 			return EINVAL;
    398  1.147       rtr 		return in_lifaddr_ioctl(so, cmd, data, ifp);
    399  1.135    dyoung 	case SIOCGIFADDRPREF:
    400  1.114    dyoung 	case SIOCSIFADDRPREF:
    401  1.117    dyoung 		if (ifp == NULL)
    402   1.48    itojun 			return EINVAL;
    403  1.147       rtr 		return ifaddrpref_ioctl(so, cmd, data, ifp);
    404   1.48    itojun 	}
    405   1.48    itojun 
    406  1.178     ozaki 	bound = curlwp_bind();
    407    1.1       cgd 	/*
    408    1.1       cgd 	 * Find address for this interface, if it exists.
    409    1.1       cgd 	 */
    410  1.117    dyoung 	if (ifp != NULL)
    411  1.178     ozaki 		ia = in_get_ia_from_ifp_psref(ifp, &psref);
    412    1.1       cgd 
    413  1.153       roy 	hostIsNew = 1;		/* moved here to appease gcc */
    414    1.1       cgd 	switch (cmd) {
    415    1.1       cgd 	case SIOCAIFADDR:
    416    1.1       cgd 	case SIOCDIFADDR:
    417   1.43  christos 	case SIOCGIFALIAS:
    418  1.152       roy 	case SIOCGIFAFLAG_IN:
    419  1.170     ozaki 		if (ifra->ifra_addr.sin_family == AF_INET) {
    420  1.178     ozaki 			int s;
    421  1.178     ozaki 
    422  1.178     ozaki 			if (ia != NULL)
    423  1.178     ozaki 				ia4_release(ia, &psref);
    424  1.178     ozaki 			s = pserialize_read_enter();
    425  1.170     ozaki 			IN_ADDRHASH_READER_FOREACH(ia,
    426  1.170     ozaki 			    ifra->ifra_addr.sin_addr.s_addr) {
    427  1.117    dyoung 				if (ia->ia_ifp == ifp &&
    428   1.38       tls 				    in_hosteq(ia->ia_addr.sin_addr,
    429   1.38       tls 				    ifra->ifra_addr.sin_addr))
    430   1.28   mycroft 					break;
    431   1.28   mycroft 			}
    432  1.178     ozaki 			if (ia != NULL)
    433  1.178     ozaki 				ia4_acquire(ia, &psref);
    434  1.178     ozaki 			pserialize_read_exit(s);
    435  1.170     ozaki 		}
    436  1.152       roy 		if ((cmd == SIOCDIFADDR ||
    437  1.152       roy 		    cmd == SIOCGIFALIAS ||
    438  1.152       roy 		    cmd == SIOCGIFAFLAG_IN) &&
    439  1.178     ozaki 		    ia == NULL) {
    440  1.178     ozaki 			error = EADDRNOTAVAIL;
    441  1.178     ozaki 			goto out;
    442  1.178     ozaki 		}
    443  1.105     seanb 
    444  1.105     seanb 		if (cmd == SIOCDIFADDR &&
    445  1.105     seanb 		    ifra->ifra_addr.sin_family == AF_UNSPEC) {
    446  1.105     seanb 			ifra->ifra_addr.sin_family = AF_INET;
    447  1.105     seanb 		}
    448    1.1       cgd 		/* FALLTHROUGH */
    449    1.1       cgd 	case SIOCSIFADDR:
    450  1.152       roy 		if (ia == NULL || ia->ia_addr.sin_family != AF_INET)
    451  1.152       roy 			;
    452  1.152       roy 		else if (ifra->ifra_addr.sin_len == 0) {
    453  1.152       roy 			ifra->ifra_addr = ia->ia_addr;
    454  1.152       roy 			hostIsNew = 0;
    455  1.152       roy 		} else if (in_hosteq(ia->ia_addr.sin_addr,
    456  1.152       roy 		           ifra->ifra_addr.sin_addr))
    457  1.152       roy 			hostIsNew = 0;
    458  1.152       roy 		/* FALLTHROUGH */
    459   1.52    itojun 	case SIOCSIFDSTADDR:
    460  1.178     ozaki 		if (ifra->ifra_addr.sin_family != AF_INET) {
    461  1.178     ozaki 			error = EAFNOSUPPORT;
    462  1.178     ozaki 			goto out;
    463  1.178     ozaki 		}
    464   1.54    itojun 		/* FALLTHROUGH */
    465    1.1       cgd 	case SIOCSIFNETMASK:
    466  1.117    dyoung 		if (ifp == NULL)
    467   1.44  christos 			panic("in_control");
    468   1.44  christos 
    469  1.152       roy 		if (cmd == SIOCGIFALIAS || cmd == SIOCGIFAFLAG_IN)
    470   1.44  christos 			break;
    471   1.44  christos 
    472  1.100      yamt 		if (ia == NULL &&
    473  1.178     ozaki 		    (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR)) {
    474  1.178     ozaki 			error = EADDRNOTAVAIL;
    475  1.178     ozaki 			goto out;
    476  1.178     ozaki 		}
    477  1.100      yamt 
    478  1.147       rtr 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
    479  1.113      elad 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    480  1.178     ozaki 		    NULL) != 0) {
    481  1.178     ozaki 			error = EPERM;
    482  1.178     ozaki 			goto out;
    483  1.178     ozaki 		}
    484    1.1       cgd 
    485  1.136    dyoung 		if (ia == NULL) {
    486  1.129    cegger 			ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
    487  1.178     ozaki 			if (ia == NULL) {
    488  1.178     ozaki 				error = ENOBUFS;
    489  1.178     ozaki 				goto out;
    490  1.178     ozaki 			}
    491   1.21   mycroft 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    492   1.21   mycroft 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    493   1.21   mycroft 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
    494  1.114    dyoung #ifdef IPSELSRC
    495  1.114    dyoung 			ia->ia_ifa.ifa_getifa = in_getifa;
    496  1.114    dyoung #else /* IPSELSRC */
    497  1.114    dyoung 			ia->ia_ifa.ifa_getifa = NULL;
    498  1.114    dyoung #endif /* IPSELSRC */
    499    1.1       cgd 			ia->ia_sockmask.sin_len = 8;
    500  1.149  christos 			ia->ia_sockmask.sin_family = AF_INET;
    501    1.1       cgd 			if (ifp->if_flags & IFF_BROADCAST) {
    502    1.1       cgd 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
    503    1.1       cgd 				ia->ia_broadaddr.sin_family = AF_INET;
    504    1.1       cgd 			}
    505    1.1       cgd 			ia->ia_ifp = ifp;
    506  1.141       tls 			ia->ia_idsalt = cprng_fast32() % 65535;
    507   1.24   mycroft 			LIST_INIT(&ia->ia_multiaddrs);
    508  1.170     ozaki 			IN_ADDRHASH_ENTRY_INIT(ia);
    509  1.171     ozaki 			IN_ADDRLIST_ENTRY_INIT(ia);
    510  1.178     ozaki 			ifa_psref_init(&ia->ia_ifa);
    511  1.169     ozaki 
    512  1.100      yamt 			newifaddr = 1;
    513   1.81    simonb 		}
    514    1.1       cgd 		break;
    515    1.1       cgd 
    516    1.1       cgd 	case SIOCSIFBRDADDR:
    517  1.147       rtr 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
    518  1.113      elad 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    519  1.178     ozaki 		    NULL) != 0) {
    520  1.178     ozaki 			error = EPERM;
    521  1.178     ozaki 			goto out;
    522  1.178     ozaki 		}
    523    1.1       cgd 		/* FALLTHROUGH */
    524    1.1       cgd 
    525    1.1       cgd 	case SIOCGIFADDR:
    526    1.1       cgd 	case SIOCGIFNETMASK:
    527    1.1       cgd 	case SIOCGIFDSTADDR:
    528    1.1       cgd 	case SIOCGIFBRDADDR:
    529  1.178     ozaki 		if (ia == NULL) {
    530  1.178     ozaki 			error = EADDRNOTAVAIL;
    531  1.178     ozaki 			goto out;
    532  1.178     ozaki 		}
    533    1.1       cgd 		break;
    534    1.1       cgd 	}
    535  1.100      yamt 	error = 0;
    536    1.1       cgd 	switch (cmd) {
    537    1.1       cgd 
    538    1.1       cgd 	case SIOCGIFADDR:
    539  1.118    dyoung 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
    540    1.1       cgd 		break;
    541    1.1       cgd 
    542    1.1       cgd 	case SIOCGIFBRDADDR:
    543  1.178     ozaki 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
    544  1.178     ozaki 			error = EINVAL;
    545  1.178     ozaki 			goto out;
    546  1.178     ozaki 		}
    547  1.118    dyoung 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
    548    1.1       cgd 		break;
    549    1.1       cgd 
    550    1.1       cgd 	case SIOCGIFDSTADDR:
    551  1.178     ozaki 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
    552  1.178     ozaki 			error = EINVAL;
    553  1.178     ozaki 			goto out;
    554  1.178     ozaki 		}
    555  1.118    dyoung 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
    556    1.1       cgd 		break;
    557    1.1       cgd 
    558    1.1       cgd 	case SIOCGIFNETMASK:
    559  1.149  christos 		/*
    560  1.149  christos 		 * We keep the number of trailing zero bytes the sin_len field
    561  1.149  christos 		 * of ia_sockmask, so we fix this before we pass it back to
    562  1.149  christos 		 * userland.
    563  1.149  christos 		 */
    564  1.149  christos 		oldaddr = ia->ia_sockmask;
    565  1.149  christos 		oldaddr.sin_len = sizeof(struct sockaddr_in);
    566  1.149  christos 		ifreq_setaddr(cmd, ifr, (const void *)&oldaddr);
    567    1.1       cgd 		break;
    568    1.1       cgd 
    569    1.1       cgd 	case SIOCSIFDSTADDR:
    570  1.178     ozaki 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
    571  1.178     ozaki 			error = EINVAL;
    572  1.178     ozaki 			goto out;
    573  1.178     ozaki 		}
    574    1.1       cgd 		oldaddr = ia->ia_dstaddr;
    575  1.118    dyoung 		ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
    576  1.139    dyoung 		if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) {
    577    1.1       cgd 			ia->ia_dstaddr = oldaddr;
    578  1.178     ozaki 			goto out;
    579    1.1       cgd 		}
    580    1.1       cgd 		if (ia->ia_flags & IFA_ROUTE) {
    581   1.21   mycroft 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
    582  1.117    dyoung 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
    583   1.21   mycroft 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    584  1.117    dyoung 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
    585    1.1       cgd 		}
    586    1.1       cgd 		break;
    587    1.1       cgd 
    588    1.1       cgd 	case SIOCSIFBRDADDR:
    589  1.178     ozaki 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
    590  1.178     ozaki 			error = EINVAL;
    591  1.178     ozaki 			goto out;
    592  1.178     ozaki 		}
    593  1.118    dyoung 		ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
    594    1.1       cgd 		break;
    595    1.1       cgd 
    596    1.1       cgd 	case SIOCSIFADDR:
    597  1.169     ozaki 		if (!newifaddr) {
    598  1.178     ozaki 			mutex_enter(&in_ifaddr_lock);
    599  1.169     ozaki 			LIST_REMOVE(ia, ia_hash);
    600  1.170     ozaki 			IN_ADDRHASH_WRITER_REMOVE(ia);
    601  1.178     ozaki 			mutex_exit(&in_ifaddr_lock);
    602  1.169     ozaki 			need_reinsert = true;
    603  1.169     ozaki 		}
    604  1.184       roy 		error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
    605  1.184       roy 		    NULL, 1);
    606  1.169     ozaki 
    607  1.169     ozaki 		run_hook = true;
    608  1.100      yamt 		break;
    609    1.1       cgd 
    610    1.1       cgd 	case SIOCSIFNETMASK:
    611  1.185       roy 		in_scrubprefix(ia);
    612  1.118    dyoung 		ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
    613   1.97   mycroft 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    614  1.169     ozaki 		if (!newifaddr) {
    615  1.178     ozaki 			mutex_enter(&in_ifaddr_lock);
    616  1.169     ozaki 			LIST_REMOVE(ia, ia_hash);
    617  1.170     ozaki 			IN_ADDRHASH_WRITER_REMOVE(ia);
    618  1.178     ozaki 			mutex_exit(&in_ifaddr_lock);
    619  1.169     ozaki 			need_reinsert = true;
    620  1.169     ozaki 		}
    621  1.184       roy 		error = in_ifinit(ifp, ia, NULL, NULL, 0);
    622  1.100      yamt 		break;
    623    1.1       cgd 
    624    1.1       cgd 	case SIOCAIFADDR:
    625    1.1       cgd 		maskIsNew = 0;
    626    1.1       cgd 		if (ifra->ifra_mask.sin_len) {
    627  1.185       roy 			in_scrubprefix(ia);
    628    1.1       cgd 			ia->ia_sockmask = ifra->ifra_mask;
    629   1.20   mycroft 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    630    1.1       cgd 			maskIsNew = 1;
    631    1.1       cgd 		}
    632    1.1       cgd 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    633    1.1       cgd 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
    634  1.184       roy 			new_dstaddr = &ifra->ifra_dstaddr;
    635    1.1       cgd 			maskIsNew  = 1; /* We lie; but the effect's the same */
    636  1.184       roy 		} else
    637  1.184       roy 			new_dstaddr = NULL;
    638    1.1       cgd 		if (ifra->ifra_addr.sin_family == AF_INET &&
    639   1.56    itojun 		    (hostIsNew || maskIsNew)) {
    640  1.169     ozaki 			if (!newifaddr) {
    641  1.178     ozaki 				mutex_enter(&in_ifaddr_lock);
    642  1.169     ozaki 				LIST_REMOVE(ia, ia_hash);
    643  1.170     ozaki 				IN_ADDRHASH_WRITER_REMOVE(ia);
    644  1.178     ozaki 				mutex_exit(&in_ifaddr_lock);
    645  1.169     ozaki 				need_reinsert = true;
    646  1.169     ozaki 			}
    647  1.184       roy 			error = in_ifinit(ifp, ia, &ifra->ifra_addr,
    648  1.184       roy 			    new_dstaddr, 0);
    649   1.56    itojun 		}
    650    1.1       cgd 		if ((ifp->if_flags & IFF_BROADCAST) &&
    651    1.1       cgd 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
    652    1.1       cgd 			ia->ia_broadaddr = ifra->ifra_broadaddr;
    653  1.169     ozaki 		run_hook = true;
    654  1.100      yamt 		break;
    655   1.43  christos 
    656   1.43  christos 	case SIOCGIFALIAS:
    657   1.43  christos 		ifra->ifra_mask = ia->ia_sockmask;
    658   1.43  christos 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    659   1.43  christos 		    (ia->ia_dstaddr.sin_family == AF_INET))
    660   1.43  christos 			ifra->ifra_dstaddr = ia->ia_dstaddr;
    661   1.43  christos 		else if ((ifp->if_flags & IFF_BROADCAST) &&
    662   1.43  christos 		    (ia->ia_broadaddr.sin_family == AF_INET))
    663   1.43  christos 			ifra->ifra_broadaddr = ia->ia_broadaddr;
    664   1.43  christos 		else
    665  1.117    dyoung 			memset(&ifra->ifra_broadaddr, 0,
    666   1.48    itojun 			      sizeof(ifra->ifra_broadaddr));
    667  1.100      yamt 		break;
    668    1.1       cgd 
    669  1.152       roy 	case SIOCGIFAFLAG_IN:
    670  1.152       roy 		ifr->ifr_addrflags = ia->ia4_flags;
    671  1.152       roy 		break;
    672  1.152       roy 
    673    1.1       cgd 	case SIOCDIFADDR:
    674  1.178     ozaki 		ia4_release(ia, &psref);
    675  1.121    dyoung 		in_purgeaddr(&ia->ia_ifa);
    676  1.178     ozaki 		ia = NULL;
    677  1.169     ozaki 		run_hook = true;
    678    1.1       cgd 		break;
    679   1.19   mycroft 
    680   1.19   mycroft #ifdef MROUTING
    681   1.19   mycroft 	case SIOCGETVIFCNT:
    682   1.19   mycroft 	case SIOCGETSGCNT:
    683  1.100      yamt 		error = mrt_ioctl(so, cmd, data);
    684  1.100      yamt 		break;
    685   1.19   mycroft #endif /* MROUTING */
    686    1.1       cgd 
    687    1.1       cgd 	default:
    688  1.178     ozaki 		error = ENOTTY;
    689  1.178     ozaki 		goto out;
    690  1.100      yamt 	}
    691  1.100      yamt 
    692  1.169     ozaki 	/*
    693  1.169     ozaki 	 * XXX insert regardless of error to make in_purgeaddr below work.
    694  1.169     ozaki 	 * Need to improve.
    695  1.169     ozaki 	 */
    696  1.169     ozaki 	if (newifaddr) {
    697  1.169     ozaki 		ifaref(&ia->ia_ifa);
    698  1.169     ozaki 		ifa_insert(ifp, &ia->ia_ifa);
    699  1.178     ozaki 
    700  1.178     ozaki 		mutex_enter(&in_ifaddr_lock);
    701  1.178     ozaki 		TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
    702  1.171     ozaki 		IN_ADDRLIST_WRITER_INSERT_TAIL(ia);
    703  1.169     ozaki 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
    704  1.169     ozaki 		    ia, ia_hash);
    705  1.170     ozaki 		IN_ADDRHASH_WRITER_INSERT_HEAD(ia);
    706  1.178     ozaki 		mutex_exit(&in_ifaddr_lock);
    707  1.169     ozaki 	} else if (need_reinsert) {
    708  1.178     ozaki 		mutex_enter(&in_ifaddr_lock);
    709  1.169     ozaki 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
    710  1.169     ozaki 		    ia, ia_hash);
    711  1.170     ozaki 		IN_ADDRHASH_WRITER_INSERT_HEAD(ia);
    712  1.178     ozaki 		mutex_exit(&in_ifaddr_lock);
    713  1.169     ozaki 	}
    714  1.169     ozaki 
    715  1.169     ozaki 	if (error == 0) {
    716  1.169     ozaki 		if (run_hook)
    717  1.169     ozaki 			(void)pfil_run_hooks(if_pfil,
    718  1.169     ozaki 			    (struct mbuf **)cmd, ifp, PFIL_IFADDR);
    719  1.169     ozaki 	} else if (newifaddr) {
    720  1.100      yamt 		KASSERT(ia != NULL);
    721  1.121    dyoung 		in_purgeaddr(&ia->ia_ifa);
    722  1.178     ozaki 		ia = NULL;
    723    1.1       cgd 	}
    724  1.100      yamt 
    725  1.178     ozaki out:
    726  1.178     ozaki 	if (!newifaddr && ia != NULL)
    727  1.178     ozaki 		ia4_release(ia, &psref);
    728  1.178     ozaki 	curlwp_bindx(bound);
    729  1.100      yamt 	return error;
    730   1.50   thorpej }
    731   1.50   thorpej 
    732  1.177     ozaki int
    733  1.177     ozaki in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
    734  1.177     ozaki {
    735  1.177     ozaki 	int error;
    736  1.177     ozaki 
    737  1.177     ozaki 	mutex_enter(softnet_lock);
    738  1.177     ozaki 	error = in_control0(so, cmd, data, ifp);
    739  1.177     ozaki 	mutex_exit(softnet_lock);
    740  1.177     ozaki 
    741  1.177     ozaki 	return error;
    742  1.177     ozaki }
    743  1.177     ozaki 
    744  1.150       roy /* Add ownaddr as loopback rtentry. */
    745  1.150       roy static void
    746  1.150       roy in_ifaddlocal(struct ifaddr *ifa)
    747  1.150       roy {
    748  1.151       roy 	struct in_ifaddr *ia;
    749  1.151       roy 
    750  1.151       roy 	ia = (struct in_ifaddr *)ifa;
    751  1.151       roy 	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY ||
    752  1.151       roy 	    (ia->ia_ifp->if_flags & IFF_POINTOPOINT &&
    753  1.151       roy 	    in_hosteq(ia->ia_dstaddr.sin_addr, ia->ia_addr.sin_addr)))
    754  1.151       roy 	{
    755  1.151       roy 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
    756  1.151       roy 		return;
    757  1.151       roy 	}
    758  1.150       roy 
    759  1.150       roy 	rt_ifa_addlocal(ifa);
    760  1.150       roy }
    761  1.150       roy 
    762  1.168     ozaki /* Remove loopback entry of ownaddr */
    763  1.150       roy static void
    764  1.150       roy in_ifremlocal(struct ifaddr *ifa)
    765  1.150       roy {
    766  1.150       roy 	struct in_ifaddr *ia, *p;
    767  1.150       roy 	struct ifaddr *alt_ifa = NULL;
    768  1.150       roy 	int ia_count = 0;
    769  1.178     ozaki 	int s;
    770  1.178     ozaki 	struct psref psref;
    771  1.178     ozaki 	int bound = curlwp_bind();
    772  1.150       roy 
    773  1.150       roy 	ia = (struct in_ifaddr *)ifa;
    774  1.150       roy 	/* Delete the entry if exactly one ifaddr matches the
    775  1.150       roy 	 * address, ifa->ifa_addr. */
    776  1.178     ozaki 	s = pserialize_read_enter();
    777  1.171     ozaki 	IN_ADDRLIST_READER_FOREACH(p) {
    778  1.150       roy 		if (!in_hosteq(p->ia_addr.sin_addr, ia->ia_addr.sin_addr))
    779  1.150       roy 			continue;
    780  1.150       roy 		if (p->ia_ifp != ia->ia_ifp)
    781  1.150       roy 			alt_ifa = &p->ia_ifa;
    782  1.150       roy 		if (++ia_count > 1 && alt_ifa != NULL)
    783  1.150       roy 			break;
    784  1.150       roy 	}
    785  1.178     ozaki 	if (alt_ifa != NULL && ia_count > 1)
    786  1.178     ozaki 		ifa_acquire(alt_ifa, &psref);
    787  1.178     ozaki 	pserialize_read_exit(s);
    788  1.150       roy 
    789  1.150       roy 	if (ia_count == 0)
    790  1.178     ozaki 		goto out;
    791  1.150       roy 
    792  1.150       roy 	rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa);
    793  1.178     ozaki 	if (alt_ifa != NULL && ia_count > 1)
    794  1.178     ozaki 		ifa_release(alt_ifa, &psref);
    795  1.178     ozaki out:
    796  1.178     ozaki 	curlwp_bindx(bound);
    797  1.150       roy }
    798  1.150       roy 
    799  1.183       roy static void
    800  1.183       roy in_scrubaddr(struct in_ifaddr *ia)
    801  1.183       roy {
    802  1.183       roy 
    803  1.183       roy 	/* stop DAD processing */
    804  1.183       roy 	if (ia->ia_dad_stop != NULL)
    805  1.183       roy 		ia->ia_dad_stop(&ia->ia_ifa);
    806  1.183       roy 
    807  1.185       roy 	in_scrubprefix(ia);
    808  1.183       roy 	in_ifremlocal(&ia->ia_ifa);
    809  1.185       roy 
    810  1.183       roy 	if (ia->ia_allhosts != NULL) {
    811  1.183       roy 		in_delmulti(ia->ia_allhosts);
    812  1.183       roy 		ia->ia_allhosts = NULL;
    813  1.183       roy 	}
    814  1.183       roy }
    815  1.183       roy 
    816  1.178     ozaki /*
    817  1.178     ozaki  * Depends on it isn't called in concurrent. It should be guaranteed
    818  1.178     ozaki  * by ifa->ifa_ifp's ioctl lock. The possible callers are in_control
    819  1.178     ozaki  * and if_purgeaddrs; the former is called iva ifa->ifa_ifp's ioctl
    820  1.178     ozaki  * and the latter is called via ifa->ifa_ifp's if_detach. The functions
    821  1.178     ozaki  * never be executed in concurrent.
    822  1.178     ozaki  */
    823   1.50   thorpej void
    824  1.121    dyoung in_purgeaddr(struct ifaddr *ifa)
    825   1.50   thorpej {
    826  1.183       roy 	struct in_ifaddr *ia = (void *) ifa;
    827  1.121    dyoung 	struct ifnet *ifp = ifa->ifa_ifp;
    828   1.50   thorpej 
    829  1.178     ozaki 	KASSERT(!ifa_held(ifa));
    830  1.178     ozaki 
    831  1.183       roy 	in_scrubaddr(ia);
    832  1.178     ozaki 
    833  1.178     ozaki 	mutex_enter(&in_ifaddr_lock);
    834   1.50   thorpej 	LIST_REMOVE(ia, ia_hash);
    835  1.170     ozaki 	IN_ADDRHASH_WRITER_REMOVE(ia);
    836   1.93  jonathan 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
    837  1.171     ozaki 	IN_ADDRLIST_WRITER_REMOVE(ia);
    838  1.178     ozaki 	ifa_remove(ifp, &ia->ia_ifa);
    839  1.178     ozaki 	mutex_exit(&in_ifaddr_lock);
    840  1.178     ozaki 
    841  1.178     ozaki 	IN_ADDRHASH_ENTRY_DESTROY(ia);
    842  1.171     ozaki 	IN_ADDRLIST_ENTRY_DESTROY(ia);
    843  1.148     rmind 	ifafree(&ia->ia_ifa);
    844   1.50   thorpej 	in_setmaxmtu();
    845   1.51   thorpej }
    846   1.51   thorpej 
    847   1.51   thorpej void
    848  1.111       tls in_purgeif(struct ifnet *ifp)		/* MUST be called at splsoftnet() */
    849   1.51   thorpej {
    850  1.121    dyoung 	if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
    851  1.111       tls 	igmp_purgeif(ifp);		/* manipulates pools */
    852   1.89    itojun #ifdef MROUTING
    853   1.89    itojun 	ip_mrouter_detach(ifp);
    854   1.89    itojun #endif
    855   1.48    itojun }
    856   1.48    itojun 
    857   1.48    itojun /*
    858   1.48    itojun  * SIOC[GAD]LIFADDR.
    859   1.48    itojun  *	SIOCGLIFADDR: get first address. (???)
    860   1.48    itojun  *	SIOCGLIFADDR with IFLR_PREFIX:
    861   1.48    itojun  *		get first address that matches the specified prefix.
    862   1.48    itojun  *	SIOCALIFADDR: add the specified address.
    863   1.48    itojun  *	SIOCALIFADDR with IFLR_PREFIX:
    864   1.48    itojun  *		EINVAL since we can't deduce hostid part of the address.
    865   1.48    itojun  *	SIOCDLIFADDR: delete the specified address.
    866   1.48    itojun  *	SIOCDLIFADDR with IFLR_PREFIX:
    867   1.48    itojun  *		delete the first address that matches the specified prefix.
    868   1.48    itojun  * return values:
    869   1.48    itojun  *	EINVAL on invalid parameters
    870   1.48    itojun  *	EADDRNOTAVAIL on prefix match failed/specified address not found
    871   1.48    itojun  *	other values may be returned from in_ioctl()
    872   1.48    itojun  */
    873   1.48    itojun static int
    874  1.116  christos in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
    875  1.147       rtr     struct ifnet *ifp)
    876   1.48    itojun {
    877   1.48    itojun 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
    878   1.48    itojun 	struct ifaddr *ifa;
    879   1.49    itojun 	struct sockaddr *sa;
    880   1.48    itojun 
    881   1.48    itojun 	/* sanity checks */
    882  1.119    dyoung 	if (data == NULL || ifp == NULL) {
    883   1.48    itojun 		panic("invalid argument to in_lifaddr_ioctl");
    884   1.48    itojun 		/*NOTRECHED*/
    885   1.48    itojun 	}
    886   1.48    itojun 
    887   1.48    itojun 	switch (cmd) {
    888   1.48    itojun 	case SIOCGLIFADDR:
    889   1.48    itojun 		/* address must be specified on GET with IFLR_PREFIX */
    890   1.48    itojun 		if ((iflr->flags & IFLR_PREFIX) == 0)
    891   1.48    itojun 			break;
    892   1.48    itojun 		/*FALLTHROUGH*/
    893   1.48    itojun 	case SIOCALIFADDR:
    894   1.48    itojun 	case SIOCDLIFADDR:
    895   1.48    itojun 		/* address must be specified on ADD and DELETE */
    896   1.49    itojun 		sa = (struct sockaddr *)&iflr->addr;
    897   1.49    itojun 		if (sa->sa_family != AF_INET)
    898   1.48    itojun 			return EINVAL;
    899   1.49    itojun 		if (sa->sa_len != sizeof(struct sockaddr_in))
    900   1.48    itojun 			return EINVAL;
    901   1.48    itojun 		/* XXX need improvement */
    902   1.49    itojun 		sa = (struct sockaddr *)&iflr->dstaddr;
    903  1.126    dyoung 		if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
    904   1.48    itojun 			return EINVAL;
    905  1.126    dyoung 		if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
    906   1.48    itojun 			return EINVAL;
    907   1.48    itojun 		break;
    908   1.48    itojun 	default: /*shouldn't happen*/
    909   1.48    itojun #if 0
    910   1.48    itojun 		panic("invalid cmd to in_lifaddr_ioctl");
    911   1.48    itojun 		/*NOTREACHED*/
    912   1.48    itojun #else
    913   1.48    itojun 		return EOPNOTSUPP;
    914   1.48    itojun #endif
    915   1.48    itojun 	}
    916  1.124    dyoung 	if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
    917   1.48    itojun 		return EINVAL;
    918   1.48    itojun 
    919   1.48    itojun 	switch (cmd) {
    920   1.48    itojun 	case SIOCALIFADDR:
    921   1.48    itojun 	    {
    922   1.48    itojun 		struct in_aliasreq ifra;
    923   1.48    itojun 
    924   1.48    itojun 		if (iflr->flags & IFLR_PREFIX)
    925   1.48    itojun 			return EINVAL;
    926   1.48    itojun 
    927  1.110      elad 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
    928  1.132    cegger 		memset(&ifra, 0, sizeof(ifra));
    929  1.134   tsutsui 		memcpy(ifra.ifra_name, iflr->iflr_name,
    930   1.48    itojun 			sizeof(ifra.ifra_name));
    931   1.48    itojun 
    932  1.134   tsutsui 		memcpy(&ifra.ifra_addr, &iflr->addr,
    933   1.49    itojun 			((struct sockaddr *)&iflr->addr)->sa_len);
    934   1.48    itojun 
    935   1.49    itojun 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
    936  1.134   tsutsui 			memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
    937   1.49    itojun 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
    938   1.48    itojun 		}
    939   1.48    itojun 
    940   1.48    itojun 		ifra.ifra_mask.sin_family = AF_INET;
    941   1.48    itojun 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
    942   1.48    itojun 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
    943   1.48    itojun 
    944  1.147       rtr 		return in_control(so, SIOCAIFADDR, &ifra, ifp);
    945   1.48    itojun 	    }
    946   1.48    itojun 	case SIOCGLIFADDR:
    947   1.48    itojun 	case SIOCDLIFADDR:
    948   1.48    itojun 	    {
    949   1.48    itojun 		struct in_ifaddr *ia;
    950   1.48    itojun 		struct in_addr mask, candidate, match;
    951   1.48    itojun 		struct sockaddr_in *sin;
    952  1.179     ozaki 		int cmp, s;
    953   1.48    itojun 
    954  1.132    cegger 		memset(&mask, 0, sizeof(mask));
    955  1.132    cegger 		memset(&match, 0, sizeof(match));	/* XXX gcc */
    956   1.48    itojun 		if (iflr->flags & IFLR_PREFIX) {
    957   1.48    itojun 			/* lookup a prefix rather than address. */
    958   1.48    itojun 			in_len2mask(&mask, iflr->prefixlen);
    959   1.48    itojun 
    960   1.48    itojun 			sin = (struct sockaddr_in *)&iflr->addr;
    961   1.48    itojun 			match.s_addr = sin->sin_addr.s_addr;
    962   1.48    itojun 			match.s_addr &= mask.s_addr;
    963   1.48    itojun 
    964   1.48    itojun 			/* if you set extra bits, that's wrong */
    965   1.48    itojun 			if (match.s_addr != sin->sin_addr.s_addr)
    966   1.48    itojun 				return EINVAL;
    967   1.48    itojun 
    968   1.48    itojun 			cmp = 1;
    969   1.48    itojun 		} else {
    970   1.48    itojun 			if (cmd == SIOCGLIFADDR) {
    971   1.48    itojun 				/* on getting an address, take the 1st match */
    972   1.48    itojun 				cmp = 0;	/*XXX*/
    973   1.48    itojun 			} else {
    974   1.48    itojun 				/* on deleting an address, do exact match */
    975   1.48    itojun 				in_len2mask(&mask, 32);
    976   1.48    itojun 				sin = (struct sockaddr_in *)&iflr->addr;
    977   1.48    itojun 				match.s_addr = sin->sin_addr.s_addr;
    978   1.48    itojun 
    979   1.48    itojun 				cmp = 1;
    980   1.48    itojun 			}
    981   1.48    itojun 		}
    982   1.48    itojun 
    983  1.179     ozaki 		s = pserialize_read_enter();
    984  1.172     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
    985   1.95    itojun 			if (ifa->ifa_addr->sa_family != AF_INET)
    986   1.48    itojun 				continue;
    987  1.119    dyoung 			if (cmp == 0)
    988   1.48    itojun 				break;
    989  1.143       gdt 			candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
    990   1.48    itojun 			candidate.s_addr &= mask.s_addr;
    991   1.48    itojun 			if (candidate.s_addr == match.s_addr)
    992   1.48    itojun 				break;
    993   1.48    itojun 		}
    994  1.179     ozaki 		if (ifa == NULL) {
    995  1.179     ozaki 			pserialize_read_exit(s);
    996   1.48    itojun 			return EADDRNOTAVAIL;
    997  1.179     ozaki 		}
    998   1.48    itojun 		ia = (struct in_ifaddr *)ifa;
    999   1.48    itojun 
   1000   1.48    itojun 		if (cmd == SIOCGLIFADDR) {
   1001   1.48    itojun 			/* fill in the if_laddrreq structure */
   1002  1.134   tsutsui 			memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
   1003   1.48    itojun 
   1004   1.48    itojun 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1005  1.134   tsutsui 				memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
   1006   1.48    itojun 					ia->ia_dstaddr.sin_len);
   1007   1.48    itojun 			} else
   1008  1.132    cegger 				memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
   1009   1.48    itojun 
   1010   1.48    itojun 			iflr->prefixlen =
   1011   1.48    itojun 				in_mask2len(&ia->ia_sockmask.sin_addr);
   1012   1.48    itojun 
   1013   1.48    itojun 			iflr->flags = 0;	/*XXX*/
   1014  1.179     ozaki 			pserialize_read_exit(s);
   1015   1.48    itojun 
   1016   1.48    itojun 			return 0;
   1017   1.48    itojun 		} else {
   1018   1.48    itojun 			struct in_aliasreq ifra;
   1019   1.48    itojun 
   1020  1.110      elad 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
   1021  1.132    cegger 			memset(&ifra, 0, sizeof(ifra));
   1022  1.134   tsutsui 			memcpy(ifra.ifra_name, iflr->iflr_name,
   1023   1.48    itojun 				sizeof(ifra.ifra_name));
   1024   1.48    itojun 
   1025  1.134   tsutsui 			memcpy(&ifra.ifra_addr, &ia->ia_addr,
   1026   1.48    itojun 				ia->ia_addr.sin_len);
   1027   1.48    itojun 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1028  1.134   tsutsui 				memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
   1029   1.48    itojun 					ia->ia_dstaddr.sin_len);
   1030   1.48    itojun 			}
   1031  1.134   tsutsui 			memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
   1032   1.48    itojun 				ia->ia_sockmask.sin_len);
   1033  1.179     ozaki 			pserialize_read_exit(s);
   1034   1.48    itojun 
   1035  1.147       rtr 			return in_control(so, SIOCDIFADDR, &ifra, ifp);
   1036   1.48    itojun 		}
   1037   1.48    itojun 	    }
   1038   1.48    itojun 	}
   1039   1.48    itojun 
   1040   1.48    itojun 	return EOPNOTSUPP;	/*just for safety*/
   1041    1.1       cgd }
   1042    1.1       cgd 
   1043    1.1       cgd /*
   1044    1.1       cgd  * Initialize an interface's internet address
   1045    1.1       cgd  * and routing table entry.
   1046    1.1       cgd  */
   1047   1.12   mycroft int
   1048  1.103     perry in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
   1049  1.184       roy     const struct sockaddr_in *sin, const struct sockaddr_in *dst, int scrub)
   1050    1.1       cgd {
   1051   1.97   mycroft 	u_int32_t i;
   1052  1.184       roy 	struct sockaddr_in oldaddr, olddst;
   1053  1.182       roy 	int s, oldflags, flags = RTF_UP, error, hostIsNew;
   1054    1.1       cgd 
   1055  1.118    dyoung 	if (sin == NULL)
   1056   1.97   mycroft 		sin = &ia->ia_addr;
   1057  1.184       roy 	if (dst == NULL)
   1058  1.184       roy 		dst = &ia->ia_dstaddr;
   1059   1.97   mycroft 
   1060   1.32   mycroft 	/*
   1061   1.32   mycroft 	 * Set up new addresses.
   1062   1.32   mycroft 	 */
   1063    1.1       cgd 	oldaddr = ia->ia_addr;
   1064  1.184       roy 	olddst = ia->ia_dstaddr;
   1065  1.182       roy 	oldflags = ia->ia4_flags;
   1066    1.1       cgd 	ia->ia_addr = *sin;
   1067  1.184       roy 	ia->ia_dstaddr = *dst;
   1068  1.182       roy 	hostIsNew = oldaddr.sin_family != AF_INET ||
   1069  1.182       roy 	    !in_hosteq(ia->ia_addr.sin_addr, oldaddr.sin_addr);
   1070  1.184       roy 	if (!scrub)
   1071  1.184       roy 		scrub = oldaddr.sin_family != ia->ia_dstaddr.sin_family ||
   1072  1.184       roy 		    !in_hosteq(ia->ia_dstaddr.sin_addr, olddst.sin_addr);
   1073   1.38       tls 
   1074  1.182       roy 	/*
   1075  1.182       roy 	 * Configure address flags.
   1076  1.182       roy 	 * We need to do this early because they maybe adjusted
   1077  1.182       roy 	 * by if_addr_init depending on the address.
   1078  1.182       roy 	 */
   1079  1.182       roy 	if (ia->ia4_flags & IN_IFF_DUPLICATED) {
   1080  1.182       roy 		ia->ia4_flags &= ~IN_IFF_DUPLICATED;
   1081  1.182       roy 		hostIsNew = 1;
   1082  1.182       roy 	}
   1083  1.182       roy 	if (ifp->if_link_state == LINK_STATE_DOWN) {
   1084  1.182       roy 		ia->ia4_flags |= IN_IFF_DETACHED;
   1085  1.182       roy 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
   1086  1.182       roy 	} else if (hostIsNew && if_do_dad(ifp))
   1087  1.182       roy 		ia->ia4_flags |= IN_IFF_TRYTENTATIVE;
   1088  1.152       roy 
   1089    1.1       cgd 	/*
   1090    1.1       cgd 	 * Give the interface a chance to initialize
   1091    1.1       cgd 	 * if this is its first address,
   1092    1.1       cgd 	 * and to validate the address if necessary.
   1093    1.1       cgd 	 */
   1094  1.182       roy 	s = splnet();
   1095  1.182       roy 	error = if_addr_init(ifp, &ia->ia_ifa, true);
   1096  1.182       roy 	splx(s);
   1097  1.156       roy 	/* Now clear the try tentative flag, it's job is done. */
   1098  1.156       roy 	ia->ia4_flags &= ~IN_IFF_TRYTENTATIVE;
   1099  1.182       roy 	if (error != 0) {
   1100  1.182       roy 		ia->ia_addr = oldaddr;
   1101  1.184       roy 		ia->ia_dstaddr = olddst;
   1102  1.182       roy 		ia->ia4_flags = oldflags;
   1103  1.182       roy 		return error;
   1104  1.182       roy 	}
   1105  1.156       roy 
   1106  1.183       roy 	if (scrub || hostIsNew) {
   1107  1.182       roy 		int newflags = ia->ia4_flags;
   1108  1.182       roy 
   1109   1.21   mycroft 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
   1110  1.184       roy 		ia->ia_ifa.ifa_dstaddr = sintosa(&olddst);
   1111  1.182       roy 		ia->ia4_flags = oldflags;
   1112  1.183       roy 		if (hostIsNew)
   1113  1.183       roy 			in_scrubaddr(ia);
   1114  1.183       roy 		else if (scrub)
   1115  1.185       roy 			in_scrubprefix(ia);
   1116   1.21   mycroft 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
   1117  1.184       roy 		ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
   1118  1.182       roy 		ia->ia4_flags = newflags;
   1119    1.1       cgd 	}
   1120   1.35   thorpej 
   1121  1.150       roy 	/* Add the local route to the address */
   1122  1.150       roy 	in_ifaddlocal(&ia->ia_ifa);
   1123  1.150       roy 
   1124   1.97   mycroft 	i = ia->ia_addr.sin_addr.s_addr;
   1125    1.1       cgd 	if (IN_CLASSA(i))
   1126    1.1       cgd 		ia->ia_netmask = IN_CLASSA_NET;
   1127    1.1       cgd 	else if (IN_CLASSB(i))
   1128    1.1       cgd 		ia->ia_netmask = IN_CLASSB_NET;
   1129    1.1       cgd 	else
   1130    1.1       cgd 		ia->ia_netmask = IN_CLASSC_NET;
   1131    1.1       cgd 	/*
   1132   1.12   mycroft 	 * The subnet mask usually includes at least the standard network part,
   1133   1.12   mycroft 	 * but may may be smaller in the case of supernetting.
   1134   1.12   mycroft 	 * If it is set, we believe it.
   1135    1.1       cgd 	 */
   1136   1.12   mycroft 	if (ia->ia_subnetmask == 0) {
   1137   1.12   mycroft 		ia->ia_subnetmask = ia->ia_netmask;
   1138   1.20   mycroft 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
   1139   1.12   mycroft 	} else
   1140   1.12   mycroft 		ia->ia_netmask &= ia->ia_subnetmask;
   1141   1.35   thorpej 
   1142   1.12   mycroft 	ia->ia_net = i & ia->ia_netmask;
   1143    1.1       cgd 	ia->ia_subnet = i & ia->ia_subnetmask;
   1144   1.12   mycroft 	in_socktrim(&ia->ia_sockmask);
   1145   1.35   thorpej 	/* re-calculate the "in_maxmtu" value */
   1146   1.35   thorpej 	in_setmaxmtu();
   1147    1.1       cgd 	/*
   1148    1.1       cgd 	 * Add route for the network.
   1149    1.1       cgd 	 */
   1150   1.12   mycroft 	ia->ia_ifa.ifa_metric = ifp->if_metric;
   1151    1.1       cgd 	if (ifp->if_flags & IFF_BROADCAST) {
   1152   1.12   mycroft 		ia->ia_broadaddr.sin_addr.s_addr =
   1153   1.20   mycroft 			ia->ia_subnet | ~ia->ia_subnetmask;
   1154    1.1       cgd 		ia->ia_netbroadcast.s_addr =
   1155   1.20   mycroft 			ia->ia_net | ~ia->ia_netmask;
   1156    1.1       cgd 	} else if (ifp->if_flags & IFF_LOOPBACK) {
   1157   1.83      onoe 		ia->ia_dstaddr = ia->ia_addr;
   1158    1.1       cgd 		flags |= RTF_HOST;
   1159    1.1       cgd 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
   1160    1.1       cgd 		if (ia->ia_dstaddr.sin_family != AF_INET)
   1161    1.1       cgd 			return (0);
   1162    1.1       cgd 		flags |= RTF_HOST;
   1163    1.1       cgd 	}
   1164   1.67    itojun 	error = in_addprefix(ia, flags);
   1165    1.5   hpeyerl 	/*
   1166    1.5   hpeyerl 	 * If the interface supports multicast, join the "all hosts"
   1167    1.5   hpeyerl 	 * multicast group on that interface.
   1168    1.5   hpeyerl 	 */
   1169   1.65     enami 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
   1170    1.5   hpeyerl 		struct in_addr addr;
   1171    1.5   hpeyerl 
   1172   1.20   mycroft 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
   1173   1.65     enami 		ia->ia_allhosts = in_addmulti(&addr, ifp);
   1174    1.5   hpeyerl 	}
   1175  1.152       roy 
   1176  1.182       roy 	if (hostIsNew &&
   1177  1.182       roy 	    ia->ia4_flags & IN_IFF_TENTATIVE &&
   1178  1.182       roy 	    if_do_dad(ifp))
   1179  1.156       roy 		ia->ia_dad_start((struct ifaddr *)ia);
   1180  1.152       roy 
   1181  1.182       roy 	return error;
   1182    1.1       cgd }
   1183   1.67    itojun 
   1184   1.67    itojun #define rtinitflags(x) \
   1185   1.68    itojun 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
   1186   1.68    itojun 	    ? RTF_HOST : 0)
   1187   1.67    itojun 
   1188   1.67    itojun /*
   1189   1.67    itojun  * add a route to prefix ("connected route" in cisco terminology).
   1190   1.67    itojun  * does nothing if there's some interface address with the same prefix already.
   1191   1.67    itojun  */
   1192   1.67    itojun static int
   1193  1.103     perry in_addprefix(struct in_ifaddr *target, int flags)
   1194   1.67    itojun {
   1195   1.67    itojun 	struct in_ifaddr *ia;
   1196   1.67    itojun 	struct in_addr prefix, mask, p;
   1197   1.67    itojun 	int error;
   1198  1.178     ozaki 	int s;
   1199   1.67    itojun 
   1200   1.67    itojun 	if ((flags & RTF_HOST) != 0)
   1201   1.67    itojun 		prefix = target->ia_dstaddr.sin_addr;
   1202   1.85    itojun 	else {
   1203   1.67    itojun 		prefix = target->ia_addr.sin_addr;
   1204   1.85    itojun 		mask = target->ia_sockmask.sin_addr;
   1205   1.85    itojun 		prefix.s_addr &= mask.s_addr;
   1206   1.85    itojun 	}
   1207   1.67    itojun 
   1208  1.178     ozaki 	s = pserialize_read_enter();
   1209  1.171     ozaki 	IN_ADDRLIST_READER_FOREACH(ia) {
   1210   1.67    itojun 		if (rtinitflags(ia))
   1211   1.67    itojun 			p = ia->ia_dstaddr.sin_addr;
   1212   1.85    itojun 		else {
   1213   1.67    itojun 			p = ia->ia_addr.sin_addr;
   1214   1.85    itojun 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
   1215   1.85    itojun 		}
   1216   1.85    itojun 
   1217   1.67    itojun 		if (prefix.s_addr != p.s_addr)
   1218   1.67    itojun 			continue;
   1219   1.67    itojun 
   1220   1.67    itojun 		/*
   1221   1.67    itojun 		 * if we got a matching prefix route inserted by other
   1222   1.69    martin 		 * interface address, we don't need to bother
   1223  1.114    dyoung 		 *
   1224  1.114    dyoung 		 * XXX RADIX_MPATH implications here? -dyoung
   1225   1.67    itojun 		 */
   1226  1.178     ozaki 		if (ia->ia_flags & IFA_ROUTE) {
   1227  1.178     ozaki 			pserialize_read_exit(s);
   1228   1.67    itojun 			return 0;
   1229  1.178     ozaki 		}
   1230   1.67    itojun 	}
   1231  1.178     ozaki 	pserialize_read_exit(s);
   1232   1.67    itojun 
   1233   1.67    itojun 	/*
   1234   1.67    itojun 	 * noone seem to have prefix route.  insert it.
   1235   1.67    itojun 	 */
   1236  1.119    dyoung 	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
   1237  1.119    dyoung 	if (error == 0)
   1238   1.67    itojun 		target->ia_flags |= IFA_ROUTE;
   1239  1.130       roy 	else if (error == EEXIST) {
   1240  1.150       roy 		/*
   1241  1.130       roy 		 * the fact the route already exists is not an error.
   1242  1.150       roy 		 */
   1243  1.130       roy 		error = 0;
   1244  1.130       roy 	}
   1245   1.67    itojun 	return error;
   1246   1.67    itojun }
   1247   1.67    itojun 
   1248   1.67    itojun /*
   1249   1.67    itojun  * remove a route to prefix ("connected route" in cisco terminology).
   1250   1.67    itojun  * re-installs the route by using another interface address, if there's one
   1251   1.67    itojun  * with the same prefix (otherwise we lose the route mistakenly).
   1252   1.67    itojun  */
   1253   1.67    itojun static int
   1254  1.103     perry in_scrubprefix(struct in_ifaddr *target)
   1255   1.67    itojun {
   1256   1.67    itojun 	struct in_ifaddr *ia;
   1257   1.67    itojun 	struct in_addr prefix, mask, p;
   1258   1.67    itojun 	int error;
   1259  1.178     ozaki 	int s;
   1260   1.67    itojun 
   1261  1.183       roy 	/* If we don't have IFA_ROUTE we have nothing to do */
   1262  1.150       roy 	if ((target->ia_flags & IFA_ROUTE) == 0)
   1263   1.67    itojun 		return 0;
   1264   1.67    itojun 
   1265   1.67    itojun 	if (rtinitflags(target))
   1266   1.67    itojun 		prefix = target->ia_dstaddr.sin_addr;
   1267   1.85    itojun 	else {
   1268   1.67    itojun 		prefix = target->ia_addr.sin_addr;
   1269   1.85    itojun 		mask = target->ia_sockmask.sin_addr;
   1270   1.85    itojun 		prefix.s_addr &= mask.s_addr;
   1271   1.85    itojun 	}
   1272   1.67    itojun 
   1273  1.178     ozaki 	s = pserialize_read_enter();
   1274  1.171     ozaki 	IN_ADDRLIST_READER_FOREACH(ia) {
   1275   1.67    itojun 		if (rtinitflags(ia))
   1276   1.67    itojun 			p = ia->ia_dstaddr.sin_addr;
   1277   1.85    itojun 		else {
   1278   1.67    itojun 			p = ia->ia_addr.sin_addr;
   1279   1.85    itojun 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
   1280   1.85    itojun 		}
   1281   1.85    itojun 
   1282   1.67    itojun 		if (prefix.s_addr != p.s_addr)
   1283   1.67    itojun 			continue;
   1284   1.67    itojun 
   1285   1.67    itojun 		/*
   1286   1.67    itojun 		 * if we got a matching prefix route, move IFA_ROUTE to him
   1287   1.67    itojun 		 */
   1288   1.67    itojun 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
   1289  1.178     ozaki 			struct psref psref;
   1290  1.178     ozaki 			int bound = curlwp_bind();
   1291  1.178     ozaki 
   1292  1.178     ozaki 			ia4_acquire(ia, &psref);
   1293  1.178     ozaki 			pserialize_read_exit(s);
   1294  1.178     ozaki 
   1295  1.119    dyoung 			rtinit(&target->ia_ifa, RTM_DELETE,
   1296   1.67    itojun 			    rtinitflags(target));
   1297   1.67    itojun 			target->ia_flags &= ~IFA_ROUTE;
   1298   1.67    itojun 
   1299  1.119    dyoung 			error = rtinit(&ia->ia_ifa, RTM_ADD,
   1300   1.67    itojun 			    rtinitflags(ia) | RTF_UP);
   1301   1.67    itojun 			if (error == 0)
   1302   1.67    itojun 				ia->ia_flags |= IFA_ROUTE;
   1303  1.178     ozaki 
   1304  1.178     ozaki 			ia4_release(ia, &psref);
   1305  1.178     ozaki 			curlwp_bindx(bound);
   1306  1.178     ozaki 
   1307   1.67    itojun 			return error;
   1308   1.67    itojun 		}
   1309   1.67    itojun 	}
   1310  1.178     ozaki 	pserialize_read_exit(s);
   1311   1.67    itojun 
   1312   1.67    itojun 	/*
   1313   1.67    itojun 	 * noone seem to have prefix route.  remove it.
   1314   1.67    itojun 	 */
   1315  1.119    dyoung 	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
   1316   1.67    itojun 	target->ia_flags &= ~IFA_ROUTE;
   1317   1.67    itojun 	return 0;
   1318   1.67    itojun }
   1319   1.67    itojun 
   1320   1.67    itojun #undef rtinitflags
   1321    1.1       cgd 
   1322    1.1       cgd /*
   1323    1.1       cgd  * Return 1 if the address might be a local broadcast address.
   1324    1.1       cgd  */
   1325    1.8   mycroft int
   1326  1.103     perry in_broadcast(struct in_addr in, struct ifnet *ifp)
   1327    1.1       cgd {
   1328   1.59  augustss 	struct ifaddr *ifa;
   1329  1.178     ozaki 	int s;
   1330  1.178     ozaki 
   1331  1.178     ozaki 	KASSERT(ifp != NULL);
   1332    1.1       cgd 
   1333   1.12   mycroft 	if (in.s_addr == INADDR_BROADCAST ||
   1334   1.32   mycroft 	    in_nullhost(in))
   1335   1.12   mycroft 		return 1;
   1336   1.12   mycroft 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
   1337   1.12   mycroft 		return 0;
   1338    1.1       cgd 	/*
   1339    1.1       cgd 	 * Look through the list of addresses for a match
   1340    1.1       cgd 	 * with a broadcast address.
   1341    1.1       cgd 	 */
   1342   1.22   mycroft #define ia (ifatoia(ifa))
   1343  1.178     ozaki 	s = pserialize_read_enter();
   1344  1.178     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   1345   1.12   mycroft 		if (ifa->ifa_addr->sa_family == AF_INET &&
   1346   1.75    itojun 		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
   1347   1.32   mycroft 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
   1348   1.32   mycroft 		     in_hosteq(in, ia->ia_netbroadcast) ||
   1349   1.77    itojun 		     (hostzeroisbroadcast &&
   1350   1.47  sommerfe 		      /*
   1351   1.47  sommerfe 		       * Check for old-style (host 0) broadcast.
   1352   1.47  sommerfe 		       */
   1353   1.47  sommerfe 		      (in.s_addr == ia->ia_subnet ||
   1354  1.178     ozaki 		       in.s_addr == ia->ia_net)))) {
   1355  1.178     ozaki 			pserialize_read_exit(s);
   1356   1.47  sommerfe 			return 1;
   1357  1.178     ozaki 		}
   1358  1.178     ozaki 	}
   1359  1.178     ozaki 	pserialize_read_exit(s);
   1360    1.1       cgd 	return (0);
   1361   1.12   mycroft #undef ia
   1362   1.64    itojun }
   1363   1.64    itojun 
   1364   1.64    itojun /*
   1365  1.152       roy  * perform DAD when interface becomes IFF_UP.
   1366  1.152       roy  */
   1367  1.152       roy void
   1368  1.152       roy in_if_link_up(struct ifnet *ifp)
   1369  1.152       roy {
   1370  1.152       roy 	struct ifaddr *ifa;
   1371  1.152       roy 	struct in_ifaddr *ia;
   1372  1.179     ozaki 	int s, bound;
   1373  1.152       roy 
   1374  1.152       roy 	/* Ensure it's sane to run DAD */
   1375  1.152       roy 	if (ifp->if_link_state == LINK_STATE_DOWN)
   1376  1.152       roy 		return;
   1377  1.152       roy 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
   1378  1.152       roy 		return;
   1379  1.152       roy 
   1380  1.179     ozaki 	bound = curlwp_bind();
   1381  1.179     ozaki 	s = pserialize_read_enter();
   1382  1.172     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   1383  1.179     ozaki 		struct psref psref;
   1384  1.179     ozaki 
   1385  1.152       roy 		if (ifa->ifa_addr->sa_family != AF_INET)
   1386  1.152       roy 			continue;
   1387  1.179     ozaki 		ifa_acquire(ifa, &psref);
   1388  1.179     ozaki 		pserialize_read_exit(s);
   1389  1.179     ozaki 
   1390  1.152       roy 		ia = (struct in_ifaddr *)ifa;
   1391  1.152       roy 
   1392  1.152       roy 		/* If detached then mark as tentative */
   1393  1.152       roy 		if (ia->ia4_flags & IN_IFF_DETACHED) {
   1394  1.152       roy 			ia->ia4_flags &= ~IN_IFF_DETACHED;
   1395  1.156       roy 			if (if_do_dad(ifp) && ia->ia_dad_start != NULL)
   1396  1.152       roy 				ia->ia4_flags |= IN_IFF_TENTATIVE;
   1397  1.156       roy 			else if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0)
   1398  1.152       roy 				rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1399  1.152       roy 		}
   1400  1.152       roy 
   1401  1.152       roy 		if (ia->ia4_flags & IN_IFF_TENTATIVE) {
   1402  1.152       roy 			/* Clear the duplicated flag as we're starting DAD. */
   1403  1.152       roy 			ia->ia4_flags &= ~IN_IFF_DUPLICATED;
   1404  1.156       roy 			ia->ia_dad_start(ifa);
   1405  1.152       roy 		}
   1406  1.179     ozaki 
   1407  1.179     ozaki 		s = pserialize_read_enter();
   1408  1.179     ozaki 		ifa_release(ifa, &psref);
   1409  1.152       roy 	}
   1410  1.179     ozaki 	pserialize_read_exit(s);
   1411  1.179     ozaki 	curlwp_bindx(bound);
   1412  1.152       roy }
   1413  1.152       roy 
   1414  1.152       roy void
   1415  1.152       roy in_if_up(struct ifnet *ifp)
   1416  1.152       roy {
   1417  1.152       roy 
   1418  1.152       roy 	/* interface may not support link state, so bring it up also */
   1419  1.152       roy 	in_if_link_up(ifp);
   1420  1.152       roy }
   1421  1.152       roy 
   1422  1.152       roy /*
   1423  1.152       roy  * Mark all addresses as detached.
   1424  1.152       roy  */
   1425  1.152       roy void
   1426  1.152       roy in_if_link_down(struct ifnet *ifp)
   1427  1.152       roy {
   1428  1.152       roy 	struct ifaddr *ifa;
   1429  1.152       roy 	struct in_ifaddr *ia;
   1430  1.179     ozaki 	int s, bound;
   1431  1.152       roy 
   1432  1.179     ozaki 	bound = curlwp_bind();
   1433  1.179     ozaki 	s = pserialize_read_enter();
   1434  1.172     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   1435  1.179     ozaki 		struct psref psref;
   1436  1.179     ozaki 
   1437  1.152       roy 		if (ifa->ifa_addr->sa_family != AF_INET)
   1438  1.152       roy 			continue;
   1439  1.179     ozaki 		ifa_acquire(ifa, &psref);
   1440  1.179     ozaki 		pserialize_read_exit(s);
   1441  1.179     ozaki 
   1442  1.152       roy 		ia = (struct in_ifaddr *)ifa;
   1443  1.152       roy 
   1444  1.152       roy 		/* Stop DAD processing */
   1445  1.156       roy 		if (ia->ia_dad_stop != NULL)
   1446  1.156       roy 			ia->ia_dad_stop(ifa);
   1447  1.152       roy 
   1448  1.152       roy 		/*
   1449  1.152       roy 		 * Mark the address as detached.
   1450  1.152       roy 		 */
   1451  1.152       roy 		if (!(ia->ia4_flags & IN_IFF_DETACHED)) {
   1452  1.152       roy 			ia->ia4_flags |= IN_IFF_DETACHED;
   1453  1.152       roy 			ia->ia4_flags &=
   1454  1.152       roy 			    ~(IN_IFF_TENTATIVE | IN_IFF_DUPLICATED);
   1455  1.152       roy 			rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1456  1.152       roy 		}
   1457  1.179     ozaki 
   1458  1.179     ozaki 		s = pserialize_read_enter();
   1459  1.179     ozaki 		ifa_release(ifa, &psref);
   1460  1.152       roy 	}
   1461  1.179     ozaki 	pserialize_read_exit(s);
   1462  1.179     ozaki 	curlwp_bindx(bound);
   1463  1.152       roy }
   1464  1.152       roy 
   1465  1.152       roy void
   1466  1.152       roy in_if_down(struct ifnet *ifp)
   1467  1.152       roy {
   1468  1.152       roy 
   1469  1.152       roy 	in_if_link_down(ifp);
   1470  1.152       roy }
   1471  1.152       roy 
   1472  1.152       roy void
   1473  1.152       roy in_if_link_state_change(struct ifnet *ifp, int link_state)
   1474  1.152       roy {
   1475  1.152       roy 
   1476  1.152       roy 	switch (link_state) {
   1477  1.152       roy 	case LINK_STATE_DOWN:
   1478  1.152       roy 		in_if_link_down(ifp);
   1479  1.152       roy 		break;
   1480  1.152       roy 	case LINK_STATE_UP:
   1481  1.152       roy 		in_if_link_up(ifp);
   1482  1.152       roy 		break;
   1483  1.152       roy 	}
   1484  1.152       roy }
   1485  1.152       roy 
   1486  1.152       roy /*
   1487  1.146     rmind  * in_lookup_multi: look up the in_multi record for a given IP
   1488  1.146     rmind  * multicast address on a given interface.  If no matching record is
   1489  1.146     rmind  * found, return NULL.
   1490  1.146     rmind  */
   1491  1.146     rmind struct in_multi *
   1492  1.146     rmind in_lookup_multi(struct in_addr addr, ifnet_t *ifp)
   1493  1.146     rmind {
   1494  1.146     rmind 	struct in_multi *inm;
   1495  1.146     rmind 
   1496  1.146     rmind 	KASSERT(rw_lock_held(&in_multilock));
   1497  1.146     rmind 
   1498  1.146     rmind 	LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) {
   1499  1.146     rmind 		if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp)
   1500  1.146     rmind 			break;
   1501  1.146     rmind 	}
   1502  1.146     rmind 	return inm;
   1503  1.146     rmind }
   1504  1.146     rmind 
   1505  1.146     rmind /*
   1506  1.146     rmind  * in_multi_group: check whether the address belongs to an IP multicast
   1507  1.146     rmind  * group we are joined on this interface.  Returns true or false.
   1508  1.146     rmind  */
   1509  1.146     rmind bool
   1510  1.146     rmind in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags)
   1511  1.146     rmind {
   1512  1.146     rmind 	bool ingroup;
   1513  1.146     rmind 
   1514  1.146     rmind 	if (__predict_true(flags & IP_IGMP_MCAST) == 0) {
   1515  1.146     rmind 		rw_enter(&in_multilock, RW_READER);
   1516  1.146     rmind 		ingroup = in_lookup_multi(addr, ifp) != NULL;
   1517  1.146     rmind 		rw_exit(&in_multilock);
   1518  1.146     rmind 	} else {
   1519  1.146     rmind 		/* XXX Recursive call from ip_output(). */
   1520  1.146     rmind 		KASSERT(rw_lock_held(&in_multilock));
   1521  1.146     rmind 		ingroup = in_lookup_multi(addr, ifp) != NULL;
   1522  1.146     rmind 	}
   1523  1.146     rmind 	return ingroup;
   1524  1.146     rmind }
   1525  1.146     rmind 
   1526  1.146     rmind /*
   1527    1.5   hpeyerl  * Add an address to the list of IP multicast addresses for a given interface.
   1528    1.5   hpeyerl  */
   1529    1.5   hpeyerl struct in_multi *
   1530  1.146     rmind in_addmulti(struct in_addr *ap, ifnet_t *ifp)
   1531    1.5   hpeyerl {
   1532  1.118    dyoung 	struct sockaddr_in sin;
   1533   1.59  augustss 	struct in_multi *inm;
   1534    1.5   hpeyerl 
   1535    1.5   hpeyerl 	/*
   1536    1.5   hpeyerl 	 * See if address already in list.
   1537    1.5   hpeyerl 	 */
   1538  1.146     rmind 	rw_enter(&in_multilock, RW_WRITER);
   1539  1.146     rmind 	inm = in_lookup_multi(*ap, ifp);
   1540    1.5   hpeyerl 	if (inm != NULL) {
   1541    1.5   hpeyerl 		/*
   1542    1.5   hpeyerl 		 * Found it; just increment the reference count.
   1543    1.5   hpeyerl 		 */
   1544  1.146     rmind 		inm->inm_refcount++;
   1545  1.146     rmind 		rw_exit(&in_multilock);
   1546  1.146     rmind 		return inm;
   1547  1.146     rmind 	}
   1548  1.146     rmind 
   1549  1.146     rmind 	/*
   1550  1.146     rmind 	 * New address; allocate a new multicast record.
   1551  1.146     rmind 	 */
   1552  1.146     rmind 	inm = pool_get(&inmulti_pool, PR_NOWAIT);
   1553  1.146     rmind 	if (inm == NULL) {
   1554  1.146     rmind 		rw_exit(&in_multilock);
   1555  1.146     rmind 		return NULL;
   1556  1.146     rmind 	}
   1557  1.146     rmind 	inm->inm_addr = *ap;
   1558  1.146     rmind 	inm->inm_ifp = ifp;
   1559  1.146     rmind 	inm->inm_refcount = 1;
   1560  1.146     rmind 
   1561  1.146     rmind 	/*
   1562  1.146     rmind 	 * Ask the network driver to update its multicast reception
   1563  1.146     rmind 	 * filter appropriately for the new address.
   1564  1.146     rmind 	 */
   1565  1.146     rmind 	sockaddr_in_init(&sin, ap, 0);
   1566  1.146     rmind 	if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
   1567  1.146     rmind 		rw_exit(&in_multilock);
   1568  1.146     rmind 		pool_put(&inmulti_pool, inm);
   1569  1.146     rmind 		return NULL;
   1570  1.146     rmind 	}
   1571  1.146     rmind 
   1572  1.146     rmind 	/*
   1573  1.146     rmind 	 * Let IGMP know that we have joined a new IP multicast group.
   1574  1.146     rmind 	 */
   1575  1.146     rmind 	if (igmp_joingroup(inm) != 0) {
   1576  1.146     rmind 		rw_exit(&in_multilock);
   1577  1.146     rmind 		pool_put(&inmulti_pool, inm);
   1578  1.146     rmind 		return NULL;
   1579    1.5   hpeyerl 	}
   1580  1.146     rmind 	LIST_INSERT_HEAD(
   1581  1.146     rmind 	    &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
   1582  1.146     rmind 	    inm, inm_list);
   1583  1.146     rmind 	in_multientries++;
   1584  1.146     rmind 	rw_exit(&in_multilock);
   1585  1.146     rmind 
   1586  1.146     rmind 	return inm;
   1587    1.5   hpeyerl }
   1588    1.5   hpeyerl 
   1589    1.5   hpeyerl /*
   1590    1.5   hpeyerl  * Delete a multicast address record.
   1591    1.5   hpeyerl  */
   1592   1.26  christos void
   1593  1.103     perry in_delmulti(struct in_multi *inm)
   1594    1.5   hpeyerl {
   1595  1.118    dyoung 	struct sockaddr_in sin;
   1596    1.5   hpeyerl 
   1597  1.146     rmind 	rw_enter(&in_multilock, RW_WRITER);
   1598  1.146     rmind 	if (--inm->inm_refcount > 0) {
   1599  1.146     rmind 		rw_exit(&in_multilock);
   1600  1.146     rmind 		return;
   1601  1.146     rmind 	}
   1602  1.146     rmind 
   1603  1.146     rmind 	/*
   1604  1.146     rmind 	 * No remaining claims to this record; let IGMP know that
   1605  1.146     rmind 	 * we are leaving the multicast group.
   1606  1.146     rmind 	 */
   1607  1.146     rmind 	igmp_leavegroup(inm);
   1608  1.146     rmind 
   1609  1.146     rmind 	/*
   1610  1.146     rmind 	 * Notify the network driver to update its multicast reception
   1611  1.146     rmind 	 * filter.
   1612  1.146     rmind 	 */
   1613  1.146     rmind 	sockaddr_in_init(&sin, &inm->inm_addr, 0);
   1614  1.146     rmind 	if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
   1615  1.146     rmind 
   1616  1.146     rmind 	/*
   1617  1.146     rmind 	 * Unlink from list.
   1618  1.146     rmind 	 */
   1619  1.146     rmind 	LIST_REMOVE(inm, inm_list);
   1620  1.146     rmind 	in_multientries--;
   1621  1.146     rmind 	rw_exit(&in_multilock);
   1622  1.146     rmind 
   1623  1.146     rmind 	pool_put(&inmulti_pool, inm);
   1624  1.146     rmind }
   1625  1.146     rmind 
   1626  1.146     rmind /*
   1627  1.146     rmind  * in_next_multi: step through all of the in_multi records, one at a time.
   1628  1.146     rmind  * The current position is remembered in "step", which the caller must
   1629  1.146     rmind  * provide.  in_first_multi(), below, must be called to initialize "step"
   1630  1.146     rmind  * and get the first record.  Both macros return a NULL "inm" when there
   1631  1.146     rmind  * are no remaining records.
   1632  1.146     rmind  */
   1633  1.146     rmind struct in_multi *
   1634  1.146     rmind in_next_multi(struct in_multistep *step)
   1635  1.146     rmind {
   1636  1.146     rmind 	struct in_multi *inm;
   1637  1.146     rmind 
   1638  1.146     rmind 	KASSERT(rw_lock_held(&in_multilock));
   1639  1.146     rmind 
   1640  1.146     rmind 	while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) {
   1641  1.146     rmind 		step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]);
   1642  1.146     rmind 	}
   1643  1.146     rmind 	if ((inm = step->i_inm) != NULL) {
   1644  1.146     rmind 		step->i_inm = LIST_NEXT(inm, inm_list);
   1645    1.5   hpeyerl 	}
   1646  1.146     rmind 	return inm;
   1647  1.146     rmind }
   1648  1.146     rmind 
   1649  1.146     rmind struct in_multi *
   1650  1.146     rmind in_first_multi(struct in_multistep *step)
   1651  1.146     rmind {
   1652  1.146     rmind 	KASSERT(rw_lock_held(&in_multilock));
   1653  1.146     rmind 
   1654  1.146     rmind 	step->i_n = 0;
   1655  1.146     rmind 	step->i_inm = LIST_FIRST(&in_multihashtbl[0]);
   1656  1.146     rmind 	return in_next_multi(step);
   1657  1.146     rmind }
   1658  1.146     rmind 
   1659  1.146     rmind void
   1660  1.146     rmind in_multi_lock(int op)
   1661  1.146     rmind {
   1662  1.146     rmind 	rw_enter(&in_multilock, op);
   1663  1.146     rmind }
   1664  1.146     rmind 
   1665  1.146     rmind void
   1666  1.146     rmind in_multi_unlock(void)
   1667  1.146     rmind {
   1668  1.146     rmind 	rw_exit(&in_multilock);
   1669  1.146     rmind }
   1670  1.146     rmind 
   1671  1.146     rmind int
   1672  1.146     rmind in_multi_lock_held(void)
   1673  1.146     rmind {
   1674  1.146     rmind 	return rw_lock_held(&in_multilock);
   1675    1.5   hpeyerl }
   1676  1.145     rmind 
   1677  1.178     ozaki struct in_ifaddr *
   1678  1.145     rmind in_selectsrc(struct sockaddr_in *sin, struct route *ro,
   1679  1.178     ozaki     int soopts, struct ip_moptions *mopts, int *errorp, struct psref *psref)
   1680  1.145     rmind {
   1681  1.145     rmind 	struct rtentry *rt = NULL;
   1682  1.145     rmind 	struct in_ifaddr *ia = NULL;
   1683  1.145     rmind 
   1684  1.178     ozaki 	KASSERT(ISSET(curlwp->l_pflag, LP_BOUND));
   1685  1.145     rmind 	/*
   1686  1.145     rmind          * If route is known or can be allocated now, take the
   1687  1.145     rmind          * source address from the interface.  Otherwise, punt.
   1688  1.145     rmind 	 */
   1689  1.145     rmind 	if ((soopts & SO_DONTROUTE) != 0)
   1690  1.145     rmind 		rtcache_free(ro);
   1691  1.145     rmind 	else {
   1692  1.145     rmind 		union {
   1693  1.145     rmind 			struct sockaddr		dst;
   1694  1.145     rmind 			struct sockaddr_in	dst4;
   1695  1.145     rmind 		} u;
   1696  1.145     rmind 
   1697  1.145     rmind 		sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
   1698  1.145     rmind 		rt = rtcache_lookup(ro, &u.dst);
   1699  1.145     rmind 	}
   1700  1.145     rmind 	/*
   1701  1.145     rmind 	 * If we found a route, use the address
   1702  1.145     rmind 	 * corresponding to the outgoing interface
   1703  1.145     rmind 	 * unless it is the loopback (in case a route
   1704  1.145     rmind 	 * to our address on another net goes to loopback).
   1705  1.145     rmind 	 *
   1706  1.145     rmind 	 * XXX Is this still true?  Do we care?
   1707  1.145     rmind 	 */
   1708  1.178     ozaki 	if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
   1709  1.178     ozaki 		int s;
   1710  1.178     ozaki 		struct ifaddr *ifa;
   1711  1.178     ozaki 		/*
   1712  1.178     ozaki 		 * Just in case. May not need to do this workaround.
   1713  1.178     ozaki 		 * Revisit when working on rtentry MP-ification.
   1714  1.178     ozaki 		 */
   1715  1.178     ozaki 		s = pserialize_read_enter();
   1716  1.178     ozaki 		IFADDR_READER_FOREACH(ifa, rt->rt_ifp) {
   1717  1.178     ozaki 			if (ifa == rt->rt_ifa)
   1718  1.178     ozaki 				break;
   1719  1.178     ozaki 		}
   1720  1.178     ozaki 		if (ifa != NULL)
   1721  1.178     ozaki 			ifa_acquire(ifa, psref);
   1722  1.178     ozaki 		pserialize_read_exit(s);
   1723  1.178     ozaki 
   1724  1.178     ozaki 		ia = ifatoia(ifa);
   1725  1.178     ozaki 	}
   1726  1.145     rmind 	if (ia == NULL) {
   1727  1.145     rmind 		u_int16_t fport = sin->sin_port;
   1728  1.178     ozaki 		struct ifaddr *ifa;
   1729  1.178     ozaki 		int s;
   1730  1.145     rmind 
   1731  1.145     rmind 		sin->sin_port = 0;
   1732  1.178     ozaki 		ifa = ifa_ifwithladdr_psref(sintosa(sin), psref);
   1733  1.145     rmind 		sin->sin_port = fport;
   1734  1.178     ozaki 		if (ifa == NULL) {
   1735  1.145     rmind 			/* Find 1st non-loopback AF_INET address */
   1736  1.178     ozaki 			s = pserialize_read_enter();
   1737  1.171     ozaki 			IN_ADDRLIST_READER_FOREACH(ia) {
   1738  1.145     rmind 				if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
   1739  1.145     rmind 					break;
   1740  1.145     rmind 			}
   1741  1.178     ozaki 			if (ia != NULL)
   1742  1.178     ozaki 				ia4_acquire(ia, psref);
   1743  1.178     ozaki 			pserialize_read_exit(s);
   1744  1.178     ozaki 		} else {
   1745  1.178     ozaki 			/* ia is already referenced by psref */
   1746  1.178     ozaki 			ia = ifatoia(ifa);
   1747  1.145     rmind 		}
   1748  1.145     rmind 		if (ia == NULL) {
   1749  1.145     rmind 			*errorp = EADDRNOTAVAIL;
   1750  1.145     rmind 			return NULL;
   1751  1.145     rmind 		}
   1752  1.145     rmind 	}
   1753  1.145     rmind 	/*
   1754  1.145     rmind 	 * If the destination address is multicast and an outgoing
   1755  1.145     rmind 	 * interface has been set as a multicast option, use the
   1756  1.145     rmind 	 * address of that interface as our source address.
   1757  1.145     rmind 	 */
   1758  1.145     rmind 	if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
   1759  1.145     rmind 		struct ip_moptions *imo;
   1760  1.145     rmind 
   1761  1.145     rmind 		imo = mopts;
   1762  1.167     ozaki 		if (imo->imo_multicast_if_index != 0) {
   1763  1.167     ozaki 			struct ifnet *ifp;
   1764  1.178     ozaki 			int s;
   1765  1.167     ozaki 
   1766  1.178     ozaki 			if (ia != NULL)
   1767  1.178     ozaki 				ia4_release(ia, psref);
   1768  1.178     ozaki 			s = pserialize_read_enter();
   1769  1.167     ozaki 			ifp = if_byindex(imo->imo_multicast_if_index);
   1770  1.167     ozaki 			if (ifp != NULL) {
   1771  1.178     ozaki 				/* XXX */
   1772  1.178     ozaki 				ia = in_get_ia_from_ifp_psref(ifp, psref);
   1773  1.167     ozaki 			} else
   1774  1.167     ozaki 				ia = NULL;
   1775  1.167     ozaki 			if (ia == NULL || ia->ia4_flags & IN_IFF_NOTREADY) {
   1776  1.167     ozaki 				pserialize_read_exit(s);
   1777  1.178     ozaki 				if (ia != NULL)
   1778  1.178     ozaki 					ia4_release(ia, psref);
   1779  1.145     rmind 				*errorp = EADDRNOTAVAIL;
   1780  1.145     rmind 				return NULL;
   1781  1.145     rmind 			}
   1782  1.167     ozaki 			pserialize_read_exit(s);
   1783  1.145     rmind 		}
   1784  1.145     rmind 	}
   1785  1.145     rmind 	if (ia->ia_ifa.ifa_getifa != NULL) {
   1786  1.145     rmind 		ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
   1787  1.145     rmind 		                                      sintosa(sin)));
   1788  1.152       roy 		if (ia == NULL) {
   1789  1.152       roy 			*errorp = EADDRNOTAVAIL;
   1790  1.152       roy 			return NULL;
   1791  1.152       roy 		}
   1792  1.178     ozaki 		/* FIXME NOMPSAFE */
   1793  1.178     ozaki 		ia4_acquire(ia, psref);
   1794  1.145     rmind 	}
   1795  1.145     rmind #ifdef GETIFA_DEBUG
   1796  1.145     rmind 	else
   1797  1.145     rmind 		printf("%s: missing ifa_getifa\n", __func__);
   1798  1.145     rmind #endif
   1799  1.178     ozaki 	return ia;
   1800  1.145     rmind }
   1801  1.145     rmind 
   1802  1.163     ozaki #if NARP > 0
   1803  1.161     ozaki 
   1804  1.158     ozaki struct in_llentry {
   1805  1.158     ozaki 	struct llentry		base;
   1806  1.158     ozaki };
   1807  1.158     ozaki 
   1808  1.158     ozaki #define	IN_LLTBL_DEFAULT_HSIZE	32
   1809  1.158     ozaki #define	IN_LLTBL_HASH(k, h) \
   1810  1.158     ozaki 	(((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
   1811  1.158     ozaki 
   1812  1.158     ozaki /*
   1813  1.158     ozaki  * Do actual deallocation of @lle.
   1814  1.158     ozaki  * Called by LLE_FREE_LOCKED when number of references
   1815  1.158     ozaki  * drops to zero.
   1816  1.158     ozaki  */
   1817  1.158     ozaki static void
   1818  1.158     ozaki in_lltable_destroy_lle(struct llentry *lle)
   1819  1.158     ozaki {
   1820  1.158     ozaki 
   1821  1.158     ozaki 	LLE_WUNLOCK(lle);
   1822  1.158     ozaki 	LLE_LOCK_DESTROY(lle);
   1823  1.158     ozaki 	kmem_intr_free(lle, sizeof(*lle));
   1824  1.158     ozaki }
   1825  1.158     ozaki 
   1826  1.158     ozaki static struct llentry *
   1827  1.158     ozaki in_lltable_new(struct in_addr addr4, u_int flags)
   1828  1.158     ozaki {
   1829  1.158     ozaki 	struct in_llentry *lle;
   1830  1.158     ozaki 
   1831  1.158     ozaki 	lle = kmem_intr_zalloc(sizeof(*lle), KM_NOSLEEP);
   1832  1.158     ozaki 	if (lle == NULL)		/* NB: caller generates msg */
   1833  1.158     ozaki 		return NULL;
   1834  1.158     ozaki 
   1835  1.158     ozaki 	/*
   1836  1.158     ozaki 	 * For IPv4 this will trigger "arpresolve" to generate
   1837  1.158     ozaki 	 * an ARP request.
   1838  1.158     ozaki 	 */
   1839  1.158     ozaki 	lle->base.la_expire = time_uptime; /* mark expired */
   1840  1.158     ozaki 	lle->base.r_l3addr.addr4 = addr4;
   1841  1.158     ozaki 	lle->base.lle_refcnt = 1;
   1842  1.158     ozaki 	lle->base.lle_free = in_lltable_destroy_lle;
   1843  1.158     ozaki 	LLE_LOCK_INIT(&lle->base);
   1844  1.159     ozaki 	callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
   1845  1.158     ozaki 
   1846  1.158     ozaki 	return (&lle->base);
   1847  1.158     ozaki }
   1848  1.158     ozaki 
   1849  1.158     ozaki #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(			\
   1850  1.158     ozaki 	    (((ntohl((d).s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
   1851  1.158     ozaki 
   1852  1.158     ozaki static int
   1853  1.158     ozaki in_lltable_match_prefix(const struct sockaddr *prefix,
   1854  1.158     ozaki     const struct sockaddr *mask, u_int flags, struct llentry *lle)
   1855  1.158     ozaki {
   1856  1.158     ozaki 	const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
   1857  1.158     ozaki 	const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
   1858  1.158     ozaki 
   1859  1.158     ozaki 	/*
   1860  1.158     ozaki 	 * (flags & LLE_STATIC) means deleting all entries
   1861  1.158     ozaki 	 * including static ARP entries.
   1862  1.158     ozaki 	 */
   1863  1.158     ozaki 	if (IN_ARE_MASKED_ADDR_EQUAL(lle->r_l3addr.addr4, pfx, msk) &&
   1864  1.158     ozaki 	    ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)))
   1865  1.158     ozaki 		return (1);
   1866  1.158     ozaki 
   1867  1.158     ozaki 	return (0);
   1868  1.158     ozaki }
   1869  1.158     ozaki 
   1870  1.158     ozaki static void
   1871  1.158     ozaki in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
   1872  1.158     ozaki {
   1873  1.160     ozaki 	struct ifnet *ifp __diagused;
   1874  1.158     ozaki 	size_t pkts_dropped;
   1875  1.158     ozaki 
   1876  1.158     ozaki 	LLE_WLOCK_ASSERT(lle);
   1877  1.158     ozaki 	KASSERT(llt != NULL);
   1878  1.158     ozaki 
   1879  1.158     ozaki 	/* Unlink entry from table if not already */
   1880  1.158     ozaki 	if ((lle->la_flags & LLE_LINKED) != 0) {
   1881  1.158     ozaki 		ifp = llt->llt_ifp;
   1882  1.158     ozaki 		IF_AFDATA_WLOCK_ASSERT(ifp);
   1883  1.158     ozaki 		lltable_unlink_entry(llt, lle);
   1884  1.158     ozaki 	}
   1885  1.158     ozaki 
   1886  1.158     ozaki 	/* cancel timer */
   1887  1.164     ozaki 	if (callout_halt(&lle->lle_timer, &lle->lle_lock))
   1888  1.158     ozaki 		LLE_REMREF(lle);
   1889  1.158     ozaki 
   1890  1.158     ozaki 	/* Drop hold queue */
   1891  1.158     ozaki 	pkts_dropped = llentry_free(lle);
   1892  1.159     ozaki 	arp_stat_add(ARP_STAT_DFRDROPPED, (uint64_t)pkts_dropped);
   1893  1.158     ozaki }
   1894  1.158     ozaki 
   1895  1.158     ozaki static int
   1896  1.158     ozaki in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
   1897  1.158     ozaki {
   1898  1.158     ozaki 	struct rtentry *rt;
   1899  1.158     ozaki 	int error = EINVAL;
   1900  1.158     ozaki 
   1901  1.158     ozaki 	KASSERTMSG(l3addr->sa_family == AF_INET,
   1902  1.158     ozaki 	    "sin_family %d", l3addr->sa_family);
   1903  1.158     ozaki 
   1904  1.158     ozaki 	rt = rtalloc1(l3addr, 0);
   1905  1.158     ozaki 	if (rt == NULL)
   1906  1.158     ozaki 		return error;
   1907  1.158     ozaki 
   1908  1.158     ozaki 	/*
   1909  1.158     ozaki 	 * If the gateway for an existing host route matches the target L3
   1910  1.158     ozaki 	 * address, which is a special route inserted by some implementation
   1911  1.158     ozaki 	 * such as MANET, and the interface is of the correct type, then
   1912  1.158     ozaki 	 * allow for ARP to proceed.
   1913  1.158     ozaki 	 */
   1914  1.158     ozaki 	if (rt->rt_flags & RTF_GATEWAY) {
   1915  1.158     ozaki 		if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
   1916  1.158     ozaki 		    rt->rt_ifp->if_type != IFT_ETHER ||
   1917  1.158     ozaki #ifdef __FreeBSD__
   1918  1.158     ozaki 		    (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
   1919  1.159     ozaki #else
   1920  1.158     ozaki 		    (rt->rt_ifp->if_flags & IFF_NOARP) != 0 ||
   1921  1.158     ozaki #endif
   1922  1.158     ozaki 		    memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
   1923  1.158     ozaki 		    sizeof(in_addr_t)) != 0) {
   1924  1.158     ozaki 			goto error;
   1925  1.158     ozaki 		}
   1926  1.158     ozaki 	}
   1927  1.158     ozaki 
   1928  1.158     ozaki 	/*
   1929  1.158     ozaki 	 * Make sure that at least the destination address is covered
   1930  1.158     ozaki 	 * by the route. This is for handling the case where 2 or more
   1931  1.158     ozaki 	 * interfaces have the same prefix. An incoming packet arrives
   1932  1.158     ozaki 	 * on one interface and the corresponding outgoing packet leaves
   1933  1.158     ozaki 	 * another interface.
   1934  1.158     ozaki 	 */
   1935  1.158     ozaki 	if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
   1936  1.158     ozaki 		const char *sa, *mask, *addr, *lim;
   1937  1.158     ozaki 		int len;
   1938  1.158     ozaki 
   1939  1.158     ozaki 		mask = (const char *)rt_mask(rt);
   1940  1.158     ozaki 		/*
   1941  1.158     ozaki 		 * Just being extra cautious to avoid some custom
   1942  1.158     ozaki 		 * code getting into trouble.
   1943  1.158     ozaki 		 */
   1944  1.158     ozaki 		if (mask == NULL)
   1945  1.158     ozaki 			goto error;
   1946  1.158     ozaki 
   1947  1.158     ozaki 		sa = (const char *)rt_getkey(rt);
   1948  1.158     ozaki 		addr = (const char *)l3addr;
   1949  1.158     ozaki 		len = ((const struct sockaddr_in *)l3addr)->sin_len;
   1950  1.158     ozaki 		lim = addr + len;
   1951  1.158     ozaki 
   1952  1.158     ozaki 		for ( ; addr < lim; sa++, mask++, addr++) {
   1953  1.158     ozaki 			if ((*sa ^ *addr) & *mask) {
   1954  1.158     ozaki #ifdef DIAGNOSTIC
   1955  1.158     ozaki 				log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
   1956  1.158     ozaki 				    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
   1957  1.158     ozaki #endif
   1958  1.158     ozaki 				goto error;
   1959  1.158     ozaki 			}
   1960  1.158     ozaki 		}
   1961  1.158     ozaki 	}
   1962  1.158     ozaki 
   1963  1.158     ozaki 	error = 0;
   1964  1.158     ozaki error:
   1965  1.162     ozaki 	rtfree(rt);
   1966  1.158     ozaki 	return error;
   1967  1.158     ozaki }
   1968  1.158     ozaki 
   1969  1.158     ozaki static inline uint32_t
   1970  1.158     ozaki in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
   1971  1.158     ozaki {
   1972  1.158     ozaki 
   1973  1.158     ozaki 	return (IN_LLTBL_HASH(dst.s_addr, hsize));
   1974  1.158     ozaki }
   1975  1.158     ozaki 
   1976  1.158     ozaki static uint32_t
   1977  1.158     ozaki in_lltable_hash(const struct llentry *lle, uint32_t hsize)
   1978  1.158     ozaki {
   1979  1.158     ozaki 
   1980  1.158     ozaki 	return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
   1981  1.158     ozaki }
   1982  1.158     ozaki 
   1983  1.158     ozaki static void
   1984  1.158     ozaki in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
   1985  1.158     ozaki {
   1986  1.158     ozaki 	struct sockaddr_in *sin;
   1987  1.158     ozaki 
   1988  1.158     ozaki 	sin = (struct sockaddr_in *)sa;
   1989  1.158     ozaki 	memset(sin, 0, sizeof(*sin));
   1990  1.158     ozaki 	sin->sin_family = AF_INET;
   1991  1.158     ozaki 	sin->sin_len = sizeof(*sin);
   1992  1.158     ozaki 	sin->sin_addr = lle->r_l3addr.addr4;
   1993  1.158     ozaki }
   1994  1.158     ozaki 
   1995  1.158     ozaki static inline struct llentry *
   1996  1.158     ozaki in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
   1997  1.158     ozaki {
   1998  1.158     ozaki 	struct llentry *lle;
   1999  1.158     ozaki 	struct llentries *lleh;
   2000  1.158     ozaki 	u_int hashidx;
   2001  1.158     ozaki 
   2002  1.158     ozaki 	hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
   2003  1.158     ozaki 	lleh = &llt->lle_head[hashidx];
   2004  1.158     ozaki 	LIST_FOREACH(lle, lleh, lle_next) {
   2005  1.158     ozaki 		if (lle->la_flags & LLE_DELETED)
   2006  1.158     ozaki 			continue;
   2007  1.158     ozaki 		if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
   2008  1.158     ozaki 			break;
   2009  1.158     ozaki 	}
   2010  1.158     ozaki 
   2011  1.158     ozaki 	return (lle);
   2012  1.158     ozaki }
   2013  1.158     ozaki 
   2014  1.158     ozaki static int
   2015  1.158     ozaki in_lltable_delete(struct lltable *llt, u_int flags,
   2016  1.158     ozaki     const struct sockaddr *l3addr)
   2017  1.158     ozaki {
   2018  1.158     ozaki 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
   2019  1.160     ozaki 	struct ifnet *ifp __diagused = llt->llt_ifp;
   2020  1.158     ozaki 	struct llentry *lle;
   2021  1.158     ozaki 
   2022  1.158     ozaki 	IF_AFDATA_WLOCK_ASSERT(ifp);
   2023  1.158     ozaki 	KASSERTMSG(l3addr->sa_family == AF_INET,
   2024  1.158     ozaki 	    "sin_family %d", l3addr->sa_family);
   2025  1.158     ozaki 
   2026  1.158     ozaki 	lle = in_lltable_find_dst(llt, sin->sin_addr);
   2027  1.158     ozaki 	if (lle == NULL) {
   2028  1.158     ozaki #ifdef DIAGNOSTIC
   2029  1.158     ozaki 		log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
   2030  1.158     ozaki #endif
   2031  1.158     ozaki 		return (ENOENT);
   2032  1.158     ozaki 	}
   2033  1.158     ozaki 
   2034  1.165     ozaki 	LLE_WLOCK(lle);
   2035  1.165     ozaki 	lle->la_flags |= LLE_DELETED;
   2036  1.158     ozaki #ifdef DIAGNOSTIC
   2037  1.165     ozaki 	log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
   2038  1.158     ozaki #endif
   2039  1.165     ozaki 	if ((lle->la_flags & (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
   2040  1.165     ozaki 		llentry_free(lle);
   2041  1.165     ozaki 	else
   2042  1.165     ozaki 		LLE_WUNLOCK(lle);
   2043  1.158     ozaki 
   2044  1.158     ozaki 	return (0);
   2045  1.158     ozaki }
   2046  1.158     ozaki 
   2047  1.158     ozaki static struct llentry *
   2048  1.158     ozaki in_lltable_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
   2049  1.158     ozaki {
   2050  1.158     ozaki 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
   2051  1.158     ozaki 	struct ifnet *ifp = llt->llt_ifp;
   2052  1.158     ozaki 	struct llentry *lle;
   2053  1.158     ozaki 
   2054  1.158     ozaki 	IF_AFDATA_WLOCK_ASSERT(ifp);
   2055  1.158     ozaki 	KASSERTMSG(l3addr->sa_family == AF_INET,
   2056  1.158     ozaki 	    "sin_family %d", l3addr->sa_family);
   2057  1.158     ozaki 
   2058  1.158     ozaki 	lle = in_lltable_find_dst(llt, sin->sin_addr);
   2059  1.158     ozaki 
   2060  1.158     ozaki 	if (lle != NULL) {
   2061  1.158     ozaki 		LLE_WLOCK(lle);
   2062  1.158     ozaki 		return (lle);
   2063  1.158     ozaki 	}
   2064  1.158     ozaki 
   2065  1.158     ozaki 	/* no existing record, we need to create new one */
   2066  1.158     ozaki 
   2067  1.158     ozaki 	/*
   2068  1.158     ozaki 	 * A route that covers the given address must have
   2069  1.158     ozaki 	 * been installed 1st because we are doing a resolution,
   2070  1.158     ozaki 	 * verify this.
   2071  1.158     ozaki 	 */
   2072  1.158     ozaki 	if (!(flags & LLE_IFADDR) &&
   2073  1.158     ozaki 	    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
   2074  1.158     ozaki 		return (NULL);
   2075  1.158     ozaki 
   2076  1.158     ozaki 	lle = in_lltable_new(sin->sin_addr, flags);
   2077  1.158     ozaki 	if (lle == NULL) {
   2078  1.158     ozaki 		log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
   2079  1.158     ozaki 		return (NULL);
   2080  1.158     ozaki 	}
   2081  1.158     ozaki 	lle->la_flags = flags;
   2082  1.158     ozaki 	if ((flags & LLE_IFADDR) == LLE_IFADDR) {
   2083  1.158     ozaki 		memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
   2084  1.158     ozaki 		lle->la_flags |= (LLE_VALID | LLE_STATIC);
   2085  1.158     ozaki 	}
   2086  1.158     ozaki 
   2087  1.158     ozaki 	lltable_link_entry(llt, lle);
   2088  1.158     ozaki 	LLE_WLOCK(lle);
   2089  1.158     ozaki 
   2090  1.158     ozaki 	return (lle);
   2091  1.158     ozaki }
   2092  1.158     ozaki 
   2093  1.158     ozaki /*
   2094  1.158     ozaki  * Return NULL if not found or marked for deletion.
   2095  1.158     ozaki  * If found return lle read locked.
   2096  1.158     ozaki  */
   2097  1.158     ozaki static struct llentry *
   2098  1.158     ozaki in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
   2099  1.158     ozaki {
   2100  1.158     ozaki 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
   2101  1.158     ozaki 	struct llentry *lle;
   2102  1.158     ozaki 
   2103  1.158     ozaki 	IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
   2104  1.158     ozaki 	KASSERTMSG(l3addr->sa_family == AF_INET,
   2105  1.158     ozaki 	    "sin_family %d", l3addr->sa_family);
   2106  1.158     ozaki 
   2107  1.158     ozaki 	lle = in_lltable_find_dst(llt, sin->sin_addr);
   2108  1.158     ozaki 
   2109  1.158     ozaki 	if (lle == NULL)
   2110  1.158     ozaki 		return NULL;
   2111  1.158     ozaki 
   2112  1.158     ozaki 	if (flags & LLE_EXCLUSIVE)
   2113  1.158     ozaki 		LLE_WLOCK(lle);
   2114  1.158     ozaki 	else
   2115  1.158     ozaki 		LLE_RLOCK(lle);
   2116  1.158     ozaki 
   2117  1.158     ozaki 	return lle;
   2118  1.158     ozaki }
   2119  1.158     ozaki 
   2120  1.165     ozaki static int
   2121  1.165     ozaki in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
   2122  1.165     ozaki     struct rt_walkarg *w)
   2123  1.165     ozaki {
   2124  1.165     ozaki 	struct sockaddr_in sin;
   2125  1.165     ozaki 
   2126  1.165     ozaki 	LLTABLE_LOCK_ASSERT();
   2127  1.165     ozaki 
   2128  1.165     ozaki 	/* skip deleted entries */
   2129  1.165     ozaki 	if (lle->la_flags & LLE_DELETED)
   2130  1.165     ozaki 		return 0;
   2131  1.165     ozaki 
   2132  1.165     ozaki 	sockaddr_in_init(&sin, &lle->r_l3addr.addr4, 0);
   2133  1.165     ozaki 
   2134  1.165     ozaki 	return lltable_dump_entry(llt, lle, w, sintosa(&sin));
   2135  1.165     ozaki }
   2136  1.165     ozaki 
   2137  1.163     ozaki #endif /* NARP > 0 */
   2138  1.161     ozaki 
   2139  1.175  christos static int
   2140  1.175  christos in_multicast_sysctl(SYSCTLFN_ARGS)
   2141  1.175  christos {
   2142  1.175  christos 	struct ifnet *ifp;
   2143  1.175  christos 	struct ifaddr *ifa;
   2144  1.175  christos 	struct in_ifaddr *ifa4;
   2145  1.175  christos 	struct in_multi *inm;
   2146  1.175  christos 	uint32_t tmp;
   2147  1.175  christos 	int error;
   2148  1.175  christos 	size_t written;
   2149  1.175  christos 	struct psref psref;
   2150  1.175  christos 	int bound;
   2151  1.175  christos 
   2152  1.175  christos 	if (namelen != 1)
   2153  1.175  christos 		return EINVAL;
   2154  1.175  christos 
   2155  1.175  christos 	bound = curlwp_bind();
   2156  1.175  christos 	ifp = if_get_byindex(name[0], &psref);
   2157  1.175  christos 	if (ifp == NULL) {
   2158  1.175  christos 		curlwp_bindx(bound);
   2159  1.175  christos 		return ENODEV;
   2160  1.175  christos 	}
   2161  1.175  christos 
   2162  1.175  christos 	if (oldp == NULL) {
   2163  1.175  christos 		*oldlenp = 0;
   2164  1.175  christos 		IFADDR_FOREACH(ifa, ifp) {
   2165  1.175  christos 			if (ifa->ifa_addr->sa_family != AF_INET)
   2166  1.175  christos 				continue;
   2167  1.175  christos 			ifa4 = (void *)ifa;
   2168  1.175  christos 			LIST_FOREACH(inm, &ifa4->ia_multiaddrs, inm_list) {
   2169  1.175  christos 				*oldlenp += 2 * sizeof(struct in_addr) +
   2170  1.175  christos 				    sizeof(uint32_t);
   2171  1.175  christos 			}
   2172  1.175  christos 		}
   2173  1.175  christos 		if_put(ifp, &psref);
   2174  1.175  christos 		curlwp_bindx(bound);
   2175  1.175  christos 		return 0;
   2176  1.175  christos 	}
   2177  1.175  christos 
   2178  1.175  christos 	error = 0;
   2179  1.175  christos 	written = 0;
   2180  1.175  christos 	IFADDR_FOREACH(ifa, ifp) {
   2181  1.175  christos 		if (ifa->ifa_addr->sa_family != AF_INET)
   2182  1.175  christos 			continue;
   2183  1.175  christos 		ifa4 = (void *)ifa;
   2184  1.175  christos 		LIST_FOREACH(inm, &ifa4->ia_multiaddrs, inm_list) {
   2185  1.175  christos 			if (written + 2 * sizeof(struct in_addr) +
   2186  1.175  christos 			    sizeof(uint32_t) > *oldlenp)
   2187  1.175  christos 				goto done;
   2188  1.175  christos 			error = sysctl_copyout(l, &ifa4->ia_addr.sin_addr,
   2189  1.175  christos 			    oldp, sizeof(struct in_addr));
   2190  1.175  christos 			if (error)
   2191  1.175  christos 				goto done;
   2192  1.175  christos 			oldp = (char *)oldp + sizeof(struct in_addr);
   2193  1.175  christos 			written += sizeof(struct in_addr);
   2194  1.175  christos 			error = sysctl_copyout(l, &inm->inm_addr,
   2195  1.175  christos 			    oldp, sizeof(struct in_addr));
   2196  1.175  christos 			if (error)
   2197  1.175  christos 				goto done;
   2198  1.175  christos 			oldp = (char *)oldp + sizeof(struct in_addr);
   2199  1.175  christos 			written += sizeof(struct in_addr);
   2200  1.175  christos 			tmp = inm->inm_refcount;
   2201  1.175  christos 			error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
   2202  1.175  christos 			if (error)
   2203  1.175  christos 				goto done;
   2204  1.175  christos 			oldp = (char *)oldp + sizeof(tmp);
   2205  1.175  christos 			written += sizeof(tmp);
   2206  1.175  christos 		}
   2207  1.175  christos 	}
   2208  1.175  christos done:
   2209  1.175  christos 	if_put(ifp, &psref);
   2210  1.175  christos 	curlwp_bindx(bound);
   2211  1.175  christos 	*oldlenp = written;
   2212  1.175  christos 	return error;
   2213  1.175  christos }
   2214  1.175  christos 
   2215  1.145     rmind static void
   2216  1.145     rmind in_sysctl_init(struct sysctllog **clog)
   2217  1.145     rmind {
   2218  1.145     rmind 	sysctl_createv(clog, 0, NULL, NULL,
   2219  1.145     rmind 		       CTLFLAG_PERMANENT,
   2220  1.145     rmind 		       CTLTYPE_NODE, "inet",
   2221  1.145     rmind 		       SYSCTL_DESCR("PF_INET related settings"),
   2222  1.145     rmind 		       NULL, 0, NULL, 0,
   2223  1.145     rmind 		       CTL_NET, PF_INET, CTL_EOL);
   2224  1.145     rmind 	sysctl_createv(clog, 0, NULL, NULL,
   2225  1.145     rmind 		       CTLFLAG_PERMANENT,
   2226  1.175  christos 		       CTLTYPE_NODE, "multicast",
   2227  1.175  christos 		       SYSCTL_DESCR("Multicast information"),
   2228  1.175  christos 		       in_multicast_sysctl, 0, NULL, 0,
   2229  1.175  christos 		       CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
   2230  1.175  christos 	sysctl_createv(clog, 0, NULL, NULL,
   2231  1.175  christos 		       CTLFLAG_PERMANENT,
   2232  1.145     rmind 		       CTLTYPE_NODE, "ip",
   2233  1.145     rmind 		       SYSCTL_DESCR("IPv4 related settings"),
   2234  1.145     rmind 		       NULL, 0, NULL, 0,
   2235  1.145     rmind 		       CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
   2236  1.145     rmind 
   2237  1.145     rmind 	sysctl_createv(clog, 0, NULL, NULL,
   2238  1.145     rmind 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2239  1.145     rmind 		       CTLTYPE_INT, "subnetsarelocal",
   2240  1.145     rmind 		       SYSCTL_DESCR("Whether logical subnets are considered "
   2241  1.145     rmind 				    "local"),
   2242  1.145     rmind 		       NULL, 0, &subnetsarelocal, 0,
   2243  1.145     rmind 		       CTL_NET, PF_INET, IPPROTO_IP,
   2244  1.145     rmind 		       IPCTL_SUBNETSARELOCAL, CTL_EOL);
   2245  1.145     rmind 	sysctl_createv(clog, 0, NULL, NULL,
   2246  1.145     rmind 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2247  1.145     rmind 		       CTLTYPE_INT, "hostzerobroadcast",
   2248  1.145     rmind 		       SYSCTL_DESCR("All zeroes address is broadcast address"),
   2249  1.145     rmind 		       NULL, 0, &hostzeroisbroadcast, 0,
   2250  1.145     rmind 		       CTL_NET, PF_INET, IPPROTO_IP,
   2251  1.145     rmind 		       IPCTL_HOSTZEROBROADCAST, CTL_EOL);
   2252  1.145     rmind }
   2253  1.158     ozaki 
   2254  1.163     ozaki #if NARP > 0
   2255  1.163     ozaki 
   2256  1.158     ozaki static struct lltable *
   2257  1.158     ozaki in_lltattach(struct ifnet *ifp)
   2258  1.158     ozaki {
   2259  1.158     ozaki 	struct lltable *llt;
   2260  1.158     ozaki 
   2261  1.158     ozaki 	llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
   2262  1.158     ozaki 	llt->llt_af = AF_INET;
   2263  1.158     ozaki 	llt->llt_ifp = ifp;
   2264  1.158     ozaki 
   2265  1.158     ozaki 	llt->llt_lookup = in_lltable_lookup;
   2266  1.158     ozaki 	llt->llt_create = in_lltable_create;
   2267  1.158     ozaki 	llt->llt_delete = in_lltable_delete;
   2268  1.158     ozaki 	llt->llt_dump_entry = in_lltable_dump_entry;
   2269  1.158     ozaki 	llt->llt_hash = in_lltable_hash;
   2270  1.158     ozaki 	llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
   2271  1.158     ozaki 	llt->llt_free_entry = in_lltable_free_entry;
   2272  1.158     ozaki 	llt->llt_match_prefix = in_lltable_match_prefix;
   2273  1.158     ozaki 	lltable_link(llt);
   2274  1.158     ozaki 
   2275  1.158     ozaki 	return (llt);
   2276  1.158     ozaki }
   2277  1.163     ozaki 
   2278  1.163     ozaki #endif /* NARP > 0 */
   2279  1.158     ozaki 
   2280  1.158     ozaki void *
   2281  1.158     ozaki in_domifattach(struct ifnet *ifp)
   2282  1.158     ozaki {
   2283  1.158     ozaki 	struct in_ifinfo *ii;
   2284  1.158     ozaki 
   2285  1.158     ozaki 	ii = kmem_zalloc(sizeof(struct in_ifinfo), KM_SLEEP);
   2286  1.158     ozaki 	KASSERT(ii != NULL);
   2287  1.158     ozaki 
   2288  1.163     ozaki #if NARP > 0
   2289  1.158     ozaki 	ii->ii_llt = in_lltattach(ifp);
   2290  1.161     ozaki #endif
   2291  1.158     ozaki 
   2292  1.158     ozaki #ifdef IPSELSRC
   2293  1.158     ozaki 	ii->ii_selsrc = in_selsrc_domifattach(ifp);
   2294  1.158     ozaki 	KASSERT(ii->ii_selsrc != NULL);
   2295  1.158     ozaki #endif
   2296  1.158     ozaki 
   2297  1.158     ozaki 	return ii;
   2298  1.158     ozaki }
   2299  1.158     ozaki 
   2300  1.158     ozaki void
   2301  1.158     ozaki in_domifdetach(struct ifnet *ifp, void *aux)
   2302  1.158     ozaki {
   2303  1.158     ozaki 	struct in_ifinfo *ii = aux;
   2304  1.158     ozaki 
   2305  1.158     ozaki #ifdef IPSELSRC
   2306  1.158     ozaki 	in_selsrc_domifdetach(ifp, ii->ii_selsrc);
   2307  1.158     ozaki #endif
   2308  1.163     ozaki #if NARP > 0
   2309  1.158     ozaki 	lltable_free(ii->ii_llt);
   2310  1.161     ozaki #endif
   2311  1.158     ozaki 	kmem_free(ii, sizeof(struct in_ifinfo));
   2312  1.158     ozaki }
   2313