xref: /freebsd/sys/netinet/udp_usrreq.c (revision 0b4539ee542f7011160c07bf52af7f16739f4a92)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
46dfab5b1SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
53329b236SRobert Watson  *	The Regents of the University of California.
63144b7d3SRobert Watson  * Copyright (c) 2008 Robert N. M. Watson
7fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8e06e816fSKevin Lo  * Copyright (c) 2014 Kevin Lo
93329b236SRobert Watson  * All rights reserved.
10df8bae1dSRodney W. Grimes  *
11fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
12fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
13fa046d87SRobert Watson  *
14df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
15df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
16df8bae1dSRodney W. Grimes  * are met:
17df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
19df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
20df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
21df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
22fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
394b421e2dSMike Silbersack #include <sys/cdefs.h>
4079288c11SBjoern A. Zeeb #include "opt_inet.h"
41cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
42f5514f08SRobert Watson #include "opt_ipsec.h"
430c325f53SAlexander V. Chernikov #include "opt_route.h"
449d3ddf43SAdrian Chadd #include "opt_rss.h"
45cfa1ca9dSYoshinobu Inoue 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47960ed29cSSeigo Tanimura #include <sys/domain.h>
484f590175SPaul Saab #include <sys/eventhandler.h>
49960ed29cSSeigo Tanimura #include <sys/jail.h>
50b110a8a2SGarrett Wollman #include <sys/kernel.h>
51960ed29cSSeigo Tanimura #include <sys/lock.h>
52df8bae1dSRodney W. Grimes #include <sys/malloc.h>
53df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
54acd3428bSRobert Watson #include <sys/priv.h>
55490d50b6SBrian Feldman #include <sys/proc.h>
56df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5757f60867SMark Johnston #include <sys/sdt.h>
58960ed29cSSeigo Tanimura #include <sys/signalvar.h>
59df8bae1dSRodney W. Grimes #include <sys/socket.h>
60df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
61960ed29cSSeigo Tanimura #include <sys/sx.h>
62b5e8ce9fSBruce Evans #include <sys/sysctl.h>
63816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
64f5514f08SRobert Watson #include <sys/systm.h>
658781d8e9SBruce Evans 
6669c2d429SJeff Roberson #include <vm/uma.h>
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes #include <net/if.h>
6976039bc8SGleb Smirnoff #include <net/if_var.h>
70df8bae1dSRodney W. Grimes #include <net/route.h>
71983066f0SAlexander V. Chernikov #include <net/route/nhop.h>
72b2bdc62aSAdrian Chadd #include <net/rss_config.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <netinet/in.h>
7557f60867SMark Johnston #include <netinet/in_kdtrace.h>
760c325f53SAlexander V. Chernikov #include <netinet/in_fib.h>
77960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
78f5514f08SRobert Watson #include <netinet/in_systm.h>
79960ed29cSSeigo Tanimura #include <netinet/in_var.h>
80df8bae1dSRodney W. Grimes #include <netinet/ip.h>
81cfa1ca9dSYoshinobu Inoue #ifdef INET6
82cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
83cfa1ca9dSYoshinobu Inoue #endif
84960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
85960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
86df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
87ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
88cfa1ca9dSYoshinobu Inoue #ifdef INET6
89cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
90cfa1ca9dSYoshinobu Inoue #endif
91df8bae1dSRodney W. Grimes #include <netinet/udp.h>
92df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
93e06e816fSKevin Lo #include <netinet/udplite.h>
948ad1a83bSAdrian Chadd #include <netinet/in_rss.h>
95df8bae1dSRodney W. Grimes 
96fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
97b9234fafSSam Leffler 
98db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
99db4f9cc7SJonathan Lemon 
100aed55708SRobert Watson #include <security/mac/mac_framework.h>
101aed55708SRobert Watson 
102df8bae1dSRodney W. Grimes /*
103e06e816fSKevin Lo  * UDP and UDP-Lite protocols implementation.
104df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
105e06e816fSKevin Lo  * Per RFC 3828, July, 2004.
106df8bae1dSRodney W. Grimes  */
10774eb3236SWarner Losh 
10874eb3236SWarner Losh /*
1093329b236SRobert Watson  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
1103329b236SRobert Watson  * removes the only data integrity mechanism for packets and malformed
111f5514f08SRobert Watson  * packets that would otherwise be discarded due to bad checksums, and may
112f5514f08SRobert Watson  * cause problems (especially for NFS data blocks).
11374eb3236SWarner Losh  */
11440b676beSBjoern A. Zeeb VNET_DEFINE(int, udp_cksum) = 1;
1156df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW,
11640b676beSBjoern A. Zeeb     &VNET_NAME(udp_cksum), 0, "compute udp checksum");
117df8bae1dSRodney W. Grimes 
118334fc582SBjoern A. Zeeb VNET_DEFINE(int, udp_log_in_vain) = 0;
119334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
120334fc582SBjoern A. Zeeb     &VNET_NAME(udp_log_in_vain), 0, "Log all incoming UDP packets");
121816a3d83SPoul-Henning Kamp 
12282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, udp_blackhole) = 0;
1236df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
124eddfbb76SRobert Watson     &VNET_NAME(udp_blackhole), 0,
1253329b236SRobert Watson     "Do not send port unreachables for refused connects");
1263ea9a7cfSGleb Smirnoff VNET_DEFINE(bool, udp_blackhole_local) = false;
1273ea9a7cfSGleb Smirnoff SYSCTL_BOOL(_net_inet_udp, OID_AUTO, blackhole_local, CTLFLAG_VNET |
1283ea9a7cfSGleb Smirnoff     CTLFLAG_RW, &VNET_NAME(udp_blackhole_local), false,
1293ea9a7cfSGleb Smirnoff     "Enforce net.inet.udp.blackhole for locally originated packets");
13016f7f31fSGeoff Rehmet 
13143bbb6aaSRobert Watson u_long	udp_sendspace = 9216;		/* really max datagram size */
13243bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
13343bbb6aaSRobert Watson     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
13443bbb6aaSRobert Watson 
13543bbb6aaSRobert Watson u_long	udp_recvspace = 40 * (1024 +
13643bbb6aaSRobert Watson #ifdef INET6
13743bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in6)
13843bbb6aaSRobert Watson #else
13943bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in)
14043bbb6aaSRobert Watson #endif
141e62b9bcaSSergey Kandaurov 				      );	/* 40 1K datagrams */
14243bbb6aaSRobert Watson 
14343bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14443bbb6aaSRobert Watson     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
14543bbb6aaSRobert Watson 
146eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, udbinfo);
147e06e816fSKevin Lo VNET_DEFINE(struct inpcbinfo, ulitecbinfo);
14815bd2b43SDavid Greenman 
14915bd2b43SDavid Greenman #ifndef UDBHASHSIZE
150e2ed8f35SAlexander Motin #define	UDBHASHSIZE	128
15115bd2b43SDavid Greenman #endif
15215bd2b43SDavid Greenman 
1535b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat);		/* from udp_var.h */
1545b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(udpstat);
1555b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat,
1565b7cb97cSAndrey V. Elsukov     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
157f2ea20e6SGarrett Wollman 
1585b7cb97cSAndrey V. Elsukov #ifdef VIMAGE
1595b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(udpstat);
1605b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */
16179288c11SBjoern A. Zeeb #ifdef INET
162bc725eafSRobert Watson static void	udp_detach(struct socket *so);
16379288c11SBjoern A. Zeeb #endif
16479288c11SBjoern A. Zeeb 
165483fe965SGleb Smirnoff INPCBSTORAGE_DEFINE(udpcbstor, udpcb, "udpinp", "udp_inpcb", "udp", "udphash");
166483fe965SGleb Smirnoff INPCBSTORAGE_DEFINE(udplitecbstor, udpcb, "udpliteinp", "udplite_inpcb",
1670aa120d5SGleb Smirnoff     "udplite", "udplitehash");
168e06e816fSKevin Lo 
16989128ff3SGleb Smirnoff static void
17078b1fc05SGleb Smirnoff udp_vnet_init(void *arg __unused)
171df8bae1dSRodney W. Grimes {
172af1ee11dSRobert Watson 
1738ad1a83bSAdrian Chadd 	/*
1748ad1a83bSAdrian Chadd 	 * For now default to 2-tuple UDP hashing - until the fragment
1758ad1a83bSAdrian Chadd 	 * reassembly code can also update the flowid.
1768ad1a83bSAdrian Chadd 	 *
1778ad1a83bSAdrian Chadd 	 * Once we can calculate the flowid that way and re-establish
1788ad1a83bSAdrian Chadd 	 * a 4-tuple, flip this to 4-tuple.
1798ad1a83bSAdrian Chadd 	 */
180fec8a8c7SGleb Smirnoff 	in_pcbinfo_init(&V_udbinfo, &udpcbstor, UDBHASHSIZE, UDBHASHSIZE);
18189128ff3SGleb Smirnoff 	/* Additional pcbinfo for UDP-Lite */
182fec8a8c7SGleb Smirnoff 	in_pcbinfo_init(&V_ulitecbinfo, &udplitecbstor, UDBHASHSIZE,
183fec8a8c7SGleb Smirnoff 	    UDBHASHSIZE);
184e06e816fSKevin Lo }
18578b1fc05SGleb Smirnoff VNET_SYSINIT(udp_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
18678b1fc05SGleb Smirnoff     udp_vnet_init, NULL);
187e06e816fSKevin Lo 
188315e3e38SRobert Watson /*
189315e3e38SRobert Watson  * Kernel module interface for updating udpstat.  The argument is an index
190315e3e38SRobert Watson  * into udpstat treated as an array of u_long.  While this encodes the
191315e3e38SRobert Watson  * general layout of udpstat into the caller, it doesn't encode its location,
192315e3e38SRobert Watson  * so that future changes to add, for example, per-CPU stats support won't
193315e3e38SRobert Watson  * cause binary compatibility problems for kernel modules.
194315e3e38SRobert Watson  */
195315e3e38SRobert Watson void
196315e3e38SRobert Watson kmod_udpstat_inc(int statnum)
197315e3e38SRobert Watson {
198315e3e38SRobert Watson 
1995b7cb97cSAndrey V. Elsukov 	counter_u64_add(VNET(udpstat)[statnum], 1);
200315e3e38SRobert Watson }
201315e3e38SRobert Watson 
202bc29160dSMarko Zec #ifdef VIMAGE
2033f58662dSBjoern A. Zeeb static void
2043f58662dSBjoern A. Zeeb udp_destroy(void *unused __unused)
205bc29160dSMarko Zec {
206bc29160dSMarko Zec 
2079bcd427bSRobert Watson 	in_pcbinfo_destroy(&V_udbinfo);
208294a609fSGleb Smirnoff 	in_pcbinfo_destroy(&V_ulitecbinfo);
209bc29160dSMarko Zec }
2103f58662dSBjoern A. Zeeb VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL);
211bc29160dSMarko Zec #endif
212bc29160dSMarko Zec 
21379288c11SBjoern A. Zeeb #ifdef INET
21443bbb6aaSRobert Watson /*
21543bbb6aaSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
21643bbb6aaSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
21743bbb6aaSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
21843bbb6aaSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
21943bbb6aaSRobert Watson  * into the socket code.
220c0d1be08SRandall Stewart  *
221c0d1be08SRandall Stewart  * In the normal case udp_append() will return 0, indicating that you
222c0d1be08SRandall Stewart  * must unlock the inp. However if a tunneling protocol is in place we increment
223c0d1be08SRandall Stewart  * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we
224c0d1be08SRandall Stewart  * then decrement the reference count. If the inp_rele returns 1, indicating the
225c0d1be08SRandall Stewart  * inp is gone, we return that to the caller to tell them *not* to unlock
226c0d1be08SRandall Stewart  * the inp. In the case of multi-cast this will cause the distribution
227c0d1be08SRandall Stewart  * to stop (though most tunneling protocols known currently do *not* use
228c0d1be08SRandall Stewart  * multicast).
22943bbb6aaSRobert Watson  */
230c0d1be08SRandall Stewart static int
23143bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
23243bbb6aaSRobert Watson     struct sockaddr_in *udp_in)
23343bbb6aaSRobert Watson {
23443bbb6aaSRobert Watson 	struct sockaddr *append_sa;
23543bbb6aaSRobert Watson 	struct socket *so;
236dce33a45SErmal Luçi 	struct mbuf *tmpopts, *opts = NULL;
23743bbb6aaSRobert Watson #ifdef INET6
23843bbb6aaSRobert Watson 	struct sockaddr_in6 udp_in6;
23943bbb6aaSRobert Watson #endif
2407b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
241742e7210SKristof Provost 	bool filtered;
24243bbb6aaSRobert Watson 
243fa046d87SRobert Watson 	INP_LOCK_ASSERT(inp);
24443bbb6aaSRobert Watson 
24579bb84fbSEdward Tomasz Napierala 	/*
24679bb84fbSEdward Tomasz Napierala 	 * Engage the tunneling protocol.
24779bb84fbSEdward Tomasz Napierala 	 */
24879bb84fbSEdward Tomasz Napierala 	up = intoudpcb(inp);
24979bb84fbSEdward Tomasz Napierala 	if (up->u_tun_func != NULL) {
250c0d1be08SRandall Stewart 		in_pcbref(inp);
251c0d1be08SRandall Stewart 		INP_RUNLOCK(inp);
252742e7210SKristof Provost 		filtered = (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&udp_in[0],
25381d3ec17SBryan Venteicher 		    up->u_tun_ctx);
254c0d1be08SRandall Stewart 		INP_RLOCK(inp);
255742e7210SKristof Provost 		if (filtered)
256c0d1be08SRandall Stewart 			return (in_pcbrele_rlocked(inp));
25779bb84fbSEdward Tomasz Napierala 	}
25879bb84fbSEdward Tomasz Napierala 
25979bb84fbSEdward Tomasz Napierala 	off += sizeof(struct udphdr);
26079bb84fbSEdward Tomasz Napierala 
261fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
26243bbb6aaSRobert Watson 	/* Check AH/ESP integrity. */
263fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
264fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, n, inp) != 0) {
26543bbb6aaSRobert Watson 		m_freem(n);
266c0d1be08SRandall Stewart 		return (0);
26743bbb6aaSRobert Watson 	}
268fcf59617SAndrey V. Elsukov 	if (up->u_flags & UF_ESPINUDP) {/* IPSec UDP encaps. */
269fcf59617SAndrey V. Elsukov 		if (IPSEC_ENABLED(ipv4) &&
27080044c78SXavier Beaudouin 		    UDPENCAP_INPUT(ipv4, n, off, AF_INET) != 0)
271fcf59617SAndrey V. Elsukov 			return (0);	/* Consumed. */
2727b495c44SVANHULLEBUS Yvan 	}
27343bbb6aaSRobert Watson #endif /* IPSEC */
27443bbb6aaSRobert Watson #ifdef MAC
27530d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, n) != 0) {
27643bbb6aaSRobert Watson 		m_freem(n);
277c0d1be08SRandall Stewart 		return (0);
27843bbb6aaSRobert Watson 	}
27979288c11SBjoern A. Zeeb #endif /* MAC */
28043bbb6aaSRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
28143bbb6aaSRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
28243bbb6aaSRobert Watson #ifdef INET6
2839a38ba81SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6)
28448d48eb9SBjoern A. Zeeb 			(void)ip6_savecontrol_v4(inp, n, &opts, NULL);
2859a38ba81SBjoern A. Zeeb 		else
28679288c11SBjoern A. Zeeb #endif /* INET6 */
28743bbb6aaSRobert Watson 			ip_savecontrol(inp, &opts, ip, n);
28843bbb6aaSRobert Watson 	}
289dce33a45SErmal Luçi 	if ((inp->inp_vflag & INP_IPV4) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
290b46667c6SGleb Smirnoff 		tmpopts = sbcreatecontrol(&udp_in[1],
291b46667c6SGleb Smirnoff 		    sizeof(struct sockaddr_in), IP_ORIGDSTADDR, IPPROTO_IP,
292b46667c6SGleb Smirnoff 		    M_NOWAIT);
293dce33a45SErmal Luçi 		if (tmpopts) {
294dce33a45SErmal Luçi 			if (opts) {
295dce33a45SErmal Luçi 				tmpopts->m_next = opts;
296dce33a45SErmal Luçi 				opts = tmpopts;
297dce33a45SErmal Luçi 			} else
298dce33a45SErmal Luçi 				opts = tmpopts;
299dce33a45SErmal Luçi 		}
300dce33a45SErmal Luçi 	}
30143bbb6aaSRobert Watson #ifdef INET6
30243bbb6aaSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
30343bbb6aaSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
30443bbb6aaSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
30543bbb6aaSRobert Watson 		udp_in6.sin6_family = AF_INET6;
306dce33a45SErmal Luçi 		in6_sin_2_v4mapsin6(&udp_in[0], &udp_in6);
30743bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
30843bbb6aaSRobert Watson 	} else
30979288c11SBjoern A. Zeeb #endif /* INET6 */
310dce33a45SErmal Luçi 		append_sa = (struct sockaddr *)&udp_in[0];
31143bbb6aaSRobert Watson 	m_adj(n, off);
31243bbb6aaSRobert Watson 
31343bbb6aaSRobert Watson 	so = inp->inp_socket;
31443bbb6aaSRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
31543bbb6aaSRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
316a61c24ddSKonstantin Kukushkin 		soroverflow_locked(so);
31743bbb6aaSRobert Watson 		m_freem(n);
31843bbb6aaSRobert Watson 		if (opts)
31943bbb6aaSRobert Watson 			m_freem(opts);
320026decb8SRobert Watson 		UDPSTAT_INC(udps_fullsock);
32143bbb6aaSRobert Watson 	} else
32243bbb6aaSRobert Watson 		sorwakeup_locked(so);
323c0d1be08SRandall Stewart 	return (0);
32443bbb6aaSRobert Watson }
32543bbb6aaSRobert Watson 
326db0ac6deSCy Schubert static bool
327db0ac6deSCy Schubert udp_multi_match(const struct inpcb *inp, void *v)
328db0ac6deSCy Schubert {
329db0ac6deSCy Schubert 	struct ip *ip = v;
330db0ac6deSCy Schubert 	struct udphdr *uh = (struct udphdr *)(ip + 1);
331db0ac6deSCy Schubert 
332db0ac6deSCy Schubert 	if (inp->inp_lport != uh->uh_dport)
333db0ac6deSCy Schubert 		return (false);
334db0ac6deSCy Schubert #ifdef INET6
335db0ac6deSCy Schubert 	if ((inp->inp_vflag & INP_IPV4) == 0)
336db0ac6deSCy Schubert 		return (false);
337db0ac6deSCy Schubert #endif
338db0ac6deSCy Schubert 	if (inp->inp_laddr.s_addr != INADDR_ANY &&
339db0ac6deSCy Schubert 	    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
340db0ac6deSCy Schubert 		return (false);
341db0ac6deSCy Schubert 	if (inp->inp_faddr.s_addr != INADDR_ANY &&
342db0ac6deSCy Schubert 	    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
343db0ac6deSCy Schubert 		return (false);
344db0ac6deSCy Schubert 	if (inp->inp_fport != 0 &&
345db0ac6deSCy Schubert 	    inp->inp_fport != uh->uh_sport)
346db0ac6deSCy Schubert 		return (false);
347db0ac6deSCy Schubert 
348db0ac6deSCy Schubert 	return (true);
349db0ac6deSCy Schubert }
350db0ac6deSCy Schubert 
351db0ac6deSCy Schubert static int
352db0ac6deSCy Schubert udp_multi_input(struct mbuf *m, int proto, struct sockaddr_in *udp_in)
353db0ac6deSCy Schubert {
354db0ac6deSCy Schubert 	struct ip *ip = mtod(m, struct ip *);
355db0ac6deSCy Schubert 	struct inpcb_iterator inpi = INP_ITERATOR(udp_get_inpcbinfo(proto),
356db0ac6deSCy Schubert 	    INPLOOKUP_RLOCKPCB, udp_multi_match, ip);
357bd1d0850SGleb Smirnoff #ifdef KDTRACE_HOOKS
358db0ac6deSCy Schubert 	struct udphdr *uh = (struct udphdr *)(ip + 1);
359bd1d0850SGleb Smirnoff #endif
360db0ac6deSCy Schubert 	struct inpcb *inp;
361db0ac6deSCy Schubert 	struct mbuf *n;
362db0ac6deSCy Schubert 	int appends = 0;
363db0ac6deSCy Schubert 
364db0ac6deSCy Schubert 	MPASS(ip->ip_hl == sizeof(struct ip) >> 2);
365db0ac6deSCy Schubert 
366db0ac6deSCy Schubert 	while ((inp = inp_next(&inpi)) != NULL) {
367db0ac6deSCy Schubert 		/*
368db0ac6deSCy Schubert 		 * XXXRW: Because we weren't holding either the inpcb
369db0ac6deSCy Schubert 		 * or the hash lock when we checked for a match
370db0ac6deSCy Schubert 		 * before, we should probably recheck now that the
371db0ac6deSCy Schubert 		 * inpcb lock is held.
372db0ac6deSCy Schubert 		 */
373db0ac6deSCy Schubert 		/*
374db0ac6deSCy Schubert 		 * Handle socket delivery policy for any-source
375db0ac6deSCy Schubert 		 * and source-specific multicast. [RFC3678]
376db0ac6deSCy Schubert 		 */
377db0ac6deSCy Schubert 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
378db0ac6deSCy Schubert 			struct ip_moptions	*imo;
379db0ac6deSCy Schubert 			struct sockaddr_in	 group;
380db0ac6deSCy Schubert 			int			 blocked;
381db0ac6deSCy Schubert 
382db0ac6deSCy Schubert 			imo = inp->inp_moptions;
383db0ac6deSCy Schubert 			if (imo == NULL)
384db0ac6deSCy Schubert 				continue;
385db0ac6deSCy Schubert 			bzero(&group, sizeof(struct sockaddr_in));
386db0ac6deSCy Schubert 			group.sin_len = sizeof(struct sockaddr_in);
387db0ac6deSCy Schubert 			group.sin_family = AF_INET;
388db0ac6deSCy Schubert 			group.sin_addr = ip->ip_dst;
389db0ac6deSCy Schubert 
390db0ac6deSCy Schubert 			blocked = imo_multi_filter(imo, m->m_pkthdr.rcvif,
391db0ac6deSCy Schubert 				(struct sockaddr *)&group,
392db0ac6deSCy Schubert 				(struct sockaddr *)&udp_in[0]);
393db0ac6deSCy Schubert 			if (blocked != MCAST_PASS) {
394db0ac6deSCy Schubert 				if (blocked == MCAST_NOTGMEMBER)
395db0ac6deSCy Schubert 					IPSTAT_INC(ips_notmember);
396db0ac6deSCy Schubert 				if (blocked == MCAST_NOTSMEMBER ||
397db0ac6deSCy Schubert 				    blocked == MCAST_MUTED)
398db0ac6deSCy Schubert 					UDPSTAT_INC(udps_filtermcast);
399db0ac6deSCy Schubert 				continue;
400db0ac6deSCy Schubert 			}
401db0ac6deSCy Schubert 		}
402db0ac6deSCy Schubert 		if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) {
403db0ac6deSCy Schubert 			if (proto == IPPROTO_UDPLITE)
404db0ac6deSCy Schubert 				UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
405db0ac6deSCy Schubert 			else
406db0ac6deSCy Schubert 				UDP_PROBE(receive, NULL, inp, ip, inp, uh);
407db0ac6deSCy Schubert 			if (udp_append(inp, ip, n, sizeof(struct ip), udp_in)) {
408db0ac6deSCy Schubert 				break;
409db0ac6deSCy Schubert 			} else
410db0ac6deSCy Schubert 				appends++;
411db0ac6deSCy Schubert 		}
412db0ac6deSCy Schubert 		/*
413db0ac6deSCy Schubert 		 * Don't look for additional matches if this one does
414db0ac6deSCy Schubert 		 * not have either the SO_REUSEPORT or SO_REUSEADDR
415db0ac6deSCy Schubert 		 * socket options set.  This heuristic avoids
416db0ac6deSCy Schubert 		 * searching through all pcbs in the common case of a
417db0ac6deSCy Schubert 		 * non-shared port.  It assumes that an application
418db0ac6deSCy Schubert 		 * will never clear these options after setting them.
419db0ac6deSCy Schubert 		 */
420db0ac6deSCy Schubert 		if ((inp->inp_socket->so_options &
421db0ac6deSCy Schubert 		    (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) {
422db0ac6deSCy Schubert 			INP_RUNLOCK(inp);
423db0ac6deSCy Schubert 			break;
424db0ac6deSCy Schubert 		}
425db0ac6deSCy Schubert 	}
426db0ac6deSCy Schubert 
427db0ac6deSCy Schubert 	if (appends == 0) {
428db0ac6deSCy Schubert 		/*
429db0ac6deSCy Schubert 		 * No matching pcb found; discard datagram.  (No need
430db0ac6deSCy Schubert 		 * to send an ICMP Port Unreachable for a broadcast
431db0ac6deSCy Schubert 		 * or multicast datgram.)
432db0ac6deSCy Schubert 		 */
433db0ac6deSCy Schubert 		UDPSTAT_INC(udps_noport);
434db0ac6deSCy Schubert 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)))
435db0ac6deSCy Schubert 			UDPSTAT_INC(udps_noportmcast);
436db0ac6deSCy Schubert 		else
437db0ac6deSCy Schubert 			UDPSTAT_INC(udps_noportbcast);
438db0ac6deSCy Schubert 	}
439014f98b1SMark Johnston 	m_freem(m);
440db0ac6deSCy Schubert 
441db0ac6deSCy Schubert 	return (IPPROTO_DONE);
442db0ac6deSCy Schubert }
443db0ac6deSCy Schubert 
44478b1fc05SGleb Smirnoff static int
4458f5a8818SKevin Lo udp_input(struct mbuf **mp, int *offp, int proto)
446df8bae1dSRodney W. Grimes {
4473329b236SRobert Watson 	struct ip *ip;
4483329b236SRobert Watson 	struct udphdr *uh;
44971498f30SBruce M Simpson 	struct ifnet *ifp;
4503329b236SRobert Watson 	struct inpcb *inp;
4518f134647SGleb Smirnoff 	uint16_t len, ip_len;
452e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
453dce33a45SErmal Luçi 	struct sockaddr_in udp_in[2];
4548f5a8818SKevin Lo 	struct mbuf *m;
4550b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
4568f5a8818SKevin Lo 	int cscov_partial, iphlen;
457df8bae1dSRodney W. Grimes 
4588f5a8818SKevin Lo 	m = *mp;
4598f5a8818SKevin Lo 	iphlen = *offp;
46071498f30SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
4618f5a8818SKevin Lo 	*mp = NULL;
462026decb8SRobert Watson 	UDPSTAT_INC(udps_ipackets);
463df8bae1dSRodney W. Grimes 
464df8bae1dSRodney W. Grimes 	/*
4653329b236SRobert Watson 	 * Strip IP options, if any; should skip this, make available to
4663329b236SRobert Watson 	 * user, and use on returned packets, but we don't yet have a way to
4673329b236SRobert Watson 	 * check the checksum with options still present.
468df8bae1dSRodney W. Grimes 	 */
469df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
470105bd211SGleb Smirnoff 		ip_stripoptions(m);
471df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
472df8bae1dSRodney W. Grimes 	}
473df8bae1dSRodney W. Grimes 
474df8bae1dSRodney W. Grimes 	/*
475df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
476df8bae1dSRodney W. Grimes 	 */
477df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
478d1b18731SKevin Lo 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) {
479026decb8SRobert Watson 			UDPSTAT_INC(udps_hdrops);
4808f5a8818SKevin Lo 			return (IPPROTO_DONE);
481df8bae1dSRodney W. Grimes 		}
482df8bae1dSRodney W. Grimes 	}
483503f4e47SBjoern A. Zeeb 	ip = mtod(m, struct ip *);
484df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
4858f5a8818SKevin Lo 	cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0;
486df8bae1dSRodney W. Grimes 
4873329b236SRobert Watson 	/*
4883329b236SRobert Watson 	 * Destination port of 0 is illegal, based on RFC768.
4893329b236SRobert Watson 	 */
490686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
491f76fcf6dSJeffrey Hsu 		goto badunlocked;
492686cdd19SJun-ichiro itojun Hagino 
493df8bae1dSRodney W. Grimes 	/*
4943329b236SRobert Watson 	 * Construct sockaddr format source address.  Stuff source address
4953329b236SRobert Watson 	 * and datagram in user buffer.
496b9234fafSSam Leffler 	 */
497dce33a45SErmal Luçi 	bzero(&udp_in[0], sizeof(struct sockaddr_in) * 2);
498dce33a45SErmal Luçi 	udp_in[0].sin_len = sizeof(struct sockaddr_in);
499dce33a45SErmal Luçi 	udp_in[0].sin_family = AF_INET;
500dce33a45SErmal Luçi 	udp_in[0].sin_port = uh->uh_sport;
501dce33a45SErmal Luçi 	udp_in[0].sin_addr = ip->ip_src;
502dce33a45SErmal Luçi 	udp_in[1].sin_len = sizeof(struct sockaddr_in);
503dce33a45SErmal Luçi 	udp_in[1].sin_family = AF_INET;
504dce33a45SErmal Luçi 	udp_in[1].sin_port = uh->uh_dport;
505dce33a45SErmal Luçi 	udp_in[1].sin_addr = ip->ip_dst;
506b9234fafSSam Leffler 
507b9234fafSSam Leffler 	/*
508af1ee11dSRobert Watson 	 * Make mbuf data length reflect UDP length.  If not enough data to
509af1ee11dSRobert Watson 	 * reflect UDP length, drop.
510df8bae1dSRodney W. Grimes 	 */
511df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
5128ad458a4SGleb Smirnoff 	ip_len = ntohs(ip->ip_len) - iphlen;
5130f4a0366SMichael Tuexen 	if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) {
514e06e816fSKevin Lo 		/* Zero means checksum over the complete packet. */
5150f4a0366SMichael Tuexen 		if (len == 0)
516e06e816fSKevin Lo 			len = ip_len;
517e06e816fSKevin Lo 		cscov_partial = 0;
518e06e816fSKevin Lo 	}
5198f134647SGleb Smirnoff 	if (ip_len != len) {
5208f134647SGleb Smirnoff 		if (len > ip_len || len < sizeof(struct udphdr)) {
521026decb8SRobert Watson 			UDPSTAT_INC(udps_badlen);
522f76fcf6dSJeffrey Hsu 			goto badunlocked;
523df8bae1dSRodney W. Grimes 		}
5248f5a8818SKevin Lo 		if (proto == IPPROTO_UDP)
5258f134647SGleb Smirnoff 			m_adj(m, len - ip_len);
526df8bae1dSRodney W. Grimes 	}
5273329b236SRobert Watson 
528df8bae1dSRodney W. Grimes 	/*
529df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
530df8bae1dSRodney W. Grimes 	 */
5316dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
53239629c92SDavid Malone 		u_short uh_sum;
53339629c92SDavid Malone 
534e06e816fSKevin Lo 		if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
535e06e816fSKevin Lo 		    !cscov_partial) {
536db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
53739629c92SDavid Malone 				uh_sum = m->m_pkthdr.csum_data;
538db4f9cc7SJonathan Lemon 			else
53939629c92SDavid Malone 				uh_sum = in_pseudo(ip->ip_src.s_addr,
540506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
5418f5a8818SKevin Lo 				    m->m_pkthdr.csum_data + proto));
54239629c92SDavid Malone 			uh_sum ^= 0xffff;
543db4f9cc7SJonathan Lemon 		} else {
5443358df29SGleb Smirnoff 			char b[offsetof(struct ipovly, ih_src)];
5453358df29SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
546af1ee11dSRobert Watson 
5473358df29SGleb Smirnoff 			bcopy(ipov, b, sizeof(b));
5483358df29SGleb Smirnoff 			bzero(ipov, sizeof(ipov->ih_x1));
5493358df29SGleb Smirnoff 			ipov->ih_len = (proto == IPPROTO_UDP) ?
550e06e816fSKevin Lo 			    uh->uh_ulen : htons(ip_len);
55139629c92SDavid Malone 			uh_sum = in_cksum(m, len + sizeof (struct ip));
5523358df29SGleb Smirnoff 			bcopy(b, ipov, sizeof(b));
553db4f9cc7SJonathan Lemon 		}
55439629c92SDavid Malone 		if (uh_sum) {
555026decb8SRobert Watson 			UDPSTAT_INC(udps_badsum);
556df8bae1dSRodney W. Grimes 			m_freem(m);
5578f5a8818SKevin Lo 			return (IPPROTO_DONE);
558df8bae1dSRodney W. Grimes 		}
559c6d81a34SMichael Tuexen 	} else {
560c6d81a34SMichael Tuexen 		if (proto == IPPROTO_UDP) {
561026decb8SRobert Watson 			UDPSTAT_INC(udps_nosum);
562c6d81a34SMichael Tuexen 		} else {
563c6d81a34SMichael Tuexen 			/* UDPLite requires a checksum */
564c6d81a34SMichael Tuexen 			/* XXX: What is the right UDPLite MIB counter here? */
565c6d81a34SMichael Tuexen 			m_freem(m);
566c6d81a34SMichael Tuexen 			return (IPPROTO_DONE);
567c6d81a34SMichael Tuexen 		}
568c6d81a34SMichael Tuexen 	}
569df8bae1dSRodney W. Grimes 
570266f97b5SCy Schubert 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
571db0ac6deSCy Schubert 	    in_broadcast(ip->ip_dst, ifp))
572db0ac6deSCy Schubert 		return (udp_multi_input(m, proto, udp_in));
573266f97b5SCy Schubert 
574db0ac6deSCy Schubert 	pcbinfo = udp_get_inpcbinfo(proto);
5753329b236SRobert Watson 
576df8bae1dSRodney W. Grimes 	/*
5776d6a026bSDavid Greenman 	 * Locate pcb for datagram.
578db0ac6deSCy Schubert 	 *
5798a006adbSBjoern A. Zeeb 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
5808a006adbSBjoern A. Zeeb 	 */
581ffdbf9daSAndrey V. Elsukov 	if ((m->m_flags & M_IP_NEXTHOP) &&
582c1de64a4SAndrey V. Elsukov 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
5838a006adbSBjoern A. Zeeb 		struct sockaddr_in *next_hop;
5848a006adbSBjoern A. Zeeb 
5858a006adbSBjoern A. Zeeb 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
5868a006adbSBjoern A. Zeeb 
5878a006adbSBjoern A. Zeeb 		/*
5888a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
5898a006adbSBjoern A. Zeeb 		 * Already got one like this?
5908a006adbSBjoern A. Zeeb 		 */
591e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
5928a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m);
5938a006adbSBjoern A. Zeeb 		if (!inp) {
5948a006adbSBjoern A. Zeeb 			/*
5958a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
5968a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
5978a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
5988a006adbSBjoern A. Zeeb 			 */
599e06e816fSKevin Lo 			inp = in_pcblookup(pcbinfo, ip->ip_src,
6008a006adbSBjoern A. Zeeb 			    uh->uh_sport, next_hop->sin_addr,
6018a006adbSBjoern A. Zeeb 			    next_hop->sin_port ? htons(next_hop->sin_port) :
6028a006adbSBjoern A. Zeeb 			    uh->uh_dport, INPLOOKUP_WILDCARD |
6038a006adbSBjoern A. Zeeb 			    INPLOOKUP_RLOCKPCB, ifp);
6048a006adbSBjoern A. Zeeb 		}
6058a006adbSBjoern A. Zeeb 		/* Remove the tag from the packet. We don't need it anymore. */
6068a006adbSBjoern A. Zeeb 		m_tag_delete(m, fwd_tag);
607ffdbf9daSAndrey V. Elsukov 		m->m_flags &= ~M_IP_NEXTHOP;
6088a006adbSBjoern A. Zeeb 	} else
609e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
6108a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
6118a006adbSBjoern A. Zeeb 		    INPLOOKUP_RLOCKPCB, ifp, m);
61215bd2b43SDavid Greenman 	if (inp == NULL) {
613334fc582SBjoern A. Zeeb 		if (V_udp_log_in_vain) {
614edf0313bSEric van Gyzen 			char src[INET_ADDRSTRLEN];
615edf0313bSEric van Gyzen 			char dst[INET_ADDRSTRLEN];
61675cfc95fSAndrey A. Chernov 
617592071e8SBruce Evans 			log(LOG_INFO,
618592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
619edf0313bSEric van Gyzen 			    inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport),
620edf0313bSEric van Gyzen 			    inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport));
62175cfc95fSAndrey A. Chernov 		}
6227bda9663SMichael Tuexen 		if (proto == IPPROTO_UDPLITE)
6237bda9663SMichael Tuexen 			UDPLITE_PROBE(receive, NULL, NULL, ip, NULL, uh);
6247bda9663SMichael Tuexen 		else
625e1526d5aSMichael Tuexen 			UDP_PROBE(receive, NULL, NULL, ip, NULL, uh);
626026decb8SRobert Watson 		UDPSTAT_INC(udps_noport);
627df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
628026decb8SRobert Watson 			UDPSTAT_INC(udps_noportbcast);
629fa046d87SRobert Watson 			goto badunlocked;
630df8bae1dSRodney W. Grimes 		}
6313ea9a7cfSGleb Smirnoff 		if (V_udp_blackhole && (V_udp_blackhole_local ||
6323ea9a7cfSGleb Smirnoff 		    !in_localip(ip->ip_src)))
633fa046d87SRobert Watson 			goto badunlocked;
6341cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
635fa046d87SRobert Watson 			goto badunlocked;
636582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
6378f5a8818SKevin Lo 		return (IPPROTO_DONE);
638df8bae1dSRodney W. Grimes 	}
6393329b236SRobert Watson 
6403329b236SRobert Watson 	/*
6413329b236SRobert Watson 	 * Check the minimum TTL for socket.
6423329b236SRobert Watson 	 */
643fa046d87SRobert Watson 	INP_RLOCK_ASSERT(inp);
64410cc62b7SRobert Watson 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
6457bda9663SMichael Tuexen 		if (proto == IPPROTO_UDPLITE)
6467bda9663SMichael Tuexen 			UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
6477bda9663SMichael Tuexen 		else
648e1526d5aSMichael Tuexen 			UDP_PROBE(receive, NULL, inp, ip, inp, uh);
64910cc62b7SRobert Watson 		INP_RUNLOCK(inp);
650fa046d87SRobert Watson 		m_freem(m);
6518f5a8818SKevin Lo 		return (IPPROTO_DONE);
65210cc62b7SRobert Watson 	}
653e06e816fSKevin Lo 	if (cscov_partial) {
654e06e816fSKevin Lo 		struct udpcb *up;
655e06e816fSKevin Lo 
656e06e816fSKevin Lo 		up = intoudpcb(inp);
65783e95fb3SMichael Tuexen 		if (up->u_rxcslen == 0 || up->u_rxcslen > len) {
658e06e816fSKevin Lo 			INP_RUNLOCK(inp);
659e06e816fSKevin Lo 			m_freem(m);
6608f5a8818SKevin Lo 			return (IPPROTO_DONE);
661e06e816fSKevin Lo 		}
662e06e816fSKevin Lo 	}
66357f60867SMark Johnston 
6647bda9663SMichael Tuexen 	if (proto == IPPROTO_UDPLITE)
6657bda9663SMichael Tuexen 		UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
6667bda9663SMichael Tuexen 	else
6671ad19fb6SMark Johnston 		UDP_PROBE(receive, NULL, inp, ip, inp, uh);
668dce33a45SErmal Luçi 	if (udp_append(inp, ip, m, iphlen, udp_in) == 0)
669119d85f6SRobert Watson 		INP_RUNLOCK(inp);
6708f5a8818SKevin Lo 	return (IPPROTO_DONE);
67161ffc0b1SJeffrey Hsu 
672f76fcf6dSJeffrey Hsu badunlocked:
673df8bae1dSRodney W. Grimes 	m_freem(m);
6748f5a8818SKevin Lo 	return (IPPROTO_DONE);
675cfa1ca9dSYoshinobu Inoue }
67679288c11SBjoern A. Zeeb #endif /* INET */
677cfa1ca9dSYoshinobu Inoue 
678cfa1ca9dSYoshinobu Inoue /*
6793329b236SRobert Watson  * Notify a udp user of an asynchronous error; just wake up so that they can
6803329b236SRobert Watson  * collect error status.
681df8bae1dSRodney W. Grimes  */
6823ce144eaSJeffrey Hsu struct inpcb *
6833329b236SRobert Watson udp_notify(struct inpcb *inp, int errno)
684df8bae1dSRodney W. Grimes {
6853329b236SRobert Watson 
686083a010cSRyan Stone 	INP_WLOCK_ASSERT(inp);
68784cc0778SGeorge V. Neville-Neil 	if ((errno == EHOSTUNREACH || errno == ENETUNREACH ||
688983066f0SAlexander V. Chernikov 	     errno == EHOSTDOWN) && inp->inp_route.ro_nh) {
689983066f0SAlexander V. Chernikov 		NH_FREE(inp->inp_route.ro_nh);
690983066f0SAlexander V. Chernikov 		inp->inp_route.ro_nh = (struct nhop_object *)NULL;
69184cc0778SGeorge V. Neville-Neil 	}
6928501a69cSRobert Watson 
693df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
694df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
695df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
6963329b236SRobert Watson 	return (inp);
697df8bae1dSRodney W. Grimes }
698df8bae1dSRodney W. Grimes 
69979288c11SBjoern A. Zeeb #ifdef INET
700e06e816fSKevin Lo static void
701fcb3f813SGleb Smirnoff udp_common_ctlinput(struct icmp *icmp, struct inpcbinfo *pcbinfo)
702df8bae1dSRodney W. Grimes {
703fcb3f813SGleb Smirnoff 	struct ip *ip = &icmp->icmp_ip;
704c693a045SJonathan Lemon 	struct udphdr *uh;
705c693a045SJonathan Lemon 	struct inpcb *inp;
706c693a045SJonathan Lemon 
707fcb3f813SGleb Smirnoff 	if (icmp_errmap(icmp) == 0)
70897d8d152SAndre Oppermann 		return;
7093329b236SRobert Watson 
710df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
711fcb3f813SGleb Smirnoff 	inp = in_pcblookup(pcbinfo, ip->ip_dst, uh->uh_dport, ip->ip_src,
712c0fc81e9SGleb Smirnoff 	    uh->uh_sport, INPLOOKUP_WLOCKPCB, NULL);
713f76fcf6dSJeffrey Hsu 	if (inp != NULL) {
714083a010cSRyan Stone 		INP_WLOCK_ASSERT(inp);
715c0fc81e9SGleb Smirnoff 		if (inp->inp_socket != NULL)
716fcb3f813SGleb Smirnoff 			udp_notify(inp, icmp_errmap(icmp));
717083a010cSRyan Stone 		INP_WUNLOCK(inp);
718abb901c5SRandall Stewart 	} else {
719fcb3f813SGleb Smirnoff 		inp = in_pcblookup(pcbinfo, ip->ip_dst, uh->uh_dport,
720abb901c5SRandall Stewart 		    ip->ip_src, uh->uh_sport,
721abb901c5SRandall Stewart 		    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
722abb901c5SRandall Stewart 		if (inp != NULL) {
723abb901c5SRandall Stewart 			struct udpcb *up;
724bb77f0c2SGleb Smirnoff 			udp_tun_icmp_t *func;
725abb901c5SRandall Stewart 
726abb901c5SRandall Stewart 			up = intoudpcb(inp);
72746374cbfSMatt Macy 			func = up->u_icmp_func;
728abb901c5SRandall Stewart 			INP_RUNLOCK(inp);
72946374cbfSMatt Macy 			if (func != NULL)
730fcb3f813SGleb Smirnoff 				func(icmp);
731abb901c5SRandall Stewart 		}
732f76fcf6dSJeffrey Hsu 	}
733df8bae1dSRodney W. Grimes }
73478b1fc05SGleb Smirnoff 
73578b1fc05SGleb Smirnoff static void
736fcb3f813SGleb Smirnoff udp_ctlinput(struct icmp *icmp)
737e06e816fSKevin Lo {
738e06e816fSKevin Lo 
739fcb3f813SGleb Smirnoff 	return (udp_common_ctlinput(icmp, &V_udbinfo));
740e06e816fSKevin Lo }
741e06e816fSKevin Lo 
74278b1fc05SGleb Smirnoff static void
743fcb3f813SGleb Smirnoff udplite_ctlinput(struct icmp *icmp)
744e06e816fSKevin Lo {
745e06e816fSKevin Lo 
746fcb3f813SGleb Smirnoff 	return (udp_common_ctlinput(icmp, &V_ulitecbinfo));
747e06e816fSKevin Lo }
74879288c11SBjoern A. Zeeb #endif /* INET */
749df8bae1dSRodney W. Grimes 
7500312fbe9SPoul-Henning Kamp static int
75182d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
75298271db4SGarrett Wollman {
753db0ac6deSCy Schubert 	struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_udbinfo,
754db0ac6deSCy Schubert 	    INPLOOKUP_RLOCKPCB);
75598271db4SGarrett Wollman 	struct xinpgen xig;
756032677ceSGleb Smirnoff 	struct inpcb *inp;
757032677ceSGleb Smirnoff 	int error;
75898271db4SGarrett Wollman 
759032677ceSGleb Smirnoff 	if (req->newptr != 0)
760032677ceSGleb Smirnoff 		return (EPERM);
761032677ceSGleb Smirnoff 
76298271db4SGarrett Wollman 	if (req->oldptr == 0) {
763032677ceSGleb Smirnoff 		int n;
764032677ceSGleb Smirnoff 
765603724d3SBjoern A. Zeeb 		n = V_udbinfo.ipi_count;
766c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
767c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
7683329b236SRobert Watson 		return (0);
76998271db4SGarrett Wollman 	}
77098271db4SGarrett Wollman 
771032677ceSGleb Smirnoff 	if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
77247934cefSDon Lewis 		return (error);
7735c38b6dbSDon Lewis 
77479db6fe7SMark Johnston 	bzero(&xig, sizeof(xig));
77598271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
776032677ceSGleb Smirnoff 	xig.xig_count = V_udbinfo.ipi_count;
777032677ceSGleb Smirnoff 	xig.xig_gen = V_udbinfo.ipi_gencnt;
77898271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
77998271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
78098271db4SGarrett Wollman 	if (error)
7813329b236SRobert Watson 		return (error);
78299208b82SMatt Macy 
783db0ac6deSCy Schubert 	while ((inp = inp_next(&inpi)) != NULL) {
784032677ceSGleb Smirnoff 		if (inp->inp_gencnt <= xig.xig_gen &&
785032677ceSGleb Smirnoff 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
78698271db4SGarrett Wollman 			struct xinpcb xi;
787d0e157f6SBjoern A. Zeeb 
788cc65eb4eSGleb Smirnoff 			in_pcbtoxinpcb(inp, &xi);
789266f97b5SCy Schubert 			error = SYSCTL_OUT(req, &xi, sizeof xi);
790db0ac6deSCy Schubert 			if (error) {
791266f97b5SCy Schubert 				INP_RUNLOCK(inp);
792db0ac6deSCy Schubert 				break;
79398271db4SGarrett Wollman 			}
794db0ac6deSCy Schubert 		}
795db0ac6deSCy Schubert 	}
796d0e157f6SBjoern A. Zeeb 
79798271db4SGarrett Wollman 	if (!error) {
79898271db4SGarrett Wollman 		/*
7993329b236SRobert Watson 		 * Give the user an updated idea of our state.  If the
8003329b236SRobert Watson 		 * generation differs from what we told her before, she knows
8013329b236SRobert Watson 		 * that something happened while we were processing this
8023329b236SRobert Watson 		 * request, and it might be necessary to retry.
80398271db4SGarrett Wollman 		 */
804603724d3SBjoern A. Zeeb 		xig.xig_gen = V_udbinfo.ipi_gencnt;
80598271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
806603724d3SBjoern A. Zeeb 		xig.xig_count = V_udbinfo.ipi_count;
80798271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
80898271db4SGarrett Wollman 	}
809032677ceSGleb Smirnoff 
8103329b236SRobert Watson 	return (error);
81198271db4SGarrett Wollman }
81298271db4SGarrett Wollman 
81379c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
8147029da5cSPawel Biernacki     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
8157029da5cSPawel Biernacki     udp_pcblist, "S,xinpcb",
8167029da5cSPawel Biernacki     "List of active UDP sockets");
81798271db4SGarrett Wollman 
81879288c11SBjoern A. Zeeb #ifdef INET
81998271db4SGarrett Wollman static int
82082d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
821490d50b6SBrian Feldman {
822c0511d3bSBrian Feldman 	struct xucred xuc;
823490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
824d797164aSGleb Smirnoff 	struct epoch_tracker et;
825490d50b6SBrian Feldman 	struct inpcb *inp;
826277afaffSRobert Watson 	int error;
827490d50b6SBrian Feldman 
82832f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
829490d50b6SBrian Feldman 	if (error)
830490d50b6SBrian Feldman 		return (error);
831490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
832490d50b6SBrian Feldman 	if (error)
833490d50b6SBrian Feldman 		return (error);
834d797164aSGleb Smirnoff 	NET_EPOCH_ENTER(et);
835fa046d87SRobert Watson 	inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
836fa046d87SRobert Watson 	    addrs[0].sin_addr, addrs[0].sin_port,
837fa046d87SRobert Watson 	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
838d797164aSGleb Smirnoff 	NET_EPOCH_EXIT(et);
8399622e84fSRobert Watson 	if (inp != NULL) {
840fa046d87SRobert Watson 		INP_RLOCK_ASSERT(inp);
8419622e84fSRobert Watson 		if (inp->inp_socket == NULL)
8429622e84fSRobert Watson 			error = ENOENT;
8439622e84fSRobert Watson 		if (error == 0)
844f08ef6c5SBjoern A. Zeeb 			error = cr_canseeinpcb(req->td->td_ucred, inp);
8459622e84fSRobert Watson 		if (error == 0)
84686d02c5cSBjoern A. Zeeb 			cru2x(inp->inp_cred, &xuc);
8479622e84fSRobert Watson 		INP_RUNLOCK(inp);
848fa046d87SRobert Watson 	} else
8499622e84fSRobert Watson 		error = ENOENT;
8500e1eebb8SDon Lewis 	if (error == 0)
8510e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
852490d50b6SBrian Feldman 	return (error);
853490d50b6SBrian Feldman }
854490d50b6SBrian Feldman 
8557ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
8567029da5cSPawel Biernacki     CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
8577029da5cSPawel Biernacki     0, 0, udp_getcred, "S,xucred",
8587029da5cSPawel Biernacki     "Get the xucred of a UDP connection");
85979288c11SBjoern A. Zeeb #endif /* INET */
860490d50b6SBrian Feldman 
8617b495c44SVANHULLEBUS Yvan int
8627b495c44SVANHULLEBUS Yvan udp_ctloutput(struct socket *so, struct sockopt *sopt)
8637b495c44SVANHULLEBUS Yvan {
8647b495c44SVANHULLEBUS Yvan 	struct inpcb *inp;
8657b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
866e06e816fSKevin Lo 	int isudplite, error, optval;
8677b495c44SVANHULLEBUS Yvan 
868e06e816fSKevin Lo 	error = 0;
869e06e816fSKevin Lo 	isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0;
8707b495c44SVANHULLEBUS Yvan 	inp = sotoinpcb(so);
8717b495c44SVANHULLEBUS Yvan 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
8727b495c44SVANHULLEBUS Yvan 	INP_WLOCK(inp);
873e06e816fSKevin Lo 	if (sopt->sopt_level != so->so_proto->pr_protocol) {
8747b495c44SVANHULLEBUS Yvan #ifdef INET6
8757b495c44SVANHULLEBUS Yvan 		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
8767b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
8777b495c44SVANHULLEBUS Yvan 			error = ip6_ctloutput(so, sopt);
87879288c11SBjoern A. Zeeb 		}
8797b495c44SVANHULLEBUS Yvan #endif
88079288c11SBjoern A. Zeeb #if defined(INET) && defined(INET6)
88179288c11SBjoern A. Zeeb 		else
88279288c11SBjoern A. Zeeb #endif
88379288c11SBjoern A. Zeeb #ifdef INET
88479288c11SBjoern A. Zeeb 		{
8857b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
8867b495c44SVANHULLEBUS Yvan 			error = ip_ctloutput(so, sopt);
8877b495c44SVANHULLEBUS Yvan 		}
8887b495c44SVANHULLEBUS Yvan #endif
8897b495c44SVANHULLEBUS Yvan 		return (error);
8907b495c44SVANHULLEBUS Yvan 	}
8917b495c44SVANHULLEBUS Yvan 
8927b495c44SVANHULLEBUS Yvan 	switch (sopt->sopt_dir) {
8937b495c44SVANHULLEBUS Yvan 	case SOPT_SET:
8947b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
895fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
89680044c78SXavier Beaudouin #if defined(INET) || defined(INET6)
8977b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
89880044c78SXavier Beaudouin #ifdef INET
89980044c78SXavier Beaudouin 			if (INP_SOCKAF(so) == AF_INET) {
900fcf59617SAndrey V. Elsukov 				if (!IPSEC_ENABLED(ipv4)) {
9017b495c44SVANHULLEBUS Yvan 					INP_WUNLOCK(inp);
902fcf59617SAndrey V. Elsukov 					return (ENOPROTOOPT);
9037b495c44SVANHULLEBUS Yvan 				}
90480044c78SXavier Beaudouin 				error = UDPENCAP_PCBCTL(ipv4, inp, sopt);
9057b495c44SVANHULLEBUS Yvan 				break;
90680044c78SXavier Beaudouin 			}
907fcf59617SAndrey V. Elsukov #endif /* INET */
90880044c78SXavier Beaudouin #ifdef INET6
90980044c78SXavier Beaudouin 			if (INP_SOCKAF(so) == AF_INET6) {
91080044c78SXavier Beaudouin 				if (!IPSEC_ENABLED(ipv6)) {
91180044c78SXavier Beaudouin 					INP_WUNLOCK(inp);
91280044c78SXavier Beaudouin 					return (ENOPROTOOPT);
91380044c78SXavier Beaudouin 				}
91480044c78SXavier Beaudouin 				error = UDPENCAP_PCBCTL(ipv6, inp, sopt);
91580044c78SXavier Beaudouin 				break;
91680044c78SXavier Beaudouin 			}
91780044c78SXavier Beaudouin #endif /* INET6 */
91880044c78SXavier Beaudouin 			INP_WUNLOCK(inp);
91980044c78SXavier Beaudouin 			return (EINVAL);
92080044c78SXavier Beaudouin #endif /* INET || INET6 */
92180044c78SXavier Beaudouin 
922fcf59617SAndrey V. Elsukov #endif /* IPSEC */
923e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
924e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
925e06e816fSKevin Lo 			if (!isudplite) {
926e06e816fSKevin Lo 				INP_WUNLOCK(inp);
927e06e816fSKevin Lo 				error = ENOPROTOOPT;
928e06e816fSKevin Lo 				break;
929e06e816fSKevin Lo 			}
930e06e816fSKevin Lo 			INP_WUNLOCK(inp);
931e06e816fSKevin Lo 			error = sooptcopyin(sopt, &optval, sizeof(optval),
932e06e816fSKevin Lo 			    sizeof(optval));
933e06e816fSKevin Lo 			if (error != 0)
934e06e816fSKevin Lo 				break;
935e06e816fSKevin Lo 			inp = sotoinpcb(so);
936e06e816fSKevin Lo 			KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
937e06e816fSKevin Lo 			INP_WLOCK(inp);
938e06e816fSKevin Lo 			up = intoudpcb(inp);
939e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
94003f90784SMichael Tuexen 			if ((optval != 0 && optval < 8) || (optval > 65535)) {
941e06e816fSKevin Lo 				INP_WUNLOCK(inp);
942e06e816fSKevin Lo 				error = EINVAL;
943e06e816fSKevin Lo 				break;
944e06e816fSKevin Lo 			}
945e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
946e06e816fSKevin Lo 				up->u_txcslen = optval;
947e06e816fSKevin Lo 			else
948e06e816fSKevin Lo 				up->u_rxcslen = optval;
949e06e816fSKevin Lo 			INP_WUNLOCK(inp);
950e06e816fSKevin Lo 			break;
9517b495c44SVANHULLEBUS Yvan 		default:
9527b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9537b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
9547b495c44SVANHULLEBUS Yvan 			break;
9557b495c44SVANHULLEBUS Yvan 		}
9567b495c44SVANHULLEBUS Yvan 		break;
9577b495c44SVANHULLEBUS Yvan 	case SOPT_GET:
9587b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
959fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
96080044c78SXavier Beaudouin #if defined(INET) || defined(INET6)
9617b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
96280044c78SXavier Beaudouin #ifdef INET
96380044c78SXavier Beaudouin 			if (INP_SOCKAF(so) == AF_INET) {
964fcf59617SAndrey V. Elsukov 				if (!IPSEC_ENABLED(ipv4)) {
9657b495c44SVANHULLEBUS Yvan 					INP_WUNLOCK(inp);
966fcf59617SAndrey V. Elsukov 					return (ENOPROTOOPT);
967fcf59617SAndrey V. Elsukov 				}
96880044c78SXavier Beaudouin 				error = UDPENCAP_PCBCTL(ipv4, inp, sopt);
9697b495c44SVANHULLEBUS Yvan 				break;
97080044c78SXavier Beaudouin 			}
971fcf59617SAndrey V. Elsukov #endif /* INET */
97280044c78SXavier Beaudouin #ifdef INET6
97380044c78SXavier Beaudouin 			if (INP_SOCKAF(so) == AF_INET6) {
97480044c78SXavier Beaudouin 				if (!IPSEC_ENABLED(ipv6)) {
97580044c78SXavier Beaudouin 					INP_WUNLOCK(inp);
97680044c78SXavier Beaudouin 					return (ENOPROTOOPT);
97780044c78SXavier Beaudouin 				}
97880044c78SXavier Beaudouin 				error = UDPENCAP_PCBCTL(ipv6, inp, sopt);
97980044c78SXavier Beaudouin 				break;
98080044c78SXavier Beaudouin 			}
98180044c78SXavier Beaudouin #endif /* INET6 */
98280044c78SXavier Beaudouin 			INP_WUNLOCK(inp);
98380044c78SXavier Beaudouin 			return (EINVAL);
98480044c78SXavier Beaudouin #endif /* INET || INET6 */
98580044c78SXavier Beaudouin 
986fcf59617SAndrey V. Elsukov #endif /* IPSEC */
987e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
988e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
989e06e816fSKevin Lo 			if (!isudplite) {
990e06e816fSKevin Lo 				INP_WUNLOCK(inp);
991e06e816fSKevin Lo 				error = ENOPROTOOPT;
992e06e816fSKevin Lo 				break;
993e06e816fSKevin Lo 			}
994e06e816fSKevin Lo 			up = intoudpcb(inp);
995e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
996e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
997e06e816fSKevin Lo 				optval = up->u_txcslen;
998e06e816fSKevin Lo 			else
999e06e816fSKevin Lo 				optval = up->u_rxcslen;
1000e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1001e06e816fSKevin Lo 			error = sooptcopyout(sopt, &optval, sizeof(optval));
1002e06e816fSKevin Lo 			break;
10037b495c44SVANHULLEBUS Yvan 		default:
10047b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10057b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
10067b495c44SVANHULLEBUS Yvan 			break;
10077b495c44SVANHULLEBUS Yvan 		}
10087b495c44SVANHULLEBUS Yvan 		break;
10097b495c44SVANHULLEBUS Yvan 	}
10107b495c44SVANHULLEBUS Yvan 	return (error);
10117b495c44SVANHULLEBUS Yvan }
10127b495c44SVANHULLEBUS Yvan 
101379288c11SBjoern A. Zeeb #ifdef INET
1014a9839c4aSBjoern A. Zeeb #ifdef INET6
1015a9839c4aSBjoern A. Zeeb /* The logic here is derived from ip6_setpktopt(). See comments there. */
1016a9839c4aSBjoern A. Zeeb static int
1017a9839c4aSBjoern A. Zeeb udp_v4mapped_pktinfo(struct cmsghdr *cm, struct sockaddr_in * src,
1018a9839c4aSBjoern A. Zeeb     struct inpcb *inp, int flags)
1019a9839c4aSBjoern A. Zeeb {
1020a9839c4aSBjoern A. Zeeb 	struct ifnet *ifp;
1021a9839c4aSBjoern A. Zeeb 	struct in6_pktinfo *pktinfo;
1022a9839c4aSBjoern A. Zeeb 	struct in_addr ia;
1023a9839c4aSBjoern A. Zeeb 
1024a9839c4aSBjoern A. Zeeb 	if ((flags & PRUS_IPV6) == 0)
1025a9839c4aSBjoern A. Zeeb 		return (0);
1026a9839c4aSBjoern A. Zeeb 
1027a9839c4aSBjoern A. Zeeb 	if (cm->cmsg_level != IPPROTO_IPV6)
1028a9839c4aSBjoern A. Zeeb 		return (0);
1029a9839c4aSBjoern A. Zeeb 
1030a9839c4aSBjoern A. Zeeb 	if  (cm->cmsg_type != IPV6_2292PKTINFO &&
1031a9839c4aSBjoern A. Zeeb 	    cm->cmsg_type != IPV6_PKTINFO)
1032a9839c4aSBjoern A. Zeeb 		return (0);
1033a9839c4aSBjoern A. Zeeb 
1034a9839c4aSBjoern A. Zeeb 	if (cm->cmsg_len !=
1035a9839c4aSBjoern A. Zeeb 	    CMSG_LEN(sizeof(struct in6_pktinfo)))
1036a9839c4aSBjoern A. Zeeb 		return (EINVAL);
1037a9839c4aSBjoern A. Zeeb 
1038a9839c4aSBjoern A. Zeeb 	pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
1039a9839c4aSBjoern A. Zeeb 	if (!IN6_IS_ADDR_V4MAPPED(&pktinfo->ipi6_addr) &&
1040a9839c4aSBjoern A. Zeeb 	    !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr))
1041a9839c4aSBjoern A. Zeeb 		return (EINVAL);
1042a9839c4aSBjoern A. Zeeb 
1043a9839c4aSBjoern A. Zeeb 	/* Validate the interface index if specified. */
1044a9839c4aSBjoern A. Zeeb 	if (pktinfo->ipi6_ifindex) {
1045d74b7baeSGleb Smirnoff 		struct epoch_tracker et;
1046d74b7baeSGleb Smirnoff 
1047d74b7baeSGleb Smirnoff 		NET_EPOCH_ENTER(et);
1048a9839c4aSBjoern A. Zeeb 		ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
1049d74b7baeSGleb Smirnoff 		NET_EPOCH_EXIT(et);	/* XXXGL: unsafe ifp */
1050a9839c4aSBjoern A. Zeeb 		if (ifp == NULL)
1051a9839c4aSBjoern A. Zeeb 			return (ENXIO);
1052d74b7baeSGleb Smirnoff 	} else
1053d74b7baeSGleb Smirnoff 		ifp = NULL;
1054a9839c4aSBjoern A. Zeeb 	if (ifp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
1055a9839c4aSBjoern A. Zeeb 		ia.s_addr = pktinfo->ipi6_addr.s6_addr32[3];
1056a9839c4aSBjoern A. Zeeb 		if (in_ifhasaddr(ifp, ia) == 0)
1057a9839c4aSBjoern A. Zeeb 			return (EADDRNOTAVAIL);
1058a9839c4aSBjoern A. Zeeb 	}
1059a9839c4aSBjoern A. Zeeb 
1060a9839c4aSBjoern A. Zeeb 	bzero(src, sizeof(*src));
1061a9839c4aSBjoern A. Zeeb 	src->sin_family = AF_INET;
1062a9839c4aSBjoern A. Zeeb 	src->sin_len = sizeof(*src);
1063a9839c4aSBjoern A. Zeeb 	src->sin_port = inp->inp_lport;
1064a9839c4aSBjoern A. Zeeb 	src->sin_addr.s_addr = pktinfo->ipi6_addr.s6_addr32[3];
1065a9839c4aSBjoern A. Zeeb 
1066a9839c4aSBjoern A. Zeeb 	return (0);
1067a9839c4aSBjoern A. Zeeb }
106832920f03SGleb Smirnoff #endif	/* INET6 */
1069a9839c4aSBjoern A. Zeeb 
107032920f03SGleb Smirnoff int
107132920f03SGleb Smirnoff udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
107232920f03SGleb Smirnoff     struct mbuf *control, struct thread *td)
1073df8bae1dSRodney W. Grimes {
107432920f03SGleb Smirnoff 	struct inpcb *inp;
10753329b236SRobert Watson 	struct udpiphdr *ui;
107632920f03SGleb Smirnoff 	int len, error = 0;
107790162a4eSIan Dowse 	struct in_addr faddr, laddr;
1078c557ae16SIan Dowse 	struct cmsghdr *cm;
1079e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1080c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
10816573d758SMatt Macy 	struct epoch_tracker et;
1082e06e816fSKevin Lo 	int cscov_partial = 0;
1083374ce248SMitchell Horne 	int ipflags = 0;
108490162a4eSIan Dowse 	u_short fport, lport;
1085abca3ae7SMichael Tuexen 	u_char tos, vflagsav;
1086e06e816fSKevin Lo 	uint8_t pr;
1087e06e816fSKevin Lo 	uint16_t cscov = 0;
10889d3ddf43SAdrian Chadd 	uint32_t flowid = 0;
1089c2529042SHans Petter Selasky 	uint8_t flowtype = M_HASHTYPE_NONE;
10904d32367aSMichael Tuexen 	bool use_cached_route;
1091df8bae1dSRodney W. Grimes 
109232920f03SGleb Smirnoff 	inp = sotoinpcb(so);
109332920f03SGleb Smirnoff 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
109432920f03SGleb Smirnoff 
109532920f03SGleb Smirnoff 	if (addr != NULL) {
109632920f03SGleb Smirnoff 		if (addr->sa_family != AF_INET)
109732920f03SGleb Smirnoff 			error = EAFNOSUPPORT;
109832920f03SGleb Smirnoff 		else if (addr->sa_len != sizeof(struct sockaddr_in))
109932920f03SGleb Smirnoff 			error = EINVAL;
110032920f03SGleb Smirnoff 		if (__predict_false(error != 0)) {
110132920f03SGleb Smirnoff 			m_freem(control);
110232920f03SGleb Smirnoff 			m_freem(m);
110332920f03SGleb Smirnoff 			return (error);
110432920f03SGleb Smirnoff 		}
110532920f03SGleb Smirnoff 	}
110632920f03SGleb Smirnoff 
110732920f03SGleb Smirnoff 	len = m->m_pkthdr.len;
1108430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1109c557ae16SIan Dowse 		if (control)
1110c557ae16SIan Dowse 			m_freem(control);
11115c32ea65SRobert Watson 		m_freem(m);
11123329b236SRobert Watson 		return (EMSGSIZE);
1113430d30d8SBill Fenner 	}
1114430d30d8SBill Fenner 
11151b7f0384SBruce M Simpson 	src.sin_family = 0;
111684cc0778SGeorge V. Neville-Neil 	sin = (struct sockaddr_in *)addr;
11172435e507SGleb Smirnoff 
1118eafaa1bcSBjoern A. Zeeb 	/*
11194d32367aSMichael Tuexen 	 * udp_send() may need to temporarily bind or connect the current
11204d32367aSMichael Tuexen 	 * inpcb.  As such, we don't know up front whether we will need the
11214d32367aSMichael Tuexen 	 * pcbinfo lock or not.  Do any work to decide what is needed up
11224d32367aSMichael Tuexen 	 * front before acquiring any locks.
11232435e507SGleb Smirnoff 	 *
11242435e507SGleb Smirnoff 	 * We will need network epoch in either case, to safely lookup into
11252435e507SGleb Smirnoff 	 * pcb hash.
1126eafaa1bcSBjoern A. Zeeb 	 */
11274d32367aSMichael Tuexen 	use_cached_route = sin == NULL || (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0);
11284d32367aSMichael Tuexen 	if (use_cached_route || (flags & PRUS_IPV6) != 0)
11292435e507SGleb Smirnoff 		INP_WLOCK(inp);
11302435e507SGleb Smirnoff 	else
11310cfdff24SBjoern A. Zeeb 		INP_RLOCK(inp);
11322435e507SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1133f584d74bSMichael Tuexen 	tos = inp->inp_ip_tos;
1134c557ae16SIan Dowse 	if (control != NULL) {
1135c557ae16SIan Dowse 		/*
11363329b236SRobert Watson 		 * XXX: Currently, we assume all the optional information is
11373329b236SRobert Watson 		 * stored in a single mbuf.
1138c557ae16SIan Dowse 		 */
1139c557ae16SIan Dowse 		if (control->m_next) {
1140c557ae16SIan Dowse 			m_freem(control);
11412435e507SGleb Smirnoff 			error = EINVAL;
11422435e507SGleb Smirnoff 			goto release;
1143c557ae16SIan Dowse 		}
1144c557ae16SIan Dowse 		for (; control->m_len > 0;
1145c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
1146c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1147c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
1148af1ee11dSRobert Watson 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
1149af1ee11dSRobert Watson 			    || cm->cmsg_len > control->m_len) {
1150c557ae16SIan Dowse 				error = EINVAL;
1151c557ae16SIan Dowse 				break;
1152c557ae16SIan Dowse 			}
1153a9839c4aSBjoern A. Zeeb #ifdef INET6
1154a9839c4aSBjoern A. Zeeb 			error = udp_v4mapped_pktinfo(cm, &src, inp, flags);
1155a9839c4aSBjoern A. Zeeb 			if (error != 0)
1156a9839c4aSBjoern A. Zeeb 				break;
1157a9839c4aSBjoern A. Zeeb #endif
1158c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
1159c557ae16SIan Dowse 				continue;
1160c557ae16SIan Dowse 
1161c557ae16SIan Dowse 			switch (cm->cmsg_type) {
1162c557ae16SIan Dowse 			case IP_SENDSRCADDR:
1163c557ae16SIan Dowse 				if (cm->cmsg_len !=
1164c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
1165c557ae16SIan Dowse 					error = EINVAL;
1166c557ae16SIan Dowse 					break;
1167c557ae16SIan Dowse 				}
1168c557ae16SIan Dowse 				bzero(&src, sizeof(src));
1169c557ae16SIan Dowse 				src.sin_family = AF_INET;
1170c557ae16SIan Dowse 				src.sin_len = sizeof(src);
1171c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
1172af1ee11dSRobert Watson 				src.sin_addr =
1173af1ee11dSRobert Watson 				    *(struct in_addr *)CMSG_DATA(cm);
1174c557ae16SIan Dowse 				break;
1175af1ee11dSRobert Watson 
1176f584d74bSMichael Tuexen 			case IP_TOS:
1177f584d74bSMichael Tuexen 				if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
1178f584d74bSMichael Tuexen 					error = EINVAL;
1179f584d74bSMichael Tuexen 					break;
1180f584d74bSMichael Tuexen 				}
1181f584d74bSMichael Tuexen 				tos = *(u_char *)CMSG_DATA(cm);
1182f584d74bSMichael Tuexen 				break;
1183f584d74bSMichael Tuexen 
11849d3ddf43SAdrian Chadd 			case IP_FLOWID:
11859d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
11869d3ddf43SAdrian Chadd 					error = EINVAL;
11879d3ddf43SAdrian Chadd 					break;
11889d3ddf43SAdrian Chadd 				}
11899d3ddf43SAdrian Chadd 				flowid = *(uint32_t *) CMSG_DATA(cm);
11909d3ddf43SAdrian Chadd 				break;
11919d3ddf43SAdrian Chadd 
11929d3ddf43SAdrian Chadd 			case IP_FLOWTYPE:
11939d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
11949d3ddf43SAdrian Chadd 					error = EINVAL;
11959d3ddf43SAdrian Chadd 					break;
11969d3ddf43SAdrian Chadd 				}
1197c2529042SHans Petter Selasky 				flowtype = *(uint32_t *) CMSG_DATA(cm);
11989d3ddf43SAdrian Chadd 				break;
11999d3ddf43SAdrian Chadd 
12009d3ddf43SAdrian Chadd #ifdef	RSS
12019d3ddf43SAdrian Chadd 			case IP_RSSBUCKETID:
12029d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
12039d3ddf43SAdrian Chadd 					error = EINVAL;
12049d3ddf43SAdrian Chadd 					break;
12059d3ddf43SAdrian Chadd 				}
12069d3ddf43SAdrian Chadd 				/* This is just a placeholder for now */
12079d3ddf43SAdrian Chadd 				break;
12089d3ddf43SAdrian Chadd #endif	/* RSS */
1209c557ae16SIan Dowse 			default:
1210c557ae16SIan Dowse 				error = ENOPROTOOPT;
1211c557ae16SIan Dowse 				break;
1212c557ae16SIan Dowse 			}
1213c557ae16SIan Dowse 			if (error)
1214c557ae16SIan Dowse 				break;
1215c557ae16SIan Dowse 		}
1216c557ae16SIan Dowse 		m_freem(control);
1217d8acd268SMark Johnston 		control = NULL;
1218c557ae16SIan Dowse 	}
12192435e507SGleb Smirnoff 	if (error)
12202435e507SGleb Smirnoff 		goto release;
12215c32ea65SRobert Watson 
1222e06e816fSKevin Lo 	pr = inp->inp_socket->so_proto->pr_protocol;
1223a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(pr);
12245c32ea65SRobert Watson 
12251b7f0384SBruce M Simpson 	/*
12261b7f0384SBruce M Simpson 	 * If the IP_SENDSRCADDR control message was specified, override the
12271b7f0384SBruce M Simpson 	 * source address for this datagram.  Its use is invalidated if the
12281b7f0384SBruce M Simpson 	 * address thus specified is incomplete or clobbers other inpcbs.
12291b7f0384SBruce M Simpson 	 */
123090162a4eSIan Dowse 	laddr = inp->inp_laddr;
123190162a4eSIan Dowse 	lport = inp->inp_lport;
12321b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET) {
12331b7f0384SBruce M Simpson 		if ((lport == 0) ||
12341b7f0384SBruce M Simpson 		    (laddr.s_addr == INADDR_ANY &&
12351b7f0384SBruce M Simpson 		     src.sin_addr.s_addr == INADDR_ANY)) {
1236c557ae16SIan Dowse 			error = EINVAL;
1237c557ae16SIan Dowse 			goto release;
1238c557ae16SIan Dowse 		}
1239abca3ae7SMichael Tuexen 		if ((flags & PRUS_IPV6) != 0) {
1240abca3ae7SMichael Tuexen 			vflagsav = inp->inp_vflag;
1241abca3ae7SMichael Tuexen 			inp->inp_vflag |= INP_IPV4;
1242abca3ae7SMichael Tuexen 			inp->inp_vflag &= ~INP_IPV6;
1243abca3ae7SMichael Tuexen 		}
1244db0ac6deSCy Schubert 		INP_HASH_WLOCK(pcbinfo);
124596871af0SGleb Smirnoff 		error = in_pcbbind_setup(inp, &src, &laddr.s_addr, &lport,
124696871af0SGleb Smirnoff 		    td->td_ucred);
1247db0ac6deSCy Schubert 		INP_HASH_WUNLOCK(pcbinfo);
1248abca3ae7SMichael Tuexen 		if ((flags & PRUS_IPV6) != 0)
1249abca3ae7SMichael Tuexen 			inp->inp_vflag = vflagsav;
1250c557ae16SIan Dowse 		if (error)
1251c557ae16SIan Dowse 			goto release;
1252c557ae16SIan Dowse 	}
1253c557ae16SIan Dowse 
12543144b7d3SRobert Watson 	/*
12553144b7d3SRobert Watson 	 * If a UDP socket has been connected, then a local address/port will
12563144b7d3SRobert Watson 	 * have been selected and bound.
12573144b7d3SRobert Watson 	 *
125843cc0bc1SRobert Watson 	 * If a UDP socket has not been connected to, then an explicit
12593144b7d3SRobert Watson 	 * destination address must be used, in which case a local
12603144b7d3SRobert Watson 	 * address/port may not have been selected and bound.
12613144b7d3SRobert Watson 	 */
126243cc0bc1SRobert Watson 	if (sin != NULL) {
1263c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
1264df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1265df8bae1dSRodney W. Grimes 			error = EISCONN;
1266df8bae1dSRodney W. Grimes 			goto release;
1267df8bae1dSRodney W. Grimes 		}
12683144b7d3SRobert Watson 
12693144b7d3SRobert Watson 		/*
12703144b7d3SRobert Watson 		 * Jail may rewrite the destination address, so let it do
12713144b7d3SRobert Watson 		 * that before we use it.
12723144b7d3SRobert Watson 		 */
1273b89e82ddSJamie Gritton 		error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1274b89e82ddSJamie Gritton 		if (error)
1275413628a7SBjoern A. Zeeb 			goto release;
12763144b7d3SRobert Watson 
12773144b7d3SRobert Watson 		/*
127843cc0bc1SRobert Watson 		 * If a local address or port hasn't yet been selected, or if
127943cc0bc1SRobert Watson 		 * the destination address needs to be rewritten due to using
128043cc0bc1SRobert Watson 		 * a special INADDR_ constant, invoke in_pcbconnect_setup()
128143cc0bc1SRobert Watson 		 * to do the heavy lifting.  Once a port is selected, we
128243cc0bc1SRobert Watson 		 * commit the binding back to the socket; we also commit the
128343cc0bc1SRobert Watson 		 * binding of the address if in jail.
128443cc0bc1SRobert Watson 		 *
128543cc0bc1SRobert Watson 		 * If we already have a valid binding and we're not
128643cc0bc1SRobert Watson 		 * requesting a destination address rewrite, use a fast path.
12873144b7d3SRobert Watson 		 */
128843cc0bc1SRobert Watson 		if (inp->inp_laddr.s_addr == INADDR_ANY ||
128943cc0bc1SRobert Watson 		    inp->inp_lport == 0 ||
129043cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_ANY ||
129143cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_BROADCAST) {
1292abca3ae7SMichael Tuexen 			if ((flags & PRUS_IPV6) != 0) {
1293abca3ae7SMichael Tuexen 				vflagsav = inp->inp_vflag;
1294abca3ae7SMichael Tuexen 				inp->inp_vflag |= INP_IPV4;
1295abca3ae7SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1296abca3ae7SMichael Tuexen 			}
1297db0ac6deSCy Schubert 			INP_HASH_WLOCK(pcbinfo);
1298a9d22cceSGleb Smirnoff 			error = in_pcbconnect_setup(inp, sin, &laddr.s_addr,
12999e46ff4dSGleb Smirnoff 			    &lport, &faddr.s_addr, &fport, td->td_ucred);
1300abca3ae7SMichael Tuexen 			if ((flags & PRUS_IPV6) != 0)
1301abca3ae7SMichael Tuexen 				inp->inp_vflag = vflagsav;
1302db0ac6deSCy Schubert 			if (error) {
1303db0ac6deSCy Schubert 				INP_HASH_WUNLOCK(pcbinfo);
130490162a4eSIan Dowse 				goto release;
1305db0ac6deSCy Schubert 			}
130690162a4eSIan Dowse 
130743cc0bc1SRobert Watson 			/*
130843cc0bc1SRobert Watson 			 * XXXRW: Why not commit the port if the address is
130943cc0bc1SRobert Watson 			 * !INADDR_ANY?
131043cc0bc1SRobert Watson 			 */
131190162a4eSIan Dowse 			/* Commit the local port if newly assigned. */
131290162a4eSIan Dowse 			if (inp->inp_laddr.s_addr == INADDR_ANY &&
131390162a4eSIan Dowse 			    inp->inp_lport == 0) {
1314c4d585aeSRobert Watson 				INP_WLOCK_ASSERT(inp);
13153a1757b9SGleb Smirnoff 				/*
131643cc0bc1SRobert Watson 				 * Remember addr if jailed, to prevent
131743cc0bc1SRobert Watson 				 * rebinding.
13183a1757b9SGleb Smirnoff 				 */
13190304c731SJamie Gritton 				if (prison_flag(td->td_ucred, PR_IP4))
13203a1757b9SGleb Smirnoff 					inp->inp_laddr = laddr;
132190162a4eSIan Dowse 				inp->inp_lport = lport;
13222435e507SGleb Smirnoff 				error = in_pcbinshash(inp);
13232435e507SGleb Smirnoff 				INP_HASH_WUNLOCK(pcbinfo);
13242435e507SGleb Smirnoff 				if (error != 0) {
132590162a4eSIan Dowse 					inp->inp_lport = 0;
132690162a4eSIan Dowse 					error = EAGAIN;
1327df8bae1dSRodney W. Grimes 					goto release;
1328df8bae1dSRodney W. Grimes 				}
132990162a4eSIan Dowse 				inp->inp_flags |= INP_ANONPORT;
1330db0ac6deSCy Schubert 			} else
1331db0ac6deSCy Schubert 				INP_HASH_WUNLOCK(pcbinfo);
1332df8bae1dSRodney W. Grimes 		} else {
133343cc0bc1SRobert Watson 			faddr = sin->sin_addr;
133443cc0bc1SRobert Watson 			fport = sin->sin_port;
133543cc0bc1SRobert Watson 		}
133643cc0bc1SRobert Watson 	} else {
1337c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
133890162a4eSIan Dowse 		faddr = inp->inp_faddr;
133990162a4eSIan Dowse 		fport = inp->inp_fport;
134090162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
1341df8bae1dSRodney W. Grimes 			error = ENOTCONN;
1342df8bae1dSRodney W. Grimes 			goto release;
1343df8bae1dSRodney W. Grimes 		}
1344df8bae1dSRodney W. Grimes 	}
1345e6ccd709SRobert Watson 
1346df8bae1dSRodney W. Grimes 	/*
1347e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
1348392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
1349392e8407SRobert Watson 	 * since we won't use that space at this layer.
1350df8bae1dSRodney W. Grimes 	 */
1351eb1b1807SGleb Smirnoff 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1352e6ccd709SRobert Watson 	if (m == NULL) {
1353df8bae1dSRodney W. Grimes 		error = ENOBUFS;
135449b19bfcSBruce M Simpson 		goto release;
1355df8bae1dSRodney W. Grimes 	}
1356e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
1357e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
1358392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
1359df8bae1dSRodney W. Grimes 
1360df8bae1dSRodney W. Grimes 	/*
13613329b236SRobert Watson 	 * Fill in mbuf with extended UDP header and addresses and length put
13623329b236SRobert Watson 	 * into network format.
1363df8bae1dSRodney W. Grimes 	 */
1364df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
1365aa64a8f5SMichael Tuexen 	/*
1366aa64a8f5SMichael Tuexen 	 * Filling only those fields of udpiphdr that participate in the
1367aa64a8f5SMichael Tuexen 	 * checksum calculation. The rest must be zeroed and will be filled
1368aa64a8f5SMichael Tuexen 	 * later.
1369aa64a8f5SMichael Tuexen 	 */
1370aa64a8f5SMichael Tuexen 	bzero(ui->ui_x1, sizeof(ui->ui_x1));
1371e06e816fSKevin Lo 	ui->ui_pr = pr;
137290162a4eSIan Dowse 	ui->ui_src = laddr;
137390162a4eSIan Dowse 	ui->ui_dst = faddr;
137490162a4eSIan Dowse 	ui->ui_sport = lport;
137590162a4eSIan Dowse 	ui->ui_dport = fport;
1376db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1377e06e816fSKevin Lo 	if (pr == IPPROTO_UDPLITE) {
1378e06e816fSKevin Lo 		struct udpcb *up;
1379e06e816fSKevin Lo 		uint16_t plen;
1380e06e816fSKevin Lo 
1381e06e816fSKevin Lo 		up = intoudpcb(inp);
1382e06e816fSKevin Lo 		cscov = up->u_txcslen;
1383e06e816fSKevin Lo 		plen = (u_short)len + sizeof(struct udphdr);
1384e06e816fSKevin Lo 		if (cscov >= plen)
1385e06e816fSKevin Lo 			cscov = 0;
1386e06e816fSKevin Lo 		ui->ui_len = htons(plen);
1387e06e816fSKevin Lo 		ui->ui_ulen = htons(cscov);
1388e06e816fSKevin Lo 		/*
1389e06e816fSKevin Lo 		 * For UDP-Lite, checksum coverage length of zero means
1390e06e816fSKevin Lo 		 * the entire UDPLite packet is covered by the checksum.
1391e06e816fSKevin Lo 		 */
1392e06e816fSKevin Lo 		cscov_partial = (cscov == 0) ? 0 : 1;
139334fc9072SMichael Tuexen 	}
1394df8bae1dSRodney W. Grimes 
1395b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
1396b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
1397b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
1398b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
13996fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
14008afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
14018afa2304SBruce M Simpson 
14021175d9d5SRobert Watson #ifdef MAC
14031175d9d5SRobert Watson 	mac_inpcb_create_mbuf(inp, m);
14041175d9d5SRobert Watson #endif
14051175d9d5SRobert Watson 
1406df8bae1dSRodney W. Grimes 	/*
1407db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
1408df8bae1dSRodney W. Grimes 	 */
1409e06e816fSKevin Lo 	ui->ui_sum = 0;
1410a485f139SMichael Tuexen 	if (pr == IPPROTO_UDPLITE) {
1411e06e816fSKevin Lo 		if (inp->inp_flags & INP_ONESBCAST)
1412e06e816fSKevin Lo 			faddr.s_addr = INADDR_BROADCAST;
1413a485f139SMichael Tuexen 		if (cscov_partial) {
1414e06e816fSKevin Lo 			if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1415e06e816fSKevin Lo 				ui->ui_sum = 0xffff;
1416a485f139SMichael Tuexen 		} else {
1417a485f139SMichael Tuexen 			if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1418a485f139SMichael Tuexen 				ui->ui_sum = 0xffff;
1419a485f139SMichael Tuexen 		}
1420a485f139SMichael Tuexen 	} else if (V_udp_cksum) {
14216fbfd582SAndre Oppermann 		if (inp->inp_flags & INP_ONESBCAST)
14228a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
14238a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1424e06e816fSKevin Lo 		    htons((u_short)len + sizeof(struct udphdr) + pr));
1425db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
1426db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1427e06e816fSKevin Lo 	}
1428aa64a8f5SMichael Tuexen 	/*
1429aa64a8f5SMichael Tuexen 	 * After finishing the checksum computation, fill the remaining fields
1430aa64a8f5SMichael Tuexen 	 * of udpiphdr.
1431aa64a8f5SMichael Tuexen 	 */
1432aa64a8f5SMichael Tuexen 	((struct ip *)ui)->ip_v = IPVERSION;
1433aa64a8f5SMichael Tuexen 	((struct ip *)ui)->ip_tos = tos;
14348f134647SGleb Smirnoff 	((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1435aa64a8f5SMichael Tuexen 	if (inp->inp_flags & INP_DONTFRAG)
1436aa64a8f5SMichael Tuexen 		((struct ip *)ui)->ip_off |= htons(IP_DF);
1437aa64a8f5SMichael Tuexen 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;
1438026decb8SRobert Watson 	UDPSTAT_INC(udps_opackets);
1439cfa1ca9dSYoshinobu Inoue 
14409d3ddf43SAdrian Chadd 	/*
14419d3ddf43SAdrian Chadd 	 * Setup flowid / RSS information for outbound socket.
14429d3ddf43SAdrian Chadd 	 *
14439d3ddf43SAdrian Chadd 	 * Once the UDP code decides to set a flowid some other way,
14449d3ddf43SAdrian Chadd 	 * this allows the flowid to be overridden by userland.
14459d3ddf43SAdrian Chadd 	 */
1446c2529042SHans Petter Selasky 	if (flowtype != M_HASHTYPE_NONE) {
14479d3ddf43SAdrian Chadd 		m->m_pkthdr.flowid = flowid;
1448c2529042SHans Petter Selasky 		M_HASHTYPE_SET(m, flowtype);
1449f9a6e8d7SBjoern A. Zeeb 	}
14500c325f53SAlexander V. Chernikov #if defined(ROUTE_MPATH) || defined(RSS)
14510c325f53SAlexander V. Chernikov 	else if (CALC_FLOWID_OUTBOUND_SENDTO) {
14528ad1a83bSAdrian Chadd 		uint32_t hash_val, hash_type;
14530c325f53SAlexander V. Chernikov 
14540c325f53SAlexander V. Chernikov 		hash_val = fib4_calc_packet_hash(laddr, faddr,
14550c325f53SAlexander V. Chernikov 		    lport, fport, pr, &hash_type);
14568ad1a83bSAdrian Chadd 		m->m_pkthdr.flowid = hash_val;
14578ad1a83bSAdrian Chadd 		M_HASHTYPE_SET(m, hash_type);
14588ad1a83bSAdrian Chadd 	}
14599d3ddf43SAdrian Chadd 
14608ad1a83bSAdrian Chadd 	/*
14618ad1a83bSAdrian Chadd 	 * Don't override with the inp cached flowid value.
14628ad1a83bSAdrian Chadd 	 *
14638ad1a83bSAdrian Chadd 	 * Depending upon the kind of send being done, the inp
14648ad1a83bSAdrian Chadd 	 * flowid/flowtype values may actually not be appropriate
14658ad1a83bSAdrian Chadd 	 * for this particular socket send.
14668ad1a83bSAdrian Chadd 	 *
14678ad1a83bSAdrian Chadd 	 * We should either leave the flowid at zero (which is what is
14688ad1a83bSAdrian Chadd 	 * currently done) or set it to some software generated
14698ad1a83bSAdrian Chadd 	 * hash value based on the packet contents.
14708ad1a83bSAdrian Chadd 	 */
14719d3ddf43SAdrian Chadd 	ipflags |= IP_NODEFAULTFLOWID;
14729d3ddf43SAdrian Chadd #endif	/* RSS */
14739d3ddf43SAdrian Chadd 
14747bda9663SMichael Tuexen 	if (pr == IPPROTO_UDPLITE)
14757bda9663SMichael Tuexen 		UDPLITE_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
14767bda9663SMichael Tuexen 	else
147757f60867SMark Johnston 		UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
147884cc0778SGeorge V. Neville-Neil 	error = ip_output(m, inp->inp_options,
14794d32367aSMichael Tuexen 	    use_cached_route ? &inp->inp_route : NULL, ipflags,
14805d846453SSam Leffler 	    inp->inp_moptions, inp);
14812435e507SGleb Smirnoff 	INP_UNLOCK(inp);
14822435e507SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1483df8bae1dSRodney W. Grimes 	return (error);
1484df8bae1dSRodney W. Grimes 
1485df8bae1dSRodney W. Grimes release:
14862435e507SGleb Smirnoff 	INP_UNLOCK(inp);
14872435e507SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1488df8bae1dSRodney W. Grimes 	m_freem(m);
1489df8bae1dSRodney W. Grimes 	return (error);
1490df8bae1dSRodney W. Grimes }
1491df8bae1dSRodney W. Grimes 
1492c93db4abSGleb Smirnoff void
1493d0390e05SGarrett Wollman udp_abort(struct socket *so)
1494df8bae1dSRodney W. Grimes {
1495d0390e05SGarrett Wollman 	struct inpcb *inp;
1496e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1497df8bae1dSRodney W. Grimes 
1498a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1499d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
150014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
15018501a69cSRobert Watson 	INP_WLOCK(inp);
1502a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1503e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1504a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1505e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1506d0390e05SGarrett Wollman 		soisdisconnected(so);
1507a152f8a3SRobert Watson 	}
15088501a69cSRobert Watson 	INP_WUNLOCK(inp);
1509df8bae1dSRodney W. Grimes }
1510df8bae1dSRodney W. Grimes 
1511d0390e05SGarrett Wollman static int
1512b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
1513d0390e05SGarrett Wollman {
1514630ba2c5SMatt Macy 	static uint32_t udp_flowid;
1515e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1516483fe965SGleb Smirnoff 	struct inpcb *inp;
1517483fe965SGleb Smirnoff 	struct udpcb *up;
1518277afaffSRobert Watson 	int error;
1519d0390e05SGarrett Wollman 
1520a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1521d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
152214ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1523cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
1524f24618aaSRobert Watson 	if (error)
15253329b236SRobert Watson 		return (error);
1526e06e816fSKevin Lo 	error = in_pcballoc(so, pcbinfo);
1527db0ac6deSCy Schubert 	if (error)
15283329b236SRobert Watson 		return (error);
1529cfa1ca9dSYoshinobu Inoue 
153068b5629bSRobert Watson 	inp = sotoinpcb(so);
1531603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
1532630ba2c5SMatt Macy 	inp->inp_flowid = atomic_fetchadd_int(&udp_flowid, 1);
1533630ba2c5SMatt Macy 	inp->inp_flowtype = M_HASHTYPE_OPAQUE;
1534483fe965SGleb Smirnoff 	up = intoudpcb(inp);
1535483fe965SGleb Smirnoff 	bzero(&up->u_start_zero, u_zero_size);
1536266f97b5SCy Schubert 	INP_WUNLOCK(inp);
1537db0ac6deSCy Schubert 
1538c7c7ea4bSRandall Stewart 	return (0);
1539c7c7ea4bSRandall Stewart }
154079288c11SBjoern A. Zeeb #endif /* INET */
1541c7c7ea4bSRandall Stewart 
1542c7c7ea4bSRandall Stewart int
1543abb901c5SRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx)
1544c7c7ea4bSRandall Stewart {
1545c7c7ea4bSRandall Stewart 	struct inpcb *inp;
15466a9148feSBjoern A. Zeeb 	struct udpcb *up;
1547c7c7ea4bSRandall Stewart 
154868b5629bSRobert Watson 	KASSERT(so->so_type == SOCK_DGRAM,
154968b5629bSRobert Watson 	    ("udp_set_kernel_tunneling: !dgram"));
155068b5629bSRobert Watson 	inp = sotoinpcb(so);
155168b5629bSRobert Watson 	KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1552c7c7ea4bSRandall Stewart 	INP_WLOCK(inp);
15536a9148feSBjoern A. Zeeb 	up = intoudpcb(inp);
1554995cba5aSKristof Provost 	if ((f != NULL || i != NULL) && ((up->u_tun_func != NULL) ||
1555995cba5aSKristof Provost 	    (up->u_icmp_func != NULL))) {
1556bbb0e3d9SRandall Stewart 		INP_WUNLOCK(inp);
1557bbb0e3d9SRandall Stewart 		return (EBUSY);
1558bbb0e3d9SRandall Stewart 	}
15596a9148feSBjoern A. Zeeb 	up->u_tun_func = f;
1560abb901c5SRandall Stewart 	up->u_icmp_func = i;
156181d3ec17SBryan Venteicher 	up->u_tun_ctx = ctx;
15628501a69cSRobert Watson 	INP_WUNLOCK(inp);
15633329b236SRobert Watson 	return (0);
1564df8bae1dSRodney W. Grimes }
1565d0390e05SGarrett Wollman 
156679288c11SBjoern A. Zeeb #ifdef INET
1567d0390e05SGarrett Wollman static int
1568b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1569d0390e05SGarrett Wollman {
1570d0390e05SGarrett Wollman 	struct inpcb *inp;
1571e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1572f96603b5SMark Johnston 	struct sockaddr_in *sinp;
1573277afaffSRobert Watson 	int error;
1574d0390e05SGarrett Wollman 
1575a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1576d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
157714ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1578f161d294SMark Johnston 
1579f96603b5SMark Johnston 	sinp = (struct sockaddr_in *)nam;
1580f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
1581f96603b5SMark Johnston 		/*
1582f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
1583f96603b5SMark Johnston 		 */
1584f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
15853f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
1586f96603b5SMark Johnston 		    sinp->sin_addr.s_addr != INADDR_ANY)
1587f161d294SMark Johnston 			return (EAFNOSUPPORT);
1588f96603b5SMark Johnston 		nam->sa_family = AF_INET;
1589f96603b5SMark Johnston 	}
1590f161d294SMark Johnston 	if (nam->sa_len != sizeof(struct sockaddr_in))
1591f161d294SMark Johnston 		return (EINVAL);
1592f161d294SMark Johnston 
15938501a69cSRobert Watson 	INP_WLOCK(inp);
1594e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
159596871af0SGleb Smirnoff 	error = in_pcbbind(inp, sinp, td->td_ucred);
1596e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
15978501a69cSRobert Watson 	INP_WUNLOCK(inp);
15983329b236SRobert Watson 	return (error);
1599d0390e05SGarrett Wollman }
1600d0390e05SGarrett Wollman 
1601a152f8a3SRobert Watson static void
1602a152f8a3SRobert Watson udp_close(struct socket *so)
1603a152f8a3SRobert Watson {
1604a152f8a3SRobert Watson 	struct inpcb *inp;
1605e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1606a152f8a3SRobert Watson 
1607a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1608a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1609a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
16108501a69cSRobert Watson 	INP_WLOCK(inp);
1611a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1612e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1613a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1614e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1615a152f8a3SRobert Watson 		soisdisconnected(so);
1616a152f8a3SRobert Watson 	}
16178501a69cSRobert Watson 	INP_WUNLOCK(inp);
1618a152f8a3SRobert Watson }
1619a152f8a3SRobert Watson 
1620d0390e05SGarrett Wollman static int
1621b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1622d0390e05SGarrett Wollman {
1623c1604fe4SGleb Smirnoff 	struct epoch_tracker et;
1624d0390e05SGarrett Wollman 	struct inpcb *inp;
1625e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
162675c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1627e06e816fSKevin Lo 	int error;
1628d0390e05SGarrett Wollman 
1629a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1630d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
163114ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1632f161d294SMark Johnston 
1633f161d294SMark Johnston 	sin = (struct sockaddr_in *)nam;
1634f161d294SMark Johnston 	if (sin->sin_family != AF_INET)
1635f161d294SMark Johnston 		return (EAFNOSUPPORT);
1636f161d294SMark Johnston 	if (sin->sin_len != sizeof(*sin))
1637f161d294SMark Johnston 		return (EINVAL);
1638f161d294SMark Johnston 
16398501a69cSRobert Watson 	INP_WLOCK(inp);
1640f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
16418501a69cSRobert Watson 		INP_WUNLOCK(inp);
16423329b236SRobert Watson 		return (EISCONN);
1643f76fcf6dSJeffrey Hsu 	}
1644b89e82ddSJamie Gritton 	error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1645b89e82ddSJamie Gritton 	if (error != 0) {
1646413628a7SBjoern A. Zeeb 		INP_WUNLOCK(inp);
1647b89e82ddSJamie Gritton 		return (error);
1648413628a7SBjoern A. Zeeb 	}
1649c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1650e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1651*0b4539eeSGleb Smirnoff 	error = in_pcbconnect(inp, sin, td->td_ucred);
1652e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
1653c1604fe4SGleb Smirnoff 	NET_EPOCH_EXIT(et);
16544cc20ab1SSeigo Tanimura 	if (error == 0)
1655df8bae1dSRodney W. Grimes 		soisconnected(so);
16568501a69cSRobert Watson 	INP_WUNLOCK(inp);
16573329b236SRobert Watson 	return (error);
1658df8bae1dSRodney W. Grimes }
1659d0390e05SGarrett Wollman 
1660bc725eafSRobert Watson static void
1661d0390e05SGarrett Wollman udp_detach(struct socket *so)
1662d0390e05SGarrett Wollman {
1663d0390e05SGarrett Wollman 	struct inpcb *inp;
1664d0390e05SGarrett Wollman 
1665d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
166614ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1667a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1668a152f8a3SRobert Watson 	    ("udp_detach: not disconnected"));
16698501a69cSRobert Watson 	INP_WLOCK(inp);
167014ba8addSRobert Watson 	in_pcbfree(inp);
1671d0390e05SGarrett Wollman }
1672d0390e05SGarrett Wollman 
1673c93db4abSGleb Smirnoff int
1674d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1675d0390e05SGarrett Wollman {
1676d0390e05SGarrett Wollman 	struct inpcb *inp;
1677e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1678d0390e05SGarrett Wollman 
1679a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1680d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
168114ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
16828501a69cSRobert Watson 	INP_WLOCK(inp);
1683f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
16848501a69cSRobert Watson 		INP_WUNLOCK(inp);
16853329b236SRobert Watson 		return (ENOTCONN);
1686f76fcf6dSJeffrey Hsu 	}
1687e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1688df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1689e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
1690d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1691d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1692d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
16938501a69cSRobert Watson 	INP_WUNLOCK(inp);
16943329b236SRobert Watson 	return (0);
1695df8bae1dSRodney W. Grimes }
169679288c11SBjoern A. Zeeb #endif /* INET */
1697d0390e05SGarrett Wollman 
169876429de4SYoshinobu Inoue int
16995bba2728SGleb Smirnoff udp_shutdown(struct socket *so, enum shutdown_how how)
1700d0390e05SGarrett Wollman {
17015bba2728SGleb Smirnoff 	int error;
1702d0390e05SGarrett Wollman 
17035bba2728SGleb Smirnoff 	SOCK_LOCK(so);
17045bba2728SGleb Smirnoff 	if (!(so->so_state & SS_ISCONNECTED))
17055bba2728SGleb Smirnoff 		/*
17065bba2728SGleb Smirnoff 		 * POSIX mandates us to just return ENOTCONN when shutdown(2) is
17075bba2728SGleb Smirnoff 		 * invoked on a datagram sockets, however historically we would
17085bba2728SGleb Smirnoff 		 * actually tear socket down.  This is known to be leveraged by
17095bba2728SGleb Smirnoff 		 * some applications to unblock process waiting in recv(2) by
17105bba2728SGleb Smirnoff 		 * other process that it shares that socket with.  Try to meet
17115bba2728SGleb Smirnoff 		 * both backward-compatibility and POSIX requirements by forcing
17125bba2728SGleb Smirnoff 		 * ENOTCONN but still flushing buffers and performing wakeup(9).
17135bba2728SGleb Smirnoff 		 *
17145bba2728SGleb Smirnoff 		 * XXXGL: it remains unknown what applications expect this
17155bba2728SGleb Smirnoff 		 * behavior and is this isolated to unix/dgram or inet/dgram or
17165bba2728SGleb Smirnoff 		 * both.  See: D10351, D3039.
17175bba2728SGleb Smirnoff 		 */
17185bba2728SGleb Smirnoff 		error = ENOTCONN;
17195bba2728SGleb Smirnoff 	else
17205bba2728SGleb Smirnoff 		error = 0;
17215bba2728SGleb Smirnoff 	SOCK_UNLOCK(so);
17225bba2728SGleb Smirnoff 
17235bba2728SGleb Smirnoff 	switch (how) {
17245bba2728SGleb Smirnoff 	case SHUT_RD:
1725ce69e373SGleb Smirnoff 		sorflush(so);
17265bba2728SGleb Smirnoff 		break;
17275bba2728SGleb Smirnoff 	case SHUT_RDWR:
1728ce69e373SGleb Smirnoff 		sorflush(so);
17295bba2728SGleb Smirnoff 		/* FALLTHROUGH */
17305bba2728SGleb Smirnoff 	case SHUT_WR:
1731d0390e05SGarrett Wollman 		socantsendmore(so);
17325bba2728SGleb Smirnoff 	}
17335bba2728SGleb Smirnoff 
17345bba2728SGleb Smirnoff 	return (error);
1735d0390e05SGarrett Wollman }
1736d0390e05SGarrett Wollman 
173779288c11SBjoern A. Zeeb #ifdef INET
1738e7d02be1SGleb Smirnoff #define	UDP_PROTOSW							\
1739e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_DGRAM,				\
1740e7d02be1SGleb Smirnoff 	.pr_flags =		PR_ATOMIC | PR_ADDR | PR_CAPATTACH,	\
1741e7d02be1SGleb Smirnoff 	.pr_ctloutput =		udp_ctloutput,				\
1742e7d02be1SGleb Smirnoff 	.pr_abort =		udp_abort,				\
1743e7d02be1SGleb Smirnoff 	.pr_attach =		udp_attach,				\
1744e7d02be1SGleb Smirnoff 	.pr_bind =		udp_bind,				\
1745e7d02be1SGleb Smirnoff 	.pr_connect =		udp_connect,				\
1746e7d02be1SGleb Smirnoff 	.pr_control =		in_control,				\
1747e7d02be1SGleb Smirnoff 	.pr_detach =		udp_detach,				\
1748e7d02be1SGleb Smirnoff 	.pr_disconnect =	udp_disconnect,				\
1749e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,				\
1750e7d02be1SGleb Smirnoff 	.pr_send =		udp_send,				\
1751e7d02be1SGleb Smirnoff 	.pr_soreceive =		soreceive_dgram,			\
1752e7d02be1SGleb Smirnoff 	.pr_sosend =		sosend_dgram,				\
1753e7d02be1SGleb Smirnoff 	.pr_shutdown =		udp_shutdown,				\
1754e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,				\
1755e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,			\
1756e7d02be1SGleb Smirnoff 	.pr_close =		udp_close
1757e7d02be1SGleb Smirnoff 
1758e7d02be1SGleb Smirnoff struct protosw udp_protosw = {
1759e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_UDP,
1760e7d02be1SGleb Smirnoff 	UDP_PROTOSW
1761e7d02be1SGleb Smirnoff };
1762e7d02be1SGleb Smirnoff 
1763e7d02be1SGleb Smirnoff struct protosw udplite_protosw = {
1764e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_UDPLITE,
1765e7d02be1SGleb Smirnoff 	UDP_PROTOSW
1766d0390e05SGarrett Wollman };
176778b1fc05SGleb Smirnoff 
176878b1fc05SGleb Smirnoff static void
176978b1fc05SGleb Smirnoff udp_init(void *arg __unused)
177078b1fc05SGleb Smirnoff {
177178b1fc05SGleb Smirnoff 
177278b1fc05SGleb Smirnoff 	IPPROTO_REGISTER(IPPROTO_UDP, udp_input, udp_ctlinput);
177378b1fc05SGleb Smirnoff 	IPPROTO_REGISTER(IPPROTO_UDPLITE, udp_input, udplite_ctlinput);
177478b1fc05SGleb Smirnoff }
177578b1fc05SGleb Smirnoff SYSINIT(udp_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, udp_init, NULL);
177679288c11SBjoern A. Zeeb #endif /* INET */
1777