1c398230bSWarner Losh /*- 26dfab5b1SGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 33329b236SRobert Watson * The Regents of the University of California. 43329b236SRobert Watson * All rights reserved. 5df8bae1dSRodney W. Grimes * 6df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 7df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 8df8bae1dSRodney W. Grimes * are met: 9df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 10df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 11df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 13df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 14df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 15df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 16df8bae1dSRodney W. Grimes * without specific prior written permission. 17df8bae1dSRodney W. Grimes * 18df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28df8bae1dSRodney W. Grimes * SUCH DAMAGE. 29df8bae1dSRodney W. Grimes * 306dfab5b1SGarrett Wollman * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 31c3aac50fSPeter Wemm * $FreeBSD$ 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 340b4ae859SGleb Smirnoff #include "opt_ipfw.h" 35cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 36f5514f08SRobert Watson #include "opt_ipsec.h" 37bdb3fa18SRobert Watson #include "opt_mac.h" 38cfa1ca9dSYoshinobu Inoue 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40960ed29cSSeigo Tanimura #include <sys/domain.h> 414f590175SPaul Saab #include <sys/eventhandler.h> 42960ed29cSSeigo Tanimura #include <sys/jail.h> 43b110a8a2SGarrett Wollman #include <sys/kernel.h> 44960ed29cSSeigo Tanimura #include <sys/lock.h> 45df8bae1dSRodney W. Grimes #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47acd3428bSRobert Watson #include <sys/priv.h> 48490d50b6SBrian Feldman #include <sys/proc.h> 49df8bae1dSRodney W. Grimes #include <sys/protosw.h> 50960ed29cSSeigo Tanimura #include <sys/signalvar.h> 51df8bae1dSRodney W. Grimes #include <sys/socket.h> 52df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 53960ed29cSSeigo Tanimura #include <sys/sx.h> 54b5e8ce9fSBruce Evans #include <sys/sysctl.h> 55816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 56f5514f08SRobert Watson #include <sys/systm.h> 578781d8e9SBruce Evans 5869c2d429SJeff Roberson #include <vm/uma.h> 59df8bae1dSRodney W. Grimes 60df8bae1dSRodney W. Grimes #include <net/if.h> 61df8bae1dSRodney W. Grimes #include <net/route.h> 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes #include <netinet/in.h> 64960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 65f5514f08SRobert Watson #include <netinet/in_systm.h> 66960ed29cSSeigo Tanimura #include <netinet/in_var.h> 67df8bae1dSRodney W. Grimes #include <netinet/ip.h> 68cfa1ca9dSYoshinobu Inoue #ifdef INET6 69cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 70cfa1ca9dSYoshinobu Inoue #endif 71960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h> 72960ed29cSSeigo Tanimura #include <netinet/icmp_var.h> 73df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 74ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 75cfa1ca9dSYoshinobu Inoue #ifdef INET6 76cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 77cfa1ca9dSYoshinobu Inoue #endif 78df8bae1dSRodney W. Grimes #include <netinet/udp.h> 79df8bae1dSRodney W. Grimes #include <netinet/udp_var.h> 80df8bae1dSRodney W. Grimes 81b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 82b9234fafSSam Leffler #include <netipsec/ipsec.h> 833329b236SRobert Watson #endif 84b9234fafSSam Leffler 85db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 86db4f9cc7SJonathan Lemon 87aed55708SRobert Watson #include <security/mac/mac_framework.h> 88aed55708SRobert Watson 89df8bae1dSRodney W. Grimes /* 90df8bae1dSRodney W. Grimes * UDP protocol implementation. 91df8bae1dSRodney W. Grimes * Per RFC 768, August, 1980. 92df8bae1dSRodney W. Grimes */ 9374eb3236SWarner Losh 9474eb3236SWarner Losh /* 953329b236SRobert Watson * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums 963329b236SRobert Watson * removes the only data integrity mechanism for packets and malformed 97f5514f08SRobert Watson * packets that would otherwise be discarded due to bad checksums, and may 98f5514f08SRobert Watson * cause problems (especially for NFS data blocks). 9974eb3236SWarner Losh */ 100f5514f08SRobert Watson static int udp_cksum = 1; 101f5514f08SRobert Watson SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum, 1023329b236SRobert Watson 0, ""); 103df8bae1dSRodney W. Grimes 104afdb4274SRobert Watson int udp_log_in_vain = 0; 105816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 106afdb4274SRobert Watson &udp_log_in_vain, 0, "Log all incoming UDP packets"); 107816a3d83SPoul-Henning Kamp 10843bbb6aaSRobert Watson int udp_blackhole = 0; 10943bbb6aaSRobert Watson SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, &udp_blackhole, 0, 1103329b236SRobert Watson "Do not send port unreachables for refused connects"); 11116f7f31fSGeoff Rehmet 11243bbb6aaSRobert Watson u_long udp_sendspace = 9216; /* really max datagram size */ 11343bbb6aaSRobert Watson /* 40 1K datagrams */ 11443bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 11543bbb6aaSRobert Watson &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 11643bbb6aaSRobert Watson 11743bbb6aaSRobert Watson u_long udp_recvspace = 40 * (1024 + 11843bbb6aaSRobert Watson #ifdef INET6 11943bbb6aaSRobert Watson sizeof(struct sockaddr_in6) 12043bbb6aaSRobert Watson #else 12143bbb6aaSRobert Watson sizeof(struct sockaddr_in) 12243bbb6aaSRobert Watson #endif 12343bbb6aaSRobert Watson ); 12443bbb6aaSRobert Watson 12543bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 12643bbb6aaSRobert Watson &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 12743bbb6aaSRobert Watson 12876429de4SYoshinobu Inoue struct inpcbhead udb; /* from udp_var.h */ 1297a2aab80SBrian Feldman struct inpcbinfo udbinfo; 13015bd2b43SDavid Greenman 13115bd2b43SDavid Greenman #ifndef UDBHASHSIZE 132c3229e05SDavid Greenman #define UDBHASHSIZE 16 13315bd2b43SDavid Greenman #endif 13415bd2b43SDavid Greenman 13576429de4SYoshinobu Inoue struct udpstat udpstat; /* from udp_var.h */ 1363329b236SRobert Watson SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, &udpstat, 1373329b236SRobert Watson udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 138f2ea20e6SGarrett Wollman 139bc725eafSRobert Watson static void udp_detach(struct socket *so); 1404d77a549SAlfred Perlstein static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 1414d77a549SAlfred Perlstein struct mbuf *, struct thread *); 142df8bae1dSRodney W. Grimes 1434f590175SPaul Saab static void 1444f590175SPaul Saab udp_zone_change(void *tag) 1454f590175SPaul Saab { 1464f590175SPaul Saab 1474f590175SPaul Saab uma_zone_set_max(udbinfo.ipi_zone, maxsockets); 1484f590175SPaul Saab } 1494f590175SPaul Saab 150d915b280SStephan Uphoff static int 151d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags) 152d915b280SStephan Uphoff { 153af1ee11dSRobert Watson struct inpcb *inp; 15408651e1fSJohn Baldwin 155af1ee11dSRobert Watson inp = mem; 156d915b280SStephan Uphoff INP_LOCK_INIT(inp, "inp", "udpinp"); 157d915b280SStephan Uphoff return (0); 158d915b280SStephan Uphoff } 159d915b280SStephan Uphoff 160df8bae1dSRodney W. Grimes void 161af1ee11dSRobert Watson udp_init(void) 162df8bae1dSRodney W. Grimes { 163af1ee11dSRobert Watson 164f76fcf6dSJeffrey Hsu INP_INFO_LOCK_INIT(&udbinfo, "udp"); 16515bd2b43SDavid Greenman LIST_INIT(&udb); 166712fc218SRobert Watson udbinfo.ipi_listhead = &udb; 167712fc218SRobert Watson udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB, 168712fc218SRobert Watson &udbinfo.ipi_hashmask); 169712fc218SRobert Watson udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB, 170712fc218SRobert Watson &udbinfo.ipi_porthashmask); 17169c2d429SJeff Roberson udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL, 172d915b280SStephan Uphoff NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 17369c2d429SJeff Roberson uma_zone_set_max(udbinfo.ipi_zone, maxsockets); 1744f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL, 1754f590175SPaul Saab EVENTHANDLER_PRI_ANY); 176df8bae1dSRodney W. Grimes } 177df8bae1dSRodney W. Grimes 17843bbb6aaSRobert Watson /* 17943bbb6aaSRobert Watson * Subroutine of udp_input(), which appends the provided mbuf chain to the 18043bbb6aaSRobert Watson * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 18143bbb6aaSRobert Watson * contains the source address. If the socket ends up being an IPv6 socket, 18243bbb6aaSRobert Watson * udp_append() will convert to a sockaddr_in6 before passing the address 18343bbb6aaSRobert Watson * into the socket code. 18443bbb6aaSRobert Watson */ 18543bbb6aaSRobert Watson static void 18643bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off, 18743bbb6aaSRobert Watson struct sockaddr_in *udp_in) 18843bbb6aaSRobert Watson { 18943bbb6aaSRobert Watson struct sockaddr *append_sa; 19043bbb6aaSRobert Watson struct socket *so; 19143bbb6aaSRobert Watson struct mbuf *opts = 0; 19243bbb6aaSRobert Watson #ifdef INET6 19343bbb6aaSRobert Watson struct sockaddr_in6 udp_in6; 19443bbb6aaSRobert Watson #endif 19543bbb6aaSRobert Watson 19643bbb6aaSRobert Watson INP_LOCK_ASSERT(inp); 19743bbb6aaSRobert Watson 19843bbb6aaSRobert Watson #ifdef IPSEC 19943bbb6aaSRobert Watson /* Check AH/ESP integrity. */ 20043bbb6aaSRobert Watson if (ipsec4_in_reject(n, inp)) { 20143bbb6aaSRobert Watson m_freem(n); 20243bbb6aaSRobert Watson ipsec4stat.in_polvio++; 20343bbb6aaSRobert Watson return; 20443bbb6aaSRobert Watson } 20543bbb6aaSRobert Watson #endif /* IPSEC */ 20643bbb6aaSRobert Watson #ifdef MAC 20743bbb6aaSRobert Watson if (mac_check_inpcb_deliver(inp, n) != 0) { 20843bbb6aaSRobert Watson m_freem(n); 20943bbb6aaSRobert Watson return; 21043bbb6aaSRobert Watson } 21143bbb6aaSRobert Watson #endif 21243bbb6aaSRobert Watson if (inp->inp_flags & INP_CONTROLOPTS || 21343bbb6aaSRobert Watson inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 21443bbb6aaSRobert Watson #ifdef INET6 21543bbb6aaSRobert Watson if (inp->inp_vflag & INP_IPV6) { 21643bbb6aaSRobert Watson int savedflags; 21743bbb6aaSRobert Watson 21843bbb6aaSRobert Watson savedflags = inp->inp_flags; 21943bbb6aaSRobert Watson inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 22043bbb6aaSRobert Watson ip6_savecontrol(inp, n, &opts); 22143bbb6aaSRobert Watson inp->inp_flags = savedflags; 22243bbb6aaSRobert Watson } else 22343bbb6aaSRobert Watson #endif 22443bbb6aaSRobert Watson ip_savecontrol(inp, &opts, ip, n); 22543bbb6aaSRobert Watson } 22643bbb6aaSRobert Watson #ifdef INET6 22743bbb6aaSRobert Watson if (inp->inp_vflag & INP_IPV6) { 22843bbb6aaSRobert Watson bzero(&udp_in6, sizeof(udp_in6)); 22943bbb6aaSRobert Watson udp_in6.sin6_len = sizeof(udp_in6); 23043bbb6aaSRobert Watson udp_in6.sin6_family = AF_INET6; 23143bbb6aaSRobert Watson in6_sin_2_v4mapsin6(udp_in, &udp_in6); 23243bbb6aaSRobert Watson append_sa = (struct sockaddr *)&udp_in6; 23343bbb6aaSRobert Watson } else 23443bbb6aaSRobert Watson #endif 23543bbb6aaSRobert Watson append_sa = (struct sockaddr *)udp_in; 23643bbb6aaSRobert Watson m_adj(n, off); 23743bbb6aaSRobert Watson 23843bbb6aaSRobert Watson so = inp->inp_socket; 23943bbb6aaSRobert Watson SOCKBUF_LOCK(&so->so_rcv); 24043bbb6aaSRobert Watson if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 24143bbb6aaSRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 24243bbb6aaSRobert Watson m_freem(n); 24343bbb6aaSRobert Watson if (opts) 24443bbb6aaSRobert Watson m_freem(opts); 24543bbb6aaSRobert Watson udpstat.udps_fullsock++; 24643bbb6aaSRobert Watson } else 24743bbb6aaSRobert Watson sorwakeup_locked(so); 24843bbb6aaSRobert Watson } 24943bbb6aaSRobert Watson 250df8bae1dSRodney W. Grimes void 2513329b236SRobert Watson udp_input(struct mbuf *m, int off) 252df8bae1dSRodney W. Grimes { 253cfa1ca9dSYoshinobu Inoue int iphlen = off; 2543329b236SRobert Watson struct ip *ip; 2553329b236SRobert Watson struct udphdr *uh; 25671498f30SBruce M Simpson struct ifnet *ifp; 2573329b236SRobert Watson struct inpcb *inp; 258df8bae1dSRodney W. Grimes int len; 259df8bae1dSRodney W. Grimes struct ip save_ip; 260d4b509bdSRobert Watson struct sockaddr_in udp_in; 2610b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD 2620b4ae859SGleb Smirnoff struct m_tag *fwd_tag; 2630b4ae859SGleb Smirnoff #endif 264df8bae1dSRodney W. Grimes 26571498f30SBruce M Simpson ifp = m->m_pkthdr.rcvif; 266df8bae1dSRodney W. Grimes udpstat.udps_ipackets++; 267df8bae1dSRodney W. Grimes 268df8bae1dSRodney W. Grimes /* 2693329b236SRobert Watson * Strip IP options, if any; should skip this, make available to 2703329b236SRobert Watson * user, and use on returned packets, but we don't yet have a way to 2713329b236SRobert Watson * check the checksum with options still present. 272df8bae1dSRodney W. Grimes */ 273df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 274df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 275df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 276df8bae1dSRodney W. Grimes } 277df8bae1dSRodney W. Grimes 278df8bae1dSRodney W. Grimes /* 279df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 280df8bae1dSRodney W. Grimes */ 281df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 282df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 283df8bae1dSRodney W. Grimes if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 284df8bae1dSRodney W. Grimes udpstat.udps_hdrops++; 285df8bae1dSRodney W. Grimes return; 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 288df8bae1dSRodney W. Grimes } 289df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 290df8bae1dSRodney W. Grimes 2913329b236SRobert Watson /* 2923329b236SRobert Watson * Destination port of 0 is illegal, based on RFC768. 2933329b236SRobert Watson */ 294686cdd19SJun-ichiro itojun Hagino if (uh->uh_dport == 0) 295f76fcf6dSJeffrey Hsu goto badunlocked; 296686cdd19SJun-ichiro itojun Hagino 297df8bae1dSRodney W. Grimes /* 2983329b236SRobert Watson * Construct sockaddr format source address. Stuff source address 2993329b236SRobert Watson * and datagram in user buffer. 300b9234fafSSam Leffler */ 301d4b509bdSRobert Watson bzero(&udp_in, sizeof(udp_in)); 302d4b509bdSRobert Watson udp_in.sin_len = sizeof(udp_in); 303d4b509bdSRobert Watson udp_in.sin_family = AF_INET; 304b9234fafSSam Leffler udp_in.sin_port = uh->uh_sport; 305b9234fafSSam Leffler udp_in.sin_addr = ip->ip_src; 306b9234fafSSam Leffler 307b9234fafSSam Leffler /* 308af1ee11dSRobert Watson * Make mbuf data length reflect UDP length. If not enough data to 309af1ee11dSRobert Watson * reflect UDP length, drop. 310df8bae1dSRodney W. Grimes */ 311df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 312df8bae1dSRodney W. Grimes if (ip->ip_len != len) { 3137eb7a449SAndras Olah if (len > ip->ip_len || len < sizeof(struct udphdr)) { 314df8bae1dSRodney W. Grimes udpstat.udps_badlen++; 315f76fcf6dSJeffrey Hsu goto badunlocked; 316df8bae1dSRodney W. Grimes } 317df8bae1dSRodney W. Grimes m_adj(m, len - ip->ip_len); 318df8bae1dSRodney W. Grimes /* ip->ip_len = len; */ 319df8bae1dSRodney W. Grimes } 3203329b236SRobert Watson 321df8bae1dSRodney W. Grimes /* 3223329b236SRobert Watson * Save a copy of the IP header in case we want restore it for 3233329b236SRobert Watson * sending an ICMP error message in response. 324df8bae1dSRodney W. Grimes */ 32543bbb6aaSRobert Watson if (!udp_blackhole) 326df8bae1dSRodney W. Grimes save_ip = *ip; 327cce418d3SMatt Jacob else 328cce418d3SMatt Jacob memset(&save_ip, 0, sizeof(save_ip)); 329df8bae1dSRodney W. Grimes 330df8bae1dSRodney W. Grimes /* 331df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 332df8bae1dSRodney W. Grimes */ 3336dfab5b1SGarrett Wollman if (uh->uh_sum) { 33439629c92SDavid Malone u_short uh_sum; 33539629c92SDavid Malone 336db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 337db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 33839629c92SDavid Malone uh_sum = m->m_pkthdr.csum_data; 339db4f9cc7SJonathan Lemon else 34039629c92SDavid Malone uh_sum = in_pseudo(ip->ip_src.s_addr, 341506f4949SRuslan Ermilov ip->ip_dst.s_addr, htonl((u_short)len + 342db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data + IPPROTO_UDP)); 34339629c92SDavid Malone uh_sum ^= 0xffff; 344db4f9cc7SJonathan Lemon } else { 345cb342100SHajimu UMEMOTO char b[9]; 346af1ee11dSRobert Watson 347cb342100SHajimu UMEMOTO bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 3486effc713SDoug Rabson bzero(((struct ipovly *)ip)->ih_x1, 9); 349df8bae1dSRodney W. Grimes ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 35039629c92SDavid Malone uh_sum = in_cksum(m, len + sizeof (struct ip)); 351cb342100SHajimu UMEMOTO bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 352db4f9cc7SJonathan Lemon } 35339629c92SDavid Malone if (uh_sum) { 354df8bae1dSRodney W. Grimes udpstat.udps_badsum++; 355df8bae1dSRodney W. Grimes m_freem(m); 356df8bae1dSRodney W. Grimes return; 357df8bae1dSRodney W. Grimes } 358fb9aaba0SRuslan Ermilov } else 359fb9aaba0SRuslan Ermilov udpstat.udps_nosum++; 360df8bae1dSRodney W. Grimes 3610b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD 3623329b236SRobert Watson /* 3633329b236SRobert Watson * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 3643329b236SRobert Watson */ 3650b4ae859SGleb Smirnoff fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 3660b4ae859SGleb Smirnoff if (fwd_tag != NULL) { 3670b4ae859SGleb Smirnoff struct sockaddr_in *next_hop; 3680b4ae859SGleb Smirnoff 3693329b236SRobert Watson /* 3703329b236SRobert Watson * Do the hack. 3713329b236SRobert Watson */ 3720b4ae859SGleb Smirnoff next_hop = (struct sockaddr_in *)(fwd_tag + 1); 3730b4ae859SGleb Smirnoff ip->ip_dst = next_hop->sin_addr; 3740b4ae859SGleb Smirnoff uh->uh_dport = ntohs(next_hop->sin_port); 3753329b236SRobert Watson 3763329b236SRobert Watson /* 3773329b236SRobert Watson * Remove the tag from the packet. We don't need it anymore. 3783329b236SRobert Watson */ 3790b4ae859SGleb Smirnoff m_tag_delete(m, fwd_tag); 3800b4ae859SGleb Smirnoff } 3810b4ae859SGleb Smirnoff #endif 3820b4ae859SGleb Smirnoff 383f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 384df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 38571498f30SBruce M Simpson in_broadcast(ip->ip_dst, ifp)) { 38682c23ebaSBill Fenner struct inpcb *last; 38771498f30SBruce M Simpson struct ip_moptions *imo; 3883329b236SRobert Watson 389df8bae1dSRodney W. Grimes last = NULL; 390cfa1ca9dSYoshinobu Inoue LIST_FOREACH(inp, &udb, inp_list) { 3919c1df695SRobert Watson if (inp->inp_lport != uh->uh_dport) 392f76fcf6dSJeffrey Hsu continue; 393cfa1ca9dSYoshinobu Inoue #ifdef INET6 394369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 3959c1df695SRobert Watson continue; 396cfa1ca9dSYoshinobu Inoue #endif 39771498f30SBruce M Simpson if (inp->inp_laddr.s_addr != INADDR_ANY && 39871498f30SBruce M Simpson inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 3999c1df695SRobert Watson continue; 40071498f30SBruce M Simpson if (inp->inp_faddr.s_addr != INADDR_ANY && 40171498f30SBruce M Simpson inp->inp_faddr.s_addr != ip->ip_src.s_addr) 40271498f30SBruce M Simpson continue; 40371498f30SBruce M Simpson /* 40471498f30SBruce M Simpson * XXX: Do not check source port of incoming datagram 40571498f30SBruce M Simpson * unless inp_connect() has been called to bind the 40671498f30SBruce M Simpson * fport part of the 4-tuple; the source could be 40771498f30SBruce M Simpson * trying to talk to us with an ephemeral port. 40871498f30SBruce M Simpson */ 40971498f30SBruce M Simpson if (inp->inp_fport != 0 && 410df8bae1dSRodney W. Grimes inp->inp_fport != uh->uh_sport) 4119c1df695SRobert Watson continue; 41271498f30SBruce M Simpson 41371498f30SBruce M Simpson INP_LOCK(inp); 414df8bae1dSRodney W. Grimes 41583453a06SBruce M Simpson /* 41671498f30SBruce M Simpson * Handle socket delivery policy for any-source 41771498f30SBruce M Simpson * and source-specific multicast. [RFC3678] 41883453a06SBruce M Simpson */ 41971498f30SBruce M Simpson imo = inp->inp_moptions; 42071498f30SBruce M Simpson if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 42171498f30SBruce M Simpson imo != NULL) { 42271498f30SBruce M Simpson struct sockaddr_in sin; 42371498f30SBruce M Simpson struct in_msource *ims; 42471498f30SBruce M Simpson int blocked, mode; 42571498f30SBruce M Simpson size_t idx; 42683453a06SBruce M Simpson 42771498f30SBruce M Simpson bzero(&sin, sizeof(struct sockaddr_in)); 42871498f30SBruce M Simpson sin.sin_len = sizeof(struct sockaddr_in); 42971498f30SBruce M Simpson sin.sin_family = AF_INET; 43071498f30SBruce M Simpson sin.sin_addr = ip->ip_dst; 43171498f30SBruce M Simpson 43271498f30SBruce M Simpson blocked = 0; 43371498f30SBruce M Simpson idx = imo_match_group(imo, ifp, 43471498f30SBruce M Simpson (struct sockaddr *)&sin); 43571498f30SBruce M Simpson if (idx == -1) { 43671498f30SBruce M Simpson /* 43771498f30SBruce M Simpson * No group membership for this socket. 43871498f30SBruce M Simpson * Do not bump udps_noportbcast, as 43971498f30SBruce M Simpson * this will happen further down. 44071498f30SBruce M Simpson */ 44171498f30SBruce M Simpson blocked++; 44271498f30SBruce M Simpson } else { 44371498f30SBruce M Simpson /* 44471498f30SBruce M Simpson * Check for a multicast source filter 44571498f30SBruce M Simpson * entry on this socket for this group. 44671498f30SBruce M Simpson * MCAST_EXCLUDE is the default 44771498f30SBruce M Simpson * behaviour. It means default accept; 44871498f30SBruce M Simpson * entries, if present, denote sources 44971498f30SBruce M Simpson * to be excluded from delivery. 45071498f30SBruce M Simpson */ 45171498f30SBruce M Simpson ims = imo_match_source(imo, idx, 45271498f30SBruce M Simpson (struct sockaddr *)&udp_in); 45371498f30SBruce M Simpson mode = imo->imo_mfilters[idx].imf_fmode; 45471498f30SBruce M Simpson if ((ims != NULL && 45571498f30SBruce M Simpson mode == MCAST_EXCLUDE) || 45671498f30SBruce M Simpson (ims == NULL && 45771498f30SBruce M Simpson mode == MCAST_INCLUDE)) { 45871498f30SBruce M Simpson #ifdef DIAGNOSTIC 45971498f30SBruce M Simpson if (bootverbose) { 46071498f30SBruce M Simpson printf("%s: blocked by" 46171498f30SBruce M Simpson " source filter\n", 46271498f30SBruce M Simpson __func__); 46371498f30SBruce M Simpson } 46471498f30SBruce M Simpson #endif 46571498f30SBruce M Simpson udpstat.udps_filtermcast++; 46671498f30SBruce M Simpson blocked++; 46783453a06SBruce M Simpson } 46883453a06SBruce M Simpson } 46971498f30SBruce M Simpson if (blocked != 0) { 4709c1df695SRobert Watson INP_UNLOCK(inp); 4719c1df695SRobert Watson continue; 4729c1df695SRobert Watson } 47383453a06SBruce M Simpson } 474df8bae1dSRodney W. Grimes if (last != NULL) { 475df8bae1dSRodney W. Grimes struct mbuf *n; 476df8bae1dSRodney W. Grimes 477032dcc76SLuigi Rizzo n = m_copy(m, 0, M_COPYALL); 478365433d9SRobert Watson if (n != NULL) 4793329b236SRobert Watson udp_append(last, ip, n, iphlen + 4803329b236SRobert Watson sizeof(struct udphdr), &udp_in); 481f76fcf6dSJeffrey Hsu INP_UNLOCK(last); 482df8bae1dSRodney W. Grimes } 48382c23ebaSBill Fenner last = inp; 484df8bae1dSRodney W. Grimes /* 485df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 486df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 4873329b236SRobert Watson * socket options set. This heuristic avoids 4883329b236SRobert Watson * searching through all pcbs in the common case of a 4893329b236SRobert Watson * non-shared port. It assumes that an application 4903329b236SRobert Watson * will never clear these options after setting them. 491df8bae1dSRodney W. Grimes */ 4923329b236SRobert Watson if ((last->inp_socket->so_options & 4933329b236SRobert Watson (SO_REUSEPORT|SO_REUSEADDR)) == 0) 494df8bae1dSRodney W. Grimes break; 495df8bae1dSRodney W. Grimes } 496df8bae1dSRodney W. Grimes 497df8bae1dSRodney W. Grimes if (last == NULL) { 498df8bae1dSRodney W. Grimes /* 4993329b236SRobert Watson * No matching pcb found; discard datagram. (No need 5003329b236SRobert Watson * to send an ICMP Port Unreachable for a broadcast 5013329b236SRobert Watson * or multicast datgram.) 502df8bae1dSRodney W. Grimes */ 503df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 50461ffc0b1SJeffrey Hsu goto badheadlocked; 505df8bae1dSRodney W. Grimes } 506d4b509bdSRobert Watson udp_append(last, ip, m, iphlen + sizeof(struct udphdr), 507d4b509bdSRobert Watson &udp_in); 5089b657230SSam Leffler INP_UNLOCK(last); 5096b8e5a98SRobert Watson INP_INFO_RUNLOCK(&udbinfo); 510df8bae1dSRodney W. Grimes return; 511df8bae1dSRodney W. Grimes } 5123329b236SRobert Watson 513df8bae1dSRodney W. Grimes /* 5146d6a026bSDavid Greenman * Locate pcb for datagram. 515df8bae1dSRodney W. Grimes */ 516c3229e05SDavid Greenman inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 51771498f30SBruce M Simpson ip->ip_dst, uh->uh_dport, 1, ifp); 51815bd2b43SDavid Greenman if (inp == NULL) { 519afdb4274SRobert Watson if (udp_log_in_vain) { 520df5c0b8aSBill Fenner char buf[4*sizeof "123"]; 52175cfc95fSAndrey A. Chernov 52275cfc95fSAndrey A. Chernov strcpy(buf, inet_ntoa(ip->ip_dst)); 523592071e8SBruce Evans log(LOG_INFO, 524592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 525592071e8SBruce Evans buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 526592071e8SBruce Evans ntohs(uh->uh_sport)); 52775cfc95fSAndrey A. Chernov } 528df8bae1dSRodney W. Grimes udpstat.udps_noport++; 529df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 530df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 53161ffc0b1SJeffrey Hsu goto badheadlocked; 532df8bae1dSRodney W. Grimes } 53343bbb6aaSRobert Watson if (udp_blackhole) 53461ffc0b1SJeffrey Hsu goto badheadlocked; 5351cbd978eSLuigi Rizzo if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 5361cbd978eSLuigi Rizzo goto badheadlocked; 53704287599SRuslan Ermilov *ip = save_ip; 53804287599SRuslan Ermilov ip->ip_len += iphlen; 539582a7760SBruce Evans icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 540f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 541df8bae1dSRodney W. Grimes return; 542df8bae1dSRodney W. Grimes } 5433329b236SRobert Watson 5443329b236SRobert Watson /* 5453329b236SRobert Watson * Check the minimum TTL for socket. 5463329b236SRobert Watson */ 54761ffc0b1SJeffrey Hsu INP_LOCK(inp); 548936cd18dSAndre Oppermann if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) 549936cd18dSAndre Oppermann goto badheadlocked; 550d4b509bdSRobert Watson udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in); 551f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 5526b8e5a98SRobert Watson INP_INFO_RUNLOCK(&udbinfo); 553df8bae1dSRodney W. Grimes return; 55461ffc0b1SJeffrey Hsu 55561ffc0b1SJeffrey Hsu badheadlocked: 556f76fcf6dSJeffrey Hsu if (inp) 557f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 5586b8e5a98SRobert Watson INP_INFO_RUNLOCK(&udbinfo); 559f76fcf6dSJeffrey Hsu badunlocked: 560df8bae1dSRodney W. Grimes m_freem(m); 561cfa1ca9dSYoshinobu Inoue } 562cfa1ca9dSYoshinobu Inoue 563cfa1ca9dSYoshinobu Inoue /* 5643329b236SRobert Watson * Notify a udp user of an asynchronous error; just wake up so that they can 5653329b236SRobert Watson * collect error status. 566df8bae1dSRodney W. Grimes */ 5673ce144eaSJeffrey Hsu struct inpcb * 5683329b236SRobert Watson udp_notify(struct inpcb *inp, int errno) 569df8bae1dSRodney W. Grimes { 5703329b236SRobert Watson 571df8bae1dSRodney W. Grimes inp->inp_socket->so_error = errno; 572df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 573df8bae1dSRodney W. Grimes sowwakeup(inp->inp_socket); 5743329b236SRobert Watson return (inp); 575df8bae1dSRodney W. Grimes } 576df8bae1dSRodney W. Grimes 577df8bae1dSRodney W. Grimes void 5783329b236SRobert Watson udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 579df8bae1dSRodney W. Grimes { 580c693a045SJonathan Lemon struct ip *ip = vip; 581c693a045SJonathan Lemon struct udphdr *uh; 582c693a045SJonathan Lemon struct in_addr faddr; 583c693a045SJonathan Lemon struct inpcb *inp; 584c693a045SJonathan Lemon 585c693a045SJonathan Lemon faddr = ((struct sockaddr_in *)sa)->sin_addr; 586c693a045SJonathan Lemon if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 587c693a045SJonathan Lemon return; 588df8bae1dSRodney W. Grimes 58997d8d152SAndre Oppermann /* 59097d8d152SAndre Oppermann * Redirects don't need to be handled up here. 59197d8d152SAndre Oppermann */ 59297d8d152SAndre Oppermann if (PRC_IS_REDIRECT(cmd)) 59397d8d152SAndre Oppermann return; 5943329b236SRobert Watson 59597d8d152SAndre Oppermann /* 59697d8d152SAndre Oppermann * Hostdead is ugly because it goes linearly through all PCBs. 5973329b236SRobert Watson * 5983329b236SRobert Watson * XXX: We never get this from ICMP, otherwise it makes an excellent 5993329b236SRobert Watson * DoS attack on machines with many connections. 60097d8d152SAndre Oppermann */ 60197d8d152SAndre Oppermann if (cmd == PRC_HOSTDEAD) 602af1ee11dSRobert Watson ip = NULL; 603d1c54148SJesper Skriver else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 604df8bae1dSRodney W. Grimes return; 605af1ee11dSRobert Watson if (ip != NULL) { 606df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 607f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 608c693a045SJonathan Lemon inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, 609c693a045SJonathan Lemon ip->ip_src, uh->uh_sport, 0, NULL); 610f76fcf6dSJeffrey Hsu if (inp != NULL) { 611f76fcf6dSJeffrey Hsu INP_LOCK(inp); 612f76fcf6dSJeffrey Hsu if (inp->inp_socket != NULL) { 613f5514f08SRobert Watson udp_notify(inp, inetctlerrmap[cmd]); 614f76fcf6dSJeffrey Hsu } 615f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 616f76fcf6dSJeffrey Hsu } 617f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 618df8bae1dSRodney W. Grimes } else 619f5514f08SRobert Watson in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], 620f5514f08SRobert Watson udp_notify); 621df8bae1dSRodney W. Grimes } 622df8bae1dSRodney W. Grimes 6230312fbe9SPoul-Henning Kamp static int 62482d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS) 62598271db4SGarrett Wollman { 626277afaffSRobert Watson int error, i, n; 62798271db4SGarrett Wollman struct inpcb *inp, **inp_list; 62898271db4SGarrett Wollman inp_gen_t gencnt; 62998271db4SGarrett Wollman struct xinpgen xig; 63098271db4SGarrett Wollman 63198271db4SGarrett Wollman /* 632f5514f08SRobert Watson * The process of preparing the PCB list is too time-consuming and 63398271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 63498271db4SGarrett Wollman */ 63598271db4SGarrett Wollman if (req->oldptr == 0) { 63698271db4SGarrett Wollman n = udbinfo.ipi_count; 63798271db4SGarrett Wollman req->oldidx = 2 * (sizeof xig) 63898271db4SGarrett Wollman + (n + n/8) * sizeof(struct xinpcb); 6393329b236SRobert Watson return (0); 64098271db4SGarrett Wollman } 64198271db4SGarrett Wollman 64298271db4SGarrett Wollman if (req->newptr != 0) 6433329b236SRobert Watson return (EPERM); 64498271db4SGarrett Wollman 64598271db4SGarrett Wollman /* 64698271db4SGarrett Wollman * OK, now we're committed to doing something. 64798271db4SGarrett Wollman */ 6484b40c56cSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 64998271db4SGarrett Wollman gencnt = udbinfo.ipi_gencnt; 65098271db4SGarrett Wollman n = udbinfo.ipi_count; 6514b40c56cSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 65298271db4SGarrett Wollman 65347934cefSDon Lewis error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 6545c38b6dbSDon Lewis + n * sizeof(struct xinpcb)); 65547934cefSDon Lewis if (error != 0) 65647934cefSDon Lewis return (error); 6575c38b6dbSDon Lewis 65898271db4SGarrett Wollman xig.xig_len = sizeof xig; 65998271db4SGarrett Wollman xig.xig_count = n; 66098271db4SGarrett Wollman xig.xig_gen = gencnt; 66198271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 66298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 66398271db4SGarrett Wollman if (error) 6643329b236SRobert Watson return (error); 66598271db4SGarrett Wollman 666a163d034SWarner Losh inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 66798271db4SGarrett Wollman if (inp_list == 0) 6683329b236SRobert Watson return (ENOMEM); 66998271db4SGarrett Wollman 670f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 671712fc218SRobert Watson for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n; 672fc2ffbe6SPoul-Henning Kamp inp = LIST_NEXT(inp, inp_list)) { 673f76fcf6dSJeffrey Hsu INP_LOCK(inp); 6742ded288cSJeffrey Hsu if (inp->inp_gencnt <= gencnt && 6752ded288cSJeffrey Hsu cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 67698271db4SGarrett Wollman inp_list[i++] = inp; 677f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 6784787fd37SPaul Saab } 679f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 68098271db4SGarrett Wollman n = i; 68198271db4SGarrett Wollman 68298271db4SGarrett Wollman error = 0; 68398271db4SGarrett Wollman for (i = 0; i < n; i++) { 68498271db4SGarrett Wollman inp = inp_list[i]; 685d915b280SStephan Uphoff INP_LOCK(inp); 68698271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 68798271db4SGarrett Wollman struct xinpcb xi; 688fd94099eSColin Percival bzero(&xi, sizeof(xi)); 68998271db4SGarrett Wollman xi.xi_len = sizeof xi; 69098271db4SGarrett Wollman /* XXX should avoid extra copy */ 69198271db4SGarrett Wollman bcopy(inp, &xi.xi_inp, sizeof *inp); 69298271db4SGarrett Wollman if (inp->inp_socket) 69398271db4SGarrett Wollman sotoxsocket(inp->inp_socket, &xi.xi_socket); 6944b40c56cSJeffrey Hsu xi.xi_inp.inp_gencnt = inp->inp_gencnt; 695d915b280SStephan Uphoff INP_UNLOCK(inp); 69698271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 697d915b280SStephan Uphoff } else 698d915b280SStephan Uphoff INP_UNLOCK(inp); 69998271db4SGarrett Wollman } 70098271db4SGarrett Wollman if (!error) { 70198271db4SGarrett Wollman /* 7023329b236SRobert Watson * Give the user an updated idea of our state. If the 7033329b236SRobert Watson * generation differs from what we told her before, she knows 7043329b236SRobert Watson * that something happened while we were processing this 7053329b236SRobert Watson * request, and it might be necessary to retry. 70698271db4SGarrett Wollman */ 707f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 70898271db4SGarrett Wollman xig.xig_gen = udbinfo.ipi_gencnt; 70998271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 71098271db4SGarrett Wollman xig.xig_count = udbinfo.ipi_count; 711f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 71298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 71398271db4SGarrett Wollman } 71498271db4SGarrett Wollman free(inp_list, M_TEMP); 7153329b236SRobert Watson return (error); 71698271db4SGarrett Wollman } 71798271db4SGarrett Wollman 71898271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 71998271db4SGarrett Wollman udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 72098271db4SGarrett Wollman 72198271db4SGarrett Wollman static int 72282d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS) 723490d50b6SBrian Feldman { 724c0511d3bSBrian Feldman struct xucred xuc; 725490d50b6SBrian Feldman struct sockaddr_in addrs[2]; 726490d50b6SBrian Feldman struct inpcb *inp; 727277afaffSRobert Watson int error; 728490d50b6SBrian Feldman 72932f9753cSRobert Watson error = priv_check(req->td, PRIV_NETINET_GETCRED); 730490d50b6SBrian Feldman if (error) 731490d50b6SBrian Feldman return (error); 732490d50b6SBrian Feldman error = SYSCTL_IN(req, addrs, sizeof(addrs)); 733490d50b6SBrian Feldman if (error) 734490d50b6SBrian Feldman return (error); 735f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 736490d50b6SBrian Feldman inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 737cfa1ca9dSYoshinobu Inoue addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 7382d20c83fSDon Lewis if (inp == NULL || inp->inp_socket == NULL) { 739490d50b6SBrian Feldman error = ENOENT; 740490d50b6SBrian Feldman goto out; 741490d50b6SBrian Feldman } 74229dc1288SRobert Watson error = cr_canseesocket(req->td->td_ucred, inp->inp_socket); 7437ce87f12SDavid Malone if (error) 7447ce87f12SDavid Malone goto out; 74576183f34SDima Dorfman cru2x(inp->inp_socket->so_cred, &xuc); 746490d50b6SBrian Feldman out: 747f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 7480e1eebb8SDon Lewis if (error == 0) 7490e1eebb8SDon Lewis error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 750490d50b6SBrian Feldman return (error); 751490d50b6SBrian Feldman } 752490d50b6SBrian Feldman 7537ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 7547ce87f12SDavid Malone CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 7557ce87f12SDavid Malone udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 756490d50b6SBrian Feldman 757490d50b6SBrian Feldman static int 7583329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, 7593329b236SRobert Watson struct mbuf *control, struct thread *td) 760df8bae1dSRodney W. Grimes { 7613329b236SRobert Watson struct udpiphdr *ui; 7623329b236SRobert Watson int len = m->m_pkthdr.len; 76390162a4eSIan Dowse struct in_addr faddr, laddr; 764c557ae16SIan Dowse struct cmsghdr *cm; 765c557ae16SIan Dowse struct sockaddr_in *sin, src; 76690162a4eSIan Dowse int error = 0; 7678afa2304SBruce M Simpson int ipflags; 76890162a4eSIan Dowse u_short fport, lport; 7695c32ea65SRobert Watson int unlock_udbinfo; 770df8bae1dSRodney W. Grimes 7715c32ea65SRobert Watson /* 7725c32ea65SRobert Watson * udp_output() may need to temporarily bind or connect the current 773f5514f08SRobert Watson * inpcb. As such, we don't know up front whether we will need the 774f5514f08SRobert Watson * pcbinfo lock or not. Do any work to decide what is needed up 775f5514f08SRobert Watson * front before acquiring any locks. 7765c32ea65SRobert Watson */ 777430d30d8SBill Fenner if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 778c557ae16SIan Dowse if (control) 779c557ae16SIan Dowse m_freem(control); 7805c32ea65SRobert Watson m_freem(m); 7813329b236SRobert Watson return (EMSGSIZE); 782430d30d8SBill Fenner } 783430d30d8SBill Fenner 7841b7f0384SBruce M Simpson src.sin_family = 0; 785c557ae16SIan Dowse if (control != NULL) { 786c557ae16SIan Dowse /* 7873329b236SRobert Watson * XXX: Currently, we assume all the optional information is 7883329b236SRobert Watson * stored in a single mbuf. 789c557ae16SIan Dowse */ 790c557ae16SIan Dowse if (control->m_next) { 791c557ae16SIan Dowse m_freem(control); 7925c32ea65SRobert Watson m_freem(m); 7933329b236SRobert Watson return (EINVAL); 794c557ae16SIan Dowse } 795c557ae16SIan Dowse for (; control->m_len > 0; 796c557ae16SIan Dowse control->m_data += CMSG_ALIGN(cm->cmsg_len), 797c557ae16SIan Dowse control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 798c557ae16SIan Dowse cm = mtod(control, struct cmsghdr *); 799af1ee11dSRobert Watson if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 800af1ee11dSRobert Watson || cm->cmsg_len > control->m_len) { 801c557ae16SIan Dowse error = EINVAL; 802c557ae16SIan Dowse break; 803c557ae16SIan Dowse } 804c557ae16SIan Dowse if (cm->cmsg_level != IPPROTO_IP) 805c557ae16SIan Dowse continue; 806c557ae16SIan Dowse 807c557ae16SIan Dowse switch (cm->cmsg_type) { 808c557ae16SIan Dowse case IP_SENDSRCADDR: 809c557ae16SIan Dowse if (cm->cmsg_len != 810c557ae16SIan Dowse CMSG_LEN(sizeof(struct in_addr))) { 811c557ae16SIan Dowse error = EINVAL; 812c557ae16SIan Dowse break; 813c557ae16SIan Dowse } 814c557ae16SIan Dowse bzero(&src, sizeof(src)); 815c557ae16SIan Dowse src.sin_family = AF_INET; 816c557ae16SIan Dowse src.sin_len = sizeof(src); 817c557ae16SIan Dowse src.sin_port = inp->inp_lport; 818af1ee11dSRobert Watson src.sin_addr = 819af1ee11dSRobert Watson *(struct in_addr *)CMSG_DATA(cm); 820c557ae16SIan Dowse break; 821af1ee11dSRobert Watson 822c557ae16SIan Dowse default: 823c557ae16SIan Dowse error = ENOPROTOOPT; 824c557ae16SIan Dowse break; 825c557ae16SIan Dowse } 826c557ae16SIan Dowse if (error) 827c557ae16SIan Dowse break; 828c557ae16SIan Dowse } 829c557ae16SIan Dowse m_freem(control); 830c557ae16SIan Dowse } 8315c32ea65SRobert Watson if (error) { 8325c32ea65SRobert Watson m_freem(m); 8333329b236SRobert Watson return (error); 8345c32ea65SRobert Watson } 8355c32ea65SRobert Watson 8361b7f0384SBruce M Simpson if (src.sin_family == AF_INET || addr != NULL) { 8375c32ea65SRobert Watson INP_INFO_WLOCK(&udbinfo); 8385c32ea65SRobert Watson unlock_udbinfo = 1; 8395c32ea65SRobert Watson } else 8405c32ea65SRobert Watson unlock_udbinfo = 0; 8415c32ea65SRobert Watson INP_LOCK(inp); 8425c32ea65SRobert Watson 8435c32ea65SRobert Watson #ifdef MAC 8445c32ea65SRobert Watson mac_create_mbuf_from_inpcb(inp, m); 8455c32ea65SRobert Watson #endif 8465c32ea65SRobert Watson 8471b7f0384SBruce M Simpson /* 8481b7f0384SBruce M Simpson * If the IP_SENDSRCADDR control message was specified, override the 8491b7f0384SBruce M Simpson * source address for this datagram. Its use is invalidated if the 8501b7f0384SBruce M Simpson * address thus specified is incomplete or clobbers other inpcbs. 8511b7f0384SBruce M Simpson */ 85290162a4eSIan Dowse laddr = inp->inp_laddr; 85390162a4eSIan Dowse lport = inp->inp_lport; 8541b7f0384SBruce M Simpson if (src.sin_family == AF_INET) { 8551b7f0384SBruce M Simpson if ((lport == 0) || 8561b7f0384SBruce M Simpson (laddr.s_addr == INADDR_ANY && 8571b7f0384SBruce M Simpson src.sin_addr.s_addr == INADDR_ANY)) { 858c557ae16SIan Dowse error = EINVAL; 859c557ae16SIan Dowse goto release; 860c557ae16SIan Dowse } 861c557ae16SIan Dowse error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 862b0330ed9SPawel Jakub Dawidek &laddr.s_addr, &lport, td->td_ucred); 863c557ae16SIan Dowse if (error) 864c557ae16SIan Dowse goto release; 865c557ae16SIan Dowse } 866c557ae16SIan Dowse 867df8bae1dSRodney W. Grimes if (addr) { 86875c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)addr; 869812d8653SSam Leffler if (jailed(td->td_ucred)) 8703329b236SRobert Watson prison_remote_ip(td->td_ucred, 0, 8713329b236SRobert Watson &sin->sin_addr.s_addr); 872df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 873df8bae1dSRodney W. Grimes error = EISCONN; 874df8bae1dSRodney W. Grimes goto release; 875df8bae1dSRodney W. Grimes } 87690162a4eSIan Dowse error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport, 877b0330ed9SPawel Jakub Dawidek &faddr.s_addr, &fport, NULL, td->td_ucred); 87890162a4eSIan Dowse if (error) 87990162a4eSIan Dowse goto release; 88090162a4eSIan Dowse 88190162a4eSIan Dowse /* Commit the local port if newly assigned. */ 88290162a4eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && 88390162a4eSIan Dowse inp->inp_lport == 0) { 8843a1757b9SGleb Smirnoff /* 8853a1757b9SGleb Smirnoff * Remember addr if jailed, to prevent rebinding. 8863a1757b9SGleb Smirnoff */ 8873a1757b9SGleb Smirnoff if (jailed(td->td_ucred)) 8883a1757b9SGleb Smirnoff inp->inp_laddr = laddr; 88990162a4eSIan Dowse inp->inp_lport = lport; 89090162a4eSIan Dowse if (in_pcbinshash(inp) != 0) { 89190162a4eSIan Dowse inp->inp_lport = 0; 89290162a4eSIan Dowse error = EAGAIN; 893df8bae1dSRodney W. Grimes goto release; 894df8bae1dSRodney W. Grimes } 89590162a4eSIan Dowse inp->inp_flags |= INP_ANONPORT; 89690162a4eSIan Dowse } 897df8bae1dSRodney W. Grimes } else { 89890162a4eSIan Dowse faddr = inp->inp_faddr; 89990162a4eSIan Dowse fport = inp->inp_fport; 90090162a4eSIan Dowse if (faddr.s_addr == INADDR_ANY) { 901df8bae1dSRodney W. Grimes error = ENOTCONN; 902df8bae1dSRodney W. Grimes goto release; 903df8bae1dSRodney W. Grimes } 904df8bae1dSRodney W. Grimes } 905e6ccd709SRobert Watson 906df8bae1dSRodney W. Grimes /* 907e6ccd709SRobert Watson * Calculate data length and get a mbuf for UDP, IP, and possible 908392e8407SRobert Watson * link-layer headers. Immediate slide the data pointer back forward 909392e8407SRobert Watson * since we won't use that space at this layer. 910df8bae1dSRodney W. Grimes */ 911e6ccd709SRobert Watson M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT); 912e6ccd709SRobert Watson if (m == NULL) { 913df8bae1dSRodney W. Grimes error = ENOBUFS; 91449b19bfcSBruce M Simpson goto release; 915df8bae1dSRodney W. Grimes } 916e6ccd709SRobert Watson m->m_data += max_linkhdr; 917e6ccd709SRobert Watson m->m_len -= max_linkhdr; 918392e8407SRobert Watson m->m_pkthdr.len -= max_linkhdr; 919df8bae1dSRodney W. Grimes 920df8bae1dSRodney W. Grimes /* 9213329b236SRobert Watson * Fill in mbuf with extended UDP header and addresses and length put 9223329b236SRobert Watson * into network format. 923df8bae1dSRodney W. Grimes */ 924df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 925db4f9cc7SJonathan Lemon bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 926df8bae1dSRodney W. Grimes ui->ui_pr = IPPROTO_UDP; 92790162a4eSIan Dowse ui->ui_src = laddr; 92890162a4eSIan Dowse ui->ui_dst = faddr; 92990162a4eSIan Dowse ui->ui_sport = lport; 93090162a4eSIan Dowse ui->ui_dport = fport; 931db4f9cc7SJonathan Lemon ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 932df8bae1dSRodney W. Grimes 933b2828ad2SAndre Oppermann /* 934b2828ad2SAndre Oppermann * Set the Don't Fragment bit in the IP header. 935b2828ad2SAndre Oppermann */ 936b2828ad2SAndre Oppermann if (inp->inp_flags & INP_DONTFRAG) { 937b2828ad2SAndre Oppermann struct ip *ip; 9383329b236SRobert Watson 939b2828ad2SAndre Oppermann ip = (struct ip *)&ui->ui_i; 940b2828ad2SAndre Oppermann ip->ip_off |= IP_DF; 941b2828ad2SAndre Oppermann } 942b2828ad2SAndre Oppermann 943b5d47ff5SJohn-Mark Gurney ipflags = 0; 944b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_DONTROUTE) 945b5d47ff5SJohn-Mark Gurney ipflags |= IP_ROUTETOIF; 946b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_BROADCAST) 947b5d47ff5SJohn-Mark Gurney ipflags |= IP_ALLOWBROADCAST; 9486fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 9498afa2304SBruce M Simpson ipflags |= IP_SENDONES; 9508afa2304SBruce M Simpson 951df8bae1dSRodney W. Grimes /* 952db4f9cc7SJonathan Lemon * Set up checksum and output datagram. 953df8bae1dSRodney W. Grimes */ 954f5514f08SRobert Watson if (udp_cksum) { 9556fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 9568a538743SBruce M Simpson faddr.s_addr = INADDR_BROADCAST; 9578a538743SBruce M Simpson ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 958db4f9cc7SJonathan Lemon htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 959db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_UDP; 960db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 9613329b236SRobert Watson } else 962db4f9cc7SJonathan Lemon ui->ui_sum = 0; 963df8bae1dSRodney W. Grimes ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 964ca98b82cSDavid Greenman ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 965ca98b82cSDavid Greenman ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 966df8bae1dSRodney W. Grimes udpstat.udps_opackets++; 967cfa1ca9dSYoshinobu Inoue 9685c32ea65SRobert Watson if (unlock_udbinfo) 9695c32ea65SRobert Watson INP_INFO_WUNLOCK(&udbinfo); 97097d8d152SAndre Oppermann error = ip_output(m, inp->inp_options, NULL, ipflags, 9715d846453SSam Leffler inp->inp_moptions, inp); 9725c32ea65SRobert Watson INP_UNLOCK(inp); 973df8bae1dSRodney W. Grimes return (error); 974df8bae1dSRodney W. Grimes 975df8bae1dSRodney W. Grimes release: 9765c32ea65SRobert Watson INP_UNLOCK(inp); 9775c32ea65SRobert Watson if (unlock_udbinfo) 9785c32ea65SRobert Watson INP_INFO_WUNLOCK(&udbinfo); 979df8bae1dSRodney W. Grimes m_freem(m); 980df8bae1dSRodney W. Grimes return (error); 981df8bae1dSRodney W. Grimes } 982df8bae1dSRodney W. Grimes 983ac45e92fSRobert Watson static void 984d0390e05SGarrett Wollman udp_abort(struct socket *so) 985df8bae1dSRodney W. Grimes { 986d0390e05SGarrett Wollman struct inpcb *inp; 987df8bae1dSRodney W. Grimes 988d0390e05SGarrett Wollman inp = sotoinpcb(so); 98914ba8addSRobert Watson KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 99014ba8addSRobert Watson INP_INFO_WLOCK(&udbinfo); 991f76fcf6dSJeffrey Hsu INP_LOCK(inp); 992a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 993a152f8a3SRobert Watson in_pcbdisconnect(inp); 994a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 995d0390e05SGarrett Wollman soisdisconnected(so); 996a152f8a3SRobert Watson } 997a152f8a3SRobert Watson INP_UNLOCK(inp); 998f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 999df8bae1dSRodney W. Grimes } 1000df8bae1dSRodney W. Grimes 1001d0390e05SGarrett Wollman static int 1002b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td) 1003d0390e05SGarrett Wollman { 1004d0390e05SGarrett Wollman struct inpcb *inp; 1005277afaffSRobert Watson int error; 1006d0390e05SGarrett Wollman 1007d0390e05SGarrett Wollman inp = sotoinpcb(so); 100814ba8addSRobert Watson KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 1009cfa1ca9dSYoshinobu Inoue error = soreserve(so, udp_sendspace, udp_recvspace); 1010f24618aaSRobert Watson if (error) 10113329b236SRobert Watson return (error); 1012f24618aaSRobert Watson INP_INFO_WLOCK(&udbinfo); 1013d915b280SStephan Uphoff error = in_pcballoc(so, &udbinfo); 101453b57cd1SSam Leffler if (error) { 101553b57cd1SSam Leffler INP_INFO_WUNLOCK(&udbinfo); 10163329b236SRobert Watson return (error); 101753b57cd1SSam Leffler } 1018cfa1ca9dSYoshinobu Inoue 1019cfa1ca9dSYoshinobu Inoue inp = (struct inpcb *)so->so_pcb; 1020f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 1021cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 1022cfa1ca9dSYoshinobu Inoue inp->inp_ip_ttl = ip_defttl; 1023f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 10243329b236SRobert Watson return (0); 1025df8bae1dSRodney W. Grimes } 1026d0390e05SGarrett Wollman 1027d0390e05SGarrett Wollman static int 1028b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1029d0390e05SGarrett Wollman { 1030d0390e05SGarrett Wollman struct inpcb *inp; 1031277afaffSRobert Watson int error; 1032d0390e05SGarrett Wollman 1033d0390e05SGarrett Wollman inp = sotoinpcb(so); 103414ba8addSRobert Watson KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 103514ba8addSRobert Watson INP_INFO_WLOCK(&udbinfo); 1036f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1037b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, nam, td->td_ucred); 1038f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1039f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 10403329b236SRobert Watson return (error); 1041d0390e05SGarrett Wollman } 1042d0390e05SGarrett Wollman 1043a152f8a3SRobert Watson static void 1044a152f8a3SRobert Watson udp_close(struct socket *so) 1045a152f8a3SRobert Watson { 1046a152f8a3SRobert Watson struct inpcb *inp; 1047a152f8a3SRobert Watson 1048a152f8a3SRobert Watson inp = sotoinpcb(so); 1049a152f8a3SRobert Watson KASSERT(inp != NULL, ("udp_close: inp == NULL")); 1050a152f8a3SRobert Watson INP_INFO_WLOCK(&udbinfo); 1051a152f8a3SRobert Watson INP_LOCK(inp); 1052a152f8a3SRobert Watson if (inp->inp_faddr.s_addr != INADDR_ANY) { 1053a152f8a3SRobert Watson in_pcbdisconnect(inp); 1054a152f8a3SRobert Watson inp->inp_laddr.s_addr = INADDR_ANY; 1055a152f8a3SRobert Watson soisdisconnected(so); 1056a152f8a3SRobert Watson } 1057a152f8a3SRobert Watson INP_UNLOCK(inp); 1058a152f8a3SRobert Watson INP_INFO_WUNLOCK(&udbinfo); 1059a152f8a3SRobert Watson } 1060a152f8a3SRobert Watson 1061d0390e05SGarrett Wollman static int 1062b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1063d0390e05SGarrett Wollman { 1064d0390e05SGarrett Wollman struct inpcb *inp; 1065277afaffSRobert Watson int error; 106675c13541SPoul-Henning Kamp struct sockaddr_in *sin; 1067d0390e05SGarrett Wollman 1068d0390e05SGarrett Wollman inp = sotoinpcb(so); 106914ba8addSRobert Watson KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 107014ba8addSRobert Watson INP_INFO_WLOCK(&udbinfo); 1071f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1072f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr != INADDR_ANY) { 1073f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1074f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 10753329b236SRobert Watson return (EISCONN); 1076f76fcf6dSJeffrey Hsu } 107775c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)nam; 1078812d8653SSam Leffler if (jailed(td->td_ucred)) 1079a854ed98SJohn Baldwin prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr); 1080b0330ed9SPawel Jakub Dawidek error = in_pcbconnect(inp, nam, td->td_ucred); 10814cc20ab1SSeigo Tanimura if (error == 0) 1082df8bae1dSRodney W. Grimes soisconnected(so); 1083f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1084f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 10853329b236SRobert Watson return (error); 1086df8bae1dSRodney W. Grimes } 1087d0390e05SGarrett Wollman 1088bc725eafSRobert Watson static void 1089d0390e05SGarrett Wollman udp_detach(struct socket *so) 1090d0390e05SGarrett Wollman { 1091d0390e05SGarrett Wollman struct inpcb *inp; 1092d0390e05SGarrett Wollman 1093d0390e05SGarrett Wollman inp = sotoinpcb(so); 109414ba8addSRobert Watson KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1095a152f8a3SRobert Watson KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1096a152f8a3SRobert Watson ("udp_detach: not disconnected")); 109714ba8addSRobert Watson INP_INFO_WLOCK(&udbinfo); 1098f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1099d0390e05SGarrett Wollman in_pcbdetach(inp); 110014ba8addSRobert Watson in_pcbfree(inp); 1101f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 1102d0390e05SGarrett Wollman } 1103d0390e05SGarrett Wollman 1104d0390e05SGarrett Wollman static int 1105d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 1106d0390e05SGarrett Wollman { 1107d0390e05SGarrett Wollman struct inpcb *inp; 1108d0390e05SGarrett Wollman 1109d0390e05SGarrett Wollman inp = sotoinpcb(so); 111014ba8addSRobert Watson KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 111114ba8addSRobert Watson INP_INFO_WLOCK(&udbinfo); 1112f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1113f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr == INADDR_ANY) { 1114f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 1115f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 11163329b236SRobert Watson return (ENOTCONN); 1117f76fcf6dSJeffrey Hsu } 1118d0390e05SGarrett Wollman 1119df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 1120df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 1121d45e4f99SMaxim Konovalov SOCK_LOCK(so); 1122d45e4f99SMaxim Konovalov so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1123d45e4f99SMaxim Konovalov SOCK_UNLOCK(so); 1124f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1125f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 11263329b236SRobert Watson return (0); 1127df8bae1dSRodney W. Grimes } 1128df8bae1dSRodney W. Grimes 1129d0390e05SGarrett Wollman static int 113057bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1131b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1132d0390e05SGarrett Wollman { 1133d0390e05SGarrett Wollman struct inpcb *inp; 1134d0390e05SGarrett Wollman 1135d0390e05SGarrett Wollman inp = sotoinpcb(so); 113614ba8addSRobert Watson KASSERT(inp != NULL, ("udp_send: inp == NULL")); 11373329b236SRobert Watson return (udp_output(inp, m, addr, control, td)); 1138d0390e05SGarrett Wollman } 1139d0390e05SGarrett Wollman 114076429de4SYoshinobu Inoue int 1141d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 1142d0390e05SGarrett Wollman { 1143d0390e05SGarrett Wollman struct inpcb *inp; 1144d0390e05SGarrett Wollman 1145d0390e05SGarrett Wollman inp = sotoinpcb(so); 114614ba8addSRobert Watson KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 1147f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1148d0390e05SGarrett Wollman socantsendmore(so); 1149f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 11503329b236SRobert Watson return (0); 1151d0390e05SGarrett Wollman } 1152d0390e05SGarrett Wollman 1153d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 1154756d52a1SPoul-Henning Kamp .pru_abort = udp_abort, 1155756d52a1SPoul-Henning Kamp .pru_attach = udp_attach, 1156756d52a1SPoul-Henning Kamp .pru_bind = udp_bind, 1157756d52a1SPoul-Henning Kamp .pru_connect = udp_connect, 1158756d52a1SPoul-Henning Kamp .pru_control = in_control, 1159756d52a1SPoul-Henning Kamp .pru_detach = udp_detach, 1160756d52a1SPoul-Henning Kamp .pru_disconnect = udp_disconnect, 116154d642bbSRobert Watson .pru_peeraddr = in_getpeeraddr, 1162756d52a1SPoul-Henning Kamp .pru_send = udp_send, 116359b8854eSRobert Watson .pru_sosend = sosend_dgram, 1164756d52a1SPoul-Henning Kamp .pru_shutdown = udp_shutdown, 116554d642bbSRobert Watson .pru_sockaddr = in_getsockaddr, 1166a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 1167a152f8a3SRobert Watson .pru_close = udp_close, 1168d0390e05SGarrett Wollman }; 1169