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" 40bdb3fa18SRobert Watson #include "opt_mac.h" 41cfa1ca9dSYoshinobu Inoue 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43960ed29cSSeigo Tanimura #include <sys/domain.h> 444f590175SPaul Saab #include <sys/eventhandler.h> 45960ed29cSSeigo Tanimura #include <sys/jail.h> 46b110a8a2SGarrett Wollman #include <sys/kernel.h> 47960ed29cSSeigo Tanimura #include <sys/lock.h> 48df8bae1dSRodney W. Grimes #include <sys/malloc.h> 49df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 50acd3428bSRobert Watson #include <sys/priv.h> 51490d50b6SBrian Feldman #include <sys/proc.h> 52df8bae1dSRodney W. Grimes #include <sys/protosw.h> 53960ed29cSSeigo Tanimura #include <sys/signalvar.h> 54df8bae1dSRodney W. Grimes #include <sys/socket.h> 55df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 56960ed29cSSeigo Tanimura #include <sys/sx.h> 57b5e8ce9fSBruce Evans #include <sys/sysctl.h> 58816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 59f5514f08SRobert Watson #include <sys/systm.h> 60603724d3SBjoern A. Zeeb #include <sys/vimage.h> 618781d8e9SBruce Evans 6269c2d429SJeff Roberson #include <vm/uma.h> 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes #include <net/if.h> 65df8bae1dSRodney W. Grimes #include <net/route.h> 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes #include <netinet/in.h> 68960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 69f5514f08SRobert Watson #include <netinet/in_systm.h> 70960ed29cSSeigo Tanimura #include <netinet/in_var.h> 71df8bae1dSRodney W. Grimes #include <netinet/ip.h> 72cfa1ca9dSYoshinobu Inoue #ifdef INET6 73cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 74cfa1ca9dSYoshinobu Inoue #endif 75960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h> 76960ed29cSSeigo Tanimura #include <netinet/icmp_var.h> 77df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 78ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 79cfa1ca9dSYoshinobu Inoue #ifdef INET6 80cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 81cfa1ca9dSYoshinobu Inoue #endif 82df8bae1dSRodney W. Grimes #include <netinet/udp.h> 83df8bae1dSRodney W. Grimes #include <netinet/udp_var.h> 844b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 85df8bae1dSRodney W. Grimes 86b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 87b9234fafSSam Leffler #include <netipsec/ipsec.h> 883329b236SRobert Watson #endif 89b9234fafSSam Leffler 90db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 91db4f9cc7SJonathan Lemon 92aed55708SRobert Watson #include <security/mac/mac_framework.h> 93aed55708SRobert Watson 94df8bae1dSRodney W. Grimes /* 95df8bae1dSRodney W. Grimes * UDP protocol implementation. 96df8bae1dSRodney W. Grimes * Per RFC 768, August, 1980. 97df8bae1dSRodney W. Grimes */ 9874eb3236SWarner Losh 9944e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 10044e33a07SMarko Zec int udp_blackhole; 10144e33a07SMarko Zec #endif 10244e33a07SMarko Zec 10374eb3236SWarner Losh /* 1043329b236SRobert Watson * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums 1053329b236SRobert Watson * removes the only data integrity mechanism for packets and malformed 106f5514f08SRobert Watson * packets that would otherwise be discarded due to bad checksums, and may 107f5514f08SRobert Watson * cause problems (especially for NFS data blocks). 10874eb3236SWarner Losh */ 109f5514f08SRobert Watson static int udp_cksum = 1; 110f5514f08SRobert Watson SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum, 11141698ebfSTom Rhodes 0, "compute udp checksum"); 112df8bae1dSRodney W. Grimes 113afdb4274SRobert Watson int udp_log_in_vain = 0; 114816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 115afdb4274SRobert Watson &udp_log_in_vain, 0, "Log all incoming UDP packets"); 116816a3d83SPoul-Henning Kamp 11797021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_udp, OID_AUTO, blackhole, 11897021c24SMarko Zec CTLFLAG_RW, udp_blackhole, 0, 1193329b236SRobert Watson "Do not send port unreachables for refused connects"); 12016f7f31fSGeoff Rehmet 12143bbb6aaSRobert Watson u_long udp_sendspace = 9216; /* really max datagram size */ 12243bbb6aaSRobert Watson /* 40 1K datagrams */ 12343bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 12443bbb6aaSRobert Watson &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 12543bbb6aaSRobert Watson 12643bbb6aaSRobert Watson u_long udp_recvspace = 40 * (1024 + 12743bbb6aaSRobert Watson #ifdef INET6 12843bbb6aaSRobert Watson sizeof(struct sockaddr_in6) 12943bbb6aaSRobert Watson #else 13043bbb6aaSRobert Watson sizeof(struct sockaddr_in) 13143bbb6aaSRobert Watson #endif 13243bbb6aaSRobert Watson ); 13343bbb6aaSRobert Watson 13443bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 13543bbb6aaSRobert Watson &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 13643bbb6aaSRobert Watson 13744e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 13876429de4SYoshinobu Inoue struct inpcbhead udb; /* from udp_var.h */ 1397a2aab80SBrian Feldman struct inpcbinfo udbinfo; 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 { 1584f590175SPaul Saab 159603724d3SBjoern A. Zeeb uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 1604f590175SPaul Saab } 1614f590175SPaul Saab 162d915b280SStephan Uphoff static int 163d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags) 164d915b280SStephan Uphoff { 165af1ee11dSRobert Watson struct inpcb *inp; 16608651e1fSJohn Baldwin 167af1ee11dSRobert Watson inp = mem; 168d915b280SStephan Uphoff INP_LOCK_INIT(inp, "inp", "udpinp"); 169d915b280SStephan Uphoff return (0); 170d915b280SStephan Uphoff } 171d915b280SStephan Uphoff 172df8bae1dSRodney W. Grimes void 173af1ee11dSRobert Watson udp_init(void) 174df8bae1dSRodney W. Grimes { 1758b615593SMarko Zec INIT_VNET_INET(curvnet); 176af1ee11dSRobert Watson 17744e33a07SMarko Zec V_udp_blackhole = 0; 17844e33a07SMarko Zec 179603724d3SBjoern A. Zeeb INP_INFO_LOCK_INIT(&V_udbinfo, "udp"); 180603724d3SBjoern A. Zeeb LIST_INIT(&V_udb); 1812c0d658fSJulian Elischer V_udbinfo.ipi_listhead = &V_udb; 182603724d3SBjoern A. Zeeb V_udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB, 183603724d3SBjoern A. Zeeb &V_udbinfo.ipi_hashmask); 184603724d3SBjoern A. Zeeb V_udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB, 185603724d3SBjoern A. Zeeb &V_udbinfo.ipi_porthashmask); 186603724d3SBjoern A. Zeeb V_udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL, 187d915b280SStephan Uphoff NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 188603724d3SBjoern A. Zeeb uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 1894f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL, 1904f590175SPaul Saab EVENTHANDLER_PRI_ANY); 191df8bae1dSRodney W. Grimes } 192df8bae1dSRodney W. Grimes 19343bbb6aaSRobert Watson /* 19443bbb6aaSRobert Watson * Subroutine of udp_input(), which appends the provided mbuf chain to the 19543bbb6aaSRobert Watson * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 19643bbb6aaSRobert Watson * contains the source address. If the socket ends up being an IPv6 socket, 19743bbb6aaSRobert Watson * udp_append() will convert to a sockaddr_in6 before passing the address 19843bbb6aaSRobert Watson * into the socket code. 19943bbb6aaSRobert Watson */ 20043bbb6aaSRobert Watson static void 20143bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off, 20243bbb6aaSRobert Watson struct sockaddr_in *udp_in) 20343bbb6aaSRobert Watson { 20443bbb6aaSRobert Watson struct sockaddr *append_sa; 20543bbb6aaSRobert Watson struct socket *so; 20643bbb6aaSRobert Watson struct mbuf *opts = 0; 20743bbb6aaSRobert Watson #ifdef INET6 20843bbb6aaSRobert Watson struct sockaddr_in6 udp_in6; 20943bbb6aaSRobert Watson #endif 21043bbb6aaSRobert Watson 211119d85f6SRobert Watson INP_RLOCK_ASSERT(inp); 21243bbb6aaSRobert Watson 21343bbb6aaSRobert Watson #ifdef IPSEC 21443bbb6aaSRobert Watson /* Check AH/ESP integrity. */ 21543bbb6aaSRobert Watson if (ipsec4_in_reject(n, inp)) { 2168b615593SMarko Zec INIT_VNET_IPSEC(curvnet); 21743bbb6aaSRobert Watson m_freem(n); 218603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 21943bbb6aaSRobert Watson return; 22043bbb6aaSRobert Watson } 22143bbb6aaSRobert Watson #endif /* IPSEC */ 22243bbb6aaSRobert Watson #ifdef MAC 22330d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, n) != 0) { 22443bbb6aaSRobert Watson m_freem(n); 22543bbb6aaSRobert Watson return; 22643bbb6aaSRobert Watson } 22743bbb6aaSRobert Watson #endif 22843bbb6aaSRobert Watson if (inp->inp_flags & INP_CONTROLOPTS || 22943bbb6aaSRobert Watson inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 23043bbb6aaSRobert Watson #ifdef INET6 2319a38ba81SBjoern A. Zeeb if (inp->inp_vflag & INP_IPV6) 23248d48eb9SBjoern A. Zeeb (void)ip6_savecontrol_v4(inp, n, &opts, NULL); 2339a38ba81SBjoern A. Zeeb else 23443bbb6aaSRobert Watson #endif 23543bbb6aaSRobert Watson ip_savecontrol(inp, &opts, ip, n); 23643bbb6aaSRobert Watson } 23743bbb6aaSRobert Watson #ifdef INET6 23843bbb6aaSRobert Watson if (inp->inp_vflag & INP_IPV6) { 23943bbb6aaSRobert Watson bzero(&udp_in6, sizeof(udp_in6)); 24043bbb6aaSRobert Watson udp_in6.sin6_len = sizeof(udp_in6); 24143bbb6aaSRobert Watson udp_in6.sin6_family = AF_INET6; 24243bbb6aaSRobert Watson in6_sin_2_v4mapsin6(udp_in, &udp_in6); 24343bbb6aaSRobert Watson append_sa = (struct sockaddr *)&udp_in6; 24443bbb6aaSRobert Watson } else 24543bbb6aaSRobert Watson #endif 24643bbb6aaSRobert Watson append_sa = (struct sockaddr *)udp_in; 24743bbb6aaSRobert Watson m_adj(n, off); 24843bbb6aaSRobert Watson 24943bbb6aaSRobert Watson so = inp->inp_socket; 25043bbb6aaSRobert Watson SOCKBUF_LOCK(&so->so_rcv); 25143bbb6aaSRobert Watson if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 2528b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 25343bbb6aaSRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 25443bbb6aaSRobert Watson m_freem(n); 25543bbb6aaSRobert Watson if (opts) 25643bbb6aaSRobert Watson m_freem(opts); 257603724d3SBjoern A. Zeeb V_udpstat.udps_fullsock++; 25843bbb6aaSRobert Watson } else 25943bbb6aaSRobert Watson sorwakeup_locked(so); 26043bbb6aaSRobert Watson } 26143bbb6aaSRobert Watson 262df8bae1dSRodney W. Grimes void 2633329b236SRobert Watson udp_input(struct mbuf *m, int off) 264df8bae1dSRodney W. Grimes { 2658b615593SMarko Zec INIT_VNET_INET(curvnet); 266cfa1ca9dSYoshinobu Inoue int iphlen = off; 2673329b236SRobert Watson struct ip *ip; 2683329b236SRobert Watson struct udphdr *uh; 26971498f30SBruce M Simpson struct ifnet *ifp; 2703329b236SRobert Watson struct inpcb *inp; 271df8bae1dSRodney W. Grimes int len; 272df8bae1dSRodney W. Grimes struct ip save_ip; 273d4b509bdSRobert Watson struct sockaddr_in udp_in; 2740b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD 2750b4ae859SGleb Smirnoff struct m_tag *fwd_tag; 2760b4ae859SGleb Smirnoff #endif 277df8bae1dSRodney W. Grimes 27871498f30SBruce M Simpson ifp = m->m_pkthdr.rcvif; 279603724d3SBjoern A. Zeeb V_udpstat.udps_ipackets++; 280df8bae1dSRodney W. Grimes 281df8bae1dSRodney W. Grimes /* 2823329b236SRobert Watson * Strip IP options, if any; should skip this, make available to 2833329b236SRobert Watson * user, and use on returned packets, but we don't yet have a way to 2843329b236SRobert Watson * check the checksum with options still present. 285df8bae1dSRodney W. Grimes */ 286df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 287df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 288df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 289df8bae1dSRodney W. Grimes } 290df8bae1dSRodney W. Grimes 291df8bae1dSRodney W. Grimes /* 292df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 293df8bae1dSRodney W. Grimes */ 294df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 295df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 296df8bae1dSRodney W. Grimes if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 297603724d3SBjoern A. Zeeb V_udpstat.udps_hdrops++; 298df8bae1dSRodney W. Grimes return; 299df8bae1dSRodney W. Grimes } 300df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 301df8bae1dSRodney W. Grimes } 302df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 303df8bae1dSRodney W. Grimes 3043329b236SRobert Watson /* 3053329b236SRobert Watson * Destination port of 0 is illegal, based on RFC768. 3063329b236SRobert Watson */ 307686cdd19SJun-ichiro itojun Hagino if (uh->uh_dport == 0) 308f76fcf6dSJeffrey Hsu goto badunlocked; 309686cdd19SJun-ichiro itojun Hagino 310df8bae1dSRodney W. Grimes /* 3113329b236SRobert Watson * Construct sockaddr format source address. Stuff source address 3123329b236SRobert Watson * and datagram in user buffer. 313b9234fafSSam Leffler */ 314d4b509bdSRobert Watson bzero(&udp_in, sizeof(udp_in)); 315d4b509bdSRobert Watson udp_in.sin_len = sizeof(udp_in); 316d4b509bdSRobert Watson udp_in.sin_family = AF_INET; 317b9234fafSSam Leffler udp_in.sin_port = uh->uh_sport; 318b9234fafSSam Leffler udp_in.sin_addr = ip->ip_src; 319b9234fafSSam Leffler 320b9234fafSSam Leffler /* 321af1ee11dSRobert Watson * Make mbuf data length reflect UDP length. If not enough data to 322af1ee11dSRobert Watson * reflect UDP length, drop. 323df8bae1dSRodney W. Grimes */ 324df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 325df8bae1dSRodney W. Grimes if (ip->ip_len != len) { 3267eb7a449SAndras Olah if (len > ip->ip_len || len < sizeof(struct udphdr)) { 327603724d3SBjoern A. Zeeb V_udpstat.udps_badlen++; 328f76fcf6dSJeffrey Hsu goto badunlocked; 329df8bae1dSRodney W. Grimes } 330df8bae1dSRodney W. Grimes m_adj(m, len - ip->ip_len); 331df8bae1dSRodney W. Grimes /* ip->ip_len = len; */ 332df8bae1dSRodney W. Grimes } 3333329b236SRobert Watson 334df8bae1dSRodney W. Grimes /* 3353329b236SRobert Watson * Save a copy of the IP header in case we want restore it for 3363329b236SRobert Watson * sending an ICMP error message in response. 337df8bae1dSRodney W. Grimes */ 338603724d3SBjoern A. Zeeb if (!V_udp_blackhole) 339df8bae1dSRodney W. Grimes save_ip = *ip; 340cce418d3SMatt Jacob else 341cce418d3SMatt Jacob memset(&save_ip, 0, sizeof(save_ip)); 342df8bae1dSRodney W. Grimes 343df8bae1dSRodney W. Grimes /* 344df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 345df8bae1dSRodney W. Grimes */ 3466dfab5b1SGarrett Wollman if (uh->uh_sum) { 34739629c92SDavid Malone u_short uh_sum; 34839629c92SDavid Malone 349db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 350db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 35139629c92SDavid Malone uh_sum = m->m_pkthdr.csum_data; 352db4f9cc7SJonathan Lemon else 35339629c92SDavid Malone uh_sum = in_pseudo(ip->ip_src.s_addr, 354506f4949SRuslan Ermilov ip->ip_dst.s_addr, htonl((u_short)len + 355db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data + IPPROTO_UDP)); 35639629c92SDavid Malone uh_sum ^= 0xffff; 357db4f9cc7SJonathan Lemon } else { 358cb342100SHajimu UMEMOTO char b[9]; 359af1ee11dSRobert Watson 360cb342100SHajimu UMEMOTO bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 3616effc713SDoug Rabson bzero(((struct ipovly *)ip)->ih_x1, 9); 362df8bae1dSRodney W. Grimes ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 36339629c92SDavid Malone uh_sum = in_cksum(m, len + sizeof (struct ip)); 364cb342100SHajimu UMEMOTO bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 365db4f9cc7SJonathan Lemon } 36639629c92SDavid Malone if (uh_sum) { 367603724d3SBjoern A. Zeeb V_udpstat.udps_badsum++; 368df8bae1dSRodney W. Grimes m_freem(m); 369df8bae1dSRodney W. Grimes return; 370df8bae1dSRodney W. Grimes } 371fb9aaba0SRuslan Ermilov } else 372603724d3SBjoern A. Zeeb V_udpstat.udps_nosum++; 373df8bae1dSRodney W. Grimes 3740b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD 3753329b236SRobert Watson /* 3763329b236SRobert Watson * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 3773329b236SRobert Watson */ 3780b4ae859SGleb Smirnoff fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 3790b4ae859SGleb Smirnoff if (fwd_tag != NULL) { 3800b4ae859SGleb Smirnoff struct sockaddr_in *next_hop; 3810b4ae859SGleb Smirnoff 3823329b236SRobert Watson /* 3833329b236SRobert Watson * Do the hack. 3843329b236SRobert Watson */ 3850b4ae859SGleb Smirnoff next_hop = (struct sockaddr_in *)(fwd_tag + 1); 3860b4ae859SGleb Smirnoff ip->ip_dst = next_hop->sin_addr; 3870b4ae859SGleb Smirnoff uh->uh_dport = ntohs(next_hop->sin_port); 3883329b236SRobert Watson 3893329b236SRobert Watson /* 3903329b236SRobert Watson * Remove the tag from the packet. We don't need it anymore. 3913329b236SRobert Watson */ 3920b4ae859SGleb Smirnoff m_tag_delete(m, fwd_tag); 3930b4ae859SGleb Smirnoff } 3940b4ae859SGleb Smirnoff #endif 3950b4ae859SGleb Smirnoff 396603724d3SBjoern A. Zeeb INP_INFO_RLOCK(&V_udbinfo); 397df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 39871498f30SBruce M Simpson in_broadcast(ip->ip_dst, ifp)) { 39982c23ebaSBill Fenner struct inpcb *last; 40071498f30SBruce M Simpson struct ip_moptions *imo; 4013329b236SRobert Watson 402df8bae1dSRodney W. Grimes last = NULL; 403603724d3SBjoern A. Zeeb LIST_FOREACH(inp, &V_udb, inp_list) { 4049c1df695SRobert Watson if (inp->inp_lport != uh->uh_dport) 405f76fcf6dSJeffrey Hsu continue; 406cfa1ca9dSYoshinobu Inoue #ifdef INET6 407369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 4089c1df695SRobert Watson continue; 409cfa1ca9dSYoshinobu Inoue #endif 41071498f30SBruce M Simpson if (inp->inp_laddr.s_addr != INADDR_ANY && 41171498f30SBruce M Simpson inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 4129c1df695SRobert Watson continue; 41371498f30SBruce M Simpson if (inp->inp_faddr.s_addr != INADDR_ANY && 41471498f30SBruce M Simpson inp->inp_faddr.s_addr != ip->ip_src.s_addr) 41571498f30SBruce M Simpson continue; 41671498f30SBruce M Simpson /* 41771498f30SBruce M Simpson * XXX: Do not check source port of incoming datagram 41871498f30SBruce M Simpson * unless inp_connect() has been called to bind the 41971498f30SBruce M Simpson * fport part of the 4-tuple; the source could be 42071498f30SBruce M Simpson * trying to talk to us with an ephemeral port. 42171498f30SBruce M Simpson */ 42271498f30SBruce M Simpson if (inp->inp_fport != 0 && 423df8bae1dSRodney W. Grimes inp->inp_fport != uh->uh_sport) 4249c1df695SRobert Watson continue; 42571498f30SBruce M Simpson 426119d85f6SRobert Watson INP_RLOCK(inp); 427df8bae1dSRodney W. Grimes 42883453a06SBruce M Simpson /* 42971498f30SBruce M Simpson * Handle socket delivery policy for any-source 43071498f30SBruce M Simpson * and source-specific multicast. [RFC3678] 43183453a06SBruce M Simpson */ 43271498f30SBruce M Simpson imo = inp->inp_moptions; 43371498f30SBruce M Simpson if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 43471498f30SBruce M Simpson imo != NULL) { 43571498f30SBruce M Simpson struct sockaddr_in sin; 43671498f30SBruce M Simpson struct in_msource *ims; 43771498f30SBruce M Simpson int blocked, mode; 43871498f30SBruce M Simpson size_t idx; 43983453a06SBruce M Simpson 44071498f30SBruce M Simpson bzero(&sin, sizeof(struct sockaddr_in)); 44171498f30SBruce M Simpson sin.sin_len = sizeof(struct sockaddr_in); 44271498f30SBruce M Simpson sin.sin_family = AF_INET; 44371498f30SBruce M Simpson sin.sin_addr = ip->ip_dst; 44471498f30SBruce M Simpson 44571498f30SBruce M Simpson blocked = 0; 44671498f30SBruce M Simpson idx = imo_match_group(imo, ifp, 44771498f30SBruce M Simpson (struct sockaddr *)&sin); 44871498f30SBruce M Simpson if (idx == -1) { 44971498f30SBruce M Simpson /* 45071498f30SBruce M Simpson * No group membership for this socket. 45171498f30SBruce M Simpson * Do not bump udps_noportbcast, as 45271498f30SBruce M Simpson * this will happen further down. 45371498f30SBruce M Simpson */ 45471498f30SBruce M Simpson blocked++; 45571498f30SBruce M Simpson } else { 45671498f30SBruce M Simpson /* 45771498f30SBruce M Simpson * Check for a multicast source filter 45871498f30SBruce M Simpson * entry on this socket for this group. 45971498f30SBruce M Simpson * MCAST_EXCLUDE is the default 46071498f30SBruce M Simpson * behaviour. It means default accept; 46171498f30SBruce M Simpson * entries, if present, denote sources 46271498f30SBruce M Simpson * to be excluded from delivery. 46371498f30SBruce M Simpson */ 46471498f30SBruce M Simpson ims = imo_match_source(imo, idx, 46571498f30SBruce M Simpson (struct sockaddr *)&udp_in); 46671498f30SBruce M Simpson mode = imo->imo_mfilters[idx].imf_fmode; 46771498f30SBruce M Simpson if ((ims != NULL && 46871498f30SBruce M Simpson mode == MCAST_EXCLUDE) || 46971498f30SBruce M Simpson (ims == NULL && 47071498f30SBruce M Simpson mode == MCAST_INCLUDE)) { 47171498f30SBruce M Simpson #ifdef DIAGNOSTIC 47271498f30SBruce M Simpson if (bootverbose) { 47371498f30SBruce M Simpson printf("%s: blocked by" 47471498f30SBruce M Simpson " source filter\n", 47571498f30SBruce M Simpson __func__); 47671498f30SBruce M Simpson } 47771498f30SBruce M Simpson #endif 478603724d3SBjoern A. Zeeb V_udpstat.udps_filtermcast++; 47971498f30SBruce M Simpson blocked++; 48083453a06SBruce M Simpson } 48183453a06SBruce M Simpson } 48271498f30SBruce M Simpson if (blocked != 0) { 483119d85f6SRobert Watson INP_RUNLOCK(inp); 4849c1df695SRobert Watson continue; 4859c1df695SRobert Watson } 48683453a06SBruce M Simpson } 487df8bae1dSRodney W. Grimes if (last != NULL) { 488df8bae1dSRodney W. Grimes struct mbuf *n; 489df8bae1dSRodney W. Grimes 490032dcc76SLuigi Rizzo n = m_copy(m, 0, M_COPYALL); 491c7c7ea4bSRandall Stewart if (last->inp_ppcb == NULL) { 492365433d9SRobert Watson if (n != NULL) 493c7c7ea4bSRandall Stewart udp_append(last, 494c7c7ea4bSRandall Stewart ip, n, 495c7c7ea4bSRandall Stewart iphlen + 496c7c7ea4bSRandall Stewart sizeof(struct udphdr), 497c7c7ea4bSRandall Stewart &udp_in); 498119d85f6SRobert Watson INP_RUNLOCK(last); 499c7c7ea4bSRandall Stewart } else { 500c7c7ea4bSRandall Stewart /* 501c7c7ea4bSRandall Stewart * Engage the tunneling protocol we 502c7c7ea4bSRandall Stewart * will have to leave the info_lock 503c7c7ea4bSRandall Stewart * up, since we are hunting through 504bbb0e3d9SRandall Stewart * multiple UDP's. 505c7c7ea4bSRandall Stewart * 506c7c7ea4bSRandall Stewart */ 507c7c7ea4bSRandall Stewart udp_tun_func_t tunnel_func; 508c7c7ea4bSRandall Stewart 509c7c7ea4bSRandall Stewart tunnel_func = (udp_tun_func_t)last->inp_ppcb; 510c7c7ea4bSRandall Stewart tunnel_func(n, iphlen, last); 511c7c7ea4bSRandall Stewart INP_RUNLOCK(last); 512c7c7ea4bSRandall Stewart } 513df8bae1dSRodney W. Grimes } 51482c23ebaSBill Fenner last = inp; 515df8bae1dSRodney W. Grimes /* 516df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 517df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 5183329b236SRobert Watson * socket options set. This heuristic avoids 5193329b236SRobert Watson * searching through all pcbs in the common case of a 5203329b236SRobert Watson * non-shared port. It assumes that an application 5213329b236SRobert Watson * will never clear these options after setting them. 522df8bae1dSRodney W. Grimes */ 5233329b236SRobert Watson if ((last->inp_socket->so_options & 5243329b236SRobert Watson (SO_REUSEPORT|SO_REUSEADDR)) == 0) 525df8bae1dSRodney W. Grimes break; 526df8bae1dSRodney W. Grimes } 527df8bae1dSRodney W. Grimes 528df8bae1dSRodney W. Grimes if (last == NULL) { 529df8bae1dSRodney W. Grimes /* 5303329b236SRobert Watson * No matching pcb found; discard datagram. (No need 5313329b236SRobert Watson * to send an ICMP Port Unreachable for a broadcast 5323329b236SRobert Watson * or multicast datgram.) 533df8bae1dSRodney W. Grimes */ 534603724d3SBjoern A. Zeeb V_udpstat.udps_noportbcast++; 53561ffc0b1SJeffrey Hsu goto badheadlocked; 536df8bae1dSRodney W. Grimes } 537c7c7ea4bSRandall Stewart if (last->inp_ppcb == NULL) { 538d4b509bdSRobert Watson udp_append(last, ip, m, iphlen + sizeof(struct udphdr), 539d4b509bdSRobert Watson &udp_in); 540119d85f6SRobert Watson INP_RUNLOCK(last); 541603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 542c7c7ea4bSRandall Stewart } else { 543c7c7ea4bSRandall Stewart /* 544bbb0e3d9SRandall Stewart * Engage the tunneling protocol. 545c7c7ea4bSRandall Stewart */ 546c7c7ea4bSRandall Stewart udp_tun_func_t tunnel_func; 547c7c7ea4bSRandall Stewart 548c7c7ea4bSRandall Stewart tunnel_func = (udp_tun_func_t)last->inp_ppcb; 549c7c7ea4bSRandall Stewart tunnel_func(m, iphlen, last); 550c7c7ea4bSRandall Stewart INP_RUNLOCK(last); 551c7c7ea4bSRandall Stewart INP_INFO_RUNLOCK(&V_udbinfo); 552c7c7ea4bSRandall Stewart } 553df8bae1dSRodney W. Grimes return; 554df8bae1dSRodney W. Grimes } 5553329b236SRobert Watson 556df8bae1dSRodney W. Grimes /* 5576d6a026bSDavid Greenman * Locate pcb for datagram. 558df8bae1dSRodney W. Grimes */ 559603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_udbinfo, ip->ip_src, uh->uh_sport, 56071498f30SBruce M Simpson ip->ip_dst, uh->uh_dport, 1, ifp); 56115bd2b43SDavid Greenman if (inp == NULL) { 562afdb4274SRobert Watson if (udp_log_in_vain) { 563df5c0b8aSBill Fenner char buf[4*sizeof "123"]; 56475cfc95fSAndrey A. Chernov 56575cfc95fSAndrey A. Chernov strcpy(buf, inet_ntoa(ip->ip_dst)); 566592071e8SBruce Evans log(LOG_INFO, 567592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 568592071e8SBruce Evans buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 569592071e8SBruce Evans ntohs(uh->uh_sport)); 57075cfc95fSAndrey A. Chernov } 571603724d3SBjoern A. Zeeb V_udpstat.udps_noport++; 572df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 573603724d3SBjoern A. Zeeb V_udpstat.udps_noportbcast++; 57461ffc0b1SJeffrey Hsu goto badheadlocked; 575df8bae1dSRodney W. Grimes } 576603724d3SBjoern A. Zeeb if (V_udp_blackhole) 57761ffc0b1SJeffrey Hsu goto badheadlocked; 5781cbd978eSLuigi Rizzo if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 5791cbd978eSLuigi Rizzo goto badheadlocked; 58004287599SRuslan Ermilov *ip = save_ip; 58104287599SRuslan Ermilov ip->ip_len += iphlen; 582582a7760SBruce Evans icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 583603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 584df8bae1dSRodney W. Grimes return; 585df8bae1dSRodney W. Grimes } 5863329b236SRobert Watson 5873329b236SRobert Watson /* 5883329b236SRobert Watson * Check the minimum TTL for socket. 5893329b236SRobert Watson */ 590119d85f6SRobert Watson INP_RLOCK(inp); 591603724d3SBjoern A. Zeeb INP_INFO_RUNLOCK(&V_udbinfo); 59210cc62b7SRobert Watson if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) { 59310cc62b7SRobert Watson INP_RUNLOCK(inp); 59410cc62b7SRobert Watson goto badunlocked; 59510cc62b7SRobert Watson } 596c7c7ea4bSRandall Stewart if (inp->inp_ppcb != NULL) { 597c7c7ea4bSRandall Stewart /* 598bbb0e3d9SRandall Stewart * Engage the tunneling protocol. 599c7c7ea4bSRandall Stewart */ 600c7c7ea4bSRandall Stewart udp_tun_func_t tunnel_func; 601c7c7ea4bSRandall Stewart 602c7c7ea4bSRandall Stewart tunnel_func = (udp_tun_func_t)inp->inp_ppcb; 603c7c7ea4bSRandall Stewart tunnel_func(m, iphlen, inp); 604c7c7ea4bSRandall Stewart INP_RUNLOCK(inp); 605c7c7ea4bSRandall Stewart return; 606c7c7ea4bSRandall Stewart } 607d4b509bdSRobert Watson udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in); 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 */ 10243a1757b9SGleb Smirnoff if (jailed(td->td_ucred)) 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 */ 1112603724d3SBjoern A. Zeeb V_udpstat.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; 1178603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 1179cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1180603724d3SBjoern A. Zeeb inp->inp_ip_ttl = V_ip_defttl; 1181c7c7ea4bSRandall Stewart /* 1182c7c7ea4bSRandall Stewart * UDP does not have a per-protocol pcb (inp->inp_ppcb). 1183c7c7ea4bSRandall Stewart * We use this pointer for kernel tunneling pointer. 1184c7c7ea4bSRandall Stewart * If we ever need to have a protocol block we will 1185c7c7ea4bSRandall Stewart * need to move this function pointer there. Null 1186c7c7ea4bSRandall Stewart * in this pointer means "do the normal thing". 1187c7c7ea4bSRandall Stewart */ 1188c7c7ea4bSRandall Stewart inp->inp_ppcb = NULL; 1189c7c7ea4bSRandall Stewart INP_WUNLOCK(inp); 1190c7c7ea4bSRandall Stewart return (0); 1191c7c7ea4bSRandall Stewart } 1192c7c7ea4bSRandall Stewart 1193c7c7ea4bSRandall Stewart int 1194c7c7ea4bSRandall Stewart udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f) 1195c7c7ea4bSRandall Stewart { 1196c7c7ea4bSRandall Stewart struct inpcb *inp; 1197c7c7ea4bSRandall Stewart 1198c7c7ea4bSRandall Stewart inp = (struct inpcb *)so->so_pcb; 1199bbb0e3d9SRandall Stewart KASSERT(so->so_type == SOCK_DGRAM, ("udp_set_kernel_tunneling: !dgram")); 1200bbb0e3d9SRandall Stewart KASSERT(so->so_pcb != NULL, ("udp_set_kernel_tunneling: NULL inp")); 1201c7c7ea4bSRandall Stewart if (so->so_type != SOCK_DGRAM) { 1202c7c7ea4bSRandall Stewart /* Not UDP socket... sorry! */ 1203c7c7ea4bSRandall Stewart return (ENOTSUP); 1204c7c7ea4bSRandall Stewart } 1205c7c7ea4bSRandall Stewart if (inp == NULL) { 1206c7c7ea4bSRandall Stewart /* NULL INP? */ 1207c7c7ea4bSRandall Stewart return (EINVAL); 1208c7c7ea4bSRandall Stewart } 1209c7c7ea4bSRandall Stewart INP_WLOCK(inp); 1210bbb0e3d9SRandall Stewart if (inp->inp_ppcb != NULL) { 1211bbb0e3d9SRandall Stewart INP_WUNLOCK(inp); 1212bbb0e3d9SRandall Stewart return (EBUSY); 1213bbb0e3d9SRandall Stewart } 1214c7c7ea4bSRandall Stewart inp->inp_ppcb = f; 12158501a69cSRobert Watson INP_WUNLOCK(inp); 12163329b236SRobert Watson return (0); 1217df8bae1dSRodney W. Grimes } 1218d0390e05SGarrett Wollman 1219d0390e05SGarrett Wollman static int 1220b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1221d0390e05SGarrett Wollman { 12228b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1223d0390e05SGarrett Wollman struct inpcb *inp; 1224277afaffSRobert Watson int error; 1225d0390e05SGarrett Wollman 1226d0390e05SGarrett Wollman inp = sotoinpcb(so); 122714ba8addSRobert Watson KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 1228603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 12298501a69cSRobert Watson INP_WLOCK(inp); 1230b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, nam, td->td_ucred); 12318501a69cSRobert Watson INP_WUNLOCK(inp); 1232603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 12333329b236SRobert Watson return (error); 1234d0390e05SGarrett Wollman } 1235d0390e05SGarrett Wollman 1236a152f8a3SRobert Watson static void 1237a152f8a3SRobert Watson udp_close(struct socket *so) 1238a152f8a3SRobert Watson { 12398b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1240a152f8a3SRobert Watson struct inpcb *inp; 1241a152f8a3SRobert Watson 1242a152f8a3SRobert Watson inp = sotoinpcb(so); 1243a152f8a3SRobert Watson KASSERT(inp != NULL, ("udp_close: inp == NULL")); 1244603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 12458501a69cSRobert Watson INP_WLOCK(inp); 1246a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1247a152f8a3SRobert Watson in_pcbdisconnect(inp); 1248a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1249a152f8a3SRobert Watson soisdisconnected(so); 1250a152f8a3SRobert Watson } 12518501a69cSRobert Watson INP_WUNLOCK(inp); 1252603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 1253a152f8a3SRobert Watson } 1254a152f8a3SRobert Watson 1255d0390e05SGarrett Wollman static int 1256b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1257d0390e05SGarrett Wollman { 12588b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1259d0390e05SGarrett Wollman struct inpcb *inp; 1260277afaffSRobert Watson int error; 126175c13541SPoul-Henning Kamp struct sockaddr_in *sin; 1262d0390e05SGarrett Wollman 1263d0390e05SGarrett Wollman inp = sotoinpcb(so); 126414ba8addSRobert Watson KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 1265603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 12668501a69cSRobert Watson INP_WLOCK(inp); 1267f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr != INADDR_ANY) { 12688501a69cSRobert Watson INP_WUNLOCK(inp); 1269603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 12703329b236SRobert Watson return (EISCONN); 1271f76fcf6dSJeffrey Hsu } 127275c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)nam; 1273b89e82ddSJamie Gritton error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1274b89e82ddSJamie Gritton if (error != 0) { 1275413628a7SBjoern A. Zeeb INP_WUNLOCK(inp); 1276385195c0SMarko Zec INP_INFO_WUNLOCK(&V_udbinfo); 1277b89e82ddSJamie Gritton return (error); 1278413628a7SBjoern A. Zeeb } 1279b0330ed9SPawel Jakub Dawidek error = in_pcbconnect(inp, nam, td->td_ucred); 12804cc20ab1SSeigo Tanimura if (error == 0) 1281df8bae1dSRodney W. Grimes soisconnected(so); 12828501a69cSRobert Watson INP_WUNLOCK(inp); 1283603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 12843329b236SRobert Watson return (error); 1285df8bae1dSRodney W. Grimes } 1286d0390e05SGarrett Wollman 1287bc725eafSRobert Watson static void 1288d0390e05SGarrett Wollman udp_detach(struct socket *so) 1289d0390e05SGarrett Wollman { 12908b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1291d0390e05SGarrett Wollman struct inpcb *inp; 1292d0390e05SGarrett Wollman 1293d0390e05SGarrett Wollman inp = sotoinpcb(so); 129414ba8addSRobert Watson KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1295a152f8a3SRobert Watson KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1296a152f8a3SRobert Watson ("udp_detach: not disconnected")); 1297603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 12988501a69cSRobert Watson INP_WLOCK(inp); 1299d0390e05SGarrett Wollman in_pcbdetach(inp); 130014ba8addSRobert Watson in_pcbfree(inp); 1301603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 1302d0390e05SGarrett Wollman } 1303d0390e05SGarrett Wollman 1304d0390e05SGarrett Wollman static int 1305d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 1306d0390e05SGarrett Wollman { 13078b615593SMarko Zec INIT_VNET_INET(so->so_vnet); 1308d0390e05SGarrett Wollman struct inpcb *inp; 1309d0390e05SGarrett Wollman 1310d0390e05SGarrett Wollman inp = sotoinpcb(so); 131114ba8addSRobert Watson KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 1312603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_udbinfo); 13138501a69cSRobert Watson INP_WLOCK(inp); 1314f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr == INADDR_ANY) { 13158501a69cSRobert Watson INP_WUNLOCK(inp); 1316603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 13173329b236SRobert Watson return (ENOTCONN); 1318f76fcf6dSJeffrey Hsu } 1319d0390e05SGarrett Wollman 1320df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 1321df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 1322d45e4f99SMaxim Konovalov SOCK_LOCK(so); 1323d45e4f99SMaxim Konovalov so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1324d45e4f99SMaxim Konovalov SOCK_UNLOCK(so); 13258501a69cSRobert Watson INP_WUNLOCK(inp); 1326603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_udbinfo); 13273329b236SRobert Watson return (0); 1328df8bae1dSRodney W. Grimes } 1329df8bae1dSRodney W. Grimes 1330d0390e05SGarrett Wollman static int 133157bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1332b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1333d0390e05SGarrett Wollman { 1334d0390e05SGarrett Wollman struct inpcb *inp; 1335d0390e05SGarrett Wollman 1336d0390e05SGarrett Wollman inp = sotoinpcb(so); 133714ba8addSRobert Watson KASSERT(inp != NULL, ("udp_send: inp == NULL")); 13383329b236SRobert Watson return (udp_output(inp, m, addr, control, td)); 1339d0390e05SGarrett Wollman } 1340d0390e05SGarrett Wollman 134176429de4SYoshinobu Inoue int 1342d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 1343d0390e05SGarrett Wollman { 1344d0390e05SGarrett Wollman struct inpcb *inp; 1345d0390e05SGarrett Wollman 1346d0390e05SGarrett Wollman inp = sotoinpcb(so); 134714ba8addSRobert Watson KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 13488501a69cSRobert Watson INP_WLOCK(inp); 1349d0390e05SGarrett Wollman socantsendmore(so); 13508501a69cSRobert Watson INP_WUNLOCK(inp); 13513329b236SRobert Watson return (0); 1352d0390e05SGarrett Wollman } 1353d0390e05SGarrett Wollman 1354d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 1355756d52a1SPoul-Henning Kamp .pru_abort = udp_abort, 1356756d52a1SPoul-Henning Kamp .pru_attach = udp_attach, 1357756d52a1SPoul-Henning Kamp .pru_bind = udp_bind, 1358756d52a1SPoul-Henning Kamp .pru_connect = udp_connect, 1359756d52a1SPoul-Henning Kamp .pru_control = in_control, 1360756d52a1SPoul-Henning Kamp .pru_detach = udp_detach, 1361756d52a1SPoul-Henning Kamp .pru_disconnect = udp_disconnect, 136254d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1363756d52a1SPoul-Henning Kamp .pru_send = udp_send, 13645df3e839SRobert Watson .pru_soreceive = soreceive_dgram, 136559b8854eSRobert Watson .pru_sosend = sosend_dgram, 1366756d52a1SPoul-Henning Kamp .pru_shutdown = udp_shutdown, 136754d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1368a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1369a152f8a3SRobert Watson .pru_close = udp_close, 1370d0390e05SGarrett Wollman }; 1371