xref: /freebsd/sys/netinet/udp_usrreq.c (revision 46374cbf5411afc82c297a4643afaab1ec2c6b7c)
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"
479d3ddf43SAdrian Chadd #include "opt_rss.h"
48cfa1ca9dSYoshinobu Inoue 
49df8bae1dSRodney W. Grimes #include <sys/param.h>
50960ed29cSSeigo Tanimura #include <sys/domain.h>
514f590175SPaul Saab #include <sys/eventhandler.h>
52960ed29cSSeigo Tanimura #include <sys/jail.h>
53b110a8a2SGarrett Wollman #include <sys/kernel.h>
54960ed29cSSeigo Tanimura #include <sys/lock.h>
55df8bae1dSRodney W. Grimes #include <sys/malloc.h>
56df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
57acd3428bSRobert Watson #include <sys/priv.h>
58490d50b6SBrian Feldman #include <sys/proc.h>
59df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6057f60867SMark Johnston #include <sys/sdt.h>
61960ed29cSSeigo Tanimura #include <sys/signalvar.h>
62df8bae1dSRodney W. Grimes #include <sys/socket.h>
63df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
64960ed29cSSeigo Tanimura #include <sys/sx.h>
65b5e8ce9fSBruce Evans #include <sys/sysctl.h>
66816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
67f5514f08SRobert Watson #include <sys/systm.h>
688781d8e9SBruce Evans 
6969c2d429SJeff Roberson #include <vm/uma.h>
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <net/if.h>
7276039bc8SGleb Smirnoff #include <net/if_var.h>
73df8bae1dSRodney W. Grimes #include <net/route.h>
74b2bdc62aSAdrian Chadd #include <net/rss_config.h>
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes #include <netinet/in.h>
7757f60867SMark Johnston #include <netinet/in_kdtrace.h>
78960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
79f5514f08SRobert Watson #include <netinet/in_systm.h>
80960ed29cSSeigo Tanimura #include <netinet/in_var.h>
81df8bae1dSRodney W. Grimes #include <netinet/ip.h>
82cfa1ca9dSYoshinobu Inoue #ifdef INET6
83cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
84cfa1ca9dSYoshinobu Inoue #endif
85960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
86960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
87df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
88ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
89cfa1ca9dSYoshinobu Inoue #ifdef INET6
90cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
91cfa1ca9dSYoshinobu Inoue #endif
92df8bae1dSRodney W. Grimes #include <netinet/udp.h>
93df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
94e06e816fSKevin Lo #include <netinet/udplite.h>
958ad1a83bSAdrian Chadd #include <netinet/in_rss.h>
96df8bae1dSRodney W. Grimes 
97fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
98b9234fafSSam Leffler 
99db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
100db4f9cc7SJonathan Lemon 
101aed55708SRobert Watson #include <security/mac/mac_framework.h>
102aed55708SRobert Watson 
103df8bae1dSRodney W. Grimes /*
104e06e816fSKevin Lo  * UDP and UDP-Lite protocols implementation.
105df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
106e06e816fSKevin Lo  * Per RFC 3828, July, 2004.
107df8bae1dSRodney W. Grimes  */
10874eb3236SWarner Losh 
10974eb3236SWarner Losh /*
1103329b236SRobert Watson  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
1113329b236SRobert Watson  * removes the only data integrity mechanism for packets and malformed
112f5514f08SRobert Watson  * packets that would otherwise be discarded due to bad checksums, and may
113f5514f08SRobert Watson  * cause problems (especially for NFS data blocks).
11474eb3236SWarner Losh  */
11540b676beSBjoern A. Zeeb VNET_DEFINE(int, udp_cksum) = 1;
1166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW,
11740b676beSBjoern A. Zeeb     &VNET_NAME(udp_cksum), 0, "compute udp checksum");
118df8bae1dSRodney W. Grimes 
119afdb4274SRobert Watson int	udp_log_in_vain = 0;
120816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
121afdb4274SRobert Watson     &udp_log_in_vain, 0, "Log all incoming UDP packets");
122816a3d83SPoul-Henning Kamp 
12382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, udp_blackhole) = 0;
1246df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
125eddfbb76SRobert Watson     &VNET_NAME(udp_blackhole), 0,
1263329b236SRobert Watson     "Do not send port unreachables for refused connects");
12716f7f31fSGeoff Rehmet 
12843bbb6aaSRobert Watson u_long	udp_sendspace = 9216;		/* really max datagram size */
12943bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
13043bbb6aaSRobert Watson     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
13143bbb6aaSRobert Watson 
13243bbb6aaSRobert Watson u_long	udp_recvspace = 40 * (1024 +
13343bbb6aaSRobert Watson #ifdef INET6
13443bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in6)
13543bbb6aaSRobert Watson #else
13643bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in)
13743bbb6aaSRobert Watson #endif
138e62b9bcaSSergey Kandaurov 				      );	/* 40 1K datagrams */
13943bbb6aaSRobert Watson 
14043bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14143bbb6aaSRobert Watson     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
14243bbb6aaSRobert Watson 
143eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, udb);		/* from udp_var.h */
144eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, udbinfo);
145e06e816fSKevin Lo VNET_DEFINE(struct inpcbhead, ulitecb);
146e06e816fSKevin Lo VNET_DEFINE(struct inpcbinfo, ulitecbinfo);
1473e288e62SDimitry Andric static VNET_DEFINE(uma_zone_t, udpcb_zone);
1481e77c105SRobert Watson #define	V_udpcb_zone			VNET(udpcb_zone)
14915bd2b43SDavid Greenman 
15015bd2b43SDavid Greenman #ifndef UDBHASHSIZE
151e2ed8f35SAlexander Motin #define	UDBHASHSIZE	128
15215bd2b43SDavid Greenman #endif
15315bd2b43SDavid Greenman 
1545b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat);		/* from udp_var.h */
1555b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(udpstat);
1565b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat,
1575b7cb97cSAndrey V. Elsukov     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
158f2ea20e6SGarrett Wollman 
1595b7cb97cSAndrey V. Elsukov #ifdef VIMAGE
1605b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(udpstat);
1615b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */
16279288c11SBjoern A. Zeeb #ifdef INET
163bc725eafSRobert Watson static void	udp_detach(struct socket *so);
1644d77a549SAlfred Perlstein static int	udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1654d77a549SAlfred Perlstein 		    struct mbuf *, struct thread *);
16679288c11SBjoern A. Zeeb #endif
16779288c11SBjoern A. Zeeb 
1684f590175SPaul Saab static void
1694f590175SPaul Saab udp_zone_change(void *tag)
1704f590175SPaul Saab {
1714f590175SPaul Saab 
172603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
1736a9148feSBjoern A. Zeeb 	uma_zone_set_max(V_udpcb_zone, maxsockets);
1744f590175SPaul Saab }
1754f590175SPaul Saab 
176d915b280SStephan Uphoff static int
177d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags)
178d915b280SStephan Uphoff {
179af1ee11dSRobert Watson 	struct inpcb *inp;
18008651e1fSJohn Baldwin 
181af1ee11dSRobert Watson 	inp = mem;
182d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "udpinp");
183d915b280SStephan Uphoff 	return (0);
184d915b280SStephan Uphoff }
185d915b280SStephan Uphoff 
186e06e816fSKevin Lo static int
187e06e816fSKevin Lo udplite_inpcb_init(void *mem, int size, int flags)
188e06e816fSKevin Lo {
189e06e816fSKevin Lo 	struct inpcb *inp;
190e06e816fSKevin Lo 
191e06e816fSKevin Lo 	inp = mem;
192e06e816fSKevin Lo 	INP_LOCK_INIT(inp, "inp", "udpliteinp");
193e06e816fSKevin Lo 	return (0);
194e06e816fSKevin Lo }
195e06e816fSKevin Lo 
196df8bae1dSRodney W. Grimes void
197af1ee11dSRobert Watson udp_init(void)
198df8bae1dSRodney W. Grimes {
199af1ee11dSRobert Watson 
2008ad1a83bSAdrian Chadd 	/*
2018ad1a83bSAdrian Chadd 	 * For now default to 2-tuple UDP hashing - until the fragment
2028ad1a83bSAdrian Chadd 	 * reassembly code can also update the flowid.
2038ad1a83bSAdrian Chadd 	 *
2048ad1a83bSAdrian Chadd 	 * Once we can calculate the flowid that way and re-establish
2058ad1a83bSAdrian Chadd 	 * a 4-tuple, flip this to 4-tuple.
2068ad1a83bSAdrian Chadd 	 */
2079bcd427bSRobert Watson 	in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE,
208cc487c16SGleb Smirnoff 	    "udp_inpcb", udp_inpcb_init, IPI_HASHFIELDS_2TUPLE);
2096a9148feSBjoern A. Zeeb 	V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb),
210a8da5dd6SCraig Rodrigues 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2116a9148feSBjoern A. Zeeb 	uma_zone_set_max(V_udpcb_zone, maxsockets);
2126acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(V_udpcb_zone, "kern.ipc.maxsockets limit reached");
2134f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
2144f590175SPaul Saab 	    EVENTHANDLER_PRI_ANY);
215df8bae1dSRodney W. Grimes }
216df8bae1dSRodney W. Grimes 
217e06e816fSKevin Lo void
218e06e816fSKevin Lo udplite_init(void)
219e06e816fSKevin Lo {
220e06e816fSKevin Lo 
221e06e816fSKevin Lo 	in_pcbinfo_init(&V_ulitecbinfo, "udplite", &V_ulitecb, UDBHASHSIZE,
222cc487c16SGleb Smirnoff 	    UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init,
223cc487c16SGleb Smirnoff 	    IPI_HASHFIELDS_2TUPLE);
224e06e816fSKevin Lo }
225e06e816fSKevin Lo 
226315e3e38SRobert Watson /*
227315e3e38SRobert Watson  * Kernel module interface for updating udpstat.  The argument is an index
228315e3e38SRobert Watson  * into udpstat treated as an array of u_long.  While this encodes the
229315e3e38SRobert Watson  * general layout of udpstat into the caller, it doesn't encode its location,
230315e3e38SRobert Watson  * so that future changes to add, for example, per-CPU stats support won't
231315e3e38SRobert Watson  * cause binary compatibility problems for kernel modules.
232315e3e38SRobert Watson  */
233315e3e38SRobert Watson void
234315e3e38SRobert Watson kmod_udpstat_inc(int statnum)
235315e3e38SRobert Watson {
236315e3e38SRobert Watson 
2375b7cb97cSAndrey V. Elsukov 	counter_u64_add(VNET(udpstat)[statnum], 1);
238315e3e38SRobert Watson }
239315e3e38SRobert Watson 
2406a9148feSBjoern A. Zeeb int
2416a9148feSBjoern A. Zeeb udp_newudpcb(struct inpcb *inp)
2426a9148feSBjoern A. Zeeb {
2436a9148feSBjoern A. Zeeb 	struct udpcb *up;
2446a9148feSBjoern A. Zeeb 
2456a9148feSBjoern A. Zeeb 	up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO);
2466a9148feSBjoern A. Zeeb 	if (up == NULL)
2476a9148feSBjoern A. Zeeb 		return (ENOBUFS);
2486a9148feSBjoern A. Zeeb 	inp->inp_ppcb = up;
2496a9148feSBjoern A. Zeeb 	return (0);
2506a9148feSBjoern A. Zeeb }
2516a9148feSBjoern A. Zeeb 
2526a9148feSBjoern A. Zeeb void
2536a9148feSBjoern A. Zeeb udp_discardcb(struct udpcb *up)
2546a9148feSBjoern A. Zeeb {
2556a9148feSBjoern A. Zeeb 
2566a9148feSBjoern A. Zeeb 	uma_zfree(V_udpcb_zone, up);
2576a9148feSBjoern A. Zeeb }
2586a9148feSBjoern A. Zeeb 
259bc29160dSMarko Zec #ifdef VIMAGE
2603f58662dSBjoern A. Zeeb static void
2613f58662dSBjoern A. Zeeb udp_destroy(void *unused __unused)
262bc29160dSMarko Zec {
263bc29160dSMarko Zec 
2649bcd427bSRobert Watson 	in_pcbinfo_destroy(&V_udbinfo);
265391dab1cSBjoern A. Zeeb 	uma_zdestroy(V_udpcb_zone);
266bc29160dSMarko Zec }
2673f58662dSBjoern A. Zeeb VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL);
268e06e816fSKevin Lo 
2693f58662dSBjoern A. Zeeb static void
2703f58662dSBjoern A. Zeeb udplite_destroy(void *unused __unused)
271e06e816fSKevin Lo {
272e06e816fSKevin Lo 
273e06e816fSKevin Lo 	in_pcbinfo_destroy(&V_ulitecbinfo);
274e06e816fSKevin Lo }
2753f58662dSBjoern A. Zeeb VNET_SYSUNINIT(udplite, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udplite_destroy,
2763f58662dSBjoern A. Zeeb     NULL);
277bc29160dSMarko Zec #endif
278bc29160dSMarko Zec 
27979288c11SBjoern A. Zeeb #ifdef INET
28043bbb6aaSRobert Watson /*
28143bbb6aaSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
28243bbb6aaSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
28343bbb6aaSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
28443bbb6aaSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
28543bbb6aaSRobert Watson  * into the socket code.
286c0d1be08SRandall Stewart  *
287c0d1be08SRandall Stewart  * In the normal case udp_append() will return 0, indicating that you
288c0d1be08SRandall Stewart  * must unlock the inp. However if a tunneling protocol is in place we increment
289c0d1be08SRandall Stewart  * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we
290c0d1be08SRandall Stewart  * then decrement the reference count. If the inp_rele returns 1, indicating the
291c0d1be08SRandall Stewart  * inp is gone, we return that to the caller to tell them *not* to unlock
292c0d1be08SRandall Stewart  * the inp. In the case of multi-cast this will cause the distribution
293c0d1be08SRandall Stewart  * to stop (though most tunneling protocols known currently do *not* use
294c0d1be08SRandall Stewart  * multicast).
29543bbb6aaSRobert Watson  */
296c0d1be08SRandall Stewart static int
29743bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
29843bbb6aaSRobert Watson     struct sockaddr_in *udp_in)
29943bbb6aaSRobert Watson {
30043bbb6aaSRobert Watson 	struct sockaddr *append_sa;
30143bbb6aaSRobert Watson 	struct socket *so;
302dce33a45SErmal Luçi 	struct mbuf *tmpopts, *opts = NULL;
30343bbb6aaSRobert Watson #ifdef INET6
30443bbb6aaSRobert Watson 	struct sockaddr_in6 udp_in6;
30543bbb6aaSRobert Watson #endif
3067b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
30743bbb6aaSRobert Watson 
308fa046d87SRobert Watson 	INP_LOCK_ASSERT(inp);
30943bbb6aaSRobert Watson 
31079bb84fbSEdward Tomasz Napierala 	/*
31179bb84fbSEdward Tomasz Napierala 	 * Engage the tunneling protocol.
31279bb84fbSEdward Tomasz Napierala 	 */
31379bb84fbSEdward Tomasz Napierala 	up = intoudpcb(inp);
31479bb84fbSEdward Tomasz Napierala 	if (up->u_tun_func != NULL) {
315c0d1be08SRandall Stewart 		in_pcbref(inp);
316c0d1be08SRandall Stewart 		INP_RUNLOCK(inp);
317dce33a45SErmal Luçi 		(*up->u_tun_func)(n, off, inp, (struct sockaddr *)&udp_in[0],
31881d3ec17SBryan Venteicher 		    up->u_tun_ctx);
319c0d1be08SRandall Stewart 		INP_RLOCK(inp);
320c0d1be08SRandall Stewart 		return (in_pcbrele_rlocked(inp));
32179bb84fbSEdward Tomasz Napierala 	}
32279bb84fbSEdward Tomasz Napierala 
32379bb84fbSEdward Tomasz Napierala 	off += sizeof(struct udphdr);
32479bb84fbSEdward Tomasz Napierala 
325fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
32643bbb6aaSRobert Watson 	/* Check AH/ESP integrity. */
327fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
328fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, n, inp) != 0) {
32943bbb6aaSRobert Watson 		m_freem(n);
330c0d1be08SRandall Stewart 		return (0);
33143bbb6aaSRobert Watson 	}
332fcf59617SAndrey V. Elsukov 	if (up->u_flags & UF_ESPINUDP) {/* IPSec UDP encaps. */
333fcf59617SAndrey V. Elsukov 		if (IPSEC_ENABLED(ipv4) &&
334fcf59617SAndrey V. Elsukov 		    UDPENCAP_INPUT(n, off, AF_INET) != 0)
335fcf59617SAndrey V. Elsukov 			return (0);	/* Consumed. */
3367b495c44SVANHULLEBUS Yvan 	}
33743bbb6aaSRobert Watson #endif /* IPSEC */
33843bbb6aaSRobert Watson #ifdef MAC
33930d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, n) != 0) {
34043bbb6aaSRobert Watson 		m_freem(n);
341c0d1be08SRandall Stewart 		return (0);
34243bbb6aaSRobert Watson 	}
34379288c11SBjoern A. Zeeb #endif /* MAC */
34443bbb6aaSRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
34543bbb6aaSRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
34643bbb6aaSRobert Watson #ifdef INET6
3479a38ba81SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6)
34848d48eb9SBjoern A. Zeeb 			(void)ip6_savecontrol_v4(inp, n, &opts, NULL);
3499a38ba81SBjoern A. Zeeb 		else
35079288c11SBjoern A. Zeeb #endif /* INET6 */
35143bbb6aaSRobert Watson 			ip_savecontrol(inp, &opts, ip, n);
35243bbb6aaSRobert Watson 	}
353dce33a45SErmal Luçi 	if ((inp->inp_vflag & INP_IPV4) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
354dce33a45SErmal Luçi 		tmpopts = sbcreatecontrol((caddr_t)&udp_in[1],
355dce33a45SErmal Luçi 			sizeof(struct sockaddr_in), IP_ORIGDSTADDR, IPPROTO_IP);
356dce33a45SErmal Luçi 		if (tmpopts) {
357dce33a45SErmal Luçi 			if (opts) {
358dce33a45SErmal Luçi 				tmpopts->m_next = opts;
359dce33a45SErmal Luçi 				opts = tmpopts;
360dce33a45SErmal Luçi 			} else
361dce33a45SErmal Luçi 				opts = tmpopts;
362dce33a45SErmal Luçi 		}
363dce33a45SErmal Luçi 	}
36443bbb6aaSRobert Watson #ifdef INET6
36543bbb6aaSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
36643bbb6aaSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
36743bbb6aaSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
36843bbb6aaSRobert Watson 		udp_in6.sin6_family = AF_INET6;
369dce33a45SErmal Luçi 		in6_sin_2_v4mapsin6(&udp_in[0], &udp_in6);
37043bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
37143bbb6aaSRobert Watson 	} else
37279288c11SBjoern A. Zeeb #endif /* INET6 */
373dce33a45SErmal Luçi 		append_sa = (struct sockaddr *)&udp_in[0];
37443bbb6aaSRobert Watson 	m_adj(n, off);
37543bbb6aaSRobert Watson 
37643bbb6aaSRobert Watson 	so = inp->inp_socket;
37743bbb6aaSRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
37843bbb6aaSRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
37943bbb6aaSRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
38043bbb6aaSRobert Watson 		m_freem(n);
38143bbb6aaSRobert Watson 		if (opts)
38243bbb6aaSRobert Watson 			m_freem(opts);
383026decb8SRobert Watson 		UDPSTAT_INC(udps_fullsock);
38443bbb6aaSRobert Watson 	} else
38543bbb6aaSRobert Watson 		sorwakeup_locked(so);
386c0d1be08SRandall Stewart 	return (0);
38743bbb6aaSRobert Watson }
38843bbb6aaSRobert Watson 
3898f5a8818SKevin Lo int
3908f5a8818SKevin Lo udp_input(struct mbuf **mp, int *offp, int proto)
391df8bae1dSRodney W. Grimes {
3923329b236SRobert Watson 	struct ip *ip;
3933329b236SRobert Watson 	struct udphdr *uh;
39471498f30SBruce M Simpson 	struct ifnet *ifp;
3953329b236SRobert Watson 	struct inpcb *inp;
3968f134647SGleb Smirnoff 	uint16_t len, ip_len;
397e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
398df8bae1dSRodney W. Grimes 	struct ip save_ip;
399dce33a45SErmal Luçi 	struct sockaddr_in udp_in[2];
4008f5a8818SKevin Lo 	struct mbuf *m;
4010b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
4028f5a8818SKevin Lo 	int cscov_partial, iphlen;
403df8bae1dSRodney W. Grimes 
4048f5a8818SKevin Lo 	m = *mp;
4058f5a8818SKevin Lo 	iphlen = *offp;
40671498f30SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
4078f5a8818SKevin Lo 	*mp = NULL;
408026decb8SRobert Watson 	UDPSTAT_INC(udps_ipackets);
409df8bae1dSRodney W. Grimes 
410df8bae1dSRodney W. Grimes 	/*
4113329b236SRobert Watson 	 * Strip IP options, if any; should skip this, make available to
4123329b236SRobert Watson 	 * user, and use on returned packets, but we don't yet have a way to
4133329b236SRobert Watson 	 * check the checksum with options still present.
414df8bae1dSRodney W. Grimes 	 */
415df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
416105bd211SGleb Smirnoff 		ip_stripoptions(m);
417df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
418df8bae1dSRodney W. Grimes 	}
419df8bae1dSRodney W. Grimes 
420df8bae1dSRodney W. Grimes 	/*
421df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
422df8bae1dSRodney W. Grimes 	 */
423df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
424df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
425d1b18731SKevin Lo 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) {
426026decb8SRobert Watson 			UDPSTAT_INC(udps_hdrops);
4278f5a8818SKevin Lo 			return (IPPROTO_DONE);
428df8bae1dSRodney W. Grimes 		}
429df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
430df8bae1dSRodney W. Grimes 	}
431df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
4328f5a8818SKevin Lo 	cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0;
433df8bae1dSRodney W. Grimes 
4343329b236SRobert Watson 	/*
4353329b236SRobert Watson 	 * Destination port of 0 is illegal, based on RFC768.
4363329b236SRobert Watson 	 */
437686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
438f76fcf6dSJeffrey Hsu 		goto badunlocked;
439686cdd19SJun-ichiro itojun Hagino 
440df8bae1dSRodney W. Grimes 	/*
4413329b236SRobert Watson 	 * Construct sockaddr format source address.  Stuff source address
4423329b236SRobert Watson 	 * and datagram in user buffer.
443b9234fafSSam Leffler 	 */
444dce33a45SErmal Luçi 	bzero(&udp_in[0], sizeof(struct sockaddr_in) * 2);
445dce33a45SErmal Luçi 	udp_in[0].sin_len = sizeof(struct sockaddr_in);
446dce33a45SErmal Luçi 	udp_in[0].sin_family = AF_INET;
447dce33a45SErmal Luçi 	udp_in[0].sin_port = uh->uh_sport;
448dce33a45SErmal Luçi 	udp_in[0].sin_addr = ip->ip_src;
449dce33a45SErmal Luçi 	udp_in[1].sin_len = sizeof(struct sockaddr_in);
450dce33a45SErmal Luçi 	udp_in[1].sin_family = AF_INET;
451dce33a45SErmal Luçi 	udp_in[1].sin_port = uh->uh_dport;
452dce33a45SErmal Luçi 	udp_in[1].sin_addr = ip->ip_dst;
453b9234fafSSam Leffler 
454b9234fafSSam Leffler 	/*
455af1ee11dSRobert Watson 	 * Make mbuf data length reflect UDP length.  If not enough data to
456af1ee11dSRobert Watson 	 * reflect UDP length, drop.
457df8bae1dSRodney W. Grimes 	 */
458df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
4598ad458a4SGleb Smirnoff 	ip_len = ntohs(ip->ip_len) - iphlen;
4600f4a0366SMichael Tuexen 	if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) {
461e06e816fSKevin Lo 		/* Zero means checksum over the complete packet. */
4620f4a0366SMichael Tuexen 		if (len == 0)
463e06e816fSKevin Lo 			len = ip_len;
464e06e816fSKevin Lo 		cscov_partial = 0;
465e06e816fSKevin Lo 	}
4668f134647SGleb Smirnoff 	if (ip_len != len) {
4678f134647SGleb Smirnoff 		if (len > ip_len || len < sizeof(struct udphdr)) {
468026decb8SRobert Watson 			UDPSTAT_INC(udps_badlen);
469f76fcf6dSJeffrey Hsu 			goto badunlocked;
470df8bae1dSRodney W. Grimes 		}
4718f5a8818SKevin Lo 		if (proto == IPPROTO_UDP)
4728f134647SGleb Smirnoff 			m_adj(m, len - ip_len);
473df8bae1dSRodney W. Grimes 	}
4743329b236SRobert Watson 
475df8bae1dSRodney W. Grimes 	/*
4763329b236SRobert Watson 	 * Save a copy of the IP header in case we want restore it for
4773329b236SRobert Watson 	 * sending an ICMP error message in response.
478df8bae1dSRodney W. Grimes 	 */
479603724d3SBjoern A. Zeeb 	if (!V_udp_blackhole)
480df8bae1dSRodney W. Grimes 		save_ip = *ip;
481cce418d3SMatt Jacob 	else
482cce418d3SMatt Jacob 		memset(&save_ip, 0, sizeof(save_ip));
483df8bae1dSRodney W. Grimes 
484df8bae1dSRodney W. Grimes 	/*
485df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
486df8bae1dSRodney W. Grimes 	 */
4876dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
48839629c92SDavid Malone 		u_short uh_sum;
48939629c92SDavid Malone 
490e06e816fSKevin Lo 		if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
491e06e816fSKevin Lo 		    !cscov_partial) {
492db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
49339629c92SDavid Malone 				uh_sum = m->m_pkthdr.csum_data;
494db4f9cc7SJonathan Lemon 			else
49539629c92SDavid Malone 				uh_sum = in_pseudo(ip->ip_src.s_addr,
496506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
4978f5a8818SKevin Lo 				    m->m_pkthdr.csum_data + proto));
49839629c92SDavid Malone 			uh_sum ^= 0xffff;
499db4f9cc7SJonathan Lemon 		} else {
500cb342100SHajimu UMEMOTO 			char b[9];
501af1ee11dSRobert Watson 
502cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
5036effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
5048f5a8818SKevin Lo 			((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ?
505e06e816fSKevin Lo 			    uh->uh_ulen : htons(ip_len);
50639629c92SDavid Malone 			uh_sum = in_cksum(m, len + sizeof (struct ip));
507cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
508db4f9cc7SJonathan Lemon 		}
50939629c92SDavid Malone 		if (uh_sum) {
510026decb8SRobert Watson 			UDPSTAT_INC(udps_badsum);
511df8bae1dSRodney W. Grimes 			m_freem(m);
5128f5a8818SKevin Lo 			return (IPPROTO_DONE);
513df8bae1dSRodney W. Grimes 		}
514c6d81a34SMichael Tuexen 	} else {
515c6d81a34SMichael Tuexen 		if (proto == IPPROTO_UDP) {
516026decb8SRobert Watson 			UDPSTAT_INC(udps_nosum);
517c6d81a34SMichael Tuexen 		} else {
518c6d81a34SMichael Tuexen 			/* UDPLite requires a checksum */
519c6d81a34SMichael Tuexen 			/* XXX: What is the right UDPLite MIB counter here? */
520c6d81a34SMichael Tuexen 			m_freem(m);
521c6d81a34SMichael Tuexen 			return (IPPROTO_DONE);
522c6d81a34SMichael Tuexen 		}
523c6d81a34SMichael Tuexen 	}
524df8bae1dSRodney W. Grimes 
525a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(proto);
526df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
5274af540d1SRyan Stone 	    in_broadcast(ip->ip_dst, ifp)) {
52882c23ebaSBill Fenner 		struct inpcb *last;
529e06e816fSKevin Lo 		struct inpcbhead *pcblist;
53071498f30SBruce M Simpson 		struct ip_moptions *imo;
5313329b236SRobert Watson 
532e06e816fSKevin Lo 		INP_INFO_RLOCK(pcbinfo);
533a86e5c96SBjoern A. Zeeb 		pcblist = udp_get_pcblist(proto);
534df8bae1dSRodney W. Grimes 		last = NULL;
535b872626dSMatt Macy 		CK_LIST_FOREACH(inp, pcblist, inp_list) {
5369c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
537f76fcf6dSJeffrey Hsu 				continue;
538cfa1ca9dSYoshinobu Inoue #ifdef INET6
539369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
5409c1df695SRobert Watson 				continue;
541cfa1ca9dSYoshinobu Inoue #endif
54271498f30SBruce M Simpson 			if (inp->inp_laddr.s_addr != INADDR_ANY &&
54371498f30SBruce M Simpson 			    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
5449c1df695SRobert Watson 				continue;
54571498f30SBruce M Simpson 			if (inp->inp_faddr.s_addr != INADDR_ANY &&
54671498f30SBruce M Simpson 			    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
54771498f30SBruce M Simpson 				continue;
54871498f30SBruce M Simpson 			if (inp->inp_fport != 0 &&
549df8bae1dSRodney W. Grimes 			    inp->inp_fport != uh->uh_sport)
5509c1df695SRobert Watson 				continue;
55171498f30SBruce M Simpson 
552119d85f6SRobert Watson 			INP_RLOCK(inp);
553df8bae1dSRodney W. Grimes 
55483453a06SBruce M Simpson 			/*
555fa046d87SRobert Watson 			 * XXXRW: Because we weren't holding either the inpcb
556fa046d87SRobert Watson 			 * or the hash lock when we checked for a match
557fa046d87SRobert Watson 			 * before, we should probably recheck now that the
558fa046d87SRobert Watson 			 * inpcb lock is held.
559fa046d87SRobert Watson 			 */
560fa046d87SRobert Watson 
561fa046d87SRobert Watson 			/*
56271498f30SBruce M Simpson 			 * Handle socket delivery policy for any-source
56371498f30SBruce M Simpson 			 * and source-specific multicast. [RFC3678]
56483453a06SBruce M Simpson 			 */
56571498f30SBruce M Simpson 			imo = inp->inp_moptions;
566a38b1c8cSRandall Stewart 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
567d10910e6SBruce M Simpson 				struct sockaddr_in	 group;
568d10910e6SBruce M Simpson 				int			 blocked;
569a38b1c8cSRandall Stewart 				if (imo == NULL) {
570a38b1c8cSRandall Stewart 					INP_RUNLOCK(inp);
571a38b1c8cSRandall Stewart 					continue;
572a38b1c8cSRandall Stewart 				}
573d10910e6SBruce M Simpson 				bzero(&group, sizeof(struct sockaddr_in));
574d10910e6SBruce M Simpson 				group.sin_len = sizeof(struct sockaddr_in);
575d10910e6SBruce M Simpson 				group.sin_family = AF_INET;
576d10910e6SBruce M Simpson 				group.sin_addr = ip->ip_dst;
57771498f30SBruce M Simpson 
578d10910e6SBruce M Simpson 				blocked = imo_multi_filter(imo, ifp,
579d10910e6SBruce M Simpson 					(struct sockaddr *)&group,
580dce33a45SErmal Luçi 					(struct sockaddr *)&udp_in[0]);
581d10910e6SBruce M Simpson 				if (blocked != MCAST_PASS) {
582d10910e6SBruce M Simpson 					if (blocked == MCAST_NOTGMEMBER)
58386425c62SRobert Watson 						IPSTAT_INC(ips_notmember);
584d10910e6SBruce M Simpson 					if (blocked == MCAST_NOTSMEMBER ||
585d10910e6SBruce M Simpson 					    blocked == MCAST_MUTED)
586026decb8SRobert Watson 						UDPSTAT_INC(udps_filtermcast);
587119d85f6SRobert Watson 					INP_RUNLOCK(inp);
5889c1df695SRobert Watson 					continue;
5899c1df695SRobert Watson 				}
59083453a06SBruce M Simpson 			}
591df8bae1dSRodney W. Grimes 			if (last != NULL) {
592df8bae1dSRodney W. Grimes 				struct mbuf *n;
593df8bae1dSRodney W. Grimes 
594c3bef61eSKevin Lo 				if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) !=
595c3bef61eSKevin Lo 				    NULL) {
596a0a9e1b5SBryan Venteicher 					UDP_PROBE(receive, NULL, last, ip,
597a0a9e1b5SBryan Venteicher 					    last, uh);
598c0d1be08SRandall Stewart 					if (udp_append(last, ip, n, iphlen,
599dce33a45SErmal Luçi 						udp_in)) {
600c0d1be08SRandall Stewart 						goto inp_lost;
601c0d1be08SRandall Stewart 					}
602c19f98ebSBryan Venteicher 				}
6036a9148feSBjoern A. Zeeb 				INP_RUNLOCK(last);
604df8bae1dSRodney W. Grimes 			}
60582c23ebaSBill Fenner 			last = inp;
606df8bae1dSRodney W. Grimes 			/*
607df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
608df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
6093329b236SRobert Watson 			 * socket options set.  This heuristic avoids
6103329b236SRobert Watson 			 * searching through all pcbs in the common case of a
6113329b236SRobert Watson 			 * non-shared port.  It assumes that an application
6123329b236SRobert Watson 			 * will never clear these options after setting them.
613df8bae1dSRodney W. Grimes 			 */
6143329b236SRobert Watson 			if ((last->inp_socket->so_options &
6151a43cff9SSean Bruno 			    (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0)
616df8bae1dSRodney W. Grimes 				break;
617df8bae1dSRodney W. Grimes 		}
618df8bae1dSRodney W. Grimes 
619df8bae1dSRodney W. Grimes 		if (last == NULL) {
620df8bae1dSRodney W. Grimes 			/*
6213329b236SRobert Watson 			 * No matching pcb found; discard datagram.  (No need
6223329b236SRobert Watson 			 * to send an ICMP Port Unreachable for a broadcast
6233329b236SRobert Watson 			 * or multicast datgram.)
624df8bae1dSRodney W. Grimes 			 */
625026decb8SRobert Watson 			UDPSTAT_INC(udps_noportbcast);
626fa046d87SRobert Watson 			if (inp)
627fa046d87SRobert Watson 				INP_RUNLOCK(inp);
628e06e816fSKevin Lo 			INP_INFO_RUNLOCK(pcbinfo);
629fa046d87SRobert Watson 			goto badunlocked;
630df8bae1dSRodney W. Grimes 		}
631a0a9e1b5SBryan Venteicher 		UDP_PROBE(receive, NULL, last, ip, last, uh);
632dce33a45SErmal Luçi 		if (udp_append(last, ip, m, iphlen, udp_in) == 0)
633c7c7ea4bSRandall Stewart 			INP_RUNLOCK(last);
634c0d1be08SRandall Stewart 	inp_lost:
635e06e816fSKevin Lo 		INP_INFO_RUNLOCK(pcbinfo);
6368f5a8818SKevin Lo 		return (IPPROTO_DONE);
637df8bae1dSRodney W. Grimes 	}
6383329b236SRobert Watson 
639df8bae1dSRodney W. Grimes 	/*
6406d6a026bSDavid Greenman 	 * Locate pcb for datagram.
641df8bae1dSRodney W. Grimes 	 */
642c1de64a4SAndrey V. Elsukov 
6438a006adbSBjoern A. Zeeb 	/*
6448a006adbSBjoern A. Zeeb 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
6458a006adbSBjoern A. Zeeb 	 */
646ffdbf9daSAndrey V. Elsukov 	if ((m->m_flags & M_IP_NEXTHOP) &&
647c1de64a4SAndrey V. Elsukov 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
6488a006adbSBjoern A. Zeeb 		struct sockaddr_in *next_hop;
6498a006adbSBjoern A. Zeeb 
6508a006adbSBjoern A. Zeeb 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
6518a006adbSBjoern A. Zeeb 
6528a006adbSBjoern A. Zeeb 		/*
6538a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
6548a006adbSBjoern A. Zeeb 		 * Already got one like this?
6558a006adbSBjoern A. Zeeb 		 */
656e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
6578a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m);
6588a006adbSBjoern A. Zeeb 		if (!inp) {
6598a006adbSBjoern A. Zeeb 			/*
6608a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
6618a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
6628a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
6638a006adbSBjoern A. Zeeb 			 */
664e06e816fSKevin Lo 			inp = in_pcblookup(pcbinfo, ip->ip_src,
6658a006adbSBjoern A. Zeeb 			    uh->uh_sport, next_hop->sin_addr,
6668a006adbSBjoern A. Zeeb 			    next_hop->sin_port ? htons(next_hop->sin_port) :
6678a006adbSBjoern A. Zeeb 			    uh->uh_dport, INPLOOKUP_WILDCARD |
6688a006adbSBjoern A. Zeeb 			    INPLOOKUP_RLOCKPCB, ifp);
6698a006adbSBjoern A. Zeeb 		}
6708a006adbSBjoern A. Zeeb 		/* Remove the tag from the packet. We don't need it anymore. */
6718a006adbSBjoern A. Zeeb 		m_tag_delete(m, fwd_tag);
672ffdbf9daSAndrey V. Elsukov 		m->m_flags &= ~M_IP_NEXTHOP;
6738a006adbSBjoern A. Zeeb 	} else
674e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
6758a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
6768a006adbSBjoern A. Zeeb 		    INPLOOKUP_RLOCKPCB, ifp, m);
67715bd2b43SDavid Greenman 	if (inp == NULL) {
678afdb4274SRobert Watson 		if (udp_log_in_vain) {
679edf0313bSEric van Gyzen 			char src[INET_ADDRSTRLEN];
680edf0313bSEric van Gyzen 			char dst[INET_ADDRSTRLEN];
68175cfc95fSAndrey A. Chernov 
682592071e8SBruce Evans 			log(LOG_INFO,
683592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
684edf0313bSEric van Gyzen 			    inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport),
685edf0313bSEric van Gyzen 			    inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport));
68675cfc95fSAndrey A. Chernov 		}
687026decb8SRobert Watson 		UDPSTAT_INC(udps_noport);
688df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
689026decb8SRobert Watson 			UDPSTAT_INC(udps_noportbcast);
690fa046d87SRobert Watson 			goto badunlocked;
691df8bae1dSRodney W. Grimes 		}
692603724d3SBjoern A. Zeeb 		if (V_udp_blackhole)
693fa046d87SRobert Watson 			goto badunlocked;
6941cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
695fa046d87SRobert Watson 			goto badunlocked;
69604287599SRuslan Ermilov 		*ip = save_ip;
697582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
6988f5a8818SKevin Lo 		return (IPPROTO_DONE);
699df8bae1dSRodney W. Grimes 	}
7003329b236SRobert Watson 
7013329b236SRobert Watson 	/*
7023329b236SRobert Watson 	 * Check the minimum TTL for socket.
7033329b236SRobert Watson 	 */
704fa046d87SRobert Watson 	INP_RLOCK_ASSERT(inp);
70510cc62b7SRobert Watson 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
70610cc62b7SRobert Watson 		INP_RUNLOCK(inp);
707fa046d87SRobert Watson 		m_freem(m);
7088f5a8818SKevin Lo 		return (IPPROTO_DONE);
70910cc62b7SRobert Watson 	}
710e06e816fSKevin Lo 	if (cscov_partial) {
711e06e816fSKevin Lo 		struct udpcb *up;
712e06e816fSKevin Lo 
713e06e816fSKevin Lo 		up = intoudpcb(inp);
71483e95fb3SMichael Tuexen 		if (up->u_rxcslen == 0 || up->u_rxcslen > len) {
715e06e816fSKevin Lo 			INP_RUNLOCK(inp);
716e06e816fSKevin Lo 			m_freem(m);
7178f5a8818SKevin Lo 			return (IPPROTO_DONE);
718e06e816fSKevin Lo 		}
719e06e816fSKevin Lo 	}
72057f60867SMark Johnston 
7211ad19fb6SMark Johnston 	UDP_PROBE(receive, NULL, inp, ip, inp, uh);
722dce33a45SErmal Luçi 	if (udp_append(inp, ip, m, iphlen, udp_in) == 0)
723119d85f6SRobert Watson 		INP_RUNLOCK(inp);
7248f5a8818SKevin Lo 	return (IPPROTO_DONE);
72561ffc0b1SJeffrey Hsu 
726f76fcf6dSJeffrey Hsu badunlocked:
727df8bae1dSRodney W. Grimes 	m_freem(m);
7288f5a8818SKevin Lo 	return (IPPROTO_DONE);
729cfa1ca9dSYoshinobu Inoue }
73079288c11SBjoern A. Zeeb #endif /* INET */
731cfa1ca9dSYoshinobu Inoue 
732cfa1ca9dSYoshinobu Inoue /*
7333329b236SRobert Watson  * Notify a udp user of an asynchronous error; just wake up so that they can
7343329b236SRobert Watson  * collect error status.
735df8bae1dSRodney W. Grimes  */
7363ce144eaSJeffrey Hsu struct inpcb *
7373329b236SRobert Watson udp_notify(struct inpcb *inp, int errno)
738df8bae1dSRodney W. Grimes {
7393329b236SRobert Watson 
740ac9ae279SRobert Watson 	/*
741ac9ae279SRobert Watson 	 * While udp_ctlinput() always calls udp_notify() with a read lock
742ac9ae279SRobert Watson 	 * when invoking it directly, in_pcbnotifyall() currently uses write
743ac9ae279SRobert Watson 	 * locks due to sharing code with TCP.  For now, accept either a read
744ac9ae279SRobert Watson 	 * or a write lock, but a read lock is sufficient.
745ac9ae279SRobert Watson 	 */
746ac9ae279SRobert Watson 	INP_LOCK_ASSERT(inp);
74784cc0778SGeorge V. Neville-Neil 	if ((errno == EHOSTUNREACH || errno == ENETUNREACH ||
74884cc0778SGeorge V. Neville-Neil 	     errno == EHOSTDOWN) && inp->inp_route.ro_rt) {
74984cc0778SGeorge V. Neville-Neil 		RTFREE(inp->inp_route.ro_rt);
75084cc0778SGeorge V. Neville-Neil 		inp->inp_route.ro_rt = (struct rtentry *)NULL;
75184cc0778SGeorge V. Neville-Neil 	}
7528501a69cSRobert Watson 
753df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
754df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
755df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
7563329b236SRobert Watson 	return (inp);
757df8bae1dSRodney W. Grimes }
758df8bae1dSRodney W. Grimes 
75979288c11SBjoern A. Zeeb #ifdef INET
760e06e816fSKevin Lo static void
761e06e816fSKevin Lo udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip,
762e06e816fSKevin Lo     struct inpcbinfo *pcbinfo)
763df8bae1dSRodney W. Grimes {
764c693a045SJonathan Lemon 	struct ip *ip = vip;
765c693a045SJonathan Lemon 	struct udphdr *uh;
766c693a045SJonathan Lemon 	struct in_addr faddr;
767c693a045SJonathan Lemon 	struct inpcb *inp;
768c693a045SJonathan Lemon 
769c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
770c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
771c693a045SJonathan Lemon 		return;
772df8bae1dSRodney W. Grimes 
77384cc0778SGeorge V. Neville-Neil 	if (PRC_IS_REDIRECT(cmd)) {
77484cc0778SGeorge V. Neville-Neil 		/* signal EHOSTDOWN, as it flushes the cached route */
7754f321dbdSBjoern A. Zeeb 		in_pcbnotifyall(&V_udbinfo, faddr, EHOSTDOWN, udp_notify);
77697d8d152SAndre Oppermann 		return;
77784cc0778SGeorge V. Neville-Neil 	}
7783329b236SRobert Watson 
77997d8d152SAndre Oppermann 	/*
78097d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
7813329b236SRobert Watson 	 *
7823329b236SRobert Watson 	 * XXX: We never get this from ICMP, otherwise it makes an excellent
7833329b236SRobert Watson 	 * DoS attack on machines with many connections.
78497d8d152SAndre Oppermann 	 */
78597d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
786af1ee11dSRobert Watson 		ip = NULL;
787d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
788df8bae1dSRodney W. Grimes 		return;
789af1ee11dSRobert Watson 	if (ip != NULL) {
790df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
791e06e816fSKevin Lo 		inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
792fa046d87SRobert Watson 		    ip->ip_src, uh->uh_sport, INPLOOKUP_RLOCKPCB, NULL);
793f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
794fa046d87SRobert Watson 			INP_RLOCK_ASSERT(inp);
795f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
796f5514f08SRobert Watson 				udp_notify(inp, inetctlerrmap[cmd]);
797f76fcf6dSJeffrey Hsu 			}
798ac9ae279SRobert Watson 			INP_RUNLOCK(inp);
799abb901c5SRandall Stewart 		} else {
800abb901c5SRandall Stewart 			inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
801abb901c5SRandall Stewart 					   ip->ip_src, uh->uh_sport,
802abb901c5SRandall Stewart 					   INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
803abb901c5SRandall Stewart 			if (inp != NULL) {
804abb901c5SRandall Stewart 				struct udpcb *up;
805*46374cbfSMatt Macy 				void *ctx;
806*46374cbfSMatt Macy 				udp_tun_icmp_t func;
807abb901c5SRandall Stewart 
808abb901c5SRandall Stewart 				up = intoudpcb(inp);
809*46374cbfSMatt Macy 				ctx = up->u_tun_ctx;
810*46374cbfSMatt Macy 				func = up->u_icmp_func;
811abb901c5SRandall Stewart 				INP_RUNLOCK(inp);
812*46374cbfSMatt Macy 				if (func != NULL)
813*46374cbfSMatt Macy 					(*func)(cmd, sa, vip, ctx);
814abb901c5SRandall Stewart 			}
815f76fcf6dSJeffrey Hsu 		}
816df8bae1dSRodney W. Grimes 	} else
817e06e816fSKevin Lo 		in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd],
818f5514f08SRobert Watson 		    udp_notify);
819df8bae1dSRodney W. Grimes }
820e06e816fSKevin Lo void
821e06e816fSKevin Lo udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
822e06e816fSKevin Lo {
823e06e816fSKevin Lo 
824e06e816fSKevin Lo 	return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo));
825e06e816fSKevin Lo }
826e06e816fSKevin Lo 
827e06e816fSKevin Lo void
828e06e816fSKevin Lo udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip)
829e06e816fSKevin Lo {
830e06e816fSKevin Lo 
831e06e816fSKevin Lo 	return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo));
832e06e816fSKevin Lo }
83379288c11SBjoern A. Zeeb #endif /* INET */
834df8bae1dSRodney W. Grimes 
8350312fbe9SPoul-Henning Kamp static int
83682d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
83798271db4SGarrett Wollman {
838277afaffSRobert Watson 	int error, i, n;
83998271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
840056b40e2SMatt Macy 	struct in_pcblist *il;
84198271db4SGarrett Wollman 	inp_gen_t gencnt;
84298271db4SGarrett Wollman 	struct xinpgen xig;
84398271db4SGarrett Wollman 
84498271db4SGarrett Wollman 	/*
845f5514f08SRobert Watson 	 * The process of preparing the PCB list is too time-consuming and
84698271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
84798271db4SGarrett Wollman 	 */
84898271db4SGarrett Wollman 	if (req->oldptr == 0) {
849603724d3SBjoern A. Zeeb 		n = V_udbinfo.ipi_count;
850c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
851c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
8523329b236SRobert Watson 		return (0);
85398271db4SGarrett Wollman 	}
85498271db4SGarrett Wollman 
85598271db4SGarrett Wollman 	if (req->newptr != 0)
8563329b236SRobert Watson 		return (EPERM);
85798271db4SGarrett Wollman 
85898271db4SGarrett Wollman 	/*
85998271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
86098271db4SGarrett Wollman 	 */
861603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_udbinfo);
862603724d3SBjoern A. Zeeb 	gencnt = V_udbinfo.ipi_gencnt;
863603724d3SBjoern A. Zeeb 	n = V_udbinfo.ipi_count;
864603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_udbinfo);
86598271db4SGarrett Wollman 
86647934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
8675c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
86847934cefSDon Lewis 	if (error != 0)
86947934cefSDon Lewis 		return (error);
8705c38b6dbSDon Lewis 
87198271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
87298271db4SGarrett Wollman 	xig.xig_count = n;
87398271db4SGarrett Wollman 	xig.xig_gen = gencnt;
87498271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
87598271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
87698271db4SGarrett Wollman 	if (error)
8773329b236SRobert Watson 		return (error);
878fe524329SMatt Macy 	il = malloc(sizeof(struct in_pcblist) + n * sizeof(struct inpcb *), M_TEMP, M_WAITOK|M_ZERO_INVARIANTS);
879056b40e2SMatt Macy 	inp_list = il->il_inp_list;
88098271db4SGarrett Wollman 
881603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_udbinfo);
882b872626dSMatt Macy 	for (inp = CK_LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n;
883b872626dSMatt Macy 	     inp = CK_LIST_NEXT(inp, inp_list)) {
884d0e157f6SBjoern A. Zeeb 		INP_WLOCK(inp);
8852ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
886d0e157f6SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
887d0e157f6SBjoern A. Zeeb 			in_pcbref(inp);
88898271db4SGarrett Wollman 			inp_list[i++] = inp;
889d0e157f6SBjoern A. Zeeb 		}
890d0e157f6SBjoern A. Zeeb 		INP_WUNLOCK(inp);
8914787fd37SPaul Saab 	}
892603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_udbinfo);
89398271db4SGarrett Wollman 	n = i;
89498271db4SGarrett Wollman 
89598271db4SGarrett Wollman 	error = 0;
89698271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
89798271db4SGarrett Wollman 		inp = inp_list[i];
8989622e84fSRobert Watson 		INP_RLOCK(inp);
89998271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
90098271db4SGarrett Wollman 			struct xinpcb xi;
901d0e157f6SBjoern A. Zeeb 
902cc65eb4eSGleb Smirnoff 			in_pcbtoxinpcb(inp, &xi);
9039622e84fSRobert Watson 			INP_RUNLOCK(inp);
90498271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
905d915b280SStephan Uphoff 		} else
9069622e84fSRobert Watson 			INP_RUNLOCK(inp);
90798271db4SGarrett Wollman 	}
908056b40e2SMatt Macy 	il->il_count = n;
909056b40e2SMatt Macy 	il->il_pcbinfo = &V_udbinfo;
910056b40e2SMatt Macy 	epoch_call(net_epoch_preempt, &il->il_epoch_ctx, in_pcblist_rele_rlocked);
911d0e157f6SBjoern A. Zeeb 
91298271db4SGarrett Wollman 	if (!error) {
91398271db4SGarrett Wollman 		/*
9143329b236SRobert Watson 		 * Give the user an updated idea of our state.  If the
9153329b236SRobert Watson 		 * generation differs from what we told her before, she knows
9163329b236SRobert Watson 		 * that something happened while we were processing this
9173329b236SRobert Watson 		 * request, and it might be necessary to retry.
91898271db4SGarrett Wollman 		 */
919603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_udbinfo);
920603724d3SBjoern A. Zeeb 		xig.xig_gen = V_udbinfo.ipi_gencnt;
92198271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
922603724d3SBjoern A. Zeeb 		xig.xig_count = V_udbinfo.ipi_count;
923603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_udbinfo);
92498271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
92598271db4SGarrett Wollman 	}
9263329b236SRobert Watson 	return (error);
92798271db4SGarrett Wollman }
92898271db4SGarrett Wollman 
92979c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
93079c3d51bSMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
93198271db4SGarrett Wollman     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
93298271db4SGarrett Wollman 
93379288c11SBjoern A. Zeeb #ifdef INET
93498271db4SGarrett Wollman static int
93582d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
936490d50b6SBrian Feldman {
937c0511d3bSBrian Feldman 	struct xucred xuc;
938490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
939490d50b6SBrian Feldman 	struct inpcb *inp;
940277afaffSRobert Watson 	int error;
941490d50b6SBrian Feldman 
94232f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
943490d50b6SBrian Feldman 	if (error)
944490d50b6SBrian Feldman 		return (error);
945490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
946490d50b6SBrian Feldman 	if (error)
947490d50b6SBrian Feldman 		return (error);
948fa046d87SRobert Watson 	inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
949fa046d87SRobert Watson 	    addrs[0].sin_addr, addrs[0].sin_port,
950fa046d87SRobert Watson 	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
9519622e84fSRobert Watson 	if (inp != NULL) {
952fa046d87SRobert Watson 		INP_RLOCK_ASSERT(inp);
9539622e84fSRobert Watson 		if (inp->inp_socket == NULL)
9549622e84fSRobert Watson 			error = ENOENT;
9559622e84fSRobert Watson 		if (error == 0)
956f08ef6c5SBjoern A. Zeeb 			error = cr_canseeinpcb(req->td->td_ucred, inp);
9579622e84fSRobert Watson 		if (error == 0)
95886d02c5cSBjoern A. Zeeb 			cru2x(inp->inp_cred, &xuc);
9599622e84fSRobert Watson 		INP_RUNLOCK(inp);
960fa046d87SRobert Watson 	} else
9619622e84fSRobert Watson 		error = ENOENT;
9620e1eebb8SDon Lewis 	if (error == 0)
9630e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
964490d50b6SBrian Feldman 	return (error);
965490d50b6SBrian Feldman }
966490d50b6SBrian Feldman 
9677ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
9687ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
9697ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
97079288c11SBjoern A. Zeeb #endif /* INET */
971490d50b6SBrian Feldman 
9727b495c44SVANHULLEBUS Yvan int
9737b495c44SVANHULLEBUS Yvan udp_ctloutput(struct socket *so, struct sockopt *sopt)
9747b495c44SVANHULLEBUS Yvan {
9757b495c44SVANHULLEBUS Yvan 	struct inpcb *inp;
9767b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
977e06e816fSKevin Lo 	int isudplite, error, optval;
9787b495c44SVANHULLEBUS Yvan 
979e06e816fSKevin Lo 	error = 0;
980e06e816fSKevin Lo 	isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0;
9817b495c44SVANHULLEBUS Yvan 	inp = sotoinpcb(so);
9827b495c44SVANHULLEBUS Yvan 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
9837b495c44SVANHULLEBUS Yvan 	INP_WLOCK(inp);
984e06e816fSKevin Lo 	if (sopt->sopt_level != so->so_proto->pr_protocol) {
9857b495c44SVANHULLEBUS Yvan #ifdef INET6
9867b495c44SVANHULLEBUS Yvan 		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
9877b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9887b495c44SVANHULLEBUS Yvan 			error = ip6_ctloutput(so, sopt);
98979288c11SBjoern A. Zeeb 		}
9907b495c44SVANHULLEBUS Yvan #endif
99179288c11SBjoern A. Zeeb #if defined(INET) && defined(INET6)
99279288c11SBjoern A. Zeeb 		else
99379288c11SBjoern A. Zeeb #endif
99479288c11SBjoern A. Zeeb #ifdef INET
99579288c11SBjoern A. Zeeb 		{
9967b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9977b495c44SVANHULLEBUS Yvan 			error = ip_ctloutput(so, sopt);
9987b495c44SVANHULLEBUS Yvan 		}
9997b495c44SVANHULLEBUS Yvan #endif
10007b495c44SVANHULLEBUS Yvan 		return (error);
10017b495c44SVANHULLEBUS Yvan 	}
10027b495c44SVANHULLEBUS Yvan 
10037b495c44SVANHULLEBUS Yvan 	switch (sopt->sopt_dir) {
10047b495c44SVANHULLEBUS Yvan 	case SOPT_SET:
10057b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
1006fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1007fcf59617SAndrey V. Elsukov #ifdef INET
10087b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
1009fcf59617SAndrey V. Elsukov 			if (!IPSEC_ENABLED(ipv4)) {
10107b495c44SVANHULLEBUS Yvan 				INP_WUNLOCK(inp);
1011fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
10127b495c44SVANHULLEBUS Yvan 			}
1013fcf59617SAndrey V. Elsukov 			error = UDPENCAP_PCBCTL(inp, sopt);
10147b495c44SVANHULLEBUS Yvan 			break;
1015fcf59617SAndrey V. Elsukov #endif /* INET */
1016fcf59617SAndrey V. Elsukov #endif /* IPSEC */
1017e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
1018e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
1019e06e816fSKevin Lo 			if (!isudplite) {
1020e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1021e06e816fSKevin Lo 				error = ENOPROTOOPT;
1022e06e816fSKevin Lo 				break;
1023e06e816fSKevin Lo 			}
1024e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1025e06e816fSKevin Lo 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1026e06e816fSKevin Lo 			    sizeof(optval));
1027e06e816fSKevin Lo 			if (error != 0)
1028e06e816fSKevin Lo 				break;
1029e06e816fSKevin Lo 			inp = sotoinpcb(so);
1030e06e816fSKevin Lo 			KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1031e06e816fSKevin Lo 			INP_WLOCK(inp);
1032e06e816fSKevin Lo 			up = intoudpcb(inp);
1033e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
103403f90784SMichael Tuexen 			if ((optval != 0 && optval < 8) || (optval > 65535)) {
1035e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1036e06e816fSKevin Lo 				error = EINVAL;
1037e06e816fSKevin Lo 				break;
1038e06e816fSKevin Lo 			}
1039e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1040e06e816fSKevin Lo 				up->u_txcslen = optval;
1041e06e816fSKevin Lo 			else
1042e06e816fSKevin Lo 				up->u_rxcslen = optval;
1043e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1044e06e816fSKevin Lo 			break;
10457b495c44SVANHULLEBUS Yvan 		default:
10467b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10477b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
10487b495c44SVANHULLEBUS Yvan 			break;
10497b495c44SVANHULLEBUS Yvan 		}
10507b495c44SVANHULLEBUS Yvan 		break;
10517b495c44SVANHULLEBUS Yvan 	case SOPT_GET:
10527b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
1053fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1054fcf59617SAndrey V. Elsukov #ifdef INET
10557b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
1056fcf59617SAndrey V. Elsukov 			if (!IPSEC_ENABLED(ipv4)) {
10577b495c44SVANHULLEBUS Yvan 				INP_WUNLOCK(inp);
1058fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1059fcf59617SAndrey V. Elsukov 			}
1060fcf59617SAndrey V. Elsukov 			error = UDPENCAP_PCBCTL(inp, sopt);
10617b495c44SVANHULLEBUS Yvan 			break;
1062fcf59617SAndrey V. Elsukov #endif /* INET */
1063fcf59617SAndrey V. Elsukov #endif /* IPSEC */
1064e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
1065e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
1066e06e816fSKevin Lo 			if (!isudplite) {
1067e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1068e06e816fSKevin Lo 				error = ENOPROTOOPT;
1069e06e816fSKevin Lo 				break;
1070e06e816fSKevin Lo 			}
1071e06e816fSKevin Lo 			up = intoudpcb(inp);
1072e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
1073e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1074e06e816fSKevin Lo 				optval = up->u_txcslen;
1075e06e816fSKevin Lo 			else
1076e06e816fSKevin Lo 				optval = up->u_rxcslen;
1077e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1078e06e816fSKevin Lo 			error = sooptcopyout(sopt, &optval, sizeof(optval));
1079e06e816fSKevin Lo 			break;
10807b495c44SVANHULLEBUS Yvan 		default:
10817b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10827b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
10837b495c44SVANHULLEBUS Yvan 			break;
10847b495c44SVANHULLEBUS Yvan 		}
10857b495c44SVANHULLEBUS Yvan 		break;
10867b495c44SVANHULLEBUS Yvan 	}
10877b495c44SVANHULLEBUS Yvan 	return (error);
10887b495c44SVANHULLEBUS Yvan }
10897b495c44SVANHULLEBUS Yvan 
109079288c11SBjoern A. Zeeb #ifdef INET
1091fa046d87SRobert Watson #define	UH_WLOCKED	2
1092fa046d87SRobert Watson #define	UH_RLOCKED	1
1093fa046d87SRobert Watson #define	UH_UNLOCKED	0
1094490d50b6SBrian Feldman static int
10953329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
10963329b236SRobert Watson     struct mbuf *control, struct thread *td)
1097df8bae1dSRodney W. Grimes {
10983329b236SRobert Watson 	struct udpiphdr *ui;
10993329b236SRobert Watson 	int len = m->m_pkthdr.len;
110090162a4eSIan Dowse 	struct in_addr faddr, laddr;
1101c557ae16SIan Dowse 	struct cmsghdr *cm;
1102e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1103c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
1104e06e816fSKevin Lo 	int cscov_partial = 0;
110590162a4eSIan Dowse 	int error = 0;
11068afa2304SBruce M Simpson 	int ipflags;
110790162a4eSIan Dowse 	u_short fport, lport;
110884cc0778SGeorge V. Neville-Neil 	int unlock_udbinfo, unlock_inp;
1109f584d74bSMichael Tuexen 	u_char tos;
1110e06e816fSKevin Lo 	uint8_t pr;
1111e06e816fSKevin Lo 	uint16_t cscov = 0;
11129d3ddf43SAdrian Chadd 	uint32_t flowid = 0;
1113c2529042SHans Petter Selasky 	uint8_t flowtype = M_HASHTYPE_NONE;
1114df8bae1dSRodney W. Grimes 
11155c32ea65SRobert Watson 	/*
11165c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
1117f5514f08SRobert Watson 	 * inpcb.  As such, we don't know up front whether we will need the
1118f5514f08SRobert Watson 	 * pcbinfo lock or not.  Do any work to decide what is needed up
1119f5514f08SRobert Watson 	 * front before acquiring any locks.
11205c32ea65SRobert Watson 	 */
1121430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1122c557ae16SIan Dowse 		if (control)
1123c557ae16SIan Dowse 			m_freem(control);
11245c32ea65SRobert Watson 		m_freem(m);
11253329b236SRobert Watson 		return (EMSGSIZE);
1126430d30d8SBill Fenner 	}
1127430d30d8SBill Fenner 
11281b7f0384SBruce M Simpson 	src.sin_family = 0;
112984cc0778SGeorge V. Neville-Neil 	sin = (struct sockaddr_in *)addr;
113084cc0778SGeorge V. Neville-Neil 	if (sin == NULL ||
113184cc0778SGeorge V. Neville-Neil 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
113284cc0778SGeorge V. Neville-Neil 		INP_WLOCK(inp);
113384cc0778SGeorge V. Neville-Neil 		unlock_inp = UH_WLOCKED;
113484cc0778SGeorge V. Neville-Neil 	} else {
11350cfdff24SBjoern A. Zeeb 		INP_RLOCK(inp);
113684cc0778SGeorge V. Neville-Neil 		unlock_inp = UH_RLOCKED;
113784cc0778SGeorge V. Neville-Neil 	}
1138f584d74bSMichael Tuexen 	tos = inp->inp_ip_tos;
1139c557ae16SIan Dowse 	if (control != NULL) {
1140c557ae16SIan Dowse 		/*
11413329b236SRobert Watson 		 * XXX: Currently, we assume all the optional information is
11423329b236SRobert Watson 		 * stored in a single mbuf.
1143c557ae16SIan Dowse 		 */
1144c557ae16SIan Dowse 		if (control->m_next) {
114584cc0778SGeorge V. Neville-Neil 			if (unlock_inp == UH_WLOCKED)
114684cc0778SGeorge V. Neville-Neil 				INP_WUNLOCK(inp);
114784cc0778SGeorge V. Neville-Neil 			else
11480cfdff24SBjoern A. Zeeb 				INP_RUNLOCK(inp);
1149c557ae16SIan Dowse 			m_freem(control);
11505c32ea65SRobert Watson 			m_freem(m);
11513329b236SRobert Watson 			return (EINVAL);
1152c557ae16SIan Dowse 		}
1153c557ae16SIan Dowse 		for (; control->m_len > 0;
1154c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
1155c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1156c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
1157af1ee11dSRobert Watson 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
1158af1ee11dSRobert Watson 			    || cm->cmsg_len > control->m_len) {
1159c557ae16SIan Dowse 				error = EINVAL;
1160c557ae16SIan Dowse 				break;
1161c557ae16SIan Dowse 			}
1162c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
1163c557ae16SIan Dowse 				continue;
1164c557ae16SIan Dowse 
1165c557ae16SIan Dowse 			switch (cm->cmsg_type) {
1166c557ae16SIan Dowse 			case IP_SENDSRCADDR:
1167c557ae16SIan Dowse 				if (cm->cmsg_len !=
1168c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
1169c557ae16SIan Dowse 					error = EINVAL;
1170c557ae16SIan Dowse 					break;
1171c557ae16SIan Dowse 				}
1172c557ae16SIan Dowse 				bzero(&src, sizeof(src));
1173c557ae16SIan Dowse 				src.sin_family = AF_INET;
1174c557ae16SIan Dowse 				src.sin_len = sizeof(src);
1175c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
1176af1ee11dSRobert Watson 				src.sin_addr =
1177af1ee11dSRobert Watson 				    *(struct in_addr *)CMSG_DATA(cm);
1178c557ae16SIan Dowse 				break;
1179af1ee11dSRobert Watson 
1180f584d74bSMichael Tuexen 			case IP_TOS:
1181f584d74bSMichael Tuexen 				if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
1182f584d74bSMichael Tuexen 					error = EINVAL;
1183f584d74bSMichael Tuexen 					break;
1184f584d74bSMichael Tuexen 				}
1185f584d74bSMichael Tuexen 				tos = *(u_char *)CMSG_DATA(cm);
1186f584d74bSMichael Tuexen 				break;
1187f584d74bSMichael Tuexen 
11889d3ddf43SAdrian Chadd 			case IP_FLOWID:
11899d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
11909d3ddf43SAdrian Chadd 					error = EINVAL;
11919d3ddf43SAdrian Chadd 					break;
11929d3ddf43SAdrian Chadd 				}
11939d3ddf43SAdrian Chadd 				flowid = *(uint32_t *) CMSG_DATA(cm);
11949d3ddf43SAdrian Chadd 				break;
11959d3ddf43SAdrian Chadd 
11969d3ddf43SAdrian Chadd 			case IP_FLOWTYPE:
11979d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
11989d3ddf43SAdrian Chadd 					error = EINVAL;
11999d3ddf43SAdrian Chadd 					break;
12009d3ddf43SAdrian Chadd 				}
1201c2529042SHans Petter Selasky 				flowtype = *(uint32_t *) CMSG_DATA(cm);
12029d3ddf43SAdrian Chadd 				break;
12039d3ddf43SAdrian Chadd 
12049d3ddf43SAdrian Chadd #ifdef	RSS
12059d3ddf43SAdrian Chadd 			case IP_RSSBUCKETID:
12069d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
12079d3ddf43SAdrian Chadd 					error = EINVAL;
12089d3ddf43SAdrian Chadd 					break;
12099d3ddf43SAdrian Chadd 				}
12109d3ddf43SAdrian Chadd 				/* This is just a placeholder for now */
12119d3ddf43SAdrian Chadd 				break;
12129d3ddf43SAdrian Chadd #endif	/* RSS */
1213c557ae16SIan Dowse 			default:
1214c557ae16SIan Dowse 				error = ENOPROTOOPT;
1215c557ae16SIan Dowse 				break;
1216c557ae16SIan Dowse 			}
1217c557ae16SIan Dowse 			if (error)
1218c557ae16SIan Dowse 				break;
1219c557ae16SIan Dowse 		}
1220c557ae16SIan Dowse 		m_freem(control);
1221c557ae16SIan Dowse 	}
12225c32ea65SRobert Watson 	if (error) {
122384cc0778SGeorge V. Neville-Neil 		if (unlock_inp == UH_WLOCKED)
122484cc0778SGeorge V. Neville-Neil 			INP_WUNLOCK(inp);
122584cc0778SGeorge V. Neville-Neil 		else
12260cfdff24SBjoern A. Zeeb 			INP_RUNLOCK(inp);
12275c32ea65SRobert Watson 		m_freem(m);
12283329b236SRobert Watson 		return (error);
12295c32ea65SRobert Watson 	}
12305c32ea65SRobert Watson 
123143cc0bc1SRobert Watson 	/*
123243cc0bc1SRobert Watson 	 * Depending on whether or not the application has bound or connected
1233ca528788SRobert Watson 	 * the socket, we may have to do varying levels of work.  The optimal
1234ca528788SRobert Watson 	 * case is for a connected UDP socket, as a global lock isn't
1235ca528788SRobert Watson 	 * required at all.
123643cc0bc1SRobert Watson 	 *
123743cc0bc1SRobert Watson 	 * In order to decide which we need, we require stability of the
123843cc0bc1SRobert Watson 	 * inpcb binding, which we ensure by acquiring a read lock on the
123943cc0bc1SRobert Watson 	 * inpcb.  This doesn't strictly follow the lock order, so we play
124043cc0bc1SRobert Watson 	 * the trylock and retry game; note that we may end up with more
124143cc0bc1SRobert Watson 	 * conservative locks than required the second time around, so later
124243cc0bc1SRobert Watson 	 * assertions have to accept that.  Further analysis of the number of
124343cc0bc1SRobert Watson 	 * misses under contention is required.
1244fa046d87SRobert Watson 	 *
1245fa046d87SRobert Watson 	 * XXXRW: Check that hash locking update here is correct.
124643cc0bc1SRobert Watson 	 */
1247e06e816fSKevin Lo 	pr = inp->inp_socket->so_proto->pr_protocol;
1248a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(pr);
124943cc0bc1SRobert Watson 	sin = (struct sockaddr_in *)addr;
125043cc0bc1SRobert Watson 	if (sin != NULL &&
125143cc0bc1SRobert Watson 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
1252e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1253fa046d87SRobert Watson 		unlock_udbinfo = UH_WLOCKED;
125443cc0bc1SRobert Watson 	} else if ((sin != NULL && (
125543cc0bc1SRobert Watson 	    (sin->sin_addr.s_addr == INADDR_ANY) ||
125643cc0bc1SRobert Watson 	    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
125743cc0bc1SRobert Watson 	    (inp->inp_laddr.s_addr == INADDR_ANY) ||
125843cc0bc1SRobert Watson 	    (inp->inp_lport == 0))) ||
125943cc0bc1SRobert Watson 	    (src.sin_family == AF_INET)) {
1260e06e816fSKevin Lo 		INP_HASH_RLOCK(pcbinfo);
1261fa046d87SRobert Watson 		unlock_udbinfo = UH_RLOCKED;
126243cc0bc1SRobert Watson 	} else
1263fa046d87SRobert Watson 		unlock_udbinfo = UH_UNLOCKED;
12645c32ea65SRobert Watson 
12651b7f0384SBruce M Simpson 	/*
12661b7f0384SBruce M Simpson 	 * If the IP_SENDSRCADDR control message was specified, override the
12671b7f0384SBruce M Simpson 	 * source address for this datagram.  Its use is invalidated if the
12681b7f0384SBruce M Simpson 	 * address thus specified is incomplete or clobbers other inpcbs.
12691b7f0384SBruce M Simpson 	 */
127090162a4eSIan Dowse 	laddr = inp->inp_laddr;
127190162a4eSIan Dowse 	lport = inp->inp_lport;
12721b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET) {
1273e06e816fSKevin Lo 		INP_HASH_LOCK_ASSERT(pcbinfo);
12741b7f0384SBruce M Simpson 		if ((lport == 0) ||
12751b7f0384SBruce M Simpson 		    (laddr.s_addr == INADDR_ANY &&
12761b7f0384SBruce M Simpson 		     src.sin_addr.s_addr == INADDR_ANY)) {
1277c557ae16SIan Dowse 			error = EINVAL;
1278c557ae16SIan Dowse 			goto release;
1279c557ae16SIan Dowse 		}
1280c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
1281b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
1282c557ae16SIan Dowse 		if (error)
1283c557ae16SIan Dowse 			goto release;
1284c557ae16SIan Dowse 	}
1285c557ae16SIan Dowse 
12863144b7d3SRobert Watson 	/*
12873144b7d3SRobert Watson 	 * If a UDP socket has been connected, then a local address/port will
12883144b7d3SRobert Watson 	 * have been selected and bound.
12893144b7d3SRobert Watson 	 *
129043cc0bc1SRobert Watson 	 * If a UDP socket has not been connected to, then an explicit
12913144b7d3SRobert Watson 	 * destination address must be used, in which case a local
12923144b7d3SRobert Watson 	 * address/port may not have been selected and bound.
12933144b7d3SRobert Watson 	 */
129443cc0bc1SRobert Watson 	if (sin != NULL) {
1295c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
1296df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1297df8bae1dSRodney W. Grimes 			error = EISCONN;
1298df8bae1dSRodney W. Grimes 			goto release;
1299df8bae1dSRodney W. Grimes 		}
13003144b7d3SRobert Watson 
13013144b7d3SRobert Watson 		/*
13023144b7d3SRobert Watson 		 * Jail may rewrite the destination address, so let it do
13033144b7d3SRobert Watson 		 * that before we use it.
13043144b7d3SRobert Watson 		 */
1305b89e82ddSJamie Gritton 		error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1306b89e82ddSJamie Gritton 		if (error)
1307413628a7SBjoern A. Zeeb 			goto release;
13083144b7d3SRobert Watson 
13093144b7d3SRobert Watson 		/*
131043cc0bc1SRobert Watson 		 * If a local address or port hasn't yet been selected, or if
131143cc0bc1SRobert Watson 		 * the destination address needs to be rewritten due to using
131243cc0bc1SRobert Watson 		 * a special INADDR_ constant, invoke in_pcbconnect_setup()
131343cc0bc1SRobert Watson 		 * to do the heavy lifting.  Once a port is selected, we
131443cc0bc1SRobert Watson 		 * commit the binding back to the socket; we also commit the
131543cc0bc1SRobert Watson 		 * binding of the address if in jail.
131643cc0bc1SRobert Watson 		 *
131743cc0bc1SRobert Watson 		 * If we already have a valid binding and we're not
131843cc0bc1SRobert Watson 		 * requesting a destination address rewrite, use a fast path.
13193144b7d3SRobert Watson 		 */
132043cc0bc1SRobert Watson 		if (inp->inp_laddr.s_addr == INADDR_ANY ||
132143cc0bc1SRobert Watson 		    inp->inp_lport == 0 ||
132243cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_ANY ||
132343cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_BROADCAST) {
1324e06e816fSKevin Lo 			INP_HASH_LOCK_ASSERT(pcbinfo);
132543cc0bc1SRobert Watson 			error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
132643cc0bc1SRobert Watson 			    &lport, &faddr.s_addr, &fport, NULL,
132743cc0bc1SRobert Watson 			    td->td_ucred);
132890162a4eSIan Dowse 			if (error)
132990162a4eSIan Dowse 				goto release;
133090162a4eSIan Dowse 
133143cc0bc1SRobert Watson 			/*
133243cc0bc1SRobert Watson 			 * XXXRW: Why not commit the port if the address is
133343cc0bc1SRobert Watson 			 * !INADDR_ANY?
133443cc0bc1SRobert Watson 			 */
133590162a4eSIan Dowse 			/* Commit the local port if newly assigned. */
133690162a4eSIan Dowse 			if (inp->inp_laddr.s_addr == INADDR_ANY &&
133790162a4eSIan Dowse 			    inp->inp_lport == 0) {
1338c4d585aeSRobert Watson 				INP_WLOCK_ASSERT(inp);
1339e06e816fSKevin Lo 				INP_HASH_WLOCK_ASSERT(pcbinfo);
13403a1757b9SGleb Smirnoff 				/*
134143cc0bc1SRobert Watson 				 * Remember addr if jailed, to prevent
134243cc0bc1SRobert Watson 				 * rebinding.
13433a1757b9SGleb Smirnoff 				 */
13440304c731SJamie Gritton 				if (prison_flag(td->td_ucred, PR_IP4))
13453a1757b9SGleb Smirnoff 					inp->inp_laddr = laddr;
134690162a4eSIan Dowse 				inp->inp_lport = lport;
134790162a4eSIan Dowse 				if (in_pcbinshash(inp) != 0) {
134890162a4eSIan Dowse 					inp->inp_lport = 0;
134990162a4eSIan Dowse 					error = EAGAIN;
1350df8bae1dSRodney W. Grimes 					goto release;
1351df8bae1dSRodney W. Grimes 				}
135290162a4eSIan Dowse 				inp->inp_flags |= INP_ANONPORT;
135390162a4eSIan Dowse 			}
1354df8bae1dSRodney W. Grimes 		} else {
135543cc0bc1SRobert Watson 			faddr = sin->sin_addr;
135643cc0bc1SRobert Watson 			fport = sin->sin_port;
135743cc0bc1SRobert Watson 		}
135843cc0bc1SRobert Watson 	} else {
1359c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
136090162a4eSIan Dowse 		faddr = inp->inp_faddr;
136190162a4eSIan Dowse 		fport = inp->inp_fport;
136290162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
1363df8bae1dSRodney W. Grimes 			error = ENOTCONN;
1364df8bae1dSRodney W. Grimes 			goto release;
1365df8bae1dSRodney W. Grimes 		}
1366df8bae1dSRodney W. Grimes 	}
1367e6ccd709SRobert Watson 
1368df8bae1dSRodney W. Grimes 	/*
1369e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
1370392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
1371392e8407SRobert Watson 	 * since we won't use that space at this layer.
1372df8bae1dSRodney W. Grimes 	 */
1373eb1b1807SGleb Smirnoff 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1374e6ccd709SRobert Watson 	if (m == NULL) {
1375df8bae1dSRodney W. Grimes 		error = ENOBUFS;
137649b19bfcSBruce M Simpson 		goto release;
1377df8bae1dSRodney W. Grimes 	}
1378e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
1379e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
1380392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
1381df8bae1dSRodney W. Grimes 
1382df8bae1dSRodney W. Grimes 	/*
13833329b236SRobert Watson 	 * Fill in mbuf with extended UDP header and addresses and length put
13843329b236SRobert Watson 	 * into network format.
1385df8bae1dSRodney W. Grimes 	 */
1386df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
1387db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
1388e06e816fSKevin Lo 	ui->ui_pr = pr;
138990162a4eSIan Dowse 	ui->ui_src = laddr;
139090162a4eSIan Dowse 	ui->ui_dst = faddr;
139190162a4eSIan Dowse 	ui->ui_sport = lport;
139290162a4eSIan Dowse 	ui->ui_dport = fport;
1393db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1394e06e816fSKevin Lo 	if (pr == IPPROTO_UDPLITE) {
1395e06e816fSKevin Lo 		struct udpcb *up;
1396e06e816fSKevin Lo 		uint16_t plen;
1397e06e816fSKevin Lo 
1398e06e816fSKevin Lo 		up = intoudpcb(inp);
1399e06e816fSKevin Lo 		cscov = up->u_txcslen;
1400e06e816fSKevin Lo 		plen = (u_short)len + sizeof(struct udphdr);
1401e06e816fSKevin Lo 		if (cscov >= plen)
1402e06e816fSKevin Lo 			cscov = 0;
1403e06e816fSKevin Lo 		ui->ui_len = htons(plen);
1404e06e816fSKevin Lo 		ui->ui_ulen = htons(cscov);
1405e06e816fSKevin Lo 		/*
1406e06e816fSKevin Lo 		 * For UDP-Lite, checksum coverage length of zero means
1407e06e816fSKevin Lo 		 * the entire UDPLite packet is covered by the checksum.
1408e06e816fSKevin Lo 		 */
1409e06e816fSKevin Lo 		cscov_partial = (cscov == 0) ? 0 : 1;
1410e06e816fSKevin Lo 	} else
1411e06e816fSKevin Lo 		ui->ui_v = IPVERSION << 4;
1412df8bae1dSRodney W. Grimes 
1413b2828ad2SAndre Oppermann 	/*
1414b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
1415b2828ad2SAndre Oppermann 	 */
1416b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
1417b2828ad2SAndre Oppermann 		struct ip *ip;
14183329b236SRobert Watson 
1419b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
14208f134647SGleb Smirnoff 		ip->ip_off |= htons(IP_DF);
1421b2828ad2SAndre Oppermann 	}
1422b2828ad2SAndre Oppermann 
1423b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
1424b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
1425b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
1426b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
1427b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
14286fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
14298afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
14308afa2304SBruce M Simpson 
14311175d9d5SRobert Watson #ifdef MAC
14321175d9d5SRobert Watson 	mac_inpcb_create_mbuf(inp, m);
14331175d9d5SRobert Watson #endif
14341175d9d5SRobert Watson 
1435df8bae1dSRodney W. Grimes 	/*
1436db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
1437df8bae1dSRodney W. Grimes 	 */
1438e06e816fSKevin Lo 	ui->ui_sum = 0;
1439a485f139SMichael Tuexen 	if (pr == IPPROTO_UDPLITE) {
1440e06e816fSKevin Lo 		if (inp->inp_flags & INP_ONESBCAST)
1441e06e816fSKevin Lo 			faddr.s_addr = INADDR_BROADCAST;
1442a485f139SMichael Tuexen 		if (cscov_partial) {
1443e06e816fSKevin Lo 			if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1444e06e816fSKevin Lo 				ui->ui_sum = 0xffff;
1445a485f139SMichael Tuexen 		} else {
1446a485f139SMichael Tuexen 			if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1447a485f139SMichael Tuexen 				ui->ui_sum = 0xffff;
1448a485f139SMichael Tuexen 		}
1449a485f139SMichael Tuexen 	} else if (V_udp_cksum) {
14506fbfd582SAndre Oppermann 		if (inp->inp_flags & INP_ONESBCAST)
14518a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
14528a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1453e06e816fSKevin Lo 		    htons((u_short)len + sizeof(struct udphdr) + pr));
1454db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
1455db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1456e06e816fSKevin Lo 	}
14578f134647SGleb Smirnoff 	((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1458ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
1459f584d74bSMichael Tuexen 	((struct ip *)ui)->ip_tos = tos;		/* XXX */
1460026decb8SRobert Watson 	UDPSTAT_INC(udps_opackets);
1461cfa1ca9dSYoshinobu Inoue 
14629d3ddf43SAdrian Chadd 	/*
14639d3ddf43SAdrian Chadd 	 * Setup flowid / RSS information for outbound socket.
14649d3ddf43SAdrian Chadd 	 *
14659d3ddf43SAdrian Chadd 	 * Once the UDP code decides to set a flowid some other way,
14669d3ddf43SAdrian Chadd 	 * this allows the flowid to be overridden by userland.
14679d3ddf43SAdrian Chadd 	 */
1468c2529042SHans Petter Selasky 	if (flowtype != M_HASHTYPE_NONE) {
14699d3ddf43SAdrian Chadd 		m->m_pkthdr.flowid = flowid;
1470c2529042SHans Petter Selasky 		M_HASHTYPE_SET(m, flowtype);
14718ad1a83bSAdrian Chadd #ifdef	RSS
14728ad1a83bSAdrian Chadd 	} else {
14738ad1a83bSAdrian Chadd 		uint32_t hash_val, hash_type;
14748ad1a83bSAdrian Chadd 		/*
14758ad1a83bSAdrian Chadd 		 * Calculate an appropriate RSS hash for UDP and
14768ad1a83bSAdrian Chadd 		 * UDP Lite.
14778ad1a83bSAdrian Chadd 		 *
14788ad1a83bSAdrian Chadd 		 * The called function will take care of figuring out
14798ad1a83bSAdrian Chadd 		 * whether a 2-tuple or 4-tuple hash is required based
14808ad1a83bSAdrian Chadd 		 * on the currently configured scheme.
14818ad1a83bSAdrian Chadd 		 *
14828ad1a83bSAdrian Chadd 		 * Later later on connected socket values should be
14838ad1a83bSAdrian Chadd 		 * cached in the inpcb and reused, rather than constantly
14848ad1a83bSAdrian Chadd 		 * re-calculating it.
14858ad1a83bSAdrian Chadd 		 *
14868ad1a83bSAdrian Chadd 		 * UDP Lite is a different protocol number and will
14878ad1a83bSAdrian Chadd 		 * likely end up being hashed as a 2-tuple until
14888ad1a83bSAdrian Chadd 		 * RSS / NICs grow UDP Lite protocol awareness.
14898ad1a83bSAdrian Chadd 		 */
14908ad1a83bSAdrian Chadd 		if (rss_proto_software_hash_v4(faddr, laddr, fport, lport,
14918ad1a83bSAdrian Chadd 		    pr, &hash_val, &hash_type) == 0) {
14928ad1a83bSAdrian Chadd 			m->m_pkthdr.flowid = hash_val;
14938ad1a83bSAdrian Chadd 			M_HASHTYPE_SET(m, hash_type);
14948ad1a83bSAdrian Chadd 		}
14958ad1a83bSAdrian Chadd #endif
14969d3ddf43SAdrian Chadd 	}
14979d3ddf43SAdrian Chadd 
14989d3ddf43SAdrian Chadd #ifdef	RSS
14998ad1a83bSAdrian Chadd 	/*
15008ad1a83bSAdrian Chadd 	 * Don't override with the inp cached flowid value.
15018ad1a83bSAdrian Chadd 	 *
15028ad1a83bSAdrian Chadd 	 * Depending upon the kind of send being done, the inp
15038ad1a83bSAdrian Chadd 	 * flowid/flowtype values may actually not be appropriate
15048ad1a83bSAdrian Chadd 	 * for this particular socket send.
15058ad1a83bSAdrian Chadd 	 *
15068ad1a83bSAdrian Chadd 	 * We should either leave the flowid at zero (which is what is
15078ad1a83bSAdrian Chadd 	 * currently done) or set it to some software generated
15088ad1a83bSAdrian Chadd 	 * hash value based on the packet contents.
15098ad1a83bSAdrian Chadd 	 */
15109d3ddf43SAdrian Chadd 	ipflags |= IP_NODEFAULTFLOWID;
15119d3ddf43SAdrian Chadd #endif	/* RSS */
15129d3ddf43SAdrian Chadd 
1513fa046d87SRobert Watson 	if (unlock_udbinfo == UH_WLOCKED)
1514e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1515fa046d87SRobert Watson 	else if (unlock_udbinfo == UH_RLOCKED)
1516e06e816fSKevin Lo 		INP_HASH_RUNLOCK(pcbinfo);
151757f60867SMark Johnston 	UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
151884cc0778SGeorge V. Neville-Neil 	error = ip_output(m, inp->inp_options,
151984cc0778SGeorge V. Neville-Neil 	    (unlock_inp == UH_WLOCKED ? &inp->inp_route : NULL), ipflags,
15205d846453SSam Leffler 	    inp->inp_moptions, inp);
152184cc0778SGeorge V. Neville-Neil 	if (unlock_inp == UH_WLOCKED)
15228501a69cSRobert Watson 		INP_WUNLOCK(inp);
1523948d0fc9SRobert Watson 	else
1524948d0fc9SRobert Watson 		INP_RUNLOCK(inp);
1525df8bae1dSRodney W. Grimes 	return (error);
1526df8bae1dSRodney W. Grimes 
1527df8bae1dSRodney W. Grimes release:
1528fa046d87SRobert Watson 	if (unlock_udbinfo == UH_WLOCKED) {
152900b460ffSRick Macklem 		KASSERT(unlock_inp == UH_WLOCKED,
153000b460ffSRick Macklem 		    ("%s: excl udbinfo lock, shared inp lock", __func__));
1531e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1532948d0fc9SRobert Watson 		INP_WUNLOCK(inp);
1533fa046d87SRobert Watson 	} else if (unlock_udbinfo == UH_RLOCKED) {
153400b460ffSRick Macklem 		KASSERT(unlock_inp == UH_RLOCKED,
153500b460ffSRick Macklem 		    ("%s: shared udbinfo lock, excl inp lock", __func__));
1536e06e816fSKevin Lo 		INP_HASH_RUNLOCK(pcbinfo);
153743cc0bc1SRobert Watson 		INP_RUNLOCK(inp);
153800b460ffSRick Macklem 	} else if (unlock_inp == UH_WLOCKED)
153900b460ffSRick Macklem 		INP_WUNLOCK(inp);
154000b460ffSRick Macklem 	else
1541948d0fc9SRobert Watson 		INP_RUNLOCK(inp);
1542df8bae1dSRodney W. Grimes 	m_freem(m);
1543df8bae1dSRodney W. Grimes 	return (error);
1544df8bae1dSRodney W. Grimes }
1545df8bae1dSRodney W. Grimes 
1546ac45e92fSRobert Watson static void
1547d0390e05SGarrett Wollman udp_abort(struct socket *so)
1548df8bae1dSRodney W. Grimes {
1549d0390e05SGarrett Wollman 	struct inpcb *inp;
1550e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1551df8bae1dSRodney W. Grimes 
1552a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1553d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
155414ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
15558501a69cSRobert Watson 	INP_WLOCK(inp);
1556a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1557e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1558a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1559a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1560e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1561d0390e05SGarrett Wollman 		soisdisconnected(so);
1562a152f8a3SRobert Watson 	}
15638501a69cSRobert Watson 	INP_WUNLOCK(inp);
1564df8bae1dSRodney W. Grimes }
1565df8bae1dSRodney W. Grimes 
1566d0390e05SGarrett Wollman static int
1567b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
1568d0390e05SGarrett Wollman {
1569630ba2c5SMatt Macy 	static uint32_t udp_flowid;
1570d0390e05SGarrett Wollman 	struct inpcb *inp;
1571e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1572277afaffSRobert Watson 	int error;
1573d0390e05SGarrett Wollman 
1574a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1575d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
157614ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1577cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
1578f24618aaSRobert Watson 	if (error)
15793329b236SRobert Watson 		return (error);
1580e06e816fSKevin Lo 	INP_INFO_WLOCK(pcbinfo);
1581e06e816fSKevin Lo 	error = in_pcballoc(so, pcbinfo);
158253b57cd1SSam Leffler 	if (error) {
1583e06e816fSKevin Lo 		INP_INFO_WUNLOCK(pcbinfo);
15843329b236SRobert Watson 		return (error);
158553b57cd1SSam Leffler 	}
1586cfa1ca9dSYoshinobu Inoue 
158768b5629bSRobert Watson 	inp = sotoinpcb(so);
1588cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1589603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
1590630ba2c5SMatt Macy 	inp->inp_flowid = atomic_fetchadd_int(&udp_flowid, 1);
1591630ba2c5SMatt Macy 	inp->inp_flowtype = M_HASHTYPE_OPAQUE;
15926a9148feSBjoern A. Zeeb 
15936a9148feSBjoern A. Zeeb 	error = udp_newudpcb(inp);
15946a9148feSBjoern A. Zeeb 	if (error) {
15956a9148feSBjoern A. Zeeb 		in_pcbdetach(inp);
15966a9148feSBjoern A. Zeeb 		in_pcbfree(inp);
1597e06e816fSKevin Lo 		INP_INFO_WUNLOCK(pcbinfo);
15986a9148feSBjoern A. Zeeb 		return (error);
15996a9148feSBjoern A. Zeeb 	}
16006a9148feSBjoern A. Zeeb 
1601c7c7ea4bSRandall Stewart 	INP_WUNLOCK(inp);
1602e06e816fSKevin Lo 	INP_INFO_WUNLOCK(pcbinfo);
1603c7c7ea4bSRandall Stewart 	return (0);
1604c7c7ea4bSRandall Stewart }
160579288c11SBjoern A. Zeeb #endif /* INET */
1606c7c7ea4bSRandall Stewart 
1607c7c7ea4bSRandall Stewart int
1608abb901c5SRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx)
1609c7c7ea4bSRandall Stewart {
1610c7c7ea4bSRandall Stewart 	struct inpcb *inp;
16116a9148feSBjoern A. Zeeb 	struct udpcb *up;
1612c7c7ea4bSRandall Stewart 
161368b5629bSRobert Watson 	KASSERT(so->so_type == SOCK_DGRAM,
161468b5629bSRobert Watson 	    ("udp_set_kernel_tunneling: !dgram"));
161568b5629bSRobert Watson 	inp = sotoinpcb(so);
161668b5629bSRobert Watson 	KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1617c7c7ea4bSRandall Stewart 	INP_WLOCK(inp);
16186a9148feSBjoern A. Zeeb 	up = intoudpcb(inp);
1619abb901c5SRandall Stewart 	if ((up->u_tun_func != NULL) ||
1620abb901c5SRandall Stewart 	    (up->u_icmp_func != NULL)) {
1621bbb0e3d9SRandall Stewart 		INP_WUNLOCK(inp);
1622bbb0e3d9SRandall Stewart 		return (EBUSY);
1623bbb0e3d9SRandall Stewart 	}
16246a9148feSBjoern A. Zeeb 	up->u_tun_func = f;
1625abb901c5SRandall Stewart 	up->u_icmp_func = i;
162681d3ec17SBryan Venteicher 	up->u_tun_ctx = ctx;
16278501a69cSRobert Watson 	INP_WUNLOCK(inp);
16283329b236SRobert Watson 	return (0);
1629df8bae1dSRodney W. Grimes }
1630d0390e05SGarrett Wollman 
163179288c11SBjoern A. Zeeb #ifdef INET
1632d0390e05SGarrett Wollman static int
1633b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1634d0390e05SGarrett Wollman {
1635d0390e05SGarrett Wollman 	struct inpcb *inp;
1636e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1637277afaffSRobert Watson 	int error;
1638d0390e05SGarrett Wollman 
1639a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1640d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
164114ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
16428501a69cSRobert Watson 	INP_WLOCK(inp);
1643e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1644b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
1645e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
16468501a69cSRobert Watson 	INP_WUNLOCK(inp);
16473329b236SRobert Watson 	return (error);
1648d0390e05SGarrett Wollman }
1649d0390e05SGarrett Wollman 
1650a152f8a3SRobert Watson static void
1651a152f8a3SRobert Watson udp_close(struct socket *so)
1652a152f8a3SRobert Watson {
1653a152f8a3SRobert Watson 	struct inpcb *inp;
1654e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1655a152f8a3SRobert Watson 
1656a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1657a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1658a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
16598501a69cSRobert Watson 	INP_WLOCK(inp);
1660a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1661e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1662a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1663a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1664e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1665a152f8a3SRobert Watson 		soisdisconnected(so);
1666a152f8a3SRobert Watson 	}
16678501a69cSRobert Watson 	INP_WUNLOCK(inp);
1668a152f8a3SRobert Watson }
1669a152f8a3SRobert Watson 
1670d0390e05SGarrett Wollman static int
1671b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1672d0390e05SGarrett Wollman {
1673d0390e05SGarrett Wollman 	struct inpcb *inp;
1674e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
167575c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1676e06e816fSKevin Lo 	int error;
1677d0390e05SGarrett Wollman 
1678a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1679d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
168014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
16818501a69cSRobert Watson 	INP_WLOCK(inp);
1682f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
16838501a69cSRobert Watson 		INP_WUNLOCK(inp);
16843329b236SRobert Watson 		return (EISCONN);
1685f76fcf6dSJeffrey Hsu 	}
168675c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1687b89e82ddSJamie Gritton 	error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1688b89e82ddSJamie Gritton 	if (error != 0) {
1689413628a7SBjoern A. Zeeb 		INP_WUNLOCK(inp);
1690b89e82ddSJamie Gritton 		return (error);
1691413628a7SBjoern A. Zeeb 	}
1692e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1693b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
1694e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
16954cc20ab1SSeigo Tanimura 	if (error == 0)
1696df8bae1dSRodney W. Grimes 		soisconnected(so);
16978501a69cSRobert Watson 	INP_WUNLOCK(inp);
16983329b236SRobert Watson 	return (error);
1699df8bae1dSRodney W. Grimes }
1700d0390e05SGarrett Wollman 
1701bc725eafSRobert Watson static void
1702d0390e05SGarrett Wollman udp_detach(struct socket *so)
1703d0390e05SGarrett Wollman {
1704d0390e05SGarrett Wollman 	struct inpcb *inp;
1705e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
17066a9148feSBjoern A. Zeeb 	struct udpcb *up;
1707d0390e05SGarrett Wollman 
1708a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1709d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
171014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1711a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1712a152f8a3SRobert Watson 	    ("udp_detach: not disconnected"));
1713e06e816fSKevin Lo 	INP_INFO_WLOCK(pcbinfo);
17148501a69cSRobert Watson 	INP_WLOCK(inp);
17156a9148feSBjoern A. Zeeb 	up = intoudpcb(inp);
17166a9148feSBjoern A. Zeeb 	KASSERT(up != NULL, ("%s: up == NULL", __func__));
1717056b40e2SMatt Macy 	/* XXX defer to epoch_call */
17186a9148feSBjoern A. Zeeb 	inp->inp_ppcb = NULL;
1719d0390e05SGarrett Wollman 	in_pcbdetach(inp);
172014ba8addSRobert Watson 	in_pcbfree(inp);
1721e06e816fSKevin Lo 	INP_INFO_WUNLOCK(pcbinfo);
17226a9148feSBjoern A. Zeeb 	udp_discardcb(up);
1723d0390e05SGarrett Wollman }
1724d0390e05SGarrett Wollman 
1725d0390e05SGarrett Wollman static int
1726d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1727d0390e05SGarrett Wollman {
1728d0390e05SGarrett Wollman 	struct inpcb *inp;
1729e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1730d0390e05SGarrett Wollman 
1731a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1732d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
173314ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
17348501a69cSRobert Watson 	INP_WLOCK(inp);
1735f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
17368501a69cSRobert Watson 		INP_WUNLOCK(inp);
17373329b236SRobert Watson 		return (ENOTCONN);
1738f76fcf6dSJeffrey Hsu 	}
1739e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1740df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1741df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1742e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
1743d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1744d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1745d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
17468501a69cSRobert Watson 	INP_WUNLOCK(inp);
17473329b236SRobert Watson 	return (0);
1748df8bae1dSRodney W. Grimes }
1749df8bae1dSRodney W. Grimes 
1750d0390e05SGarrett Wollman static int
175157bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1752b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1753d0390e05SGarrett Wollman {
1754d0390e05SGarrett Wollman 	struct inpcb *inp;
1755d0390e05SGarrett Wollman 
1756d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
175714ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
17583329b236SRobert Watson 	return (udp_output(inp, m, addr, control, td));
1759d0390e05SGarrett Wollman }
176079288c11SBjoern A. Zeeb #endif /* INET */
1761d0390e05SGarrett Wollman 
176276429de4SYoshinobu Inoue int
1763d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1764d0390e05SGarrett Wollman {
1765d0390e05SGarrett Wollman 	struct inpcb *inp;
1766d0390e05SGarrett Wollman 
1767d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
176814ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
17698501a69cSRobert Watson 	INP_WLOCK(inp);
1770d0390e05SGarrett Wollman 	socantsendmore(so);
17718501a69cSRobert Watson 	INP_WUNLOCK(inp);
17723329b236SRobert Watson 	return (0);
1773d0390e05SGarrett Wollman }
1774d0390e05SGarrett Wollman 
177579288c11SBjoern A. Zeeb #ifdef INET
1776d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1777756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1778756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1779756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1780756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1781756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1782756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1783756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
178454d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1785756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
17865df3e839SRobert Watson 	.pru_soreceive =	soreceive_dgram,
178759b8854eSRobert Watson 	.pru_sosend =		sosend_dgram,
1788756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
178954d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1790a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1791a152f8a3SRobert Watson 	.pru_close =		udp_close,
1792d0390e05SGarrett Wollman };
179379288c11SBjoern A. Zeeb #endif /* INET */
1794