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 3498271db4SGarrett Wollman * $Id: udp_usrreq.c,v 1.46 1998/03/28 10:18:26 bde Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 3826f9a767SRodney W. Grimes #include <sys/systm.h> 39b110a8a2SGarrett Wollman #include <sys/kernel.h> 40df8bae1dSRodney W. Grimes #include <sys/malloc.h> 41df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 42df8bae1dSRodney W. Grimes #include <sys/protosw.h> 43df8bae1dSRodney W. Grimes #include <sys/socket.h> 44df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 45b5e8ce9fSBruce Evans #include <sys/sysctl.h> 46816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 478781d8e9SBruce Evans 483d4d47f3SGarrett Wollman #include <vm/vm_zone.h> 49df8bae1dSRodney W. Grimes 50df8bae1dSRodney W. Grimes #include <net/if.h> 51df8bae1dSRodney W. Grimes #include <net/route.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes #include <netinet/in.h> 54df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 55df8bae1dSRodney W. Grimes #include <netinet/ip.h> 56df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 57b5e8ce9fSBruce Evans #include <netinet/in_var.h> 58df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 59df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 60df8bae1dSRodney W. Grimes #include <netinet/udp.h> 61df8bae1dSRodney W. Grimes #include <netinet/udp_var.h> 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes /* 64df8bae1dSRodney W. Grimes * UDP protocol implementation. 65df8bae1dSRodney W. Grimes * Per RFC 768, August, 1980. 66df8bae1dSRodney W. Grimes */ 67df8bae1dSRodney W. Grimes #ifndef COMPAT_42 680312fbe9SPoul-Henning Kamp static int udpcksum = 1; 69df8bae1dSRodney W. Grimes #else 700312fbe9SPoul-Henning Kamp static int udpcksum = 0; /* XXX */ 71df8bae1dSRodney W. Grimes #endif 720312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 730312fbe9SPoul-Henning Kamp &udpcksum, 0, ""); 74df8bae1dSRodney W. Grimes 75d78a37adSPaul Traina static int log_in_vain = 0; 76816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 77816a3d83SPoul-Henning Kamp &log_in_vain, 0, ""); 78816a3d83SPoul-Henning Kamp 79f708ef1bSPoul-Henning Kamp static struct inpcbhead udb; /* from udp_var.h */ 80f708ef1bSPoul-Henning Kamp static struct inpcbinfo udbinfo; 8115bd2b43SDavid Greenman 8215bd2b43SDavid Greenman #ifndef UDBHASHSIZE 83c3229e05SDavid Greenman #define UDBHASHSIZE 16 8415bd2b43SDavid Greenman #endif 8515bd2b43SDavid Greenman 86f708ef1bSPoul-Henning Kamp static struct udpstat udpstat; /* from udp_var.h */ 870312fbe9SPoul-Henning Kamp SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, 880312fbe9SPoul-Henning Kamp &udpstat, udpstat, ""); 89f2ea20e6SGarrett Wollman 900312fbe9SPoul-Henning Kamp static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 91df8bae1dSRodney W. Grimes 9257bf258eSGarrett Wollman static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *, 93a29f300eSGarrett Wollman struct mbuf *, struct proc *)); 94df8bae1dSRodney W. Grimes static void udp_notify __P((struct inpcb *, int)); 95df8bae1dSRodney W. Grimes 96df8bae1dSRodney W. Grimes void 97df8bae1dSRodney W. Grimes udp_init() 98df8bae1dSRodney W. Grimes { 9915bd2b43SDavid Greenman LIST_INIT(&udb); 10015bd2b43SDavid Greenman udbinfo.listhead = &udb; 101ddd79a97SDavid Greenman udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 1028781d8e9SBruce Evans udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 1038781d8e9SBruce Evans &udbinfo.porthashmask); 10498271db4SGarrett Wollman udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets, 1053d4d47f3SGarrett Wollman ZONE_INTERRUPT, 0); 106df8bae1dSRodney W. Grimes } 107df8bae1dSRodney W. Grimes 108df8bae1dSRodney W. Grimes void 109df8bae1dSRodney W. Grimes udp_input(m, iphlen) 110df8bae1dSRodney W. Grimes register struct mbuf *m; 111df8bae1dSRodney W. Grimes int iphlen; 112df8bae1dSRodney W. Grimes { 113df8bae1dSRodney W. Grimes register struct ip *ip; 114df8bae1dSRodney W. Grimes register struct udphdr *uh; 115df8bae1dSRodney W. Grimes register struct inpcb *inp; 116df8bae1dSRodney W. Grimes struct mbuf *opts = 0; 117df8bae1dSRodney W. Grimes int len; 118df8bae1dSRodney W. Grimes struct ip save_ip; 119df8bae1dSRodney W. Grimes 120df8bae1dSRodney W. Grimes udpstat.udps_ipackets++; 121df8bae1dSRodney W. Grimes 122df8bae1dSRodney W. Grimes /* 123df8bae1dSRodney W. Grimes * Strip IP options, if any; should skip this, 124df8bae1dSRodney W. Grimes * make available to user, and use on returned packets, 125df8bae1dSRodney W. Grimes * but we don't yet have a way to check the checksum 126df8bae1dSRodney W. Grimes * with options still present. 127df8bae1dSRodney W. Grimes */ 128df8bae1dSRodney W. Grimes if (iphlen > sizeof (struct ip)) { 129df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 130df8bae1dSRodney W. Grimes iphlen = sizeof(struct ip); 131df8bae1dSRodney W. Grimes } 132df8bae1dSRodney W. Grimes 133df8bae1dSRodney W. Grimes /* 134df8bae1dSRodney W. Grimes * Get IP and UDP header together in first mbuf. 135df8bae1dSRodney W. Grimes */ 136df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 137df8bae1dSRodney W. Grimes if (m->m_len < iphlen + sizeof(struct udphdr)) { 138df8bae1dSRodney W. Grimes if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 139df8bae1dSRodney W. Grimes udpstat.udps_hdrops++; 140df8bae1dSRodney W. Grimes return; 141df8bae1dSRodney W. Grimes } 142df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 143df8bae1dSRodney W. Grimes } 144df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + iphlen); 145df8bae1dSRodney W. Grimes 146df8bae1dSRodney W. Grimes /* 147df8bae1dSRodney W. Grimes * Make mbuf data length reflect UDP length. 148df8bae1dSRodney W. Grimes * If not enough data to reflect UDP length, drop. 149df8bae1dSRodney W. Grimes */ 150df8bae1dSRodney W. Grimes len = ntohs((u_short)uh->uh_ulen); 151df8bae1dSRodney W. Grimes if (ip->ip_len != len) { 1527eb7a449SAndras Olah if (len > ip->ip_len || len < sizeof(struct udphdr)) { 153df8bae1dSRodney W. Grimes udpstat.udps_badlen++; 154df8bae1dSRodney W. Grimes goto bad; 155df8bae1dSRodney W. Grimes } 156df8bae1dSRodney W. Grimes m_adj(m, len - ip->ip_len); 157df8bae1dSRodney W. Grimes /* ip->ip_len = len; */ 158df8bae1dSRodney W. Grimes } 159df8bae1dSRodney W. Grimes /* 160df8bae1dSRodney W. Grimes * Save a copy of the IP header in case we want restore it 161df8bae1dSRodney W. Grimes * for sending an ICMP error message in response. 162df8bae1dSRodney W. Grimes */ 163df8bae1dSRodney W. Grimes save_ip = *ip; 164df8bae1dSRodney W. Grimes 165df8bae1dSRodney W. Grimes /* 166df8bae1dSRodney W. Grimes * Checksum extended UDP header and data. 167df8bae1dSRodney W. Grimes */ 1686dfab5b1SGarrett Wollman if (uh->uh_sum) { 169df8bae1dSRodney W. Grimes ((struct ipovly *)ip)->ih_next = 0; 170df8bae1dSRodney W. Grimes ((struct ipovly *)ip)->ih_prev = 0; 171df8bae1dSRodney W. Grimes ((struct ipovly *)ip)->ih_x1 = 0; 172df8bae1dSRodney W. Grimes ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 173623ae52eSPoul-Henning Kamp uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 174623ae52eSPoul-Henning Kamp if (uh->uh_sum) { 175df8bae1dSRodney W. Grimes udpstat.udps_badsum++; 176df8bae1dSRodney W. Grimes m_freem(m); 177df8bae1dSRodney W. Grimes return; 178df8bae1dSRodney W. Grimes } 179df8bae1dSRodney W. Grimes } 180df8bae1dSRodney W. Grimes 181df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 182df8bae1dSRodney W. Grimes in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 18382c23ebaSBill Fenner struct inpcb *last; 184df8bae1dSRodney W. Grimes /* 185df8bae1dSRodney W. Grimes * Deliver a multicast or broadcast datagram to *all* sockets 186df8bae1dSRodney W. Grimes * for which the local and remote addresses and ports match 187df8bae1dSRodney W. Grimes * those of the incoming datagram. This allows more than 188df8bae1dSRodney W. Grimes * one process to receive multi/broadcasts on the same port. 189df8bae1dSRodney W. Grimes * (This really ought to be done for unicast datagrams as 190df8bae1dSRodney W. Grimes * well, but that would cause problems with existing 191df8bae1dSRodney W. Grimes * applications that open both address-specific sockets and 192df8bae1dSRodney W. Grimes * a wildcard socket listening to the same port -- they would 193df8bae1dSRodney W. Grimes * end up receiving duplicates of every unicast datagram. 194df8bae1dSRodney W. Grimes * Those applications open the multiple sockets to overcome an 195df8bae1dSRodney W. Grimes * inadequacy of the UDP socket interface, but for backwards 196df8bae1dSRodney W. Grimes * compatibility we avoid the problem here rather than 197df8bae1dSRodney W. Grimes * fixing the interface. Maybe 4.5BSD will remedy this?) 198df8bae1dSRodney W. Grimes */ 199df8bae1dSRodney W. Grimes 200df8bae1dSRodney W. Grimes /* 201df8bae1dSRodney W. Grimes * Construct sockaddr format source address. 202df8bae1dSRodney W. Grimes */ 203df8bae1dSRodney W. Grimes udp_in.sin_port = uh->uh_sport; 204df8bae1dSRodney W. Grimes udp_in.sin_addr = ip->ip_src; 205df8bae1dSRodney W. Grimes m->m_len -= sizeof (struct udpiphdr); 206df8bae1dSRodney W. Grimes m->m_data += sizeof (struct udpiphdr); 207df8bae1dSRodney W. Grimes /* 208df8bae1dSRodney W. Grimes * Locate pcb(s) for datagram. 209df8bae1dSRodney W. Grimes * (Algorithm copied from raw_intr().) 210df8bae1dSRodney W. Grimes */ 211df8bae1dSRodney W. Grimes last = NULL; 21215bd2b43SDavid Greenman for (inp = udb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 213df8bae1dSRodney W. Grimes if (inp->inp_lport != uh->uh_dport) 214df8bae1dSRodney W. Grimes continue; 215df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr != INADDR_ANY) { 216df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr != 217df8bae1dSRodney W. Grimes ip->ip_dst.s_addr) 218df8bae1dSRodney W. Grimes continue; 219df8bae1dSRodney W. Grimes } 220df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 221df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != 222df8bae1dSRodney W. Grimes ip->ip_src.s_addr || 223df8bae1dSRodney W. Grimes inp->inp_fport != uh->uh_sport) 224df8bae1dSRodney W. Grimes continue; 225df8bae1dSRodney W. Grimes } 226df8bae1dSRodney W. Grimes 227df8bae1dSRodney W. Grimes if (last != NULL) { 228df8bae1dSRodney W. Grimes struct mbuf *n; 229df8bae1dSRodney W. Grimes 230df8bae1dSRodney W. Grimes if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 23182c23ebaSBill Fenner if (last->inp_flags & INP_CONTROLOPTS 23282c23ebaSBill Fenner || last->inp_socket->so_options & SO_TIMESTAMP) 23382c23ebaSBill Fenner ip_savecontrol(last, &opts, ip, n); 23482c23ebaSBill Fenner if (sbappendaddr(&last->inp_socket->so_rcv, 235df8bae1dSRodney W. Grimes (struct sockaddr *)&udp_in, 23682c23ebaSBill Fenner n, opts) == 0) { 237df8bae1dSRodney W. Grimes m_freem(n); 23882c23ebaSBill Fenner if (opts) 23982c23ebaSBill Fenner m_freem(opts); 240df8bae1dSRodney W. Grimes udpstat.udps_fullsock++; 241df8bae1dSRodney W. Grimes } else 24282c23ebaSBill Fenner sorwakeup(last->inp_socket); 24382c23ebaSBill Fenner opts = 0; 244df8bae1dSRodney W. Grimes } 245df8bae1dSRodney W. Grimes } 24682c23ebaSBill Fenner last = inp; 247df8bae1dSRodney W. Grimes /* 248df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 249df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 250df8bae1dSRodney W. Grimes * socket options set. This heuristic avoids searching 251df8bae1dSRodney W. Grimes * through all pcbs in the common case of a non-shared 252df8bae1dSRodney W. Grimes * port. It * assumes that an application will never 253df8bae1dSRodney W. Grimes * clear these options after setting them. 254df8bae1dSRodney W. Grimes */ 255694ad0a9SSteve Price if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 256df8bae1dSRodney W. Grimes break; 257df8bae1dSRodney W. Grimes } 258df8bae1dSRodney W. Grimes 259df8bae1dSRodney W. Grimes if (last == NULL) { 260df8bae1dSRodney W. Grimes /* 261df8bae1dSRodney W. Grimes * No matching pcb found; discard datagram. 262df8bae1dSRodney W. Grimes * (No need to send an ICMP Port Unreachable 263df8bae1dSRodney W. Grimes * for a broadcast or multicast datgram.) 264df8bae1dSRodney W. Grimes */ 265df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 266df8bae1dSRodney W. Grimes goto bad; 267df8bae1dSRodney W. Grimes } 26882c23ebaSBill Fenner if (last->inp_flags & INP_CONTROLOPTS 26982c23ebaSBill Fenner || last->inp_socket->so_options & SO_TIMESTAMP) 27082c23ebaSBill Fenner ip_savecontrol(last, &opts, ip, m); 27182c23ebaSBill Fenner if (sbappendaddr(&last->inp_socket->so_rcv, 27282c23ebaSBill Fenner (struct sockaddr *)&udp_in, 27382c23ebaSBill Fenner m, opts) == 0) { 274df8bae1dSRodney W. Grimes udpstat.udps_fullsock++; 275df8bae1dSRodney W. Grimes goto bad; 276df8bae1dSRodney W. Grimes } 27782c23ebaSBill Fenner sorwakeup(last->inp_socket); 278df8bae1dSRodney W. Grimes return; 279df8bae1dSRodney W. Grimes } 280df8bae1dSRodney W. Grimes /* 2816d6a026bSDavid Greenman * Locate pcb for datagram. 282df8bae1dSRodney W. Grimes */ 283c3229e05SDavid Greenman inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 2846d6a026bSDavid Greenman ip->ip_dst, uh->uh_dport, 1); 28515bd2b43SDavid Greenman if (inp == NULL) { 28675cfc95fSAndrey A. Chernov if (log_in_vain) { 287df5c0b8aSBill Fenner char buf[4*sizeof "123"]; 28875cfc95fSAndrey A. Chernov 28975cfc95fSAndrey A. Chernov strcpy(buf, inet_ntoa(ip->ip_dst)); 290592071e8SBruce Evans log(LOG_INFO, 291592071e8SBruce Evans "Connection attempt to UDP %s:%d from %s:%d\n", 292592071e8SBruce Evans buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 293592071e8SBruce Evans ntohs(uh->uh_sport)); 29475cfc95fSAndrey A. Chernov } 295df8bae1dSRodney W. Grimes udpstat.udps_noport++; 296df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 297df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 298df8bae1dSRodney W. Grimes goto bad; 299df8bae1dSRodney W. Grimes } 300df8bae1dSRodney W. Grimes *ip = save_ip; 301df8bae1dSRodney W. Grimes icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 302df8bae1dSRodney W. Grimes return; 303df8bae1dSRodney W. Grimes } 304df8bae1dSRodney W. Grimes 305df8bae1dSRodney W. Grimes /* 306df8bae1dSRodney W. Grimes * Construct sockaddr format source address. 307df8bae1dSRodney W. Grimes * Stuff source address and datagram in user buffer. 308df8bae1dSRodney W. Grimes */ 309df8bae1dSRodney W. Grimes udp_in.sin_port = uh->uh_sport; 310df8bae1dSRodney W. Grimes udp_in.sin_addr = ip->ip_src; 31182dab6ceSGarrett Wollman if (inp->inp_flags & INP_CONTROLOPTS 31282c23ebaSBill Fenner || inp->inp_socket->so_options & SO_TIMESTAMP) 31382c23ebaSBill Fenner ip_savecontrol(inp, &opts, ip, m); 314df8bae1dSRodney W. Grimes iphlen += sizeof(struct udphdr); 315df8bae1dSRodney W. Grimes m->m_len -= iphlen; 316df8bae1dSRodney W. Grimes m->m_pkthdr.len -= iphlen; 317df8bae1dSRodney W. Grimes m->m_data += iphlen; 318df8bae1dSRodney W. Grimes if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, 319df8bae1dSRodney W. Grimes m, opts) == 0) { 320df8bae1dSRodney W. Grimes udpstat.udps_fullsock++; 321df8bae1dSRodney W. Grimes goto bad; 322df8bae1dSRodney W. Grimes } 323df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 324df8bae1dSRodney W. Grimes return; 325df8bae1dSRodney W. Grimes bad: 326df8bae1dSRodney W. Grimes m_freem(m); 327df8bae1dSRodney W. Grimes if (opts) 328df8bae1dSRodney W. Grimes m_freem(opts); 329df8bae1dSRodney W. Grimes } 330df8bae1dSRodney W. Grimes 331df8bae1dSRodney W. Grimes /* 332df8bae1dSRodney W. Grimes * Notify a udp user of an asynchronous error; 333df8bae1dSRodney W. Grimes * just wake up so that he can collect error status. 334df8bae1dSRodney W. Grimes */ 335df8bae1dSRodney W. Grimes static void 336df8bae1dSRodney W. Grimes udp_notify(inp, errno) 337df8bae1dSRodney W. Grimes register struct inpcb *inp; 338df8bae1dSRodney W. Grimes int errno; 339df8bae1dSRodney W. Grimes { 340df8bae1dSRodney W. Grimes inp->inp_socket->so_error = errno; 341df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 342df8bae1dSRodney W. Grimes sowwakeup(inp->inp_socket); 343df8bae1dSRodney W. Grimes } 344df8bae1dSRodney W. Grimes 345df8bae1dSRodney W. Grimes void 346b62d102cSBruce Evans udp_ctlinput(cmd, sa, vip) 347df8bae1dSRodney W. Grimes int cmd; 348df8bae1dSRodney W. Grimes struct sockaddr *sa; 349b62d102cSBruce Evans void *vip; 350df8bae1dSRodney W. Grimes { 351b62d102cSBruce Evans register struct ip *ip = vip; 352df8bae1dSRodney W. Grimes register struct udphdr *uh; 353df8bae1dSRodney W. Grimes 354df8bae1dSRodney W. Grimes if (!PRC_IS_REDIRECT(cmd) && 355df8bae1dSRodney W. Grimes ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) 356df8bae1dSRodney W. Grimes return; 357df8bae1dSRodney W. Grimes if (ip) { 358df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 359df8bae1dSRodney W. Grimes in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, 360df8bae1dSRodney W. Grimes cmd, udp_notify); 361df8bae1dSRodney W. Grimes } else 362df8bae1dSRodney W. Grimes in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); 363df8bae1dSRodney W. Grimes } 364df8bae1dSRodney W. Grimes 3650312fbe9SPoul-Henning Kamp static int 36698271db4SGarrett Wollman udp_pcblist SYSCTL_HANDLER_ARGS 36798271db4SGarrett Wollman { 36898271db4SGarrett Wollman int error, i, n, s; 36998271db4SGarrett Wollman struct inpcb *inp, **inp_list; 37098271db4SGarrett Wollman inp_gen_t gencnt; 37198271db4SGarrett Wollman struct xinpgen xig; 37298271db4SGarrett Wollman 37398271db4SGarrett Wollman /* 37498271db4SGarrett Wollman * The process of preparing the TCB list is too time-consuming and 37598271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 37698271db4SGarrett Wollman */ 37798271db4SGarrett Wollman if (req->oldptr == 0) { 37898271db4SGarrett Wollman n = udbinfo.ipi_count; 37998271db4SGarrett Wollman req->oldidx = 2 * (sizeof xig) 38098271db4SGarrett Wollman + (n + n/8) * sizeof(struct xinpcb); 38198271db4SGarrett Wollman return 0; 38298271db4SGarrett Wollman } 38398271db4SGarrett Wollman 38498271db4SGarrett Wollman if (req->newptr != 0) 38598271db4SGarrett Wollman return EPERM; 38698271db4SGarrett Wollman 38798271db4SGarrett Wollman /* 38898271db4SGarrett Wollman * OK, now we're committed to doing something. 38998271db4SGarrett Wollman */ 39098271db4SGarrett Wollman s = splnet(); 39198271db4SGarrett Wollman gencnt = udbinfo.ipi_gencnt; 39298271db4SGarrett Wollman n = udbinfo.ipi_count; 39398271db4SGarrett Wollman splx(s); 39498271db4SGarrett Wollman 39598271db4SGarrett Wollman xig.xig_len = sizeof xig; 39698271db4SGarrett Wollman xig.xig_count = n; 39798271db4SGarrett Wollman xig.xig_gen = gencnt; 39898271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 39998271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 40098271db4SGarrett Wollman if (error) 40198271db4SGarrett Wollman return error; 40298271db4SGarrett Wollman 40398271db4SGarrett Wollman inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 40498271db4SGarrett Wollman if (inp_list == 0) 40598271db4SGarrett Wollman return ENOMEM; 40698271db4SGarrett Wollman 40798271db4SGarrett Wollman s = splnet(); 40898271db4SGarrett Wollman for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n; 40998271db4SGarrett Wollman inp = inp->inp_list.le_next) { 41098271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) 41198271db4SGarrett Wollman inp_list[i++] = inp; 41298271db4SGarrett Wollman } 41398271db4SGarrett Wollman splx(s); 41498271db4SGarrett Wollman n = i; 41598271db4SGarrett Wollman 41698271db4SGarrett Wollman error = 0; 41798271db4SGarrett Wollman for (i = 0; i < n; i++) { 41898271db4SGarrett Wollman inp = inp_list[i]; 41998271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 42098271db4SGarrett Wollman struct xinpcb xi; 42198271db4SGarrett Wollman xi.xi_len = sizeof xi; 42298271db4SGarrett Wollman /* XXX should avoid extra copy */ 42398271db4SGarrett Wollman bcopy(inp, &xi.xi_inp, sizeof *inp); 42498271db4SGarrett Wollman if (inp->inp_socket) 42598271db4SGarrett Wollman sotoxsocket(inp->inp_socket, &xi.xi_socket); 42698271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 42798271db4SGarrett Wollman } 42898271db4SGarrett Wollman } 42998271db4SGarrett Wollman if (!error) { 43098271db4SGarrett Wollman /* 43198271db4SGarrett Wollman * Give the user an updated idea of our state. 43298271db4SGarrett Wollman * If the generation differs from what we told 43398271db4SGarrett Wollman * her before, she knows that something happened 43498271db4SGarrett Wollman * while we were processing this request, and it 43598271db4SGarrett Wollman * might be necessary to retry. 43698271db4SGarrett Wollman */ 43798271db4SGarrett Wollman s = splnet(); 43898271db4SGarrett Wollman xig.xig_gen = udbinfo.ipi_gencnt; 43998271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 44098271db4SGarrett Wollman xig.xig_count = udbinfo.ipi_count; 44198271db4SGarrett Wollman splx(s); 44298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 44398271db4SGarrett Wollman } 44498271db4SGarrett Wollman free(inp_list, M_TEMP); 44598271db4SGarrett Wollman return error; 44698271db4SGarrett Wollman } 44798271db4SGarrett Wollman 44898271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 44998271db4SGarrett Wollman udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 45098271db4SGarrett Wollman 45198271db4SGarrett Wollman static int 452a29f300eSGarrett Wollman udp_output(inp, m, addr, control, p) 453df8bae1dSRodney W. Grimes register struct inpcb *inp; 454df8bae1dSRodney W. Grimes register struct mbuf *m; 45557bf258eSGarrett Wollman struct sockaddr *addr; 45657bf258eSGarrett Wollman struct mbuf *control; 457a29f300eSGarrett Wollman struct proc *p; 458df8bae1dSRodney W. Grimes { 459df8bae1dSRodney W. Grimes register struct udpiphdr *ui; 460df8bae1dSRodney W. Grimes register int len = m->m_pkthdr.len; 461df8bae1dSRodney W. Grimes struct in_addr laddr; 46226f9a767SRodney W. Grimes int s = 0, error = 0; 463df8bae1dSRodney W. Grimes 464df8bae1dSRodney W. Grimes if (control) 465df8bae1dSRodney W. Grimes m_freem(control); /* XXX */ 466df8bae1dSRodney W. Grimes 467430d30d8SBill Fenner if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 468430d30d8SBill Fenner error = EMSGSIZE; 469430d30d8SBill Fenner goto release; 470430d30d8SBill Fenner } 471430d30d8SBill Fenner 472df8bae1dSRodney W. Grimes if (addr) { 473df8bae1dSRodney W. Grimes laddr = inp->inp_laddr; 474df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 475df8bae1dSRodney W. Grimes error = EISCONN; 476df8bae1dSRodney W. Grimes goto release; 477df8bae1dSRodney W. Grimes } 478df8bae1dSRodney W. Grimes /* 479df8bae1dSRodney W. Grimes * Must block input while temporarily connected. 480df8bae1dSRodney W. Grimes */ 481df8bae1dSRodney W. Grimes s = splnet(); 482a29f300eSGarrett Wollman error = in_pcbconnect(inp, addr, p); 483df8bae1dSRodney W. Grimes if (error) { 484df8bae1dSRodney W. Grimes splx(s); 485df8bae1dSRodney W. Grimes goto release; 486df8bae1dSRodney W. Grimes } 487df8bae1dSRodney W. Grimes } else { 488df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr == INADDR_ANY) { 489df8bae1dSRodney W. Grimes error = ENOTCONN; 490df8bae1dSRodney W. Grimes goto release; 491df8bae1dSRodney W. Grimes } 492df8bae1dSRodney W. Grimes } 493df8bae1dSRodney W. Grimes /* 494df8bae1dSRodney W. Grimes * Calculate data length and get a mbuf 495df8bae1dSRodney W. Grimes * for UDP and IP headers. 496df8bae1dSRodney W. Grimes */ 497df8bae1dSRodney W. Grimes M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 498df8bae1dSRodney W. Grimes if (m == 0) { 499df8bae1dSRodney W. Grimes error = ENOBUFS; 5001c09f774SGarrett Wollman if (addr) 5011c09f774SGarrett Wollman splx(s); 502df8bae1dSRodney W. Grimes goto release; 503df8bae1dSRodney W. Grimes } 504df8bae1dSRodney W. Grimes 505df8bae1dSRodney W. Grimes /* 506df8bae1dSRodney W. Grimes * Fill in mbuf with extended UDP header 507df8bae1dSRodney W. Grimes * and addresses and length put into network format. 508df8bae1dSRodney W. Grimes */ 509df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 510df8bae1dSRodney W. Grimes ui->ui_next = ui->ui_prev = 0; 511df8bae1dSRodney W. Grimes ui->ui_x1 = 0; 512df8bae1dSRodney W. Grimes ui->ui_pr = IPPROTO_UDP; 513df8bae1dSRodney W. Grimes ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); 514df8bae1dSRodney W. Grimes ui->ui_src = inp->inp_laddr; 515df8bae1dSRodney W. Grimes ui->ui_dst = inp->inp_faddr; 516df8bae1dSRodney W. Grimes ui->ui_sport = inp->inp_lport; 517df8bae1dSRodney W. Grimes ui->ui_dport = inp->inp_fport; 518df8bae1dSRodney W. Grimes ui->ui_ulen = ui->ui_len; 519df8bae1dSRodney W. Grimes 520df8bae1dSRodney W. Grimes /* 521df8bae1dSRodney W. Grimes * Stuff checksum and output datagram. 522df8bae1dSRodney W. Grimes */ 523df8bae1dSRodney W. Grimes ui->ui_sum = 0; 524df8bae1dSRodney W. Grimes if (udpcksum) { 525df8bae1dSRodney W. Grimes if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 526df8bae1dSRodney W. Grimes ui->ui_sum = 0xffff; 527df8bae1dSRodney W. Grimes } 528df8bae1dSRodney W. Grimes ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 529ca98b82cSDavid Greenman ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 530ca98b82cSDavid Greenman ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 531df8bae1dSRodney W. Grimes udpstat.udps_opackets++; 532df8bae1dSRodney W. Grimes error = ip_output(m, inp->inp_options, &inp->inp_route, 533df8bae1dSRodney W. Grimes inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 534df8bae1dSRodney W. Grimes inp->inp_moptions); 535df8bae1dSRodney W. Grimes 536df8bae1dSRodney W. Grimes if (addr) { 537df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 53857bf258eSGarrett Wollman inp->inp_laddr = laddr; /* XXX rehash? */ 539df8bae1dSRodney W. Grimes splx(s); 540df8bae1dSRodney W. Grimes } 541df8bae1dSRodney W. Grimes return (error); 542df8bae1dSRodney W. Grimes 543df8bae1dSRodney W. Grimes release: 544df8bae1dSRodney W. Grimes m_freem(m); 545df8bae1dSRodney W. Grimes return (error); 546df8bae1dSRodney W. Grimes } 547df8bae1dSRodney W. Grimes 5480312fbe9SPoul-Henning Kamp static u_long udp_sendspace = 9216; /* really max datagram size */ 549df8bae1dSRodney W. Grimes /* 40 1K datagrams */ 5500312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 5510312fbe9SPoul-Henning Kamp &udp_sendspace, 0, ""); 5520312fbe9SPoul-Henning Kamp 5530312fbe9SPoul-Henning Kamp static u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 5540312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 5550312fbe9SPoul-Henning Kamp &udp_recvspace, 0, ""); 556df8bae1dSRodney W. Grimes 557d0390e05SGarrett Wollman static int 558d0390e05SGarrett Wollman udp_abort(struct socket *so) 559df8bae1dSRodney W. Grimes { 560d0390e05SGarrett Wollman struct inpcb *inp; 561df8bae1dSRodney W. Grimes int s; 562df8bae1dSRodney W. Grimes 563d0390e05SGarrett Wollman inp = sotoinpcb(so); 564d0390e05SGarrett Wollman if (inp == 0) 565d0390e05SGarrett Wollman return EINVAL; /* ??? possible? panic instead? */ 566d0390e05SGarrett Wollman soisdisconnected(so); 567d0390e05SGarrett Wollman s = splnet(); 568d0390e05SGarrett Wollman in_pcbdetach(inp); 569d0390e05SGarrett Wollman splx(s); 570d0390e05SGarrett Wollman return 0; 571df8bae1dSRodney W. Grimes } 572df8bae1dSRodney W. Grimes 573d0390e05SGarrett Wollman static int 574a29f300eSGarrett Wollman udp_attach(struct socket *so, int proto, struct proc *p) 575d0390e05SGarrett Wollman { 576d0390e05SGarrett Wollman struct inpcb *inp; 577d0390e05SGarrett Wollman int s, error; 578d0390e05SGarrett Wollman 579d0390e05SGarrett Wollman inp = sotoinpcb(so); 580d0390e05SGarrett Wollman if (inp != 0) 581d0390e05SGarrett Wollman return EINVAL; 582d0390e05SGarrett Wollman 583df8bae1dSRodney W. Grimes s = splnet(); 584a29f300eSGarrett Wollman error = in_pcballoc(so, &udbinfo, p); 585df8bae1dSRodney W. Grimes splx(s); 586df8bae1dSRodney W. Grimes if (error) 587d0390e05SGarrett Wollman return error; 588df8bae1dSRodney W. Grimes error = soreserve(so, udp_sendspace, udp_recvspace); 589df8bae1dSRodney W. Grimes if (error) 590d0390e05SGarrett Wollman return error; 591ca98b82cSDavid Greenman ((struct inpcb *) so->so_pcb)->inp_ip_ttl = ip_defttl; 592d0390e05SGarrett Wollman return 0; 593df8bae1dSRodney W. Grimes } 594d0390e05SGarrett Wollman 595d0390e05SGarrett Wollman static int 59657bf258eSGarrett Wollman udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 597d0390e05SGarrett Wollman { 598d0390e05SGarrett Wollman struct inpcb *inp; 599d0390e05SGarrett Wollman int s, error; 600d0390e05SGarrett Wollman 601d0390e05SGarrett Wollman inp = sotoinpcb(so); 602d0390e05SGarrett Wollman if (inp == 0) 603d0390e05SGarrett Wollman return EINVAL; 604df8bae1dSRodney W. Grimes s = splnet(); 605a29f300eSGarrett Wollman error = in_pcbbind(inp, nam, p); 606d0390e05SGarrett Wollman splx(s); 607d0390e05SGarrett Wollman return error; 608d0390e05SGarrett Wollman } 609d0390e05SGarrett Wollman 610d0390e05SGarrett Wollman static int 61157bf258eSGarrett Wollman udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 612d0390e05SGarrett Wollman { 613d0390e05SGarrett Wollman struct inpcb *inp; 614d0390e05SGarrett Wollman int s, error; 615d0390e05SGarrett Wollman 616d0390e05SGarrett Wollman inp = sotoinpcb(so); 617d0390e05SGarrett Wollman if (inp == 0) 618d0390e05SGarrett Wollman return EINVAL; 619d0390e05SGarrett Wollman if (inp->inp_faddr.s_addr != INADDR_ANY) 620d0390e05SGarrett Wollman return EISCONN; 621d0390e05SGarrett Wollman s = splnet(); 622a29f300eSGarrett Wollman error = in_pcbconnect(inp, nam, p); 623df8bae1dSRodney W. Grimes splx(s); 624df8bae1dSRodney W. Grimes if (error == 0) 625df8bae1dSRodney W. Grimes soisconnected(so); 626d0390e05SGarrett Wollman return error; 627df8bae1dSRodney W. Grimes } 628d0390e05SGarrett Wollman 629d0390e05SGarrett Wollman static int 630d0390e05SGarrett Wollman udp_detach(struct socket *so) 631d0390e05SGarrett Wollman { 632d0390e05SGarrett Wollman struct inpcb *inp; 633d0390e05SGarrett Wollman int s; 634d0390e05SGarrett Wollman 635d0390e05SGarrett Wollman inp = sotoinpcb(so); 636d0390e05SGarrett Wollman if (inp == 0) 637d0390e05SGarrett Wollman return EINVAL; 638d0390e05SGarrett Wollman s = splnet(); 639d0390e05SGarrett Wollman in_pcbdetach(inp); 640d0390e05SGarrett Wollman splx(s); 641d0390e05SGarrett Wollman return 0; 642d0390e05SGarrett Wollman } 643d0390e05SGarrett Wollman 644d0390e05SGarrett Wollman static int 645d0390e05SGarrett Wollman udp_disconnect(struct socket *so) 646d0390e05SGarrett Wollman { 647d0390e05SGarrett Wollman struct inpcb *inp; 648d0390e05SGarrett Wollman int s; 649d0390e05SGarrett Wollman 650d0390e05SGarrett Wollman inp = sotoinpcb(so); 651d0390e05SGarrett Wollman if (inp == 0) 652d0390e05SGarrett Wollman return EINVAL; 653d0390e05SGarrett Wollman if (inp->inp_faddr.s_addr == INADDR_ANY) 654d0390e05SGarrett Wollman return ENOTCONN; 655d0390e05SGarrett Wollman 656df8bae1dSRodney W. Grimes s = splnet(); 657df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 658df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 659df8bae1dSRodney W. Grimes splx(s); 660df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTED; /* XXX */ 661d0390e05SGarrett Wollman return 0; 662df8bae1dSRodney W. Grimes } 663df8bae1dSRodney W. Grimes 664d0390e05SGarrett Wollman static int 66557bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 666a29f300eSGarrett Wollman struct mbuf *control, struct proc *p) 667d0390e05SGarrett Wollman { 668d0390e05SGarrett Wollman struct inpcb *inp; 669d0390e05SGarrett Wollman 670d0390e05SGarrett Wollman inp = sotoinpcb(so); 671d0390e05SGarrett Wollman if (inp == 0) { 672d0390e05SGarrett Wollman m_freem(m); 673d0390e05SGarrett Wollman return EINVAL; 674d0390e05SGarrett Wollman } 675a29f300eSGarrett Wollman return udp_output(inp, m, addr, control, p); 676d0390e05SGarrett Wollman } 677d0390e05SGarrett Wollman 678d0390e05SGarrett Wollman static int 679d0390e05SGarrett Wollman udp_shutdown(struct socket *so) 680d0390e05SGarrett Wollman { 681d0390e05SGarrett Wollman struct inpcb *inp; 682d0390e05SGarrett Wollman 683d0390e05SGarrett Wollman inp = sotoinpcb(so); 684d0390e05SGarrett Wollman if (inp == 0) 685d0390e05SGarrett Wollman return EINVAL; 686d0390e05SGarrett Wollman socantsendmore(so); 687d0390e05SGarrett Wollman return 0; 688d0390e05SGarrett Wollman } 689d0390e05SGarrett Wollman 690d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = { 691117bcae7SGarrett Wollman udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 692117bcae7SGarrett Wollman pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 693117bcae7SGarrett Wollman pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 694117bcae7SGarrett Wollman pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 695f8f6cbbaSPeter Wemm in_setsockaddr, sosend, soreceive, sopoll 696d0390e05SGarrett Wollman }; 697