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" 479d3ddf43SAdrian Chadd #include "opt_rss.h" 48cfa1ca9dSYoshinobu Inoue 49df8bae1dSRodney W. Grimes #include <sys/param.h> 50960ed29cSSeigo Tanimura #include <sys/domain.h> 514f590175SPaul Saab #include <sys/eventhandler.h> 52960ed29cSSeigo Tanimura #include <sys/jail.h> 53b110a8a2SGarrett Wollman #include <sys/kernel.h> 54960ed29cSSeigo Tanimura #include <sys/lock.h> 55df8bae1dSRodney W. Grimes #include <sys/malloc.h> 56df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 57acd3428bSRobert Watson #include <sys/priv.h> 58490d50b6SBrian Feldman #include <sys/proc.h> 59df8bae1dSRodney W. Grimes #include <sys/protosw.h> 6057f60867SMark Johnston #include <sys/sdt.h> 61960ed29cSSeigo Tanimura #include <sys/signalvar.h> 62df8bae1dSRodney W. Grimes #include <sys/socket.h> 63df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 64960ed29cSSeigo Tanimura #include <sys/sx.h> 65b5e8ce9fSBruce Evans #include <sys/sysctl.h> 66816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 67f5514f08SRobert Watson #include <sys/systm.h> 688781d8e9SBruce Evans 6969c2d429SJeff Roberson #include <vm/uma.h> 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes #include <net/if.h> 7276039bc8SGleb Smirnoff #include <net/if_var.h> 73df8bae1dSRodney W. Grimes #include <net/route.h> 74b2bdc62aSAdrian Chadd #include <net/rss_config.h> 75df8bae1dSRodney W. Grimes 76df8bae1dSRodney W. Grimes #include <netinet/in.h> 7757f60867SMark Johnston #include <netinet/in_kdtrace.h> 78960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 79f5514f08SRobert Watson #include <netinet/in_systm.h> 80960ed29cSSeigo Tanimura #include <netinet/in_var.h> 81df8bae1dSRodney W. Grimes #include <netinet/ip.h> 82cfa1ca9dSYoshinobu Inoue #ifdef INET6 83cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 84cfa1ca9dSYoshinobu Inoue #endif 85960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h> 86960ed29cSSeigo Tanimura #include <netinet/icmp_var.h> 87df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 88ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 89cfa1ca9dSYoshinobu Inoue #ifdef INET6 90cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 91cfa1ca9dSYoshinobu Inoue #endif 92df8bae1dSRodney W. Grimes #include <netinet/udp.h> 93df8bae1dSRodney W. Grimes #include <netinet/udp_var.h> 94e06e816fSKevin Lo #include <netinet/udplite.h> 958ad1a83bSAdrian Chadd #include <netinet/in_rss.h> 96df8bae1dSRodney W. Grimes 97fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 98b9234fafSSam Leffler 99db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 100db4f9cc7SJonathan Lemon 101aed55708SRobert Watson #include <security/mac/mac_framework.h> 102aed55708SRobert Watson 103df8bae1dSRodney W. Grimes /* 104e06e816fSKevin Lo * UDP and UDP-Lite protocols implementation. 105df8bae1dSRodney W. Grimes * Per RFC 768, August, 1980. 106e06e816fSKevin Lo * Per RFC 3828, July, 2004. 107df8bae1dSRodney W. Grimes */ 10874eb3236SWarner Losh 10974eb3236SWarner Losh /* 1103329b236SRobert Watson * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums 1113329b236SRobert Watson * removes the only data integrity mechanism for packets and malformed 112f5514f08SRobert Watson * packets that would otherwise be discarded due to bad checksums, and may 113f5514f08SRobert Watson * cause problems (especially for NFS data blocks). 11474eb3236SWarner Losh */ 11540b676beSBjoern A. Zeeb VNET_DEFINE(int, udp_cksum) = 1; 1166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW, 11740b676beSBjoern A. Zeeb &VNET_NAME(udp_cksum), 0, "compute udp checksum"); 118df8bae1dSRodney W. Grimes 119afdb4274SRobert Watson int udp_log_in_vain = 0; 120816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 121afdb4274SRobert Watson &udp_log_in_vain, 0, "Log all incoming UDP packets"); 122816a3d83SPoul-Henning Kamp 12382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, udp_blackhole) = 0; 1246df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 125eddfbb76SRobert Watson &VNET_NAME(udp_blackhole), 0, 1263329b236SRobert Watson "Do not send port unreachables for refused connects"); 12716f7f31fSGeoff Rehmet 12843bbb6aaSRobert Watson u_long udp_sendspace = 9216; /* really max datagram size */ 12943bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 13043bbb6aaSRobert Watson &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 13143bbb6aaSRobert Watson 13243bbb6aaSRobert Watson u_long udp_recvspace = 40 * (1024 + 13343bbb6aaSRobert Watson #ifdef INET6 13443bbb6aaSRobert Watson sizeof(struct sockaddr_in6) 13543bbb6aaSRobert Watson #else 13643bbb6aaSRobert Watson sizeof(struct sockaddr_in) 13743bbb6aaSRobert Watson #endif 138e62b9bcaSSergey Kandaurov ); /* 40 1K datagrams */ 13943bbb6aaSRobert Watson 14043bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 14143bbb6aaSRobert Watson &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 14243bbb6aaSRobert Watson 143eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, udb); /* from udp_var.h */ 144eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, udbinfo); 145e06e816fSKevin Lo VNET_DEFINE(struct inpcbhead, ulitecb); 146e06e816fSKevin Lo VNET_DEFINE(struct inpcbinfo, ulitecbinfo); 1475f901c92SAndrew Turner VNET_DEFINE_STATIC(uma_zone_t, udpcb_zone); 1481e77c105SRobert Watson #define V_udpcb_zone VNET(udpcb_zone) 14915bd2b43SDavid Greenman 15015bd2b43SDavid Greenman #ifndef UDBHASHSIZE 151e2ed8f35SAlexander Motin #define UDBHASHSIZE 128 15215bd2b43SDavid Greenman #endif 15315bd2b43SDavid Greenman 1545b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat); /* from udp_var.h */ 1555b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(udpstat); 1565b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat, 1575b7cb97cSAndrey V. Elsukov udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 158f2ea20e6SGarrett Wollman 1595b7cb97cSAndrey V. Elsukov #ifdef VIMAGE 1605b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(udpstat); 1615b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */ 16279288c11SBjoern A. Zeeb #ifdef INET 163bc725eafSRobert Watson static void udp_detach(struct socket *so); 1644d77a549SAlfred Perlstein static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 1654d77a549SAlfred Perlstein struct mbuf *, struct thread *); 16679288c11SBjoern A. Zeeb #endif 16779288c11SBjoern A. Zeeb 1684f590175SPaul Saab static void 1694f590175SPaul Saab udp_zone_change(void *tag) 1704f590175SPaul Saab { 1714f590175SPaul Saab 172603724d3SBjoern A. Zeeb uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 1736a9148feSBjoern A. Zeeb uma_zone_set_max(V_udpcb_zone, maxsockets); 1744f590175SPaul Saab } 1754f590175SPaul Saab 176d915b280SStephan Uphoff static int 177d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags) 178d915b280SStephan Uphoff { 179af1ee11dSRobert Watson struct inpcb *inp; 18008651e1fSJohn Baldwin 181af1ee11dSRobert Watson inp = mem; 182d915b280SStephan Uphoff INP_LOCK_INIT(inp, "inp", "udpinp"); 183d915b280SStephan Uphoff return (0); 184d915b280SStephan Uphoff } 185d915b280SStephan Uphoff 186e06e816fSKevin Lo static int 187e06e816fSKevin Lo udplite_inpcb_init(void *mem, int size, int flags) 188e06e816fSKevin Lo { 189e06e816fSKevin Lo struct inpcb *inp; 190e06e816fSKevin Lo 191e06e816fSKevin Lo inp = mem; 192e06e816fSKevin Lo INP_LOCK_INIT(inp, "inp", "udpliteinp"); 193e06e816fSKevin Lo return (0); 194e06e816fSKevin Lo } 195e06e816fSKevin Lo 196df8bae1dSRodney W. Grimes void 197af1ee11dSRobert Watson udp_init(void) 198df8bae1dSRodney W. Grimes { 199af1ee11dSRobert Watson 2008ad1a83bSAdrian Chadd /* 2018ad1a83bSAdrian Chadd * For now default to 2-tuple UDP hashing - until the fragment 2028ad1a83bSAdrian Chadd * reassembly code can also update the flowid. 2038ad1a83bSAdrian Chadd * 2048ad1a83bSAdrian Chadd * Once we can calculate the flowid that way and re-establish 2058ad1a83bSAdrian Chadd * a 4-tuple, flip this to 4-tuple. 2068ad1a83bSAdrian Chadd */ 2079bcd427bSRobert Watson in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE, 208cc487c16SGleb Smirnoff "udp_inpcb", udp_inpcb_init, IPI_HASHFIELDS_2TUPLE); 2096a9148feSBjoern A. Zeeb V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb), 210a8da5dd6SCraig Rodrigues NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2116a9148feSBjoern A. Zeeb uma_zone_set_max(V_udpcb_zone, maxsockets); 2126acd596eSPawel Jakub Dawidek uma_zone_set_warning(V_udpcb_zone, "kern.ipc.maxsockets limit reached"); 2134f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL, 2144f590175SPaul Saab EVENTHANDLER_PRI_ANY); 215df8bae1dSRodney W. Grimes } 216df8bae1dSRodney W. Grimes 217e06e816fSKevin Lo void 218e06e816fSKevin Lo udplite_init(void) 219e06e816fSKevin Lo { 220e06e816fSKevin Lo 221e06e816fSKevin Lo in_pcbinfo_init(&V_ulitecbinfo, "udplite", &V_ulitecb, UDBHASHSIZE, 222cc487c16SGleb Smirnoff UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init, 223cc487c16SGleb Smirnoff IPI_HASHFIELDS_2TUPLE); 224e06e816fSKevin Lo } 225e06e816fSKevin Lo 226315e3e38SRobert Watson /* 227315e3e38SRobert Watson * Kernel module interface for updating udpstat. The argument is an index 228315e3e38SRobert Watson * into udpstat treated as an array of u_long. While this encodes the 229315e3e38SRobert Watson * general layout of udpstat into the caller, it doesn't encode its location, 230315e3e38SRobert Watson * so that future changes to add, for example, per-CPU stats support won't 231315e3e38SRobert Watson * cause binary compatibility problems for kernel modules. 232315e3e38SRobert Watson */ 233315e3e38SRobert Watson void 234315e3e38SRobert Watson kmod_udpstat_inc(int statnum) 235315e3e38SRobert Watson { 236315e3e38SRobert Watson 2375b7cb97cSAndrey V. Elsukov counter_u64_add(VNET(udpstat)[statnum], 1); 238315e3e38SRobert Watson } 239315e3e38SRobert Watson 2406a9148feSBjoern A. Zeeb int 2416a9148feSBjoern A. Zeeb udp_newudpcb(struct inpcb *inp) 2426a9148feSBjoern A. Zeeb { 2436a9148feSBjoern A. Zeeb struct udpcb *up; 2446a9148feSBjoern A. Zeeb 2456a9148feSBjoern A. Zeeb up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO); 2466a9148feSBjoern A. Zeeb if (up == NULL) 2476a9148feSBjoern A. Zeeb return (ENOBUFS); 2486a9148feSBjoern A. Zeeb inp->inp_ppcb = up; 2496a9148feSBjoern A. Zeeb return (0); 2506a9148feSBjoern A. Zeeb } 2516a9148feSBjoern A. Zeeb 2526a9148feSBjoern A. Zeeb void 2536a9148feSBjoern A. Zeeb udp_discardcb(struct udpcb *up) 2546a9148feSBjoern A. Zeeb { 2556a9148feSBjoern A. Zeeb 2566a9148feSBjoern A. Zeeb uma_zfree(V_udpcb_zone, up); 2576a9148feSBjoern A. Zeeb } 2586a9148feSBjoern A. Zeeb 259bc29160dSMarko Zec #ifdef VIMAGE 2603f58662dSBjoern A. Zeeb static void 2613f58662dSBjoern A. Zeeb udp_destroy(void *unused __unused) 262bc29160dSMarko Zec { 263bc29160dSMarko Zec 2649bcd427bSRobert Watson in_pcbinfo_destroy(&V_udbinfo); 265391dab1cSBjoern A. Zeeb uma_zdestroy(V_udpcb_zone); 266bc29160dSMarko Zec } 2673f58662dSBjoern A. Zeeb VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL); 268e06e816fSKevin Lo 2693f58662dSBjoern A. Zeeb static void 2703f58662dSBjoern A. Zeeb udplite_destroy(void *unused __unused) 271e06e816fSKevin Lo { 272e06e816fSKevin Lo 273e06e816fSKevin Lo in_pcbinfo_destroy(&V_ulitecbinfo); 274e06e816fSKevin Lo } 2753f58662dSBjoern A. Zeeb VNET_SYSUNINIT(udplite, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udplite_destroy, 2763f58662dSBjoern A. Zeeb NULL); 277bc29160dSMarko Zec #endif 278bc29160dSMarko Zec 27979288c11SBjoern A. Zeeb #ifdef INET 28043bbb6aaSRobert Watson /* 28143bbb6aaSRobert Watson * Subroutine of udp_input(), which appends the provided mbuf chain to the 28243bbb6aaSRobert Watson * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 28343bbb6aaSRobert Watson * contains the source address. If the socket ends up being an IPv6 socket, 28443bbb6aaSRobert Watson * udp_append() will convert to a sockaddr_in6 before passing the address 28543bbb6aaSRobert Watson * into the socket code. 286c0d1be08SRandall Stewart * 287c0d1be08SRandall Stewart * In the normal case udp_append() will return 0, indicating that you 288c0d1be08SRandall Stewart * must unlock the inp. However if a tunneling protocol is in place we increment 289c0d1be08SRandall Stewart * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we 290c0d1be08SRandall Stewart * then decrement the reference count. If the inp_rele returns 1, indicating the 291c0d1be08SRandall Stewart * inp is gone, we return that to the caller to tell them *not* to unlock 292c0d1be08SRandall Stewart * the inp. In the case of multi-cast this will cause the distribution 293c0d1be08SRandall Stewart * to stop (though most tunneling protocols known currently do *not* use 294c0d1be08SRandall Stewart * multicast). 29543bbb6aaSRobert Watson */ 296c0d1be08SRandall Stewart static int 29743bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off, 29843bbb6aaSRobert Watson struct sockaddr_in *udp_in) 29943bbb6aaSRobert Watson { 30043bbb6aaSRobert Watson struct sockaddr *append_sa; 30143bbb6aaSRobert Watson struct socket *so; 302dce33a45SErmal Luçi struct mbuf *tmpopts, *opts = NULL; 30343bbb6aaSRobert Watson #ifdef INET6 30443bbb6aaSRobert Watson struct sockaddr_in6 udp_in6; 30543bbb6aaSRobert Watson #endif 3067b495c44SVANHULLEBUS Yvan struct udpcb *up; 30743bbb6aaSRobert Watson 308fa046d87SRobert Watson INP_LOCK_ASSERT(inp); 30943bbb6aaSRobert Watson 31079bb84fbSEdward Tomasz Napierala /* 31179bb84fbSEdward Tomasz Napierala * Engage the tunneling protocol. 31279bb84fbSEdward Tomasz Napierala */ 31379bb84fbSEdward Tomasz Napierala up = intoudpcb(inp); 31479bb84fbSEdward Tomasz Napierala if (up->u_tun_func != NULL) { 315c0d1be08SRandall Stewart in_pcbref(inp); 316c0d1be08SRandall Stewart INP_RUNLOCK(inp); 317dce33a45SErmal Luçi (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&udp_in[0], 31881d3ec17SBryan Venteicher up->u_tun_ctx); 319c0d1be08SRandall Stewart INP_RLOCK(inp); 320c0d1be08SRandall Stewart return (in_pcbrele_rlocked(inp)); 32179bb84fbSEdward Tomasz Napierala } 32279bb84fbSEdward Tomasz Napierala 32379bb84fbSEdward Tomasz Napierala off += sizeof(struct udphdr); 32479bb84fbSEdward Tomasz Napierala 325fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 32643bbb6aaSRobert Watson /* Check AH/ESP integrity. */ 327fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 328fcf59617SAndrey V. Elsukov IPSEC_CHECK_POLICY(ipv4, n, inp) != 0) { 32943bbb6aaSRobert Watson m_freem(n); 330c0d1be08SRandall Stewart return (0); 33143bbb6aaSRobert Watson } 332fcf59617SAndrey V. Elsukov if (up->u_flags & UF_ESPINUDP) {/* IPSec UDP encaps. */ 333fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4) && 334fcf59617SAndrey V. Elsukov UDPENCAP_INPUT(n, off, AF_INET) != 0) 335fcf59617SAndrey V. Elsukov return (0); /* Consumed. */ 3367b495c44SVANHULLEBUS Yvan } 33743bbb6aaSRobert Watson #endif /* IPSEC */ 33843bbb6aaSRobert Watson #ifdef MAC 33930d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, n) != 0) { 34043bbb6aaSRobert Watson m_freem(n); 341c0d1be08SRandall Stewart return (0); 34243bbb6aaSRobert Watson } 34379288c11SBjoern A. Zeeb #endif /* MAC */ 34443bbb6aaSRobert Watson if (inp->inp_flags & INP_CONTROLOPTS || 34543bbb6aaSRobert Watson inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 34643bbb6aaSRobert Watson #ifdef INET6 3479a38ba81SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6) 34848d48eb9SBjoern A. Zeeb (void)ip6_savecontrol_v4(inp, n, &opts, NULL); 3499a38ba81SBjoern A. Zeeb else 35079288c11SBjoern A. Zeeb #endif /* INET6 */ 35143bbb6aaSRobert Watson ip_savecontrol(inp, &opts, ip, n); 35243bbb6aaSRobert Watson } 353dce33a45SErmal Luçi if ((inp->inp_vflag & INP_IPV4) && (inp->inp_flags2 & INP_ORIGDSTADDR)) { 354dce33a45SErmal Luçi tmpopts = sbcreatecontrol((caddr_t)&udp_in[1], 355dce33a45SErmal Luçi sizeof(struct sockaddr_in), IP_ORIGDSTADDR, IPPROTO_IP); 356dce33a45SErmal Luçi if (tmpopts) { 357dce33a45SErmal Luçi if (opts) { 358dce33a45SErmal Luçi tmpopts->m_next = opts; 359dce33a45SErmal Luçi opts = tmpopts; 360dce33a45SErmal Luçi } else 361dce33a45SErmal Luçi opts = tmpopts; 362dce33a45SErmal Luçi } 363dce33a45SErmal Luçi } 36443bbb6aaSRobert Watson #ifdef INET6 36543bbb6aaSRobert Watson if (inp->inp_vflag & INP_IPV6) { 36643bbb6aaSRobert Watson bzero(&udp_in6, sizeof(udp_in6)); 36743bbb6aaSRobert Watson udp_in6.sin6_len = sizeof(udp_in6); 36843bbb6aaSRobert Watson udp_in6.sin6_family = AF_INET6; 369dce33a45SErmal Luçi in6_sin_2_v4mapsin6(&udp_in[0], &udp_in6); 37043bbb6aaSRobert Watson append_sa = (struct sockaddr *)&udp_in6; 37143bbb6aaSRobert Watson } else 37279288c11SBjoern A. Zeeb #endif /* INET6 */ 373dce33a45SErmal Luçi append_sa = (struct sockaddr *)&udp_in[0]; 37443bbb6aaSRobert Watson m_adj(n, off); 37543bbb6aaSRobert Watson 37643bbb6aaSRobert Watson so = inp->inp_socket; 37743bbb6aaSRobert Watson SOCKBUF_LOCK(&so->so_rcv); 37843bbb6aaSRobert Watson if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 37943bbb6aaSRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 38043bbb6aaSRobert Watson m_freem(n); 38143bbb6aaSRobert Watson if (opts) 38243bbb6aaSRobert Watson m_freem(opts); 383026decb8SRobert Watson UDPSTAT_INC(udps_fullsock); 38443bbb6aaSRobert Watson } else 38543bbb6aaSRobert Watson sorwakeup_locked(so); 386c0d1be08SRandall Stewart return (0); 38743bbb6aaSRobert Watson } 38843bbb6aaSRobert Watson 3898f5a8818SKevin Lo int 3908f5a8818SKevin Lo udp_input(struct mbuf **mp, int *offp, int proto) 391df8bae1dSRodney W. Grimes { 3923329b236SRobert Watson struct ip *ip; 3933329b236SRobert Watson struct udphdr *uh; 39471498f30SBruce M Simpson struct ifnet *ifp; 3953329b236SRobert Watson struct inpcb *inp; 3968f134647SGleb Smirnoff uint16_t len, ip_len; 397e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 398df8bae1dSRodney W. Grimes struct ip save_ip; 399dce33a45SErmal Luçi struct sockaddr_in udp_in[2]; 4008f5a8818SKevin Lo struct mbuf *m; 4010b4ae859SGleb Smirnoff struct m_tag *fwd_tag; 4026573d758SMatt Macy struct epoch_tracker et; 4038f5a8818SKevin Lo int cscov_partial, iphlen; 404df8bae1dSRodney W. Grimes 4058f5a8818SKevin Lo m = *mp; 4068f5a8818SKevin Lo iphlen = *offp; 40771498f30SBruce M Simpson ifp = m->m_pkthdr.rcvif; 4088f5a8818SKevin Lo *mp = NULL; 409026decb8SRobert Watson UDPSTAT_INC(udps_ipackets); 410df8bae1dSRodney W. Grimes 411df8bae1dSRodney W. Grimes /* 4123329b236SRobert Watson * Strip IP options, if any; should skip this, make available to 4133329b236SRobert Watson * user, and use on returned packets, but we don't yet have a way to 4143329b236SRobert Watson * check the checksum with options still present. 415df8bae1dSRodney W. Grimes */ 416df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 417105bd211SGleb Smirnoff ip_stripoptions(m); 418df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 419df8bae1dSRodney W. Grimes } 420df8bae1dSRodney W. Grimes 421df8bae1dSRodney W. Grimes /* 422df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 423df8bae1dSRodney W. Grimes */ 424df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 425df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 426d1b18731SKevin Lo if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) { 427026decb8SRobert Watson UDPSTAT_INC(udps_hdrops); 4288f5a8818SKevin Lo return (IPPROTO_DONE); 429df8bae1dSRodney W. Grimes } 430df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 431df8bae1dSRodney W. Grimes } 432df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 4338f5a8818SKevin Lo cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0; 434df8bae1dSRodney W. Grimes 4353329b236SRobert Watson /* 4363329b236SRobert Watson * Destination port of 0 is illegal, based on RFC768. 4373329b236SRobert Watson */ 438686cdd19SJun-ichiro itojun Hagino if (uh->uh_dport == 0) 439f76fcf6dSJeffrey Hsu goto badunlocked; 440686cdd19SJun-ichiro itojun Hagino 441df8bae1dSRodney W. Grimes /* 4423329b236SRobert Watson * Construct sockaddr format source address. Stuff source address 4433329b236SRobert Watson * and datagram in user buffer. 444b9234fafSSam Leffler */ 445dce33a45SErmal Luçi bzero(&udp_in[0], sizeof(struct sockaddr_in) * 2); 446dce33a45SErmal Luçi udp_in[0].sin_len = sizeof(struct sockaddr_in); 447dce33a45SErmal Luçi udp_in[0].sin_family = AF_INET; 448dce33a45SErmal Luçi udp_in[0].sin_port = uh->uh_sport; 449dce33a45SErmal Luçi udp_in[0].sin_addr = ip->ip_src; 450dce33a45SErmal Luçi udp_in[1].sin_len = sizeof(struct sockaddr_in); 451dce33a45SErmal Luçi udp_in[1].sin_family = AF_INET; 452dce33a45SErmal Luçi udp_in[1].sin_port = uh->uh_dport; 453dce33a45SErmal Luçi udp_in[1].sin_addr = ip->ip_dst; 454b9234fafSSam Leffler 455b9234fafSSam Leffler /* 456af1ee11dSRobert Watson * Make mbuf data length reflect UDP length. If not enough data to 457af1ee11dSRobert Watson * reflect UDP length, drop. 458df8bae1dSRodney W. Grimes */ 459df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 4608ad458a4SGleb Smirnoff ip_len = ntohs(ip->ip_len) - iphlen; 4610f4a0366SMichael Tuexen if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) { 462e06e816fSKevin Lo /* Zero means checksum over the complete packet. */ 4630f4a0366SMichael Tuexen if (len == 0) 464e06e816fSKevin Lo len = ip_len; 465e06e816fSKevin Lo cscov_partial = 0; 466e06e816fSKevin Lo } 4678f134647SGleb Smirnoff if (ip_len != len) { 4688f134647SGleb Smirnoff if (len > ip_len || len < sizeof(struct udphdr)) { 469026decb8SRobert Watson UDPSTAT_INC(udps_badlen); 470f76fcf6dSJeffrey Hsu goto badunlocked; 471df8bae1dSRodney W. Grimes } 4728f5a8818SKevin Lo if (proto == IPPROTO_UDP) 4738f134647SGleb Smirnoff m_adj(m, len - ip_len); 474df8bae1dSRodney W. Grimes } 4753329b236SRobert Watson 476df8bae1dSRodney W. Grimes /* 4773329b236SRobert Watson * Save a copy of the IP header in case we want restore it for 4783329b236SRobert Watson * sending an ICMP error message in response. 479df8bae1dSRodney W. Grimes */ 480603724d3SBjoern A. Zeeb if (!V_udp_blackhole) 481df8bae1dSRodney W. Grimes save_ip = *ip; 482cce418d3SMatt Jacob else 483cce418d3SMatt Jacob memset(&save_ip, 0, sizeof(save_ip)); 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes /* 486df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 487df8bae1dSRodney W. Grimes */ 4886dfab5b1SGarrett Wollman if (uh->uh_sum) { 48939629c92SDavid Malone u_short uh_sum; 49039629c92SDavid Malone 491e06e816fSKevin Lo if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) && 492e06e816fSKevin Lo !cscov_partial) { 493db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 49439629c92SDavid Malone uh_sum = m->m_pkthdr.csum_data; 495db4f9cc7SJonathan Lemon else 49639629c92SDavid Malone uh_sum = in_pseudo(ip->ip_src.s_addr, 497506f4949SRuslan Ermilov ip->ip_dst.s_addr, htonl((u_short)len + 4988f5a8818SKevin Lo m->m_pkthdr.csum_data + proto)); 49939629c92SDavid Malone uh_sum ^= 0xffff; 500db4f9cc7SJonathan Lemon } else { 501cb342100SHajimu UMEMOTO char b[9]; 502af1ee11dSRobert Watson 503cb342100SHajimu UMEMOTO bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 5046effc713SDoug Rabson bzero(((struct ipovly *)ip)->ih_x1, 9); 5058f5a8818SKevin Lo ((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ? 506e06e816fSKevin Lo uh->uh_ulen : htons(ip_len); 50739629c92SDavid Malone uh_sum = in_cksum(m, len + sizeof (struct ip)); 508cb342100SHajimu UMEMOTO bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 509db4f9cc7SJonathan Lemon } 51039629c92SDavid Malone if (uh_sum) { 511026decb8SRobert Watson UDPSTAT_INC(udps_badsum); 512df8bae1dSRodney W. Grimes m_freem(m); 5138f5a8818SKevin Lo return (IPPROTO_DONE); 514df8bae1dSRodney W. Grimes } 515c6d81a34SMichael Tuexen } else { 516c6d81a34SMichael Tuexen if (proto == IPPROTO_UDP) { 517026decb8SRobert Watson UDPSTAT_INC(udps_nosum); 518c6d81a34SMichael Tuexen } else { 519c6d81a34SMichael Tuexen /* UDPLite requires a checksum */ 520c6d81a34SMichael Tuexen /* XXX: What is the right UDPLite MIB counter here? */ 521c6d81a34SMichael Tuexen m_freem(m); 522c6d81a34SMichael Tuexen return (IPPROTO_DONE); 523c6d81a34SMichael Tuexen } 524c6d81a34SMichael Tuexen } 525df8bae1dSRodney W. Grimes 526a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(proto); 527df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 5284af540d1SRyan Stone in_broadcast(ip->ip_dst, ifp)) { 52982c23ebaSBill Fenner struct inpcb *last; 530e06e816fSKevin Lo struct inpcbhead *pcblist; 5313329b236SRobert Watson 5326573d758SMatt Macy INP_INFO_RLOCK_ET(pcbinfo, et); 533a86e5c96SBjoern A. Zeeb pcblist = udp_get_pcblist(proto); 534df8bae1dSRodney W. Grimes last = NULL; 535b872626dSMatt Macy CK_LIST_FOREACH(inp, pcblist, inp_list) { 5369c1df695SRobert Watson if (inp->inp_lport != uh->uh_dport) 537f76fcf6dSJeffrey Hsu continue; 538cfa1ca9dSYoshinobu Inoue #ifdef INET6 539369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 5409c1df695SRobert Watson continue; 541cfa1ca9dSYoshinobu Inoue #endif 54271498f30SBruce M Simpson if (inp->inp_laddr.s_addr != INADDR_ANY && 54371498f30SBruce M Simpson inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 5449c1df695SRobert Watson continue; 54571498f30SBruce M Simpson if (inp->inp_faddr.s_addr != INADDR_ANY && 54671498f30SBruce M Simpson inp->inp_faddr.s_addr != ip->ip_src.s_addr) 54771498f30SBruce M Simpson continue; 54871498f30SBruce M Simpson if (inp->inp_fport != 0 && 549df8bae1dSRodney W. Grimes inp->inp_fport != uh->uh_sport) 5509c1df695SRobert Watson continue; 55171498f30SBruce M Simpson 552119d85f6SRobert Watson INP_RLOCK(inp); 553df8bae1dSRodney W. Grimes 5544ba16a92SBjoern A. Zeeb if (__predict_false(inp->inp_flags2 & INP_FREED)) { 5554ba16a92SBjoern A. Zeeb INP_RUNLOCK(inp); 5564ba16a92SBjoern A. Zeeb continue; 5574ba16a92SBjoern A. Zeeb } 5584ba16a92SBjoern A. Zeeb 55983453a06SBruce M Simpson /* 560fa046d87SRobert Watson * XXXRW: Because we weren't holding either the inpcb 561fa046d87SRobert Watson * or the hash lock when we checked for a match 562fa046d87SRobert Watson * before, we should probably recheck now that the 563fa046d87SRobert Watson * inpcb lock is held. 564fa046d87SRobert Watson */ 565fa046d87SRobert Watson 566fa046d87SRobert Watson /* 56771498f30SBruce M Simpson * Handle socket delivery policy for any-source 56871498f30SBruce M Simpson * and source-specific multicast. [RFC3678] 56983453a06SBruce M Simpson */ 570a38b1c8cSRandall Stewart if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5713afdfcafSBjoern A. Zeeb struct ip_moptions *imo; 572d10910e6SBruce M Simpson struct sockaddr_in group; 573d10910e6SBruce M Simpson int blocked; 5743afdfcafSBjoern A. Zeeb 5753afdfcafSBjoern A. Zeeb imo = inp->inp_moptions; 576a38b1c8cSRandall Stewart if (imo == NULL) { 577a38b1c8cSRandall Stewart INP_RUNLOCK(inp); 578a38b1c8cSRandall Stewart continue; 579a38b1c8cSRandall Stewart } 580d10910e6SBruce M Simpson bzero(&group, sizeof(struct sockaddr_in)); 581d10910e6SBruce M Simpson group.sin_len = sizeof(struct sockaddr_in); 582d10910e6SBruce M Simpson group.sin_family = AF_INET; 583d10910e6SBruce M Simpson group.sin_addr = ip->ip_dst; 58471498f30SBruce M Simpson 585d10910e6SBruce M Simpson blocked = imo_multi_filter(imo, ifp, 586d10910e6SBruce M Simpson (struct sockaddr *)&group, 587dce33a45SErmal Luçi (struct sockaddr *)&udp_in[0]); 588d10910e6SBruce M Simpson if (blocked != MCAST_PASS) { 589d10910e6SBruce M Simpson if (blocked == MCAST_NOTGMEMBER) 59086425c62SRobert Watson IPSTAT_INC(ips_notmember); 591d10910e6SBruce M Simpson if (blocked == MCAST_NOTSMEMBER || 592d10910e6SBruce M Simpson blocked == MCAST_MUTED) 593026decb8SRobert Watson UDPSTAT_INC(udps_filtermcast); 594119d85f6SRobert Watson INP_RUNLOCK(inp); 5959c1df695SRobert Watson continue; 5969c1df695SRobert Watson } 59783453a06SBruce M Simpson } 598df8bae1dSRodney W. Grimes if (last != NULL) { 599df8bae1dSRodney W. Grimes struct mbuf *n; 600df8bae1dSRodney W. Grimes 601c3bef61eSKevin Lo if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != 602c3bef61eSKevin Lo NULL) { 6037bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 6047bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, last, ip, 605a0a9e1b5SBryan Venteicher last, uh); 6067bda9663SMichael Tuexen else 6077bda9663SMichael Tuexen UDP_PROBE(receive, NULL, last, ip, last, 6087bda9663SMichael Tuexen uh); 609c0d1be08SRandall Stewart if (udp_append(last, ip, n, iphlen, 610dce33a45SErmal Luçi udp_in)) { 611c0d1be08SRandall Stewart goto inp_lost; 612c0d1be08SRandall Stewart } 613c19f98ebSBryan Venteicher } 6146a9148feSBjoern A. Zeeb INP_RUNLOCK(last); 615df8bae1dSRodney W. Grimes } 61682c23ebaSBill Fenner last = inp; 617df8bae1dSRodney W. Grimes /* 618df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 619df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 6203329b236SRobert Watson * socket options set. This heuristic avoids 6213329b236SRobert Watson * searching through all pcbs in the common case of a 6223329b236SRobert Watson * non-shared port. It assumes that an application 6233329b236SRobert Watson * will never clear these options after setting them. 624df8bae1dSRodney W. Grimes */ 6253329b236SRobert Watson if ((last->inp_socket->so_options & 6261a43cff9SSean Bruno (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) 627df8bae1dSRodney W. Grimes break; 628df8bae1dSRodney W. Grimes } 629df8bae1dSRodney W. Grimes 630df8bae1dSRodney W. Grimes if (last == NULL) { 631df8bae1dSRodney W. Grimes /* 6323329b236SRobert Watson * No matching pcb found; discard datagram. (No need 6333329b236SRobert Watson * to send an ICMP Port Unreachable for a broadcast 6343329b236SRobert Watson * or multicast datgram.) 635df8bae1dSRodney W. Grimes */ 636026decb8SRobert Watson UDPSTAT_INC(udps_noportbcast); 637fa046d87SRobert Watson if (inp) 638fa046d87SRobert Watson INP_RUNLOCK(inp); 6396573d758SMatt Macy INP_INFO_RUNLOCK_ET(pcbinfo, et); 640fa046d87SRobert Watson goto badunlocked; 641df8bae1dSRodney W. Grimes } 6427bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 6437bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, last, ip, last, uh); 6447bda9663SMichael Tuexen else 645a0a9e1b5SBryan Venteicher UDP_PROBE(receive, NULL, last, ip, last, uh); 646dce33a45SErmal Luçi if (udp_append(last, ip, m, iphlen, udp_in) == 0) 647c7c7ea4bSRandall Stewart INP_RUNLOCK(last); 648c0d1be08SRandall Stewart inp_lost: 6496573d758SMatt Macy INP_INFO_RUNLOCK_ET(pcbinfo, et); 6508f5a8818SKevin Lo return (IPPROTO_DONE); 651df8bae1dSRodney W. Grimes } 6523329b236SRobert Watson 653df8bae1dSRodney W. Grimes /* 6546d6a026bSDavid Greenman * Locate pcb for datagram. 655df8bae1dSRodney W. Grimes */ 656c1de64a4SAndrey V. Elsukov 6578a006adbSBjoern A. Zeeb /* 6588a006adbSBjoern A. Zeeb * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 6598a006adbSBjoern A. Zeeb */ 660ffdbf9daSAndrey V. Elsukov if ((m->m_flags & M_IP_NEXTHOP) && 661c1de64a4SAndrey V. Elsukov (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 6628a006adbSBjoern A. Zeeb struct sockaddr_in *next_hop; 6638a006adbSBjoern A. Zeeb 6648a006adbSBjoern A. Zeeb next_hop = (struct sockaddr_in *)(fwd_tag + 1); 6658a006adbSBjoern A. Zeeb 6668a006adbSBjoern A. Zeeb /* 6678a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 6688a006adbSBjoern A. Zeeb * Already got one like this? 6698a006adbSBjoern A. Zeeb */ 670e06e816fSKevin Lo inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 6718a006adbSBjoern A. Zeeb ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m); 6728a006adbSBjoern A. Zeeb if (!inp) { 6738a006adbSBjoern A. Zeeb /* 6748a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 6758a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 6768a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 6778a006adbSBjoern A. Zeeb */ 678e06e816fSKevin Lo inp = in_pcblookup(pcbinfo, ip->ip_src, 6798a006adbSBjoern A. Zeeb uh->uh_sport, next_hop->sin_addr, 6808a006adbSBjoern A. Zeeb next_hop->sin_port ? htons(next_hop->sin_port) : 6818a006adbSBjoern A. Zeeb uh->uh_dport, INPLOOKUP_WILDCARD | 6828a006adbSBjoern A. Zeeb INPLOOKUP_RLOCKPCB, ifp); 6838a006adbSBjoern A. Zeeb } 6848a006adbSBjoern A. Zeeb /* Remove the tag from the packet. We don't need it anymore. */ 6858a006adbSBjoern A. Zeeb m_tag_delete(m, fwd_tag); 686ffdbf9daSAndrey V. Elsukov m->m_flags &= ~M_IP_NEXTHOP; 6878a006adbSBjoern A. Zeeb } else 688e06e816fSKevin Lo inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 6898a006adbSBjoern A. Zeeb ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD | 6908a006adbSBjoern A. Zeeb INPLOOKUP_RLOCKPCB, ifp, m); 69115bd2b43SDavid Greenman if (inp == NULL) { 692afdb4274SRobert Watson if (udp_log_in_vain) { 693edf0313bSEric van Gyzen char src[INET_ADDRSTRLEN]; 694edf0313bSEric van Gyzen char dst[INET_ADDRSTRLEN]; 69575cfc95fSAndrey A. Chernov 696592071e8SBruce Evans log(LOG_INFO, 697592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 698edf0313bSEric van Gyzen inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport), 699edf0313bSEric van Gyzen inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport)); 70075cfc95fSAndrey A. Chernov } 7017bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 7027bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, NULL, ip, NULL, uh); 7037bda9663SMichael Tuexen else 704e1526d5aSMichael Tuexen UDP_PROBE(receive, NULL, NULL, ip, NULL, uh); 705026decb8SRobert Watson UDPSTAT_INC(udps_noport); 706df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 707026decb8SRobert Watson UDPSTAT_INC(udps_noportbcast); 708fa046d87SRobert Watson goto badunlocked; 709df8bae1dSRodney W. Grimes } 710603724d3SBjoern A. Zeeb if (V_udp_blackhole) 711fa046d87SRobert Watson goto badunlocked; 7121cbd978eSLuigi Rizzo if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 713fa046d87SRobert Watson goto badunlocked; 71404287599SRuslan Ermilov *ip = save_ip; 715582a7760SBruce Evans icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 7168f5a8818SKevin Lo return (IPPROTO_DONE); 717df8bae1dSRodney W. Grimes } 7183329b236SRobert Watson 7193329b236SRobert Watson /* 7203329b236SRobert Watson * Check the minimum TTL for socket. 7213329b236SRobert Watson */ 722fa046d87SRobert Watson INP_RLOCK_ASSERT(inp); 72310cc62b7SRobert Watson if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) { 7247bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 7257bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh); 7267bda9663SMichael Tuexen else 727e1526d5aSMichael Tuexen UDP_PROBE(receive, NULL, inp, ip, inp, uh); 72810cc62b7SRobert Watson INP_RUNLOCK(inp); 729fa046d87SRobert Watson m_freem(m); 7308f5a8818SKevin Lo return (IPPROTO_DONE); 73110cc62b7SRobert Watson } 732e06e816fSKevin Lo if (cscov_partial) { 733e06e816fSKevin Lo struct udpcb *up; 734e06e816fSKevin Lo 735e06e816fSKevin Lo up = intoudpcb(inp); 73683e95fb3SMichael Tuexen if (up->u_rxcslen == 0 || up->u_rxcslen > len) { 737e06e816fSKevin Lo INP_RUNLOCK(inp); 738e06e816fSKevin Lo m_freem(m); 7398f5a8818SKevin Lo return (IPPROTO_DONE); 740e06e816fSKevin Lo } 741e06e816fSKevin Lo } 74257f60867SMark Johnston 7437bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 7447bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh); 7457bda9663SMichael Tuexen else 7461ad19fb6SMark Johnston UDP_PROBE(receive, NULL, inp, ip, inp, uh); 747dce33a45SErmal Luçi if (udp_append(inp, ip, m, iphlen, udp_in) == 0) 748119d85f6SRobert Watson INP_RUNLOCK(inp); 7498f5a8818SKevin Lo return (IPPROTO_DONE); 75061ffc0b1SJeffrey Hsu 751f76fcf6dSJeffrey Hsu badunlocked: 752df8bae1dSRodney W. Grimes m_freem(m); 7538f5a8818SKevin Lo return (IPPROTO_DONE); 754cfa1ca9dSYoshinobu Inoue } 75579288c11SBjoern A. Zeeb #endif /* INET */ 756cfa1ca9dSYoshinobu Inoue 757cfa1ca9dSYoshinobu Inoue /* 7583329b236SRobert Watson * Notify a udp user of an asynchronous error; just wake up so that they can 7593329b236SRobert Watson * collect error status. 760df8bae1dSRodney W. Grimes */ 7613ce144eaSJeffrey Hsu struct inpcb * 7623329b236SRobert Watson udp_notify(struct inpcb *inp, int errno) 763df8bae1dSRodney W. Grimes { 7643329b236SRobert Watson 765083a010cSRyan Stone INP_WLOCK_ASSERT(inp); 76684cc0778SGeorge V. Neville-Neil if ((errno == EHOSTUNREACH || errno == ENETUNREACH || 76784cc0778SGeorge V. Neville-Neil errno == EHOSTDOWN) && inp->inp_route.ro_rt) { 76884cc0778SGeorge V. Neville-Neil RTFREE(inp->inp_route.ro_rt); 76984cc0778SGeorge V. Neville-Neil inp->inp_route.ro_rt = (struct rtentry *)NULL; 77084cc0778SGeorge V. Neville-Neil } 7718501a69cSRobert Watson 772df8bae1dSRodney W. Grimes inp->inp_socket->so_error = errno; 773df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 774df8bae1dSRodney W. Grimes sowwakeup(inp->inp_socket); 7753329b236SRobert Watson return (inp); 776df8bae1dSRodney W. Grimes } 777df8bae1dSRodney W. Grimes 77879288c11SBjoern A. Zeeb #ifdef INET 779e06e816fSKevin Lo static void 780e06e816fSKevin Lo udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip, 781e06e816fSKevin Lo struct inpcbinfo *pcbinfo) 782df8bae1dSRodney W. Grimes { 783c693a045SJonathan Lemon struct ip *ip = vip; 784c693a045SJonathan Lemon struct udphdr *uh; 785c693a045SJonathan Lemon struct in_addr faddr; 786c693a045SJonathan Lemon struct inpcb *inp; 787c693a045SJonathan Lemon 788c693a045SJonathan Lemon faddr = ((struct sockaddr_in *)sa)->sin_addr; 789c693a045SJonathan Lemon if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 790c693a045SJonathan Lemon return; 791df8bae1dSRodney W. Grimes 79284cc0778SGeorge V. Neville-Neil if (PRC_IS_REDIRECT(cmd)) { 79384cc0778SGeorge V. Neville-Neil /* signal EHOSTDOWN, as it flushes the cached route */ 7944f321dbdSBjoern A. Zeeb in_pcbnotifyall(&V_udbinfo, faddr, EHOSTDOWN, udp_notify); 79597d8d152SAndre Oppermann return; 79684cc0778SGeorge V. Neville-Neil } 7973329b236SRobert Watson 79897d8d152SAndre Oppermann /* 79997d8d152SAndre Oppermann * Hostdead is ugly because it goes linearly through all PCBs. 8003329b236SRobert Watson * 8013329b236SRobert Watson * XXX: We never get this from ICMP, otherwise it makes an excellent 8023329b236SRobert Watson * DoS attack on machines with many connections. 80397d8d152SAndre Oppermann */ 80497d8d152SAndre Oppermann if (cmd == PRC_HOSTDEAD) 805af1ee11dSRobert Watson ip = NULL; 806d1c54148SJesper Skriver else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 807df8bae1dSRodney W. Grimes return; 808af1ee11dSRobert Watson if (ip != NULL) { 809df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 810e06e816fSKevin Lo inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 811083a010cSRyan Stone ip->ip_src, uh->uh_sport, INPLOOKUP_WLOCKPCB, NULL); 812f76fcf6dSJeffrey Hsu if (inp != NULL) { 813083a010cSRyan Stone INP_WLOCK_ASSERT(inp); 814f76fcf6dSJeffrey Hsu if (inp->inp_socket != NULL) { 815f5514f08SRobert Watson udp_notify(inp, inetctlerrmap[cmd]); 816f76fcf6dSJeffrey Hsu } 817083a010cSRyan Stone INP_WUNLOCK(inp); 818abb901c5SRandall Stewart } else { 819abb901c5SRandall Stewart inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 820abb901c5SRandall Stewart ip->ip_src, uh->uh_sport, 821abb901c5SRandall Stewart INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 822abb901c5SRandall Stewart if (inp != NULL) { 823abb901c5SRandall Stewart struct udpcb *up; 82446374cbfSMatt Macy void *ctx; 82546374cbfSMatt Macy udp_tun_icmp_t func; 826abb901c5SRandall Stewart 827abb901c5SRandall Stewart up = intoudpcb(inp); 82846374cbfSMatt Macy ctx = up->u_tun_ctx; 82946374cbfSMatt Macy func = up->u_icmp_func; 830abb901c5SRandall Stewart INP_RUNLOCK(inp); 83146374cbfSMatt Macy if (func != NULL) 83246374cbfSMatt Macy (*func)(cmd, sa, vip, ctx); 833abb901c5SRandall Stewart } 834f76fcf6dSJeffrey Hsu } 835df8bae1dSRodney W. Grimes } else 836e06e816fSKevin Lo in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd], 837f5514f08SRobert Watson udp_notify); 838df8bae1dSRodney W. Grimes } 839e06e816fSKevin Lo void 840e06e816fSKevin Lo udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 841e06e816fSKevin Lo { 842e06e816fSKevin Lo 843e06e816fSKevin Lo return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo)); 844e06e816fSKevin Lo } 845e06e816fSKevin Lo 846e06e816fSKevin Lo void 847e06e816fSKevin Lo udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip) 848e06e816fSKevin Lo { 849e06e816fSKevin Lo 850e06e816fSKevin Lo return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo)); 851e06e816fSKevin Lo } 85279288c11SBjoern A. Zeeb #endif /* INET */ 853df8bae1dSRodney W. Grimes 8540312fbe9SPoul-Henning Kamp static int 85582d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS) 85698271db4SGarrett Wollman { 857277afaffSRobert Watson int error, i, n; 85898271db4SGarrett Wollman struct inpcb *inp, **inp_list; 85998271db4SGarrett Wollman inp_gen_t gencnt; 86098271db4SGarrett Wollman struct xinpgen xig; 8616573d758SMatt Macy struct epoch_tracker et; 86298271db4SGarrett Wollman 86398271db4SGarrett Wollman /* 864f5514f08SRobert Watson * The process of preparing the PCB list is too time-consuming and 86598271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 86698271db4SGarrett Wollman */ 86798271db4SGarrett Wollman if (req->oldptr == 0) { 868603724d3SBjoern A. Zeeb n = V_udbinfo.ipi_count; 869c007b96aSJohn Baldwin n += imax(n / 8, 10); 870c007b96aSJohn Baldwin req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb); 8713329b236SRobert Watson return (0); 87298271db4SGarrett Wollman } 87398271db4SGarrett Wollman 87498271db4SGarrett Wollman if (req->newptr != 0) 8753329b236SRobert Watson return (EPERM); 87698271db4SGarrett Wollman 87798271db4SGarrett Wollman /* 87898271db4SGarrett Wollman * OK, now we're committed to doing something. 87998271db4SGarrett Wollman */ 8806573d758SMatt Macy INP_INFO_RLOCK_ET(&V_udbinfo, et); 881603724d3SBjoern A. Zeeb gencnt = V_udbinfo.ipi_gencnt; 882603724d3SBjoern A. Zeeb n = V_udbinfo.ipi_count; 8836573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_udbinfo, et); 88498271db4SGarrett Wollman 88547934cefSDon Lewis error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 8865c38b6dbSDon Lewis + n * sizeof(struct xinpcb)); 88747934cefSDon Lewis if (error != 0) 88847934cefSDon Lewis return (error); 8895c38b6dbSDon Lewis 89079db6fe7SMark Johnston bzero(&xig, sizeof(xig)); 89198271db4SGarrett Wollman xig.xig_len = sizeof xig; 89298271db4SGarrett Wollman xig.xig_count = n; 89398271db4SGarrett Wollman xig.xig_gen = gencnt; 89498271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 89598271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 89698271db4SGarrett Wollman if (error) 8973329b236SRobert Watson return (error); 89899208b82SMatt Macy 89999208b82SMatt Macy inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 90099208b82SMatt Macy if (inp_list == NULL) 90199208b82SMatt Macy return (ENOMEM); 90298271db4SGarrett Wollman 9036573d758SMatt Macy INP_INFO_RLOCK_ET(&V_udbinfo, et); 904b872626dSMatt Macy for (inp = CK_LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n; 905b872626dSMatt Macy inp = CK_LIST_NEXT(inp, inp_list)) { 906d0e157f6SBjoern A. Zeeb INP_WLOCK(inp); 9072ded288cSJeffrey Hsu if (inp->inp_gencnt <= gencnt && 908d0e157f6SBjoern A. Zeeb cr_canseeinpcb(req->td->td_ucred, inp) == 0) { 909d0e157f6SBjoern A. Zeeb in_pcbref(inp); 91098271db4SGarrett Wollman inp_list[i++] = inp; 911d0e157f6SBjoern A. Zeeb } 912d0e157f6SBjoern A. Zeeb INP_WUNLOCK(inp); 9134787fd37SPaul Saab } 9146573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_udbinfo, et); 91598271db4SGarrett Wollman n = i; 91698271db4SGarrett Wollman 91798271db4SGarrett Wollman error = 0; 91898271db4SGarrett Wollman for (i = 0; i < n; i++) { 91998271db4SGarrett Wollman inp = inp_list[i]; 9209622e84fSRobert Watson INP_RLOCK(inp); 92198271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 92298271db4SGarrett Wollman struct xinpcb xi; 923d0e157f6SBjoern A. Zeeb 924cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(inp, &xi); 9259622e84fSRobert Watson INP_RUNLOCK(inp); 92698271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 927d915b280SStephan Uphoff } else 9289622e84fSRobert Watson INP_RUNLOCK(inp); 92998271db4SGarrett Wollman } 93099208b82SMatt Macy INP_INFO_WLOCK(&V_udbinfo); 93199208b82SMatt Macy for (i = 0; i < n; i++) { 93299208b82SMatt Macy inp = inp_list[i]; 93399208b82SMatt Macy INP_RLOCK(inp); 93499208b82SMatt Macy if (!in_pcbrele_rlocked(inp)) 93599208b82SMatt Macy INP_RUNLOCK(inp); 93699208b82SMatt Macy } 93799208b82SMatt Macy INP_INFO_WUNLOCK(&V_udbinfo); 938d0e157f6SBjoern A. Zeeb 93998271db4SGarrett Wollman if (!error) { 94098271db4SGarrett Wollman /* 9413329b236SRobert Watson * Give the user an updated idea of our state. If the 9423329b236SRobert Watson * generation differs from what we told her before, she knows 9433329b236SRobert Watson * that something happened while we were processing this 9443329b236SRobert Watson * request, and it might be necessary to retry. 94598271db4SGarrett Wollman */ 9466573d758SMatt Macy INP_INFO_RLOCK_ET(&V_udbinfo, et); 947603724d3SBjoern A. Zeeb xig.xig_gen = V_udbinfo.ipi_gencnt; 94898271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 949603724d3SBjoern A. Zeeb xig.xig_count = V_udbinfo.ipi_count; 9506573d758SMatt Macy INP_INFO_RUNLOCK_ET(&V_udbinfo, et); 95198271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 95298271db4SGarrett Wollman } 95399208b82SMatt Macy free(inp_list, M_TEMP); 9543329b236SRobert Watson return (error); 95598271db4SGarrett Wollman } 95698271db4SGarrett Wollman 95779c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, 95879c3d51bSMatthew D Fleming CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0, 95998271db4SGarrett Wollman udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 96098271db4SGarrett Wollman 96179288c11SBjoern A. Zeeb #ifdef INET 96298271db4SGarrett Wollman static int 96382d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS) 964490d50b6SBrian Feldman { 965c0511d3bSBrian Feldman struct xucred xuc; 966490d50b6SBrian Feldman struct sockaddr_in addrs[2]; 967490d50b6SBrian Feldman struct inpcb *inp; 968277afaffSRobert Watson int error; 969490d50b6SBrian Feldman 97032f9753cSRobert Watson error = priv_check(req->td, PRIV_NETINET_GETCRED); 971490d50b6SBrian Feldman if (error) 972490d50b6SBrian Feldman return (error); 973490d50b6SBrian Feldman error = SYSCTL_IN(req, addrs, sizeof(addrs)); 974490d50b6SBrian Feldman if (error) 975490d50b6SBrian Feldman return (error); 976fa046d87SRobert Watson inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 977fa046d87SRobert Watson addrs[0].sin_addr, addrs[0].sin_port, 978fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 9799622e84fSRobert Watson if (inp != NULL) { 980fa046d87SRobert Watson INP_RLOCK_ASSERT(inp); 9819622e84fSRobert Watson if (inp->inp_socket == NULL) 9829622e84fSRobert Watson error = ENOENT; 9839622e84fSRobert Watson if (error == 0) 984f08ef6c5SBjoern A. Zeeb error = cr_canseeinpcb(req->td->td_ucred, inp); 9859622e84fSRobert Watson if (error == 0) 98686d02c5cSBjoern A. Zeeb cru2x(inp->inp_cred, &xuc); 9879622e84fSRobert Watson INP_RUNLOCK(inp); 988fa046d87SRobert Watson } else 9899622e84fSRobert Watson error = ENOENT; 9900e1eebb8SDon Lewis if (error == 0) 9910e1eebb8SDon Lewis error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 992490d50b6SBrian Feldman return (error); 993490d50b6SBrian Feldman } 994490d50b6SBrian Feldman 9957ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 9967ce87f12SDavid Malone CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 9977ce87f12SDavid Malone udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 99879288c11SBjoern A. Zeeb #endif /* INET */ 999490d50b6SBrian Feldman 10007b495c44SVANHULLEBUS Yvan int 10017b495c44SVANHULLEBUS Yvan udp_ctloutput(struct socket *so, struct sockopt *sopt) 10027b495c44SVANHULLEBUS Yvan { 10037b495c44SVANHULLEBUS Yvan struct inpcb *inp; 10047b495c44SVANHULLEBUS Yvan struct udpcb *up; 1005e06e816fSKevin Lo int isudplite, error, optval; 10067b495c44SVANHULLEBUS Yvan 1007e06e816fSKevin Lo error = 0; 1008e06e816fSKevin Lo isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0; 10097b495c44SVANHULLEBUS Yvan inp = sotoinpcb(so); 10107b495c44SVANHULLEBUS Yvan KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 10117b495c44SVANHULLEBUS Yvan INP_WLOCK(inp); 1012e06e816fSKevin Lo if (sopt->sopt_level != so->so_proto->pr_protocol) { 10137b495c44SVANHULLEBUS Yvan #ifdef INET6 10147b495c44SVANHULLEBUS Yvan if (INP_CHECK_SOCKAF(so, AF_INET6)) { 10157b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 10167b495c44SVANHULLEBUS Yvan error = ip6_ctloutput(so, sopt); 101779288c11SBjoern A. Zeeb } 10187b495c44SVANHULLEBUS Yvan #endif 101979288c11SBjoern A. Zeeb #if defined(INET) && defined(INET6) 102079288c11SBjoern A. Zeeb else 102179288c11SBjoern A. Zeeb #endif 102279288c11SBjoern A. Zeeb #ifdef INET 102379288c11SBjoern A. Zeeb { 10247b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 10257b495c44SVANHULLEBUS Yvan error = ip_ctloutput(so, sopt); 10267b495c44SVANHULLEBUS Yvan } 10277b495c44SVANHULLEBUS Yvan #endif 10287b495c44SVANHULLEBUS Yvan return (error); 10297b495c44SVANHULLEBUS Yvan } 10307b495c44SVANHULLEBUS Yvan 10317b495c44SVANHULLEBUS Yvan switch (sopt->sopt_dir) { 10327b495c44SVANHULLEBUS Yvan case SOPT_SET: 10337b495c44SVANHULLEBUS Yvan switch (sopt->sopt_name) { 1034fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1035fcf59617SAndrey V. Elsukov #ifdef INET 10367b495c44SVANHULLEBUS Yvan case UDP_ENCAP: 1037fcf59617SAndrey V. Elsukov if (!IPSEC_ENABLED(ipv4)) { 10387b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 1039fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 10407b495c44SVANHULLEBUS Yvan } 1041fcf59617SAndrey V. Elsukov error = UDPENCAP_PCBCTL(inp, sopt); 10427b495c44SVANHULLEBUS Yvan break; 1043fcf59617SAndrey V. Elsukov #endif /* INET */ 1044fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 1045e06e816fSKevin Lo case UDPLITE_SEND_CSCOV: 1046e06e816fSKevin Lo case UDPLITE_RECV_CSCOV: 1047e06e816fSKevin Lo if (!isudplite) { 1048e06e816fSKevin Lo INP_WUNLOCK(inp); 1049e06e816fSKevin Lo error = ENOPROTOOPT; 1050e06e816fSKevin Lo break; 1051e06e816fSKevin Lo } 1052e06e816fSKevin Lo INP_WUNLOCK(inp); 1053e06e816fSKevin Lo error = sooptcopyin(sopt, &optval, sizeof(optval), 1054e06e816fSKevin Lo sizeof(optval)); 1055e06e816fSKevin Lo if (error != 0) 1056e06e816fSKevin Lo break; 1057e06e816fSKevin Lo inp = sotoinpcb(so); 1058e06e816fSKevin Lo KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1059e06e816fSKevin Lo INP_WLOCK(inp); 1060e06e816fSKevin Lo up = intoudpcb(inp); 1061e06e816fSKevin Lo KASSERT(up != NULL, ("%s: up == NULL", __func__)); 106203f90784SMichael Tuexen if ((optval != 0 && optval < 8) || (optval > 65535)) { 1063e06e816fSKevin Lo INP_WUNLOCK(inp); 1064e06e816fSKevin Lo error = EINVAL; 1065e06e816fSKevin Lo break; 1066e06e816fSKevin Lo } 1067e06e816fSKevin Lo if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1068e06e816fSKevin Lo up->u_txcslen = optval; 1069e06e816fSKevin Lo else 1070e06e816fSKevin Lo up->u_rxcslen = optval; 1071e06e816fSKevin Lo INP_WUNLOCK(inp); 1072e06e816fSKevin Lo break; 10737b495c44SVANHULLEBUS Yvan default: 10747b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 10757b495c44SVANHULLEBUS Yvan error = ENOPROTOOPT; 10767b495c44SVANHULLEBUS Yvan break; 10777b495c44SVANHULLEBUS Yvan } 10787b495c44SVANHULLEBUS Yvan break; 10797b495c44SVANHULLEBUS Yvan case SOPT_GET: 10807b495c44SVANHULLEBUS Yvan switch (sopt->sopt_name) { 1081fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1082fcf59617SAndrey V. Elsukov #ifdef INET 10837b495c44SVANHULLEBUS Yvan case UDP_ENCAP: 1084fcf59617SAndrey V. Elsukov if (!IPSEC_ENABLED(ipv4)) { 10857b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 1086fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 1087fcf59617SAndrey V. Elsukov } 1088fcf59617SAndrey V. Elsukov error = UDPENCAP_PCBCTL(inp, sopt); 10897b495c44SVANHULLEBUS Yvan break; 1090fcf59617SAndrey V. Elsukov #endif /* INET */ 1091fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 1092e06e816fSKevin Lo case UDPLITE_SEND_CSCOV: 1093e06e816fSKevin Lo case UDPLITE_RECV_CSCOV: 1094e06e816fSKevin Lo if (!isudplite) { 1095e06e816fSKevin Lo INP_WUNLOCK(inp); 1096e06e816fSKevin Lo error = ENOPROTOOPT; 1097e06e816fSKevin Lo break; 1098e06e816fSKevin Lo } 1099e06e816fSKevin Lo up = intoudpcb(inp); 1100e06e816fSKevin Lo KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1101e06e816fSKevin Lo if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1102e06e816fSKevin Lo optval = up->u_txcslen; 1103e06e816fSKevin Lo else 1104e06e816fSKevin Lo optval = up->u_rxcslen; 1105e06e816fSKevin Lo INP_WUNLOCK(inp); 1106e06e816fSKevin Lo error = sooptcopyout(sopt, &optval, sizeof(optval)); 1107e06e816fSKevin Lo break; 11087b495c44SVANHULLEBUS Yvan default: 11097b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 11107b495c44SVANHULLEBUS Yvan error = ENOPROTOOPT; 11117b495c44SVANHULLEBUS Yvan break; 11127b495c44SVANHULLEBUS Yvan } 11137b495c44SVANHULLEBUS Yvan break; 11147b495c44SVANHULLEBUS Yvan } 11157b495c44SVANHULLEBUS Yvan return (error); 11167b495c44SVANHULLEBUS Yvan } 11177b495c44SVANHULLEBUS Yvan 111879288c11SBjoern A. Zeeb #ifdef INET 1119fa046d87SRobert Watson #define UH_WLOCKED 2 1120fa046d87SRobert Watson #define UH_RLOCKED 1 1121fa046d87SRobert Watson #define UH_UNLOCKED 0 1122490d50b6SBrian Feldman static int 11233329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, 11243329b236SRobert Watson struct mbuf *control, struct thread *td) 1125df8bae1dSRodney W. Grimes { 11263329b236SRobert Watson struct udpiphdr *ui; 11273329b236SRobert Watson int len = m->m_pkthdr.len; 112890162a4eSIan Dowse struct in_addr faddr, laddr; 1129c557ae16SIan Dowse struct cmsghdr *cm; 1130e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1131c557ae16SIan Dowse struct sockaddr_in *sin, src; 11326573d758SMatt Macy struct epoch_tracker et; 1133e06e816fSKevin Lo int cscov_partial = 0; 113490162a4eSIan Dowse int error = 0; 11358afa2304SBruce M Simpson int ipflags; 113690162a4eSIan Dowse u_short fport, lport; 113784cc0778SGeorge V. Neville-Neil int unlock_udbinfo, unlock_inp; 1138f584d74bSMichael Tuexen u_char tos; 1139e06e816fSKevin Lo uint8_t pr; 1140e06e816fSKevin Lo uint16_t cscov = 0; 11419d3ddf43SAdrian Chadd uint32_t flowid = 0; 1142c2529042SHans Petter Selasky uint8_t flowtype = M_HASHTYPE_NONE; 1143df8bae1dSRodney W. Grimes 11445c32ea65SRobert Watson /* 11455c32ea65SRobert Watson * udp_output() may need to temporarily bind or connect the current 1146f5514f08SRobert Watson * inpcb. As such, we don't know up front whether we will need the 1147f5514f08SRobert Watson * pcbinfo lock or not. Do any work to decide what is needed up 1148f5514f08SRobert Watson * front before acquiring any locks. 11495c32ea65SRobert Watson */ 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; 1159*eafaa1bcSBjoern A. Zeeb retry: 116084cc0778SGeorge V. Neville-Neil if (sin == NULL || 116184cc0778SGeorge V. Neville-Neil (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 116284cc0778SGeorge V. Neville-Neil INP_WLOCK(inp); 1163*eafaa1bcSBjoern A. Zeeb /* 1164*eafaa1bcSBjoern A. Zeeb * In case we lost a race and another thread bound addr/port 1165*eafaa1bcSBjoern A. Zeeb * on the inp we cannot keep the wlock (which still would be 1166*eafaa1bcSBjoern A. Zeeb * fine) as further down, based on these values we make 1167*eafaa1bcSBjoern A. Zeeb * decisions for the pcbinfo lock. If the locks are not in 1168*eafaa1bcSBjoern A. Zeeb * synch the assertions on unlock will fire, hence we go for 1169*eafaa1bcSBjoern A. Zeeb * one retry loop. 1170*eafaa1bcSBjoern A. Zeeb */ 1171*eafaa1bcSBjoern A. Zeeb if (sin != NULL && (inp->inp_laddr.s_addr != INADDR_ANY || 1172*eafaa1bcSBjoern A. Zeeb inp->inp_lport != 0)) { 1173*eafaa1bcSBjoern A. Zeeb INP_WUNLOCK(inp); 1174*eafaa1bcSBjoern A. Zeeb goto retry; 1175*eafaa1bcSBjoern A. Zeeb } 117684cc0778SGeorge V. Neville-Neil unlock_inp = UH_WLOCKED; 117784cc0778SGeorge V. Neville-Neil } else { 11780cfdff24SBjoern A. Zeeb INP_RLOCK(inp); 117984cc0778SGeorge V. Neville-Neil unlock_inp = UH_RLOCKED; 118084cc0778SGeorge V. Neville-Neil } 1181f584d74bSMichael Tuexen tos = inp->inp_ip_tos; 1182c557ae16SIan Dowse if (control != NULL) { 1183c557ae16SIan Dowse /* 11843329b236SRobert Watson * XXX: Currently, we assume all the optional information is 11853329b236SRobert Watson * stored in a single mbuf. 1186c557ae16SIan Dowse */ 1187c557ae16SIan Dowse if (control->m_next) { 118884cc0778SGeorge V. Neville-Neil if (unlock_inp == UH_WLOCKED) 118984cc0778SGeorge V. Neville-Neil INP_WUNLOCK(inp); 119084cc0778SGeorge V. Neville-Neil else 11910cfdff24SBjoern A. Zeeb INP_RUNLOCK(inp); 1192c557ae16SIan Dowse m_freem(control); 11935c32ea65SRobert Watson m_freem(m); 11943329b236SRobert Watson return (EINVAL); 1195c557ae16SIan Dowse } 1196c557ae16SIan Dowse for (; control->m_len > 0; 1197c557ae16SIan Dowse control->m_data += CMSG_ALIGN(cm->cmsg_len), 1198c557ae16SIan Dowse control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 1199c557ae16SIan Dowse cm = mtod(control, struct cmsghdr *); 1200af1ee11dSRobert Watson if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 1201af1ee11dSRobert Watson || cm->cmsg_len > control->m_len) { 1202c557ae16SIan Dowse error = EINVAL; 1203c557ae16SIan Dowse break; 1204c557ae16SIan Dowse } 1205c557ae16SIan Dowse if (cm->cmsg_level != IPPROTO_IP) 1206c557ae16SIan Dowse continue; 1207c557ae16SIan Dowse 1208c557ae16SIan Dowse switch (cm->cmsg_type) { 1209c557ae16SIan Dowse case IP_SENDSRCADDR: 1210c557ae16SIan Dowse if (cm->cmsg_len != 1211c557ae16SIan Dowse CMSG_LEN(sizeof(struct in_addr))) { 1212c557ae16SIan Dowse error = EINVAL; 1213c557ae16SIan Dowse break; 1214c557ae16SIan Dowse } 1215c557ae16SIan Dowse bzero(&src, sizeof(src)); 1216c557ae16SIan Dowse src.sin_family = AF_INET; 1217c557ae16SIan Dowse src.sin_len = sizeof(src); 1218c557ae16SIan Dowse src.sin_port = inp->inp_lport; 1219af1ee11dSRobert Watson src.sin_addr = 1220af1ee11dSRobert Watson *(struct in_addr *)CMSG_DATA(cm); 1221c557ae16SIan Dowse break; 1222af1ee11dSRobert Watson 1223f584d74bSMichael Tuexen case IP_TOS: 1224f584d74bSMichael Tuexen if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) { 1225f584d74bSMichael Tuexen error = EINVAL; 1226f584d74bSMichael Tuexen break; 1227f584d74bSMichael Tuexen } 1228f584d74bSMichael Tuexen tos = *(u_char *)CMSG_DATA(cm); 1229f584d74bSMichael Tuexen break; 1230f584d74bSMichael Tuexen 12319d3ddf43SAdrian Chadd case IP_FLOWID: 12329d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 12339d3ddf43SAdrian Chadd error = EINVAL; 12349d3ddf43SAdrian Chadd break; 12359d3ddf43SAdrian Chadd } 12369d3ddf43SAdrian Chadd flowid = *(uint32_t *) CMSG_DATA(cm); 12379d3ddf43SAdrian Chadd break; 12389d3ddf43SAdrian Chadd 12399d3ddf43SAdrian Chadd case IP_FLOWTYPE: 12409d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 12419d3ddf43SAdrian Chadd error = EINVAL; 12429d3ddf43SAdrian Chadd break; 12439d3ddf43SAdrian Chadd } 1244c2529042SHans Petter Selasky flowtype = *(uint32_t *) CMSG_DATA(cm); 12459d3ddf43SAdrian Chadd break; 12469d3ddf43SAdrian Chadd 12479d3ddf43SAdrian Chadd #ifdef RSS 12489d3ddf43SAdrian Chadd case IP_RSSBUCKETID: 12499d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 12509d3ddf43SAdrian Chadd error = EINVAL; 12519d3ddf43SAdrian Chadd break; 12529d3ddf43SAdrian Chadd } 12539d3ddf43SAdrian Chadd /* This is just a placeholder for now */ 12549d3ddf43SAdrian Chadd break; 12559d3ddf43SAdrian Chadd #endif /* RSS */ 1256c557ae16SIan Dowse default: 1257c557ae16SIan Dowse error = ENOPROTOOPT; 1258c557ae16SIan Dowse break; 1259c557ae16SIan Dowse } 1260c557ae16SIan Dowse if (error) 1261c557ae16SIan Dowse break; 1262c557ae16SIan Dowse } 1263c557ae16SIan Dowse m_freem(control); 1264c557ae16SIan Dowse } 12655c32ea65SRobert Watson if (error) { 126684cc0778SGeorge V. Neville-Neil if (unlock_inp == UH_WLOCKED) 126784cc0778SGeorge V. Neville-Neil INP_WUNLOCK(inp); 126884cc0778SGeorge V. Neville-Neil else 12690cfdff24SBjoern A. Zeeb INP_RUNLOCK(inp); 12705c32ea65SRobert Watson m_freem(m); 12713329b236SRobert Watson return (error); 12725c32ea65SRobert Watson } 12735c32ea65SRobert Watson 127443cc0bc1SRobert Watson /* 1275d86ecbe9SBjoern A. Zeeb * In the old days, depending on whether or not the application had 1276d86ecbe9SBjoern A. Zeeb * bound or connected the socket, we had to do varying levels of work. 1277d86ecbe9SBjoern A. Zeeb * The optimal case was for a connected UDP socket, as a global lock 1278d86ecbe9SBjoern A. Zeeb * wasn't required at all. 1279d86ecbe9SBjoern A. Zeeb * In order to decide which we need, we required stability of the 1280d86ecbe9SBjoern A. Zeeb * inpcb binding, which we ensured by acquiring a read lock on the 1281d86ecbe9SBjoern A. Zeeb * inpcb. This didn't strictly follow the lock order, so we played 1282d86ecbe9SBjoern A. Zeeb * the trylock and retry game. 1283d86ecbe9SBjoern A. Zeeb * With the re-introduction of the route-cache in some cases, we started 1284d86ecbe9SBjoern A. Zeeb * to acquire an early inp wlock and a possible race during re-lock 1285d86ecbe9SBjoern A. Zeeb * went away. With the introduction of epoch(9) some read locking 1286d86ecbe9SBjoern A. Zeeb * became epoch(9) and the lock-order issues also went away. 1287d86ecbe9SBjoern A. Zeeb * Due to route-cache we may now hold more conservative locks than 1288d86ecbe9SBjoern A. Zeeb * otherwise required and have split up the 2nd case in case 2 and 3 1289d86ecbe9SBjoern A. Zeeb * in order to keep the udpinfo lock level in sync with the inp one 1290d86ecbe9SBjoern A. Zeeb * for the IP_SENDSRCADDR case below. 129143cc0bc1SRobert Watson */ 1292e06e816fSKevin Lo pr = inp->inp_socket->so_proto->pr_protocol; 1293a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(pr); 129443cc0bc1SRobert Watson if (sin != NULL && 129543cc0bc1SRobert Watson (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 1296e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1297fa046d87SRobert Watson unlock_udbinfo = UH_WLOCKED; 1298d86ecbe9SBjoern A. Zeeb } else if (sin != NULL && 1299e9322998SBjoern A. Zeeb (sin->sin_addr.s_addr == INADDR_ANY || 1300e9322998SBjoern A. Zeeb sin->sin_addr.s_addr == INADDR_BROADCAST || 1301e9322998SBjoern A. Zeeb inp->inp_laddr.s_addr == INADDR_ANY || 1302d86ecbe9SBjoern A. Zeeb inp->inp_lport == 0)) { 13036573d758SMatt Macy INP_HASH_RLOCK_ET(pcbinfo, et); 1304fa046d87SRobert Watson unlock_udbinfo = UH_RLOCKED; 1305d86ecbe9SBjoern A. Zeeb } else if (src.sin_family == AF_INET) { 1306d86ecbe9SBjoern A. Zeeb if (unlock_inp == UH_WLOCKED) { 1307d86ecbe9SBjoern A. Zeeb INP_HASH_WLOCK(pcbinfo); 1308d86ecbe9SBjoern A. Zeeb unlock_udbinfo = UH_WLOCKED; 1309d86ecbe9SBjoern A. Zeeb } else { 1310d86ecbe9SBjoern A. Zeeb INP_HASH_RLOCK_ET(pcbinfo, et); 1311d86ecbe9SBjoern A. Zeeb unlock_udbinfo = UH_RLOCKED; 1312d86ecbe9SBjoern A. Zeeb } 131343cc0bc1SRobert Watson } else 1314fa046d87SRobert Watson unlock_udbinfo = UH_UNLOCKED; 13155c32ea65SRobert Watson 13161b7f0384SBruce M Simpson /* 13171b7f0384SBruce M Simpson * If the IP_SENDSRCADDR control message was specified, override the 13181b7f0384SBruce M Simpson * source address for this datagram. Its use is invalidated if the 13191b7f0384SBruce M Simpson * address thus specified is incomplete or clobbers other inpcbs. 13201b7f0384SBruce M Simpson */ 132190162a4eSIan Dowse laddr = inp->inp_laddr; 132290162a4eSIan Dowse lport = inp->inp_lport; 13231b7f0384SBruce M Simpson if (src.sin_family == AF_INET) { 1324e06e816fSKevin Lo INP_HASH_LOCK_ASSERT(pcbinfo); 13251b7f0384SBruce M Simpson if ((lport == 0) || 13261b7f0384SBruce M Simpson (laddr.s_addr == INADDR_ANY && 13271b7f0384SBruce M Simpson src.sin_addr.s_addr == INADDR_ANY)) { 1328c557ae16SIan Dowse error = EINVAL; 1329c557ae16SIan Dowse goto release; 1330c557ae16SIan Dowse } 1331c557ae16SIan Dowse error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 1332b0330ed9SPawel Jakub Dawidek &laddr.s_addr, &lport, td->td_ucred); 1333c557ae16SIan Dowse if (error) 1334c557ae16SIan Dowse goto release; 1335c557ae16SIan Dowse } 1336c557ae16SIan Dowse 13373144b7d3SRobert Watson /* 13383144b7d3SRobert Watson * If a UDP socket has been connected, then a local address/port will 13393144b7d3SRobert Watson * have been selected and bound. 13403144b7d3SRobert Watson * 134143cc0bc1SRobert Watson * If a UDP socket has not been connected to, then an explicit 13423144b7d3SRobert Watson * destination address must be used, in which case a local 13433144b7d3SRobert Watson * address/port may not have been selected and bound. 13443144b7d3SRobert Watson */ 134543cc0bc1SRobert Watson if (sin != NULL) { 1346c4d585aeSRobert Watson INP_LOCK_ASSERT(inp); 1347df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 1348df8bae1dSRodney W. Grimes error = EISCONN; 1349df8bae1dSRodney W. Grimes goto release; 1350df8bae1dSRodney W. Grimes } 13513144b7d3SRobert Watson 13523144b7d3SRobert Watson /* 13533144b7d3SRobert Watson * Jail may rewrite the destination address, so let it do 13543144b7d3SRobert Watson * that before we use it. 13553144b7d3SRobert Watson */ 1356b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1357b89e82ddSJamie Gritton if (error) 1358413628a7SBjoern A. Zeeb goto release; 13593144b7d3SRobert Watson 13603144b7d3SRobert Watson /* 136143cc0bc1SRobert Watson * If a local address or port hasn't yet been selected, or if 136243cc0bc1SRobert Watson * the destination address needs to be rewritten due to using 136343cc0bc1SRobert Watson * a special INADDR_ constant, invoke in_pcbconnect_setup() 136443cc0bc1SRobert Watson * to do the heavy lifting. Once a port is selected, we 136543cc0bc1SRobert Watson * commit the binding back to the socket; we also commit the 136643cc0bc1SRobert Watson * binding of the address if in jail. 136743cc0bc1SRobert Watson * 136843cc0bc1SRobert Watson * If we already have a valid binding and we're not 136943cc0bc1SRobert Watson * requesting a destination address rewrite, use a fast path. 13703144b7d3SRobert Watson */ 137143cc0bc1SRobert Watson if (inp->inp_laddr.s_addr == INADDR_ANY || 137243cc0bc1SRobert Watson inp->inp_lport == 0 || 137343cc0bc1SRobert Watson sin->sin_addr.s_addr == INADDR_ANY || 137443cc0bc1SRobert Watson sin->sin_addr.s_addr == INADDR_BROADCAST) { 1375e06e816fSKevin Lo INP_HASH_LOCK_ASSERT(pcbinfo); 137643cc0bc1SRobert Watson error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, 137743cc0bc1SRobert Watson &lport, &faddr.s_addr, &fport, NULL, 137843cc0bc1SRobert Watson td->td_ucred); 137990162a4eSIan Dowse if (error) 138090162a4eSIan Dowse goto release; 138190162a4eSIan Dowse 138243cc0bc1SRobert Watson /* 138343cc0bc1SRobert Watson * XXXRW: Why not commit the port if the address is 138443cc0bc1SRobert Watson * !INADDR_ANY? 138543cc0bc1SRobert Watson */ 138690162a4eSIan Dowse /* Commit the local port if newly assigned. */ 138790162a4eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && 138890162a4eSIan Dowse inp->inp_lport == 0) { 1389c4d585aeSRobert Watson INP_WLOCK_ASSERT(inp); 1390e06e816fSKevin Lo INP_HASH_WLOCK_ASSERT(pcbinfo); 13913a1757b9SGleb Smirnoff /* 139243cc0bc1SRobert Watson * Remember addr if jailed, to prevent 139343cc0bc1SRobert Watson * rebinding. 13943a1757b9SGleb Smirnoff */ 13950304c731SJamie Gritton if (prison_flag(td->td_ucred, PR_IP4)) 13963a1757b9SGleb Smirnoff inp->inp_laddr = laddr; 139790162a4eSIan Dowse inp->inp_lport = lport; 139890162a4eSIan Dowse if (in_pcbinshash(inp) != 0) { 139990162a4eSIan Dowse inp->inp_lport = 0; 140090162a4eSIan Dowse error = EAGAIN; 1401df8bae1dSRodney W. Grimes goto release; 1402df8bae1dSRodney W. Grimes } 140390162a4eSIan Dowse inp->inp_flags |= INP_ANONPORT; 140490162a4eSIan Dowse } 1405df8bae1dSRodney W. Grimes } else { 140643cc0bc1SRobert Watson faddr = sin->sin_addr; 140743cc0bc1SRobert Watson fport = sin->sin_port; 140843cc0bc1SRobert Watson } 140943cc0bc1SRobert Watson } else { 1410c4d585aeSRobert Watson INP_LOCK_ASSERT(inp); 141190162a4eSIan Dowse faddr = inp->inp_faddr; 141290162a4eSIan Dowse fport = inp->inp_fport; 141390162a4eSIan Dowse if (faddr.s_addr == INADDR_ANY) { 1414df8bae1dSRodney W. Grimes error = ENOTCONN; 1415df8bae1dSRodney W. Grimes goto release; 1416df8bae1dSRodney W. Grimes } 1417df8bae1dSRodney W. Grimes } 1418e6ccd709SRobert Watson 1419df8bae1dSRodney W. Grimes /* 1420e6ccd709SRobert Watson * Calculate data length and get a mbuf for UDP, IP, and possible 1421392e8407SRobert Watson * link-layer headers. Immediate slide the data pointer back forward 1422392e8407SRobert Watson * since we won't use that space at this layer. 1423df8bae1dSRodney W. Grimes */ 1424eb1b1807SGleb Smirnoff M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT); 1425e6ccd709SRobert Watson if (m == NULL) { 1426df8bae1dSRodney W. Grimes error = ENOBUFS; 142749b19bfcSBruce M Simpson goto release; 1428df8bae1dSRodney W. Grimes } 1429e6ccd709SRobert Watson m->m_data += max_linkhdr; 1430e6ccd709SRobert Watson m->m_len -= max_linkhdr; 1431392e8407SRobert Watson m->m_pkthdr.len -= max_linkhdr; 1432df8bae1dSRodney W. Grimes 1433df8bae1dSRodney W. Grimes /* 14343329b236SRobert Watson * Fill in mbuf with extended UDP header and addresses and length put 14353329b236SRobert Watson * into network format. 1436df8bae1dSRodney W. Grimes */ 1437df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 1438db4f9cc7SJonathan Lemon bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 143934fc9072SMichael Tuexen ui->ui_v = IPVERSION << 4; 1440e06e816fSKevin Lo ui->ui_pr = pr; 144190162a4eSIan Dowse ui->ui_src = laddr; 144290162a4eSIan Dowse ui->ui_dst = faddr; 144390162a4eSIan Dowse ui->ui_sport = lport; 144490162a4eSIan Dowse ui->ui_dport = fport; 1445db4f9cc7SJonathan Lemon ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 1446e06e816fSKevin Lo if (pr == IPPROTO_UDPLITE) { 1447e06e816fSKevin Lo struct udpcb *up; 1448e06e816fSKevin Lo uint16_t plen; 1449e06e816fSKevin Lo 1450e06e816fSKevin Lo up = intoudpcb(inp); 1451e06e816fSKevin Lo cscov = up->u_txcslen; 1452e06e816fSKevin Lo plen = (u_short)len + sizeof(struct udphdr); 1453e06e816fSKevin Lo if (cscov >= plen) 1454e06e816fSKevin Lo cscov = 0; 1455e06e816fSKevin Lo ui->ui_len = htons(plen); 1456e06e816fSKevin Lo ui->ui_ulen = htons(cscov); 1457e06e816fSKevin Lo /* 1458e06e816fSKevin Lo * For UDP-Lite, checksum coverage length of zero means 1459e06e816fSKevin Lo * the entire UDPLite packet is covered by the checksum. 1460e06e816fSKevin Lo */ 1461e06e816fSKevin Lo cscov_partial = (cscov == 0) ? 0 : 1; 146234fc9072SMichael Tuexen } 1463df8bae1dSRodney W. Grimes 1464b2828ad2SAndre Oppermann /* 1465b2828ad2SAndre Oppermann * Set the Don't Fragment bit in the IP header. 1466b2828ad2SAndre Oppermann */ 1467b2828ad2SAndre Oppermann if (inp->inp_flags & INP_DONTFRAG) { 1468b2828ad2SAndre Oppermann struct ip *ip; 14693329b236SRobert Watson 1470b2828ad2SAndre Oppermann ip = (struct ip *)&ui->ui_i; 14718f134647SGleb Smirnoff ip->ip_off |= htons(IP_DF); 1472b2828ad2SAndre Oppermann } 1473b2828ad2SAndre Oppermann 1474b5d47ff5SJohn-Mark Gurney ipflags = 0; 1475b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_DONTROUTE) 1476b5d47ff5SJohn-Mark Gurney ipflags |= IP_ROUTETOIF; 1477b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_BROADCAST) 1478b5d47ff5SJohn-Mark Gurney ipflags |= IP_ALLOWBROADCAST; 14796fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 14808afa2304SBruce M Simpson ipflags |= IP_SENDONES; 14818afa2304SBruce M Simpson 14821175d9d5SRobert Watson #ifdef MAC 14831175d9d5SRobert Watson mac_inpcb_create_mbuf(inp, m); 14841175d9d5SRobert Watson #endif 14851175d9d5SRobert Watson 1486df8bae1dSRodney W. Grimes /* 1487db4f9cc7SJonathan Lemon * Set up checksum and output datagram. 1488df8bae1dSRodney W. Grimes */ 1489e06e816fSKevin Lo ui->ui_sum = 0; 1490a485f139SMichael Tuexen if (pr == IPPROTO_UDPLITE) { 1491e06e816fSKevin Lo if (inp->inp_flags & INP_ONESBCAST) 1492e06e816fSKevin Lo faddr.s_addr = INADDR_BROADCAST; 1493a485f139SMichael Tuexen if (cscov_partial) { 1494e06e816fSKevin Lo if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0) 1495e06e816fSKevin Lo ui->ui_sum = 0xffff; 1496a485f139SMichael Tuexen } else { 1497a485f139SMichael Tuexen if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0) 1498a485f139SMichael Tuexen ui->ui_sum = 0xffff; 1499a485f139SMichael Tuexen } 1500a485f139SMichael Tuexen } else if (V_udp_cksum) { 15016fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 15028a538743SBruce M Simpson faddr.s_addr = INADDR_BROADCAST; 15038a538743SBruce M Simpson ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 1504e06e816fSKevin Lo htons((u_short)len + sizeof(struct udphdr) + pr)); 1505db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_UDP; 1506db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 1507e06e816fSKevin Lo } 15088f134647SGleb Smirnoff ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len); 1509ca98b82cSDavid Greenman ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 1510f584d74bSMichael Tuexen ((struct ip *)ui)->ip_tos = tos; /* XXX */ 1511026decb8SRobert Watson UDPSTAT_INC(udps_opackets); 1512cfa1ca9dSYoshinobu Inoue 15139d3ddf43SAdrian Chadd /* 15149d3ddf43SAdrian Chadd * Setup flowid / RSS information for outbound socket. 15159d3ddf43SAdrian Chadd * 15169d3ddf43SAdrian Chadd * Once the UDP code decides to set a flowid some other way, 15179d3ddf43SAdrian Chadd * this allows the flowid to be overridden by userland. 15189d3ddf43SAdrian Chadd */ 1519c2529042SHans Petter Selasky if (flowtype != M_HASHTYPE_NONE) { 15209d3ddf43SAdrian Chadd m->m_pkthdr.flowid = flowid; 1521c2529042SHans Petter Selasky M_HASHTYPE_SET(m, flowtype); 1522f9a6e8d7SBjoern A. Zeeb } 15238ad1a83bSAdrian Chadd #ifdef RSS 1524f9a6e8d7SBjoern A. Zeeb else { 15258ad1a83bSAdrian Chadd uint32_t hash_val, hash_type; 15268ad1a83bSAdrian Chadd /* 15278ad1a83bSAdrian Chadd * Calculate an appropriate RSS hash for UDP and 15288ad1a83bSAdrian Chadd * UDP Lite. 15298ad1a83bSAdrian Chadd * 15308ad1a83bSAdrian Chadd * The called function will take care of figuring out 15318ad1a83bSAdrian Chadd * whether a 2-tuple or 4-tuple hash is required based 15328ad1a83bSAdrian Chadd * on the currently configured scheme. 15338ad1a83bSAdrian Chadd * 15348ad1a83bSAdrian Chadd * Later later on connected socket values should be 15358ad1a83bSAdrian Chadd * cached in the inpcb and reused, rather than constantly 15368ad1a83bSAdrian Chadd * re-calculating it. 15378ad1a83bSAdrian Chadd * 15388ad1a83bSAdrian Chadd * UDP Lite is a different protocol number and will 15398ad1a83bSAdrian Chadd * likely end up being hashed as a 2-tuple until 15408ad1a83bSAdrian Chadd * RSS / NICs grow UDP Lite protocol awareness. 15418ad1a83bSAdrian Chadd */ 15428ad1a83bSAdrian Chadd if (rss_proto_software_hash_v4(faddr, laddr, fport, lport, 15438ad1a83bSAdrian Chadd pr, &hash_val, &hash_type) == 0) { 15448ad1a83bSAdrian Chadd m->m_pkthdr.flowid = hash_val; 15458ad1a83bSAdrian Chadd M_HASHTYPE_SET(m, hash_type); 15468ad1a83bSAdrian Chadd } 15479d3ddf43SAdrian Chadd } 15489d3ddf43SAdrian Chadd 15498ad1a83bSAdrian Chadd /* 15508ad1a83bSAdrian Chadd * Don't override with the inp cached flowid value. 15518ad1a83bSAdrian Chadd * 15528ad1a83bSAdrian Chadd * Depending upon the kind of send being done, the inp 15538ad1a83bSAdrian Chadd * flowid/flowtype values may actually not be appropriate 15548ad1a83bSAdrian Chadd * for this particular socket send. 15558ad1a83bSAdrian Chadd * 15568ad1a83bSAdrian Chadd * We should either leave the flowid at zero (which is what is 15578ad1a83bSAdrian Chadd * currently done) or set it to some software generated 15588ad1a83bSAdrian Chadd * hash value based on the packet contents. 15598ad1a83bSAdrian Chadd */ 15609d3ddf43SAdrian Chadd ipflags |= IP_NODEFAULTFLOWID; 15619d3ddf43SAdrian Chadd #endif /* RSS */ 15629d3ddf43SAdrian Chadd 1563fa046d87SRobert Watson if (unlock_udbinfo == UH_WLOCKED) 1564e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1565fa046d87SRobert Watson else if (unlock_udbinfo == UH_RLOCKED) 15666573d758SMatt Macy INP_HASH_RUNLOCK_ET(pcbinfo, et); 15677bda9663SMichael Tuexen if (pr == IPPROTO_UDPLITE) 15687bda9663SMichael Tuexen UDPLITE_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u); 15697bda9663SMichael Tuexen else 157057f60867SMark Johnston UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u); 157184cc0778SGeorge V. Neville-Neil error = ip_output(m, inp->inp_options, 157284cc0778SGeorge V. Neville-Neil (unlock_inp == UH_WLOCKED ? &inp->inp_route : NULL), ipflags, 15735d846453SSam Leffler inp->inp_moptions, inp); 157484cc0778SGeorge V. Neville-Neil if (unlock_inp == UH_WLOCKED) 15758501a69cSRobert Watson INP_WUNLOCK(inp); 1576948d0fc9SRobert Watson else 1577948d0fc9SRobert Watson INP_RUNLOCK(inp); 1578df8bae1dSRodney W. Grimes return (error); 1579df8bae1dSRodney W. Grimes 1580df8bae1dSRodney W. Grimes release: 1581fa046d87SRobert Watson if (unlock_udbinfo == UH_WLOCKED) { 158200b460ffSRick Macklem KASSERT(unlock_inp == UH_WLOCKED, 1583bd4549f4SBjoern A. Zeeb ("%s: excl udbinfo lock %#03x, shared inp lock %#03x, " 1584bd4549f4SBjoern A. Zeeb "sin %p daddr %#010x inp %p laddr %#010x lport %#06x " 1585bd4549f4SBjoern A. Zeeb "src fam %#04x", 1586bd4549f4SBjoern A. Zeeb __func__, unlock_udbinfo, unlock_inp, sin, 1587bd4549f4SBjoern A. Zeeb (sin != NULL) ? sin->sin_addr.s_addr : 0xfefefefe, inp, 1588bd4549f4SBjoern A. Zeeb inp->inp_laddr.s_addr, inp->inp_lport, src.sin_family)); 1589e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1590948d0fc9SRobert Watson INP_WUNLOCK(inp); 1591fa046d87SRobert Watson } else if (unlock_udbinfo == UH_RLOCKED) { 159200b460ffSRick Macklem KASSERT(unlock_inp == UH_RLOCKED, 1593bd4549f4SBjoern A. Zeeb ("%s: shared udbinfo lock %#03x, excl inp lock %#03x, " 1594bd4549f4SBjoern A. Zeeb "sin %p daddr %#010x inp %p laddr %#010x lport %#06x " 1595bd4549f4SBjoern A. Zeeb "src fam %#04x", 1596bd4549f4SBjoern A. Zeeb __func__, unlock_udbinfo, unlock_inp, sin, 1597bd4549f4SBjoern A. Zeeb (sin != NULL) ? sin->sin_addr.s_addr : 0xfefefefe, inp, 1598bd4549f4SBjoern A. Zeeb inp->inp_laddr.s_addr, inp->inp_lport, src.sin_family)); 15996573d758SMatt Macy INP_HASH_RUNLOCK_ET(pcbinfo, et); 160043cc0bc1SRobert Watson INP_RUNLOCK(inp); 160100b460ffSRick Macklem } else if (unlock_inp == UH_WLOCKED) 160200b460ffSRick Macklem INP_WUNLOCK(inp); 160300b460ffSRick Macklem else 1604948d0fc9SRobert Watson INP_RUNLOCK(inp); 1605df8bae1dSRodney W. Grimes m_freem(m); 1606df8bae1dSRodney W. Grimes return (error); 1607df8bae1dSRodney W. Grimes } 1608df8bae1dSRodney W. Grimes 1609ac45e92fSRobert Watson static void 1610d0390e05SGarrett Wollman udp_abort(struct socket *so) 1611df8bae1dSRodney W. Grimes { 1612d0390e05SGarrett Wollman struct inpcb *inp; 1613e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1614df8bae1dSRodney W. Grimes 1615a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1616d0390e05SGarrett Wollman inp = sotoinpcb(so); 161714ba8addSRobert Watson KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 16188501a69cSRobert Watson INP_WLOCK(inp); 1619a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1620e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1621a152f8a3SRobert Watson in_pcbdisconnect(inp); 1622a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1623e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1624d0390e05SGarrett Wollman soisdisconnected(so); 1625a152f8a3SRobert Watson } 16268501a69cSRobert Watson INP_WUNLOCK(inp); 1627df8bae1dSRodney W. Grimes } 1628df8bae1dSRodney W. Grimes 1629d0390e05SGarrett Wollman static int 1630b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td) 1631d0390e05SGarrett Wollman { 1632630ba2c5SMatt Macy static uint32_t udp_flowid; 1633d0390e05SGarrett Wollman struct inpcb *inp; 1634e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1635277afaffSRobert Watson int error; 1636d0390e05SGarrett Wollman 1637a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1638d0390e05SGarrett Wollman inp = sotoinpcb(so); 163914ba8addSRobert Watson KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 1640cfa1ca9dSYoshinobu Inoue error = soreserve(so, udp_sendspace, udp_recvspace); 1641f24618aaSRobert Watson if (error) 16423329b236SRobert Watson return (error); 1643e06e816fSKevin Lo INP_INFO_WLOCK(pcbinfo); 1644e06e816fSKevin Lo error = in_pcballoc(so, pcbinfo); 164553b57cd1SSam Leffler if (error) { 1646e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 16473329b236SRobert Watson return (error); 164853b57cd1SSam Leffler } 1649cfa1ca9dSYoshinobu Inoue 165068b5629bSRobert Watson inp = sotoinpcb(so); 1651cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1652603724d3SBjoern A. Zeeb inp->inp_ip_ttl = V_ip_defttl; 1653630ba2c5SMatt Macy inp->inp_flowid = atomic_fetchadd_int(&udp_flowid, 1); 1654630ba2c5SMatt Macy inp->inp_flowtype = M_HASHTYPE_OPAQUE; 16556a9148feSBjoern A. Zeeb 16566a9148feSBjoern A. Zeeb error = udp_newudpcb(inp); 16576a9148feSBjoern A. Zeeb if (error) { 16586a9148feSBjoern A. Zeeb in_pcbdetach(inp); 16596a9148feSBjoern A. Zeeb in_pcbfree(inp); 1660e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 16616a9148feSBjoern A. Zeeb return (error); 16626a9148feSBjoern A. Zeeb } 16636a9148feSBjoern A. Zeeb 1664c7c7ea4bSRandall Stewart INP_WUNLOCK(inp); 1665e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 1666c7c7ea4bSRandall Stewart return (0); 1667c7c7ea4bSRandall Stewart } 166879288c11SBjoern A. Zeeb #endif /* INET */ 1669c7c7ea4bSRandall Stewart 1670c7c7ea4bSRandall Stewart int 1671abb901c5SRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx) 1672c7c7ea4bSRandall Stewart { 1673c7c7ea4bSRandall Stewart struct inpcb *inp; 16746a9148feSBjoern A. Zeeb struct udpcb *up; 1675c7c7ea4bSRandall Stewart 167668b5629bSRobert Watson KASSERT(so->so_type == SOCK_DGRAM, 167768b5629bSRobert Watson ("udp_set_kernel_tunneling: !dgram")); 167868b5629bSRobert Watson inp = sotoinpcb(so); 167968b5629bSRobert Watson KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL")); 1680c7c7ea4bSRandall Stewart INP_WLOCK(inp); 16816a9148feSBjoern A. Zeeb up = intoudpcb(inp); 1682abb901c5SRandall Stewart if ((up->u_tun_func != NULL) || 1683abb901c5SRandall Stewart (up->u_icmp_func != NULL)) { 1684bbb0e3d9SRandall Stewart INP_WUNLOCK(inp); 1685bbb0e3d9SRandall Stewart return (EBUSY); 1686bbb0e3d9SRandall Stewart } 16876a9148feSBjoern A. Zeeb up->u_tun_func = f; 1688abb901c5SRandall Stewart up->u_icmp_func = i; 168981d3ec17SBryan Venteicher up->u_tun_ctx = ctx; 16908501a69cSRobert Watson INP_WUNLOCK(inp); 16913329b236SRobert Watson return (0); 1692df8bae1dSRodney W. Grimes } 1693d0390e05SGarrett Wollman 169479288c11SBjoern A. Zeeb #ifdef INET 1695d0390e05SGarrett Wollman static int 1696b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1697d0390e05SGarrett Wollman { 1698d0390e05SGarrett Wollman struct inpcb *inp; 1699e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1700277afaffSRobert Watson int error; 1701d0390e05SGarrett Wollman 1702a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1703d0390e05SGarrett Wollman inp = sotoinpcb(so); 170414ba8addSRobert Watson KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 17058501a69cSRobert Watson INP_WLOCK(inp); 1706e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1707b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, nam, td->td_ucred); 1708e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 17098501a69cSRobert Watson INP_WUNLOCK(inp); 17103329b236SRobert Watson return (error); 1711d0390e05SGarrett Wollman } 1712d0390e05SGarrett Wollman 1713a152f8a3SRobert Watson static void 1714a152f8a3SRobert Watson udp_close(struct socket *so) 1715a152f8a3SRobert Watson { 1716a152f8a3SRobert Watson struct inpcb *inp; 1717e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1718a152f8a3SRobert Watson 1719a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1720a152f8a3SRobert Watson inp = sotoinpcb(so); 1721a152f8a3SRobert Watson KASSERT(inp != NULL, ("udp_close: inp == NULL")); 17228501a69cSRobert Watson INP_WLOCK(inp); 1723a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1724e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1725a152f8a3SRobert Watson in_pcbdisconnect(inp); 1726a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1727e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1728a152f8a3SRobert Watson soisdisconnected(so); 1729a152f8a3SRobert Watson } 17308501a69cSRobert Watson INP_WUNLOCK(inp); 1731a152f8a3SRobert Watson } 1732a152f8a3SRobert Watson 1733d0390e05SGarrett Wollman static int 1734b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1735d0390e05SGarrett Wollman { 1736d0390e05SGarrett Wollman struct inpcb *inp; 1737e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 173875c13541SPoul-Henning Kamp struct sockaddr_in *sin; 1739e06e816fSKevin Lo int error; 1740d0390e05SGarrett Wollman 1741a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1742d0390e05SGarrett Wollman inp = sotoinpcb(so); 174314ba8addSRobert Watson KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 17448501a69cSRobert Watson INP_WLOCK(inp); 1745f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr != INADDR_ANY) { 17468501a69cSRobert Watson INP_WUNLOCK(inp); 17473329b236SRobert Watson return (EISCONN); 1748f76fcf6dSJeffrey Hsu } 174975c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)nam; 1750b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1751b89e82ddSJamie Gritton if (error != 0) { 1752413628a7SBjoern A. Zeeb INP_WUNLOCK(inp); 1753b89e82ddSJamie Gritton return (error); 1754413628a7SBjoern A. Zeeb } 1755e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1756b0330ed9SPawel Jakub Dawidek error = in_pcbconnect(inp, nam, td->td_ucred); 1757e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 17584cc20ab1SSeigo Tanimura if (error == 0) 1759df8bae1dSRodney W. Grimes soisconnected(so); 17608501a69cSRobert Watson INP_WUNLOCK(inp); 17613329b236SRobert Watson return (error); 1762df8bae1dSRodney W. Grimes } 1763d0390e05SGarrett Wollman 1764bc725eafSRobert Watson static void 1765d0390e05SGarrett Wollman udp_detach(struct socket *so) 1766d0390e05SGarrett Wollman { 1767d0390e05SGarrett Wollman struct inpcb *inp; 1768e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 17696a9148feSBjoern A. Zeeb struct udpcb *up; 1770d0390e05SGarrett Wollman 1771a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1772d0390e05SGarrett Wollman inp = sotoinpcb(so); 177314ba8addSRobert Watson KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1774a152f8a3SRobert Watson KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1775a152f8a3SRobert Watson ("udp_detach: not disconnected")); 1776e06e816fSKevin Lo INP_INFO_WLOCK(pcbinfo); 17778501a69cSRobert Watson INP_WLOCK(inp); 17786a9148feSBjoern A. Zeeb up = intoudpcb(inp); 17796a9148feSBjoern A. Zeeb KASSERT(up != NULL, ("%s: up == NULL", __func__)); 17806a9148feSBjoern A. Zeeb inp->inp_ppcb = NULL; 1781d0390e05SGarrett Wollman in_pcbdetach(inp); 178214ba8addSRobert Watson in_pcbfree(inp); 1783e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 17846a9148feSBjoern A. Zeeb udp_discardcb(up); 1785d0390e05SGarrett Wollman } 1786d0390e05SGarrett Wollman 1787d0390e05SGarrett Wollman static int 1788d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 1789d0390e05SGarrett Wollman { 1790d0390e05SGarrett Wollman struct inpcb *inp; 1791e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1792d0390e05SGarrett Wollman 1793a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1794d0390e05SGarrett Wollman inp = sotoinpcb(so); 179514ba8addSRobert Watson KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 17968501a69cSRobert Watson INP_WLOCK(inp); 1797f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr == INADDR_ANY) { 17988501a69cSRobert Watson INP_WUNLOCK(inp); 17993329b236SRobert Watson return (ENOTCONN); 1800f76fcf6dSJeffrey Hsu } 1801e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1802df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 1803df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 1804e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1805d45e4f99SMaxim Konovalov SOCK_LOCK(so); 1806d45e4f99SMaxim Konovalov so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1807d45e4f99SMaxim Konovalov SOCK_UNLOCK(so); 18088501a69cSRobert Watson INP_WUNLOCK(inp); 18093329b236SRobert Watson return (0); 1810df8bae1dSRodney W. Grimes } 1811df8bae1dSRodney W. Grimes 1812d0390e05SGarrett Wollman static int 181357bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1814b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1815d0390e05SGarrett Wollman { 1816d0390e05SGarrett Wollman struct inpcb *inp; 1817d0390e05SGarrett Wollman 1818d0390e05SGarrett Wollman inp = sotoinpcb(so); 181914ba8addSRobert Watson KASSERT(inp != NULL, ("udp_send: inp == NULL")); 18203329b236SRobert Watson return (udp_output(inp, m, addr, control, td)); 1821d0390e05SGarrett Wollman } 182279288c11SBjoern A. Zeeb #endif /* INET */ 1823d0390e05SGarrett Wollman 182476429de4SYoshinobu Inoue int 1825d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 1826d0390e05SGarrett Wollman { 1827d0390e05SGarrett Wollman struct inpcb *inp; 1828d0390e05SGarrett Wollman 1829d0390e05SGarrett Wollman inp = sotoinpcb(so); 183014ba8addSRobert Watson KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 18318501a69cSRobert Watson INP_WLOCK(inp); 1832d0390e05SGarrett Wollman socantsendmore(so); 18338501a69cSRobert Watson INP_WUNLOCK(inp); 18343329b236SRobert Watson return (0); 1835d0390e05SGarrett Wollman } 1836d0390e05SGarrett Wollman 183779288c11SBjoern A. Zeeb #ifdef INET 1838d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 1839756d52a1SPoul-Henning Kamp .pru_abort = udp_abort, 1840756d52a1SPoul-Henning Kamp .pru_attach = udp_attach, 1841756d52a1SPoul-Henning Kamp .pru_bind = udp_bind, 1842756d52a1SPoul-Henning Kamp .pru_connect = udp_connect, 1843756d52a1SPoul-Henning Kamp .pru_control = in_control, 1844756d52a1SPoul-Henning Kamp .pru_detach = udp_detach, 1845756d52a1SPoul-Henning Kamp .pru_disconnect = udp_disconnect, 184654d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1847756d52a1SPoul-Henning Kamp .pru_send = udp_send, 18485df3e839SRobert Watson .pru_soreceive = soreceive_dgram, 184959b8854eSRobert Watson .pru_sosend = sosend_dgram, 1850756d52a1SPoul-Henning Kamp .pru_shutdown = udp_shutdown, 185154d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1852a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1853a152f8a3SRobert Watson .pru_close = udp_close, 1854d0390e05SGarrett Wollman }; 185579288c11SBjoern A. Zeeb #endif /* INET */ 1856