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 119*334fc582SBjoern A. Zeeb VNET_DEFINE(int, udp_log_in_vain) = 0; 120*334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW, 121*334fc582SBjoern A. Zeeb &VNET_NAME(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; 4028f5a8818SKevin Lo int cscov_partial, iphlen; 403df8bae1dSRodney W. Grimes 4048f5a8818SKevin Lo m = *mp; 4058f5a8818SKevin Lo iphlen = *offp; 40671498f30SBruce M Simpson ifp = m->m_pkthdr.rcvif; 4078f5a8818SKevin Lo *mp = NULL; 408026decb8SRobert Watson UDPSTAT_INC(udps_ipackets); 409df8bae1dSRodney W. Grimes 410df8bae1dSRodney W. Grimes /* 4113329b236SRobert Watson * Strip IP options, if any; should skip this, make available to 4123329b236SRobert Watson * user, and use on returned packets, but we don't yet have a way to 4133329b236SRobert Watson * check the checksum with options still present. 414df8bae1dSRodney W. Grimes */ 415df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 416105bd211SGleb Smirnoff ip_stripoptions(m); 417df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 418df8bae1dSRodney W. Grimes } 419df8bae1dSRodney W. Grimes 420df8bae1dSRodney W. Grimes /* 421df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 422df8bae1dSRodney W. Grimes */ 423df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 424d1b18731SKevin Lo if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) { 425026decb8SRobert Watson UDPSTAT_INC(udps_hdrops); 4268f5a8818SKevin Lo return (IPPROTO_DONE); 427df8bae1dSRodney W. Grimes } 428df8bae1dSRodney W. Grimes } 429503f4e47SBjoern A. Zeeb ip = mtod(m, struct ip *); 430df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 4318f5a8818SKevin Lo cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0; 432df8bae1dSRodney W. Grimes 4333329b236SRobert Watson /* 4343329b236SRobert Watson * Destination port of 0 is illegal, based on RFC768. 4353329b236SRobert Watson */ 436686cdd19SJun-ichiro itojun Hagino if (uh->uh_dport == 0) 437f76fcf6dSJeffrey Hsu goto badunlocked; 438686cdd19SJun-ichiro itojun Hagino 439df8bae1dSRodney W. Grimes /* 4403329b236SRobert Watson * Construct sockaddr format source address. Stuff source address 4413329b236SRobert Watson * and datagram in user buffer. 442b9234fafSSam Leffler */ 443dce33a45SErmal Luçi bzero(&udp_in[0], sizeof(struct sockaddr_in) * 2); 444dce33a45SErmal Luçi udp_in[0].sin_len = sizeof(struct sockaddr_in); 445dce33a45SErmal Luçi udp_in[0].sin_family = AF_INET; 446dce33a45SErmal Luçi udp_in[0].sin_port = uh->uh_sport; 447dce33a45SErmal Luçi udp_in[0].sin_addr = ip->ip_src; 448dce33a45SErmal Luçi udp_in[1].sin_len = sizeof(struct sockaddr_in); 449dce33a45SErmal Luçi udp_in[1].sin_family = AF_INET; 450dce33a45SErmal Luçi udp_in[1].sin_port = uh->uh_dport; 451dce33a45SErmal Luçi udp_in[1].sin_addr = ip->ip_dst; 452b9234fafSSam Leffler 453b9234fafSSam Leffler /* 454af1ee11dSRobert Watson * Make mbuf data length reflect UDP length. If not enough data to 455af1ee11dSRobert Watson * reflect UDP length, drop. 456df8bae1dSRodney W. Grimes */ 457df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 4588ad458a4SGleb Smirnoff ip_len = ntohs(ip->ip_len) - iphlen; 4590f4a0366SMichael Tuexen if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) { 460e06e816fSKevin Lo /* Zero means checksum over the complete packet. */ 4610f4a0366SMichael Tuexen if (len == 0) 462e06e816fSKevin Lo len = ip_len; 463e06e816fSKevin Lo cscov_partial = 0; 464e06e816fSKevin Lo } 4658f134647SGleb Smirnoff if (ip_len != len) { 4668f134647SGleb Smirnoff if (len > ip_len || len < sizeof(struct udphdr)) { 467026decb8SRobert Watson UDPSTAT_INC(udps_badlen); 468f76fcf6dSJeffrey Hsu goto badunlocked; 469df8bae1dSRodney W. Grimes } 4708f5a8818SKevin Lo if (proto == IPPROTO_UDP) 4718f134647SGleb Smirnoff m_adj(m, len - ip_len); 472df8bae1dSRodney W. Grimes } 4733329b236SRobert Watson 474df8bae1dSRodney W. Grimes /* 4753329b236SRobert Watson * Save a copy of the IP header in case we want restore it for 4763329b236SRobert Watson * sending an ICMP error message in response. 477df8bae1dSRodney W. Grimes */ 478603724d3SBjoern A. Zeeb if (!V_udp_blackhole) 479df8bae1dSRodney W. Grimes save_ip = *ip; 480cce418d3SMatt Jacob else 481cce418d3SMatt Jacob memset(&save_ip, 0, sizeof(save_ip)); 482df8bae1dSRodney W. Grimes 483df8bae1dSRodney W. Grimes /* 484df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 485df8bae1dSRodney W. Grimes */ 4866dfab5b1SGarrett Wollman if (uh->uh_sum) { 48739629c92SDavid Malone u_short uh_sum; 48839629c92SDavid Malone 489e06e816fSKevin Lo if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) && 490e06e816fSKevin Lo !cscov_partial) { 491db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 49239629c92SDavid Malone uh_sum = m->m_pkthdr.csum_data; 493db4f9cc7SJonathan Lemon else 49439629c92SDavid Malone uh_sum = in_pseudo(ip->ip_src.s_addr, 495506f4949SRuslan Ermilov ip->ip_dst.s_addr, htonl((u_short)len + 4968f5a8818SKevin Lo m->m_pkthdr.csum_data + proto)); 49739629c92SDavid Malone uh_sum ^= 0xffff; 498db4f9cc7SJonathan Lemon } else { 499cb342100SHajimu UMEMOTO char b[9]; 500af1ee11dSRobert Watson 501cb342100SHajimu UMEMOTO bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 5026effc713SDoug Rabson bzero(((struct ipovly *)ip)->ih_x1, 9); 5038f5a8818SKevin Lo ((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ? 504e06e816fSKevin Lo uh->uh_ulen : htons(ip_len); 50539629c92SDavid Malone uh_sum = in_cksum(m, len + sizeof (struct ip)); 506cb342100SHajimu UMEMOTO bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 507db4f9cc7SJonathan Lemon } 50839629c92SDavid Malone if (uh_sum) { 509026decb8SRobert Watson UDPSTAT_INC(udps_badsum); 510df8bae1dSRodney W. Grimes m_freem(m); 5118f5a8818SKevin Lo return (IPPROTO_DONE); 512df8bae1dSRodney W. Grimes } 513c6d81a34SMichael Tuexen } else { 514c6d81a34SMichael Tuexen if (proto == IPPROTO_UDP) { 515026decb8SRobert Watson UDPSTAT_INC(udps_nosum); 516c6d81a34SMichael Tuexen } else { 517c6d81a34SMichael Tuexen /* UDPLite requires a checksum */ 518c6d81a34SMichael Tuexen /* XXX: What is the right UDPLite MIB counter here? */ 519c6d81a34SMichael Tuexen m_freem(m); 520c6d81a34SMichael Tuexen return (IPPROTO_DONE); 521c6d81a34SMichael Tuexen } 522c6d81a34SMichael Tuexen } 523df8bae1dSRodney W. Grimes 524a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(proto); 525df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 5264af540d1SRyan Stone in_broadcast(ip->ip_dst, ifp)) { 52782c23ebaSBill Fenner struct inpcb *last; 528e06e816fSKevin Lo struct inpcbhead *pcblist; 5293329b236SRobert Watson 53080577e55SGleb Smirnoff NET_EPOCH_ASSERT(); 53180577e55SGleb Smirnoff 532a86e5c96SBjoern A. Zeeb pcblist = udp_get_pcblist(proto); 533df8bae1dSRodney W. Grimes last = NULL; 534b872626dSMatt Macy CK_LIST_FOREACH(inp, pcblist, inp_list) { 5359c1df695SRobert Watson if (inp->inp_lport != uh->uh_dport) 536f76fcf6dSJeffrey Hsu continue; 537cfa1ca9dSYoshinobu Inoue #ifdef INET6 538369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 5399c1df695SRobert Watson continue; 540cfa1ca9dSYoshinobu Inoue #endif 54171498f30SBruce M Simpson if (inp->inp_laddr.s_addr != INADDR_ANY && 54271498f30SBruce M Simpson inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 5439c1df695SRobert Watson continue; 54471498f30SBruce M Simpson if (inp->inp_faddr.s_addr != INADDR_ANY && 54571498f30SBruce M Simpson inp->inp_faddr.s_addr != ip->ip_src.s_addr) 54671498f30SBruce M Simpson continue; 54771498f30SBruce M Simpson if (inp->inp_fport != 0 && 548df8bae1dSRodney W. Grimes inp->inp_fport != uh->uh_sport) 5499c1df695SRobert Watson continue; 55071498f30SBruce M Simpson 551119d85f6SRobert Watson INP_RLOCK(inp); 552df8bae1dSRodney W. Grimes 5534ba16a92SBjoern A. Zeeb if (__predict_false(inp->inp_flags2 & INP_FREED)) { 5544ba16a92SBjoern A. Zeeb INP_RUNLOCK(inp); 5554ba16a92SBjoern A. Zeeb continue; 5564ba16a92SBjoern A. Zeeb } 5574ba16a92SBjoern A. Zeeb 55883453a06SBruce M Simpson /* 559fa046d87SRobert Watson * XXXRW: Because we weren't holding either the inpcb 560fa046d87SRobert Watson * or the hash lock when we checked for a match 561fa046d87SRobert Watson * before, we should probably recheck now that the 562fa046d87SRobert Watson * inpcb lock is held. 563fa046d87SRobert Watson */ 564fa046d87SRobert Watson 565fa046d87SRobert Watson /* 56671498f30SBruce M Simpson * Handle socket delivery policy for any-source 56771498f30SBruce M Simpson * and source-specific multicast. [RFC3678] 56883453a06SBruce M Simpson */ 569a38b1c8cSRandall Stewart if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 5703afdfcafSBjoern A. Zeeb struct ip_moptions *imo; 571d10910e6SBruce M Simpson struct sockaddr_in group; 572d10910e6SBruce M Simpson int blocked; 5733afdfcafSBjoern A. Zeeb 5743afdfcafSBjoern A. Zeeb imo = inp->inp_moptions; 575a38b1c8cSRandall Stewart if (imo == NULL) { 576a38b1c8cSRandall Stewart INP_RUNLOCK(inp); 577a38b1c8cSRandall Stewart continue; 578a38b1c8cSRandall Stewart } 579d10910e6SBruce M Simpson bzero(&group, sizeof(struct sockaddr_in)); 580d10910e6SBruce M Simpson group.sin_len = sizeof(struct sockaddr_in); 581d10910e6SBruce M Simpson group.sin_family = AF_INET; 582d10910e6SBruce M Simpson group.sin_addr = ip->ip_dst; 58371498f30SBruce M Simpson 584d10910e6SBruce M Simpson blocked = imo_multi_filter(imo, ifp, 585d10910e6SBruce M Simpson (struct sockaddr *)&group, 586dce33a45SErmal Luçi (struct sockaddr *)&udp_in[0]); 587d10910e6SBruce M Simpson if (blocked != MCAST_PASS) { 588d10910e6SBruce M Simpson if (blocked == MCAST_NOTGMEMBER) 58986425c62SRobert Watson IPSTAT_INC(ips_notmember); 590d10910e6SBruce M Simpson if (blocked == MCAST_NOTSMEMBER || 591d10910e6SBruce M Simpson blocked == MCAST_MUTED) 592026decb8SRobert Watson UDPSTAT_INC(udps_filtermcast); 593119d85f6SRobert Watson INP_RUNLOCK(inp); 5949c1df695SRobert Watson continue; 5959c1df695SRobert Watson } 59683453a06SBruce M Simpson } 597df8bae1dSRodney W. Grimes if (last != NULL) { 598df8bae1dSRodney W. Grimes struct mbuf *n; 599df8bae1dSRodney W. Grimes 600c3bef61eSKevin Lo if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != 601c3bef61eSKevin Lo NULL) { 6027bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 6037bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, last, ip, 604a0a9e1b5SBryan Venteicher last, uh); 6057bda9663SMichael Tuexen else 6067bda9663SMichael Tuexen UDP_PROBE(receive, NULL, last, ip, last, 6077bda9663SMichael Tuexen uh); 608c0d1be08SRandall Stewart if (udp_append(last, ip, n, iphlen, 609dce33a45SErmal Luçi udp_in)) { 610c0d1be08SRandall Stewart goto inp_lost; 611c0d1be08SRandall Stewart } 612c19f98ebSBryan Venteicher } 6136a9148feSBjoern A. Zeeb INP_RUNLOCK(last); 614df8bae1dSRodney W. Grimes } 61582c23ebaSBill Fenner last = inp; 616df8bae1dSRodney W. Grimes /* 617df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 618df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 6193329b236SRobert Watson * socket options set. This heuristic avoids 6203329b236SRobert Watson * searching through all pcbs in the common case of a 6213329b236SRobert Watson * non-shared port. It assumes that an application 6223329b236SRobert Watson * will never clear these options after setting them. 623df8bae1dSRodney W. Grimes */ 6243329b236SRobert Watson if ((last->inp_socket->so_options & 6251a43cff9SSean Bruno (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) 626df8bae1dSRodney W. Grimes break; 627df8bae1dSRodney W. Grimes } 628df8bae1dSRodney W. Grimes 629df8bae1dSRodney W. Grimes if (last == NULL) { 630df8bae1dSRodney W. Grimes /* 6313329b236SRobert Watson * No matching pcb found; discard datagram. (No need 6323329b236SRobert Watson * to send an ICMP Port Unreachable for a broadcast 6333329b236SRobert Watson * or multicast datgram.) 634df8bae1dSRodney W. Grimes */ 635026decb8SRobert Watson UDPSTAT_INC(udps_noportbcast); 636fa046d87SRobert Watson if (inp) 637fa046d87SRobert Watson INP_RUNLOCK(inp); 638fa046d87SRobert Watson goto badunlocked; 639df8bae1dSRodney W. Grimes } 6407bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 6417bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, last, ip, last, uh); 6427bda9663SMichael Tuexen else 643a0a9e1b5SBryan Venteicher UDP_PROBE(receive, NULL, last, ip, last, uh); 644dce33a45SErmal Luçi if (udp_append(last, ip, m, iphlen, udp_in) == 0) 645c7c7ea4bSRandall Stewart INP_RUNLOCK(last); 646c0d1be08SRandall Stewart inp_lost: 6478f5a8818SKevin Lo return (IPPROTO_DONE); 648df8bae1dSRodney W. Grimes } 6493329b236SRobert Watson 650df8bae1dSRodney W. Grimes /* 6516d6a026bSDavid Greenman * Locate pcb for datagram. 652df8bae1dSRodney W. Grimes */ 653c1de64a4SAndrey V. Elsukov 6548a006adbSBjoern A. Zeeb /* 6558a006adbSBjoern A. Zeeb * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 6568a006adbSBjoern A. Zeeb */ 657ffdbf9daSAndrey V. Elsukov if ((m->m_flags & M_IP_NEXTHOP) && 658c1de64a4SAndrey V. Elsukov (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 6598a006adbSBjoern A. Zeeb struct sockaddr_in *next_hop; 6608a006adbSBjoern A. Zeeb 6618a006adbSBjoern A. Zeeb next_hop = (struct sockaddr_in *)(fwd_tag + 1); 6628a006adbSBjoern A. Zeeb 6638a006adbSBjoern A. Zeeb /* 6648a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 6658a006adbSBjoern A. Zeeb * Already got one like this? 6668a006adbSBjoern A. Zeeb */ 667e06e816fSKevin Lo inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 6688a006adbSBjoern A. Zeeb ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m); 6698a006adbSBjoern A. Zeeb if (!inp) { 6708a006adbSBjoern A. Zeeb /* 6718a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 6728a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 6738a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 6748a006adbSBjoern A. Zeeb */ 675e06e816fSKevin Lo inp = in_pcblookup(pcbinfo, ip->ip_src, 6768a006adbSBjoern A. Zeeb uh->uh_sport, next_hop->sin_addr, 6778a006adbSBjoern A. Zeeb next_hop->sin_port ? htons(next_hop->sin_port) : 6788a006adbSBjoern A. Zeeb uh->uh_dport, INPLOOKUP_WILDCARD | 6798a006adbSBjoern A. Zeeb INPLOOKUP_RLOCKPCB, ifp); 6808a006adbSBjoern A. Zeeb } 6818a006adbSBjoern A. Zeeb /* Remove the tag from the packet. We don't need it anymore. */ 6828a006adbSBjoern A. Zeeb m_tag_delete(m, fwd_tag); 683ffdbf9daSAndrey V. Elsukov m->m_flags &= ~M_IP_NEXTHOP; 6848a006adbSBjoern A. Zeeb } else 685e06e816fSKevin Lo inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 6868a006adbSBjoern A. Zeeb ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD | 6878a006adbSBjoern A. Zeeb INPLOOKUP_RLOCKPCB, ifp, m); 68815bd2b43SDavid Greenman if (inp == NULL) { 689*334fc582SBjoern A. Zeeb if (V_udp_log_in_vain) { 690edf0313bSEric van Gyzen char src[INET_ADDRSTRLEN]; 691edf0313bSEric van Gyzen char dst[INET_ADDRSTRLEN]; 69275cfc95fSAndrey A. Chernov 693592071e8SBruce Evans log(LOG_INFO, 694592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 695edf0313bSEric van Gyzen inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport), 696edf0313bSEric van Gyzen inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport)); 69775cfc95fSAndrey A. Chernov } 6987bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 6997bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, NULL, ip, NULL, uh); 7007bda9663SMichael Tuexen else 701e1526d5aSMichael Tuexen UDP_PROBE(receive, NULL, NULL, ip, NULL, uh); 702026decb8SRobert Watson UDPSTAT_INC(udps_noport); 703df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 704026decb8SRobert Watson UDPSTAT_INC(udps_noportbcast); 705fa046d87SRobert Watson goto badunlocked; 706df8bae1dSRodney W. Grimes } 707603724d3SBjoern A. Zeeb if (V_udp_blackhole) 708fa046d87SRobert Watson goto badunlocked; 7091cbd978eSLuigi Rizzo if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 710fa046d87SRobert Watson goto badunlocked; 71104287599SRuslan Ermilov *ip = save_ip; 712582a7760SBruce Evans icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 7138f5a8818SKevin Lo return (IPPROTO_DONE); 714df8bae1dSRodney W. Grimes } 7153329b236SRobert Watson 7163329b236SRobert Watson /* 7173329b236SRobert Watson * Check the minimum TTL for socket. 7183329b236SRobert Watson */ 719fa046d87SRobert Watson INP_RLOCK_ASSERT(inp); 72010cc62b7SRobert Watson if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) { 7217bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 7227bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh); 7237bda9663SMichael Tuexen else 724e1526d5aSMichael Tuexen UDP_PROBE(receive, NULL, inp, ip, inp, uh); 72510cc62b7SRobert Watson INP_RUNLOCK(inp); 726fa046d87SRobert Watson m_freem(m); 7278f5a8818SKevin Lo return (IPPROTO_DONE); 72810cc62b7SRobert Watson } 729e06e816fSKevin Lo if (cscov_partial) { 730e06e816fSKevin Lo struct udpcb *up; 731e06e816fSKevin Lo 732e06e816fSKevin Lo up = intoudpcb(inp); 73383e95fb3SMichael Tuexen if (up->u_rxcslen == 0 || up->u_rxcslen > len) { 734e06e816fSKevin Lo INP_RUNLOCK(inp); 735e06e816fSKevin Lo m_freem(m); 7368f5a8818SKevin Lo return (IPPROTO_DONE); 737e06e816fSKevin Lo } 738e06e816fSKevin Lo } 73957f60867SMark Johnston 7407bda9663SMichael Tuexen if (proto == IPPROTO_UDPLITE) 7417bda9663SMichael Tuexen UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh); 7427bda9663SMichael Tuexen else 7431ad19fb6SMark Johnston UDP_PROBE(receive, NULL, inp, ip, inp, uh); 744dce33a45SErmal Luçi if (udp_append(inp, ip, m, iphlen, udp_in) == 0) 745119d85f6SRobert Watson INP_RUNLOCK(inp); 7468f5a8818SKevin Lo return (IPPROTO_DONE); 74761ffc0b1SJeffrey Hsu 748f76fcf6dSJeffrey Hsu badunlocked: 749df8bae1dSRodney W. Grimes m_freem(m); 7508f5a8818SKevin Lo return (IPPROTO_DONE); 751cfa1ca9dSYoshinobu Inoue } 75279288c11SBjoern A. Zeeb #endif /* INET */ 753cfa1ca9dSYoshinobu Inoue 754cfa1ca9dSYoshinobu Inoue /* 7553329b236SRobert Watson * Notify a udp user of an asynchronous error; just wake up so that they can 7563329b236SRobert Watson * collect error status. 757df8bae1dSRodney W. Grimes */ 7583ce144eaSJeffrey Hsu struct inpcb * 7593329b236SRobert Watson udp_notify(struct inpcb *inp, int errno) 760df8bae1dSRodney W. Grimes { 7613329b236SRobert Watson 762083a010cSRyan Stone INP_WLOCK_ASSERT(inp); 76384cc0778SGeorge V. Neville-Neil if ((errno == EHOSTUNREACH || errno == ENETUNREACH || 76484cc0778SGeorge V. Neville-Neil errno == EHOSTDOWN) && inp->inp_route.ro_rt) { 76584cc0778SGeorge V. Neville-Neil RTFREE(inp->inp_route.ro_rt); 76684cc0778SGeorge V. Neville-Neil inp->inp_route.ro_rt = (struct rtentry *)NULL; 76784cc0778SGeorge V. Neville-Neil } 7688501a69cSRobert Watson 769df8bae1dSRodney W. Grimes inp->inp_socket->so_error = errno; 770df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 771df8bae1dSRodney W. Grimes sowwakeup(inp->inp_socket); 7723329b236SRobert Watson return (inp); 773df8bae1dSRodney W. Grimes } 774df8bae1dSRodney W. Grimes 77579288c11SBjoern A. Zeeb #ifdef INET 776e06e816fSKevin Lo static void 777e06e816fSKevin Lo udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip, 778e06e816fSKevin Lo struct inpcbinfo *pcbinfo) 779df8bae1dSRodney W. Grimes { 780c693a045SJonathan Lemon struct ip *ip = vip; 781c693a045SJonathan Lemon struct udphdr *uh; 782c693a045SJonathan Lemon struct in_addr faddr; 783c693a045SJonathan Lemon struct inpcb *inp; 784c693a045SJonathan Lemon 785c693a045SJonathan Lemon faddr = ((struct sockaddr_in *)sa)->sin_addr; 786c693a045SJonathan Lemon if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 787c693a045SJonathan Lemon return; 788df8bae1dSRodney W. Grimes 78984cc0778SGeorge V. Neville-Neil if (PRC_IS_REDIRECT(cmd)) { 79084cc0778SGeorge V. Neville-Neil /* signal EHOSTDOWN, as it flushes the cached route */ 7914f321dbdSBjoern A. Zeeb in_pcbnotifyall(&V_udbinfo, faddr, EHOSTDOWN, udp_notify); 79297d8d152SAndre Oppermann return; 79384cc0778SGeorge V. Neville-Neil } 7943329b236SRobert Watson 79597d8d152SAndre Oppermann /* 79697d8d152SAndre Oppermann * Hostdead is ugly because it goes linearly through all PCBs. 7973329b236SRobert Watson * 7983329b236SRobert Watson * XXX: We never get this from ICMP, otherwise it makes an excellent 7993329b236SRobert Watson * DoS attack on machines with many connections. 80097d8d152SAndre Oppermann */ 80197d8d152SAndre Oppermann if (cmd == PRC_HOSTDEAD) 802af1ee11dSRobert Watson ip = NULL; 803d1c54148SJesper Skriver else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 804df8bae1dSRodney W. Grimes return; 805af1ee11dSRobert Watson if (ip != NULL) { 806df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 807e06e816fSKevin Lo inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 808083a010cSRyan Stone ip->ip_src, uh->uh_sport, INPLOOKUP_WLOCKPCB, NULL); 809f76fcf6dSJeffrey Hsu if (inp != NULL) { 810083a010cSRyan Stone INP_WLOCK_ASSERT(inp); 811f76fcf6dSJeffrey Hsu if (inp->inp_socket != NULL) { 812f5514f08SRobert Watson udp_notify(inp, inetctlerrmap[cmd]); 813f76fcf6dSJeffrey Hsu } 814083a010cSRyan Stone INP_WUNLOCK(inp); 815abb901c5SRandall Stewart } else { 816abb901c5SRandall Stewart inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 817abb901c5SRandall Stewart ip->ip_src, uh->uh_sport, 818abb901c5SRandall Stewart INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 819abb901c5SRandall Stewart if (inp != NULL) { 820abb901c5SRandall Stewart struct udpcb *up; 82146374cbfSMatt Macy void *ctx; 82246374cbfSMatt Macy udp_tun_icmp_t func; 823abb901c5SRandall Stewart 824abb901c5SRandall Stewart up = intoudpcb(inp); 82546374cbfSMatt Macy ctx = up->u_tun_ctx; 82646374cbfSMatt Macy func = up->u_icmp_func; 827abb901c5SRandall Stewart INP_RUNLOCK(inp); 82846374cbfSMatt Macy if (func != NULL) 82946374cbfSMatt Macy (*func)(cmd, sa, vip, ctx); 830abb901c5SRandall Stewart } 831f76fcf6dSJeffrey Hsu } 832df8bae1dSRodney W. Grimes } else 833e06e816fSKevin Lo in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd], 834f5514f08SRobert Watson udp_notify); 835df8bae1dSRodney W. Grimes } 836e06e816fSKevin Lo void 837e06e816fSKevin Lo udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 838e06e816fSKevin Lo { 839e06e816fSKevin Lo 840e06e816fSKevin Lo return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo)); 841e06e816fSKevin Lo } 842e06e816fSKevin Lo 843e06e816fSKevin Lo void 844e06e816fSKevin Lo udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip) 845e06e816fSKevin Lo { 846e06e816fSKevin Lo 847e06e816fSKevin Lo return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo)); 848e06e816fSKevin Lo } 84979288c11SBjoern A. Zeeb #endif /* INET */ 850df8bae1dSRodney W. Grimes 8510312fbe9SPoul-Henning Kamp static int 85282d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS) 85398271db4SGarrett Wollman { 85498271db4SGarrett Wollman struct xinpgen xig; 8556573d758SMatt Macy struct epoch_tracker et; 856032677ceSGleb Smirnoff struct inpcb *inp; 857032677ceSGleb Smirnoff int error; 85898271db4SGarrett Wollman 859032677ceSGleb Smirnoff if (req->newptr != 0) 860032677ceSGleb Smirnoff return (EPERM); 861032677ceSGleb Smirnoff 86298271db4SGarrett Wollman if (req->oldptr == 0) { 863032677ceSGleb Smirnoff int n; 864032677ceSGleb Smirnoff 865603724d3SBjoern A. Zeeb n = V_udbinfo.ipi_count; 866c007b96aSJohn Baldwin n += imax(n / 8, 10); 867c007b96aSJohn Baldwin req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb); 8683329b236SRobert Watson return (0); 86998271db4SGarrett Wollman } 87098271db4SGarrett Wollman 871032677ceSGleb Smirnoff if ((error = sysctl_wire_old_buffer(req, 0)) != 0) 87247934cefSDon Lewis return (error); 8735c38b6dbSDon Lewis 87479db6fe7SMark Johnston bzero(&xig, sizeof(xig)); 87598271db4SGarrett Wollman xig.xig_len = sizeof xig; 876032677ceSGleb Smirnoff xig.xig_count = V_udbinfo.ipi_count; 877032677ceSGleb Smirnoff xig.xig_gen = V_udbinfo.ipi_gencnt; 87898271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 87998271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 88098271db4SGarrett Wollman if (error) 8813329b236SRobert Watson return (error); 88299208b82SMatt Macy 883032677ceSGleb Smirnoff NET_EPOCH_ENTER(et); 884032677ceSGleb Smirnoff for (inp = CK_LIST_FIRST(V_udbinfo.ipi_listhead); 885032677ceSGleb Smirnoff inp != NULL; 886b872626dSMatt Macy inp = CK_LIST_NEXT(inp, inp_list)) { 8879622e84fSRobert Watson INP_RLOCK(inp); 888032677ceSGleb Smirnoff if (inp->inp_gencnt <= xig.xig_gen && 889032677ceSGleb Smirnoff cr_canseeinpcb(req->td->td_ucred, inp) == 0) { 89098271db4SGarrett Wollman struct xinpcb xi; 891d0e157f6SBjoern A. Zeeb 892cc65eb4eSGleb Smirnoff in_pcbtoxinpcb(inp, &xi); 8939622e84fSRobert Watson INP_RUNLOCK(inp); 89498271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 895032677ceSGleb Smirnoff if (error) 896032677ceSGleb Smirnoff break; 897d915b280SStephan Uphoff } else 8989622e84fSRobert Watson INP_RUNLOCK(inp); 89998271db4SGarrett Wollman } 900032677ceSGleb Smirnoff NET_EPOCH_EXIT(et); 901d0e157f6SBjoern A. Zeeb 90298271db4SGarrett Wollman if (!error) { 90398271db4SGarrett Wollman /* 9043329b236SRobert Watson * Give the user an updated idea of our state. If the 9053329b236SRobert Watson * generation differs from what we told her before, she knows 9063329b236SRobert Watson * that something happened while we were processing this 9073329b236SRobert Watson * request, and it might be necessary to retry. 90898271db4SGarrett Wollman */ 909603724d3SBjoern A. Zeeb xig.xig_gen = V_udbinfo.ipi_gencnt; 91098271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 911603724d3SBjoern A. Zeeb xig.xig_count = V_udbinfo.ipi_count; 91298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 91398271db4SGarrett Wollman } 914032677ceSGleb Smirnoff 9153329b236SRobert Watson return (error); 91698271db4SGarrett Wollman } 91798271db4SGarrett Wollman 91879c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, 91979c3d51bSMatthew D Fleming CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0, 92098271db4SGarrett Wollman udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 92198271db4SGarrett Wollman 92279288c11SBjoern A. Zeeb #ifdef INET 92398271db4SGarrett Wollman static int 92482d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS) 925490d50b6SBrian Feldman { 926c0511d3bSBrian Feldman struct xucred xuc; 927490d50b6SBrian Feldman struct sockaddr_in addrs[2]; 928d797164aSGleb Smirnoff struct epoch_tracker et; 929490d50b6SBrian Feldman struct inpcb *inp; 930277afaffSRobert Watson int error; 931490d50b6SBrian Feldman 93232f9753cSRobert Watson error = priv_check(req->td, PRIV_NETINET_GETCRED); 933490d50b6SBrian Feldman if (error) 934490d50b6SBrian Feldman return (error); 935490d50b6SBrian Feldman error = SYSCTL_IN(req, addrs, sizeof(addrs)); 936490d50b6SBrian Feldman if (error) 937490d50b6SBrian Feldman return (error); 938d797164aSGleb Smirnoff NET_EPOCH_ENTER(et); 939fa046d87SRobert Watson inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 940fa046d87SRobert Watson addrs[0].sin_addr, addrs[0].sin_port, 941fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 942d797164aSGleb Smirnoff NET_EPOCH_EXIT(et); 9439622e84fSRobert Watson if (inp != NULL) { 944fa046d87SRobert Watson INP_RLOCK_ASSERT(inp); 9459622e84fSRobert Watson if (inp->inp_socket == NULL) 9469622e84fSRobert Watson error = ENOENT; 9479622e84fSRobert Watson if (error == 0) 948f08ef6c5SBjoern A. Zeeb error = cr_canseeinpcb(req->td->td_ucred, inp); 9499622e84fSRobert Watson if (error == 0) 95086d02c5cSBjoern A. Zeeb cru2x(inp->inp_cred, &xuc); 9519622e84fSRobert Watson INP_RUNLOCK(inp); 952fa046d87SRobert Watson } else 9539622e84fSRobert Watson error = ENOENT; 9540e1eebb8SDon Lewis if (error == 0) 9550e1eebb8SDon Lewis error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 956490d50b6SBrian Feldman return (error); 957490d50b6SBrian Feldman } 958490d50b6SBrian Feldman 9597ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 9607ce87f12SDavid Malone CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 9617ce87f12SDavid Malone udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 96279288c11SBjoern A. Zeeb #endif /* INET */ 963490d50b6SBrian Feldman 9647b495c44SVANHULLEBUS Yvan int 9657b495c44SVANHULLEBUS Yvan udp_ctloutput(struct socket *so, struct sockopt *sopt) 9667b495c44SVANHULLEBUS Yvan { 9677b495c44SVANHULLEBUS Yvan struct inpcb *inp; 9687b495c44SVANHULLEBUS Yvan struct udpcb *up; 969e06e816fSKevin Lo int isudplite, error, optval; 9707b495c44SVANHULLEBUS Yvan 971e06e816fSKevin Lo error = 0; 972e06e816fSKevin Lo isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0; 9737b495c44SVANHULLEBUS Yvan inp = sotoinpcb(so); 9747b495c44SVANHULLEBUS Yvan KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 9757b495c44SVANHULLEBUS Yvan INP_WLOCK(inp); 976e06e816fSKevin Lo if (sopt->sopt_level != so->so_proto->pr_protocol) { 9777b495c44SVANHULLEBUS Yvan #ifdef INET6 9787b495c44SVANHULLEBUS Yvan if (INP_CHECK_SOCKAF(so, AF_INET6)) { 9797b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 9807b495c44SVANHULLEBUS Yvan error = ip6_ctloutput(so, sopt); 98179288c11SBjoern A. Zeeb } 9827b495c44SVANHULLEBUS Yvan #endif 98379288c11SBjoern A. Zeeb #if defined(INET) && defined(INET6) 98479288c11SBjoern A. Zeeb else 98579288c11SBjoern A. Zeeb #endif 98679288c11SBjoern A. Zeeb #ifdef INET 98779288c11SBjoern A. Zeeb { 9887b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 9897b495c44SVANHULLEBUS Yvan error = ip_ctloutput(so, sopt); 9907b495c44SVANHULLEBUS Yvan } 9917b495c44SVANHULLEBUS Yvan #endif 9927b495c44SVANHULLEBUS Yvan return (error); 9937b495c44SVANHULLEBUS Yvan } 9947b495c44SVANHULLEBUS Yvan 9957b495c44SVANHULLEBUS Yvan switch (sopt->sopt_dir) { 9967b495c44SVANHULLEBUS Yvan case SOPT_SET: 9977b495c44SVANHULLEBUS Yvan switch (sopt->sopt_name) { 998fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 999fcf59617SAndrey V. Elsukov #ifdef INET 10007b495c44SVANHULLEBUS Yvan case UDP_ENCAP: 1001fcf59617SAndrey V. Elsukov if (!IPSEC_ENABLED(ipv4)) { 10027b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 1003fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 10047b495c44SVANHULLEBUS Yvan } 1005fcf59617SAndrey V. Elsukov error = UDPENCAP_PCBCTL(inp, sopt); 10067b495c44SVANHULLEBUS Yvan break; 1007fcf59617SAndrey V. Elsukov #endif /* INET */ 1008fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 1009e06e816fSKevin Lo case UDPLITE_SEND_CSCOV: 1010e06e816fSKevin Lo case UDPLITE_RECV_CSCOV: 1011e06e816fSKevin Lo if (!isudplite) { 1012e06e816fSKevin Lo INP_WUNLOCK(inp); 1013e06e816fSKevin Lo error = ENOPROTOOPT; 1014e06e816fSKevin Lo break; 1015e06e816fSKevin Lo } 1016e06e816fSKevin Lo INP_WUNLOCK(inp); 1017e06e816fSKevin Lo error = sooptcopyin(sopt, &optval, sizeof(optval), 1018e06e816fSKevin Lo sizeof(optval)); 1019e06e816fSKevin Lo if (error != 0) 1020e06e816fSKevin Lo break; 1021e06e816fSKevin Lo inp = sotoinpcb(so); 1022e06e816fSKevin Lo KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1023e06e816fSKevin Lo INP_WLOCK(inp); 1024e06e816fSKevin Lo up = intoudpcb(inp); 1025e06e816fSKevin Lo KASSERT(up != NULL, ("%s: up == NULL", __func__)); 102603f90784SMichael Tuexen if ((optval != 0 && optval < 8) || (optval > 65535)) { 1027e06e816fSKevin Lo INP_WUNLOCK(inp); 1028e06e816fSKevin Lo error = EINVAL; 1029e06e816fSKevin Lo break; 1030e06e816fSKevin Lo } 1031e06e816fSKevin Lo if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1032e06e816fSKevin Lo up->u_txcslen = optval; 1033e06e816fSKevin Lo else 1034e06e816fSKevin Lo up->u_rxcslen = optval; 1035e06e816fSKevin Lo INP_WUNLOCK(inp); 1036e06e816fSKevin Lo break; 10377b495c44SVANHULLEBUS Yvan default: 10387b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 10397b495c44SVANHULLEBUS Yvan error = ENOPROTOOPT; 10407b495c44SVANHULLEBUS Yvan break; 10417b495c44SVANHULLEBUS Yvan } 10427b495c44SVANHULLEBUS Yvan break; 10437b495c44SVANHULLEBUS Yvan case SOPT_GET: 10447b495c44SVANHULLEBUS Yvan switch (sopt->sopt_name) { 1045fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1046fcf59617SAndrey V. Elsukov #ifdef INET 10477b495c44SVANHULLEBUS Yvan case UDP_ENCAP: 1048fcf59617SAndrey V. Elsukov if (!IPSEC_ENABLED(ipv4)) { 10497b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 1050fcf59617SAndrey V. Elsukov return (ENOPROTOOPT); 1051fcf59617SAndrey V. Elsukov } 1052fcf59617SAndrey V. Elsukov error = UDPENCAP_PCBCTL(inp, sopt); 10537b495c44SVANHULLEBUS Yvan break; 1054fcf59617SAndrey V. Elsukov #endif /* INET */ 1055fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 1056e06e816fSKevin Lo case UDPLITE_SEND_CSCOV: 1057e06e816fSKevin Lo case UDPLITE_RECV_CSCOV: 1058e06e816fSKevin Lo if (!isudplite) { 1059e06e816fSKevin Lo INP_WUNLOCK(inp); 1060e06e816fSKevin Lo error = ENOPROTOOPT; 1061e06e816fSKevin Lo break; 1062e06e816fSKevin Lo } 1063e06e816fSKevin Lo up = intoudpcb(inp); 1064e06e816fSKevin Lo KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1065e06e816fSKevin Lo if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1066e06e816fSKevin Lo optval = up->u_txcslen; 1067e06e816fSKevin Lo else 1068e06e816fSKevin Lo optval = up->u_rxcslen; 1069e06e816fSKevin Lo INP_WUNLOCK(inp); 1070e06e816fSKevin Lo error = sooptcopyout(sopt, &optval, sizeof(optval)); 1071e06e816fSKevin Lo break; 10727b495c44SVANHULLEBUS Yvan default: 10737b495c44SVANHULLEBUS Yvan INP_WUNLOCK(inp); 10747b495c44SVANHULLEBUS Yvan error = ENOPROTOOPT; 10757b495c44SVANHULLEBUS Yvan break; 10767b495c44SVANHULLEBUS Yvan } 10777b495c44SVANHULLEBUS Yvan break; 10787b495c44SVANHULLEBUS Yvan } 10797b495c44SVANHULLEBUS Yvan return (error); 10807b495c44SVANHULLEBUS Yvan } 10817b495c44SVANHULLEBUS Yvan 108279288c11SBjoern A. Zeeb #ifdef INET 1083490d50b6SBrian Feldman static int 10843329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, 10853329b236SRobert Watson struct mbuf *control, struct thread *td) 1086df8bae1dSRodney W. Grimes { 10873329b236SRobert Watson struct udpiphdr *ui; 10883329b236SRobert Watson int len = m->m_pkthdr.len; 108990162a4eSIan Dowse struct in_addr faddr, laddr; 1090c557ae16SIan Dowse struct cmsghdr *cm; 1091e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1092c557ae16SIan Dowse struct sockaddr_in *sin, src; 10936573d758SMatt Macy struct epoch_tracker et; 1094e06e816fSKevin Lo int cscov_partial = 0; 109590162a4eSIan Dowse int error = 0; 10968afa2304SBruce M Simpson int ipflags; 109790162a4eSIan Dowse u_short fport, lport; 1098f584d74bSMichael Tuexen u_char tos; 1099e06e816fSKevin Lo uint8_t pr; 1100e06e816fSKevin Lo uint16_t cscov = 0; 11019d3ddf43SAdrian Chadd uint32_t flowid = 0; 1102c2529042SHans Petter Selasky uint8_t flowtype = M_HASHTYPE_NONE; 1103df8bae1dSRodney W. Grimes 1104430d30d8SBill Fenner if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 1105c557ae16SIan Dowse if (control) 1106c557ae16SIan Dowse m_freem(control); 11075c32ea65SRobert Watson m_freem(m); 11083329b236SRobert Watson return (EMSGSIZE); 1109430d30d8SBill Fenner } 1110430d30d8SBill Fenner 11111b7f0384SBruce M Simpson src.sin_family = 0; 111284cc0778SGeorge V. Neville-Neil sin = (struct sockaddr_in *)addr; 11132435e507SGleb Smirnoff 1114eafaa1bcSBjoern A. Zeeb /* 11152435e507SGleb Smirnoff * udp_output() may need to temporarily bind or connect the current 11162435e507SGleb Smirnoff * inpcb. As such, we don't know up front whether we will need the 11172435e507SGleb Smirnoff * pcbinfo lock or not. Do any work to decide what is needed up 11182435e507SGleb Smirnoff * front before acquiring any locks. 11192435e507SGleb Smirnoff * 11202435e507SGleb Smirnoff * We will need network epoch in either case, to safely lookup into 11212435e507SGleb Smirnoff * pcb hash. 1122eafaa1bcSBjoern A. Zeeb */ 11232435e507SGleb Smirnoff if (sin == NULL || 11242435e507SGleb Smirnoff (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) 11252435e507SGleb Smirnoff INP_WLOCK(inp); 11262435e507SGleb Smirnoff else 11270cfdff24SBjoern A. Zeeb INP_RLOCK(inp); 11282435e507SGleb Smirnoff NET_EPOCH_ENTER(et); 1129f584d74bSMichael Tuexen tos = inp->inp_ip_tos; 1130c557ae16SIan Dowse if (control != NULL) { 1131c557ae16SIan Dowse /* 11323329b236SRobert Watson * XXX: Currently, we assume all the optional information is 11333329b236SRobert Watson * stored in a single mbuf. 1134c557ae16SIan Dowse */ 1135c557ae16SIan Dowse if (control->m_next) { 1136c557ae16SIan Dowse m_freem(control); 11372435e507SGleb Smirnoff error = EINVAL; 11382435e507SGleb Smirnoff goto release; 1139c557ae16SIan Dowse } 1140c557ae16SIan Dowse for (; control->m_len > 0; 1141c557ae16SIan Dowse control->m_data += CMSG_ALIGN(cm->cmsg_len), 1142c557ae16SIan Dowse control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 1143c557ae16SIan Dowse cm = mtod(control, struct cmsghdr *); 1144af1ee11dSRobert Watson if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 1145af1ee11dSRobert Watson || cm->cmsg_len > control->m_len) { 1146c557ae16SIan Dowse error = EINVAL; 1147c557ae16SIan Dowse break; 1148c557ae16SIan Dowse } 1149c557ae16SIan Dowse if (cm->cmsg_level != IPPROTO_IP) 1150c557ae16SIan Dowse continue; 1151c557ae16SIan Dowse 1152c557ae16SIan Dowse switch (cm->cmsg_type) { 1153c557ae16SIan Dowse case IP_SENDSRCADDR: 1154c557ae16SIan Dowse if (cm->cmsg_len != 1155c557ae16SIan Dowse CMSG_LEN(sizeof(struct in_addr))) { 1156c557ae16SIan Dowse error = EINVAL; 1157c557ae16SIan Dowse break; 1158c557ae16SIan Dowse } 1159c557ae16SIan Dowse bzero(&src, sizeof(src)); 1160c557ae16SIan Dowse src.sin_family = AF_INET; 1161c557ae16SIan Dowse src.sin_len = sizeof(src); 1162c557ae16SIan Dowse src.sin_port = inp->inp_lport; 1163af1ee11dSRobert Watson src.sin_addr = 1164af1ee11dSRobert Watson *(struct in_addr *)CMSG_DATA(cm); 1165c557ae16SIan Dowse break; 1166af1ee11dSRobert Watson 1167f584d74bSMichael Tuexen case IP_TOS: 1168f584d74bSMichael Tuexen if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) { 1169f584d74bSMichael Tuexen error = EINVAL; 1170f584d74bSMichael Tuexen break; 1171f584d74bSMichael Tuexen } 1172f584d74bSMichael Tuexen tos = *(u_char *)CMSG_DATA(cm); 1173f584d74bSMichael Tuexen break; 1174f584d74bSMichael Tuexen 11759d3ddf43SAdrian Chadd case IP_FLOWID: 11769d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 11779d3ddf43SAdrian Chadd error = EINVAL; 11789d3ddf43SAdrian Chadd break; 11799d3ddf43SAdrian Chadd } 11809d3ddf43SAdrian Chadd flowid = *(uint32_t *) CMSG_DATA(cm); 11819d3ddf43SAdrian Chadd break; 11829d3ddf43SAdrian Chadd 11839d3ddf43SAdrian Chadd case IP_FLOWTYPE: 11849d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 11859d3ddf43SAdrian Chadd error = EINVAL; 11869d3ddf43SAdrian Chadd break; 11879d3ddf43SAdrian Chadd } 1188c2529042SHans Petter Selasky flowtype = *(uint32_t *) CMSG_DATA(cm); 11899d3ddf43SAdrian Chadd break; 11909d3ddf43SAdrian Chadd 11919d3ddf43SAdrian Chadd #ifdef RSS 11929d3ddf43SAdrian Chadd case IP_RSSBUCKETID: 11939d3ddf43SAdrian Chadd if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 11949d3ddf43SAdrian Chadd error = EINVAL; 11959d3ddf43SAdrian Chadd break; 11969d3ddf43SAdrian Chadd } 11979d3ddf43SAdrian Chadd /* This is just a placeholder for now */ 11989d3ddf43SAdrian Chadd break; 11999d3ddf43SAdrian Chadd #endif /* RSS */ 1200c557ae16SIan Dowse default: 1201c557ae16SIan Dowse error = ENOPROTOOPT; 1202c557ae16SIan Dowse break; 1203c557ae16SIan Dowse } 1204c557ae16SIan Dowse if (error) 1205c557ae16SIan Dowse break; 1206c557ae16SIan Dowse } 1207c557ae16SIan Dowse m_freem(control); 1208c557ae16SIan Dowse } 12092435e507SGleb Smirnoff if (error) 12102435e507SGleb Smirnoff goto release; 12115c32ea65SRobert Watson 1212e06e816fSKevin Lo pr = inp->inp_socket->so_proto->pr_protocol; 1213a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(pr); 12145c32ea65SRobert Watson 12151b7f0384SBruce M Simpson /* 12161b7f0384SBruce M Simpson * If the IP_SENDSRCADDR control message was specified, override the 12171b7f0384SBruce M Simpson * source address for this datagram. Its use is invalidated if the 12181b7f0384SBruce M Simpson * address thus specified is incomplete or clobbers other inpcbs. 12191b7f0384SBruce M Simpson */ 122090162a4eSIan Dowse laddr = inp->inp_laddr; 122190162a4eSIan Dowse lport = inp->inp_lport; 12221b7f0384SBruce M Simpson if (src.sin_family == AF_INET) { 1223e06e816fSKevin Lo INP_HASH_LOCK_ASSERT(pcbinfo); 12241b7f0384SBruce M Simpson if ((lport == 0) || 12251b7f0384SBruce M Simpson (laddr.s_addr == INADDR_ANY && 12261b7f0384SBruce M Simpson src.sin_addr.s_addr == INADDR_ANY)) { 1227c557ae16SIan Dowse error = EINVAL; 1228c557ae16SIan Dowse goto release; 1229c557ae16SIan Dowse } 1230c557ae16SIan Dowse error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 1231b0330ed9SPawel Jakub Dawidek &laddr.s_addr, &lport, td->td_ucred); 1232c557ae16SIan Dowse if (error) 1233c557ae16SIan Dowse goto release; 1234c557ae16SIan Dowse } 1235c557ae16SIan Dowse 12363144b7d3SRobert Watson /* 12373144b7d3SRobert Watson * If a UDP socket has been connected, then a local address/port will 12383144b7d3SRobert Watson * have been selected and bound. 12393144b7d3SRobert Watson * 124043cc0bc1SRobert Watson * If a UDP socket has not been connected to, then an explicit 12413144b7d3SRobert Watson * destination address must be used, in which case a local 12423144b7d3SRobert Watson * address/port may not have been selected and bound. 12433144b7d3SRobert Watson */ 124443cc0bc1SRobert Watson if (sin != NULL) { 1245c4d585aeSRobert Watson INP_LOCK_ASSERT(inp); 1246df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 1247df8bae1dSRodney W. Grimes error = EISCONN; 1248df8bae1dSRodney W. Grimes goto release; 1249df8bae1dSRodney W. Grimes } 12503144b7d3SRobert Watson 12513144b7d3SRobert Watson /* 12523144b7d3SRobert Watson * Jail may rewrite the destination address, so let it do 12533144b7d3SRobert Watson * that before we use it. 12543144b7d3SRobert Watson */ 1255b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1256b89e82ddSJamie Gritton if (error) 1257413628a7SBjoern A. Zeeb goto release; 12583144b7d3SRobert Watson 12593144b7d3SRobert Watson /* 126043cc0bc1SRobert Watson * If a local address or port hasn't yet been selected, or if 126143cc0bc1SRobert Watson * the destination address needs to be rewritten due to using 126243cc0bc1SRobert Watson * a special INADDR_ constant, invoke in_pcbconnect_setup() 126343cc0bc1SRobert Watson * to do the heavy lifting. Once a port is selected, we 126443cc0bc1SRobert Watson * commit the binding back to the socket; we also commit the 126543cc0bc1SRobert Watson * binding of the address if in jail. 126643cc0bc1SRobert Watson * 126743cc0bc1SRobert Watson * If we already have a valid binding and we're not 126843cc0bc1SRobert Watson * requesting a destination address rewrite, use a fast path. 12693144b7d3SRobert Watson */ 127043cc0bc1SRobert Watson if (inp->inp_laddr.s_addr == INADDR_ANY || 127143cc0bc1SRobert Watson inp->inp_lport == 0 || 127243cc0bc1SRobert Watson sin->sin_addr.s_addr == INADDR_ANY || 127343cc0bc1SRobert Watson sin->sin_addr.s_addr == INADDR_BROADCAST) { 1274e06e816fSKevin Lo INP_HASH_LOCK_ASSERT(pcbinfo); 127543cc0bc1SRobert Watson error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, 127643cc0bc1SRobert Watson &lport, &faddr.s_addr, &fport, NULL, 127743cc0bc1SRobert Watson td->td_ucred); 127890162a4eSIan Dowse if (error) 127990162a4eSIan Dowse goto release; 128090162a4eSIan Dowse 128143cc0bc1SRobert Watson /* 128243cc0bc1SRobert Watson * XXXRW: Why not commit the port if the address is 128343cc0bc1SRobert Watson * !INADDR_ANY? 128443cc0bc1SRobert Watson */ 128590162a4eSIan Dowse /* Commit the local port if newly assigned. */ 128690162a4eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && 128790162a4eSIan Dowse inp->inp_lport == 0) { 1288c4d585aeSRobert Watson INP_WLOCK_ASSERT(inp); 12893a1757b9SGleb Smirnoff /* 129043cc0bc1SRobert Watson * Remember addr if jailed, to prevent 129143cc0bc1SRobert Watson * rebinding. 12923a1757b9SGleb Smirnoff */ 12930304c731SJamie Gritton if (prison_flag(td->td_ucred, PR_IP4)) 12943a1757b9SGleb Smirnoff inp->inp_laddr = laddr; 129590162a4eSIan Dowse inp->inp_lport = lport; 12962435e507SGleb Smirnoff INP_HASH_WLOCK(pcbinfo); 12972435e507SGleb Smirnoff error = in_pcbinshash(inp); 12982435e507SGleb Smirnoff INP_HASH_WUNLOCK(pcbinfo); 12992435e507SGleb Smirnoff if (error != 0) { 130090162a4eSIan Dowse inp->inp_lport = 0; 130190162a4eSIan Dowse error = EAGAIN; 1302df8bae1dSRodney W. Grimes goto release; 1303df8bae1dSRodney W. Grimes } 130490162a4eSIan Dowse inp->inp_flags |= INP_ANONPORT; 130590162a4eSIan Dowse } 1306df8bae1dSRodney W. Grimes } else { 130743cc0bc1SRobert Watson faddr = sin->sin_addr; 130843cc0bc1SRobert Watson fport = sin->sin_port; 130943cc0bc1SRobert Watson } 131043cc0bc1SRobert Watson } else { 1311c4d585aeSRobert Watson INP_LOCK_ASSERT(inp); 131290162a4eSIan Dowse faddr = inp->inp_faddr; 131390162a4eSIan Dowse fport = inp->inp_fport; 131490162a4eSIan Dowse if (faddr.s_addr == INADDR_ANY) { 1315df8bae1dSRodney W. Grimes error = ENOTCONN; 1316df8bae1dSRodney W. Grimes goto release; 1317df8bae1dSRodney W. Grimes } 1318df8bae1dSRodney W. Grimes } 1319e6ccd709SRobert Watson 1320df8bae1dSRodney W. Grimes /* 1321e6ccd709SRobert Watson * Calculate data length and get a mbuf for UDP, IP, and possible 1322392e8407SRobert Watson * link-layer headers. Immediate slide the data pointer back forward 1323392e8407SRobert Watson * since we won't use that space at this layer. 1324df8bae1dSRodney W. Grimes */ 1325eb1b1807SGleb Smirnoff M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT); 1326e6ccd709SRobert Watson if (m == NULL) { 1327df8bae1dSRodney W. Grimes error = ENOBUFS; 132849b19bfcSBruce M Simpson goto release; 1329df8bae1dSRodney W. Grimes } 1330e6ccd709SRobert Watson m->m_data += max_linkhdr; 1331e6ccd709SRobert Watson m->m_len -= max_linkhdr; 1332392e8407SRobert Watson m->m_pkthdr.len -= max_linkhdr; 1333df8bae1dSRodney W. Grimes 1334df8bae1dSRodney W. Grimes /* 13353329b236SRobert Watson * Fill in mbuf with extended UDP header and addresses and length put 13363329b236SRobert Watson * into network format. 1337df8bae1dSRodney W. Grimes */ 1338df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 1339db4f9cc7SJonathan Lemon bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 134034fc9072SMichael Tuexen ui->ui_v = IPVERSION << 4; 1341e06e816fSKevin Lo ui->ui_pr = pr; 134290162a4eSIan Dowse ui->ui_src = laddr; 134390162a4eSIan Dowse ui->ui_dst = faddr; 134490162a4eSIan Dowse ui->ui_sport = lport; 134590162a4eSIan Dowse ui->ui_dport = fport; 1346db4f9cc7SJonathan Lemon ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 1347e06e816fSKevin Lo if (pr == IPPROTO_UDPLITE) { 1348e06e816fSKevin Lo struct udpcb *up; 1349e06e816fSKevin Lo uint16_t plen; 1350e06e816fSKevin Lo 1351e06e816fSKevin Lo up = intoudpcb(inp); 1352e06e816fSKevin Lo cscov = up->u_txcslen; 1353e06e816fSKevin Lo plen = (u_short)len + sizeof(struct udphdr); 1354e06e816fSKevin Lo if (cscov >= plen) 1355e06e816fSKevin Lo cscov = 0; 1356e06e816fSKevin Lo ui->ui_len = htons(plen); 1357e06e816fSKevin Lo ui->ui_ulen = htons(cscov); 1358e06e816fSKevin Lo /* 1359e06e816fSKevin Lo * For UDP-Lite, checksum coverage length of zero means 1360e06e816fSKevin Lo * the entire UDPLite packet is covered by the checksum. 1361e06e816fSKevin Lo */ 1362e06e816fSKevin Lo cscov_partial = (cscov == 0) ? 0 : 1; 136334fc9072SMichael Tuexen } 1364df8bae1dSRodney W. Grimes 1365b2828ad2SAndre Oppermann /* 1366b2828ad2SAndre Oppermann * Set the Don't Fragment bit in the IP header. 1367b2828ad2SAndre Oppermann */ 1368b2828ad2SAndre Oppermann if (inp->inp_flags & INP_DONTFRAG) { 1369b2828ad2SAndre Oppermann struct ip *ip; 13703329b236SRobert Watson 1371b2828ad2SAndre Oppermann ip = (struct ip *)&ui->ui_i; 13728f134647SGleb Smirnoff ip->ip_off |= htons(IP_DF); 1373b2828ad2SAndre Oppermann } 1374b2828ad2SAndre Oppermann 1375b5d47ff5SJohn-Mark Gurney ipflags = 0; 1376b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_DONTROUTE) 1377b5d47ff5SJohn-Mark Gurney ipflags |= IP_ROUTETOIF; 1378b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_BROADCAST) 1379b5d47ff5SJohn-Mark Gurney ipflags |= IP_ALLOWBROADCAST; 13806fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 13818afa2304SBruce M Simpson ipflags |= IP_SENDONES; 13828afa2304SBruce M Simpson 13831175d9d5SRobert Watson #ifdef MAC 13841175d9d5SRobert Watson mac_inpcb_create_mbuf(inp, m); 13851175d9d5SRobert Watson #endif 13861175d9d5SRobert Watson 1387df8bae1dSRodney W. Grimes /* 1388db4f9cc7SJonathan Lemon * Set up checksum and output datagram. 1389df8bae1dSRodney W. Grimes */ 1390e06e816fSKevin Lo ui->ui_sum = 0; 1391a485f139SMichael Tuexen if (pr == IPPROTO_UDPLITE) { 1392e06e816fSKevin Lo if (inp->inp_flags & INP_ONESBCAST) 1393e06e816fSKevin Lo faddr.s_addr = INADDR_BROADCAST; 1394a485f139SMichael Tuexen if (cscov_partial) { 1395e06e816fSKevin Lo if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0) 1396e06e816fSKevin Lo ui->ui_sum = 0xffff; 1397a485f139SMichael Tuexen } else { 1398a485f139SMichael Tuexen if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0) 1399a485f139SMichael Tuexen ui->ui_sum = 0xffff; 1400a485f139SMichael Tuexen } 1401a485f139SMichael Tuexen } else if (V_udp_cksum) { 14026fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 14038a538743SBruce M Simpson faddr.s_addr = INADDR_BROADCAST; 14048a538743SBruce M Simpson ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 1405e06e816fSKevin Lo htons((u_short)len + sizeof(struct udphdr) + pr)); 1406db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_UDP; 1407db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 1408e06e816fSKevin Lo } 14098f134647SGleb Smirnoff ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len); 1410ca98b82cSDavid Greenman ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 1411f584d74bSMichael Tuexen ((struct ip *)ui)->ip_tos = tos; /* XXX */ 1412026decb8SRobert Watson UDPSTAT_INC(udps_opackets); 1413cfa1ca9dSYoshinobu Inoue 14149d3ddf43SAdrian Chadd /* 14159d3ddf43SAdrian Chadd * Setup flowid / RSS information for outbound socket. 14169d3ddf43SAdrian Chadd * 14179d3ddf43SAdrian Chadd * Once the UDP code decides to set a flowid some other way, 14189d3ddf43SAdrian Chadd * this allows the flowid to be overridden by userland. 14199d3ddf43SAdrian Chadd */ 1420c2529042SHans Petter Selasky if (flowtype != M_HASHTYPE_NONE) { 14219d3ddf43SAdrian Chadd m->m_pkthdr.flowid = flowid; 1422c2529042SHans Petter Selasky M_HASHTYPE_SET(m, flowtype); 1423f9a6e8d7SBjoern A. Zeeb } 14248ad1a83bSAdrian Chadd #ifdef RSS 1425f9a6e8d7SBjoern A. Zeeb else { 14268ad1a83bSAdrian Chadd uint32_t hash_val, hash_type; 14278ad1a83bSAdrian Chadd /* 14288ad1a83bSAdrian Chadd * Calculate an appropriate RSS hash for UDP and 14298ad1a83bSAdrian Chadd * UDP Lite. 14308ad1a83bSAdrian Chadd * 14318ad1a83bSAdrian Chadd * The called function will take care of figuring out 14328ad1a83bSAdrian Chadd * whether a 2-tuple or 4-tuple hash is required based 14338ad1a83bSAdrian Chadd * on the currently configured scheme. 14348ad1a83bSAdrian Chadd * 14358ad1a83bSAdrian Chadd * Later later on connected socket values should be 14368ad1a83bSAdrian Chadd * cached in the inpcb and reused, rather than constantly 14378ad1a83bSAdrian Chadd * re-calculating it. 14388ad1a83bSAdrian Chadd * 14398ad1a83bSAdrian Chadd * UDP Lite is a different protocol number and will 14408ad1a83bSAdrian Chadd * likely end up being hashed as a 2-tuple until 14418ad1a83bSAdrian Chadd * RSS / NICs grow UDP Lite protocol awareness. 14428ad1a83bSAdrian Chadd */ 14438ad1a83bSAdrian Chadd if (rss_proto_software_hash_v4(faddr, laddr, fport, lport, 14448ad1a83bSAdrian Chadd pr, &hash_val, &hash_type) == 0) { 14458ad1a83bSAdrian Chadd m->m_pkthdr.flowid = hash_val; 14468ad1a83bSAdrian Chadd M_HASHTYPE_SET(m, hash_type); 14478ad1a83bSAdrian Chadd } 14489d3ddf43SAdrian Chadd } 14499d3ddf43SAdrian Chadd 14508ad1a83bSAdrian Chadd /* 14518ad1a83bSAdrian Chadd * Don't override with the inp cached flowid value. 14528ad1a83bSAdrian Chadd * 14538ad1a83bSAdrian Chadd * Depending upon the kind of send being done, the inp 14548ad1a83bSAdrian Chadd * flowid/flowtype values may actually not be appropriate 14558ad1a83bSAdrian Chadd * for this particular socket send. 14568ad1a83bSAdrian Chadd * 14578ad1a83bSAdrian Chadd * We should either leave the flowid at zero (which is what is 14588ad1a83bSAdrian Chadd * currently done) or set it to some software generated 14598ad1a83bSAdrian Chadd * hash value based on the packet contents. 14608ad1a83bSAdrian Chadd */ 14619d3ddf43SAdrian Chadd ipflags |= IP_NODEFAULTFLOWID; 14629d3ddf43SAdrian Chadd #endif /* RSS */ 14639d3ddf43SAdrian Chadd 14647bda9663SMichael Tuexen if (pr == IPPROTO_UDPLITE) 14657bda9663SMichael Tuexen UDPLITE_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u); 14667bda9663SMichael Tuexen else 146757f60867SMark Johnston UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u); 146884cc0778SGeorge V. Neville-Neil error = ip_output(m, inp->inp_options, 14692435e507SGleb Smirnoff INP_WLOCKED(inp) ? &inp->inp_route : NULL, ipflags, 14705d846453SSam Leffler inp->inp_moptions, inp); 14712435e507SGleb Smirnoff INP_UNLOCK(inp); 14722435e507SGleb Smirnoff NET_EPOCH_EXIT(et); 1473df8bae1dSRodney W. Grimes return (error); 1474df8bae1dSRodney W. Grimes 1475df8bae1dSRodney W. Grimes release: 14762435e507SGleb Smirnoff INP_UNLOCK(inp); 14772435e507SGleb Smirnoff NET_EPOCH_EXIT(et); 1478df8bae1dSRodney W. Grimes m_freem(m); 1479df8bae1dSRodney W. Grimes return (error); 1480df8bae1dSRodney W. Grimes } 1481df8bae1dSRodney W. Grimes 1482ac45e92fSRobert Watson static void 1483d0390e05SGarrett Wollman udp_abort(struct socket *so) 1484df8bae1dSRodney W. Grimes { 1485d0390e05SGarrett Wollman struct inpcb *inp; 1486e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1487df8bae1dSRodney W. Grimes 1488a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1489d0390e05SGarrett Wollman inp = sotoinpcb(so); 149014ba8addSRobert Watson KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 14918501a69cSRobert Watson INP_WLOCK(inp); 1492a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1493e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1494a152f8a3SRobert Watson in_pcbdisconnect(inp); 1495a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1496e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1497d0390e05SGarrett Wollman soisdisconnected(so); 1498a152f8a3SRobert Watson } 14998501a69cSRobert Watson INP_WUNLOCK(inp); 1500df8bae1dSRodney W. Grimes } 1501df8bae1dSRodney W. Grimes 1502d0390e05SGarrett Wollman static int 1503b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td) 1504d0390e05SGarrett Wollman { 1505630ba2c5SMatt Macy static uint32_t udp_flowid; 1506d0390e05SGarrett Wollman struct inpcb *inp; 1507e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1508277afaffSRobert Watson int error; 1509d0390e05SGarrett Wollman 1510a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1511d0390e05SGarrett Wollman inp = sotoinpcb(so); 151214ba8addSRobert Watson KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 1513cfa1ca9dSYoshinobu Inoue error = soreserve(so, udp_sendspace, udp_recvspace); 1514f24618aaSRobert Watson if (error) 15153329b236SRobert Watson return (error); 1516e06e816fSKevin Lo INP_INFO_WLOCK(pcbinfo); 1517e06e816fSKevin Lo error = in_pcballoc(so, pcbinfo); 151853b57cd1SSam Leffler if (error) { 1519e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 15203329b236SRobert Watson return (error); 152153b57cd1SSam Leffler } 1522cfa1ca9dSYoshinobu Inoue 152368b5629bSRobert Watson inp = sotoinpcb(so); 1524cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1525603724d3SBjoern A. Zeeb inp->inp_ip_ttl = V_ip_defttl; 1526630ba2c5SMatt Macy inp->inp_flowid = atomic_fetchadd_int(&udp_flowid, 1); 1527630ba2c5SMatt Macy inp->inp_flowtype = M_HASHTYPE_OPAQUE; 15286a9148feSBjoern A. Zeeb 15296a9148feSBjoern A. Zeeb error = udp_newudpcb(inp); 15306a9148feSBjoern A. Zeeb if (error) { 15316a9148feSBjoern A. Zeeb in_pcbdetach(inp); 15326a9148feSBjoern A. Zeeb in_pcbfree(inp); 1533e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 15346a9148feSBjoern A. Zeeb return (error); 15356a9148feSBjoern A. Zeeb } 15366a9148feSBjoern A. Zeeb 1537c7c7ea4bSRandall Stewart INP_WUNLOCK(inp); 1538e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 1539c7c7ea4bSRandall Stewart return (0); 1540c7c7ea4bSRandall Stewart } 154179288c11SBjoern A. Zeeb #endif /* INET */ 1542c7c7ea4bSRandall Stewart 1543c7c7ea4bSRandall Stewart int 1544abb901c5SRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx) 1545c7c7ea4bSRandall Stewart { 1546c7c7ea4bSRandall Stewart struct inpcb *inp; 15476a9148feSBjoern A. Zeeb struct udpcb *up; 1548c7c7ea4bSRandall Stewart 154968b5629bSRobert Watson KASSERT(so->so_type == SOCK_DGRAM, 155068b5629bSRobert Watson ("udp_set_kernel_tunneling: !dgram")); 155168b5629bSRobert Watson inp = sotoinpcb(so); 155268b5629bSRobert Watson KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL")); 1553c7c7ea4bSRandall Stewart INP_WLOCK(inp); 15546a9148feSBjoern A. Zeeb up = intoudpcb(inp); 1555abb901c5SRandall Stewart if ((up->u_tun_func != NULL) || 1556abb901c5SRandall Stewart (up->u_icmp_func != NULL)) { 1557bbb0e3d9SRandall Stewart INP_WUNLOCK(inp); 1558bbb0e3d9SRandall Stewart return (EBUSY); 1559bbb0e3d9SRandall Stewart } 15606a9148feSBjoern A. Zeeb up->u_tun_func = f; 1561abb901c5SRandall Stewart up->u_icmp_func = i; 156281d3ec17SBryan Venteicher up->u_tun_ctx = ctx; 15638501a69cSRobert Watson INP_WUNLOCK(inp); 15643329b236SRobert Watson return (0); 1565df8bae1dSRodney W. Grimes } 1566d0390e05SGarrett Wollman 156779288c11SBjoern A. Zeeb #ifdef INET 1568d0390e05SGarrett Wollman static int 1569b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1570d0390e05SGarrett Wollman { 1571d0390e05SGarrett Wollman struct inpcb *inp; 1572e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1573277afaffSRobert Watson int error; 1574d0390e05SGarrett Wollman 1575a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1576d0390e05SGarrett Wollman inp = sotoinpcb(so); 157714ba8addSRobert Watson KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 15788501a69cSRobert Watson INP_WLOCK(inp); 1579e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1580b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, nam, td->td_ucred); 1581e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 15828501a69cSRobert Watson INP_WUNLOCK(inp); 15833329b236SRobert Watson return (error); 1584d0390e05SGarrett Wollman } 1585d0390e05SGarrett Wollman 1586a152f8a3SRobert Watson static void 1587a152f8a3SRobert Watson udp_close(struct socket *so) 1588a152f8a3SRobert Watson { 1589a152f8a3SRobert Watson struct inpcb *inp; 1590e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1591a152f8a3SRobert Watson 1592a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1593a152f8a3SRobert Watson inp = sotoinpcb(so); 1594a152f8a3SRobert Watson KASSERT(inp != NULL, ("udp_close: inp == NULL")); 15958501a69cSRobert Watson INP_WLOCK(inp); 1596a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1597e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1598a152f8a3SRobert Watson in_pcbdisconnect(inp); 1599a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1600e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1601a152f8a3SRobert Watson soisdisconnected(so); 1602a152f8a3SRobert Watson } 16038501a69cSRobert Watson INP_WUNLOCK(inp); 1604a152f8a3SRobert Watson } 1605a152f8a3SRobert Watson 1606d0390e05SGarrett Wollman static int 1607b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1608d0390e05SGarrett Wollman { 1609d0390e05SGarrett Wollman struct inpcb *inp; 1610e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 161175c13541SPoul-Henning Kamp struct sockaddr_in *sin; 1612e06e816fSKevin Lo int error; 1613d0390e05SGarrett Wollman 1614a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1615d0390e05SGarrett Wollman inp = sotoinpcb(so); 161614ba8addSRobert Watson KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 16178501a69cSRobert Watson INP_WLOCK(inp); 1618f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr != INADDR_ANY) { 16198501a69cSRobert Watson INP_WUNLOCK(inp); 16203329b236SRobert Watson return (EISCONN); 1621f76fcf6dSJeffrey Hsu } 162275c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)nam; 1623b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1624b89e82ddSJamie Gritton if (error != 0) { 1625413628a7SBjoern A. Zeeb INP_WUNLOCK(inp); 1626b89e82ddSJamie Gritton return (error); 1627413628a7SBjoern A. Zeeb } 1628e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1629b0330ed9SPawel Jakub Dawidek error = in_pcbconnect(inp, nam, td->td_ucred); 1630e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 16314cc20ab1SSeigo Tanimura if (error == 0) 1632df8bae1dSRodney W. Grimes soisconnected(so); 16338501a69cSRobert Watson INP_WUNLOCK(inp); 16343329b236SRobert Watson return (error); 1635df8bae1dSRodney W. Grimes } 1636d0390e05SGarrett Wollman 1637bc725eafSRobert Watson static void 1638d0390e05SGarrett Wollman udp_detach(struct socket *so) 1639d0390e05SGarrett Wollman { 1640d0390e05SGarrett Wollman struct inpcb *inp; 1641e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 16426a9148feSBjoern A. Zeeb struct udpcb *up; 1643d0390e05SGarrett Wollman 1644a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1645d0390e05SGarrett Wollman inp = sotoinpcb(so); 164614ba8addSRobert Watson KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1647a152f8a3SRobert Watson KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1648a152f8a3SRobert Watson ("udp_detach: not disconnected")); 1649e06e816fSKevin Lo INP_INFO_WLOCK(pcbinfo); 16508501a69cSRobert Watson INP_WLOCK(inp); 16516a9148feSBjoern A. Zeeb up = intoudpcb(inp); 16526a9148feSBjoern A. Zeeb KASSERT(up != NULL, ("%s: up == NULL", __func__)); 16536a9148feSBjoern A. Zeeb inp->inp_ppcb = NULL; 1654d0390e05SGarrett Wollman in_pcbdetach(inp); 165514ba8addSRobert Watson in_pcbfree(inp); 1656e06e816fSKevin Lo INP_INFO_WUNLOCK(pcbinfo); 16576a9148feSBjoern A. Zeeb udp_discardcb(up); 1658d0390e05SGarrett Wollman } 1659d0390e05SGarrett Wollman 1660d0390e05SGarrett Wollman static int 1661d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 1662d0390e05SGarrett Wollman { 1663d0390e05SGarrett Wollman struct inpcb *inp; 1664e06e816fSKevin Lo struct inpcbinfo *pcbinfo; 1665d0390e05SGarrett Wollman 1666a86e5c96SBjoern A. Zeeb pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1667d0390e05SGarrett Wollman inp = sotoinpcb(so); 166814ba8addSRobert Watson KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 16698501a69cSRobert Watson INP_WLOCK(inp); 1670f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr == INADDR_ANY) { 16718501a69cSRobert Watson INP_WUNLOCK(inp); 16723329b236SRobert Watson return (ENOTCONN); 1673f76fcf6dSJeffrey Hsu } 1674e06e816fSKevin Lo INP_HASH_WLOCK(pcbinfo); 1675df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 1676df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 1677e06e816fSKevin Lo INP_HASH_WUNLOCK(pcbinfo); 1678d45e4f99SMaxim Konovalov SOCK_LOCK(so); 1679d45e4f99SMaxim Konovalov so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1680d45e4f99SMaxim Konovalov SOCK_UNLOCK(so); 16818501a69cSRobert Watson INP_WUNLOCK(inp); 16823329b236SRobert Watson return (0); 1683df8bae1dSRodney W. Grimes } 1684df8bae1dSRodney W. Grimes 1685d0390e05SGarrett Wollman static int 168657bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1687b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1688d0390e05SGarrett Wollman { 1689d0390e05SGarrett Wollman struct inpcb *inp; 1690d0390e05SGarrett Wollman 1691d0390e05SGarrett Wollman inp = sotoinpcb(so); 169214ba8addSRobert Watson KASSERT(inp != NULL, ("udp_send: inp == NULL")); 16933329b236SRobert Watson return (udp_output(inp, m, addr, control, td)); 1694d0390e05SGarrett Wollman } 169579288c11SBjoern A. Zeeb #endif /* INET */ 1696d0390e05SGarrett Wollman 169776429de4SYoshinobu Inoue int 1698d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 1699d0390e05SGarrett Wollman { 1700d0390e05SGarrett Wollman struct inpcb *inp; 1701d0390e05SGarrett Wollman 1702d0390e05SGarrett Wollman inp = sotoinpcb(so); 170314ba8addSRobert Watson KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 17048501a69cSRobert Watson INP_WLOCK(inp); 1705d0390e05SGarrett Wollman socantsendmore(so); 17068501a69cSRobert Watson INP_WUNLOCK(inp); 17073329b236SRobert Watson return (0); 1708d0390e05SGarrett Wollman } 1709d0390e05SGarrett Wollman 171079288c11SBjoern A. Zeeb #ifdef INET 1711d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 1712756d52a1SPoul-Henning Kamp .pru_abort = udp_abort, 1713756d52a1SPoul-Henning Kamp .pru_attach = udp_attach, 1714756d52a1SPoul-Henning Kamp .pru_bind = udp_bind, 1715756d52a1SPoul-Henning Kamp .pru_connect = udp_connect, 1716756d52a1SPoul-Henning Kamp .pru_control = in_control, 1717756d52a1SPoul-Henning Kamp .pru_detach = udp_detach, 1718756d52a1SPoul-Henning Kamp .pru_disconnect = udp_disconnect, 171954d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1720756d52a1SPoul-Henning Kamp .pru_send = udp_send, 17215df3e839SRobert Watson .pru_soreceive = soreceive_dgram, 172259b8854eSRobert Watson .pru_sosend = sosend_dgram, 1723756d52a1SPoul-Henning Kamp .pru_shutdown = udp_shutdown, 172454d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1725a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1726a152f8a3SRobert Watson .pru_close = udp_close, 1727d0390e05SGarrett Wollman }; 172879288c11SBjoern A. Zeeb #endif /* INET */ 1729