xref: /freebsd/sys/netinet/udp_usrreq.c (revision 483fe96511ec02a3f077f9a59f1a96acb3640dea)
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  *
386dfab5b1SGarrett Wollman  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
414b421e2dSMike Silbersack #include <sys/cdefs.h>
424b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
434b421e2dSMike Silbersack 
4479288c11SBjoern A. Zeeb #include "opt_inet.h"
45cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
46f5514f08SRobert Watson #include "opt_ipsec.h"
470c325f53SAlexander V. Chernikov #include "opt_route.h"
489d3ddf43SAdrian Chadd #include "opt_rss.h"
49cfa1ca9dSYoshinobu Inoue 
50df8bae1dSRodney W. Grimes #include <sys/param.h>
51960ed29cSSeigo Tanimura #include <sys/domain.h>
524f590175SPaul Saab #include <sys/eventhandler.h>
53960ed29cSSeigo Tanimura #include <sys/jail.h>
54b110a8a2SGarrett Wollman #include <sys/kernel.h>
55960ed29cSSeigo Tanimura #include <sys/lock.h>
56df8bae1dSRodney W. Grimes #include <sys/malloc.h>
57df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
58acd3428bSRobert Watson #include <sys/priv.h>
59490d50b6SBrian Feldman #include <sys/proc.h>
60df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6157f60867SMark Johnston #include <sys/sdt.h>
62960ed29cSSeigo Tanimura #include <sys/signalvar.h>
63df8bae1dSRodney W. Grimes #include <sys/socket.h>
64df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
65960ed29cSSeigo Tanimura #include <sys/sx.h>
66b5e8ce9fSBruce Evans #include <sys/sysctl.h>
67816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
68f5514f08SRobert Watson #include <sys/systm.h>
698781d8e9SBruce Evans 
7069c2d429SJeff Roberson #include <vm/uma.h>
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes #include <net/if.h>
7376039bc8SGleb Smirnoff #include <net/if_var.h>
74df8bae1dSRodney W. Grimes #include <net/route.h>
75983066f0SAlexander V. Chernikov #include <net/route/nhop.h>
76b2bdc62aSAdrian Chadd #include <net/rss_config.h>
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes #include <netinet/in.h>
7957f60867SMark Johnston #include <netinet/in_kdtrace.h>
800c325f53SAlexander V. Chernikov #include <netinet/in_fib.h>
81960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
82f5514f08SRobert Watson #include <netinet/in_systm.h>
83960ed29cSSeigo Tanimura #include <netinet/in_var.h>
84df8bae1dSRodney W. Grimes #include <netinet/ip.h>
85cfa1ca9dSYoshinobu Inoue #ifdef INET6
86cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
87cfa1ca9dSYoshinobu Inoue #endif
88960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
89960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
90df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
91ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
92cfa1ca9dSYoshinobu Inoue #ifdef INET6
93cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
94cfa1ca9dSYoshinobu Inoue #endif
95df8bae1dSRodney W. Grimes #include <netinet/udp.h>
96df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
97e06e816fSKevin Lo #include <netinet/udplite.h>
988ad1a83bSAdrian Chadd #include <netinet/in_rss.h>
99df8bae1dSRodney W. Grimes 
100fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
101b9234fafSSam Leffler 
102db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
103db4f9cc7SJonathan Lemon 
104aed55708SRobert Watson #include <security/mac/mac_framework.h>
105aed55708SRobert Watson 
106df8bae1dSRodney W. Grimes /*
107e06e816fSKevin Lo  * UDP and UDP-Lite protocols implementation.
108df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
109e06e816fSKevin Lo  * Per RFC 3828, July, 2004.
110df8bae1dSRodney W. Grimes  */
11174eb3236SWarner Losh 
11274eb3236SWarner Losh /*
1133329b236SRobert Watson  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
1143329b236SRobert Watson  * removes the only data integrity mechanism for packets and malformed
115f5514f08SRobert Watson  * packets that would otherwise be discarded due to bad checksums, and may
116f5514f08SRobert Watson  * cause problems (especially for NFS data blocks).
11774eb3236SWarner Losh  */
11840b676beSBjoern A. Zeeb VNET_DEFINE(int, udp_cksum) = 1;
1196df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW,
12040b676beSBjoern A. Zeeb     &VNET_NAME(udp_cksum), 0, "compute udp checksum");
121df8bae1dSRodney W. Grimes 
122334fc582SBjoern A. Zeeb VNET_DEFINE(int, udp_log_in_vain) = 0;
123334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
124334fc582SBjoern A. Zeeb     &VNET_NAME(udp_log_in_vain), 0, "Log all incoming UDP packets");
125816a3d83SPoul-Henning Kamp 
12682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, udp_blackhole) = 0;
1276df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
128eddfbb76SRobert Watson     &VNET_NAME(udp_blackhole), 0,
1293329b236SRobert Watson     "Do not send port unreachables for refused connects");
1303ea9a7cfSGleb Smirnoff VNET_DEFINE(bool, udp_blackhole_local) = false;
1313ea9a7cfSGleb Smirnoff SYSCTL_BOOL(_net_inet_udp, OID_AUTO, blackhole_local, CTLFLAG_VNET |
1323ea9a7cfSGleb Smirnoff     CTLFLAG_RW, &VNET_NAME(udp_blackhole_local), false,
1333ea9a7cfSGleb Smirnoff     "Enforce net.inet.udp.blackhole for locally originated packets");
13416f7f31fSGeoff Rehmet 
13543bbb6aaSRobert Watson u_long	udp_sendspace = 9216;		/* really max datagram size */
13643bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
13743bbb6aaSRobert Watson     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
13843bbb6aaSRobert Watson 
13943bbb6aaSRobert Watson u_long	udp_recvspace = 40 * (1024 +
14043bbb6aaSRobert Watson #ifdef INET6
14143bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in6)
14243bbb6aaSRobert Watson #else
14343bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in)
14443bbb6aaSRobert Watson #endif
145e62b9bcaSSergey Kandaurov 				      );	/* 40 1K datagrams */
14643bbb6aaSRobert Watson 
14743bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14843bbb6aaSRobert Watson     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
14943bbb6aaSRobert Watson 
150eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, udbinfo);
151e06e816fSKevin Lo VNET_DEFINE(struct inpcbinfo, ulitecbinfo);
15215bd2b43SDavid Greenman 
15315bd2b43SDavid Greenman #ifndef UDBHASHSIZE
154e2ed8f35SAlexander Motin #define	UDBHASHSIZE	128
15515bd2b43SDavid Greenman #endif
15615bd2b43SDavid Greenman 
1575b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat);		/* from udp_var.h */
1585b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(udpstat);
1595b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat,
1605b7cb97cSAndrey V. Elsukov     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
161f2ea20e6SGarrett Wollman 
1625b7cb97cSAndrey V. Elsukov #ifdef VIMAGE
1635b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(udpstat);
1645b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */
16579288c11SBjoern A. Zeeb #ifdef INET
166bc725eafSRobert Watson static void	udp_detach(struct socket *so);
1674d77a549SAlfred Perlstein static int	udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
168a9839c4aSBjoern A. Zeeb 		    struct mbuf *, struct thread *, int);
16979288c11SBjoern A. Zeeb #endif
17079288c11SBjoern A. Zeeb 
171*483fe965SGleb Smirnoff INPCBSTORAGE_DEFINE(udpcbstor, udpcb, "udpinp", "udp_inpcb", "udp", "udphash");
172*483fe965SGleb Smirnoff INPCBSTORAGE_DEFINE(udplitecbstor, udpcb, "udpliteinp", "udplite_inpcb",
1730aa120d5SGleb Smirnoff     "udplite", "udplitehash");
174e06e816fSKevin Lo 
17589128ff3SGleb Smirnoff static void
17678b1fc05SGleb Smirnoff udp_vnet_init(void *arg __unused)
177df8bae1dSRodney W. Grimes {
178af1ee11dSRobert Watson 
1798ad1a83bSAdrian Chadd 	/*
1808ad1a83bSAdrian Chadd 	 * For now default to 2-tuple UDP hashing - until the fragment
1818ad1a83bSAdrian Chadd 	 * reassembly code can also update the flowid.
1828ad1a83bSAdrian Chadd 	 *
1838ad1a83bSAdrian Chadd 	 * Once we can calculate the flowid that way and re-establish
1848ad1a83bSAdrian Chadd 	 * a 4-tuple, flip this to 4-tuple.
1858ad1a83bSAdrian Chadd 	 */
186fec8a8c7SGleb Smirnoff 	in_pcbinfo_init(&V_udbinfo, &udpcbstor, UDBHASHSIZE, UDBHASHSIZE);
18789128ff3SGleb Smirnoff 	/* Additional pcbinfo for UDP-Lite */
188fec8a8c7SGleb Smirnoff 	in_pcbinfo_init(&V_ulitecbinfo, &udplitecbstor, UDBHASHSIZE,
189fec8a8c7SGleb Smirnoff 	    UDBHASHSIZE);
190e06e816fSKevin Lo }
19178b1fc05SGleb Smirnoff VNET_SYSINIT(udp_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
19278b1fc05SGleb Smirnoff     udp_vnet_init, NULL);
193e06e816fSKevin Lo 
194315e3e38SRobert Watson /*
195315e3e38SRobert Watson  * Kernel module interface for updating udpstat.  The argument is an index
196315e3e38SRobert Watson  * into udpstat treated as an array of u_long.  While this encodes the
197315e3e38SRobert Watson  * general layout of udpstat into the caller, it doesn't encode its location,
198315e3e38SRobert Watson  * so that future changes to add, for example, per-CPU stats support won't
199315e3e38SRobert Watson  * cause binary compatibility problems for kernel modules.
200315e3e38SRobert Watson  */
201315e3e38SRobert Watson void
202315e3e38SRobert Watson kmod_udpstat_inc(int statnum)
203315e3e38SRobert Watson {
204315e3e38SRobert Watson 
2055b7cb97cSAndrey V. Elsukov 	counter_u64_add(VNET(udpstat)[statnum], 1);
206315e3e38SRobert Watson }
207315e3e38SRobert Watson 
208bc29160dSMarko Zec #ifdef VIMAGE
2093f58662dSBjoern A. Zeeb static void
2103f58662dSBjoern A. Zeeb udp_destroy(void *unused __unused)
211bc29160dSMarko Zec {
212bc29160dSMarko Zec 
2139bcd427bSRobert Watson 	in_pcbinfo_destroy(&V_udbinfo);
214294a609fSGleb Smirnoff 	in_pcbinfo_destroy(&V_ulitecbinfo);
215bc29160dSMarko Zec }
2163f58662dSBjoern A. Zeeb VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL);
217bc29160dSMarko Zec #endif
218bc29160dSMarko Zec 
21979288c11SBjoern A. Zeeb #ifdef INET
22043bbb6aaSRobert Watson /*
22143bbb6aaSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
22243bbb6aaSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
22343bbb6aaSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
22443bbb6aaSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
22543bbb6aaSRobert Watson  * into the socket code.
226c0d1be08SRandall Stewart  *
227c0d1be08SRandall Stewart  * In the normal case udp_append() will return 0, indicating that you
228c0d1be08SRandall Stewart  * must unlock the inp. However if a tunneling protocol is in place we increment
229c0d1be08SRandall Stewart  * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we
230c0d1be08SRandall Stewart  * then decrement the reference count. If the inp_rele returns 1, indicating the
231c0d1be08SRandall Stewart  * inp is gone, we return that to the caller to tell them *not* to unlock
232c0d1be08SRandall Stewart  * the inp. In the case of multi-cast this will cause the distribution
233c0d1be08SRandall Stewart  * to stop (though most tunneling protocols known currently do *not* use
234c0d1be08SRandall Stewart  * multicast).
23543bbb6aaSRobert Watson  */
236c0d1be08SRandall Stewart static int
23743bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
23843bbb6aaSRobert Watson     struct sockaddr_in *udp_in)
23943bbb6aaSRobert Watson {
24043bbb6aaSRobert Watson 	struct sockaddr *append_sa;
24143bbb6aaSRobert Watson 	struct socket *so;
242dce33a45SErmal Luçi 	struct mbuf *tmpopts, *opts = NULL;
24343bbb6aaSRobert Watson #ifdef INET6
24443bbb6aaSRobert Watson 	struct sockaddr_in6 udp_in6;
24543bbb6aaSRobert Watson #endif
2467b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
247742e7210SKristof Provost 	bool filtered;
24843bbb6aaSRobert Watson 
249fa046d87SRobert Watson 	INP_LOCK_ASSERT(inp);
25043bbb6aaSRobert Watson 
25179bb84fbSEdward Tomasz Napierala 	/*
25279bb84fbSEdward Tomasz Napierala 	 * Engage the tunneling protocol.
25379bb84fbSEdward Tomasz Napierala 	 */
25479bb84fbSEdward Tomasz Napierala 	up = intoudpcb(inp);
25579bb84fbSEdward Tomasz Napierala 	if (up->u_tun_func != NULL) {
256c0d1be08SRandall Stewart 		in_pcbref(inp);
257c0d1be08SRandall Stewart 		INP_RUNLOCK(inp);
258742e7210SKristof Provost 		filtered = (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&udp_in[0],
25981d3ec17SBryan Venteicher 		    up->u_tun_ctx);
260c0d1be08SRandall Stewart 		INP_RLOCK(inp);
261742e7210SKristof Provost 		if (filtered)
262c0d1be08SRandall Stewart 			return (in_pcbrele_rlocked(inp));
26379bb84fbSEdward Tomasz Napierala 	}
26479bb84fbSEdward Tomasz Napierala 
26579bb84fbSEdward Tomasz Napierala 	off += sizeof(struct udphdr);
26679bb84fbSEdward Tomasz Napierala 
267fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
26843bbb6aaSRobert Watson 	/* Check AH/ESP integrity. */
269fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
270fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, n, inp) != 0) {
27143bbb6aaSRobert Watson 		m_freem(n);
272c0d1be08SRandall Stewart 		return (0);
27343bbb6aaSRobert Watson 	}
274fcf59617SAndrey V. Elsukov 	if (up->u_flags & UF_ESPINUDP) {/* IPSec UDP encaps. */
275fcf59617SAndrey V. Elsukov 		if (IPSEC_ENABLED(ipv4) &&
276fcf59617SAndrey V. Elsukov 		    UDPENCAP_INPUT(n, off, AF_INET) != 0)
277fcf59617SAndrey V. Elsukov 			return (0);	/* Consumed. */
2787b495c44SVANHULLEBUS Yvan 	}
27943bbb6aaSRobert Watson #endif /* IPSEC */
28043bbb6aaSRobert Watson #ifdef MAC
28130d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, n) != 0) {
28243bbb6aaSRobert Watson 		m_freem(n);
283c0d1be08SRandall Stewart 		return (0);
28443bbb6aaSRobert Watson 	}
28579288c11SBjoern A. Zeeb #endif /* MAC */
28643bbb6aaSRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
28743bbb6aaSRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
28843bbb6aaSRobert Watson #ifdef INET6
2899a38ba81SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6)
29048d48eb9SBjoern A. Zeeb 			(void)ip6_savecontrol_v4(inp, n, &opts, NULL);
2919a38ba81SBjoern A. Zeeb 		else
29279288c11SBjoern A. Zeeb #endif /* INET6 */
29343bbb6aaSRobert Watson 			ip_savecontrol(inp, &opts, ip, n);
29443bbb6aaSRobert Watson 	}
295dce33a45SErmal Luçi 	if ((inp->inp_vflag & INP_IPV4) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
296b46667c6SGleb Smirnoff 		tmpopts = sbcreatecontrol(&udp_in[1],
297b46667c6SGleb Smirnoff 		    sizeof(struct sockaddr_in), IP_ORIGDSTADDR, IPPROTO_IP,
298b46667c6SGleb Smirnoff 		    M_NOWAIT);
299dce33a45SErmal Luçi 		if (tmpopts) {
300dce33a45SErmal Luçi 			if (opts) {
301dce33a45SErmal Luçi 				tmpopts->m_next = opts;
302dce33a45SErmal Luçi 				opts = tmpopts;
303dce33a45SErmal Luçi 			} else
304dce33a45SErmal Luçi 				opts = tmpopts;
305dce33a45SErmal Luçi 		}
306dce33a45SErmal Luçi 	}
30743bbb6aaSRobert Watson #ifdef INET6
30843bbb6aaSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
30943bbb6aaSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
31043bbb6aaSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
31143bbb6aaSRobert Watson 		udp_in6.sin6_family = AF_INET6;
312dce33a45SErmal Luçi 		in6_sin_2_v4mapsin6(&udp_in[0], &udp_in6);
31343bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
31443bbb6aaSRobert Watson 	} else
31579288c11SBjoern A. Zeeb #endif /* INET6 */
316dce33a45SErmal Luçi 		append_sa = (struct sockaddr *)&udp_in[0];
31743bbb6aaSRobert Watson 	m_adj(n, off);
31843bbb6aaSRobert Watson 
31943bbb6aaSRobert Watson 	so = inp->inp_socket;
32043bbb6aaSRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
32143bbb6aaSRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
322a61c24ddSKonstantin Kukushkin 		soroverflow_locked(so);
32343bbb6aaSRobert Watson 		m_freem(n);
32443bbb6aaSRobert Watson 		if (opts)
32543bbb6aaSRobert Watson 			m_freem(opts);
326026decb8SRobert Watson 		UDPSTAT_INC(udps_fullsock);
32743bbb6aaSRobert Watson 	} else
32843bbb6aaSRobert Watson 		sorwakeup_locked(so);
329c0d1be08SRandall Stewart 	return (0);
33043bbb6aaSRobert Watson }
33143bbb6aaSRobert Watson 
332db0ac6deSCy Schubert static bool
333db0ac6deSCy Schubert udp_multi_match(const struct inpcb *inp, void *v)
334db0ac6deSCy Schubert {
335db0ac6deSCy Schubert 	struct ip *ip = v;
336db0ac6deSCy Schubert 	struct udphdr *uh = (struct udphdr *)(ip + 1);
337db0ac6deSCy Schubert 
338db0ac6deSCy Schubert 	if (inp->inp_lport != uh->uh_dport)
339db0ac6deSCy Schubert 		return (false);
340db0ac6deSCy Schubert #ifdef INET6
341db0ac6deSCy Schubert 	if ((inp->inp_vflag & INP_IPV4) == 0)
342db0ac6deSCy Schubert 		return (false);
343db0ac6deSCy Schubert #endif
344db0ac6deSCy Schubert 	if (inp->inp_laddr.s_addr != INADDR_ANY &&
345db0ac6deSCy Schubert 	    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
346db0ac6deSCy Schubert 		return (false);
347db0ac6deSCy Schubert 	if (inp->inp_faddr.s_addr != INADDR_ANY &&
348db0ac6deSCy Schubert 	    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
349db0ac6deSCy Schubert 		return (false);
350db0ac6deSCy Schubert 	if (inp->inp_fport != 0 &&
351db0ac6deSCy Schubert 	    inp->inp_fport != uh->uh_sport)
352db0ac6deSCy Schubert 		return (false);
353db0ac6deSCy Schubert 
354db0ac6deSCy Schubert 	return (true);
355db0ac6deSCy Schubert }
356db0ac6deSCy Schubert 
357db0ac6deSCy Schubert static int
358db0ac6deSCy Schubert udp_multi_input(struct mbuf *m, int proto, struct sockaddr_in *udp_in)
359db0ac6deSCy Schubert {
360db0ac6deSCy Schubert 	struct ip *ip = mtod(m, struct ip *);
361db0ac6deSCy Schubert 	struct inpcb_iterator inpi = INP_ITERATOR(udp_get_inpcbinfo(proto),
362db0ac6deSCy Schubert 	    INPLOOKUP_RLOCKPCB, udp_multi_match, ip);
363bd1d0850SGleb Smirnoff #ifdef KDTRACE_HOOKS
364db0ac6deSCy Schubert 	struct udphdr *uh = (struct udphdr *)(ip + 1);
365bd1d0850SGleb Smirnoff #endif
366db0ac6deSCy Schubert 	struct inpcb *inp;
367db0ac6deSCy Schubert 	struct mbuf *n;
368db0ac6deSCy Schubert 	int appends = 0;
369db0ac6deSCy Schubert 
370db0ac6deSCy Schubert 	MPASS(ip->ip_hl == sizeof(struct ip) >> 2);
371db0ac6deSCy Schubert 
372db0ac6deSCy Schubert 	while ((inp = inp_next(&inpi)) != NULL) {
373db0ac6deSCy Schubert 		/*
374db0ac6deSCy Schubert 		 * XXXRW: Because we weren't holding either the inpcb
375db0ac6deSCy Schubert 		 * or the hash lock when we checked for a match
376db0ac6deSCy Schubert 		 * before, we should probably recheck now that the
377db0ac6deSCy Schubert 		 * inpcb lock is held.
378db0ac6deSCy Schubert 		 */
379db0ac6deSCy Schubert 		/*
380db0ac6deSCy Schubert 		 * Handle socket delivery policy for any-source
381db0ac6deSCy Schubert 		 * and source-specific multicast. [RFC3678]
382db0ac6deSCy Schubert 		 */
383db0ac6deSCy Schubert 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
384db0ac6deSCy Schubert 			struct ip_moptions	*imo;
385db0ac6deSCy Schubert 			struct sockaddr_in	 group;
386db0ac6deSCy Schubert 			int			 blocked;
387db0ac6deSCy Schubert 
388db0ac6deSCy Schubert 			imo = inp->inp_moptions;
389db0ac6deSCy Schubert 			if (imo == NULL)
390db0ac6deSCy Schubert 				continue;
391db0ac6deSCy Schubert 			bzero(&group, sizeof(struct sockaddr_in));
392db0ac6deSCy Schubert 			group.sin_len = sizeof(struct sockaddr_in);
393db0ac6deSCy Schubert 			group.sin_family = AF_INET;
394db0ac6deSCy Schubert 			group.sin_addr = ip->ip_dst;
395db0ac6deSCy Schubert 
396db0ac6deSCy Schubert 			blocked = imo_multi_filter(imo, m->m_pkthdr.rcvif,
397db0ac6deSCy Schubert 				(struct sockaddr *)&group,
398db0ac6deSCy Schubert 				(struct sockaddr *)&udp_in[0]);
399db0ac6deSCy Schubert 			if (blocked != MCAST_PASS) {
400db0ac6deSCy Schubert 				if (blocked == MCAST_NOTGMEMBER)
401db0ac6deSCy Schubert 					IPSTAT_INC(ips_notmember);
402db0ac6deSCy Schubert 				if (blocked == MCAST_NOTSMEMBER ||
403db0ac6deSCy Schubert 				    blocked == MCAST_MUTED)
404db0ac6deSCy Schubert 					UDPSTAT_INC(udps_filtermcast);
405db0ac6deSCy Schubert 				continue;
406db0ac6deSCy Schubert 			}
407db0ac6deSCy Schubert 		}
408db0ac6deSCy Schubert 		if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) {
409db0ac6deSCy Schubert 			if (proto == IPPROTO_UDPLITE)
410db0ac6deSCy Schubert 				UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
411db0ac6deSCy Schubert 			else
412db0ac6deSCy Schubert 				UDP_PROBE(receive, NULL, inp, ip, inp, uh);
413db0ac6deSCy Schubert 			if (udp_append(inp, ip, n, sizeof(struct ip), udp_in)) {
414db0ac6deSCy Schubert 				break;
415db0ac6deSCy Schubert 			} else
416db0ac6deSCy Schubert 				appends++;
417db0ac6deSCy Schubert 		}
418db0ac6deSCy Schubert 		/*
419db0ac6deSCy Schubert 		 * Don't look for additional matches if this one does
420db0ac6deSCy Schubert 		 * not have either the SO_REUSEPORT or SO_REUSEADDR
421db0ac6deSCy Schubert 		 * socket options set.  This heuristic avoids
422db0ac6deSCy Schubert 		 * searching through all pcbs in the common case of a
423db0ac6deSCy Schubert 		 * non-shared port.  It assumes that an application
424db0ac6deSCy Schubert 		 * will never clear these options after setting them.
425db0ac6deSCy Schubert 		 */
426db0ac6deSCy Schubert 		if ((inp->inp_socket->so_options &
427db0ac6deSCy Schubert 		    (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) {
428db0ac6deSCy Schubert 			INP_RUNLOCK(inp);
429db0ac6deSCy Schubert 			break;
430db0ac6deSCy Schubert 		}
431db0ac6deSCy Schubert 	}
432db0ac6deSCy Schubert 
433db0ac6deSCy Schubert 	if (appends == 0) {
434db0ac6deSCy Schubert 		/*
435db0ac6deSCy Schubert 		 * No matching pcb found; discard datagram.  (No need
436db0ac6deSCy Schubert 		 * to send an ICMP Port Unreachable for a broadcast
437db0ac6deSCy Schubert 		 * or multicast datgram.)
438db0ac6deSCy Schubert 		 */
439db0ac6deSCy Schubert 		UDPSTAT_INC(udps_noport);
440db0ac6deSCy Schubert 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)))
441db0ac6deSCy Schubert 			UDPSTAT_INC(udps_noportmcast);
442db0ac6deSCy Schubert 		else
443db0ac6deSCy Schubert 			UDPSTAT_INC(udps_noportbcast);
444db0ac6deSCy Schubert 	}
445014f98b1SMark Johnston 	m_freem(m);
446db0ac6deSCy Schubert 
447db0ac6deSCy Schubert 	return (IPPROTO_DONE);
448db0ac6deSCy Schubert }
449db0ac6deSCy Schubert 
45078b1fc05SGleb Smirnoff static int
4518f5a8818SKevin Lo udp_input(struct mbuf **mp, int *offp, int proto)
452df8bae1dSRodney W. Grimes {
4533329b236SRobert Watson 	struct ip *ip;
4543329b236SRobert Watson 	struct udphdr *uh;
45571498f30SBruce M Simpson 	struct ifnet *ifp;
4563329b236SRobert Watson 	struct inpcb *inp;
4578f134647SGleb Smirnoff 	uint16_t len, ip_len;
458e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
459dce33a45SErmal Luçi 	struct sockaddr_in udp_in[2];
4608f5a8818SKevin Lo 	struct mbuf *m;
4610b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
4628f5a8818SKevin Lo 	int cscov_partial, iphlen;
463df8bae1dSRodney W. Grimes 
4648f5a8818SKevin Lo 	m = *mp;
4658f5a8818SKevin Lo 	iphlen = *offp;
46671498f30SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
4678f5a8818SKevin Lo 	*mp = NULL;
468026decb8SRobert Watson 	UDPSTAT_INC(udps_ipackets);
469df8bae1dSRodney W. Grimes 
470df8bae1dSRodney W. Grimes 	/*
4713329b236SRobert Watson 	 * Strip IP options, if any; should skip this, make available to
4723329b236SRobert Watson 	 * user, and use on returned packets, but we don't yet have a way to
4733329b236SRobert Watson 	 * check the checksum with options still present.
474df8bae1dSRodney W. Grimes 	 */
475df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
476105bd211SGleb Smirnoff 		ip_stripoptions(m);
477df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
478df8bae1dSRodney W. Grimes 	}
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes 	/*
481df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
482df8bae1dSRodney W. Grimes 	 */
483df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
484d1b18731SKevin Lo 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) {
485026decb8SRobert Watson 			UDPSTAT_INC(udps_hdrops);
4868f5a8818SKevin Lo 			return (IPPROTO_DONE);
487df8bae1dSRodney W. Grimes 		}
488df8bae1dSRodney W. Grimes 	}
489503f4e47SBjoern A. Zeeb 	ip = mtod(m, struct ip *);
490df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
4918f5a8818SKevin Lo 	cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0;
492df8bae1dSRodney W. Grimes 
4933329b236SRobert Watson 	/*
4943329b236SRobert Watson 	 * Destination port of 0 is illegal, based on RFC768.
4953329b236SRobert Watson 	 */
496686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
497f76fcf6dSJeffrey Hsu 		goto badunlocked;
498686cdd19SJun-ichiro itojun Hagino 
499df8bae1dSRodney W. Grimes 	/*
5003329b236SRobert Watson 	 * Construct sockaddr format source address.  Stuff source address
5013329b236SRobert Watson 	 * and datagram in user buffer.
502b9234fafSSam Leffler 	 */
503dce33a45SErmal Luçi 	bzero(&udp_in[0], sizeof(struct sockaddr_in) * 2);
504dce33a45SErmal Luçi 	udp_in[0].sin_len = sizeof(struct sockaddr_in);
505dce33a45SErmal Luçi 	udp_in[0].sin_family = AF_INET;
506dce33a45SErmal Luçi 	udp_in[0].sin_port = uh->uh_sport;
507dce33a45SErmal Luçi 	udp_in[0].sin_addr = ip->ip_src;
508dce33a45SErmal Luçi 	udp_in[1].sin_len = sizeof(struct sockaddr_in);
509dce33a45SErmal Luçi 	udp_in[1].sin_family = AF_INET;
510dce33a45SErmal Luçi 	udp_in[1].sin_port = uh->uh_dport;
511dce33a45SErmal Luçi 	udp_in[1].sin_addr = ip->ip_dst;
512b9234fafSSam Leffler 
513b9234fafSSam Leffler 	/*
514af1ee11dSRobert Watson 	 * Make mbuf data length reflect UDP length.  If not enough data to
515af1ee11dSRobert Watson 	 * reflect UDP length, drop.
516df8bae1dSRodney W. Grimes 	 */
517df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
5188ad458a4SGleb Smirnoff 	ip_len = ntohs(ip->ip_len) - iphlen;
5190f4a0366SMichael Tuexen 	if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) {
520e06e816fSKevin Lo 		/* Zero means checksum over the complete packet. */
5210f4a0366SMichael Tuexen 		if (len == 0)
522e06e816fSKevin Lo 			len = ip_len;
523e06e816fSKevin Lo 		cscov_partial = 0;
524e06e816fSKevin Lo 	}
5258f134647SGleb Smirnoff 	if (ip_len != len) {
5268f134647SGleb Smirnoff 		if (len > ip_len || len < sizeof(struct udphdr)) {
527026decb8SRobert Watson 			UDPSTAT_INC(udps_badlen);
528f76fcf6dSJeffrey Hsu 			goto badunlocked;
529df8bae1dSRodney W. Grimes 		}
5308f5a8818SKevin Lo 		if (proto == IPPROTO_UDP)
5318f134647SGleb Smirnoff 			m_adj(m, len - ip_len);
532df8bae1dSRodney W. Grimes 	}
5333329b236SRobert Watson 
534df8bae1dSRodney W. Grimes 	/*
535df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
536df8bae1dSRodney W. Grimes 	 */
5376dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
53839629c92SDavid Malone 		u_short uh_sum;
53939629c92SDavid Malone 
540e06e816fSKevin Lo 		if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
541e06e816fSKevin Lo 		    !cscov_partial) {
542db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
54339629c92SDavid Malone 				uh_sum = m->m_pkthdr.csum_data;
544db4f9cc7SJonathan Lemon 			else
54539629c92SDavid Malone 				uh_sum = in_pseudo(ip->ip_src.s_addr,
546506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
5478f5a8818SKevin Lo 				    m->m_pkthdr.csum_data + proto));
54839629c92SDavid Malone 			uh_sum ^= 0xffff;
549db4f9cc7SJonathan Lemon 		} else {
5503358df29SGleb Smirnoff 			char b[offsetof(struct ipovly, ih_src)];
5513358df29SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
552af1ee11dSRobert Watson 
5533358df29SGleb Smirnoff 			bcopy(ipov, b, sizeof(b));
5543358df29SGleb Smirnoff 			bzero(ipov, sizeof(ipov->ih_x1));
5553358df29SGleb Smirnoff 			ipov->ih_len = (proto == IPPROTO_UDP) ?
556e06e816fSKevin Lo 			    uh->uh_ulen : htons(ip_len);
55739629c92SDavid Malone 			uh_sum = in_cksum(m, len + sizeof (struct ip));
5583358df29SGleb Smirnoff 			bcopy(b, ipov, sizeof(b));
559db4f9cc7SJonathan Lemon 		}
56039629c92SDavid Malone 		if (uh_sum) {
561026decb8SRobert Watson 			UDPSTAT_INC(udps_badsum);
562df8bae1dSRodney W. Grimes 			m_freem(m);
5638f5a8818SKevin Lo 			return (IPPROTO_DONE);
564df8bae1dSRodney W. Grimes 		}
565c6d81a34SMichael Tuexen 	} else {
566c6d81a34SMichael Tuexen 		if (proto == IPPROTO_UDP) {
567026decb8SRobert Watson 			UDPSTAT_INC(udps_nosum);
568c6d81a34SMichael Tuexen 		} else {
569c6d81a34SMichael Tuexen 			/* UDPLite requires a checksum */
570c6d81a34SMichael Tuexen 			/* XXX: What is the right UDPLite MIB counter here? */
571c6d81a34SMichael Tuexen 			m_freem(m);
572c6d81a34SMichael Tuexen 			return (IPPROTO_DONE);
573c6d81a34SMichael Tuexen 		}
574c6d81a34SMichael Tuexen 	}
575df8bae1dSRodney W. Grimes 
576266f97b5SCy Schubert 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
577db0ac6deSCy Schubert 	    in_broadcast(ip->ip_dst, ifp))
578db0ac6deSCy Schubert 		return (udp_multi_input(m, proto, udp_in));
579266f97b5SCy Schubert 
580db0ac6deSCy Schubert 	pcbinfo = udp_get_inpcbinfo(proto);
5813329b236SRobert Watson 
582df8bae1dSRodney W. Grimes 	/*
5836d6a026bSDavid Greenman 	 * Locate pcb for datagram.
584db0ac6deSCy Schubert 	 *
5858a006adbSBjoern A. Zeeb 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
5868a006adbSBjoern A. Zeeb 	 */
587ffdbf9daSAndrey V. Elsukov 	if ((m->m_flags & M_IP_NEXTHOP) &&
588c1de64a4SAndrey V. Elsukov 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
5898a006adbSBjoern A. Zeeb 		struct sockaddr_in *next_hop;
5908a006adbSBjoern A. Zeeb 
5918a006adbSBjoern A. Zeeb 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
5928a006adbSBjoern A. Zeeb 
5938a006adbSBjoern A. Zeeb 		/*
5948a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
5958a006adbSBjoern A. Zeeb 		 * Already got one like this?
5968a006adbSBjoern A. Zeeb 		 */
597e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
5988a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m);
5998a006adbSBjoern A. Zeeb 		if (!inp) {
6008a006adbSBjoern A. Zeeb 			/*
6018a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
6028a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
6038a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
6048a006adbSBjoern A. Zeeb 			 */
605e06e816fSKevin Lo 			inp = in_pcblookup(pcbinfo, ip->ip_src,
6068a006adbSBjoern A. Zeeb 			    uh->uh_sport, next_hop->sin_addr,
6078a006adbSBjoern A. Zeeb 			    next_hop->sin_port ? htons(next_hop->sin_port) :
6088a006adbSBjoern A. Zeeb 			    uh->uh_dport, INPLOOKUP_WILDCARD |
6098a006adbSBjoern A. Zeeb 			    INPLOOKUP_RLOCKPCB, ifp);
6108a006adbSBjoern A. Zeeb 		}
6118a006adbSBjoern A. Zeeb 		/* Remove the tag from the packet. We don't need it anymore. */
6128a006adbSBjoern A. Zeeb 		m_tag_delete(m, fwd_tag);
613ffdbf9daSAndrey V. Elsukov 		m->m_flags &= ~M_IP_NEXTHOP;
6148a006adbSBjoern A. Zeeb 	} else
615e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
6168a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
6178a006adbSBjoern A. Zeeb 		    INPLOOKUP_RLOCKPCB, ifp, m);
61815bd2b43SDavid Greenman 	if (inp == NULL) {
619334fc582SBjoern A. Zeeb 		if (V_udp_log_in_vain) {
620edf0313bSEric van Gyzen 			char src[INET_ADDRSTRLEN];
621edf0313bSEric van Gyzen 			char dst[INET_ADDRSTRLEN];
62275cfc95fSAndrey A. Chernov 
623592071e8SBruce Evans 			log(LOG_INFO,
624592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
625edf0313bSEric van Gyzen 			    inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport),
626edf0313bSEric van Gyzen 			    inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport));
62775cfc95fSAndrey A. Chernov 		}
6287bda9663SMichael Tuexen 		if (proto == IPPROTO_UDPLITE)
6297bda9663SMichael Tuexen 			UDPLITE_PROBE(receive, NULL, NULL, ip, NULL, uh);
6307bda9663SMichael Tuexen 		else
631e1526d5aSMichael Tuexen 			UDP_PROBE(receive, NULL, NULL, ip, NULL, uh);
632026decb8SRobert Watson 		UDPSTAT_INC(udps_noport);
633df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
634026decb8SRobert Watson 			UDPSTAT_INC(udps_noportbcast);
635fa046d87SRobert Watson 			goto badunlocked;
636df8bae1dSRodney W. Grimes 		}
6373ea9a7cfSGleb Smirnoff 		if (V_udp_blackhole && (V_udp_blackhole_local ||
6383ea9a7cfSGleb Smirnoff 		    !in_localip(ip->ip_src)))
639fa046d87SRobert Watson 			goto badunlocked;
6401cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
641fa046d87SRobert Watson 			goto badunlocked;
642582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
6438f5a8818SKevin Lo 		return (IPPROTO_DONE);
644df8bae1dSRodney W. Grimes 	}
6453329b236SRobert Watson 
6463329b236SRobert Watson 	/*
6473329b236SRobert Watson 	 * Check the minimum TTL for socket.
6483329b236SRobert Watson 	 */
649fa046d87SRobert Watson 	INP_RLOCK_ASSERT(inp);
65010cc62b7SRobert Watson 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
6517bda9663SMichael Tuexen 		if (proto == IPPROTO_UDPLITE)
6527bda9663SMichael Tuexen 			UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
6537bda9663SMichael Tuexen 		else
654e1526d5aSMichael Tuexen 			UDP_PROBE(receive, NULL, inp, ip, inp, uh);
65510cc62b7SRobert Watson 		INP_RUNLOCK(inp);
656fa046d87SRobert Watson 		m_freem(m);
6578f5a8818SKevin Lo 		return (IPPROTO_DONE);
65810cc62b7SRobert Watson 	}
659e06e816fSKevin Lo 	if (cscov_partial) {
660e06e816fSKevin Lo 		struct udpcb *up;
661e06e816fSKevin Lo 
662e06e816fSKevin Lo 		up = intoudpcb(inp);
66383e95fb3SMichael Tuexen 		if (up->u_rxcslen == 0 || up->u_rxcslen > len) {
664e06e816fSKevin Lo 			INP_RUNLOCK(inp);
665e06e816fSKevin Lo 			m_freem(m);
6668f5a8818SKevin Lo 			return (IPPROTO_DONE);
667e06e816fSKevin Lo 		}
668e06e816fSKevin Lo 	}
66957f60867SMark Johnston 
6707bda9663SMichael Tuexen 	if (proto == IPPROTO_UDPLITE)
6717bda9663SMichael Tuexen 		UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
6727bda9663SMichael Tuexen 	else
6731ad19fb6SMark Johnston 		UDP_PROBE(receive, NULL, inp, ip, inp, uh);
674dce33a45SErmal Luçi 	if (udp_append(inp, ip, m, iphlen, udp_in) == 0)
675119d85f6SRobert Watson 		INP_RUNLOCK(inp);
6768f5a8818SKevin Lo 	return (IPPROTO_DONE);
67761ffc0b1SJeffrey Hsu 
678f76fcf6dSJeffrey Hsu badunlocked:
679df8bae1dSRodney W. Grimes 	m_freem(m);
6808f5a8818SKevin Lo 	return (IPPROTO_DONE);
681cfa1ca9dSYoshinobu Inoue }
68279288c11SBjoern A. Zeeb #endif /* INET */
683cfa1ca9dSYoshinobu Inoue 
684cfa1ca9dSYoshinobu Inoue /*
6853329b236SRobert Watson  * Notify a udp user of an asynchronous error; just wake up so that they can
6863329b236SRobert Watson  * collect error status.
687df8bae1dSRodney W. Grimes  */
6883ce144eaSJeffrey Hsu struct inpcb *
6893329b236SRobert Watson udp_notify(struct inpcb *inp, int errno)
690df8bae1dSRodney W. Grimes {
6913329b236SRobert Watson 
692083a010cSRyan Stone 	INP_WLOCK_ASSERT(inp);
69384cc0778SGeorge V. Neville-Neil 	if ((errno == EHOSTUNREACH || errno == ENETUNREACH ||
694983066f0SAlexander V. Chernikov 	     errno == EHOSTDOWN) && inp->inp_route.ro_nh) {
695983066f0SAlexander V. Chernikov 		NH_FREE(inp->inp_route.ro_nh);
696983066f0SAlexander V. Chernikov 		inp->inp_route.ro_nh = (struct nhop_object *)NULL;
69784cc0778SGeorge V. Neville-Neil 	}
6988501a69cSRobert Watson 
699df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
700df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
701df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
7023329b236SRobert Watson 	return (inp);
703df8bae1dSRodney W. Grimes }
704df8bae1dSRodney W. Grimes 
70579288c11SBjoern A. Zeeb #ifdef INET
706e06e816fSKevin Lo static void
707fcb3f813SGleb Smirnoff udp_common_ctlinput(struct icmp *icmp, struct inpcbinfo *pcbinfo)
708df8bae1dSRodney W. Grimes {
709fcb3f813SGleb Smirnoff 	struct ip *ip = &icmp->icmp_ip;
710c693a045SJonathan Lemon 	struct udphdr *uh;
711c693a045SJonathan Lemon 	struct inpcb *inp;
712c693a045SJonathan Lemon 
713fcb3f813SGleb Smirnoff 	if (icmp_errmap(icmp) == 0)
71497d8d152SAndre Oppermann 		return;
7153329b236SRobert Watson 
716df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
717fcb3f813SGleb Smirnoff 	inp = in_pcblookup(pcbinfo, ip->ip_dst, uh->uh_dport, ip->ip_src,
718c0fc81e9SGleb Smirnoff 	    uh->uh_sport, INPLOOKUP_WLOCKPCB, NULL);
719f76fcf6dSJeffrey Hsu 	if (inp != NULL) {
720083a010cSRyan Stone 		INP_WLOCK_ASSERT(inp);
721c0fc81e9SGleb Smirnoff 		if (inp->inp_socket != NULL)
722fcb3f813SGleb Smirnoff 			udp_notify(inp, icmp_errmap(icmp));
723083a010cSRyan Stone 		INP_WUNLOCK(inp);
724abb901c5SRandall Stewart 	} else {
725fcb3f813SGleb Smirnoff 		inp = in_pcblookup(pcbinfo, ip->ip_dst, uh->uh_dport,
726abb901c5SRandall Stewart 		    ip->ip_src, uh->uh_sport,
727abb901c5SRandall Stewart 		    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
728abb901c5SRandall Stewart 		if (inp != NULL) {
729abb901c5SRandall Stewart 			struct udpcb *up;
730bb77f0c2SGleb Smirnoff 			udp_tun_icmp_t *func;
731abb901c5SRandall Stewart 
732abb901c5SRandall Stewart 			up = intoudpcb(inp);
73346374cbfSMatt Macy 			func = up->u_icmp_func;
734abb901c5SRandall Stewart 			INP_RUNLOCK(inp);
73546374cbfSMatt Macy 			if (func != NULL)
736fcb3f813SGleb Smirnoff 				func(icmp);
737abb901c5SRandall Stewart 		}
738f76fcf6dSJeffrey Hsu 	}
739df8bae1dSRodney W. Grimes }
74078b1fc05SGleb Smirnoff 
74178b1fc05SGleb Smirnoff static void
742fcb3f813SGleb Smirnoff udp_ctlinput(struct icmp *icmp)
743e06e816fSKevin Lo {
744e06e816fSKevin Lo 
745fcb3f813SGleb Smirnoff 	return (udp_common_ctlinput(icmp, &V_udbinfo));
746e06e816fSKevin Lo }
747e06e816fSKevin Lo 
74878b1fc05SGleb Smirnoff static void
749fcb3f813SGleb Smirnoff udplite_ctlinput(struct icmp *icmp)
750e06e816fSKevin Lo {
751e06e816fSKevin Lo 
752fcb3f813SGleb Smirnoff 	return (udp_common_ctlinput(icmp, &V_ulitecbinfo));
753e06e816fSKevin Lo }
75479288c11SBjoern A. Zeeb #endif /* INET */
755df8bae1dSRodney W. Grimes 
7560312fbe9SPoul-Henning Kamp static int
75782d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
75898271db4SGarrett Wollman {
759db0ac6deSCy Schubert 	struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_udbinfo,
760db0ac6deSCy Schubert 	    INPLOOKUP_RLOCKPCB);
76198271db4SGarrett Wollman 	struct xinpgen xig;
762032677ceSGleb Smirnoff 	struct inpcb *inp;
763032677ceSGleb Smirnoff 	int error;
76498271db4SGarrett Wollman 
765032677ceSGleb Smirnoff 	if (req->newptr != 0)
766032677ceSGleb Smirnoff 		return (EPERM);
767032677ceSGleb Smirnoff 
76898271db4SGarrett Wollman 	if (req->oldptr == 0) {
769032677ceSGleb Smirnoff 		int n;
770032677ceSGleb Smirnoff 
771603724d3SBjoern A. Zeeb 		n = V_udbinfo.ipi_count;
772c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
773c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
7743329b236SRobert Watson 		return (0);
77598271db4SGarrett Wollman 	}
77698271db4SGarrett Wollman 
777032677ceSGleb Smirnoff 	if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
77847934cefSDon Lewis 		return (error);
7795c38b6dbSDon Lewis 
78079db6fe7SMark Johnston 	bzero(&xig, sizeof(xig));
78198271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
782032677ceSGleb Smirnoff 	xig.xig_count = V_udbinfo.ipi_count;
783032677ceSGleb Smirnoff 	xig.xig_gen = V_udbinfo.ipi_gencnt;
78498271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
78598271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
78698271db4SGarrett Wollman 	if (error)
7873329b236SRobert Watson 		return (error);
78899208b82SMatt Macy 
789db0ac6deSCy Schubert 	while ((inp = inp_next(&inpi)) != NULL) {
790032677ceSGleb Smirnoff 		if (inp->inp_gencnt <= xig.xig_gen &&
791032677ceSGleb Smirnoff 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
79298271db4SGarrett Wollman 			struct xinpcb xi;
793d0e157f6SBjoern A. Zeeb 
794cc65eb4eSGleb Smirnoff 			in_pcbtoxinpcb(inp, &xi);
795266f97b5SCy Schubert 			error = SYSCTL_OUT(req, &xi, sizeof xi);
796db0ac6deSCy Schubert 			if (error) {
797266f97b5SCy Schubert 				INP_RUNLOCK(inp);
798db0ac6deSCy Schubert 				break;
79998271db4SGarrett Wollman 			}
800db0ac6deSCy Schubert 		}
801db0ac6deSCy Schubert 	}
802d0e157f6SBjoern A. Zeeb 
80398271db4SGarrett Wollman 	if (!error) {
80498271db4SGarrett Wollman 		/*
8053329b236SRobert Watson 		 * Give the user an updated idea of our state.  If the
8063329b236SRobert Watson 		 * generation differs from what we told her before, she knows
8073329b236SRobert Watson 		 * that something happened while we were processing this
8083329b236SRobert Watson 		 * request, and it might be necessary to retry.
80998271db4SGarrett Wollman 		 */
810603724d3SBjoern A. Zeeb 		xig.xig_gen = V_udbinfo.ipi_gencnt;
81198271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
812603724d3SBjoern A. Zeeb 		xig.xig_count = V_udbinfo.ipi_count;
81398271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
81498271db4SGarrett Wollman 	}
815032677ceSGleb Smirnoff 
8163329b236SRobert Watson 	return (error);
81798271db4SGarrett Wollman }
81898271db4SGarrett Wollman 
81979c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
8207029da5cSPawel Biernacki     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
8217029da5cSPawel Biernacki     udp_pcblist, "S,xinpcb",
8227029da5cSPawel Biernacki     "List of active UDP sockets");
82398271db4SGarrett Wollman 
82479288c11SBjoern A. Zeeb #ifdef INET
82598271db4SGarrett Wollman static int
82682d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
827490d50b6SBrian Feldman {
828c0511d3bSBrian Feldman 	struct xucred xuc;
829490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
830d797164aSGleb Smirnoff 	struct epoch_tracker et;
831490d50b6SBrian Feldman 	struct inpcb *inp;
832277afaffSRobert Watson 	int error;
833490d50b6SBrian Feldman 
83432f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
835490d50b6SBrian Feldman 	if (error)
836490d50b6SBrian Feldman 		return (error);
837490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
838490d50b6SBrian Feldman 	if (error)
839490d50b6SBrian Feldman 		return (error);
840d797164aSGleb Smirnoff 	NET_EPOCH_ENTER(et);
841fa046d87SRobert Watson 	inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
842fa046d87SRobert Watson 	    addrs[0].sin_addr, addrs[0].sin_port,
843fa046d87SRobert Watson 	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
844d797164aSGleb Smirnoff 	NET_EPOCH_EXIT(et);
8459622e84fSRobert Watson 	if (inp != NULL) {
846fa046d87SRobert Watson 		INP_RLOCK_ASSERT(inp);
8479622e84fSRobert Watson 		if (inp->inp_socket == NULL)
8489622e84fSRobert Watson 			error = ENOENT;
8499622e84fSRobert Watson 		if (error == 0)
850f08ef6c5SBjoern A. Zeeb 			error = cr_canseeinpcb(req->td->td_ucred, inp);
8519622e84fSRobert Watson 		if (error == 0)
85286d02c5cSBjoern A. Zeeb 			cru2x(inp->inp_cred, &xuc);
8539622e84fSRobert Watson 		INP_RUNLOCK(inp);
854fa046d87SRobert Watson 	} else
8559622e84fSRobert Watson 		error = ENOENT;
8560e1eebb8SDon Lewis 	if (error == 0)
8570e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
858490d50b6SBrian Feldman 	return (error);
859490d50b6SBrian Feldman }
860490d50b6SBrian Feldman 
8617ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
8627029da5cSPawel Biernacki     CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
8637029da5cSPawel Biernacki     0, 0, udp_getcred, "S,xucred",
8647029da5cSPawel Biernacki     "Get the xucred of a UDP connection");
86579288c11SBjoern A. Zeeb #endif /* INET */
866490d50b6SBrian Feldman 
8677b495c44SVANHULLEBUS Yvan int
8687b495c44SVANHULLEBUS Yvan udp_ctloutput(struct socket *so, struct sockopt *sopt)
8697b495c44SVANHULLEBUS Yvan {
8707b495c44SVANHULLEBUS Yvan 	struct inpcb *inp;
8717b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
872e06e816fSKevin Lo 	int isudplite, error, optval;
8737b495c44SVANHULLEBUS Yvan 
874e06e816fSKevin Lo 	error = 0;
875e06e816fSKevin Lo 	isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0;
8767b495c44SVANHULLEBUS Yvan 	inp = sotoinpcb(so);
8777b495c44SVANHULLEBUS Yvan 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
8787b495c44SVANHULLEBUS Yvan 	INP_WLOCK(inp);
879e06e816fSKevin Lo 	if (sopt->sopt_level != so->so_proto->pr_protocol) {
8807b495c44SVANHULLEBUS Yvan #ifdef INET6
8817b495c44SVANHULLEBUS Yvan 		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
8827b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
8837b495c44SVANHULLEBUS Yvan 			error = ip6_ctloutput(so, sopt);
88479288c11SBjoern A. Zeeb 		}
8857b495c44SVANHULLEBUS Yvan #endif
88679288c11SBjoern A. Zeeb #if defined(INET) && defined(INET6)
88779288c11SBjoern A. Zeeb 		else
88879288c11SBjoern A. Zeeb #endif
88979288c11SBjoern A. Zeeb #ifdef INET
89079288c11SBjoern A. Zeeb 		{
8917b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
8927b495c44SVANHULLEBUS Yvan 			error = ip_ctloutput(so, sopt);
8937b495c44SVANHULLEBUS Yvan 		}
8947b495c44SVANHULLEBUS Yvan #endif
8957b495c44SVANHULLEBUS Yvan 		return (error);
8967b495c44SVANHULLEBUS Yvan 	}
8977b495c44SVANHULLEBUS Yvan 
8987b495c44SVANHULLEBUS Yvan 	switch (sopt->sopt_dir) {
8997b495c44SVANHULLEBUS Yvan 	case SOPT_SET:
9007b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
901fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
902fcf59617SAndrey V. Elsukov #ifdef INET
9037b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
904fcf59617SAndrey V. Elsukov 			if (!IPSEC_ENABLED(ipv4)) {
9057b495c44SVANHULLEBUS Yvan 				INP_WUNLOCK(inp);
906fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
9077b495c44SVANHULLEBUS Yvan 			}
908fcf59617SAndrey V. Elsukov 			error = UDPENCAP_PCBCTL(inp, sopt);
9097b495c44SVANHULLEBUS Yvan 			break;
910fcf59617SAndrey V. Elsukov #endif /* INET */
911fcf59617SAndrey V. Elsukov #endif /* IPSEC */
912e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
913e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
914e06e816fSKevin Lo 			if (!isudplite) {
915e06e816fSKevin Lo 				INP_WUNLOCK(inp);
916e06e816fSKevin Lo 				error = ENOPROTOOPT;
917e06e816fSKevin Lo 				break;
918e06e816fSKevin Lo 			}
919e06e816fSKevin Lo 			INP_WUNLOCK(inp);
920e06e816fSKevin Lo 			error = sooptcopyin(sopt, &optval, sizeof(optval),
921e06e816fSKevin Lo 			    sizeof(optval));
922e06e816fSKevin Lo 			if (error != 0)
923e06e816fSKevin Lo 				break;
924e06e816fSKevin Lo 			inp = sotoinpcb(so);
925e06e816fSKevin Lo 			KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
926e06e816fSKevin Lo 			INP_WLOCK(inp);
927e06e816fSKevin Lo 			up = intoudpcb(inp);
928e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
92903f90784SMichael Tuexen 			if ((optval != 0 && optval < 8) || (optval > 65535)) {
930e06e816fSKevin Lo 				INP_WUNLOCK(inp);
931e06e816fSKevin Lo 				error = EINVAL;
932e06e816fSKevin Lo 				break;
933e06e816fSKevin Lo 			}
934e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
935e06e816fSKevin Lo 				up->u_txcslen = optval;
936e06e816fSKevin Lo 			else
937e06e816fSKevin Lo 				up->u_rxcslen = optval;
938e06e816fSKevin Lo 			INP_WUNLOCK(inp);
939e06e816fSKevin Lo 			break;
9407b495c44SVANHULLEBUS Yvan 		default:
9417b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9427b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
9437b495c44SVANHULLEBUS Yvan 			break;
9447b495c44SVANHULLEBUS Yvan 		}
9457b495c44SVANHULLEBUS Yvan 		break;
9467b495c44SVANHULLEBUS Yvan 	case SOPT_GET:
9477b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
948fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
949fcf59617SAndrey V. Elsukov #ifdef INET
9507b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
951fcf59617SAndrey V. Elsukov 			if (!IPSEC_ENABLED(ipv4)) {
9527b495c44SVANHULLEBUS Yvan 				INP_WUNLOCK(inp);
953fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
954fcf59617SAndrey V. Elsukov 			}
955fcf59617SAndrey V. Elsukov 			error = UDPENCAP_PCBCTL(inp, sopt);
9567b495c44SVANHULLEBUS Yvan 			break;
957fcf59617SAndrey V. Elsukov #endif /* INET */
958fcf59617SAndrey V. Elsukov #endif /* IPSEC */
959e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
960e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
961e06e816fSKevin Lo 			if (!isudplite) {
962e06e816fSKevin Lo 				INP_WUNLOCK(inp);
963e06e816fSKevin Lo 				error = ENOPROTOOPT;
964e06e816fSKevin Lo 				break;
965e06e816fSKevin Lo 			}
966e06e816fSKevin Lo 			up = intoudpcb(inp);
967e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
968e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
969e06e816fSKevin Lo 				optval = up->u_txcslen;
970e06e816fSKevin Lo 			else
971e06e816fSKevin Lo 				optval = up->u_rxcslen;
972e06e816fSKevin Lo 			INP_WUNLOCK(inp);
973e06e816fSKevin Lo 			error = sooptcopyout(sopt, &optval, sizeof(optval));
974e06e816fSKevin Lo 			break;
9757b495c44SVANHULLEBUS Yvan 		default:
9767b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9777b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
9787b495c44SVANHULLEBUS Yvan 			break;
9797b495c44SVANHULLEBUS Yvan 		}
9807b495c44SVANHULLEBUS Yvan 		break;
9817b495c44SVANHULLEBUS Yvan 	}
9827b495c44SVANHULLEBUS Yvan 	return (error);
9837b495c44SVANHULLEBUS Yvan }
9847b495c44SVANHULLEBUS Yvan 
98579288c11SBjoern A. Zeeb #ifdef INET
986a9839c4aSBjoern A. Zeeb #ifdef INET6
987a9839c4aSBjoern A. Zeeb /* The logic here is derived from ip6_setpktopt(). See comments there. */
988a9839c4aSBjoern A. Zeeb static int
989a9839c4aSBjoern A. Zeeb udp_v4mapped_pktinfo(struct cmsghdr *cm, struct sockaddr_in * src,
990a9839c4aSBjoern A. Zeeb     struct inpcb *inp, int flags)
991a9839c4aSBjoern A. Zeeb {
992a9839c4aSBjoern A. Zeeb 	struct ifnet *ifp;
993a9839c4aSBjoern A. Zeeb 	struct in6_pktinfo *pktinfo;
994a9839c4aSBjoern A. Zeeb 	struct in_addr ia;
995a9839c4aSBjoern A. Zeeb 
996a9839c4aSBjoern A. Zeeb 	if ((flags & PRUS_IPV6) == 0)
997a9839c4aSBjoern A. Zeeb 		return (0);
998a9839c4aSBjoern A. Zeeb 
999a9839c4aSBjoern A. Zeeb 	if (cm->cmsg_level != IPPROTO_IPV6)
1000a9839c4aSBjoern A. Zeeb 		return (0);
1001a9839c4aSBjoern A. Zeeb 
1002a9839c4aSBjoern A. Zeeb 	if  (cm->cmsg_type != IPV6_2292PKTINFO &&
1003a9839c4aSBjoern A. Zeeb 	    cm->cmsg_type != IPV6_PKTINFO)
1004a9839c4aSBjoern A. Zeeb 		return (0);
1005a9839c4aSBjoern A. Zeeb 
1006a9839c4aSBjoern A. Zeeb 	if (cm->cmsg_len !=
1007a9839c4aSBjoern A. Zeeb 	    CMSG_LEN(sizeof(struct in6_pktinfo)))
1008a9839c4aSBjoern A. Zeeb 		return (EINVAL);
1009a9839c4aSBjoern A. Zeeb 
1010a9839c4aSBjoern A. Zeeb 	pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
1011a9839c4aSBjoern A. Zeeb 	if (!IN6_IS_ADDR_V4MAPPED(&pktinfo->ipi6_addr) &&
1012a9839c4aSBjoern A. Zeeb 	    !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr))
1013a9839c4aSBjoern A. Zeeb 		return (EINVAL);
1014a9839c4aSBjoern A. Zeeb 
1015a9839c4aSBjoern A. Zeeb 	/* Validate the interface index if specified. */
1016a9839c4aSBjoern A. Zeeb 	if (pktinfo->ipi6_ifindex) {
1017d74b7baeSGleb Smirnoff 		struct epoch_tracker et;
1018d74b7baeSGleb Smirnoff 
1019d74b7baeSGleb Smirnoff 		NET_EPOCH_ENTER(et);
1020a9839c4aSBjoern A. Zeeb 		ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
1021d74b7baeSGleb Smirnoff 		NET_EPOCH_EXIT(et);	/* XXXGL: unsafe ifp */
1022a9839c4aSBjoern A. Zeeb 		if (ifp == NULL)
1023a9839c4aSBjoern A. Zeeb 			return (ENXIO);
1024d74b7baeSGleb Smirnoff 	} else
1025d74b7baeSGleb Smirnoff 		ifp = NULL;
1026a9839c4aSBjoern A. Zeeb 	if (ifp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
1027a9839c4aSBjoern A. Zeeb 		ia.s_addr = pktinfo->ipi6_addr.s6_addr32[3];
1028a9839c4aSBjoern A. Zeeb 		if (in_ifhasaddr(ifp, ia) == 0)
1029a9839c4aSBjoern A. Zeeb 			return (EADDRNOTAVAIL);
1030a9839c4aSBjoern A. Zeeb 	}
1031a9839c4aSBjoern A. Zeeb 
1032a9839c4aSBjoern A. Zeeb 	bzero(src, sizeof(*src));
1033a9839c4aSBjoern A. Zeeb 	src->sin_family = AF_INET;
1034a9839c4aSBjoern A. Zeeb 	src->sin_len = sizeof(*src);
1035a9839c4aSBjoern A. Zeeb 	src->sin_port = inp->inp_lport;
1036a9839c4aSBjoern A. Zeeb 	src->sin_addr.s_addr = pktinfo->ipi6_addr.s6_addr32[3];
1037a9839c4aSBjoern A. Zeeb 
1038a9839c4aSBjoern A. Zeeb 	return (0);
1039a9839c4aSBjoern A. Zeeb }
1040a9839c4aSBjoern A. Zeeb #endif
1041a9839c4aSBjoern A. Zeeb 
1042490d50b6SBrian Feldman static int
10433329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
1044a9839c4aSBjoern A. Zeeb     struct mbuf *control, struct thread *td, int flags)
1045df8bae1dSRodney W. Grimes {
10463329b236SRobert Watson 	struct udpiphdr *ui;
10473329b236SRobert Watson 	int len = m->m_pkthdr.len;
104890162a4eSIan Dowse 	struct in_addr faddr, laddr;
1049c557ae16SIan Dowse 	struct cmsghdr *cm;
1050e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1051c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
10526573d758SMatt Macy 	struct epoch_tracker et;
1053e06e816fSKevin Lo 	int cscov_partial = 0;
105490162a4eSIan Dowse 	int error = 0;
1055374ce248SMitchell Horne 	int ipflags = 0;
105690162a4eSIan Dowse 	u_short fport, lport;
1057f584d74bSMichael Tuexen 	u_char tos;
1058e06e816fSKevin Lo 	uint8_t pr;
1059e06e816fSKevin Lo 	uint16_t cscov = 0;
10609d3ddf43SAdrian Chadd 	uint32_t flowid = 0;
1061c2529042SHans Petter Selasky 	uint8_t flowtype = M_HASHTYPE_NONE;
1062df8bae1dSRodney W. Grimes 
1063430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1064c557ae16SIan Dowse 		if (control)
1065c557ae16SIan Dowse 			m_freem(control);
10665c32ea65SRobert Watson 		m_freem(m);
10673329b236SRobert Watson 		return (EMSGSIZE);
1068430d30d8SBill Fenner 	}
1069430d30d8SBill Fenner 
10701b7f0384SBruce M Simpson 	src.sin_family = 0;
107184cc0778SGeorge V. Neville-Neil 	sin = (struct sockaddr_in *)addr;
10722435e507SGleb Smirnoff 
1073eafaa1bcSBjoern A. Zeeb 	/*
10742435e507SGleb Smirnoff 	 * udp_output() may need to temporarily bind or connect the current
10752435e507SGleb Smirnoff 	 * inpcb.  As such, we don't know up front whether we will need the
10762435e507SGleb Smirnoff 	 * pcbinfo lock or not.  Do any work to decide what is needed up
10772435e507SGleb Smirnoff 	 * front before acquiring any locks.
10782435e507SGleb Smirnoff 	 *
10792435e507SGleb Smirnoff 	 * We will need network epoch in either case, to safely lookup into
10802435e507SGleb Smirnoff 	 * pcb hash.
1081eafaa1bcSBjoern A. Zeeb 	 */
10822435e507SGleb Smirnoff 	if (sin == NULL ||
10832435e507SGleb Smirnoff 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0))
10842435e507SGleb Smirnoff 		INP_WLOCK(inp);
10852435e507SGleb Smirnoff 	else
10860cfdff24SBjoern A. Zeeb 		INP_RLOCK(inp);
10872435e507SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1088f584d74bSMichael Tuexen 	tos = inp->inp_ip_tos;
1089c557ae16SIan Dowse 	if (control != NULL) {
1090c557ae16SIan Dowse 		/*
10913329b236SRobert Watson 		 * XXX: Currently, we assume all the optional information is
10923329b236SRobert Watson 		 * stored in a single mbuf.
1093c557ae16SIan Dowse 		 */
1094c557ae16SIan Dowse 		if (control->m_next) {
1095c557ae16SIan Dowse 			m_freem(control);
10962435e507SGleb Smirnoff 			error = EINVAL;
10972435e507SGleb Smirnoff 			goto release;
1098c557ae16SIan Dowse 		}
1099c557ae16SIan Dowse 		for (; control->m_len > 0;
1100c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
1101c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1102c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
1103af1ee11dSRobert Watson 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
1104af1ee11dSRobert Watson 			    || cm->cmsg_len > control->m_len) {
1105c557ae16SIan Dowse 				error = EINVAL;
1106c557ae16SIan Dowse 				break;
1107c557ae16SIan Dowse 			}
1108a9839c4aSBjoern A. Zeeb #ifdef INET6
1109a9839c4aSBjoern A. Zeeb 			error = udp_v4mapped_pktinfo(cm, &src, inp, flags);
1110a9839c4aSBjoern A. Zeeb 			if (error != 0)
1111a9839c4aSBjoern A. Zeeb 				break;
1112a9839c4aSBjoern A. Zeeb #endif
1113c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
1114c557ae16SIan Dowse 				continue;
1115c557ae16SIan Dowse 
1116c557ae16SIan Dowse 			switch (cm->cmsg_type) {
1117c557ae16SIan Dowse 			case IP_SENDSRCADDR:
1118c557ae16SIan Dowse 				if (cm->cmsg_len !=
1119c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
1120c557ae16SIan Dowse 					error = EINVAL;
1121c557ae16SIan Dowse 					break;
1122c557ae16SIan Dowse 				}
1123c557ae16SIan Dowse 				bzero(&src, sizeof(src));
1124c557ae16SIan Dowse 				src.sin_family = AF_INET;
1125c557ae16SIan Dowse 				src.sin_len = sizeof(src);
1126c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
1127af1ee11dSRobert Watson 				src.sin_addr =
1128af1ee11dSRobert Watson 				    *(struct in_addr *)CMSG_DATA(cm);
1129c557ae16SIan Dowse 				break;
1130af1ee11dSRobert Watson 
1131f584d74bSMichael Tuexen 			case IP_TOS:
1132f584d74bSMichael Tuexen 				if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
1133f584d74bSMichael Tuexen 					error = EINVAL;
1134f584d74bSMichael Tuexen 					break;
1135f584d74bSMichael Tuexen 				}
1136f584d74bSMichael Tuexen 				tos = *(u_char *)CMSG_DATA(cm);
1137f584d74bSMichael Tuexen 				break;
1138f584d74bSMichael Tuexen 
11399d3ddf43SAdrian Chadd 			case IP_FLOWID:
11409d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
11419d3ddf43SAdrian Chadd 					error = EINVAL;
11429d3ddf43SAdrian Chadd 					break;
11439d3ddf43SAdrian Chadd 				}
11449d3ddf43SAdrian Chadd 				flowid = *(uint32_t *) CMSG_DATA(cm);
11459d3ddf43SAdrian Chadd 				break;
11469d3ddf43SAdrian Chadd 
11479d3ddf43SAdrian Chadd 			case IP_FLOWTYPE:
11489d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
11499d3ddf43SAdrian Chadd 					error = EINVAL;
11509d3ddf43SAdrian Chadd 					break;
11519d3ddf43SAdrian Chadd 				}
1152c2529042SHans Petter Selasky 				flowtype = *(uint32_t *) CMSG_DATA(cm);
11539d3ddf43SAdrian Chadd 				break;
11549d3ddf43SAdrian Chadd 
11559d3ddf43SAdrian Chadd #ifdef	RSS
11569d3ddf43SAdrian Chadd 			case IP_RSSBUCKETID:
11579d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
11589d3ddf43SAdrian Chadd 					error = EINVAL;
11599d3ddf43SAdrian Chadd 					break;
11609d3ddf43SAdrian Chadd 				}
11619d3ddf43SAdrian Chadd 				/* This is just a placeholder for now */
11629d3ddf43SAdrian Chadd 				break;
11639d3ddf43SAdrian Chadd #endif	/* RSS */
1164c557ae16SIan Dowse 			default:
1165c557ae16SIan Dowse 				error = ENOPROTOOPT;
1166c557ae16SIan Dowse 				break;
1167c557ae16SIan Dowse 			}
1168c557ae16SIan Dowse 			if (error)
1169c557ae16SIan Dowse 				break;
1170c557ae16SIan Dowse 		}
1171c557ae16SIan Dowse 		m_freem(control);
1172d8acd268SMark Johnston 		control = NULL;
1173c557ae16SIan Dowse 	}
11742435e507SGleb Smirnoff 	if (error)
11752435e507SGleb Smirnoff 		goto release;
11765c32ea65SRobert Watson 
1177e06e816fSKevin Lo 	pr = inp->inp_socket->so_proto->pr_protocol;
1178a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(pr);
11795c32ea65SRobert Watson 
11801b7f0384SBruce M Simpson 	/*
11811b7f0384SBruce M Simpson 	 * If the IP_SENDSRCADDR control message was specified, override the
11821b7f0384SBruce M Simpson 	 * source address for this datagram.  Its use is invalidated if the
11831b7f0384SBruce M Simpson 	 * address thus specified is incomplete or clobbers other inpcbs.
11841b7f0384SBruce M Simpson 	 */
118590162a4eSIan Dowse 	laddr = inp->inp_laddr;
118690162a4eSIan Dowse 	lport = inp->inp_lport;
11871b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET) {
11881b7f0384SBruce M Simpson 		if ((lport == 0) ||
11891b7f0384SBruce M Simpson 		    (laddr.s_addr == INADDR_ANY &&
11901b7f0384SBruce M Simpson 		     src.sin_addr.s_addr == INADDR_ANY)) {
1191c557ae16SIan Dowse 			error = EINVAL;
1192c557ae16SIan Dowse 			goto release;
1193c557ae16SIan Dowse 		}
1194db0ac6deSCy Schubert 		INP_HASH_WLOCK(pcbinfo);
1195c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
1196b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
1197db0ac6deSCy Schubert 		INP_HASH_WUNLOCK(pcbinfo);
1198c557ae16SIan Dowse 		if (error)
1199c557ae16SIan Dowse 			goto release;
1200c557ae16SIan Dowse 	}
1201c557ae16SIan Dowse 
12023144b7d3SRobert Watson 	/*
12033144b7d3SRobert Watson 	 * If a UDP socket has been connected, then a local address/port will
12043144b7d3SRobert Watson 	 * have been selected and bound.
12053144b7d3SRobert Watson 	 *
120643cc0bc1SRobert Watson 	 * If a UDP socket has not been connected to, then an explicit
12073144b7d3SRobert Watson 	 * destination address must be used, in which case a local
12083144b7d3SRobert Watson 	 * address/port may not have been selected and bound.
12093144b7d3SRobert Watson 	 */
121043cc0bc1SRobert Watson 	if (sin != NULL) {
1211c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
1212df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1213df8bae1dSRodney W. Grimes 			error = EISCONN;
1214df8bae1dSRodney W. Grimes 			goto release;
1215df8bae1dSRodney W. Grimes 		}
12163144b7d3SRobert Watson 
12173144b7d3SRobert Watson 		/*
12183144b7d3SRobert Watson 		 * Jail may rewrite the destination address, so let it do
12193144b7d3SRobert Watson 		 * that before we use it.
12203144b7d3SRobert Watson 		 */
1221b89e82ddSJamie Gritton 		error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1222b89e82ddSJamie Gritton 		if (error)
1223413628a7SBjoern A. Zeeb 			goto release;
12243144b7d3SRobert Watson 
12253144b7d3SRobert Watson 		/*
122643cc0bc1SRobert Watson 		 * If a local address or port hasn't yet been selected, or if
122743cc0bc1SRobert Watson 		 * the destination address needs to be rewritten due to using
122843cc0bc1SRobert Watson 		 * a special INADDR_ constant, invoke in_pcbconnect_setup()
122943cc0bc1SRobert Watson 		 * to do the heavy lifting.  Once a port is selected, we
123043cc0bc1SRobert Watson 		 * commit the binding back to the socket; we also commit the
123143cc0bc1SRobert Watson 		 * binding of the address if in jail.
123243cc0bc1SRobert Watson 		 *
123343cc0bc1SRobert Watson 		 * If we already have a valid binding and we're not
123443cc0bc1SRobert Watson 		 * requesting a destination address rewrite, use a fast path.
12353144b7d3SRobert Watson 		 */
123643cc0bc1SRobert Watson 		if (inp->inp_laddr.s_addr == INADDR_ANY ||
123743cc0bc1SRobert Watson 		    inp->inp_lport == 0 ||
123843cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_ANY ||
123943cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_BROADCAST) {
1240db0ac6deSCy Schubert 			INP_HASH_WLOCK(pcbinfo);
124143cc0bc1SRobert Watson 			error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
124243cc0bc1SRobert Watson 			    &lport, &faddr.s_addr, &fport, NULL,
124343cc0bc1SRobert Watson 			    td->td_ucred);
1244db0ac6deSCy Schubert 			if (error) {
1245db0ac6deSCy Schubert 				INP_HASH_WUNLOCK(pcbinfo);
124690162a4eSIan Dowse 				goto release;
1247db0ac6deSCy Schubert 			}
124890162a4eSIan Dowse 
124943cc0bc1SRobert Watson 			/*
125043cc0bc1SRobert Watson 			 * XXXRW: Why not commit the port if the address is
125143cc0bc1SRobert Watson 			 * !INADDR_ANY?
125243cc0bc1SRobert Watson 			 */
125390162a4eSIan Dowse 			/* Commit the local port if newly assigned. */
125490162a4eSIan Dowse 			if (inp->inp_laddr.s_addr == INADDR_ANY &&
125590162a4eSIan Dowse 			    inp->inp_lport == 0) {
1256c4d585aeSRobert Watson 				INP_WLOCK_ASSERT(inp);
12573a1757b9SGleb Smirnoff 				/*
125843cc0bc1SRobert Watson 				 * Remember addr if jailed, to prevent
125943cc0bc1SRobert Watson 				 * rebinding.
12603a1757b9SGleb Smirnoff 				 */
12610304c731SJamie Gritton 				if (prison_flag(td->td_ucred, PR_IP4))
12623a1757b9SGleb Smirnoff 					inp->inp_laddr = laddr;
126390162a4eSIan Dowse 				inp->inp_lport = lport;
12642435e507SGleb Smirnoff 				error = in_pcbinshash(inp);
12652435e507SGleb Smirnoff 				INP_HASH_WUNLOCK(pcbinfo);
12662435e507SGleb Smirnoff 				if (error != 0) {
126790162a4eSIan Dowse 					inp->inp_lport = 0;
126890162a4eSIan Dowse 					error = EAGAIN;
1269df8bae1dSRodney W. Grimes 					goto release;
1270df8bae1dSRodney W. Grimes 				}
127190162a4eSIan Dowse 				inp->inp_flags |= INP_ANONPORT;
1272db0ac6deSCy Schubert 			} else
1273db0ac6deSCy Schubert 				INP_HASH_WUNLOCK(pcbinfo);
1274df8bae1dSRodney W. Grimes 		} else {
127543cc0bc1SRobert Watson 			faddr = sin->sin_addr;
127643cc0bc1SRobert Watson 			fport = sin->sin_port;
127743cc0bc1SRobert Watson 		}
127843cc0bc1SRobert Watson 	} else {
1279c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
128090162a4eSIan Dowse 		faddr = inp->inp_faddr;
128190162a4eSIan Dowse 		fport = inp->inp_fport;
128290162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
1283df8bae1dSRodney W. Grimes 			error = ENOTCONN;
1284df8bae1dSRodney W. Grimes 			goto release;
1285df8bae1dSRodney W. Grimes 		}
1286df8bae1dSRodney W. Grimes 	}
1287e6ccd709SRobert Watson 
1288df8bae1dSRodney W. Grimes 	/*
1289e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
1290392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
1291392e8407SRobert Watson 	 * since we won't use that space at this layer.
1292df8bae1dSRodney W. Grimes 	 */
1293eb1b1807SGleb Smirnoff 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1294e6ccd709SRobert Watson 	if (m == NULL) {
1295df8bae1dSRodney W. Grimes 		error = ENOBUFS;
129649b19bfcSBruce M Simpson 		goto release;
1297df8bae1dSRodney W. Grimes 	}
1298e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
1299e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
1300392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
1301df8bae1dSRodney W. Grimes 
1302df8bae1dSRodney W. Grimes 	/*
13033329b236SRobert Watson 	 * Fill in mbuf with extended UDP header and addresses and length put
13043329b236SRobert Watson 	 * into network format.
1305df8bae1dSRodney W. Grimes 	 */
1306df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
1307db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
130834fc9072SMichael Tuexen 	ui->ui_v = IPVERSION << 4;
1309e06e816fSKevin Lo 	ui->ui_pr = pr;
131090162a4eSIan Dowse 	ui->ui_src = laddr;
131190162a4eSIan Dowse 	ui->ui_dst = faddr;
131290162a4eSIan Dowse 	ui->ui_sport = lport;
131390162a4eSIan Dowse 	ui->ui_dport = fport;
1314db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1315e06e816fSKevin Lo 	if (pr == IPPROTO_UDPLITE) {
1316e06e816fSKevin Lo 		struct udpcb *up;
1317e06e816fSKevin Lo 		uint16_t plen;
1318e06e816fSKevin Lo 
1319e06e816fSKevin Lo 		up = intoudpcb(inp);
1320e06e816fSKevin Lo 		cscov = up->u_txcslen;
1321e06e816fSKevin Lo 		plen = (u_short)len + sizeof(struct udphdr);
1322e06e816fSKevin Lo 		if (cscov >= plen)
1323e06e816fSKevin Lo 			cscov = 0;
1324e06e816fSKevin Lo 		ui->ui_len = htons(plen);
1325e06e816fSKevin Lo 		ui->ui_ulen = htons(cscov);
1326e06e816fSKevin Lo 		/*
1327e06e816fSKevin Lo 		 * For UDP-Lite, checksum coverage length of zero means
1328e06e816fSKevin Lo 		 * the entire UDPLite packet is covered by the checksum.
1329e06e816fSKevin Lo 		 */
1330e06e816fSKevin Lo 		cscov_partial = (cscov == 0) ? 0 : 1;
133134fc9072SMichael Tuexen 	}
1332df8bae1dSRodney W. Grimes 
1333b2828ad2SAndre Oppermann 	/*
1334b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
1335b2828ad2SAndre Oppermann 	 */
1336b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
1337b2828ad2SAndre Oppermann 		struct ip *ip;
13383329b236SRobert Watson 
1339b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
13408f134647SGleb Smirnoff 		ip->ip_off |= htons(IP_DF);
1341b2828ad2SAndre Oppermann 	}
1342b2828ad2SAndre Oppermann 
1343b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
1344b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
1345b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
1346b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
13476fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
13488afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
13498afa2304SBruce M Simpson 
13501175d9d5SRobert Watson #ifdef MAC
13511175d9d5SRobert Watson 	mac_inpcb_create_mbuf(inp, m);
13521175d9d5SRobert Watson #endif
13531175d9d5SRobert Watson 
1354df8bae1dSRodney W. Grimes 	/*
1355db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
1356df8bae1dSRodney W. Grimes 	 */
1357e06e816fSKevin Lo 	ui->ui_sum = 0;
1358a485f139SMichael Tuexen 	if (pr == IPPROTO_UDPLITE) {
1359e06e816fSKevin Lo 		if (inp->inp_flags & INP_ONESBCAST)
1360e06e816fSKevin Lo 			faddr.s_addr = INADDR_BROADCAST;
1361a485f139SMichael Tuexen 		if (cscov_partial) {
1362e06e816fSKevin Lo 			if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1363e06e816fSKevin Lo 				ui->ui_sum = 0xffff;
1364a485f139SMichael Tuexen 		} else {
1365a485f139SMichael Tuexen 			if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1366a485f139SMichael Tuexen 				ui->ui_sum = 0xffff;
1367a485f139SMichael Tuexen 		}
1368a485f139SMichael Tuexen 	} else if (V_udp_cksum) {
13696fbfd582SAndre Oppermann 		if (inp->inp_flags & INP_ONESBCAST)
13708a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
13718a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1372e06e816fSKevin Lo 		    htons((u_short)len + sizeof(struct udphdr) + pr));
1373db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
1374db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1375e06e816fSKevin Lo 	}
13768f134647SGleb Smirnoff 	((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1377ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
1378f584d74bSMichael Tuexen 	((struct ip *)ui)->ip_tos = tos;		/* XXX */
1379026decb8SRobert Watson 	UDPSTAT_INC(udps_opackets);
1380cfa1ca9dSYoshinobu Inoue 
13819d3ddf43SAdrian Chadd 	/*
13829d3ddf43SAdrian Chadd 	 * Setup flowid / RSS information for outbound socket.
13839d3ddf43SAdrian Chadd 	 *
13849d3ddf43SAdrian Chadd 	 * Once the UDP code decides to set a flowid some other way,
13859d3ddf43SAdrian Chadd 	 * this allows the flowid to be overridden by userland.
13869d3ddf43SAdrian Chadd 	 */
1387c2529042SHans Petter Selasky 	if (flowtype != M_HASHTYPE_NONE) {
13889d3ddf43SAdrian Chadd 		m->m_pkthdr.flowid = flowid;
1389c2529042SHans Petter Selasky 		M_HASHTYPE_SET(m, flowtype);
1390f9a6e8d7SBjoern A. Zeeb 	}
13910c325f53SAlexander V. Chernikov #if defined(ROUTE_MPATH) || defined(RSS)
13920c325f53SAlexander V. Chernikov 	else if (CALC_FLOWID_OUTBOUND_SENDTO) {
13938ad1a83bSAdrian Chadd 		uint32_t hash_val, hash_type;
13940c325f53SAlexander V. Chernikov 
13950c325f53SAlexander V. Chernikov 		hash_val = fib4_calc_packet_hash(laddr, faddr,
13960c325f53SAlexander V. Chernikov 		    lport, fport, pr, &hash_type);
13978ad1a83bSAdrian Chadd 		m->m_pkthdr.flowid = hash_val;
13988ad1a83bSAdrian Chadd 		M_HASHTYPE_SET(m, hash_type);
13998ad1a83bSAdrian Chadd 	}
14009d3ddf43SAdrian Chadd 
14018ad1a83bSAdrian Chadd 	/*
14028ad1a83bSAdrian Chadd 	 * Don't override with the inp cached flowid value.
14038ad1a83bSAdrian Chadd 	 *
14048ad1a83bSAdrian Chadd 	 * Depending upon the kind of send being done, the inp
14058ad1a83bSAdrian Chadd 	 * flowid/flowtype values may actually not be appropriate
14068ad1a83bSAdrian Chadd 	 * for this particular socket send.
14078ad1a83bSAdrian Chadd 	 *
14088ad1a83bSAdrian Chadd 	 * We should either leave the flowid at zero (which is what is
14098ad1a83bSAdrian Chadd 	 * currently done) or set it to some software generated
14108ad1a83bSAdrian Chadd 	 * hash value based on the packet contents.
14118ad1a83bSAdrian Chadd 	 */
14129d3ddf43SAdrian Chadd 	ipflags |= IP_NODEFAULTFLOWID;
14139d3ddf43SAdrian Chadd #endif	/* RSS */
14149d3ddf43SAdrian Chadd 
14157bda9663SMichael Tuexen 	if (pr == IPPROTO_UDPLITE)
14167bda9663SMichael Tuexen 		UDPLITE_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
14177bda9663SMichael Tuexen 	else
141857f60867SMark Johnston 		UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
141984cc0778SGeorge V. Neville-Neil 	error = ip_output(m, inp->inp_options,
14202435e507SGleb Smirnoff 	    INP_WLOCKED(inp) ? &inp->inp_route : NULL, ipflags,
14215d846453SSam Leffler 	    inp->inp_moptions, inp);
14222435e507SGleb Smirnoff 	INP_UNLOCK(inp);
14232435e507SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1424df8bae1dSRodney W. Grimes 	return (error);
1425df8bae1dSRodney W. Grimes 
1426df8bae1dSRodney W. Grimes release:
14272435e507SGleb Smirnoff 	INP_UNLOCK(inp);
14282435e507SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1429df8bae1dSRodney W. Grimes 	m_freem(m);
1430df8bae1dSRodney W. Grimes 	return (error);
1431df8bae1dSRodney W. Grimes }
1432df8bae1dSRodney W. Grimes 
1433c93db4abSGleb Smirnoff pr_abort_t udp_abort;			/* shared with udp6_usrreq.c */
1434c93db4abSGleb Smirnoff void
1435d0390e05SGarrett Wollman udp_abort(struct socket *so)
1436df8bae1dSRodney W. Grimes {
1437d0390e05SGarrett Wollman 	struct inpcb *inp;
1438e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1439df8bae1dSRodney W. Grimes 
1440a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1441d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
144214ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
14438501a69cSRobert Watson 	INP_WLOCK(inp);
1444a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1445e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1446a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1447a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1448e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1449d0390e05SGarrett Wollman 		soisdisconnected(so);
1450a152f8a3SRobert Watson 	}
14518501a69cSRobert Watson 	INP_WUNLOCK(inp);
1452df8bae1dSRodney W. Grimes }
1453df8bae1dSRodney W. Grimes 
1454d0390e05SGarrett Wollman static int
1455b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
1456d0390e05SGarrett Wollman {
1457630ba2c5SMatt Macy 	static uint32_t udp_flowid;
1458e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1459*483fe965SGleb Smirnoff 	struct inpcb *inp;
1460*483fe965SGleb Smirnoff 	struct udpcb *up;
1461277afaffSRobert Watson 	int error;
1462d0390e05SGarrett Wollman 
1463a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1464d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
146514ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1466cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
1467f24618aaSRobert Watson 	if (error)
14683329b236SRobert Watson 		return (error);
1469e06e816fSKevin Lo 	error = in_pcballoc(so, pcbinfo);
1470db0ac6deSCy Schubert 	if (error)
14713329b236SRobert Watson 		return (error);
1472cfa1ca9dSYoshinobu Inoue 
147368b5629bSRobert Watson 	inp = sotoinpcb(so);
1474603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
1475630ba2c5SMatt Macy 	inp->inp_flowid = atomic_fetchadd_int(&udp_flowid, 1);
1476630ba2c5SMatt Macy 	inp->inp_flowtype = M_HASHTYPE_OPAQUE;
1477*483fe965SGleb Smirnoff 	up = intoudpcb(inp);
1478*483fe965SGleb Smirnoff 	bzero(&up->u_start_zero, u_zero_size);
1479266f97b5SCy Schubert 	INP_WUNLOCK(inp);
1480db0ac6deSCy Schubert 
1481c7c7ea4bSRandall Stewart 	return (0);
1482c7c7ea4bSRandall Stewart }
148379288c11SBjoern A. Zeeb #endif /* INET */
1484c7c7ea4bSRandall Stewart 
1485c7c7ea4bSRandall Stewart int
1486abb901c5SRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx)
1487c7c7ea4bSRandall Stewart {
1488c7c7ea4bSRandall Stewart 	struct inpcb *inp;
14896a9148feSBjoern A. Zeeb 	struct udpcb *up;
1490c7c7ea4bSRandall Stewart 
149168b5629bSRobert Watson 	KASSERT(so->so_type == SOCK_DGRAM,
149268b5629bSRobert Watson 	    ("udp_set_kernel_tunneling: !dgram"));
149368b5629bSRobert Watson 	inp = sotoinpcb(so);
149468b5629bSRobert Watson 	KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1495c7c7ea4bSRandall Stewart 	INP_WLOCK(inp);
14966a9148feSBjoern A. Zeeb 	up = intoudpcb(inp);
1497995cba5aSKristof Provost 	if ((f != NULL || i != NULL) && ((up->u_tun_func != NULL) ||
1498995cba5aSKristof Provost 	    (up->u_icmp_func != NULL))) {
1499bbb0e3d9SRandall Stewart 		INP_WUNLOCK(inp);
1500bbb0e3d9SRandall Stewart 		return (EBUSY);
1501bbb0e3d9SRandall Stewart 	}
15026a9148feSBjoern A. Zeeb 	up->u_tun_func = f;
1503abb901c5SRandall Stewart 	up->u_icmp_func = i;
150481d3ec17SBryan Venteicher 	up->u_tun_ctx = ctx;
15058501a69cSRobert Watson 	INP_WUNLOCK(inp);
15063329b236SRobert Watson 	return (0);
1507df8bae1dSRodney W. Grimes }
1508d0390e05SGarrett Wollman 
150979288c11SBjoern A. Zeeb #ifdef INET
1510d0390e05SGarrett Wollman static int
1511b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1512d0390e05SGarrett Wollman {
1513d0390e05SGarrett Wollman 	struct inpcb *inp;
1514e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1515f96603b5SMark Johnston 	struct sockaddr_in *sinp;
1516277afaffSRobert Watson 	int error;
1517d0390e05SGarrett Wollman 
1518a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1519d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
152014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1521f161d294SMark Johnston 
1522f96603b5SMark Johnston 	sinp = (struct sockaddr_in *)nam;
1523f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
1524f96603b5SMark Johnston 		/*
1525f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
1526f96603b5SMark Johnston 		 */
1527f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
15283f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
1529f96603b5SMark Johnston 		    sinp->sin_addr.s_addr != INADDR_ANY)
1530f161d294SMark Johnston 			return (EAFNOSUPPORT);
1531f96603b5SMark Johnston 		nam->sa_family = AF_INET;
1532f96603b5SMark Johnston 	}
1533f161d294SMark Johnston 	if (nam->sa_len != sizeof(struct sockaddr_in))
1534f161d294SMark Johnston 		return (EINVAL);
1535f161d294SMark Johnston 
15368501a69cSRobert Watson 	INP_WLOCK(inp);
1537e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1538b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
1539e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
15408501a69cSRobert Watson 	INP_WUNLOCK(inp);
15413329b236SRobert Watson 	return (error);
1542d0390e05SGarrett Wollman }
1543d0390e05SGarrett Wollman 
1544a152f8a3SRobert Watson static void
1545a152f8a3SRobert Watson udp_close(struct socket *so)
1546a152f8a3SRobert Watson {
1547a152f8a3SRobert Watson 	struct inpcb *inp;
1548e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1549a152f8a3SRobert Watson 
1550a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1551a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1552a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
15538501a69cSRobert Watson 	INP_WLOCK(inp);
1554a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1555e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1556a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1557a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1558e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1559a152f8a3SRobert Watson 		soisdisconnected(so);
1560a152f8a3SRobert Watson 	}
15618501a69cSRobert Watson 	INP_WUNLOCK(inp);
1562a152f8a3SRobert Watson }
1563a152f8a3SRobert Watson 
1564d0390e05SGarrett Wollman static int
1565b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1566d0390e05SGarrett Wollman {
1567c1604fe4SGleb Smirnoff 	struct epoch_tracker et;
1568d0390e05SGarrett Wollman 	struct inpcb *inp;
1569e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
157075c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1571e06e816fSKevin Lo 	int error;
1572d0390e05SGarrett Wollman 
1573a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1574d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
157514ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1576f161d294SMark Johnston 
1577f161d294SMark Johnston 	sin = (struct sockaddr_in *)nam;
1578f161d294SMark Johnston 	if (sin->sin_family != AF_INET)
1579f161d294SMark Johnston 		return (EAFNOSUPPORT);
1580f161d294SMark Johnston 	if (sin->sin_len != sizeof(*sin))
1581f161d294SMark Johnston 		return (EINVAL);
1582f161d294SMark Johnston 
15838501a69cSRobert Watson 	INP_WLOCK(inp);
1584f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
15858501a69cSRobert Watson 		INP_WUNLOCK(inp);
15863329b236SRobert Watson 		return (EISCONN);
1587f76fcf6dSJeffrey Hsu 	}
1588b89e82ddSJamie Gritton 	error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1589b89e82ddSJamie Gritton 	if (error != 0) {
1590413628a7SBjoern A. Zeeb 		INP_WUNLOCK(inp);
1591b89e82ddSJamie Gritton 		return (error);
1592413628a7SBjoern A. Zeeb 	}
1593c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1594e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1595db0ac6deSCy Schubert 	error = in_pcbconnect(inp, nam, td->td_ucred, true);
1596e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
1597c1604fe4SGleb Smirnoff 	NET_EPOCH_EXIT(et);
15984cc20ab1SSeigo Tanimura 	if (error == 0)
1599df8bae1dSRodney W. Grimes 		soisconnected(so);
16008501a69cSRobert Watson 	INP_WUNLOCK(inp);
16013329b236SRobert Watson 	return (error);
1602df8bae1dSRodney W. Grimes }
1603d0390e05SGarrett Wollman 
1604bc725eafSRobert Watson static void
1605d0390e05SGarrett Wollman udp_detach(struct socket *so)
1606d0390e05SGarrett Wollman {
1607d0390e05SGarrett Wollman 	struct inpcb *inp;
1608d0390e05SGarrett Wollman 
1609d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
161014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1611a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1612a152f8a3SRobert Watson 	    ("udp_detach: not disconnected"));
16138501a69cSRobert Watson 	INP_WLOCK(inp);
1614d0390e05SGarrett Wollman 	in_pcbdetach(inp);
161514ba8addSRobert Watson 	in_pcbfree(inp);
1616d0390e05SGarrett Wollman }
1617d0390e05SGarrett Wollman 
1618c93db4abSGleb Smirnoff pr_disconnect_t udp_disconnect;		/* shared with udp6_usrreq.c */
1619c93db4abSGleb Smirnoff int
1620d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1621d0390e05SGarrett Wollman {
1622d0390e05SGarrett Wollman 	struct inpcb *inp;
1623e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1624d0390e05SGarrett Wollman 
1625a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1626d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
162714ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
16288501a69cSRobert Watson 	INP_WLOCK(inp);
1629f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
16308501a69cSRobert Watson 		INP_WUNLOCK(inp);
16313329b236SRobert Watson 		return (ENOTCONN);
1632f76fcf6dSJeffrey Hsu 	}
1633e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1634df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1635df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1636e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
1637d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1638d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1639d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
16408501a69cSRobert Watson 	INP_WUNLOCK(inp);
16413329b236SRobert Watson 	return (0);
1642df8bae1dSRodney W. Grimes }
1643df8bae1dSRodney W. Grimes 
1644c93db4abSGleb Smirnoff pr_send_t udp_send;			/* shared with udp6_usrreq.c */
1645c93db4abSGleb Smirnoff int
164657bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1647b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1648d0390e05SGarrett Wollman {
1649d0390e05SGarrett Wollman 	struct inpcb *inp;
1650f161d294SMark Johnston 	int error;
1651d0390e05SGarrett Wollman 
1652d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
165314ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1654f161d294SMark Johnston 
1655f161d294SMark Johnston 	if (addr != NULL) {
1656f161d294SMark Johnston 		error = 0;
1657f161d294SMark Johnston 		if (addr->sa_family != AF_INET)
1658f161d294SMark Johnston 			error = EAFNOSUPPORT;
1659f161d294SMark Johnston 		else if (addr->sa_len != sizeof(struct sockaddr_in))
1660f161d294SMark Johnston 			error = EINVAL;
1661f161d294SMark Johnston 		if (__predict_false(error != 0)) {
1662f161d294SMark Johnston 			m_freem(control);
1663f161d294SMark Johnston 			m_freem(m);
1664f161d294SMark Johnston 			return (error);
1665f161d294SMark Johnston 		}
1666f161d294SMark Johnston 	}
1667a9839c4aSBjoern A. Zeeb 	return (udp_output(inp, m, addr, control, td, flags));
1668d0390e05SGarrett Wollman }
166979288c11SBjoern A. Zeeb #endif /* INET */
1670d0390e05SGarrett Wollman 
167176429de4SYoshinobu Inoue int
1672d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1673d0390e05SGarrett Wollman {
1674d0390e05SGarrett Wollman 	struct inpcb *inp;
1675d0390e05SGarrett Wollman 
1676d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
167714ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
16788501a69cSRobert Watson 	INP_WLOCK(inp);
1679d0390e05SGarrett Wollman 	socantsendmore(so);
16808501a69cSRobert Watson 	INP_WUNLOCK(inp);
16813329b236SRobert Watson 	return (0);
1682d0390e05SGarrett Wollman }
1683d0390e05SGarrett Wollman 
168479288c11SBjoern A. Zeeb #ifdef INET
1685e7d02be1SGleb Smirnoff #define	UDP_PROTOSW							\
1686e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_DGRAM,				\
1687e7d02be1SGleb Smirnoff 	.pr_flags =		PR_ATOMIC | PR_ADDR | PR_CAPATTACH,	\
1688e7d02be1SGleb Smirnoff 	.pr_ctloutput =		udp_ctloutput,				\
1689e7d02be1SGleb Smirnoff 	.pr_abort =		udp_abort,				\
1690e7d02be1SGleb Smirnoff 	.pr_attach =		udp_attach,				\
1691e7d02be1SGleb Smirnoff 	.pr_bind =		udp_bind,				\
1692e7d02be1SGleb Smirnoff 	.pr_connect =		udp_connect,				\
1693e7d02be1SGleb Smirnoff 	.pr_control =		in_control,				\
1694e7d02be1SGleb Smirnoff 	.pr_detach =		udp_detach,				\
1695e7d02be1SGleb Smirnoff 	.pr_disconnect =	udp_disconnect,				\
1696e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,				\
1697e7d02be1SGleb Smirnoff 	.pr_send =		udp_send,				\
1698e7d02be1SGleb Smirnoff 	.pr_soreceive =		soreceive_dgram,			\
1699e7d02be1SGleb Smirnoff 	.pr_sosend =		sosend_dgram,				\
1700e7d02be1SGleb Smirnoff 	.pr_shutdown =		udp_shutdown,				\
1701e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,				\
1702e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,			\
1703e7d02be1SGleb Smirnoff 	.pr_close =		udp_close
1704e7d02be1SGleb Smirnoff 
1705e7d02be1SGleb Smirnoff struct protosw udp_protosw = {
1706e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_UDP,
1707e7d02be1SGleb Smirnoff 	UDP_PROTOSW
1708e7d02be1SGleb Smirnoff };
1709e7d02be1SGleb Smirnoff 
1710e7d02be1SGleb Smirnoff struct protosw udplite_protosw = {
1711e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_UDPLITE,
1712e7d02be1SGleb Smirnoff 	UDP_PROTOSW
1713d0390e05SGarrett Wollman };
171478b1fc05SGleb Smirnoff 
171578b1fc05SGleb Smirnoff static void
171678b1fc05SGleb Smirnoff udp_init(void *arg __unused)
171778b1fc05SGleb Smirnoff {
171878b1fc05SGleb Smirnoff 
171978b1fc05SGleb Smirnoff 	IPPROTO_REGISTER(IPPROTO_UDP, udp_input, udp_ctlinput);
172078b1fc05SGleb Smirnoff 	IPPROTO_REGISTER(IPPROTO_UDPLITE, udp_input, udplite_ctlinput);
172178b1fc05SGleb Smirnoff }
172278b1fc05SGleb Smirnoff SYSINIT(udp_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, udp_init, NULL);
172379288c11SBjoern A. Zeeb #endif /* INET */
1724