Home | History | Annotate | Line # | Download | only in netinet
in_pcb.c revision 1.184
      1  1.184  riastrad /*	$NetBSD: in_pcb.c,v 1.184 2020/08/20 21:21:32 riastradh Exp $	*/
      2   1.59    itojun 
      3   1.59    itojun /*
      4   1.59    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5   1.59    itojun  * All rights reserved.
      6   1.78    itojun  *
      7   1.59    itojun  * Redistribution and use in source and binary forms, with or without
      8   1.59    itojun  * modification, are permitted provided that the following conditions
      9   1.59    itojun  * are met:
     10   1.59    itojun  * 1. Redistributions of source code must retain the above copyright
     11   1.59    itojun  *    notice, this list of conditions and the following disclaimer.
     12   1.59    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.59    itojun  *    notice, this list of conditions and the following disclaimer in the
     14   1.59    itojun  *    documentation and/or other materials provided with the distribution.
     15   1.59    itojun  * 3. Neither the name of the project nor the names of its contributors
     16   1.59    itojun  *    may be used to endorse or promote products derived from this software
     17   1.59    itojun  *    without specific prior written permission.
     18   1.78    itojun  *
     19   1.59    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20   1.59    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.59    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.59    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23   1.59    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.59    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.59    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.59    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.59    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.59    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.59    itojun  * SUCH DAMAGE.
     30   1.59    itojun  */
     31   1.57   thorpej 
     32   1.57   thorpej /*-
     33  1.138    dyoung  * Copyright (c) 1998, 2011 The NetBSD Foundation, Inc.
     34   1.57   thorpej  * All rights reserved.
     35   1.57   thorpej  *
     36   1.57   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     37  1.138    dyoung  * by Coyote Point Systems, Inc.
     38  1.138    dyoung  * This code is derived from software contributed to The NetBSD Foundation
     39   1.57   thorpej  * by Public Access Networks Corporation ("Panix").  It was developed under
     40   1.57   thorpej  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     41   1.57   thorpej  *
     42   1.57   thorpej  * Redistribution and use in source and binary forms, with or without
     43   1.57   thorpej  * modification, are permitted provided that the following conditions
     44   1.57   thorpej  * are met:
     45   1.57   thorpej  * 1. Redistributions of source code must retain the above copyright
     46   1.57   thorpej  *    notice, this list of conditions and the following disclaimer.
     47   1.57   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.57   thorpej  *    notice, this list of conditions and the following disclaimer in the
     49   1.57   thorpej  *    documentation and/or other materials provided with the distribution.
     50   1.57   thorpej  *
     51   1.57   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     52   1.57   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     53   1.57   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     54   1.57   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     55   1.57   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     56   1.57   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     57   1.57   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     58   1.57   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     59   1.57   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     60   1.57   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     61   1.57   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     62   1.57   thorpej  */
     63   1.11       cgd 
     64    1.1       cgd /*
     65   1.44   thorpej  * Copyright (c) 1982, 1986, 1991, 1993, 1995
     66   1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
     67    1.1       cgd  *
     68    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     69    1.1       cgd  * modification, are permitted provided that the following conditions
     70    1.1       cgd  * are met:
     71    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     72    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     73    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     74    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     75    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     76   1.86       agc  * 3. Neither the name of the University nor the names of its contributors
     77    1.1       cgd  *    may be used to endorse or promote products derived from this software
     78    1.1       cgd  *    without specific prior written permission.
     79    1.1       cgd  *
     80    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     81    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     82    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     83    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     84    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     85    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     86    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     87    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     88    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     89    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     90    1.1       cgd  * SUCH DAMAGE.
     91    1.1       cgd  *
     92   1.44   thorpej  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
     93    1.1       cgd  */
     94   1.73     lukem 
     95   1.73     lukem #include <sys/cdefs.h>
     96  1.184  riastrad __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.184 2020/08/20 21:21:32 riastradh Exp $");
     97   1.60   thorpej 
     98  1.162     pooka #ifdef _KERNEL_OPT
     99   1.88    itojun #include "opt_inet.h"
    100   1.60   thorpej #include "opt_ipsec.h"
    101  1.162     pooka #endif
    102    1.1       cgd 
    103    1.7   mycroft #include <sys/param.h>
    104    1.7   mycroft #include <sys/systm.h>
    105    1.7   mycroft #include <sys/mbuf.h>
    106    1.7   mycroft #include <sys/socket.h>
    107    1.7   mycroft #include <sys/socketvar.h>
    108    1.7   mycroft #include <sys/ioctl.h>
    109   1.10   mycroft #include <sys/errno.h>
    110   1.10   mycroft #include <sys/time.h>
    111  1.128     pooka #include <sys/once.h>
    112   1.52   thorpej #include <sys/pool.h>
    113   1.10   mycroft #include <sys/proc.h>
    114  1.102      elad #include <sys/kauth.h>
    115  1.129     pooka #include <sys/uidinfo.h>
    116  1.132      elad #include <sys/domain.h>
    117    1.1       cgd 
    118    1.7   mycroft #include <net/if.h>
    119    1.7   mycroft #include <net/route.h>
    120    1.1       cgd 
    121    1.7   mycroft #include <netinet/in.h>
    122    1.7   mycroft #include <netinet/in_systm.h>
    123    1.7   mycroft #include <netinet/ip.h>
    124    1.7   mycroft #include <netinet/in_pcb.h>
    125    1.7   mycroft #include <netinet/in_var.h>
    126    1.7   mycroft #include <netinet/ip_var.h>
    127  1.143  christos #include <netinet/portalgo.h>
    128    1.1       cgd 
    129   1.88    itojun #ifdef INET6
    130   1.88    itojun #include <netinet/ip6.h>
    131   1.88    itojun #include <netinet6/ip6_var.h>
    132   1.88    itojun #include <netinet6/in6_pcb.h>
    133   1.88    itojun #endif
    134   1.88    itojun 
    135  1.145  christos #ifdef IPSEC
    136   1.87  jonathan #include <netipsec/ipsec.h>
    137   1.87  jonathan #include <netipsec/key.h>
    138   1.59    itojun #endif /* IPSEC */
    139   1.59    itojun 
    140  1.138    dyoung #include <netinet/tcp_vtw.h>
    141  1.138    dyoung 
    142    1.1       cgd struct	in_addr zeroin_addr;
    143    1.1       cgd 
    144   1.90    provos #define	INPCBHASH_PORT(table, lport) \
    145   1.92    itojun 	&(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
    146   1.33   mycroft #define	INPCBHASH_BIND(table, laddr, lport) \
    147   1.33   mycroft 	&(table)->inpt_bindhashtbl[ \
    148   1.33   mycroft 	    ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
    149   1.33   mycroft #define	INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
    150   1.33   mycroft 	&(table)->inpt_connecthashtbl[ \
    151   1.33   mycroft 	    ((ntohl((faddr).s_addr) + ntohs(fport)) + \
    152   1.33   mycroft 	     (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
    153   1.33   mycroft 
    154   1.43     lukem int	anonportmin = IPPORT_ANONMIN;
    155   1.43     lukem int	anonportmax = IPPORT_ANONMAX;
    156   1.67      tron int	lowportmin  = IPPORT_RESERVEDMIN;
    157   1.67      tron int	lowportmax  = IPPORT_RESERVEDMAX;
    158   1.43     lukem 
    159  1.128     pooka static struct pool inpcb_pool;
    160  1.128     pooka 
    161  1.128     pooka static int
    162  1.128     pooka inpcb_poolinit(void)
    163  1.128     pooka {
    164  1.128     pooka 
    165  1.128     pooka 	pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0, "inpcbpl", NULL,
    166  1.128     pooka 	    IPL_NET);
    167  1.128     pooka 	return 0;
    168  1.128     pooka }
    169   1.52   thorpej 
    170   1.18   mycroft void
    171   1.98     perry in_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
    172   1.18   mycroft {
    173  1.128     pooka 	static ONCE_DECL(control);
    174   1.18   mycroft 
    175  1.146  christos 	TAILQ_INIT(&table->inpt_queue);
    176  1.125        ad 	table->inpt_porthashtbl = hashinit(bindhashsize, HASH_LIST, true,
    177  1.125        ad 	    &table->inpt_porthash);
    178  1.125        ad 	table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, true,
    179  1.125        ad 	    &table->inpt_bindhash);
    180  1.125        ad 	table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST, true,
    181  1.125        ad 	    &table->inpt_connecthash);
    182   1.47     lukem 	table->inpt_lastlow = IPPORT_RESERVEDMAX;
    183   1.47     lukem 	table->inpt_lastport = (u_int16_t)anonportmax;
    184  1.128     pooka 
    185  1.128     pooka 	RUN_ONCE(&control, inpcb_poolinit);
    186   1.18   mycroft }
    187   1.18   mycroft 
    188   1.10   mycroft int
    189   1.98     perry in_pcballoc(struct socket *so, void *v)
    190    1.1       cgd {
    191   1.25  christos 	struct inpcbtable *table = v;
    192   1.64  augustss 	struct inpcb *inp;
    193   1.24   mycroft 	int s;
    194    1.1       cgd 
    195  1.177     ozaki 	KASSERT(so->so_proto->pr_domain->dom_family == AF_INET);
    196  1.177     ozaki 
    197   1.52   thorpej 	inp = pool_get(&inpcb_pool, PR_NOWAIT);
    198   1.10   mycroft 	if (inp == NULL)
    199    1.1       cgd 		return (ENOBUFS);
    200  1.139  christos 	memset(inp, 0, sizeof(*inp));
    201   1.88    itojun 	inp->inp_af = AF_INET;
    202   1.18   mycroft 	inp->inp_table = table;
    203    1.1       cgd 	inp->inp_socket = so;
    204   1.39      matt 	inp->inp_errormtu = -1;
    205  1.143  christos 	inp->inp_portalgo = PORTALGO_DEFAULT;
    206  1.139  christos 	inp->inp_bindportonsend = false;
    207  1.181  christos 	inp->inp_prefsrcip.s_addr = INADDR_ANY;
    208  1.184  riastrad 	inp->inp_overudp_cb = NULL;
    209  1.184  riastrad 	inp->inp_overudp_arg = NULL;
    210  1.145  christos #if defined(IPSEC)
    211  1.148  christos 	if (ipsec_enabled) {
    212  1.148  christos 		int error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
    213  1.148  christos 		if (error != 0) {
    214  1.148  christos 			pool_put(&inpcb_pool, inp);
    215  1.148  christos 			return error;
    216  1.148  christos 		}
    217  1.178     ozaki 		inp->inp_sp->sp_inph = (struct inpcb_hdr *)inp;
    218   1.70    itojun 	}
    219   1.70    itojun #endif
    220   1.33   mycroft 	so->so_pcb = inp;
    221  1.175     ozaki 	s = splsoftnet();
    222  1.146  christos 	TAILQ_INSERT_HEAD(&table->inpt_queue, &inp->inp_head, inph_queue);
    223   1.90    provos 	LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
    224   1.90    provos 	    inph_lhash);
    225   1.33   mycroft 	in_pcbstate(inp, INP_ATTACHED);
    226   1.24   mycroft 	splx(s);
    227    1.1       cgd 	return (0);
    228    1.1       cgd }
    229    1.8   mycroft 
    230  1.132      elad static int
    231  1.134      elad in_pcbsetport(struct sockaddr_in *sin, struct inpcb *inp, kauth_cred_t cred)
    232    1.1       cgd {
    233  1.132      elad 	struct inpcbtable *table = inp->inp_table;
    234   1.64  augustss 	struct socket *so = inp->inp_socket;
    235  1.132      elad 	u_int16_t *lastport;
    236   1.13       cgd 	u_int16_t lport = 0;
    237  1.134      elad 	enum kauth_network_req req;
    238  1.134      elad 	int error;
    239    1.1       cgd 
    240  1.132      elad 	if (inp->inp_flags & INP_LOWPORT) {
    241  1.132      elad #ifndef IPNOPRIVPORTS
    242  1.134      elad 		req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
    243  1.134      elad #else
    244  1.134      elad 		req = KAUTH_REQ_NETWORK_BIND_PORT;
    245  1.132      elad #endif
    246  1.134      elad 
    247  1.132      elad 		lastport = &table->inpt_lastlow;
    248  1.132      elad 	} else {
    249  1.134      elad 		req = KAUTH_REQ_NETWORK_BIND_PORT;
    250  1.134      elad 
    251  1.132      elad 		lastport = &table->inpt_lastport;
    252  1.132      elad 	}
    253  1.134      elad 
    254  1.134      elad 	/* XXX-kauth: KAUTH_REQ_NETWORK_BIND_AUTOASSIGN_{,PRIV}PORT */
    255  1.134      elad 	error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req, so, sin,
    256  1.134      elad 	    NULL);
    257  1.134      elad 	if (error)
    258  1.137      elad 		return (EACCES);
    259  1.134      elad 
    260  1.139  christos        /*
    261  1.139  christos         * Use RFC6056 randomized port selection
    262  1.139  christos         */
    263  1.143  christos 	error = portalgo_randport(&lport, &inp->inp_head, cred);
    264  1.139  christos 	if (error)
    265  1.139  christos 		return error;
    266  1.132      elad 
    267  1.132      elad 	inp->inp_flags |= INP_ANONPORT;
    268  1.132      elad 	*lastport = lport;
    269  1.132      elad 	lport = htons(lport);
    270  1.132      elad 	inp->inp_lport = lport;
    271  1.132      elad 	in_pcbstate(inp, INP_BOUND);
    272  1.132      elad 
    273  1.132      elad 	return (0);
    274  1.132      elad }
    275   1.88    itojun 
    276  1.179       ryo int
    277  1.179       ryo in_pcbbindableaddr(struct sockaddr_in *sin, kauth_cred_t cred)
    278  1.132      elad {
    279  1.168     ozaki 	int error = EADDRNOTAVAIL;
    280  1.168     ozaki 	struct ifaddr *ifa = NULL;
    281  1.168     ozaki 	int s;
    282  1.168     ozaki 
    283   1.28   mycroft 	if (sin->sin_family != AF_INET)
    284   1.28   mycroft 		return (EAFNOSUPPORT);
    285  1.132      elad 
    286  1.168     ozaki 	s = pserialize_read_enter();
    287  1.136      elad 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
    288  1.136      elad 		/* Always succeed; port reuse handled in in_pcbbind_port(). */
    289  1.136      elad 	} else if (!in_nullhost(sin->sin_addr)) {
    290  1.168     ozaki 		struct in_ifaddr *ia;
    291  1.132      elad 
    292  1.166     ozaki 		ia = in_get_ia(sin->sin_addr);
    293  1.132      elad 		/* check for broadcast addresses */
    294  1.168     ozaki 		if (ia == NULL) {
    295  1.168     ozaki 			ifa = ifa_ifwithaddr(sintosa(sin));
    296  1.168     ozaki 			if (ifa != NULL)
    297  1.168     ozaki 				ia = ifatoia(ifa);
    298  1.168     ozaki 		}
    299  1.132      elad 		if (ia == NULL)
    300  1.168     ozaki 			goto error;
    301  1.170       roy 		if (ia->ia4_flags & IN_IFF_DUPLICATED)
    302  1.168     ozaki 			goto error;
    303  1.132      elad 	}
    304  1.179       ryo 	error = 0;
    305  1.179       ryo  error:
    306  1.168     ozaki 	pserialize_read_exit(s);
    307  1.179       ryo 	return error;
    308  1.179       ryo }
    309  1.132      elad 
    310  1.179       ryo static int
    311  1.179       ryo in_pcbbind_addr(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred)
    312  1.179       ryo {
    313  1.179       ryo 	int error;
    314  1.132      elad 
    315  1.179       ryo 	error = in_pcbbindableaddr(sin, cred);
    316  1.179       ryo 	if (error == 0)
    317  1.179       ryo 		inp->inp_laddr = sin->sin_addr;
    318  1.168     ozaki 	return error;
    319  1.132      elad }
    320  1.132      elad 
    321  1.132      elad static int
    322  1.132      elad in_pcbbind_port(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred)
    323  1.132      elad {
    324  1.132      elad 	struct inpcbtable *table = inp->inp_table;
    325  1.132      elad 	struct socket *so = inp->inp_socket;
    326  1.132      elad 	int reuseport = (so->so_options & SO_REUSEPORT);
    327  1.133      elad 	int wild = 0, error;
    328  1.132      elad 
    329   1.28   mycroft 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
    330   1.10   mycroft 		/*
    331   1.28   mycroft 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
    332   1.28   mycroft 		 * allow complete duplication of binding if
    333   1.28   mycroft 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
    334   1.28   mycroft 		 * and a multicast address is bound on both
    335   1.28   mycroft 		 * new and duplicated sockets.
    336   1.10   mycroft 		 */
    337  1.155     seanb 		if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT))
    338   1.28   mycroft 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
    339  1.132      elad 	}
    340  1.132      elad 
    341  1.132      elad 	if (sin->sin_port == 0) {
    342  1.134      elad 		error = in_pcbsetport(sin, inp, cred);
    343  1.132      elad 		if (error)
    344  1.132      elad 			return (error);
    345  1.132      elad 	} else {
    346   1.28   mycroft 		struct inpcb *t;
    347  1.138    dyoung 		vestigial_inpcb_t vestige;
    348   1.88    itojun #ifdef INET6
    349   1.88    itojun 		struct in6pcb *t6;
    350   1.88    itojun 		struct in6_addr mapped;
    351   1.88    itojun #endif
    352  1.133      elad 		enum kauth_network_req req;
    353  1.132      elad 
    354  1.132      elad 		if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
    355  1.132      elad 			wild = 1;
    356  1.132      elad 
    357   1.31     perry #ifndef IPNOPRIVPORTS
    358  1.133      elad 		if (ntohs(sin->sin_port) < IPPORT_RESERVED)
    359  1.133      elad 			req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
    360  1.133      elad 		else
    361  1.133      elad #endif /* !IPNOPRIVPORTS */
    362  1.133      elad 			req = KAUTH_REQ_NETWORK_BIND_PORT;
    363  1.133      elad 
    364  1.133      elad 		error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req,
    365  1.133      elad 		    so, sin, NULL);
    366  1.133      elad 		if (error)
    367  1.137      elad 			return (EACCES);
    368  1.133      elad 
    369   1.88    itojun #ifdef INET6
    370  1.163       rtr 		in6_in_2_v4mapin6(&sin->sin_addr, &mapped);
    371  1.138    dyoung 		t6 = in6_pcblookup_port(table, &mapped, sin->sin_port, wild, &vestige);
    372   1.88    itojun 		if (t6 && (reuseport & t6->in6p_socket->so_options) == 0)
    373   1.88    itojun 			return (EADDRINUSE);
    374  1.138    dyoung 		if (!t6 && vestige.valid) {
    375  1.138    dyoung 		    if (!!reuseport != !!vestige.reuse_port) {
    376  1.138    dyoung 			return EADDRINUSE;
    377  1.138    dyoung 		    }
    378  1.138    dyoung 		}
    379   1.88    itojun #endif
    380  1.133      elad 
    381  1.133      elad 		/* XXX-kauth */
    382   1.99  christos 		if (so->so_uidinfo->ui_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
    383  1.138    dyoung 			t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, 1, &vestige);
    384  1.133      elad 			/*
    385  1.133      elad 			 * XXX:	investigate ramifications of loosening this
    386  1.133      elad 			 *	restriction so that as long as both ports have
    387  1.133      elad 			 *	SO_REUSEPORT allow the bind
    388  1.133      elad 			 */
    389   1.58     lukem 			if (t &&
    390   1.58     lukem 			    (!in_nullhost(sin->sin_addr) ||
    391   1.58     lukem 			     !in_nullhost(t->inp_laddr) ||
    392   1.58     lukem 			     (t->inp_socket->so_options & SO_REUSEPORT) == 0)
    393   1.99  christos 			    && (so->so_uidinfo->ui_uid != t->inp_socket->so_uidinfo->ui_uid)) {
    394   1.58     lukem 				return (EADDRINUSE);
    395   1.58     lukem 			}
    396  1.138    dyoung 			if (!t && vestige.valid) {
    397  1.138    dyoung 				if ((!in_nullhost(sin->sin_addr)
    398  1.138    dyoung 				     || !in_nullhost(vestige.laddr.v4)
    399  1.138    dyoung 				     || !vestige.reuse_port)
    400  1.138    dyoung 				    && so->so_uidinfo->ui_uid != vestige.uid) {
    401  1.138    dyoung 					return EADDRINUSE;
    402  1.138    dyoung 				}
    403  1.138    dyoung 			}
    404   1.58     lukem 		}
    405  1.138    dyoung 		t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, wild, &vestige);
    406   1.28   mycroft 		if (t && (reuseport & t->inp_socket->so_options) == 0)
    407   1.28   mycroft 			return (EADDRINUSE);
    408  1.138    dyoung 		if (!t
    409  1.138    dyoung 		    && vestige.valid
    410  1.138    dyoung 		    && !(reuseport && vestige.reuse_port))
    411  1.138    dyoung 			return EADDRINUSE;
    412  1.132      elad 
    413  1.132      elad 		inp->inp_lport = sin->sin_port;
    414  1.132      elad 		in_pcbstate(inp, INP_BOUND);
    415    1.1       cgd 	}
    416   1.45     lukem 
    417   1.90    provos 	LIST_REMOVE(&inp->inp_head, inph_lhash);
    418   1.90    provos 	LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
    419   1.90    provos 	    inph_lhash);
    420  1.132      elad 
    421  1.132      elad 	return (0);
    422  1.132      elad }
    423  1.132      elad 
    424  1.132      elad int
    425  1.156       rtr in_pcbbind(void *v, struct sockaddr_in *sin, struct lwp *l)
    426  1.132      elad {
    427  1.132      elad 	struct inpcb *inp = v;
    428  1.134      elad 	struct sockaddr_in lsin;
    429  1.132      elad 	int error;
    430  1.132      elad 
    431  1.132      elad 	if (inp->inp_af != AF_INET)
    432  1.132      elad 		return (EINVAL);
    433  1.132      elad 
    434  1.165     ozaki 	if (IN_ADDRLIST_READER_EMPTY())
    435  1.132      elad 		return (EADDRNOTAVAIL);
    436  1.132      elad 	if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
    437  1.132      elad 		return (EINVAL);
    438  1.132      elad 
    439  1.156       rtr 	if (NULL != sin) {
    440  1.156       rtr 		if (sin->sin_len != sizeof(*sin))
    441  1.132      elad 			return (EINVAL);
    442  1.132      elad 	} else {
    443  1.134      elad 		lsin = *((const struct sockaddr_in *)
    444  1.134      elad 		    inp->inp_socket->so_proto->pr_domain->dom_sa_any);
    445  1.134      elad 		sin = &lsin;
    446  1.132      elad 	}
    447  1.132      elad 
    448  1.132      elad 	/* Bind address. */
    449  1.151       rtr 	error = in_pcbbind_addr(inp, sin, l->l_cred);
    450  1.132      elad 	if (error)
    451  1.132      elad 		return (error);
    452  1.132      elad 
    453  1.132      elad 	/* Bind port. */
    454  1.151       rtr 	error = in_pcbbind_port(inp, sin, l->l_cred);
    455  1.132      elad 	if (error) {
    456  1.132      elad 		inp->inp_laddr.s_addr = INADDR_ANY;
    457  1.132      elad 
    458  1.132      elad 		return (error);
    459  1.132      elad 	}
    460  1.132      elad 
    461    1.1       cgd 	return (0);
    462    1.1       cgd }
    463    1.1       cgd 
    464    1.1       cgd /*
    465    1.1       cgd  * Connect from a socket to a specified address.
    466    1.1       cgd  * Both address and port must be specified in argument sin.
    467    1.1       cgd  * If don't have a local address for this socket yet,
    468    1.1       cgd  * then pick one.
    469    1.1       cgd  */
    470   1.10   mycroft int
    471  1.160       rtr in_pcbconnect(void *v, struct sockaddr_in *sin, struct lwp *l)
    472    1.1       cgd {
    473   1.64  augustss 	struct inpcb *inp = v;
    474  1.138    dyoung 	vestigial_inpcb_t vestige;
    475   1.40   thorpej 	int error;
    476  1.167     ozaki 	struct in_addr laddr;
    477    1.1       cgd 
    478   1.88    itojun 	if (inp->inp_af != AF_INET)
    479   1.88    itojun 		return (EINVAL);
    480   1.88    itojun 
    481  1.158       rtr 	if (sin->sin_len != sizeof (*sin))
    482  1.158       rtr 		return (EINVAL);
    483    1.1       cgd 	if (sin->sin_family != AF_INET)
    484    1.1       cgd 		return (EAFNOSUPPORT);
    485    1.1       cgd 	if (sin->sin_port == 0)
    486    1.1       cgd 		return (EADDRNOTAVAIL);
    487  1.144  christos 
    488  1.144  christos 	if (IN_MULTICAST(sin->sin_addr.s_addr) &&
    489  1.144  christos 	    inp->inp_socket->so_type == SOCK_STREAM)
    490  1.144  christos 		return EADDRNOTAVAIL;
    491  1.144  christos 
    492  1.165     ozaki 	if (!IN_ADDRLIST_READER_EMPTY()) {
    493    1.1       cgd 		/*
    494    1.1       cgd 		 * If the destination address is INADDR_ANY,
    495   1.49       tls 		 * use any local address (likely loopback).
    496    1.1       cgd 		 * If the supplied address is INADDR_BROADCAST,
    497   1.49       tls 		 * use the broadcast address of an interface
    498   1.49       tls 		 * which supports broadcast. (loopback does not)
    499    1.1       cgd 		 */
    500   1.49       tls 
    501   1.72      matt 		if (in_nullhost(sin->sin_addr)) {
    502  1.165     ozaki 			/* XXX racy */
    503   1.72      matt 			sin->sin_addr =
    504  1.165     ozaki 			    IN_ADDRLIST_READER_FIRST()->ia_addr.sin_addr;
    505   1.72      matt 		} else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
    506  1.167     ozaki 			struct in_ifaddr *ia;
    507  1.168     ozaki 			int s = pserialize_read_enter();
    508  1.165     ozaki 			IN_ADDRLIST_READER_FOREACH(ia) {
    509   1.72      matt 				if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
    510   1.72      matt 					sin->sin_addr =
    511   1.72      matt 					    ia->ia_broadaddr.sin_addr;
    512   1.72      matt 					break;
    513   1.72      matt 				}
    514   1.49       tls 			}
    515  1.168     ozaki 			pserialize_read_exit(s);
    516   1.72      matt 		}
    517    1.1       cgd 	}
    518   1.32   mycroft 	/*
    519   1.32   mycroft 	 * If we haven't bound which network number to use as ours,
    520   1.32   mycroft 	 * we will use the number of the outgoing interface.
    521   1.32   mycroft 	 * This depends on having done a routing lookup, which
    522   1.32   mycroft 	 * we will probably have to do anyway, so we might
    523   1.32   mycroft 	 * as well do it now.  On the other hand if we are
    524   1.32   mycroft 	 * sending to multiple destinations we may have already
    525   1.32   mycroft 	 * done the lookup, so see if we can use the route
    526   1.32   mycroft 	 * from before.  In any case, we only
    527   1.32   mycroft 	 * chose a port number once, even if sending to multiple
    528   1.32   mycroft 	 * destinations.
    529   1.32   mycroft 	 */
    530   1.32   mycroft 	if (in_nullhost(inp->inp_laddr)) {
    531  1.100  christos 		int xerror;
    532  1.168     ozaki 		struct in_ifaddr *ia, *_ia;
    533  1.168     ozaki 		int s;
    534  1.168     ozaki 		struct psref psref;
    535  1.168     ozaki 		int bound;
    536  1.168     ozaki 
    537  1.168     ozaki 		bound = curlwp_bind();
    538  1.168     ozaki 		ia = in_selectsrc(sin, &inp->inp_route,
    539  1.168     ozaki 		    inp->inp_socket->so_options, inp->inp_moptions, &xerror,
    540  1.168     ozaki 		    &psref);
    541  1.168     ozaki 		if (ia == NULL) {
    542  1.168     ozaki 			curlwp_bindx(bound);
    543  1.100  christos 			if (xerror == 0)
    544  1.100  christos 				xerror = EADDRNOTAVAIL;
    545  1.100  christos 			return xerror;
    546   1.59    itojun 		}
    547  1.168     ozaki 		s = pserialize_read_enter();
    548  1.168     ozaki 		_ia = in_get_ia(IA_SIN(ia)->sin_addr);
    549  1.168     ozaki 		if (_ia == NULL) {
    550  1.168     ozaki 			pserialize_read_exit(s);
    551  1.168     ozaki 			ia4_release(ia, &psref);
    552  1.168     ozaki 			curlwp_bindx(bound);
    553   1.83    itojun 			return (EADDRNOTAVAIL);
    554  1.168     ozaki 		}
    555  1.168     ozaki 		pserialize_read_exit(s);
    556  1.168     ozaki 		laddr = IA_SIN(ia)->sin_addr;
    557  1.168     ozaki 		ia4_release(ia, &psref);
    558  1.168     ozaki 		curlwp_bindx(bound);
    559  1.167     ozaki 	} else
    560  1.167     ozaki 		laddr = inp->inp_laddr;
    561   1.33   mycroft 	if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
    562  1.168     ozaki 	                         laddr, inp->inp_lport, &vestige) != NULL ||
    563  1.168     ozaki 	    vestige.valid) {
    564    1.1       cgd 		return (EADDRINUSE);
    565  1.168     ozaki 	}
    566   1.32   mycroft 	if (in_nullhost(inp->inp_laddr)) {
    567   1.40   thorpej 		if (inp->inp_lport == 0) {
    568  1.151       rtr 			error = in_pcbbind(inp, NULL, l);
    569   1.40   thorpej 			/*
    570   1.40   thorpej 			 * This used to ignore the return value
    571   1.40   thorpej 			 * completely, but we need to check for
    572   1.40   thorpej 			 * ephemeral port shortage.
    573  1.101       dsl 			 * And attempts to request low ports if not root.
    574   1.40   thorpej 			 */
    575  1.101       dsl 			if (error != 0)
    576   1.40   thorpej 				return (error);
    577   1.40   thorpej 		}
    578  1.167     ozaki 		inp->inp_laddr = laddr;
    579    1.1       cgd 	}
    580    1.1       cgd 	inp->inp_faddr = sin->sin_addr;
    581    1.1       cgd 	inp->inp_fport = sin->sin_port;
    582  1.139  christos 
    583  1.139  christos         /* Late bind, if needed */
    584  1.139  christos 	if (inp->inp_bindportonsend) {
    585  1.139  christos                struct sockaddr_in lsin = *((const struct sockaddr_in *)
    586  1.139  christos 		    inp->inp_socket->so_proto->pr_domain->dom_sa_any);
    587  1.139  christos 		lsin.sin_addr = inp->inp_laddr;
    588  1.139  christos 		lsin.sin_port = 0;
    589  1.139  christos 
    590  1.139  christos                if ((error = in_pcbbind_port(inp, &lsin, l->l_cred)) != 0)
    591  1.139  christos                        return error;
    592  1.139  christos 	}
    593  1.139  christos 
    594   1.33   mycroft 	in_pcbstate(inp, INP_CONNECTED);
    595  1.145  christos #if defined(IPSEC)
    596  1.148  christos 	if (ipsec_enabled && inp->inp_socket->so_type == SOCK_STREAM)
    597   1.71    itojun 		ipsec_pcbconn(inp->inp_sp);
    598   1.71    itojun #endif
    599    1.1       cgd 	return (0);
    600    1.1       cgd }
    601    1.1       cgd 
    602   1.25  christos void
    603   1.98     perry in_pcbdisconnect(void *v)
    604    1.1       cgd {
    605   1.25  christos 	struct inpcb *inp = v;
    606    1.1       cgd 
    607   1.88    itojun 	if (inp->inp_af != AF_INET)
    608   1.88    itojun 		return;
    609   1.88    itojun 
    610   1.32   mycroft 	inp->inp_faddr = zeroin_addr;
    611    1.1       cgd 	inp->inp_fport = 0;
    612   1.33   mycroft 	in_pcbstate(inp, INP_BOUND);
    613  1.145  christos #if defined(IPSEC)
    614  1.148  christos 	if (ipsec_enabled)
    615  1.148  christos 		ipsec_pcbdisconn(inp->inp_sp);
    616   1.71    itojun #endif
    617   1.93    itojun 	if (inp->inp_socket->so_state & SS_NOFDREF)
    618   1.93    itojun 		in_pcbdetach(inp);
    619    1.1       cgd }
    620    1.1       cgd 
    621   1.25  christos void
    622   1.98     perry in_pcbdetach(void *v)
    623    1.1       cgd {
    624   1.25  christos 	struct inpcb *inp = v;
    625    1.1       cgd 	struct socket *so = inp->inp_socket;
    626   1.24   mycroft 	int s;
    627    1.1       cgd 
    628   1.88    itojun 	if (inp->inp_af != AF_INET)
    629   1.88    itojun 		return;
    630   1.88    itojun 
    631  1.145  christos #if defined(IPSEC)
    632  1.148  christos 	if (ipsec_enabled)
    633  1.182      maxv 		ipsec_delete_pcbpolicy(inp);
    634  1.150     rmind #endif
    635  1.150     rmind 	so->so_pcb = NULL;
    636  1.150     rmind 
    637  1.175     ozaki 	s = splsoftnet();
    638   1.33   mycroft 	in_pcbstate(inp, INP_ATTACHED);
    639   1.90    provos 	LIST_REMOVE(&inp->inp_head, inph_lhash);
    640  1.146  christos 	TAILQ_REMOVE(&inp->inp_table->inpt_queue, &inp->inp_head, inph_queue);
    641   1.24   mycroft 	splx(s);
    642  1.150     rmind 
    643  1.150     rmind 	if (inp->inp_options) {
    644  1.150     rmind 		m_free(inp->inp_options);
    645  1.150     rmind 	}
    646  1.150     rmind 	rtcache_free(&inp->inp_route);
    647  1.152     rmind 	ip_freemoptions(inp->inp_moptions);
    648  1.127       spz 	sofree(so);			/* drops the socket's lock */
    649  1.150     rmind 
    650  1.150     rmind 	pool_put(&inpcb_pool, inp);
    651  1.126      matt 	mutex_enter(softnet_lock);	/* reacquire the softnet_lock */
    652    1.1       cgd }
    653    1.1       cgd 
    654   1.25  christos void
    655  1.157       rtr in_setsockaddr(struct inpcb *inp, struct sockaddr_in *sin)
    656    1.1       cgd {
    657   1.78    itojun 
    658   1.88    itojun 	if (inp->inp_af != AF_INET)
    659   1.88    itojun 		return;
    660   1.88    itojun 
    661  1.119    dyoung 	sockaddr_in_init(sin, &inp->inp_laddr, inp->inp_lport);
    662    1.1       cgd }
    663    1.1       cgd 
    664   1.25  christos void
    665  1.157       rtr in_setpeeraddr(struct inpcb *inp, struct sockaddr_in *sin)
    666    1.1       cgd {
    667   1.78    itojun 
    668   1.88    itojun 	if (inp->inp_af != AF_INET)
    669   1.88    itojun 		return;
    670   1.88    itojun 
    671  1.119    dyoung 	sockaddr_in_init(sin, &inp->inp_faddr, inp->inp_fport);
    672    1.1       cgd }
    673    1.1       cgd 
    674    1.1       cgd /*
    675    1.1       cgd  * Pass some notification to all connections of a protocol
    676    1.1       cgd  * associated with address dst.  The local address and/or port numbers
    677    1.1       cgd  * may be specified to limit the search.  The "usual action" will be
    678    1.1       cgd  * taken, depending on the ctlinput cmd.  The caller must filter any
    679    1.1       cgd  * cmds that are uninteresting (e.g., no error in the map).
    680    1.1       cgd  * Call the protocol specific routine (if any) to report
    681    1.1       cgd  * any errors for each matching socket.
    682    1.1       cgd  *
    683   1.22   mycroft  * Must be called at splsoftnet.
    684    1.1       cgd  */
    685   1.37   thorpej int
    686   1.98     perry in_pcbnotify(struct inpcbtable *table, struct in_addr faddr, u_int fport_arg,
    687   1.98     perry     struct in_addr laddr, u_int lport_arg, int errno,
    688   1.98     perry     void (*notify)(struct inpcb *, int))
    689    1.1       cgd {
    690   1.33   mycroft 	struct inpcbhead *head;
    691   1.64  augustss 	struct inpcb *inp, *ninp;
    692   1.13       cgd 	u_int16_t fport = fport_arg, lport = lport_arg;
    693   1.37   thorpej 	int nmatch;
    694    1.1       cgd 
    695   1.33   mycroft 	if (in_nullhost(faddr) || notify == 0)
    696   1.37   thorpej 		return (0);
    697    1.1       cgd 
    698   1.37   thorpej 	nmatch = 0;
    699   1.33   mycroft 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
    700   1.88    itojun 	for (inp = (struct inpcb *)LIST_FIRST(head); inp != NULL; inp = ninp) {
    701   1.88    itojun 		ninp = (struct inpcb *)LIST_NEXT(inp, inp_hash);
    702   1.88    itojun 		if (inp->inp_af != AF_INET)
    703   1.88    itojun 			continue;
    704   1.33   mycroft 		if (in_hosteq(inp->inp_faddr, faddr) &&
    705   1.33   mycroft 		    inp->inp_fport == fport &&
    706   1.33   mycroft 		    inp->inp_lport == lport &&
    707   1.37   thorpej 		    in_hosteq(inp->inp_laddr, laddr)) {
    708   1.33   mycroft 			(*notify)(inp, errno);
    709   1.37   thorpej 			nmatch++;
    710   1.37   thorpej 		}
    711    1.1       cgd 	}
    712   1.37   thorpej 	return (nmatch);
    713   1.18   mycroft }
    714   1.18   mycroft 
    715   1.20   mycroft void
    716   1.98     perry in_pcbnotifyall(struct inpcbtable *table, struct in_addr faddr, int errno,
    717   1.98     perry     void (*notify)(struct inpcb *, int))
    718   1.18   mycroft {
    719  1.146  christos 	struct inpcb_hdr *inph, *ninph;
    720   1.18   mycroft 
    721   1.33   mycroft 	if (in_nullhost(faddr) || notify == 0)
    722   1.18   mycroft 		return;
    723   1.18   mycroft 
    724  1.146  christos 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
    725  1.146  christos 		struct inpcb *inp = (struct inpcb *)inph;
    726   1.88    itojun 		if (inp->inp_af != AF_INET)
    727   1.88    itojun 			continue;
    728   1.33   mycroft 		if (in_hosteq(inp->inp_faddr, faddr))
    729   1.33   mycroft 			(*notify)(inp, errno);
    730   1.63   thorpej 	}
    731   1.63   thorpej }
    732   1.63   thorpej 
    733   1.63   thorpej void
    734  1.154     seanb in_purgeifmcast(struct ip_moptions *imo, struct ifnet *ifp)
    735  1.154     seanb {
    736  1.154     seanb 	int i, gap;
    737  1.154     seanb 
    738  1.176     ozaki 	/* The owner of imo should be protected by solock */
    739  1.164     ozaki 	KASSERT(ifp != NULL);
    740  1.164     ozaki 
    741  1.154     seanb 	if (imo == NULL)
    742  1.154     seanb 		return;
    743  1.154     seanb 
    744  1.154     seanb 	/*
    745  1.154     seanb 	 * Unselect the outgoing interface if it is being
    746  1.154     seanb 	 * detached.
    747  1.154     seanb 	 */
    748  1.164     ozaki 	if (imo->imo_multicast_if_index == ifp->if_index)
    749  1.164     ozaki 		imo->imo_multicast_if_index = 0;
    750  1.154     seanb 
    751  1.154     seanb 	/*
    752  1.154     seanb 	 * Drop multicast group membership if we joined
    753  1.154     seanb 	 * through the interface being detached.
    754  1.154     seanb 	 */
    755  1.154     seanb 	for (i = 0, gap = 0; i < imo->imo_num_memberships; i++) {
    756  1.154     seanb 		if (imo->imo_membership[i]->inm_ifp == ifp) {
    757  1.154     seanb 			in_delmulti(imo->imo_membership[i]);
    758  1.154     seanb 			gap++;
    759  1.154     seanb 		} else if (gap != 0)
    760  1.154     seanb 			imo->imo_membership[i - gap] = imo->imo_membership[i];
    761  1.154     seanb 	}
    762  1.154     seanb 	imo->imo_num_memberships -= gap;
    763  1.154     seanb }
    764  1.154     seanb 
    765  1.154     seanb void
    766   1.98     perry in_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
    767   1.63   thorpej {
    768  1.146  christos 	struct inpcb_hdr *inph, *ninph;
    769   1.63   thorpej 
    770  1.146  christos 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
    771  1.146  christos 		struct inpcb *inp = (struct inpcb *)inph;
    772  1.176     ozaki 		bool need_unlock = false;
    773  1.176     ozaki 
    774   1.88    itojun 		if (inp->inp_af != AF_INET)
    775   1.88    itojun 			continue;
    776  1.176     ozaki 
    777  1.176     ozaki 		/* The caller holds either one of inps' lock */
    778  1.176     ozaki 		if (!inp_locked(inp)) {
    779  1.176     ozaki 			inp_lock(inp);
    780  1.176     ozaki 			need_unlock = true;
    781  1.176     ozaki 		}
    782  1.176     ozaki 
    783  1.180     ozaki 		/* IFNET_LOCK must be taken after solock */
    784  1.154     seanb 		in_purgeifmcast(inp->inp_moptions, ifp);
    785  1.176     ozaki 
    786  1.176     ozaki 		if (need_unlock)
    787  1.176     ozaki 			inp_unlock(inp);
    788   1.69    itojun 	}
    789   1.69    itojun }
    790   1.69    itojun 
    791   1.69    itojun void
    792   1.98     perry in_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
    793   1.69    itojun {
    794  1.121    dyoung 	struct rtentry *rt;
    795  1.146  christos 	struct inpcb_hdr *inph, *ninph;
    796   1.69    itojun 
    797  1.146  christos 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
    798  1.146  christos 		struct inpcb *inp = (struct inpcb *)inph;
    799   1.88    itojun 		if (inp->inp_af != AF_INET)
    800   1.88    itojun 			continue;
    801  1.122    dyoung 		if ((rt = rtcache_validate(&inp->inp_route)) != NULL &&
    802  1.171     ozaki 		    rt->rt_ifp == ifp) {
    803  1.171     ozaki 			rtcache_unref(rt, &inp->inp_route);
    804   1.69    itojun 			in_rtchange(inp, 0);
    805  1.171     ozaki 		} else
    806  1.171     ozaki 			rtcache_unref(rt, &inp->inp_route);
    807    1.1       cgd 	}
    808    1.1       cgd }
    809    1.1       cgd 
    810    1.1       cgd /*
    811    1.1       cgd  * Check for alternatives when higher level complains
    812    1.1       cgd  * about service problems.  For now, invalidate cached
    813    1.1       cgd  * routing information.  If the route was created dynamically
    814    1.1       cgd  * (by a redirect), time to try a default gateway again.
    815    1.1       cgd  */
    816   1.25  christos void
    817   1.98     perry in_losing(struct inpcb *inp)
    818    1.1       cgd {
    819   1.64  augustss 	struct rtentry *rt;
    820   1.10   mycroft 	struct rt_addrinfo info;
    821    1.1       cgd 
    822   1.88    itojun 	if (inp->inp_af != AF_INET)
    823   1.88    itojun 		return;
    824   1.88    itojun 
    825  1.122    dyoung 	if ((rt = rtcache_validate(&inp->inp_route)) == NULL)
    826  1.122    dyoung 		return;
    827  1.122    dyoung 
    828  1.122    dyoung 	memset(&info, 0, sizeof(info));
    829  1.122    dyoung 	info.rti_info[RTAX_DST] = rtcache_getdst(&inp->inp_route);
    830  1.122    dyoung 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    831  1.122    dyoung 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    832  1.122    dyoung 	rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
    833  1.171     ozaki 	if (rt->rt_flags & RTF_DYNAMIC) {
    834  1.171     ozaki 		int error;
    835  1.171     ozaki 		struct rtentry *nrt;
    836  1.171     ozaki 
    837  1.171     ozaki 		error = rtrequest(RTM_DELETE, rt_getkey(rt),
    838  1.171     ozaki 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt);
    839  1.171     ozaki 		rtcache_unref(rt, &inp->inp_route);
    840  1.171     ozaki 		if (error == 0)
    841  1.172     ozaki 			rt_free(nrt);
    842  1.171     ozaki 	} else
    843  1.171     ozaki 		rtcache_unref(rt, &inp->inp_route);
    844  1.122    dyoung 	/*
    845  1.122    dyoung 	 * A new route can be allocated
    846  1.122    dyoung 	 * the next time output is attempted.
    847  1.122    dyoung 	 */
    848  1.122    dyoung 	rtcache_free(&inp->inp_route);
    849    1.1       cgd }
    850    1.1       cgd 
    851    1.1       cgd /*
    852  1.111    dyoung  * After a routing change, flush old routing.  A new route can be
    853  1.111    dyoung  * allocated the next time output is attempted.
    854    1.1       cgd  */
    855   1.10   mycroft void
    856  1.109  christos in_rtchange(struct inpcb *inp, int errno)
    857    1.1       cgd {
    858   1.32   mycroft 
    859   1.88    itojun 	if (inp->inp_af != AF_INET)
    860   1.88    itojun 		return;
    861   1.88    itojun 
    862  1.112     joerg 	rtcache_free(&inp->inp_route);
    863  1.112     joerg 
    864   1.49       tls 	/* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
    865    1.1       cgd }
    866    1.1       cgd 
    867    1.1       cgd struct inpcb *
    868   1.98     perry in_pcblookup_port(struct inpcbtable *table, struct in_addr laddr,
    869  1.138    dyoung 		  u_int lport_arg, int lookup_wildcard, vestigial_inpcb_t *vp)
    870    1.1       cgd {
    871   1.90    provos 	struct inpcbhead *head;
    872   1.88    itojun 	struct inpcb_hdr *inph;
    873  1.142      yamt 	struct inpcb *match = NULL;
    874  1.142      yamt 	int matchwild = 3;
    875  1.142      yamt 	int wildcard;
    876   1.33   mycroft 	u_int16_t lport = lport_arg;
    877    1.1       cgd 
    878  1.138    dyoung 	if (vp)
    879  1.138    dyoung 		vp->valid = 0;
    880  1.138    dyoung 
    881   1.90    provos 	head = INPCBHASH_PORT(table, lport);
    882   1.90    provos 	LIST_FOREACH(inph, head, inph_lhash) {
    883  1.142      yamt 		struct inpcb * const inp = (struct inpcb *)inph;
    884  1.142      yamt 
    885   1.88    itojun 		if (inp->inp_af != AF_INET)
    886   1.88    itojun 			continue;
    887    1.1       cgd 		if (inp->inp_lport != lport)
    888    1.1       cgd 			continue;
    889  1.142      yamt 		/*
    890  1.142      yamt 		 * check if inp's faddr and laddr match with ours.
    891  1.142      yamt 		 * our faddr is considered null.
    892  1.142      yamt 		 * count the number of wildcard matches. (0 - 2)
    893  1.142      yamt 		 *
    894  1.142      yamt 		 *	null	null	match
    895  1.142      yamt 		 *	A	null	wildcard match
    896  1.142      yamt 		 *	null	B	wildcard match
    897  1.142      yamt 		 *	A	B	non match
    898  1.142      yamt 		 *	A	A	match
    899  1.142      yamt 		 */
    900    1.1       cgd 		wildcard = 0;
    901   1.33   mycroft 		if (!in_nullhost(inp->inp_faddr))
    902   1.33   mycroft 			wildcard++;
    903   1.32   mycroft 		if (in_nullhost(inp->inp_laddr)) {
    904   1.32   mycroft 			if (!in_nullhost(laddr))
    905    1.1       cgd 				wildcard++;
    906    1.1       cgd 		} else {
    907   1.32   mycroft 			if (in_nullhost(laddr))
    908    1.1       cgd 				wildcard++;
    909   1.32   mycroft 			else {
    910   1.32   mycroft 				if (!in_hosteq(inp->inp_laddr, laddr))
    911   1.32   mycroft 					continue;
    912   1.32   mycroft 			}
    913    1.1       cgd 		}
    914   1.54     lukem 		if (wildcard && !lookup_wildcard)
    915    1.1       cgd 			continue;
    916  1.142      yamt 		/*
    917  1.142      yamt 		 * prefer an address with less wildcards.
    918  1.142      yamt 		 */
    919    1.1       cgd 		if (wildcard < matchwild) {
    920    1.1       cgd 			match = inp;
    921    1.1       cgd 			matchwild = wildcard;
    922    1.1       cgd 			if (matchwild == 0)
    923    1.1       cgd 				break;
    924    1.1       cgd 		}
    925    1.1       cgd 	}
    926  1.138    dyoung 	if (match && matchwild == 0)
    927  1.138    dyoung 		return match;
    928  1.138    dyoung 
    929  1.138    dyoung 	if (vp && table->vestige) {
    930  1.138    dyoung 		void	*state = (*table->vestige->init_ports4)(laddr, lport_arg, lookup_wildcard);
    931  1.138    dyoung 		vestigial_inpcb_t better;
    932  1.138    dyoung 
    933  1.138    dyoung 		while (table->vestige
    934  1.138    dyoung 		       && (*table->vestige->next_port4)(state, vp)) {
    935  1.138    dyoung 
    936  1.138    dyoung 			if (vp->lport != lport)
    937  1.138    dyoung 				continue;
    938  1.138    dyoung 			wildcard = 0;
    939  1.138    dyoung 			if (!in_nullhost(vp->faddr.v4))
    940  1.138    dyoung 				wildcard++;
    941  1.138    dyoung 			if (in_nullhost(vp->laddr.v4)) {
    942  1.138    dyoung 				if (!in_nullhost(laddr))
    943  1.138    dyoung 					wildcard++;
    944  1.138    dyoung 			} else {
    945  1.138    dyoung 				if (in_nullhost(laddr))
    946  1.138    dyoung 					wildcard++;
    947  1.138    dyoung 				else {
    948  1.138    dyoung 					if (!in_hosteq(vp->laddr.v4, laddr))
    949  1.138    dyoung 						continue;
    950  1.138    dyoung 				}
    951  1.138    dyoung 			}
    952  1.138    dyoung 			if (wildcard && !lookup_wildcard)
    953  1.138    dyoung 				continue;
    954  1.138    dyoung 			if (wildcard < matchwild) {
    955  1.138    dyoung 				better = *vp;
    956  1.138    dyoung 				match  = (void*)&better;
    957  1.138    dyoung 
    958  1.138    dyoung 				matchwild = wildcard;
    959  1.138    dyoung 				if (matchwild == 0)
    960  1.138    dyoung 					break;
    961  1.138    dyoung 			}
    962  1.138    dyoung 		}
    963  1.138    dyoung 
    964  1.138    dyoung 		if (match) {
    965  1.138    dyoung 			if (match != (void*)&better)
    966  1.138    dyoung 				return match;
    967  1.138    dyoung 			else {
    968  1.138    dyoung 				*vp = better;
    969  1.138    dyoung 				return 0;
    970  1.138    dyoung 			}
    971  1.138    dyoung 		}
    972  1.138    dyoung 	}
    973  1.138    dyoung 
    974    1.1       cgd 	return (match);
    975   1.24   mycroft }
    976   1.24   mycroft 
    977   1.24   mycroft #ifdef DIAGNOSTIC
    978   1.24   mycroft int	in_pcbnotifymiss = 0;
    979   1.24   mycroft #endif
    980   1.24   mycroft 
    981   1.24   mycroft struct inpcb *
    982   1.98     perry in_pcblookup_connect(struct inpcbtable *table,
    983   1.98     perry     struct in_addr faddr, u_int fport_arg,
    984  1.138    dyoung     struct in_addr laddr, u_int lport_arg,
    985  1.138    dyoung     vestigial_inpcb_t *vp)
    986   1.24   mycroft {
    987   1.24   mycroft 	struct inpcbhead *head;
    988   1.88    itojun 	struct inpcb_hdr *inph;
    989   1.64  augustss 	struct inpcb *inp;
    990   1.24   mycroft 	u_int16_t fport = fport_arg, lport = lport_arg;
    991   1.24   mycroft 
    992  1.138    dyoung 	if (vp)
    993  1.138    dyoung 		vp->valid = 0;
    994  1.138    dyoung 
    995   1.33   mycroft 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
    996   1.88    itojun 	LIST_FOREACH(inph, head, inph_hash) {
    997   1.88    itojun 		inp = (struct inpcb *)inph;
    998   1.88    itojun 		if (inp->inp_af != AF_INET)
    999   1.88    itojun 			continue;
   1000   1.88    itojun 
   1001   1.33   mycroft 		if (in_hosteq(inp->inp_faddr, faddr) &&
   1002   1.33   mycroft 		    inp->inp_fport == fport &&
   1003   1.33   mycroft 		    inp->inp_lport == lport &&
   1004   1.33   mycroft 		    in_hosteq(inp->inp_laddr, laddr))
   1005   1.33   mycroft 			goto out;
   1006   1.24   mycroft 	}
   1007  1.138    dyoung 	if (vp && table->vestige) {
   1008  1.138    dyoung 		if ((*table->vestige->lookup4)(faddr, fport_arg,
   1009  1.138    dyoung 					       laddr, lport_arg, vp))
   1010  1.138    dyoung 			return 0;
   1011  1.138    dyoung 	}
   1012  1.138    dyoung 
   1013   1.24   mycroft #ifdef DIAGNOSTIC
   1014   1.33   mycroft 	if (in_pcbnotifymiss) {
   1015   1.35  christos 		printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
   1016   1.24   mycroft 		    ntohl(faddr.s_addr), ntohs(fport),
   1017   1.24   mycroft 		    ntohl(laddr.s_addr), ntohs(lport));
   1018   1.24   mycroft 	}
   1019   1.24   mycroft #endif
   1020   1.33   mycroft 	return (0);
   1021   1.33   mycroft 
   1022   1.33   mycroft out:
   1023   1.33   mycroft 	/* Move this PCB to the head of hash chain. */
   1024   1.88    itojun 	inph = &inp->inp_head;
   1025   1.88    itojun 	if (inph != LIST_FIRST(head)) {
   1026   1.88    itojun 		LIST_REMOVE(inph, inph_hash);
   1027   1.88    itojun 		LIST_INSERT_HEAD(head, inph, inph_hash);
   1028   1.33   mycroft 	}
   1029   1.33   mycroft 	return (inp);
   1030   1.33   mycroft }
   1031   1.33   mycroft 
   1032   1.33   mycroft struct inpcb *
   1033   1.98     perry in_pcblookup_bind(struct inpcbtable *table,
   1034   1.98     perry     struct in_addr laddr, u_int lport_arg)
   1035   1.33   mycroft {
   1036   1.33   mycroft 	struct inpcbhead *head;
   1037   1.88    itojun 	struct inpcb_hdr *inph;
   1038   1.64  augustss 	struct inpcb *inp;
   1039   1.33   mycroft 	u_int16_t lport = lport_arg;
   1040   1.33   mycroft 
   1041   1.33   mycroft 	head = INPCBHASH_BIND(table, laddr, lport);
   1042   1.88    itojun 	LIST_FOREACH(inph, head, inph_hash) {
   1043   1.88    itojun 		inp = (struct inpcb *)inph;
   1044   1.88    itojun 		if (inp->inp_af != AF_INET)
   1045   1.88    itojun 			continue;
   1046   1.88    itojun 
   1047   1.33   mycroft 		if (inp->inp_lport == lport &&
   1048   1.33   mycroft 		    in_hosteq(inp->inp_laddr, laddr))
   1049   1.33   mycroft 			goto out;
   1050   1.33   mycroft 	}
   1051   1.33   mycroft 	head = INPCBHASH_BIND(table, zeroin_addr, lport);
   1052   1.88    itojun 	LIST_FOREACH(inph, head, inph_hash) {
   1053   1.88    itojun 		inp = (struct inpcb *)inph;
   1054   1.88    itojun 		if (inp->inp_af != AF_INET)
   1055   1.88    itojun 			continue;
   1056   1.88    itojun 
   1057   1.33   mycroft 		if (inp->inp_lport == lport &&
   1058   1.33   mycroft 		    in_hosteq(inp->inp_laddr, zeroin_addr))
   1059   1.33   mycroft 			goto out;
   1060   1.33   mycroft 	}
   1061   1.33   mycroft #ifdef DIAGNOSTIC
   1062   1.33   mycroft 	if (in_pcbnotifymiss) {
   1063   1.35  christos 		printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
   1064   1.33   mycroft 		    ntohl(laddr.s_addr), ntohs(lport));
   1065   1.33   mycroft 	}
   1066   1.33   mycroft #endif
   1067   1.33   mycroft 	return (0);
   1068   1.33   mycroft 
   1069   1.33   mycroft out:
   1070   1.33   mycroft 	/* Move this PCB to the head of hash chain. */
   1071   1.88    itojun 	inph = &inp->inp_head;
   1072   1.88    itojun 	if (inph != LIST_FIRST(head)) {
   1073   1.88    itojun 		LIST_REMOVE(inph, inph_hash);
   1074   1.88    itojun 		LIST_INSERT_HEAD(head, inph, inph_hash);
   1075   1.33   mycroft 	}
   1076   1.24   mycroft 	return (inp);
   1077   1.33   mycroft }
   1078   1.33   mycroft 
   1079   1.33   mycroft void
   1080   1.98     perry in_pcbstate(struct inpcb *inp, int state)
   1081   1.33   mycroft {
   1082   1.33   mycroft 
   1083   1.88    itojun 	if (inp->inp_af != AF_INET)
   1084   1.88    itojun 		return;
   1085   1.88    itojun 
   1086   1.33   mycroft 	if (inp->inp_state > INP_ATTACHED)
   1087   1.88    itojun 		LIST_REMOVE(&inp->inp_head, inph_hash);
   1088   1.33   mycroft 
   1089   1.33   mycroft 	switch (state) {
   1090   1.33   mycroft 	case INP_BOUND:
   1091   1.33   mycroft 		LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
   1092   1.88    itojun 		    inp->inp_laddr, inp->inp_lport), &inp->inp_head,
   1093   1.88    itojun 		    inph_hash);
   1094   1.33   mycroft 		break;
   1095   1.33   mycroft 	case INP_CONNECTED:
   1096   1.33   mycroft 		LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
   1097   1.33   mycroft 		    inp->inp_faddr, inp->inp_fport,
   1098   1.88    itojun 		    inp->inp_laddr, inp->inp_lport), &inp->inp_head,
   1099   1.88    itojun 		    inph_hash);
   1100   1.33   mycroft 		break;
   1101   1.33   mycroft 	}
   1102   1.33   mycroft 
   1103   1.33   mycroft 	inp->inp_state = state;
   1104   1.38   thorpej }
   1105   1.38   thorpej 
   1106   1.38   thorpej struct rtentry *
   1107   1.98     perry in_pcbrtentry(struct inpcb *inp)
   1108   1.38   thorpej {
   1109   1.38   thorpej 	struct route *ro;
   1110  1.117    dyoung 	union {
   1111  1.117    dyoung 		struct sockaddr		dst;
   1112  1.117    dyoung 		struct sockaddr_in	dst4;
   1113  1.117    dyoung 	} u;
   1114   1.88    itojun 
   1115   1.88    itojun 	if (inp->inp_af != AF_INET)
   1116   1.88    itojun 		return (NULL);
   1117   1.38   thorpej 
   1118   1.38   thorpej 	ro = &inp->inp_route;
   1119   1.38   thorpej 
   1120  1.117    dyoung 	sockaddr_in_init(&u.dst4, &inp->inp_faddr, 0);
   1121  1.117    dyoung 	return rtcache_lookup(ro, &u.dst);
   1122   1.59    itojun }
   1123  1.171     ozaki 
   1124  1.171     ozaki void
   1125  1.171     ozaki in_pcbrtentry_unref(struct rtentry *rt, struct inpcb *inp)
   1126  1.171     ozaki {
   1127  1.171     ozaki 
   1128  1.171     ozaki 	rtcache_unref(rt, &inp->inp_route);
   1129  1.171     ozaki }
   1130