xref: /freebsd/sys/netinet/udp_usrreq.c (revision abb901c5d78fb904202a4b98f2996c3ad9afd542)
1c398230bSWarner Losh /*-
26dfab5b1SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
33329b236SRobert Watson  *	The Regents of the University of California.
43144b7d3SRobert Watson  * Copyright (c) 2008 Robert N. M. Watson
5fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6e06e816fSKevin Lo  * Copyright (c) 2014 Kevin Lo
73329b236SRobert Watson  * All rights reserved.
8df8bae1dSRodney W. Grimes  *
9fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
10fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
11fa046d87SRobert Watson  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
366dfab5b1SGarrett Wollman  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
394b421e2dSMike Silbersack #include <sys/cdefs.h>
404b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
414b421e2dSMike Silbersack 
420b4ae859SGleb Smirnoff #include "opt_ipfw.h"
4379288c11SBjoern A. Zeeb #include "opt_inet.h"
44cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
45f5514f08SRobert Watson #include "opt_ipsec.h"
469d3ddf43SAdrian Chadd #include "opt_rss.h"
47cfa1ca9dSYoshinobu Inoue 
48df8bae1dSRodney W. Grimes #include <sys/param.h>
49960ed29cSSeigo Tanimura #include <sys/domain.h>
504f590175SPaul Saab #include <sys/eventhandler.h>
51960ed29cSSeigo Tanimura #include <sys/jail.h>
52b110a8a2SGarrett Wollman #include <sys/kernel.h>
53960ed29cSSeigo Tanimura #include <sys/lock.h>
54df8bae1dSRodney W. Grimes #include <sys/malloc.h>
55df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
56acd3428bSRobert Watson #include <sys/priv.h>
57490d50b6SBrian Feldman #include <sys/proc.h>
58df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5957f60867SMark Johnston #include <sys/sdt.h>
60960ed29cSSeigo Tanimura #include <sys/signalvar.h>
61df8bae1dSRodney W. Grimes #include <sys/socket.h>
62df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
63960ed29cSSeigo Tanimura #include <sys/sx.h>
64b5e8ce9fSBruce Evans #include <sys/sysctl.h>
65816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
66f5514f08SRobert Watson #include <sys/systm.h>
678781d8e9SBruce Evans 
6869c2d429SJeff Roberson #include <vm/uma.h>
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <net/if.h>
7176039bc8SGleb Smirnoff #include <net/if_var.h>
72df8bae1dSRodney W. Grimes #include <net/route.h>
73b2bdc62aSAdrian Chadd #include <net/rss_config.h>
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes #include <netinet/in.h>
7657f60867SMark Johnston #include <netinet/in_kdtrace.h>
77960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
78f5514f08SRobert Watson #include <netinet/in_systm.h>
79960ed29cSSeigo Tanimura #include <netinet/in_var.h>
80df8bae1dSRodney W. Grimes #include <netinet/ip.h>
81cfa1ca9dSYoshinobu Inoue #ifdef INET6
82cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
83cfa1ca9dSYoshinobu Inoue #endif
84960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
85960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
86df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
87ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
88cfa1ca9dSYoshinobu Inoue #ifdef INET6
89cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
90cfa1ca9dSYoshinobu Inoue #endif
91df8bae1dSRodney W. Grimes #include <netinet/udp.h>
92df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
93e06e816fSKevin Lo #include <netinet/udplite.h>
948ad1a83bSAdrian Chadd #include <netinet/in_rss.h>
95df8bae1dSRodney W. Grimes 
96b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
97b9234fafSSam Leffler #include <netipsec/ipsec.h>
987b495c44SVANHULLEBUS Yvan #include <netipsec/esp.h>
993329b236SRobert Watson #endif
100b9234fafSSam Leffler 
101db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
102db4f9cc7SJonathan Lemon 
103aed55708SRobert Watson #include <security/mac/mac_framework.h>
104aed55708SRobert Watson 
105df8bae1dSRodney W. Grimes /*
106e06e816fSKevin Lo  * UDP and UDP-Lite protocols implementation.
107df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
108e06e816fSKevin Lo  * Per RFC 3828, July, 2004.
109df8bae1dSRodney W. Grimes  */
11074eb3236SWarner Losh 
11174eb3236SWarner Losh /*
1123329b236SRobert Watson  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
1133329b236SRobert Watson  * removes the only data integrity mechanism for packets and malformed
114f5514f08SRobert Watson  * packets that would otherwise be discarded due to bad checksums, and may
115f5514f08SRobert Watson  * cause problems (especially for NFS data blocks).
11674eb3236SWarner Losh  */
11740b676beSBjoern A. Zeeb VNET_DEFINE(int, udp_cksum) = 1;
1186df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW,
11940b676beSBjoern A. Zeeb     &VNET_NAME(udp_cksum), 0, "compute udp checksum");
120df8bae1dSRodney W. Grimes 
121afdb4274SRobert Watson int	udp_log_in_vain = 0;
122816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
123afdb4274SRobert Watson     &udp_log_in_vain, 0, "Log all incoming UDP packets");
124816a3d83SPoul-Henning Kamp 
12582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, udp_blackhole) = 0;
1266df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
127eddfbb76SRobert Watson     &VNET_NAME(udp_blackhole), 0,
1283329b236SRobert Watson     "Do not send port unreachables for refused connects");
12916f7f31fSGeoff Rehmet 
13043bbb6aaSRobert Watson u_long	udp_sendspace = 9216;		/* really max datagram size */
13143bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
13243bbb6aaSRobert Watson     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
13343bbb6aaSRobert Watson 
13443bbb6aaSRobert Watson u_long	udp_recvspace = 40 * (1024 +
13543bbb6aaSRobert Watson #ifdef INET6
13643bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in6)
13743bbb6aaSRobert Watson #else
13843bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in)
13943bbb6aaSRobert Watson #endif
140e62b9bcaSSergey Kandaurov 				      );	/* 40 1K datagrams */
14143bbb6aaSRobert Watson 
14243bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14343bbb6aaSRobert Watson     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
14443bbb6aaSRobert Watson 
145eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, udb);		/* from udp_var.h */
146eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, udbinfo);
147e06e816fSKevin Lo VNET_DEFINE(struct inpcbhead, ulitecb);
148e06e816fSKevin Lo VNET_DEFINE(struct inpcbinfo, ulitecbinfo);
1493e288e62SDimitry Andric static VNET_DEFINE(uma_zone_t, udpcb_zone);
1501e77c105SRobert Watson #define	V_udpcb_zone			VNET(udpcb_zone)
15115bd2b43SDavid Greenman 
15215bd2b43SDavid Greenman #ifndef UDBHASHSIZE
153e2ed8f35SAlexander Motin #define	UDBHASHSIZE	128
15415bd2b43SDavid Greenman #endif
15515bd2b43SDavid Greenman 
1565b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat);		/* from udp_var.h */
1575b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(udpstat);
1585b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat,
1595b7cb97cSAndrey V. Elsukov     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
160f2ea20e6SGarrett Wollman 
1615b7cb97cSAndrey V. Elsukov #ifdef VIMAGE
1625b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(udpstat);
1635b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */
16479288c11SBjoern A. Zeeb #ifdef INET
165bc725eafSRobert Watson static void	udp_detach(struct socket *so);
1664d77a549SAlfred Perlstein static int	udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1674d77a549SAlfred Perlstein 		    struct mbuf *, struct thread *);
16879288c11SBjoern A. Zeeb #endif
16979288c11SBjoern A. Zeeb 
1707b495c44SVANHULLEBUS Yvan #ifdef IPSEC
1717b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
1727b495c44SVANHULLEBUS Yvan #define	UF_ESPINUDP_ALL	(UF_ESPINUDP_NON_IKE|UF_ESPINUDP)
1737b495c44SVANHULLEBUS Yvan #ifdef INET
1747b495c44SVANHULLEBUS Yvan static struct mbuf *udp4_espdecap(struct inpcb *, struct mbuf *, int);
1757b495c44SVANHULLEBUS Yvan #endif
1767b495c44SVANHULLEBUS Yvan #endif /* IPSEC_NAT_T */
1777b495c44SVANHULLEBUS Yvan #endif /* IPSEC */
178df8bae1dSRodney W. Grimes 
1794f590175SPaul Saab static void
1804f590175SPaul Saab udp_zone_change(void *tag)
1814f590175SPaul Saab {
1824f590175SPaul Saab 
183603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
1846a9148feSBjoern A. Zeeb 	uma_zone_set_max(V_udpcb_zone, maxsockets);
1854f590175SPaul Saab }
1864f590175SPaul Saab 
187d915b280SStephan Uphoff static int
188d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags)
189d915b280SStephan Uphoff {
190af1ee11dSRobert Watson 	struct inpcb *inp;
19108651e1fSJohn Baldwin 
192af1ee11dSRobert Watson 	inp = mem;
193d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "udpinp");
194d915b280SStephan Uphoff 	return (0);
195d915b280SStephan Uphoff }
196d915b280SStephan Uphoff 
197e06e816fSKevin Lo static int
198e06e816fSKevin Lo udplite_inpcb_init(void *mem, int size, int flags)
199e06e816fSKevin Lo {
200e06e816fSKevin Lo 	struct inpcb *inp;
201e06e816fSKevin Lo 
202e06e816fSKevin Lo 	inp = mem;
203e06e816fSKevin Lo 	INP_LOCK_INIT(inp, "inp", "udpliteinp");
204e06e816fSKevin Lo 	return (0);
205e06e816fSKevin Lo }
206e06e816fSKevin Lo 
207df8bae1dSRodney W. Grimes void
208af1ee11dSRobert Watson udp_init(void)
209df8bae1dSRodney W. Grimes {
210af1ee11dSRobert Watson 
2118ad1a83bSAdrian Chadd 	/*
2128ad1a83bSAdrian Chadd 	 * For now default to 2-tuple UDP hashing - until the fragment
2138ad1a83bSAdrian Chadd 	 * reassembly code can also update the flowid.
2148ad1a83bSAdrian Chadd 	 *
2158ad1a83bSAdrian Chadd 	 * Once we can calculate the flowid that way and re-establish
2168ad1a83bSAdrian Chadd 	 * a 4-tuple, flip this to 4-tuple.
2178ad1a83bSAdrian Chadd 	 */
2189bcd427bSRobert Watson 	in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE,
219a8da5dd6SCraig Rodrigues 	    "udp_inpcb", udp_inpcb_init, NULL, 0,
22052cd27cbSRobert Watson 	    IPI_HASHFIELDS_2TUPLE);
2216a9148feSBjoern A. Zeeb 	V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb),
222a8da5dd6SCraig Rodrigues 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2236a9148feSBjoern A. Zeeb 	uma_zone_set_max(V_udpcb_zone, maxsockets);
2246acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(V_udpcb_zone, "kern.ipc.maxsockets limit reached");
2254f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
2264f590175SPaul Saab 	    EVENTHANDLER_PRI_ANY);
227df8bae1dSRodney W. Grimes }
228df8bae1dSRodney W. Grimes 
229e06e816fSKevin Lo void
230e06e816fSKevin Lo udplite_init(void)
231e06e816fSKevin Lo {
232e06e816fSKevin Lo 
233e06e816fSKevin Lo 	in_pcbinfo_init(&V_ulitecbinfo, "udplite", &V_ulitecb, UDBHASHSIZE,
234e06e816fSKevin Lo 	    UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init, NULL,
235a8da5dd6SCraig Rodrigues 	    0, IPI_HASHFIELDS_2TUPLE);
236e06e816fSKevin Lo }
237e06e816fSKevin Lo 
238315e3e38SRobert Watson /*
239315e3e38SRobert Watson  * Kernel module interface for updating udpstat.  The argument is an index
240315e3e38SRobert Watson  * into udpstat treated as an array of u_long.  While this encodes the
241315e3e38SRobert Watson  * general layout of udpstat into the caller, it doesn't encode its location,
242315e3e38SRobert Watson  * so that future changes to add, for example, per-CPU stats support won't
243315e3e38SRobert Watson  * cause binary compatibility problems for kernel modules.
244315e3e38SRobert Watson  */
245315e3e38SRobert Watson void
246315e3e38SRobert Watson kmod_udpstat_inc(int statnum)
247315e3e38SRobert Watson {
248315e3e38SRobert Watson 
2495b7cb97cSAndrey V. Elsukov 	counter_u64_add(VNET(udpstat)[statnum], 1);
250315e3e38SRobert Watson }
251315e3e38SRobert Watson 
2526a9148feSBjoern A. Zeeb int
2536a9148feSBjoern A. Zeeb udp_newudpcb(struct inpcb *inp)
2546a9148feSBjoern A. Zeeb {
2556a9148feSBjoern A. Zeeb 	struct udpcb *up;
2566a9148feSBjoern A. Zeeb 
2576a9148feSBjoern A. Zeeb 	up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO);
2586a9148feSBjoern A. Zeeb 	if (up == NULL)
2596a9148feSBjoern A. Zeeb 		return (ENOBUFS);
2606a9148feSBjoern A. Zeeb 	inp->inp_ppcb = up;
2616a9148feSBjoern A. Zeeb 	return (0);
2626a9148feSBjoern A. Zeeb }
2636a9148feSBjoern A. Zeeb 
2646a9148feSBjoern A. Zeeb void
2656a9148feSBjoern A. Zeeb udp_discardcb(struct udpcb *up)
2666a9148feSBjoern A. Zeeb {
2676a9148feSBjoern A. Zeeb 
2686a9148feSBjoern A. Zeeb 	uma_zfree(V_udpcb_zone, up);
2696a9148feSBjoern A. Zeeb }
2706a9148feSBjoern A. Zeeb 
271bc29160dSMarko Zec #ifdef VIMAGE
272bc29160dSMarko Zec void
273bc29160dSMarko Zec udp_destroy(void)
274bc29160dSMarko Zec {
275bc29160dSMarko Zec 
2769bcd427bSRobert Watson 	in_pcbinfo_destroy(&V_udbinfo);
277391dab1cSBjoern A. Zeeb 	uma_zdestroy(V_udpcb_zone);
278bc29160dSMarko Zec }
279e06e816fSKevin Lo 
280e06e816fSKevin Lo void
281e06e816fSKevin Lo udplite_destroy(void)
282e06e816fSKevin Lo {
283e06e816fSKevin Lo 
284e06e816fSKevin Lo 	in_pcbinfo_destroy(&V_ulitecbinfo);
285e06e816fSKevin Lo }
286bc29160dSMarko Zec #endif
287bc29160dSMarko Zec 
28879288c11SBjoern A. Zeeb #ifdef INET
28943bbb6aaSRobert Watson /*
29043bbb6aaSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
29143bbb6aaSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
29243bbb6aaSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
29343bbb6aaSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
29443bbb6aaSRobert Watson  * into the socket code.
295c0d1be08SRandall Stewart  *
296c0d1be08SRandall Stewart  * In the normal case udp_append() will return 0, indicating that you
297c0d1be08SRandall Stewart  * must unlock the inp. However if a tunneling protocol is in place we increment
298c0d1be08SRandall Stewart  * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we
299c0d1be08SRandall Stewart  * then decrement the reference count. If the inp_rele returns 1, indicating the
300c0d1be08SRandall Stewart  * inp is gone, we return that to the caller to tell them *not* to unlock
301c0d1be08SRandall Stewart  * the inp. In the case of multi-cast this will cause the distribution
302c0d1be08SRandall Stewart  * to stop (though most tunneling protocols known currently do *not* use
303c0d1be08SRandall Stewart  * multicast).
30443bbb6aaSRobert Watson  */
305c0d1be08SRandall Stewart static int
30643bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
30743bbb6aaSRobert Watson     struct sockaddr_in *udp_in)
30843bbb6aaSRobert Watson {
30943bbb6aaSRobert Watson 	struct sockaddr *append_sa;
31043bbb6aaSRobert Watson 	struct socket *so;
31199d628d5SPedro F. Giffuni 	struct mbuf *opts = NULL;
31243bbb6aaSRobert Watson #ifdef INET6
31343bbb6aaSRobert Watson 	struct sockaddr_in6 udp_in6;
31443bbb6aaSRobert Watson #endif
3157b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
31643bbb6aaSRobert Watson 
317fa046d87SRobert Watson 	INP_LOCK_ASSERT(inp);
31843bbb6aaSRobert Watson 
31979bb84fbSEdward Tomasz Napierala 	/*
32079bb84fbSEdward Tomasz Napierala 	 * Engage the tunneling protocol.
32179bb84fbSEdward Tomasz Napierala 	 */
32279bb84fbSEdward Tomasz Napierala 	up = intoudpcb(inp);
32379bb84fbSEdward Tomasz Napierala 	if (up->u_tun_func != NULL) {
324c0d1be08SRandall Stewart 		in_pcbref(inp);
325c0d1be08SRandall Stewart 		INP_RUNLOCK(inp);
32681d3ec17SBryan Venteicher 		(*up->u_tun_func)(n, off, inp, (struct sockaddr *)udp_in,
32781d3ec17SBryan Venteicher 		    up->u_tun_ctx);
328c0d1be08SRandall Stewart 		INP_RLOCK(inp);
329c0d1be08SRandall Stewart 		return (in_pcbrele_rlocked(inp));
33079bb84fbSEdward Tomasz Napierala 	}
33179bb84fbSEdward Tomasz Napierala 
33279bb84fbSEdward Tomasz Napierala 	off += sizeof(struct udphdr);
33379bb84fbSEdward Tomasz Napierala 
33443bbb6aaSRobert Watson #ifdef IPSEC
33543bbb6aaSRobert Watson 	/* Check AH/ESP integrity. */
33643bbb6aaSRobert Watson 	if (ipsec4_in_reject(n, inp)) {
33743bbb6aaSRobert Watson 		m_freem(n);
338c0d1be08SRandall Stewart 		return (0);
33943bbb6aaSRobert Watson 	}
3407b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
3417b495c44SVANHULLEBUS Yvan 	up = intoudpcb(inp);
3427b495c44SVANHULLEBUS Yvan 	KASSERT(up != NULL, ("%s: udpcb NULL", __func__));
3437b495c44SVANHULLEBUS Yvan 	if (up->u_flags & UF_ESPINUDP_ALL) {	/* IPSec UDP encaps. */
3447b495c44SVANHULLEBUS Yvan 		n = udp4_espdecap(inp, n, off);
3457b495c44SVANHULLEBUS Yvan 		if (n == NULL)				/* Consumed. */
346c0d1be08SRandall Stewart 			return (0);
3477b495c44SVANHULLEBUS Yvan 	}
3487b495c44SVANHULLEBUS Yvan #endif /* IPSEC_NAT_T */
34943bbb6aaSRobert Watson #endif /* IPSEC */
35043bbb6aaSRobert Watson #ifdef MAC
35130d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, n) != 0) {
35243bbb6aaSRobert Watson 		m_freem(n);
353c0d1be08SRandall Stewart 		return (0);
35443bbb6aaSRobert Watson 	}
35579288c11SBjoern A. Zeeb #endif /* MAC */
35643bbb6aaSRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
35743bbb6aaSRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
35843bbb6aaSRobert Watson #ifdef INET6
3599a38ba81SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6)
36048d48eb9SBjoern A. Zeeb 			(void)ip6_savecontrol_v4(inp, n, &opts, NULL);
3619a38ba81SBjoern A. Zeeb 		else
36279288c11SBjoern A. Zeeb #endif /* INET6 */
36343bbb6aaSRobert Watson 			ip_savecontrol(inp, &opts, ip, n);
36443bbb6aaSRobert Watson 	}
36543bbb6aaSRobert Watson #ifdef INET6
36643bbb6aaSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
36743bbb6aaSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
36843bbb6aaSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
36943bbb6aaSRobert Watson 		udp_in6.sin6_family = AF_INET6;
37043bbb6aaSRobert Watson 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
37143bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
37243bbb6aaSRobert Watson 	} else
37379288c11SBjoern A. Zeeb #endif /* INET6 */
37443bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)udp_in;
37543bbb6aaSRobert Watson 	m_adj(n, off);
37643bbb6aaSRobert Watson 
37743bbb6aaSRobert Watson 	so = inp->inp_socket;
37843bbb6aaSRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
37943bbb6aaSRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
38043bbb6aaSRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
38143bbb6aaSRobert Watson 		m_freem(n);
38243bbb6aaSRobert Watson 		if (opts)
38343bbb6aaSRobert Watson 			m_freem(opts);
384026decb8SRobert Watson 		UDPSTAT_INC(udps_fullsock);
38543bbb6aaSRobert Watson 	} else
38643bbb6aaSRobert Watson 		sorwakeup_locked(so);
387c0d1be08SRandall Stewart 	return (0);
38843bbb6aaSRobert Watson }
38943bbb6aaSRobert Watson 
3908f5a8818SKevin Lo int
3918f5a8818SKevin Lo udp_input(struct mbuf **mp, int *offp, int proto)
392df8bae1dSRodney W. Grimes {
3933329b236SRobert Watson 	struct ip *ip;
3943329b236SRobert Watson 	struct udphdr *uh;
39571498f30SBruce M Simpson 	struct ifnet *ifp;
3963329b236SRobert Watson 	struct inpcb *inp;
3978f134647SGleb Smirnoff 	uint16_t len, ip_len;
398e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
399df8bae1dSRodney W. Grimes 	struct ip save_ip;
400d4b509bdSRobert Watson 	struct sockaddr_in udp_in;
4018f5a8818SKevin Lo 	struct mbuf *m;
4020b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
4038f5a8818SKevin Lo 	int cscov_partial, iphlen;
404df8bae1dSRodney W. Grimes 
4058f5a8818SKevin Lo 	m = *mp;
4068f5a8818SKevin Lo 	iphlen = *offp;
40771498f30SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
4088f5a8818SKevin Lo 	*mp = NULL;
409026decb8SRobert Watson 	UDPSTAT_INC(udps_ipackets);
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 	/*
4123329b236SRobert Watson 	 * Strip IP options, if any; should skip this, make available to
4133329b236SRobert Watson 	 * user, and use on returned packets, but we don't yet have a way to
4143329b236SRobert Watson 	 * check the checksum with options still present.
415df8bae1dSRodney W. Grimes 	 */
416df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
417105bd211SGleb Smirnoff 		ip_stripoptions(m);
418df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
419df8bae1dSRodney W. Grimes 	}
420df8bae1dSRodney W. Grimes 
421df8bae1dSRodney W. Grimes 	/*
422df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
423df8bae1dSRodney W. Grimes 	 */
424df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
425df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
426d1b18731SKevin Lo 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) {
427026decb8SRobert Watson 			UDPSTAT_INC(udps_hdrops);
4288f5a8818SKevin Lo 			return (IPPROTO_DONE);
429df8bae1dSRodney W. Grimes 		}
430df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
431df8bae1dSRodney W. Grimes 	}
432df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
4338f5a8818SKevin Lo 	cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0;
434df8bae1dSRodney W. Grimes 
4353329b236SRobert Watson 	/*
4363329b236SRobert Watson 	 * Destination port of 0 is illegal, based on RFC768.
4373329b236SRobert Watson 	 */
438686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
439f76fcf6dSJeffrey Hsu 		goto badunlocked;
440686cdd19SJun-ichiro itojun Hagino 
441df8bae1dSRodney W. Grimes 	/*
4423329b236SRobert Watson 	 * Construct sockaddr format source address.  Stuff source address
4433329b236SRobert Watson 	 * and datagram in user buffer.
444b9234fafSSam Leffler 	 */
445d4b509bdSRobert Watson 	bzero(&udp_in, sizeof(udp_in));
446d4b509bdSRobert Watson 	udp_in.sin_len = sizeof(udp_in);
447d4b509bdSRobert Watson 	udp_in.sin_family = AF_INET;
448b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
449b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
450b9234fafSSam Leffler 
451b9234fafSSam Leffler 	/*
452af1ee11dSRobert Watson 	 * Make mbuf data length reflect UDP length.  If not enough data to
453af1ee11dSRobert Watson 	 * reflect UDP length, drop.
454df8bae1dSRodney W. Grimes 	 */
455df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
4568ad458a4SGleb Smirnoff 	ip_len = ntohs(ip->ip_len) - iphlen;
4570f4a0366SMichael Tuexen 	if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) {
458e06e816fSKevin Lo 		/* Zero means checksum over the complete packet. */
4590f4a0366SMichael Tuexen 		if (len == 0)
460e06e816fSKevin Lo 			len = ip_len;
461e06e816fSKevin Lo 		cscov_partial = 0;
462e06e816fSKevin Lo 	}
4638f134647SGleb Smirnoff 	if (ip_len != len) {
4648f134647SGleb Smirnoff 		if (len > ip_len || len < sizeof(struct udphdr)) {
465026decb8SRobert Watson 			UDPSTAT_INC(udps_badlen);
466f76fcf6dSJeffrey Hsu 			goto badunlocked;
467df8bae1dSRodney W. Grimes 		}
4688f5a8818SKevin Lo 		if (proto == IPPROTO_UDP)
4698f134647SGleb Smirnoff 			m_adj(m, len - ip_len);
470df8bae1dSRodney W. Grimes 	}
4713329b236SRobert Watson 
472df8bae1dSRodney W. Grimes 	/*
4733329b236SRobert Watson 	 * Save a copy of the IP header in case we want restore it for
4743329b236SRobert Watson 	 * sending an ICMP error message in response.
475df8bae1dSRodney W. Grimes 	 */
476603724d3SBjoern A. Zeeb 	if (!V_udp_blackhole)
477df8bae1dSRodney W. Grimes 		save_ip = *ip;
478cce418d3SMatt Jacob 	else
479cce418d3SMatt Jacob 		memset(&save_ip, 0, sizeof(save_ip));
480df8bae1dSRodney W. Grimes 
481df8bae1dSRodney W. Grimes 	/*
482df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
483df8bae1dSRodney W. Grimes 	 */
4846dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
48539629c92SDavid Malone 		u_short uh_sum;
48639629c92SDavid Malone 
487e06e816fSKevin Lo 		if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
488e06e816fSKevin Lo 		    !cscov_partial) {
489db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
49039629c92SDavid Malone 				uh_sum = m->m_pkthdr.csum_data;
491db4f9cc7SJonathan Lemon 			else
49239629c92SDavid Malone 				uh_sum = in_pseudo(ip->ip_src.s_addr,
493506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
4948f5a8818SKevin Lo 				    m->m_pkthdr.csum_data + proto));
49539629c92SDavid Malone 			uh_sum ^= 0xffff;
496db4f9cc7SJonathan Lemon 		} else {
497cb342100SHajimu UMEMOTO 			char b[9];
498af1ee11dSRobert Watson 
499cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
5006effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
5018f5a8818SKevin Lo 			((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ?
502e06e816fSKevin Lo 			    uh->uh_ulen : htons(ip_len);
50339629c92SDavid Malone 			uh_sum = in_cksum(m, len + sizeof (struct ip));
504cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
505db4f9cc7SJonathan Lemon 		}
50639629c92SDavid Malone 		if (uh_sum) {
507026decb8SRobert Watson 			UDPSTAT_INC(udps_badsum);
508df8bae1dSRodney W. Grimes 			m_freem(m);
5098f5a8818SKevin Lo 			return (IPPROTO_DONE);
510df8bae1dSRodney W. Grimes 		}
511c6d81a34SMichael Tuexen 	} else {
512c6d81a34SMichael Tuexen 		if (proto == IPPROTO_UDP) {
513026decb8SRobert Watson 			UDPSTAT_INC(udps_nosum);
514c6d81a34SMichael Tuexen 		} else {
515c6d81a34SMichael Tuexen 			/* UDPLite requires a checksum */
516c6d81a34SMichael Tuexen 			/* XXX: What is the right UDPLite MIB counter here? */
517c6d81a34SMichael Tuexen 			m_freem(m);
518c6d81a34SMichael Tuexen 			return (IPPROTO_DONE);
519c6d81a34SMichael Tuexen 		}
520c6d81a34SMichael Tuexen 	}
521df8bae1dSRodney W. Grimes 
522a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(proto);
523df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
52471498f30SBruce M Simpson 	    in_broadcast(ip->ip_dst, ifp)) {
52582c23ebaSBill Fenner 		struct inpcb *last;
526e06e816fSKevin Lo 		struct inpcbhead *pcblist;
52771498f30SBruce M Simpson 		struct ip_moptions *imo;
5283329b236SRobert Watson 
529e06e816fSKevin Lo 		INP_INFO_RLOCK(pcbinfo);
530a86e5c96SBjoern A. Zeeb 		pcblist = udp_get_pcblist(proto);
531df8bae1dSRodney W. Grimes 		last = NULL;
532e06e816fSKevin Lo 		LIST_FOREACH(inp, pcblist, inp_list) {
5339c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
534f76fcf6dSJeffrey Hsu 				continue;
535cfa1ca9dSYoshinobu Inoue #ifdef INET6
536369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
5379c1df695SRobert Watson 				continue;
538cfa1ca9dSYoshinobu Inoue #endif
53971498f30SBruce M Simpson 			if (inp->inp_laddr.s_addr != INADDR_ANY &&
54071498f30SBruce M Simpson 			    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
5419c1df695SRobert Watson 				continue;
54271498f30SBruce M Simpson 			if (inp->inp_faddr.s_addr != INADDR_ANY &&
54371498f30SBruce M Simpson 			    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
54471498f30SBruce M Simpson 				continue;
54571498f30SBruce M Simpson 			if (inp->inp_fport != 0 &&
546df8bae1dSRodney W. Grimes 			    inp->inp_fport != uh->uh_sport)
5479c1df695SRobert Watson 				continue;
54871498f30SBruce M Simpson 
549119d85f6SRobert Watson 			INP_RLOCK(inp);
550df8bae1dSRodney W. Grimes 
55183453a06SBruce M Simpson 			/*
552fa046d87SRobert Watson 			 * XXXRW: Because we weren't holding either the inpcb
553fa046d87SRobert Watson 			 * or the hash lock when we checked for a match
554fa046d87SRobert Watson 			 * before, we should probably recheck now that the
555fa046d87SRobert Watson 			 * inpcb lock is held.
556fa046d87SRobert Watson 			 */
557fa046d87SRobert Watson 
558fa046d87SRobert Watson 			/*
55971498f30SBruce M Simpson 			 * Handle socket delivery policy for any-source
56071498f30SBruce M Simpson 			 * and source-specific multicast. [RFC3678]
56183453a06SBruce M Simpson 			 */
56271498f30SBruce M Simpson 			imo = inp->inp_moptions;
563a38b1c8cSRandall Stewart 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
564d10910e6SBruce M Simpson 				struct sockaddr_in	 group;
565d10910e6SBruce M Simpson 				int			 blocked;
566a38b1c8cSRandall Stewart 				if (imo == NULL) {
567a38b1c8cSRandall Stewart 					INP_RUNLOCK(inp);
568a38b1c8cSRandall Stewart 					continue;
569a38b1c8cSRandall Stewart 				}
570d10910e6SBruce M Simpson 				bzero(&group, sizeof(struct sockaddr_in));
571d10910e6SBruce M Simpson 				group.sin_len = sizeof(struct sockaddr_in);
572d10910e6SBruce M Simpson 				group.sin_family = AF_INET;
573d10910e6SBruce M Simpson 				group.sin_addr = ip->ip_dst;
57471498f30SBruce M Simpson 
575d10910e6SBruce M Simpson 				blocked = imo_multi_filter(imo, ifp,
576d10910e6SBruce M Simpson 					(struct sockaddr *)&group,
57771498f30SBruce M Simpson 					(struct sockaddr *)&udp_in);
578d10910e6SBruce M Simpson 				if (blocked != MCAST_PASS) {
579d10910e6SBruce M Simpson 					if (blocked == MCAST_NOTGMEMBER)
58086425c62SRobert Watson 						IPSTAT_INC(ips_notmember);
581d10910e6SBruce M Simpson 					if (blocked == MCAST_NOTSMEMBER ||
582d10910e6SBruce M Simpson 					    blocked == MCAST_MUTED)
583026decb8SRobert Watson 						UDPSTAT_INC(udps_filtermcast);
584119d85f6SRobert Watson 					INP_RUNLOCK(inp);
5859c1df695SRobert Watson 					continue;
5869c1df695SRobert Watson 				}
58783453a06SBruce M Simpson 			}
588df8bae1dSRodney W. Grimes 			if (last != NULL) {
589df8bae1dSRodney W. Grimes 				struct mbuf *n;
590df8bae1dSRodney W. Grimes 
591c19f98ebSBryan Venteicher 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
592a0a9e1b5SBryan Venteicher 					UDP_PROBE(receive, NULL, last, ip,
593a0a9e1b5SBryan Venteicher 					    last, uh);
594c0d1be08SRandall Stewart 					if (udp_append(last, ip, n, iphlen,
595c0d1be08SRandall Stewart 						&udp_in)) {
596c0d1be08SRandall Stewart 						goto inp_lost;
597c0d1be08SRandall Stewart 					}
598c19f98ebSBryan Venteicher 				}
5996a9148feSBjoern A. Zeeb 				INP_RUNLOCK(last);
600df8bae1dSRodney W. Grimes 			}
60182c23ebaSBill Fenner 			last = inp;
602df8bae1dSRodney W. Grimes 			/*
603df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
604df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
6053329b236SRobert Watson 			 * socket options set.  This heuristic avoids
6063329b236SRobert Watson 			 * searching through all pcbs in the common case of a
6073329b236SRobert Watson 			 * non-shared port.  It assumes that an application
6083329b236SRobert Watson 			 * will never clear these options after setting them.
609df8bae1dSRodney W. Grimes 			 */
6103329b236SRobert Watson 			if ((last->inp_socket->so_options &
6113329b236SRobert Watson 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
612df8bae1dSRodney W. Grimes 				break;
613df8bae1dSRodney W. Grimes 		}
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes 		if (last == NULL) {
616df8bae1dSRodney W. Grimes 			/*
6173329b236SRobert Watson 			 * No matching pcb found; discard datagram.  (No need
6183329b236SRobert Watson 			 * to send an ICMP Port Unreachable for a broadcast
6193329b236SRobert Watson 			 * or multicast datgram.)
620df8bae1dSRodney W. Grimes 			 */
621026decb8SRobert Watson 			UDPSTAT_INC(udps_noportbcast);
622fa046d87SRobert Watson 			if (inp)
623fa046d87SRobert Watson 				INP_RUNLOCK(inp);
624e06e816fSKevin Lo 			INP_INFO_RUNLOCK(pcbinfo);
625fa046d87SRobert Watson 			goto badunlocked;
626df8bae1dSRodney W. Grimes 		}
627a0a9e1b5SBryan Venteicher 		UDP_PROBE(receive, NULL, last, ip, last, uh);
628c0d1be08SRandall Stewart 		if (udp_append(last, ip, m, iphlen, &udp_in) == 0)
629c7c7ea4bSRandall Stewart 			INP_RUNLOCK(last);
630c0d1be08SRandall Stewart 	inp_lost:
631e06e816fSKevin Lo 		INP_INFO_RUNLOCK(pcbinfo);
6328f5a8818SKevin Lo 		return (IPPROTO_DONE);
633df8bae1dSRodney W. Grimes 	}
6343329b236SRobert Watson 
635df8bae1dSRodney W. Grimes 	/*
6366d6a026bSDavid Greenman 	 * Locate pcb for datagram.
637df8bae1dSRodney W. Grimes 	 */
638c1de64a4SAndrey V. Elsukov 
6398a006adbSBjoern A. Zeeb 	/*
6408a006adbSBjoern A. Zeeb 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
6418a006adbSBjoern A. Zeeb 	 */
642ffdbf9daSAndrey V. Elsukov 	if ((m->m_flags & M_IP_NEXTHOP) &&
643c1de64a4SAndrey V. Elsukov 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
6448a006adbSBjoern A. Zeeb 		struct sockaddr_in *next_hop;
6458a006adbSBjoern A. Zeeb 
6468a006adbSBjoern A. Zeeb 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
6478a006adbSBjoern A. Zeeb 
6488a006adbSBjoern A. Zeeb 		/*
6498a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
6508a006adbSBjoern A. Zeeb 		 * Already got one like this?
6518a006adbSBjoern A. Zeeb 		 */
652e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
6538a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m);
6548a006adbSBjoern A. Zeeb 		if (!inp) {
6558a006adbSBjoern A. Zeeb 			/*
6568a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
6578a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
6588a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
6598a006adbSBjoern A. Zeeb 			 */
660e06e816fSKevin Lo 			inp = in_pcblookup(pcbinfo, ip->ip_src,
6618a006adbSBjoern A. Zeeb 			    uh->uh_sport, next_hop->sin_addr,
6628a006adbSBjoern A. Zeeb 			    next_hop->sin_port ? htons(next_hop->sin_port) :
6638a006adbSBjoern A. Zeeb 			    uh->uh_dport, INPLOOKUP_WILDCARD |
6648a006adbSBjoern A. Zeeb 			    INPLOOKUP_RLOCKPCB, ifp);
6658a006adbSBjoern A. Zeeb 		}
6668a006adbSBjoern A. Zeeb 		/* Remove the tag from the packet. We don't need it anymore. */
6678a006adbSBjoern A. Zeeb 		m_tag_delete(m, fwd_tag);
668ffdbf9daSAndrey V. Elsukov 		m->m_flags &= ~M_IP_NEXTHOP;
6698a006adbSBjoern A. Zeeb 	} else
670e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
6718a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
6728a006adbSBjoern A. Zeeb 		    INPLOOKUP_RLOCKPCB, ifp, m);
67315bd2b43SDavid Greenman 	if (inp == NULL) {
674afdb4274SRobert Watson 		if (udp_log_in_vain) {
675df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
67675cfc95fSAndrey A. Chernov 
67775cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
678592071e8SBruce Evans 			log(LOG_INFO,
679592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
680592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
681592071e8SBruce Evans 			    ntohs(uh->uh_sport));
68275cfc95fSAndrey A. Chernov 		}
683026decb8SRobert Watson 		UDPSTAT_INC(udps_noport);
684df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
685026decb8SRobert Watson 			UDPSTAT_INC(udps_noportbcast);
686fa046d87SRobert Watson 			goto badunlocked;
687df8bae1dSRodney W. Grimes 		}
688603724d3SBjoern A. Zeeb 		if (V_udp_blackhole)
689fa046d87SRobert Watson 			goto badunlocked;
6901cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
691fa046d87SRobert Watson 			goto badunlocked;
69204287599SRuslan Ermilov 		*ip = save_ip;
693582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
6948f5a8818SKevin Lo 		return (IPPROTO_DONE);
695df8bae1dSRodney W. Grimes 	}
6963329b236SRobert Watson 
6973329b236SRobert Watson 	/*
6983329b236SRobert Watson 	 * Check the minimum TTL for socket.
6993329b236SRobert Watson 	 */
700fa046d87SRobert Watson 	INP_RLOCK_ASSERT(inp);
70110cc62b7SRobert Watson 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
70210cc62b7SRobert Watson 		INP_RUNLOCK(inp);
703fa046d87SRobert Watson 		m_freem(m);
7048f5a8818SKevin Lo 		return (IPPROTO_DONE);
70510cc62b7SRobert Watson 	}
706e06e816fSKevin Lo 	if (cscov_partial) {
707e06e816fSKevin Lo 		struct udpcb *up;
708e06e816fSKevin Lo 
709e06e816fSKevin Lo 		up = intoudpcb(inp);
71083e95fb3SMichael Tuexen 		if (up->u_rxcslen == 0 || up->u_rxcslen > len) {
711e06e816fSKevin Lo 			INP_RUNLOCK(inp);
712e06e816fSKevin Lo 			m_freem(m);
7138f5a8818SKevin Lo 			return (IPPROTO_DONE);
714e06e816fSKevin Lo 		}
715e06e816fSKevin Lo 	}
71657f60867SMark Johnston 
7171ad19fb6SMark Johnston 	UDP_PROBE(receive, NULL, inp, ip, inp, uh);
718c0d1be08SRandall Stewart 	if (udp_append(inp, ip, m, iphlen, &udp_in) == 0)
719119d85f6SRobert Watson 		INP_RUNLOCK(inp);
7208f5a8818SKevin Lo 	return (IPPROTO_DONE);
72161ffc0b1SJeffrey Hsu 
722f76fcf6dSJeffrey Hsu badunlocked:
723df8bae1dSRodney W. Grimes 	m_freem(m);
7248f5a8818SKevin Lo 	return (IPPROTO_DONE);
725cfa1ca9dSYoshinobu Inoue }
72679288c11SBjoern A. Zeeb #endif /* INET */
727cfa1ca9dSYoshinobu Inoue 
728cfa1ca9dSYoshinobu Inoue /*
7293329b236SRobert Watson  * Notify a udp user of an asynchronous error; just wake up so that they can
7303329b236SRobert Watson  * collect error status.
731df8bae1dSRodney W. Grimes  */
7323ce144eaSJeffrey Hsu struct inpcb *
7333329b236SRobert Watson udp_notify(struct inpcb *inp, int errno)
734df8bae1dSRodney W. Grimes {
7353329b236SRobert Watson 
736ac9ae279SRobert Watson 	/*
737ac9ae279SRobert Watson 	 * While udp_ctlinput() always calls udp_notify() with a read lock
738ac9ae279SRobert Watson 	 * when invoking it directly, in_pcbnotifyall() currently uses write
739ac9ae279SRobert Watson 	 * locks due to sharing code with TCP.  For now, accept either a read
740ac9ae279SRobert Watson 	 * or a write lock, but a read lock is sufficient.
741ac9ae279SRobert Watson 	 */
742ac9ae279SRobert Watson 	INP_LOCK_ASSERT(inp);
74384cc0778SGeorge V. Neville-Neil 	if ((errno == EHOSTUNREACH || errno == ENETUNREACH ||
74484cc0778SGeorge V. Neville-Neil 	     errno == EHOSTDOWN) && inp->inp_route.ro_rt) {
74584cc0778SGeorge V. Neville-Neil 		RTFREE(inp->inp_route.ro_rt);
74684cc0778SGeorge V. Neville-Neil 		inp->inp_route.ro_rt = (struct rtentry *)NULL;
74784cc0778SGeorge V. Neville-Neil 	}
7488501a69cSRobert Watson 
749df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
750df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
751df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
7523329b236SRobert Watson 	return (inp);
753df8bae1dSRodney W. Grimes }
754df8bae1dSRodney W. Grimes 
75579288c11SBjoern A. Zeeb #ifdef INET
756e06e816fSKevin Lo static void
757e06e816fSKevin Lo udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip,
758e06e816fSKevin Lo     struct inpcbinfo *pcbinfo)
759df8bae1dSRodney W. Grimes {
760c693a045SJonathan Lemon 	struct ip *ip = vip;
761c693a045SJonathan Lemon 	struct udphdr *uh;
762c693a045SJonathan Lemon 	struct in_addr faddr;
763c693a045SJonathan Lemon 	struct inpcb *inp;
764c693a045SJonathan Lemon 
765c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
766c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
767c693a045SJonathan Lemon 		return;
768df8bae1dSRodney W. Grimes 
76984cc0778SGeorge V. Neville-Neil 	if (PRC_IS_REDIRECT(cmd)) {
77084cc0778SGeorge V. Neville-Neil 		/* signal EHOSTDOWN, as it flushes the cached route */
7714f321dbdSBjoern A. Zeeb 		in_pcbnotifyall(&V_udbinfo, faddr, EHOSTDOWN, udp_notify);
77297d8d152SAndre Oppermann 		return;
77384cc0778SGeorge V. Neville-Neil 	}
7743329b236SRobert Watson 
77597d8d152SAndre Oppermann 	/*
77697d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
7773329b236SRobert Watson 	 *
7783329b236SRobert Watson 	 * XXX: We never get this from ICMP, otherwise it makes an excellent
7793329b236SRobert Watson 	 * DoS attack on machines with many connections.
78097d8d152SAndre Oppermann 	 */
78197d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
782af1ee11dSRobert Watson 		ip = NULL;
783d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
784df8bae1dSRodney W. Grimes 		return;
785af1ee11dSRobert Watson 	if (ip != NULL) {
786df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
787e06e816fSKevin Lo 		inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
788fa046d87SRobert Watson 		    ip->ip_src, uh->uh_sport, INPLOOKUP_RLOCKPCB, NULL);
789f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
790fa046d87SRobert Watson 			INP_RLOCK_ASSERT(inp);
791f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
792f5514f08SRobert Watson 				udp_notify(inp, inetctlerrmap[cmd]);
793f76fcf6dSJeffrey Hsu 			}
794ac9ae279SRobert Watson 			INP_RUNLOCK(inp);
795*abb901c5SRandall Stewart 		} else {
796*abb901c5SRandall Stewart 			inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
797*abb901c5SRandall Stewart 					   ip->ip_src, uh->uh_sport,
798*abb901c5SRandall Stewart 					   INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
799*abb901c5SRandall Stewart 			if (inp != NULL) {
800*abb901c5SRandall Stewart 				struct udpcb *up;
801*abb901c5SRandall Stewart 
802*abb901c5SRandall Stewart 				up = intoudpcb(inp);
803*abb901c5SRandall Stewart 				if (up->u_icmp_func != NULL) {
804*abb901c5SRandall Stewart 					INP_RUNLOCK(inp);
805*abb901c5SRandall Stewart 					(*up->u_icmp_func)(cmd, sa, vip, up->u_tun_ctx);
806*abb901c5SRandall Stewart 				} else {
807*abb901c5SRandall Stewart 					INP_RUNLOCK(inp);
808*abb901c5SRandall Stewart 				}
809*abb901c5SRandall Stewart 			}
810f76fcf6dSJeffrey Hsu 		}
811df8bae1dSRodney W. Grimes 	} else
812e06e816fSKevin Lo 		in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd],
813f5514f08SRobert Watson 		    udp_notify);
814df8bae1dSRodney W. Grimes }
815e06e816fSKevin Lo void
816e06e816fSKevin Lo udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
817e06e816fSKevin Lo {
818e06e816fSKevin Lo 
819e06e816fSKevin Lo 	return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo));
820e06e816fSKevin Lo }
821e06e816fSKevin Lo 
822e06e816fSKevin Lo void
823e06e816fSKevin Lo udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip)
824e06e816fSKevin Lo {
825e06e816fSKevin Lo 
826e06e816fSKevin Lo 	return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo));
827e06e816fSKevin Lo }
82879288c11SBjoern A. Zeeb #endif /* INET */
829df8bae1dSRodney W. Grimes 
8300312fbe9SPoul-Henning Kamp static int
83182d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
83298271db4SGarrett Wollman {
833277afaffSRobert Watson 	int error, i, n;
83498271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
83598271db4SGarrett Wollman 	inp_gen_t gencnt;
83698271db4SGarrett Wollman 	struct xinpgen xig;
83798271db4SGarrett Wollman 
83898271db4SGarrett Wollman 	/*
839f5514f08SRobert Watson 	 * The process of preparing the PCB list is too time-consuming and
84098271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
84198271db4SGarrett Wollman 	 */
84298271db4SGarrett Wollman 	if (req->oldptr == 0) {
843603724d3SBjoern A. Zeeb 		n = V_udbinfo.ipi_count;
844c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
845c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
8463329b236SRobert Watson 		return (0);
84798271db4SGarrett Wollman 	}
84898271db4SGarrett Wollman 
84998271db4SGarrett Wollman 	if (req->newptr != 0)
8503329b236SRobert Watson 		return (EPERM);
85198271db4SGarrett Wollman 
85298271db4SGarrett Wollman 	/*
85398271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
85498271db4SGarrett Wollman 	 */
855603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_udbinfo);
856603724d3SBjoern A. Zeeb 	gencnt = V_udbinfo.ipi_gencnt;
857603724d3SBjoern A. Zeeb 	n = V_udbinfo.ipi_count;
858603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_udbinfo);
85998271db4SGarrett Wollman 
86047934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
8615c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
86247934cefSDon Lewis 	if (error != 0)
86347934cefSDon Lewis 		return (error);
8645c38b6dbSDon Lewis 
86598271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
86698271db4SGarrett Wollman 	xig.xig_count = n;
86798271db4SGarrett Wollman 	xig.xig_gen = gencnt;
86898271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
86998271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
87098271db4SGarrett Wollman 	if (error)
8713329b236SRobert Watson 		return (error);
87298271db4SGarrett Wollman 
873a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
87499d628d5SPedro F. Giffuni 	if (inp_list == NULL)
8753329b236SRobert Watson 		return (ENOMEM);
87698271db4SGarrett Wollman 
877603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_udbinfo);
878603724d3SBjoern A. Zeeb 	for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n;
879fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
880d0e157f6SBjoern A. Zeeb 		INP_WLOCK(inp);
8812ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
882d0e157f6SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
883d0e157f6SBjoern A. Zeeb 			in_pcbref(inp);
88498271db4SGarrett Wollman 			inp_list[i++] = inp;
885d0e157f6SBjoern A. Zeeb 		}
886d0e157f6SBjoern A. Zeeb 		INP_WUNLOCK(inp);
8874787fd37SPaul Saab 	}
888603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_udbinfo);
88998271db4SGarrett Wollman 	n = i;
89098271db4SGarrett Wollman 
89198271db4SGarrett Wollman 	error = 0;
89298271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
89398271db4SGarrett Wollman 		inp = inp_list[i];
8949622e84fSRobert Watson 		INP_RLOCK(inp);
89598271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
89698271db4SGarrett Wollman 			struct xinpcb xi;
897d0e157f6SBjoern A. Zeeb 
898fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
89998271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
90098271db4SGarrett Wollman 			/* XXX should avoid extra copy */
90198271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
90298271db4SGarrett Wollman 			if (inp->inp_socket)
90398271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
9044b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
9059622e84fSRobert Watson 			INP_RUNLOCK(inp);
90698271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
907d915b280SStephan Uphoff 		} else
9089622e84fSRobert Watson 			INP_RUNLOCK(inp);
90998271db4SGarrett Wollman 	}
910d0e157f6SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_udbinfo);
911d0e157f6SBjoern A. Zeeb 	for (i = 0; i < n; i++) {
912d0e157f6SBjoern A. Zeeb 		inp = inp_list[i];
913fa046d87SRobert Watson 		INP_RLOCK(inp);
914fa046d87SRobert Watson 		if (!in_pcbrele_rlocked(inp))
915fa046d87SRobert Watson 			INP_RUNLOCK(inp);
916d0e157f6SBjoern A. Zeeb 	}
917d0e157f6SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_udbinfo);
918d0e157f6SBjoern A. Zeeb 
91998271db4SGarrett Wollman 	if (!error) {
92098271db4SGarrett Wollman 		/*
9213329b236SRobert Watson 		 * Give the user an updated idea of our state.  If the
9223329b236SRobert Watson 		 * generation differs from what we told her before, she knows
9233329b236SRobert Watson 		 * that something happened while we were processing this
9243329b236SRobert Watson 		 * request, and it might be necessary to retry.
92598271db4SGarrett Wollman 		 */
926603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_udbinfo);
927603724d3SBjoern A. Zeeb 		xig.xig_gen = V_udbinfo.ipi_gencnt;
92898271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
929603724d3SBjoern A. Zeeb 		xig.xig_count = V_udbinfo.ipi_count;
930603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_udbinfo);
93198271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
93298271db4SGarrett Wollman 	}
93398271db4SGarrett Wollman 	free(inp_list, M_TEMP);
9343329b236SRobert Watson 	return (error);
93598271db4SGarrett Wollman }
93698271db4SGarrett Wollman 
93779c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
93879c3d51bSMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
93998271db4SGarrett Wollman     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
94098271db4SGarrett Wollman 
94179288c11SBjoern A. Zeeb #ifdef INET
94298271db4SGarrett Wollman static int
94382d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
944490d50b6SBrian Feldman {
945c0511d3bSBrian Feldman 	struct xucred xuc;
946490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
947490d50b6SBrian Feldman 	struct inpcb *inp;
948277afaffSRobert Watson 	int error;
949490d50b6SBrian Feldman 
95032f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
951490d50b6SBrian Feldman 	if (error)
952490d50b6SBrian Feldman 		return (error);
953490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
954490d50b6SBrian Feldman 	if (error)
955490d50b6SBrian Feldman 		return (error);
956fa046d87SRobert Watson 	inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
957fa046d87SRobert Watson 	    addrs[0].sin_addr, addrs[0].sin_port,
958fa046d87SRobert Watson 	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
9599622e84fSRobert Watson 	if (inp != NULL) {
960fa046d87SRobert Watson 		INP_RLOCK_ASSERT(inp);
9619622e84fSRobert Watson 		if (inp->inp_socket == NULL)
9629622e84fSRobert Watson 			error = ENOENT;
9639622e84fSRobert Watson 		if (error == 0)
964f08ef6c5SBjoern A. Zeeb 			error = cr_canseeinpcb(req->td->td_ucred, inp);
9659622e84fSRobert Watson 		if (error == 0)
96686d02c5cSBjoern A. Zeeb 			cru2x(inp->inp_cred, &xuc);
9679622e84fSRobert Watson 		INP_RUNLOCK(inp);
968fa046d87SRobert Watson 	} else
9699622e84fSRobert Watson 		error = ENOENT;
9700e1eebb8SDon Lewis 	if (error == 0)
9710e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
972490d50b6SBrian Feldman 	return (error);
973490d50b6SBrian Feldman }
974490d50b6SBrian Feldman 
9757ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
9767ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
9777ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
97879288c11SBjoern A. Zeeb #endif /* INET */
979490d50b6SBrian Feldman 
9807b495c44SVANHULLEBUS Yvan int
9817b495c44SVANHULLEBUS Yvan udp_ctloutput(struct socket *so, struct sockopt *sopt)
9827b495c44SVANHULLEBUS Yvan {
9837b495c44SVANHULLEBUS Yvan 	struct inpcb *inp;
9847b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
985e06e816fSKevin Lo 	int isudplite, error, optval;
9867b495c44SVANHULLEBUS Yvan 
987e06e816fSKevin Lo 	error = 0;
988e06e816fSKevin Lo 	isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0;
9897b495c44SVANHULLEBUS Yvan 	inp = sotoinpcb(so);
9907b495c44SVANHULLEBUS Yvan 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
9917b495c44SVANHULLEBUS Yvan 	INP_WLOCK(inp);
992e06e816fSKevin Lo 	if (sopt->sopt_level != so->so_proto->pr_protocol) {
9937b495c44SVANHULLEBUS Yvan #ifdef INET6
9947b495c44SVANHULLEBUS Yvan 		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
9957b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9967b495c44SVANHULLEBUS Yvan 			error = ip6_ctloutput(so, sopt);
99779288c11SBjoern A. Zeeb 		}
9987b495c44SVANHULLEBUS Yvan #endif
99979288c11SBjoern A. Zeeb #if defined(INET) && defined(INET6)
100079288c11SBjoern A. Zeeb 		else
100179288c11SBjoern A. Zeeb #endif
100279288c11SBjoern A. Zeeb #ifdef INET
100379288c11SBjoern A. Zeeb 		{
10047b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10057b495c44SVANHULLEBUS Yvan 			error = ip_ctloutput(so, sopt);
10067b495c44SVANHULLEBUS Yvan 		}
10077b495c44SVANHULLEBUS Yvan #endif
10087b495c44SVANHULLEBUS Yvan 		return (error);
10097b495c44SVANHULLEBUS Yvan 	}
10107b495c44SVANHULLEBUS Yvan 
10117b495c44SVANHULLEBUS Yvan 	switch (sopt->sopt_dir) {
10127b495c44SVANHULLEBUS Yvan 	case SOPT_SET:
10137b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
10147b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
10157b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10167b495c44SVANHULLEBUS Yvan 			error = sooptcopyin(sopt, &optval, sizeof optval,
10177b495c44SVANHULLEBUS Yvan 					    sizeof optval);
10187b495c44SVANHULLEBUS Yvan 			if (error)
10197b495c44SVANHULLEBUS Yvan 				break;
10207b495c44SVANHULLEBUS Yvan 			inp = sotoinpcb(so);
10217b495c44SVANHULLEBUS Yvan 			KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
10227b495c44SVANHULLEBUS Yvan 			INP_WLOCK(inp);
10237b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
10247b495c44SVANHULLEBUS Yvan 			up = intoudpcb(inp);
10257b495c44SVANHULLEBUS Yvan 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
10267b495c44SVANHULLEBUS Yvan #endif
10277b495c44SVANHULLEBUS Yvan 			switch (optval) {
10287b495c44SVANHULLEBUS Yvan 			case 0:
10297b495c44SVANHULLEBUS Yvan 				/* Clear all UDP encap. */
10307b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
10317b495c44SVANHULLEBUS Yvan 				up->u_flags &= ~UF_ESPINUDP_ALL;
10327b495c44SVANHULLEBUS Yvan #endif
10337b495c44SVANHULLEBUS Yvan 				break;
10347b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
10357b495c44SVANHULLEBUS Yvan 			case UDP_ENCAP_ESPINUDP:
10367b495c44SVANHULLEBUS Yvan 			case UDP_ENCAP_ESPINUDP_NON_IKE:
10377b495c44SVANHULLEBUS Yvan 				up->u_flags &= ~UF_ESPINUDP_ALL;
10387b495c44SVANHULLEBUS Yvan 				if (optval == UDP_ENCAP_ESPINUDP)
10397b495c44SVANHULLEBUS Yvan 					up->u_flags |= UF_ESPINUDP;
10407b495c44SVANHULLEBUS Yvan 				else if (optval == UDP_ENCAP_ESPINUDP_NON_IKE)
10417b495c44SVANHULLEBUS Yvan 					up->u_flags |= UF_ESPINUDP_NON_IKE;
10427b495c44SVANHULLEBUS Yvan 				break;
10437b495c44SVANHULLEBUS Yvan #endif
10447b495c44SVANHULLEBUS Yvan 			default:
10457b495c44SVANHULLEBUS Yvan 				error = EINVAL;
10467b495c44SVANHULLEBUS Yvan 				break;
10477b495c44SVANHULLEBUS Yvan 			}
10487b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10497b495c44SVANHULLEBUS Yvan 			break;
1050e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
1051e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
1052e06e816fSKevin Lo 			if (!isudplite) {
1053e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1054e06e816fSKevin Lo 				error = ENOPROTOOPT;
1055e06e816fSKevin Lo 				break;
1056e06e816fSKevin Lo 			}
1057e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1058e06e816fSKevin Lo 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1059e06e816fSKevin Lo 			    sizeof(optval));
1060e06e816fSKevin Lo 			if (error != 0)
1061e06e816fSKevin Lo 				break;
1062e06e816fSKevin Lo 			inp = sotoinpcb(so);
1063e06e816fSKevin Lo 			KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1064e06e816fSKevin Lo 			INP_WLOCK(inp);
1065e06e816fSKevin Lo 			up = intoudpcb(inp);
1066e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
106703f90784SMichael Tuexen 			if ((optval != 0 && optval < 8) || (optval > 65535)) {
1068e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1069e06e816fSKevin Lo 				error = EINVAL;
1070e06e816fSKevin Lo 				break;
1071e06e816fSKevin Lo 			}
1072e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1073e06e816fSKevin Lo 				up->u_txcslen = optval;
1074e06e816fSKevin Lo 			else
1075e06e816fSKevin Lo 				up->u_rxcslen = optval;
1076e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1077e06e816fSKevin Lo 			break;
10787b495c44SVANHULLEBUS Yvan 		default:
10797b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10807b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
10817b495c44SVANHULLEBUS Yvan 			break;
10827b495c44SVANHULLEBUS Yvan 		}
10837b495c44SVANHULLEBUS Yvan 		break;
10847b495c44SVANHULLEBUS Yvan 	case SOPT_GET:
10857b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
10867b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
10877b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
10887b495c44SVANHULLEBUS Yvan 			up = intoudpcb(inp);
10897b495c44SVANHULLEBUS Yvan 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
10907b495c44SVANHULLEBUS Yvan 			optval = up->u_flags & UF_ESPINUDP_ALL;
10917b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10927b495c44SVANHULLEBUS Yvan 			error = sooptcopyout(sopt, &optval, sizeof optval);
10937b495c44SVANHULLEBUS Yvan 			break;
10947b495c44SVANHULLEBUS Yvan #endif
1095e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
1096e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
1097e06e816fSKevin Lo 			if (!isudplite) {
1098e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1099e06e816fSKevin Lo 				error = ENOPROTOOPT;
1100e06e816fSKevin Lo 				break;
1101e06e816fSKevin Lo 			}
1102e06e816fSKevin Lo 			up = intoudpcb(inp);
1103e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
1104e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1105e06e816fSKevin Lo 				optval = up->u_txcslen;
1106e06e816fSKevin Lo 			else
1107e06e816fSKevin Lo 				optval = up->u_rxcslen;
1108e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1109e06e816fSKevin Lo 			error = sooptcopyout(sopt, &optval, sizeof(optval));
1110e06e816fSKevin Lo 			break;
11117b495c44SVANHULLEBUS Yvan 		default:
11127b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
11137b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
11147b495c44SVANHULLEBUS Yvan 			break;
11157b495c44SVANHULLEBUS Yvan 		}
11167b495c44SVANHULLEBUS Yvan 		break;
11177b495c44SVANHULLEBUS Yvan 	}
11187b495c44SVANHULLEBUS Yvan 	return (error);
11197b495c44SVANHULLEBUS Yvan }
11207b495c44SVANHULLEBUS Yvan 
112179288c11SBjoern A. Zeeb #ifdef INET
1122fa046d87SRobert Watson #define	UH_WLOCKED	2
1123fa046d87SRobert Watson #define	UH_RLOCKED	1
1124fa046d87SRobert Watson #define	UH_UNLOCKED	0
1125490d50b6SBrian Feldman static int
11263329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
11273329b236SRobert Watson     struct mbuf *control, struct thread *td)
1128df8bae1dSRodney W. Grimes {
11293329b236SRobert Watson 	struct udpiphdr *ui;
11303329b236SRobert Watson 	int len = m->m_pkthdr.len;
113190162a4eSIan Dowse 	struct in_addr faddr, laddr;
1132c557ae16SIan Dowse 	struct cmsghdr *cm;
1133e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1134c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
1135e06e816fSKevin Lo 	int cscov_partial = 0;
113690162a4eSIan Dowse 	int error = 0;
11378afa2304SBruce M Simpson 	int ipflags;
113890162a4eSIan Dowse 	u_short fport, lport;
113984cc0778SGeorge V. Neville-Neil 	int unlock_udbinfo, unlock_inp;
1140f584d74bSMichael Tuexen 	u_char tos;
1141e06e816fSKevin Lo 	uint8_t pr;
1142e06e816fSKevin Lo 	uint16_t cscov = 0;
11439d3ddf43SAdrian Chadd 	uint32_t flowid = 0;
1144c2529042SHans Petter Selasky 	uint8_t flowtype = M_HASHTYPE_NONE;
1145df8bae1dSRodney W. Grimes 
11465c32ea65SRobert Watson 	/*
11475c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
1148f5514f08SRobert Watson 	 * inpcb.  As such, we don't know up front whether we will need the
1149f5514f08SRobert Watson 	 * pcbinfo lock or not.  Do any work to decide what is needed up
1150f5514f08SRobert Watson 	 * front before acquiring any locks.
11515c32ea65SRobert Watson 	 */
1152430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1153c557ae16SIan Dowse 		if (control)
1154c557ae16SIan Dowse 			m_freem(control);
11555c32ea65SRobert Watson 		m_freem(m);
11563329b236SRobert Watson 		return (EMSGSIZE);
1157430d30d8SBill Fenner 	}
1158430d30d8SBill Fenner 
11591b7f0384SBruce M Simpson 	src.sin_family = 0;
116084cc0778SGeorge V. Neville-Neil 	sin = (struct sockaddr_in *)addr;
116184cc0778SGeorge V. Neville-Neil 	if (sin == NULL ||
116284cc0778SGeorge V. Neville-Neil 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
116384cc0778SGeorge V. Neville-Neil 		INP_WLOCK(inp);
116484cc0778SGeorge V. Neville-Neil 		unlock_inp = UH_WLOCKED;
116584cc0778SGeorge V. Neville-Neil 	} else {
11660cfdff24SBjoern A. Zeeb 		INP_RLOCK(inp);
116784cc0778SGeorge V. Neville-Neil 		unlock_inp = UH_RLOCKED;
116884cc0778SGeorge V. Neville-Neil 	}
1169f584d74bSMichael Tuexen 	tos = inp->inp_ip_tos;
1170c557ae16SIan Dowse 	if (control != NULL) {
1171c557ae16SIan Dowse 		/*
11723329b236SRobert Watson 		 * XXX: Currently, we assume all the optional information is
11733329b236SRobert Watson 		 * stored in a single mbuf.
1174c557ae16SIan Dowse 		 */
1175c557ae16SIan Dowse 		if (control->m_next) {
117684cc0778SGeorge V. Neville-Neil 			if (unlock_inp == UH_WLOCKED)
117784cc0778SGeorge V. Neville-Neil 				INP_WUNLOCK(inp);
117884cc0778SGeorge V. Neville-Neil 			else
11790cfdff24SBjoern A. Zeeb 				INP_RUNLOCK(inp);
1180c557ae16SIan Dowse 			m_freem(control);
11815c32ea65SRobert Watson 			m_freem(m);
11823329b236SRobert Watson 			return (EINVAL);
1183c557ae16SIan Dowse 		}
1184c557ae16SIan Dowse 		for (; control->m_len > 0;
1185c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
1186c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1187c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
1188af1ee11dSRobert Watson 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
1189af1ee11dSRobert Watson 			    || cm->cmsg_len > control->m_len) {
1190c557ae16SIan Dowse 				error = EINVAL;
1191c557ae16SIan Dowse 				break;
1192c557ae16SIan Dowse 			}
1193c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
1194c557ae16SIan Dowse 				continue;
1195c557ae16SIan Dowse 
1196c557ae16SIan Dowse 			switch (cm->cmsg_type) {
1197c557ae16SIan Dowse 			case IP_SENDSRCADDR:
1198c557ae16SIan Dowse 				if (cm->cmsg_len !=
1199c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
1200c557ae16SIan Dowse 					error = EINVAL;
1201c557ae16SIan Dowse 					break;
1202c557ae16SIan Dowse 				}
1203c557ae16SIan Dowse 				bzero(&src, sizeof(src));
1204c557ae16SIan Dowse 				src.sin_family = AF_INET;
1205c557ae16SIan Dowse 				src.sin_len = sizeof(src);
1206c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
1207af1ee11dSRobert Watson 				src.sin_addr =
1208af1ee11dSRobert Watson 				    *(struct in_addr *)CMSG_DATA(cm);
1209c557ae16SIan Dowse 				break;
1210af1ee11dSRobert Watson 
1211f584d74bSMichael Tuexen 			case IP_TOS:
1212f584d74bSMichael Tuexen 				if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
1213f584d74bSMichael Tuexen 					error = EINVAL;
1214f584d74bSMichael Tuexen 					break;
1215f584d74bSMichael Tuexen 				}
1216f584d74bSMichael Tuexen 				tos = *(u_char *)CMSG_DATA(cm);
1217f584d74bSMichael Tuexen 				break;
1218f584d74bSMichael Tuexen 
12199d3ddf43SAdrian Chadd 			case IP_FLOWID:
12209d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
12219d3ddf43SAdrian Chadd 					error = EINVAL;
12229d3ddf43SAdrian Chadd 					break;
12239d3ddf43SAdrian Chadd 				}
12249d3ddf43SAdrian Chadd 				flowid = *(uint32_t *) CMSG_DATA(cm);
12259d3ddf43SAdrian Chadd 				break;
12269d3ddf43SAdrian Chadd 
12279d3ddf43SAdrian Chadd 			case IP_FLOWTYPE:
12289d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
12299d3ddf43SAdrian Chadd 					error = EINVAL;
12309d3ddf43SAdrian Chadd 					break;
12319d3ddf43SAdrian Chadd 				}
1232c2529042SHans Petter Selasky 				flowtype = *(uint32_t *) CMSG_DATA(cm);
12339d3ddf43SAdrian Chadd 				break;
12349d3ddf43SAdrian Chadd 
12359d3ddf43SAdrian Chadd #ifdef	RSS
12369d3ddf43SAdrian Chadd 			case IP_RSSBUCKETID:
12379d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
12389d3ddf43SAdrian Chadd 					error = EINVAL;
12399d3ddf43SAdrian Chadd 					break;
12409d3ddf43SAdrian Chadd 				}
12419d3ddf43SAdrian Chadd 				/* This is just a placeholder for now */
12429d3ddf43SAdrian Chadd 				break;
12439d3ddf43SAdrian Chadd #endif	/* RSS */
1244c557ae16SIan Dowse 			default:
1245c557ae16SIan Dowse 				error = ENOPROTOOPT;
1246c557ae16SIan Dowse 				break;
1247c557ae16SIan Dowse 			}
1248c557ae16SIan Dowse 			if (error)
1249c557ae16SIan Dowse 				break;
1250c557ae16SIan Dowse 		}
1251c557ae16SIan Dowse 		m_freem(control);
1252c557ae16SIan Dowse 	}
12535c32ea65SRobert Watson 	if (error) {
125484cc0778SGeorge V. Neville-Neil 		if (unlock_inp == UH_WLOCKED)
125584cc0778SGeorge V. Neville-Neil 			INP_WUNLOCK(inp);
125684cc0778SGeorge V. Neville-Neil 		else
12570cfdff24SBjoern A. Zeeb 			INP_RUNLOCK(inp);
12585c32ea65SRobert Watson 		m_freem(m);
12593329b236SRobert Watson 		return (error);
12605c32ea65SRobert Watson 	}
12615c32ea65SRobert Watson 
126243cc0bc1SRobert Watson 	/*
126343cc0bc1SRobert Watson 	 * Depending on whether or not the application has bound or connected
1264ca528788SRobert Watson 	 * the socket, we may have to do varying levels of work.  The optimal
1265ca528788SRobert Watson 	 * case is for a connected UDP socket, as a global lock isn't
1266ca528788SRobert Watson 	 * required at all.
126743cc0bc1SRobert Watson 	 *
126843cc0bc1SRobert Watson 	 * In order to decide which we need, we require stability of the
126943cc0bc1SRobert Watson 	 * inpcb binding, which we ensure by acquiring a read lock on the
127043cc0bc1SRobert Watson 	 * inpcb.  This doesn't strictly follow the lock order, so we play
127143cc0bc1SRobert Watson 	 * the trylock and retry game; note that we may end up with more
127243cc0bc1SRobert Watson 	 * conservative locks than required the second time around, so later
127343cc0bc1SRobert Watson 	 * assertions have to accept that.  Further analysis of the number of
127443cc0bc1SRobert Watson 	 * misses under contention is required.
1275fa046d87SRobert Watson 	 *
1276fa046d87SRobert Watson 	 * XXXRW: Check that hash locking update here is correct.
127743cc0bc1SRobert Watson 	 */
1278e06e816fSKevin Lo 	pr = inp->inp_socket->so_proto->pr_protocol;
1279a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(pr);
128043cc0bc1SRobert Watson 	sin = (struct sockaddr_in *)addr;
128143cc0bc1SRobert Watson 	if (sin != NULL &&
128243cc0bc1SRobert Watson 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
1283e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1284fa046d87SRobert Watson 		unlock_udbinfo = UH_WLOCKED;
128543cc0bc1SRobert Watson 	} else if ((sin != NULL && (
128643cc0bc1SRobert Watson 	    (sin->sin_addr.s_addr == INADDR_ANY) ||
128743cc0bc1SRobert Watson 	    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
128843cc0bc1SRobert Watson 	    (inp->inp_laddr.s_addr == INADDR_ANY) ||
128943cc0bc1SRobert Watson 	    (inp->inp_lport == 0))) ||
129043cc0bc1SRobert Watson 	    (src.sin_family == AF_INET)) {
1291e06e816fSKevin Lo 		INP_HASH_RLOCK(pcbinfo);
1292fa046d87SRobert Watson 		unlock_udbinfo = UH_RLOCKED;
129343cc0bc1SRobert Watson 	} else
1294fa046d87SRobert Watson 		unlock_udbinfo = UH_UNLOCKED;
12955c32ea65SRobert Watson 
12961b7f0384SBruce M Simpson 	/*
12971b7f0384SBruce M Simpson 	 * If the IP_SENDSRCADDR control message was specified, override the
12981b7f0384SBruce M Simpson 	 * source address for this datagram.  Its use is invalidated if the
12991b7f0384SBruce M Simpson 	 * address thus specified is incomplete or clobbers other inpcbs.
13001b7f0384SBruce M Simpson 	 */
130190162a4eSIan Dowse 	laddr = inp->inp_laddr;
130290162a4eSIan Dowse 	lport = inp->inp_lport;
13031b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET) {
1304e06e816fSKevin Lo 		INP_HASH_LOCK_ASSERT(pcbinfo);
13051b7f0384SBruce M Simpson 		if ((lport == 0) ||
13061b7f0384SBruce M Simpson 		    (laddr.s_addr == INADDR_ANY &&
13071b7f0384SBruce M Simpson 		     src.sin_addr.s_addr == INADDR_ANY)) {
1308c557ae16SIan Dowse 			error = EINVAL;
1309c557ae16SIan Dowse 			goto release;
1310c557ae16SIan Dowse 		}
1311c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
1312b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
1313c557ae16SIan Dowse 		if (error)
1314c557ae16SIan Dowse 			goto release;
1315c557ae16SIan Dowse 	}
1316c557ae16SIan Dowse 
13173144b7d3SRobert Watson 	/*
13183144b7d3SRobert Watson 	 * If a UDP socket has been connected, then a local address/port will
13193144b7d3SRobert Watson 	 * have been selected and bound.
13203144b7d3SRobert Watson 	 *
132143cc0bc1SRobert Watson 	 * If a UDP socket has not been connected to, then an explicit
13223144b7d3SRobert Watson 	 * destination address must be used, in which case a local
13233144b7d3SRobert Watson 	 * address/port may not have been selected and bound.
13243144b7d3SRobert Watson 	 */
132543cc0bc1SRobert Watson 	if (sin != NULL) {
1326c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
1327df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1328df8bae1dSRodney W. Grimes 			error = EISCONN;
1329df8bae1dSRodney W. Grimes 			goto release;
1330df8bae1dSRodney W. Grimes 		}
13313144b7d3SRobert Watson 
13323144b7d3SRobert Watson 		/*
13333144b7d3SRobert Watson 		 * Jail may rewrite the destination address, so let it do
13343144b7d3SRobert Watson 		 * that before we use it.
13353144b7d3SRobert Watson 		 */
1336b89e82ddSJamie Gritton 		error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1337b89e82ddSJamie Gritton 		if (error)
1338413628a7SBjoern A. Zeeb 			goto release;
13393144b7d3SRobert Watson 
13403144b7d3SRobert Watson 		/*
134143cc0bc1SRobert Watson 		 * If a local address or port hasn't yet been selected, or if
134243cc0bc1SRobert Watson 		 * the destination address needs to be rewritten due to using
134343cc0bc1SRobert Watson 		 * a special INADDR_ constant, invoke in_pcbconnect_setup()
134443cc0bc1SRobert Watson 		 * to do the heavy lifting.  Once a port is selected, we
134543cc0bc1SRobert Watson 		 * commit the binding back to the socket; we also commit the
134643cc0bc1SRobert Watson 		 * binding of the address if in jail.
134743cc0bc1SRobert Watson 		 *
134843cc0bc1SRobert Watson 		 * If we already have a valid binding and we're not
134943cc0bc1SRobert Watson 		 * requesting a destination address rewrite, use a fast path.
13503144b7d3SRobert Watson 		 */
135143cc0bc1SRobert Watson 		if (inp->inp_laddr.s_addr == INADDR_ANY ||
135243cc0bc1SRobert Watson 		    inp->inp_lport == 0 ||
135343cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_ANY ||
135443cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_BROADCAST) {
1355e06e816fSKevin Lo 			INP_HASH_LOCK_ASSERT(pcbinfo);
135643cc0bc1SRobert Watson 			error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
135743cc0bc1SRobert Watson 			    &lport, &faddr.s_addr, &fport, NULL,
135843cc0bc1SRobert Watson 			    td->td_ucred);
135990162a4eSIan Dowse 			if (error)
136090162a4eSIan Dowse 				goto release;
136190162a4eSIan Dowse 
136243cc0bc1SRobert Watson 			/*
136343cc0bc1SRobert Watson 			 * XXXRW: Why not commit the port if the address is
136443cc0bc1SRobert Watson 			 * !INADDR_ANY?
136543cc0bc1SRobert Watson 			 */
136690162a4eSIan Dowse 			/* Commit the local port if newly assigned. */
136790162a4eSIan Dowse 			if (inp->inp_laddr.s_addr == INADDR_ANY &&
136890162a4eSIan Dowse 			    inp->inp_lport == 0) {
1369c4d585aeSRobert Watson 				INP_WLOCK_ASSERT(inp);
1370e06e816fSKevin Lo 				INP_HASH_WLOCK_ASSERT(pcbinfo);
13713a1757b9SGleb Smirnoff 				/*
137243cc0bc1SRobert Watson 				 * Remember addr if jailed, to prevent
137343cc0bc1SRobert Watson 				 * rebinding.
13743a1757b9SGleb Smirnoff 				 */
13750304c731SJamie Gritton 				if (prison_flag(td->td_ucred, PR_IP4))
13763a1757b9SGleb Smirnoff 					inp->inp_laddr = laddr;
137790162a4eSIan Dowse 				inp->inp_lport = lport;
137890162a4eSIan Dowse 				if (in_pcbinshash(inp) != 0) {
137990162a4eSIan Dowse 					inp->inp_lport = 0;
138090162a4eSIan Dowse 					error = EAGAIN;
1381df8bae1dSRodney W. Grimes 					goto release;
1382df8bae1dSRodney W. Grimes 				}
138390162a4eSIan Dowse 				inp->inp_flags |= INP_ANONPORT;
138490162a4eSIan Dowse 			}
1385df8bae1dSRodney W. Grimes 		} else {
138643cc0bc1SRobert Watson 			faddr = sin->sin_addr;
138743cc0bc1SRobert Watson 			fport = sin->sin_port;
138843cc0bc1SRobert Watson 		}
138943cc0bc1SRobert Watson 	} else {
1390c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
139190162a4eSIan Dowse 		faddr = inp->inp_faddr;
139290162a4eSIan Dowse 		fport = inp->inp_fport;
139390162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
1394df8bae1dSRodney W. Grimes 			error = ENOTCONN;
1395df8bae1dSRodney W. Grimes 			goto release;
1396df8bae1dSRodney W. Grimes 		}
1397df8bae1dSRodney W. Grimes 	}
1398e6ccd709SRobert Watson 
1399df8bae1dSRodney W. Grimes 	/*
1400e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
1401392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
1402392e8407SRobert Watson 	 * since we won't use that space at this layer.
1403df8bae1dSRodney W. Grimes 	 */
1404eb1b1807SGleb Smirnoff 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1405e6ccd709SRobert Watson 	if (m == NULL) {
1406df8bae1dSRodney W. Grimes 		error = ENOBUFS;
140749b19bfcSBruce M Simpson 		goto release;
1408df8bae1dSRodney W. Grimes 	}
1409e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
1410e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
1411392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
1412df8bae1dSRodney W. Grimes 
1413df8bae1dSRodney W. Grimes 	/*
14143329b236SRobert Watson 	 * Fill in mbuf with extended UDP header and addresses and length put
14153329b236SRobert Watson 	 * into network format.
1416df8bae1dSRodney W. Grimes 	 */
1417df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
1418db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
1419e06e816fSKevin Lo 	ui->ui_pr = pr;
142090162a4eSIan Dowse 	ui->ui_src = laddr;
142190162a4eSIan Dowse 	ui->ui_dst = faddr;
142290162a4eSIan Dowse 	ui->ui_sport = lport;
142390162a4eSIan Dowse 	ui->ui_dport = fport;
1424db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1425e06e816fSKevin Lo 	if (pr == IPPROTO_UDPLITE) {
1426e06e816fSKevin Lo 		struct udpcb *up;
1427e06e816fSKevin Lo 		uint16_t plen;
1428e06e816fSKevin Lo 
1429e06e816fSKevin Lo 		up = intoudpcb(inp);
1430e06e816fSKevin Lo 		cscov = up->u_txcslen;
1431e06e816fSKevin Lo 		plen = (u_short)len + sizeof(struct udphdr);
1432e06e816fSKevin Lo 		if (cscov >= plen)
1433e06e816fSKevin Lo 			cscov = 0;
1434e06e816fSKevin Lo 		ui->ui_len = htons(plen);
1435e06e816fSKevin Lo 		ui->ui_ulen = htons(cscov);
1436e06e816fSKevin Lo 		/*
1437e06e816fSKevin Lo 		 * For UDP-Lite, checksum coverage length of zero means
1438e06e816fSKevin Lo 		 * the entire UDPLite packet is covered by the checksum.
1439e06e816fSKevin Lo 		 */
1440e06e816fSKevin Lo 		cscov_partial = (cscov == 0) ? 0 : 1;
1441e06e816fSKevin Lo 	} else
1442e06e816fSKevin Lo 		ui->ui_v = IPVERSION << 4;
1443df8bae1dSRodney W. Grimes 
1444b2828ad2SAndre Oppermann 	/*
1445b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
1446b2828ad2SAndre Oppermann 	 */
1447b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
1448b2828ad2SAndre Oppermann 		struct ip *ip;
14493329b236SRobert Watson 
1450b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
14518f134647SGleb Smirnoff 		ip->ip_off |= htons(IP_DF);
1452b2828ad2SAndre Oppermann 	}
1453b2828ad2SAndre Oppermann 
1454b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
1455b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
1456b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
1457b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
1458b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
14596fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
14608afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
14618afa2304SBruce M Simpson 
14621175d9d5SRobert Watson #ifdef MAC
14631175d9d5SRobert Watson 	mac_inpcb_create_mbuf(inp, m);
14641175d9d5SRobert Watson #endif
14651175d9d5SRobert Watson 
1466df8bae1dSRodney W. Grimes 	/*
1467db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
1468df8bae1dSRodney W. Grimes 	 */
1469e06e816fSKevin Lo 	ui->ui_sum = 0;
1470a485f139SMichael Tuexen 	if (pr == IPPROTO_UDPLITE) {
1471e06e816fSKevin Lo 		if (inp->inp_flags & INP_ONESBCAST)
1472e06e816fSKevin Lo 			faddr.s_addr = INADDR_BROADCAST;
1473a485f139SMichael Tuexen 		if (cscov_partial) {
1474e06e816fSKevin Lo 			if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1475e06e816fSKevin Lo 				ui->ui_sum = 0xffff;
1476a485f139SMichael Tuexen 		} else {
1477a485f139SMichael Tuexen 			if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1478a485f139SMichael Tuexen 				ui->ui_sum = 0xffff;
1479a485f139SMichael Tuexen 		}
1480a485f139SMichael Tuexen 	} else if (V_udp_cksum) {
14816fbfd582SAndre Oppermann 		if (inp->inp_flags & INP_ONESBCAST)
14828a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
14838a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1484e06e816fSKevin Lo 		    htons((u_short)len + sizeof(struct udphdr) + pr));
1485db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
1486db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1487e06e816fSKevin Lo 	}
14888f134647SGleb Smirnoff 	((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1489ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
1490f584d74bSMichael Tuexen 	((struct ip *)ui)->ip_tos = tos;		/* XXX */
1491026decb8SRobert Watson 	UDPSTAT_INC(udps_opackets);
1492cfa1ca9dSYoshinobu Inoue 
14939d3ddf43SAdrian Chadd 	/*
14949d3ddf43SAdrian Chadd 	 * Setup flowid / RSS information for outbound socket.
14959d3ddf43SAdrian Chadd 	 *
14969d3ddf43SAdrian Chadd 	 * Once the UDP code decides to set a flowid some other way,
14979d3ddf43SAdrian Chadd 	 * this allows the flowid to be overridden by userland.
14989d3ddf43SAdrian Chadd 	 */
1499c2529042SHans Petter Selasky 	if (flowtype != M_HASHTYPE_NONE) {
15009d3ddf43SAdrian Chadd 		m->m_pkthdr.flowid = flowid;
1501c2529042SHans Petter Selasky 		M_HASHTYPE_SET(m, flowtype);
15028ad1a83bSAdrian Chadd #ifdef	RSS
15038ad1a83bSAdrian Chadd 	} else {
15048ad1a83bSAdrian Chadd 		uint32_t hash_val, hash_type;
15058ad1a83bSAdrian Chadd 		/*
15068ad1a83bSAdrian Chadd 		 * Calculate an appropriate RSS hash for UDP and
15078ad1a83bSAdrian Chadd 		 * UDP Lite.
15088ad1a83bSAdrian Chadd 		 *
15098ad1a83bSAdrian Chadd 		 * The called function will take care of figuring out
15108ad1a83bSAdrian Chadd 		 * whether a 2-tuple or 4-tuple hash is required based
15118ad1a83bSAdrian Chadd 		 * on the currently configured scheme.
15128ad1a83bSAdrian Chadd 		 *
15138ad1a83bSAdrian Chadd 		 * Later later on connected socket values should be
15148ad1a83bSAdrian Chadd 		 * cached in the inpcb and reused, rather than constantly
15158ad1a83bSAdrian Chadd 		 * re-calculating it.
15168ad1a83bSAdrian Chadd 		 *
15178ad1a83bSAdrian Chadd 		 * UDP Lite is a different protocol number and will
15188ad1a83bSAdrian Chadd 		 * likely end up being hashed as a 2-tuple until
15198ad1a83bSAdrian Chadd 		 * RSS / NICs grow UDP Lite protocol awareness.
15208ad1a83bSAdrian Chadd 		 */
15218ad1a83bSAdrian Chadd 		if (rss_proto_software_hash_v4(faddr, laddr, fport, lport,
15228ad1a83bSAdrian Chadd 		    pr, &hash_val, &hash_type) == 0) {
15238ad1a83bSAdrian Chadd 			m->m_pkthdr.flowid = hash_val;
15248ad1a83bSAdrian Chadd 			M_HASHTYPE_SET(m, hash_type);
15258ad1a83bSAdrian Chadd 		}
15268ad1a83bSAdrian Chadd #endif
15279d3ddf43SAdrian Chadd 	}
15289d3ddf43SAdrian Chadd 
15299d3ddf43SAdrian Chadd #ifdef	RSS
15308ad1a83bSAdrian Chadd 	/*
15318ad1a83bSAdrian Chadd 	 * Don't override with the inp cached flowid value.
15328ad1a83bSAdrian Chadd 	 *
15338ad1a83bSAdrian Chadd 	 * Depending upon the kind of send being done, the inp
15348ad1a83bSAdrian Chadd 	 * flowid/flowtype values may actually not be appropriate
15358ad1a83bSAdrian Chadd 	 * for this particular socket send.
15368ad1a83bSAdrian Chadd 	 *
15378ad1a83bSAdrian Chadd 	 * We should either leave the flowid at zero (which is what is
15388ad1a83bSAdrian Chadd 	 * currently done) or set it to some software generated
15398ad1a83bSAdrian Chadd 	 * hash value based on the packet contents.
15408ad1a83bSAdrian Chadd 	 */
15419d3ddf43SAdrian Chadd 	ipflags |= IP_NODEFAULTFLOWID;
15429d3ddf43SAdrian Chadd #endif	/* RSS */
15439d3ddf43SAdrian Chadd 
1544fa046d87SRobert Watson 	if (unlock_udbinfo == UH_WLOCKED)
1545e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1546fa046d87SRobert Watson 	else if (unlock_udbinfo == UH_RLOCKED)
1547e06e816fSKevin Lo 		INP_HASH_RUNLOCK(pcbinfo);
154857f60867SMark Johnston 	UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
154984cc0778SGeorge V. Neville-Neil 	error = ip_output(m, inp->inp_options,
155084cc0778SGeorge V. Neville-Neil 	    (unlock_inp == UH_WLOCKED ? &inp->inp_route : NULL), ipflags,
15515d846453SSam Leffler 	    inp->inp_moptions, inp);
155284cc0778SGeorge V. Neville-Neil 	if (unlock_inp == UH_WLOCKED)
15538501a69cSRobert Watson 		INP_WUNLOCK(inp);
1554948d0fc9SRobert Watson 	else
1555948d0fc9SRobert Watson 		INP_RUNLOCK(inp);
1556df8bae1dSRodney W. Grimes 	return (error);
1557df8bae1dSRodney W. Grimes 
1558df8bae1dSRodney W. Grimes release:
1559fa046d87SRobert Watson 	if (unlock_udbinfo == UH_WLOCKED) {
1560e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1561948d0fc9SRobert Watson 		INP_WUNLOCK(inp);
1562fa046d87SRobert Watson 	} else if (unlock_udbinfo == UH_RLOCKED) {
1563e06e816fSKevin Lo 		INP_HASH_RUNLOCK(pcbinfo);
156443cc0bc1SRobert Watson 		INP_RUNLOCK(inp);
1565948d0fc9SRobert Watson 	} else
1566948d0fc9SRobert Watson 		INP_RUNLOCK(inp);
1567df8bae1dSRodney W. Grimes 	m_freem(m);
1568df8bae1dSRodney W. Grimes 	return (error);
1569df8bae1dSRodney W. Grimes }
1570df8bae1dSRodney W. Grimes 
15717b495c44SVANHULLEBUS Yvan 
15727b495c44SVANHULLEBUS Yvan #if defined(IPSEC) && defined(IPSEC_NAT_T)
15737b495c44SVANHULLEBUS Yvan /*
15747b495c44SVANHULLEBUS Yvan  * Potentially decap ESP in UDP frame.  Check for an ESP header
15757b495c44SVANHULLEBUS Yvan  * and optional marker; if present, strip the UDP header and
15767b495c44SVANHULLEBUS Yvan  * push the result through IPSec.
15777b495c44SVANHULLEBUS Yvan  *
15787b495c44SVANHULLEBUS Yvan  * Returns mbuf to be processed (potentially re-allocated) or
15797b495c44SVANHULLEBUS Yvan  * NULL if consumed and/or processed.
15807b495c44SVANHULLEBUS Yvan  */
15817b495c44SVANHULLEBUS Yvan static struct mbuf *
15827b495c44SVANHULLEBUS Yvan udp4_espdecap(struct inpcb *inp, struct mbuf *m, int off)
15837b495c44SVANHULLEBUS Yvan {
15847b495c44SVANHULLEBUS Yvan 	size_t minlen, payload, skip, iphlen;
15857b495c44SVANHULLEBUS Yvan 	caddr_t data;
15867b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
15877b495c44SVANHULLEBUS Yvan 	struct m_tag *tag;
15887b495c44SVANHULLEBUS Yvan 	struct udphdr *udphdr;
15897b495c44SVANHULLEBUS Yvan 	struct ip *ip;
15907b495c44SVANHULLEBUS Yvan 
15917b495c44SVANHULLEBUS Yvan 	INP_RLOCK_ASSERT(inp);
15927b495c44SVANHULLEBUS Yvan 
15937b495c44SVANHULLEBUS Yvan 	/*
15947b495c44SVANHULLEBUS Yvan 	 * Pull up data so the longest case is contiguous:
15957b495c44SVANHULLEBUS Yvan 	 *    IP/UDP hdr + non ESP marker + ESP hdr.
15967b495c44SVANHULLEBUS Yvan 	 */
15977b495c44SVANHULLEBUS Yvan 	minlen = off + sizeof(uint64_t) + sizeof(struct esp);
15987b495c44SVANHULLEBUS Yvan 	if (minlen > m->m_pkthdr.len)
15997b495c44SVANHULLEBUS Yvan 		minlen = m->m_pkthdr.len;
16007b495c44SVANHULLEBUS Yvan 	if ((m = m_pullup(m, minlen)) == NULL) {
16016794f460SAndrey V. Elsukov 		IPSECSTAT_INC(ips_in_inval);
16027b495c44SVANHULLEBUS Yvan 		return (NULL);		/* Bypass caller processing. */
16037b495c44SVANHULLEBUS Yvan 	}
16047b495c44SVANHULLEBUS Yvan 	data = mtod(m, caddr_t);	/* Points to ip header. */
16057b495c44SVANHULLEBUS Yvan 	payload = m->m_len - off;	/* Size of payload. */
16067b495c44SVANHULLEBUS Yvan 
16077b495c44SVANHULLEBUS Yvan 	if (payload == 1 && data[off] == '\xff')
16087b495c44SVANHULLEBUS Yvan 		return (m);		/* NB: keepalive packet, no decap. */
16097b495c44SVANHULLEBUS Yvan 
16107b495c44SVANHULLEBUS Yvan 	up = intoudpcb(inp);
16117b495c44SVANHULLEBUS Yvan 	KASSERT(up != NULL, ("%s: udpcb NULL", __func__));
16127b495c44SVANHULLEBUS Yvan 	KASSERT((up->u_flags & UF_ESPINUDP_ALL) != 0,
16137b495c44SVANHULLEBUS Yvan 	    ("u_flags 0x%x", up->u_flags));
16147b495c44SVANHULLEBUS Yvan 
16157b495c44SVANHULLEBUS Yvan 	/*
16167b495c44SVANHULLEBUS Yvan 	 * Check that the payload is large enough to hold an
16177b495c44SVANHULLEBUS Yvan 	 * ESP header and compute the amount of data to remove.
16187b495c44SVANHULLEBUS Yvan 	 *
16197b495c44SVANHULLEBUS Yvan 	 * NB: the caller has already done a pullup for us.
16207b495c44SVANHULLEBUS Yvan 	 * XXX can we assume alignment and eliminate bcopys?
16217b495c44SVANHULLEBUS Yvan 	 */
16227b495c44SVANHULLEBUS Yvan 	if (up->u_flags & UF_ESPINUDP_NON_IKE) {
16237b495c44SVANHULLEBUS Yvan 		/*
16247b495c44SVANHULLEBUS Yvan 		 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
16257b495c44SVANHULLEBUS Yvan 		 * draft-ietf-ipsec-udp-encaps-(00/)01.txt, ignoring
16267b495c44SVANHULLEBUS Yvan 		 * possible AH mode non-IKE marker+non-ESP marker
16277b495c44SVANHULLEBUS Yvan 		 * from draft-ietf-ipsec-udp-encaps-00.txt.
16287b495c44SVANHULLEBUS Yvan 		 */
16297b495c44SVANHULLEBUS Yvan 		uint64_t marker;
16307b495c44SVANHULLEBUS Yvan 
16317b495c44SVANHULLEBUS Yvan 		if (payload <= sizeof(uint64_t) + sizeof(struct esp))
16327b495c44SVANHULLEBUS Yvan 			return (m);	/* NB: no decap. */
16337b495c44SVANHULLEBUS Yvan 		bcopy(data + off, &marker, sizeof(uint64_t));
16347b495c44SVANHULLEBUS Yvan 		if (marker != 0)	/* Non-IKE marker. */
16357b495c44SVANHULLEBUS Yvan 			return (m);	/* NB: no decap. */
16367b495c44SVANHULLEBUS Yvan 		skip = sizeof(uint64_t) + sizeof(struct udphdr);
16377b495c44SVANHULLEBUS Yvan 	} else {
16387b495c44SVANHULLEBUS Yvan 		uint32_t spi;
16397b495c44SVANHULLEBUS Yvan 
16407b495c44SVANHULLEBUS Yvan 		if (payload <= sizeof(struct esp)) {
16416794f460SAndrey V. Elsukov 			IPSECSTAT_INC(ips_in_inval);
16427b495c44SVANHULLEBUS Yvan 			m_freem(m);
16437b495c44SVANHULLEBUS Yvan 			return (NULL);	/* Discard. */
16447b495c44SVANHULLEBUS Yvan 		}
16457b495c44SVANHULLEBUS Yvan 		bcopy(data + off, &spi, sizeof(uint32_t));
16467b495c44SVANHULLEBUS Yvan 		if (spi == 0)		/* Non-ESP marker. */
16477b495c44SVANHULLEBUS Yvan 			return (m);	/* NB: no decap. */
16487b495c44SVANHULLEBUS Yvan 		skip = sizeof(struct udphdr);
16497b495c44SVANHULLEBUS Yvan 	}
16507b495c44SVANHULLEBUS Yvan 
16517b495c44SVANHULLEBUS Yvan 	/*
16527b495c44SVANHULLEBUS Yvan 	 * Setup a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
16537b495c44SVANHULLEBUS Yvan 	 * the UDP ports. This is required if we want to select
16547b495c44SVANHULLEBUS Yvan 	 * the right SPD for multiple hosts behind same NAT.
16557b495c44SVANHULLEBUS Yvan 	 *
16567b495c44SVANHULLEBUS Yvan 	 * NB: ports are maintained in network byte order everywhere
16577b495c44SVANHULLEBUS Yvan 	 *     in the NAT-T code.
16587b495c44SVANHULLEBUS Yvan 	 */
16597b495c44SVANHULLEBUS Yvan 	tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
16607b495c44SVANHULLEBUS Yvan 		2 * sizeof(uint16_t), M_NOWAIT);
16617b495c44SVANHULLEBUS Yvan 	if (tag == NULL) {
16626794f460SAndrey V. Elsukov 		IPSECSTAT_INC(ips_in_nomem);
16637b495c44SVANHULLEBUS Yvan 		m_freem(m);
16647b495c44SVANHULLEBUS Yvan 		return (NULL);		/* Discard. */
16657b495c44SVANHULLEBUS Yvan 	}
16667b495c44SVANHULLEBUS Yvan 	iphlen = off - sizeof(struct udphdr);
16677b495c44SVANHULLEBUS Yvan 	udphdr = (struct udphdr *)(data + iphlen);
16687b495c44SVANHULLEBUS Yvan 	((uint16_t *)(tag + 1))[0] = udphdr->uh_sport;
16697b495c44SVANHULLEBUS Yvan 	((uint16_t *)(tag + 1))[1] = udphdr->uh_dport;
16707b495c44SVANHULLEBUS Yvan 	m_tag_prepend(m, tag);
16717b495c44SVANHULLEBUS Yvan 
16727b495c44SVANHULLEBUS Yvan 	/*
16737b495c44SVANHULLEBUS Yvan 	 * Remove the UDP header (and possibly the non ESP marker)
16747b495c44SVANHULLEBUS Yvan 	 * IP header length is iphlen
16757b495c44SVANHULLEBUS Yvan 	 * Before:
16767b495c44SVANHULLEBUS Yvan 	 *   <--- off --->
16777b495c44SVANHULLEBUS Yvan 	 *   +----+------+-----+
16787b495c44SVANHULLEBUS Yvan 	 *   | IP |  UDP | ESP |
16797b495c44SVANHULLEBUS Yvan 	 *   +----+------+-----+
16807b495c44SVANHULLEBUS Yvan 	 *        <-skip->
16817b495c44SVANHULLEBUS Yvan 	 * After:
16827b495c44SVANHULLEBUS Yvan 	 *          +----+-----+
16837b495c44SVANHULLEBUS Yvan 	 *          | IP | ESP |
16847b495c44SVANHULLEBUS Yvan 	 *          +----+-----+
16857b495c44SVANHULLEBUS Yvan 	 *   <-skip->
16867b495c44SVANHULLEBUS Yvan 	 */
16877b495c44SVANHULLEBUS Yvan 	ovbcopy(data, data + skip, iphlen);
16887b495c44SVANHULLEBUS Yvan 	m_adj(m, skip);
16897b495c44SVANHULLEBUS Yvan 
16907b495c44SVANHULLEBUS Yvan 	ip = mtod(m, struct ip *);
16918f134647SGleb Smirnoff 	ip->ip_len = htons(ntohs(ip->ip_len) - skip);
16927b495c44SVANHULLEBUS Yvan 	ip->ip_p = IPPROTO_ESP;
16937b495c44SVANHULLEBUS Yvan 
16947b495c44SVANHULLEBUS Yvan 	/*
16957b495c44SVANHULLEBUS Yvan 	 * We cannot yet update the cksums so clear any
16967b495c44SVANHULLEBUS Yvan 	 * h/w cksum flags as they are no longer valid.
16977b495c44SVANHULLEBUS Yvan 	 */
16987b495c44SVANHULLEBUS Yvan 	if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)
16997b495c44SVANHULLEBUS Yvan 		m->m_pkthdr.csum_flags &= ~(CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
17007b495c44SVANHULLEBUS Yvan 
1701705f4d9cSErmal Luçi 	(void) ipsec_common_input(m, iphlen, offsetof(struct ip, ip_p),
1702705f4d9cSErmal Luçi 				AF_INET, ip->ip_p);
17037b495c44SVANHULLEBUS Yvan 	return (NULL);			/* NB: consumed, bypass processing. */
17047b495c44SVANHULLEBUS Yvan }
17057b495c44SVANHULLEBUS Yvan #endif /* defined(IPSEC) && defined(IPSEC_NAT_T) */
17067b495c44SVANHULLEBUS Yvan 
1707ac45e92fSRobert Watson static void
1708d0390e05SGarrett Wollman udp_abort(struct socket *so)
1709df8bae1dSRodney W. Grimes {
1710d0390e05SGarrett Wollman 	struct inpcb *inp;
1711e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1712df8bae1dSRodney W. Grimes 
1713a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1714d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
171514ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
17168501a69cSRobert Watson 	INP_WLOCK(inp);
1717a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1718e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1719a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1720a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1721e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1722d0390e05SGarrett Wollman 		soisdisconnected(so);
1723a152f8a3SRobert Watson 	}
17248501a69cSRobert Watson 	INP_WUNLOCK(inp);
1725df8bae1dSRodney W. Grimes }
1726df8bae1dSRodney W. Grimes 
1727d0390e05SGarrett Wollman static int
1728b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
1729d0390e05SGarrett Wollman {
1730d0390e05SGarrett Wollman 	struct inpcb *inp;
1731e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1732277afaffSRobert Watson 	int error;
1733d0390e05SGarrett Wollman 
1734a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1735d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
173614ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1737cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
1738f24618aaSRobert Watson 	if (error)
17393329b236SRobert Watson 		return (error);
1740e06e816fSKevin Lo 	INP_INFO_WLOCK(pcbinfo);
1741e06e816fSKevin Lo 	error = in_pcballoc(so, pcbinfo);
174253b57cd1SSam Leffler 	if (error) {
1743e06e816fSKevin Lo 		INP_INFO_WUNLOCK(pcbinfo);
17443329b236SRobert Watson 		return (error);
174553b57cd1SSam Leffler 	}
1746cfa1ca9dSYoshinobu Inoue 
174768b5629bSRobert Watson 	inp = sotoinpcb(so);
1748cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1749603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
17506a9148feSBjoern A. Zeeb 
17516a9148feSBjoern A. Zeeb 	error = udp_newudpcb(inp);
17526a9148feSBjoern A. Zeeb 	if (error) {
17536a9148feSBjoern A. Zeeb 		in_pcbdetach(inp);
17546a9148feSBjoern A. Zeeb 		in_pcbfree(inp);
1755e06e816fSKevin Lo 		INP_INFO_WUNLOCK(pcbinfo);
17566a9148feSBjoern A. Zeeb 		return (error);
17576a9148feSBjoern A. Zeeb 	}
17586a9148feSBjoern A. Zeeb 
1759c7c7ea4bSRandall Stewart 	INP_WUNLOCK(inp);
1760e06e816fSKevin Lo 	INP_INFO_WUNLOCK(pcbinfo);
1761c7c7ea4bSRandall Stewart 	return (0);
1762c7c7ea4bSRandall Stewart }
176379288c11SBjoern A. Zeeb #endif /* INET */
1764c7c7ea4bSRandall Stewart 
1765c7c7ea4bSRandall Stewart int
1766*abb901c5SRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx)
1767c7c7ea4bSRandall Stewart {
1768c7c7ea4bSRandall Stewart 	struct inpcb *inp;
17696a9148feSBjoern A. Zeeb 	struct udpcb *up;
1770c7c7ea4bSRandall Stewart 
177168b5629bSRobert Watson 	KASSERT(so->so_type == SOCK_DGRAM,
177268b5629bSRobert Watson 	    ("udp_set_kernel_tunneling: !dgram"));
177368b5629bSRobert Watson 	inp = sotoinpcb(so);
177468b5629bSRobert Watson 	KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1775c7c7ea4bSRandall Stewart 	INP_WLOCK(inp);
17766a9148feSBjoern A. Zeeb 	up = intoudpcb(inp);
1777*abb901c5SRandall Stewart 	if ((up->u_tun_func != NULL) ||
1778*abb901c5SRandall Stewart 	    (up->u_icmp_func != NULL)) {
1779bbb0e3d9SRandall Stewart 		INP_WUNLOCK(inp);
1780bbb0e3d9SRandall Stewart 		return (EBUSY);
1781bbb0e3d9SRandall Stewart 	}
17826a9148feSBjoern A. Zeeb 	up->u_tun_func = f;
1783*abb901c5SRandall Stewart 	up->u_icmp_func = i;
178481d3ec17SBryan Venteicher 	up->u_tun_ctx = ctx;
17858501a69cSRobert Watson 	INP_WUNLOCK(inp);
17863329b236SRobert Watson 	return (0);
1787df8bae1dSRodney W. Grimes }
1788d0390e05SGarrett Wollman 
178979288c11SBjoern A. Zeeb #ifdef INET
1790d0390e05SGarrett Wollman static int
1791b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1792d0390e05SGarrett Wollman {
1793d0390e05SGarrett Wollman 	struct inpcb *inp;
1794e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1795277afaffSRobert Watson 	int error;
1796d0390e05SGarrett Wollman 
1797a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1798d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
179914ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
18008501a69cSRobert Watson 	INP_WLOCK(inp);
1801e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1802b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
1803e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
18048501a69cSRobert Watson 	INP_WUNLOCK(inp);
18053329b236SRobert Watson 	return (error);
1806d0390e05SGarrett Wollman }
1807d0390e05SGarrett Wollman 
1808a152f8a3SRobert Watson static void
1809a152f8a3SRobert Watson udp_close(struct socket *so)
1810a152f8a3SRobert Watson {
1811a152f8a3SRobert Watson 	struct inpcb *inp;
1812e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1813a152f8a3SRobert Watson 
1814a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1815a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1816a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
18178501a69cSRobert Watson 	INP_WLOCK(inp);
1818a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1819e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1820a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1821a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1822e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1823a152f8a3SRobert Watson 		soisdisconnected(so);
1824a152f8a3SRobert Watson 	}
18258501a69cSRobert Watson 	INP_WUNLOCK(inp);
1826a152f8a3SRobert Watson }
1827a152f8a3SRobert Watson 
1828d0390e05SGarrett Wollman static int
1829b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1830d0390e05SGarrett Wollman {
1831d0390e05SGarrett Wollman 	struct inpcb *inp;
1832e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
183375c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1834e06e816fSKevin Lo 	int error;
1835d0390e05SGarrett Wollman 
1836a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1837d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
183814ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
18398501a69cSRobert Watson 	INP_WLOCK(inp);
1840f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
18418501a69cSRobert Watson 		INP_WUNLOCK(inp);
18423329b236SRobert Watson 		return (EISCONN);
1843f76fcf6dSJeffrey Hsu 	}
184475c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1845b89e82ddSJamie Gritton 	error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1846b89e82ddSJamie Gritton 	if (error != 0) {
1847413628a7SBjoern A. Zeeb 		INP_WUNLOCK(inp);
1848b89e82ddSJamie Gritton 		return (error);
1849413628a7SBjoern A. Zeeb 	}
1850e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1851b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
1852e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
18534cc20ab1SSeigo Tanimura 	if (error == 0)
1854df8bae1dSRodney W. Grimes 		soisconnected(so);
18558501a69cSRobert Watson 	INP_WUNLOCK(inp);
18563329b236SRobert Watson 	return (error);
1857df8bae1dSRodney W. Grimes }
1858d0390e05SGarrett Wollman 
1859bc725eafSRobert Watson static void
1860d0390e05SGarrett Wollman udp_detach(struct socket *so)
1861d0390e05SGarrett Wollman {
1862d0390e05SGarrett Wollman 	struct inpcb *inp;
1863e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
18646a9148feSBjoern A. Zeeb 	struct udpcb *up;
1865d0390e05SGarrett Wollman 
1866a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1867d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
186814ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1869a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1870a152f8a3SRobert Watson 	    ("udp_detach: not disconnected"));
1871e06e816fSKevin Lo 	INP_INFO_WLOCK(pcbinfo);
18728501a69cSRobert Watson 	INP_WLOCK(inp);
18736a9148feSBjoern A. Zeeb 	up = intoudpcb(inp);
18746a9148feSBjoern A. Zeeb 	KASSERT(up != NULL, ("%s: up == NULL", __func__));
18756a9148feSBjoern A. Zeeb 	inp->inp_ppcb = NULL;
1876d0390e05SGarrett Wollman 	in_pcbdetach(inp);
187714ba8addSRobert Watson 	in_pcbfree(inp);
1878e06e816fSKevin Lo 	INP_INFO_WUNLOCK(pcbinfo);
18796a9148feSBjoern A. Zeeb 	udp_discardcb(up);
1880d0390e05SGarrett Wollman }
1881d0390e05SGarrett Wollman 
1882d0390e05SGarrett Wollman static int
1883d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1884d0390e05SGarrett Wollman {
1885d0390e05SGarrett Wollman 	struct inpcb *inp;
1886e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1887d0390e05SGarrett Wollman 
1888a86e5c96SBjoern A. Zeeb 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1889d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
189014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
18918501a69cSRobert Watson 	INP_WLOCK(inp);
1892f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
18938501a69cSRobert Watson 		INP_WUNLOCK(inp);
18943329b236SRobert Watson 		return (ENOTCONN);
1895f76fcf6dSJeffrey Hsu 	}
1896e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1897df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1898df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1899e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
1900d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1901d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1902d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
19038501a69cSRobert Watson 	INP_WUNLOCK(inp);
19043329b236SRobert Watson 	return (0);
1905df8bae1dSRodney W. Grimes }
1906df8bae1dSRodney W. Grimes 
1907d0390e05SGarrett Wollman static int
190857bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1909b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1910d0390e05SGarrett Wollman {
1911d0390e05SGarrett Wollman 	struct inpcb *inp;
1912d0390e05SGarrett Wollman 
1913d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
191414ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
19153329b236SRobert Watson 	return (udp_output(inp, m, addr, control, td));
1916d0390e05SGarrett Wollman }
191779288c11SBjoern A. Zeeb #endif /* INET */
1918d0390e05SGarrett Wollman 
191976429de4SYoshinobu Inoue int
1920d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1921d0390e05SGarrett Wollman {
1922d0390e05SGarrett Wollman 	struct inpcb *inp;
1923d0390e05SGarrett Wollman 
1924d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
192514ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
19268501a69cSRobert Watson 	INP_WLOCK(inp);
1927d0390e05SGarrett Wollman 	socantsendmore(so);
19288501a69cSRobert Watson 	INP_WUNLOCK(inp);
19293329b236SRobert Watson 	return (0);
1930d0390e05SGarrett Wollman }
1931d0390e05SGarrett Wollman 
193279288c11SBjoern A. Zeeb #ifdef INET
1933d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1934756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1935756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1936756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1937756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1938756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1939756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1940756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
194154d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1942756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
19435df3e839SRobert Watson 	.pru_soreceive =	soreceive_dgram,
194459b8854eSRobert Watson 	.pru_sosend =		sosend_dgram,
1945756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
194654d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1947a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1948a152f8a3SRobert Watson 	.pru_close =		udp_close,
1949d0390e05SGarrett Wollman };
195079288c11SBjoern A. Zeeb #endif /* INET */
1951