1c398230bSWarner Losh /*- 26dfab5b1SGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 33329b236SRobert Watson * The Regents of the University of California. 43144b7d3SRobert Watson * Copyright (c) 2008 Robert N. M. Watson 53329b236SRobert Watson * All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 316dfab5b1SGarrett Wollman * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 344b421e2dSMike Silbersack #include <sys/cdefs.h> 354b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 364b421e2dSMike Silbersack 370b4ae859SGleb Smirnoff #include "opt_ipfw.h" 38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 39f5514f08SRobert Watson #include "opt_ipsec.h" 40cfa1ca9dSYoshinobu Inoue 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42960ed29cSSeigo Tanimura #include <sys/domain.h> 434f590175SPaul Saab #include <sys/eventhandler.h> 44960ed29cSSeigo Tanimura #include <sys/jail.h> 45b110a8a2SGarrett Wollman #include <sys/kernel.h> 46960ed29cSSeigo Tanimura #include <sys/lock.h> 47df8bae1dSRodney W. Grimes #include <sys/malloc.h> 48df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 49acd3428bSRobert Watson #include <sys/priv.h> 50490d50b6SBrian Feldman #include <sys/proc.h> 51df8bae1dSRodney W. Grimes #include <sys/protosw.h> 52960ed29cSSeigo Tanimura #include <sys/signalvar.h> 53df8bae1dSRodney W. Grimes #include <sys/socket.h> 54df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 55960ed29cSSeigo Tanimura #include <sys/sx.h> 56b5e8ce9fSBruce Evans #include <sys/sysctl.h> 57816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 58f5514f08SRobert Watson #include <sys/systm.h> 59603724d3SBjoern A. Zeeb #include <sys/vimage.h> 608781d8e9SBruce Evans 6169c2d429SJeff Roberson #include <vm/uma.h> 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes #include <net/if.h> 64df8bae1dSRodney W. Grimes #include <net/route.h> 65df8bae1dSRodney W. Grimes 66df8bae1dSRodney W. Grimes #include <netinet/in.h> 67960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 68f5514f08SRobert Watson #include <netinet/in_systm.h> 69960ed29cSSeigo Tanimura #include <netinet/in_var.h> 70df8bae1dSRodney W. Grimes #include <netinet/ip.h> 71cfa1ca9dSYoshinobu Inoue #ifdef INET6 72cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 73cfa1ca9dSYoshinobu Inoue #endif 74960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h> 75960ed29cSSeigo Tanimura #include <netinet/icmp_var.h> 76df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 77ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 78cfa1ca9dSYoshinobu Inoue #ifdef INET6 79cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 80cfa1ca9dSYoshinobu Inoue #endif 81df8bae1dSRodney W. Grimes #include <netinet/udp.h> 82df8bae1dSRodney W. Grimes #include <netinet/udp_var.h> 834b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 84df8bae1dSRodney W. Grimes 85b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 86b9234fafSSam Leffler #include <netipsec/ipsec.h> 873329b236SRobert Watson #endif 88b9234fafSSam Leffler 89db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 90db4f9cc7SJonathan Lemon 91aed55708SRobert Watson #include <security/mac/mac_framework.h> 92aed55708SRobert Watson 93df8bae1dSRodney W. Grimes /* 94df8bae1dSRodney W. Grimes * UDP protocol implementation. 95df8bae1dSRodney W. Grimes * Per RFC 768, August, 1980. 96df8bae1dSRodney W. Grimes */ 9774eb3236SWarner Losh 9844e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 9944e33a07SMarko Zec int udp_blackhole; 10044e33a07SMarko Zec #endif 10144e33a07SMarko Zec 10274eb3236SWarner Losh /* 1033329b236SRobert Watson * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums 1043329b236SRobert Watson * removes the only data integrity mechanism for packets and malformed 105f5514f08SRobert Watson * packets that would otherwise be discarded due to bad checksums, and may 106f5514f08SRobert Watson * cause problems (especially for NFS data blocks). 10774eb3236SWarner Losh */ 108f5514f08SRobert Watson static int udp_cksum = 1; 109f5514f08SRobert Watson SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum, 11041698ebfSTom Rhodes 0, "compute udp checksum"); 111df8bae1dSRodney W. Grimes 112afdb4274SRobert Watson int udp_log_in_vain = 0; 113816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 114afdb4274SRobert Watson &udp_log_in_vain, 0, "Log all incoming UDP packets"); 115816a3d83SPoul-Henning Kamp 11697021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_udp, OID_AUTO, blackhole, 11797021c24SMarko Zec CTLFLAG_RW, udp_blackhole, 0, 1183329b236SRobert Watson "Do not send port unreachables for refused connects"); 11916f7f31fSGeoff Rehmet 12043bbb6aaSRobert Watson u_long udp_sendspace = 9216; /* really max datagram size */ 12143bbb6aaSRobert Watson /* 40 1K datagrams */ 12243bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 12343bbb6aaSRobert Watson &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 12443bbb6aaSRobert Watson 12543bbb6aaSRobert Watson u_long udp_recvspace = 40 * (1024 + 12643bbb6aaSRobert Watson #ifdef INET6 12743bbb6aaSRobert Watson sizeof(struct sockaddr_in6) 12843bbb6aaSRobert Watson #else 12943bbb6aaSRobert Watson sizeof(struct sockaddr_in) 13043bbb6aaSRobert Watson #endif 13143bbb6aaSRobert Watson ); 13243bbb6aaSRobert Watson 13343bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 13443bbb6aaSRobert Watson &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 13543bbb6aaSRobert Watson 13644e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 13776429de4SYoshinobu Inoue struct inpcbhead udb; /* from udp_var.h */ 1387a2aab80SBrian Feldman struct inpcbinfo udbinfo; 1396a9148feSBjoern A. Zeeb static uma_zone_t udpcb_zone; 14044e33a07SMarko Zec struct udpstat udpstat; /* from udp_var.h */ 14144e33a07SMarko Zec #endif 14215bd2b43SDavid Greenman 14315bd2b43SDavid Greenman #ifndef UDBHASHSIZE 144e2ed8f35SAlexander Motin #define UDBHASHSIZE 128 14515bd2b43SDavid Greenman #endif 14615bd2b43SDavid Greenman 1478b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_udp, UDPCTL_STATS, stats, 1488b615593SMarko Zec CTLFLAG_RW, udpstat, udpstat, 1498b615593SMarko Zec "UDP statistics (struct udpstat, netinet/udp_var.h)"); 150f2ea20e6SGarrett Wollman 151bc725eafSRobert Watson static void udp_detach(struct socket *so); 1524d77a549SAlfred Perlstein static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 1534d77a549SAlfred Perlstein struct mbuf *, struct thread *); 154df8bae1dSRodney W. Grimes 1554f590175SPaul Saab static void 1564f590175SPaul Saab udp_zone_change(void *tag) 1574f590175SPaul Saab { 158093f25f8SMarko Zec INIT_VNET_INET(curvnet); 1594f590175SPaul Saab 160603724d3SBjoern A. Zeeb uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 1616a9148feSBjoern A. Zeeb uma_zone_set_max(V_udpcb_zone, maxsockets); 1624f590175SPaul Saab } 1634f590175SPaul Saab 164d915b280SStephan Uphoff static int 165d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags) 166d915b280SStephan Uphoff { 167af1ee11dSRobert Watson struct inpcb *inp; 16808651e1fSJohn Baldwin 169af1ee11dSRobert Watson inp = mem; 170d915b280SStephan Uphoff INP_LOCK_INIT(inp, "inp", "udpinp"); 171d915b280SStephan Uphoff return (0); 172d915b280SStephan Uphoff } 173d915b280SStephan Uphoff 174df8bae1dSRodney W. Grimes void 175af1ee11dSRobert Watson udp_init(void) 176df8bae1dSRodney W. Grimes { 1778b615593SMarko Zec INIT_VNET_INET(curvnet); 178af1ee11dSRobert Watson 17944e33a07SMarko Zec V_udp_blackhole = 0; 18044e33a07SMarko Zec 181603724d3SBjoern A. Zeeb INP_INFO_LOCK_INIT(&V_udbinfo, "udp"); 182603724d3SBjoern A. Zeeb LIST_INIT(&V_udb); 183f6dfe47aSMarko Zec #ifdef VIMAGE 184f6dfe47aSMarko Zec V_udbinfo.ipi_vnet = curvnet; 185f6dfe47aSMarko Zec #endif 1862c0d658fSJulian Elischer V_udbinfo.ipi_listhead = &V_udb; 187603724d3SBjoern A. Zeeb V_udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB, 188603724d3SBjoern A. Zeeb &V_udbinfo.ipi_hashmask); 189603724d3SBjoern A. Zeeb V_udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB, 190603724d3SBjoern A. Zeeb &V_udbinfo.ipi_porthashmask); 1916a9148feSBjoern A. Zeeb V_udbinfo.ipi_zone = uma_zcreate("udp_inpcb", sizeof(struct inpcb), 1926a9148feSBjoern A. Zeeb NULL, NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 193603724d3SBjoern A. Zeeb uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 1946a9148feSBjoern A. Zeeb 1956a9148feSBjoern A. Zeeb V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb), 1966a9148feSBjoern A. Zeeb NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1976a9148feSBjoern A. Zeeb uma_zone_set_max(V_udpcb_zone, maxsockets); 1986a9148feSBjoern A. Zeeb 1994f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL, 2004f590175SPaul Saab EVENTHANDLER_PRI_ANY); 201df8bae1dSRodney W. Grimes } 202df8bae1dSRodney W. Grimes 2036a9148feSBjoern A. Zeeb int 2046a9148feSBjoern A. Zeeb udp_newudpcb(struct inpcb *inp) 2056a9148feSBjoern A. Zeeb { 2066a9148feSBjoern A. Zeeb INIT_VNET_INET(curvnet); 2076a9148feSBjoern A. Zeeb struct udpcb *up; 2086a9148feSBjoern A. Zeeb 2096a9148feSBjoern A. Zeeb up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO); 2106a9148feSBjoern A. Zeeb if (up == NULL) 2116a9148feSBjoern A. Zeeb return (ENOBUFS); 2126a9148feSBjoern A. Zeeb inp->inp_ppcb = up; 2136a9148feSBjoern A. Zeeb return (0); 2146a9148feSBjoern A. Zeeb } 2156a9148feSBjoern A. Zeeb 2166a9148feSBjoern A. Zeeb void 2176a9148feSBjoern A. Zeeb udp_discardcb(struct udpcb *up) 2186a9148feSBjoern A. Zeeb { 2196a9148feSBjoern A. Zeeb INIT_VNET_INET(curvnet); 2206a9148feSBjoern A. Zeeb 2216a9148feSBjoern A. Zeeb uma_zfree(V_udpcb_zone, up); 2226a9148feSBjoern A. Zeeb } 2236a9148feSBjoern A. Zeeb 224bc29160dSMarko Zec #ifdef VIMAGE 225bc29160dSMarko Zec void 226bc29160dSMarko Zec udp_destroy(void) 227bc29160dSMarko Zec { 228bc29160dSMarko Zec INIT_VNET_INET(curvnet); 229bc29160dSMarko Zec 230bc29160dSMarko Zec hashdestroy(V_udbinfo.ipi_hashbase, M_PCB, 231bc29160dSMarko Zec V_udbinfo.ipi_hashmask); 232bc29160dSMarko Zec hashdestroy(V_udbinfo.ipi_porthashbase, M_PCB, 233bc29160dSMarko Zec V_udbinfo.ipi_porthashmask); 234bc29160dSMarko Zec INP_INFO_LOCK_DESTROY(&V_udbinfo); 235bc29160dSMarko Zec } 236bc29160dSMarko Zec #endif 237bc29160dSMarko Zec 23843bbb6aaSRobert Watson /* 23943bbb6aaSRobert Watson * Subroutine of udp_input(), which appends the provided mbuf chain to the 24043bbb6aaSRobert Watson * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 24143bbb6aaSRobert Watson * contains the source address. If the socket ends up being an IPv6 socket, 24243bbb6aaSRobert Watson * udp_append() will convert to a sockaddr_in6 before passing the address 24343bbb6aaSRobert Watson * into the socket code. 24443bbb6aaSRobert Watson */ 24543bbb6aaSRobert Watson static void 24643bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off, 24743bbb6aaSRobert Watson struct sockaddr_in *udp_in) 24843bbb6aaSRobert Watson { 24943bbb6aaSRobert Watson struct sockaddr *append_sa; 25043bbb6aaSRobert Watson struct socket *so; 25143bbb6aaSRobert Watson struct mbuf *opts = 0; 25243bbb6aaSRobert Watson #ifdef INET6 25343bbb6aaSRobert Watson struct sockaddr_in6 udp_in6; 25443bbb6aaSRobert Watson #endif 25543bbb6aaSRobert Watson 256119d85f6SRobert Watson INP_RLOCK_ASSERT(inp); 25743bbb6aaSRobert Watson 25843bbb6aaSRobert Watson #ifdef IPSEC 25943bbb6aaSRobert Watson /* Check AH/ESP integrity. */ 26043bbb6aaSRobert Watson if (ipsec4_in_reject(n, inp)) { 2618b615593SMarko Zec INIT_VNET_IPSEC(curvnet); 26243bbb6aaSRobert Watson m_freem(n); 263603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 26443bbb6aaSRobert Watson return; 26543bbb6aaSRobert Watson } 26643bbb6aaSRobert Watson #endif /* IPSEC */ 26743bbb6aaSRobert Watson #ifdef MAC 26830d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, n) != 0) { 26943bbb6aaSRobert Watson m_freem(n); 27043bbb6aaSRobert Watson return; 27143bbb6aaSRobert Watson } 27243bbb6aaSRobert Watson #endif 27343bbb6aaSRobert Watson if (inp->inp_flags & INP_CONTROLOPTS || 27443bbb6aaSRobert Watson inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 27543bbb6aaSRobert Watson #ifdef INET6 2769a38ba81SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6) 27748d48eb9SBjoern A. Zeeb (void)ip6_savecontrol_v4(inp, n, &opts, NULL); 2789a38ba81SBjoern A. Zeeb else 27943bbb6aaSRobert Watson #endif 28043bbb6aaSRobert Watson ip_savecontrol(inp, &opts, ip, n); 28143bbb6aaSRobert Watson } 28243bbb6aaSRobert Watson #ifdef INET6 28343bbb6aaSRobert Watson if (inp->inp_vflag & INP_IPV6) { 28443bbb6aaSRobert Watson bzero(&udp_in6, sizeof(udp_in6)); 28543bbb6aaSRobert Watson udp_in6.sin6_len = sizeof(udp_in6); 28643bbb6aaSRobert Watson udp_in6.sin6_family = AF_INET6; 28743bbb6aaSRobert Watson in6_sin_2_v4mapsin6(udp_in, &udp_in6); 28843bbb6aaSRobert Watson append_sa = (struct sockaddr *)&udp_in6; 28943bbb6aaSRobert Watson } else 29043bbb6aaSRobert Watson #endif 29143bbb6aaSRobert Watson append_sa = (struct sockaddr *)udp_in; 29243bbb6aaSRobert Watson m_adj(n, off); 29343bbb6aaSRobert Watson 29443bbb6aaSRobert Watson so = inp->inp_socket; 29543bbb6aaSRobert Watson SOCKBUF_LOCK(&so->so_rcv); 29643bbb6aaSRobert Watson if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 2978b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 29843bbb6aaSRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 29943bbb6aaSRobert Watson m_freem(n); 30043bbb6aaSRobert Watson if (opts) 30143bbb6aaSRobert Watson m_freem(opts); 302026decb8SRobert Watson UDPSTAT_INC(udps_fullsock); 30343bbb6aaSRobert Watson } else 30443bbb6aaSRobert Watson sorwakeup_locked(so); 30543bbb6aaSRobert Watson } 30643bbb6aaSRobert Watson 307df8bae1dSRodney W. Grimes void 3083329b236SRobert Watson udp_input(struct mbuf *m, int off) 309df8bae1dSRodney W. Grimes { 3108b615593SMarko Zec INIT_VNET_INET(curvnet); 311cfa1ca9dSYoshinobu Inoue int iphlen = off; 3123329b236SRobert Watson struct ip *ip; 3133329b236SRobert Watson struct udphdr *uh; 31471498f30SBruce M Simpson struct ifnet *ifp; 3153329b236SRobert Watson struct inpcb *inp; 3166a9148feSBjoern A. Zeeb struct udpcb *up; 317df8bae1dSRodney W. Grimes int len; 318df8bae1dSRodney W. Grimes struct ip save_ip; 319d4b509bdSRobert Watson struct sockaddr_in udp_in; 3200b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD 3210b4ae859SGleb Smirnoff struct m_tag *fwd_tag; 3220b4ae859SGleb Smirnoff #endif 323df8bae1dSRodney W. Grimes 32471498f30SBruce M Simpson ifp = m->m_pkthdr.rcvif; 325026decb8SRobert Watson UDPSTAT_INC(udps_ipackets); 326df8bae1dSRodney W. Grimes 327df8bae1dSRodney W. Grimes /* 3283329b236SRobert Watson * Strip IP options, if any; should skip this, make available to 3293329b236SRobert Watson * user, and use on returned packets, but we don't yet have a way to 3303329b236SRobert Watson * check the checksum with options still present. 331df8bae1dSRodney W. Grimes */ 332df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 333df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 334df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 335df8bae1dSRodney W. Grimes } 336df8bae1dSRodney W. Grimes 337df8bae1dSRodney W. Grimes /* 338df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 339df8bae1dSRodney W. Grimes */ 340df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 341df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 342df8bae1dSRodney W. Grimes if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 343026decb8SRobert Watson UDPSTAT_INC(udps_hdrops); 344df8bae1dSRodney W. Grimes return; 345df8bae1dSRodney W. Grimes } 346df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 347df8bae1dSRodney W. Grimes } 348df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 349df8bae1dSRodney W. Grimes 3503329b236SRobert Watson /* 3513329b236SRobert Watson * Destination port of 0 is illegal, based on RFC768. 3523329b236SRobert Watson */ 353686cdd19SJun-ichiro itojun Hagino if (uh->uh_dport == 0) 354f76fcf6dSJeffrey Hsu goto badunlocked; 355686cdd19SJun-ichiro itojun Hagino 356df8bae1dSRodney W. Grimes /* 3573329b236SRobert Watson * Construct sockaddr format source address. Stuff source address 3583329b236SRobert Watson * and datagram in user buffer. 359b9234fafSSam Leffler */ 360d4b509bdSRobert Watson bzero(&udp_in, sizeof(udp_in)); 361d4b509bdSRobert Watson udp_in.sin_len = sizeof(udp_in); 362d4b509bdSRobert Watson udp_in.sin_family = AF_INET; 363b9234fafSSam Leffler udp_in.sin_port = uh->uh_sport; 364b9234fafSSam Leffler udp_in.sin_addr = ip->ip_src; 365b9234fafSSam Leffler 366b9234fafSSam Leffler /* 367af1ee11dSRobert Watson * Make mbuf data length reflect UDP length. If not enough data to 368af1ee11dSRobert Watson * reflect UDP length, drop. 369df8bae1dSRodney W. Grimes */ 370df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 371df8bae1dSRodney W. Grimes if (ip->ip_len != len) { 3727eb7a449SAndras Olah if (len > ip->ip_len || len < sizeof(struct udphdr)) { 373026decb8SRobert Watson UDPSTAT_INC(udps_badlen); 374f76fcf6dSJeffrey Hsu goto badunlocked; 375df8bae1dSRodney W. Grimes } 376df8bae1dSRodney W. Grimes m_adj(m, len - ip->ip_len); 377df8bae1dSRodney W. Grimes /* ip->ip_len = len; */ 378df8bae1dSRodney W. Grimes } 3793329b236SRobert Watson 380df8bae1dSRodney W. Grimes /* 3813329b236SRobert Watson * Save a copy of the IP header in case we want restore it for 3823329b236SRobert Watson * sending an ICMP error message in response. 383df8bae1dSRodney W. Grimes */ 384603724d3SBjoern A. Zeeb if (!V_udp_blackhole) 385df8bae1dSRodney W. Grimes save_ip = *ip; 386cce418d3SMatt Jacob else 387cce418d3SMatt Jacob memset(&save_ip, 0, sizeof(save_ip)); 388df8bae1dSRodney W. Grimes 389df8bae1dSRodney W. Grimes /* 390df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 391df8bae1dSRodney W. Grimes */ 3926dfab5b1SGarrett Wollman if (uh->uh_sum) { 39339629c92SDavid Malone u_short uh_sum; 39439629c92SDavid Malone 395db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 396db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 39739629c92SDavid Malone uh_sum = m->m_pkthdr.csum_data; 398db4f9cc7SJonathan Lemon else 39939629c92SDavid Malone uh_sum = in_pseudo(ip->ip_src.s_addr, 400506f4949SRuslan Ermilov ip->ip_dst.s_addr, htonl((u_short)len + 401db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data + IPPROTO_UDP)); 40239629c92SDavid Malone uh_sum ^= 0xffff; 403db4f9cc7SJonathan Lemon } else { 404cb342100SHajimu UMEMOTO char b[9]; 405af1ee11dSRobert Watson 406cb342100SHajimu UMEMOTO bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 4076effc713SDoug Rabson bzero(((struct ipovly *)ip)->ih_x1, 9); 408df8bae1dSRodney W. Grimes ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 40939629c92SDavid Malone uh_sum = in_cksum(m, len + sizeof (struct ip)); 410cb342100SHajimu UMEMOTO bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 411db4f9cc7SJonathan Lemon } 41239629c92SDavid Malone if (uh_sum) { 413026decb8SRobert Watson UDPSTAT_INC(udps_badsum); 414df8bae1dSRodney W. Grimes m_freem(m); 415df8bae1dSRodney W. Grimes return; 416df8bae1dSRodney W. Grimes } 417fb9aaba0SRuslan Ermilov } else 418026decb8SRobert Watson UDPSTAT_INC(udps_nosum); 419df8bae1dSRodney W. Grimes 4200b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD 4213329b236SRobert Watson /* 4223329b236SRobert Watson * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 4233329b236SRobert Watson */ 4240b4ae859SGleb Smirnoff fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 4250b4ae859SGleb Smirnoff if (fwd_tag != NULL) { 4260b4ae859SGleb Smirnoff struct sockaddr_in *next_hop; 4270b4ae859SGleb Smirnoff 4283329b236SRobert Watson /* 4293329b236SRobert Watson * Do the hack. 4303329b236SRobert Watson */ 4310b4ae859SGleb Smirnoff next_hop = (struct sockaddr_in *)(fwd_tag + 1); 4320b4ae859SGleb Smirnoff ip->ip_dst = next_hop->sin_addr; 4330b4ae859SGleb Smirnoff uh->uh_dport = ntohs(next_hop->sin_port); 4343329b236SRobert Watson 4353329b236SRobert Watson /* 4363329b236SRobert Watson * Remove the tag from the packet. We don't need it anymore. 4373329b236SRobert Watson */ 4380b4ae859SGleb Smirnoff m_tag_delete(m, fwd_tag); 4390b4ae859SGleb Smirnoff } 4400b4ae859SGleb Smirnoff #endif 4410b4ae859SGleb Smirnoff 442603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 443df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 44471498f30SBruce M Simpson in_broadcast(ip->ip_dst, ifp)) { 44582c23ebaSBill Fenner struct inpcb *last; 44671498f30SBruce M Simpson struct ip_moptions *imo; 4473329b236SRobert Watson 448df8bae1dSRodney W. Grimes last = NULL; 449603724d3SBjoern A. Zeeb LIST_FOREACH(inp, &V_udb, inp_list) { 4509c1df695SRobert Watson if (inp->inp_lport != uh->uh_dport) 451f76fcf6dSJeffrey Hsu continue; 452cfa1ca9dSYoshinobu Inoue #ifdef INET6 453369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 4549c1df695SRobert Watson continue; 455cfa1ca9dSYoshinobu Inoue #endif 45671498f30SBruce M Simpson if (inp->inp_laddr.s_addr != INADDR_ANY && 45771498f30SBruce M Simpson inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 4589c1df695SRobert Watson continue; 45971498f30SBruce M Simpson if (inp->inp_faddr.s_addr != INADDR_ANY && 46071498f30SBruce M Simpson inp->inp_faddr.s_addr != ip->ip_src.s_addr) 46171498f30SBruce M Simpson continue; 46271498f30SBruce M Simpson if (inp->inp_fport != 0 && 463df8bae1dSRodney W. Grimes inp->inp_fport != uh->uh_sport) 4649c1df695SRobert Watson continue; 46571498f30SBruce M Simpson 466119d85f6SRobert Watson INP_RLOCK(inp); 467df8bae1dSRodney W. Grimes 46883453a06SBruce M Simpson /* 46971498f30SBruce M Simpson * Handle socket delivery policy for any-source 47071498f30SBruce M Simpson * and source-specific multicast. [RFC3678] 47183453a06SBruce M Simpson */ 47271498f30SBruce M Simpson imo = inp->inp_moptions; 47371498f30SBruce M Simpson if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 47471498f30SBruce M Simpson imo != NULL) { 475d10910e6SBruce M Simpson struct sockaddr_in group; 476d10910e6SBruce M Simpson int blocked; 47783453a06SBruce M Simpson 478d10910e6SBruce M Simpson bzero(&group, sizeof(struct sockaddr_in)); 479d10910e6SBruce M Simpson group.sin_len = sizeof(struct sockaddr_in); 480d10910e6SBruce M Simpson group.sin_family = AF_INET; 481d10910e6SBruce M Simpson group.sin_addr = ip->ip_dst; 48271498f30SBruce M Simpson 483d10910e6SBruce M Simpson blocked = imo_multi_filter(imo, ifp, 484d10910e6SBruce M Simpson (struct sockaddr *)&group, 48571498f30SBruce M Simpson (struct sockaddr *)&udp_in); 486d10910e6SBruce M Simpson if (blocked != MCAST_PASS) { 487d10910e6SBruce M Simpson if (blocked == MCAST_NOTGMEMBER) 48886425c62SRobert Watson IPSTAT_INC(ips_notmember); 489d10910e6SBruce M Simpson if (blocked == MCAST_NOTSMEMBER || 490d10910e6SBruce M Simpson blocked == MCAST_MUTED) 491026decb8SRobert Watson UDPSTAT_INC(udps_filtermcast); 492119d85f6SRobert Watson INP_RUNLOCK(inp); 4939c1df695SRobert Watson continue; 4949c1df695SRobert Watson } 49583453a06SBruce M Simpson } 496df8bae1dSRodney W. Grimes if (last != NULL) { 497df8bae1dSRodney W. Grimes struct mbuf *n; 498df8bae1dSRodney W. Grimes 499032dcc76SLuigi Rizzo n = m_copy(m, 0, M_COPYALL); 5006a9148feSBjoern A. Zeeb up = intoudpcb(last); 5016a9148feSBjoern A. Zeeb if (up->u_tun_func == NULL) { 502365433d9SRobert Watson if (n != NULL) 503c7c7ea4bSRandall Stewart udp_append(last, 504c7c7ea4bSRandall Stewart ip, n, 505c7c7ea4bSRandall Stewart iphlen + 506c7c7ea4bSRandall Stewart sizeof(struct udphdr), 507c7c7ea4bSRandall Stewart &udp_in); 508c7c7ea4bSRandall Stewart } else { 509c7c7ea4bSRandall Stewart /* 510c7c7ea4bSRandall Stewart * Engage the tunneling protocol we 511c7c7ea4bSRandall Stewart * will have to leave the info_lock 512c7c7ea4bSRandall Stewart * up, since we are hunting through 513bbb0e3d9SRandall Stewart * multiple UDP's. 514c7c7ea4bSRandall Stewart */ 515c7c7ea4bSRandall Stewart 5166a9148feSBjoern A. Zeeb (*up->u_tun_func)(n, iphlen, last); 517c7c7ea4bSRandall Stewart } 5186a9148feSBjoern A. Zeeb INP_RUNLOCK(last); 519df8bae1dSRodney W. Grimes } 52082c23ebaSBill Fenner last = inp; 521df8bae1dSRodney W. Grimes /* 522df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 523df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 5243329b236SRobert Watson * socket options set. This heuristic avoids 5253329b236SRobert Watson * searching through all pcbs in the common case of a 5263329b236SRobert Watson * non-shared port. It assumes that an application 5273329b236SRobert Watson * will never clear these options after setting them. 528df8bae1dSRodney W. Grimes */ 5293329b236SRobert Watson if ((last->inp_socket->so_options & 5303329b236SRobert Watson (SO_REUSEPORT|SO_REUSEADDR)) == 0) 531df8bae1dSRodney W. Grimes break; 532df8bae1dSRodney W. Grimes } 533df8bae1dSRodney W. Grimes 534df8bae1dSRodney W. Grimes if (last == NULL) { 535df8bae1dSRodney W. Grimes /* 5363329b236SRobert Watson * No matching pcb found; discard datagram. (No need 5373329b236SRobert Watson * to send an ICMP Port Unreachable for a broadcast 5383329b236SRobert Watson * or multicast datgram.) 539df8bae1dSRodney W. Grimes */ 540026decb8SRobert Watson UDPSTAT_INC(udps_noportbcast); 54161ffc0b1SJeffrey Hsu goto badheadlocked; 542df8bae1dSRodney W. Grimes } 5436a9148feSBjoern A. Zeeb up = intoudpcb(last); 5446a9148feSBjoern A. Zeeb if (up->u_tun_func == NULL) { 545d4b509bdSRobert Watson udp_append(last, ip, m, iphlen + sizeof(struct udphdr), 546d4b509bdSRobert Watson &udp_in); 547c7c7ea4bSRandall Stewart } else { 548c7c7ea4bSRandall Stewart /* 549bbb0e3d9SRandall Stewart * Engage the tunneling protocol. 550c7c7ea4bSRandall Stewart */ 5516a9148feSBjoern A. Zeeb (*up->u_tun_func)(m, iphlen, last); 5526a9148feSBjoern A. Zeeb } 553c7c7ea4bSRandall Stewart INP_RUNLOCK(last); 554c7c7ea4bSRandall Stewart INP_INFO_RUNLOCK(&V_udbinfo); 555df8bae1dSRodney W. Grimes return; 556df8bae1dSRodney W. Grimes } 5573329b236SRobert Watson 558df8bae1dSRodney W. Grimes /* 5596d6a026bSDavid Greenman * Locate pcb for datagram. 560df8bae1dSRodney W. Grimes */ 561603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_udbinfo, ip->ip_src, uh->uh_sport, 56271498f30SBruce M Simpson ip->ip_dst, uh->uh_dport, 1, ifp); 56315bd2b43SDavid Greenman if (inp == NULL) { 564afdb4274SRobert Watson if (udp_log_in_vain) { 565df5c0b8aSBill Fenner char buf[4*sizeof "123"]; 56675cfc95fSAndrey A. Chernov 56775cfc95fSAndrey A. Chernov strcpy(buf, inet_ntoa(ip->ip_dst)); 568592071e8SBruce Evans log(LOG_INFO, 569592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 570592071e8SBruce Evans buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 571592071e8SBruce Evans ntohs(uh->uh_sport)); 57275cfc95fSAndrey A. Chernov } 573026decb8SRobert Watson UDPSTAT_INC(udps_noport); 574df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 575026decb8SRobert Watson UDPSTAT_INC(udps_noportbcast); 57661ffc0b1SJeffrey Hsu goto badheadlocked; 577df8bae1dSRodney W. Grimes } 578603724d3SBjoern A. Zeeb if (V_udp_blackhole) 57961ffc0b1SJeffrey Hsu goto badheadlocked; 5801cbd978eSLuigi Rizzo if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 5811cbd978eSLuigi Rizzo goto badheadlocked; 58204287599SRuslan Ermilov *ip = save_ip; 58304287599SRuslan Ermilov ip->ip_len += iphlen; 584582a7760SBruce Evans icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 585603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 586df8bae1dSRodney W. Grimes return; 587df8bae1dSRodney W. Grimes } 5883329b236SRobert Watson 5893329b236SRobert Watson /* 5903329b236SRobert Watson * Check the minimum TTL for socket. 5913329b236SRobert Watson */ 592119d85f6SRobert Watson INP_RLOCK(inp); 593603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 59410cc62b7SRobert Watson if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) { 59510cc62b7SRobert Watson INP_RUNLOCK(inp); 59610cc62b7SRobert Watson goto badunlocked; 59710cc62b7SRobert Watson } 5986a9148feSBjoern A. Zeeb up = intoudpcb(inp); 5996a9148feSBjoern A. Zeeb if (up->u_tun_func == NULL) { 6006a9148feSBjoern A. Zeeb udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in); 6016a9148feSBjoern A. Zeeb } else { 602c7c7ea4bSRandall Stewart /* 603bbb0e3d9SRandall Stewart * Engage the tunneling protocol. 604c7c7ea4bSRandall Stewart */ 605c7c7ea4bSRandall Stewart 6066a9148feSBjoern A. Zeeb (*up->u_tun_func)(m, iphlen, inp); 607c7c7ea4bSRandall Stewart } 608119d85f6SRobert Watson INP_RUNLOCK(inp); 609df8bae1dSRodney W. Grimes return; 61061ffc0b1SJeffrey Hsu 61161ffc0b1SJeffrey Hsu badheadlocked: 612f76fcf6dSJeffrey Hsu if (inp) 613119d85f6SRobert Watson INP_RUNLOCK(inp); 614603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 615f76fcf6dSJeffrey Hsu badunlocked: 616df8bae1dSRodney W. Grimes m_freem(m); 617cfa1ca9dSYoshinobu Inoue } 618cfa1ca9dSYoshinobu Inoue 619cfa1ca9dSYoshinobu Inoue /* 6203329b236SRobert Watson * Notify a udp user of an asynchronous error; just wake up so that they can 6213329b236SRobert Watson * collect error status. 622df8bae1dSRodney W. Grimes */ 6233ce144eaSJeffrey Hsu struct inpcb * 6243329b236SRobert Watson udp_notify(struct inpcb *inp, int errno) 625df8bae1dSRodney W. Grimes { 6263329b236SRobert Watson 627ac9ae279SRobert Watson /* 628ac9ae279SRobert Watson * While udp_ctlinput() always calls udp_notify() with a read lock 629ac9ae279SRobert Watson * when invoking it directly, in_pcbnotifyall() currently uses write 630ac9ae279SRobert Watson * locks due to sharing code with TCP. For now, accept either a read 631ac9ae279SRobert Watson * or a write lock, but a read lock is sufficient. 632ac9ae279SRobert Watson */ 633ac9ae279SRobert Watson INP_LOCK_ASSERT(inp); 6348501a69cSRobert Watson 635df8bae1dSRodney W. Grimes inp->inp_socket->so_error = errno; 636df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 637df8bae1dSRodney W. Grimes sowwakeup(inp->inp_socket); 6383329b236SRobert Watson return (inp); 639df8bae1dSRodney W. Grimes } 640df8bae1dSRodney W. Grimes 641df8bae1dSRodney W. Grimes void 6423329b236SRobert Watson udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 643df8bae1dSRodney W. Grimes { 6448b615593SMarko Zec INIT_VNET_INET(curvnet); 645c693a045SJonathan Lemon struct ip *ip = vip; 646c693a045SJonathan Lemon struct udphdr *uh; 647c693a045SJonathan Lemon struct in_addr faddr; 648c693a045SJonathan Lemon struct inpcb *inp; 649c693a045SJonathan Lemon 650c693a045SJonathan Lemon faddr = ((struct sockaddr_in *)sa)->sin_addr; 651c693a045SJonathan Lemon if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 652c693a045SJonathan Lemon return; 653df8bae1dSRodney W. Grimes 65497d8d152SAndre Oppermann /* 65597d8d152SAndre Oppermann * Redirects don't need to be handled up here. 65697d8d152SAndre Oppermann */ 65797d8d152SAndre Oppermann if (PRC_IS_REDIRECT(cmd)) 65897d8d152SAndre Oppermann return; 6593329b236SRobert Watson 66097d8d152SAndre Oppermann /* 66197d8d152SAndre Oppermann * Hostdead is ugly because it goes linearly through all PCBs. 6623329b236SRobert Watson * 6633329b236SRobert Watson * XXX: We never get this from ICMP, otherwise it makes an excellent 6643329b236SRobert Watson * DoS attack on machines with many connections. 66597d8d152SAndre Oppermann */ 66697d8d152SAndre Oppermann if (cmd == PRC_HOSTDEAD) 667af1ee11dSRobert Watson ip = NULL; 668d1c54148SJesper Skriver else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 669df8bae1dSRodney W. Grimes return; 670af1ee11dSRobert Watson if (ip != NULL) { 671df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 672603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 673603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_udbinfo, faddr, uh->uh_dport, 674c693a045SJonathan Lemon ip->ip_src, uh->uh_sport, 0, NULL); 675f76fcf6dSJeffrey Hsu if (inp != NULL) { 676ac9ae279SRobert Watson INP_RLOCK(inp); 677f76fcf6dSJeffrey Hsu if (inp->inp_socket != NULL) { 678f5514f08SRobert Watson udp_notify(inp, inetctlerrmap[cmd]); 679f76fcf6dSJeffrey Hsu } 680ac9ae279SRobert Watson INP_RUNLOCK(inp); 681f76fcf6dSJeffrey Hsu } 682603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 683df8bae1dSRodney W. Grimes } else 684603724d3SBjoern A. Zeeb in_pcbnotifyall(&V_udbinfo, faddr, inetctlerrmap[cmd], 685f5514f08SRobert Watson udp_notify); 686df8bae1dSRodney W. Grimes } 687df8bae1dSRodney W. Grimes 6880312fbe9SPoul-Henning Kamp static int 68982d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS) 69098271db4SGarrett Wollman { 6918b615593SMarko Zec INIT_VNET_INET(curvnet); 692277afaffSRobert Watson int error, i, n; 69398271db4SGarrett Wollman struct inpcb *inp, **inp_list; 69498271db4SGarrett Wollman inp_gen_t gencnt; 69598271db4SGarrett Wollman struct xinpgen xig; 69698271db4SGarrett Wollman 69798271db4SGarrett Wollman /* 698f5514f08SRobert Watson * The process of preparing the PCB list is too time-consuming and 69998271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 70098271db4SGarrett Wollman */ 70198271db4SGarrett Wollman if (req->oldptr == 0) { 702603724d3SBjoern A. Zeeb n = V_udbinfo.ipi_count; 70398271db4SGarrett Wollman req->oldidx = 2 * (sizeof xig) 70498271db4SGarrett Wollman + (n + n/8) * sizeof(struct xinpcb); 7053329b236SRobert Watson return (0); 70698271db4SGarrett Wollman } 70798271db4SGarrett Wollman 70898271db4SGarrett Wollman if (req->newptr != 0) 7093329b236SRobert Watson return (EPERM); 71098271db4SGarrett Wollman 71198271db4SGarrett Wollman /* 71298271db4SGarrett Wollman * OK, now we're committed to doing something. 71398271db4SGarrett Wollman */ 714603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 715603724d3SBjoern A. Zeeb gencnt = V_udbinfo.ipi_gencnt; 716603724d3SBjoern A. Zeeb n = V_udbinfo.ipi_count; 717603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 71898271db4SGarrett Wollman 71947934cefSDon Lewis error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 7205c38b6dbSDon Lewis + n * sizeof(struct xinpcb)); 72147934cefSDon Lewis if (error != 0) 72247934cefSDon Lewis return (error); 7235c38b6dbSDon Lewis 72498271db4SGarrett Wollman xig.xig_len = sizeof xig; 72598271db4SGarrett Wollman xig.xig_count = n; 72698271db4SGarrett Wollman xig.xig_gen = gencnt; 72798271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 72898271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 72998271db4SGarrett Wollman if (error) 7303329b236SRobert Watson return (error); 73198271db4SGarrett Wollman 732a163d034SWarner Losh inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 73398271db4SGarrett Wollman if (inp_list == 0) 7343329b236SRobert Watson return (ENOMEM); 73598271db4SGarrett Wollman 736603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 737603724d3SBjoern A. Zeeb for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n; 738fc2ffbe6SPoul-Henning Kamp inp = LIST_NEXT(inp, inp_list)) { 7399622e84fSRobert Watson INP_RLOCK(inp); 7402ded288cSJeffrey Hsu if (inp->inp_gencnt <= gencnt && 741f08ef6c5SBjoern A. Zeeb cr_canseeinpcb(req->td->td_ucred, inp) == 0) 74298271db4SGarrett Wollman inp_list[i++] = inp; 7439622e84fSRobert Watson INP_RUNLOCK(inp); 7444787fd37SPaul Saab } 745603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 74698271db4SGarrett Wollman n = i; 74798271db4SGarrett Wollman 74898271db4SGarrett Wollman error = 0; 74998271db4SGarrett Wollman for (i = 0; i < n; i++) { 75098271db4SGarrett Wollman inp = inp_list[i]; 7519622e84fSRobert Watson INP_RLOCK(inp); 75298271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 75398271db4SGarrett Wollman struct xinpcb xi; 754fd94099eSColin Percival bzero(&xi, sizeof(xi)); 75598271db4SGarrett Wollman xi.xi_len = sizeof xi; 75698271db4SGarrett Wollman /* XXX should avoid extra copy */ 75798271db4SGarrett Wollman bcopy(inp, &xi.xi_inp, sizeof *inp); 75898271db4SGarrett Wollman if (inp->inp_socket) 75998271db4SGarrett Wollman sotoxsocket(inp->inp_socket, &xi.xi_socket); 7604b40c56cSJeffrey Hsu xi.xi_inp.inp_gencnt = inp->inp_gencnt; 7619622e84fSRobert Watson INP_RUNLOCK(inp); 76298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 763d915b280SStephan Uphoff } else 7649622e84fSRobert Watson INP_RUNLOCK(inp); 76598271db4SGarrett Wollman } 76698271db4SGarrett Wollman if (!error) { 76798271db4SGarrett Wollman /* 7683329b236SRobert Watson * Give the user an updated idea of our state. If the 7693329b236SRobert Watson * generation differs from what we told her before, she knows 7703329b236SRobert Watson * that something happened while we were processing this 7713329b236SRobert Watson * request, and it might be necessary to retry. 77298271db4SGarrett Wollman */ 773603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 774603724d3SBjoern A. Zeeb xig.xig_gen = V_udbinfo.ipi_gencnt; 77598271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 776603724d3SBjoern A. Zeeb xig.xig_count = V_udbinfo.ipi_count; 777603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 77898271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 77998271db4SGarrett Wollman } 78098271db4SGarrett Wollman free(inp_list, M_TEMP); 7813329b236SRobert Watson return (error); 78298271db4SGarrett Wollman } 78398271db4SGarrett Wollman 78498271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 78598271db4SGarrett Wollman udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 78698271db4SGarrett Wollman 78798271db4SGarrett Wollman static int 78882d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS) 789490d50b6SBrian Feldman { 7908b615593SMarko Zec INIT_VNET_INET(curvnet); 791c0511d3bSBrian Feldman struct xucred xuc; 792490d50b6SBrian Feldman struct sockaddr_in addrs[2]; 793490d50b6SBrian Feldman struct inpcb *inp; 794277afaffSRobert Watson int error; 795490d50b6SBrian Feldman 79632f9753cSRobert Watson error = priv_check(req->td, PRIV_NETINET_GETCRED); 797490d50b6SBrian Feldman if (error) 798490d50b6SBrian Feldman return (error); 799490d50b6SBrian Feldman error = SYSCTL_IN(req, addrs, sizeof(addrs)); 800490d50b6SBrian Feldman if (error) 801490d50b6SBrian Feldman return (error); 802603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 803603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 804cfa1ca9dSYoshinobu Inoue addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 8059622e84fSRobert Watson if (inp != NULL) { 8069622e84fSRobert Watson INP_RLOCK(inp); 807603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 8089622e84fSRobert Watson if (inp->inp_socket == NULL) 8099622e84fSRobert Watson error = ENOENT; 8109622e84fSRobert Watson if (error == 0) 811f08ef6c5SBjoern A. Zeeb error = cr_canseeinpcb(req->td->td_ucred, inp); 8129622e84fSRobert Watson if (error == 0) 81386d02c5cSBjoern A. Zeeb cru2x(inp->inp_cred, &xuc); 8149622e84fSRobert Watson INP_RUNLOCK(inp); 8159622e84fSRobert Watson } else { 816603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 8179622e84fSRobert Watson error = ENOENT; 8189622e84fSRobert Watson } 8190e1eebb8SDon Lewis if (error == 0) 8200e1eebb8SDon Lewis error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 821490d50b6SBrian Feldman return (error); 822490d50b6SBrian Feldman } 823490d50b6SBrian Feldman 8247ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 8257ce87f12SDavid Malone CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 8267ce87f12SDavid Malone udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 827490d50b6SBrian Feldman 828490d50b6SBrian Feldman static int 8293329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, 8303329b236SRobert Watson struct mbuf *control, struct thread *td) 831df8bae1dSRodney W. Grimes { 8328b615593SMarko Zec INIT_VNET_INET(inp->inp_vnet); 8333329b236SRobert Watson struct udpiphdr *ui; 8343329b236SRobert Watson int len = m->m_pkthdr.len; 83590162a4eSIan Dowse struct in_addr faddr, laddr; 836c557ae16SIan Dowse struct cmsghdr *cm; 837c557ae16SIan Dowse struct sockaddr_in *sin, src; 83890162a4eSIan Dowse int error = 0; 8398afa2304SBruce M Simpson int ipflags; 84090162a4eSIan Dowse u_short fport, lport; 8415c32ea65SRobert Watson int unlock_udbinfo; 842df8bae1dSRodney W. Grimes 8435c32ea65SRobert Watson /* 8445c32ea65SRobert Watson * udp_output() may need to temporarily bind or connect the current 845f5514f08SRobert Watson * inpcb. As such, we don't know up front whether we will need the 846f5514f08SRobert Watson * pcbinfo lock or not. Do any work to decide what is needed up 847f5514f08SRobert Watson * front before acquiring any locks. 8485c32ea65SRobert Watson */ 849430d30d8SBill Fenner if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 850c557ae16SIan Dowse if (control) 851c557ae16SIan Dowse m_freem(control); 8525c32ea65SRobert Watson m_freem(m); 8533329b236SRobert Watson return (EMSGSIZE); 854430d30d8SBill Fenner } 855430d30d8SBill Fenner 8561b7f0384SBruce M Simpson src.sin_family = 0; 857c557ae16SIan Dowse if (control != NULL) { 858c557ae16SIan Dowse /* 8593329b236SRobert Watson * XXX: Currently, we assume all the optional information is 8603329b236SRobert Watson * stored in a single mbuf. 861c557ae16SIan Dowse */ 862c557ae16SIan Dowse if (control->m_next) { 863c557ae16SIan Dowse m_freem(control); 8645c32ea65SRobert Watson m_freem(m); 8653329b236SRobert Watson return (EINVAL); 866c557ae16SIan Dowse } 867c557ae16SIan Dowse for (; control->m_len > 0; 868c557ae16SIan Dowse control->m_data += CMSG_ALIGN(cm->cmsg_len), 869c557ae16SIan Dowse control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 870c557ae16SIan Dowse cm = mtod(control, struct cmsghdr *); 871af1ee11dSRobert Watson if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 872af1ee11dSRobert Watson || cm->cmsg_len > control->m_len) { 873c557ae16SIan Dowse error = EINVAL; 874c557ae16SIan Dowse break; 875c557ae16SIan Dowse } 876c557ae16SIan Dowse if (cm->cmsg_level != IPPROTO_IP) 877c557ae16SIan Dowse continue; 878c557ae16SIan Dowse 879c557ae16SIan Dowse switch (cm->cmsg_type) { 880c557ae16SIan Dowse case IP_SENDSRCADDR: 881c557ae16SIan Dowse if (cm->cmsg_len != 882c557ae16SIan Dowse CMSG_LEN(sizeof(struct in_addr))) { 883c557ae16SIan Dowse error = EINVAL; 884c557ae16SIan Dowse break; 885c557ae16SIan Dowse } 886c557ae16SIan Dowse bzero(&src, sizeof(src)); 887c557ae16SIan Dowse src.sin_family = AF_INET; 888c557ae16SIan Dowse src.sin_len = sizeof(src); 889c557ae16SIan Dowse src.sin_port = inp->inp_lport; 890af1ee11dSRobert Watson src.sin_addr = 891af1ee11dSRobert Watson *(struct in_addr *)CMSG_DATA(cm); 892c557ae16SIan Dowse break; 893af1ee11dSRobert Watson 894c557ae16SIan Dowse default: 895c557ae16SIan Dowse error = ENOPROTOOPT; 896c557ae16SIan Dowse break; 897c557ae16SIan Dowse } 898c557ae16SIan Dowse if (error) 899c557ae16SIan Dowse break; 900c557ae16SIan Dowse } 901c557ae16SIan Dowse m_freem(control); 902c557ae16SIan Dowse } 9035c32ea65SRobert Watson if (error) { 9045c32ea65SRobert Watson m_freem(m); 9053329b236SRobert Watson return (error); 9065c32ea65SRobert Watson } 9075c32ea65SRobert Watson 90843cc0bc1SRobert Watson /* 90943cc0bc1SRobert Watson * Depending on whether or not the application has bound or connected 910ca528788SRobert Watson * the socket, we may have to do varying levels of work. The optimal 911ca528788SRobert Watson * case is for a connected UDP socket, as a global lock isn't 912ca528788SRobert Watson * required at all. 91343cc0bc1SRobert Watson * 91443cc0bc1SRobert Watson * In order to decide which we need, we require stability of the 91543cc0bc1SRobert Watson * inpcb binding, which we ensure by acquiring a read lock on the 91643cc0bc1SRobert Watson * inpcb. This doesn't strictly follow the lock order, so we play 91743cc0bc1SRobert Watson * the trylock and retry game; note that we may end up with more 91843cc0bc1SRobert Watson * conservative locks than required the second time around, so later 91943cc0bc1SRobert Watson * assertions have to accept that. Further analysis of the number of 92043cc0bc1SRobert Watson * misses under contention is required. 92143cc0bc1SRobert Watson */ 92243cc0bc1SRobert Watson sin = (struct sockaddr_in *)addr; 92343cc0bc1SRobert Watson INP_RLOCK(inp); 92443cc0bc1SRobert Watson if (sin != NULL && 92543cc0bc1SRobert Watson (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 92643cc0bc1SRobert Watson INP_RUNLOCK(inp); 927603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 9288501a69cSRobert Watson INP_WLOCK(inp); 92943cc0bc1SRobert Watson unlock_udbinfo = 2; 93043cc0bc1SRobert Watson } else if ((sin != NULL && ( 93143cc0bc1SRobert Watson (sin->sin_addr.s_addr == INADDR_ANY) || 93243cc0bc1SRobert Watson (sin->sin_addr.s_addr == INADDR_BROADCAST) || 93343cc0bc1SRobert Watson (inp->inp_laddr.s_addr == INADDR_ANY) || 93443cc0bc1SRobert Watson (inp->inp_lport == 0))) || 93543cc0bc1SRobert Watson (src.sin_family == AF_INET)) { 936603724d3SBjoern A. Zeeb if (!INP_INFO_TRY_RLOCK(&V_udbinfo)) { 93743cc0bc1SRobert Watson INP_RUNLOCK(inp); 938603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 939948d0fc9SRobert Watson INP_RLOCK(inp); 940948d0fc9SRobert Watson } 94143cc0bc1SRobert Watson unlock_udbinfo = 1; 94243cc0bc1SRobert Watson } else 94343cc0bc1SRobert Watson unlock_udbinfo = 0; 9445c32ea65SRobert Watson 9451b7f0384SBruce M Simpson /* 9461b7f0384SBruce M Simpson * If the IP_SENDSRCADDR control message was specified, override the 9471b7f0384SBruce M Simpson * source address for this datagram. Its use is invalidated if the 9481b7f0384SBruce M Simpson * address thus specified is incomplete or clobbers other inpcbs. 9491b7f0384SBruce M Simpson */ 95090162a4eSIan Dowse laddr = inp->inp_laddr; 95190162a4eSIan Dowse lport = inp->inp_lport; 9521b7f0384SBruce M Simpson if (src.sin_family == AF_INET) { 953603724d3SBjoern A. Zeeb INP_INFO_LOCK_ASSERT(&V_udbinfo); 9541b7f0384SBruce M Simpson if ((lport == 0) || 9551b7f0384SBruce M Simpson (laddr.s_addr == INADDR_ANY && 9561b7f0384SBruce M Simpson src.sin_addr.s_addr == INADDR_ANY)) { 957c557ae16SIan Dowse error = EINVAL; 958c557ae16SIan Dowse goto release; 959c557ae16SIan Dowse } 960c557ae16SIan Dowse error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 961b0330ed9SPawel Jakub Dawidek &laddr.s_addr, &lport, td->td_ucred); 962c557ae16SIan Dowse if (error) 963c557ae16SIan Dowse goto release; 964c557ae16SIan Dowse } 965c557ae16SIan Dowse 9663144b7d3SRobert Watson /* 9673144b7d3SRobert Watson * If a UDP socket has been connected, then a local address/port will 9683144b7d3SRobert Watson * have been selected and bound. 9693144b7d3SRobert Watson * 97043cc0bc1SRobert Watson * If a UDP socket has not been connected to, then an explicit 9713144b7d3SRobert Watson * destination address must be used, in which case a local 9723144b7d3SRobert Watson * address/port may not have been selected and bound. 9733144b7d3SRobert Watson */ 97443cc0bc1SRobert Watson if (sin != NULL) { 975c4d585aeSRobert Watson INP_LOCK_ASSERT(inp); 976df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 977df8bae1dSRodney W. Grimes error = EISCONN; 978df8bae1dSRodney W. Grimes goto release; 979df8bae1dSRodney W. Grimes } 9803144b7d3SRobert Watson 9813144b7d3SRobert Watson /* 9823144b7d3SRobert Watson * Jail may rewrite the destination address, so let it do 9833144b7d3SRobert Watson * that before we use it. 9843144b7d3SRobert Watson */ 985b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 986b89e82ddSJamie Gritton if (error) 987413628a7SBjoern A. Zeeb goto release; 9883144b7d3SRobert Watson 9893144b7d3SRobert Watson /* 99043cc0bc1SRobert Watson * If a local address or port hasn't yet been selected, or if 99143cc0bc1SRobert Watson * the destination address needs to be rewritten due to using 99243cc0bc1SRobert Watson * a special INADDR_ constant, invoke in_pcbconnect_setup() 99343cc0bc1SRobert Watson * to do the heavy lifting. Once a port is selected, we 99443cc0bc1SRobert Watson * commit the binding back to the socket; we also commit the 99543cc0bc1SRobert Watson * binding of the address if in jail. 99643cc0bc1SRobert Watson * 99743cc0bc1SRobert Watson * If we already have a valid binding and we're not 99843cc0bc1SRobert Watson * requesting a destination address rewrite, use a fast path. 9993144b7d3SRobert Watson */ 100043cc0bc1SRobert Watson if (inp->inp_laddr.s_addr == INADDR_ANY || 100143cc0bc1SRobert Watson inp->inp_lport == 0 || 100243cc0bc1SRobert Watson sin->sin_addr.s_addr == INADDR_ANY || 100343cc0bc1SRobert Watson sin->sin_addr.s_addr == INADDR_BROADCAST) { 1004603724d3SBjoern A. Zeeb INP_INFO_LOCK_ASSERT(&V_udbinfo); 100543cc0bc1SRobert Watson error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, 100643cc0bc1SRobert Watson &lport, &faddr.s_addr, &fport, NULL, 100743cc0bc1SRobert Watson td->td_ucred); 100890162a4eSIan Dowse if (error) 100990162a4eSIan Dowse goto release; 101090162a4eSIan Dowse 101143cc0bc1SRobert Watson /* 101243cc0bc1SRobert Watson * XXXRW: Why not commit the port if the address is 101343cc0bc1SRobert Watson * !INADDR_ANY? 101443cc0bc1SRobert Watson */ 101590162a4eSIan Dowse /* Commit the local port if newly assigned. */ 101690162a4eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && 101790162a4eSIan Dowse inp->inp_lport == 0) { 1018603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_udbinfo); 1019c4d585aeSRobert Watson INP_WLOCK_ASSERT(inp); 10203a1757b9SGleb Smirnoff /* 102143cc0bc1SRobert Watson * Remember addr if jailed, to prevent 102243cc0bc1SRobert Watson * rebinding. 10233a1757b9SGleb Smirnoff */ 10240304c731SJamie Gritton if (prison_flag(td->td_ucred, PR_IP4)) 10253a1757b9SGleb Smirnoff inp->inp_laddr = laddr; 102690162a4eSIan Dowse inp->inp_lport = lport; 102790162a4eSIan Dowse if (in_pcbinshash(inp) != 0) { 102890162a4eSIan Dowse inp->inp_lport = 0; 102990162a4eSIan Dowse error = EAGAIN; 1030df8bae1dSRodney W. Grimes goto release; 1031df8bae1dSRodney W. Grimes } 103290162a4eSIan Dowse inp->inp_flags |= INP_ANONPORT; 103390162a4eSIan Dowse } 1034df8bae1dSRodney W. Grimes } else { 103543cc0bc1SRobert Watson faddr = sin->sin_addr; 103643cc0bc1SRobert Watson fport = sin->sin_port; 103743cc0bc1SRobert Watson } 103843cc0bc1SRobert Watson } else { 1039c4d585aeSRobert Watson INP_LOCK_ASSERT(inp); 104090162a4eSIan Dowse faddr = inp->inp_faddr; 104190162a4eSIan Dowse fport = inp->inp_fport; 104290162a4eSIan Dowse if (faddr.s_addr == INADDR_ANY) { 1043df8bae1dSRodney W. Grimes error = ENOTCONN; 1044df8bae1dSRodney W. Grimes goto release; 1045df8bae1dSRodney W. Grimes } 1046df8bae1dSRodney W. Grimes } 1047e6ccd709SRobert Watson 1048df8bae1dSRodney W. Grimes /* 1049e6ccd709SRobert Watson * Calculate data length and get a mbuf for UDP, IP, and possible 1050392e8407SRobert Watson * link-layer headers. Immediate slide the data pointer back forward 1051392e8407SRobert Watson * since we won't use that space at this layer. 1052df8bae1dSRodney W. Grimes */ 1053e6ccd709SRobert Watson M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT); 1054e6ccd709SRobert Watson if (m == NULL) { 1055df8bae1dSRodney W. Grimes error = ENOBUFS; 105649b19bfcSBruce M Simpson goto release; 1057df8bae1dSRodney W. Grimes } 1058e6ccd709SRobert Watson m->m_data += max_linkhdr; 1059e6ccd709SRobert Watson m->m_len -= max_linkhdr; 1060392e8407SRobert Watson m->m_pkthdr.len -= max_linkhdr; 1061df8bae1dSRodney W. Grimes 1062df8bae1dSRodney W. Grimes /* 10633329b236SRobert Watson * Fill in mbuf with extended UDP header and addresses and length put 10643329b236SRobert Watson * into network format. 1065df8bae1dSRodney W. Grimes */ 1066df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 1067db4f9cc7SJonathan Lemon bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 1068df8bae1dSRodney W. Grimes ui->ui_pr = IPPROTO_UDP; 106990162a4eSIan Dowse ui->ui_src = laddr; 107090162a4eSIan Dowse ui->ui_dst = faddr; 107190162a4eSIan Dowse ui->ui_sport = lport; 107290162a4eSIan Dowse ui->ui_dport = fport; 1073db4f9cc7SJonathan Lemon ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 1074df8bae1dSRodney W. Grimes 1075b2828ad2SAndre Oppermann /* 1076b2828ad2SAndre Oppermann * Set the Don't Fragment bit in the IP header. 1077b2828ad2SAndre Oppermann */ 1078b2828ad2SAndre Oppermann if (inp->inp_flags & INP_DONTFRAG) { 1079b2828ad2SAndre Oppermann struct ip *ip; 10803329b236SRobert Watson 1081b2828ad2SAndre Oppermann ip = (struct ip *)&ui->ui_i; 1082b2828ad2SAndre Oppermann ip->ip_off |= IP_DF; 1083b2828ad2SAndre Oppermann } 1084b2828ad2SAndre Oppermann 1085b5d47ff5SJohn-Mark Gurney ipflags = 0; 1086b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_DONTROUTE) 1087b5d47ff5SJohn-Mark Gurney ipflags |= IP_ROUTETOIF; 1088b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_BROADCAST) 1089b5d47ff5SJohn-Mark Gurney ipflags |= IP_ALLOWBROADCAST; 10906fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 10918afa2304SBruce M Simpson ipflags |= IP_SENDONES; 10928afa2304SBruce M Simpson 10931175d9d5SRobert Watson #ifdef MAC 10941175d9d5SRobert Watson mac_inpcb_create_mbuf(inp, m); 10951175d9d5SRobert Watson #endif 10961175d9d5SRobert Watson 1097df8bae1dSRodney W. Grimes /* 1098db4f9cc7SJonathan Lemon * Set up checksum and output datagram. 1099df8bae1dSRodney W. Grimes */ 1100f5514f08SRobert Watson if (udp_cksum) { 11016fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 11028a538743SBruce M Simpson faddr.s_addr = INADDR_BROADCAST; 11038a538743SBruce M Simpson ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 1104db4f9cc7SJonathan Lemon htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 1105db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_UDP; 1106db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 11073329b236SRobert Watson } else 1108db4f9cc7SJonathan Lemon ui->ui_sum = 0; 1109df8bae1dSRodney W. Grimes ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 1110ca98b82cSDavid Greenman ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 1111ca98b82cSDavid Greenman ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 1112026decb8SRobert Watson UDPSTAT_INC(udps_opackets); 1113cfa1ca9dSYoshinobu Inoue 111443cc0bc1SRobert Watson if (unlock_udbinfo == 2) 1115603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 111643cc0bc1SRobert Watson else if (unlock_udbinfo == 1) 1117603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 111897d8d152SAndre Oppermann error = ip_output(m, inp->inp_options, NULL, ipflags, 11195d846453SSam Leffler inp->inp_moptions, inp); 112043cc0bc1SRobert Watson if (unlock_udbinfo == 2) 11218501a69cSRobert Watson INP_WUNLOCK(inp); 1122948d0fc9SRobert Watson else 1123948d0fc9SRobert Watson INP_RUNLOCK(inp); 1124df8bae1dSRodney W. Grimes return (error); 1125df8bae1dSRodney W. Grimes 1126df8bae1dSRodney W. Grimes release: 112743cc0bc1SRobert Watson if (unlock_udbinfo == 2) { 1128948d0fc9SRobert Watson INP_WUNLOCK(inp); 1129603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 113043cc0bc1SRobert Watson } else if (unlock_udbinfo == 1) { 113143cc0bc1SRobert Watson INP_RUNLOCK(inp); 1132603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 1133948d0fc9SRobert Watson } else 1134948d0fc9SRobert Watson INP_RUNLOCK(inp); 1135df8bae1dSRodney W. Grimes m_freem(m); 1136df8bae1dSRodney W. Grimes return (error); 1137df8bae1dSRodney W. Grimes } 1138df8bae1dSRodney W. Grimes 1139ac45e92fSRobert Watson static void 1140d0390e05SGarrett Wollman udp_abort(struct socket *so) 1141df8bae1dSRodney W. Grimes { 11428b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1143d0390e05SGarrett Wollman struct inpcb *inp; 1144df8bae1dSRodney W. Grimes 1145d0390e05SGarrett Wollman inp = sotoinpcb(so); 114614ba8addSRobert Watson KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 1147603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 11488501a69cSRobert Watson INP_WLOCK(inp); 1149a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1150a152f8a3SRobert Watson in_pcbdisconnect(inp); 1151a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1152d0390e05SGarrett Wollman soisdisconnected(so); 1153a152f8a3SRobert Watson } 11548501a69cSRobert Watson INP_WUNLOCK(inp); 1155603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 1156df8bae1dSRodney W. Grimes } 1157df8bae1dSRodney W. Grimes 1158d0390e05SGarrett Wollman static int 1159b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td) 1160d0390e05SGarrett Wollman { 11618b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1162d0390e05SGarrett Wollman struct inpcb *inp; 1163277afaffSRobert Watson int error; 1164d0390e05SGarrett Wollman 1165d0390e05SGarrett Wollman inp = sotoinpcb(so); 116614ba8addSRobert Watson KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 1167cfa1ca9dSYoshinobu Inoue error = soreserve(so, udp_sendspace, udp_recvspace); 1168f24618aaSRobert Watson if (error) 11693329b236SRobert Watson return (error); 1170603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 1171603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_udbinfo); 117253b57cd1SSam Leffler if (error) { 1173603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 11743329b236SRobert Watson return (error); 117553b57cd1SSam Leffler } 1176cfa1ca9dSYoshinobu Inoue 1177cfa1ca9dSYoshinobu Inoue inp = (struct inpcb *)so->so_pcb; 1178cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1179603724d3SBjoern A. Zeeb inp->inp_ip_ttl = V_ip_defttl; 11806a9148feSBjoern A. Zeeb 11816a9148feSBjoern A. Zeeb error = udp_newudpcb(inp); 11826a9148feSBjoern A. Zeeb if (error) { 11836a9148feSBjoern A. Zeeb in_pcbdetach(inp); 11846a9148feSBjoern A. Zeeb in_pcbfree(inp); 11856a9148feSBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 11866a9148feSBjoern A. Zeeb return (error); 11876a9148feSBjoern A. Zeeb } 11886a9148feSBjoern A. Zeeb 1189c7c7ea4bSRandall Stewart INP_WUNLOCK(inp); 11906a9148feSBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 1191c7c7ea4bSRandall Stewart return (0); 1192c7c7ea4bSRandall Stewart } 1193c7c7ea4bSRandall Stewart 1194c7c7ea4bSRandall Stewart int 1195c7c7ea4bSRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f) 1196c7c7ea4bSRandall Stewart { 1197c7c7ea4bSRandall Stewart struct inpcb *inp; 11986a9148feSBjoern A. Zeeb struct udpcb *up; 1199c7c7ea4bSRandall Stewart 1200bbb0e3d9SRandall Stewart KASSERT(so->so_type == SOCK_DGRAM, ("udp_set_kernel_tunneling: !dgram")); 1201bbb0e3d9SRandall Stewart KASSERT(so->so_pcb != NULL, ("udp_set_kernel_tunneling: NULL inp")); 1202c7c7ea4bSRandall Stewart if (so->so_type != SOCK_DGRAM) { 1203c7c7ea4bSRandall Stewart /* Not UDP socket... sorry! */ 1204c7c7ea4bSRandall Stewart return (ENOTSUP); 1205c7c7ea4bSRandall Stewart } 12066a9148feSBjoern A. Zeeb inp = (struct inpcb *)so->so_pcb; 1207c7c7ea4bSRandall Stewart if (inp == NULL) { 1208c7c7ea4bSRandall Stewart /* NULL INP? */ 1209c7c7ea4bSRandall Stewart return (EINVAL); 1210c7c7ea4bSRandall Stewart } 1211c7c7ea4bSRandall Stewart INP_WLOCK(inp); 12126a9148feSBjoern A. Zeeb up = intoudpcb(inp); 12136a9148feSBjoern A. Zeeb if (up->u_tun_func != NULL) { 1214bbb0e3d9SRandall Stewart INP_WUNLOCK(inp); 1215bbb0e3d9SRandall Stewart return (EBUSY); 1216bbb0e3d9SRandall Stewart } 12176a9148feSBjoern A. Zeeb up->u_tun_func = f; 12188501a69cSRobert Watson INP_WUNLOCK(inp); 12193329b236SRobert Watson return (0); 1220df8bae1dSRodney W. Grimes } 1221d0390e05SGarrett Wollman 1222d0390e05SGarrett Wollman static int 1223b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1224d0390e05SGarrett Wollman { 12258b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1226d0390e05SGarrett Wollman struct inpcb *inp; 1227277afaffSRobert Watson int error; 1228d0390e05SGarrett Wollman 1229d0390e05SGarrett Wollman inp = sotoinpcb(so); 123014ba8addSRobert Watson KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 1231603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 12328501a69cSRobert Watson INP_WLOCK(inp); 1233b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, nam, td->td_ucred); 12348501a69cSRobert Watson INP_WUNLOCK(inp); 1235603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 12363329b236SRobert Watson return (error); 1237d0390e05SGarrett Wollman } 1238d0390e05SGarrett Wollman 1239a152f8a3SRobert Watson static void 1240a152f8a3SRobert Watson udp_close(struct socket *so) 1241a152f8a3SRobert Watson { 12428b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1243a152f8a3SRobert Watson struct inpcb *inp; 1244a152f8a3SRobert Watson 1245a152f8a3SRobert Watson inp = sotoinpcb(so); 1246a152f8a3SRobert Watson KASSERT(inp != NULL, ("udp_close: inp == NULL")); 1247603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 12488501a69cSRobert Watson INP_WLOCK(inp); 1249a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1250a152f8a3SRobert Watson in_pcbdisconnect(inp); 1251a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1252a152f8a3SRobert Watson soisdisconnected(so); 1253a152f8a3SRobert Watson } 12548501a69cSRobert Watson INP_WUNLOCK(inp); 1255603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 1256a152f8a3SRobert Watson } 1257a152f8a3SRobert Watson 1258d0390e05SGarrett Wollman static int 1259b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1260d0390e05SGarrett Wollman { 12618b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1262d0390e05SGarrett Wollman struct inpcb *inp; 1263277afaffSRobert Watson int error; 126475c13541SPoul-Henning Kamp struct sockaddr_in *sin; 1265d0390e05SGarrett Wollman 1266d0390e05SGarrett Wollman inp = sotoinpcb(so); 126714ba8addSRobert Watson KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 1268603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 12698501a69cSRobert Watson INP_WLOCK(inp); 1270f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr != INADDR_ANY) { 12718501a69cSRobert Watson INP_WUNLOCK(inp); 1272603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 12733329b236SRobert Watson return (EISCONN); 1274f76fcf6dSJeffrey Hsu } 127575c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)nam; 1276b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1277b89e82ddSJamie Gritton if (error != 0) { 1278413628a7SBjoern A. Zeeb INP_WUNLOCK(inp); 1279385195c0SMarko Zec INP_INFO_WUNLOCK(&V_udbinfo); 1280b89e82ddSJamie Gritton return (error); 1281413628a7SBjoern A. Zeeb } 1282b0330ed9SPawel Jakub Dawidek error = in_pcbconnect(inp, nam, td->td_ucred); 12834cc20ab1SSeigo Tanimura if (error == 0) 1284df8bae1dSRodney W. Grimes soisconnected(so); 12858501a69cSRobert Watson INP_WUNLOCK(inp); 1286603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 12873329b236SRobert Watson return (error); 1288df8bae1dSRodney W. Grimes } 1289d0390e05SGarrett Wollman 1290bc725eafSRobert Watson static void 1291d0390e05SGarrett Wollman udp_detach(struct socket *so) 1292d0390e05SGarrett Wollman { 12938b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1294d0390e05SGarrett Wollman struct inpcb *inp; 12956a9148feSBjoern A. Zeeb struct udpcb *up; 1296d0390e05SGarrett Wollman 1297d0390e05SGarrett Wollman inp = sotoinpcb(so); 129814ba8addSRobert Watson KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1299a152f8a3SRobert Watson KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1300a152f8a3SRobert Watson ("udp_detach: not disconnected")); 1301603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 13028501a69cSRobert Watson INP_WLOCK(inp); 13036a9148feSBjoern A. Zeeb up = intoudpcb(inp); 13046a9148feSBjoern A. Zeeb KASSERT(up != NULL, ("%s: up == NULL", __func__)); 13056a9148feSBjoern A. Zeeb inp->inp_ppcb = NULL; 1306d0390e05SGarrett Wollman in_pcbdetach(inp); 130714ba8addSRobert Watson in_pcbfree(inp); 1308603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 13096a9148feSBjoern A. Zeeb udp_discardcb(up); 1310d0390e05SGarrett Wollman } 1311d0390e05SGarrett Wollman 1312d0390e05SGarrett Wollman static int 1313d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 1314d0390e05SGarrett Wollman { 13158b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1316d0390e05SGarrett Wollman struct inpcb *inp; 1317d0390e05SGarrett Wollman 1318d0390e05SGarrett Wollman inp = sotoinpcb(so); 131914ba8addSRobert Watson KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 1320603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 13218501a69cSRobert Watson INP_WLOCK(inp); 1322f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr == INADDR_ANY) { 13238501a69cSRobert Watson INP_WUNLOCK(inp); 1324603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 13253329b236SRobert Watson return (ENOTCONN); 1326f76fcf6dSJeffrey Hsu } 1327d0390e05SGarrett Wollman 1328df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 1329df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 1330d45e4f99SMaxim Konovalov SOCK_LOCK(so); 1331d45e4f99SMaxim Konovalov so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1332d45e4f99SMaxim Konovalov SOCK_UNLOCK(so); 13338501a69cSRobert Watson INP_WUNLOCK(inp); 1334603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 13353329b236SRobert Watson return (0); 1336df8bae1dSRodney W. Grimes } 1337df8bae1dSRodney W. Grimes 1338d0390e05SGarrett Wollman static int 133957bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1340b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1341d0390e05SGarrett Wollman { 1342d0390e05SGarrett Wollman struct inpcb *inp; 1343d0390e05SGarrett Wollman 1344d0390e05SGarrett Wollman inp = sotoinpcb(so); 134514ba8addSRobert Watson KASSERT(inp != NULL, ("udp_send: inp == NULL")); 13463329b236SRobert Watson return (udp_output(inp, m, addr, control, td)); 1347d0390e05SGarrett Wollman } 1348d0390e05SGarrett Wollman 134976429de4SYoshinobu Inoue int 1350d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 1351d0390e05SGarrett Wollman { 1352d0390e05SGarrett Wollman struct inpcb *inp; 1353d0390e05SGarrett Wollman 1354d0390e05SGarrett Wollman inp = sotoinpcb(so); 135514ba8addSRobert Watson KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 13568501a69cSRobert Watson INP_WLOCK(inp); 1357d0390e05SGarrett Wollman socantsendmore(so); 13588501a69cSRobert Watson INP_WUNLOCK(inp); 13593329b236SRobert Watson return (0); 1360d0390e05SGarrett Wollman } 1361d0390e05SGarrett Wollman 1362d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 1363756d52a1SPoul-Henning Kamp .pru_abort = udp_abort, 1364756d52a1SPoul-Henning Kamp .pru_attach = udp_attach, 1365756d52a1SPoul-Henning Kamp .pru_bind = udp_bind, 1366756d52a1SPoul-Henning Kamp .pru_connect = udp_connect, 1367756d52a1SPoul-Henning Kamp .pru_control = in_control, 1368756d52a1SPoul-Henning Kamp .pru_detach = udp_detach, 1369756d52a1SPoul-Henning Kamp .pru_disconnect = udp_disconnect, 137054d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1371756d52a1SPoul-Henning Kamp .pru_send = udp_send, 13725df3e839SRobert Watson .pru_soreceive = soreceive_dgram, 137359b8854eSRobert Watson .pru_sosend = sosend_dgram, 1374756d52a1SPoul-Henning Kamp .pru_shutdown = udp_shutdown, 137554d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1376a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1377a152f8a3SRobert Watson .pru_close = udp_close, 1378d0390e05SGarrett Wollman }; 1379