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 40db4f9cc7SJonathan Lemon #include <stddef.h> 41df8bae1dSRodney W. Grimes #include <sys/param.h> 4226f9a767SRodney W. Grimes #include <sys/systm.h> 43b110a8a2SGarrett Wollman #include <sys/kernel.h> 44df8bae1dSRodney W. Grimes #include <sys/malloc.h> 45df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 46cfa1ca9dSYoshinobu Inoue #include <sys/domain.h> 47490d50b6SBrian Feldman #include <sys/proc.h> 48df8bae1dSRodney W. Grimes #include <sys/protosw.h> 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51b5e8ce9fSBruce Evans #include <sys/sysctl.h> 52816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 538781d8e9SBruce Evans 543d4d47f3SGarrett Wollman #include <vm/vm_zone.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 */ 1110312fbe9SPoul-Henning Kamp SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, 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 129cfa1ca9dSYoshinobu Inoue static void udp_append __P((struct inpcb *last, struct ip *ip, 130cfa1ca9dSYoshinobu Inoue struct mbuf *n, int off)); 131cfa1ca9dSYoshinobu Inoue #ifdef INET6 132cfa1ca9dSYoshinobu Inoue static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip)); 133cfa1ca9dSYoshinobu Inoue #endif 134cfa1ca9dSYoshinobu Inoue 135cfa1ca9dSYoshinobu Inoue static int udp_detach __P((struct socket *so)); 13657bf258eSGarrett Wollman static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *, 137a29f300eSGarrett Wollman struct mbuf *, struct proc *)); 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); 14798271db4SGarrett Wollman udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets, 1483d4d47f3SGarrett Wollman ZONE_INTERRUPT, 0); 149df8bae1dSRodney W. Grimes } 150df8bae1dSRodney W. Grimes 151df8bae1dSRodney W. Grimes void 152cfa1ca9dSYoshinobu Inoue udp_input(m, off, proto) 153df8bae1dSRodney W. Grimes register struct mbuf *m; 154cfa1ca9dSYoshinobu Inoue int off, proto; 155df8bae1dSRodney W. Grimes { 156cfa1ca9dSYoshinobu Inoue int iphlen = off; 157df8bae1dSRodney W. Grimes register struct ip *ip; 158df8bae1dSRodney W. Grimes register struct udphdr *uh; 159df8bae1dSRodney W. Grimes register struct inpcb *inp; 160df8bae1dSRodney W. Grimes struct mbuf *opts = 0; 161df8bae1dSRodney W. Grimes int len; 162df8bae1dSRodney W. Grimes struct ip save_ip; 163cfa1ca9dSYoshinobu Inoue struct sockaddr *append_sa; 164df8bae1dSRodney W. Grimes 165df8bae1dSRodney W. Grimes udpstat.udps_ipackets++; 166df8bae1dSRodney W. Grimes 167df8bae1dSRodney W. Grimes /* 168df8bae1dSRodney W. Grimes * Strip IP options, if any; should skip this, 169df8bae1dSRodney W. Grimes * make available to user, and use on returned packets, 170df8bae1dSRodney W. Grimes * but we don't yet have a way to check the checksum 171df8bae1dSRodney W. Grimes * with options still present. 172df8bae1dSRodney W. Grimes */ 173df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 174df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 175df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 176df8bae1dSRodney W. Grimes } 177df8bae1dSRodney W. Grimes 178df8bae1dSRodney W. Grimes /* 179df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 180df8bae1dSRodney W. Grimes */ 181df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 182df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 183df8bae1dSRodney W. Grimes if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 184df8bae1dSRodney W. Grimes udpstat.udps_hdrops++; 185df8bae1dSRodney W. Grimes return; 186df8bae1dSRodney W. Grimes } 187df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 188df8bae1dSRodney W. Grimes } 189df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 190df8bae1dSRodney W. Grimes 191df8bae1dSRodney W. Grimes /* 192df8bae1dSRodney W. Grimes * Make mbuf data length reflect UDP length. 193df8bae1dSRodney W. Grimes * If not enough data to reflect UDP length, drop. 194df8bae1dSRodney W. Grimes */ 195df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 196df8bae1dSRodney W. Grimes if (ip->ip_len != len) { 1977eb7a449SAndras Olah if (len > ip->ip_len || len < sizeof(struct udphdr)) { 198df8bae1dSRodney W. Grimes udpstat.udps_badlen++; 199df8bae1dSRodney W. Grimes goto bad; 200df8bae1dSRodney W. Grimes } 201df8bae1dSRodney W. Grimes m_adj(m, len - ip->ip_len); 202df8bae1dSRodney W. Grimes /* ip->ip_len = len; */ 203df8bae1dSRodney W. Grimes } 204df8bae1dSRodney W. Grimes /* 205df8bae1dSRodney W. Grimes * Save a copy of the IP header in case we want restore it 206df8bae1dSRodney W. Grimes * for sending an ICMP error message in response. 207df8bae1dSRodney W. Grimes */ 208df8bae1dSRodney W. Grimes save_ip = *ip; 209df8bae1dSRodney W. Grimes 210df8bae1dSRodney W. Grimes /* 211df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 212df8bae1dSRodney W. Grimes */ 2136dfab5b1SGarrett Wollman if (uh->uh_sum) { 214db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 215db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 216db4f9cc7SJonathan Lemon uh->uh_sum = m->m_pkthdr.csum_data; 217db4f9cc7SJonathan Lemon else 218db4f9cc7SJonathan Lemon uh->uh_sum = in_pseudo(ip->ip_src.s_addr, 219db4f9cc7SJonathan Lemon ip->ip_dst.s_addr, htonl(ip->ip_len + 220db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data + IPPROTO_UDP)); 221db4f9cc7SJonathan Lemon uh->uh_sum ^= 0xffff; 222db4f9cc7SJonathan Lemon } else { 2236effc713SDoug Rabson bzero(((struct ipovly *)ip)->ih_x1, 9); 224df8bae1dSRodney W. Grimes ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 225623ae52eSPoul-Henning Kamp uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 226db4f9cc7SJonathan Lemon } 227623ae52eSPoul-Henning Kamp if (uh->uh_sum) { 228df8bae1dSRodney W. Grimes udpstat.udps_badsum++; 229df8bae1dSRodney W. Grimes m_freem(m); 230df8bae1dSRodney W. Grimes return; 231df8bae1dSRodney W. Grimes } 232df8bae1dSRodney W. Grimes } 233df8bae1dSRodney W. Grimes 234df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 235df8bae1dSRodney W. Grimes in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 23682c23ebaSBill Fenner struct inpcb *last; 237df8bae1dSRodney W. Grimes /* 238df8bae1dSRodney W. Grimes * Deliver a multicast or broadcast datagram to *all* sockets 239df8bae1dSRodney W. Grimes * for which the local and remote addresses and ports match 240df8bae1dSRodney W. Grimes * those of the incoming datagram. This allows more than 241df8bae1dSRodney W. Grimes * one process to receive multi/broadcasts on the same port. 242df8bae1dSRodney W. Grimes * (This really ought to be done for unicast datagrams as 243df8bae1dSRodney W. Grimes * well, but that would cause problems with existing 244df8bae1dSRodney W. Grimes * applications that open both address-specific sockets and 245df8bae1dSRodney W. Grimes * a wildcard socket listening to the same port -- they would 246df8bae1dSRodney W. Grimes * end up receiving duplicates of every unicast datagram. 247df8bae1dSRodney W. Grimes * Those applications open the multiple sockets to overcome an 248df8bae1dSRodney W. Grimes * inadequacy of the UDP socket interface, but for backwards 249df8bae1dSRodney W. Grimes * compatibility we avoid the problem here rather than 250df8bae1dSRodney W. Grimes * fixing the interface. Maybe 4.5BSD will remedy this?) 251df8bae1dSRodney W. Grimes */ 252df8bae1dSRodney W. Grimes 253df8bae1dSRodney W. Grimes /* 254df8bae1dSRodney W. Grimes * Construct sockaddr format source address. 255df8bae1dSRodney W. Grimes */ 256df8bae1dSRodney W. Grimes udp_in.sin_port = uh->uh_sport; 257df8bae1dSRodney W. Grimes udp_in.sin_addr = ip->ip_src; 258df8bae1dSRodney W. Grimes /* 259df8bae1dSRodney W. Grimes * Locate pcb(s) for datagram. 260df8bae1dSRodney W. Grimes * (Algorithm copied from raw_intr().) 261df8bae1dSRodney W. Grimes */ 262df8bae1dSRodney W. Grimes last = NULL; 263cfa1ca9dSYoshinobu Inoue #ifdef INET6 264cfa1ca9dSYoshinobu Inoue udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; 265cfa1ca9dSYoshinobu Inoue #endif 266cfa1ca9dSYoshinobu Inoue LIST_FOREACH(inp, &udb, inp_list) { 267cfa1ca9dSYoshinobu Inoue #ifdef INET6 268369dc8ceSEivind Eklund if ((inp->inp_vflag & INP_IPV4) == 0) 269cfa1ca9dSYoshinobu Inoue continue; 270cfa1ca9dSYoshinobu Inoue #endif 271df8bae1dSRodney W. Grimes if (inp->inp_lport != uh->uh_dport) 272df8bae1dSRodney W. Grimes continue; 273df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr != INADDR_ANY) { 274df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr != 275df8bae1dSRodney W. Grimes ip->ip_dst.s_addr) 276df8bae1dSRodney W. Grimes continue; 277df8bae1dSRodney W. Grimes } 278df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 279df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != 280df8bae1dSRodney W. Grimes ip->ip_src.s_addr || 281df8bae1dSRodney W. Grimes inp->inp_fport != uh->uh_sport) 282df8bae1dSRodney W. Grimes continue; 283df8bae1dSRodney W. Grimes } 284df8bae1dSRodney W. Grimes 285df8bae1dSRodney W. Grimes if (last != NULL) { 286df8bae1dSRodney W. Grimes struct mbuf *n; 287df8bae1dSRodney W. Grimes 288cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 289cfa1ca9dSYoshinobu Inoue /* check AH/ESP integrity. */ 290cfa1ca9dSYoshinobu Inoue if (ipsec4_in_reject_so(m, last->inp_socket)) 291cfa1ca9dSYoshinobu Inoue ipsecstat.in_polvio++; 292cfa1ca9dSYoshinobu Inoue /* do not inject data to pcb */ 293cfa1ca9dSYoshinobu Inoue else 294cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 295cfa1ca9dSYoshinobu Inoue if ((n = m_copy(m, 0, M_COPYALL)) != NULL) 296cfa1ca9dSYoshinobu Inoue udp_append(last, ip, n, 297cfa1ca9dSYoshinobu Inoue iphlen + 298cfa1ca9dSYoshinobu Inoue sizeof(struct udphdr)); 299df8bae1dSRodney W. Grimes } 30082c23ebaSBill Fenner last = inp; 301df8bae1dSRodney W. Grimes /* 302df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 303df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 304df8bae1dSRodney W. Grimes * socket options set. This heuristic avoids searching 305df8bae1dSRodney W. Grimes * through all pcbs in the common case of a non-shared 306df8bae1dSRodney W. Grimes * port. It * assumes that an application will never 307df8bae1dSRodney W. Grimes * clear these options after setting them. 308df8bae1dSRodney W. Grimes */ 309694ad0a9SSteve Price if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 310df8bae1dSRodney W. Grimes break; 311df8bae1dSRodney W. Grimes } 312df8bae1dSRodney W. Grimes 313df8bae1dSRodney W. Grimes if (last == NULL) { 314df8bae1dSRodney W. Grimes /* 315df8bae1dSRodney W. Grimes * No matching pcb found; discard datagram. 316df8bae1dSRodney W. Grimes * (No need to send an ICMP Port Unreachable 317df8bae1dSRodney W. Grimes * for a broadcast or multicast datgram.) 318df8bae1dSRodney W. Grimes */ 319df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 320df8bae1dSRodney W. Grimes goto bad; 321df8bae1dSRodney W. Grimes } 322cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 323cfa1ca9dSYoshinobu Inoue /* check AH/ESP integrity. */ 324cfa1ca9dSYoshinobu Inoue if (ipsec4_in_reject_so(m, last->inp_socket)) { 325cfa1ca9dSYoshinobu Inoue ipsecstat.in_polvio++; 326df8bae1dSRodney W. Grimes goto bad; 327df8bae1dSRodney W. Grimes } 328cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 329cfa1ca9dSYoshinobu Inoue udp_append(last, ip, m, iphlen + sizeof(struct udphdr)); 330df8bae1dSRodney W. Grimes return; 331df8bae1dSRodney W. Grimes } 332df8bae1dSRodney W. Grimes /* 3336d6a026bSDavid Greenman * Locate pcb for datagram. 334df8bae1dSRodney W. Grimes */ 335c3229e05SDavid Greenman inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 336cfa1ca9dSYoshinobu Inoue ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 33715bd2b43SDavid Greenman if (inp == NULL) { 33875cfc95fSAndrey A. Chernov if (log_in_vain) { 339df5c0b8aSBill Fenner char buf[4*sizeof "123"]; 34075cfc95fSAndrey A. Chernov 34175cfc95fSAndrey A. Chernov strcpy(buf, inet_ntoa(ip->ip_dst)); 342592071e8SBruce Evans log(LOG_INFO, 343592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 344592071e8SBruce Evans buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 345592071e8SBruce Evans ntohs(uh->uh_sport)); 34675cfc95fSAndrey A. Chernov } 347df8bae1dSRodney W. Grimes udpstat.udps_noport++; 348df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 349df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 350df8bae1dSRodney W. Grimes goto bad; 351df8bae1dSRodney W. Grimes } 352df8bae1dSRodney W. Grimes *ip = save_ip; 35351508de1SMatthew Dillon if (badport_bandlim(0) < 0) 35451508de1SMatthew Dillon goto bad; 355582a7760SBruce Evans if (blackhole) 35612b4fd06SPoul-Henning Kamp goto bad; 357582a7760SBruce Evans icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 358df8bae1dSRodney W. Grimes return; 359df8bae1dSRodney W. Grimes } 360cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 361cfa1ca9dSYoshinobu Inoue if (ipsec4_in_reject_so(m, inp->inp_socket)) { 362cfa1ca9dSYoshinobu Inoue ipsecstat.in_polvio++; 363cfa1ca9dSYoshinobu Inoue goto bad; 364cfa1ca9dSYoshinobu Inoue } 365cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 366df8bae1dSRodney W. Grimes 367df8bae1dSRodney W. Grimes /* 368df8bae1dSRodney W. Grimes * Construct sockaddr format source address. 369df8bae1dSRodney W. Grimes * Stuff source address and datagram in user buffer. 370df8bae1dSRodney W. Grimes */ 371df8bae1dSRodney W. Grimes udp_in.sin_port = uh->uh_sport; 372df8bae1dSRodney W. Grimes udp_in.sin_addr = ip->ip_src; 37382dab6ceSGarrett Wollman if (inp->inp_flags & INP_CONTROLOPTS 374cfa1ca9dSYoshinobu Inoue || inp->inp_socket->so_options & SO_TIMESTAMP) { 375cfa1ca9dSYoshinobu Inoue #ifdef INET6 376cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) { 377cfa1ca9dSYoshinobu Inoue int savedflags; 378cfa1ca9dSYoshinobu Inoue 379cfa1ca9dSYoshinobu Inoue ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 380cfa1ca9dSYoshinobu Inoue savedflags = inp->inp_flags; 381cfa1ca9dSYoshinobu Inoue inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 382cfa1ca9dSYoshinobu Inoue ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m); 383cfa1ca9dSYoshinobu Inoue inp->inp_flags = savedflags; 384cfa1ca9dSYoshinobu Inoue } else 385cfa1ca9dSYoshinobu Inoue #endif 38682c23ebaSBill Fenner ip_savecontrol(inp, &opts, ip, m); 387cfa1ca9dSYoshinobu Inoue } 388df8bae1dSRodney W. Grimes iphlen += sizeof(struct udphdr); 389cfa1ca9dSYoshinobu Inoue m_adj(m, iphlen); 390cfa1ca9dSYoshinobu Inoue #ifdef INET6 391cfa1ca9dSYoshinobu Inoue if (inp->inp_vflag & INP_IPV6) { 392cfa1ca9dSYoshinobu Inoue in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 393cfa1ca9dSYoshinobu Inoue append_sa = (struct sockaddr *)&udp_in6; 394cfa1ca9dSYoshinobu Inoue } else 395cfa1ca9dSYoshinobu Inoue #endif 396cfa1ca9dSYoshinobu Inoue append_sa = (struct sockaddr *)&udp_in; 397cfa1ca9dSYoshinobu Inoue if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) { 398df8bae1dSRodney W. Grimes udpstat.udps_fullsock++; 399df8bae1dSRodney W. Grimes goto bad; 400df8bae1dSRodney W. Grimes } 401df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 402df8bae1dSRodney W. Grimes return; 403df8bae1dSRodney W. Grimes bad: 404df8bae1dSRodney W. Grimes m_freem(m); 405df8bae1dSRodney W. Grimes if (opts) 406df8bae1dSRodney W. Grimes m_freem(opts); 407cfa1ca9dSYoshinobu Inoue return; 408cfa1ca9dSYoshinobu Inoue } 409cfa1ca9dSYoshinobu Inoue 410cfa1ca9dSYoshinobu Inoue #if defined(INET6) 411cfa1ca9dSYoshinobu Inoue static void 412cfa1ca9dSYoshinobu Inoue ip_2_ip6_hdr(ip6, ip) 413cfa1ca9dSYoshinobu Inoue struct ip6_hdr *ip6; 414cfa1ca9dSYoshinobu Inoue struct ip *ip; 415cfa1ca9dSYoshinobu Inoue { 416cfa1ca9dSYoshinobu Inoue bzero(ip6, sizeof(*ip6)); 417cfa1ca9dSYoshinobu Inoue 418cfa1ca9dSYoshinobu Inoue ip6->ip6_vfc = IPV6_VERSION; 419cfa1ca9dSYoshinobu Inoue ip6->ip6_plen = ip->ip_len; 420cfa1ca9dSYoshinobu Inoue ip6->ip6_nxt = ip->ip_p; 421cfa1ca9dSYoshinobu Inoue ip6->ip6_hlim = ip->ip_ttl; 422cfa1ca9dSYoshinobu Inoue ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = 423cfa1ca9dSYoshinobu Inoue IPV6_ADDR_INT32_SMP; 424cfa1ca9dSYoshinobu Inoue ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; 425cfa1ca9dSYoshinobu Inoue ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; 426cfa1ca9dSYoshinobu Inoue } 427cfa1ca9dSYoshinobu Inoue #endif 428cfa1ca9dSYoshinobu Inoue 429cfa1ca9dSYoshinobu Inoue /* 430cfa1ca9dSYoshinobu Inoue * subroutine of udp_input(), mainly for source code readability. 431cfa1ca9dSYoshinobu Inoue * caller must properly init udp_ip6 and udp_in6 beforehand. 432cfa1ca9dSYoshinobu Inoue */ 433cfa1ca9dSYoshinobu Inoue static void 434cfa1ca9dSYoshinobu Inoue udp_append(last, ip, n, off) 435cfa1ca9dSYoshinobu Inoue struct inpcb *last; 436cfa1ca9dSYoshinobu Inoue struct ip *ip; 437cfa1ca9dSYoshinobu Inoue struct mbuf *n; 438cfa1ca9dSYoshinobu Inoue int off; 439cfa1ca9dSYoshinobu Inoue { 440cfa1ca9dSYoshinobu Inoue struct sockaddr *append_sa; 441cfa1ca9dSYoshinobu Inoue struct mbuf *opts = 0; 442cfa1ca9dSYoshinobu Inoue 443cfa1ca9dSYoshinobu Inoue if (last->inp_flags & INP_CONTROLOPTS || 444cfa1ca9dSYoshinobu Inoue last->inp_socket->so_options & SO_TIMESTAMP) { 445cfa1ca9dSYoshinobu Inoue #ifdef INET6 446cfa1ca9dSYoshinobu Inoue if (last->inp_vflag & INP_IPV6) { 447cfa1ca9dSYoshinobu Inoue int savedflags; 448cfa1ca9dSYoshinobu Inoue 449cfa1ca9dSYoshinobu Inoue if (udp_ip6.uip6_init_done == 0) { 450cfa1ca9dSYoshinobu Inoue ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 451cfa1ca9dSYoshinobu Inoue udp_ip6.uip6_init_done = 1; 452cfa1ca9dSYoshinobu Inoue } 453cfa1ca9dSYoshinobu Inoue savedflags = last->inp_flags; 454cfa1ca9dSYoshinobu Inoue last->inp_flags &= ~INP_UNMAPPABLEOPTS; 455cfa1ca9dSYoshinobu Inoue ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n); 456cfa1ca9dSYoshinobu Inoue last->inp_flags = savedflags; 457cfa1ca9dSYoshinobu Inoue } else 458cfa1ca9dSYoshinobu Inoue #endif 459cfa1ca9dSYoshinobu Inoue ip_savecontrol(last, &opts, ip, n); 460cfa1ca9dSYoshinobu Inoue } 461cfa1ca9dSYoshinobu Inoue #ifdef INET6 462cfa1ca9dSYoshinobu Inoue if (last->inp_vflag & INP_IPV6) { 463cfa1ca9dSYoshinobu Inoue if (udp_in6.uin6_init_done == 0) { 464cfa1ca9dSYoshinobu Inoue in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 465cfa1ca9dSYoshinobu Inoue udp_in6.uin6_init_done = 1; 466cfa1ca9dSYoshinobu Inoue } 467cfa1ca9dSYoshinobu Inoue append_sa = (struct sockaddr *)&udp_in6.uin6_sin; 468cfa1ca9dSYoshinobu Inoue } else 469cfa1ca9dSYoshinobu Inoue #endif 470cfa1ca9dSYoshinobu Inoue append_sa = (struct sockaddr *)&udp_in; 471cfa1ca9dSYoshinobu Inoue m_adj(n, off); 472cfa1ca9dSYoshinobu Inoue if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) { 473cfa1ca9dSYoshinobu Inoue m_freem(n); 474cfa1ca9dSYoshinobu Inoue if (opts) 475cfa1ca9dSYoshinobu Inoue m_freem(opts); 476cfa1ca9dSYoshinobu Inoue udpstat.udps_fullsock++; 477cfa1ca9dSYoshinobu Inoue } else 478cfa1ca9dSYoshinobu Inoue sorwakeup(last->inp_socket); 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 */ 48576429de4SYoshinobu Inoue void 486df8bae1dSRodney W. Grimes udp_notify(inp, errno) 487df8bae1dSRodney W. Grimes register struct inpcb *inp; 488df8bae1dSRodney W. Grimes 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); 493df8bae1dSRodney W. Grimes } 494df8bae1dSRodney W. Grimes 495df8bae1dSRodney W. Grimes void 496b62d102cSBruce Evans udp_ctlinput(cmd, sa, vip) 497df8bae1dSRodney W. Grimes int cmd; 498df8bae1dSRodney W. Grimes struct sockaddr *sa; 499b62d102cSBruce Evans void *vip; 500df8bae1dSRodney W. Grimes { 501b62d102cSBruce Evans register struct ip *ip = vip; 502df8bae1dSRodney W. Grimes register struct udphdr *uh; 503df8bae1dSRodney W. Grimes 504df8bae1dSRodney W. Grimes if (!PRC_IS_REDIRECT(cmd) && 505df8bae1dSRodney W. Grimes ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) 506df8bae1dSRodney W. Grimes return; 507df8bae1dSRodney W. Grimes if (ip) { 508df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 509df8bae1dSRodney W. Grimes in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, 510df8bae1dSRodney W. Grimes cmd, udp_notify); 511df8bae1dSRodney W. Grimes } else 512df8bae1dSRodney W. Grimes in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); 513df8bae1dSRodney W. Grimes } 514df8bae1dSRodney W. Grimes 5150312fbe9SPoul-Henning Kamp static int 51682d9ae4eSPoul-Henning Kamp udp_pcblist (SYSCTL_HANDLER_ARGS) 51798271db4SGarrett Wollman { 51898271db4SGarrett Wollman int error, i, n, s; 51998271db4SGarrett Wollman struct inpcb *inp, **inp_list; 52098271db4SGarrett Wollman inp_gen_t gencnt; 52198271db4SGarrett Wollman struct xinpgen xig; 52298271db4SGarrett Wollman 52398271db4SGarrett Wollman /* 52498271db4SGarrett Wollman * The process of preparing the TCB list is too time-consuming and 52598271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 52698271db4SGarrett Wollman */ 52798271db4SGarrett Wollman if (req->oldptr == 0) { 52898271db4SGarrett Wollman n = udbinfo.ipi_count; 52998271db4SGarrett Wollman req->oldidx = 2 * (sizeof xig) 53098271db4SGarrett Wollman + (n + n/8) * sizeof(struct xinpcb); 53198271db4SGarrett Wollman return 0; 53298271db4SGarrett Wollman } 53398271db4SGarrett Wollman 53498271db4SGarrett Wollman if (req->newptr != 0) 53598271db4SGarrett Wollman return EPERM; 53698271db4SGarrett Wollman 53798271db4SGarrett Wollman /* 53898271db4SGarrett Wollman * OK, now we're committed to doing something. 53998271db4SGarrett Wollman */ 54098271db4SGarrett Wollman s = splnet(); 54198271db4SGarrett Wollman gencnt = udbinfo.ipi_gencnt; 54298271db4SGarrett Wollman n = udbinfo.ipi_count; 54398271db4SGarrett Wollman splx(s); 54498271db4SGarrett Wollman 54598271db4SGarrett Wollman xig.xig_len = sizeof xig; 54698271db4SGarrett Wollman xig.xig_count = n; 54798271db4SGarrett Wollman xig.xig_gen = gencnt; 54898271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 54998271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 55098271db4SGarrett Wollman if (error) 55198271db4SGarrett Wollman return error; 55298271db4SGarrett Wollman 55398271db4SGarrett Wollman inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 55498271db4SGarrett Wollman if (inp_list == 0) 55598271db4SGarrett Wollman return ENOMEM; 55698271db4SGarrett Wollman 55798271db4SGarrett Wollman s = splnet(); 55898271db4SGarrett Wollman for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n; 55998271db4SGarrett Wollman inp = inp->inp_list.le_next) { 56075c13541SPoul-Henning Kamp if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp)) 56198271db4SGarrett Wollman inp_list[i++] = inp; 56298271db4SGarrett Wollman } 56398271db4SGarrett Wollman splx(s); 56498271db4SGarrett Wollman n = i; 56598271db4SGarrett Wollman 56698271db4SGarrett Wollman error = 0; 56798271db4SGarrett Wollman for (i = 0; i < n; i++) { 56898271db4SGarrett Wollman inp = inp_list[i]; 56998271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 57098271db4SGarrett Wollman struct xinpcb xi; 57198271db4SGarrett Wollman xi.xi_len = sizeof xi; 57298271db4SGarrett Wollman /* XXX should avoid extra copy */ 57398271db4SGarrett Wollman bcopy(inp, &xi.xi_inp, sizeof *inp); 57498271db4SGarrett Wollman if (inp->inp_socket) 57598271db4SGarrett Wollman sotoxsocket(inp->inp_socket, &xi.xi_socket); 57698271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 57798271db4SGarrett Wollman } 57898271db4SGarrett Wollman } 57998271db4SGarrett Wollman if (!error) { 58098271db4SGarrett Wollman /* 58198271db4SGarrett Wollman * Give the user an updated idea of our state. 58298271db4SGarrett Wollman * If the generation differs from what we told 58398271db4SGarrett Wollman * her before, she knows that something happened 58498271db4SGarrett Wollman * while we were processing this request, and it 58598271db4SGarrett Wollman * might be necessary to retry. 58698271db4SGarrett Wollman */ 58798271db4SGarrett Wollman s = splnet(); 58898271db4SGarrett Wollman xig.xig_gen = udbinfo.ipi_gencnt; 58998271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 59098271db4SGarrett Wollman xig.xig_count = udbinfo.ipi_count; 59198271db4SGarrett Wollman splx(s); 59298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 59398271db4SGarrett Wollman } 59498271db4SGarrett Wollman free(inp_list, M_TEMP); 59598271db4SGarrett Wollman return error; 59698271db4SGarrett Wollman } 59798271db4SGarrett Wollman 59898271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 59998271db4SGarrett Wollman udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 60098271db4SGarrett Wollman 60198271db4SGarrett Wollman static int 60282d9ae4eSPoul-Henning Kamp udp_getcred (SYSCTL_HANDLER_ARGS) 603490d50b6SBrian Feldman { 604490d50b6SBrian Feldman struct sockaddr_in addrs[2]; 605490d50b6SBrian Feldman struct inpcb *inp; 606490d50b6SBrian Feldman int error, s; 607490d50b6SBrian Feldman 608490d50b6SBrian Feldman error = suser(req->p); 609490d50b6SBrian Feldman if (error) 610490d50b6SBrian Feldman return (error); 611490d50b6SBrian Feldman error = SYSCTL_IN(req, addrs, sizeof(addrs)); 612490d50b6SBrian Feldman if (error) 613490d50b6SBrian Feldman return (error); 614490d50b6SBrian Feldman s = splnet(); 615490d50b6SBrian Feldman inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 616cfa1ca9dSYoshinobu Inoue addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 6172f9a2132SBrian Feldman if (inp == NULL || inp->inp_socket == NULL) { 618490d50b6SBrian Feldman error = ENOENT; 619490d50b6SBrian Feldman goto out; 620490d50b6SBrian Feldman } 6212f9a2132SBrian Feldman error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred)); 622490d50b6SBrian Feldman out: 623490d50b6SBrian Feldman splx(s); 624490d50b6SBrian Feldman return (error); 625490d50b6SBrian Feldman } 626490d50b6SBrian Feldman 627490d50b6SBrian Feldman SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 628490d50b6SBrian Feldman 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection"); 629490d50b6SBrian Feldman 630490d50b6SBrian Feldman static int 631a29f300eSGarrett Wollman udp_output(inp, m, addr, control, p) 632df8bae1dSRodney W. Grimes register struct inpcb *inp; 633d25f3712SBrian Feldman struct mbuf *m; 63457bf258eSGarrett Wollman struct sockaddr *addr; 63557bf258eSGarrett Wollman struct mbuf *control; 636a29f300eSGarrett Wollman struct proc *p; 637df8bae1dSRodney W. Grimes { 638df8bae1dSRodney W. Grimes register struct udpiphdr *ui; 639df8bae1dSRodney W. Grimes register int len = m->m_pkthdr.len; 640df8bae1dSRodney W. Grimes struct in_addr laddr; 64175c13541SPoul-Henning Kamp struct sockaddr_in *sin; 64226f9a767SRodney W. Grimes int s = 0, error = 0; 643df8bae1dSRodney W. Grimes 644df8bae1dSRodney W. Grimes if (control) 645df8bae1dSRodney W. Grimes m_freem(control); /* XXX */ 646df8bae1dSRodney W. Grimes 647430d30d8SBill Fenner if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 648430d30d8SBill Fenner error = EMSGSIZE; 649430d30d8SBill Fenner goto release; 650430d30d8SBill Fenner } 651430d30d8SBill Fenner 652df8bae1dSRodney W. Grimes if (addr) { 65375c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)addr; 65475c13541SPoul-Henning Kamp prison_remote_ip(p, 0, &sin->sin_addr.s_addr); 655df8bae1dSRodney W. Grimes laddr = inp->inp_laddr; 656df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 657df8bae1dSRodney W. Grimes error = EISCONN; 658df8bae1dSRodney W. Grimes goto release; 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes /* 661df8bae1dSRodney W. Grimes * Must block input while temporarily connected. 662df8bae1dSRodney W. Grimes */ 663df8bae1dSRodney W. Grimes s = splnet(); 664a29f300eSGarrett Wollman error = in_pcbconnect(inp, addr, p); 665df8bae1dSRodney W. Grimes if (error) { 666df8bae1dSRodney W. Grimes splx(s); 667df8bae1dSRodney W. Grimes goto release; 668df8bae1dSRodney W. Grimes } 669df8bae1dSRodney W. Grimes } else { 670df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr == INADDR_ANY) { 671df8bae1dSRodney W. Grimes error = ENOTCONN; 672df8bae1dSRodney W. Grimes goto release; 673df8bae1dSRodney W. Grimes } 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes /* 676df8bae1dSRodney W. Grimes * Calculate data length and get a mbuf 677df8bae1dSRodney W. Grimes * for UDP and IP headers. 678df8bae1dSRodney W. Grimes */ 679df8bae1dSRodney W. Grimes M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 680df8bae1dSRodney W. Grimes if (m == 0) { 681df8bae1dSRodney W. Grimes error = ENOBUFS; 6821c09f774SGarrett Wollman if (addr) 6831c09f774SGarrett Wollman splx(s); 684df8bae1dSRodney W. Grimes goto release; 685df8bae1dSRodney W. Grimes } 686df8bae1dSRodney W. Grimes 687df8bae1dSRodney W. Grimes /* 688df8bae1dSRodney W. Grimes * Fill in mbuf with extended UDP header 689df8bae1dSRodney W. Grimes * and addresses and length put into network format. 690df8bae1dSRodney W. Grimes */ 691df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 692db4f9cc7SJonathan Lemon bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 693df8bae1dSRodney W. Grimes ui->ui_pr = IPPROTO_UDP; 694df8bae1dSRodney W. Grimes ui->ui_src = inp->inp_laddr; 695df8bae1dSRodney W. Grimes ui->ui_dst = inp->inp_faddr; 696df8bae1dSRodney W. Grimes ui->ui_sport = inp->inp_lport; 697df8bae1dSRodney W. Grimes ui->ui_dport = inp->inp_fport; 698db4f9cc7SJonathan Lemon ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 699df8bae1dSRodney W. Grimes 700df8bae1dSRodney W. Grimes /* 701db4f9cc7SJonathan Lemon * Set up checksum and output datagram. 702df8bae1dSRodney W. Grimes */ 703df8bae1dSRodney W. Grimes if (udpcksum) { 704db4f9cc7SJonathan Lemon ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, 705db4f9cc7SJonathan Lemon htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 706db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_UDP; 707db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 708db4f9cc7SJonathan Lemon } else { 709db4f9cc7SJonathan Lemon ui->ui_sum = 0; 710df8bae1dSRodney W. Grimes } 711df8bae1dSRodney W. Grimes ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 712ca98b82cSDavid Greenman ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 713ca98b82cSDavid Greenman ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 714df8bae1dSRodney W. Grimes udpstat.udps_opackets++; 715cfa1ca9dSYoshinobu Inoue 716cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 717cfa1ca9dSYoshinobu Inoue m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket; 718cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 719cfa1ca9dSYoshinobu Inoue 720df8bae1dSRodney W. Grimes error = ip_output(m, inp->inp_options, &inp->inp_route, 7216a800098SYoshinobu Inoue (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) 7226a800098SYoshinobu Inoue | IP_SOCKINMRCVIF, 723df8bae1dSRodney W. Grimes inp->inp_moptions); 724df8bae1dSRodney W. Grimes 725df8bae1dSRodney W. Grimes if (addr) { 726df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 72757bf258eSGarrett Wollman inp->inp_laddr = laddr; /* XXX rehash? */ 728df8bae1dSRodney W. Grimes splx(s); 729df8bae1dSRodney W. Grimes } 730df8bae1dSRodney W. Grimes return (error); 731df8bae1dSRodney W. Grimes 732df8bae1dSRodney W. Grimes release: 733df8bae1dSRodney W. Grimes m_freem(m); 734df8bae1dSRodney W. Grimes return (error); 735df8bae1dSRodney W. Grimes } 736df8bae1dSRodney W. Grimes 73776429de4SYoshinobu Inoue u_long udp_sendspace = 9216; /* really max datagram size */ 738df8bae1dSRodney W. Grimes /* 40 1K datagrams */ 7390312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 7403d177f46SBill Fumerola &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 7410312fbe9SPoul-Henning Kamp 742cfa1ca9dSYoshinobu Inoue u_long udp_recvspace = 40 * (1024 + 743cfa1ca9dSYoshinobu Inoue #ifdef INET6 744cfa1ca9dSYoshinobu Inoue sizeof(struct sockaddr_in6) 745cfa1ca9dSYoshinobu Inoue #else 746cfa1ca9dSYoshinobu Inoue sizeof(struct sockaddr_in) 747cfa1ca9dSYoshinobu Inoue #endif 748cfa1ca9dSYoshinobu Inoue ); 7490312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 7503d177f46SBill Fumerola &udp_recvspace, 0, "Maximum incoming UDP datagram size"); 751df8bae1dSRodney W. Grimes 752d0390e05SGarrett Wollman static int 753d0390e05SGarrett Wollman udp_abort(struct socket *so) 754df8bae1dSRodney W. Grimes { 755d0390e05SGarrett Wollman struct inpcb *inp; 756df8bae1dSRodney W. Grimes int s; 757df8bae1dSRodney W. Grimes 758d0390e05SGarrett Wollman inp = sotoinpcb(so); 759d0390e05SGarrett Wollman if (inp == 0) 760d0390e05SGarrett Wollman return EINVAL; /* ??? possible? panic instead? */ 761d0390e05SGarrett Wollman soisdisconnected(so); 762d0390e05SGarrett Wollman s = splnet(); 763d0390e05SGarrett Wollman in_pcbdetach(inp); 764d0390e05SGarrett Wollman splx(s); 765d0390e05SGarrett Wollman return 0; 766df8bae1dSRodney W. Grimes } 767df8bae1dSRodney W. Grimes 768d0390e05SGarrett Wollman static int 769a29f300eSGarrett Wollman udp_attach(struct socket *so, int proto, struct proc *p) 770d0390e05SGarrett Wollman { 771d0390e05SGarrett Wollman struct inpcb *inp; 772d0390e05SGarrett Wollman int s, error; 773d0390e05SGarrett Wollman 774d0390e05SGarrett Wollman inp = sotoinpcb(so); 775d0390e05SGarrett Wollman if (inp != 0) 776d0390e05SGarrett Wollman return EINVAL; 777d0390e05SGarrett Wollman 778cfa1ca9dSYoshinobu Inoue error = soreserve(so, udp_sendspace, udp_recvspace); 779cfa1ca9dSYoshinobu Inoue if (error) 780cfa1ca9dSYoshinobu Inoue return error; 781df8bae1dSRodney W. Grimes s = splnet(); 782a29f300eSGarrett Wollman error = in_pcballoc(so, &udbinfo, p); 783df8bae1dSRodney W. Grimes splx(s); 784df8bae1dSRodney W. Grimes if (error) 785d0390e05SGarrett Wollman return error; 786cfa1ca9dSYoshinobu Inoue 787cfa1ca9dSYoshinobu Inoue inp = (struct inpcb *)so->so_pcb; 788cfa1ca9dSYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 789cfa1ca9dSYoshinobu Inoue inp->inp_ip_ttl = ip_defttl; 790cfa1ca9dSYoshinobu Inoue #ifdef IPSEC 791cfa1ca9dSYoshinobu Inoue error = ipsec_init_policy(so, &inp->inp_sp); 792cfa1ca9dSYoshinobu Inoue if (error != 0) { 793cfa1ca9dSYoshinobu Inoue in_pcbdetach(inp); 794d0390e05SGarrett Wollman return error; 795cfa1ca9dSYoshinobu Inoue } 796cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/ 797d0390e05SGarrett Wollman return 0; 798df8bae1dSRodney W. Grimes } 799d0390e05SGarrett Wollman 800d0390e05SGarrett Wollman static int 80157bf258eSGarrett Wollman udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 802d0390e05SGarrett Wollman { 803d0390e05SGarrett Wollman struct inpcb *inp; 804d0390e05SGarrett Wollman int s, error; 805d0390e05SGarrett Wollman 806d0390e05SGarrett Wollman inp = sotoinpcb(so); 807d0390e05SGarrett Wollman if (inp == 0) 808d0390e05SGarrett Wollman return EINVAL; 809df8bae1dSRodney W. Grimes s = splnet(); 810a29f300eSGarrett Wollman error = in_pcbbind(inp, nam, p); 811d0390e05SGarrett Wollman splx(s); 812d0390e05SGarrett Wollman return error; 813d0390e05SGarrett Wollman } 814d0390e05SGarrett Wollman 815d0390e05SGarrett Wollman static int 81657bf258eSGarrett Wollman udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 817d0390e05SGarrett Wollman { 818d0390e05SGarrett Wollman struct inpcb *inp; 819d0390e05SGarrett Wollman int s, error; 82075c13541SPoul-Henning Kamp struct sockaddr_in *sin; 821d0390e05SGarrett Wollman 822d0390e05SGarrett Wollman inp = sotoinpcb(so); 823d0390e05SGarrett Wollman if (inp == 0) 824d0390e05SGarrett Wollman return EINVAL; 825d0390e05SGarrett Wollman if (inp->inp_faddr.s_addr != INADDR_ANY) 826d0390e05SGarrett Wollman return EISCONN; 827d0390e05SGarrett Wollman s = splnet(); 82875c13541SPoul-Henning Kamp sin = (struct sockaddr_in *)nam; 82975c13541SPoul-Henning Kamp prison_remote_ip(p, 0, &sin->sin_addr.s_addr); 830a29f300eSGarrett Wollman error = in_pcbconnect(inp, nam, p); 831df8bae1dSRodney W. Grimes splx(s); 832df8bae1dSRodney W. Grimes if (error == 0) 833df8bae1dSRodney W. Grimes soisconnected(so); 834d0390e05SGarrett Wollman return error; 835df8bae1dSRodney W. Grimes } 836d0390e05SGarrett Wollman 837d0390e05SGarrett Wollman static int 838d0390e05SGarrett Wollman udp_detach(struct socket *so) 839d0390e05SGarrett Wollman { 840d0390e05SGarrett Wollman struct inpcb *inp; 841d0390e05SGarrett Wollman int s; 842d0390e05SGarrett Wollman 843d0390e05SGarrett Wollman inp = sotoinpcb(so); 844d0390e05SGarrett Wollman if (inp == 0) 845d0390e05SGarrett Wollman return EINVAL; 846d0390e05SGarrett Wollman s = splnet(); 847d0390e05SGarrett Wollman in_pcbdetach(inp); 848d0390e05SGarrett Wollman splx(s); 849d0390e05SGarrett Wollman return 0; 850d0390e05SGarrett Wollman } 851d0390e05SGarrett Wollman 852d0390e05SGarrett Wollman static int 853d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 854d0390e05SGarrett Wollman { 855d0390e05SGarrett Wollman struct inpcb *inp; 856d0390e05SGarrett Wollman int s; 857d0390e05SGarrett Wollman 858d0390e05SGarrett Wollman inp = sotoinpcb(so); 859d0390e05SGarrett Wollman if (inp == 0) 860d0390e05SGarrett Wollman return EINVAL; 861d0390e05SGarrett Wollman if (inp->inp_faddr.s_addr == INADDR_ANY) 862d0390e05SGarrett Wollman return ENOTCONN; 863d0390e05SGarrett Wollman 864df8bae1dSRodney W. Grimes s = splnet(); 865df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 866df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 867df8bae1dSRodney W. Grimes splx(s); 868df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTED; /* XXX */ 869d0390e05SGarrett Wollman return 0; 870df8bae1dSRodney W. Grimes } 871df8bae1dSRodney W. Grimes 872d0390e05SGarrett Wollman static int 87357bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 874a29f300eSGarrett Wollman struct mbuf *control, struct proc *p) 875d0390e05SGarrett Wollman { 876d0390e05SGarrett Wollman struct inpcb *inp; 877d0390e05SGarrett Wollman 878d0390e05SGarrett Wollman inp = sotoinpcb(so); 879d0390e05SGarrett Wollman if (inp == 0) { 880d0390e05SGarrett Wollman m_freem(m); 881d0390e05SGarrett Wollman return EINVAL; 882d0390e05SGarrett Wollman } 883a29f300eSGarrett Wollman return udp_output(inp, m, addr, control, p); 884d0390e05SGarrett Wollman } 885d0390e05SGarrett Wollman 88676429de4SYoshinobu Inoue int 887d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 888d0390e05SGarrett Wollman { 889d0390e05SGarrett Wollman struct inpcb *inp; 890d0390e05SGarrett Wollman 891d0390e05SGarrett Wollman inp = sotoinpcb(so); 892d0390e05SGarrett Wollman if (inp == 0) 893d0390e05SGarrett Wollman return EINVAL; 894d0390e05SGarrett Wollman socantsendmore(so); 895d0390e05SGarrett Wollman return 0; 896d0390e05SGarrett Wollman } 897d0390e05SGarrett Wollman 898d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 899117bcae7SGarrett Wollman udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 900117bcae7SGarrett Wollman pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 901117bcae7SGarrett Wollman pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 902117bcae7SGarrett Wollman pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 903f8f6cbbaSPeter Wemm in_setsockaddr, sosend, soreceive, sopoll 904d0390e05SGarrett Wollman }; 905