1c398230bSWarner Losh /*- 26dfab5b1SGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 296dfab5b1SGarrett Wollman * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 336a800098SYoshinobu Inoue #include "opt_ipsec.h" 34cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 35bdb3fa18SRobert Watson #include "opt_mac.h" 36cfa1ca9dSYoshinobu Inoue 37df8bae1dSRodney W. Grimes #include <sys/param.h> 3826f9a767SRodney W. Grimes #include <sys/systm.h> 39960ed29cSSeigo Tanimura #include <sys/domain.h> 40960ed29cSSeigo Tanimura #include <sys/jail.h> 41b110a8a2SGarrett Wollman #include <sys/kernel.h> 42960ed29cSSeigo Tanimura #include <sys/lock.h> 43bdb3fa18SRobert Watson #include <sys/mac.h> 44df8bae1dSRodney W. Grimes #include <sys/malloc.h> 45df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 46490d50b6SBrian Feldman #include <sys/proc.h> 47df8bae1dSRodney W. Grimes #include <sys/protosw.h> 48960ed29cSSeigo Tanimura #include <sys/signalvar.h> 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51960ed29cSSeigo Tanimura #include <sys/sx.h> 52b5e8ce9fSBruce Evans #include <sys/sysctl.h> 53816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 548781d8e9SBruce Evans 5569c2d429SJeff Roberson #include <vm/uma.h> 56df8bae1dSRodney W. Grimes 57df8bae1dSRodney W. Grimes #include <net/if.h> 58df8bae1dSRodney W. Grimes #include <net/route.h> 59df8bae1dSRodney W. Grimes 60df8bae1dSRodney W. Grimes #include <netinet/in.h> 61df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 62960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 63960ed29cSSeigo Tanimura #include <netinet/in_var.h> 64df8bae1dSRodney W. Grimes #include <netinet/ip.h> 65cfa1ca9dSYoshinobu Inoue #ifdef INET6 66cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 67cfa1ca9dSYoshinobu Inoue #endif 68960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h> 69960ed29cSSeigo Tanimura #include <netinet/icmp_var.h> 70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 71cfa1ca9dSYoshinobu Inoue #ifdef INET6 72cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 73cfa1ca9dSYoshinobu Inoue #endif 74df8bae1dSRodney W. Grimes #include <netinet/udp.h> 75df8bae1dSRodney W. Grimes #include <netinet/udp_var.h> 76df8bae1dSRodney W. Grimes 77b9234fafSSam Leffler #ifdef FAST_IPSEC 78b9234fafSSam Leffler #include <netipsec/ipsec.h> 79b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 80b9234fafSSam Leffler 81cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 82cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h> 83cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 84cfa1ca9dSYoshinobu Inoue 85db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 86db4f9cc7SJonathan Lemon 87df8bae1dSRodney W. Grimes /* 88df8bae1dSRodney W. Grimes * UDP protocol implementation. 89df8bae1dSRodney W. Grimes * Per RFC 768, August, 1980. 90df8bae1dSRodney W. Grimes */ 91df8bae1dSRodney W. Grimes #ifndef COMPAT_42 920312fbe9SPoul-Henning Kamp static int udpcksum = 1; 93df8bae1dSRodney W. Grimes #else 940312fbe9SPoul-Henning Kamp static int udpcksum = 0; /* XXX */ 95df8bae1dSRodney W. Grimes #endif 960312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 970312fbe9SPoul-Henning Kamp &udpcksum, 0, ""); 98df8bae1dSRodney W. Grimes 99032dcc76SLuigi Rizzo int log_in_vain = 0; 100816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 1013d177f46SBill Fumerola &log_in_vain, 0, "Log all incoming UDP packets"); 102816a3d83SPoul-Henning Kamp 103032dcc76SLuigi Rizzo static int blackhole = 0; 10416f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, 10516f7f31fSGeoff Rehmet &blackhole, 0, "Do not send port unreachables for refused connects"); 10616f7f31fSGeoff Rehmet 10783453a06SBruce M Simpson static int strict_mcast_mship = 0; 10883453a06SBruce M Simpson SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW, 10983453a06SBruce M Simpson &strict_mcast_mship, 0, "Only send multicast to member sockets"); 11083453a06SBruce M Simpson 11176429de4SYoshinobu Inoue struct inpcbhead udb; /* from udp_var.h */ 112cfa1ca9dSYoshinobu Inoue #define udb6 udb /* for KAME src sync over BSD*'s */ 1137a2aab80SBrian Feldman struct inpcbinfo udbinfo; 11415bd2b43SDavid Greenman 11515bd2b43SDavid Greenman #ifndef UDBHASHSIZE 116c3229e05SDavid Greenman #define UDBHASHSIZE 16 11715bd2b43SDavid Greenman #endif 11815bd2b43SDavid Greenman 11976429de4SYoshinobu Inoue struct udpstat udpstat; /* from udp_var.h */ 120c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, 1213d177f46SBill Fumerola &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 122f2ea20e6SGarrett Wollman 123c1cd65baSBruce Evans static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, 124d4b509bdSRobert Watson int off, struct sockaddr_in *udp_in); 125cfa1ca9dSYoshinobu Inoue 1264d77a549SAlfred Perlstein static int udp_detach(struct socket *so); 1274d77a549SAlfred Perlstein static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 1284d77a549SAlfred Perlstein struct mbuf *, struct thread *); 129df8bae1dSRodney W. Grimes 130df8bae1dSRodney W. Grimes void 131032dcc76SLuigi Rizzo udp_init() 132df8bae1dSRodney W. Grimes { 133f76fcf6dSJeffrey Hsu INP_INFO_LOCK_INIT(&udbinfo, "udp"); 13415bd2b43SDavid Greenman LIST_INIT(&udb); 13515bd2b43SDavid Greenman udbinfo.listhead = &udb; 136ddd79a97SDavid Greenman udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 1378781d8e9SBruce Evans udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 1388781d8e9SBruce Evans &udbinfo.porthashmask); 13969c2d429SJeff Roberson udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL, 140420a2811SAndre Oppermann NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 14169c2d429SJeff Roberson uma_zone_set_max(udbinfo.ipi_zone, maxsockets); 142df8bae1dSRodney W. Grimes } 143df8bae1dSRodney W. Grimes 144df8bae1dSRodney W. Grimes void 145032dcc76SLuigi Rizzo udp_input(m, off) 146032dcc76SLuigi Rizzo register struct mbuf *m; 147032dcc76SLuigi Rizzo int off; 148df8bae1dSRodney W. Grimes { 149cfa1ca9dSYoshinobu Inoue int iphlen = off; 150032dcc76SLuigi Rizzo register struct ip *ip; 151032dcc76SLuigi Rizzo register struct udphdr *uh; 152032dcc76SLuigi Rizzo register struct inpcb *inp; 153df8bae1dSRodney W. Grimes struct mbuf *opts = 0; 154df8bae1dSRodney W. Grimes int len; 155df8bae1dSRodney W. Grimes struct ip save_ip; 156d4b509bdSRobert Watson struct sockaddr_in udp_in; 157df8bae1dSRodney W. Grimes 158df8bae1dSRodney W. Grimes udpstat.udps_ipackets++; 159df8bae1dSRodney W. Grimes 160df8bae1dSRodney W. Grimes /* 161df8bae1dSRodney W. Grimes * Strip IP options, if any; should skip this, 162df8bae1dSRodney W. Grimes * make available to user, and use on returned packets, 163df8bae1dSRodney W. Grimes * but we don't yet have a way to check the checksum 164df8bae1dSRodney W. Grimes * with options still present. 165df8bae1dSRodney W. Grimes */ 166df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 167df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 168df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 169df8bae1dSRodney W. Grimes } 170df8bae1dSRodney W. Grimes 171df8bae1dSRodney W. Grimes /* 172df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 173df8bae1dSRodney W. Grimes */ 174df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 175df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 176df8bae1dSRodney W. Grimes if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 177df8bae1dSRodney W. Grimes udpstat.udps_hdrops++; 178df8bae1dSRodney W. Grimes return; 179df8bae1dSRodney W. Grimes } 180df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 181df8bae1dSRodney W. Grimes } 182df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 183df8bae1dSRodney W. Grimes 184686cdd19SJun-ichiro itojun Hagino /* destination port of 0 is illegal, based on RFC768. */ 185686cdd19SJun-ichiro itojun Hagino if (uh->uh_dport == 0) 186f76fcf6dSJeffrey Hsu goto badunlocked; 187686cdd19SJun-ichiro itojun Hagino 188df8bae1dSRodney W. Grimes /* 189b9234fafSSam Leffler * Construct sockaddr format source address. 190b9234fafSSam Leffler * Stuff source address and datagram in user buffer. 191b9234fafSSam Leffler */ 192d4b509bdSRobert Watson bzero(&udp_in, sizeof(udp_in)); 193d4b509bdSRobert Watson udp_in.sin_len = sizeof(udp_in); 194d4b509bdSRobert Watson udp_in.sin_family = AF_INET; 195b9234fafSSam Leffler udp_in.sin_port = uh->uh_sport; 196b9234fafSSam Leffler udp_in.sin_addr = ip->ip_src; 197b9234fafSSam Leffler 198b9234fafSSam Leffler /* 199df8bae1dSRodney W. Grimes * Make mbuf data length reflect UDP length. 200df8bae1dSRodney W. Grimes * If not enough data to reflect UDP length, drop. 201df8bae1dSRodney W. Grimes */ 202df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 203df8bae1dSRodney W. Grimes if (ip->ip_len != len) { 2047eb7a449SAndras Olah if (len > ip->ip_len || len < sizeof(struct udphdr)) { 205df8bae1dSRodney W. Grimes udpstat.udps_badlen++; 206f76fcf6dSJeffrey Hsu goto badunlocked; 207df8bae1dSRodney W. Grimes } 208df8bae1dSRodney W. Grimes m_adj(m, len - ip->ip_len); 209df8bae1dSRodney W. Grimes /* ip->ip_len = len; */ 210df8bae1dSRodney W. Grimes } 211df8bae1dSRodney W. Grimes /* 212df8bae1dSRodney W. Grimes * Save a copy of the IP header in case we want restore it 213df8bae1dSRodney W. Grimes * for sending an ICMP error message in response. 214df8bae1dSRodney W. Grimes */ 21548cb400fSRuslan Ermilov if (!blackhole) 216df8bae1dSRodney W. Grimes save_ip = *ip; 217df8bae1dSRodney W. Grimes 218df8bae1dSRodney W. Grimes /* 219df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 220df8bae1dSRodney W. Grimes */ 2216dfab5b1SGarrett Wollman if (uh->uh_sum) { 222db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 223db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 224db4f9cc7SJonathan Lemon uh->uh_sum = m->m_pkthdr.csum_data; 225db4f9cc7SJonathan Lemon else 226db4f9cc7SJonathan Lemon uh->uh_sum = in_pseudo(ip->ip_src.s_addr, 227506f4949SRuslan Ermilov ip->ip_dst.s_addr, htonl((u_short)len + 228db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data + IPPROTO_UDP)); 229db4f9cc7SJonathan Lemon uh->uh_sum ^= 0xffff; 230db4f9cc7SJonathan Lemon } else { 231cb342100SHajimu UMEMOTO char b[9]; 232cb342100SHajimu UMEMOTO bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 2336effc713SDoug Rabson bzero(((struct ipovly *)ip)->ih_x1, 9); 234df8bae1dSRodney W. Grimes ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 235623ae52eSPoul-Henning Kamp uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 236cb342100SHajimu UMEMOTO bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 237db4f9cc7SJonathan Lemon } 238623ae52eSPoul-Henning Kamp if (uh->uh_sum) { 239df8bae1dSRodney W. Grimes udpstat.udps_badsum++; 240df8bae1dSRodney W. Grimes m_freem(m); 241df8bae1dSRodney W. Grimes return; 242df8bae1dSRodney W. Grimes } 243fb9aaba0SRuslan Ermilov } else 244fb9aaba0SRuslan Ermilov udpstat.udps_nosum++; 245df8bae1dSRodney W. Grimes 246f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 247f76fcf6dSJeffrey Hsu 248df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 249df8bae1dSRodney W. Grimes in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 25082c23ebaSBill Fenner struct inpcb *last; 251df8bae1dSRodney W. Grimes /* 252df8bae1dSRodney W. Grimes * Deliver a multicast or broadcast datagram to *all* sockets 253df8bae1dSRodney W. Grimes * for which the local and remote addresses and ports match 254df8bae1dSRodney W. Grimes * those of the incoming datagram. This allows more than 255df8bae1dSRodney W. Grimes * one process to receive multi/broadcasts on the same port. 256df8bae1dSRodney W. Grimes * (This really ought to be done for unicast datagrams as 257df8bae1dSRodney W. Grimes * well, but that would cause problems with existing 258df8bae1dSRodney W. Grimes * applications that open both address-specific sockets and 259df8bae1dSRodney W. Grimes * a wildcard socket listening to the same port -- they would 260df8bae1dSRodney W. Grimes * end up receiving duplicates of every unicast datagram. 261df8bae1dSRodney W. Grimes * Those applications open the multiple sockets to overcome an 262df8bae1dSRodney W. Grimes * inadequacy of the UDP socket interface, but for backwards 263df8bae1dSRodney W. Grimes * compatibility we avoid the problem here rather than 264df8bae1dSRodney W. Grimes * fixing the interface. Maybe 4.5BSD will remedy this?) 265df8bae1dSRodney W. Grimes */ 266df8bae1dSRodney W. Grimes 267df8bae1dSRodney W. Grimes /* 268df8bae1dSRodney W. Grimes * Locate pcb(s) for datagram. 269df8bae1dSRodney W. Grimes * (Algorithm copied from raw_intr().) 270df8bae1dSRodney W. Grimes */ 271df8bae1dSRodney W. Grimes last = NULL; 272cfa1ca9dSYoshinobu Inoue LIST_FOREACH(inp, &udb, inp_list) { 2739c1df695SRobert Watson if (inp->inp_lport != uh->uh_dport) 274f76fcf6dSJeffrey Hsu continue; 275cfa1ca9dSYoshinobu Inoue #ifdef INET6 276369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 2779c1df695SRobert Watson continue; 278cfa1ca9dSYoshinobu Inoue #endif 279df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr != INADDR_ANY) { 280f76fcf6dSJeffrey Hsu if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 2819c1df695SRobert Watson continue; 282df8bae1dSRodney W. Grimes } 283df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 284df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != 285df8bae1dSRodney W. Grimes ip->ip_src.s_addr || 286df8bae1dSRodney W. Grimes inp->inp_fport != uh->uh_sport) 2879c1df695SRobert Watson continue; 288df8bae1dSRodney W. Grimes } 2899c1df695SRobert Watson INP_LOCK(inp); 290df8bae1dSRodney W. Grimes 29183453a06SBruce M Simpson /* 29283453a06SBruce M Simpson * Check multicast packets to make sure they are only 29383453a06SBruce M Simpson * sent to sockets with multicast memberships for the 29483453a06SBruce M Simpson * packet's destination address and arrival interface 29583453a06SBruce M Simpson */ 29683453a06SBruce M Simpson #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)]) 29783453a06SBruce M Simpson #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships) 29883453a06SBruce M Simpson if (strict_mcast_mship && inp->inp_moptions != NULL) { 29983453a06SBruce M Simpson int mship, foundmship = 0; 30083453a06SBruce M Simpson 30183453a06SBruce M Simpson for (mship = 0; mship < NMSHIPS(inp); mship++) { 30283453a06SBruce M Simpson if (MSHIP(inp, mship)->inm_addr.s_addr 30383453a06SBruce M Simpson == ip->ip_dst.s_addr && 30483453a06SBruce M Simpson MSHIP(inp, mship)->inm_ifp 30583453a06SBruce M Simpson == m->m_pkthdr.rcvif) { 30683453a06SBruce M Simpson foundmship = 1; 30783453a06SBruce M Simpson break; 30883453a06SBruce M Simpson } 30983453a06SBruce M Simpson } 3109c1df695SRobert Watson if (foundmship == 0) { 3119c1df695SRobert Watson INP_UNLOCK(inp); 3129c1df695SRobert Watson continue; 3139c1df695SRobert Watson } 31483453a06SBruce M Simpson } 31583453a06SBruce M Simpson #undef NMSHIPS 31683453a06SBruce M Simpson #undef MSHIP 317df8bae1dSRodney W. Grimes if (last != NULL) { 318df8bae1dSRodney W. Grimes struct mbuf *n; 319df8bae1dSRodney W. Grimes 320032dcc76SLuigi Rizzo n = m_copy(m, 0, M_COPYALL); 321365433d9SRobert Watson if (n != NULL) 322cfa1ca9dSYoshinobu Inoue udp_append(last, ip, n, 323cfa1ca9dSYoshinobu Inoue iphlen + 324d4b509bdSRobert Watson sizeof(struct udphdr), 325d4b509bdSRobert Watson &udp_in); 326f76fcf6dSJeffrey Hsu INP_UNLOCK(last); 327df8bae1dSRodney W. Grimes } 32882c23ebaSBill Fenner last = inp; 329df8bae1dSRodney W. Grimes /* 330df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 331df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 332df8bae1dSRodney W. Grimes * socket options set. This heuristic avoids searching 333df8bae1dSRodney W. Grimes * through all pcbs in the common case of a non-shared 334df8bae1dSRodney W. Grimes * port. It * assumes that an application will never 335df8bae1dSRodney W. Grimes * clear these options after setting them. 336df8bae1dSRodney W. Grimes */ 3374cc20ab1SSeigo Tanimura if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 338df8bae1dSRodney W. Grimes break; 339df8bae1dSRodney W. Grimes } 340df8bae1dSRodney W. Grimes 341df8bae1dSRodney W. Grimes if (last == NULL) { 342df8bae1dSRodney W. Grimes /* 343df8bae1dSRodney W. Grimes * No matching pcb found; discard datagram. 344df8bae1dSRodney W. Grimes * (No need to send an ICMP Port Unreachable 345df8bae1dSRodney W. Grimes * for a broadcast or multicast datgram.) 346df8bae1dSRodney W. Grimes */ 347df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 34861ffc0b1SJeffrey Hsu goto badheadlocked; 349df8bae1dSRodney W. Grimes } 350d4b509bdSRobert Watson udp_append(last, ip, m, iphlen + sizeof(struct udphdr), 351d4b509bdSRobert Watson &udp_in); 3529b657230SSam Leffler INP_UNLOCK(last); 3536b8e5a98SRobert Watson INP_INFO_RUNLOCK(&udbinfo); 354df8bae1dSRodney W. Grimes return; 355df8bae1dSRodney W. Grimes } 356df8bae1dSRodney W. Grimes /* 3576d6a026bSDavid Greenman * Locate pcb for datagram. 358df8bae1dSRodney W. Grimes */ 359c3229e05SDavid Greenman inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 360cfa1ca9dSYoshinobu Inoue ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 36115bd2b43SDavid Greenman if (inp == NULL) { 36275cfc95fSAndrey A. Chernov if (log_in_vain) { 363df5c0b8aSBill Fenner char buf[4*sizeof "123"]; 36475cfc95fSAndrey A. Chernov 36575cfc95fSAndrey A. Chernov strcpy(buf, inet_ntoa(ip->ip_dst)); 366592071e8SBruce Evans log(LOG_INFO, 367592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 368592071e8SBruce Evans buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 369592071e8SBruce Evans ntohs(uh->uh_sport)); 37075cfc95fSAndrey A. Chernov } 371df8bae1dSRodney W. Grimes udpstat.udps_noport++; 372df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 373df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 37461ffc0b1SJeffrey Hsu goto badheadlocked; 375df8bae1dSRodney W. Grimes } 376582a7760SBruce Evans if (blackhole) 37761ffc0b1SJeffrey Hsu goto badheadlocked; 3781cbd978eSLuigi Rizzo if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 3791cbd978eSLuigi Rizzo goto badheadlocked; 38004287599SRuslan Ermilov *ip = save_ip; 38104287599SRuslan Ermilov ip->ip_len += iphlen; 382582a7760SBruce Evans icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 383f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 384df8bae1dSRodney W. Grimes return; 385df8bae1dSRodney W. Grimes } 38661ffc0b1SJeffrey Hsu INP_LOCK(inp); 387d4b509bdSRobert Watson udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in); 388f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 3896b8e5a98SRobert Watson INP_INFO_RUNLOCK(&udbinfo); 390df8bae1dSRodney W. Grimes return; 39161ffc0b1SJeffrey Hsu 39261ffc0b1SJeffrey Hsu badheadlocked: 393f76fcf6dSJeffrey Hsu if (inp) 394f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 3956b8e5a98SRobert Watson INP_INFO_RUNLOCK(&udbinfo); 396f76fcf6dSJeffrey Hsu badunlocked: 397df8bae1dSRodney W. Grimes m_freem(m); 398df8bae1dSRodney W. Grimes if (opts) 399df8bae1dSRodney W. Grimes m_freem(opts); 400cfa1ca9dSYoshinobu Inoue return; 401cfa1ca9dSYoshinobu Inoue } 402cfa1ca9dSYoshinobu Inoue 403cfa1ca9dSYoshinobu Inoue /* 404d4b509bdSRobert Watson * Subroutine of udp_input(), which appends the provided mbuf chain to the 405d4b509bdSRobert Watson * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 406d4b509bdSRobert Watson * contains the source address. If the socket ends up being an IPv6 socket, 407d4b509bdSRobert Watson * udp_append() will convert to a sockaddr_in6 before passing the address 408d4b509bdSRobert Watson * into the socket code. 409cfa1ca9dSYoshinobu Inoue */ 410cfa1ca9dSYoshinobu Inoue static void 411d4b509bdSRobert Watson udp_append(last, ip, n, off, udp_in) 412032dcc76SLuigi Rizzo struct inpcb *last; 413032dcc76SLuigi Rizzo struct ip *ip; 414032dcc76SLuigi Rizzo struct mbuf *n; 415032dcc76SLuigi Rizzo int off; 416d4b509bdSRobert Watson struct sockaddr_in *udp_in; 417cfa1ca9dSYoshinobu Inoue { 418cfa1ca9dSYoshinobu Inoue struct sockaddr *append_sa; 4191e4d7da7SRobert Watson struct socket *so; 420cfa1ca9dSYoshinobu Inoue struct mbuf *opts = 0; 421c83c1318SPoul-Henning Kamp #ifdef INET6 422d4b509bdSRobert Watson struct sockaddr_in6 udp_in6; 423c83c1318SPoul-Henning Kamp #endif 424cfa1ca9dSYoshinobu Inoue 42587f2bb8cSRobert Watson INP_LOCK_ASSERT(last); 42687f2bb8cSRobert Watson 427da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC) 428da0f4099SHajimu UMEMOTO /* check AH/ESP integrity. */ 429da0f4099SHajimu UMEMOTO if (ipsec4_in_reject(n, last)) { 430b9234fafSSam Leffler #ifdef IPSEC 431b9234fafSSam Leffler ipsecstat.in_polvio++; 432b9234fafSSam Leffler #endif /*IPSEC*/ 433b9234fafSSam Leffler m_freem(n); 434b9234fafSSam Leffler return; 435b9234fafSSam Leffler } 436da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/ 437b9234fafSSam Leffler #ifdef MAC 438a557af22SRobert Watson if (mac_check_inpcb_deliver(last, n) != 0) { 439b9234fafSSam Leffler m_freem(n); 440b9234fafSSam Leffler return; 441b9234fafSSam Leffler } 442b9234fafSSam Leffler #endif 443cfa1ca9dSYoshinobu Inoue if (last->inp_flags & INP_CONTROLOPTS || 444be8a62e8SPoul-Henning Kamp last->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 445cfa1ca9dSYoshinobu Inoue #ifdef INET6 446cfa1ca9dSYoshinobu Inoue if (last->inp_vflag & INP_IPV6) { 447cfa1ca9dSYoshinobu Inoue int savedflags; 448cfa1ca9dSYoshinobu Inoue 449cfa1ca9dSYoshinobu Inoue savedflags = last->inp_flags; 450cfa1ca9dSYoshinobu Inoue last->inp_flags &= ~INP_UNMAPPABLEOPTS; 45111de19f4SHajimu UMEMOTO ip6_savecontrol(last, n, &opts); 452cfa1ca9dSYoshinobu Inoue last->inp_flags = savedflags; 453cfa1ca9dSYoshinobu Inoue } else 454cfa1ca9dSYoshinobu Inoue #endif 455cfa1ca9dSYoshinobu Inoue ip_savecontrol(last, &opts, ip, n); 4564cc20ab1SSeigo Tanimura } 457cfa1ca9dSYoshinobu Inoue #ifdef INET6 458cfa1ca9dSYoshinobu Inoue if (last->inp_vflag & INP_IPV6) { 459d4b509bdSRobert Watson bzero(&udp_in6, sizeof(udp_in6)); 460d4b509bdSRobert Watson udp_in6.sin6_len = sizeof(udp_in6); 461d4b509bdSRobert Watson udp_in6.sin6_family = AF_INET6; 462d4b509bdSRobert Watson in6_sin_2_v4mapsin6(udp_in, &udp_in6); 463d4b509bdSRobert Watson append_sa = (struct sockaddr *)&udp_in6; 464cfa1ca9dSYoshinobu Inoue } else 465cfa1ca9dSYoshinobu Inoue #endif 466d4b509bdSRobert Watson append_sa = (struct sockaddr *)udp_in; 467cfa1ca9dSYoshinobu Inoue m_adj(n, off); 4681e4d7da7SRobert Watson 4691e4d7da7SRobert Watson so = last->inp_socket; 4701e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 4711e4d7da7SRobert Watson if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 472cfa1ca9dSYoshinobu Inoue m_freem(n); 473cfa1ca9dSYoshinobu Inoue if (opts) 474cfa1ca9dSYoshinobu Inoue m_freem(opts); 475cfa1ca9dSYoshinobu Inoue udpstat.udps_fullsock++; 4761e4d7da7SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 4774cc20ab1SSeigo Tanimura } else 4781e4d7da7SRobert Watson sorwakeup_locked(so); 479df8bae1dSRodney W. Grimes } 480df8bae1dSRodney W. Grimes 481df8bae1dSRodney W. Grimes /* 482df8bae1dSRodney W. Grimes * Notify a udp user of an asynchronous error; 483df8bae1dSRodney W. Grimes * just wake up so that he can collect error status. 484df8bae1dSRodney W. Grimes */ 4853ce144eaSJeffrey Hsu struct inpcb * 486032dcc76SLuigi Rizzo udp_notify(inp, errno) 487032dcc76SLuigi Rizzo register struct inpcb *inp; 488032dcc76SLuigi Rizzo int errno; 489df8bae1dSRodney W. Grimes { 490df8bae1dSRodney W. Grimes inp->inp_socket->so_error = errno; 491df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 492df8bae1dSRodney W. Grimes sowwakeup(inp->inp_socket); 4933ce144eaSJeffrey Hsu return inp; 494df8bae1dSRodney W. Grimes } 495df8bae1dSRodney W. Grimes 496df8bae1dSRodney W. Grimes void 497032dcc76SLuigi Rizzo udp_ctlinput(cmd, sa, vip) 498032dcc76SLuigi Rizzo int cmd; 499032dcc76SLuigi Rizzo struct sockaddr *sa; 500032dcc76SLuigi Rizzo void *vip; 501df8bae1dSRodney W. Grimes { 502c693a045SJonathan Lemon struct ip *ip = vip; 503c693a045SJonathan Lemon struct udphdr *uh; 5043ce144eaSJeffrey Hsu struct inpcb *(*notify)(struct inpcb *, int) = udp_notify; 505c693a045SJonathan Lemon struct in_addr faddr; 506c693a045SJonathan Lemon struct inpcb *inp; 507c693a045SJonathan Lemon int s; 508c693a045SJonathan Lemon 509c693a045SJonathan Lemon faddr = ((struct sockaddr_in *)sa)->sin_addr; 510c693a045SJonathan Lemon if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 511c693a045SJonathan Lemon return; 512df8bae1dSRodney W. Grimes 51397d8d152SAndre Oppermann /* 51497d8d152SAndre Oppermann * Redirects don't need to be handled up here. 51597d8d152SAndre Oppermann */ 51697d8d152SAndre Oppermann if (PRC_IS_REDIRECT(cmd)) 51797d8d152SAndre Oppermann return; 51897d8d152SAndre Oppermann /* 51997d8d152SAndre Oppermann * Hostdead is ugly because it goes linearly through all PCBs. 52097d8d152SAndre Oppermann * XXX: We never get this from ICMP, otherwise it makes an 52197d8d152SAndre Oppermann * excellent DoS attack on machines with many connections. 52297d8d152SAndre Oppermann */ 52397d8d152SAndre Oppermann if (cmd == PRC_HOSTDEAD) 524d1c54148SJesper Skriver ip = 0; 525d1c54148SJesper Skriver else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 526df8bae1dSRodney W. Grimes return; 527df8bae1dSRodney W. Grimes if (ip) { 528c693a045SJonathan Lemon s = splnet(); 529df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 530f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 531c693a045SJonathan Lemon inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, 532c693a045SJonathan Lemon ip->ip_src, uh->uh_sport, 0, NULL); 533f76fcf6dSJeffrey Hsu if (inp != NULL) { 534f76fcf6dSJeffrey Hsu INP_LOCK(inp); 535f76fcf6dSJeffrey Hsu if (inp->inp_socket != NULL) { 536c693a045SJonathan Lemon (*notify)(inp, inetctlerrmap[cmd]); 537f76fcf6dSJeffrey Hsu } 538f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 539f76fcf6dSJeffrey Hsu } 540f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 541c693a045SJonathan Lemon splx(s); 542df8bae1dSRodney W. Grimes } else 543f76fcf6dSJeffrey Hsu in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify); 544df8bae1dSRodney W. Grimes } 545df8bae1dSRodney W. Grimes 5460312fbe9SPoul-Henning Kamp static int 54782d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS) 54898271db4SGarrett Wollman { 54998271db4SGarrett Wollman int error, i, n, s; 55098271db4SGarrett Wollman struct inpcb *inp, **inp_list; 55198271db4SGarrett Wollman inp_gen_t gencnt; 55298271db4SGarrett Wollman struct xinpgen xig; 55398271db4SGarrett Wollman 55498271db4SGarrett Wollman /* 55598271db4SGarrett Wollman * The process of preparing the TCB list is too time-consuming and 55698271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 55798271db4SGarrett Wollman */ 55898271db4SGarrett Wollman if (req->oldptr == 0) { 55998271db4SGarrett Wollman n = udbinfo.ipi_count; 56098271db4SGarrett Wollman req->oldidx = 2 * (sizeof xig) 56198271db4SGarrett Wollman + (n + n/8) * sizeof(struct xinpcb); 56298271db4SGarrett Wollman return 0; 56398271db4SGarrett Wollman } 56498271db4SGarrett Wollman 56598271db4SGarrett Wollman if (req->newptr != 0) 56698271db4SGarrett Wollman return EPERM; 56798271db4SGarrett Wollman 56898271db4SGarrett Wollman /* 56998271db4SGarrett Wollman * OK, now we're committed to doing something. 57098271db4SGarrett Wollman */ 57198271db4SGarrett Wollman s = splnet(); 5724b40c56cSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 57398271db4SGarrett Wollman gencnt = udbinfo.ipi_gencnt; 57498271db4SGarrett Wollman n = udbinfo.ipi_count; 5754b40c56cSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 57698271db4SGarrett Wollman splx(s); 57798271db4SGarrett Wollman 57847934cefSDon Lewis error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 5795c38b6dbSDon Lewis + n * sizeof(struct xinpcb)); 58047934cefSDon Lewis if (error != 0) 58147934cefSDon Lewis return (error); 5825c38b6dbSDon Lewis 58398271db4SGarrett Wollman xig.xig_len = sizeof xig; 58498271db4SGarrett Wollman xig.xig_count = n; 58598271db4SGarrett Wollman xig.xig_gen = gencnt; 58698271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 58798271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 58898271db4SGarrett Wollman if (error) 58998271db4SGarrett Wollman return error; 59098271db4SGarrett Wollman 591a163d034SWarner Losh inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 59298271db4SGarrett Wollman if (inp_list == 0) 59398271db4SGarrett Wollman return ENOMEM; 59498271db4SGarrett Wollman 59598271db4SGarrett Wollman s = splnet(); 596f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 597fc2ffbe6SPoul-Henning Kamp for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n; 598fc2ffbe6SPoul-Henning Kamp inp = LIST_NEXT(inp, inp_list)) { 599f76fcf6dSJeffrey Hsu INP_LOCK(inp); 6002ded288cSJeffrey Hsu if (inp->inp_gencnt <= gencnt && 6012ded288cSJeffrey Hsu cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 60298271db4SGarrett Wollman inp_list[i++] = inp; 603f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 6044787fd37SPaul Saab } 605f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 60698271db4SGarrett Wollman splx(s); 60798271db4SGarrett Wollman n = i; 60898271db4SGarrett Wollman 60998271db4SGarrett Wollman error = 0; 61098271db4SGarrett Wollman for (i = 0; i < n; i++) { 61198271db4SGarrett Wollman inp = inp_list[i]; 61298271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 61398271db4SGarrett Wollman struct xinpcb xi; 61498271db4SGarrett Wollman xi.xi_len = sizeof xi; 61598271db4SGarrett Wollman /* XXX should avoid extra copy */ 61698271db4SGarrett Wollman bcopy(inp, &xi.xi_inp, sizeof *inp); 61798271db4SGarrett Wollman if (inp->inp_socket) 61898271db4SGarrett Wollman sotoxsocket(inp->inp_socket, &xi.xi_socket); 6194b40c56cSJeffrey Hsu xi.xi_inp.inp_gencnt = inp->inp_gencnt; 62098271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 62198271db4SGarrett Wollman } 62298271db4SGarrett Wollman } 62398271db4SGarrett Wollman if (!error) { 62498271db4SGarrett Wollman /* 62598271db4SGarrett Wollman * Give the user an updated idea of our state. 62698271db4SGarrett Wollman * If the generation differs from what we told 62798271db4SGarrett Wollman * her before, she knows that something happened 62898271db4SGarrett Wollman * while we were processing this request, and it 62998271db4SGarrett Wollman * might be necessary to retry. 63098271db4SGarrett Wollman */ 63198271db4SGarrett Wollman s = splnet(); 632f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 63398271db4SGarrett Wollman xig.xig_gen = udbinfo.ipi_gencnt; 63498271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 63598271db4SGarrett Wollman xig.xig_count = udbinfo.ipi_count; 636f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 63798271db4SGarrett Wollman splx(s); 63898271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 63998271db4SGarrett Wollman } 64098271db4SGarrett Wollman free(inp_list, M_TEMP); 64198271db4SGarrett Wollman return error; 64298271db4SGarrett Wollman } 64398271db4SGarrett Wollman 64498271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 64598271db4SGarrett Wollman udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 64698271db4SGarrett Wollman 64798271db4SGarrett Wollman static int 64882d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS) 649490d50b6SBrian Feldman { 650c0511d3bSBrian Feldman struct xucred xuc; 651490d50b6SBrian Feldman struct sockaddr_in addrs[2]; 652490d50b6SBrian Feldman struct inpcb *inp; 653490d50b6SBrian Feldman int error, s; 654490d50b6SBrian Feldman 65556f21b9dSColin Percival error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL); 656490d50b6SBrian Feldman if (error) 657490d50b6SBrian Feldman return (error); 658490d50b6SBrian Feldman error = SYSCTL_IN(req, addrs, sizeof(addrs)); 659490d50b6SBrian Feldman if (error) 660490d50b6SBrian Feldman return (error); 661490d50b6SBrian Feldman s = splnet(); 662f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 663490d50b6SBrian Feldman inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 664cfa1ca9dSYoshinobu Inoue addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 6652d20c83fSDon Lewis if (inp == NULL || inp->inp_socket == NULL) { 666490d50b6SBrian Feldman error = ENOENT; 667490d50b6SBrian Feldman goto out; 668490d50b6SBrian Feldman } 66929dc1288SRobert Watson error = cr_canseesocket(req->td->td_ucred, inp->inp_socket); 6707ce87f12SDavid Malone if (error) 6717ce87f12SDavid Malone goto out; 67276183f34SDima Dorfman cru2x(inp->inp_socket->so_cred, &xuc); 673490d50b6SBrian Feldman out: 674f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 675490d50b6SBrian Feldman splx(s); 6760e1eebb8SDon Lewis if (error == 0) 6770e1eebb8SDon Lewis error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 678490d50b6SBrian Feldman return (error); 679490d50b6SBrian Feldman } 680490d50b6SBrian Feldman 6817ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 6827ce87f12SDavid Malone CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 6837ce87f12SDavid Malone udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 684490d50b6SBrian Feldman 685490d50b6SBrian Feldman static int 686032dcc76SLuigi Rizzo udp_output(inp, m, addr, control, td) 687032dcc76SLuigi Rizzo register struct inpcb *inp; 688032dcc76SLuigi Rizzo struct mbuf *m; 689032dcc76SLuigi Rizzo struct sockaddr *addr; 690032dcc76SLuigi Rizzo struct mbuf *control; 691032dcc76SLuigi Rizzo struct thread *td; 692df8bae1dSRodney W. Grimes { 693032dcc76SLuigi Rizzo register struct udpiphdr *ui; 694032dcc76SLuigi Rizzo register int len = m->m_pkthdr.len; 69590162a4eSIan Dowse struct in_addr faddr, laddr; 696c557ae16SIan Dowse struct cmsghdr *cm; 697c557ae16SIan Dowse struct sockaddr_in *sin, src; 69890162a4eSIan Dowse int error = 0; 6998afa2304SBruce M Simpson int ipflags; 70090162a4eSIan Dowse u_short fport, lport; 7015c32ea65SRobert Watson int unlock_udbinfo; 702df8bae1dSRodney W. Grimes 7035c32ea65SRobert Watson /* 7045c32ea65SRobert Watson * udp_output() may need to temporarily bind or connect the current 7055c32ea65SRobert Watson * inpcb. As such, we don't know up front what inpcb locks we will 7065c32ea65SRobert Watson * need. Do any work to decide what is needed up front before 7075c32ea65SRobert Watson * acquiring locks. 7085c32ea65SRobert Watson */ 709430d30d8SBill Fenner if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 710c557ae16SIan Dowse if (control) 711c557ae16SIan Dowse m_freem(control); 7125c32ea65SRobert Watson m_freem(m); 7135c32ea65SRobert Watson return EMSGSIZE; 714430d30d8SBill Fenner } 715430d30d8SBill Fenner 716c557ae16SIan Dowse src.sin_addr.s_addr = INADDR_ANY; 717c557ae16SIan Dowse if (control != NULL) { 718c557ae16SIan Dowse /* 719c557ae16SIan Dowse * XXX: Currently, we assume all the optional information 720c557ae16SIan Dowse * is stored in a single mbuf. 721c557ae16SIan Dowse */ 722c557ae16SIan Dowse if (control->m_next) { 723c557ae16SIan Dowse m_freem(control); 7245c32ea65SRobert Watson m_freem(m); 7255c32ea65SRobert Watson return EINVAL; 726c557ae16SIan Dowse } 727c557ae16SIan Dowse for (; control->m_len > 0; 728c557ae16SIan Dowse control->m_data += CMSG_ALIGN(cm->cmsg_len), 729c557ae16SIan Dowse control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 730c557ae16SIan Dowse cm = mtod(control, struct cmsghdr *); 731c557ae16SIan Dowse if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 || 732c557ae16SIan Dowse cm->cmsg_len > control->m_len) { 733c557ae16SIan Dowse error = EINVAL; 734c557ae16SIan Dowse break; 735c557ae16SIan Dowse } 736c557ae16SIan Dowse if (cm->cmsg_level != IPPROTO_IP) 737c557ae16SIan Dowse continue; 738c557ae16SIan Dowse 739c557ae16SIan Dowse switch (cm->cmsg_type) { 740c557ae16SIan Dowse case IP_SENDSRCADDR: 741c557ae16SIan Dowse if (cm->cmsg_len != 742c557ae16SIan Dowse CMSG_LEN(sizeof(struct in_addr))) { 743c557ae16SIan Dowse error = EINVAL; 744c557ae16SIan Dowse break; 745c557ae16SIan Dowse } 746c557ae16SIan Dowse bzero(&src, sizeof(src)); 747c557ae16SIan Dowse src.sin_family = AF_INET; 748c557ae16SIan Dowse src.sin_len = sizeof(src); 749c557ae16SIan Dowse src.sin_port = inp->inp_lport; 750c557ae16SIan Dowse src.sin_addr = *(struct in_addr *)CMSG_DATA(cm); 751c557ae16SIan Dowse break; 752c557ae16SIan Dowse default: 753c557ae16SIan Dowse error = ENOPROTOOPT; 754c557ae16SIan Dowse break; 755c557ae16SIan Dowse } 756c557ae16SIan Dowse if (error) 757c557ae16SIan Dowse break; 758c557ae16SIan Dowse } 759c557ae16SIan Dowse m_freem(control); 760c557ae16SIan Dowse } 7615c32ea65SRobert Watson if (error) { 7625c32ea65SRobert Watson m_freem(m); 7635c32ea65SRobert Watson return error; 7645c32ea65SRobert Watson } 7655c32ea65SRobert Watson 7665c32ea65SRobert Watson if (src.sin_addr.s_addr != INADDR_ANY || 7675c32ea65SRobert Watson addr != NULL) { 7685c32ea65SRobert Watson INP_INFO_WLOCK(&udbinfo); 7695c32ea65SRobert Watson unlock_udbinfo = 1; 7705c32ea65SRobert Watson } else 7715c32ea65SRobert Watson unlock_udbinfo = 0; 7725c32ea65SRobert Watson INP_LOCK(inp); 7735c32ea65SRobert Watson 7745c32ea65SRobert Watson #ifdef MAC 7755c32ea65SRobert Watson mac_create_mbuf_from_inpcb(inp, m); 7765c32ea65SRobert Watson #endif 7775c32ea65SRobert Watson 77890162a4eSIan Dowse laddr = inp->inp_laddr; 77990162a4eSIan Dowse lport = inp->inp_lport; 780c557ae16SIan Dowse if (src.sin_addr.s_addr != INADDR_ANY) { 781c557ae16SIan Dowse if (lport == 0) { 782c557ae16SIan Dowse error = EINVAL; 783c557ae16SIan Dowse goto release; 784c557ae16SIan Dowse } 785c557ae16SIan Dowse error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 786b0330ed9SPawel Jakub Dawidek &laddr.s_addr, &lport, td->td_ucred); 787c557ae16SIan Dowse if (error) 788c557ae16SIan Dowse goto release; 789c557ae16SIan Dowse } 790c557ae16SIan Dowse 791df8bae1dSRodney W. Grimes if (addr) { 79275c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)addr; 793a854ed98SJohn Baldwin if (td && jailed(td->td_ucred)) 794a854ed98SJohn Baldwin prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr); 795df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 796df8bae1dSRodney W. Grimes error = EISCONN; 797df8bae1dSRodney W. Grimes goto release; 798df8bae1dSRodney W. Grimes } 79990162a4eSIan Dowse error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport, 800b0330ed9SPawel Jakub Dawidek &faddr.s_addr, &fport, NULL, td->td_ucred); 80190162a4eSIan Dowse if (error) 80290162a4eSIan Dowse goto release; 80390162a4eSIan Dowse 80490162a4eSIan Dowse /* Commit the local port if newly assigned. */ 80590162a4eSIan Dowse if (inp->inp_laddr.s_addr == INADDR_ANY && 80690162a4eSIan Dowse inp->inp_lport == 0) { 80790162a4eSIan Dowse inp->inp_lport = lport; 80890162a4eSIan Dowse if (in_pcbinshash(inp) != 0) { 80990162a4eSIan Dowse inp->inp_lport = 0; 81090162a4eSIan Dowse error = EAGAIN; 811df8bae1dSRodney W. Grimes goto release; 812df8bae1dSRodney W. Grimes } 81390162a4eSIan Dowse inp->inp_flags |= INP_ANONPORT; 81490162a4eSIan Dowse } 815df8bae1dSRodney W. Grimes } else { 81690162a4eSIan Dowse faddr = inp->inp_faddr; 81790162a4eSIan Dowse fport = inp->inp_fport; 81890162a4eSIan Dowse if (faddr.s_addr == INADDR_ANY) { 819df8bae1dSRodney W. Grimes error = ENOTCONN; 820df8bae1dSRodney W. Grimes goto release; 821df8bae1dSRodney W. Grimes } 822df8bae1dSRodney W. Grimes } 823e6ccd709SRobert Watson 824df8bae1dSRodney W. Grimes /* 825e6ccd709SRobert Watson * Calculate data length and get a mbuf for UDP, IP, and possible 826392e8407SRobert Watson * link-layer headers. Immediate slide the data pointer back forward 827392e8407SRobert Watson * since we won't use that space at this layer. 828df8bae1dSRodney W. Grimes */ 829e6ccd709SRobert Watson M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT); 830e6ccd709SRobert Watson if (m == NULL) { 831df8bae1dSRodney W. Grimes error = ENOBUFS; 83249b19bfcSBruce M Simpson goto release; 833df8bae1dSRodney W. Grimes } 834e6ccd709SRobert Watson m->m_data += max_linkhdr; 835e6ccd709SRobert Watson m->m_len -= max_linkhdr; 836392e8407SRobert Watson m->m_pkthdr.len -= max_linkhdr; 837df8bae1dSRodney W. Grimes 838df8bae1dSRodney W. Grimes /* 839df8bae1dSRodney W. Grimes * Fill in mbuf with extended UDP header 840df8bae1dSRodney W. Grimes * and addresses and length put into network format. 841df8bae1dSRodney W. Grimes */ 842df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 843db4f9cc7SJonathan Lemon bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 844df8bae1dSRodney W. Grimes ui->ui_pr = IPPROTO_UDP; 84590162a4eSIan Dowse ui->ui_src = laddr; 84690162a4eSIan Dowse ui->ui_dst = faddr; 84790162a4eSIan Dowse ui->ui_sport = lport; 84890162a4eSIan Dowse ui->ui_dport = fport; 849db4f9cc7SJonathan Lemon ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 850df8bae1dSRodney W. Grimes 851b5d47ff5SJohn-Mark Gurney ipflags = 0; 852b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_DONTROUTE) 853b5d47ff5SJohn-Mark Gurney ipflags |= IP_ROUTETOIF; 854b5d47ff5SJohn-Mark Gurney if (inp->inp_socket->so_options & SO_BROADCAST) 855b5d47ff5SJohn-Mark Gurney ipflags |= IP_ALLOWBROADCAST; 8568afa2304SBruce M Simpson if (inp->inp_flags & INP_ONESBCAST) 8578afa2304SBruce M Simpson ipflags |= IP_SENDONES; 8588afa2304SBruce M Simpson 859df8bae1dSRodney W. Grimes /* 860db4f9cc7SJonathan Lemon * Set up checksum and output datagram. 861df8bae1dSRodney W. Grimes */ 862df8bae1dSRodney W. Grimes if (udpcksum) { 8638a538743SBruce M Simpson if (inp->inp_flags & INP_ONESBCAST) 8648a538743SBruce M Simpson faddr.s_addr = INADDR_BROADCAST; 8658a538743SBruce M Simpson ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 866db4f9cc7SJonathan Lemon htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 867db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_UDP; 868db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 869db4f9cc7SJonathan Lemon } else { 870db4f9cc7SJonathan Lemon ui->ui_sum = 0; 871df8bae1dSRodney W. Grimes } 872df8bae1dSRodney W. Grimes ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 873ca98b82cSDavid Greenman ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 874ca98b82cSDavid Greenman ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 875df8bae1dSRodney W. Grimes udpstat.udps_opackets++; 876cfa1ca9dSYoshinobu Inoue 8775c32ea65SRobert Watson if (unlock_udbinfo) 8785c32ea65SRobert Watson INP_INFO_WUNLOCK(&udbinfo); 87997d8d152SAndre Oppermann error = ip_output(m, inp->inp_options, NULL, ipflags, 8805d846453SSam Leffler inp->inp_moptions, inp); 8815c32ea65SRobert Watson INP_UNLOCK(inp); 882df8bae1dSRodney W. Grimes return (error); 883df8bae1dSRodney W. Grimes 884df8bae1dSRodney W. Grimes release: 8855c32ea65SRobert Watson INP_UNLOCK(inp); 8865c32ea65SRobert Watson if (unlock_udbinfo) 8875c32ea65SRobert Watson INP_INFO_WUNLOCK(&udbinfo); 888df8bae1dSRodney W. Grimes m_freem(m); 889df8bae1dSRodney W. Grimes return (error); 890df8bae1dSRodney W. Grimes } 891df8bae1dSRodney W. Grimes 89276429de4SYoshinobu Inoue u_long udp_sendspace = 9216; /* really max datagram size */ 893032dcc76SLuigi Rizzo /* 40 1K datagrams */ 8940312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 8953d177f46SBill Fumerola &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 8960312fbe9SPoul-Henning Kamp 897cfa1ca9dSYoshinobu Inoue u_long udp_recvspace = 40 * (1024 + 898cfa1ca9dSYoshinobu Inoue #ifdef INET6 899cfa1ca9dSYoshinobu Inoue sizeof(struct sockaddr_in6) 900cfa1ca9dSYoshinobu Inoue #else 901cfa1ca9dSYoshinobu Inoue sizeof(struct sockaddr_in) 902cfa1ca9dSYoshinobu Inoue #endif 903cfa1ca9dSYoshinobu Inoue ); 9040312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 9050ca2861fSRuslan Ermilov &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 906df8bae1dSRodney W. Grimes 907d0390e05SGarrett Wollman static int 908d0390e05SGarrett Wollman udp_abort(struct socket *so) 909df8bae1dSRodney W. Grimes { 910d0390e05SGarrett Wollman struct inpcb *inp; 911df8bae1dSRodney W. Grimes int s; 912df8bae1dSRodney W. Grimes 913f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&udbinfo); 914d0390e05SGarrett Wollman inp = sotoinpcb(so); 915f76fcf6dSJeffrey Hsu if (inp == 0) { 916f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 917d0390e05SGarrett Wollman return EINVAL; /* ??? possible? panic instead? */ 918f76fcf6dSJeffrey Hsu } 919f76fcf6dSJeffrey Hsu INP_LOCK(inp); 920d0390e05SGarrett Wollman soisdisconnected(so); 921d0390e05SGarrett Wollman s = splnet(); 922d0390e05SGarrett Wollman in_pcbdetach(inp); 923f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 924d0390e05SGarrett Wollman splx(s); 925d0390e05SGarrett Wollman return 0; 926df8bae1dSRodney W. Grimes } 927df8bae1dSRodney W. Grimes 928d0390e05SGarrett Wollman static int 929b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td) 930d0390e05SGarrett Wollman { 931d0390e05SGarrett Wollman struct inpcb *inp; 932d0390e05SGarrett Wollman int s, error; 933d0390e05SGarrett Wollman 934f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&udbinfo); 935d0390e05SGarrett Wollman inp = sotoinpcb(so); 936f76fcf6dSJeffrey Hsu if (inp != 0) { 937f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 938d0390e05SGarrett Wollman return EINVAL; 939f76fcf6dSJeffrey Hsu } 940cfa1ca9dSYoshinobu Inoue error = soreserve(so, udp_sendspace, udp_recvspace); 941f76fcf6dSJeffrey Hsu if (error) { 942f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 943cfa1ca9dSYoshinobu Inoue return error; 944f76fcf6dSJeffrey Hsu } 945df8bae1dSRodney W. Grimes s = splnet(); 9466823b823SPawel Jakub Dawidek error = in_pcballoc(so, &udbinfo, "udpinp"); 947df8bae1dSRodney W. Grimes splx(s); 94853b57cd1SSam Leffler if (error) { 94953b57cd1SSam Leffler INP_INFO_WUNLOCK(&udbinfo); 950d0390e05SGarrett Wollman return error; 95153b57cd1SSam Leffler } 952cfa1ca9dSYoshinobu Inoue 953cfa1ca9dSYoshinobu Inoue inp = (struct inpcb *)so->so_pcb; 954f76fcf6dSJeffrey Hsu INP_LOCK(inp); 955f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 956cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 957cfa1ca9dSYoshinobu Inoue inp->inp_ip_ttl = ip_defttl; 958f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 959d0390e05SGarrett Wollman return 0; 960df8bae1dSRodney W. Grimes } 961d0390e05SGarrett Wollman 962d0390e05SGarrett Wollman static int 963b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 964d0390e05SGarrett Wollman { 965d0390e05SGarrett Wollman struct inpcb *inp; 966d0390e05SGarrett Wollman int s, error; 967d0390e05SGarrett Wollman 968f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&udbinfo); 969d0390e05SGarrett Wollman inp = sotoinpcb(so); 970f76fcf6dSJeffrey Hsu if (inp == 0) { 971f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 972d0390e05SGarrett Wollman return EINVAL; 973f76fcf6dSJeffrey Hsu } 974f76fcf6dSJeffrey Hsu INP_LOCK(inp); 975df8bae1dSRodney W. Grimes s = splnet(); 976b0330ed9SPawel Jakub Dawidek error = in_pcbbind(inp, nam, td->td_ucred); 977d0390e05SGarrett Wollman splx(s); 978f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 979f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 980d0390e05SGarrett Wollman return error; 981d0390e05SGarrett Wollman } 982d0390e05SGarrett Wollman 983d0390e05SGarrett Wollman static int 984b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 985d0390e05SGarrett Wollman { 986d0390e05SGarrett Wollman struct inpcb *inp; 987d0390e05SGarrett Wollman int s, error; 98875c13541SPoul-Henning Kamp struct sockaddr_in *sin; 989d0390e05SGarrett Wollman 990f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&udbinfo); 991d0390e05SGarrett Wollman inp = sotoinpcb(so); 992f76fcf6dSJeffrey Hsu if (inp == 0) { 993f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 994d0390e05SGarrett Wollman return EINVAL; 995f76fcf6dSJeffrey Hsu } 996f76fcf6dSJeffrey Hsu INP_LOCK(inp); 997f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr != INADDR_ANY) { 998f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 999f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 1000d0390e05SGarrett Wollman return EISCONN; 1001f76fcf6dSJeffrey Hsu } 1002d0390e05SGarrett Wollman s = splnet(); 100375c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)nam; 1004a854ed98SJohn Baldwin if (td && jailed(td->td_ucred)) 1005a854ed98SJohn Baldwin prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr); 1006b0330ed9SPawel Jakub Dawidek error = in_pcbconnect(inp, nam, td->td_ucred); 1007df8bae1dSRodney W. Grimes splx(s); 10084cc20ab1SSeigo Tanimura if (error == 0) 1009df8bae1dSRodney W. Grimes soisconnected(so); 1010f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1011f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 1012d0390e05SGarrett Wollman return error; 1013df8bae1dSRodney W. Grimes } 1014d0390e05SGarrett Wollman 1015d0390e05SGarrett Wollman static int 1016d0390e05SGarrett Wollman udp_detach(struct socket *so) 1017d0390e05SGarrett Wollman { 1018d0390e05SGarrett Wollman struct inpcb *inp; 1019d0390e05SGarrett Wollman int s; 1020d0390e05SGarrett Wollman 1021f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&udbinfo); 1022d0390e05SGarrett Wollman inp = sotoinpcb(so); 1023f76fcf6dSJeffrey Hsu if (inp == 0) { 1024f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 1025d0390e05SGarrett Wollman return EINVAL; 1026f76fcf6dSJeffrey Hsu } 1027f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1028d0390e05SGarrett Wollman s = splnet(); 1029d0390e05SGarrett Wollman in_pcbdetach(inp); 1030f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 1031d0390e05SGarrett Wollman splx(s); 1032d0390e05SGarrett Wollman return 0; 1033d0390e05SGarrett Wollman } 1034d0390e05SGarrett Wollman 1035d0390e05SGarrett Wollman static int 1036d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 1037d0390e05SGarrett Wollman { 1038d0390e05SGarrett Wollman struct inpcb *inp; 1039d0390e05SGarrett Wollman int s; 1040d0390e05SGarrett Wollman 1041f76fcf6dSJeffrey Hsu INP_INFO_WLOCK(&udbinfo); 1042d0390e05SGarrett Wollman inp = sotoinpcb(so); 1043f76fcf6dSJeffrey Hsu if (inp == 0) { 1044f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 1045d0390e05SGarrett Wollman return EINVAL; 1046f76fcf6dSJeffrey Hsu } 1047f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1048f76fcf6dSJeffrey Hsu if (inp->inp_faddr.s_addr == INADDR_ANY) { 1049f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 1050f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1051d0390e05SGarrett Wollman return ENOTCONN; 1052f76fcf6dSJeffrey Hsu } 1053d0390e05SGarrett Wollman 1054df8bae1dSRodney W. Grimes s = splnet(); 1055df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 1056df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 1057f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1058f76fcf6dSJeffrey Hsu INP_INFO_WUNLOCK(&udbinfo); 1059df8bae1dSRodney W. Grimes splx(s); 1060df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1061d0390e05SGarrett Wollman return 0; 1062df8bae1dSRodney W. Grimes } 1063df8bae1dSRodney W. Grimes 1064d0390e05SGarrett Wollman static int 106557bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1066b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 1067d0390e05SGarrett Wollman { 1068d0390e05SGarrett Wollman struct inpcb *inp; 1069d0390e05SGarrett Wollman 1070d0390e05SGarrett Wollman inp = sotoinpcb(so); 10715c32ea65SRobert Watson return udp_output(inp, m, addr, control, td); 1072d0390e05SGarrett Wollman } 1073d0390e05SGarrett Wollman 107476429de4SYoshinobu Inoue int 1075d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 1076d0390e05SGarrett Wollman { 1077d0390e05SGarrett Wollman struct inpcb *inp; 1078d0390e05SGarrett Wollman 1079f76fcf6dSJeffrey Hsu INP_INFO_RLOCK(&udbinfo); 1080d0390e05SGarrett Wollman inp = sotoinpcb(so); 1081f76fcf6dSJeffrey Hsu if (inp == 0) { 1082f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 1083d0390e05SGarrett Wollman return EINVAL; 1084f76fcf6dSJeffrey Hsu } 1085f76fcf6dSJeffrey Hsu INP_LOCK(inp); 1086f76fcf6dSJeffrey Hsu INP_INFO_RUNLOCK(&udbinfo); 1087d0390e05SGarrett Wollman socantsendmore(so); 1088f76fcf6dSJeffrey Hsu INP_UNLOCK(inp); 1089d0390e05SGarrett Wollman return 0; 1090d0390e05SGarrett Wollman } 1091d0390e05SGarrett Wollman 1092f76fcf6dSJeffrey Hsu /* 1093f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setsockaddr. We just pass down 1094f76fcf6dSJeffrey Hsu * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 1095f76fcf6dSJeffrey Hsu * here because in_setsockaddr will call malloc and might block. 1096f76fcf6dSJeffrey Hsu */ 1097f76fcf6dSJeffrey Hsu static int 1098f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam) 1099f76fcf6dSJeffrey Hsu { 1100f76fcf6dSJeffrey Hsu return (in_setsockaddr(so, nam, &udbinfo)); 1101f76fcf6dSJeffrey Hsu } 1102f76fcf6dSJeffrey Hsu 1103f76fcf6dSJeffrey Hsu /* 1104f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setpeeraddr. We just pass down 1105f76fcf6dSJeffrey Hsu * the pcbinfo for in_setpeeraddr to lock. 1106f76fcf6dSJeffrey Hsu */ 1107f76fcf6dSJeffrey Hsu static int 1108f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam) 1109f76fcf6dSJeffrey Hsu { 1110f76fcf6dSJeffrey Hsu return (in_setpeeraddr(so, nam, &udbinfo)); 1111f76fcf6dSJeffrey Hsu } 1112f76fcf6dSJeffrey Hsu 1113d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 1114756d52a1SPoul-Henning Kamp .pru_abort = udp_abort, 1115756d52a1SPoul-Henning Kamp .pru_attach = udp_attach, 1116756d52a1SPoul-Henning Kamp .pru_bind = udp_bind, 1117756d52a1SPoul-Henning Kamp .pru_connect = udp_connect, 1118756d52a1SPoul-Henning Kamp .pru_control = in_control, 1119756d52a1SPoul-Henning Kamp .pru_detach = udp_detach, 1120756d52a1SPoul-Henning Kamp .pru_disconnect = udp_disconnect, 1121756d52a1SPoul-Henning Kamp .pru_peeraddr = udp_peeraddr, 1122756d52a1SPoul-Henning Kamp .pru_send = udp_send, 1123756d52a1SPoul-Henning Kamp .pru_shutdown = udp_shutdown, 1124756d52a1SPoul-Henning Kamp .pru_sockaddr = udp_sockaddr, 1125756d52a1SPoul-Henning Kamp .pru_sosetlabel = in_pcbsosetlabel 1126d0390e05SGarrett Wollman }; 1127