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. 20fbbd9655SWarner Losh * 3. 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 4279288c11SBjoern A. Zeeb #include "opt_inet.h" 43cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 44f5514f08SRobert Watson #include "opt_ipsec.h" 459d3ddf43SAdrian Chadd #include "opt_rss.h" 46cfa1ca9dSYoshinobu Inoue 47df8bae1dSRodney W. Grimes #include <sys/param.h> 48960ed29cSSeigo Tanimura #include <sys/domain.h> 494f590175SPaul Saab #include <sys/eventhandler.h> 50960ed29cSSeigo Tanimura #include <sys/jail.h> 51b110a8a2SGarrett Wollman #include <sys/kernel.h> 52960ed29cSSeigo Tanimura #include <sys/lock.h> 53df8bae1dSRodney W. Grimes #include <sys/malloc.h> 54df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 55acd3428bSRobert Watson #include <sys/priv.h> 56490d50b6SBrian Feldman #include <sys/proc.h> 57df8bae1dSRodney W. Grimes #include <sys/protosw.h> 5857f60867SMark Johnston #include <sys/sdt.h> 59960ed29cSSeigo Tanimura #include <sys/signalvar.h> 60df8bae1dSRodney W. Grimes #include <sys/socket.h> 61df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 62960ed29cSSeigo Tanimura #include <sys/sx.h> 63b5e8ce9fSBruce Evans #include <sys/sysctl.h> 64816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 65f5514f08SRobert Watson #include <sys/systm.h> 668781d8e9SBruce Evans 6769c2d429SJeff Roberson #include <vm/uma.h> 68df8bae1dSRodney W. Grimes 69df8bae1dSRodney W. Grimes #include <net/if.h> 7076039bc8SGleb Smirnoff #include <net/if_var.h> 71df8bae1dSRodney W. Grimes #include <net/route.h> 72b2bdc62aSAdrian Chadd #include <net/rss_config.h> 73df8bae1dSRodney W. Grimes 74df8bae1dSRodney W. Grimes #include <netinet/in.h> 7557f60867SMark Johnston #include <netinet/in_kdtrace.h> 76960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 77f5514f08SRobert Watson #include <netinet/in_systm.h> 78960ed29cSSeigo Tanimura #include <netinet/in_var.h> 79df8bae1dSRodney W. Grimes #include <netinet/ip.h> 80cfa1ca9dSYoshinobu Inoue #ifdef INET6 81cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 82cfa1ca9dSYoshinobu Inoue #endif 83960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h> 84960ed29cSSeigo Tanimura #include <netinet/icmp_var.h> 85df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 86ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 87cfa1ca9dSYoshinobu Inoue #ifdef INET6 88cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 89cfa1ca9dSYoshinobu Inoue #endif 90df8bae1dSRodney W. Grimes #include <netinet/udp.h> 91df8bae1dSRodney W. Grimes #include <netinet/udp_var.h> 92e06e816fSKevin Lo #include <netinet/udplite.h> 938ad1a83bSAdrian Chadd #include <netinet/in_rss.h> 94df8bae1dSRodney W. Grimes 95fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 96b9234fafSSam Leffler 97db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 98db4f9cc7SJonathan Lemon 99aed55708SRobert Watson #include <security/mac/mac_framework.h> 100aed55708SRobert Watson 101df8bae1dSRodney W. Grimes /* 102e06e816fSKevin Lo * UDP and UDP-Lite protocols implementation. 103df8bae1dSRodney W. Grimes * Per RFC 768, August, 1980. 104e06e816fSKevin Lo * Per RFC 3828, July, 2004. 105df8bae1dSRodney W. Grimes */ 10674eb3236SWarner Losh 10774eb3236SWarner Losh /* 1083329b236SRobert Watson * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums 1093329b236SRobert Watson * removes the only data integrity mechanism for packets and malformed 110f5514f08SRobert Watson * packets that would otherwise be discarded due to bad checksums, and may 111f5514f08SRobert Watson * cause problems (especially for NFS data blocks). 11274eb3236SWarner Losh */ 11340b676beSBjoern A. Zeeb VNET_DEFINE(int, udp_cksum) = 1; 1146df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW, 11540b676beSBjoern A. Zeeb &VNET_NAME(udp_cksum), 0, "compute udp checksum"); 116df8bae1dSRodney W. Grimes 117afdb4274SRobert Watson int udp_log_in_vain = 0; 118816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 119afdb4274SRobert Watson &udp_log_in_vain, 0, "Log all incoming UDP packets"); 120816a3d83SPoul-Henning Kamp 12182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, udp_blackhole) = 0; 1226df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 123eddfbb76SRobert Watson &VNET_NAME(udp_blackhole), 0, 1243329b236SRobert Watson "Do not send port unreachables for refused connects"); 12516f7f31fSGeoff Rehmet 12623424a20SRyan Stone static VNET_DEFINE(int, udp_require_l2_bcast) = 0; 1279da85a91SMarko Zec #define V_udp_require_l2_bcast VNET(udp_require_l2_bcast) 12841029db1SRyan Stone SYSCTL_INT(_net_inet_udp, OID_AUTO, require_l2_bcast, CTLFLAG_VNET | CTLFLAG_RW, 12941029db1SRyan Stone &VNET_NAME(udp_require_l2_bcast), 0, 13041029db1SRyan Stone "Only treat packets sent to an L2 broadcast address as broadcast packets"); 13141029db1SRyan Stone 13243bbb6aaSRobert Watson u_long udp_sendspace = 9216; /* really max datagram size */ 13343bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 13443bbb6aaSRobert Watson &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 13543bbb6aaSRobert Watson 13643bbb6aaSRobert Watson u_long udp_recvspace = 40 * (1024 + 13743bbb6aaSRobert Watson #ifdef INET6 13843bbb6aaSRobert Watson sizeof(struct sockaddr_in6) 13943bbb6aaSRobert Watson #else 14043bbb6aaSRobert Watson sizeof(struct sockaddr_in) 14143bbb6aaSRobert Watson #endif 142e62b9bcaSSergey Kandaurov ); /* 40 1K datagrams */ 14343bbb6aaSRobert Watson 14443bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 14543bbb6aaSRobert Watson &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 14643bbb6aaSRobert Watson 147eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, udb); /* from udp_var.h */ 148eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, udbinfo); 149e06e816fSKevin Lo VNET_DEFINE(struct inpcbhead, ulitecb); 150e06e816fSKevin Lo VNET_DEFINE(struct inpcbinfo, ulitecbinfo); 1513e288e62SDimitry Andric static VNET_DEFINE(uma_zone_t, udpcb_zone); 1521e77c105SRobert Watson #define V_udpcb_zone VNET(udpcb_zone) 15315bd2b43SDavid Greenman 15415bd2b43SDavid Greenman #ifndef UDBHASHSIZE 155e2ed8f35SAlexander Motin #define UDBHASHSIZE 128 15615bd2b43SDavid Greenman #endif 15715bd2b43SDavid Greenman 1585b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat); /* from udp_var.h */ 1595b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(udpstat); 1605b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat, 1615b7cb97cSAndrey V. Elsukov udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 162f2ea20e6SGarrett Wollman 1635b7cb97cSAndrey V. Elsukov #ifdef VIMAGE 1645b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(udpstat); 1655b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */ 16679288c11SBjoern A. Zeeb #ifdef INET 167bc725eafSRobert Watson static void udp_detach(struct socket *so); 1684d77a549SAlfred Perlstein static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 1694d77a549SAlfred Perlstein struct mbuf *, struct thread *); 17079288c11SBjoern A. Zeeb #endif 17179288c11SBjoern A. Zeeb 1724f590175SPaul Saab static void 1734f590175SPaul Saab udp_zone_change(void *tag) 1744f590175SPaul Saab { 1754f590175SPaul Saab 176603724d3SBjoern A. Zeeb uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 1776a9148feSBjoern A. Zeeb uma_zone_set_max(V_udpcb_zone, maxsockets); 1784f590175SPaul Saab } 1794f590175SPaul Saab 180d915b280SStephan Uphoff static int 181d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags) 182d915b280SStephan Uphoff { 183af1ee11dSRobert Watson struct inpcb *inp; 18408651e1fSJohn Baldwin 185af1ee11dSRobert Watson inp = mem; 186d915b280SStephan Uphoff INP_LOCK_INIT(inp, "inp", "udpinp"); 187d915b280SStephan Uphoff return (0); 188d915b280SStephan Uphoff } 189d915b280SStephan Uphoff 190e06e816fSKevin Lo static int 191e06e816fSKevin Lo udplite_inpcb_init(void *mem, int size, int flags) 192e06e816fSKevin Lo { 193e06e816fSKevin Lo struct inpcb *inp; 194e06e816fSKevin Lo 195e06e816fSKevin Lo inp = mem; 196e06e816fSKevin Lo INP_LOCK_INIT(inp, "inp", "udpliteinp"); 197e06e816fSKevin Lo return (0); 198e06e816fSKevin Lo } 199e06e816fSKevin Lo 200df8bae1dSRodney W. Grimes void 201af1ee11dSRobert Watson udp_init(void) 202df8bae1dSRodney W. Grimes { 203af1ee11dSRobert Watson 2048ad1a83bSAdrian Chadd /* 2058ad1a83bSAdrian Chadd * For now default to 2-tuple UDP hashing - until the fragment 2068ad1a83bSAdrian Chadd * reassembly code can also update the flowid. 2078ad1a83bSAdrian Chadd * 2088ad1a83bSAdrian Chadd * Once we can calculate the flowid that way and re-establish 2098ad1a83bSAdrian Chadd * a 4-tuple, flip this to 4-tuple. 2108ad1a83bSAdrian Chadd */ 2119bcd427bSRobert Watson in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE, 212a8da5dd6SCraig Rodrigues "udp_inpcb", udp_inpcb_init, NULL, 0, 21352cd27cbSRobert Watson IPI_HASHFIELDS_2TUPLE); 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 226e06e816fSKevin Lo in_pcbinfo_init(&V_ulitecbinfo, "udplite", &V_ulitecb, UDBHASHSIZE, 227e06e816fSKevin Lo UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init, NULL, 228a8da5dd6SCraig Rodrigues 0, IPI_HASHFIELDS_2TUPLE); 229e06e816fSKevin Lo } 230e06e816fSKevin Lo 231315e3e38SRobert Watson /* 232315e3e38SRobert Watson * Kernel module interface for updating udpstat. The argument is an index 233315e3e38SRobert Watson * into udpstat treated as an array of u_long. While this encodes the 234315e3e38SRobert Watson * general layout of udpstat into the caller, it doesn't encode its location, 235315e3e38SRobert Watson * so that future changes to add, for example, per-CPU stats support won't 236315e3e38SRobert Watson * cause binary compatibility problems for kernel modules. 237315e3e38SRobert Watson */ 238315e3e38SRobert Watson void 239315e3e38SRobert Watson kmod_udpstat_inc(int statnum) 240315e3e38SRobert Watson { 241315e3e38SRobert Watson 2425b7cb97cSAndrey V. Elsukov counter_u64_add(VNET(udpstat)[statnum], 1); 243315e3e38SRobert Watson } 244315e3e38SRobert Watson 2456a9148feSBjoern A. Zeeb int 2466a9148feSBjoern A. Zeeb udp_newudpcb(struct inpcb *inp) 2476a9148feSBjoern A. Zeeb { 2486a9148feSBjoern A. Zeeb struct udpcb *up; 2496a9148feSBjoern A. Zeeb 2506a9148feSBjoern A. Zeeb up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO); 2516a9148feSBjoern A. Zeeb if (up == NULL) 2526a9148feSBjoern A. Zeeb return (ENOBUFS); 2536a9148feSBjoern A. Zeeb inp->inp_ppcb = up; 2546a9148feSBjoern A. Zeeb return (0); 2556a9148feSBjoern A. Zeeb } 2566a9148feSBjoern A. Zeeb 2576a9148feSBjoern A. Zeeb void 2586a9148feSBjoern A. Zeeb udp_discardcb(struct udpcb *up) 2596a9148feSBjoern A. Zeeb { 2606a9148feSBjoern A. Zeeb 2616a9148feSBjoern A. Zeeb uma_zfree(V_udpcb_zone, up); 2626a9148feSBjoern A. Zeeb } 2636a9148feSBjoern A. Zeeb 264bc29160dSMarko Zec #ifdef VIMAGE 2653f58662dSBjoern A. Zeeb static void 2663f58662dSBjoern A. Zeeb udp_destroy(void *unused __unused) 267bc29160dSMarko Zec { 268bc29160dSMarko Zec 2699bcd427bSRobert Watson in_pcbinfo_destroy(&V_udbinfo); 270391dab1cSBjoern A. Zeeb uma_zdestroy(V_udpcb_zone); 271bc29160dSMarko Zec } 2723f58662dSBjoern A. Zeeb VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL); 273e06e816fSKevin Lo 2743f58662dSBjoern A. Zeeb static void 2753f58662dSBjoern A. Zeeb udplite_destroy(void *unused __unused) 276e06e816fSKevin Lo { 277e06e816fSKevin Lo 278e06e816fSKevin Lo in_pcbinfo_destroy(&V_ulitecbinfo); 279e06e816fSKevin Lo } 2803f58662dSBjoern A. Zeeb VNET_SYSUNINIT(udplite, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udplite_destroy, 2813f58662dSBjoern A. Zeeb NULL); 282bc29160dSMarko Zec #endif 283bc29160dSMarko Zec 28479288c11SBjoern A. Zeeb #ifdef INET 28543bbb6aaSRobert Watson /* 28643bbb6aaSRobert Watson * Subroutine of udp_input(), which appends the provided mbuf chain to the 28743bbb6aaSRobert Watson * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 28843bbb6aaSRobert Watson * contains the source address. If the socket ends up being an IPv6 socket, 28943bbb6aaSRobert Watson * udp_append() will convert to a sockaddr_in6 before passing the address 29043bbb6aaSRobert Watson * into the socket code. 291c0d1be08SRandall Stewart * 292c0d1be08SRandall Stewart * In the normal case udp_append() will return 0, indicating that you 293c0d1be08SRandall Stewart * must unlock the inp. However if a tunneling protocol is in place we increment 294c0d1be08SRandall Stewart * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we 295c0d1be08SRandall Stewart * then decrement the reference count. If the inp_rele returns 1, indicating the 296c0d1be08SRandall Stewart * inp is gone, we return that to the caller to tell them *not* to unlock 297c0d1be08SRandall Stewart * the inp. In the case of multi-cast this will cause the distribution 298c0d1be08SRandall Stewart * to stop (though most tunneling protocols known currently do *not* use 299c0d1be08SRandall Stewart * multicast). 30043bbb6aaSRobert Watson */ 301c0d1be08SRandall Stewart static int 30243bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off, 30343bbb6aaSRobert Watson struct sockaddr_in *udp_in) 30443bbb6aaSRobert Watson { 30543bbb6aaSRobert Watson struct sockaddr *append_sa; 30643bbb6aaSRobert Watson struct socket *so; 307*dce33a45SErmal Luçi struct mbuf *tmpopts, *opts = NULL; 30843bbb6aaSRobert Watson #ifdef INET6 30943bbb6aaSRobert Watson struct sockaddr_in6 udp_in6; 31043bbb6aaSRobert Watson #endif 3117b495c44SVANHULLEBUS Yvan struct udpcb *up; 31243bbb6aaSRobert Watson 313fa046d87SRobert Watson INP_LOCK_ASSERT(inp); 31443bbb6aaSRobert Watson 31579bb84fbSEdward Tomasz Napierala /* 31679bb84fbSEdward Tomasz Napierala * Engage the tunneling protocol. 31779bb84fbSEdward Tomasz Napierala */ 31879bb84fbSEdward Tomasz Napierala up = intoudpcb(inp); 31979bb84fbSEdward Tomasz Napierala if (up->u_tun_func != NULL) { 320c0d1be08SRandall Stewart in_pcbref(inp); 321c0d1be08SRandall Stewart INP_RUNLOCK(inp); 322*dce33a45SErmal Luçi (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&udp_in[0], 32381d3ec17SBryan Venteicher up->u_tun_ctx); 324c0d1be08SRandall Stewart INP_RLOCK(inp); 325c0d1be08SRandall Stewart return (in_pcbrele_rlocked(inp)); 32679bb84fbSEdward Tomasz Napierala } 32779bb84fbSEdward Tomasz Napierala 32879bb84fbSEdward Tomasz Napierala off += sizeof(struct udphdr); 32979bb84fbSEdward Tomasz Napierala 330fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 33143bbb6aaSRobert Watson /* Check AH/ESP integrity. */ 332fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 333fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, n, inp) != 0) { 33443bbb6aaSRobert Watson m_freem(n); 335c0d1be08SRandall Stewart return (0); 33643bbb6aaSRobert Watson } 337fcf59617SAndrey V. Elsukov if (up->u_flags & UF_ESPINUDP) {/* IPSec UDP encaps. */ 338fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 339fcf59617SAndrey V. Elsukov UDPENCAP_INPUT(n, off, AF_INET) != 0) 340fcf59617SAndrey V. Elsukov return (0); /* Consumed. */ 3417b495c44SVANHULLEBUS Yvan } 34243bbb6aaSRobert Watson #endif /* IPSEC */ 34343bbb6aaSRobert Watson #ifdef MAC 34430d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, n) != 0) { 34543bbb6aaSRobert Watson m_freem(n); 346c0d1be08SRandall Stewart return (0); 34743bbb6aaSRobert Watson } 34879288c11SBjoern A. Zeeb #endif /* MAC */ 34943bbb6aaSRobert Watson if (inp->inp_flags & INP_CONTROLOPTS || 35043bbb6aaSRobert Watson inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 35143bbb6aaSRobert Watson #ifdef INET6 3529a38ba81SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6) 35348d48eb9SBjoern A. Zeeb (void)ip6_savecontrol_v4(inp, n, &opts, NULL); 3549a38ba81SBjoern A. Zeeb else 35579288c11SBjoern A. Zeeb #endif /* INET6 */ 35643bbb6aaSRobert Watson ip_savecontrol(inp, &opts, ip, n); 35743bbb6aaSRobert Watson } 358*dce33a45SErmal Luçi if ((inp->inp_vflag & INP_IPV4) && (inp->inp_flags2 & INP_ORIGDSTADDR)) { 359*dce33a45SErmal Luçi tmpopts = sbcreatecontrol((caddr_t)&udp_in[1], 360*dce33a45SErmal Luçi sizeof(struct sockaddr_in), IP_ORIGDSTADDR, IPPROTO_IP); 361*dce33a45SErmal Luçi if (tmpopts) { 362*dce33a45SErmal Luçi if (opts) { 363*dce33a45SErmal Luçi tmpopts->m_next = opts; 364*dce33a45SErmal Luçi opts = tmpopts; 365*dce33a45SErmal Luçi } else 366*dce33a45SErmal Luçi opts = tmpopts; 367*dce33a45SErmal Luçi } 368*dce33a45SErmal Luçi } 36943bbb6aaSRobert Watson #ifdef INET6 37043bbb6aaSRobert Watson if (inp->inp_vflag & INP_IPV6) { 37143bbb6aaSRobert Watson bzero(&udp_in6, sizeof(udp_in6)); 37243bbb6aaSRobert Watson udp_in6.sin6_len = sizeof(udp_in6); 37343bbb6aaSRobert Watson udp_in6.sin6_family = AF_INET6; 374*dce33a45SErmal Luçi in6_sin_2_v4mapsin6(&udp_in[0], &udp_in6); 37543bbb6aaSRobert Watson append_sa = (struct sockaddr *)&udp_in6; 37643bbb6aaSRobert Watson } else 37779288c11SBjoern A. Zeeb #endif /* INET6 */ 378*dce33a45SErmal Luçi append_sa = (struct sockaddr *)&udp_in[0]; 37943bbb6aaSRobert Watson m_adj(n, off); 38043bbb6aaSRobert Watson 38143bbb6aaSRobert Watson so = inp->inp_socket; 38243bbb6aaSRobert Watson SOCKBUF_LOCK(&so->so_rcv); 38343bbb6aaSRobert Watson if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 38443bbb6aaSRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 38543bbb6aaSRobert Watson m_freem(n); 38643bbb6aaSRobert Watson if (opts) 38743bbb6aaSRobert Watson m_freem(opts); 388026decb8SRobert Watson UDPSTAT_INC(udps_fullsock); 38943bbb6aaSRobert Watson } else 39043bbb6aaSRobert Watson sorwakeup_locked(so); 391c0d1be08SRandall Stewart return (0); 39243bbb6aaSRobert Watson } 39343bbb6aaSRobert Watson 3948f5a8818SKevin Lo int 3958f5a8818SKevin Lo udp_input(struct mbuf **mp, int *offp, int proto) 396df8bae1dSRodney W. Grimes { 3973329b236SRobert Watson struct ip *ip; 3983329b236SRobert Watson struct udphdr *uh; 39971498f30SBruce M Simpson struct ifnet *ifp; 4003329b236SRobert Watson struct inpcb *inp; 4018f134647SGleb Smirnoff uint16_t len, ip_len; 402e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 403df8bae1dSRodney W. Grimes struct ip save_ip; 404*dce33a45SErmal Luçi struct sockaddr_in udp_in[2]; 4058f5a8818SKevin Lo struct mbuf *m; 4060b4ae859SGleb Smirnoff struct m_tag *fwd_tag; 4078f5a8818SKevin Lo int cscov_partial, iphlen; 408df8bae1dSRodney W. Grimes 4098f5a8818SKevin Lo m = *mp; 4108f5a8818SKevin Lo iphlen = *offp; 41171498f30SBruce M Simpson ifp = m->m_pkthdr.rcvif; 4128f5a8818SKevin Lo *mp = NULL; 413026decb8SRobert Watson UDPSTAT_INC(udps_ipackets); 414df8bae1dSRodney W. Grimes 415df8bae1dSRodney W. Grimes /* 4163329b236SRobert Watson * Strip IP options, if any; should skip this, make available to 4173329b236SRobert Watson * user, and use on returned packets, but we don't yet have a way to 4183329b236SRobert Watson * check the checksum with options still present. 419df8bae1dSRodney W. Grimes */ 420df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 421105bd211SGleb Smirnoff ip_stripoptions(m); 422df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 423df8bae1dSRodney W. Grimes } 424df8bae1dSRodney W. Grimes 425df8bae1dSRodney W. Grimes /* 426df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 427df8bae1dSRodney W. Grimes */ 428df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 429df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 430d1b18731SKevin Lo if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) { 431026decb8SRobert Watson UDPSTAT_INC(udps_hdrops); 4328f5a8818SKevin Lo return (IPPROTO_DONE); 433df8bae1dSRodney W. Grimes } 434df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 435df8bae1dSRodney W. Grimes } 436df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 4378f5a8818SKevin Lo cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0; 438df8bae1dSRodney W. Grimes 4393329b236SRobert Watson /* 4403329b236SRobert Watson * Destination port of 0 is illegal, based on RFC768. 4413329b236SRobert Watson */ 442686cdd19SJun-ichiro itojun Hagino if (uh->uh_dport == 0) 443f76fcf6dSJeffrey Hsu goto badunlocked; 444686cdd19SJun-ichiro itojun Hagino 445df8bae1dSRodney W. Grimes /* 4463329b236SRobert Watson * Construct sockaddr format source address. Stuff source address 4473329b236SRobert Watson * and datagram in user buffer. 448b9234fafSSam Leffler */ 449*dce33a45SErmal Luçi bzero(&udp_in[0], sizeof(struct sockaddr_in) * 2); 450*dce33a45SErmal Luçi udp_in[0].sin_len = sizeof(struct sockaddr_in); 451*dce33a45SErmal Luçi udp_in[0].sin_family = AF_INET; 452*dce33a45SErmal Luçi udp_in[0].sin_port = uh->uh_sport; 453*dce33a45SErmal Luçi udp_in[0].sin_addr = ip->ip_src; 454*dce33a45SErmal Luçi udp_in[1].sin_len = sizeof(struct sockaddr_in); 455*dce33a45SErmal Luçi udp_in[1].sin_family = AF_INET; 456*dce33a45SErmal Luçi udp_in[1].sin_port = uh->uh_dport; 457*dce33a45SErmal Luçi udp_in[1].sin_addr = ip->ip_dst; 458b9234fafSSam Leffler 459b9234fafSSam Leffler /* 460af1ee11dSRobert Watson * Make mbuf data length reflect UDP length. If not enough data to 461af1ee11dSRobert Watson * reflect UDP length, drop. 462df8bae1dSRodney W. Grimes */ 463df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 4648ad458a4SGleb Smirnoff ip_len = ntohs(ip->ip_len) - iphlen; 4650f4a0366SMichael Tuexen if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) { 466e06e816fSKevin Lo /* Zero means checksum over the complete packet. */ 4670f4a0366SMichael Tuexen if (len == 0) 468e06e816fSKevin Lo len = ip_len; 469e06e816fSKevin Lo cscov_partial = 0; 470e06e816fSKevin Lo } 4718f134647SGleb Smirnoff if (ip_len != len) { 4728f134647SGleb Smirnoff if (len > ip_len || len < sizeof(struct udphdr)) { 473026decb8SRobert Watson UDPSTAT_INC(udps_badlen); 474f76fcf6dSJeffrey Hsu goto badunlocked; 475df8bae1dSRodney W. Grimes } 4768f5a8818SKevin Lo if (proto == IPPROTO_UDP) 4778f134647SGleb Smirnoff m_adj(m, len - ip_len); 478df8bae1dSRodney W. Grimes } 4793329b236SRobert Watson 480df8bae1dSRodney W. Grimes /* 4813329b236SRobert Watson * Save a copy of the IP header in case we want restore it for 4823329b236SRobert Watson * sending an ICMP error message in response. 483df8bae1dSRodney W. Grimes */ 484603724d3SBjoern A. Zeeb if (!V_udp_blackhole) 485df8bae1dSRodney W. Grimes save_ip = *ip; 486cce418d3SMatt Jacob else 487cce418d3SMatt Jacob memset(&save_ip, 0, sizeof(save_ip)); 488df8bae1dSRodney W. Grimes 489df8bae1dSRodney W. Grimes /* 490df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 491df8bae1dSRodney W. Grimes */ 4926dfab5b1SGarrett Wollman if (uh->uh_sum) { 49339629c92SDavid Malone u_short uh_sum; 49439629c92SDavid Malone 495e06e816fSKevin Lo if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) && 496e06e816fSKevin Lo !cscov_partial) { 497db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 49839629c92SDavid Malone uh_sum = m->m_pkthdr.csum_data; 499db4f9cc7SJonathan Lemon else 50039629c92SDavid Malone uh_sum = in_pseudo(ip->ip_src.s_addr, 501506f4949SRuslan Ermilov ip->ip_dst.s_addr, htonl((u_short)len + 5028f5a8818SKevin Lo m->m_pkthdr.csum_data + proto)); 50339629c92SDavid Malone uh_sum ^= 0xffff; 504db4f9cc7SJonathan Lemon } else { 505cb342100SHajimu UMEMOTO char b[9]; 506af1ee11dSRobert Watson 507cb342100SHajimu UMEMOTO bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 5086effc713SDoug Rabson bzero(((struct ipovly *)ip)->ih_x1, 9); 5098f5a8818SKevin Lo ((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ? 510e06e816fSKevin Lo uh->uh_ulen : htons(ip_len); 51139629c92SDavid Malone uh_sum = in_cksum(m, len + sizeof (struct ip)); 512cb342100SHajimu UMEMOTO bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 513db4f9cc7SJonathan Lemon } 51439629c92SDavid Malone if (uh_sum) { 515026decb8SRobert Watson UDPSTAT_INC(udps_badsum); 516df8bae1dSRodney W. Grimes m_freem(m); 5178f5a8818SKevin Lo return (IPPROTO_DONE); 518df8bae1dSRodney W. Grimes } 519c6d81a34SMichael Tuexen } else { 520c6d81a34SMichael Tuexen if (proto == IPPROTO_UDP) { 521026decb8SRobert Watson UDPSTAT_INC(udps_nosum); 522c6d81a34SMichael Tuexen } else { 523c6d81a34SMichael Tuexen /* UDPLite requires a checksum */ 524c6d81a34SMichael Tuexen /* XXX: What is the right UDPLite MIB counter here? */ 525c6d81a34SMichael Tuexen m_freem(m); 526c6d81a34SMichael Tuexen return (IPPROTO_DONE); 527c6d81a34SMichael Tuexen } 528c6d81a34SMichael Tuexen } 529df8bae1dSRodney W. Grimes 530a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(proto); 531df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 5329da85a91SMarko Zec ((!V_udp_require_l2_bcast || m->m_flags & M_BCAST) && 53341029db1SRyan Stone in_broadcast(ip->ip_dst, ifp))) { 53482c23ebaSBill Fenner struct inpcb *last; 535e06e816fSKevin Lo struct inpcbhead *pcblist; 53671498f30SBruce M Simpson struct ip_moptions *imo; 5373329b236SRobert Watson 538e06e816fSKevin Lo INP_INFO_RLOCK(pcbinfo); 539a86e5c96SBjoern A. Zeeb pcblist = udp_get_pcblist(proto); 540df8bae1dSRodney W. Grimes last = NULL; 541e06e816fSKevin Lo LIST_FOREACH(inp, pcblist, inp_list) { 5429c1df695SRobert Watson if (inp->inp_lport != uh->uh_dport) 543f76fcf6dSJeffrey Hsu continue; 544cfa1ca9dSYoshinobu Inoue #ifdef INET6 545369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 5469c1df695SRobert Watson continue; 547cfa1ca9dSYoshinobu Inoue #endif 54871498f30SBruce M Simpson if (inp->inp_laddr.s_addr != INADDR_ANY && 54971498f30SBruce M Simpson inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 5509c1df695SRobert Watson continue; 55171498f30SBruce M Simpson if (inp->inp_faddr.s_addr != INADDR_ANY && 55271498f30SBruce M Simpson inp->inp_faddr.s_addr != ip->ip_src.s_addr) 55371498f30SBruce M Simpson continue; 55471498f30SBruce M Simpson if (inp->inp_fport != 0 && 555df8bae1dSRodney W. Grimes inp->inp_fport != uh->uh_sport) 5569c1df695SRobert Watson continue; 55771498f30SBruce M Simpson 558119d85f6SRobert Watson INP_RLOCK(inp); 559df8bae1dSRodney W. Grimes 56083453a06SBruce M Simpson /* 561fa046d87SRobert Watson * XXXRW: Because we weren't holding either the inpcb 562fa046d87SRobert Watson * or the hash lock when we checked for a match 563fa046d87SRobert Watson * before, we should probably recheck now that the 564fa046d87SRobert Watson * inpcb lock is held. 565fa046d87SRobert Watson */ 566fa046d87SRobert Watson 567fa046d87SRobert Watson /* 56871498f30SBruce M Simpson * Handle socket delivery policy for any-source 56971498f30SBruce M Simpson * and source-specific multicast. [RFC3678] 57083453a06SBruce M Simpson */ 57171498f30SBruce M Simpson imo = inp->inp_moptions; 572a38b1c8cSRandall Stewart if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 573d10910e6SBruce M Simpson struct sockaddr_in group; 574d10910e6SBruce M Simpson int blocked; 575a38b1c8cSRandall Stewart if (imo == NULL) { 576a38b1c8cSRandall Stewart INP_RUNLOCK(inp); 577a38b1c8cSRandall Stewart continue; 578a38b1c8cSRandall Stewart } 579d10910e6SBruce M Simpson bzero(&group, sizeof(struct sockaddr_in)); 580d10910e6SBruce M Simpson group.sin_len = sizeof(struct sockaddr_in); 581d10910e6SBruce M Simpson group.sin_family = AF_INET; 582d10910e6SBruce M Simpson group.sin_addr = ip->ip_dst; 58371498f30SBruce M Simpson 584d10910e6SBruce M Simpson blocked = imo_multi_filter(imo, ifp, 585d10910e6SBruce M Simpson (struct sockaddr *)&group, 586*dce33a45SErmal Luçi (struct sockaddr *)&udp_in[0]); 587d10910e6SBruce M Simpson if (blocked != MCAST_PASS) { 588d10910e6SBruce M Simpson if (blocked == MCAST_NOTGMEMBER) 58986425c62SRobert Watson IPSTAT_INC(ips_notmember); 590d10910e6SBruce M Simpson if (blocked == MCAST_NOTSMEMBER || 591d10910e6SBruce M Simpson blocked == MCAST_MUTED) 592026decb8SRobert Watson UDPSTAT_INC(udps_filtermcast); 593119d85f6SRobert Watson INP_RUNLOCK(inp); 5949c1df695SRobert Watson continue; 5959c1df695SRobert Watson } 59683453a06SBruce M Simpson } 597df8bae1dSRodney W. Grimes if (last != NULL) { 598df8bae1dSRodney W. Grimes struct mbuf *n; 599df8bae1dSRodney W. Grimes 600c3bef61eSKevin Lo if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != 601c3bef61eSKevin Lo NULL) { 602a0a9e1b5SBryan Venteicher UDP_PROBE(receive, NULL, last, ip, 603a0a9e1b5SBryan Venteicher last, uh); 604c0d1be08SRandall Stewart if (udp_append(last, ip, n, iphlen, 605*dce33a45SErmal Luçi udp_in)) { 606c0d1be08SRandall Stewart goto inp_lost; 607c0d1be08SRandall Stewart } 608c19f98ebSBryan Venteicher } 6096a9148feSBjoern A. Zeeb INP_RUNLOCK(last); 610df8bae1dSRodney W. Grimes } 61182c23ebaSBill Fenner last = inp; 612df8bae1dSRodney W. Grimes /* 613df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 614df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 6153329b236SRobert Watson * socket options set. This heuristic avoids 6163329b236SRobert Watson * searching through all pcbs in the common case of a 6173329b236SRobert Watson * non-shared port. It assumes that an application 6183329b236SRobert Watson * will never clear these options after setting them. 619df8bae1dSRodney W. Grimes */ 6203329b236SRobert Watson if ((last->inp_socket->so_options & 6213329b236SRobert Watson (SO_REUSEPORT|SO_REUSEADDR)) == 0) 622df8bae1dSRodney W. Grimes break; 623df8bae1dSRodney W. Grimes } 624df8bae1dSRodney W. Grimes 625df8bae1dSRodney W. Grimes if (last == NULL) { 626df8bae1dSRodney W. Grimes /* 6273329b236SRobert Watson * No matching pcb found; discard datagram. (No need 6283329b236SRobert Watson * to send an ICMP Port Unreachable for a broadcast 6293329b236SRobert Watson * or multicast datgram.) 630df8bae1dSRodney W. Grimes */ 631026decb8SRobert Watson UDPSTAT_INC(udps_noportbcast); 632fa046d87SRobert Watson if (inp) 633fa046d87SRobert Watson INP_RUNLOCK(inp); 634e06e816fSKevin Lo INP_INFO_RUNLOCK(pcbinfo); 635fa046d87SRobert Watson goto badunlocked; 636df8bae1dSRodney W. Grimes } 637a0a9e1b5SBryan Venteicher UDP_PROBE(receive, NULL, last, ip, last, uh); 638*dce33a45SErmal Luçi if (udp_append(last, ip, m, iphlen, udp_in) == 0) 639c7c7ea4bSRandall Stewart INP_RUNLOCK(last); 640c0d1be08SRandall Stewart inp_lost: 641e06e816fSKevin Lo INP_INFO_RUNLOCK(pcbinfo); 6428f5a8818SKevin Lo return (IPPROTO_DONE); 643df8bae1dSRodney W. Grimes } 6443329b236SRobert Watson 645df8bae1dSRodney W. Grimes /* 6466d6a026bSDavid Greenman * Locate pcb for datagram. 647df8bae1dSRodney W. Grimes */ 648c1de64a4SAndrey V. Elsukov 6498a006adbSBjoern A. Zeeb /* 6508a006adbSBjoern A. Zeeb * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 6518a006adbSBjoern A. Zeeb */ 652ffdbf9daSAndrey V. Elsukov if ((m->m_flags & M_IP_NEXTHOP) && 653c1de64a4SAndrey V. Elsukov (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 6548a006adbSBjoern A. Zeeb struct sockaddr_in *next_hop; 6558a006adbSBjoern A. Zeeb 6568a006adbSBjoern A. Zeeb next_hop = (struct sockaddr_in *)(fwd_tag + 1); 6578a006adbSBjoern A. Zeeb 6588a006adbSBjoern A. Zeeb /* 6598a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 6608a006adbSBjoern A. Zeeb * Already got one like this? 6618a006adbSBjoern A. Zeeb */ 662e06e816fSKevin Lo inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 6638a006adbSBjoern A. Zeeb ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m); 6648a006adbSBjoern A. Zeeb if (!inp) { 6658a006adbSBjoern A. Zeeb /* 6668a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 6678a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 6688a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 6698a006adbSBjoern A. Zeeb */ 670e06e816fSKevin Lo inp = in_pcblookup(pcbinfo, ip->ip_src, 6718a006adbSBjoern A. Zeeb uh->uh_sport, next_hop->sin_addr, 6728a006adbSBjoern A. Zeeb next_hop->sin_port ? htons(next_hop->sin_port) : 6738a006adbSBjoern A. Zeeb uh->uh_dport, INPLOOKUP_WILDCARD | 6748a006adbSBjoern A. Zeeb INPLOOKUP_RLOCKPCB, ifp); 6758a006adbSBjoern A. Zeeb } 6768a006adbSBjoern A. Zeeb /* Remove the tag from the packet. We don't need it anymore. */ 6778a006adbSBjoern A. Zeeb m_tag_delete(m, fwd_tag); 678ffdbf9daSAndrey V. Elsukov m->m_flags &= ~M_IP_NEXTHOP; 6798a006adbSBjoern A. Zeeb } else 680e06e816fSKevin Lo inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 6818a006adbSBjoern A. Zeeb ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD | 6828a006adbSBjoern A. Zeeb INPLOOKUP_RLOCKPCB, ifp, m); 68315bd2b43SDavid Greenman if (inp == NULL) { 684afdb4274SRobert Watson if (udp_log_in_vain) { 685edf0313bSEric van Gyzen char src[INET_ADDRSTRLEN]; 686edf0313bSEric van Gyzen char dst[INET_ADDRSTRLEN]; 68775cfc95fSAndrey A. Chernov 688592071e8SBruce Evans log(LOG_INFO, 689592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 690edf0313bSEric van Gyzen inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport), 691edf0313bSEric van Gyzen inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport)); 69275cfc95fSAndrey A. Chernov } 693026decb8SRobert Watson UDPSTAT_INC(udps_noport); 694df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 695026decb8SRobert Watson UDPSTAT_INC(udps_noportbcast); 696fa046d87SRobert Watson goto badunlocked; 697df8bae1dSRodney W. Grimes } 698603724d3SBjoern A. Zeeb if (V_udp_blackhole) 699fa046d87SRobert Watson goto badunlocked; 7001cbd978eSLuigi Rizzo if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 701fa046d87SRobert Watson goto badunlocked; 70204287599SRuslan Ermilov *ip = save_ip; 703582a7760SBruce Evans icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 7048f5a8818SKevin Lo return (IPPROTO_DONE); 705df8bae1dSRodney W. Grimes } 7063329b236SRobert Watson 7073329b236SRobert Watson /* 7083329b236SRobert Watson * Check the minimum TTL for socket. 7093329b236SRobert Watson */ 710fa046d87SRobert Watson INP_RLOCK_ASSERT(inp); 71110cc62b7SRobert Watson if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) { 71210cc62b7SRobert Watson INP_RUNLOCK(inp); 713fa046d87SRobert Watson m_freem(m); 7148f5a8818SKevin Lo return (IPPROTO_DONE); 71510cc62b7SRobert Watson } 716e06e816fSKevin Lo if (cscov_partial) { 717e06e816fSKevin Lo struct udpcb *up; 718e06e816fSKevin Lo 719e06e816fSKevin Lo up = intoudpcb(inp); 72083e95fb3SMichael Tuexen if (up->u_rxcslen == 0 || up->u_rxcslen > len) { 721e06e816fSKevin Lo INP_RUNLOCK(inp); 722e06e816fSKevin Lo m_freem(m); 7238f5a8818SKevin Lo return (IPPROTO_DONE); 724e06e816fSKevin Lo } 725e06e816fSKevin Lo } 72657f60867SMark Johnston 7271ad19fb6SMark Johnston UDP_PROBE(receive, NULL, inp, ip, inp, uh); 728*dce33a45SErmal Luçi if (udp_append(inp, ip, m, iphlen, udp_in) == 0) 729119d85f6SRobert Watson INP_RUNLOCK(inp); 7308f5a8818SKevin Lo return (IPPROTO_DONE); 73161ffc0b1SJeffrey Hsu 732f76fcf6dSJeffrey Hsu badunlocked: 733df8bae1dSRodney W. Grimes m_freem(m); 7348f5a8818SKevin Lo return (IPPROTO_DONE); 735cfa1ca9dSYoshinobu Inoue } 73679288c11SBjoern A. Zeeb #endif /* INET */ 737cfa1ca9dSYoshinobu Inoue 738cfa1ca9dSYoshinobu Inoue /* 7393329b236SRobert Watson * Notify a udp user of an asynchronous error; just wake up so that they can 7403329b236SRobert Watson * collect error status. 741df8bae1dSRodney W. Grimes */ 7423ce144eaSJeffrey Hsu struct inpcb * 7433329b236SRobert Watson udp_notify(struct inpcb *inp, int errno) 744df8bae1dSRodney W. Grimes { 7453329b236SRobert Watson 746ac9ae279SRobert Watson /* 747ac9ae279SRobert Watson * While udp_ctlinput() always calls udp_notify() with a read lock 748ac9ae279SRobert Watson * when invoking it directly, in_pcbnotifyall() currently uses write 749ac9ae279SRobert Watson * locks due to sharing code with TCP. For now, accept either a read 750ac9ae279SRobert Watson * or a write lock, but a read lock is sufficient. 751ac9ae279SRobert Watson */ 752ac9ae279SRobert Watson INP_LOCK_ASSERT(inp); 75384cc0778SGeorge V. Neville-Neil if ((errno == EHOSTUNREACH || errno == ENETUNREACH || 75484cc0778SGeorge V. Neville-Neil errno == EHOSTDOWN) && inp->inp_route.ro_rt) { 75584cc0778SGeorge V. Neville-Neil RTFREE(inp->inp_route.ro_rt); 75684cc0778SGeorge V. Neville-Neil inp->inp_route.ro_rt = (struct rtentry *)NULL; 75784cc0778SGeorge V. Neville-Neil } 7588501a69cSRobert Watson 759df8bae1dSRodney W. Grimes inp->inp_socket->so_error = errno; 760df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 761df8bae1dSRodney W. Grimes sowwakeup(inp->inp_socket); 7623329b236SRobert Watson return (inp); 763df8bae1dSRodney W. Grimes } 764df8bae1dSRodney W. Grimes 76579288c11SBjoern A. Zeeb #ifdef INET 766e06e816fSKevin Lo static void 767e06e816fSKevin Lo udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip, 768e06e816fSKevin Lo struct inpcbinfo *pcbinfo) 769df8bae1dSRodney W. Grimes { 770c693a045SJonathan Lemon struct ip *ip = vip; 771c693a045SJonathan Lemon struct udphdr *uh; 772c693a045SJonathan Lemon struct in_addr faddr; 773c693a045SJonathan Lemon struct inpcb *inp; 774c693a045SJonathan Lemon 775c693a045SJonathan Lemon faddr = ((struct sockaddr_in *)sa)->sin_addr; 776c693a045SJonathan Lemon if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 777c693a045SJonathan Lemon return; 778df8bae1dSRodney W. Grimes 77984cc0778SGeorge V. Neville-Neil if (PRC_IS_REDIRECT(cmd)) { 78084cc0778SGeorge V. Neville-Neil /* signal EHOSTDOWN, as it flushes the cached route */ 7814f321dbdSBjoern A. Zeeb in_pcbnotifyall(&V_udbinfo, faddr, EHOSTDOWN, udp_notify); 78297d8d152SAndre Oppermann return; 78384cc0778SGeorge V. Neville-Neil } 7843329b236SRobert Watson 78597d8d152SAndre Oppermann /* 78697d8d152SAndre Oppermann * Hostdead is ugly because it goes linearly through all PCBs. 7873329b236SRobert Watson * 7883329b236SRobert Watson * XXX: We never get this from ICMP, otherwise it makes an excellent 7893329b236SRobert Watson * DoS attack on machines with many connections. 79097d8d152SAndre Oppermann */ 79197d8d152SAndre Oppermann if (cmd == PRC_HOSTDEAD) 792af1ee11dSRobert Watson ip = NULL; 793d1c54148SJesper Skriver else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 794df8bae1dSRodney W. Grimes return; 795af1ee11dSRobert Watson if (ip != NULL) { 796df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 797e06e816fSKevin Lo inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 798fa046d87SRobert Watson ip->ip_src, uh->uh_sport, INPLOOKUP_RLOCKPCB, NULL); 799f76fcf6dSJeffrey Hsu if (inp != NULL) { 800fa046d87SRobert Watson INP_RLOCK_ASSERT(inp); 801f76fcf6dSJeffrey Hsu if (inp->inp_socket != NULL) { 802f5514f08SRobert Watson udp_notify(inp, inetctlerrmap[cmd]); 803f76fcf6dSJeffrey Hsu } 804ac9ae279SRobert Watson INP_RUNLOCK(inp); 805abb901c5SRandall Stewart } else { 806abb901c5SRandall Stewart inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 807abb901c5SRandall Stewart ip->ip_src, uh->uh_sport, 808abb901c5SRandall Stewart INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 809abb901c5SRandall Stewart if (inp != NULL) { 810abb901c5SRandall Stewart struct udpcb *up; 811abb901c5SRandall Stewart 812abb901c5SRandall Stewart up = intoudpcb(inp); 813abb901c5SRandall Stewart if (up->u_icmp_func != NULL) { 814abb901c5SRandall Stewart INP_RUNLOCK(inp); 815abb901c5SRandall Stewart (*up->u_icmp_func)(cmd, sa, vip, up->u_tun_ctx); 816abb901c5SRandall Stewart } else { 817abb901c5SRandall Stewart INP_RUNLOCK(inp); 818abb901c5SRandall Stewart } 819abb901c5SRandall Stewart } 820f76fcf6dSJeffrey Hsu } 821df8bae1dSRodney W. Grimes } else 822e06e816fSKevin Lo in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd], 823f5514f08SRobert Watson udp_notify); 824df8bae1dSRodney W. Grimes } 825e06e816fSKevin Lo void 826e06e816fSKevin Lo udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 827e06e816fSKevin Lo { 828e06e816fSKevin Lo 829e06e816fSKevin Lo return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo)); 830e06e816fSKevin Lo } 831e06e816fSKevin Lo 832e06e816fSKevin Lo void 833e06e816fSKevin Lo udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip) 834e06e816fSKevin Lo { 835e06e816fSKevin Lo 836e06e816fSKevin Lo return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo)); 837e06e816fSKevin Lo } 83879288c11SBjoern A. Zeeb #endif /* INET */ 839df8bae1dSRodney W. Grimes 8400312fbe9SPoul-Henning Kamp static int 84182d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS) 84298271db4SGarrett Wollman { 843277afaffSRobert Watson int error, i, n; 84498271db4SGarrett Wollman struct inpcb *inp, **inp_list; 84598271db4SGarrett Wollman inp_gen_t gencnt; 84698271db4SGarrett Wollman struct xinpgen xig; 84798271db4SGarrett Wollman 84898271db4SGarrett Wollman /* 849f5514f08SRobert Watson * The process of preparing the PCB list is too time-consuming and 85098271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 85198271db4SGarrett Wollman */ 85298271db4SGarrett Wollman if (req->oldptr == 0) { 853603724d3SBjoern A. Zeeb n = V_udbinfo.ipi_count; 854c007b96aSJohn Baldwin n += imax(n / 8, 10); 855c007b96aSJohn Baldwin req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb); 8563329b236SRobert Watson return (0); 85798271db4SGarrett Wollman } 85898271db4SGarrett Wollman 85998271db4SGarrett Wollman if (req->newptr != 0) 8603329b236SRobert Watson return (EPERM); 86198271db4SGarrett Wollman 86298271db4SGarrett Wollman /* 86398271db4SGarrett Wollman * OK, now we're committed to doing something. 86498271db4SGarrett Wollman */ 865603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 866603724d3SBjoern A. Zeeb gencnt = V_udbinfo.ipi_gencnt; 867603724d3SBjoern A. Zeeb n = V_udbinfo.ipi_count; 868603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 86998271db4SGarrett Wollman 87047934cefSDon Lewis error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 8715c38b6dbSDon Lewis + n * sizeof(struct xinpcb)); 87247934cefSDon Lewis if (error != 0) 87347934cefSDon Lewis return (error); 8745c38b6dbSDon Lewis 87598271db4SGarrett Wollman xig.xig_len = sizeof xig; 87698271db4SGarrett Wollman xig.xig_count = n; 87798271db4SGarrett Wollman xig.xig_gen = gencnt; 87898271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 87998271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 88098271db4SGarrett Wollman if (error) 8813329b236SRobert Watson return (error); 88298271db4SGarrett Wollman 883a163d034SWarner Losh inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 88499d628d5SPedro F. Giffuni if (inp_list == NULL) 8853329b236SRobert Watson return (ENOMEM); 88698271db4SGarrett Wollman 887603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 888603724d3SBjoern A. Zeeb for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n; 889fc2ffbe6SPoul-Henning Kamp inp = LIST_NEXT(inp, inp_list)) { 890d0e157f6SBjoern A. Zeeb INP_WLOCK(inp); 8912ded288cSJeffrey Hsu if (inp->inp_gencnt <= gencnt && 892d0e157f6SBjoern A. Zeeb cr_canseeinpcb(req->td->td_ucred, inp) == 0) { 893d0e157f6SBjoern A. Zeeb in_pcbref(inp); 89498271db4SGarrett Wollman inp_list[i++] = inp; 895d0e157f6SBjoern A. Zeeb } 896d0e157f6SBjoern A. Zeeb INP_WUNLOCK(inp); 8974787fd37SPaul Saab } 898603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 89998271db4SGarrett Wollman n = i; 90098271db4SGarrett Wollman 90198271db4SGarrett Wollman error = 0; 90298271db4SGarrett Wollman for (i = 0; i < n; i++) { 90398271db4SGarrett Wollman inp = inp_list[i]; 9049622e84fSRobert Watson INP_RLOCK(inp); 90598271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 90698271db4SGarrett Wollman struct xinpcb xi; 907d0e157f6SBjoern A. Zeeb 908fd94099eSColin Percival bzero(&xi, sizeof(xi)); 90998271db4SGarrett Wollman xi.xi_len = sizeof xi; 91098271db4SGarrett Wollman /* XXX should avoid extra copy */ 91198271db4SGarrett Wollman bcopy(inp, &xi.xi_inp, sizeof *inp); 91298271db4SGarrett Wollman if (inp->inp_socket) 91398271db4SGarrett Wollman sotoxsocket(inp->inp_socket, &xi.xi_socket); 9144b40c56cSJeffrey Hsu xi.xi_inp.inp_gencnt = inp->inp_gencnt; 9159622e84fSRobert Watson INP_RUNLOCK(inp); 91698271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 917d915b280SStephan Uphoff } else 9189622e84fSRobert Watson INP_RUNLOCK(inp); 91998271db4SGarrett Wollman } 920d0e157f6SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 921d0e157f6SBjoern A. Zeeb for (i = 0; i < n; i++) { 922d0e157f6SBjoern A. Zeeb inp = inp_list[i]; 923fa046d87SRobert Watson INP_RLOCK(inp); 924fa046d87SRobert Watson if (!in_pcbrele_rlocked(inp)) 925fa046d87SRobert Watson INP_RUNLOCK(inp); 926d0e157f6SBjoern A. Zeeb } 927d0e157f6SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 928d0e157f6SBjoern A. Zeeb 92998271db4SGarrett Wollman if (!error) { 93098271db4SGarrett Wollman /* 9313329b236SRobert Watson * Give the user an updated idea of our state. If the 9323329b236SRobert Watson * generation differs from what we told her before, she knows 9333329b236SRobert Watson * that something happened while we were processing this 9343329b236SRobert Watson * request, and it might be necessary to retry. 93598271db4SGarrett Wollman */ 936603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 937603724d3SBjoern A. Zeeb xig.xig_gen = V_udbinfo.ipi_gencnt; 93898271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 939603724d3SBjoern A. Zeeb xig.xig_count = V_udbinfo.ipi_count; 940603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 94198271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 94298271db4SGarrett Wollman } 94398271db4SGarrett Wollman free(inp_list, M_TEMP); 9443329b236SRobert Watson return (error); 94598271db4SGarrett Wollman } 94698271db4SGarrett Wollman 94779c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, 94879c3d51bSMatthew D Fleming CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0, 94998271db4SGarrett Wollman udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 95098271db4SGarrett Wollman 95179288c11SBjoern A. Zeeb #ifdef INET 95298271db4SGarrett Wollman static int 95382d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS) 954490d50b6SBrian Feldman { 955c0511d3bSBrian Feldman struct xucred xuc; 956490d50b6SBrian Feldman struct sockaddr_in addrs[2]; 957490d50b6SBrian Feldman struct inpcb *inp; 958277afaffSRobert Watson int error; 959490d50b6SBrian Feldman 96032f9753cSRobert Watson error = priv_check(req->td, PRIV_NETINET_GETCRED); 961490d50b6SBrian Feldman if (error) 962490d50b6SBrian Feldman return (error); 963490d50b6SBrian Feldman error = SYSCTL_IN(req, addrs, sizeof(addrs)); 964490d50b6SBrian Feldman if (error) 965490d50b6SBrian Feldman return (error); 966fa046d87SRobert Watson inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 967fa046d87SRobert Watson addrs[0].sin_addr, addrs[0].sin_port, 968fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 9699622e84fSRobert Watson if (inp != NULL) { 970fa046d87SRobert Watson INP_RLOCK_ASSERT(inp); 9719622e84fSRobert Watson if (inp->inp_socket == NULL) 9729622e84fSRobert Watson error = ENOENT; 9739622e84fSRobert Watson if (error == 0) 974f08ef6c5SBjoern A. Zeeb error = cr_canseeinpcb(req->td->td_ucred, inp); 9759622e84fSRobert Watson if (error == 0) 97686d02c5cSBjoern A. Zeeb cru2x(inp->inp_cred, &xuc); 9779622e84fSRobert Watson INP_RUNLOCK(inp); 978fa046d87SRobert Watson } else 9799622e84fSRobert Watson error = ENOENT; 9800e1eebb8SDon Lewis if (error == 0) 9810e1eebb8SDon Lewis error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 982490d50b6SBrian Feldman return (error); 983490d50b6SBrian Feldman } 984490d50b6SBrian Feldman 9857ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 9867ce87f12SDavid Malone CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 9877ce87f12SDavid Malone udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 98879288c11SBjoern A. Zeeb #endif /* INET */ 989490d50b6SBrian Feldman 9907b495c44SVANHULLEBUS Yvan int 9917b495c44SVANHULLEBUS Yvan udp_ctloutput(struct socket *so, struct sockopt *sopt) 9927b495c44SVANHULLEBUS Yvan { 9937b495c44SVANHULLEBUS Yvan struct inpcb *inp; 9947b495c44SVANHULLEBUS Yvan struct udpcb *up; 995e06e816fSKevin Lo int isudplite, error, optval; 9967b495c44SVANHULLEBUS Yvan 997e06e816fSKevin Lo error = 0; 998e06e816fSKevin Lo isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0; 9997b495c44SVANHULLEBUS Yvan inp = sotoinpcb(so); 10007b495c44SVANHULLEBUS Yvan KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 10017b495c44SVANHULLEBUS Yvan INP_WLOCK(inp); 1002e06e816fSKevin Lo if (sopt->sopt_level != so->so_proto->pr_protocol) { 10037b495c44SVANHULLEBUS Yvan #ifdef INET6 10047b495c44SVANHULLEBUS Yvan if (INP_CHECK_SOCKAF(so, AF_INET6)) { 10057b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 10067b495c44SVANHULLEBUS Yvan error = ip6_ctloutput(so, sopt); 100779288c11SBjoern A. Zeeb } 10087b495c44SVANHULLEBUS Yvan #endif 100979288c11SBjoern A. Zeeb #if defined(INET) && defined(INET6) 101079288c11SBjoern A. Zeeb else 101179288c11SBjoern A. Zeeb #endif 101279288c11SBjoern A. Zeeb #ifdef INET 101379288c11SBjoern A. Zeeb { 10147b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 10157b495c44SVANHULLEBUS Yvan error = ip_ctloutput(so, sopt); 10167b495c44SVANHULLEBUS Yvan } 10177b495c44SVANHULLEBUS Yvan #endif 10187b495c44SVANHULLEBUS Yvan return (error); 10197b495c44SVANHULLEBUS Yvan } 10207b495c44SVANHULLEBUS Yvan 10217b495c44SVANHULLEBUS Yvan switch (sopt->sopt_dir) { 10227b495c44SVANHULLEBUS Yvan case SOPT_SET: 10237b495c44SVANHULLEBUS Yvan switch (sopt->sopt_name) { 1024fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1025fcf59617SAndrey V. Elsukov #ifdef INET 10267b495c44SVANHULLEBUS Yvan case UDP_ENCAP: 1027fcf59617SAndrey V. Elsukov if (!IPSEC_ENABLED(ipv4)) { 10287b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 1029fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 10307b495c44SVANHULLEBUS Yvan } 1031fcf59617SAndrey V. Elsukov error = UDPENCAP_PCBCTL(inp, sopt); 10327b495c44SVANHULLEBUS Yvan break; 1033fcf59617SAndrey V. Elsukov #endif /* INET */ 1034fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 1035e06e816fSKevin Lo case UDPLITE_SEND_CSCOV: 1036e06e816fSKevin Lo case UDPLITE_RECV_CSCOV: 1037e06e816fSKevin Lo if (!isudplite) { 1038e06e816fSKevin Lo INP_WUNLOCK(inp); 1039e06e816fSKevin Lo error = ENOPROTOOPT; 1040e06e816fSKevin Lo break; 1041e06e816fSKevin Lo } 1042e06e816fSKevin Lo INP_WUNLOCK(inp); 1043e06e816fSKevin Lo error = sooptcopyin(sopt, &optval, sizeof(optval), 1044e06e816fSKevin Lo sizeof(optval)); 1045e06e816fSKevin Lo if (error != 0) 1046e06e816fSKevin Lo break; 1047e06e816fSKevin Lo inp = sotoinpcb(so); 1048e06e816fSKevin Lo KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1049e06e816fSKevin Lo INP_WLOCK(inp); 1050e06e816fSKevin Lo up = intoudpcb(inp); 1051e06e816fSKevin Lo KASSERT(up != NULL, ("%s: up == NULL", __func__)); 105203f90784SMichael Tuexen if ((optval != 0 && optval < 8) || (optval > 65535)) { 1053e06e816fSKevin Lo INP_WUNLOCK(inp); 1054e06e816fSKevin Lo error = EINVAL; 1055e06e816fSKevin Lo break; 1056e06e816fSKevin Lo } 1057e06e816fSKevin Lo if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1058e06e816fSKevin Lo up->u_txcslen = optval; 1059e06e816fSKevin Lo else 1060e06e816fSKevin Lo up->u_rxcslen = optval; 1061e06e816fSKevin Lo INP_WUNLOCK(inp); 1062e06e816fSKevin Lo break; 10637b495c44SVANHULLEBUS Yvan default: 10647b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 10657b495c44SVANHULLEBUS Yvan error = ENOPROTOOPT; 10667b495c44SVANHULLEBUS Yvan break; 10677b495c44SVANHULLEBUS Yvan } 10687b495c44SVANHULLEBUS Yvan break; 10697b495c44SVANHULLEBUS Yvan case SOPT_GET: 10707b495c44SVANHULLEBUS Yvan switch (sopt->sopt_name) { 1071fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1072fcf59617SAndrey V. Elsukov #ifdef INET 10737b495c44SVANHULLEBUS Yvan case UDP_ENCAP: 1074fcf59617SAndrey V. Elsukov if (!IPSEC_ENABLED(ipv4)) { 10757b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 1076fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 1077fcf59617SAndrey V. Elsukov } 1078fcf59617SAndrey V. Elsukov error = UDPENCAP_PCBCTL(inp, sopt); 10797b495c44SVANHULLEBUS Yvan break; 1080fcf59617SAndrey V. Elsukov #endif /* INET */ 1081fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 1082e06e816fSKevin Lo case UDPLITE_SEND_CSCOV: 1083e06e816fSKevin Lo case UDPLITE_RECV_CSCOV: 1084e06e816fSKevin Lo if (!isudplite) { 1085e06e816fSKevin Lo INP_WUNLOCK(inp); 1086e06e816fSKevin Lo error = ENOPROTOOPT; 1087e06e816fSKevin Lo break; 1088e06e816fSKevin Lo } 1089e06e816fSKevin Lo up = intoudpcb(inp); 1090e06e816fSKevin Lo KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1091e06e816fSKevin Lo if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1092e06e816fSKevin Lo optval = up->u_txcslen; 1093e06e816fSKevin Lo else 1094e06e816fSKevin Lo optval = up->u_rxcslen; 1095e06e816fSKevin Lo INP_WUNLOCK(inp); 1096e06e816fSKevin Lo error = sooptcopyout(sopt, &optval, sizeof(optval)); 1097e06e816fSKevin Lo break; 10987b495c44SVANHULLEBUS Yvan default: 10997b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 11007b495c44SVANHULLEBUS Yvan error = ENOPROTOOPT; 11017b495c44SVANHULLEBUS Yvan break; 11027b495c44SVANHULLEBUS Yvan } 11037b495c44SVANHULLEBUS Yvan break; 11047b495c44SVANHULLEBUS Yvan } 11057b495c44SVANHULLEBUS Yvan return (error); 11067b495c44SVANHULLEBUS Yvan } 11077b495c44SVANHULLEBUS Yvan 110879288c11SBjoern A. Zeeb #ifdef INET 1109fa046d87SRobert Watson #define UH_WLOCKED 2 1110fa046d87SRobert Watson #define UH_RLOCKED 1 1111fa046d87SRobert Watson #define UH_UNLOCKED 0 1112490d50b6SBrian Feldman static int 11133329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, 11143329b236SRobert Watson struct mbuf *control, struct thread *td) 1115df8bae1dSRodney W. Grimes { 11163329b236SRobert Watson struct udpiphdr *ui; 11173329b236SRobert Watson int len = m->m_pkthdr.len; 111890162a4eSIan Dowse struct in_addr faddr, laddr; 1119c557ae16SIan Dowse struct cmsghdr *cm; 1120e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1121c557ae16SIan Dowse struct sockaddr_in *sin, src; 1122e06e816fSKevin Lo int cscov_partial = 0; 112390162a4eSIan Dowse int error = 0; 11248afa2304SBruce M Simpson int ipflags; 112590162a4eSIan Dowse u_short fport, lport; 112684cc0778SGeorge V. Neville-Neil int unlock_udbinfo, unlock_inp; 1127f584d74bSMichael Tuexen u_char tos; 1128e06e816fSKevin Lo uint8_t pr; 1129e06e816fSKevin Lo uint16_t cscov = 0; 11309d3ddf43SAdrian Chadd uint32_t flowid = 0; 1131c2529042SHans Petter Selasky uint8_t flowtype = M_HASHTYPE_NONE; 1132df8bae1dSRodney W. Grimes 11335c32ea65SRobert Watson /* 11345c32ea65SRobert Watson * udp_output() may need to temporarily bind or connect the current 1135f5514f08SRobert Watson * inpcb. As such, we don't know up front whether we will need the 1136f5514f08SRobert Watson * pcbinfo lock or not. Do any work to decide what is needed up 1137f5514f08SRobert Watson * front before acquiring any locks. 11385c32ea65SRobert Watson */ 1139430d30d8SBill Fenner if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 1140c557ae16SIan Dowse if (control) 1141c557ae16SIan Dowse m_freem(control); 11425c32ea65SRobert Watson m_freem(m); 11433329b236SRobert Watson return (EMSGSIZE); 1144430d30d8SBill Fenner } 1145430d30d8SBill Fenner 11461b7f0384SBruce M Simpson src.sin_family = 0; 114784cc0778SGeorge V. Neville-Neil sin = (struct sockaddr_in *)addr; 114884cc0778SGeorge V. Neville-Neil if (sin == NULL || 114984cc0778SGeorge V. Neville-Neil (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 115084cc0778SGeorge V. Neville-Neil INP_WLOCK(inp); 115184cc0778SGeorge V. Neville-Neil unlock_inp = UH_WLOCKED; 115284cc0778SGeorge V. Neville-Neil } else { 11530cfdff24SBjoern A. Zeeb INP_RLOCK(inp); 115484cc0778SGeorge V. Neville-Neil unlock_inp = UH_RLOCKED; 115584cc0778SGeorge V. Neville-Neil } 1156f584d74bSMichael Tuexen tos = inp->inp_ip_tos; 1157c557ae16SIan Dowse if (control != NULL) { 1158c557ae16SIan Dowse /* 11593329b236SRobert Watson * XXX: Currently, we assume all the optional information is 11603329b236SRobert Watson * stored in a single mbuf. 1161c557ae16SIan Dowse */ 1162c557ae16SIan Dowse if (control->m_next) { 116384cc0778SGeorge V. Neville-Neil if (unlock_inp == UH_WLOCKED) 116484cc0778SGeorge V. Neville-Neil INP_WUNLOCK(inp); 116584cc0778SGeorge V. Neville-Neil else 11660cfdff24SBjoern A. Zeeb INP_RUNLOCK(inp); 1167c557ae16SIan Dowse m_freem(control); 11685c32ea65SRobert Watson m_freem(m); 11693329b236SRobert Watson return (EINVAL); 1170c557ae16SIan Dowse } 1171c557ae16SIan Dowse for (; control->m_len > 0; 1172c557ae16SIan Dowse control->m_data += CMSG_ALIGN(cm->cmsg_len), 1173c557ae16SIan Dowse control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 1174c557ae16SIan Dowse cm = mtod(control, struct cmsghdr *); 1175af1ee11dSRobert Watson if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 1176af1ee11dSRobert Watson || cm->cmsg_len > control->m_len) { 1177c557ae16SIan Dowse error = EINVAL; 1178c557ae16SIan Dowse break; 1179c557ae16SIan Dowse } 1180c557ae16SIan Dowse if (cm->cmsg_level != IPPROTO_IP) 1181c557ae16SIan Dowse continue; 1182c557ae16SIan Dowse 1183c557ae16SIan Dowse switch (cm->cmsg_type) { 1184c557ae16SIan Dowse case IP_SENDSRCADDR: 1185c557ae16SIan Dowse if (cm->cmsg_len != 1186c557ae16SIan Dowse CMSG_LEN(sizeof(struct in_addr))) { 1187c557ae16SIan Dowse error = EINVAL; 1188c557ae16SIan Dowse break; 1189c557ae16SIan Dowse } 1190c557ae16SIan Dowse bzero(&src, sizeof(src)); 1191c557ae16SIan Dowse src.sin_family = AF_INET; 1192c557ae16SIan Dowse src.sin_len = sizeof(src); 1193c557ae16SIan Dowse src.sin_port = inp->inp_lport; 1194af1ee11dSRobert Watson src.sin_addr = 1195af1ee11dSRobert Watson *(struct in_addr *)CMSG_DATA(cm); 1196c557ae16SIan Dowse break; 1197af1ee11dSRobert Watson 1198f584d74bSMichael Tuexen case IP_TOS: 1199f584d74bSMichael Tuexen if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) { 1200f584d74bSMichael Tuexen error = EINVAL; 1201f584d74bSMichael Tuexen break; 1202f584d74bSMichael Tuexen } 1203f584d74bSMichael Tuexen tos = *(u_char *)CMSG_DATA(cm); 1204f584d74bSMichael Tuexen break; 1205f584d74bSMichael Tuexen 12069d3ddf43SAdrian Chadd case IP_FLOWID: 12079d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 12089d3ddf43SAdrian Chadd error = EINVAL; 12099d3ddf43SAdrian Chadd break; 12109d3ddf43SAdrian Chadd } 12119d3ddf43SAdrian Chadd flowid = *(uint32_t *) CMSG_DATA(cm); 12129d3ddf43SAdrian Chadd break; 12139d3ddf43SAdrian Chadd 12149d3ddf43SAdrian Chadd case IP_FLOWTYPE: 12159d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 12169d3ddf43SAdrian Chadd error = EINVAL; 12179d3ddf43SAdrian Chadd break; 12189d3ddf43SAdrian Chadd } 1219c2529042SHans Petter Selasky flowtype = *(uint32_t *) CMSG_DATA(cm); 12209d3ddf43SAdrian Chadd break; 12219d3ddf43SAdrian Chadd 12229d3ddf43SAdrian Chadd #ifdef RSS 12239d3ddf43SAdrian Chadd case IP_RSSBUCKETID: 12249d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 12259d3ddf43SAdrian Chadd error = EINVAL; 12269d3ddf43SAdrian Chadd break; 12279d3ddf43SAdrian Chadd } 12289d3ddf43SAdrian Chadd /* This is just a placeholder for now */ 12299d3ddf43SAdrian Chadd break; 12309d3ddf43SAdrian Chadd #endif /* RSS */ 1231c557ae16SIan Dowse default: 1232c557ae16SIan Dowse error = ENOPROTOOPT; 1233c557ae16SIan Dowse break; 1234c557ae16SIan Dowse } 1235c557ae16SIan Dowse if (error) 1236c557ae16SIan Dowse break; 1237c557ae16SIan Dowse } 1238c557ae16SIan Dowse m_freem(control); 1239c557ae16SIan Dowse } 12405c32ea65SRobert Watson if (error) { 124184cc0778SGeorge V. Neville-Neil if (unlock_inp == UH_WLOCKED) 124284cc0778SGeorge V. Neville-Neil INP_WUNLOCK(inp); 124384cc0778SGeorge V. Neville-Neil else 12440cfdff24SBjoern A. Zeeb INP_RUNLOCK(inp); 12455c32ea65SRobert Watson m_freem(m); 12463329b236SRobert Watson return (error); 12475c32ea65SRobert Watson } 12485c32ea65SRobert Watson 124943cc0bc1SRobert Watson /* 125043cc0bc1SRobert Watson * Depending on whether or not the application has bound or connected 1251ca528788SRobert Watson * the socket, we may have to do varying levels of work. The optimal 1252ca528788SRobert Watson * case is for a connected UDP socket, as a global lock isn't 1253ca528788SRobert Watson * required at all. 125443cc0bc1SRobert Watson * 125543cc0bc1SRobert Watson * In order to decide which we need, we require stability of the 125643cc0bc1SRobert Watson * inpcb binding, which we ensure by acquiring a read lock on the 125743cc0bc1SRobert Watson * inpcb. This doesn't strictly follow the lock order, so we play 125843cc0bc1SRobert Watson * the trylock and retry game; note that we may end up with more 125943cc0bc1SRobert Watson * conservative locks than required the second time around, so later 126043cc0bc1SRobert Watson * assertions have to accept that. Further analysis of the number of 126143cc0bc1SRobert Watson * misses under contention is required. 1262fa046d87SRobert Watson * 1263fa046d87SRobert Watson * XXXRW: Check that hash locking update here is correct. 126443cc0bc1SRobert Watson */ 1265e06e816fSKevin Lo pr = inp->inp_socket->so_proto->pr_protocol; 1266a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(pr); 126743cc0bc1SRobert Watson sin = (struct sockaddr_in *)addr; 126843cc0bc1SRobert Watson if (sin != NULL && 126943cc0bc1SRobert Watson (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 1270e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1271fa046d87SRobert Watson unlock_udbinfo = UH_WLOCKED; 127243cc0bc1SRobert Watson } else if ((sin != NULL && ( 127343cc0bc1SRobert Watson (sin->sin_addr.s_addr == INADDR_ANY) || 127443cc0bc1SRobert Watson (sin->sin_addr.s_addr == INADDR_BROADCAST) || 127543cc0bc1SRobert Watson (inp->inp_laddr.s_addr == INADDR_ANY) || 127643cc0bc1SRobert Watson (inp->inp_lport == 0))) || 127743cc0bc1SRobert Watson (src.sin_family == AF_INET)) { 1278e06e816fSKevin Lo INP_HASH_RLOCK(pcbinfo); 1279fa046d87SRobert Watson unlock_udbinfo = UH_RLOCKED; 128043cc0bc1SRobert Watson } else 1281fa046d87SRobert Watson unlock_udbinfo = UH_UNLOCKED; 12825c32ea65SRobert Watson 12831b7f0384SBruce M Simpson /* 12841b7f0384SBruce M Simpson * If the IP_SENDSRCADDR control message was specified, override the 12851b7f0384SBruce M Simpson * source address for this datagram. Its use is invalidated if the 12861b7f0384SBruce M Simpson * address thus specified is incomplete or clobbers other inpcbs. 12871b7f0384SBruce M Simpson */ 128890162a4eSIan Dowse laddr = inp->inp_laddr; 128990162a4eSIan Dowse lport = inp->inp_lport; 12901b7f0384SBruce M Simpson if (src.sin_family == AF_INET) { 1291e06e816fSKevin Lo INP_HASH_LOCK_ASSERT(pcbinfo); 12921b7f0384SBruce M Simpson if ((lport == 0) || 12931b7f0384SBruce M Simpson (laddr.s_addr == INADDR_ANY && 12941b7f0384SBruce M Simpson src.sin_addr.s_addr == INADDR_ANY)) { 1295c557ae16SIan Dowse error = EINVAL; 1296c557ae16SIan Dowse goto release; 1297c557ae16SIan Dowse } 1298c557ae16SIan Dowse error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 1299b0330ed9SPawel Jakub Dawidek &laddr.s_addr, &lport, td->td_ucred); 1300c557ae16SIan Dowse if (error) 1301c557ae16SIan Dowse goto release; 1302c557ae16SIan Dowse } 1303c557ae16SIan Dowse 13043144b7d3SRobert Watson /* 13053144b7d3SRobert Watson * If a UDP socket has been connected, then a local address/port will 13063144b7d3SRobert Watson * have been selected and bound. 13073144b7d3SRobert Watson * 130843cc0bc1SRobert Watson * If a UDP socket has not been connected to, then an explicit 13093144b7d3SRobert Watson * destination address must be used, in which case a local 13103144b7d3SRobert Watson * address/port may not have been selected and bound. 13113144b7d3SRobert Watson */ 131243cc0bc1SRobert Watson if (sin != NULL) { 1313c4d585aeSRobert Watson INP_LOCK_ASSERT(inp); 1314df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 1315df8bae1dSRodney W. Grimes error = EISCONN; 1316df8bae1dSRodney W. Grimes goto release; 1317df8bae1dSRodney W. Grimes } 13183144b7d3SRobert Watson 13193144b7d3SRobert Watson /* 13203144b7d3SRobert Watson * Jail may rewrite the destination address, so let it do 13213144b7d3SRobert Watson * that before we use it. 13223144b7d3SRobert Watson */ 1323b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1324b89e82ddSJamie Gritton if (error) 1325413628a7SBjoern A. Zeeb goto release; 13263144b7d3SRobert Watson 13273144b7d3SRobert Watson /* 132843cc0bc1SRobert Watson * If a local address or port hasn't yet been selected, or if 132943cc0bc1SRobert Watson * the destination address needs to be rewritten due to using 133043cc0bc1SRobert Watson * a special INADDR_ constant, invoke in_pcbconnect_setup() 133143cc0bc1SRobert Watson * to do the heavy lifting. Once a port is selected, we 133243cc0bc1SRobert Watson * commit the binding back to the socket; we also commit the 133343cc0bc1SRobert Watson * binding of the address if in jail. 133443cc0bc1SRobert Watson * 133543cc0bc1SRobert Watson * If we already have a valid binding and we're not 133643cc0bc1SRobert Watson * requesting a destination address rewrite, use a fast path. 13373144b7d3SRobert Watson */ 133843cc0bc1SRobert Watson if (inp->inp_laddr.s_addr == INADDR_ANY || 133943cc0bc1SRobert Watson inp->inp_lport == 0 || 134043cc0bc1SRobert Watson sin->sin_addr.s_addr == INADDR_ANY || 134143cc0bc1SRobert Watson sin->sin_addr.s_addr == INADDR_BROADCAST) { 1342e06e816fSKevin Lo INP_HASH_LOCK_ASSERT(pcbinfo); 134343cc0bc1SRobert Watson error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, 134443cc0bc1SRobert Watson &lport, &faddr.s_addr, &fport, NULL, 134543cc0bc1SRobert Watson td->td_ucred); 134690162a4eSIan Dowse if (error) 134790162a4eSIan Dowse goto release; 134890162a4eSIan Dowse 134943cc0bc1SRobert Watson /* 135043cc0bc1SRobert Watson * XXXRW: Why not commit the port if the address is 135143cc0bc1SRobert Watson * !INADDR_ANY? 135243cc0bc1SRobert Watson */ 135390162a4eSIan Dowse /* Commit the local port if newly assigned. */ 135490162a4eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && 135590162a4eSIan Dowse inp->inp_lport == 0) { 1356c4d585aeSRobert Watson INP_WLOCK_ASSERT(inp); 1357e06e816fSKevin Lo INP_HASH_WLOCK_ASSERT(pcbinfo); 13583a1757b9SGleb Smirnoff /* 135943cc0bc1SRobert Watson * Remember addr if jailed, to prevent 136043cc0bc1SRobert Watson * rebinding. 13613a1757b9SGleb Smirnoff */ 13620304c731SJamie Gritton if (prison_flag(td->td_ucred, PR_IP4)) 13633a1757b9SGleb Smirnoff inp->inp_laddr = laddr; 136490162a4eSIan Dowse inp->inp_lport = lport; 136590162a4eSIan Dowse if (in_pcbinshash(inp) != 0) { 136690162a4eSIan Dowse inp->inp_lport = 0; 136790162a4eSIan Dowse error = EAGAIN; 1368df8bae1dSRodney W. Grimes goto release; 1369df8bae1dSRodney W. Grimes } 137090162a4eSIan Dowse inp->inp_flags |= INP_ANONPORT; 137190162a4eSIan Dowse } 1372df8bae1dSRodney W. Grimes } else { 137343cc0bc1SRobert Watson faddr = sin->sin_addr; 137443cc0bc1SRobert Watson fport = sin->sin_port; 137543cc0bc1SRobert Watson } 137643cc0bc1SRobert Watson } else { 1377c4d585aeSRobert Watson INP_LOCK_ASSERT(inp); 137890162a4eSIan Dowse faddr = inp->inp_faddr; 137990162a4eSIan Dowse fport = inp->inp_fport; 138090162a4eSIan Dowse if (faddr.s_addr == INADDR_ANY) { 1381df8bae1dSRodney W. Grimes error = ENOTCONN; 1382df8bae1dSRodney W. Grimes goto release; 1383df8bae1dSRodney W. Grimes } 1384df8bae1dSRodney W. Grimes } 1385e6ccd709SRobert Watson 1386df8bae1dSRodney W. Grimes /* 1387e6ccd709SRobert Watson * Calculate data length and get a mbuf for UDP, IP, and possible 1388392e8407SRobert Watson * link-layer headers. Immediate slide the data pointer back forward 1389392e8407SRobert Watson * since we won't use that space at this layer. 1390df8bae1dSRodney W. Grimes */ 1391eb1b1807SGleb Smirnoff M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT); 1392e6ccd709SRobert Watson if (m == NULL) { 1393df8bae1dSRodney W. Grimes error = ENOBUFS; 139449b19bfcSBruce M Simpson goto release; 1395df8bae1dSRodney W. Grimes } 1396e6ccd709SRobert Watson m->m_data += max_linkhdr; 1397e6ccd709SRobert Watson m->m_len -= max_linkhdr; 1398392e8407SRobert Watson m->m_pkthdr.len -= max_linkhdr; 1399df8bae1dSRodney W. Grimes 1400df8bae1dSRodney W. Grimes /* 14013329b236SRobert Watson * Fill in mbuf with extended UDP header and addresses and length put 14023329b236SRobert Watson * into network format. 1403df8bae1dSRodney W. Grimes */ 1404df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 1405db4f9cc7SJonathan Lemon bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 1406e06e816fSKevin Lo ui->ui_pr = pr; 140790162a4eSIan Dowse ui->ui_src = laddr; 140890162a4eSIan Dowse ui->ui_dst = faddr; 140990162a4eSIan Dowse ui->ui_sport = lport; 141090162a4eSIan Dowse ui->ui_dport = fport; 1411db4f9cc7SJonathan Lemon ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 1412e06e816fSKevin Lo if (pr == IPPROTO_UDPLITE) { 1413e06e816fSKevin Lo struct udpcb *up; 1414e06e816fSKevin Lo uint16_t plen; 1415e06e816fSKevin Lo 1416e06e816fSKevin Lo up = intoudpcb(inp); 1417e06e816fSKevin Lo cscov = up->u_txcslen; 1418e06e816fSKevin Lo plen = (u_short)len + sizeof(struct udphdr); 1419e06e816fSKevin Lo if (cscov >= plen) 1420e06e816fSKevin Lo cscov = 0; 1421e06e816fSKevin Lo ui->ui_len = htons(plen); 1422e06e816fSKevin Lo ui->ui_ulen = htons(cscov); 1423e06e816fSKevin Lo /* 1424e06e816fSKevin Lo * For UDP-Lite, checksum coverage length of zero means 1425e06e816fSKevin Lo * the entire UDPLite packet is covered by the checksum. 1426e06e816fSKevin Lo */ 1427e06e816fSKevin Lo cscov_partial = (cscov == 0) ? 0 : 1; 1428e06e816fSKevin Lo } else 1429e06e816fSKevin Lo ui->ui_v = IPVERSION << 4; 1430df8bae1dSRodney W. Grimes 1431b2828ad2SAndre Oppermann /* 1432b2828ad2SAndre Oppermann * Set the Don't Fragment bit in the IP header. 1433b2828ad2SAndre Oppermann */ 1434b2828ad2SAndre Oppermann if (inp->inp_flags & INP_DONTFRAG) { 1435b2828ad2SAndre Oppermann struct ip *ip; 14363329b236SRobert Watson 1437b2828ad2SAndre Oppermann ip = (struct ip *)&ui->ui_i; 14388f134647SGleb Smirnoff ip->ip_off |= htons(IP_DF); 1439b2828ad2SAndre Oppermann } 1440b2828ad2SAndre Oppermann 1441b5d47ff5SJohn-Mark Gurney ipflags = 0; 1442b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_DONTROUTE) 1443b5d47ff5SJohn-Mark Gurney ipflags |= IP_ROUTETOIF; 1444b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_BROADCAST) 1445b5d47ff5SJohn-Mark Gurney ipflags |= IP_ALLOWBROADCAST; 14466fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 14478afa2304SBruce M Simpson ipflags |= IP_SENDONES; 14488afa2304SBruce M Simpson 14491175d9d5SRobert Watson #ifdef MAC 14501175d9d5SRobert Watson mac_inpcb_create_mbuf(inp, m); 14511175d9d5SRobert Watson #endif 14521175d9d5SRobert Watson 1453df8bae1dSRodney W. Grimes /* 1454db4f9cc7SJonathan Lemon * Set up checksum and output datagram. 1455df8bae1dSRodney W. Grimes */ 1456e06e816fSKevin Lo ui->ui_sum = 0; 1457a485f139SMichael Tuexen if (pr == IPPROTO_UDPLITE) { 1458e06e816fSKevin Lo if (inp->inp_flags & INP_ONESBCAST) 1459e06e816fSKevin Lo faddr.s_addr = INADDR_BROADCAST; 1460a485f139SMichael Tuexen if (cscov_partial) { 1461e06e816fSKevin Lo if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0) 1462e06e816fSKevin Lo ui->ui_sum = 0xffff; 1463a485f139SMichael Tuexen } else { 1464a485f139SMichael Tuexen if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0) 1465a485f139SMichael Tuexen ui->ui_sum = 0xffff; 1466a485f139SMichael Tuexen } 1467a485f139SMichael Tuexen } else if (V_udp_cksum) { 14686fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 14698a538743SBruce M Simpson faddr.s_addr = INADDR_BROADCAST; 14708a538743SBruce M Simpson ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 1471e06e816fSKevin Lo htons((u_short)len + sizeof(struct udphdr) + pr)); 1472db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_UDP; 1473db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 1474e06e816fSKevin Lo } 14758f134647SGleb Smirnoff ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len); 1476ca98b82cSDavid Greenman ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 1477f584d74bSMichael Tuexen ((struct ip *)ui)->ip_tos = tos; /* XXX */ 1478026decb8SRobert Watson UDPSTAT_INC(udps_opackets); 1479cfa1ca9dSYoshinobu Inoue 14809d3ddf43SAdrian Chadd /* 14819d3ddf43SAdrian Chadd * Setup flowid / RSS information for outbound socket. 14829d3ddf43SAdrian Chadd * 14839d3ddf43SAdrian Chadd * Once the UDP code decides to set a flowid some other way, 14849d3ddf43SAdrian Chadd * this allows the flowid to be overridden by userland. 14859d3ddf43SAdrian Chadd */ 1486c2529042SHans Petter Selasky if (flowtype != M_HASHTYPE_NONE) { 14879d3ddf43SAdrian Chadd m->m_pkthdr.flowid = flowid; 1488c2529042SHans Petter Selasky M_HASHTYPE_SET(m, flowtype); 14898ad1a83bSAdrian Chadd #ifdef RSS 14908ad1a83bSAdrian Chadd } else { 14918ad1a83bSAdrian Chadd uint32_t hash_val, hash_type; 14928ad1a83bSAdrian Chadd /* 14938ad1a83bSAdrian Chadd * Calculate an appropriate RSS hash for UDP and 14948ad1a83bSAdrian Chadd * UDP Lite. 14958ad1a83bSAdrian Chadd * 14968ad1a83bSAdrian Chadd * The called function will take care of figuring out 14978ad1a83bSAdrian Chadd * whether a 2-tuple or 4-tuple hash is required based 14988ad1a83bSAdrian Chadd * on the currently configured scheme. 14998ad1a83bSAdrian Chadd * 15008ad1a83bSAdrian Chadd * Later later on connected socket values should be 15018ad1a83bSAdrian Chadd * cached in the inpcb and reused, rather than constantly 15028ad1a83bSAdrian Chadd * re-calculating it. 15038ad1a83bSAdrian Chadd * 15048ad1a83bSAdrian Chadd * UDP Lite is a different protocol number and will 15058ad1a83bSAdrian Chadd * likely end up being hashed as a 2-tuple until 15068ad1a83bSAdrian Chadd * RSS / NICs grow UDP Lite protocol awareness. 15078ad1a83bSAdrian Chadd */ 15088ad1a83bSAdrian Chadd if (rss_proto_software_hash_v4(faddr, laddr, fport, lport, 15098ad1a83bSAdrian Chadd pr, &hash_val, &hash_type) == 0) { 15108ad1a83bSAdrian Chadd m->m_pkthdr.flowid = hash_val; 15118ad1a83bSAdrian Chadd M_HASHTYPE_SET(m, hash_type); 15128ad1a83bSAdrian Chadd } 15138ad1a83bSAdrian Chadd #endif 15149d3ddf43SAdrian Chadd } 15159d3ddf43SAdrian Chadd 15169d3ddf43SAdrian Chadd #ifdef RSS 15178ad1a83bSAdrian Chadd /* 15188ad1a83bSAdrian Chadd * Don't override with the inp cached flowid value. 15198ad1a83bSAdrian Chadd * 15208ad1a83bSAdrian Chadd * Depending upon the kind of send being done, the inp 15218ad1a83bSAdrian Chadd * flowid/flowtype values may actually not be appropriate 15228ad1a83bSAdrian Chadd * for this particular socket send. 15238ad1a83bSAdrian Chadd * 15248ad1a83bSAdrian Chadd * We should either leave the flowid at zero (which is what is 15258ad1a83bSAdrian Chadd * currently done) or set it to some software generated 15268ad1a83bSAdrian Chadd * hash value based on the packet contents. 15278ad1a83bSAdrian Chadd */ 15289d3ddf43SAdrian Chadd ipflags |= IP_NODEFAULTFLOWID; 15299d3ddf43SAdrian Chadd #endif /* RSS */ 15309d3ddf43SAdrian Chadd 1531fa046d87SRobert Watson if (unlock_udbinfo == UH_WLOCKED) 1532e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1533fa046d87SRobert Watson else if (unlock_udbinfo == UH_RLOCKED) 1534e06e816fSKevin Lo INP_HASH_RUNLOCK(pcbinfo); 153557f60867SMark Johnston UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u); 153684cc0778SGeorge V. Neville-Neil error = ip_output(m, inp->inp_options, 153784cc0778SGeorge V. Neville-Neil (unlock_inp == UH_WLOCKED ? &inp->inp_route : NULL), ipflags, 15385d846453SSam Leffler inp->inp_moptions, inp); 153984cc0778SGeorge V. Neville-Neil if (unlock_inp == UH_WLOCKED) 15408501a69cSRobert Watson INP_WUNLOCK(inp); 1541948d0fc9SRobert Watson else 1542948d0fc9SRobert Watson INP_RUNLOCK(inp); 1543df8bae1dSRodney W. Grimes return (error); 1544df8bae1dSRodney W. Grimes 1545df8bae1dSRodney W. Grimes release: 1546fa046d87SRobert Watson if (unlock_udbinfo == UH_WLOCKED) { 154700b460ffSRick Macklem KASSERT(unlock_inp == UH_WLOCKED, 154800b460ffSRick Macklem ("%s: excl udbinfo lock, shared inp lock", __func__)); 1549e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1550948d0fc9SRobert Watson INP_WUNLOCK(inp); 1551fa046d87SRobert Watson } else if (unlock_udbinfo == UH_RLOCKED) { 155200b460ffSRick Macklem KASSERT(unlock_inp == UH_RLOCKED, 155300b460ffSRick Macklem ("%s: shared udbinfo lock, excl inp lock", __func__)); 1554e06e816fSKevin Lo INP_HASH_RUNLOCK(pcbinfo); 155543cc0bc1SRobert Watson INP_RUNLOCK(inp); 155600b460ffSRick Macklem } else if (unlock_inp == UH_WLOCKED) 155700b460ffSRick Macklem INP_WUNLOCK(inp); 155800b460ffSRick Macklem else 1559948d0fc9SRobert Watson INP_RUNLOCK(inp); 1560df8bae1dSRodney W. Grimes m_freem(m); 1561df8bae1dSRodney W. Grimes return (error); 1562df8bae1dSRodney W. Grimes } 1563df8bae1dSRodney W. Grimes 1564ac45e92fSRobert Watson static void 1565d0390e05SGarrett Wollman udp_abort(struct socket *so) 1566df8bae1dSRodney W. Grimes { 1567d0390e05SGarrett Wollman struct inpcb *inp; 1568e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1569df8bae1dSRodney W. Grimes 1570a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1571d0390e05SGarrett Wollman inp = sotoinpcb(so); 157214ba8addSRobert Watson KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 15738501a69cSRobert Watson INP_WLOCK(inp); 1574a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1575e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1576a152f8a3SRobert Watson in_pcbdisconnect(inp); 1577a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1578e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1579d0390e05SGarrett Wollman soisdisconnected(so); 1580a152f8a3SRobert Watson } 15818501a69cSRobert Watson INP_WUNLOCK(inp); 1582df8bae1dSRodney W. Grimes } 1583df8bae1dSRodney W. Grimes 1584d0390e05SGarrett Wollman static int 1585b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td) 1586d0390e05SGarrett Wollman { 1587d0390e05SGarrett Wollman struct inpcb *inp; 1588e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1589277afaffSRobert Watson int error; 1590d0390e05SGarrett Wollman 1591a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1592d0390e05SGarrett Wollman inp = sotoinpcb(so); 159314ba8addSRobert Watson KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 1594cfa1ca9dSYoshinobu Inoue error = soreserve(so, udp_sendspace, udp_recvspace); 1595f24618aaSRobert Watson if (error) 15963329b236SRobert Watson return (error); 1597e06e816fSKevin Lo INP_INFO_WLOCK(pcbinfo); 1598e06e816fSKevin Lo error = in_pcballoc(so, pcbinfo); 159953b57cd1SSam Leffler if (error) { 1600e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 16013329b236SRobert Watson return (error); 160253b57cd1SSam Leffler } 1603cfa1ca9dSYoshinobu Inoue 160468b5629bSRobert Watson inp = sotoinpcb(so); 1605cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1606603724d3SBjoern A. Zeeb inp->inp_ip_ttl = V_ip_defttl; 16076a9148feSBjoern A. Zeeb 16086a9148feSBjoern A. Zeeb error = udp_newudpcb(inp); 16096a9148feSBjoern A. Zeeb if (error) { 16106a9148feSBjoern A. Zeeb in_pcbdetach(inp); 16116a9148feSBjoern A. Zeeb in_pcbfree(inp); 1612e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 16136a9148feSBjoern A. Zeeb return (error); 16146a9148feSBjoern A. Zeeb } 16156a9148feSBjoern A. Zeeb 1616c7c7ea4bSRandall Stewart INP_WUNLOCK(inp); 1617e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 1618c7c7ea4bSRandall Stewart return (0); 1619c7c7ea4bSRandall Stewart } 162079288c11SBjoern A. Zeeb #endif /* INET */ 1621c7c7ea4bSRandall Stewart 1622c7c7ea4bSRandall Stewart int 1623abb901c5SRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx) 1624c7c7ea4bSRandall Stewart { 1625c7c7ea4bSRandall Stewart struct inpcb *inp; 16266a9148feSBjoern A. Zeeb struct udpcb *up; 1627c7c7ea4bSRandall Stewart 162868b5629bSRobert Watson KASSERT(so->so_type == SOCK_DGRAM, 162968b5629bSRobert Watson ("udp_set_kernel_tunneling: !dgram")); 163068b5629bSRobert Watson inp = sotoinpcb(so); 163168b5629bSRobert Watson KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL")); 1632c7c7ea4bSRandall Stewart INP_WLOCK(inp); 16336a9148feSBjoern A. Zeeb up = intoudpcb(inp); 1634abb901c5SRandall Stewart if ((up->u_tun_func != NULL) || 1635abb901c5SRandall Stewart (up->u_icmp_func != NULL)) { 1636bbb0e3d9SRandall Stewart INP_WUNLOCK(inp); 1637bbb0e3d9SRandall Stewart return (EBUSY); 1638bbb0e3d9SRandall Stewart } 16396a9148feSBjoern A. Zeeb up->u_tun_func = f; 1640abb901c5SRandall Stewart up->u_icmp_func = i; 164181d3ec17SBryan Venteicher up->u_tun_ctx = ctx; 16428501a69cSRobert Watson INP_WUNLOCK(inp); 16433329b236SRobert Watson return (0); 1644df8bae1dSRodney W. Grimes } 1645d0390e05SGarrett Wollman 164679288c11SBjoern A. Zeeb #ifdef INET 1647d0390e05SGarrett Wollman static int 1648b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1649d0390e05SGarrett Wollman { 1650d0390e05SGarrett Wollman struct inpcb *inp; 1651e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1652277afaffSRobert Watson int error; 1653d0390e05SGarrett Wollman 1654a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1655d0390e05SGarrett Wollman inp = sotoinpcb(so); 165614ba8addSRobert Watson KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 16578501a69cSRobert Watson INP_WLOCK(inp); 1658e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1659b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, nam, td->td_ucred); 1660e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 16618501a69cSRobert Watson INP_WUNLOCK(inp); 16623329b236SRobert Watson return (error); 1663d0390e05SGarrett Wollman } 1664d0390e05SGarrett Wollman 1665a152f8a3SRobert Watson static void 1666a152f8a3SRobert Watson udp_close(struct socket *so) 1667a152f8a3SRobert Watson { 1668a152f8a3SRobert Watson struct inpcb *inp; 1669e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1670a152f8a3SRobert Watson 1671a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1672a152f8a3SRobert Watson inp = sotoinpcb(so); 1673a152f8a3SRobert Watson KASSERT(inp != NULL, ("udp_close: inp == NULL")); 16748501a69cSRobert Watson INP_WLOCK(inp); 1675a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1676e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1677a152f8a3SRobert Watson in_pcbdisconnect(inp); 1678a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1679e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1680a152f8a3SRobert Watson soisdisconnected(so); 1681a152f8a3SRobert Watson } 16828501a69cSRobert Watson INP_WUNLOCK(inp); 1683a152f8a3SRobert Watson } 1684a152f8a3SRobert Watson 1685d0390e05SGarrett Wollman static int 1686b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1687d0390e05SGarrett Wollman { 1688d0390e05SGarrett Wollman struct inpcb *inp; 1689e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 169075c13541SPoul-Henning Kamp struct sockaddr_in *sin; 1691e06e816fSKevin Lo int error; 1692d0390e05SGarrett Wollman 1693a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1694d0390e05SGarrett Wollman inp = sotoinpcb(so); 169514ba8addSRobert Watson KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 16968501a69cSRobert Watson INP_WLOCK(inp); 1697f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr != INADDR_ANY) { 16988501a69cSRobert Watson INP_WUNLOCK(inp); 16993329b236SRobert Watson return (EISCONN); 1700f76fcf6dSJeffrey Hsu } 170175c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)nam; 1702b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1703b89e82ddSJamie Gritton if (error != 0) { 1704413628a7SBjoern A. Zeeb INP_WUNLOCK(inp); 1705b89e82ddSJamie Gritton return (error); 1706413628a7SBjoern A. Zeeb } 1707e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1708b0330ed9SPawel Jakub Dawidek error = in_pcbconnect(inp, nam, td->td_ucred); 1709e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 17104cc20ab1SSeigo Tanimura if (error == 0) 1711df8bae1dSRodney W. Grimes soisconnected(so); 17128501a69cSRobert Watson INP_WUNLOCK(inp); 17133329b236SRobert Watson return (error); 1714df8bae1dSRodney W. Grimes } 1715d0390e05SGarrett Wollman 1716bc725eafSRobert Watson static void 1717d0390e05SGarrett Wollman udp_detach(struct socket *so) 1718d0390e05SGarrett Wollman { 1719d0390e05SGarrett Wollman struct inpcb *inp; 1720e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 17216a9148feSBjoern A. Zeeb struct udpcb *up; 1722d0390e05SGarrett Wollman 1723a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1724d0390e05SGarrett Wollman inp = sotoinpcb(so); 172514ba8addSRobert Watson KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1726a152f8a3SRobert Watson KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1727a152f8a3SRobert Watson ("udp_detach: not disconnected")); 1728e06e816fSKevin Lo INP_INFO_WLOCK(pcbinfo); 17298501a69cSRobert Watson INP_WLOCK(inp); 17306a9148feSBjoern A. Zeeb up = intoudpcb(inp); 17316a9148feSBjoern A. Zeeb KASSERT(up != NULL, ("%s: up == NULL", __func__)); 17326a9148feSBjoern A. Zeeb inp->inp_ppcb = NULL; 1733d0390e05SGarrett Wollman in_pcbdetach(inp); 173414ba8addSRobert Watson in_pcbfree(inp); 1735e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 17366a9148feSBjoern A. Zeeb udp_discardcb(up); 1737d0390e05SGarrett Wollman } 1738d0390e05SGarrett Wollman 1739d0390e05SGarrett Wollman static int 1740d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 1741d0390e05SGarrett Wollman { 1742d0390e05SGarrett Wollman struct inpcb *inp; 1743e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1744d0390e05SGarrett Wollman 1745a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1746d0390e05SGarrett Wollman inp = sotoinpcb(so); 174714ba8addSRobert Watson KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 17488501a69cSRobert Watson INP_WLOCK(inp); 1749f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr == INADDR_ANY) { 17508501a69cSRobert Watson INP_WUNLOCK(inp); 17513329b236SRobert Watson return (ENOTCONN); 1752f76fcf6dSJeffrey Hsu } 1753e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1754df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 1755df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 1756e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1757d45e4f99SMaxim Konovalov SOCK_LOCK(so); 1758d45e4f99SMaxim Konovalov so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1759d45e4f99SMaxim Konovalov SOCK_UNLOCK(so); 17608501a69cSRobert Watson INP_WUNLOCK(inp); 17613329b236SRobert Watson return (0); 1762df8bae1dSRodney W. Grimes } 1763df8bae1dSRodney W. Grimes 1764d0390e05SGarrett Wollman static int 176557bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1766b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1767d0390e05SGarrett Wollman { 1768d0390e05SGarrett Wollman struct inpcb *inp; 1769d0390e05SGarrett Wollman 1770d0390e05SGarrett Wollman inp = sotoinpcb(so); 177114ba8addSRobert Watson KASSERT(inp != NULL, ("udp_send: inp == NULL")); 17723329b236SRobert Watson return (udp_output(inp, m, addr, control, td)); 1773d0390e05SGarrett Wollman } 177479288c11SBjoern A. Zeeb #endif /* INET */ 1775d0390e05SGarrett Wollman 177676429de4SYoshinobu Inoue int 1777d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 1778d0390e05SGarrett Wollman { 1779d0390e05SGarrett Wollman struct inpcb *inp; 1780d0390e05SGarrett Wollman 1781d0390e05SGarrett Wollman inp = sotoinpcb(so); 178214ba8addSRobert Watson KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 17838501a69cSRobert Watson INP_WLOCK(inp); 1784d0390e05SGarrett Wollman socantsendmore(so); 17858501a69cSRobert Watson INP_WUNLOCK(inp); 17863329b236SRobert Watson return (0); 1787d0390e05SGarrett Wollman } 1788d0390e05SGarrett Wollman 178979288c11SBjoern A. Zeeb #ifdef INET 1790d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 1791756d52a1SPoul-Henning Kamp .pru_abort = udp_abort, 1792756d52a1SPoul-Henning Kamp .pru_attach = udp_attach, 1793756d52a1SPoul-Henning Kamp .pru_bind = udp_bind, 1794756d52a1SPoul-Henning Kamp .pru_connect = udp_connect, 1795756d52a1SPoul-Henning Kamp .pru_control = in_control, 1796756d52a1SPoul-Henning Kamp .pru_detach = udp_detach, 1797756d52a1SPoul-Henning Kamp .pru_disconnect = udp_disconnect, 179854d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1799756d52a1SPoul-Henning Kamp .pru_send = udp_send, 18005df3e839SRobert Watson .pru_soreceive = soreceive_dgram, 180159b8854eSRobert Watson .pru_sosend = sosend_dgram, 1802756d52a1SPoul-Henning Kamp .pru_shutdown = udp_shutdown, 180354d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1804a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1805a152f8a3SRobert Watson .pru_close = udp_close, 1806d0390e05SGarrett Wollman }; 180779288c11SBjoern A. Zeeb #endif /* INET */ 1808