1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 46dfab5b1SGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 53329b236SRobert Watson * The Regents of the University of California. 63144b7d3SRobert Watson * Copyright (c) 2008 Robert N. M. Watson 7fa046d87SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 8e06e816fSKevin Lo * Copyright (c) 2014 Kevin Lo 93329b236SRobert Watson * All rights reserved. 10df8bae1dSRodney W. Grimes * 11fa046d87SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 12fa046d87SRobert Watson * contract to Juniper Networks, Inc. 13fa046d87SRobert Watson * 14df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 16df8bae1dSRodney W. Grimes * are met: 17df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 386dfab5b1SGarrett Wollman * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 414b421e2dSMike Silbersack #include <sys/cdefs.h> 424b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 434b421e2dSMike Silbersack 4479288c11SBjoern A. Zeeb #include "opt_inet.h" 45cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 46f5514f08SRobert Watson #include "opt_ipsec.h" 470c325f53SAlexander V. Chernikov #include "opt_route.h" 489d3ddf43SAdrian Chadd #include "opt_rss.h" 49cfa1ca9dSYoshinobu Inoue 50df8bae1dSRodney W. Grimes #include <sys/param.h> 51960ed29cSSeigo Tanimura #include <sys/domain.h> 524f590175SPaul Saab #include <sys/eventhandler.h> 53960ed29cSSeigo Tanimura #include <sys/jail.h> 54b110a8a2SGarrett Wollman #include <sys/kernel.h> 55960ed29cSSeigo Tanimura #include <sys/lock.h> 56df8bae1dSRodney W. Grimes #include <sys/malloc.h> 57df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 58acd3428bSRobert Watson #include <sys/priv.h> 59490d50b6SBrian Feldman #include <sys/proc.h> 60df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6157f60867SMark Johnston #include <sys/sdt.h> 62960ed29cSSeigo Tanimura #include <sys/signalvar.h> 63df8bae1dSRodney W. Grimes #include <sys/socket.h> 64df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 65960ed29cSSeigo Tanimura #include <sys/sx.h> 66b5e8ce9fSBruce Evans #include <sys/sysctl.h> 67816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 68f5514f08SRobert Watson #include <sys/systm.h> 698781d8e9SBruce Evans 7069c2d429SJeff Roberson #include <vm/uma.h> 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes #include <net/if.h> 7376039bc8SGleb Smirnoff #include <net/if_var.h> 74df8bae1dSRodney W. Grimes #include <net/route.h> 75983066f0SAlexander V. Chernikov #include <net/route/nhop.h> 76b2bdc62aSAdrian Chadd #include <net/rss_config.h> 77df8bae1dSRodney W. Grimes 78df8bae1dSRodney W. Grimes #include <netinet/in.h> 7957f60867SMark Johnston #include <netinet/in_kdtrace.h> 800c325f53SAlexander V. Chernikov #include <netinet/in_fib.h> 81960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 82f5514f08SRobert Watson #include <netinet/in_systm.h> 83960ed29cSSeigo Tanimura #include <netinet/in_var.h> 84df8bae1dSRodney W. Grimes #include <netinet/ip.h> 85cfa1ca9dSYoshinobu Inoue #ifdef INET6 86cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 87cfa1ca9dSYoshinobu Inoue #endif 88960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h> 89960ed29cSSeigo Tanimura #include <netinet/icmp_var.h> 90df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 91ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 92cfa1ca9dSYoshinobu Inoue #ifdef INET6 93cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 94cfa1ca9dSYoshinobu Inoue #endif 95df8bae1dSRodney W. Grimes #include <netinet/udp.h> 96df8bae1dSRodney W. Grimes #include <netinet/udp_var.h> 97e06e816fSKevin Lo #include <netinet/udplite.h> 988ad1a83bSAdrian Chadd #include <netinet/in_rss.h> 99df8bae1dSRodney W. Grimes 100fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 101b9234fafSSam Leffler 102db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 103db4f9cc7SJonathan Lemon 104aed55708SRobert Watson #include <security/mac/mac_framework.h> 105aed55708SRobert Watson 106df8bae1dSRodney W. Grimes /* 107e06e816fSKevin Lo * UDP and UDP-Lite protocols implementation. 108df8bae1dSRodney W. Grimes * Per RFC 768, August, 1980. 109e06e816fSKevin Lo * Per RFC 3828, July, 2004. 110df8bae1dSRodney W. Grimes */ 11174eb3236SWarner Losh 11274eb3236SWarner Losh /* 1133329b236SRobert Watson * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums 1143329b236SRobert Watson * removes the only data integrity mechanism for packets and malformed 115f5514f08SRobert Watson * packets that would otherwise be discarded due to bad checksums, and may 116f5514f08SRobert Watson * cause problems (especially for NFS data blocks). 11774eb3236SWarner Losh */ 11840b676beSBjoern A. Zeeb VNET_DEFINE(int, udp_cksum) = 1; 1196df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW, 12040b676beSBjoern A. Zeeb &VNET_NAME(udp_cksum), 0, "compute udp checksum"); 121df8bae1dSRodney W. Grimes 122334fc582SBjoern A. Zeeb VNET_DEFINE(int, udp_log_in_vain) = 0; 123334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW, 124334fc582SBjoern A. Zeeb &VNET_NAME(udp_log_in_vain), 0, "Log all incoming UDP packets"); 125816a3d83SPoul-Henning Kamp 12682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, udp_blackhole) = 0; 1276df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 128eddfbb76SRobert Watson &VNET_NAME(udp_blackhole), 0, 1293329b236SRobert Watson "Do not send port unreachables for refused connects"); 1303ea9a7cfSGleb Smirnoff VNET_DEFINE(bool, udp_blackhole_local) = false; 1313ea9a7cfSGleb Smirnoff SYSCTL_BOOL(_net_inet_udp, OID_AUTO, blackhole_local, CTLFLAG_VNET | 1323ea9a7cfSGleb Smirnoff CTLFLAG_RW, &VNET_NAME(udp_blackhole_local), false, 1333ea9a7cfSGleb Smirnoff "Enforce net.inet.udp.blackhole for locally originated packets"); 13416f7f31fSGeoff Rehmet 13543bbb6aaSRobert Watson u_long udp_sendspace = 9216; /* really max datagram size */ 13643bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 13743bbb6aaSRobert Watson &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 13843bbb6aaSRobert Watson 13943bbb6aaSRobert Watson u_long udp_recvspace = 40 * (1024 + 14043bbb6aaSRobert Watson #ifdef INET6 14143bbb6aaSRobert Watson sizeof(struct sockaddr_in6) 14243bbb6aaSRobert Watson #else 14343bbb6aaSRobert Watson sizeof(struct sockaddr_in) 14443bbb6aaSRobert Watson #endif 145e62b9bcaSSergey Kandaurov ); /* 40 1K datagrams */ 14643bbb6aaSRobert Watson 14743bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 14843bbb6aaSRobert Watson &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 14943bbb6aaSRobert Watson 150eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, udbinfo); 151e06e816fSKevin Lo VNET_DEFINE(struct inpcbinfo, ulitecbinfo); 1525f901c92SAndrew Turner VNET_DEFINE_STATIC(uma_zone_t, udpcb_zone); 1531e77c105SRobert Watson #define V_udpcb_zone VNET(udpcb_zone) 15415bd2b43SDavid Greenman 15515bd2b43SDavid Greenman #ifndef UDBHASHSIZE 156e2ed8f35SAlexander Motin #define UDBHASHSIZE 128 15715bd2b43SDavid Greenman #endif 15815bd2b43SDavid Greenman 1595b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat); /* from udp_var.h */ 1605b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(udpstat); 1615b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat, 1625b7cb97cSAndrey V. Elsukov udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 163f2ea20e6SGarrett Wollman 1645b7cb97cSAndrey V. Elsukov #ifdef VIMAGE 1655b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(udpstat); 1665b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */ 16779288c11SBjoern A. Zeeb #ifdef INET 168bc725eafSRobert Watson static void udp_detach(struct socket *so); 1694d77a549SAlfred Perlstein static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 170a9839c4aSBjoern A. Zeeb struct mbuf *, struct thread *, int); 17179288c11SBjoern A. Zeeb #endif 17279288c11SBjoern A. Zeeb 1734f590175SPaul Saab static void 1744f590175SPaul Saab udp_zone_change(void *tag) 1754f590175SPaul Saab { 1764f590175SPaul Saab 177603724d3SBjoern A. Zeeb uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 1786a9148feSBjoern A. Zeeb uma_zone_set_max(V_udpcb_zone, maxsockets); 1794f590175SPaul Saab } 1804f590175SPaul Saab 181d915b280SStephan Uphoff static int 182d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags) 183d915b280SStephan Uphoff { 184af1ee11dSRobert Watson struct inpcb *inp; 18508651e1fSJohn Baldwin 186af1ee11dSRobert Watson inp = mem; 187d915b280SStephan Uphoff INP_LOCK_INIT(inp, "inp", "udpinp"); 188d915b280SStephan Uphoff return (0); 189d915b280SStephan Uphoff } 190d915b280SStephan Uphoff 191e06e816fSKevin Lo static int 192e06e816fSKevin Lo udplite_inpcb_init(void *mem, int size, int flags) 193e06e816fSKevin Lo { 194e06e816fSKevin Lo struct inpcb *inp; 195e06e816fSKevin Lo 196e06e816fSKevin Lo inp = mem; 197e06e816fSKevin Lo INP_LOCK_INIT(inp, "inp", "udpliteinp"); 198e06e816fSKevin Lo return (0); 199e06e816fSKevin Lo } 200e06e816fSKevin Lo 201df8bae1dSRodney W. Grimes void 202af1ee11dSRobert Watson udp_init(void) 203df8bae1dSRodney W. Grimes { 204af1ee11dSRobert Watson 2058ad1a83bSAdrian Chadd /* 2068ad1a83bSAdrian Chadd * For now default to 2-tuple UDP hashing - until the fragment 2078ad1a83bSAdrian Chadd * reassembly code can also update the flowid. 2088ad1a83bSAdrian Chadd * 2098ad1a83bSAdrian Chadd * Once we can calculate the flowid that way and re-establish 2108ad1a83bSAdrian Chadd * a 4-tuple, flip this to 4-tuple. 2118ad1a83bSAdrian Chadd */ 212db0ac6deSCy Schubert in_pcbinfo_init(&V_udbinfo, "udp", UDBHASHSIZE, UDBHASHSIZE, 213db0ac6deSCy Schubert "udp_inpcb", udp_inpcb_init); 2146a9148feSBjoern A. Zeeb V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb), 215a8da5dd6SCraig Rodrigues NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2166a9148feSBjoern A. Zeeb uma_zone_set_max(V_udpcb_zone, maxsockets); 2176acd596eSPawel Jakub Dawidek uma_zone_set_warning(V_udpcb_zone, "kern.ipc.maxsockets limit reached"); 2184f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL, 2194f590175SPaul Saab EVENTHANDLER_PRI_ANY); 220df8bae1dSRodney W. Grimes } 221df8bae1dSRodney W. Grimes 222e06e816fSKevin Lo void 223e06e816fSKevin Lo udplite_init(void) 224e06e816fSKevin Lo { 225e06e816fSKevin Lo 226db0ac6deSCy Schubert in_pcbinfo_init(&V_ulitecbinfo, "udplite", UDBHASHSIZE, 227db0ac6deSCy Schubert UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init); 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 2643f58662dSBjoern A. Zeeb static void 2653f58662dSBjoern A. Zeeb udp_destroy(void *unused __unused) 266bc29160dSMarko Zec { 267bc29160dSMarko Zec 2689bcd427bSRobert Watson in_pcbinfo_destroy(&V_udbinfo); 269391dab1cSBjoern A. Zeeb uma_zdestroy(V_udpcb_zone); 270bc29160dSMarko Zec } 2713f58662dSBjoern A. Zeeb VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL); 272e06e816fSKevin Lo 2733f58662dSBjoern A. Zeeb static void 2743f58662dSBjoern A. Zeeb udplite_destroy(void *unused __unused) 275e06e816fSKevin Lo { 276e06e816fSKevin Lo 277e06e816fSKevin Lo in_pcbinfo_destroy(&V_ulitecbinfo); 278e06e816fSKevin Lo } 2793f58662dSBjoern A. Zeeb VNET_SYSUNINIT(udplite, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udplite_destroy, 2803f58662dSBjoern A. Zeeb NULL); 281bc29160dSMarko Zec #endif 282bc29160dSMarko Zec 28379288c11SBjoern A. Zeeb #ifdef INET 28443bbb6aaSRobert Watson /* 28543bbb6aaSRobert Watson * Subroutine of udp_input(), which appends the provided mbuf chain to the 28643bbb6aaSRobert Watson * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 28743bbb6aaSRobert Watson * contains the source address. If the socket ends up being an IPv6 socket, 28843bbb6aaSRobert Watson * udp_append() will convert to a sockaddr_in6 before passing the address 28943bbb6aaSRobert Watson * into the socket code. 290c0d1be08SRandall Stewart * 291c0d1be08SRandall Stewart * In the normal case udp_append() will return 0, indicating that you 292c0d1be08SRandall Stewart * must unlock the inp. However if a tunneling protocol is in place we increment 293c0d1be08SRandall Stewart * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we 294c0d1be08SRandall Stewart * then decrement the reference count. If the inp_rele returns 1, indicating the 295c0d1be08SRandall Stewart * inp is gone, we return that to the caller to tell them *not* to unlock 296c0d1be08SRandall Stewart * the inp. In the case of multi-cast this will cause the distribution 297c0d1be08SRandall Stewart * to stop (though most tunneling protocols known currently do *not* use 298c0d1be08SRandall Stewart * multicast). 29943bbb6aaSRobert Watson */ 300c0d1be08SRandall Stewart static int 30143bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off, 30243bbb6aaSRobert Watson struct sockaddr_in *udp_in) 30343bbb6aaSRobert Watson { 30443bbb6aaSRobert Watson struct sockaddr *append_sa; 30543bbb6aaSRobert Watson struct socket *so; 306dce33a45SErmal Luçi struct mbuf *tmpopts, *opts = NULL; 30743bbb6aaSRobert Watson #ifdef INET6 30843bbb6aaSRobert Watson struct sockaddr_in6 udp_in6; 30943bbb6aaSRobert Watson #endif 3107b495c44SVANHULLEBUS Yvan struct udpcb *up; 31143bbb6aaSRobert Watson 312fa046d87SRobert Watson INP_LOCK_ASSERT(inp); 31343bbb6aaSRobert Watson 31479bb84fbSEdward Tomasz Napierala /* 31579bb84fbSEdward Tomasz Napierala * Engage the tunneling protocol. 31679bb84fbSEdward Tomasz Napierala */ 31779bb84fbSEdward Tomasz Napierala up = intoudpcb(inp); 31879bb84fbSEdward Tomasz Napierala if (up->u_tun_func != NULL) { 319c0d1be08SRandall Stewart in_pcbref(inp); 320c0d1be08SRandall Stewart INP_RUNLOCK(inp); 321dce33a45SErmal Luçi (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&udp_in[0], 32281d3ec17SBryan Venteicher up->u_tun_ctx); 323c0d1be08SRandall Stewart INP_RLOCK(inp); 324c0d1be08SRandall Stewart return (in_pcbrele_rlocked(inp)); 32579bb84fbSEdward Tomasz Napierala } 32679bb84fbSEdward Tomasz Napierala 32779bb84fbSEdward Tomasz Napierala off += sizeof(struct udphdr); 32879bb84fbSEdward Tomasz Napierala 329fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 33043bbb6aaSRobert Watson /* Check AH/ESP integrity. */ 331fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 332fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, n, inp) != 0) { 33343bbb6aaSRobert Watson m_freem(n); 334c0d1be08SRandall Stewart return (0); 33543bbb6aaSRobert Watson } 336fcf59617SAndrey V. Elsukov if (up->u_flags & UF_ESPINUDP) {/* IPSec UDP encaps. */ 337fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 338fcf59617SAndrey V. Elsukov UDPENCAP_INPUT(n, off, AF_INET) != 0) 339fcf59617SAndrey V. Elsukov return (0); /* Consumed. */ 3407b495c44SVANHULLEBUS Yvan } 34143bbb6aaSRobert Watson #endif /* IPSEC */ 34243bbb6aaSRobert Watson #ifdef MAC 34330d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, n) != 0) { 34443bbb6aaSRobert Watson m_freem(n); 345c0d1be08SRandall Stewart return (0); 34643bbb6aaSRobert Watson } 34779288c11SBjoern A. Zeeb #endif /* MAC */ 34843bbb6aaSRobert Watson if (inp->inp_flags & INP_CONTROLOPTS || 34943bbb6aaSRobert Watson inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 35043bbb6aaSRobert Watson #ifdef INET6 3519a38ba81SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6) 35248d48eb9SBjoern A. Zeeb (void)ip6_savecontrol_v4(inp, n, &opts, NULL); 3539a38ba81SBjoern A. Zeeb else 35479288c11SBjoern A. Zeeb #endif /* INET6 */ 35543bbb6aaSRobert Watson ip_savecontrol(inp, &opts, ip, n); 35643bbb6aaSRobert Watson } 357dce33a45SErmal Luçi if ((inp->inp_vflag & INP_IPV4) && (inp->inp_flags2 & INP_ORIGDSTADDR)) { 358dce33a45SErmal Luçi tmpopts = sbcreatecontrol((caddr_t)&udp_in[1], 359dce33a45SErmal Luçi sizeof(struct sockaddr_in), IP_ORIGDSTADDR, IPPROTO_IP); 360dce33a45SErmal Luçi if (tmpopts) { 361dce33a45SErmal Luçi if (opts) { 362dce33a45SErmal Luçi tmpopts->m_next = opts; 363dce33a45SErmal Luçi opts = tmpopts; 364dce33a45SErmal Luçi } else 365dce33a45SErmal Luçi opts = tmpopts; 366dce33a45SErmal Luçi } 367dce33a45SErmal Luçi } 36843bbb6aaSRobert Watson #ifdef INET6 36943bbb6aaSRobert Watson if (inp->inp_vflag & INP_IPV6) { 37043bbb6aaSRobert Watson bzero(&udp_in6, sizeof(udp_in6)); 37143bbb6aaSRobert Watson udp_in6.sin6_len = sizeof(udp_in6); 37243bbb6aaSRobert Watson udp_in6.sin6_family = AF_INET6; 373dce33a45SErmal Luçi in6_sin_2_v4mapsin6(&udp_in[0], &udp_in6); 37443bbb6aaSRobert Watson append_sa = (struct sockaddr *)&udp_in6; 37543bbb6aaSRobert Watson } else 37679288c11SBjoern A. Zeeb #endif /* INET6 */ 377dce33a45SErmal Luçi append_sa = (struct sockaddr *)&udp_in[0]; 37843bbb6aaSRobert Watson m_adj(n, off); 37943bbb6aaSRobert Watson 38043bbb6aaSRobert Watson so = inp->inp_socket; 38143bbb6aaSRobert Watson SOCKBUF_LOCK(&so->so_rcv); 38243bbb6aaSRobert Watson if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 383a61c24ddSKonstantin Kukushkin soroverflow_locked(so); 38443bbb6aaSRobert Watson m_freem(n); 38543bbb6aaSRobert Watson if (opts) 38643bbb6aaSRobert Watson m_freem(opts); 387026decb8SRobert Watson UDPSTAT_INC(udps_fullsock); 38843bbb6aaSRobert Watson } else 38943bbb6aaSRobert Watson sorwakeup_locked(so); 390c0d1be08SRandall Stewart return (0); 39143bbb6aaSRobert Watson } 39243bbb6aaSRobert Watson 393db0ac6deSCy Schubert static bool 394db0ac6deSCy Schubert udp_multi_match(const struct inpcb *inp, void *v) 395db0ac6deSCy Schubert { 396db0ac6deSCy Schubert struct ip *ip = v; 397db0ac6deSCy Schubert struct udphdr *uh = (struct udphdr *)(ip + 1); 398db0ac6deSCy Schubert 399db0ac6deSCy Schubert if (inp->inp_lport != uh->uh_dport) 400db0ac6deSCy Schubert return (false); 401db0ac6deSCy Schubert #ifdef INET6 402db0ac6deSCy Schubert if ((inp->inp_vflag & INP_IPV4) == 0) 403db0ac6deSCy Schubert return (false); 404db0ac6deSCy Schubert #endif 405db0ac6deSCy Schubert if (inp->inp_laddr.s_addr != INADDR_ANY && 406db0ac6deSCy Schubert inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 407db0ac6deSCy Schubert return (false); 408db0ac6deSCy Schubert if (inp->inp_faddr.s_addr != INADDR_ANY && 409db0ac6deSCy Schubert inp->inp_faddr.s_addr != ip->ip_src.s_addr) 410db0ac6deSCy Schubert return (false); 411db0ac6deSCy Schubert if (inp->inp_fport != 0 && 412db0ac6deSCy Schubert inp->inp_fport != uh->uh_sport) 413db0ac6deSCy Schubert return (false); 414db0ac6deSCy Schubert 415db0ac6deSCy Schubert return (true); 416db0ac6deSCy Schubert } 417db0ac6deSCy Schubert 418db0ac6deSCy Schubert static int 419db0ac6deSCy Schubert udp_multi_input(struct mbuf *m, int proto, struct sockaddr_in *udp_in) 420db0ac6deSCy Schubert { 421db0ac6deSCy Schubert struct ip *ip = mtod(m, struct ip *); 422db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ITERATOR(udp_get_inpcbinfo(proto), 423db0ac6deSCy Schubert INPLOOKUP_RLOCKPCB, udp_multi_match, ip); 424bd1d0850SGleb Smirnoff #ifdef KDTRACE_HOOKS 425db0ac6deSCy Schubert struct udphdr *uh = (struct udphdr *)(ip + 1); 426bd1d0850SGleb Smirnoff #endif 427db0ac6deSCy Schubert struct inpcb *inp; 428db0ac6deSCy Schubert struct mbuf *n; 429db0ac6deSCy Schubert int appends = 0; 430db0ac6deSCy Schubert 431db0ac6deSCy Schubert MPASS(ip->ip_hl == sizeof(struct ip) >> 2); 432db0ac6deSCy Schubert 433db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) { 434db0ac6deSCy Schubert /* 435db0ac6deSCy Schubert * XXXRW: Because we weren't holding either the inpcb 436db0ac6deSCy Schubert * or the hash lock when we checked for a match 437db0ac6deSCy Schubert * before, we should probably recheck now that the 438db0ac6deSCy Schubert * inpcb lock is held. 439db0ac6deSCy Schubert */ 440db0ac6deSCy Schubert /* 441db0ac6deSCy Schubert * Handle socket delivery policy for any-source 442db0ac6deSCy Schubert * and source-specific multicast. [RFC3678] 443db0ac6deSCy Schubert */ 444db0ac6deSCy Schubert if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 445db0ac6deSCy Schubert struct ip_moptions *imo; 446db0ac6deSCy Schubert struct sockaddr_in group; 447db0ac6deSCy Schubert int blocked; 448db0ac6deSCy Schubert 449db0ac6deSCy Schubert imo = inp->inp_moptions; 450db0ac6deSCy Schubert if (imo == NULL) 451db0ac6deSCy Schubert continue; 452db0ac6deSCy Schubert bzero(&group, sizeof(struct sockaddr_in)); 453db0ac6deSCy Schubert group.sin_len = sizeof(struct sockaddr_in); 454db0ac6deSCy Schubert group.sin_family = AF_INET; 455db0ac6deSCy Schubert group.sin_addr = ip->ip_dst; 456db0ac6deSCy Schubert 457db0ac6deSCy Schubert blocked = imo_multi_filter(imo, m->m_pkthdr.rcvif, 458db0ac6deSCy Schubert (struct sockaddr *)&group, 459db0ac6deSCy Schubert (struct sockaddr *)&udp_in[0]); 460db0ac6deSCy Schubert if (blocked != MCAST_PASS) { 461db0ac6deSCy Schubert if (blocked == MCAST_NOTGMEMBER) 462db0ac6deSCy Schubert IPSTAT_INC(ips_notmember); 463db0ac6deSCy Schubert if (blocked == MCAST_NOTSMEMBER || 464db0ac6deSCy Schubert blocked == MCAST_MUTED) 465db0ac6deSCy Schubert UDPSTAT_INC(udps_filtermcast); 466db0ac6deSCy Schubert continue; 467db0ac6deSCy Schubert } 468db0ac6deSCy Schubert } 469db0ac6deSCy Schubert if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) { 470db0ac6deSCy Schubert if (proto == IPPROTO_UDPLITE) 471db0ac6deSCy Schubert UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh); 472db0ac6deSCy Schubert else 473db0ac6deSCy Schubert UDP_PROBE(receive, NULL, inp, ip, inp, uh); 474db0ac6deSCy Schubert if (udp_append(inp, ip, n, sizeof(struct ip), udp_in)) { 475db0ac6deSCy Schubert INP_RUNLOCK(inp); 476db0ac6deSCy Schubert break; 477db0ac6deSCy Schubert } else 478db0ac6deSCy Schubert appends++; 479db0ac6deSCy Schubert } 480db0ac6deSCy Schubert /* 481db0ac6deSCy Schubert * Don't look for additional matches if this one does 482db0ac6deSCy Schubert * not have either the SO_REUSEPORT or SO_REUSEADDR 483db0ac6deSCy Schubert * socket options set. This heuristic avoids 484db0ac6deSCy Schubert * searching through all pcbs in the common case of a 485db0ac6deSCy Schubert * non-shared port. It assumes that an application 486db0ac6deSCy Schubert * will never clear these options after setting them. 487db0ac6deSCy Schubert */ 488db0ac6deSCy Schubert if ((inp->inp_socket->so_options & 489db0ac6deSCy Schubert (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) { 490db0ac6deSCy Schubert INP_RUNLOCK(inp); 491db0ac6deSCy Schubert break; 492db0ac6deSCy Schubert } 493db0ac6deSCy Schubert } 494db0ac6deSCy Schubert m_freem(m); 495db0ac6deSCy Schubert 496db0ac6deSCy Schubert if (appends == 0) { 497db0ac6deSCy Schubert /* 498db0ac6deSCy Schubert * No matching pcb found; discard datagram. (No need 499db0ac6deSCy Schubert * to send an ICMP Port Unreachable for a broadcast 500db0ac6deSCy Schubert * or multicast datgram.) 501db0ac6deSCy Schubert */ 502db0ac6deSCy Schubert UDPSTAT_INC(udps_noport); 503db0ac6deSCy Schubert if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) 504db0ac6deSCy Schubert UDPSTAT_INC(udps_noportmcast); 505db0ac6deSCy Schubert else 506db0ac6deSCy Schubert UDPSTAT_INC(udps_noportbcast); 507db0ac6deSCy Schubert } 508db0ac6deSCy Schubert 509db0ac6deSCy Schubert return (IPPROTO_DONE); 510db0ac6deSCy Schubert } 511db0ac6deSCy Schubert 5128f5a8818SKevin Lo int 5138f5a8818SKevin Lo udp_input(struct mbuf **mp, int *offp, int proto) 514df8bae1dSRodney W. Grimes { 5153329b236SRobert Watson struct ip *ip; 5163329b236SRobert Watson struct udphdr *uh; 51771498f30SBruce M Simpson struct ifnet *ifp; 5183329b236SRobert Watson struct inpcb *inp; 5198f134647SGleb Smirnoff uint16_t len, ip_len; 520e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 521dce33a45SErmal Luçi struct sockaddr_in udp_in[2]; 5228f5a8818SKevin Lo struct mbuf *m; 5230b4ae859SGleb Smirnoff struct m_tag *fwd_tag; 5248f5a8818SKevin Lo int cscov_partial, iphlen; 525df8bae1dSRodney W. Grimes 5268f5a8818SKevin Lo m = *mp; 5278f5a8818SKevin Lo iphlen = *offp; 52871498f30SBruce M Simpson ifp = m->m_pkthdr.rcvif; 5298f5a8818SKevin Lo *mp = NULL; 530026decb8SRobert Watson UDPSTAT_INC(udps_ipackets); 531df8bae1dSRodney W. Grimes 532df8bae1dSRodney W. Grimes /* 5333329b236SRobert Watson * Strip IP options, if any; should skip this, make available to 5343329b236SRobert Watson * user, and use on returned packets, but we don't yet have a way to 5353329b236SRobert Watson * check the checksum with options still present. 536df8bae1dSRodney W. Grimes */ 537df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 538105bd211SGleb Smirnoff ip_stripoptions(m); 539df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 540df8bae1dSRodney W. Grimes } 541df8bae1dSRodney W. Grimes 542df8bae1dSRodney W. Grimes /* 543df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 544df8bae1dSRodney W. Grimes */ 545df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 546d1b18731SKevin Lo if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) { 547026decb8SRobert Watson UDPSTAT_INC(udps_hdrops); 5488f5a8818SKevin Lo return (IPPROTO_DONE); 549df8bae1dSRodney W. Grimes } 550df8bae1dSRodney W. Grimes } 551503f4e47SBjoern A. Zeeb ip = mtod(m, struct ip *); 552df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 5538f5a8818SKevin Lo cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0; 554df8bae1dSRodney W. Grimes 5553329b236SRobert Watson /* 5563329b236SRobert Watson * Destination port of 0 is illegal, based on RFC768. 5573329b236SRobert Watson */ 558686cdd19SJun-ichiro itojun Hagino if (uh->uh_dport == 0) 559f76fcf6dSJeffrey Hsu goto badunlocked; 560686cdd19SJun-ichiro itojun Hagino 561df8bae1dSRodney W. Grimes /* 5623329b236SRobert Watson * Construct sockaddr format source address. Stuff source address 5633329b236SRobert Watson * and datagram in user buffer. 564b9234fafSSam Leffler */ 565dce33a45SErmal Luçi bzero(&udp_in[0], sizeof(struct sockaddr_in) * 2); 566dce33a45SErmal Luçi udp_in[0].sin_len = sizeof(struct sockaddr_in); 567dce33a45SErmal Luçi udp_in[0].sin_family = AF_INET; 568dce33a45SErmal Luçi udp_in[0].sin_port = uh->uh_sport; 569dce33a45SErmal Luçi udp_in[0].sin_addr = ip->ip_src; 570dce33a45SErmal Luçi udp_in[1].sin_len = sizeof(struct sockaddr_in); 571dce33a45SErmal Luçi udp_in[1].sin_family = AF_INET; 572dce33a45SErmal Luçi udp_in[1].sin_port = uh->uh_dport; 573dce33a45SErmal Luçi udp_in[1].sin_addr = ip->ip_dst; 574b9234fafSSam Leffler 575b9234fafSSam Leffler /* 576af1ee11dSRobert Watson * Make mbuf data length reflect UDP length. If not enough data to 577af1ee11dSRobert Watson * reflect UDP length, drop. 578df8bae1dSRodney W. Grimes */ 579df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 5808ad458a4SGleb Smirnoff ip_len = ntohs(ip->ip_len) - iphlen; 5810f4a0366SMichael Tuexen if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) { 582e06e816fSKevin Lo /* Zero means checksum over the complete packet. */ 5830f4a0366SMichael Tuexen if (len == 0) 584e06e816fSKevin Lo len = ip_len; 585e06e816fSKevin Lo cscov_partial = 0; 586e06e816fSKevin Lo } 5878f134647SGleb Smirnoff if (ip_len != len) { 5888f134647SGleb Smirnoff if (len > ip_len || len < sizeof(struct udphdr)) { 589026decb8SRobert Watson UDPSTAT_INC(udps_badlen); 590f76fcf6dSJeffrey Hsu goto badunlocked; 591df8bae1dSRodney W. Grimes } 5928f5a8818SKevin Lo if (proto == IPPROTO_UDP) 5938f134647SGleb Smirnoff m_adj(m, len - ip_len); 594df8bae1dSRodney W. Grimes } 5953329b236SRobert Watson 596df8bae1dSRodney W. Grimes /* 597df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 598df8bae1dSRodney W. Grimes */ 5996dfab5b1SGarrett Wollman if (uh->uh_sum) { 60039629c92SDavid Malone u_short uh_sum; 60139629c92SDavid Malone 602e06e816fSKevin Lo if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) && 603e06e816fSKevin Lo !cscov_partial) { 604db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 60539629c92SDavid Malone uh_sum = m->m_pkthdr.csum_data; 606db4f9cc7SJonathan Lemon else 60739629c92SDavid Malone uh_sum = in_pseudo(ip->ip_src.s_addr, 608506f4949SRuslan Ermilov ip->ip_dst.s_addr, htonl((u_short)len + 6098f5a8818SKevin Lo m->m_pkthdr.csum_data + proto)); 61039629c92SDavid Malone uh_sum ^= 0xffff; 611db4f9cc7SJonathan Lemon } else { 6123358df29SGleb Smirnoff char b[offsetof(struct ipovly, ih_src)]; 6133358df29SGleb Smirnoff struct ipovly *ipov = (struct ipovly *)ip; 614af1ee11dSRobert Watson 6153358df29SGleb Smirnoff bcopy(ipov, b, sizeof(b)); 6163358df29SGleb Smirnoff bzero(ipov, sizeof(ipov->ih_x1)); 6173358df29SGleb Smirnoff ipov->ih_len = (proto == IPPROTO_UDP) ? 618e06e816fSKevin Lo uh->uh_ulen : htons(ip_len); 61939629c92SDavid Malone uh_sum = in_cksum(m, len + sizeof (struct ip)); 6203358df29SGleb Smirnoff bcopy(b, ipov, sizeof(b)); 621db4f9cc7SJonathan Lemon } 62239629c92SDavid Malone if (uh_sum) { 623026decb8SRobert Watson UDPSTAT_INC(udps_badsum); 624df8bae1dSRodney W. Grimes m_freem(m); 6258f5a8818SKevin Lo return (IPPROTO_DONE); 626df8bae1dSRodney W. Grimes } 627c6d81a34SMichael Tuexen } else { 628c6d81a34SMichael Tuexen if (proto == IPPROTO_UDP) { 629026decb8SRobert Watson UDPSTAT_INC(udps_nosum); 630c6d81a34SMichael Tuexen } else { 631c6d81a34SMichael Tuexen /* UDPLite requires a checksum */ 632c6d81a34SMichael Tuexen /* XXX: What is the right UDPLite MIB counter here? */ 633c6d81a34SMichael Tuexen m_freem(m); 634c6d81a34SMichael Tuexen return (IPPROTO_DONE); 635c6d81a34SMichael Tuexen } 636c6d81a34SMichael Tuexen } 637df8bae1dSRodney W. Grimes 638266f97b5SCy Schubert if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 639db0ac6deSCy Schubert in_broadcast(ip->ip_dst, ifp)) 640db0ac6deSCy Schubert return (udp_multi_input(m, proto, udp_in)); 641266f97b5SCy Schubert 642db0ac6deSCy Schubert pcbinfo = udp_get_inpcbinfo(proto); 6433329b236SRobert Watson 644df8bae1dSRodney W. Grimes /* 6456d6a026bSDavid Greenman * Locate pcb for datagram. 646db0ac6deSCy Schubert * 6478a006adbSBjoern A. Zeeb * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 6488a006adbSBjoern A. Zeeb */ 649ffdbf9daSAndrey V. Elsukov if ((m->m_flags & M_IP_NEXTHOP) && 650c1de64a4SAndrey V. Elsukov (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 6518a006adbSBjoern A. Zeeb struct sockaddr_in *next_hop; 6528a006adbSBjoern A. Zeeb 6538a006adbSBjoern A. Zeeb next_hop = (struct sockaddr_in *)(fwd_tag + 1); 6548a006adbSBjoern A. Zeeb 6558a006adbSBjoern A. Zeeb /* 6568a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 6578a006adbSBjoern A. Zeeb * Already got one like this? 6588a006adbSBjoern A. Zeeb */ 659e06e816fSKevin Lo inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 6608a006adbSBjoern A. Zeeb ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m); 6618a006adbSBjoern A. Zeeb if (!inp) { 6628a006adbSBjoern A. Zeeb /* 6638a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 6648a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 6658a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 6668a006adbSBjoern A. Zeeb */ 667e06e816fSKevin Lo inp = in_pcblookup(pcbinfo, ip->ip_src, 6688a006adbSBjoern A. Zeeb uh->uh_sport, next_hop->sin_addr, 6698a006adbSBjoern A. Zeeb next_hop->sin_port ? htons(next_hop->sin_port) : 6708a006adbSBjoern A. Zeeb uh->uh_dport, INPLOOKUP_WILDCARD | 6718a006adbSBjoern A. Zeeb INPLOOKUP_RLOCKPCB, ifp); 6728a006adbSBjoern A. Zeeb } 6738a006adbSBjoern A. Zeeb /* Remove the tag from the packet. We don't need it anymore. */ 6748a006adbSBjoern A. Zeeb m_tag_delete(m, fwd_tag); 675ffdbf9daSAndrey V. Elsukov m->m_flags &= ~M_IP_NEXTHOP; 6768a006adbSBjoern A. Zeeb } else 677e06e816fSKevin Lo inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 6788a006adbSBjoern A. Zeeb ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD | 6798a006adbSBjoern A. Zeeb INPLOOKUP_RLOCKPCB, ifp, m); 68015bd2b43SDavid Greenman if (inp == NULL) { 681334fc582SBjoern A. Zeeb if (V_udp_log_in_vain) { 682edf0313bSEric van Gyzen char src[INET_ADDRSTRLEN]; 683edf0313bSEric van Gyzen char dst[INET_ADDRSTRLEN]; 68475cfc95fSAndrey A. Chernov 685592071e8SBruce Evans log(LOG_INFO, 686592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 687edf0313bSEric van Gyzen inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport), 688edf0313bSEric van Gyzen inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport)); 68975cfc95fSAndrey A. Chernov } 6907bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 6917bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, NULL, ip, NULL, uh); 6927bda9663SMichael Tuexen else 693e1526d5aSMichael Tuexen UDP_PROBE(receive, NULL, NULL, ip, NULL, uh); 694026decb8SRobert Watson UDPSTAT_INC(udps_noport); 695df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 696026decb8SRobert Watson UDPSTAT_INC(udps_noportbcast); 697fa046d87SRobert Watson goto badunlocked; 698df8bae1dSRodney W. Grimes } 6993ea9a7cfSGleb Smirnoff if (V_udp_blackhole && (V_udp_blackhole_local || 7003ea9a7cfSGleb Smirnoff !in_localip(ip->ip_src))) 701fa046d87SRobert Watson goto badunlocked; 7021cbd978eSLuigi Rizzo if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 703fa046d87SRobert Watson goto badunlocked; 704582a7760SBruce Evans icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 7058f5a8818SKevin Lo return (IPPROTO_DONE); 706df8bae1dSRodney W. Grimes } 7073329b236SRobert Watson 7083329b236SRobert Watson /* 7093329b236SRobert Watson * Check the minimum TTL for socket. 7103329b236SRobert Watson */ 711fa046d87SRobert Watson INP_RLOCK_ASSERT(inp); 71210cc62b7SRobert Watson if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) { 7137bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 7147bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh); 7157bda9663SMichael Tuexen else 716e1526d5aSMichael Tuexen UDP_PROBE(receive, NULL, inp, ip, inp, uh); 71710cc62b7SRobert Watson INP_RUNLOCK(inp); 718fa046d87SRobert Watson m_freem(m); 7198f5a8818SKevin Lo return (IPPROTO_DONE); 72010cc62b7SRobert Watson } 721e06e816fSKevin Lo if (cscov_partial) { 722e06e816fSKevin Lo struct udpcb *up; 723e06e816fSKevin Lo 724e06e816fSKevin Lo up = intoudpcb(inp); 72583e95fb3SMichael Tuexen if (up->u_rxcslen == 0 || up->u_rxcslen > len) { 726e06e816fSKevin Lo INP_RUNLOCK(inp); 727e06e816fSKevin Lo m_freem(m); 7288f5a8818SKevin Lo return (IPPROTO_DONE); 729e06e816fSKevin Lo } 730e06e816fSKevin Lo } 73157f60867SMark Johnston 7327bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 7337bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh); 7347bda9663SMichael Tuexen else 7351ad19fb6SMark Johnston UDP_PROBE(receive, NULL, inp, ip, inp, uh); 736dce33a45SErmal Luçi if (udp_append(inp, ip, m, iphlen, udp_in) == 0) 737119d85f6SRobert Watson INP_RUNLOCK(inp); 7388f5a8818SKevin Lo return (IPPROTO_DONE); 73961ffc0b1SJeffrey Hsu 740f76fcf6dSJeffrey Hsu badunlocked: 741df8bae1dSRodney W. Grimes m_freem(m); 7428f5a8818SKevin Lo return (IPPROTO_DONE); 743cfa1ca9dSYoshinobu Inoue } 74479288c11SBjoern A. Zeeb #endif /* INET */ 745cfa1ca9dSYoshinobu Inoue 746cfa1ca9dSYoshinobu Inoue /* 7473329b236SRobert Watson * Notify a udp user of an asynchronous error; just wake up so that they can 7483329b236SRobert Watson * collect error status. 749df8bae1dSRodney W. Grimes */ 7503ce144eaSJeffrey Hsu struct inpcb * 7513329b236SRobert Watson udp_notify(struct inpcb *inp, int errno) 752df8bae1dSRodney W. Grimes { 7533329b236SRobert Watson 754083a010cSRyan Stone INP_WLOCK_ASSERT(inp); 75584cc0778SGeorge V. Neville-Neil if ((errno == EHOSTUNREACH || errno == ENETUNREACH || 756983066f0SAlexander V. Chernikov errno == EHOSTDOWN) && inp->inp_route.ro_nh) { 757983066f0SAlexander V. Chernikov NH_FREE(inp->inp_route.ro_nh); 758983066f0SAlexander V. Chernikov inp->inp_route.ro_nh = (struct nhop_object *)NULL; 75984cc0778SGeorge V. Neville-Neil } 7608501a69cSRobert Watson 761df8bae1dSRodney W. Grimes inp->inp_socket->so_error = errno; 762df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 763df8bae1dSRodney W. Grimes sowwakeup(inp->inp_socket); 7643329b236SRobert Watson return (inp); 765df8bae1dSRodney W. Grimes } 766df8bae1dSRodney W. Grimes 76779288c11SBjoern A. Zeeb #ifdef INET 768e06e816fSKevin Lo static void 769e06e816fSKevin Lo udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip, 770e06e816fSKevin Lo struct inpcbinfo *pcbinfo) 771df8bae1dSRodney W. Grimes { 772c693a045SJonathan Lemon struct ip *ip = vip; 773c693a045SJonathan Lemon struct udphdr *uh; 774c693a045SJonathan Lemon struct in_addr faddr; 775c693a045SJonathan Lemon struct inpcb *inp; 776c693a045SJonathan Lemon 777c693a045SJonathan Lemon faddr = ((struct sockaddr_in *)sa)->sin_addr; 778c693a045SJonathan Lemon if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 779c693a045SJonathan Lemon return; 780df8bae1dSRodney W. Grimes 78184cc0778SGeorge V. Neville-Neil if (PRC_IS_REDIRECT(cmd)) { 78284cc0778SGeorge V. Neville-Neil /* signal EHOSTDOWN, as it flushes the cached route */ 7834f321dbdSBjoern A. Zeeb in_pcbnotifyall(&V_udbinfo, faddr, EHOSTDOWN, udp_notify); 78497d8d152SAndre Oppermann return; 78584cc0778SGeorge V. Neville-Neil } 7863329b236SRobert Watson 78797d8d152SAndre Oppermann /* 78897d8d152SAndre Oppermann * Hostdead is ugly because it goes linearly through all PCBs. 7893329b236SRobert Watson * 7903329b236SRobert Watson * XXX: We never get this from ICMP, otherwise it makes an excellent 7913329b236SRobert Watson * DoS attack on machines with many connections. 79297d8d152SAndre Oppermann */ 79397d8d152SAndre Oppermann if (cmd == PRC_HOSTDEAD) 794af1ee11dSRobert Watson ip = NULL; 795d1c54148SJesper Skriver else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 796df8bae1dSRodney W. Grimes return; 797af1ee11dSRobert Watson if (ip != NULL) { 798df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 799e06e816fSKevin Lo inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 800083a010cSRyan Stone ip->ip_src, uh->uh_sport, INPLOOKUP_WLOCKPCB, NULL); 801f76fcf6dSJeffrey Hsu if (inp != NULL) { 802083a010cSRyan Stone INP_WLOCK_ASSERT(inp); 803f76fcf6dSJeffrey Hsu if (inp->inp_socket != NULL) { 804f5514f08SRobert Watson udp_notify(inp, inetctlerrmap[cmd]); 805f76fcf6dSJeffrey Hsu } 806083a010cSRyan Stone INP_WUNLOCK(inp); 807abb901c5SRandall Stewart } else { 808abb901c5SRandall Stewart inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 809abb901c5SRandall Stewart ip->ip_src, uh->uh_sport, 810abb901c5SRandall Stewart INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 811abb901c5SRandall Stewart if (inp != NULL) { 812abb901c5SRandall Stewart struct udpcb *up; 81346374cbfSMatt Macy void *ctx; 81446374cbfSMatt Macy udp_tun_icmp_t func; 815abb901c5SRandall Stewart 816abb901c5SRandall Stewart up = intoudpcb(inp); 81746374cbfSMatt Macy ctx = up->u_tun_ctx; 81846374cbfSMatt Macy func = up->u_icmp_func; 819abb901c5SRandall Stewart INP_RUNLOCK(inp); 82046374cbfSMatt Macy if (func != NULL) 82146374cbfSMatt Macy (*func)(cmd, sa, vip, ctx); 822abb901c5SRandall Stewart } 823f76fcf6dSJeffrey Hsu } 824df8bae1dSRodney W. Grimes } else 825e06e816fSKevin Lo in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd], 826f5514f08SRobert Watson udp_notify); 827df8bae1dSRodney W. Grimes } 828e06e816fSKevin Lo void 829e06e816fSKevin Lo udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 830e06e816fSKevin Lo { 831e06e816fSKevin Lo 832e06e816fSKevin Lo return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo)); 833e06e816fSKevin Lo } 834e06e816fSKevin Lo 835e06e816fSKevin Lo void 836e06e816fSKevin Lo udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip) 837e06e816fSKevin Lo { 838e06e816fSKevin Lo 839e06e816fSKevin Lo return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo)); 840e06e816fSKevin Lo } 84179288c11SBjoern A. Zeeb #endif /* INET */ 842df8bae1dSRodney W. Grimes 8430312fbe9SPoul-Henning Kamp static int 84482d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS) 84598271db4SGarrett Wollman { 846db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_udbinfo, 847db0ac6deSCy Schubert INPLOOKUP_RLOCKPCB); 84898271db4SGarrett Wollman struct xinpgen xig; 849032677ceSGleb Smirnoff struct inpcb *inp; 850032677ceSGleb Smirnoff int error; 85198271db4SGarrett Wollman 852032677ceSGleb Smirnoff if (req->newptr != 0) 853032677ceSGleb Smirnoff return (EPERM); 854032677ceSGleb Smirnoff 85598271db4SGarrett Wollman if (req->oldptr == 0) { 856032677ceSGleb Smirnoff int n; 857032677ceSGleb Smirnoff 858603724d3SBjoern A. Zeeb n = V_udbinfo.ipi_count; 859c007b96aSJohn Baldwin n += imax(n / 8, 10); 860c007b96aSJohn Baldwin req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb); 8613329b236SRobert Watson return (0); 86298271db4SGarrett Wollman } 86398271db4SGarrett Wollman 864032677ceSGleb Smirnoff if ((error = sysctl_wire_old_buffer(req, 0)) != 0) 86547934cefSDon Lewis return (error); 8665c38b6dbSDon Lewis 86779db6fe7SMark Johnston bzero(&xig, sizeof(xig)); 86898271db4SGarrett Wollman xig.xig_len = sizeof xig; 869032677ceSGleb Smirnoff xig.xig_count = V_udbinfo.ipi_count; 870032677ceSGleb Smirnoff xig.xig_gen = V_udbinfo.ipi_gencnt; 87198271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 87298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 87398271db4SGarrett Wollman if (error) 8743329b236SRobert Watson return (error); 87599208b82SMatt Macy 876db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) { 877032677ceSGleb Smirnoff if (inp->inp_gencnt <= xig.xig_gen && 878032677ceSGleb Smirnoff cr_canseeinpcb(req->td->td_ucred, inp) == 0) { 87998271db4SGarrett Wollman struct xinpcb xi; 880d0e157f6SBjoern A. Zeeb 881cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(inp, &xi); 882266f97b5SCy Schubert error = SYSCTL_OUT(req, &xi, sizeof xi); 883db0ac6deSCy Schubert if (error) { 884266f97b5SCy Schubert INP_RUNLOCK(inp); 885db0ac6deSCy Schubert break; 88698271db4SGarrett Wollman } 887db0ac6deSCy Schubert } 888db0ac6deSCy Schubert } 889d0e157f6SBjoern A. Zeeb 89098271db4SGarrett Wollman if (!error) { 89198271db4SGarrett Wollman /* 8923329b236SRobert Watson * Give the user an updated idea of our state. If the 8933329b236SRobert Watson * generation differs from what we told her before, she knows 8943329b236SRobert Watson * that something happened while we were processing this 8953329b236SRobert Watson * request, and it might be necessary to retry. 89698271db4SGarrett Wollman */ 897603724d3SBjoern A. Zeeb xig.xig_gen = V_udbinfo.ipi_gencnt; 89898271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 899603724d3SBjoern A. Zeeb xig.xig_count = V_udbinfo.ipi_count; 90098271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 90198271db4SGarrett Wollman } 902032677ceSGleb Smirnoff 9033329b236SRobert Watson return (error); 90498271db4SGarrett Wollman } 90598271db4SGarrett Wollman 90679c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, 9077029da5cSPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 9087029da5cSPawel Biernacki udp_pcblist, "S,xinpcb", 9097029da5cSPawel Biernacki "List of active UDP sockets"); 91098271db4SGarrett Wollman 91179288c11SBjoern A. Zeeb #ifdef INET 91298271db4SGarrett Wollman static int 91382d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS) 914490d50b6SBrian Feldman { 915c0511d3bSBrian Feldman struct xucred xuc; 916490d50b6SBrian Feldman struct sockaddr_in addrs[2]; 917d797164aSGleb Smirnoff struct epoch_tracker et; 918490d50b6SBrian Feldman struct inpcb *inp; 919277afaffSRobert Watson int error; 920490d50b6SBrian Feldman 92132f9753cSRobert Watson error = priv_check(req->td, PRIV_NETINET_GETCRED); 922490d50b6SBrian Feldman if (error) 923490d50b6SBrian Feldman return (error); 924490d50b6SBrian Feldman error = SYSCTL_IN(req, addrs, sizeof(addrs)); 925490d50b6SBrian Feldman if (error) 926490d50b6SBrian Feldman return (error); 927d797164aSGleb Smirnoff NET_EPOCH_ENTER(et); 928fa046d87SRobert Watson inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 929fa046d87SRobert Watson addrs[0].sin_addr, addrs[0].sin_port, 930fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 931d797164aSGleb Smirnoff NET_EPOCH_EXIT(et); 9329622e84fSRobert Watson if (inp != NULL) { 933fa046d87SRobert Watson INP_RLOCK_ASSERT(inp); 9349622e84fSRobert Watson if (inp->inp_socket == NULL) 9359622e84fSRobert Watson error = ENOENT; 9369622e84fSRobert Watson if (error == 0) 937f08ef6c5SBjoern A. Zeeb error = cr_canseeinpcb(req->td->td_ucred, inp); 9389622e84fSRobert Watson if (error == 0) 93986d02c5cSBjoern A. Zeeb cru2x(inp->inp_cred, &xuc); 9409622e84fSRobert Watson INP_RUNLOCK(inp); 941fa046d87SRobert Watson } else 9429622e84fSRobert Watson error = ENOENT; 9430e1eebb8SDon Lewis if (error == 0) 9440e1eebb8SDon Lewis error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 945490d50b6SBrian Feldman return (error); 946490d50b6SBrian Feldman } 947490d50b6SBrian Feldman 9487ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 9497029da5cSPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE, 9507029da5cSPawel Biernacki 0, 0, udp_getcred, "S,xucred", 9517029da5cSPawel Biernacki "Get the xucred of a UDP connection"); 95279288c11SBjoern A. Zeeb #endif /* INET */ 953490d50b6SBrian Feldman 9547b495c44SVANHULLEBUS Yvan int 9557b495c44SVANHULLEBUS Yvan udp_ctloutput(struct socket *so, struct sockopt *sopt) 9567b495c44SVANHULLEBUS Yvan { 9577b495c44SVANHULLEBUS Yvan struct inpcb *inp; 9587b495c44SVANHULLEBUS Yvan struct udpcb *up; 959e06e816fSKevin Lo int isudplite, error, optval; 9607b495c44SVANHULLEBUS Yvan 961e06e816fSKevin Lo error = 0; 962e06e816fSKevin Lo isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0; 9637b495c44SVANHULLEBUS Yvan inp = sotoinpcb(so); 9647b495c44SVANHULLEBUS Yvan KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 9657b495c44SVANHULLEBUS Yvan INP_WLOCK(inp); 966e06e816fSKevin Lo if (sopt->sopt_level != so->so_proto->pr_protocol) { 9677b495c44SVANHULLEBUS Yvan #ifdef INET6 9687b495c44SVANHULLEBUS Yvan if (INP_CHECK_SOCKAF(so, AF_INET6)) { 9697b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 9707b495c44SVANHULLEBUS Yvan error = ip6_ctloutput(so, sopt); 97179288c11SBjoern A. Zeeb } 9727b495c44SVANHULLEBUS Yvan #endif 97379288c11SBjoern A. Zeeb #if defined(INET) && defined(INET6) 97479288c11SBjoern A. Zeeb else 97579288c11SBjoern A. Zeeb #endif 97679288c11SBjoern A. Zeeb #ifdef INET 97779288c11SBjoern A. Zeeb { 9787b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 9797b495c44SVANHULLEBUS Yvan error = ip_ctloutput(so, sopt); 9807b495c44SVANHULLEBUS Yvan } 9817b495c44SVANHULLEBUS Yvan #endif 9827b495c44SVANHULLEBUS Yvan return (error); 9837b495c44SVANHULLEBUS Yvan } 9847b495c44SVANHULLEBUS Yvan 9857b495c44SVANHULLEBUS Yvan switch (sopt->sopt_dir) { 9867b495c44SVANHULLEBUS Yvan case SOPT_SET: 9877b495c44SVANHULLEBUS Yvan switch (sopt->sopt_name) { 988fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 989fcf59617SAndrey V. Elsukov #ifdef INET 9907b495c44SVANHULLEBUS Yvan case UDP_ENCAP: 991fcf59617SAndrey V. Elsukov if (!IPSEC_ENABLED(ipv4)) { 9927b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 993fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 9947b495c44SVANHULLEBUS Yvan } 995fcf59617SAndrey V. Elsukov error = UDPENCAP_PCBCTL(inp, sopt); 9967b495c44SVANHULLEBUS Yvan break; 997fcf59617SAndrey V. Elsukov #endif /* INET */ 998fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 999e06e816fSKevin Lo case UDPLITE_SEND_CSCOV: 1000e06e816fSKevin Lo case UDPLITE_RECV_CSCOV: 1001e06e816fSKevin Lo if (!isudplite) { 1002e06e816fSKevin Lo INP_WUNLOCK(inp); 1003e06e816fSKevin Lo error = ENOPROTOOPT; 1004e06e816fSKevin Lo break; 1005e06e816fSKevin Lo } 1006e06e816fSKevin Lo INP_WUNLOCK(inp); 1007e06e816fSKevin Lo error = sooptcopyin(sopt, &optval, sizeof(optval), 1008e06e816fSKevin Lo sizeof(optval)); 1009e06e816fSKevin Lo if (error != 0) 1010e06e816fSKevin Lo break; 1011e06e816fSKevin Lo inp = sotoinpcb(so); 1012e06e816fSKevin Lo KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1013e06e816fSKevin Lo INP_WLOCK(inp); 1014e06e816fSKevin Lo up = intoudpcb(inp); 1015e06e816fSKevin Lo KASSERT(up != NULL, ("%s: up == NULL", __func__)); 101603f90784SMichael Tuexen if ((optval != 0 && optval < 8) || (optval > 65535)) { 1017e06e816fSKevin Lo INP_WUNLOCK(inp); 1018e06e816fSKevin Lo error = EINVAL; 1019e06e816fSKevin Lo break; 1020e06e816fSKevin Lo } 1021e06e816fSKevin Lo if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1022e06e816fSKevin Lo up->u_txcslen = optval; 1023e06e816fSKevin Lo else 1024e06e816fSKevin Lo up->u_rxcslen = optval; 1025e06e816fSKevin Lo INP_WUNLOCK(inp); 1026e06e816fSKevin Lo break; 10277b495c44SVANHULLEBUS Yvan default: 10287b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 10297b495c44SVANHULLEBUS Yvan error = ENOPROTOOPT; 10307b495c44SVANHULLEBUS Yvan break; 10317b495c44SVANHULLEBUS Yvan } 10327b495c44SVANHULLEBUS Yvan break; 10337b495c44SVANHULLEBUS Yvan case SOPT_GET: 10347b495c44SVANHULLEBUS Yvan switch (sopt->sopt_name) { 1035fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1036fcf59617SAndrey V. Elsukov #ifdef INET 10377b495c44SVANHULLEBUS Yvan case UDP_ENCAP: 1038fcf59617SAndrey V. Elsukov if (!IPSEC_ENABLED(ipv4)) { 10397b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 1040fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 1041fcf59617SAndrey V. Elsukov } 1042fcf59617SAndrey V. Elsukov error = UDPENCAP_PCBCTL(inp, sopt); 10437b495c44SVANHULLEBUS Yvan break; 1044fcf59617SAndrey V. Elsukov #endif /* INET */ 1045fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 1046e06e816fSKevin Lo case UDPLITE_SEND_CSCOV: 1047e06e816fSKevin Lo case UDPLITE_RECV_CSCOV: 1048e06e816fSKevin Lo if (!isudplite) { 1049e06e816fSKevin Lo INP_WUNLOCK(inp); 1050e06e816fSKevin Lo error = ENOPROTOOPT; 1051e06e816fSKevin Lo break; 1052e06e816fSKevin Lo } 1053e06e816fSKevin Lo up = intoudpcb(inp); 1054e06e816fSKevin Lo KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1055e06e816fSKevin Lo if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1056e06e816fSKevin Lo optval = up->u_txcslen; 1057e06e816fSKevin Lo else 1058e06e816fSKevin Lo optval = up->u_rxcslen; 1059e06e816fSKevin Lo INP_WUNLOCK(inp); 1060e06e816fSKevin Lo error = sooptcopyout(sopt, &optval, sizeof(optval)); 1061e06e816fSKevin Lo break; 10627b495c44SVANHULLEBUS Yvan default: 10637b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 10647b495c44SVANHULLEBUS Yvan error = ENOPROTOOPT; 10657b495c44SVANHULLEBUS Yvan break; 10667b495c44SVANHULLEBUS Yvan } 10677b495c44SVANHULLEBUS Yvan break; 10687b495c44SVANHULLEBUS Yvan } 10697b495c44SVANHULLEBUS Yvan return (error); 10707b495c44SVANHULLEBUS Yvan } 10717b495c44SVANHULLEBUS Yvan 107279288c11SBjoern A. Zeeb #ifdef INET 1073a9839c4aSBjoern A. Zeeb #ifdef INET6 1074a9839c4aSBjoern A. Zeeb /* The logic here is derived from ip6_setpktopt(). See comments there. */ 1075a9839c4aSBjoern A. Zeeb static int 1076a9839c4aSBjoern A. Zeeb udp_v4mapped_pktinfo(struct cmsghdr *cm, struct sockaddr_in * src, 1077a9839c4aSBjoern A. Zeeb struct inpcb *inp, int flags) 1078a9839c4aSBjoern A. Zeeb { 1079a9839c4aSBjoern A. Zeeb struct ifnet *ifp; 1080a9839c4aSBjoern A. Zeeb struct in6_pktinfo *pktinfo; 1081a9839c4aSBjoern A. Zeeb struct in_addr ia; 1082a9839c4aSBjoern A. Zeeb 1083a9839c4aSBjoern A. Zeeb if ((flags & PRUS_IPV6) == 0) 1084a9839c4aSBjoern A. Zeeb return (0); 1085a9839c4aSBjoern A. Zeeb 1086a9839c4aSBjoern A. Zeeb if (cm->cmsg_level != IPPROTO_IPV6) 1087a9839c4aSBjoern A. Zeeb return (0); 1088a9839c4aSBjoern A. Zeeb 1089a9839c4aSBjoern A. Zeeb if (cm->cmsg_type != IPV6_2292PKTINFO && 1090a9839c4aSBjoern A. Zeeb cm->cmsg_type != IPV6_PKTINFO) 1091a9839c4aSBjoern A. Zeeb return (0); 1092a9839c4aSBjoern A. Zeeb 1093a9839c4aSBjoern A. Zeeb if (cm->cmsg_len != 1094a9839c4aSBjoern A. Zeeb CMSG_LEN(sizeof(struct in6_pktinfo))) 1095a9839c4aSBjoern A. Zeeb return (EINVAL); 1096a9839c4aSBjoern A. Zeeb 1097a9839c4aSBjoern A. Zeeb pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm); 1098a9839c4aSBjoern A. Zeeb if (!IN6_IS_ADDR_V4MAPPED(&pktinfo->ipi6_addr) && 1099a9839c4aSBjoern A. Zeeb !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) 1100a9839c4aSBjoern A. Zeeb return (EINVAL); 1101a9839c4aSBjoern A. Zeeb 1102a9839c4aSBjoern A. Zeeb /* Validate the interface index if specified. */ 1103a9839c4aSBjoern A. Zeeb if (pktinfo->ipi6_ifindex) { 1104*d74b7baeSGleb Smirnoff struct epoch_tracker et; 1105*d74b7baeSGleb Smirnoff 1106*d74b7baeSGleb Smirnoff NET_EPOCH_ENTER(et); 1107a9839c4aSBjoern A. Zeeb ifp = ifnet_byindex(pktinfo->ipi6_ifindex); 1108*d74b7baeSGleb Smirnoff NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */ 1109a9839c4aSBjoern A. Zeeb if (ifp == NULL) 1110a9839c4aSBjoern A. Zeeb return (ENXIO); 1111*d74b7baeSGleb Smirnoff } else 1112*d74b7baeSGleb Smirnoff ifp = NULL; 1113a9839c4aSBjoern A. Zeeb if (ifp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 1114a9839c4aSBjoern A. Zeeb ia.s_addr = pktinfo->ipi6_addr.s6_addr32[3]; 1115a9839c4aSBjoern A. Zeeb if (in_ifhasaddr(ifp, ia) == 0) 1116a9839c4aSBjoern A. Zeeb return (EADDRNOTAVAIL); 1117a9839c4aSBjoern A. Zeeb } 1118a9839c4aSBjoern A. Zeeb 1119a9839c4aSBjoern A. Zeeb bzero(src, sizeof(*src)); 1120a9839c4aSBjoern A. Zeeb src->sin_family = AF_INET; 1121a9839c4aSBjoern A. Zeeb src->sin_len = sizeof(*src); 1122a9839c4aSBjoern A. Zeeb src->sin_port = inp->inp_lport; 1123a9839c4aSBjoern A. Zeeb src->sin_addr.s_addr = pktinfo->ipi6_addr.s6_addr32[3]; 1124a9839c4aSBjoern A. Zeeb 1125a9839c4aSBjoern A. Zeeb return (0); 1126a9839c4aSBjoern A. Zeeb } 1127a9839c4aSBjoern A. Zeeb #endif 1128a9839c4aSBjoern A. Zeeb 1129490d50b6SBrian Feldman static int 11303329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, 1131a9839c4aSBjoern A. Zeeb struct mbuf *control, struct thread *td, int flags) 1132df8bae1dSRodney W. Grimes { 11333329b236SRobert Watson struct udpiphdr *ui; 11343329b236SRobert Watson int len = m->m_pkthdr.len; 113590162a4eSIan Dowse struct in_addr faddr, laddr; 1136c557ae16SIan Dowse struct cmsghdr *cm; 1137e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1138c557ae16SIan Dowse struct sockaddr_in *sin, src; 11396573d758SMatt Macy struct epoch_tracker et; 1140e06e816fSKevin Lo int cscov_partial = 0; 114190162a4eSIan Dowse int error = 0; 1142374ce248SMitchell Horne int ipflags = 0; 114390162a4eSIan Dowse u_short fport, lport; 1144f584d74bSMichael Tuexen u_char tos; 1145e06e816fSKevin Lo uint8_t pr; 1146e06e816fSKevin Lo uint16_t cscov = 0; 11479d3ddf43SAdrian Chadd uint32_t flowid = 0; 1148c2529042SHans Petter Selasky uint8_t flowtype = M_HASHTYPE_NONE; 1149df8bae1dSRodney W. Grimes 1150430d30d8SBill Fenner if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 1151c557ae16SIan Dowse if (control) 1152c557ae16SIan Dowse m_freem(control); 11535c32ea65SRobert Watson m_freem(m); 11543329b236SRobert Watson return (EMSGSIZE); 1155430d30d8SBill Fenner } 1156430d30d8SBill Fenner 11571b7f0384SBruce M Simpson src.sin_family = 0; 115884cc0778SGeorge V. Neville-Neil sin = (struct sockaddr_in *)addr; 11592435e507SGleb Smirnoff 1160eafaa1bcSBjoern A. Zeeb /* 11612435e507SGleb Smirnoff * udp_output() may need to temporarily bind or connect the current 11622435e507SGleb Smirnoff * inpcb. As such, we don't know up front whether we will need the 11632435e507SGleb Smirnoff * pcbinfo lock or not. Do any work to decide what is needed up 11642435e507SGleb Smirnoff * front before acquiring any locks. 11652435e507SGleb Smirnoff * 11662435e507SGleb Smirnoff * We will need network epoch in either case, to safely lookup into 11672435e507SGleb Smirnoff * pcb hash. 1168eafaa1bcSBjoern A. Zeeb */ 11692435e507SGleb Smirnoff if (sin == NULL || 11702435e507SGleb Smirnoff (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) 11712435e507SGleb Smirnoff INP_WLOCK(inp); 11722435e507SGleb Smirnoff else 11730cfdff24SBjoern A. Zeeb INP_RLOCK(inp); 11742435e507SGleb Smirnoff NET_EPOCH_ENTER(et); 1175f584d74bSMichael Tuexen tos = inp->inp_ip_tos; 1176c557ae16SIan Dowse if (control != NULL) { 1177c557ae16SIan Dowse /* 11783329b236SRobert Watson * XXX: Currently, we assume all the optional information is 11793329b236SRobert Watson * stored in a single mbuf. 1180c557ae16SIan Dowse */ 1181c557ae16SIan Dowse if (control->m_next) { 1182c557ae16SIan Dowse m_freem(control); 11832435e507SGleb Smirnoff error = EINVAL; 11842435e507SGleb Smirnoff goto release; 1185c557ae16SIan Dowse } 1186c557ae16SIan Dowse for (; control->m_len > 0; 1187c557ae16SIan Dowse control->m_data += CMSG_ALIGN(cm->cmsg_len), 1188c557ae16SIan Dowse control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 1189c557ae16SIan Dowse cm = mtod(control, struct cmsghdr *); 1190af1ee11dSRobert Watson if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 1191af1ee11dSRobert Watson || cm->cmsg_len > control->m_len) { 1192c557ae16SIan Dowse error = EINVAL; 1193c557ae16SIan Dowse break; 1194c557ae16SIan Dowse } 1195a9839c4aSBjoern A. Zeeb #ifdef INET6 1196a9839c4aSBjoern A. Zeeb error = udp_v4mapped_pktinfo(cm, &src, inp, flags); 1197a9839c4aSBjoern A. Zeeb if (error != 0) 1198a9839c4aSBjoern A. Zeeb break; 1199a9839c4aSBjoern A. Zeeb #endif 1200c557ae16SIan Dowse if (cm->cmsg_level != IPPROTO_IP) 1201c557ae16SIan Dowse continue; 1202c557ae16SIan Dowse 1203c557ae16SIan Dowse switch (cm->cmsg_type) { 1204c557ae16SIan Dowse case IP_SENDSRCADDR: 1205c557ae16SIan Dowse if (cm->cmsg_len != 1206c557ae16SIan Dowse CMSG_LEN(sizeof(struct in_addr))) { 1207c557ae16SIan Dowse error = EINVAL; 1208c557ae16SIan Dowse break; 1209c557ae16SIan Dowse } 1210c557ae16SIan Dowse bzero(&src, sizeof(src)); 1211c557ae16SIan Dowse src.sin_family = AF_INET; 1212c557ae16SIan Dowse src.sin_len = sizeof(src); 1213c557ae16SIan Dowse src.sin_port = inp->inp_lport; 1214af1ee11dSRobert Watson src.sin_addr = 1215af1ee11dSRobert Watson *(struct in_addr *)CMSG_DATA(cm); 1216c557ae16SIan Dowse break; 1217af1ee11dSRobert Watson 1218f584d74bSMichael Tuexen case IP_TOS: 1219f584d74bSMichael Tuexen if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) { 1220f584d74bSMichael Tuexen error = EINVAL; 1221f584d74bSMichael Tuexen break; 1222f584d74bSMichael Tuexen } 1223f584d74bSMichael Tuexen tos = *(u_char *)CMSG_DATA(cm); 1224f584d74bSMichael Tuexen break; 1225f584d74bSMichael Tuexen 12269d3ddf43SAdrian Chadd case IP_FLOWID: 12279d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 12289d3ddf43SAdrian Chadd error = EINVAL; 12299d3ddf43SAdrian Chadd break; 12309d3ddf43SAdrian Chadd } 12319d3ddf43SAdrian Chadd flowid = *(uint32_t *) CMSG_DATA(cm); 12329d3ddf43SAdrian Chadd break; 12339d3ddf43SAdrian Chadd 12349d3ddf43SAdrian Chadd case IP_FLOWTYPE: 12359d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 12369d3ddf43SAdrian Chadd error = EINVAL; 12379d3ddf43SAdrian Chadd break; 12389d3ddf43SAdrian Chadd } 1239c2529042SHans Petter Selasky flowtype = *(uint32_t *) CMSG_DATA(cm); 12409d3ddf43SAdrian Chadd break; 12419d3ddf43SAdrian Chadd 12429d3ddf43SAdrian Chadd #ifdef RSS 12439d3ddf43SAdrian Chadd case IP_RSSBUCKETID: 12449d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 12459d3ddf43SAdrian Chadd error = EINVAL; 12469d3ddf43SAdrian Chadd break; 12479d3ddf43SAdrian Chadd } 12489d3ddf43SAdrian Chadd /* This is just a placeholder for now */ 12499d3ddf43SAdrian Chadd break; 12509d3ddf43SAdrian Chadd #endif /* RSS */ 1251c557ae16SIan Dowse default: 1252c557ae16SIan Dowse error = ENOPROTOOPT; 1253c557ae16SIan Dowse break; 1254c557ae16SIan Dowse } 1255c557ae16SIan Dowse if (error) 1256c557ae16SIan Dowse break; 1257c557ae16SIan Dowse } 1258c557ae16SIan Dowse m_freem(control); 1259d8acd268SMark Johnston control = NULL; 1260c557ae16SIan Dowse } 12612435e507SGleb Smirnoff if (error) 12622435e507SGleb Smirnoff goto release; 12635c32ea65SRobert Watson 1264e06e816fSKevin Lo pr = inp->inp_socket->so_proto->pr_protocol; 1265a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(pr); 12665c32ea65SRobert Watson 12671b7f0384SBruce M Simpson /* 12681b7f0384SBruce M Simpson * If the IP_SENDSRCADDR control message was specified, override the 12691b7f0384SBruce M Simpson * source address for this datagram. Its use is invalidated if the 12701b7f0384SBruce M Simpson * address thus specified is incomplete or clobbers other inpcbs. 12711b7f0384SBruce M Simpson */ 127290162a4eSIan Dowse laddr = inp->inp_laddr; 127390162a4eSIan Dowse lport = inp->inp_lport; 12741b7f0384SBruce M Simpson if (src.sin_family == AF_INET) { 12751b7f0384SBruce M Simpson if ((lport == 0) || 12761b7f0384SBruce M Simpson (laddr.s_addr == INADDR_ANY && 12771b7f0384SBruce M Simpson src.sin_addr.s_addr == INADDR_ANY)) { 1278c557ae16SIan Dowse error = EINVAL; 1279c557ae16SIan Dowse goto release; 1280c557ae16SIan Dowse } 1281db0ac6deSCy Schubert INP_HASH_WLOCK(pcbinfo); 1282c557ae16SIan Dowse error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 1283b0330ed9SPawel Jakub Dawidek &laddr.s_addr, &lport, td->td_ucred); 1284db0ac6deSCy Schubert INP_HASH_WUNLOCK(pcbinfo); 1285c557ae16SIan Dowse if (error) 1286c557ae16SIan Dowse goto release; 1287c557ae16SIan Dowse } 1288c557ae16SIan Dowse 12893144b7d3SRobert Watson /* 12903144b7d3SRobert Watson * If a UDP socket has been connected, then a local address/port will 12913144b7d3SRobert Watson * have been selected and bound. 12923144b7d3SRobert Watson * 129343cc0bc1SRobert Watson * If a UDP socket has not been connected to, then an explicit 12943144b7d3SRobert Watson * destination address must be used, in which case a local 12953144b7d3SRobert Watson * address/port may not have been selected and bound. 12963144b7d3SRobert Watson */ 129743cc0bc1SRobert Watson if (sin != NULL) { 1298c4d585aeSRobert Watson INP_LOCK_ASSERT(inp); 1299df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 1300df8bae1dSRodney W. Grimes error = EISCONN; 1301df8bae1dSRodney W. Grimes goto release; 1302df8bae1dSRodney W. Grimes } 13033144b7d3SRobert Watson 13043144b7d3SRobert Watson /* 13053144b7d3SRobert Watson * Jail may rewrite the destination address, so let it do 13063144b7d3SRobert Watson * that before we use it. 13073144b7d3SRobert Watson */ 1308b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1309b89e82ddSJamie Gritton if (error) 1310413628a7SBjoern A. Zeeb goto release; 13113144b7d3SRobert Watson 13123144b7d3SRobert Watson /* 131343cc0bc1SRobert Watson * If a local address or port hasn't yet been selected, or if 131443cc0bc1SRobert Watson * the destination address needs to be rewritten due to using 131543cc0bc1SRobert Watson * a special INADDR_ constant, invoke in_pcbconnect_setup() 131643cc0bc1SRobert Watson * to do the heavy lifting. Once a port is selected, we 131743cc0bc1SRobert Watson * commit the binding back to the socket; we also commit the 131843cc0bc1SRobert Watson * binding of the address if in jail. 131943cc0bc1SRobert Watson * 132043cc0bc1SRobert Watson * If we already have a valid binding and we're not 132143cc0bc1SRobert Watson * requesting a destination address rewrite, use a fast path. 13223144b7d3SRobert Watson */ 132343cc0bc1SRobert Watson if (inp->inp_laddr.s_addr == INADDR_ANY || 132443cc0bc1SRobert Watson inp->inp_lport == 0 || 132543cc0bc1SRobert Watson sin->sin_addr.s_addr == INADDR_ANY || 132643cc0bc1SRobert Watson sin->sin_addr.s_addr == INADDR_BROADCAST) { 1327db0ac6deSCy Schubert INP_HASH_WLOCK(pcbinfo); 132843cc0bc1SRobert Watson error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, 132943cc0bc1SRobert Watson &lport, &faddr.s_addr, &fport, NULL, 133043cc0bc1SRobert Watson td->td_ucred); 1331db0ac6deSCy Schubert if (error) { 1332db0ac6deSCy Schubert INP_HASH_WUNLOCK(pcbinfo); 133390162a4eSIan Dowse goto release; 1334db0ac6deSCy Schubert } 133590162a4eSIan Dowse 133643cc0bc1SRobert Watson /* 133743cc0bc1SRobert Watson * XXXRW: Why not commit the port if the address is 133843cc0bc1SRobert Watson * !INADDR_ANY? 133943cc0bc1SRobert Watson */ 134090162a4eSIan Dowse /* Commit the local port if newly assigned. */ 134190162a4eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && 134290162a4eSIan Dowse inp->inp_lport == 0) { 1343c4d585aeSRobert Watson INP_WLOCK_ASSERT(inp); 13443a1757b9SGleb Smirnoff /* 134543cc0bc1SRobert Watson * Remember addr if jailed, to prevent 134643cc0bc1SRobert Watson * rebinding. 13473a1757b9SGleb Smirnoff */ 13480304c731SJamie Gritton if (prison_flag(td->td_ucred, PR_IP4)) 13493a1757b9SGleb Smirnoff inp->inp_laddr = laddr; 135090162a4eSIan Dowse inp->inp_lport = lport; 13512435e507SGleb Smirnoff error = in_pcbinshash(inp); 13522435e507SGleb Smirnoff INP_HASH_WUNLOCK(pcbinfo); 13532435e507SGleb Smirnoff if (error != 0) { 135490162a4eSIan Dowse inp->inp_lport = 0; 135590162a4eSIan Dowse error = EAGAIN; 1356df8bae1dSRodney W. Grimes goto release; 1357df8bae1dSRodney W. Grimes } 135890162a4eSIan Dowse inp->inp_flags |= INP_ANONPORT; 1359db0ac6deSCy Schubert } else 1360db0ac6deSCy Schubert INP_HASH_WUNLOCK(pcbinfo); 1361df8bae1dSRodney W. Grimes } else { 136243cc0bc1SRobert Watson faddr = sin->sin_addr; 136343cc0bc1SRobert Watson fport = sin->sin_port; 136443cc0bc1SRobert Watson } 136543cc0bc1SRobert Watson } else { 1366c4d585aeSRobert Watson INP_LOCK_ASSERT(inp); 136790162a4eSIan Dowse faddr = inp->inp_faddr; 136890162a4eSIan Dowse fport = inp->inp_fport; 136990162a4eSIan Dowse if (faddr.s_addr == INADDR_ANY) { 1370df8bae1dSRodney W. Grimes error = ENOTCONN; 1371df8bae1dSRodney W. Grimes goto release; 1372df8bae1dSRodney W. Grimes } 1373df8bae1dSRodney W. Grimes } 1374e6ccd709SRobert Watson 1375df8bae1dSRodney W. Grimes /* 1376e6ccd709SRobert Watson * Calculate data length and get a mbuf for UDP, IP, and possible 1377392e8407SRobert Watson * link-layer headers. Immediate slide the data pointer back forward 1378392e8407SRobert Watson * since we won't use that space at this layer. 1379df8bae1dSRodney W. Grimes */ 1380eb1b1807SGleb Smirnoff M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT); 1381e6ccd709SRobert Watson if (m == NULL) { 1382df8bae1dSRodney W. Grimes error = ENOBUFS; 138349b19bfcSBruce M Simpson goto release; 1384df8bae1dSRodney W. Grimes } 1385e6ccd709SRobert Watson m->m_data += max_linkhdr; 1386e6ccd709SRobert Watson m->m_len -= max_linkhdr; 1387392e8407SRobert Watson m->m_pkthdr.len -= max_linkhdr; 1388df8bae1dSRodney W. Grimes 1389df8bae1dSRodney W. Grimes /* 13903329b236SRobert Watson * Fill in mbuf with extended UDP header and addresses and length put 13913329b236SRobert Watson * into network format. 1392df8bae1dSRodney W. Grimes */ 1393df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 1394db4f9cc7SJonathan Lemon bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 139534fc9072SMichael Tuexen ui->ui_v = IPVERSION << 4; 1396e06e816fSKevin Lo ui->ui_pr = pr; 139790162a4eSIan Dowse ui->ui_src = laddr; 139890162a4eSIan Dowse ui->ui_dst = faddr; 139990162a4eSIan Dowse ui->ui_sport = lport; 140090162a4eSIan Dowse ui->ui_dport = fport; 1401db4f9cc7SJonathan Lemon ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 1402e06e816fSKevin Lo if (pr == IPPROTO_UDPLITE) { 1403e06e816fSKevin Lo struct udpcb *up; 1404e06e816fSKevin Lo uint16_t plen; 1405e06e816fSKevin Lo 1406e06e816fSKevin Lo up = intoudpcb(inp); 1407e06e816fSKevin Lo cscov = up->u_txcslen; 1408e06e816fSKevin Lo plen = (u_short)len + sizeof(struct udphdr); 1409e06e816fSKevin Lo if (cscov >= plen) 1410e06e816fSKevin Lo cscov = 0; 1411e06e816fSKevin Lo ui->ui_len = htons(plen); 1412e06e816fSKevin Lo ui->ui_ulen = htons(cscov); 1413e06e816fSKevin Lo /* 1414e06e816fSKevin Lo * For UDP-Lite, checksum coverage length of zero means 1415e06e816fSKevin Lo * the entire UDPLite packet is covered by the checksum. 1416e06e816fSKevin Lo */ 1417e06e816fSKevin Lo cscov_partial = (cscov == 0) ? 0 : 1; 141834fc9072SMichael Tuexen } 1419df8bae1dSRodney W. Grimes 1420b2828ad2SAndre Oppermann /* 1421b2828ad2SAndre Oppermann * Set the Don't Fragment bit in the IP header. 1422b2828ad2SAndre Oppermann */ 1423b2828ad2SAndre Oppermann if (inp->inp_flags & INP_DONTFRAG) { 1424b2828ad2SAndre Oppermann struct ip *ip; 14253329b236SRobert Watson 1426b2828ad2SAndre Oppermann ip = (struct ip *)&ui->ui_i; 14278f134647SGleb Smirnoff ip->ip_off |= htons(IP_DF); 1428b2828ad2SAndre Oppermann } 1429b2828ad2SAndre Oppermann 1430b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_DONTROUTE) 1431b5d47ff5SJohn-Mark Gurney ipflags |= IP_ROUTETOIF; 1432b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_BROADCAST) 1433b5d47ff5SJohn-Mark Gurney ipflags |= IP_ALLOWBROADCAST; 14346fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 14358afa2304SBruce M Simpson ipflags |= IP_SENDONES; 14368afa2304SBruce M Simpson 14371175d9d5SRobert Watson #ifdef MAC 14381175d9d5SRobert Watson mac_inpcb_create_mbuf(inp, m); 14391175d9d5SRobert Watson #endif 14401175d9d5SRobert Watson 1441df8bae1dSRodney W. Grimes /* 1442db4f9cc7SJonathan Lemon * Set up checksum and output datagram. 1443df8bae1dSRodney W. Grimes */ 1444e06e816fSKevin Lo ui->ui_sum = 0; 1445a485f139SMichael Tuexen if (pr == IPPROTO_UDPLITE) { 1446e06e816fSKevin Lo if (inp->inp_flags & INP_ONESBCAST) 1447e06e816fSKevin Lo faddr.s_addr = INADDR_BROADCAST; 1448a485f139SMichael Tuexen if (cscov_partial) { 1449e06e816fSKevin Lo if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0) 1450e06e816fSKevin Lo ui->ui_sum = 0xffff; 1451a485f139SMichael Tuexen } else { 1452a485f139SMichael Tuexen if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0) 1453a485f139SMichael Tuexen ui->ui_sum = 0xffff; 1454a485f139SMichael Tuexen } 1455a485f139SMichael Tuexen } else if (V_udp_cksum) { 14566fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 14578a538743SBruce M Simpson faddr.s_addr = INADDR_BROADCAST; 14588a538743SBruce M Simpson ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 1459e06e816fSKevin Lo htons((u_short)len + sizeof(struct udphdr) + pr)); 1460db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_UDP; 1461db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 1462e06e816fSKevin Lo } 14638f134647SGleb Smirnoff ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len); 1464ca98b82cSDavid Greenman ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 1465f584d74bSMichael Tuexen ((struct ip *)ui)->ip_tos = tos; /* XXX */ 1466026decb8SRobert Watson UDPSTAT_INC(udps_opackets); 1467cfa1ca9dSYoshinobu Inoue 14689d3ddf43SAdrian Chadd /* 14699d3ddf43SAdrian Chadd * Setup flowid / RSS information for outbound socket. 14709d3ddf43SAdrian Chadd * 14719d3ddf43SAdrian Chadd * Once the UDP code decides to set a flowid some other way, 14729d3ddf43SAdrian Chadd * this allows the flowid to be overridden by userland. 14739d3ddf43SAdrian Chadd */ 1474c2529042SHans Petter Selasky if (flowtype != M_HASHTYPE_NONE) { 14759d3ddf43SAdrian Chadd m->m_pkthdr.flowid = flowid; 1476c2529042SHans Petter Selasky M_HASHTYPE_SET(m, flowtype); 1477f9a6e8d7SBjoern A. Zeeb } 14780c325f53SAlexander V. Chernikov #if defined(ROUTE_MPATH) || defined(RSS) 14790c325f53SAlexander V. Chernikov else if (CALC_FLOWID_OUTBOUND_SENDTO) { 14808ad1a83bSAdrian Chadd uint32_t hash_val, hash_type; 14810c325f53SAlexander V. Chernikov 14820c325f53SAlexander V. Chernikov hash_val = fib4_calc_packet_hash(laddr, faddr, 14830c325f53SAlexander V. Chernikov lport, fport, pr, &hash_type); 14848ad1a83bSAdrian Chadd m->m_pkthdr.flowid = hash_val; 14858ad1a83bSAdrian Chadd M_HASHTYPE_SET(m, hash_type); 14868ad1a83bSAdrian Chadd } 14879d3ddf43SAdrian Chadd 14888ad1a83bSAdrian Chadd /* 14898ad1a83bSAdrian Chadd * Don't override with the inp cached flowid value. 14908ad1a83bSAdrian Chadd * 14918ad1a83bSAdrian Chadd * Depending upon the kind of send being done, the inp 14928ad1a83bSAdrian Chadd * flowid/flowtype values may actually not be appropriate 14938ad1a83bSAdrian Chadd * for this particular socket send. 14948ad1a83bSAdrian Chadd * 14958ad1a83bSAdrian Chadd * We should either leave the flowid at zero (which is what is 14968ad1a83bSAdrian Chadd * currently done) or set it to some software generated 14978ad1a83bSAdrian Chadd * hash value based on the packet contents. 14988ad1a83bSAdrian Chadd */ 14999d3ddf43SAdrian Chadd ipflags |= IP_NODEFAULTFLOWID; 15009d3ddf43SAdrian Chadd #endif /* RSS */ 15019d3ddf43SAdrian Chadd 15027bda9663SMichael Tuexen if (pr == IPPROTO_UDPLITE) 15037bda9663SMichael Tuexen UDPLITE_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u); 15047bda9663SMichael Tuexen else 150557f60867SMark Johnston UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u); 150684cc0778SGeorge V. Neville-Neil error = ip_output(m, inp->inp_options, 15072435e507SGleb Smirnoff INP_WLOCKED(inp) ? &inp->inp_route : NULL, ipflags, 15085d846453SSam Leffler inp->inp_moptions, inp); 15092435e507SGleb Smirnoff INP_UNLOCK(inp); 15102435e507SGleb Smirnoff NET_EPOCH_EXIT(et); 1511df8bae1dSRodney W. Grimes return (error); 1512df8bae1dSRodney W. Grimes 1513df8bae1dSRodney W. Grimes release: 15142435e507SGleb Smirnoff INP_UNLOCK(inp); 15152435e507SGleb Smirnoff NET_EPOCH_EXIT(et); 1516df8bae1dSRodney W. Grimes m_freem(m); 1517df8bae1dSRodney W. Grimes return (error); 1518df8bae1dSRodney W. Grimes } 1519df8bae1dSRodney W. Grimes 1520ac45e92fSRobert Watson static void 1521d0390e05SGarrett Wollman udp_abort(struct socket *so) 1522df8bae1dSRodney W. Grimes { 1523d0390e05SGarrett Wollman struct inpcb *inp; 1524e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1525df8bae1dSRodney W. Grimes 1526a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1527d0390e05SGarrett Wollman inp = sotoinpcb(so); 152814ba8addSRobert Watson KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 15298501a69cSRobert Watson INP_WLOCK(inp); 1530a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1531e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1532a152f8a3SRobert Watson in_pcbdisconnect(inp); 1533a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1534e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1535d0390e05SGarrett Wollman soisdisconnected(so); 1536a152f8a3SRobert Watson } 15378501a69cSRobert Watson INP_WUNLOCK(inp); 1538df8bae1dSRodney W. Grimes } 1539df8bae1dSRodney W. Grimes 1540d0390e05SGarrett Wollman static int 1541b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td) 1542d0390e05SGarrett Wollman { 1543630ba2c5SMatt Macy static uint32_t udp_flowid; 1544d0390e05SGarrett Wollman struct inpcb *inp; 1545e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1546277afaffSRobert Watson int error; 1547d0390e05SGarrett Wollman 1548a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1549d0390e05SGarrett Wollman inp = sotoinpcb(so); 155014ba8addSRobert Watson KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 1551cfa1ca9dSYoshinobu Inoue error = soreserve(so, udp_sendspace, udp_recvspace); 1552f24618aaSRobert Watson if (error) 15533329b236SRobert Watson return (error); 1554e06e816fSKevin Lo error = in_pcballoc(so, pcbinfo); 1555db0ac6deSCy Schubert if (error) 15563329b236SRobert Watson return (error); 1557cfa1ca9dSYoshinobu Inoue 155868b5629bSRobert Watson inp = sotoinpcb(so); 1559cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1560603724d3SBjoern A. Zeeb inp->inp_ip_ttl = V_ip_defttl; 1561630ba2c5SMatt Macy inp->inp_flowid = atomic_fetchadd_int(&udp_flowid, 1); 1562630ba2c5SMatt Macy inp->inp_flowtype = M_HASHTYPE_OPAQUE; 15636a9148feSBjoern A. Zeeb 15646a9148feSBjoern A. Zeeb error = udp_newudpcb(inp); 15656a9148feSBjoern A. Zeeb if (error) { 15666a9148feSBjoern A. Zeeb in_pcbdetach(inp); 15676a9148feSBjoern A. Zeeb in_pcbfree(inp); 15686a9148feSBjoern A. Zeeb return (error); 15696a9148feSBjoern A. Zeeb } 1570266f97b5SCy Schubert INP_WUNLOCK(inp); 1571db0ac6deSCy Schubert 1572c7c7ea4bSRandall Stewart return (0); 1573c7c7ea4bSRandall Stewart } 157479288c11SBjoern A. Zeeb #endif /* INET */ 1575c7c7ea4bSRandall Stewart 1576c7c7ea4bSRandall Stewart int 1577abb901c5SRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx) 1578c7c7ea4bSRandall Stewart { 1579c7c7ea4bSRandall Stewart struct inpcb *inp; 15806a9148feSBjoern A. Zeeb struct udpcb *up; 1581c7c7ea4bSRandall Stewart 158268b5629bSRobert Watson KASSERT(so->so_type == SOCK_DGRAM, 158368b5629bSRobert Watson ("udp_set_kernel_tunneling: !dgram")); 158468b5629bSRobert Watson inp = sotoinpcb(so); 158568b5629bSRobert Watson KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL")); 1586c7c7ea4bSRandall Stewart INP_WLOCK(inp); 15876a9148feSBjoern A. Zeeb up = intoudpcb(inp); 1588abb901c5SRandall Stewart if ((up->u_tun_func != NULL) || 1589abb901c5SRandall Stewart (up->u_icmp_func != NULL)) { 1590bbb0e3d9SRandall Stewart INP_WUNLOCK(inp); 1591bbb0e3d9SRandall Stewart return (EBUSY); 1592bbb0e3d9SRandall Stewart } 15936a9148feSBjoern A. Zeeb up->u_tun_func = f; 1594abb901c5SRandall Stewart up->u_icmp_func = i; 159581d3ec17SBryan Venteicher up->u_tun_ctx = ctx; 15968501a69cSRobert Watson INP_WUNLOCK(inp); 15973329b236SRobert Watson return (0); 1598df8bae1dSRodney W. Grimes } 1599d0390e05SGarrett Wollman 160079288c11SBjoern A. Zeeb #ifdef INET 1601d0390e05SGarrett Wollman static int 1602b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1603d0390e05SGarrett Wollman { 1604d0390e05SGarrett Wollman struct inpcb *inp; 1605e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1606f96603b5SMark Johnston struct sockaddr_in *sinp; 1607277afaffSRobert Watson int error; 1608d0390e05SGarrett Wollman 1609a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1610d0390e05SGarrett Wollman inp = sotoinpcb(so); 161114ba8addSRobert Watson KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 1612f161d294SMark Johnston 1613f96603b5SMark Johnston sinp = (struct sockaddr_in *)nam; 1614f96603b5SMark Johnston if (nam->sa_family != AF_INET) { 1615f96603b5SMark Johnston /* 1616f96603b5SMark Johnston * Preserve compatibility with old programs. 1617f96603b5SMark Johnston */ 1618f96603b5SMark Johnston if (nam->sa_family != AF_UNSPEC || 16193f1f6b6eSMichael Tuexen nam->sa_len < offsetof(struct sockaddr_in, sin_zero) || 1620f96603b5SMark Johnston sinp->sin_addr.s_addr != INADDR_ANY) 1621f161d294SMark Johnston return (EAFNOSUPPORT); 1622f96603b5SMark Johnston nam->sa_family = AF_INET; 1623f96603b5SMark Johnston } 1624f161d294SMark Johnston if (nam->sa_len != sizeof(struct sockaddr_in)) 1625f161d294SMark Johnston return (EINVAL); 1626f161d294SMark Johnston 16278501a69cSRobert Watson INP_WLOCK(inp); 1628e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1629b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, nam, td->td_ucred); 1630e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 16318501a69cSRobert Watson INP_WUNLOCK(inp); 16323329b236SRobert Watson return (error); 1633d0390e05SGarrett Wollman } 1634d0390e05SGarrett Wollman 1635a152f8a3SRobert Watson static void 1636a152f8a3SRobert Watson udp_close(struct socket *so) 1637a152f8a3SRobert Watson { 1638a152f8a3SRobert Watson struct inpcb *inp; 1639e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1640a152f8a3SRobert Watson 1641a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1642a152f8a3SRobert Watson inp = sotoinpcb(so); 1643a152f8a3SRobert Watson KASSERT(inp != NULL, ("udp_close: inp == NULL")); 16448501a69cSRobert Watson INP_WLOCK(inp); 1645a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1646e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1647a152f8a3SRobert Watson in_pcbdisconnect(inp); 1648a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1649e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1650a152f8a3SRobert Watson soisdisconnected(so); 1651a152f8a3SRobert Watson } 16528501a69cSRobert Watson INP_WUNLOCK(inp); 1653a152f8a3SRobert Watson } 1654a152f8a3SRobert Watson 1655d0390e05SGarrett Wollman static int 1656b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1657d0390e05SGarrett Wollman { 1658c1604fe4SGleb Smirnoff struct epoch_tracker et; 1659d0390e05SGarrett Wollman struct inpcb *inp; 1660e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 166175c13541SPoul-Henning Kamp struct sockaddr_in *sin; 1662e06e816fSKevin Lo int error; 1663d0390e05SGarrett Wollman 1664a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1665d0390e05SGarrett Wollman inp = sotoinpcb(so); 166614ba8addSRobert Watson KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 1667f161d294SMark Johnston 1668f161d294SMark Johnston sin = (struct sockaddr_in *)nam; 1669f161d294SMark Johnston if (sin->sin_family != AF_INET) 1670f161d294SMark Johnston return (EAFNOSUPPORT); 1671f161d294SMark Johnston if (sin->sin_len != sizeof(*sin)) 1672f161d294SMark Johnston return (EINVAL); 1673f161d294SMark Johnston 16748501a69cSRobert Watson INP_WLOCK(inp); 1675f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr != INADDR_ANY) { 16768501a69cSRobert Watson INP_WUNLOCK(inp); 16773329b236SRobert Watson return (EISCONN); 1678f76fcf6dSJeffrey Hsu } 1679b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1680b89e82ddSJamie Gritton if (error != 0) { 1681413628a7SBjoern A. Zeeb INP_WUNLOCK(inp); 1682b89e82ddSJamie Gritton return (error); 1683413628a7SBjoern A. Zeeb } 1684c1604fe4SGleb Smirnoff NET_EPOCH_ENTER(et); 1685e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1686db0ac6deSCy Schubert error = in_pcbconnect(inp, nam, td->td_ucred, true); 1687e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1688c1604fe4SGleb Smirnoff NET_EPOCH_EXIT(et); 16894cc20ab1SSeigo Tanimura if (error == 0) 1690df8bae1dSRodney W. Grimes soisconnected(so); 16918501a69cSRobert Watson INP_WUNLOCK(inp); 16923329b236SRobert Watson return (error); 1693df8bae1dSRodney W. Grimes } 1694d0390e05SGarrett Wollman 1695bc725eafSRobert Watson static void 1696d0390e05SGarrett Wollman udp_detach(struct socket *so) 1697d0390e05SGarrett Wollman { 1698d0390e05SGarrett Wollman struct inpcb *inp; 16996a9148feSBjoern A. Zeeb struct udpcb *up; 1700d0390e05SGarrett Wollman 1701d0390e05SGarrett Wollman inp = sotoinpcb(so); 170214ba8addSRobert Watson KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1703a152f8a3SRobert Watson KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1704a152f8a3SRobert Watson ("udp_detach: not disconnected")); 17058501a69cSRobert Watson INP_WLOCK(inp); 17066a9148feSBjoern A. Zeeb up = intoudpcb(inp); 17076a9148feSBjoern A. Zeeb KASSERT(up != NULL, ("%s: up == NULL", __func__)); 17086a9148feSBjoern A. Zeeb inp->inp_ppcb = NULL; 1709d0390e05SGarrett Wollman in_pcbdetach(inp); 171014ba8addSRobert Watson in_pcbfree(inp); 17116a9148feSBjoern A. Zeeb udp_discardcb(up); 1712d0390e05SGarrett Wollman } 1713d0390e05SGarrett Wollman 1714d0390e05SGarrett Wollman static int 1715d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 1716d0390e05SGarrett Wollman { 1717d0390e05SGarrett Wollman struct inpcb *inp; 1718e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1719d0390e05SGarrett Wollman 1720a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1721d0390e05SGarrett Wollman inp = sotoinpcb(so); 172214ba8addSRobert Watson KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 17238501a69cSRobert Watson INP_WLOCK(inp); 1724f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr == INADDR_ANY) { 17258501a69cSRobert Watson INP_WUNLOCK(inp); 17263329b236SRobert Watson return (ENOTCONN); 1727f76fcf6dSJeffrey Hsu } 1728e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1729df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 1730df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 1731e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1732d45e4f99SMaxim Konovalov SOCK_LOCK(so); 1733d45e4f99SMaxim Konovalov so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1734d45e4f99SMaxim Konovalov SOCK_UNLOCK(so); 17358501a69cSRobert Watson INP_WUNLOCK(inp); 17363329b236SRobert Watson return (0); 1737df8bae1dSRodney W. Grimes } 1738df8bae1dSRodney W. Grimes 1739d0390e05SGarrett Wollman static int 174057bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1741b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1742d0390e05SGarrett Wollman { 1743d0390e05SGarrett Wollman struct inpcb *inp; 1744f161d294SMark Johnston int error; 1745d0390e05SGarrett Wollman 1746d0390e05SGarrett Wollman inp = sotoinpcb(so); 174714ba8addSRobert Watson KASSERT(inp != NULL, ("udp_send: inp == NULL")); 1748f161d294SMark Johnston 1749f161d294SMark Johnston if (addr != NULL) { 1750f161d294SMark Johnston error = 0; 1751f161d294SMark Johnston if (addr->sa_family != AF_INET) 1752f161d294SMark Johnston error = EAFNOSUPPORT; 1753f161d294SMark Johnston else if (addr->sa_len != sizeof(struct sockaddr_in)) 1754f161d294SMark Johnston error = EINVAL; 1755f161d294SMark Johnston if (__predict_false(error != 0)) { 1756f161d294SMark Johnston m_freem(control); 1757f161d294SMark Johnston m_freem(m); 1758f161d294SMark Johnston return (error); 1759f161d294SMark Johnston } 1760f161d294SMark Johnston } 1761a9839c4aSBjoern A. Zeeb return (udp_output(inp, m, addr, control, td, flags)); 1762d0390e05SGarrett Wollman } 176379288c11SBjoern A. Zeeb #endif /* INET */ 1764d0390e05SGarrett Wollman 176576429de4SYoshinobu Inoue int 1766d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 1767d0390e05SGarrett Wollman { 1768d0390e05SGarrett Wollman struct inpcb *inp; 1769d0390e05SGarrett Wollman 1770d0390e05SGarrett Wollman inp = sotoinpcb(so); 177114ba8addSRobert Watson KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 17728501a69cSRobert Watson INP_WLOCK(inp); 1773d0390e05SGarrett Wollman socantsendmore(so); 17748501a69cSRobert Watson INP_WUNLOCK(inp); 17753329b236SRobert Watson return (0); 1776d0390e05SGarrett Wollman } 1777d0390e05SGarrett Wollman 177879288c11SBjoern A. Zeeb #ifdef INET 1779d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 1780756d52a1SPoul-Henning Kamp .pru_abort = udp_abort, 1781756d52a1SPoul-Henning Kamp .pru_attach = udp_attach, 1782756d52a1SPoul-Henning Kamp .pru_bind = udp_bind, 1783756d52a1SPoul-Henning Kamp .pru_connect = udp_connect, 1784756d52a1SPoul-Henning Kamp .pru_control = in_control, 1785756d52a1SPoul-Henning Kamp .pru_detach = udp_detach, 1786756d52a1SPoul-Henning Kamp .pru_disconnect = udp_disconnect, 178754d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1788756d52a1SPoul-Henning Kamp .pru_send = udp_send, 17895df3e839SRobert Watson .pru_soreceive = soreceive_dgram, 179059b8854eSRobert Watson .pru_sosend = sosend_dgram, 1791756d52a1SPoul-Henning Kamp .pru_shutdown = udp_shutdown, 179254d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1793a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1794a152f8a3SRobert Watson .pru_close = udp_close, 1795d0390e05SGarrett Wollman }; 179679288c11SBjoern A. Zeeb #endif /* INET */ 1797