1df8bae1dSRodney W. Grimes /* 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 * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 336dfab5b1SGarrett Wollman * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 376a800098SYoshinobu Inoue #include "opt_ipsec.h" 38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 39cfa1ca9dSYoshinobu Inoue 40df8bae1dSRodney W. Grimes #include <sys/param.h> 4126f9a767SRodney W. Grimes #include <sys/systm.h> 42b110a8a2SGarrett Wollman #include <sys/kernel.h> 43df8bae1dSRodney W. Grimes #include <sys/malloc.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 46490d50b6SBrian Feldman #include <sys/proc.h> 47df8bae1dSRodney W. Grimes #include <sys/protosw.h> 48df8bae1dSRodney W. Grimes #include <sys/socket.h> 49df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 50b5e8ce9fSBruce Evans #include <sys/sysctl.h> 51816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 5291421ba2SRobert Watson #include <sys/jail.h> 538781d8e9SBruce Evans 5469c2d429SJeff Roberson #include <vm/uma.h> 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes #include <net/if.h> 57df8bae1dSRodney W. Grimes #include <net/route.h> 58df8bae1dSRodney W. Grimes 59df8bae1dSRodney W. Grimes #include <netinet/in.h> 60df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 61df8bae1dSRodney W. Grimes #include <netinet/ip.h> 62cfa1ca9dSYoshinobu Inoue #ifdef INET6 63cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 64cfa1ca9dSYoshinobu Inoue #endif 65df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 66b5e8ce9fSBruce Evans #include <netinet/in_var.h> 67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 68cfa1ca9dSYoshinobu Inoue #ifdef INET6 69cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 70cfa1ca9dSYoshinobu Inoue #endif 71df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 7251508de1SMatthew Dillon #include <netinet/icmp_var.h> 73df8bae1dSRodney W. Grimes #include <netinet/udp.h> 74df8bae1dSRodney W. Grimes #include <netinet/udp_var.h> 75df8bae1dSRodney W. Grimes 76cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 77cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h> 78cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 79cfa1ca9dSYoshinobu Inoue 80db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 81db4f9cc7SJonathan Lemon 82df8bae1dSRodney W. Grimes /* 83df8bae1dSRodney W. Grimes * UDP protocol implementation. 84df8bae1dSRodney W. Grimes * Per RFC 768, August, 1980. 85df8bae1dSRodney W. Grimes */ 86df8bae1dSRodney W. Grimes #ifndef COMPAT_42 870312fbe9SPoul-Henning Kamp static int udpcksum = 1; 88df8bae1dSRodney W. Grimes #else 890312fbe9SPoul-Henning Kamp static int udpcksum = 0; /* XXX */ 90df8bae1dSRodney W. Grimes #endif 910312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 920312fbe9SPoul-Henning Kamp &udpcksum, 0, ""); 93df8bae1dSRodney W. Grimes 9476429de4SYoshinobu Inoue int log_in_vain = 0; 95816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 963d177f46SBill Fumerola &log_in_vain, 0, "Log all incoming UDP packets"); 97816a3d83SPoul-Henning Kamp 9816f7f31fSGeoff Rehmet static int blackhole = 0; 9916f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, 10016f7f31fSGeoff Rehmet &blackhole, 0, "Do not send port unreachables for refused connects"); 10116f7f31fSGeoff Rehmet 10276429de4SYoshinobu Inoue struct inpcbhead udb; /* from udp_var.h */ 103cfa1ca9dSYoshinobu Inoue #define udb6 udb /* for KAME src sync over BSD*'s */ 1047a2aab80SBrian Feldman struct inpcbinfo udbinfo; 10515bd2b43SDavid Greenman 10615bd2b43SDavid Greenman #ifndef UDBHASHSIZE 107c3229e05SDavid Greenman #define UDBHASHSIZE 16 10815bd2b43SDavid Greenman #endif 10915bd2b43SDavid Greenman 11076429de4SYoshinobu Inoue struct udpstat udpstat; /* from udp_var.h */ 111c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, 1123d177f46SBill Fumerola &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 113f2ea20e6SGarrett Wollman 1140312fbe9SPoul-Henning Kamp static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 115cfa1ca9dSYoshinobu Inoue #ifdef INET6 116cfa1ca9dSYoshinobu Inoue struct udp_in6 { 117cfa1ca9dSYoshinobu Inoue struct sockaddr_in6 uin6_sin; 118cfa1ca9dSYoshinobu Inoue u_char uin6_init_done : 1; 119cfa1ca9dSYoshinobu Inoue } udp_in6 = { 120cfa1ca9dSYoshinobu Inoue { sizeof(udp_in6.uin6_sin), AF_INET6 }, 121cfa1ca9dSYoshinobu Inoue 0 122cfa1ca9dSYoshinobu Inoue }; 123cfa1ca9dSYoshinobu Inoue struct udp_ip6 { 124cfa1ca9dSYoshinobu Inoue struct ip6_hdr uip6_ip6; 125cfa1ca9dSYoshinobu Inoue u_char uip6_init_done : 1; 126cfa1ca9dSYoshinobu Inoue } udp_ip6; 127cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 128df8bae1dSRodney W. Grimes 1294d77a549SAlfred Perlstein static void udp_append(struct inpcb *last, struct ip *ip, 1304d77a549SAlfred Perlstein struct mbuf *n, int off); 131cfa1ca9dSYoshinobu Inoue #ifdef INET6 1324d77a549SAlfred Perlstein static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip); 133cfa1ca9dSYoshinobu Inoue #endif 134cfa1ca9dSYoshinobu Inoue 1354d77a549SAlfred Perlstein static int udp_detach(struct socket *so); 1364d77a549SAlfred Perlstein static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 1374d77a549SAlfred Perlstein struct mbuf *, struct thread *); 138df8bae1dSRodney W. Grimes 139df8bae1dSRodney W. Grimes void 140df8bae1dSRodney W. Grimes udp_init() 141df8bae1dSRodney W. Grimes { 14215bd2b43SDavid Greenman LIST_INIT(&udb); 14315bd2b43SDavid Greenman udbinfo.listhead = &udb; 144ddd79a97SDavid Greenman udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 1458781d8e9SBruce Evans udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 1468781d8e9SBruce Evans &udbinfo.porthashmask); 14769c2d429SJeff Roberson udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL, 14869c2d429SJeff Roberson NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 14969c2d429SJeff Roberson uma_zone_set_max(udbinfo.ipi_zone, maxsockets); 150df8bae1dSRodney W. Grimes } 151df8bae1dSRodney W. Grimes 152df8bae1dSRodney W. Grimes void 153f0ffb944SJulian Elischer udp_input(m, off) 154df8bae1dSRodney W. Grimes register struct mbuf *m; 155f0ffb944SJulian Elischer int off; 156df8bae1dSRodney W. Grimes { 157cfa1ca9dSYoshinobu Inoue int iphlen = off; 158df8bae1dSRodney W. Grimes register struct ip *ip; 159df8bae1dSRodney W. Grimes register struct udphdr *uh; 160df8bae1dSRodney W. Grimes register struct inpcb *inp; 161df8bae1dSRodney W. Grimes struct mbuf *opts = 0; 162df8bae1dSRodney W. Grimes int len; 163df8bae1dSRodney W. Grimes struct ip save_ip; 164cfa1ca9dSYoshinobu Inoue struct sockaddr *append_sa; 165df8bae1dSRodney W. Grimes 166df8bae1dSRodney W. Grimes udpstat.udps_ipackets++; 167df8bae1dSRodney W. Grimes 168df8bae1dSRodney W. Grimes /* 169df8bae1dSRodney W. Grimes * Strip IP options, if any; should skip this, 170df8bae1dSRodney W. Grimes * make available to user, and use on returned packets, 171df8bae1dSRodney W. Grimes * but we don't yet have a way to check the checksum 172df8bae1dSRodney W. Grimes * with options still present. 173df8bae1dSRodney W. Grimes */ 174df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 175df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 176df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 177df8bae1dSRodney W. Grimes } 178df8bae1dSRodney W. Grimes 179df8bae1dSRodney W. Grimes /* 180df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 181df8bae1dSRodney W. Grimes */ 182df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 183df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 184df8bae1dSRodney W. Grimes if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 185df8bae1dSRodney W. Grimes udpstat.udps_hdrops++; 186df8bae1dSRodney W. Grimes return; 187df8bae1dSRodney W. Grimes } 188df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 189df8bae1dSRodney W. Grimes } 190df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 191df8bae1dSRodney W. Grimes 192686cdd19SJun-ichiro itojun Hagino /* destination port of 0 is illegal, based on RFC768. */ 193686cdd19SJun-ichiro itojun Hagino if (uh->uh_dport == 0) 194686cdd19SJun-ichiro itojun Hagino goto bad; 195686cdd19SJun-ichiro itojun Hagino 196df8bae1dSRodney W. Grimes /* 197df8bae1dSRodney W. Grimes * Make mbuf data length reflect UDP length. 198df8bae1dSRodney W. Grimes * If not enough data to reflect UDP length, drop. 199df8bae1dSRodney W. Grimes */ 200df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 201df8bae1dSRodney W. Grimes if (ip->ip_len != len) { 2027eb7a449SAndras Olah if (len > ip->ip_len || len < sizeof(struct udphdr)) { 203df8bae1dSRodney W. Grimes udpstat.udps_badlen++; 204df8bae1dSRodney W. Grimes goto bad; 205df8bae1dSRodney W. Grimes } 206df8bae1dSRodney W. Grimes m_adj(m, len - ip->ip_len); 207df8bae1dSRodney W. Grimes /* ip->ip_len = len; */ 208df8bae1dSRodney W. Grimes } 209df8bae1dSRodney W. Grimes /* 210df8bae1dSRodney W. Grimes * Save a copy of the IP header in case we want restore it 211df8bae1dSRodney W. Grimes * for sending an ICMP error message in response. 212df8bae1dSRodney W. Grimes */ 21348cb400fSRuslan Ermilov if (!blackhole) 214df8bae1dSRodney W. Grimes save_ip = *ip; 215df8bae1dSRodney W. Grimes 216df8bae1dSRodney W. Grimes /* 217df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 218df8bae1dSRodney W. Grimes */ 2196dfab5b1SGarrett Wollman if (uh->uh_sum) { 220db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 221db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 222db4f9cc7SJonathan Lemon uh->uh_sum = m->m_pkthdr.csum_data; 223db4f9cc7SJonathan Lemon else 224db4f9cc7SJonathan Lemon uh->uh_sum = in_pseudo(ip->ip_src.s_addr, 225506f4949SRuslan Ermilov ip->ip_dst.s_addr, htonl((u_short)len + 226db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data + IPPROTO_UDP)); 227db4f9cc7SJonathan Lemon uh->uh_sum ^= 0xffff; 228db4f9cc7SJonathan Lemon } else { 229cb342100SHajimu UMEMOTO char b[9]; 230cb342100SHajimu UMEMOTO bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 2316effc713SDoug Rabson bzero(((struct ipovly *)ip)->ih_x1, 9); 232df8bae1dSRodney W. Grimes ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 233623ae52eSPoul-Henning Kamp uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 234cb342100SHajimu UMEMOTO bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 235db4f9cc7SJonathan Lemon } 236623ae52eSPoul-Henning Kamp if (uh->uh_sum) { 237df8bae1dSRodney W. Grimes udpstat.udps_badsum++; 238df8bae1dSRodney W. Grimes m_freem(m); 239df8bae1dSRodney W. Grimes return; 240df8bae1dSRodney W. Grimes } 241fb9aaba0SRuslan Ermilov } else 242fb9aaba0SRuslan Ermilov udpstat.udps_nosum++; 243df8bae1dSRodney W. Grimes 244df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 245df8bae1dSRodney W. Grimes in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 24682c23ebaSBill Fenner struct inpcb *last; 247df8bae1dSRodney W. Grimes /* 248df8bae1dSRodney W. Grimes * Deliver a multicast or broadcast datagram to *all* sockets 249df8bae1dSRodney W. Grimes * for which the local and remote addresses and ports match 250df8bae1dSRodney W. Grimes * those of the incoming datagram. This allows more than 251df8bae1dSRodney W. Grimes * one process to receive multi/broadcasts on the same port. 252df8bae1dSRodney W. Grimes * (This really ought to be done for unicast datagrams as 253df8bae1dSRodney W. Grimes * well, but that would cause problems with existing 254df8bae1dSRodney W. Grimes * applications that open both address-specific sockets and 255df8bae1dSRodney W. Grimes * a wildcard socket listening to the same port -- they would 256df8bae1dSRodney W. Grimes * end up receiving duplicates of every unicast datagram. 257df8bae1dSRodney W. Grimes * Those applications open the multiple sockets to overcome an 258df8bae1dSRodney W. Grimes * inadequacy of the UDP socket interface, but for backwards 259df8bae1dSRodney W. Grimes * compatibility we avoid the problem here rather than 260df8bae1dSRodney W. Grimes * fixing the interface. Maybe 4.5BSD will remedy this?) 261df8bae1dSRodney W. Grimes */ 262df8bae1dSRodney W. Grimes 263df8bae1dSRodney W. Grimes /* 264df8bae1dSRodney W. Grimes * Construct sockaddr format source address. 265df8bae1dSRodney W. Grimes */ 266df8bae1dSRodney W. Grimes udp_in.sin_port = uh->uh_sport; 267df8bae1dSRodney W. Grimes udp_in.sin_addr = ip->ip_src; 268df8bae1dSRodney W. Grimes /* 269df8bae1dSRodney W. Grimes * Locate pcb(s) for datagram. 270df8bae1dSRodney W. Grimes * (Algorithm copied from raw_intr().) 271df8bae1dSRodney W. Grimes */ 272df8bae1dSRodney W. Grimes last = NULL; 273cfa1ca9dSYoshinobu Inoue #ifdef INET6 274cfa1ca9dSYoshinobu Inoue udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; 275cfa1ca9dSYoshinobu Inoue #endif 276cfa1ca9dSYoshinobu Inoue LIST_FOREACH(inp, &udb, inp_list) { 277cfa1ca9dSYoshinobu Inoue #ifdef INET6 278369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 279cfa1ca9dSYoshinobu Inoue continue; 280cfa1ca9dSYoshinobu Inoue #endif 281df8bae1dSRodney W. Grimes if (inp->inp_lport != uh->uh_dport) 282df8bae1dSRodney W. Grimes continue; 283df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr != INADDR_ANY) { 284df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr != 285df8bae1dSRodney W. Grimes ip->ip_dst.s_addr) 286df8bae1dSRodney W. Grimes continue; 287df8bae1dSRodney W. Grimes } 288df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 289df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != 290df8bae1dSRodney W. Grimes ip->ip_src.s_addr || 291df8bae1dSRodney W. Grimes inp->inp_fport != uh->uh_sport) 292df8bae1dSRodney W. Grimes continue; 293df8bae1dSRodney W. Grimes } 294df8bae1dSRodney W. Grimes 295df8bae1dSRodney W. Grimes if (last != NULL) { 296df8bae1dSRodney W. Grimes struct mbuf *n; 297df8bae1dSRodney W. Grimes 298cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 299cfa1ca9dSYoshinobu Inoue /* check AH/ESP integrity. */ 300cfa1ca9dSYoshinobu Inoue if (ipsec4_in_reject_so(m, last->inp_socket)) 301cfa1ca9dSYoshinobu Inoue ipsecstat.in_polvio++; 302cfa1ca9dSYoshinobu Inoue /* do not inject data to pcb */ 303cfa1ca9dSYoshinobu Inoue else 304cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 305cfa1ca9dSYoshinobu Inoue if ((n = m_copy(m, 0, M_COPYALL)) != NULL) 306cfa1ca9dSYoshinobu Inoue udp_append(last, ip, n, 307cfa1ca9dSYoshinobu Inoue iphlen + 308cfa1ca9dSYoshinobu Inoue sizeof(struct udphdr)); 309df8bae1dSRodney W. Grimes } 31082c23ebaSBill Fenner last = inp; 311df8bae1dSRodney W. Grimes /* 312df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 313df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 314df8bae1dSRodney W. Grimes * socket options set. This heuristic avoids searching 315df8bae1dSRodney W. Grimes * through all pcbs in the common case of a non-shared 316df8bae1dSRodney W. Grimes * port. It * assumes that an application will never 317df8bae1dSRodney W. Grimes * clear these options after setting them. 318df8bae1dSRodney W. Grimes */ 319694ad0a9SSteve Price if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 320df8bae1dSRodney W. Grimes break; 321df8bae1dSRodney W. Grimes } 322df8bae1dSRodney W. Grimes 323df8bae1dSRodney W. Grimes if (last == NULL) { 324df8bae1dSRodney W. Grimes /* 325df8bae1dSRodney W. Grimes * No matching pcb found; discard datagram. 326df8bae1dSRodney W. Grimes * (No need to send an ICMP Port Unreachable 327df8bae1dSRodney W. Grimes * for a broadcast or multicast datgram.) 328df8bae1dSRodney W. Grimes */ 329df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 330df8bae1dSRodney W. Grimes goto bad; 331df8bae1dSRodney W. Grimes } 332cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 333cfa1ca9dSYoshinobu Inoue /* check AH/ESP integrity. */ 334cfa1ca9dSYoshinobu Inoue if (ipsec4_in_reject_so(m, last->inp_socket)) { 335cfa1ca9dSYoshinobu Inoue ipsecstat.in_polvio++; 336df8bae1dSRodney W. Grimes goto bad; 337df8bae1dSRodney W. Grimes } 338cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 339cfa1ca9dSYoshinobu Inoue udp_append(last, ip, m, iphlen + sizeof(struct udphdr)); 340df8bae1dSRodney W. Grimes return; 341df8bae1dSRodney W. Grimes } 342df8bae1dSRodney W. Grimes /* 3436d6a026bSDavid Greenman * Locate pcb for datagram. 344df8bae1dSRodney W. Grimes */ 345c3229e05SDavid Greenman inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 346cfa1ca9dSYoshinobu Inoue ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 34715bd2b43SDavid Greenman if (inp == NULL) { 34875cfc95fSAndrey A. Chernov if (log_in_vain) { 349df5c0b8aSBill Fenner char buf[4*sizeof "123"]; 35075cfc95fSAndrey A. Chernov 35175cfc95fSAndrey A. Chernov strcpy(buf, inet_ntoa(ip->ip_dst)); 352592071e8SBruce Evans log(LOG_INFO, 353592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 354592071e8SBruce Evans buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 355592071e8SBruce Evans ntohs(uh->uh_sport)); 35675cfc95fSAndrey A. Chernov } 357df8bae1dSRodney W. Grimes udpstat.udps_noport++; 358df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 359df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 360df8bae1dSRodney W. Grimes goto bad; 361df8bae1dSRodney W. Grimes } 362a57815efSBosko Milekic if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 36351508de1SMatthew Dillon goto bad; 364582a7760SBruce Evans if (blackhole) 36512b4fd06SPoul-Henning Kamp goto bad; 36604287599SRuslan Ermilov *ip = save_ip; 36704287599SRuslan Ermilov ip->ip_len += iphlen; 368582a7760SBruce Evans icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 369df8bae1dSRodney W. Grimes return; 370df8bae1dSRodney W. Grimes } 371cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 372cfa1ca9dSYoshinobu Inoue if (ipsec4_in_reject_so(m, inp->inp_socket)) { 373cfa1ca9dSYoshinobu Inoue ipsecstat.in_polvio++; 374cfa1ca9dSYoshinobu Inoue goto bad; 375cfa1ca9dSYoshinobu Inoue } 376cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 377df8bae1dSRodney W. Grimes 378df8bae1dSRodney W. Grimes /* 379df8bae1dSRodney W. Grimes * Construct sockaddr format source address. 380df8bae1dSRodney W. Grimes * Stuff source address and datagram in user buffer. 381df8bae1dSRodney W. Grimes */ 382df8bae1dSRodney W. Grimes udp_in.sin_port = uh->uh_sport; 383df8bae1dSRodney W. Grimes udp_in.sin_addr = ip->ip_src; 38482dab6ceSGarrett Wollman if (inp->inp_flags & INP_CONTROLOPTS 385cfa1ca9dSYoshinobu Inoue || inp->inp_socket->so_options & SO_TIMESTAMP) { 386cfa1ca9dSYoshinobu Inoue #ifdef INET6 387cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) { 388cfa1ca9dSYoshinobu Inoue int savedflags; 389cfa1ca9dSYoshinobu Inoue 390cfa1ca9dSYoshinobu Inoue ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 391cfa1ca9dSYoshinobu Inoue savedflags = inp->inp_flags; 392cfa1ca9dSYoshinobu Inoue inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 393cfa1ca9dSYoshinobu Inoue ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m); 394cfa1ca9dSYoshinobu Inoue inp->inp_flags = savedflags; 395cfa1ca9dSYoshinobu Inoue } else 396cfa1ca9dSYoshinobu Inoue #endif 39782c23ebaSBill Fenner ip_savecontrol(inp, &opts, ip, m); 398cfa1ca9dSYoshinobu Inoue } 39933841545SHajimu UMEMOTO m_adj(m, iphlen + sizeof(struct udphdr)); 400cfa1ca9dSYoshinobu Inoue #ifdef INET6 401cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) { 402cfa1ca9dSYoshinobu Inoue in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 403cfa1ca9dSYoshinobu Inoue append_sa = (struct sockaddr *)&udp_in6; 404cfa1ca9dSYoshinobu Inoue } else 405cfa1ca9dSYoshinobu Inoue #endif 406cfa1ca9dSYoshinobu Inoue append_sa = (struct sockaddr *)&udp_in; 407cfa1ca9dSYoshinobu Inoue if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) { 408df8bae1dSRodney W. Grimes udpstat.udps_fullsock++; 409df8bae1dSRodney W. Grimes goto bad; 410df8bae1dSRodney W. Grimes } 411df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 412df8bae1dSRodney W. Grimes return; 413df8bae1dSRodney W. Grimes bad: 414df8bae1dSRodney W. Grimes m_freem(m); 415df8bae1dSRodney W. Grimes if (opts) 416df8bae1dSRodney W. Grimes m_freem(opts); 417cfa1ca9dSYoshinobu Inoue return; 418cfa1ca9dSYoshinobu Inoue } 419cfa1ca9dSYoshinobu Inoue 420686cdd19SJun-ichiro itojun Hagino #ifdef INET6 421cfa1ca9dSYoshinobu Inoue static void 422cfa1ca9dSYoshinobu Inoue ip_2_ip6_hdr(ip6, ip) 423cfa1ca9dSYoshinobu Inoue struct ip6_hdr *ip6; 424cfa1ca9dSYoshinobu Inoue struct ip *ip; 425cfa1ca9dSYoshinobu Inoue { 426cfa1ca9dSYoshinobu Inoue bzero(ip6, sizeof(*ip6)); 427cfa1ca9dSYoshinobu Inoue 428cfa1ca9dSYoshinobu Inoue ip6->ip6_vfc = IPV6_VERSION; 429cfa1ca9dSYoshinobu Inoue ip6->ip6_plen = ip->ip_len; 430cfa1ca9dSYoshinobu Inoue ip6->ip6_nxt = ip->ip_p; 431cfa1ca9dSYoshinobu Inoue ip6->ip6_hlim = ip->ip_ttl; 432cfa1ca9dSYoshinobu Inoue ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = 433cfa1ca9dSYoshinobu Inoue IPV6_ADDR_INT32_SMP; 434cfa1ca9dSYoshinobu Inoue ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; 435cfa1ca9dSYoshinobu Inoue ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; 436cfa1ca9dSYoshinobu Inoue } 437cfa1ca9dSYoshinobu Inoue #endif 438cfa1ca9dSYoshinobu Inoue 439cfa1ca9dSYoshinobu Inoue /* 440cfa1ca9dSYoshinobu Inoue * subroutine of udp_input(), mainly for source code readability. 441cfa1ca9dSYoshinobu Inoue * caller must properly init udp_ip6 and udp_in6 beforehand. 442cfa1ca9dSYoshinobu Inoue */ 443cfa1ca9dSYoshinobu Inoue static void 444cfa1ca9dSYoshinobu Inoue udp_append(last, ip, n, off) 445cfa1ca9dSYoshinobu Inoue struct inpcb *last; 446cfa1ca9dSYoshinobu Inoue struct ip *ip; 447cfa1ca9dSYoshinobu Inoue struct mbuf *n; 448cfa1ca9dSYoshinobu Inoue int off; 449cfa1ca9dSYoshinobu Inoue { 450cfa1ca9dSYoshinobu Inoue struct sockaddr *append_sa; 451cfa1ca9dSYoshinobu Inoue struct mbuf *opts = 0; 452cfa1ca9dSYoshinobu Inoue 453cfa1ca9dSYoshinobu Inoue if (last->inp_flags & INP_CONTROLOPTS || 454cfa1ca9dSYoshinobu Inoue last->inp_socket->so_options & SO_TIMESTAMP) { 455cfa1ca9dSYoshinobu Inoue #ifdef INET6 456cfa1ca9dSYoshinobu Inoue if (last->inp_vflag & INP_IPV6) { 457cfa1ca9dSYoshinobu Inoue int savedflags; 458cfa1ca9dSYoshinobu Inoue 459cfa1ca9dSYoshinobu Inoue if (udp_ip6.uip6_init_done == 0) { 460cfa1ca9dSYoshinobu Inoue ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 461cfa1ca9dSYoshinobu Inoue udp_ip6.uip6_init_done = 1; 462cfa1ca9dSYoshinobu Inoue } 463cfa1ca9dSYoshinobu Inoue savedflags = last->inp_flags; 464cfa1ca9dSYoshinobu Inoue last->inp_flags &= ~INP_UNMAPPABLEOPTS; 465cfa1ca9dSYoshinobu Inoue ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n); 466cfa1ca9dSYoshinobu Inoue last->inp_flags = savedflags; 467cfa1ca9dSYoshinobu Inoue } else 468cfa1ca9dSYoshinobu Inoue #endif 469cfa1ca9dSYoshinobu Inoue ip_savecontrol(last, &opts, ip, n); 470cfa1ca9dSYoshinobu Inoue } 471cfa1ca9dSYoshinobu Inoue #ifdef INET6 472cfa1ca9dSYoshinobu Inoue if (last->inp_vflag & INP_IPV6) { 473cfa1ca9dSYoshinobu Inoue if (udp_in6.uin6_init_done == 0) { 474cfa1ca9dSYoshinobu Inoue in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 475cfa1ca9dSYoshinobu Inoue udp_in6.uin6_init_done = 1; 476cfa1ca9dSYoshinobu Inoue } 477cfa1ca9dSYoshinobu Inoue append_sa = (struct sockaddr *)&udp_in6.uin6_sin; 478cfa1ca9dSYoshinobu Inoue } else 479cfa1ca9dSYoshinobu Inoue #endif 480cfa1ca9dSYoshinobu Inoue append_sa = (struct sockaddr *)&udp_in; 481cfa1ca9dSYoshinobu Inoue m_adj(n, off); 482cfa1ca9dSYoshinobu Inoue if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) { 483cfa1ca9dSYoshinobu Inoue m_freem(n); 484cfa1ca9dSYoshinobu Inoue if (opts) 485cfa1ca9dSYoshinobu Inoue m_freem(opts); 486cfa1ca9dSYoshinobu Inoue udpstat.udps_fullsock++; 487cfa1ca9dSYoshinobu Inoue } else 488cfa1ca9dSYoshinobu Inoue sorwakeup(last->inp_socket); 489df8bae1dSRodney W. Grimes } 490df8bae1dSRodney W. Grimes 491df8bae1dSRodney W. Grimes /* 492df8bae1dSRodney W. Grimes * Notify a udp user of an asynchronous error; 493df8bae1dSRodney W. Grimes * just wake up so that he can collect error status. 494df8bae1dSRodney W. Grimes */ 49576429de4SYoshinobu Inoue void 496df8bae1dSRodney W. Grimes udp_notify(inp, errno) 497df8bae1dSRodney W. Grimes register struct inpcb *inp; 498df8bae1dSRodney W. Grimes int errno; 499df8bae1dSRodney W. Grimes { 500df8bae1dSRodney W. Grimes inp->inp_socket->so_error = errno; 501df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 502df8bae1dSRodney W. Grimes sowwakeup(inp->inp_socket); 503df8bae1dSRodney W. Grimes } 504df8bae1dSRodney W. Grimes 505df8bae1dSRodney W. Grimes void 506b62d102cSBruce Evans udp_ctlinput(cmd, sa, vip) 507df8bae1dSRodney W. Grimes int cmd; 508df8bae1dSRodney W. Grimes struct sockaddr *sa; 509b62d102cSBruce Evans void *vip; 510df8bae1dSRodney W. Grimes { 511c693a045SJonathan Lemon struct ip *ip = vip; 512c693a045SJonathan Lemon struct udphdr *uh; 5134d77a549SAlfred Perlstein void (*notify)(struct inpcb *, int) = udp_notify; 514c693a045SJonathan Lemon struct in_addr faddr; 515c693a045SJonathan Lemon struct inpcb *inp; 516c693a045SJonathan Lemon int s; 517c693a045SJonathan Lemon 518c693a045SJonathan Lemon faddr = ((struct sockaddr_in *)sa)->sin_addr; 519c693a045SJonathan Lemon if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 520c693a045SJonathan Lemon return; 521df8bae1dSRodney W. Grimes 522d1c54148SJesper Skriver if (PRC_IS_REDIRECT(cmd)) { 523d1c54148SJesper Skriver ip = 0; 524d1c54148SJesper Skriver notify = in_rtchange; 525d1c54148SJesper Skriver } else if (cmd == PRC_HOSTDEAD) 526d1c54148SJesper Skriver ip = 0; 527d1c54148SJesper Skriver else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 528df8bae1dSRodney W. Grimes return; 529df8bae1dSRodney W. Grimes if (ip) { 530c693a045SJonathan Lemon s = splnet(); 531df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 532c693a045SJonathan Lemon inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, 533c693a045SJonathan Lemon ip->ip_src, uh->uh_sport, 0, NULL); 534c693a045SJonathan Lemon if (inp != NULL && inp->inp_socket != NULL) 535c693a045SJonathan Lemon (*notify)(inp, inetctlerrmap[cmd]); 536c693a045SJonathan Lemon splx(s); 537df8bae1dSRodney W. Grimes } else 538c693a045SJonathan Lemon in_pcbnotifyall(&udb, faddr, inetctlerrmap[cmd], notify); 539df8bae1dSRodney W. Grimes } 540df8bae1dSRodney W. Grimes 5410312fbe9SPoul-Henning Kamp static int 54282d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS) 54398271db4SGarrett Wollman { 54498271db4SGarrett Wollman int error, i, n, s; 54598271db4SGarrett Wollman struct inpcb *inp, **inp_list; 54698271db4SGarrett Wollman inp_gen_t gencnt; 54798271db4SGarrett Wollman struct xinpgen xig; 54898271db4SGarrett Wollman 54998271db4SGarrett Wollman /* 55098271db4SGarrett Wollman * The process of preparing the TCB list is too time-consuming and 55198271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 55298271db4SGarrett Wollman */ 55398271db4SGarrett Wollman if (req->oldptr == 0) { 55498271db4SGarrett Wollman n = udbinfo.ipi_count; 55598271db4SGarrett Wollman req->oldidx = 2 * (sizeof xig) 55698271db4SGarrett Wollman + (n + n/8) * sizeof(struct xinpcb); 55798271db4SGarrett Wollman return 0; 55898271db4SGarrett Wollman } 55998271db4SGarrett Wollman 56098271db4SGarrett Wollman if (req->newptr != 0) 56198271db4SGarrett Wollman return EPERM; 56298271db4SGarrett Wollman 56398271db4SGarrett Wollman /* 56498271db4SGarrett Wollman * OK, now we're committed to doing something. 56598271db4SGarrett Wollman */ 56698271db4SGarrett Wollman s = splnet(); 56798271db4SGarrett Wollman gencnt = udbinfo.ipi_gencnt; 56898271db4SGarrett Wollman n = udbinfo.ipi_count; 56998271db4SGarrett Wollman splx(s); 57098271db4SGarrett Wollman 57198271db4SGarrett Wollman xig.xig_len = sizeof xig; 57298271db4SGarrett Wollman xig.xig_count = n; 57398271db4SGarrett Wollman xig.xig_gen = gencnt; 57498271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 57598271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 57698271db4SGarrett Wollman if (error) 57798271db4SGarrett Wollman return error; 57898271db4SGarrett Wollman 57998271db4SGarrett Wollman inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 58098271db4SGarrett Wollman if (inp_list == 0) 58198271db4SGarrett Wollman return ENOMEM; 58298271db4SGarrett Wollman 58398271db4SGarrett Wollman s = splnet(); 584fc2ffbe6SPoul-Henning Kamp for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n; 585fc2ffbe6SPoul-Henning Kamp inp = LIST_NEXT(inp, inp_list)) { 5868a7d8cc6SRobert Watson if (inp->inp_gencnt <= gencnt) { 587a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred, 5888a7d8cc6SRobert Watson inp->inp_socket->so_cred)) 5894787fd37SPaul Saab continue; 59098271db4SGarrett Wollman inp_list[i++] = inp; 59198271db4SGarrett Wollman } 5924787fd37SPaul Saab } 59398271db4SGarrett Wollman splx(s); 59498271db4SGarrett Wollman n = i; 59598271db4SGarrett Wollman 59698271db4SGarrett Wollman error = 0; 59798271db4SGarrett Wollman for (i = 0; i < n; i++) { 59898271db4SGarrett Wollman inp = inp_list[i]; 59998271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 60098271db4SGarrett Wollman struct xinpcb xi; 60198271db4SGarrett Wollman xi.xi_len = sizeof xi; 60298271db4SGarrett Wollman /* XXX should avoid extra copy */ 60398271db4SGarrett Wollman bcopy(inp, &xi.xi_inp, sizeof *inp); 60498271db4SGarrett Wollman if (inp->inp_socket) 60598271db4SGarrett Wollman sotoxsocket(inp->inp_socket, &xi.xi_socket); 60698271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 60798271db4SGarrett Wollman } 60898271db4SGarrett Wollman } 60998271db4SGarrett Wollman if (!error) { 61098271db4SGarrett Wollman /* 61198271db4SGarrett Wollman * Give the user an updated idea of our state. 61298271db4SGarrett Wollman * If the generation differs from what we told 61398271db4SGarrett Wollman * her before, she knows that something happened 61498271db4SGarrett Wollman * while we were processing this request, and it 61598271db4SGarrett Wollman * might be necessary to retry. 61698271db4SGarrett Wollman */ 61798271db4SGarrett Wollman s = splnet(); 61898271db4SGarrett Wollman xig.xig_gen = udbinfo.ipi_gencnt; 61998271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 62098271db4SGarrett Wollman xig.xig_count = udbinfo.ipi_count; 62198271db4SGarrett Wollman splx(s); 62298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 62398271db4SGarrett Wollman } 62498271db4SGarrett Wollman free(inp_list, M_TEMP); 62598271db4SGarrett Wollman return error; 62698271db4SGarrett Wollman } 62798271db4SGarrett Wollman 62898271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 62998271db4SGarrett Wollman udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 63098271db4SGarrett Wollman 63198271db4SGarrett Wollman static int 63282d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS) 633490d50b6SBrian Feldman { 634c0511d3bSBrian Feldman struct xucred xuc; 635490d50b6SBrian Feldman struct sockaddr_in addrs[2]; 636490d50b6SBrian Feldman struct inpcb *inp; 637490d50b6SBrian Feldman int error, s; 638490d50b6SBrian Feldman 639ce178806SRobert Watson error = suser_xxx(0, req->td->td_proc, PRISON_ROOT); 640490d50b6SBrian Feldman if (error) 641490d50b6SBrian Feldman return (error); 642490d50b6SBrian Feldman error = SYSCTL_IN(req, addrs, sizeof(addrs)); 643490d50b6SBrian Feldman if (error) 644490d50b6SBrian Feldman return (error); 645490d50b6SBrian Feldman s = splnet(); 646490d50b6SBrian Feldman inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 647cfa1ca9dSYoshinobu Inoue addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 6482f9a2132SBrian Feldman if (inp == NULL || inp->inp_socket == NULL) { 649490d50b6SBrian Feldman error = ENOENT; 650490d50b6SBrian Feldman goto out; 651490d50b6SBrian Feldman } 652a854ed98SJohn Baldwin error = cr_cansee(req->td->td_ucred, inp->inp_socket->so_cred); 6537ce87f12SDavid Malone if (error) 6547ce87f12SDavid Malone goto out; 65576183f34SDima Dorfman cru2x(inp->inp_socket->so_cred, &xuc); 656c0511d3bSBrian Feldman error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 657490d50b6SBrian Feldman out: 658490d50b6SBrian Feldman splx(s); 659490d50b6SBrian Feldman return (error); 660490d50b6SBrian Feldman } 661490d50b6SBrian Feldman 6627ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 6637ce87f12SDavid Malone CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 6647ce87f12SDavid Malone udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 665490d50b6SBrian Feldman 666490d50b6SBrian Feldman static int 667b40ce416SJulian Elischer udp_output(inp, m, addr, control, td) 668df8bae1dSRodney W. Grimes register struct inpcb *inp; 669d25f3712SBrian Feldman struct mbuf *m; 67057bf258eSGarrett Wollman struct sockaddr *addr; 67157bf258eSGarrett Wollman struct mbuf *control; 672b40ce416SJulian Elischer struct thread *td; 673df8bae1dSRodney W. Grimes { 674df8bae1dSRodney W. Grimes register struct udpiphdr *ui; 675df8bae1dSRodney W. Grimes register int len = m->m_pkthdr.len; 676df8bae1dSRodney W. Grimes struct in_addr laddr; 67775c13541SPoul-Henning Kamp struct sockaddr_in *sin; 67826f9a767SRodney W. Grimes int s = 0, error = 0; 679df8bae1dSRodney W. Grimes 680df8bae1dSRodney W. Grimes if (control) 681df8bae1dSRodney W. Grimes m_freem(control); /* XXX */ 682df8bae1dSRodney W. Grimes 683430d30d8SBill Fenner if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 684430d30d8SBill Fenner error = EMSGSIZE; 685430d30d8SBill Fenner goto release; 686430d30d8SBill Fenner } 687430d30d8SBill Fenner 688df8bae1dSRodney W. Grimes if (addr) { 68975c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)addr; 690a854ed98SJohn Baldwin if (td && jailed(td->td_ucred)) 691a854ed98SJohn Baldwin prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr); 692df8bae1dSRodney W. Grimes laddr = inp->inp_laddr; 693df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 694df8bae1dSRodney W. Grimes error = EISCONN; 695df8bae1dSRodney W. Grimes goto release; 696df8bae1dSRodney W. Grimes } 697df8bae1dSRodney W. Grimes /* 698df8bae1dSRodney W. Grimes * Must block input while temporarily connected. 699df8bae1dSRodney W. Grimes */ 700df8bae1dSRodney W. Grimes s = splnet(); 701b40ce416SJulian Elischer error = in_pcbconnect(inp, addr, td); 702df8bae1dSRodney W. Grimes if (error) { 703df8bae1dSRodney W. Grimes splx(s); 704df8bae1dSRodney W. Grimes goto release; 705df8bae1dSRodney W. Grimes } 706df8bae1dSRodney W. Grimes } else { 707df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr == INADDR_ANY) { 708df8bae1dSRodney W. Grimes error = ENOTCONN; 709df8bae1dSRodney W. Grimes goto release; 710df8bae1dSRodney W. Grimes } 711df8bae1dSRodney W. Grimes } 712df8bae1dSRodney W. Grimes /* 713df8bae1dSRodney W. Grimes * Calculate data length and get a mbuf 714df8bae1dSRodney W. Grimes * for UDP and IP headers. 715df8bae1dSRodney W. Grimes */ 716df8bae1dSRodney W. Grimes M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 717df8bae1dSRodney W. Grimes if (m == 0) { 718df8bae1dSRodney W. Grimes error = ENOBUFS; 7191c09f774SGarrett Wollman if (addr) 7201c09f774SGarrett Wollman splx(s); 721df8bae1dSRodney W. Grimes goto release; 722df8bae1dSRodney W. Grimes } 723df8bae1dSRodney W. Grimes 724df8bae1dSRodney W. Grimes /* 725df8bae1dSRodney W. Grimes * Fill in mbuf with extended UDP header 726df8bae1dSRodney W. Grimes * and addresses and length put into network format. 727df8bae1dSRodney W. Grimes */ 728df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 729db4f9cc7SJonathan Lemon bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 730df8bae1dSRodney W. Grimes ui->ui_pr = IPPROTO_UDP; 731df8bae1dSRodney W. Grimes ui->ui_src = inp->inp_laddr; 732df8bae1dSRodney W. Grimes ui->ui_dst = inp->inp_faddr; 733df8bae1dSRodney W. Grimes ui->ui_sport = inp->inp_lport; 734df8bae1dSRodney W. Grimes ui->ui_dport = inp->inp_fport; 735db4f9cc7SJonathan Lemon ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 736df8bae1dSRodney W. Grimes 737df8bae1dSRodney W. Grimes /* 738db4f9cc7SJonathan Lemon * Set up checksum and output datagram. 739df8bae1dSRodney W. Grimes */ 740df8bae1dSRodney W. Grimes if (udpcksum) { 741db4f9cc7SJonathan Lemon ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, 742db4f9cc7SJonathan Lemon htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 743db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_UDP; 744db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 745db4f9cc7SJonathan Lemon } else { 746db4f9cc7SJonathan Lemon ui->ui_sum = 0; 747df8bae1dSRodney W. Grimes } 748df8bae1dSRodney W. Grimes ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 749ca98b82cSDavid Greenman ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 750ca98b82cSDavid Greenman ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 751df8bae1dSRodney W. Grimes udpstat.udps_opackets++; 752cfa1ca9dSYoshinobu Inoue 753cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 75433841545SHajimu UMEMOTO if (ipsec_setsocket(m, inp->inp_socket) != 0) { 75533841545SHajimu UMEMOTO error = ENOBUFS; 75633841545SHajimu UMEMOTO goto release; 75733841545SHajimu UMEMOTO } 758cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 759df8bae1dSRodney W. Grimes error = ip_output(m, inp->inp_options, &inp->inp_route, 760686cdd19SJun-ichiro itojun Hagino (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)), 761df8bae1dSRodney W. Grimes inp->inp_moptions); 762df8bae1dSRodney W. Grimes 763df8bae1dSRodney W. Grimes if (addr) { 764df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 76557bf258eSGarrett Wollman inp->inp_laddr = laddr; /* XXX rehash? */ 766df8bae1dSRodney W. Grimes splx(s); 767df8bae1dSRodney W. Grimes } 768df8bae1dSRodney W. Grimes return (error); 769df8bae1dSRodney W. Grimes 770df8bae1dSRodney W. Grimes release: 771df8bae1dSRodney W. Grimes m_freem(m); 772df8bae1dSRodney W. Grimes return (error); 773df8bae1dSRodney W. Grimes } 774df8bae1dSRodney W. Grimes 77576429de4SYoshinobu Inoue u_long udp_sendspace = 9216; /* really max datagram size */ 776df8bae1dSRodney W. Grimes /* 40 1K datagrams */ 7770312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 7783d177f46SBill Fumerola &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 7790312fbe9SPoul-Henning Kamp 780cfa1ca9dSYoshinobu Inoue u_long udp_recvspace = 40 * (1024 + 781cfa1ca9dSYoshinobu Inoue #ifdef INET6 782cfa1ca9dSYoshinobu Inoue sizeof(struct sockaddr_in6) 783cfa1ca9dSYoshinobu Inoue #else 784cfa1ca9dSYoshinobu Inoue sizeof(struct sockaddr_in) 785cfa1ca9dSYoshinobu Inoue #endif 786cfa1ca9dSYoshinobu Inoue ); 7870312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 7883d177f46SBill Fumerola &udp_recvspace, 0, "Maximum incoming UDP datagram size"); 789df8bae1dSRodney W. Grimes 790d0390e05SGarrett Wollman static int 791d0390e05SGarrett Wollman udp_abort(struct socket *so) 792df8bae1dSRodney W. Grimes { 793d0390e05SGarrett Wollman struct inpcb *inp; 794df8bae1dSRodney W. Grimes int s; 795df8bae1dSRodney W. Grimes 796d0390e05SGarrett Wollman inp = sotoinpcb(so); 797d0390e05SGarrett Wollman if (inp == 0) 798d0390e05SGarrett Wollman return EINVAL; /* ??? possible? panic instead? */ 799d0390e05SGarrett Wollman soisdisconnected(so); 800d0390e05SGarrett Wollman s = splnet(); 801d0390e05SGarrett Wollman in_pcbdetach(inp); 802d0390e05SGarrett Wollman splx(s); 803d0390e05SGarrett Wollman return 0; 804df8bae1dSRodney W. Grimes } 805df8bae1dSRodney W. Grimes 806d0390e05SGarrett Wollman static int 807b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td) 808d0390e05SGarrett Wollman { 809d0390e05SGarrett Wollman struct inpcb *inp; 810d0390e05SGarrett Wollman int s, error; 811d0390e05SGarrett Wollman 812d0390e05SGarrett Wollman inp = sotoinpcb(so); 813d0390e05SGarrett Wollman if (inp != 0) 814d0390e05SGarrett Wollman return EINVAL; 815d0390e05SGarrett Wollman 816cfa1ca9dSYoshinobu Inoue error = soreserve(so, udp_sendspace, udp_recvspace); 817cfa1ca9dSYoshinobu Inoue if (error) 818cfa1ca9dSYoshinobu Inoue return error; 819df8bae1dSRodney W. Grimes s = splnet(); 820b40ce416SJulian Elischer error = in_pcballoc(so, &udbinfo, td); 821df8bae1dSRodney W. Grimes splx(s); 822df8bae1dSRodney W. Grimes if (error) 823d0390e05SGarrett Wollman return error; 824cfa1ca9dSYoshinobu Inoue 825cfa1ca9dSYoshinobu Inoue inp = (struct inpcb *)so->so_pcb; 826cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 827cfa1ca9dSYoshinobu Inoue inp->inp_ip_ttl = ip_defttl; 828d0390e05SGarrett Wollman return 0; 829df8bae1dSRodney W. Grimes } 830d0390e05SGarrett Wollman 831d0390e05SGarrett Wollman static int 832b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 833d0390e05SGarrett Wollman { 834d0390e05SGarrett Wollman struct inpcb *inp; 835d0390e05SGarrett Wollman int s, error; 836d0390e05SGarrett Wollman 837d0390e05SGarrett Wollman inp = sotoinpcb(so); 838d0390e05SGarrett Wollman if (inp == 0) 839d0390e05SGarrett Wollman return EINVAL; 840df8bae1dSRodney W. Grimes s = splnet(); 841b40ce416SJulian Elischer error = in_pcbbind(inp, nam, td); 842d0390e05SGarrett Wollman splx(s); 843d0390e05SGarrett Wollman return error; 844d0390e05SGarrett Wollman } 845d0390e05SGarrett Wollman 846d0390e05SGarrett Wollman static int 847b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 848d0390e05SGarrett Wollman { 849d0390e05SGarrett Wollman struct inpcb *inp; 850d0390e05SGarrett Wollman int s, error; 85175c13541SPoul-Henning Kamp struct sockaddr_in *sin; 852d0390e05SGarrett Wollman 853d0390e05SGarrett Wollman inp = sotoinpcb(so); 854d0390e05SGarrett Wollman if (inp == 0) 855d0390e05SGarrett Wollman return EINVAL; 856d0390e05SGarrett Wollman if (inp->inp_faddr.s_addr != INADDR_ANY) 857d0390e05SGarrett Wollman return EISCONN; 858d0390e05SGarrett Wollman s = splnet(); 85975c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)nam; 860a854ed98SJohn Baldwin if (td && jailed(td->td_ucred)) 861a854ed98SJohn Baldwin prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr); 862b40ce416SJulian Elischer error = in_pcbconnect(inp, nam, td); 863df8bae1dSRodney W. Grimes splx(s); 864df8bae1dSRodney W. Grimes if (error == 0) 865df8bae1dSRodney W. Grimes soisconnected(so); 866d0390e05SGarrett Wollman return error; 867df8bae1dSRodney W. Grimes } 868d0390e05SGarrett Wollman 869d0390e05SGarrett Wollman static int 870d0390e05SGarrett Wollman udp_detach(struct socket *so) 871d0390e05SGarrett Wollman { 872d0390e05SGarrett Wollman struct inpcb *inp; 873d0390e05SGarrett Wollman int s; 874d0390e05SGarrett Wollman 875d0390e05SGarrett Wollman inp = sotoinpcb(so); 876d0390e05SGarrett Wollman if (inp == 0) 877d0390e05SGarrett Wollman return EINVAL; 878d0390e05SGarrett Wollman s = splnet(); 879d0390e05SGarrett Wollman in_pcbdetach(inp); 880d0390e05SGarrett Wollman splx(s); 881d0390e05SGarrett Wollman return 0; 882d0390e05SGarrett Wollman } 883d0390e05SGarrett Wollman 884d0390e05SGarrett Wollman static int 885d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 886d0390e05SGarrett Wollman { 887d0390e05SGarrett Wollman struct inpcb *inp; 888d0390e05SGarrett Wollman int s; 889d0390e05SGarrett Wollman 890d0390e05SGarrett Wollman inp = sotoinpcb(so); 891d0390e05SGarrett Wollman if (inp == 0) 892d0390e05SGarrett Wollman return EINVAL; 893d0390e05SGarrett Wollman if (inp->inp_faddr.s_addr == INADDR_ANY) 894d0390e05SGarrett Wollman return ENOTCONN; 895d0390e05SGarrett Wollman 896df8bae1dSRodney W. Grimes s = splnet(); 897df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 898df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 899df8bae1dSRodney W. Grimes splx(s); 900df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTED; /* XXX */ 901d0390e05SGarrett Wollman return 0; 902df8bae1dSRodney W. Grimes } 903df8bae1dSRodney W. Grimes 904d0390e05SGarrett Wollman static int 90557bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 906b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 907d0390e05SGarrett Wollman { 908d0390e05SGarrett Wollman struct inpcb *inp; 909d0390e05SGarrett Wollman 910d0390e05SGarrett Wollman inp = sotoinpcb(so); 911d0390e05SGarrett Wollman if (inp == 0) { 912d0390e05SGarrett Wollman m_freem(m); 913d0390e05SGarrett Wollman return EINVAL; 914d0390e05SGarrett Wollman } 915b40ce416SJulian Elischer return udp_output(inp, m, addr, control, td); 916d0390e05SGarrett Wollman } 917d0390e05SGarrett Wollman 91876429de4SYoshinobu Inoue int 919d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 920d0390e05SGarrett Wollman { 921d0390e05SGarrett Wollman struct inpcb *inp; 922d0390e05SGarrett Wollman 923d0390e05SGarrett Wollman inp = sotoinpcb(so); 924d0390e05SGarrett Wollman if (inp == 0) 925d0390e05SGarrett Wollman return EINVAL; 926d0390e05SGarrett Wollman socantsendmore(so); 927d0390e05SGarrett Wollman return 0; 928d0390e05SGarrett Wollman } 929d0390e05SGarrett Wollman 930d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 931117bcae7SGarrett Wollman udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 932117bcae7SGarrett Wollman pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 933117bcae7SGarrett Wollman pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 934117bcae7SGarrett Wollman pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 935f8f6cbbaSPeter Wemm in_setsockaddr, sosend, soreceive, sopoll 936d0390e05SGarrett Wollman }; 937