xref: /freebsd/sys/netinet/udp_usrreq.c (revision 0f4a03663b6c81caebededd6d0ffc9fda8de3209)
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>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <netinet/in.h>
7557f60867SMark Johnston #include <netinet/in_kdtrace.h>
76960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
77f5514f08SRobert Watson #include <netinet/in_systm.h>
78960ed29cSSeigo Tanimura #include <netinet/in_var.h>
79df8bae1dSRodney W. Grimes #include <netinet/ip.h>
80cfa1ca9dSYoshinobu Inoue #ifdef INET6
81cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
82cfa1ca9dSYoshinobu Inoue #endif
83960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
84960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
85df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
86ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
87cfa1ca9dSYoshinobu Inoue #ifdef INET6
88cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
89cfa1ca9dSYoshinobu Inoue #endif
90df8bae1dSRodney W. Grimes #include <netinet/udp.h>
91df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
92e06e816fSKevin Lo #include <netinet/udplite.h>
938ad1a83bSAdrian Chadd #include <netinet/in_rss.h>
94df8bae1dSRodney W. Grimes 
95b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
96b9234fafSSam Leffler #include <netipsec/ipsec.h>
977b495c44SVANHULLEBUS Yvan #include <netipsec/esp.h>
983329b236SRobert Watson #endif
99b9234fafSSam Leffler 
100db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
101db4f9cc7SJonathan Lemon 
102aed55708SRobert Watson #include <security/mac/mac_framework.h>
103aed55708SRobert Watson 
104df8bae1dSRodney W. Grimes /*
105e06e816fSKevin Lo  * UDP and UDP-Lite protocols implementation.
106df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
107e06e816fSKevin Lo  * Per RFC 3828, July, 2004.
108df8bae1dSRodney W. Grimes  */
10974eb3236SWarner Losh 
11074eb3236SWarner Losh /*
1113329b236SRobert Watson  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
1123329b236SRobert Watson  * removes the only data integrity mechanism for packets and malformed
113f5514f08SRobert Watson  * packets that would otherwise be discarded due to bad checksums, and may
114f5514f08SRobert Watson  * cause problems (especially for NFS data blocks).
11574eb3236SWarner Losh  */
11640b676beSBjoern A. Zeeb VNET_DEFINE(int, udp_cksum) = 1;
11740b676beSBjoern A. Zeeb SYSCTL_VNET_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
11840b676beSBjoern A. Zeeb     &VNET_NAME(udp_cksum), 0, "compute udp checksum");
119df8bae1dSRodney W. Grimes 
120afdb4274SRobert Watson int	udp_log_in_vain = 0;
121816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
122afdb4274SRobert Watson     &udp_log_in_vain, 0, "Log all incoming UDP packets");
123816a3d83SPoul-Henning Kamp 
12482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, udp_blackhole) = 0;
125eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
126eddfbb76SRobert Watson     &VNET_NAME(udp_blackhole), 0,
1273329b236SRobert Watson     "Do not send port unreachables for refused connects");
12816f7f31fSGeoff Rehmet 
12943bbb6aaSRobert Watson u_long	udp_sendspace = 9216;		/* really max datagram size */
13043bbb6aaSRobert Watson 					/* 40 1K datagrams */
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
14043bbb6aaSRobert Watson 				      );
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,
21952cd27cbSRobert Watson 	    "udp_inpcb", udp_inpcb_init, NULL, UMA_ZONE_NOFREE,
22052cd27cbSRobert Watson 	    IPI_HASHFIELDS_2TUPLE);
2216a9148feSBjoern A. Zeeb 	V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb),
2226a9148feSBjoern A. Zeeb 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
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,
235e06e816fSKevin Lo 	    UMA_ZONE_NOFREE, 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.
29543bbb6aaSRobert Watson  */
29643bbb6aaSRobert Watson static void
29743bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
29843bbb6aaSRobert Watson     struct sockaddr_in *udp_in)
29943bbb6aaSRobert Watson {
30043bbb6aaSRobert Watson 	struct sockaddr *append_sa;
30143bbb6aaSRobert Watson 	struct socket *so;
30243bbb6aaSRobert Watson 	struct mbuf *opts = 0;
30343bbb6aaSRobert Watson #ifdef INET6
30443bbb6aaSRobert Watson 	struct sockaddr_in6 udp_in6;
30543bbb6aaSRobert Watson #endif
3067b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
30743bbb6aaSRobert Watson 
308fa046d87SRobert Watson 	INP_LOCK_ASSERT(inp);
30943bbb6aaSRobert Watson 
31079bb84fbSEdward Tomasz Napierala 	/*
31179bb84fbSEdward Tomasz Napierala 	 * Engage the tunneling protocol.
31279bb84fbSEdward Tomasz Napierala 	 */
31379bb84fbSEdward Tomasz Napierala 	up = intoudpcb(inp);
31479bb84fbSEdward Tomasz Napierala 	if (up->u_tun_func != NULL) {
31579bb84fbSEdward Tomasz Napierala 		(*up->u_tun_func)(n, off, inp);
31679bb84fbSEdward Tomasz Napierala 		return;
31779bb84fbSEdward Tomasz Napierala 	}
31879bb84fbSEdward Tomasz Napierala 
31979bb84fbSEdward Tomasz Napierala 	if (n == NULL)
32079bb84fbSEdward Tomasz Napierala 		return;
32179bb84fbSEdward Tomasz Napierala 
32279bb84fbSEdward Tomasz Napierala 	off += sizeof(struct udphdr);
32379bb84fbSEdward Tomasz Napierala 
32443bbb6aaSRobert Watson #ifdef IPSEC
32543bbb6aaSRobert Watson 	/* Check AH/ESP integrity. */
32643bbb6aaSRobert Watson 	if (ipsec4_in_reject(n, inp)) {
32743bbb6aaSRobert Watson 		m_freem(n);
3286794f460SAndrey V. Elsukov 		IPSECSTAT_INC(ips_in_polvio);
32943bbb6aaSRobert Watson 		return;
33043bbb6aaSRobert Watson 	}
3317b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
3327b495c44SVANHULLEBUS Yvan 	up = intoudpcb(inp);
3337b495c44SVANHULLEBUS Yvan 	KASSERT(up != NULL, ("%s: udpcb NULL", __func__));
3347b495c44SVANHULLEBUS Yvan 	if (up->u_flags & UF_ESPINUDP_ALL) {	/* IPSec UDP encaps. */
3357b495c44SVANHULLEBUS Yvan 		n = udp4_espdecap(inp, n, off);
3367b495c44SVANHULLEBUS Yvan 		if (n == NULL)				/* Consumed. */
3377b495c44SVANHULLEBUS Yvan 			return;
3387b495c44SVANHULLEBUS Yvan 	}
3397b495c44SVANHULLEBUS Yvan #endif /* IPSEC_NAT_T */
34043bbb6aaSRobert Watson #endif /* IPSEC */
34143bbb6aaSRobert Watson #ifdef MAC
34230d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, n) != 0) {
34343bbb6aaSRobert Watson 		m_freem(n);
34443bbb6aaSRobert Watson 		return;
34543bbb6aaSRobert Watson 	}
34679288c11SBjoern A. Zeeb #endif /* MAC */
34743bbb6aaSRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
34843bbb6aaSRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
34943bbb6aaSRobert Watson #ifdef INET6
3509a38ba81SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6)
35148d48eb9SBjoern A. Zeeb 			(void)ip6_savecontrol_v4(inp, n, &opts, NULL);
3529a38ba81SBjoern A. Zeeb 		else
35379288c11SBjoern A. Zeeb #endif /* INET6 */
35443bbb6aaSRobert Watson 			ip_savecontrol(inp, &opts, ip, n);
35543bbb6aaSRobert Watson 	}
35643bbb6aaSRobert Watson #ifdef INET6
35743bbb6aaSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
35843bbb6aaSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
35943bbb6aaSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
36043bbb6aaSRobert Watson 		udp_in6.sin6_family = AF_INET6;
36143bbb6aaSRobert Watson 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
36243bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
36343bbb6aaSRobert Watson 	} else
36479288c11SBjoern A. Zeeb #endif /* INET6 */
36543bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)udp_in;
36643bbb6aaSRobert Watson 	m_adj(n, off);
36743bbb6aaSRobert Watson 
36843bbb6aaSRobert Watson 	so = inp->inp_socket;
36943bbb6aaSRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
37043bbb6aaSRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
37143bbb6aaSRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
37243bbb6aaSRobert Watson 		m_freem(n);
37343bbb6aaSRobert Watson 		if (opts)
37443bbb6aaSRobert Watson 			m_freem(opts);
375026decb8SRobert Watson 		UDPSTAT_INC(udps_fullsock);
37643bbb6aaSRobert Watson 	} else
37743bbb6aaSRobert Watson 		sorwakeup_locked(so);
37843bbb6aaSRobert Watson }
37943bbb6aaSRobert Watson 
3808f5a8818SKevin Lo int
3818f5a8818SKevin Lo udp_input(struct mbuf **mp, int *offp, int proto)
382df8bae1dSRodney W. Grimes {
3833329b236SRobert Watson 	struct ip *ip;
3843329b236SRobert Watson 	struct udphdr *uh;
38571498f30SBruce M Simpson 	struct ifnet *ifp;
3863329b236SRobert Watson 	struct inpcb *inp;
3878f134647SGleb Smirnoff 	uint16_t len, ip_len;
388e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
389df8bae1dSRodney W. Grimes 	struct ip save_ip;
390d4b509bdSRobert Watson 	struct sockaddr_in udp_in;
3918f5a8818SKevin Lo 	struct mbuf *m;
3920b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
3938f5a8818SKevin Lo 	int cscov_partial, iphlen;
394df8bae1dSRodney W. Grimes 
3958f5a8818SKevin Lo 	m = *mp;
3968f5a8818SKevin Lo 	iphlen = *offp;
39771498f30SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
3988f5a8818SKevin Lo 	*mp = NULL;
399026decb8SRobert Watson 	UDPSTAT_INC(udps_ipackets);
400df8bae1dSRodney W. Grimes 
401df8bae1dSRodney W. Grimes 	/*
4023329b236SRobert Watson 	 * Strip IP options, if any; should skip this, make available to
4033329b236SRobert Watson 	 * user, and use on returned packets, but we don't yet have a way to
4043329b236SRobert Watson 	 * check the checksum with options still present.
405df8bae1dSRodney W. Grimes 	 */
406df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
407105bd211SGleb Smirnoff 		ip_stripoptions(m);
408df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
409df8bae1dSRodney W. Grimes 	}
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 	/*
412df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
413df8bae1dSRodney W. Grimes 	 */
414df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
415df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
416d1b18731SKevin Lo 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) {
417026decb8SRobert Watson 			UDPSTAT_INC(udps_hdrops);
4188f5a8818SKevin Lo 			return (IPPROTO_DONE);
419df8bae1dSRodney W. Grimes 		}
420df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
421df8bae1dSRodney W. Grimes 	}
422df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
4238f5a8818SKevin Lo 	cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0;
424df8bae1dSRodney W. Grimes 
4253329b236SRobert Watson 	/*
4263329b236SRobert Watson 	 * Destination port of 0 is illegal, based on RFC768.
4273329b236SRobert Watson 	 */
428686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
429f76fcf6dSJeffrey Hsu 		goto badunlocked;
430686cdd19SJun-ichiro itojun Hagino 
431df8bae1dSRodney W. Grimes 	/*
4323329b236SRobert Watson 	 * Construct sockaddr format source address.  Stuff source address
4333329b236SRobert Watson 	 * and datagram in user buffer.
434b9234fafSSam Leffler 	 */
435d4b509bdSRobert Watson 	bzero(&udp_in, sizeof(udp_in));
436d4b509bdSRobert Watson 	udp_in.sin_len = sizeof(udp_in);
437d4b509bdSRobert Watson 	udp_in.sin_family = AF_INET;
438b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
439b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
440b9234fafSSam Leffler 
441b9234fafSSam Leffler 	/*
442af1ee11dSRobert Watson 	 * Make mbuf data length reflect UDP length.  If not enough data to
443af1ee11dSRobert Watson 	 * reflect UDP length, drop.
444df8bae1dSRodney W. Grimes 	 */
445df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
4468ad458a4SGleb Smirnoff 	ip_len = ntohs(ip->ip_len) - iphlen;
447*0f4a0366SMichael Tuexen 	if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) {
448e06e816fSKevin Lo 		/* Zero means checksum over the complete packet. */
449*0f4a0366SMichael Tuexen 		if (len == 0)
450e06e816fSKevin Lo 			len = ip_len;
451e06e816fSKevin Lo 		cscov_partial = 0;
452e06e816fSKevin Lo 	}
4538f134647SGleb Smirnoff 	if (ip_len != len) {
4548f134647SGleb Smirnoff 		if (len > ip_len || len < sizeof(struct udphdr)) {
455026decb8SRobert Watson 			UDPSTAT_INC(udps_badlen);
456f76fcf6dSJeffrey Hsu 			goto badunlocked;
457df8bae1dSRodney W. Grimes 		}
4588f5a8818SKevin Lo 		if (proto == IPPROTO_UDP)
4598f134647SGleb Smirnoff 			m_adj(m, len - ip_len);
460df8bae1dSRodney W. Grimes 	}
4613329b236SRobert Watson 
462df8bae1dSRodney W. Grimes 	/*
4633329b236SRobert Watson 	 * Save a copy of the IP header in case we want restore it for
4643329b236SRobert Watson 	 * sending an ICMP error message in response.
465df8bae1dSRodney W. Grimes 	 */
466603724d3SBjoern A. Zeeb 	if (!V_udp_blackhole)
467df8bae1dSRodney W. Grimes 		save_ip = *ip;
468cce418d3SMatt Jacob 	else
469cce418d3SMatt Jacob 		memset(&save_ip, 0, sizeof(save_ip));
470df8bae1dSRodney W. Grimes 
471df8bae1dSRodney W. Grimes 	/*
472df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
473df8bae1dSRodney W. Grimes 	 */
4746dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
47539629c92SDavid Malone 		u_short uh_sum;
47639629c92SDavid Malone 
477e06e816fSKevin Lo 		if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
478e06e816fSKevin Lo 		    !cscov_partial) {
479db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
48039629c92SDavid Malone 				uh_sum = m->m_pkthdr.csum_data;
481db4f9cc7SJonathan Lemon 			else
48239629c92SDavid Malone 				uh_sum = in_pseudo(ip->ip_src.s_addr,
483506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
4848f5a8818SKevin Lo 				    m->m_pkthdr.csum_data + proto));
48539629c92SDavid Malone 			uh_sum ^= 0xffff;
486db4f9cc7SJonathan Lemon 		} else {
487cb342100SHajimu UMEMOTO 			char b[9];
488af1ee11dSRobert Watson 
489cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
4906effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
4918f5a8818SKevin Lo 			((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ?
492e06e816fSKevin Lo 			    uh->uh_ulen : htons(ip_len);
49339629c92SDavid Malone 			uh_sum = in_cksum(m, len + sizeof (struct ip));
494cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
495db4f9cc7SJonathan Lemon 		}
49639629c92SDavid Malone 		if (uh_sum) {
497026decb8SRobert Watson 			UDPSTAT_INC(udps_badsum);
498df8bae1dSRodney W. Grimes 			m_freem(m);
4998f5a8818SKevin Lo 			return (IPPROTO_DONE);
500df8bae1dSRodney W. Grimes 		}
501fb9aaba0SRuslan Ermilov 	} else
502026decb8SRobert Watson 		UDPSTAT_INC(udps_nosum);
503df8bae1dSRodney W. Grimes 
5048f5a8818SKevin Lo 	pcbinfo = get_inpcbinfo(proto);
505df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
50671498f30SBruce M Simpson 	    in_broadcast(ip->ip_dst, ifp)) {
50782c23ebaSBill Fenner 		struct inpcb *last;
508e06e816fSKevin Lo 		struct inpcbhead *pcblist;
50971498f30SBruce M Simpson 		struct ip_moptions *imo;
5103329b236SRobert Watson 
511e06e816fSKevin Lo 		INP_INFO_RLOCK(pcbinfo);
5128f5a8818SKevin Lo 		pcblist = get_pcblist(proto);
513df8bae1dSRodney W. Grimes 		last = NULL;
514e06e816fSKevin Lo 		LIST_FOREACH(inp, pcblist, inp_list) {
5159c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
516f76fcf6dSJeffrey Hsu 				continue;
517cfa1ca9dSYoshinobu Inoue #ifdef INET6
518369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
5199c1df695SRobert Watson 				continue;
520cfa1ca9dSYoshinobu Inoue #endif
52171498f30SBruce M Simpson 			if (inp->inp_laddr.s_addr != INADDR_ANY &&
52271498f30SBruce M Simpson 			    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
5239c1df695SRobert Watson 				continue;
52471498f30SBruce M Simpson 			if (inp->inp_faddr.s_addr != INADDR_ANY &&
52571498f30SBruce M Simpson 			    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
52671498f30SBruce M Simpson 				continue;
52771498f30SBruce M Simpson 			if (inp->inp_fport != 0 &&
528df8bae1dSRodney W. Grimes 			    inp->inp_fport != uh->uh_sport)
5299c1df695SRobert Watson 				continue;
53071498f30SBruce M Simpson 
531119d85f6SRobert Watson 			INP_RLOCK(inp);
532df8bae1dSRodney W. Grimes 
53383453a06SBruce M Simpson 			/*
534fa046d87SRobert Watson 			 * XXXRW: Because we weren't holding either the inpcb
535fa046d87SRobert Watson 			 * or the hash lock when we checked for a match
536fa046d87SRobert Watson 			 * before, we should probably recheck now that the
537fa046d87SRobert Watson 			 * inpcb lock is held.
538fa046d87SRobert Watson 			 */
539fa046d87SRobert Watson 
540fa046d87SRobert Watson 			/*
54171498f30SBruce M Simpson 			 * Handle socket delivery policy for any-source
54271498f30SBruce M Simpson 			 * and source-specific multicast. [RFC3678]
54383453a06SBruce M Simpson 			 */
54471498f30SBruce M Simpson 			imo = inp->inp_moptions;
545a38b1c8cSRandall Stewart 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
546d10910e6SBruce M Simpson 				struct sockaddr_in	 group;
547d10910e6SBruce M Simpson 				int			 blocked;
548a38b1c8cSRandall Stewart 				if (imo == NULL) {
549a38b1c8cSRandall Stewart 					INP_RUNLOCK(inp);
550a38b1c8cSRandall Stewart 					continue;
551a38b1c8cSRandall Stewart 				}
552d10910e6SBruce M Simpson 				bzero(&group, sizeof(struct sockaddr_in));
553d10910e6SBruce M Simpson 				group.sin_len = sizeof(struct sockaddr_in);
554d10910e6SBruce M Simpson 				group.sin_family = AF_INET;
555d10910e6SBruce M Simpson 				group.sin_addr = ip->ip_dst;
55671498f30SBruce M Simpson 
557d10910e6SBruce M Simpson 				blocked = imo_multi_filter(imo, ifp,
558d10910e6SBruce M Simpson 					(struct sockaddr *)&group,
55971498f30SBruce M Simpson 					(struct sockaddr *)&udp_in);
560d10910e6SBruce M Simpson 				if (blocked != MCAST_PASS) {
561d10910e6SBruce M Simpson 					if (blocked == MCAST_NOTGMEMBER)
56286425c62SRobert Watson 						IPSTAT_INC(ips_notmember);
563d10910e6SBruce M Simpson 					if (blocked == MCAST_NOTSMEMBER ||
564d10910e6SBruce M Simpson 					    blocked == MCAST_MUTED)
565026decb8SRobert Watson 						UDPSTAT_INC(udps_filtermcast);
566119d85f6SRobert Watson 					INP_RUNLOCK(inp);
5679c1df695SRobert Watson 					continue;
5689c1df695SRobert Watson 				}
56983453a06SBruce M Simpson 			}
570df8bae1dSRodney W. Grimes 			if (last != NULL) {
571df8bae1dSRodney W. Grimes 				struct mbuf *n;
572df8bae1dSRodney W. Grimes 
573032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
57479bb84fbSEdward Tomasz Napierala 				udp_append(last, ip, n, iphlen, &udp_in);
5756a9148feSBjoern A. Zeeb 				INP_RUNLOCK(last);
576df8bae1dSRodney W. Grimes 			}
57782c23ebaSBill Fenner 			last = inp;
578df8bae1dSRodney W. Grimes 			/*
579df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
580df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
5813329b236SRobert Watson 			 * socket options set.  This heuristic avoids
5823329b236SRobert Watson 			 * searching through all pcbs in the common case of a
5833329b236SRobert Watson 			 * non-shared port.  It assumes that an application
5843329b236SRobert Watson 			 * will never clear these options after setting them.
585df8bae1dSRodney W. Grimes 			 */
5863329b236SRobert Watson 			if ((last->inp_socket->so_options &
5873329b236SRobert Watson 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
588df8bae1dSRodney W. Grimes 				break;
589df8bae1dSRodney W. Grimes 		}
590df8bae1dSRodney W. Grimes 
591df8bae1dSRodney W. Grimes 		if (last == NULL) {
592df8bae1dSRodney W. Grimes 			/*
5933329b236SRobert Watson 			 * No matching pcb found; discard datagram.  (No need
5943329b236SRobert Watson 			 * to send an ICMP Port Unreachable for a broadcast
5953329b236SRobert Watson 			 * or multicast datgram.)
596df8bae1dSRodney W. Grimes 			 */
597026decb8SRobert Watson 			UDPSTAT_INC(udps_noportbcast);
598fa046d87SRobert Watson 			if (inp)
599fa046d87SRobert Watson 				INP_RUNLOCK(inp);
600e06e816fSKevin Lo 			INP_INFO_RUNLOCK(pcbinfo);
601fa046d87SRobert Watson 			goto badunlocked;
602df8bae1dSRodney W. Grimes 		}
60379bb84fbSEdward Tomasz Napierala 		udp_append(last, ip, m, iphlen, &udp_in);
604c7c7ea4bSRandall Stewart 		INP_RUNLOCK(last);
605e06e816fSKevin Lo 		INP_INFO_RUNLOCK(pcbinfo);
6068f5a8818SKevin Lo 		return (IPPROTO_DONE);
607df8bae1dSRodney W. Grimes 	}
6083329b236SRobert Watson 
609df8bae1dSRodney W. Grimes 	/*
6106d6a026bSDavid Greenman 	 * Locate pcb for datagram.
611df8bae1dSRodney W. Grimes 	 */
612c1de64a4SAndrey V. Elsukov 
6138a006adbSBjoern A. Zeeb 	/*
6148a006adbSBjoern A. Zeeb 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
6158a006adbSBjoern A. Zeeb 	 */
616ffdbf9daSAndrey V. Elsukov 	if ((m->m_flags & M_IP_NEXTHOP) &&
617c1de64a4SAndrey V. Elsukov 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
6188a006adbSBjoern A. Zeeb 		struct sockaddr_in *next_hop;
6198a006adbSBjoern A. Zeeb 
6208a006adbSBjoern A. Zeeb 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
6218a006adbSBjoern A. Zeeb 
6228a006adbSBjoern A. Zeeb 		/*
6238a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
6248a006adbSBjoern A. Zeeb 		 * Already got one like this?
6258a006adbSBjoern A. Zeeb 		 */
626e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
6278a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m);
6288a006adbSBjoern A. Zeeb 		if (!inp) {
6298a006adbSBjoern A. Zeeb 			/*
6308a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
6318a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
6328a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
6338a006adbSBjoern A. Zeeb 			 */
634e06e816fSKevin Lo 			inp = in_pcblookup(pcbinfo, ip->ip_src,
6358a006adbSBjoern A. Zeeb 			    uh->uh_sport, next_hop->sin_addr,
6368a006adbSBjoern A. Zeeb 			    next_hop->sin_port ? htons(next_hop->sin_port) :
6378a006adbSBjoern A. Zeeb 			    uh->uh_dport, INPLOOKUP_WILDCARD |
6388a006adbSBjoern A. Zeeb 			    INPLOOKUP_RLOCKPCB, ifp);
6398a006adbSBjoern A. Zeeb 		}
6408a006adbSBjoern A. Zeeb 		/* Remove the tag from the packet. We don't need it anymore. */
6418a006adbSBjoern A. Zeeb 		m_tag_delete(m, fwd_tag);
642ffdbf9daSAndrey V. Elsukov 		m->m_flags &= ~M_IP_NEXTHOP;
6438a006adbSBjoern A. Zeeb 	} else
644e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
6458a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
6468a006adbSBjoern A. Zeeb 		    INPLOOKUP_RLOCKPCB, ifp, m);
64715bd2b43SDavid Greenman 	if (inp == NULL) {
648afdb4274SRobert Watson 		if (udp_log_in_vain) {
649df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
65075cfc95fSAndrey A. Chernov 
65175cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
652592071e8SBruce Evans 			log(LOG_INFO,
653592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
654592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
655592071e8SBruce Evans 			    ntohs(uh->uh_sport));
65675cfc95fSAndrey A. Chernov 		}
657026decb8SRobert Watson 		UDPSTAT_INC(udps_noport);
658df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
659026decb8SRobert Watson 			UDPSTAT_INC(udps_noportbcast);
660fa046d87SRobert Watson 			goto badunlocked;
661df8bae1dSRodney W. Grimes 		}
662603724d3SBjoern A. Zeeb 		if (V_udp_blackhole)
663fa046d87SRobert Watson 			goto badunlocked;
6641cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
665fa046d87SRobert Watson 			goto badunlocked;
66604287599SRuslan Ermilov 		*ip = save_ip;
667582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
6688f5a8818SKevin Lo 		return (IPPROTO_DONE);
669df8bae1dSRodney W. Grimes 	}
6703329b236SRobert Watson 
6713329b236SRobert Watson 	/*
6723329b236SRobert Watson 	 * Check the minimum TTL for socket.
6733329b236SRobert Watson 	 */
674fa046d87SRobert Watson 	INP_RLOCK_ASSERT(inp);
67510cc62b7SRobert Watson 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
67610cc62b7SRobert Watson 		INP_RUNLOCK(inp);
677fa046d87SRobert Watson 		m_freem(m);
6788f5a8818SKevin Lo 		return (IPPROTO_DONE);
67910cc62b7SRobert Watson 	}
680e06e816fSKevin Lo 	if (cscov_partial) {
681e06e816fSKevin Lo 		struct udpcb *up;
682e06e816fSKevin Lo 
683e06e816fSKevin Lo 		up = intoudpcb(inp);
684e06e816fSKevin Lo 		if (up->u_rxcslen > len) {
685e06e816fSKevin Lo 			INP_RUNLOCK(inp);
686e06e816fSKevin Lo 			m_freem(m);
6878f5a8818SKevin Lo 			return (IPPROTO_DONE);
688e06e816fSKevin Lo 		}
689e06e816fSKevin Lo 	}
69057f60867SMark Johnston 
6911ad19fb6SMark Johnston 	UDP_PROBE(receive, NULL, inp, ip, inp, uh);
69279bb84fbSEdward Tomasz Napierala 	udp_append(inp, ip, m, iphlen, &udp_in);
693119d85f6SRobert Watson 	INP_RUNLOCK(inp);
6948f5a8818SKevin Lo 	return (IPPROTO_DONE);
69561ffc0b1SJeffrey Hsu 
696f76fcf6dSJeffrey Hsu badunlocked:
697df8bae1dSRodney W. Grimes 	m_freem(m);
6988f5a8818SKevin Lo 	return (IPPROTO_DONE);
699cfa1ca9dSYoshinobu Inoue }
70079288c11SBjoern A. Zeeb #endif /* INET */
701cfa1ca9dSYoshinobu Inoue 
702cfa1ca9dSYoshinobu Inoue /*
7033329b236SRobert Watson  * Notify a udp user of an asynchronous error; just wake up so that they can
7043329b236SRobert Watson  * collect error status.
705df8bae1dSRodney W. Grimes  */
7063ce144eaSJeffrey Hsu struct inpcb *
7073329b236SRobert Watson udp_notify(struct inpcb *inp, int errno)
708df8bae1dSRodney W. Grimes {
7093329b236SRobert Watson 
710ac9ae279SRobert Watson 	/*
711ac9ae279SRobert Watson 	 * While udp_ctlinput() always calls udp_notify() with a read lock
712ac9ae279SRobert Watson 	 * when invoking it directly, in_pcbnotifyall() currently uses write
713ac9ae279SRobert Watson 	 * locks due to sharing code with TCP.  For now, accept either a read
714ac9ae279SRobert Watson 	 * or a write lock, but a read lock is sufficient.
715ac9ae279SRobert Watson 	 */
716ac9ae279SRobert Watson 	INP_LOCK_ASSERT(inp);
7178501a69cSRobert Watson 
718df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
719df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
720df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
7213329b236SRobert Watson 	return (inp);
722df8bae1dSRodney W. Grimes }
723df8bae1dSRodney W. Grimes 
72479288c11SBjoern A. Zeeb #ifdef INET
725e06e816fSKevin Lo static void
726e06e816fSKevin Lo udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip,
727e06e816fSKevin Lo     struct inpcbinfo *pcbinfo)
728df8bae1dSRodney W. Grimes {
729c693a045SJonathan Lemon 	struct ip *ip = vip;
730c693a045SJonathan Lemon 	struct udphdr *uh;
731c693a045SJonathan Lemon 	struct in_addr faddr;
732c693a045SJonathan Lemon 	struct inpcb *inp;
733c693a045SJonathan Lemon 
734c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
735c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
736c693a045SJonathan Lemon 		return;
737df8bae1dSRodney W. Grimes 
73897d8d152SAndre Oppermann 	/*
73997d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
74097d8d152SAndre Oppermann 	 */
74197d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
74297d8d152SAndre Oppermann 		return;
7433329b236SRobert Watson 
74497d8d152SAndre Oppermann 	/*
74597d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
7463329b236SRobert Watson 	 *
7473329b236SRobert Watson 	 * XXX: We never get this from ICMP, otherwise it makes an excellent
7483329b236SRobert Watson 	 * DoS attack on machines with many connections.
74997d8d152SAndre Oppermann 	 */
75097d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
751af1ee11dSRobert Watson 		ip = NULL;
752d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
753df8bae1dSRodney W. Grimes 		return;
754af1ee11dSRobert Watson 	if (ip != NULL) {
755df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
756e06e816fSKevin Lo 		inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
757fa046d87SRobert Watson 		    ip->ip_src, uh->uh_sport, INPLOOKUP_RLOCKPCB, NULL);
758f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
759fa046d87SRobert Watson 			INP_RLOCK_ASSERT(inp);
760f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
761f5514f08SRobert Watson 				udp_notify(inp, inetctlerrmap[cmd]);
762f76fcf6dSJeffrey Hsu 			}
763ac9ae279SRobert Watson 			INP_RUNLOCK(inp);
764f76fcf6dSJeffrey Hsu 		}
765df8bae1dSRodney W. Grimes 	} else
766e06e816fSKevin Lo 		in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd],
767f5514f08SRobert Watson 		    udp_notify);
768df8bae1dSRodney W. Grimes }
769e06e816fSKevin Lo void
770e06e816fSKevin Lo udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
771e06e816fSKevin Lo {
772e06e816fSKevin Lo 
773e06e816fSKevin Lo 	return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo));
774e06e816fSKevin Lo }
775e06e816fSKevin Lo 
776e06e816fSKevin Lo void
777e06e816fSKevin Lo udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip)
778e06e816fSKevin Lo {
779e06e816fSKevin Lo 
780e06e816fSKevin Lo 	return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo));
781e06e816fSKevin Lo }
78279288c11SBjoern A. Zeeb #endif /* INET */
783df8bae1dSRodney W. Grimes 
7840312fbe9SPoul-Henning Kamp static int
78582d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
78698271db4SGarrett Wollman {
787277afaffSRobert Watson 	int error, i, n;
78898271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
78998271db4SGarrett Wollman 	inp_gen_t gencnt;
79098271db4SGarrett Wollman 	struct xinpgen xig;
79198271db4SGarrett Wollman 
79298271db4SGarrett Wollman 	/*
793f5514f08SRobert Watson 	 * The process of preparing the PCB list is too time-consuming and
79498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
79598271db4SGarrett Wollman 	 */
79698271db4SGarrett Wollman 	if (req->oldptr == 0) {
797603724d3SBjoern A. Zeeb 		n = V_udbinfo.ipi_count;
798c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
799c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
8003329b236SRobert Watson 		return (0);
80198271db4SGarrett Wollman 	}
80298271db4SGarrett Wollman 
80398271db4SGarrett Wollman 	if (req->newptr != 0)
8043329b236SRobert Watson 		return (EPERM);
80598271db4SGarrett Wollman 
80698271db4SGarrett Wollman 	/*
80798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
80898271db4SGarrett Wollman 	 */
809603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_udbinfo);
810603724d3SBjoern A. Zeeb 	gencnt = V_udbinfo.ipi_gencnt;
811603724d3SBjoern A. Zeeb 	n = V_udbinfo.ipi_count;
812603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_udbinfo);
81398271db4SGarrett Wollman 
81447934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
8155c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
81647934cefSDon Lewis 	if (error != 0)
81747934cefSDon Lewis 		return (error);
8185c38b6dbSDon Lewis 
81998271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
82098271db4SGarrett Wollman 	xig.xig_count = n;
82198271db4SGarrett Wollman 	xig.xig_gen = gencnt;
82298271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
82398271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
82498271db4SGarrett Wollman 	if (error)
8253329b236SRobert Watson 		return (error);
82698271db4SGarrett Wollman 
827a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
82898271db4SGarrett Wollman 	if (inp_list == 0)
8293329b236SRobert Watson 		return (ENOMEM);
83098271db4SGarrett Wollman 
831603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_udbinfo);
832603724d3SBjoern A. Zeeb 	for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n;
833fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
834d0e157f6SBjoern A. Zeeb 		INP_WLOCK(inp);
8352ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
836d0e157f6SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
837d0e157f6SBjoern A. Zeeb 			in_pcbref(inp);
83898271db4SGarrett Wollman 			inp_list[i++] = inp;
839d0e157f6SBjoern A. Zeeb 		}
840d0e157f6SBjoern A. Zeeb 		INP_WUNLOCK(inp);
8414787fd37SPaul Saab 	}
842603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_udbinfo);
84398271db4SGarrett Wollman 	n = i;
84498271db4SGarrett Wollman 
84598271db4SGarrett Wollman 	error = 0;
84698271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
84798271db4SGarrett Wollman 		inp = inp_list[i];
8489622e84fSRobert Watson 		INP_RLOCK(inp);
84998271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
85098271db4SGarrett Wollman 			struct xinpcb xi;
851d0e157f6SBjoern A. Zeeb 
852fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
85398271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
85498271db4SGarrett Wollman 			/* XXX should avoid extra copy */
85598271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
85698271db4SGarrett Wollman 			if (inp->inp_socket)
85798271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
8584b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
8599622e84fSRobert Watson 			INP_RUNLOCK(inp);
86098271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
861d915b280SStephan Uphoff 		} else
8629622e84fSRobert Watson 			INP_RUNLOCK(inp);
86398271db4SGarrett Wollman 	}
864d0e157f6SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_udbinfo);
865d0e157f6SBjoern A. Zeeb 	for (i = 0; i < n; i++) {
866d0e157f6SBjoern A. Zeeb 		inp = inp_list[i];
867fa046d87SRobert Watson 		INP_RLOCK(inp);
868fa046d87SRobert Watson 		if (!in_pcbrele_rlocked(inp))
869fa046d87SRobert Watson 			INP_RUNLOCK(inp);
870d0e157f6SBjoern A. Zeeb 	}
871d0e157f6SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_udbinfo);
872d0e157f6SBjoern A. Zeeb 
87398271db4SGarrett Wollman 	if (!error) {
87498271db4SGarrett Wollman 		/*
8753329b236SRobert Watson 		 * Give the user an updated idea of our state.  If the
8763329b236SRobert Watson 		 * generation differs from what we told her before, she knows
8773329b236SRobert Watson 		 * that something happened while we were processing this
8783329b236SRobert Watson 		 * request, and it might be necessary to retry.
87998271db4SGarrett Wollman 		 */
880603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_udbinfo);
881603724d3SBjoern A. Zeeb 		xig.xig_gen = V_udbinfo.ipi_gencnt;
88298271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
883603724d3SBjoern A. Zeeb 		xig.xig_count = V_udbinfo.ipi_count;
884603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_udbinfo);
88598271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
88698271db4SGarrett Wollman 	}
88798271db4SGarrett Wollman 	free(inp_list, M_TEMP);
8883329b236SRobert Watson 	return (error);
88998271db4SGarrett Wollman }
89098271db4SGarrett Wollman 
89179c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
89279c3d51bSMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
89398271db4SGarrett Wollman     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
89498271db4SGarrett Wollman 
89579288c11SBjoern A. Zeeb #ifdef INET
89698271db4SGarrett Wollman static int
89782d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
898490d50b6SBrian Feldman {
899c0511d3bSBrian Feldman 	struct xucred xuc;
900490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
901490d50b6SBrian Feldman 	struct inpcb *inp;
902277afaffSRobert Watson 	int error;
903490d50b6SBrian Feldman 
90432f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
905490d50b6SBrian Feldman 	if (error)
906490d50b6SBrian Feldman 		return (error);
907490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
908490d50b6SBrian Feldman 	if (error)
909490d50b6SBrian Feldman 		return (error);
910fa046d87SRobert Watson 	inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
911fa046d87SRobert Watson 	    addrs[0].sin_addr, addrs[0].sin_port,
912fa046d87SRobert Watson 	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
9139622e84fSRobert Watson 	if (inp != NULL) {
914fa046d87SRobert Watson 		INP_RLOCK_ASSERT(inp);
9159622e84fSRobert Watson 		if (inp->inp_socket == NULL)
9169622e84fSRobert Watson 			error = ENOENT;
9179622e84fSRobert Watson 		if (error == 0)
918f08ef6c5SBjoern A. Zeeb 			error = cr_canseeinpcb(req->td->td_ucred, inp);
9199622e84fSRobert Watson 		if (error == 0)
92086d02c5cSBjoern A. Zeeb 			cru2x(inp->inp_cred, &xuc);
9219622e84fSRobert Watson 		INP_RUNLOCK(inp);
922fa046d87SRobert Watson 	} else
9239622e84fSRobert Watson 		error = ENOENT;
9240e1eebb8SDon Lewis 	if (error == 0)
9250e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
926490d50b6SBrian Feldman 	return (error);
927490d50b6SBrian Feldman }
928490d50b6SBrian Feldman 
9297ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
9307ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
9317ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
93279288c11SBjoern A. Zeeb #endif /* INET */
933490d50b6SBrian Feldman 
9347b495c44SVANHULLEBUS Yvan int
9357b495c44SVANHULLEBUS Yvan udp_ctloutput(struct socket *so, struct sockopt *sopt)
9367b495c44SVANHULLEBUS Yvan {
9377b495c44SVANHULLEBUS Yvan 	struct inpcb *inp;
9387b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
939e06e816fSKevin Lo 	int isudplite, error, optval;
9407b495c44SVANHULLEBUS Yvan 
941e06e816fSKevin Lo 	error = 0;
942e06e816fSKevin Lo 	isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0;
9437b495c44SVANHULLEBUS Yvan 	inp = sotoinpcb(so);
9447b495c44SVANHULLEBUS Yvan 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
9457b495c44SVANHULLEBUS Yvan 	INP_WLOCK(inp);
946e06e816fSKevin Lo 	if (sopt->sopt_level != so->so_proto->pr_protocol) {
9477b495c44SVANHULLEBUS Yvan #ifdef INET6
9487b495c44SVANHULLEBUS Yvan 		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
9497b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9507b495c44SVANHULLEBUS Yvan 			error = ip6_ctloutput(so, sopt);
95179288c11SBjoern A. Zeeb 		}
9527b495c44SVANHULLEBUS Yvan #endif
95379288c11SBjoern A. Zeeb #if defined(INET) && defined(INET6)
95479288c11SBjoern A. Zeeb 		else
95579288c11SBjoern A. Zeeb #endif
95679288c11SBjoern A. Zeeb #ifdef INET
95779288c11SBjoern A. Zeeb 		{
9587b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9597b495c44SVANHULLEBUS Yvan 			error = ip_ctloutput(so, sopt);
9607b495c44SVANHULLEBUS Yvan 		}
9617b495c44SVANHULLEBUS Yvan #endif
9627b495c44SVANHULLEBUS Yvan 		return (error);
9637b495c44SVANHULLEBUS Yvan 	}
9647b495c44SVANHULLEBUS Yvan 
9657b495c44SVANHULLEBUS Yvan 	switch (sopt->sopt_dir) {
9667b495c44SVANHULLEBUS Yvan 	case SOPT_SET:
9677b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
9687b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
9697b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9707b495c44SVANHULLEBUS Yvan 			error = sooptcopyin(sopt, &optval, sizeof optval,
9717b495c44SVANHULLEBUS Yvan 					    sizeof optval);
9727b495c44SVANHULLEBUS Yvan 			if (error)
9737b495c44SVANHULLEBUS Yvan 				break;
9747b495c44SVANHULLEBUS Yvan 			inp = sotoinpcb(so);
9757b495c44SVANHULLEBUS Yvan 			KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
9767b495c44SVANHULLEBUS Yvan 			INP_WLOCK(inp);
9777b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
9787b495c44SVANHULLEBUS Yvan 			up = intoudpcb(inp);
9797b495c44SVANHULLEBUS Yvan 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
9807b495c44SVANHULLEBUS Yvan #endif
9817b495c44SVANHULLEBUS Yvan 			switch (optval) {
9827b495c44SVANHULLEBUS Yvan 			case 0:
9837b495c44SVANHULLEBUS Yvan 				/* Clear all UDP encap. */
9847b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
9857b495c44SVANHULLEBUS Yvan 				up->u_flags &= ~UF_ESPINUDP_ALL;
9867b495c44SVANHULLEBUS Yvan #endif
9877b495c44SVANHULLEBUS Yvan 				break;
9887b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
9897b495c44SVANHULLEBUS Yvan 			case UDP_ENCAP_ESPINUDP:
9907b495c44SVANHULLEBUS Yvan 			case UDP_ENCAP_ESPINUDP_NON_IKE:
9917b495c44SVANHULLEBUS Yvan 				up->u_flags &= ~UF_ESPINUDP_ALL;
9927b495c44SVANHULLEBUS Yvan 				if (optval == UDP_ENCAP_ESPINUDP)
9937b495c44SVANHULLEBUS Yvan 					up->u_flags |= UF_ESPINUDP;
9947b495c44SVANHULLEBUS Yvan 				else if (optval == UDP_ENCAP_ESPINUDP_NON_IKE)
9957b495c44SVANHULLEBUS Yvan 					up->u_flags |= UF_ESPINUDP_NON_IKE;
9967b495c44SVANHULLEBUS Yvan 				break;
9977b495c44SVANHULLEBUS Yvan #endif
9987b495c44SVANHULLEBUS Yvan 			default:
9997b495c44SVANHULLEBUS Yvan 				error = EINVAL;
10007b495c44SVANHULLEBUS Yvan 				break;
10017b495c44SVANHULLEBUS Yvan 			}
10027b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10037b495c44SVANHULLEBUS Yvan 			break;
1004e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
1005e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
1006e06e816fSKevin Lo 			if (!isudplite) {
1007e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1008e06e816fSKevin Lo 				error = ENOPROTOOPT;
1009e06e816fSKevin Lo 				break;
1010e06e816fSKevin Lo 			}
1011e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1012e06e816fSKevin Lo 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1013e06e816fSKevin Lo 			    sizeof(optval));
1014e06e816fSKevin Lo 			if (error != 0)
1015e06e816fSKevin Lo 				break;
1016e06e816fSKevin Lo 			inp = sotoinpcb(so);
1017e06e816fSKevin Lo 			KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1018e06e816fSKevin Lo 			INP_WLOCK(inp);
1019e06e816fSKevin Lo 			up = intoudpcb(inp);
1020e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
102103f90784SMichael Tuexen 			if ((optval != 0 && optval < 8) || (optval > 65535)) {
1022e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1023e06e816fSKevin Lo 				error = EINVAL;
1024e06e816fSKevin Lo 				break;
1025e06e816fSKevin Lo 			}
1026e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1027e06e816fSKevin Lo 				up->u_txcslen = optval;
1028e06e816fSKevin Lo 			else
1029e06e816fSKevin Lo 				up->u_rxcslen = optval;
1030e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1031e06e816fSKevin Lo 			break;
10327b495c44SVANHULLEBUS Yvan 		default:
10337b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10347b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
10357b495c44SVANHULLEBUS Yvan 			break;
10367b495c44SVANHULLEBUS Yvan 		}
10377b495c44SVANHULLEBUS Yvan 		break;
10387b495c44SVANHULLEBUS Yvan 	case SOPT_GET:
10397b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
10407b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
10417b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
10427b495c44SVANHULLEBUS Yvan 			up = intoudpcb(inp);
10437b495c44SVANHULLEBUS Yvan 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
10447b495c44SVANHULLEBUS Yvan 			optval = up->u_flags & UF_ESPINUDP_ALL;
10457b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10467b495c44SVANHULLEBUS Yvan 			error = sooptcopyout(sopt, &optval, sizeof optval);
10477b495c44SVANHULLEBUS Yvan 			break;
10487b495c44SVANHULLEBUS Yvan #endif
1049e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
1050e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
1051e06e816fSKevin Lo 			if (!isudplite) {
1052e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1053e06e816fSKevin Lo 				error = ENOPROTOOPT;
1054e06e816fSKevin Lo 				break;
1055e06e816fSKevin Lo 			}
1056e06e816fSKevin Lo 			up = intoudpcb(inp);
1057e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
1058e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1059e06e816fSKevin Lo 				optval = up->u_txcslen;
1060e06e816fSKevin Lo 			else
1061e06e816fSKevin Lo 				optval = up->u_rxcslen;
1062e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1063e06e816fSKevin Lo 			error = sooptcopyout(sopt, &optval, sizeof(optval));
1064e06e816fSKevin Lo 			break;
10657b495c44SVANHULLEBUS Yvan 		default:
10667b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10677b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
10687b495c44SVANHULLEBUS Yvan 			break;
10697b495c44SVANHULLEBUS Yvan 		}
10707b495c44SVANHULLEBUS Yvan 		break;
10717b495c44SVANHULLEBUS Yvan 	}
10727b495c44SVANHULLEBUS Yvan 	return (error);
10737b495c44SVANHULLEBUS Yvan }
10747b495c44SVANHULLEBUS Yvan 
107579288c11SBjoern A. Zeeb #ifdef INET
1076fa046d87SRobert Watson #define	UH_WLOCKED	2
1077fa046d87SRobert Watson #define	UH_RLOCKED	1
1078fa046d87SRobert Watson #define	UH_UNLOCKED	0
1079490d50b6SBrian Feldman static int
10803329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
10813329b236SRobert Watson     struct mbuf *control, struct thread *td)
1082df8bae1dSRodney W. Grimes {
10833329b236SRobert Watson 	struct udpiphdr *ui;
10843329b236SRobert Watson 	int len = m->m_pkthdr.len;
108590162a4eSIan Dowse 	struct in_addr faddr, laddr;
1086c557ae16SIan Dowse 	struct cmsghdr *cm;
1087e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1088c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
1089e06e816fSKevin Lo 	int cscov_partial = 0;
109090162a4eSIan Dowse 	int error = 0;
10918afa2304SBruce M Simpson 	int ipflags;
109290162a4eSIan Dowse 	u_short fport, lport;
10935c32ea65SRobert Watson 	int unlock_udbinfo;
1094f584d74bSMichael Tuexen 	u_char tos;
1095e06e816fSKevin Lo 	uint8_t pr;
1096e06e816fSKevin Lo 	uint16_t cscov = 0;
10979d3ddf43SAdrian Chadd 	uint32_t flowid = 0;
10989d3ddf43SAdrian Chadd 	int flowid_type = 0;
10999d3ddf43SAdrian Chadd 	int use_flowid = 0;
1100df8bae1dSRodney W. Grimes 
11015c32ea65SRobert Watson 	/*
11025c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
1103f5514f08SRobert Watson 	 * inpcb.  As such, we don't know up front whether we will need the
1104f5514f08SRobert Watson 	 * pcbinfo lock or not.  Do any work to decide what is needed up
1105f5514f08SRobert Watson 	 * front before acquiring any locks.
11065c32ea65SRobert Watson 	 */
1107430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1108c557ae16SIan Dowse 		if (control)
1109c557ae16SIan Dowse 			m_freem(control);
11105c32ea65SRobert Watson 		m_freem(m);
11113329b236SRobert Watson 		return (EMSGSIZE);
1112430d30d8SBill Fenner 	}
1113430d30d8SBill Fenner 
11141b7f0384SBruce M Simpson 	src.sin_family = 0;
11150cfdff24SBjoern A. Zeeb 	INP_RLOCK(inp);
1116f584d74bSMichael Tuexen 	tos = inp->inp_ip_tos;
1117c557ae16SIan Dowse 	if (control != NULL) {
1118c557ae16SIan Dowse 		/*
11193329b236SRobert Watson 		 * XXX: Currently, we assume all the optional information is
11203329b236SRobert Watson 		 * stored in a single mbuf.
1121c557ae16SIan Dowse 		 */
1122c557ae16SIan Dowse 		if (control->m_next) {
11230cfdff24SBjoern A. Zeeb 			INP_RUNLOCK(inp);
1124c557ae16SIan Dowse 			m_freem(control);
11255c32ea65SRobert Watson 			m_freem(m);
11263329b236SRobert Watson 			return (EINVAL);
1127c557ae16SIan Dowse 		}
1128c557ae16SIan Dowse 		for (; control->m_len > 0;
1129c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
1130c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1131c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
1132af1ee11dSRobert Watson 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
1133af1ee11dSRobert Watson 			    || cm->cmsg_len > control->m_len) {
1134c557ae16SIan Dowse 				error = EINVAL;
1135c557ae16SIan Dowse 				break;
1136c557ae16SIan Dowse 			}
1137c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
1138c557ae16SIan Dowse 				continue;
1139c557ae16SIan Dowse 
1140c557ae16SIan Dowse 			switch (cm->cmsg_type) {
1141c557ae16SIan Dowse 			case IP_SENDSRCADDR:
1142c557ae16SIan Dowse 				if (cm->cmsg_len !=
1143c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
1144c557ae16SIan Dowse 					error = EINVAL;
1145c557ae16SIan Dowse 					break;
1146c557ae16SIan Dowse 				}
1147c557ae16SIan Dowse 				bzero(&src, sizeof(src));
1148c557ae16SIan Dowse 				src.sin_family = AF_INET;
1149c557ae16SIan Dowse 				src.sin_len = sizeof(src);
1150c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
1151af1ee11dSRobert Watson 				src.sin_addr =
1152af1ee11dSRobert Watson 				    *(struct in_addr *)CMSG_DATA(cm);
1153c557ae16SIan Dowse 				break;
1154af1ee11dSRobert Watson 
1155f584d74bSMichael Tuexen 			case IP_TOS:
1156f584d74bSMichael Tuexen 				if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
1157f584d74bSMichael Tuexen 					error = EINVAL;
1158f584d74bSMichael Tuexen 					break;
1159f584d74bSMichael Tuexen 				}
1160f584d74bSMichael Tuexen 				tos = *(u_char *)CMSG_DATA(cm);
1161f584d74bSMichael Tuexen 				break;
1162f584d74bSMichael Tuexen 
11639d3ddf43SAdrian Chadd 			case IP_FLOWID:
11649d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
11659d3ddf43SAdrian Chadd 					error = EINVAL;
11669d3ddf43SAdrian Chadd 					break;
11679d3ddf43SAdrian Chadd 				}
11689d3ddf43SAdrian Chadd 				flowid = *(uint32_t *) CMSG_DATA(cm);
11699d3ddf43SAdrian Chadd 				break;
11709d3ddf43SAdrian Chadd 
11719d3ddf43SAdrian Chadd 			case IP_FLOWTYPE:
11729d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
11739d3ddf43SAdrian Chadd 					error = EINVAL;
11749d3ddf43SAdrian Chadd 					break;
11759d3ddf43SAdrian Chadd 				}
11769d3ddf43SAdrian Chadd 				flowid_type = *(uint32_t *) CMSG_DATA(cm);
11779d3ddf43SAdrian Chadd 				use_flowid = 1;
11789d3ddf43SAdrian Chadd 				break;
11799d3ddf43SAdrian Chadd 
11809d3ddf43SAdrian Chadd #ifdef	RSS
11819d3ddf43SAdrian Chadd 			case IP_RSSBUCKETID:
11829d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
11839d3ddf43SAdrian Chadd 					error = EINVAL;
11849d3ddf43SAdrian Chadd 					break;
11859d3ddf43SAdrian Chadd 				}
11869d3ddf43SAdrian Chadd 				/* This is just a placeholder for now */
11879d3ddf43SAdrian Chadd 				break;
11889d3ddf43SAdrian Chadd #endif	/* RSS */
1189c557ae16SIan Dowse 			default:
1190c557ae16SIan Dowse 				error = ENOPROTOOPT;
1191c557ae16SIan Dowse 				break;
1192c557ae16SIan Dowse 			}
1193c557ae16SIan Dowse 			if (error)
1194c557ae16SIan Dowse 				break;
1195c557ae16SIan Dowse 		}
1196c557ae16SIan Dowse 		m_freem(control);
1197c557ae16SIan Dowse 	}
11985c32ea65SRobert Watson 	if (error) {
11990cfdff24SBjoern A. Zeeb 		INP_RUNLOCK(inp);
12005c32ea65SRobert Watson 		m_freem(m);
12013329b236SRobert Watson 		return (error);
12025c32ea65SRobert Watson 	}
12035c32ea65SRobert Watson 
120443cc0bc1SRobert Watson 	/*
120543cc0bc1SRobert Watson 	 * Depending on whether or not the application has bound or connected
1206ca528788SRobert Watson 	 * the socket, we may have to do varying levels of work.  The optimal
1207ca528788SRobert Watson 	 * case is for a connected UDP socket, as a global lock isn't
1208ca528788SRobert Watson 	 * required at all.
120943cc0bc1SRobert Watson 	 *
121043cc0bc1SRobert Watson 	 * In order to decide which we need, we require stability of the
121143cc0bc1SRobert Watson 	 * inpcb binding, which we ensure by acquiring a read lock on the
121243cc0bc1SRobert Watson 	 * inpcb.  This doesn't strictly follow the lock order, so we play
121343cc0bc1SRobert Watson 	 * the trylock and retry game; note that we may end up with more
121443cc0bc1SRobert Watson 	 * conservative locks than required the second time around, so later
121543cc0bc1SRobert Watson 	 * assertions have to accept that.  Further analysis of the number of
121643cc0bc1SRobert Watson 	 * misses under contention is required.
1217fa046d87SRobert Watson 	 *
1218fa046d87SRobert Watson 	 * XXXRW: Check that hash locking update here is correct.
121943cc0bc1SRobert Watson 	 */
1220e06e816fSKevin Lo 	pr = inp->inp_socket->so_proto->pr_protocol;
1221e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(pr);
122243cc0bc1SRobert Watson 	sin = (struct sockaddr_in *)addr;
122343cc0bc1SRobert Watson 	if (sin != NULL &&
122443cc0bc1SRobert Watson 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
122543cc0bc1SRobert Watson 		INP_RUNLOCK(inp);
12268501a69cSRobert Watson 		INP_WLOCK(inp);
1227e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1228fa046d87SRobert Watson 		unlock_udbinfo = UH_WLOCKED;
122943cc0bc1SRobert Watson 	} else if ((sin != NULL && (
123043cc0bc1SRobert Watson 	    (sin->sin_addr.s_addr == INADDR_ANY) ||
123143cc0bc1SRobert Watson 	    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
123243cc0bc1SRobert Watson 	    (inp->inp_laddr.s_addr == INADDR_ANY) ||
123343cc0bc1SRobert Watson 	    (inp->inp_lport == 0))) ||
123443cc0bc1SRobert Watson 	    (src.sin_family == AF_INET)) {
1235e06e816fSKevin Lo 		INP_HASH_RLOCK(pcbinfo);
1236fa046d87SRobert Watson 		unlock_udbinfo = UH_RLOCKED;
123743cc0bc1SRobert Watson 	} else
1238fa046d87SRobert Watson 		unlock_udbinfo = UH_UNLOCKED;
12395c32ea65SRobert Watson 
12401b7f0384SBruce M Simpson 	/*
12411b7f0384SBruce M Simpson 	 * If the IP_SENDSRCADDR control message was specified, override the
12421b7f0384SBruce M Simpson 	 * source address for this datagram.  Its use is invalidated if the
12431b7f0384SBruce M Simpson 	 * address thus specified is incomplete or clobbers other inpcbs.
12441b7f0384SBruce M Simpson 	 */
124590162a4eSIan Dowse 	laddr = inp->inp_laddr;
124690162a4eSIan Dowse 	lport = inp->inp_lport;
12471b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET) {
1248e06e816fSKevin Lo 		INP_HASH_LOCK_ASSERT(pcbinfo);
12491b7f0384SBruce M Simpson 		if ((lport == 0) ||
12501b7f0384SBruce M Simpson 		    (laddr.s_addr == INADDR_ANY &&
12511b7f0384SBruce M Simpson 		     src.sin_addr.s_addr == INADDR_ANY)) {
1252c557ae16SIan Dowse 			error = EINVAL;
1253c557ae16SIan Dowse 			goto release;
1254c557ae16SIan Dowse 		}
1255c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
1256b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
1257c557ae16SIan Dowse 		if (error)
1258c557ae16SIan Dowse 			goto release;
1259c557ae16SIan Dowse 	}
1260c557ae16SIan Dowse 
12613144b7d3SRobert Watson 	/*
12623144b7d3SRobert Watson 	 * If a UDP socket has been connected, then a local address/port will
12633144b7d3SRobert Watson 	 * have been selected and bound.
12643144b7d3SRobert Watson 	 *
126543cc0bc1SRobert Watson 	 * If a UDP socket has not been connected to, then an explicit
12663144b7d3SRobert Watson 	 * destination address must be used, in which case a local
12673144b7d3SRobert Watson 	 * address/port may not have been selected and bound.
12683144b7d3SRobert Watson 	 */
126943cc0bc1SRobert Watson 	if (sin != NULL) {
1270c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
1271df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1272df8bae1dSRodney W. Grimes 			error = EISCONN;
1273df8bae1dSRodney W. Grimes 			goto release;
1274df8bae1dSRodney W. Grimes 		}
12753144b7d3SRobert Watson 
12763144b7d3SRobert Watson 		/*
12773144b7d3SRobert Watson 		 * Jail may rewrite the destination address, so let it do
12783144b7d3SRobert Watson 		 * that before we use it.
12793144b7d3SRobert Watson 		 */
1280b89e82ddSJamie Gritton 		error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1281b89e82ddSJamie Gritton 		if (error)
1282413628a7SBjoern A. Zeeb 			goto release;
12833144b7d3SRobert Watson 
12843144b7d3SRobert Watson 		/*
128543cc0bc1SRobert Watson 		 * If a local address or port hasn't yet been selected, or if
128643cc0bc1SRobert Watson 		 * the destination address needs to be rewritten due to using
128743cc0bc1SRobert Watson 		 * a special INADDR_ constant, invoke in_pcbconnect_setup()
128843cc0bc1SRobert Watson 		 * to do the heavy lifting.  Once a port is selected, we
128943cc0bc1SRobert Watson 		 * commit the binding back to the socket; we also commit the
129043cc0bc1SRobert Watson 		 * binding of the address if in jail.
129143cc0bc1SRobert Watson 		 *
129243cc0bc1SRobert Watson 		 * If we already have a valid binding and we're not
129343cc0bc1SRobert Watson 		 * requesting a destination address rewrite, use a fast path.
12943144b7d3SRobert Watson 		 */
129543cc0bc1SRobert Watson 		if (inp->inp_laddr.s_addr == INADDR_ANY ||
129643cc0bc1SRobert Watson 		    inp->inp_lport == 0 ||
129743cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_ANY ||
129843cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_BROADCAST) {
1299e06e816fSKevin Lo 			INP_HASH_LOCK_ASSERT(pcbinfo);
130043cc0bc1SRobert Watson 			error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
130143cc0bc1SRobert Watson 			    &lport, &faddr.s_addr, &fport, NULL,
130243cc0bc1SRobert Watson 			    td->td_ucred);
130390162a4eSIan Dowse 			if (error)
130490162a4eSIan Dowse 				goto release;
130590162a4eSIan Dowse 
130643cc0bc1SRobert Watson 			/*
130743cc0bc1SRobert Watson 			 * XXXRW: Why not commit the port if the address is
130843cc0bc1SRobert Watson 			 * !INADDR_ANY?
130943cc0bc1SRobert Watson 			 */
131090162a4eSIan Dowse 			/* Commit the local port if newly assigned. */
131190162a4eSIan Dowse 			if (inp->inp_laddr.s_addr == INADDR_ANY &&
131290162a4eSIan Dowse 			    inp->inp_lport == 0) {
1313c4d585aeSRobert Watson 				INP_WLOCK_ASSERT(inp);
1314e06e816fSKevin Lo 				INP_HASH_WLOCK_ASSERT(pcbinfo);
13153a1757b9SGleb Smirnoff 				/*
131643cc0bc1SRobert Watson 				 * Remember addr if jailed, to prevent
131743cc0bc1SRobert Watson 				 * rebinding.
13183a1757b9SGleb Smirnoff 				 */
13190304c731SJamie Gritton 				if (prison_flag(td->td_ucred, PR_IP4))
13203a1757b9SGleb Smirnoff 					inp->inp_laddr = laddr;
132190162a4eSIan Dowse 				inp->inp_lport = lport;
132290162a4eSIan Dowse 				if (in_pcbinshash(inp) != 0) {
132390162a4eSIan Dowse 					inp->inp_lport = 0;
132490162a4eSIan Dowse 					error = EAGAIN;
1325df8bae1dSRodney W. Grimes 					goto release;
1326df8bae1dSRodney W. Grimes 				}
132790162a4eSIan Dowse 				inp->inp_flags |= INP_ANONPORT;
132890162a4eSIan Dowse 			}
1329df8bae1dSRodney W. Grimes 		} else {
133043cc0bc1SRobert Watson 			faddr = sin->sin_addr;
133143cc0bc1SRobert Watson 			fport = sin->sin_port;
133243cc0bc1SRobert Watson 		}
133343cc0bc1SRobert Watson 	} else {
1334c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
133590162a4eSIan Dowse 		faddr = inp->inp_faddr;
133690162a4eSIan Dowse 		fport = inp->inp_fport;
133790162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
1338df8bae1dSRodney W. Grimes 			error = ENOTCONN;
1339df8bae1dSRodney W. Grimes 			goto release;
1340df8bae1dSRodney W. Grimes 		}
1341df8bae1dSRodney W. Grimes 	}
1342e6ccd709SRobert Watson 
1343df8bae1dSRodney W. Grimes 	/*
1344e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
1345392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
1346392e8407SRobert Watson 	 * since we won't use that space at this layer.
1347df8bae1dSRodney W. Grimes 	 */
1348eb1b1807SGleb Smirnoff 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1349e6ccd709SRobert Watson 	if (m == NULL) {
1350df8bae1dSRodney W. Grimes 		error = ENOBUFS;
135149b19bfcSBruce M Simpson 		goto release;
1352df8bae1dSRodney W. Grimes 	}
1353e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
1354e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
1355392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
1356df8bae1dSRodney W. Grimes 
1357df8bae1dSRodney W. Grimes 	/*
13583329b236SRobert Watson 	 * Fill in mbuf with extended UDP header and addresses and length put
13593329b236SRobert Watson 	 * into network format.
1360df8bae1dSRodney W. Grimes 	 */
1361df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
1362db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
1363e06e816fSKevin Lo 	ui->ui_pr = pr;
136490162a4eSIan Dowse 	ui->ui_src = laddr;
136590162a4eSIan Dowse 	ui->ui_dst = faddr;
136690162a4eSIan Dowse 	ui->ui_sport = lport;
136790162a4eSIan Dowse 	ui->ui_dport = fport;
1368db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1369e06e816fSKevin Lo 	if (pr == IPPROTO_UDPLITE) {
1370e06e816fSKevin Lo 		struct udpcb *up;
1371e06e816fSKevin Lo 		uint16_t plen;
1372e06e816fSKevin Lo 
1373e06e816fSKevin Lo 		up = intoudpcb(inp);
1374e06e816fSKevin Lo 		cscov = up->u_txcslen;
1375e06e816fSKevin Lo 		plen = (u_short)len + sizeof(struct udphdr);
1376e06e816fSKevin Lo 		if (cscov >= plen)
1377e06e816fSKevin Lo 			cscov = 0;
1378e06e816fSKevin Lo 		ui->ui_len = htons(plen);
1379e06e816fSKevin Lo 		ui->ui_ulen = htons(cscov);
1380e06e816fSKevin Lo 		/*
1381e06e816fSKevin Lo 		 * For UDP-Lite, checksum coverage length of zero means
1382e06e816fSKevin Lo 		 * the entire UDPLite packet is covered by the checksum.
1383e06e816fSKevin Lo 		 */
1384e06e816fSKevin Lo 		cscov_partial = (cscov == 0) ? 0 : 1;
1385e06e816fSKevin Lo 	} else
1386e06e816fSKevin Lo 		ui->ui_v = IPVERSION << 4;
1387df8bae1dSRodney W. Grimes 
1388b2828ad2SAndre Oppermann 	/*
1389b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
1390b2828ad2SAndre Oppermann 	 */
1391b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
1392b2828ad2SAndre Oppermann 		struct ip *ip;
13933329b236SRobert Watson 
1394b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
13958f134647SGleb Smirnoff 		ip->ip_off |= htons(IP_DF);
1396b2828ad2SAndre Oppermann 	}
1397b2828ad2SAndre Oppermann 
1398b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
1399b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
1400b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
1401b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
1402b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
14036fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
14048afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
14058afa2304SBruce M Simpson 
14061175d9d5SRobert Watson #ifdef MAC
14071175d9d5SRobert Watson 	mac_inpcb_create_mbuf(inp, m);
14081175d9d5SRobert Watson #endif
14091175d9d5SRobert Watson 
1410df8bae1dSRodney W. Grimes 	/*
1411db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
1412df8bae1dSRodney W. Grimes 	 */
1413e06e816fSKevin Lo 	ui->ui_sum = 0;
1414a485f139SMichael Tuexen 	if (pr == IPPROTO_UDPLITE) {
1415e06e816fSKevin Lo 		if (inp->inp_flags & INP_ONESBCAST)
1416e06e816fSKevin Lo 			faddr.s_addr = INADDR_BROADCAST;
1417a485f139SMichael Tuexen 		if (cscov_partial) {
1418e06e816fSKevin Lo 			if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1419e06e816fSKevin Lo 				ui->ui_sum = 0xffff;
1420a485f139SMichael Tuexen 		} else {
1421a485f139SMichael Tuexen 			if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1422a485f139SMichael Tuexen 				ui->ui_sum = 0xffff;
1423a485f139SMichael Tuexen 		}
1424a485f139SMichael Tuexen 	} else if (V_udp_cksum) {
14256fbfd582SAndre Oppermann 		if (inp->inp_flags & INP_ONESBCAST)
14268a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
14278a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1428e06e816fSKevin Lo 		    htons((u_short)len + sizeof(struct udphdr) + pr));
1429db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
1430db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1431e06e816fSKevin Lo 	}
14328f134647SGleb Smirnoff 	((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1433ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
1434f584d74bSMichael Tuexen 	((struct ip *)ui)->ip_tos = tos;		/* XXX */
1435026decb8SRobert Watson 	UDPSTAT_INC(udps_opackets);
1436cfa1ca9dSYoshinobu Inoue 
14379d3ddf43SAdrian Chadd 	/*
14389d3ddf43SAdrian Chadd 	 * Setup flowid / RSS information for outbound socket.
14399d3ddf43SAdrian Chadd 	 *
14409d3ddf43SAdrian Chadd 	 * Once the UDP code decides to set a flowid some other way,
14419d3ddf43SAdrian Chadd 	 * this allows the flowid to be overridden by userland.
14429d3ddf43SAdrian Chadd 	 */
14439d3ddf43SAdrian Chadd 	if (use_flowid) {
14449d3ddf43SAdrian Chadd 		m->m_flags |= M_FLOWID;
14459d3ddf43SAdrian Chadd 		m->m_pkthdr.flowid = flowid;
14469d3ddf43SAdrian Chadd 		M_HASHTYPE_SET(m, flowid_type);
14478ad1a83bSAdrian Chadd #ifdef	RSS
14488ad1a83bSAdrian Chadd 	} else {
14498ad1a83bSAdrian Chadd 		uint32_t hash_val, hash_type;
14508ad1a83bSAdrian Chadd 		/*
14518ad1a83bSAdrian Chadd 		 * Calculate an appropriate RSS hash for UDP and
14528ad1a83bSAdrian Chadd 		 * UDP Lite.
14538ad1a83bSAdrian Chadd 		 *
14548ad1a83bSAdrian Chadd 		 * The called function will take care of figuring out
14558ad1a83bSAdrian Chadd 		 * whether a 2-tuple or 4-tuple hash is required based
14568ad1a83bSAdrian Chadd 		 * on the currently configured scheme.
14578ad1a83bSAdrian Chadd 		 *
14588ad1a83bSAdrian Chadd 		 * Later later on connected socket values should be
14598ad1a83bSAdrian Chadd 		 * cached in the inpcb and reused, rather than constantly
14608ad1a83bSAdrian Chadd 		 * re-calculating it.
14618ad1a83bSAdrian Chadd 		 *
14628ad1a83bSAdrian Chadd 		 * UDP Lite is a different protocol number and will
14638ad1a83bSAdrian Chadd 		 * likely end up being hashed as a 2-tuple until
14648ad1a83bSAdrian Chadd 		 * RSS / NICs grow UDP Lite protocol awareness.
14658ad1a83bSAdrian Chadd 		 */
14668ad1a83bSAdrian Chadd 		if (rss_proto_software_hash_v4(faddr, laddr, fport, lport,
14678ad1a83bSAdrian Chadd 		    pr, &hash_val, &hash_type) == 0) {
14688ad1a83bSAdrian Chadd 			m->m_pkthdr.flowid = hash_val;
14698ad1a83bSAdrian Chadd 			m->m_flags |= M_FLOWID;
14708ad1a83bSAdrian Chadd 			M_HASHTYPE_SET(m, hash_type);
14718ad1a83bSAdrian Chadd 		}
14728ad1a83bSAdrian Chadd #endif
14739d3ddf43SAdrian Chadd 	}
14749d3ddf43SAdrian Chadd 
14759d3ddf43SAdrian Chadd #ifdef	RSS
14768ad1a83bSAdrian Chadd 	/*
14778ad1a83bSAdrian Chadd 	 * Don't override with the inp cached flowid value.
14788ad1a83bSAdrian Chadd 	 *
14798ad1a83bSAdrian Chadd 	 * Depending upon the kind of send being done, the inp
14808ad1a83bSAdrian Chadd 	 * flowid/flowtype values may actually not be appropriate
14818ad1a83bSAdrian Chadd 	 * for this particular socket send.
14828ad1a83bSAdrian Chadd 	 *
14838ad1a83bSAdrian Chadd 	 * We should either leave the flowid at zero (which is what is
14848ad1a83bSAdrian Chadd 	 * currently done) or set it to some software generated
14858ad1a83bSAdrian Chadd 	 * hash value based on the packet contents.
14868ad1a83bSAdrian Chadd 	 */
14879d3ddf43SAdrian Chadd 	ipflags |= IP_NODEFAULTFLOWID;
14889d3ddf43SAdrian Chadd #endif	/* RSS */
14899d3ddf43SAdrian Chadd 
1490fa046d87SRobert Watson 	if (unlock_udbinfo == UH_WLOCKED)
1491e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1492fa046d87SRobert Watson 	else if (unlock_udbinfo == UH_RLOCKED)
1493e06e816fSKevin Lo 		INP_HASH_RUNLOCK(pcbinfo);
149457f60867SMark Johnston 	UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
149597d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
14965d846453SSam Leffler 	    inp->inp_moptions, inp);
1497fa046d87SRobert Watson 	if (unlock_udbinfo == UH_WLOCKED)
14988501a69cSRobert Watson 		INP_WUNLOCK(inp);
1499948d0fc9SRobert Watson 	else
1500948d0fc9SRobert Watson 		INP_RUNLOCK(inp);
1501df8bae1dSRodney W. Grimes 	return (error);
1502df8bae1dSRodney W. Grimes 
1503df8bae1dSRodney W. Grimes release:
1504fa046d87SRobert Watson 	if (unlock_udbinfo == UH_WLOCKED) {
1505e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1506948d0fc9SRobert Watson 		INP_WUNLOCK(inp);
1507fa046d87SRobert Watson 	} else if (unlock_udbinfo == UH_RLOCKED) {
1508e06e816fSKevin Lo 		INP_HASH_RUNLOCK(pcbinfo);
150943cc0bc1SRobert Watson 		INP_RUNLOCK(inp);
1510948d0fc9SRobert Watson 	} else
1511948d0fc9SRobert Watson 		INP_RUNLOCK(inp);
1512df8bae1dSRodney W. Grimes 	m_freem(m);
1513df8bae1dSRodney W. Grimes 	return (error);
1514df8bae1dSRodney W. Grimes }
1515df8bae1dSRodney W. Grimes 
15167b495c44SVANHULLEBUS Yvan 
15177b495c44SVANHULLEBUS Yvan #if defined(IPSEC) && defined(IPSEC_NAT_T)
15187b495c44SVANHULLEBUS Yvan /*
15197b495c44SVANHULLEBUS Yvan  * Potentially decap ESP in UDP frame.  Check for an ESP header
15207b495c44SVANHULLEBUS Yvan  * and optional marker; if present, strip the UDP header and
15217b495c44SVANHULLEBUS Yvan  * push the result through IPSec.
15227b495c44SVANHULLEBUS Yvan  *
15237b495c44SVANHULLEBUS Yvan  * Returns mbuf to be processed (potentially re-allocated) or
15247b495c44SVANHULLEBUS Yvan  * NULL if consumed and/or processed.
15257b495c44SVANHULLEBUS Yvan  */
15267b495c44SVANHULLEBUS Yvan static struct mbuf *
15277b495c44SVANHULLEBUS Yvan udp4_espdecap(struct inpcb *inp, struct mbuf *m, int off)
15287b495c44SVANHULLEBUS Yvan {
15297b495c44SVANHULLEBUS Yvan 	size_t minlen, payload, skip, iphlen;
15307b495c44SVANHULLEBUS Yvan 	caddr_t data;
15317b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
15327b495c44SVANHULLEBUS Yvan 	struct m_tag *tag;
15337b495c44SVANHULLEBUS Yvan 	struct udphdr *udphdr;
15347b495c44SVANHULLEBUS Yvan 	struct ip *ip;
15357b495c44SVANHULLEBUS Yvan 
15367b495c44SVANHULLEBUS Yvan 	INP_RLOCK_ASSERT(inp);
15377b495c44SVANHULLEBUS Yvan 
15387b495c44SVANHULLEBUS Yvan 	/*
15397b495c44SVANHULLEBUS Yvan 	 * Pull up data so the longest case is contiguous:
15407b495c44SVANHULLEBUS Yvan 	 *    IP/UDP hdr + non ESP marker + ESP hdr.
15417b495c44SVANHULLEBUS Yvan 	 */
15427b495c44SVANHULLEBUS Yvan 	minlen = off + sizeof(uint64_t) + sizeof(struct esp);
15437b495c44SVANHULLEBUS Yvan 	if (minlen > m->m_pkthdr.len)
15447b495c44SVANHULLEBUS Yvan 		minlen = m->m_pkthdr.len;
15457b495c44SVANHULLEBUS Yvan 	if ((m = m_pullup(m, minlen)) == NULL) {
15466794f460SAndrey V. Elsukov 		IPSECSTAT_INC(ips_in_inval);
15477b495c44SVANHULLEBUS Yvan 		return (NULL);		/* Bypass caller processing. */
15487b495c44SVANHULLEBUS Yvan 	}
15497b495c44SVANHULLEBUS Yvan 	data = mtod(m, caddr_t);	/* Points to ip header. */
15507b495c44SVANHULLEBUS Yvan 	payload = m->m_len - off;	/* Size of payload. */
15517b495c44SVANHULLEBUS Yvan 
15527b495c44SVANHULLEBUS Yvan 	if (payload == 1 && data[off] == '\xff')
15537b495c44SVANHULLEBUS Yvan 		return (m);		/* NB: keepalive packet, no decap. */
15547b495c44SVANHULLEBUS Yvan 
15557b495c44SVANHULLEBUS Yvan 	up = intoudpcb(inp);
15567b495c44SVANHULLEBUS Yvan 	KASSERT(up != NULL, ("%s: udpcb NULL", __func__));
15577b495c44SVANHULLEBUS Yvan 	KASSERT((up->u_flags & UF_ESPINUDP_ALL) != 0,
15587b495c44SVANHULLEBUS Yvan 	    ("u_flags 0x%x", up->u_flags));
15597b495c44SVANHULLEBUS Yvan 
15607b495c44SVANHULLEBUS Yvan 	/*
15617b495c44SVANHULLEBUS Yvan 	 * Check that the payload is large enough to hold an
15627b495c44SVANHULLEBUS Yvan 	 * ESP header and compute the amount of data to remove.
15637b495c44SVANHULLEBUS Yvan 	 *
15647b495c44SVANHULLEBUS Yvan 	 * NB: the caller has already done a pullup for us.
15657b495c44SVANHULLEBUS Yvan 	 * XXX can we assume alignment and eliminate bcopys?
15667b495c44SVANHULLEBUS Yvan 	 */
15677b495c44SVANHULLEBUS Yvan 	if (up->u_flags & UF_ESPINUDP_NON_IKE) {
15687b495c44SVANHULLEBUS Yvan 		/*
15697b495c44SVANHULLEBUS Yvan 		 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
15707b495c44SVANHULLEBUS Yvan 		 * draft-ietf-ipsec-udp-encaps-(00/)01.txt, ignoring
15717b495c44SVANHULLEBUS Yvan 		 * possible AH mode non-IKE marker+non-ESP marker
15727b495c44SVANHULLEBUS Yvan 		 * from draft-ietf-ipsec-udp-encaps-00.txt.
15737b495c44SVANHULLEBUS Yvan 		 */
15747b495c44SVANHULLEBUS Yvan 		uint64_t marker;
15757b495c44SVANHULLEBUS Yvan 
15767b495c44SVANHULLEBUS Yvan 		if (payload <= sizeof(uint64_t) + sizeof(struct esp))
15777b495c44SVANHULLEBUS Yvan 			return (m);	/* NB: no decap. */
15787b495c44SVANHULLEBUS Yvan 		bcopy(data + off, &marker, sizeof(uint64_t));
15797b495c44SVANHULLEBUS Yvan 		if (marker != 0)	/* Non-IKE marker. */
15807b495c44SVANHULLEBUS Yvan 			return (m);	/* NB: no decap. */
15817b495c44SVANHULLEBUS Yvan 		skip = sizeof(uint64_t) + sizeof(struct udphdr);
15827b495c44SVANHULLEBUS Yvan 	} else {
15837b495c44SVANHULLEBUS Yvan 		uint32_t spi;
15847b495c44SVANHULLEBUS Yvan 
15857b495c44SVANHULLEBUS Yvan 		if (payload <= sizeof(struct esp)) {
15866794f460SAndrey V. Elsukov 			IPSECSTAT_INC(ips_in_inval);
15877b495c44SVANHULLEBUS Yvan 			m_freem(m);
15887b495c44SVANHULLEBUS Yvan 			return (NULL);	/* Discard. */
15897b495c44SVANHULLEBUS Yvan 		}
15907b495c44SVANHULLEBUS Yvan 		bcopy(data + off, &spi, sizeof(uint32_t));
15917b495c44SVANHULLEBUS Yvan 		if (spi == 0)		/* Non-ESP marker. */
15927b495c44SVANHULLEBUS Yvan 			return (m);	/* NB: no decap. */
15937b495c44SVANHULLEBUS Yvan 		skip = sizeof(struct udphdr);
15947b495c44SVANHULLEBUS Yvan 	}
15957b495c44SVANHULLEBUS Yvan 
15967b495c44SVANHULLEBUS Yvan 	/*
15977b495c44SVANHULLEBUS Yvan 	 * Setup a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
15987b495c44SVANHULLEBUS Yvan 	 * the UDP ports. This is required if we want to select
15997b495c44SVANHULLEBUS Yvan 	 * the right SPD for multiple hosts behind same NAT.
16007b495c44SVANHULLEBUS Yvan 	 *
16017b495c44SVANHULLEBUS Yvan 	 * NB: ports are maintained in network byte order everywhere
16027b495c44SVANHULLEBUS Yvan 	 *     in the NAT-T code.
16037b495c44SVANHULLEBUS Yvan 	 */
16047b495c44SVANHULLEBUS Yvan 	tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
16057b495c44SVANHULLEBUS Yvan 		2 * sizeof(uint16_t), M_NOWAIT);
16067b495c44SVANHULLEBUS Yvan 	if (tag == NULL) {
16076794f460SAndrey V. Elsukov 		IPSECSTAT_INC(ips_in_nomem);
16087b495c44SVANHULLEBUS Yvan 		m_freem(m);
16097b495c44SVANHULLEBUS Yvan 		return (NULL);		/* Discard. */
16107b495c44SVANHULLEBUS Yvan 	}
16117b495c44SVANHULLEBUS Yvan 	iphlen = off - sizeof(struct udphdr);
16127b495c44SVANHULLEBUS Yvan 	udphdr = (struct udphdr *)(data + iphlen);
16137b495c44SVANHULLEBUS Yvan 	((uint16_t *)(tag + 1))[0] = udphdr->uh_sport;
16147b495c44SVANHULLEBUS Yvan 	((uint16_t *)(tag + 1))[1] = udphdr->uh_dport;
16157b495c44SVANHULLEBUS Yvan 	m_tag_prepend(m, tag);
16167b495c44SVANHULLEBUS Yvan 
16177b495c44SVANHULLEBUS Yvan 	/*
16187b495c44SVANHULLEBUS Yvan 	 * Remove the UDP header (and possibly the non ESP marker)
16197b495c44SVANHULLEBUS Yvan 	 * IP header length is iphlen
16207b495c44SVANHULLEBUS Yvan 	 * Before:
16217b495c44SVANHULLEBUS Yvan 	 *   <--- off --->
16227b495c44SVANHULLEBUS Yvan 	 *   +----+------+-----+
16237b495c44SVANHULLEBUS Yvan 	 *   | IP |  UDP | ESP |
16247b495c44SVANHULLEBUS Yvan 	 *   +----+------+-----+
16257b495c44SVANHULLEBUS Yvan 	 *        <-skip->
16267b495c44SVANHULLEBUS Yvan 	 * After:
16277b495c44SVANHULLEBUS Yvan 	 *          +----+-----+
16287b495c44SVANHULLEBUS Yvan 	 *          | IP | ESP |
16297b495c44SVANHULLEBUS Yvan 	 *          +----+-----+
16307b495c44SVANHULLEBUS Yvan 	 *   <-skip->
16317b495c44SVANHULLEBUS Yvan 	 */
16327b495c44SVANHULLEBUS Yvan 	ovbcopy(data, data + skip, iphlen);
16337b495c44SVANHULLEBUS Yvan 	m_adj(m, skip);
16347b495c44SVANHULLEBUS Yvan 
16357b495c44SVANHULLEBUS Yvan 	ip = mtod(m, struct ip *);
16368f134647SGleb Smirnoff 	ip->ip_len = htons(ntohs(ip->ip_len) - skip);
16377b495c44SVANHULLEBUS Yvan 	ip->ip_p = IPPROTO_ESP;
16387b495c44SVANHULLEBUS Yvan 
16397b495c44SVANHULLEBUS Yvan 	/*
16407b495c44SVANHULLEBUS Yvan 	 * We cannot yet update the cksums so clear any
16417b495c44SVANHULLEBUS Yvan 	 * h/w cksum flags as they are no longer valid.
16427b495c44SVANHULLEBUS Yvan 	 */
16437b495c44SVANHULLEBUS Yvan 	if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)
16447b495c44SVANHULLEBUS Yvan 		m->m_pkthdr.csum_flags &= ~(CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
16457b495c44SVANHULLEBUS Yvan 
16467b495c44SVANHULLEBUS Yvan 	(void) ipsec4_common_input(m, iphlen, ip->ip_p);
16477b495c44SVANHULLEBUS Yvan 	return (NULL);			/* NB: consumed, bypass processing. */
16487b495c44SVANHULLEBUS Yvan }
16497b495c44SVANHULLEBUS Yvan #endif /* defined(IPSEC) && defined(IPSEC_NAT_T) */
16507b495c44SVANHULLEBUS Yvan 
1651ac45e92fSRobert Watson static void
1652d0390e05SGarrett Wollman udp_abort(struct socket *so)
1653df8bae1dSRodney W. Grimes {
1654d0390e05SGarrett Wollman 	struct inpcb *inp;
1655e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1656df8bae1dSRodney W. Grimes 
1657e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1658d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
165914ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
16608501a69cSRobert Watson 	INP_WLOCK(inp);
1661a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1662e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1663a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1664a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1665e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1666d0390e05SGarrett Wollman 		soisdisconnected(so);
1667a152f8a3SRobert Watson 	}
16688501a69cSRobert Watson 	INP_WUNLOCK(inp);
1669df8bae1dSRodney W. Grimes }
1670df8bae1dSRodney W. Grimes 
1671d0390e05SGarrett Wollman static int
1672b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
1673d0390e05SGarrett Wollman {
1674d0390e05SGarrett Wollman 	struct inpcb *inp;
1675e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1676277afaffSRobert Watson 	int error;
1677d0390e05SGarrett Wollman 
1678e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1679d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
168014ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1681cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
1682f24618aaSRobert Watson 	if (error)
16833329b236SRobert Watson 		return (error);
1684e06e816fSKevin Lo 	INP_INFO_WLOCK(pcbinfo);
1685e06e816fSKevin Lo 	error = in_pcballoc(so, pcbinfo);
168653b57cd1SSam Leffler 	if (error) {
1687e06e816fSKevin Lo 		INP_INFO_WUNLOCK(pcbinfo);
16883329b236SRobert Watson 		return (error);
168953b57cd1SSam Leffler 	}
1690cfa1ca9dSYoshinobu Inoue 
169168b5629bSRobert Watson 	inp = sotoinpcb(so);
1692cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1693603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
16946a9148feSBjoern A. Zeeb 
16956a9148feSBjoern A. Zeeb 	error = udp_newudpcb(inp);
16966a9148feSBjoern A. Zeeb 	if (error) {
16976a9148feSBjoern A. Zeeb 		in_pcbdetach(inp);
16986a9148feSBjoern A. Zeeb 		in_pcbfree(inp);
1699e06e816fSKevin Lo 		INP_INFO_WUNLOCK(pcbinfo);
17006a9148feSBjoern A. Zeeb 		return (error);
17016a9148feSBjoern A. Zeeb 	}
17026a9148feSBjoern A. Zeeb 
1703c7c7ea4bSRandall Stewart 	INP_WUNLOCK(inp);
1704e06e816fSKevin Lo 	INP_INFO_WUNLOCK(pcbinfo);
1705c7c7ea4bSRandall Stewart 	return (0);
1706c7c7ea4bSRandall Stewart }
170779288c11SBjoern A. Zeeb #endif /* INET */
1708c7c7ea4bSRandall Stewart 
1709c7c7ea4bSRandall Stewart int
1710c7c7ea4bSRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f)
1711c7c7ea4bSRandall Stewart {
1712c7c7ea4bSRandall Stewart 	struct inpcb *inp;
17136a9148feSBjoern A. Zeeb 	struct udpcb *up;
1714c7c7ea4bSRandall Stewart 
171568b5629bSRobert Watson 	KASSERT(so->so_type == SOCK_DGRAM,
171668b5629bSRobert Watson 	    ("udp_set_kernel_tunneling: !dgram"));
171768b5629bSRobert Watson 	inp = sotoinpcb(so);
171868b5629bSRobert Watson 	KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1719c7c7ea4bSRandall Stewart 	INP_WLOCK(inp);
17206a9148feSBjoern A. Zeeb 	up = intoudpcb(inp);
17216a9148feSBjoern A. Zeeb 	if (up->u_tun_func != NULL) {
1722bbb0e3d9SRandall Stewart 		INP_WUNLOCK(inp);
1723bbb0e3d9SRandall Stewart 		return (EBUSY);
1724bbb0e3d9SRandall Stewart 	}
17256a9148feSBjoern A. Zeeb 	up->u_tun_func = f;
17268501a69cSRobert Watson 	INP_WUNLOCK(inp);
17273329b236SRobert Watson 	return (0);
1728df8bae1dSRodney W. Grimes }
1729d0390e05SGarrett Wollman 
173079288c11SBjoern A. Zeeb #ifdef INET
1731d0390e05SGarrett Wollman static int
1732b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1733d0390e05SGarrett Wollman {
1734d0390e05SGarrett Wollman 	struct inpcb *inp;
1735e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1736277afaffSRobert Watson 	int error;
1737d0390e05SGarrett Wollman 
1738e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1739d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
174014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
17418501a69cSRobert Watson 	INP_WLOCK(inp);
1742e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1743b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
1744e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
17458501a69cSRobert Watson 	INP_WUNLOCK(inp);
17463329b236SRobert Watson 	return (error);
1747d0390e05SGarrett Wollman }
1748d0390e05SGarrett Wollman 
1749a152f8a3SRobert Watson static void
1750a152f8a3SRobert Watson udp_close(struct socket *so)
1751a152f8a3SRobert Watson {
1752a152f8a3SRobert Watson 	struct inpcb *inp;
1753e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1754a152f8a3SRobert Watson 
1755e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1756a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1757a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
17588501a69cSRobert Watson 	INP_WLOCK(inp);
1759a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1760e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1761a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1762a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1763e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1764a152f8a3SRobert Watson 		soisdisconnected(so);
1765a152f8a3SRobert Watson 	}
17668501a69cSRobert Watson 	INP_WUNLOCK(inp);
1767a152f8a3SRobert Watson }
1768a152f8a3SRobert Watson 
1769d0390e05SGarrett Wollman static int
1770b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1771d0390e05SGarrett Wollman {
1772d0390e05SGarrett Wollman 	struct inpcb *inp;
1773e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
177475c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1775e06e816fSKevin Lo 	int error;
1776d0390e05SGarrett Wollman 
1777e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1778d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
177914ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
17808501a69cSRobert Watson 	INP_WLOCK(inp);
1781f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
17828501a69cSRobert Watson 		INP_WUNLOCK(inp);
17833329b236SRobert Watson 		return (EISCONN);
1784f76fcf6dSJeffrey Hsu 	}
178575c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1786b89e82ddSJamie Gritton 	error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1787b89e82ddSJamie Gritton 	if (error != 0) {
1788413628a7SBjoern A. Zeeb 		INP_WUNLOCK(inp);
1789b89e82ddSJamie Gritton 		return (error);
1790413628a7SBjoern A. Zeeb 	}
1791e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1792b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
1793e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
17944cc20ab1SSeigo Tanimura 	if (error == 0)
1795df8bae1dSRodney W. Grimes 		soisconnected(so);
17968501a69cSRobert Watson 	INP_WUNLOCK(inp);
17973329b236SRobert Watson 	return (error);
1798df8bae1dSRodney W. Grimes }
1799d0390e05SGarrett Wollman 
1800bc725eafSRobert Watson static void
1801d0390e05SGarrett Wollman udp_detach(struct socket *so)
1802d0390e05SGarrett Wollman {
1803d0390e05SGarrett Wollman 	struct inpcb *inp;
1804e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
18056a9148feSBjoern A. Zeeb 	struct udpcb *up;
1806d0390e05SGarrett Wollman 
1807e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1808d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
180914ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1810a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1811a152f8a3SRobert Watson 	    ("udp_detach: not disconnected"));
1812e06e816fSKevin Lo 	INP_INFO_WLOCK(pcbinfo);
18138501a69cSRobert Watson 	INP_WLOCK(inp);
18146a9148feSBjoern A. Zeeb 	up = intoudpcb(inp);
18156a9148feSBjoern A. Zeeb 	KASSERT(up != NULL, ("%s: up == NULL", __func__));
18166a9148feSBjoern A. Zeeb 	inp->inp_ppcb = NULL;
1817d0390e05SGarrett Wollman 	in_pcbdetach(inp);
181814ba8addSRobert Watson 	in_pcbfree(inp);
1819e06e816fSKevin Lo 	INP_INFO_WUNLOCK(pcbinfo);
18206a9148feSBjoern A. Zeeb 	udp_discardcb(up);
1821d0390e05SGarrett Wollman }
1822d0390e05SGarrett Wollman 
1823d0390e05SGarrett Wollman static int
1824d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1825d0390e05SGarrett Wollman {
1826d0390e05SGarrett Wollman 	struct inpcb *inp;
1827e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1828d0390e05SGarrett Wollman 
1829e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1830d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
183114ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
18328501a69cSRobert Watson 	INP_WLOCK(inp);
1833f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
18348501a69cSRobert Watson 		INP_WUNLOCK(inp);
18353329b236SRobert Watson 		return (ENOTCONN);
1836f76fcf6dSJeffrey Hsu 	}
1837e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1838df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1839df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1840e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
1841d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1842d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1843d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
18448501a69cSRobert Watson 	INP_WUNLOCK(inp);
18453329b236SRobert Watson 	return (0);
1846df8bae1dSRodney W. Grimes }
1847df8bae1dSRodney W. Grimes 
1848d0390e05SGarrett Wollman static int
184957bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1850b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1851d0390e05SGarrett Wollman {
1852d0390e05SGarrett Wollman 	struct inpcb *inp;
1853d0390e05SGarrett Wollman 
1854d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
185514ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
18563329b236SRobert Watson 	return (udp_output(inp, m, addr, control, td));
1857d0390e05SGarrett Wollman }
185879288c11SBjoern A. Zeeb #endif /* INET */
1859d0390e05SGarrett Wollman 
186076429de4SYoshinobu Inoue int
1861d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1862d0390e05SGarrett Wollman {
1863d0390e05SGarrett Wollman 	struct inpcb *inp;
1864d0390e05SGarrett Wollman 
1865d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
186614ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
18678501a69cSRobert Watson 	INP_WLOCK(inp);
1868d0390e05SGarrett Wollman 	socantsendmore(so);
18698501a69cSRobert Watson 	INP_WUNLOCK(inp);
18703329b236SRobert Watson 	return (0);
1871d0390e05SGarrett Wollman }
1872d0390e05SGarrett Wollman 
187379288c11SBjoern A. Zeeb #ifdef INET
1874d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1875756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1876756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1877756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1878756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1879756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1880756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1881756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
188254d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1883756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
18845df3e839SRobert Watson 	.pru_soreceive =	soreceive_dgram,
188559b8854eSRobert Watson 	.pru_sosend =		sosend_dgram,
1886756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
188754d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1888a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1889a152f8a3SRobert Watson 	.pru_close =		udp_close,
1890d0390e05SGarrett Wollman };
189179288c11SBjoern A. Zeeb #endif /* INET */
1892