xref: /freebsd/sys/netinet/udp_usrreq.c (revision 9d3ddf438402c8e711dd639be6052d51cb45b680)
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"
46*9d3ddf43SAdrian 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>
93df8bae1dSRodney W. Grimes 
94b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
95b9234fafSSam Leffler #include <netipsec/ipsec.h>
967b495c44SVANHULLEBUS Yvan #include <netipsec/esp.h>
973329b236SRobert Watson #endif
98b9234fafSSam Leffler 
99db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
100db4f9cc7SJonathan Lemon 
101aed55708SRobert Watson #include <security/mac/mac_framework.h>
102aed55708SRobert Watson 
103df8bae1dSRodney W. Grimes /*
104e06e816fSKevin Lo  * UDP and UDP-Lite protocols implementation.
105df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
106e06e816fSKevin Lo  * Per RFC 3828, July, 2004.
107df8bae1dSRodney W. Grimes  */
10874eb3236SWarner Losh 
10974eb3236SWarner Losh /*
1103329b236SRobert Watson  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
1113329b236SRobert Watson  * removes the only data integrity mechanism for packets and malformed
112f5514f08SRobert Watson  * packets that would otherwise be discarded due to bad checksums, and may
113f5514f08SRobert Watson  * cause problems (especially for NFS data blocks).
11474eb3236SWarner Losh  */
11540b676beSBjoern A. Zeeb VNET_DEFINE(int, udp_cksum) = 1;
11640b676beSBjoern A. Zeeb SYSCTL_VNET_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
11740b676beSBjoern A. Zeeb     &VNET_NAME(udp_cksum), 0, "compute udp checksum");
118df8bae1dSRodney W. Grimes 
119afdb4274SRobert Watson int	udp_log_in_vain = 0;
120816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
121afdb4274SRobert Watson     &udp_log_in_vain, 0, "Log all incoming UDP packets");
122816a3d83SPoul-Henning Kamp 
12382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, udp_blackhole) = 0;
124eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
125eddfbb76SRobert Watson     &VNET_NAME(udp_blackhole), 0,
1263329b236SRobert Watson     "Do not send port unreachables for refused connects");
12716f7f31fSGeoff Rehmet 
12843bbb6aaSRobert Watson u_long	udp_sendspace = 9216;		/* really max datagram size */
12943bbb6aaSRobert Watson 					/* 40 1K datagrams */
13043bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
13143bbb6aaSRobert Watson     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
13243bbb6aaSRobert Watson 
13343bbb6aaSRobert Watson u_long	udp_recvspace = 40 * (1024 +
13443bbb6aaSRobert Watson #ifdef INET6
13543bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in6)
13643bbb6aaSRobert Watson #else
13743bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in)
13843bbb6aaSRobert Watson #endif
13943bbb6aaSRobert Watson 				      );
14043bbb6aaSRobert Watson 
14143bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14243bbb6aaSRobert Watson     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
14343bbb6aaSRobert Watson 
144eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, udb);		/* from udp_var.h */
145eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, udbinfo);
146e06e816fSKevin Lo VNET_DEFINE(struct inpcbhead, ulitecb);
147e06e816fSKevin Lo VNET_DEFINE(struct inpcbinfo, ulitecbinfo);
1483e288e62SDimitry Andric static VNET_DEFINE(uma_zone_t, udpcb_zone);
1491e77c105SRobert Watson #define	V_udpcb_zone			VNET(udpcb_zone)
15015bd2b43SDavid Greenman 
15115bd2b43SDavid Greenman #ifndef UDBHASHSIZE
152e2ed8f35SAlexander Motin #define	UDBHASHSIZE	128
15315bd2b43SDavid Greenman #endif
15415bd2b43SDavid Greenman 
1555b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat);		/* from udp_var.h */
1565b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(udpstat);
1575b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat,
1585b7cb97cSAndrey V. Elsukov     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
159f2ea20e6SGarrett Wollman 
1605b7cb97cSAndrey V. Elsukov #ifdef VIMAGE
1615b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(udpstat);
1625b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */
16379288c11SBjoern A. Zeeb #ifdef INET
164bc725eafSRobert Watson static void	udp_detach(struct socket *so);
1654d77a549SAlfred Perlstein static int	udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1664d77a549SAlfred Perlstein 		    struct mbuf *, struct thread *);
16779288c11SBjoern A. Zeeb #endif
16879288c11SBjoern A. Zeeb 
1697b495c44SVANHULLEBUS Yvan #ifdef IPSEC
1707b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
1717b495c44SVANHULLEBUS Yvan #define	UF_ESPINUDP_ALL	(UF_ESPINUDP_NON_IKE|UF_ESPINUDP)
1727b495c44SVANHULLEBUS Yvan #ifdef INET
1737b495c44SVANHULLEBUS Yvan static struct mbuf *udp4_espdecap(struct inpcb *, struct mbuf *, int);
1747b495c44SVANHULLEBUS Yvan #endif
1757b495c44SVANHULLEBUS Yvan #endif /* IPSEC_NAT_T */
1767b495c44SVANHULLEBUS Yvan #endif /* IPSEC */
177df8bae1dSRodney W. Grimes 
1784f590175SPaul Saab static void
1794f590175SPaul Saab udp_zone_change(void *tag)
1804f590175SPaul Saab {
1814f590175SPaul Saab 
182603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
1836a9148feSBjoern A. Zeeb 	uma_zone_set_max(V_udpcb_zone, maxsockets);
1844f590175SPaul Saab }
1854f590175SPaul Saab 
186d915b280SStephan Uphoff static int
187d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags)
188d915b280SStephan Uphoff {
189af1ee11dSRobert Watson 	struct inpcb *inp;
19008651e1fSJohn Baldwin 
191af1ee11dSRobert Watson 	inp = mem;
192d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "udpinp");
193d915b280SStephan Uphoff 	return (0);
194d915b280SStephan Uphoff }
195d915b280SStephan Uphoff 
196e06e816fSKevin Lo static int
197e06e816fSKevin Lo udplite_inpcb_init(void *mem, int size, int flags)
198e06e816fSKevin Lo {
199e06e816fSKevin Lo 	struct inpcb *inp;
200e06e816fSKevin Lo 
201e06e816fSKevin Lo 	inp = mem;
202e06e816fSKevin Lo 	INP_LOCK_INIT(inp, "inp", "udpliteinp");
203e06e816fSKevin Lo 	return (0);
204e06e816fSKevin Lo }
205e06e816fSKevin Lo 
206df8bae1dSRodney W. Grimes void
207af1ee11dSRobert Watson udp_init(void)
208df8bae1dSRodney W. Grimes {
209af1ee11dSRobert Watson 
2109bcd427bSRobert Watson 	in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE,
21152cd27cbSRobert Watson 	    "udp_inpcb", udp_inpcb_init, NULL, UMA_ZONE_NOFREE,
21252cd27cbSRobert Watson 	    IPI_HASHFIELDS_2TUPLE);
2136a9148feSBjoern A. Zeeb 	V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb),
2146a9148feSBjoern A. Zeeb 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2156a9148feSBjoern A. Zeeb 	uma_zone_set_max(V_udpcb_zone, maxsockets);
2166acd596eSPawel Jakub Dawidek 	uma_zone_set_warning(V_udpcb_zone, "kern.ipc.maxsockets limit reached");
2174f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
2184f590175SPaul Saab 	    EVENTHANDLER_PRI_ANY);
219df8bae1dSRodney W. Grimes }
220df8bae1dSRodney W. Grimes 
221e06e816fSKevin Lo void
222e06e816fSKevin Lo udplite_init(void)
223e06e816fSKevin Lo {
224e06e816fSKevin Lo 
225e06e816fSKevin Lo 	in_pcbinfo_init(&V_ulitecbinfo, "udplite", &V_ulitecb, UDBHASHSIZE,
226e06e816fSKevin Lo 	    UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init, NULL,
227e06e816fSKevin Lo 	    UMA_ZONE_NOFREE, IPI_HASHFIELDS_2TUPLE);
228e06e816fSKevin Lo }
229e06e816fSKevin Lo 
230315e3e38SRobert Watson /*
231315e3e38SRobert Watson  * Kernel module interface for updating udpstat.  The argument is an index
232315e3e38SRobert Watson  * into udpstat treated as an array of u_long.  While this encodes the
233315e3e38SRobert Watson  * general layout of udpstat into the caller, it doesn't encode its location,
234315e3e38SRobert Watson  * so that future changes to add, for example, per-CPU stats support won't
235315e3e38SRobert Watson  * cause binary compatibility problems for kernel modules.
236315e3e38SRobert Watson  */
237315e3e38SRobert Watson void
238315e3e38SRobert Watson kmod_udpstat_inc(int statnum)
239315e3e38SRobert Watson {
240315e3e38SRobert Watson 
2415b7cb97cSAndrey V. Elsukov 	counter_u64_add(VNET(udpstat)[statnum], 1);
242315e3e38SRobert Watson }
243315e3e38SRobert Watson 
2446a9148feSBjoern A. Zeeb int
2456a9148feSBjoern A. Zeeb udp_newudpcb(struct inpcb *inp)
2466a9148feSBjoern A. Zeeb {
2476a9148feSBjoern A. Zeeb 	struct udpcb *up;
2486a9148feSBjoern A. Zeeb 
2496a9148feSBjoern A. Zeeb 	up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO);
2506a9148feSBjoern A. Zeeb 	if (up == NULL)
2516a9148feSBjoern A. Zeeb 		return (ENOBUFS);
2526a9148feSBjoern A. Zeeb 	inp->inp_ppcb = up;
2536a9148feSBjoern A. Zeeb 	return (0);
2546a9148feSBjoern A. Zeeb }
2556a9148feSBjoern A. Zeeb 
2566a9148feSBjoern A. Zeeb void
2576a9148feSBjoern A. Zeeb udp_discardcb(struct udpcb *up)
2586a9148feSBjoern A. Zeeb {
2596a9148feSBjoern A. Zeeb 
2606a9148feSBjoern A. Zeeb 	uma_zfree(V_udpcb_zone, up);
2616a9148feSBjoern A. Zeeb }
2626a9148feSBjoern A. Zeeb 
263bc29160dSMarko Zec #ifdef VIMAGE
264bc29160dSMarko Zec void
265bc29160dSMarko Zec udp_destroy(void)
266bc29160dSMarko Zec {
267bc29160dSMarko Zec 
2689bcd427bSRobert Watson 	in_pcbinfo_destroy(&V_udbinfo);
269391dab1cSBjoern A. Zeeb 	uma_zdestroy(V_udpcb_zone);
270bc29160dSMarko Zec }
271e06e816fSKevin Lo 
272e06e816fSKevin Lo void
273e06e816fSKevin Lo udplite_destroy(void)
274e06e816fSKevin Lo {
275e06e816fSKevin Lo 
276e06e816fSKevin Lo 	in_pcbinfo_destroy(&V_ulitecbinfo);
277e06e816fSKevin Lo }
278bc29160dSMarko Zec #endif
279bc29160dSMarko Zec 
28079288c11SBjoern A. Zeeb #ifdef INET
28143bbb6aaSRobert Watson /*
28243bbb6aaSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
28343bbb6aaSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
28443bbb6aaSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
28543bbb6aaSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
28643bbb6aaSRobert Watson  * into the socket code.
28743bbb6aaSRobert Watson  */
28843bbb6aaSRobert Watson static void
28943bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
29043bbb6aaSRobert Watson     struct sockaddr_in *udp_in)
29143bbb6aaSRobert Watson {
29243bbb6aaSRobert Watson 	struct sockaddr *append_sa;
29343bbb6aaSRobert Watson 	struct socket *so;
29443bbb6aaSRobert Watson 	struct mbuf *opts = 0;
29543bbb6aaSRobert Watson #ifdef INET6
29643bbb6aaSRobert Watson 	struct sockaddr_in6 udp_in6;
29743bbb6aaSRobert Watson #endif
2987b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
29943bbb6aaSRobert Watson 
300fa046d87SRobert Watson 	INP_LOCK_ASSERT(inp);
30143bbb6aaSRobert Watson 
30279bb84fbSEdward Tomasz Napierala 	/*
30379bb84fbSEdward Tomasz Napierala 	 * Engage the tunneling protocol.
30479bb84fbSEdward Tomasz Napierala 	 */
30579bb84fbSEdward Tomasz Napierala 	up = intoudpcb(inp);
30679bb84fbSEdward Tomasz Napierala 	if (up->u_tun_func != NULL) {
30779bb84fbSEdward Tomasz Napierala 		(*up->u_tun_func)(n, off, inp);
30879bb84fbSEdward Tomasz Napierala 		return;
30979bb84fbSEdward Tomasz Napierala 	}
31079bb84fbSEdward Tomasz Napierala 
31179bb84fbSEdward Tomasz Napierala 	if (n == NULL)
31279bb84fbSEdward Tomasz Napierala 		return;
31379bb84fbSEdward Tomasz Napierala 
31479bb84fbSEdward Tomasz Napierala 	off += sizeof(struct udphdr);
31579bb84fbSEdward Tomasz Napierala 
31643bbb6aaSRobert Watson #ifdef IPSEC
31743bbb6aaSRobert Watson 	/* Check AH/ESP integrity. */
31843bbb6aaSRobert Watson 	if (ipsec4_in_reject(n, inp)) {
31943bbb6aaSRobert Watson 		m_freem(n);
3206794f460SAndrey V. Elsukov 		IPSECSTAT_INC(ips_in_polvio);
32143bbb6aaSRobert Watson 		return;
32243bbb6aaSRobert Watson 	}
3237b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
3247b495c44SVANHULLEBUS Yvan 	up = intoudpcb(inp);
3257b495c44SVANHULLEBUS Yvan 	KASSERT(up != NULL, ("%s: udpcb NULL", __func__));
3267b495c44SVANHULLEBUS Yvan 	if (up->u_flags & UF_ESPINUDP_ALL) {	/* IPSec UDP encaps. */
3277b495c44SVANHULLEBUS Yvan 		n = udp4_espdecap(inp, n, off);
3287b495c44SVANHULLEBUS Yvan 		if (n == NULL)				/* Consumed. */
3297b495c44SVANHULLEBUS Yvan 			return;
3307b495c44SVANHULLEBUS Yvan 	}
3317b495c44SVANHULLEBUS Yvan #endif /* IPSEC_NAT_T */
33243bbb6aaSRobert Watson #endif /* IPSEC */
33343bbb6aaSRobert Watson #ifdef MAC
33430d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, n) != 0) {
33543bbb6aaSRobert Watson 		m_freem(n);
33643bbb6aaSRobert Watson 		return;
33743bbb6aaSRobert Watson 	}
33879288c11SBjoern A. Zeeb #endif /* MAC */
33943bbb6aaSRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
34043bbb6aaSRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
34143bbb6aaSRobert Watson #ifdef INET6
3429a38ba81SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6)
34348d48eb9SBjoern A. Zeeb 			(void)ip6_savecontrol_v4(inp, n, &opts, NULL);
3449a38ba81SBjoern A. Zeeb 		else
34579288c11SBjoern A. Zeeb #endif /* INET6 */
34643bbb6aaSRobert Watson 			ip_savecontrol(inp, &opts, ip, n);
34743bbb6aaSRobert Watson 	}
34843bbb6aaSRobert Watson #ifdef INET6
34943bbb6aaSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
35043bbb6aaSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
35143bbb6aaSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
35243bbb6aaSRobert Watson 		udp_in6.sin6_family = AF_INET6;
35343bbb6aaSRobert Watson 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
35443bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
35543bbb6aaSRobert Watson 	} else
35679288c11SBjoern A. Zeeb #endif /* INET6 */
35743bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)udp_in;
35843bbb6aaSRobert Watson 	m_adj(n, off);
35943bbb6aaSRobert Watson 
36043bbb6aaSRobert Watson 	so = inp->inp_socket;
36143bbb6aaSRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
36243bbb6aaSRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
36343bbb6aaSRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
36443bbb6aaSRobert Watson 		m_freem(n);
36543bbb6aaSRobert Watson 		if (opts)
36643bbb6aaSRobert Watson 			m_freem(opts);
367026decb8SRobert Watson 		UDPSTAT_INC(udps_fullsock);
36843bbb6aaSRobert Watson 	} else
36943bbb6aaSRobert Watson 		sorwakeup_locked(so);
37043bbb6aaSRobert Watson }
37143bbb6aaSRobert Watson 
3728f5a8818SKevin Lo int
3738f5a8818SKevin Lo udp_input(struct mbuf **mp, int *offp, int proto)
374df8bae1dSRodney W. Grimes {
3753329b236SRobert Watson 	struct ip *ip;
3763329b236SRobert Watson 	struct udphdr *uh;
37771498f30SBruce M Simpson 	struct ifnet *ifp;
3783329b236SRobert Watson 	struct inpcb *inp;
3798f134647SGleb Smirnoff 	uint16_t len, ip_len;
380e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
381df8bae1dSRodney W. Grimes 	struct ip save_ip;
382d4b509bdSRobert Watson 	struct sockaddr_in udp_in;
3838f5a8818SKevin Lo 	struct mbuf *m;
3840b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
3858f5a8818SKevin Lo 	int cscov_partial, iphlen;
386df8bae1dSRodney W. Grimes 
3878f5a8818SKevin Lo 	m = *mp;
3888f5a8818SKevin Lo 	iphlen = *offp;
38971498f30SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
3908f5a8818SKevin Lo 	*mp = NULL;
391026decb8SRobert Watson 	UDPSTAT_INC(udps_ipackets);
392df8bae1dSRodney W. Grimes 
393df8bae1dSRodney W. Grimes 	/*
3943329b236SRobert Watson 	 * Strip IP options, if any; should skip this, make available to
3953329b236SRobert Watson 	 * user, and use on returned packets, but we don't yet have a way to
3963329b236SRobert Watson 	 * check the checksum with options still present.
397df8bae1dSRodney W. Grimes 	 */
398df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
399105bd211SGleb Smirnoff 		ip_stripoptions(m);
400df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
401df8bae1dSRodney W. Grimes 	}
402df8bae1dSRodney W. Grimes 
403df8bae1dSRodney W. Grimes 	/*
404df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
405df8bae1dSRodney W. Grimes 	 */
406df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
407df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
408d1b18731SKevin Lo 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) {
409026decb8SRobert Watson 			UDPSTAT_INC(udps_hdrops);
4108f5a8818SKevin Lo 			return (IPPROTO_DONE);
411df8bae1dSRodney W. Grimes 		}
412df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
413df8bae1dSRodney W. Grimes 	}
414df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
4158f5a8818SKevin Lo 	cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0;
416df8bae1dSRodney W. Grimes 
4173329b236SRobert Watson 	/*
4183329b236SRobert Watson 	 * Destination port of 0 is illegal, based on RFC768.
4193329b236SRobert Watson 	 */
420686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
421f76fcf6dSJeffrey Hsu 		goto badunlocked;
422686cdd19SJun-ichiro itojun Hagino 
423df8bae1dSRodney W. Grimes 	/*
4243329b236SRobert Watson 	 * Construct sockaddr format source address.  Stuff source address
4253329b236SRobert Watson 	 * and datagram in user buffer.
426b9234fafSSam Leffler 	 */
427d4b509bdSRobert Watson 	bzero(&udp_in, sizeof(udp_in));
428d4b509bdSRobert Watson 	udp_in.sin_len = sizeof(udp_in);
429d4b509bdSRobert Watson 	udp_in.sin_family = AF_INET;
430b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
431b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
432b9234fafSSam Leffler 
433b9234fafSSam Leffler 	/*
434af1ee11dSRobert Watson 	 * Make mbuf data length reflect UDP length.  If not enough data to
435af1ee11dSRobert Watson 	 * reflect UDP length, drop.
436df8bae1dSRodney W. Grimes 	 */
437df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
4388ad458a4SGleb Smirnoff 	ip_len = ntohs(ip->ip_len) - iphlen;
4398f5a8818SKevin Lo 	if (proto == IPPROTO_UDPLITE && len == 0) {
440e06e816fSKevin Lo 		/* Zero means checksum over the complete packet. */
441e06e816fSKevin Lo 		len = ip_len;
442e06e816fSKevin Lo 		cscov_partial = 0;
443e06e816fSKevin Lo 	}
4448f134647SGleb Smirnoff 	if (ip_len != len) {
4458f134647SGleb Smirnoff 		if (len > ip_len || len < sizeof(struct udphdr)) {
446026decb8SRobert Watson 			UDPSTAT_INC(udps_badlen);
447f76fcf6dSJeffrey Hsu 			goto badunlocked;
448df8bae1dSRodney W. Grimes 		}
4498f5a8818SKevin Lo 		if (proto == IPPROTO_UDP)
4508f134647SGleb Smirnoff 			m_adj(m, len - ip_len);
451df8bae1dSRodney W. Grimes 	}
4523329b236SRobert Watson 
453df8bae1dSRodney W. Grimes 	/*
4543329b236SRobert Watson 	 * Save a copy of the IP header in case we want restore it for
4553329b236SRobert Watson 	 * sending an ICMP error message in response.
456df8bae1dSRodney W. Grimes 	 */
457603724d3SBjoern A. Zeeb 	if (!V_udp_blackhole)
458df8bae1dSRodney W. Grimes 		save_ip = *ip;
459cce418d3SMatt Jacob 	else
460cce418d3SMatt Jacob 		memset(&save_ip, 0, sizeof(save_ip));
461df8bae1dSRodney W. Grimes 
462df8bae1dSRodney W. Grimes 	/*
463df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
464df8bae1dSRodney W. Grimes 	 */
4656dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
46639629c92SDavid Malone 		u_short uh_sum;
46739629c92SDavid Malone 
468e06e816fSKevin Lo 		if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
469e06e816fSKevin Lo 		    !cscov_partial) {
470db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
47139629c92SDavid Malone 				uh_sum = m->m_pkthdr.csum_data;
472db4f9cc7SJonathan Lemon 			else
47339629c92SDavid Malone 				uh_sum = in_pseudo(ip->ip_src.s_addr,
474506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
4758f5a8818SKevin Lo 				    m->m_pkthdr.csum_data + proto));
47639629c92SDavid Malone 			uh_sum ^= 0xffff;
477db4f9cc7SJonathan Lemon 		} else {
478cb342100SHajimu UMEMOTO 			char b[9];
479af1ee11dSRobert Watson 
480cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
4816effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
4828f5a8818SKevin Lo 			((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ?
483e06e816fSKevin Lo 			    uh->uh_ulen : htons(ip_len);
48439629c92SDavid Malone 			uh_sum = in_cksum(m, len + sizeof (struct ip));
485cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
486db4f9cc7SJonathan Lemon 		}
48739629c92SDavid Malone 		if (uh_sum) {
488026decb8SRobert Watson 			UDPSTAT_INC(udps_badsum);
489df8bae1dSRodney W. Grimes 			m_freem(m);
4908f5a8818SKevin Lo 			return (IPPROTO_DONE);
491df8bae1dSRodney W. Grimes 		}
492fb9aaba0SRuslan Ermilov 	} else
493026decb8SRobert Watson 		UDPSTAT_INC(udps_nosum);
494df8bae1dSRodney W. Grimes 
4958f5a8818SKevin Lo 	pcbinfo = get_inpcbinfo(proto);
496df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
49771498f30SBruce M Simpson 	    in_broadcast(ip->ip_dst, ifp)) {
49882c23ebaSBill Fenner 		struct inpcb *last;
499e06e816fSKevin Lo 		struct inpcbhead *pcblist;
50071498f30SBruce M Simpson 		struct ip_moptions *imo;
5013329b236SRobert Watson 
502e06e816fSKevin Lo 		INP_INFO_RLOCK(pcbinfo);
5038f5a8818SKevin Lo 		pcblist = get_pcblist(proto);
504df8bae1dSRodney W. Grimes 		last = NULL;
505e06e816fSKevin Lo 		LIST_FOREACH(inp, pcblist, inp_list) {
5069c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
507f76fcf6dSJeffrey Hsu 				continue;
508cfa1ca9dSYoshinobu Inoue #ifdef INET6
509369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
5109c1df695SRobert Watson 				continue;
511cfa1ca9dSYoshinobu Inoue #endif
51271498f30SBruce M Simpson 			if (inp->inp_laddr.s_addr != INADDR_ANY &&
51371498f30SBruce M Simpson 			    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
5149c1df695SRobert Watson 				continue;
51571498f30SBruce M Simpson 			if (inp->inp_faddr.s_addr != INADDR_ANY &&
51671498f30SBruce M Simpson 			    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
51771498f30SBruce M Simpson 				continue;
51871498f30SBruce M Simpson 			if (inp->inp_fport != 0 &&
519df8bae1dSRodney W. Grimes 			    inp->inp_fport != uh->uh_sport)
5209c1df695SRobert Watson 				continue;
52171498f30SBruce M Simpson 
522119d85f6SRobert Watson 			INP_RLOCK(inp);
523df8bae1dSRodney W. Grimes 
52483453a06SBruce M Simpson 			/*
525fa046d87SRobert Watson 			 * XXXRW: Because we weren't holding either the inpcb
526fa046d87SRobert Watson 			 * or the hash lock when we checked for a match
527fa046d87SRobert Watson 			 * before, we should probably recheck now that the
528fa046d87SRobert Watson 			 * inpcb lock is held.
529fa046d87SRobert Watson 			 */
530fa046d87SRobert Watson 
531fa046d87SRobert Watson 			/*
53271498f30SBruce M Simpson 			 * Handle socket delivery policy for any-source
53371498f30SBruce M Simpson 			 * and source-specific multicast. [RFC3678]
53483453a06SBruce M Simpson 			 */
53571498f30SBruce M Simpson 			imo = inp->inp_moptions;
536a38b1c8cSRandall Stewart 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
537d10910e6SBruce M Simpson 				struct sockaddr_in	 group;
538d10910e6SBruce M Simpson 				int			 blocked;
539a38b1c8cSRandall Stewart 				if (imo == NULL) {
540a38b1c8cSRandall Stewart 					INP_RUNLOCK(inp);
541a38b1c8cSRandall Stewart 					continue;
542a38b1c8cSRandall Stewart 				}
543d10910e6SBruce M Simpson 				bzero(&group, sizeof(struct sockaddr_in));
544d10910e6SBruce M Simpson 				group.sin_len = sizeof(struct sockaddr_in);
545d10910e6SBruce M Simpson 				group.sin_family = AF_INET;
546d10910e6SBruce M Simpson 				group.sin_addr = ip->ip_dst;
54771498f30SBruce M Simpson 
548d10910e6SBruce M Simpson 				blocked = imo_multi_filter(imo, ifp,
549d10910e6SBruce M Simpson 					(struct sockaddr *)&group,
55071498f30SBruce M Simpson 					(struct sockaddr *)&udp_in);
551d10910e6SBruce M Simpson 				if (blocked != MCAST_PASS) {
552d10910e6SBruce M Simpson 					if (blocked == MCAST_NOTGMEMBER)
55386425c62SRobert Watson 						IPSTAT_INC(ips_notmember);
554d10910e6SBruce M Simpson 					if (blocked == MCAST_NOTSMEMBER ||
555d10910e6SBruce M Simpson 					    blocked == MCAST_MUTED)
556026decb8SRobert Watson 						UDPSTAT_INC(udps_filtermcast);
557119d85f6SRobert Watson 					INP_RUNLOCK(inp);
5589c1df695SRobert Watson 					continue;
5599c1df695SRobert Watson 				}
56083453a06SBruce M Simpson 			}
561df8bae1dSRodney W. Grimes 			if (last != NULL) {
562df8bae1dSRodney W. Grimes 				struct mbuf *n;
563df8bae1dSRodney W. Grimes 
564032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
56579bb84fbSEdward Tomasz Napierala 				udp_append(last, ip, n, iphlen, &udp_in);
5666a9148feSBjoern A. Zeeb 				INP_RUNLOCK(last);
567df8bae1dSRodney W. Grimes 			}
56882c23ebaSBill Fenner 			last = inp;
569df8bae1dSRodney W. Grimes 			/*
570df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
571df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
5723329b236SRobert Watson 			 * socket options set.  This heuristic avoids
5733329b236SRobert Watson 			 * searching through all pcbs in the common case of a
5743329b236SRobert Watson 			 * non-shared port.  It assumes that an application
5753329b236SRobert Watson 			 * will never clear these options after setting them.
576df8bae1dSRodney W. Grimes 			 */
5773329b236SRobert Watson 			if ((last->inp_socket->so_options &
5783329b236SRobert Watson 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
579df8bae1dSRodney W. Grimes 				break;
580df8bae1dSRodney W. Grimes 		}
581df8bae1dSRodney W. Grimes 
582df8bae1dSRodney W. Grimes 		if (last == NULL) {
583df8bae1dSRodney W. Grimes 			/*
5843329b236SRobert Watson 			 * No matching pcb found; discard datagram.  (No need
5853329b236SRobert Watson 			 * to send an ICMP Port Unreachable for a broadcast
5863329b236SRobert Watson 			 * or multicast datgram.)
587df8bae1dSRodney W. Grimes 			 */
588026decb8SRobert Watson 			UDPSTAT_INC(udps_noportbcast);
589fa046d87SRobert Watson 			if (inp)
590fa046d87SRobert Watson 				INP_RUNLOCK(inp);
591e06e816fSKevin Lo 			INP_INFO_RUNLOCK(pcbinfo);
592fa046d87SRobert Watson 			goto badunlocked;
593df8bae1dSRodney W. Grimes 		}
59479bb84fbSEdward Tomasz Napierala 		udp_append(last, ip, m, iphlen, &udp_in);
595c7c7ea4bSRandall Stewart 		INP_RUNLOCK(last);
596e06e816fSKevin Lo 		INP_INFO_RUNLOCK(pcbinfo);
5978f5a8818SKevin Lo 		return (IPPROTO_DONE);
598df8bae1dSRodney W. Grimes 	}
5993329b236SRobert Watson 
600df8bae1dSRodney W. Grimes 	/*
6016d6a026bSDavid Greenman 	 * Locate pcb for datagram.
602df8bae1dSRodney W. Grimes 	 */
603c1de64a4SAndrey V. Elsukov 
6048a006adbSBjoern A. Zeeb 	/*
6058a006adbSBjoern A. Zeeb 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
6068a006adbSBjoern A. Zeeb 	 */
607ffdbf9daSAndrey V. Elsukov 	if ((m->m_flags & M_IP_NEXTHOP) &&
608c1de64a4SAndrey V. Elsukov 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
6098a006adbSBjoern A. Zeeb 		struct sockaddr_in *next_hop;
6108a006adbSBjoern A. Zeeb 
6118a006adbSBjoern A. Zeeb 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
6128a006adbSBjoern A. Zeeb 
6138a006adbSBjoern A. Zeeb 		/*
6148a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
6158a006adbSBjoern A. Zeeb 		 * Already got one like this?
6168a006adbSBjoern A. Zeeb 		 */
617e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
6188a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m);
6198a006adbSBjoern A. Zeeb 		if (!inp) {
6208a006adbSBjoern A. Zeeb 			/*
6218a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
6228a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
6238a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
6248a006adbSBjoern A. Zeeb 			 */
625e06e816fSKevin Lo 			inp = in_pcblookup(pcbinfo, ip->ip_src,
6268a006adbSBjoern A. Zeeb 			    uh->uh_sport, next_hop->sin_addr,
6278a006adbSBjoern A. Zeeb 			    next_hop->sin_port ? htons(next_hop->sin_port) :
6288a006adbSBjoern A. Zeeb 			    uh->uh_dport, INPLOOKUP_WILDCARD |
6298a006adbSBjoern A. Zeeb 			    INPLOOKUP_RLOCKPCB, ifp);
6308a006adbSBjoern A. Zeeb 		}
6318a006adbSBjoern A. Zeeb 		/* Remove the tag from the packet. We don't need it anymore. */
6328a006adbSBjoern A. Zeeb 		m_tag_delete(m, fwd_tag);
633ffdbf9daSAndrey V. Elsukov 		m->m_flags &= ~M_IP_NEXTHOP;
6348a006adbSBjoern A. Zeeb 	} else
635e06e816fSKevin Lo 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
6368a006adbSBjoern A. Zeeb 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
6378a006adbSBjoern A. Zeeb 		    INPLOOKUP_RLOCKPCB, ifp, m);
63815bd2b43SDavid Greenman 	if (inp == NULL) {
639afdb4274SRobert Watson 		if (udp_log_in_vain) {
640df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
64175cfc95fSAndrey A. Chernov 
64275cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
643592071e8SBruce Evans 			log(LOG_INFO,
644592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
645592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
646592071e8SBruce Evans 			    ntohs(uh->uh_sport));
64775cfc95fSAndrey A. Chernov 		}
648026decb8SRobert Watson 		UDPSTAT_INC(udps_noport);
649df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
650026decb8SRobert Watson 			UDPSTAT_INC(udps_noportbcast);
651fa046d87SRobert Watson 			goto badunlocked;
652df8bae1dSRodney W. Grimes 		}
653603724d3SBjoern A. Zeeb 		if (V_udp_blackhole)
654fa046d87SRobert Watson 			goto badunlocked;
6551cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
656fa046d87SRobert Watson 			goto badunlocked;
65704287599SRuslan Ermilov 		*ip = save_ip;
658582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
6598f5a8818SKevin Lo 		return (IPPROTO_DONE);
660df8bae1dSRodney W. Grimes 	}
6613329b236SRobert Watson 
6623329b236SRobert Watson 	/*
6633329b236SRobert Watson 	 * Check the minimum TTL for socket.
6643329b236SRobert Watson 	 */
665fa046d87SRobert Watson 	INP_RLOCK_ASSERT(inp);
66610cc62b7SRobert Watson 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
66710cc62b7SRobert Watson 		INP_RUNLOCK(inp);
668fa046d87SRobert Watson 		m_freem(m);
6698f5a8818SKevin Lo 		return (IPPROTO_DONE);
67010cc62b7SRobert Watson 	}
671e06e816fSKevin Lo 	if (cscov_partial) {
672e06e816fSKevin Lo 		struct udpcb *up;
673e06e816fSKevin Lo 
674e06e816fSKevin Lo 		up = intoudpcb(inp);
675e06e816fSKevin Lo 		if (up->u_rxcslen > len) {
676e06e816fSKevin Lo 			INP_RUNLOCK(inp);
677e06e816fSKevin Lo 			m_freem(m);
6788f5a8818SKevin Lo 			return (IPPROTO_DONE);
679e06e816fSKevin Lo 		}
680e06e816fSKevin Lo 	}
68157f60867SMark Johnston 
6821ad19fb6SMark Johnston 	UDP_PROBE(receive, NULL, inp, ip, inp, uh);
68379bb84fbSEdward Tomasz Napierala 	udp_append(inp, ip, m, iphlen, &udp_in);
684119d85f6SRobert Watson 	INP_RUNLOCK(inp);
6858f5a8818SKevin Lo 	return (IPPROTO_DONE);
68661ffc0b1SJeffrey Hsu 
687f76fcf6dSJeffrey Hsu badunlocked:
688df8bae1dSRodney W. Grimes 	m_freem(m);
6898f5a8818SKevin Lo 	return (IPPROTO_DONE);
690cfa1ca9dSYoshinobu Inoue }
69179288c11SBjoern A. Zeeb #endif /* INET */
692cfa1ca9dSYoshinobu Inoue 
693cfa1ca9dSYoshinobu Inoue /*
6943329b236SRobert Watson  * Notify a udp user of an asynchronous error; just wake up so that they can
6953329b236SRobert Watson  * collect error status.
696df8bae1dSRodney W. Grimes  */
6973ce144eaSJeffrey Hsu struct inpcb *
6983329b236SRobert Watson udp_notify(struct inpcb *inp, int errno)
699df8bae1dSRodney W. Grimes {
7003329b236SRobert Watson 
701ac9ae279SRobert Watson 	/*
702ac9ae279SRobert Watson 	 * While udp_ctlinput() always calls udp_notify() with a read lock
703ac9ae279SRobert Watson 	 * when invoking it directly, in_pcbnotifyall() currently uses write
704ac9ae279SRobert Watson 	 * locks due to sharing code with TCP.  For now, accept either a read
705ac9ae279SRobert Watson 	 * or a write lock, but a read lock is sufficient.
706ac9ae279SRobert Watson 	 */
707ac9ae279SRobert Watson 	INP_LOCK_ASSERT(inp);
7088501a69cSRobert Watson 
709df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
710df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
711df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
7123329b236SRobert Watson 	return (inp);
713df8bae1dSRodney W. Grimes }
714df8bae1dSRodney W. Grimes 
71579288c11SBjoern A. Zeeb #ifdef INET
716e06e816fSKevin Lo static void
717e06e816fSKevin Lo udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip,
718e06e816fSKevin Lo     struct inpcbinfo *pcbinfo)
719df8bae1dSRodney W. Grimes {
720c693a045SJonathan Lemon 	struct ip *ip = vip;
721c693a045SJonathan Lemon 	struct udphdr *uh;
722c693a045SJonathan Lemon 	struct in_addr faddr;
723c693a045SJonathan Lemon 	struct inpcb *inp;
724c693a045SJonathan Lemon 
725c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
726c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
727c693a045SJonathan Lemon 		return;
728df8bae1dSRodney W. Grimes 
72997d8d152SAndre Oppermann 	/*
73097d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
73197d8d152SAndre Oppermann 	 */
73297d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
73397d8d152SAndre Oppermann 		return;
7343329b236SRobert Watson 
73597d8d152SAndre Oppermann 	/*
73697d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
7373329b236SRobert Watson 	 *
7383329b236SRobert Watson 	 * XXX: We never get this from ICMP, otherwise it makes an excellent
7393329b236SRobert Watson 	 * DoS attack on machines with many connections.
74097d8d152SAndre Oppermann 	 */
74197d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
742af1ee11dSRobert Watson 		ip = NULL;
743d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
744df8bae1dSRodney W. Grimes 		return;
745af1ee11dSRobert Watson 	if (ip != NULL) {
746df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
747e06e816fSKevin Lo 		inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
748fa046d87SRobert Watson 		    ip->ip_src, uh->uh_sport, INPLOOKUP_RLOCKPCB, NULL);
749f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
750fa046d87SRobert Watson 			INP_RLOCK_ASSERT(inp);
751f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
752f5514f08SRobert Watson 				udp_notify(inp, inetctlerrmap[cmd]);
753f76fcf6dSJeffrey Hsu 			}
754ac9ae279SRobert Watson 			INP_RUNLOCK(inp);
755f76fcf6dSJeffrey Hsu 		}
756df8bae1dSRodney W. Grimes 	} else
757e06e816fSKevin Lo 		in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd],
758f5514f08SRobert Watson 		    udp_notify);
759df8bae1dSRodney W. Grimes }
760e06e816fSKevin Lo void
761e06e816fSKevin Lo udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
762e06e816fSKevin Lo {
763e06e816fSKevin Lo 
764e06e816fSKevin Lo 	return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo));
765e06e816fSKevin Lo }
766e06e816fSKevin Lo 
767e06e816fSKevin Lo void
768e06e816fSKevin Lo udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip)
769e06e816fSKevin Lo {
770e06e816fSKevin Lo 
771e06e816fSKevin Lo 	return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo));
772e06e816fSKevin Lo }
77379288c11SBjoern A. Zeeb #endif /* INET */
774df8bae1dSRodney W. Grimes 
7750312fbe9SPoul-Henning Kamp static int
77682d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
77798271db4SGarrett Wollman {
778277afaffSRobert Watson 	int error, i, n;
77998271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
78098271db4SGarrett Wollman 	inp_gen_t gencnt;
78198271db4SGarrett Wollman 	struct xinpgen xig;
78298271db4SGarrett Wollman 
78398271db4SGarrett Wollman 	/*
784f5514f08SRobert Watson 	 * The process of preparing the PCB list is too time-consuming and
78598271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
78698271db4SGarrett Wollman 	 */
78798271db4SGarrett Wollman 	if (req->oldptr == 0) {
788603724d3SBjoern A. Zeeb 		n = V_udbinfo.ipi_count;
789c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
790c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
7913329b236SRobert Watson 		return (0);
79298271db4SGarrett Wollman 	}
79398271db4SGarrett Wollman 
79498271db4SGarrett Wollman 	if (req->newptr != 0)
7953329b236SRobert Watson 		return (EPERM);
79698271db4SGarrett Wollman 
79798271db4SGarrett Wollman 	/*
79898271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
79998271db4SGarrett Wollman 	 */
800603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_udbinfo);
801603724d3SBjoern A. Zeeb 	gencnt = V_udbinfo.ipi_gencnt;
802603724d3SBjoern A. Zeeb 	n = V_udbinfo.ipi_count;
803603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_udbinfo);
80498271db4SGarrett Wollman 
80547934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
8065c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
80747934cefSDon Lewis 	if (error != 0)
80847934cefSDon Lewis 		return (error);
8095c38b6dbSDon Lewis 
81098271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
81198271db4SGarrett Wollman 	xig.xig_count = n;
81298271db4SGarrett Wollman 	xig.xig_gen = gencnt;
81398271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
81498271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
81598271db4SGarrett Wollman 	if (error)
8163329b236SRobert Watson 		return (error);
81798271db4SGarrett Wollman 
818a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
81998271db4SGarrett Wollman 	if (inp_list == 0)
8203329b236SRobert Watson 		return (ENOMEM);
82198271db4SGarrett Wollman 
822603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_udbinfo);
823603724d3SBjoern A. Zeeb 	for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n;
824fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
825d0e157f6SBjoern A. Zeeb 		INP_WLOCK(inp);
8262ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
827d0e157f6SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
828d0e157f6SBjoern A. Zeeb 			in_pcbref(inp);
82998271db4SGarrett Wollman 			inp_list[i++] = inp;
830d0e157f6SBjoern A. Zeeb 		}
831d0e157f6SBjoern A. Zeeb 		INP_WUNLOCK(inp);
8324787fd37SPaul Saab 	}
833603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_udbinfo);
83498271db4SGarrett Wollman 	n = i;
83598271db4SGarrett Wollman 
83698271db4SGarrett Wollman 	error = 0;
83798271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
83898271db4SGarrett Wollman 		inp = inp_list[i];
8399622e84fSRobert Watson 		INP_RLOCK(inp);
84098271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
84198271db4SGarrett Wollman 			struct xinpcb xi;
842d0e157f6SBjoern A. Zeeb 
843fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
84498271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
84598271db4SGarrett Wollman 			/* XXX should avoid extra copy */
84698271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
84798271db4SGarrett Wollman 			if (inp->inp_socket)
84898271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
8494b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
8509622e84fSRobert Watson 			INP_RUNLOCK(inp);
85198271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
852d915b280SStephan Uphoff 		} else
8539622e84fSRobert Watson 			INP_RUNLOCK(inp);
85498271db4SGarrett Wollman 	}
855d0e157f6SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_udbinfo);
856d0e157f6SBjoern A. Zeeb 	for (i = 0; i < n; i++) {
857d0e157f6SBjoern A. Zeeb 		inp = inp_list[i];
858fa046d87SRobert Watson 		INP_RLOCK(inp);
859fa046d87SRobert Watson 		if (!in_pcbrele_rlocked(inp))
860fa046d87SRobert Watson 			INP_RUNLOCK(inp);
861d0e157f6SBjoern A. Zeeb 	}
862d0e157f6SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_udbinfo);
863d0e157f6SBjoern A. Zeeb 
86498271db4SGarrett Wollman 	if (!error) {
86598271db4SGarrett Wollman 		/*
8663329b236SRobert Watson 		 * Give the user an updated idea of our state.  If the
8673329b236SRobert Watson 		 * generation differs from what we told her before, she knows
8683329b236SRobert Watson 		 * that something happened while we were processing this
8693329b236SRobert Watson 		 * request, and it might be necessary to retry.
87098271db4SGarrett Wollman 		 */
871603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_udbinfo);
872603724d3SBjoern A. Zeeb 		xig.xig_gen = V_udbinfo.ipi_gencnt;
87398271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
874603724d3SBjoern A. Zeeb 		xig.xig_count = V_udbinfo.ipi_count;
875603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_udbinfo);
87698271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
87798271db4SGarrett Wollman 	}
87898271db4SGarrett Wollman 	free(inp_list, M_TEMP);
8793329b236SRobert Watson 	return (error);
88098271db4SGarrett Wollman }
88198271db4SGarrett Wollman 
88279c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
88379c3d51bSMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
88498271db4SGarrett Wollman     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
88598271db4SGarrett Wollman 
88679288c11SBjoern A. Zeeb #ifdef INET
88798271db4SGarrett Wollman static int
88882d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
889490d50b6SBrian Feldman {
890c0511d3bSBrian Feldman 	struct xucred xuc;
891490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
892490d50b6SBrian Feldman 	struct inpcb *inp;
893277afaffSRobert Watson 	int error;
894490d50b6SBrian Feldman 
89532f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
896490d50b6SBrian Feldman 	if (error)
897490d50b6SBrian Feldman 		return (error);
898490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
899490d50b6SBrian Feldman 	if (error)
900490d50b6SBrian Feldman 		return (error);
901fa046d87SRobert Watson 	inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
902fa046d87SRobert Watson 	    addrs[0].sin_addr, addrs[0].sin_port,
903fa046d87SRobert Watson 	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
9049622e84fSRobert Watson 	if (inp != NULL) {
905fa046d87SRobert Watson 		INP_RLOCK_ASSERT(inp);
9069622e84fSRobert Watson 		if (inp->inp_socket == NULL)
9079622e84fSRobert Watson 			error = ENOENT;
9089622e84fSRobert Watson 		if (error == 0)
909f08ef6c5SBjoern A. Zeeb 			error = cr_canseeinpcb(req->td->td_ucred, inp);
9109622e84fSRobert Watson 		if (error == 0)
91186d02c5cSBjoern A. Zeeb 			cru2x(inp->inp_cred, &xuc);
9129622e84fSRobert Watson 		INP_RUNLOCK(inp);
913fa046d87SRobert Watson 	} else
9149622e84fSRobert Watson 		error = ENOENT;
9150e1eebb8SDon Lewis 	if (error == 0)
9160e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
917490d50b6SBrian Feldman 	return (error);
918490d50b6SBrian Feldman }
919490d50b6SBrian Feldman 
9207ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
9217ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
9227ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
92379288c11SBjoern A. Zeeb #endif /* INET */
924490d50b6SBrian Feldman 
9257b495c44SVANHULLEBUS Yvan int
9267b495c44SVANHULLEBUS Yvan udp_ctloutput(struct socket *so, struct sockopt *sopt)
9277b495c44SVANHULLEBUS Yvan {
9287b495c44SVANHULLEBUS Yvan 	struct inpcb *inp;
9297b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
930e06e816fSKevin Lo 	int isudplite, error, optval;
9317b495c44SVANHULLEBUS Yvan 
932e06e816fSKevin Lo 	error = 0;
933e06e816fSKevin Lo 	isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0;
9347b495c44SVANHULLEBUS Yvan 	inp = sotoinpcb(so);
9357b495c44SVANHULLEBUS Yvan 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
9367b495c44SVANHULLEBUS Yvan 	INP_WLOCK(inp);
937e06e816fSKevin Lo 	if (sopt->sopt_level != so->so_proto->pr_protocol) {
9387b495c44SVANHULLEBUS Yvan #ifdef INET6
9397b495c44SVANHULLEBUS Yvan 		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
9407b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9417b495c44SVANHULLEBUS Yvan 			error = ip6_ctloutput(so, sopt);
94279288c11SBjoern A. Zeeb 		}
9437b495c44SVANHULLEBUS Yvan #endif
94479288c11SBjoern A. Zeeb #if defined(INET) && defined(INET6)
94579288c11SBjoern A. Zeeb 		else
94679288c11SBjoern A. Zeeb #endif
94779288c11SBjoern A. Zeeb #ifdef INET
94879288c11SBjoern A. Zeeb 		{
9497b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9507b495c44SVANHULLEBUS Yvan 			error = ip_ctloutput(so, sopt);
9517b495c44SVANHULLEBUS Yvan 		}
9527b495c44SVANHULLEBUS Yvan #endif
9537b495c44SVANHULLEBUS Yvan 		return (error);
9547b495c44SVANHULLEBUS Yvan 	}
9557b495c44SVANHULLEBUS Yvan 
9567b495c44SVANHULLEBUS Yvan 	switch (sopt->sopt_dir) {
9577b495c44SVANHULLEBUS Yvan 	case SOPT_SET:
9587b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
9597b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
9607b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9617b495c44SVANHULLEBUS Yvan 			error = sooptcopyin(sopt, &optval, sizeof optval,
9627b495c44SVANHULLEBUS Yvan 					    sizeof optval);
9637b495c44SVANHULLEBUS Yvan 			if (error)
9647b495c44SVANHULLEBUS Yvan 				break;
9657b495c44SVANHULLEBUS Yvan 			inp = sotoinpcb(so);
9667b495c44SVANHULLEBUS Yvan 			KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
9677b495c44SVANHULLEBUS Yvan 			INP_WLOCK(inp);
9687b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
9697b495c44SVANHULLEBUS Yvan 			up = intoudpcb(inp);
9707b495c44SVANHULLEBUS Yvan 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
9717b495c44SVANHULLEBUS Yvan #endif
9727b495c44SVANHULLEBUS Yvan 			switch (optval) {
9737b495c44SVANHULLEBUS Yvan 			case 0:
9747b495c44SVANHULLEBUS Yvan 				/* Clear all UDP encap. */
9757b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
9767b495c44SVANHULLEBUS Yvan 				up->u_flags &= ~UF_ESPINUDP_ALL;
9777b495c44SVANHULLEBUS Yvan #endif
9787b495c44SVANHULLEBUS Yvan 				break;
9797b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
9807b495c44SVANHULLEBUS Yvan 			case UDP_ENCAP_ESPINUDP:
9817b495c44SVANHULLEBUS Yvan 			case UDP_ENCAP_ESPINUDP_NON_IKE:
9827b495c44SVANHULLEBUS Yvan 				up->u_flags &= ~UF_ESPINUDP_ALL;
9837b495c44SVANHULLEBUS Yvan 				if (optval == UDP_ENCAP_ESPINUDP)
9847b495c44SVANHULLEBUS Yvan 					up->u_flags |= UF_ESPINUDP;
9857b495c44SVANHULLEBUS Yvan 				else if (optval == UDP_ENCAP_ESPINUDP_NON_IKE)
9867b495c44SVANHULLEBUS Yvan 					up->u_flags |= UF_ESPINUDP_NON_IKE;
9877b495c44SVANHULLEBUS Yvan 				break;
9887b495c44SVANHULLEBUS Yvan #endif
9897b495c44SVANHULLEBUS Yvan 			default:
9907b495c44SVANHULLEBUS Yvan 				error = EINVAL;
9917b495c44SVANHULLEBUS Yvan 				break;
9927b495c44SVANHULLEBUS Yvan 			}
9937b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
9947b495c44SVANHULLEBUS Yvan 			break;
995e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
996e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
997e06e816fSKevin Lo 			if (!isudplite) {
998e06e816fSKevin Lo 				INP_WUNLOCK(inp);
999e06e816fSKevin Lo 				error = ENOPROTOOPT;
1000e06e816fSKevin Lo 				break;
1001e06e816fSKevin Lo 			}
1002e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1003e06e816fSKevin Lo 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1004e06e816fSKevin Lo 			    sizeof(optval));
1005e06e816fSKevin Lo 			if (error != 0)
1006e06e816fSKevin Lo 				break;
1007e06e816fSKevin Lo 			inp = sotoinpcb(so);
1008e06e816fSKevin Lo 			KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1009e06e816fSKevin Lo 			INP_WLOCK(inp);
1010e06e816fSKevin Lo 			up = intoudpcb(inp);
1011e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
1012e06e816fSKevin Lo 			if (optval != 0 && optval < 8) {
1013e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1014e06e816fSKevin Lo 				error = EINVAL;
1015e06e816fSKevin Lo 				break;
1016e06e816fSKevin Lo 			}
1017e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1018e06e816fSKevin Lo 				up->u_txcslen = optval;
1019e06e816fSKevin Lo 			else
1020e06e816fSKevin Lo 				up->u_rxcslen = optval;
1021e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1022e06e816fSKevin Lo 			break;
10237b495c44SVANHULLEBUS Yvan 		default:
10247b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10257b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
10267b495c44SVANHULLEBUS Yvan 			break;
10277b495c44SVANHULLEBUS Yvan 		}
10287b495c44SVANHULLEBUS Yvan 		break;
10297b495c44SVANHULLEBUS Yvan 	case SOPT_GET:
10307b495c44SVANHULLEBUS Yvan 		switch (sopt->sopt_name) {
10317b495c44SVANHULLEBUS Yvan #ifdef IPSEC_NAT_T
10327b495c44SVANHULLEBUS Yvan 		case UDP_ENCAP:
10337b495c44SVANHULLEBUS Yvan 			up = intoudpcb(inp);
10347b495c44SVANHULLEBUS Yvan 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
10357b495c44SVANHULLEBUS Yvan 			optval = up->u_flags & UF_ESPINUDP_ALL;
10367b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10377b495c44SVANHULLEBUS Yvan 			error = sooptcopyout(sopt, &optval, sizeof optval);
10387b495c44SVANHULLEBUS Yvan 			break;
10397b495c44SVANHULLEBUS Yvan #endif
1040e06e816fSKevin Lo 		case UDPLITE_SEND_CSCOV:
1041e06e816fSKevin Lo 		case UDPLITE_RECV_CSCOV:
1042e06e816fSKevin Lo 			if (!isudplite) {
1043e06e816fSKevin Lo 				INP_WUNLOCK(inp);
1044e06e816fSKevin Lo 				error = ENOPROTOOPT;
1045e06e816fSKevin Lo 				break;
1046e06e816fSKevin Lo 			}
1047e06e816fSKevin Lo 			up = intoudpcb(inp);
1048e06e816fSKevin Lo 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
1049e06e816fSKevin Lo 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1050e06e816fSKevin Lo 				optval = up->u_txcslen;
1051e06e816fSKevin Lo 			else
1052e06e816fSKevin Lo 				optval = up->u_rxcslen;
1053e06e816fSKevin Lo 			INP_WUNLOCK(inp);
1054e06e816fSKevin Lo 			error = sooptcopyout(sopt, &optval, sizeof(optval));
1055e06e816fSKevin Lo 			break;
10567b495c44SVANHULLEBUS Yvan 		default:
10577b495c44SVANHULLEBUS Yvan 			INP_WUNLOCK(inp);
10587b495c44SVANHULLEBUS Yvan 			error = ENOPROTOOPT;
10597b495c44SVANHULLEBUS Yvan 			break;
10607b495c44SVANHULLEBUS Yvan 		}
10617b495c44SVANHULLEBUS Yvan 		break;
10627b495c44SVANHULLEBUS Yvan 	}
10637b495c44SVANHULLEBUS Yvan 	return (error);
10647b495c44SVANHULLEBUS Yvan }
10657b495c44SVANHULLEBUS Yvan 
106679288c11SBjoern A. Zeeb #ifdef INET
1067fa046d87SRobert Watson #define	UH_WLOCKED	2
1068fa046d87SRobert Watson #define	UH_RLOCKED	1
1069fa046d87SRobert Watson #define	UH_UNLOCKED	0
1070490d50b6SBrian Feldman static int
10713329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
10723329b236SRobert Watson     struct mbuf *control, struct thread *td)
1073df8bae1dSRodney W. Grimes {
10743329b236SRobert Watson 	struct udpiphdr *ui;
10753329b236SRobert Watson 	int len = m->m_pkthdr.len;
107690162a4eSIan Dowse 	struct in_addr faddr, laddr;
1077c557ae16SIan Dowse 	struct cmsghdr *cm;
1078e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1079c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
1080e06e816fSKevin Lo 	int cscov_partial = 0;
108190162a4eSIan Dowse 	int error = 0;
10828afa2304SBruce M Simpson 	int ipflags;
108390162a4eSIan Dowse 	u_short fport, lport;
10845c32ea65SRobert Watson 	int unlock_udbinfo;
1085f584d74bSMichael Tuexen 	u_char tos;
1086e06e816fSKevin Lo 	uint8_t pr;
1087e06e816fSKevin Lo 	uint16_t cscov = 0;
1088*9d3ddf43SAdrian Chadd 	uint32_t flowid = 0;
1089*9d3ddf43SAdrian Chadd 	int flowid_type = 0;
1090*9d3ddf43SAdrian Chadd 	int use_flowid = 0;
1091df8bae1dSRodney W. Grimes 
10925c32ea65SRobert Watson 	/*
10935c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
1094f5514f08SRobert Watson 	 * inpcb.  As such, we don't know up front whether we will need the
1095f5514f08SRobert Watson 	 * pcbinfo lock or not.  Do any work to decide what is needed up
1096f5514f08SRobert Watson 	 * front before acquiring any locks.
10975c32ea65SRobert Watson 	 */
1098430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1099c557ae16SIan Dowse 		if (control)
1100c557ae16SIan Dowse 			m_freem(control);
11015c32ea65SRobert Watson 		m_freem(m);
11023329b236SRobert Watson 		return (EMSGSIZE);
1103430d30d8SBill Fenner 	}
1104430d30d8SBill Fenner 
11051b7f0384SBruce M Simpson 	src.sin_family = 0;
11060cfdff24SBjoern A. Zeeb 	INP_RLOCK(inp);
1107f584d74bSMichael Tuexen 	tos = inp->inp_ip_tos;
1108c557ae16SIan Dowse 	if (control != NULL) {
1109c557ae16SIan Dowse 		/*
11103329b236SRobert Watson 		 * XXX: Currently, we assume all the optional information is
11113329b236SRobert Watson 		 * stored in a single mbuf.
1112c557ae16SIan Dowse 		 */
1113c557ae16SIan Dowse 		if (control->m_next) {
11140cfdff24SBjoern A. Zeeb 			INP_RUNLOCK(inp);
1115c557ae16SIan Dowse 			m_freem(control);
11165c32ea65SRobert Watson 			m_freem(m);
11173329b236SRobert Watson 			return (EINVAL);
1118c557ae16SIan Dowse 		}
1119c557ae16SIan Dowse 		for (; control->m_len > 0;
1120c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
1121c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1122c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
1123af1ee11dSRobert Watson 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
1124af1ee11dSRobert Watson 			    || cm->cmsg_len > control->m_len) {
1125c557ae16SIan Dowse 				error = EINVAL;
1126c557ae16SIan Dowse 				break;
1127c557ae16SIan Dowse 			}
1128c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
1129c557ae16SIan Dowse 				continue;
1130c557ae16SIan Dowse 
1131c557ae16SIan Dowse 			switch (cm->cmsg_type) {
1132c557ae16SIan Dowse 			case IP_SENDSRCADDR:
1133c557ae16SIan Dowse 				if (cm->cmsg_len !=
1134c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
1135c557ae16SIan Dowse 					error = EINVAL;
1136c557ae16SIan Dowse 					break;
1137c557ae16SIan Dowse 				}
1138c557ae16SIan Dowse 				bzero(&src, sizeof(src));
1139c557ae16SIan Dowse 				src.sin_family = AF_INET;
1140c557ae16SIan Dowse 				src.sin_len = sizeof(src);
1141c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
1142af1ee11dSRobert Watson 				src.sin_addr =
1143af1ee11dSRobert Watson 				    *(struct in_addr *)CMSG_DATA(cm);
1144c557ae16SIan Dowse 				break;
1145af1ee11dSRobert Watson 
1146f584d74bSMichael Tuexen 			case IP_TOS:
1147f584d74bSMichael Tuexen 				if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
1148f584d74bSMichael Tuexen 					error = EINVAL;
1149f584d74bSMichael Tuexen 					break;
1150f584d74bSMichael Tuexen 				}
1151f584d74bSMichael Tuexen 				tos = *(u_char *)CMSG_DATA(cm);
1152f584d74bSMichael Tuexen 				break;
1153f584d74bSMichael Tuexen 
1154*9d3ddf43SAdrian Chadd 			case IP_FLOWID:
1155*9d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1156*9d3ddf43SAdrian Chadd 					error = EINVAL;
1157*9d3ddf43SAdrian Chadd 					break;
1158*9d3ddf43SAdrian Chadd 				}
1159*9d3ddf43SAdrian Chadd 				flowid = *(uint32_t *) CMSG_DATA(cm);
1160*9d3ddf43SAdrian Chadd 				break;
1161*9d3ddf43SAdrian Chadd 
1162*9d3ddf43SAdrian Chadd 			case IP_FLOWTYPE:
1163*9d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1164*9d3ddf43SAdrian Chadd 					error = EINVAL;
1165*9d3ddf43SAdrian Chadd 					break;
1166*9d3ddf43SAdrian Chadd 				}
1167*9d3ddf43SAdrian Chadd 				flowid_type = *(uint32_t *) CMSG_DATA(cm);
1168*9d3ddf43SAdrian Chadd 				use_flowid = 1;
1169*9d3ddf43SAdrian Chadd 				break;
1170*9d3ddf43SAdrian Chadd 
1171*9d3ddf43SAdrian Chadd #ifdef	RSS
1172*9d3ddf43SAdrian Chadd 			case IP_RSSBUCKETID:
1173*9d3ddf43SAdrian Chadd 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1174*9d3ddf43SAdrian Chadd 					error = EINVAL;
1175*9d3ddf43SAdrian Chadd 					break;
1176*9d3ddf43SAdrian Chadd 				}
1177*9d3ddf43SAdrian Chadd 				/* This is just a placeholder for now */
1178*9d3ddf43SAdrian Chadd 				break;
1179*9d3ddf43SAdrian Chadd #endif	/* RSS */
1180c557ae16SIan Dowse 			default:
1181c557ae16SIan Dowse 				error = ENOPROTOOPT;
1182c557ae16SIan Dowse 				break;
1183c557ae16SIan Dowse 			}
1184c557ae16SIan Dowse 			if (error)
1185c557ae16SIan Dowse 				break;
1186c557ae16SIan Dowse 		}
1187c557ae16SIan Dowse 		m_freem(control);
1188c557ae16SIan Dowse 	}
11895c32ea65SRobert Watson 	if (error) {
11900cfdff24SBjoern A. Zeeb 		INP_RUNLOCK(inp);
11915c32ea65SRobert Watson 		m_freem(m);
11923329b236SRobert Watson 		return (error);
11935c32ea65SRobert Watson 	}
11945c32ea65SRobert Watson 
119543cc0bc1SRobert Watson 	/*
119643cc0bc1SRobert Watson 	 * Depending on whether or not the application has bound or connected
1197ca528788SRobert Watson 	 * the socket, we may have to do varying levels of work.  The optimal
1198ca528788SRobert Watson 	 * case is for a connected UDP socket, as a global lock isn't
1199ca528788SRobert Watson 	 * required at all.
120043cc0bc1SRobert Watson 	 *
120143cc0bc1SRobert Watson 	 * In order to decide which we need, we require stability of the
120243cc0bc1SRobert Watson 	 * inpcb binding, which we ensure by acquiring a read lock on the
120343cc0bc1SRobert Watson 	 * inpcb.  This doesn't strictly follow the lock order, so we play
120443cc0bc1SRobert Watson 	 * the trylock and retry game; note that we may end up with more
120543cc0bc1SRobert Watson 	 * conservative locks than required the second time around, so later
120643cc0bc1SRobert Watson 	 * assertions have to accept that.  Further analysis of the number of
120743cc0bc1SRobert Watson 	 * misses under contention is required.
1208fa046d87SRobert Watson 	 *
1209fa046d87SRobert Watson 	 * XXXRW: Check that hash locking update here is correct.
121043cc0bc1SRobert Watson 	 */
1211e06e816fSKevin Lo 	pr = inp->inp_socket->so_proto->pr_protocol;
1212e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(pr);
121343cc0bc1SRobert Watson 	sin = (struct sockaddr_in *)addr;
121443cc0bc1SRobert Watson 	if (sin != NULL &&
121543cc0bc1SRobert Watson 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
121643cc0bc1SRobert Watson 		INP_RUNLOCK(inp);
12178501a69cSRobert Watson 		INP_WLOCK(inp);
1218e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1219fa046d87SRobert Watson 		unlock_udbinfo = UH_WLOCKED;
122043cc0bc1SRobert Watson 	} else if ((sin != NULL && (
122143cc0bc1SRobert Watson 	    (sin->sin_addr.s_addr == INADDR_ANY) ||
122243cc0bc1SRobert Watson 	    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
122343cc0bc1SRobert Watson 	    (inp->inp_laddr.s_addr == INADDR_ANY) ||
122443cc0bc1SRobert Watson 	    (inp->inp_lport == 0))) ||
122543cc0bc1SRobert Watson 	    (src.sin_family == AF_INET)) {
1226e06e816fSKevin Lo 		INP_HASH_RLOCK(pcbinfo);
1227fa046d87SRobert Watson 		unlock_udbinfo = UH_RLOCKED;
122843cc0bc1SRobert Watson 	} else
1229fa046d87SRobert Watson 		unlock_udbinfo = UH_UNLOCKED;
12305c32ea65SRobert Watson 
12311b7f0384SBruce M Simpson 	/*
12321b7f0384SBruce M Simpson 	 * If the IP_SENDSRCADDR control message was specified, override the
12331b7f0384SBruce M Simpson 	 * source address for this datagram.  Its use is invalidated if the
12341b7f0384SBruce M Simpson 	 * address thus specified is incomplete or clobbers other inpcbs.
12351b7f0384SBruce M Simpson 	 */
123690162a4eSIan Dowse 	laddr = inp->inp_laddr;
123790162a4eSIan Dowse 	lport = inp->inp_lport;
12381b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET) {
1239e06e816fSKevin Lo 		INP_HASH_LOCK_ASSERT(pcbinfo);
12401b7f0384SBruce M Simpson 		if ((lport == 0) ||
12411b7f0384SBruce M Simpson 		    (laddr.s_addr == INADDR_ANY &&
12421b7f0384SBruce M Simpson 		     src.sin_addr.s_addr == INADDR_ANY)) {
1243c557ae16SIan Dowse 			error = EINVAL;
1244c557ae16SIan Dowse 			goto release;
1245c557ae16SIan Dowse 		}
1246c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
1247b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
1248c557ae16SIan Dowse 		if (error)
1249c557ae16SIan Dowse 			goto release;
1250c557ae16SIan Dowse 	}
1251c557ae16SIan Dowse 
12523144b7d3SRobert Watson 	/*
12533144b7d3SRobert Watson 	 * If a UDP socket has been connected, then a local address/port will
12543144b7d3SRobert Watson 	 * have been selected and bound.
12553144b7d3SRobert Watson 	 *
125643cc0bc1SRobert Watson 	 * If a UDP socket has not been connected to, then an explicit
12573144b7d3SRobert Watson 	 * destination address must be used, in which case a local
12583144b7d3SRobert Watson 	 * address/port may not have been selected and bound.
12593144b7d3SRobert Watson 	 */
126043cc0bc1SRobert Watson 	if (sin != NULL) {
1261c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
1262df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1263df8bae1dSRodney W. Grimes 			error = EISCONN;
1264df8bae1dSRodney W. Grimes 			goto release;
1265df8bae1dSRodney W. Grimes 		}
12663144b7d3SRobert Watson 
12673144b7d3SRobert Watson 		/*
12683144b7d3SRobert Watson 		 * Jail may rewrite the destination address, so let it do
12693144b7d3SRobert Watson 		 * that before we use it.
12703144b7d3SRobert Watson 		 */
1271b89e82ddSJamie Gritton 		error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1272b89e82ddSJamie Gritton 		if (error)
1273413628a7SBjoern A. Zeeb 			goto release;
12743144b7d3SRobert Watson 
12753144b7d3SRobert Watson 		/*
127643cc0bc1SRobert Watson 		 * If a local address or port hasn't yet been selected, or if
127743cc0bc1SRobert Watson 		 * the destination address needs to be rewritten due to using
127843cc0bc1SRobert Watson 		 * a special INADDR_ constant, invoke in_pcbconnect_setup()
127943cc0bc1SRobert Watson 		 * to do the heavy lifting.  Once a port is selected, we
128043cc0bc1SRobert Watson 		 * commit the binding back to the socket; we also commit the
128143cc0bc1SRobert Watson 		 * binding of the address if in jail.
128243cc0bc1SRobert Watson 		 *
128343cc0bc1SRobert Watson 		 * If we already have a valid binding and we're not
128443cc0bc1SRobert Watson 		 * requesting a destination address rewrite, use a fast path.
12853144b7d3SRobert Watson 		 */
128643cc0bc1SRobert Watson 		if (inp->inp_laddr.s_addr == INADDR_ANY ||
128743cc0bc1SRobert Watson 		    inp->inp_lport == 0 ||
128843cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_ANY ||
128943cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_BROADCAST) {
1290e06e816fSKevin Lo 			INP_HASH_LOCK_ASSERT(pcbinfo);
129143cc0bc1SRobert Watson 			error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
129243cc0bc1SRobert Watson 			    &lport, &faddr.s_addr, &fport, NULL,
129343cc0bc1SRobert Watson 			    td->td_ucred);
129490162a4eSIan Dowse 			if (error)
129590162a4eSIan Dowse 				goto release;
129690162a4eSIan Dowse 
129743cc0bc1SRobert Watson 			/*
129843cc0bc1SRobert Watson 			 * XXXRW: Why not commit the port if the address is
129943cc0bc1SRobert Watson 			 * !INADDR_ANY?
130043cc0bc1SRobert Watson 			 */
130190162a4eSIan Dowse 			/* Commit the local port if newly assigned. */
130290162a4eSIan Dowse 			if (inp->inp_laddr.s_addr == INADDR_ANY &&
130390162a4eSIan Dowse 			    inp->inp_lport == 0) {
1304c4d585aeSRobert Watson 				INP_WLOCK_ASSERT(inp);
1305e06e816fSKevin Lo 				INP_HASH_WLOCK_ASSERT(pcbinfo);
13063a1757b9SGleb Smirnoff 				/*
130743cc0bc1SRobert Watson 				 * Remember addr if jailed, to prevent
130843cc0bc1SRobert Watson 				 * rebinding.
13093a1757b9SGleb Smirnoff 				 */
13100304c731SJamie Gritton 				if (prison_flag(td->td_ucred, PR_IP4))
13113a1757b9SGleb Smirnoff 					inp->inp_laddr = laddr;
131290162a4eSIan Dowse 				inp->inp_lport = lport;
131390162a4eSIan Dowse 				if (in_pcbinshash(inp) != 0) {
131490162a4eSIan Dowse 					inp->inp_lport = 0;
131590162a4eSIan Dowse 					error = EAGAIN;
1316df8bae1dSRodney W. Grimes 					goto release;
1317df8bae1dSRodney W. Grimes 				}
131890162a4eSIan Dowse 				inp->inp_flags |= INP_ANONPORT;
131990162a4eSIan Dowse 			}
1320df8bae1dSRodney W. Grimes 		} else {
132143cc0bc1SRobert Watson 			faddr = sin->sin_addr;
132243cc0bc1SRobert Watson 			fport = sin->sin_port;
132343cc0bc1SRobert Watson 		}
132443cc0bc1SRobert Watson 	} else {
1325c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
132690162a4eSIan Dowse 		faddr = inp->inp_faddr;
132790162a4eSIan Dowse 		fport = inp->inp_fport;
132890162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
1329df8bae1dSRodney W. Grimes 			error = ENOTCONN;
1330df8bae1dSRodney W. Grimes 			goto release;
1331df8bae1dSRodney W. Grimes 		}
1332df8bae1dSRodney W. Grimes 	}
1333e6ccd709SRobert Watson 
1334df8bae1dSRodney W. Grimes 	/*
1335e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
1336392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
1337392e8407SRobert Watson 	 * since we won't use that space at this layer.
1338df8bae1dSRodney W. Grimes 	 */
1339eb1b1807SGleb Smirnoff 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1340e6ccd709SRobert Watson 	if (m == NULL) {
1341df8bae1dSRodney W. Grimes 		error = ENOBUFS;
134249b19bfcSBruce M Simpson 		goto release;
1343df8bae1dSRodney W. Grimes 	}
1344e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
1345e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
1346392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
1347df8bae1dSRodney W. Grimes 
1348df8bae1dSRodney W. Grimes 	/*
13493329b236SRobert Watson 	 * Fill in mbuf with extended UDP header and addresses and length put
13503329b236SRobert Watson 	 * into network format.
1351df8bae1dSRodney W. Grimes 	 */
1352df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
1353db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
1354e06e816fSKevin Lo 	ui->ui_pr = pr;
135590162a4eSIan Dowse 	ui->ui_src = laddr;
135690162a4eSIan Dowse 	ui->ui_dst = faddr;
135790162a4eSIan Dowse 	ui->ui_sport = lport;
135890162a4eSIan Dowse 	ui->ui_dport = fport;
1359db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1360e06e816fSKevin Lo 	if (pr == IPPROTO_UDPLITE) {
1361e06e816fSKevin Lo 		struct udpcb *up;
1362e06e816fSKevin Lo 		uint16_t plen;
1363e06e816fSKevin Lo 
1364e06e816fSKevin Lo 		up = intoudpcb(inp);
1365e06e816fSKevin Lo 		cscov = up->u_txcslen;
1366e06e816fSKevin Lo 		plen = (u_short)len + sizeof(struct udphdr);
1367e06e816fSKevin Lo 		if (cscov >= plen)
1368e06e816fSKevin Lo 			cscov = 0;
1369e06e816fSKevin Lo 		ui->ui_len = htons(plen);
1370e06e816fSKevin Lo 		ui->ui_ulen = htons(cscov);
1371e06e816fSKevin Lo 		/*
1372e06e816fSKevin Lo 		 * For UDP-Lite, checksum coverage length of zero means
1373e06e816fSKevin Lo 		 * the entire UDPLite packet is covered by the checksum.
1374e06e816fSKevin Lo 		 */
1375e06e816fSKevin Lo 		cscov_partial = (cscov == 0) ? 0 : 1;
1376e06e816fSKevin Lo 	} else
1377e06e816fSKevin Lo 		ui->ui_v = IPVERSION << 4;
1378df8bae1dSRodney W. Grimes 
1379b2828ad2SAndre Oppermann 	/*
1380b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
1381b2828ad2SAndre Oppermann 	 */
1382b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
1383b2828ad2SAndre Oppermann 		struct ip *ip;
13843329b236SRobert Watson 
1385b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
13868f134647SGleb Smirnoff 		ip->ip_off |= htons(IP_DF);
1387b2828ad2SAndre Oppermann 	}
1388b2828ad2SAndre Oppermann 
1389b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
1390b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
1391b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
1392b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
1393b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
13946fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
13958afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
13968afa2304SBruce M Simpson 
13971175d9d5SRobert Watson #ifdef MAC
13981175d9d5SRobert Watson 	mac_inpcb_create_mbuf(inp, m);
13991175d9d5SRobert Watson #endif
14001175d9d5SRobert Watson 
1401df8bae1dSRodney W. Grimes 	/*
1402db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
1403df8bae1dSRodney W. Grimes 	 */
1404e06e816fSKevin Lo 	ui->ui_sum = 0;
1405a485f139SMichael Tuexen 	if (pr == IPPROTO_UDPLITE) {
1406e06e816fSKevin Lo 		if (inp->inp_flags & INP_ONESBCAST)
1407e06e816fSKevin Lo 			faddr.s_addr = INADDR_BROADCAST;
1408a485f139SMichael Tuexen 		if (cscov_partial) {
1409e06e816fSKevin Lo 			if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1410e06e816fSKevin Lo 				ui->ui_sum = 0xffff;
1411a485f139SMichael Tuexen 		} else {
1412a485f139SMichael Tuexen 			if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1413a485f139SMichael Tuexen 				ui->ui_sum = 0xffff;
1414a485f139SMichael Tuexen 		}
1415a485f139SMichael Tuexen 	} else if (V_udp_cksum) {
14166fbfd582SAndre Oppermann 		if (inp->inp_flags & INP_ONESBCAST)
14178a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
14188a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1419e06e816fSKevin Lo 		    htons((u_short)len + sizeof(struct udphdr) + pr));
1420db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
1421db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1422e06e816fSKevin Lo 	}
14238f134647SGleb Smirnoff 	((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1424ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
1425f584d74bSMichael Tuexen 	((struct ip *)ui)->ip_tos = tos;		/* XXX */
1426026decb8SRobert Watson 	UDPSTAT_INC(udps_opackets);
1427cfa1ca9dSYoshinobu Inoue 
1428*9d3ddf43SAdrian Chadd 	/*
1429*9d3ddf43SAdrian Chadd 	 * Setup flowid / RSS information for outbound socket.
1430*9d3ddf43SAdrian Chadd 	 *
1431*9d3ddf43SAdrian Chadd 	 * Once the UDP code decides to set a flowid some other way,
1432*9d3ddf43SAdrian Chadd 	 * this allows the flowid to be overridden by userland.
1433*9d3ddf43SAdrian Chadd 	 */
1434*9d3ddf43SAdrian Chadd 	if (use_flowid) {
1435*9d3ddf43SAdrian Chadd 		m->m_flags |= M_FLOWID;
1436*9d3ddf43SAdrian Chadd 		m->m_pkthdr.flowid = flowid;
1437*9d3ddf43SAdrian Chadd 		M_HASHTYPE_SET(m, flowid_type);
1438*9d3ddf43SAdrian Chadd 	}
1439*9d3ddf43SAdrian Chadd 
1440*9d3ddf43SAdrian Chadd #ifdef	RSS
1441*9d3ddf43SAdrian Chadd 	ipflags |= IP_NODEFAULTFLOWID;
1442*9d3ddf43SAdrian Chadd #endif	/* RSS */
1443*9d3ddf43SAdrian Chadd 
1444fa046d87SRobert Watson 	if (unlock_udbinfo == UH_WLOCKED)
1445e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1446fa046d87SRobert Watson 	else if (unlock_udbinfo == UH_RLOCKED)
1447e06e816fSKevin Lo 		INP_HASH_RUNLOCK(pcbinfo);
144857f60867SMark Johnston 	UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
144997d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
14505d846453SSam Leffler 	    inp->inp_moptions, inp);
1451fa046d87SRobert Watson 	if (unlock_udbinfo == UH_WLOCKED)
14528501a69cSRobert Watson 		INP_WUNLOCK(inp);
1453948d0fc9SRobert Watson 	else
1454948d0fc9SRobert Watson 		INP_RUNLOCK(inp);
1455df8bae1dSRodney W. Grimes 	return (error);
1456df8bae1dSRodney W. Grimes 
1457df8bae1dSRodney W. Grimes release:
1458fa046d87SRobert Watson 	if (unlock_udbinfo == UH_WLOCKED) {
1459e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1460948d0fc9SRobert Watson 		INP_WUNLOCK(inp);
1461fa046d87SRobert Watson 	} else if (unlock_udbinfo == UH_RLOCKED) {
1462e06e816fSKevin Lo 		INP_HASH_RUNLOCK(pcbinfo);
146343cc0bc1SRobert Watson 		INP_RUNLOCK(inp);
1464948d0fc9SRobert Watson 	} else
1465948d0fc9SRobert Watson 		INP_RUNLOCK(inp);
1466df8bae1dSRodney W. Grimes 	m_freem(m);
1467df8bae1dSRodney W. Grimes 	return (error);
1468df8bae1dSRodney W. Grimes }
1469df8bae1dSRodney W. Grimes 
14707b495c44SVANHULLEBUS Yvan 
14717b495c44SVANHULLEBUS Yvan #if defined(IPSEC) && defined(IPSEC_NAT_T)
14727b495c44SVANHULLEBUS Yvan /*
14737b495c44SVANHULLEBUS Yvan  * Potentially decap ESP in UDP frame.  Check for an ESP header
14747b495c44SVANHULLEBUS Yvan  * and optional marker; if present, strip the UDP header and
14757b495c44SVANHULLEBUS Yvan  * push the result through IPSec.
14767b495c44SVANHULLEBUS Yvan  *
14777b495c44SVANHULLEBUS Yvan  * Returns mbuf to be processed (potentially re-allocated) or
14787b495c44SVANHULLEBUS Yvan  * NULL if consumed and/or processed.
14797b495c44SVANHULLEBUS Yvan  */
14807b495c44SVANHULLEBUS Yvan static struct mbuf *
14817b495c44SVANHULLEBUS Yvan udp4_espdecap(struct inpcb *inp, struct mbuf *m, int off)
14827b495c44SVANHULLEBUS Yvan {
14837b495c44SVANHULLEBUS Yvan 	size_t minlen, payload, skip, iphlen;
14847b495c44SVANHULLEBUS Yvan 	caddr_t data;
14857b495c44SVANHULLEBUS Yvan 	struct udpcb *up;
14867b495c44SVANHULLEBUS Yvan 	struct m_tag *tag;
14877b495c44SVANHULLEBUS Yvan 	struct udphdr *udphdr;
14887b495c44SVANHULLEBUS Yvan 	struct ip *ip;
14897b495c44SVANHULLEBUS Yvan 
14907b495c44SVANHULLEBUS Yvan 	INP_RLOCK_ASSERT(inp);
14917b495c44SVANHULLEBUS Yvan 
14927b495c44SVANHULLEBUS Yvan 	/*
14937b495c44SVANHULLEBUS Yvan 	 * Pull up data so the longest case is contiguous:
14947b495c44SVANHULLEBUS Yvan 	 *    IP/UDP hdr + non ESP marker + ESP hdr.
14957b495c44SVANHULLEBUS Yvan 	 */
14967b495c44SVANHULLEBUS Yvan 	minlen = off + sizeof(uint64_t) + sizeof(struct esp);
14977b495c44SVANHULLEBUS Yvan 	if (minlen > m->m_pkthdr.len)
14987b495c44SVANHULLEBUS Yvan 		minlen = m->m_pkthdr.len;
14997b495c44SVANHULLEBUS Yvan 	if ((m = m_pullup(m, minlen)) == NULL) {
15006794f460SAndrey V. Elsukov 		IPSECSTAT_INC(ips_in_inval);
15017b495c44SVANHULLEBUS Yvan 		return (NULL);		/* Bypass caller processing. */
15027b495c44SVANHULLEBUS Yvan 	}
15037b495c44SVANHULLEBUS Yvan 	data = mtod(m, caddr_t);	/* Points to ip header. */
15047b495c44SVANHULLEBUS Yvan 	payload = m->m_len - off;	/* Size of payload. */
15057b495c44SVANHULLEBUS Yvan 
15067b495c44SVANHULLEBUS Yvan 	if (payload == 1 && data[off] == '\xff')
15077b495c44SVANHULLEBUS Yvan 		return (m);		/* NB: keepalive packet, no decap. */
15087b495c44SVANHULLEBUS Yvan 
15097b495c44SVANHULLEBUS Yvan 	up = intoudpcb(inp);
15107b495c44SVANHULLEBUS Yvan 	KASSERT(up != NULL, ("%s: udpcb NULL", __func__));
15117b495c44SVANHULLEBUS Yvan 	KASSERT((up->u_flags & UF_ESPINUDP_ALL) != 0,
15127b495c44SVANHULLEBUS Yvan 	    ("u_flags 0x%x", up->u_flags));
15137b495c44SVANHULLEBUS Yvan 
15147b495c44SVANHULLEBUS Yvan 	/*
15157b495c44SVANHULLEBUS Yvan 	 * Check that the payload is large enough to hold an
15167b495c44SVANHULLEBUS Yvan 	 * ESP header and compute the amount of data to remove.
15177b495c44SVANHULLEBUS Yvan 	 *
15187b495c44SVANHULLEBUS Yvan 	 * NB: the caller has already done a pullup for us.
15197b495c44SVANHULLEBUS Yvan 	 * XXX can we assume alignment and eliminate bcopys?
15207b495c44SVANHULLEBUS Yvan 	 */
15217b495c44SVANHULLEBUS Yvan 	if (up->u_flags & UF_ESPINUDP_NON_IKE) {
15227b495c44SVANHULLEBUS Yvan 		/*
15237b495c44SVANHULLEBUS Yvan 		 * draft-ietf-ipsec-nat-t-ike-0[01].txt and
15247b495c44SVANHULLEBUS Yvan 		 * draft-ietf-ipsec-udp-encaps-(00/)01.txt, ignoring
15257b495c44SVANHULLEBUS Yvan 		 * possible AH mode non-IKE marker+non-ESP marker
15267b495c44SVANHULLEBUS Yvan 		 * from draft-ietf-ipsec-udp-encaps-00.txt.
15277b495c44SVANHULLEBUS Yvan 		 */
15287b495c44SVANHULLEBUS Yvan 		uint64_t marker;
15297b495c44SVANHULLEBUS Yvan 
15307b495c44SVANHULLEBUS Yvan 		if (payload <= sizeof(uint64_t) + sizeof(struct esp))
15317b495c44SVANHULLEBUS Yvan 			return (m);	/* NB: no decap. */
15327b495c44SVANHULLEBUS Yvan 		bcopy(data + off, &marker, sizeof(uint64_t));
15337b495c44SVANHULLEBUS Yvan 		if (marker != 0)	/* Non-IKE marker. */
15347b495c44SVANHULLEBUS Yvan 			return (m);	/* NB: no decap. */
15357b495c44SVANHULLEBUS Yvan 		skip = sizeof(uint64_t) + sizeof(struct udphdr);
15367b495c44SVANHULLEBUS Yvan 	} else {
15377b495c44SVANHULLEBUS Yvan 		uint32_t spi;
15387b495c44SVANHULLEBUS Yvan 
15397b495c44SVANHULLEBUS Yvan 		if (payload <= sizeof(struct esp)) {
15406794f460SAndrey V. Elsukov 			IPSECSTAT_INC(ips_in_inval);
15417b495c44SVANHULLEBUS Yvan 			m_freem(m);
15427b495c44SVANHULLEBUS Yvan 			return (NULL);	/* Discard. */
15437b495c44SVANHULLEBUS Yvan 		}
15447b495c44SVANHULLEBUS Yvan 		bcopy(data + off, &spi, sizeof(uint32_t));
15457b495c44SVANHULLEBUS Yvan 		if (spi == 0)		/* Non-ESP marker. */
15467b495c44SVANHULLEBUS Yvan 			return (m);	/* NB: no decap. */
15477b495c44SVANHULLEBUS Yvan 		skip = sizeof(struct udphdr);
15487b495c44SVANHULLEBUS Yvan 	}
15497b495c44SVANHULLEBUS Yvan 
15507b495c44SVANHULLEBUS Yvan 	/*
15517b495c44SVANHULLEBUS Yvan 	 * Setup a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
15527b495c44SVANHULLEBUS Yvan 	 * the UDP ports. This is required if we want to select
15537b495c44SVANHULLEBUS Yvan 	 * the right SPD for multiple hosts behind same NAT.
15547b495c44SVANHULLEBUS Yvan 	 *
15557b495c44SVANHULLEBUS Yvan 	 * NB: ports are maintained in network byte order everywhere
15567b495c44SVANHULLEBUS Yvan 	 *     in the NAT-T code.
15577b495c44SVANHULLEBUS Yvan 	 */
15587b495c44SVANHULLEBUS Yvan 	tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
15597b495c44SVANHULLEBUS Yvan 		2 * sizeof(uint16_t), M_NOWAIT);
15607b495c44SVANHULLEBUS Yvan 	if (tag == NULL) {
15616794f460SAndrey V. Elsukov 		IPSECSTAT_INC(ips_in_nomem);
15627b495c44SVANHULLEBUS Yvan 		m_freem(m);
15637b495c44SVANHULLEBUS Yvan 		return (NULL);		/* Discard. */
15647b495c44SVANHULLEBUS Yvan 	}
15657b495c44SVANHULLEBUS Yvan 	iphlen = off - sizeof(struct udphdr);
15667b495c44SVANHULLEBUS Yvan 	udphdr = (struct udphdr *)(data + iphlen);
15677b495c44SVANHULLEBUS Yvan 	((uint16_t *)(tag + 1))[0] = udphdr->uh_sport;
15687b495c44SVANHULLEBUS Yvan 	((uint16_t *)(tag + 1))[1] = udphdr->uh_dport;
15697b495c44SVANHULLEBUS Yvan 	m_tag_prepend(m, tag);
15707b495c44SVANHULLEBUS Yvan 
15717b495c44SVANHULLEBUS Yvan 	/*
15727b495c44SVANHULLEBUS Yvan 	 * Remove the UDP header (and possibly the non ESP marker)
15737b495c44SVANHULLEBUS Yvan 	 * IP header length is iphlen
15747b495c44SVANHULLEBUS Yvan 	 * Before:
15757b495c44SVANHULLEBUS Yvan 	 *   <--- off --->
15767b495c44SVANHULLEBUS Yvan 	 *   +----+------+-----+
15777b495c44SVANHULLEBUS Yvan 	 *   | IP |  UDP | ESP |
15787b495c44SVANHULLEBUS Yvan 	 *   +----+------+-----+
15797b495c44SVANHULLEBUS Yvan 	 *        <-skip->
15807b495c44SVANHULLEBUS Yvan 	 * After:
15817b495c44SVANHULLEBUS Yvan 	 *          +----+-----+
15827b495c44SVANHULLEBUS Yvan 	 *          | IP | ESP |
15837b495c44SVANHULLEBUS Yvan 	 *          +----+-----+
15847b495c44SVANHULLEBUS Yvan 	 *   <-skip->
15857b495c44SVANHULLEBUS Yvan 	 */
15867b495c44SVANHULLEBUS Yvan 	ovbcopy(data, data + skip, iphlen);
15877b495c44SVANHULLEBUS Yvan 	m_adj(m, skip);
15887b495c44SVANHULLEBUS Yvan 
15897b495c44SVANHULLEBUS Yvan 	ip = mtod(m, struct ip *);
15908f134647SGleb Smirnoff 	ip->ip_len = htons(ntohs(ip->ip_len) - skip);
15917b495c44SVANHULLEBUS Yvan 	ip->ip_p = IPPROTO_ESP;
15927b495c44SVANHULLEBUS Yvan 
15937b495c44SVANHULLEBUS Yvan 	/*
15947b495c44SVANHULLEBUS Yvan 	 * We cannot yet update the cksums so clear any
15957b495c44SVANHULLEBUS Yvan 	 * h/w cksum flags as they are no longer valid.
15967b495c44SVANHULLEBUS Yvan 	 */
15977b495c44SVANHULLEBUS Yvan 	if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)
15987b495c44SVANHULLEBUS Yvan 		m->m_pkthdr.csum_flags &= ~(CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
15997b495c44SVANHULLEBUS Yvan 
16007b495c44SVANHULLEBUS Yvan 	(void) ipsec4_common_input(m, iphlen, ip->ip_p);
16017b495c44SVANHULLEBUS Yvan 	return (NULL);			/* NB: consumed, bypass processing. */
16027b495c44SVANHULLEBUS Yvan }
16037b495c44SVANHULLEBUS Yvan #endif /* defined(IPSEC) && defined(IPSEC_NAT_T) */
16047b495c44SVANHULLEBUS Yvan 
1605ac45e92fSRobert Watson static void
1606d0390e05SGarrett Wollman udp_abort(struct socket *so)
1607df8bae1dSRodney W. Grimes {
1608d0390e05SGarrett Wollman 	struct inpcb *inp;
1609e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1610df8bae1dSRodney W. Grimes 
1611e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1612d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
161314ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
16148501a69cSRobert Watson 	INP_WLOCK(inp);
1615a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1616e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1617a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1618a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1619e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1620d0390e05SGarrett Wollman 		soisdisconnected(so);
1621a152f8a3SRobert Watson 	}
16228501a69cSRobert Watson 	INP_WUNLOCK(inp);
1623df8bae1dSRodney W. Grimes }
1624df8bae1dSRodney W. Grimes 
1625d0390e05SGarrett Wollman static int
1626b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
1627d0390e05SGarrett Wollman {
1628d0390e05SGarrett Wollman 	struct inpcb *inp;
1629e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1630277afaffSRobert Watson 	int error;
1631d0390e05SGarrett Wollman 
1632e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1633d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
163414ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1635cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
1636f24618aaSRobert Watson 	if (error)
16373329b236SRobert Watson 		return (error);
1638e06e816fSKevin Lo 	INP_INFO_WLOCK(pcbinfo);
1639e06e816fSKevin Lo 	error = in_pcballoc(so, pcbinfo);
164053b57cd1SSam Leffler 	if (error) {
1641e06e816fSKevin Lo 		INP_INFO_WUNLOCK(pcbinfo);
16423329b236SRobert Watson 		return (error);
164353b57cd1SSam Leffler 	}
1644cfa1ca9dSYoshinobu Inoue 
164568b5629bSRobert Watson 	inp = sotoinpcb(so);
1646cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1647603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
16486a9148feSBjoern A. Zeeb 
16496a9148feSBjoern A. Zeeb 	error = udp_newudpcb(inp);
16506a9148feSBjoern A. Zeeb 	if (error) {
16516a9148feSBjoern A. Zeeb 		in_pcbdetach(inp);
16526a9148feSBjoern A. Zeeb 		in_pcbfree(inp);
1653e06e816fSKevin Lo 		INP_INFO_WUNLOCK(pcbinfo);
16546a9148feSBjoern A. Zeeb 		return (error);
16556a9148feSBjoern A. Zeeb 	}
16566a9148feSBjoern A. Zeeb 
1657c7c7ea4bSRandall Stewart 	INP_WUNLOCK(inp);
1658e06e816fSKevin Lo 	INP_INFO_WUNLOCK(pcbinfo);
1659c7c7ea4bSRandall Stewart 	return (0);
1660c7c7ea4bSRandall Stewart }
166179288c11SBjoern A. Zeeb #endif /* INET */
1662c7c7ea4bSRandall Stewart 
1663c7c7ea4bSRandall Stewart int
1664c7c7ea4bSRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f)
1665c7c7ea4bSRandall Stewart {
1666c7c7ea4bSRandall Stewart 	struct inpcb *inp;
16676a9148feSBjoern A. Zeeb 	struct udpcb *up;
1668c7c7ea4bSRandall Stewart 
166968b5629bSRobert Watson 	KASSERT(so->so_type == SOCK_DGRAM,
167068b5629bSRobert Watson 	    ("udp_set_kernel_tunneling: !dgram"));
167168b5629bSRobert Watson 	inp = sotoinpcb(so);
167268b5629bSRobert Watson 	KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1673c7c7ea4bSRandall Stewart 	INP_WLOCK(inp);
16746a9148feSBjoern A. Zeeb 	up = intoudpcb(inp);
16756a9148feSBjoern A. Zeeb 	if (up->u_tun_func != NULL) {
1676bbb0e3d9SRandall Stewart 		INP_WUNLOCK(inp);
1677bbb0e3d9SRandall Stewart 		return (EBUSY);
1678bbb0e3d9SRandall Stewart 	}
16796a9148feSBjoern A. Zeeb 	up->u_tun_func = f;
16808501a69cSRobert Watson 	INP_WUNLOCK(inp);
16813329b236SRobert Watson 	return (0);
1682df8bae1dSRodney W. Grimes }
1683d0390e05SGarrett Wollman 
168479288c11SBjoern A. Zeeb #ifdef INET
1685d0390e05SGarrett Wollman static int
1686b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1687d0390e05SGarrett Wollman {
1688d0390e05SGarrett Wollman 	struct inpcb *inp;
1689e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1690277afaffSRobert Watson 	int error;
1691d0390e05SGarrett Wollman 
1692e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1693d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
169414ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
16958501a69cSRobert Watson 	INP_WLOCK(inp);
1696e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1697b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
1698e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
16998501a69cSRobert Watson 	INP_WUNLOCK(inp);
17003329b236SRobert Watson 	return (error);
1701d0390e05SGarrett Wollman }
1702d0390e05SGarrett Wollman 
1703a152f8a3SRobert Watson static void
1704a152f8a3SRobert Watson udp_close(struct socket *so)
1705a152f8a3SRobert Watson {
1706a152f8a3SRobert Watson 	struct inpcb *inp;
1707e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1708a152f8a3SRobert Watson 
1709e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1710a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1711a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
17128501a69cSRobert Watson 	INP_WLOCK(inp);
1713a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1714e06e816fSKevin Lo 		INP_HASH_WLOCK(pcbinfo);
1715a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1716a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1717e06e816fSKevin Lo 		INP_HASH_WUNLOCK(pcbinfo);
1718a152f8a3SRobert Watson 		soisdisconnected(so);
1719a152f8a3SRobert Watson 	}
17208501a69cSRobert Watson 	INP_WUNLOCK(inp);
1721a152f8a3SRobert Watson }
1722a152f8a3SRobert Watson 
1723d0390e05SGarrett Wollman static int
1724b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1725d0390e05SGarrett Wollman {
1726d0390e05SGarrett Wollman 	struct inpcb *inp;
1727e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
172875c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1729e06e816fSKevin Lo 	int error;
1730d0390e05SGarrett Wollman 
1731e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1732d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
173314ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
17348501a69cSRobert Watson 	INP_WLOCK(inp);
1735f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
17368501a69cSRobert Watson 		INP_WUNLOCK(inp);
17373329b236SRobert Watson 		return (EISCONN);
1738f76fcf6dSJeffrey Hsu 	}
173975c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1740b89e82ddSJamie Gritton 	error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1741b89e82ddSJamie Gritton 	if (error != 0) {
1742413628a7SBjoern A. Zeeb 		INP_WUNLOCK(inp);
1743b89e82ddSJamie Gritton 		return (error);
1744413628a7SBjoern A. Zeeb 	}
1745e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1746b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
1747e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
17484cc20ab1SSeigo Tanimura 	if (error == 0)
1749df8bae1dSRodney W. Grimes 		soisconnected(so);
17508501a69cSRobert Watson 	INP_WUNLOCK(inp);
17513329b236SRobert Watson 	return (error);
1752df8bae1dSRodney W. Grimes }
1753d0390e05SGarrett Wollman 
1754bc725eafSRobert Watson static void
1755d0390e05SGarrett Wollman udp_detach(struct socket *so)
1756d0390e05SGarrett Wollman {
1757d0390e05SGarrett Wollman 	struct inpcb *inp;
1758e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
17596a9148feSBjoern A. Zeeb 	struct udpcb *up;
1760d0390e05SGarrett Wollman 
1761e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1762d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
176314ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1764a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1765a152f8a3SRobert Watson 	    ("udp_detach: not disconnected"));
1766e06e816fSKevin Lo 	INP_INFO_WLOCK(pcbinfo);
17678501a69cSRobert Watson 	INP_WLOCK(inp);
17686a9148feSBjoern A. Zeeb 	up = intoudpcb(inp);
17696a9148feSBjoern A. Zeeb 	KASSERT(up != NULL, ("%s: up == NULL", __func__));
17706a9148feSBjoern A. Zeeb 	inp->inp_ppcb = NULL;
1771d0390e05SGarrett Wollman 	in_pcbdetach(inp);
177214ba8addSRobert Watson 	in_pcbfree(inp);
1773e06e816fSKevin Lo 	INP_INFO_WUNLOCK(pcbinfo);
17746a9148feSBjoern A. Zeeb 	udp_discardcb(up);
1775d0390e05SGarrett Wollman }
1776d0390e05SGarrett Wollman 
1777d0390e05SGarrett Wollman static int
1778d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1779d0390e05SGarrett Wollman {
1780d0390e05SGarrett Wollman 	struct inpcb *inp;
1781e06e816fSKevin Lo 	struct inpcbinfo *pcbinfo;
1782d0390e05SGarrett Wollman 
1783e06e816fSKevin Lo 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1784d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
178514ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
17868501a69cSRobert Watson 	INP_WLOCK(inp);
1787f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
17888501a69cSRobert Watson 		INP_WUNLOCK(inp);
17893329b236SRobert Watson 		return (ENOTCONN);
1790f76fcf6dSJeffrey Hsu 	}
1791e06e816fSKevin Lo 	INP_HASH_WLOCK(pcbinfo);
1792df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1793df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1794e06e816fSKevin Lo 	INP_HASH_WUNLOCK(pcbinfo);
1795d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1796d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1797d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
17988501a69cSRobert Watson 	INP_WUNLOCK(inp);
17993329b236SRobert Watson 	return (0);
1800df8bae1dSRodney W. Grimes }
1801df8bae1dSRodney W. Grimes 
1802d0390e05SGarrett Wollman static int
180357bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1804b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1805d0390e05SGarrett Wollman {
1806d0390e05SGarrett Wollman 	struct inpcb *inp;
1807d0390e05SGarrett Wollman 
1808d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
180914ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
18103329b236SRobert Watson 	return (udp_output(inp, m, addr, control, td));
1811d0390e05SGarrett Wollman }
181279288c11SBjoern A. Zeeb #endif /* INET */
1813d0390e05SGarrett Wollman 
181476429de4SYoshinobu Inoue int
1815d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1816d0390e05SGarrett Wollman {
1817d0390e05SGarrett Wollman 	struct inpcb *inp;
1818d0390e05SGarrett Wollman 
1819d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
182014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
18218501a69cSRobert Watson 	INP_WLOCK(inp);
1822d0390e05SGarrett Wollman 	socantsendmore(so);
18238501a69cSRobert Watson 	INP_WUNLOCK(inp);
18243329b236SRobert Watson 	return (0);
1825d0390e05SGarrett Wollman }
1826d0390e05SGarrett Wollman 
182779288c11SBjoern A. Zeeb #ifdef INET
1828d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1829756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1830756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1831756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1832756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1833756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1834756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1835756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
183654d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1837756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
18385df3e839SRobert Watson 	.pru_soreceive =	soreceive_dgram,
183959b8854eSRobert Watson 	.pru_sosend =		sosend_dgram,
1840756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
184154d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1842a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1843a152f8a3SRobert Watson 	.pru_close =		udp_close,
1844d0390e05SGarrett Wollman };
184579288c11SBjoern A. Zeeb #endif /* INET */
1846