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 346d6a026bSDavid Greenman * $Id: udp_usrreq.c,v 1.28 1996/06/08 08:19:03 bde Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 382ee45d7dSDavid Greenman #include <sys/queue.h> 3926f9a767SRodney W. Grimes #include <sys/systm.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> 45df8bae1dSRodney W. Grimes #include <sys/errno.h> 46df8bae1dSRodney W. Grimes #include <sys/stat.h> 470312fbe9SPoul-Henning Kamp #include <sys/kernel.h> 48b5e8ce9fSBruce Evans #include <sys/sysctl.h> 49816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes #include <net/if.h> 52df8bae1dSRodney W. Grimes #include <net/route.h> 53df8bae1dSRodney W. Grimes 54df8bae1dSRodney W. Grimes #include <netinet/in.h> 55df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 56df8bae1dSRodney W. Grimes #include <netinet/ip.h> 57df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 58b5e8ce9fSBruce Evans #include <netinet/in_var.h> 59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 60df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h> 61df8bae1dSRodney W. Grimes #include <netinet/udp.h> 62df8bae1dSRodney W. Grimes #include <netinet/udp_var.h> 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes /* 65df8bae1dSRodney W. Grimes * UDP protocol implementation. 66df8bae1dSRodney W. Grimes * Per RFC 768, August, 1980. 67df8bae1dSRodney W. Grimes */ 68df8bae1dSRodney W. Grimes #ifndef COMPAT_42 690312fbe9SPoul-Henning Kamp static int udpcksum = 1; 70df8bae1dSRodney W. Grimes #else 710312fbe9SPoul-Henning Kamp static int udpcksum = 0; /* XXX */ 72df8bae1dSRodney W. Grimes #endif 730312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 740312fbe9SPoul-Henning Kamp &udpcksum, 0, ""); 75df8bae1dSRodney W. Grimes 76d78a37adSPaul Traina static int log_in_vain = 0; 77816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 78816a3d83SPoul-Henning Kamp &log_in_vain, 0, ""); 79816a3d83SPoul-Henning Kamp 80f708ef1bSPoul-Henning Kamp static struct inpcbhead udb; /* from udp_var.h */ 81f708ef1bSPoul-Henning Kamp static struct inpcbinfo udbinfo; 8215bd2b43SDavid Greenman 8315bd2b43SDavid Greenman #ifndef UDBHASHSIZE 8415bd2b43SDavid Greenman #define UDBHASHSIZE 64 8515bd2b43SDavid Greenman #endif 8615bd2b43SDavid Greenman 87f708ef1bSPoul-Henning Kamp static struct udpstat udpstat; /* from udp_var.h */ 880312fbe9SPoul-Henning Kamp SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, 890312fbe9SPoul-Henning Kamp &udpstat, udpstat, ""); 90f2ea20e6SGarrett Wollman 910312fbe9SPoul-Henning Kamp static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 92df8bae1dSRodney W. Grimes 93df8bae1dSRodney W. Grimes static void udp_detach __P((struct inpcb *)); 94ff98689dSBruce Evans static int udp_output __P((struct inpcb *, struct mbuf *, struct mbuf *, 95ff98689dSBruce Evans struct mbuf *)); 96df8bae1dSRodney W. Grimes static void udp_notify __P((struct inpcb *, int)); 97df8bae1dSRodney W. Grimes static struct mbuf *udp_saveopt __P((caddr_t, int, int)); 9882dab6ceSGarrett Wollman static struct mbuf *udp_timestamp __P((void)); 99df8bae1dSRodney W. Grimes 100df8bae1dSRodney W. Grimes void 101df8bae1dSRodney W. Grimes udp_init() 102df8bae1dSRodney W. Grimes { 10315bd2b43SDavid Greenman LIST_INIT(&udb); 10415bd2b43SDavid Greenman udbinfo.listhead = &udb; 10515bd2b43SDavid Greenman udbinfo.hashbase = phashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashsize); 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)) { 183df8bae1dSRodney W. Grimes struct socket *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) { 231df8bae1dSRodney W. Grimes if (sbappendaddr(&last->so_rcv, 232df8bae1dSRodney W. Grimes (struct sockaddr *)&udp_in, 233df8bae1dSRodney W. Grimes n, (struct mbuf *)0) == 0) { 234df8bae1dSRodney W. Grimes m_freem(n); 235df8bae1dSRodney W. Grimes udpstat.udps_fullsock++; 236df8bae1dSRodney W. Grimes } else 237df8bae1dSRodney W. Grimes sorwakeup(last); 238df8bae1dSRodney W. Grimes } 239df8bae1dSRodney W. Grimes } 240df8bae1dSRodney W. Grimes last = inp->inp_socket; 241df8bae1dSRodney W. Grimes /* 242df8bae1dSRodney W. Grimes * Don't look for additional matches if this one does 243df8bae1dSRodney W. Grimes * not have either the SO_REUSEPORT or SO_REUSEADDR 244df8bae1dSRodney W. Grimes * socket options set. This heuristic avoids searching 245df8bae1dSRodney W. Grimes * through all pcbs in the common case of a non-shared 246df8bae1dSRodney W. Grimes * port. It * assumes that an application will never 247df8bae1dSRodney W. Grimes * clear these options after setting them. 248df8bae1dSRodney W. Grimes */ 249df8bae1dSRodney W. Grimes if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR) == 0)) 250df8bae1dSRodney W. Grimes break; 251df8bae1dSRodney W. Grimes } 252df8bae1dSRodney W. Grimes 253df8bae1dSRodney W. Grimes if (last == NULL) { 254df8bae1dSRodney W. Grimes /* 255df8bae1dSRodney W. Grimes * No matching pcb found; discard datagram. 256df8bae1dSRodney W. Grimes * (No need to send an ICMP Port Unreachable 257df8bae1dSRodney W. Grimes * for a broadcast or multicast datgram.) 258df8bae1dSRodney W. Grimes */ 259df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 260df8bae1dSRodney W. Grimes goto bad; 261df8bae1dSRodney W. Grimes } 262df8bae1dSRodney W. Grimes if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&udp_in, 263df8bae1dSRodney W. Grimes m, (struct mbuf *)0) == 0) { 264df8bae1dSRodney W. Grimes udpstat.udps_fullsock++; 265df8bae1dSRodney W. Grimes goto bad; 266df8bae1dSRodney W. Grimes } 267df8bae1dSRodney W. Grimes sorwakeup(last); 268df8bae1dSRodney W. Grimes return; 269df8bae1dSRodney W. Grimes } 270df8bae1dSRodney W. Grimes /* 2716d6a026bSDavid Greenman * Locate pcb for datagram. 272df8bae1dSRodney W. Grimes */ 27315bd2b43SDavid Greenman inp = in_pcblookuphash(&udbinfo, ip->ip_src, uh->uh_sport, 2746d6a026bSDavid Greenman ip->ip_dst, uh->uh_dport, 1); 27515bd2b43SDavid Greenman if (inp == NULL) { 27675cfc95fSAndrey A. Chernov if (log_in_vain) { 277df5c0b8aSBill Fenner char buf[4*sizeof "123"]; 27875cfc95fSAndrey A. Chernov 27975cfc95fSAndrey A. Chernov strcpy(buf, inet_ntoa(ip->ip_dst)); 280816a3d83SPoul-Henning Kamp log(LOG_INFO, "Connection attempt to UDP %s:%d" 281816a3d83SPoul-Henning Kamp " from %s:%d\n", 28275cfc95fSAndrey A. Chernov buf, ntohs(uh->uh_dport), 283816a3d83SPoul-Henning Kamp inet_ntoa(ip->ip_src), ntohs(uh->uh_sport)); 28475cfc95fSAndrey A. Chernov } 285df8bae1dSRodney W. Grimes udpstat.udps_noport++; 286df8bae1dSRodney W. Grimes if (m->m_flags & (M_BCAST | M_MCAST)) { 287df8bae1dSRodney W. Grimes udpstat.udps_noportbcast++; 288df8bae1dSRodney W. Grimes goto bad; 289df8bae1dSRodney W. Grimes } 290df8bae1dSRodney W. Grimes *ip = save_ip; 291df8bae1dSRodney W. Grimes icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 292df8bae1dSRodney W. Grimes return; 293df8bae1dSRodney W. Grimes } 294df8bae1dSRodney W. Grimes 295df8bae1dSRodney W. Grimes /* 296df8bae1dSRodney W. Grimes * Construct sockaddr format source address. 297df8bae1dSRodney W. Grimes * Stuff source address and datagram in user buffer. 298df8bae1dSRodney W. Grimes */ 299df8bae1dSRodney W. Grimes udp_in.sin_port = uh->uh_sport; 300df8bae1dSRodney W. Grimes udp_in.sin_addr = ip->ip_src; 30182dab6ceSGarrett Wollman if (inp->inp_flags & INP_CONTROLOPTS 30282dab6ceSGarrett Wollman || inp->inp_socket->so_options & SO_TIMESTAMP) { 303df8bae1dSRodney W. Grimes struct mbuf **mp = &opts; 304df8bae1dSRodney W. Grimes 30582dab6ceSGarrett Wollman if (inp->inp_socket->so_options & SO_TIMESTAMP) { 30682dab6ceSGarrett Wollman if (*mp = udp_timestamp()) 30782dab6ceSGarrett Wollman mp = &(*mp)->m_next; 30882dab6ceSGarrett Wollman } 309df8bae1dSRodney W. Grimes if (inp->inp_flags & INP_RECVDSTADDR) { 310df8bae1dSRodney W. Grimes *mp = udp_saveopt((caddr_t) &ip->ip_dst, 311df8bae1dSRodney W. Grimes sizeof(struct in_addr), IP_RECVDSTADDR); 312df8bae1dSRodney W. Grimes if (*mp) 313df8bae1dSRodney W. Grimes mp = &(*mp)->m_next; 314df8bae1dSRodney W. Grimes } 315df8bae1dSRodney W. Grimes #ifdef notyet 316df8bae1dSRodney W. Grimes /* options were tossed above */ 317df8bae1dSRodney W. Grimes if (inp->inp_flags & INP_RECVOPTS) { 318df8bae1dSRodney W. Grimes *mp = udp_saveopt((caddr_t) opts_deleted_above, 319df8bae1dSRodney W. Grimes sizeof(struct in_addr), IP_RECVOPTS); 320df8bae1dSRodney W. Grimes if (*mp) 321df8bae1dSRodney W. Grimes mp = &(*mp)->m_next; 322df8bae1dSRodney W. Grimes } 323df8bae1dSRodney W. Grimes /* ip_srcroute doesn't do what we want here, need to fix */ 324df8bae1dSRodney W. Grimes if (inp->inp_flags & INP_RECVRETOPTS) { 325df8bae1dSRodney W. Grimes *mp = udp_saveopt((caddr_t) ip_srcroute(), 326df8bae1dSRodney W. Grimes sizeof(struct in_addr), IP_RECVRETOPTS); 327df8bae1dSRodney W. Grimes if (*mp) 328df8bae1dSRodney W. Grimes mp = &(*mp)->m_next; 329df8bae1dSRodney W. Grimes } 330df8bae1dSRodney W. Grimes #endif 331df8bae1dSRodney W. Grimes } 332df8bae1dSRodney W. Grimes iphlen += sizeof(struct udphdr); 333df8bae1dSRodney W. Grimes m->m_len -= iphlen; 334df8bae1dSRodney W. Grimes m->m_pkthdr.len -= iphlen; 335df8bae1dSRodney W. Grimes m->m_data += iphlen; 336df8bae1dSRodney W. Grimes if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, 337df8bae1dSRodney W. Grimes m, opts) == 0) { 338df8bae1dSRodney W. Grimes udpstat.udps_fullsock++; 339df8bae1dSRodney W. Grimes goto bad; 340df8bae1dSRodney W. Grimes } 341df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 342df8bae1dSRodney W. Grimes return; 343df8bae1dSRodney W. Grimes bad: 344df8bae1dSRodney W. Grimes m_freem(m); 345df8bae1dSRodney W. Grimes if (opts) 346df8bae1dSRodney W. Grimes m_freem(opts); 347df8bae1dSRodney W. Grimes } 348df8bae1dSRodney W. Grimes 349df8bae1dSRodney W. Grimes /* 350df8bae1dSRodney W. Grimes * Create a "control" mbuf containing the specified data 351df8bae1dSRodney W. Grimes * with the specified type for presentation with a datagram. 352df8bae1dSRodney W. Grimes */ 353df8bae1dSRodney W. Grimes struct mbuf * 354df8bae1dSRodney W. Grimes udp_saveopt(p, size, type) 355df8bae1dSRodney W. Grimes caddr_t p; 356df8bae1dSRodney W. Grimes register int size; 357df8bae1dSRodney W. Grimes int type; 358df8bae1dSRodney W. Grimes { 359df8bae1dSRodney W. Grimes register struct cmsghdr *cp; 360df8bae1dSRodney W. Grimes struct mbuf *m; 361df8bae1dSRodney W. Grimes 362df8bae1dSRodney W. Grimes if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 363df8bae1dSRodney W. Grimes return ((struct mbuf *) NULL); 364df8bae1dSRodney W. Grimes cp = (struct cmsghdr *) mtod(m, struct cmsghdr *); 3650453d3cbSBruce Evans bcopy(p, CMSG_DATA(cp), size); 366df8bae1dSRodney W. Grimes size += sizeof(*cp); 367df8bae1dSRodney W. Grimes m->m_len = size; 368df8bae1dSRodney W. Grimes cp->cmsg_len = size; 369df8bae1dSRodney W. Grimes cp->cmsg_level = IPPROTO_IP; 370df8bae1dSRodney W. Grimes cp->cmsg_type = type; 371df8bae1dSRodney W. Grimes return (m); 372df8bae1dSRodney W. Grimes } 373df8bae1dSRodney W. Grimes 374df8bae1dSRodney W. Grimes /* 37582dab6ceSGarrett Wollman * Create an mbuf with the SCM_TIMESTAMP socket option data (struct timeval) 37682dab6ceSGarrett Wollman * inside. This really isn't UDP specific; but there's not really a better 37782dab6ceSGarrett Wollman * place for it yet.. 37882dab6ceSGarrett Wollman */ 37982dab6ceSGarrett Wollman static struct mbuf * 38082dab6ceSGarrett Wollman udp_timestamp() 38182dab6ceSGarrett Wollman { 38282dab6ceSGarrett Wollman register struct cmsghdr *cp; 38382dab6ceSGarrett Wollman struct mbuf *m; 38482dab6ceSGarrett Wollman struct timeval tv; 38582dab6ceSGarrett Wollman 38682dab6ceSGarrett Wollman MGET(m, M_DONTWAIT, MT_CONTROL); 38782dab6ceSGarrett Wollman if (m == 0) 38882dab6ceSGarrett Wollman return (struct mbuf *) 0; 38982dab6ceSGarrett Wollman 39082dab6ceSGarrett Wollman microtime(&tv); 39182dab6ceSGarrett Wollman cp = (struct cmsghdr *) mtod(m, struct cmsghdr *); 39282dab6ceSGarrett Wollman cp->cmsg_len = 39382dab6ceSGarrett Wollman m->m_len = sizeof(*cp) + sizeof(struct timeval); 39482dab6ceSGarrett Wollman cp->cmsg_level = SOL_SOCKET; 39582dab6ceSGarrett Wollman cp->cmsg_type = SCM_TIMESTAMP; 39682dab6ceSGarrett Wollman (void) memcpy(CMSG_DATA(cp), &tv, sizeof(struct timeval)); 39782dab6ceSGarrett Wollman return (m); 39882dab6ceSGarrett Wollman } 39982dab6ceSGarrett Wollman 40082dab6ceSGarrett Wollman /* 401df8bae1dSRodney W. Grimes * Notify a udp user of an asynchronous error; 402df8bae1dSRodney W. Grimes * just wake up so that he can collect error status. 403df8bae1dSRodney W. Grimes */ 404df8bae1dSRodney W. Grimes static void 405df8bae1dSRodney W. Grimes udp_notify(inp, errno) 406df8bae1dSRodney W. Grimes register struct inpcb *inp; 407df8bae1dSRodney W. Grimes int errno; 408df8bae1dSRodney W. Grimes { 409df8bae1dSRodney W. Grimes inp->inp_socket->so_error = errno; 410df8bae1dSRodney W. Grimes sorwakeup(inp->inp_socket); 411df8bae1dSRodney W. Grimes sowwakeup(inp->inp_socket); 412df8bae1dSRodney W. Grimes } 413df8bae1dSRodney W. Grimes 414df8bae1dSRodney W. Grimes void 415b62d102cSBruce Evans udp_ctlinput(cmd, sa, vip) 416df8bae1dSRodney W. Grimes int cmd; 417df8bae1dSRodney W. Grimes struct sockaddr *sa; 418b62d102cSBruce Evans void *vip; 419df8bae1dSRodney W. Grimes { 420b62d102cSBruce Evans register struct ip *ip = vip; 421df8bae1dSRodney W. Grimes register struct udphdr *uh; 422df8bae1dSRodney W. Grimes 423df8bae1dSRodney W. Grimes if (!PRC_IS_REDIRECT(cmd) && 424df8bae1dSRodney W. Grimes ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) 425df8bae1dSRodney W. Grimes return; 426df8bae1dSRodney W. Grimes if (ip) { 427df8bae1dSRodney W. Grimes uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 428df8bae1dSRodney W. Grimes in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, 429df8bae1dSRodney W. Grimes cmd, udp_notify); 430df8bae1dSRodney W. Grimes } else 431df8bae1dSRodney W. Grimes in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); 432df8bae1dSRodney W. Grimes } 433df8bae1dSRodney W. Grimes 4340312fbe9SPoul-Henning Kamp static int 435df8bae1dSRodney W. Grimes udp_output(inp, m, addr, control) 436df8bae1dSRodney W. Grimes register struct inpcb *inp; 437df8bae1dSRodney W. Grimes register struct mbuf *m; 438df8bae1dSRodney W. Grimes struct mbuf *addr, *control; 439df8bae1dSRodney W. Grimes { 440df8bae1dSRodney W. Grimes register struct udpiphdr *ui; 441df8bae1dSRodney W. Grimes register int len = m->m_pkthdr.len; 442df8bae1dSRodney W. Grimes struct in_addr laddr; 44326f9a767SRodney W. Grimes int s = 0, error = 0; 444df8bae1dSRodney W. Grimes 445df8bae1dSRodney W. Grimes if (control) 446df8bae1dSRodney W. Grimes m_freem(control); /* XXX */ 447df8bae1dSRodney W. Grimes 448df8bae1dSRodney W. Grimes if (addr) { 449df8bae1dSRodney W. Grimes laddr = inp->inp_laddr; 450df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 451df8bae1dSRodney W. Grimes error = EISCONN; 452df8bae1dSRodney W. Grimes goto release; 453df8bae1dSRodney W. Grimes } 454df8bae1dSRodney W. Grimes /* 455df8bae1dSRodney W. Grimes * Must block input while temporarily connected. 456df8bae1dSRodney W. Grimes */ 457df8bae1dSRodney W. Grimes s = splnet(); 458df8bae1dSRodney W. Grimes error = in_pcbconnect(inp, addr); 459df8bae1dSRodney W. Grimes if (error) { 460df8bae1dSRodney W. Grimes splx(s); 461df8bae1dSRodney W. Grimes goto release; 462df8bae1dSRodney W. Grimes } 463df8bae1dSRodney W. Grimes } else { 464df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr == INADDR_ANY) { 465df8bae1dSRodney W. Grimes error = ENOTCONN; 466df8bae1dSRodney W. Grimes goto release; 467df8bae1dSRodney W. Grimes } 468df8bae1dSRodney W. Grimes } 469df8bae1dSRodney W. Grimes /* 470df8bae1dSRodney W. Grimes * Calculate data length and get a mbuf 471df8bae1dSRodney W. Grimes * for UDP and IP headers. 472df8bae1dSRodney W. Grimes */ 473df8bae1dSRodney W. Grimes M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 474df8bae1dSRodney W. Grimes if (m == 0) { 475df8bae1dSRodney W. Grimes error = ENOBUFS; 4761c09f774SGarrett Wollman if (addr) 4771c09f774SGarrett Wollman splx(s); 478df8bae1dSRodney W. Grimes goto release; 479df8bae1dSRodney W. Grimes } 480df8bae1dSRodney W. Grimes 481df8bae1dSRodney W. Grimes /* 482df8bae1dSRodney W. Grimes * Fill in mbuf with extended UDP header 483df8bae1dSRodney W. Grimes * and addresses and length put into network format. 484df8bae1dSRodney W. Grimes */ 485df8bae1dSRodney W. Grimes ui = mtod(m, struct udpiphdr *); 486df8bae1dSRodney W. Grimes ui->ui_next = ui->ui_prev = 0; 487df8bae1dSRodney W. Grimes ui->ui_x1 = 0; 488df8bae1dSRodney W. Grimes ui->ui_pr = IPPROTO_UDP; 489df8bae1dSRodney W. Grimes ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); 490df8bae1dSRodney W. Grimes ui->ui_src = inp->inp_laddr; 491df8bae1dSRodney W. Grimes ui->ui_dst = inp->inp_faddr; 492df8bae1dSRodney W. Grimes ui->ui_sport = inp->inp_lport; 493df8bae1dSRodney W. Grimes ui->ui_dport = inp->inp_fport; 494df8bae1dSRodney W. Grimes ui->ui_ulen = ui->ui_len; 495df8bae1dSRodney W. Grimes 496df8bae1dSRodney W. Grimes /* 497df8bae1dSRodney W. Grimes * Stuff checksum and output datagram. 498df8bae1dSRodney W. Grimes */ 499df8bae1dSRodney W. Grimes ui->ui_sum = 0; 500df8bae1dSRodney W. Grimes if (udpcksum) { 501df8bae1dSRodney W. Grimes if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 502df8bae1dSRodney W. Grimes ui->ui_sum = 0xffff; 503df8bae1dSRodney W. Grimes } 504df8bae1dSRodney W. Grimes ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 505df8bae1dSRodney W. Grimes ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ 506df8bae1dSRodney W. Grimes ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ 507df8bae1dSRodney W. Grimes udpstat.udps_opackets++; 508df8bae1dSRodney W. Grimes error = ip_output(m, inp->inp_options, &inp->inp_route, 509df8bae1dSRodney W. Grimes inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 510df8bae1dSRodney W. Grimes inp->inp_moptions); 511df8bae1dSRodney W. Grimes 512df8bae1dSRodney W. Grimes if (addr) { 513df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 514df8bae1dSRodney W. Grimes inp->inp_laddr = laddr; 515df8bae1dSRodney W. Grimes splx(s); 516df8bae1dSRodney W. Grimes } 517df8bae1dSRodney W. Grimes return (error); 518df8bae1dSRodney W. Grimes 519df8bae1dSRodney W. Grimes release: 520df8bae1dSRodney W. Grimes m_freem(m); 521df8bae1dSRodney W. Grimes return (error); 522df8bae1dSRodney W. Grimes } 523df8bae1dSRodney W. Grimes 5240312fbe9SPoul-Henning Kamp static u_long udp_sendspace = 9216; /* really max datagram size */ 525df8bae1dSRodney W. Grimes /* 40 1K datagrams */ 5260312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 5270312fbe9SPoul-Henning Kamp &udp_sendspace, 0, ""); 5280312fbe9SPoul-Henning Kamp 5290312fbe9SPoul-Henning Kamp static u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 5300312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 5310312fbe9SPoul-Henning Kamp &udp_recvspace, 0, ""); 532df8bae1dSRodney W. Grimes 533df8bae1dSRodney W. Grimes /*ARGSUSED*/ 534df8bae1dSRodney W. Grimes int 535df8bae1dSRodney W. Grimes udp_usrreq(so, req, m, addr, control) 536df8bae1dSRodney W. Grimes struct socket *so; 537df8bae1dSRodney W. Grimes int req; 538df8bae1dSRodney W. Grimes struct mbuf *m, *addr, *control; 539df8bae1dSRodney W. Grimes { 540df8bae1dSRodney W. Grimes struct inpcb *inp = sotoinpcb(so); 541df8bae1dSRodney W. Grimes int error = 0; 542df8bae1dSRodney W. Grimes int s; 543df8bae1dSRodney W. Grimes 544df8bae1dSRodney W. Grimes if (req == PRU_CONTROL) 5456dfab5b1SGarrett Wollman return (in_control(so, (u_long)m, (caddr_t)addr, 546df8bae1dSRodney W. Grimes (struct ifnet *)control)); 547df8bae1dSRodney W. Grimes if (inp == NULL && req != PRU_ATTACH) { 548df8bae1dSRodney W. Grimes error = EINVAL; 549df8bae1dSRodney W. Grimes goto release; 550df8bae1dSRodney W. Grimes } 551df8bae1dSRodney W. Grimes /* 552df8bae1dSRodney W. Grimes * Note: need to block udp_input while changing 553df8bae1dSRodney W. Grimes * the udp pcb queue and/or pcb addresses. 554df8bae1dSRodney W. Grimes */ 555df8bae1dSRodney W. Grimes switch (req) { 556df8bae1dSRodney W. Grimes 557df8bae1dSRodney W. Grimes case PRU_ATTACH: 558df8bae1dSRodney W. Grimes if (inp != NULL) { 559df8bae1dSRodney W. Grimes error = EINVAL; 560df8bae1dSRodney W. Grimes break; 561df8bae1dSRodney W. Grimes } 562df8bae1dSRodney W. Grimes s = splnet(); 56315bd2b43SDavid Greenman error = in_pcballoc(so, &udbinfo); 564df8bae1dSRodney W. Grimes splx(s); 565df8bae1dSRodney W. Grimes if (error) 566df8bae1dSRodney W. Grimes break; 567df8bae1dSRodney W. Grimes error = soreserve(so, udp_sendspace, udp_recvspace); 568df8bae1dSRodney W. Grimes if (error) 569df8bae1dSRodney W. Grimes break; 570df8bae1dSRodney W. Grimes ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl; 571df8bae1dSRodney W. Grimes break; 572df8bae1dSRodney W. Grimes 573df8bae1dSRodney W. Grimes case PRU_DETACH: 574df8bae1dSRodney W. Grimes udp_detach(inp); 575df8bae1dSRodney W. Grimes break; 576df8bae1dSRodney W. Grimes 577df8bae1dSRodney W. Grimes case PRU_BIND: 578df8bae1dSRodney W. Grimes s = splnet(); 579df8bae1dSRodney W. Grimes error = in_pcbbind(inp, addr); 580df8bae1dSRodney W. Grimes splx(s); 581df8bae1dSRodney W. Grimes break; 582df8bae1dSRodney W. Grimes 583df8bae1dSRodney W. Grimes case PRU_LISTEN: 584df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 585df8bae1dSRodney W. Grimes break; 586df8bae1dSRodney W. Grimes 587df8bae1dSRodney W. Grimes case PRU_CONNECT: 588df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 589df8bae1dSRodney W. Grimes error = EISCONN; 590df8bae1dSRodney W. Grimes break; 591df8bae1dSRodney W. Grimes } 592df8bae1dSRodney W. Grimes s = splnet(); 593df8bae1dSRodney W. Grimes error = in_pcbconnect(inp, addr); 594df8bae1dSRodney W. Grimes splx(s); 595df8bae1dSRodney W. Grimes if (error == 0) 596df8bae1dSRodney W. Grimes soisconnected(so); 597df8bae1dSRodney W. Grimes break; 598df8bae1dSRodney W. Grimes 599df8bae1dSRodney W. Grimes case PRU_CONNECT2: 600df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 601df8bae1dSRodney W. Grimes break; 602df8bae1dSRodney W. Grimes 603df8bae1dSRodney W. Grimes case PRU_ACCEPT: 604df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 605df8bae1dSRodney W. Grimes break; 606df8bae1dSRodney W. Grimes 607df8bae1dSRodney W. Grimes case PRU_DISCONNECT: 608df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr == INADDR_ANY) { 609df8bae1dSRodney W. Grimes error = ENOTCONN; 610df8bae1dSRodney W. Grimes break; 611df8bae1dSRodney W. Grimes } 612df8bae1dSRodney W. Grimes s = splnet(); 613df8bae1dSRodney W. Grimes in_pcbdisconnect(inp); 614df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr = INADDR_ANY; 615df8bae1dSRodney W. Grimes splx(s); 616df8bae1dSRodney W. Grimes so->so_state &= ~SS_ISCONNECTED; /* XXX */ 617df8bae1dSRodney W. Grimes break; 618df8bae1dSRodney W. Grimes 619df8bae1dSRodney W. Grimes case PRU_SHUTDOWN: 620df8bae1dSRodney W. Grimes socantsendmore(so); 621df8bae1dSRodney W. Grimes break; 622df8bae1dSRodney W. Grimes 623df8bae1dSRodney W. Grimes case PRU_SEND: 624df8bae1dSRodney W. Grimes return (udp_output(inp, m, addr, control)); 625df8bae1dSRodney W. Grimes 626df8bae1dSRodney W. Grimes case PRU_ABORT: 627df8bae1dSRodney W. Grimes soisdisconnected(so); 628df8bae1dSRodney W. Grimes udp_detach(inp); 629df8bae1dSRodney W. Grimes break; 630df8bae1dSRodney W. Grimes 631df8bae1dSRodney W. Grimes case PRU_SOCKADDR: 632df8bae1dSRodney W. Grimes in_setsockaddr(inp, addr); 633df8bae1dSRodney W. Grimes break; 634df8bae1dSRodney W. Grimes 635df8bae1dSRodney W. Grimes case PRU_PEERADDR: 636df8bae1dSRodney W. Grimes in_setpeeraddr(inp, addr); 637df8bae1dSRodney W. Grimes break; 638df8bae1dSRodney W. Grimes 639df8bae1dSRodney W. Grimes case PRU_SENSE: 640df8bae1dSRodney W. Grimes /* 641df8bae1dSRodney W. Grimes * stat: don't bother with a blocksize. 642df8bae1dSRodney W. Grimes */ 643df8bae1dSRodney W. Grimes return (0); 644df8bae1dSRodney W. Grimes 645df8bae1dSRodney W. Grimes case PRU_SENDOOB: 646df8bae1dSRodney W. Grimes case PRU_FASTTIMO: 647df8bae1dSRodney W. Grimes case PRU_SLOWTIMO: 648df8bae1dSRodney W. Grimes case PRU_PROTORCV: 649df8bae1dSRodney W. Grimes case PRU_PROTOSEND: 650df8bae1dSRodney W. Grimes error = EOPNOTSUPP; 651df8bae1dSRodney W. Grimes break; 652df8bae1dSRodney W. Grimes 653df8bae1dSRodney W. Grimes case PRU_RCVD: 654df8bae1dSRodney W. Grimes case PRU_RCVOOB: 655df8bae1dSRodney W. Grimes return (EOPNOTSUPP); /* do not free mbuf's */ 656df8bae1dSRodney W. Grimes 657df8bae1dSRodney W. Grimes default: 658df8bae1dSRodney W. Grimes panic("udp_usrreq"); 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes 661df8bae1dSRodney W. Grimes release: 662df8bae1dSRodney W. Grimes if (control) { 663df8bae1dSRodney W. Grimes printf("udp control data unexpectedly retained\n"); 664df8bae1dSRodney W. Grimes m_freem(control); 665df8bae1dSRodney W. Grimes } 666df8bae1dSRodney W. Grimes if (m) 667df8bae1dSRodney W. Grimes m_freem(m); 668df8bae1dSRodney W. Grimes return (error); 669df8bae1dSRodney W. Grimes } 670df8bae1dSRodney W. Grimes 671df8bae1dSRodney W. Grimes static void 672df8bae1dSRodney W. Grimes udp_detach(inp) 673df8bae1dSRodney W. Grimes struct inpcb *inp; 674df8bae1dSRodney W. Grimes { 675df8bae1dSRodney W. Grimes int s = splnet(); 676df8bae1dSRodney W. Grimes 677df8bae1dSRodney W. Grimes in_pcbdetach(inp); 678df8bae1dSRodney W. Grimes splx(s); 679df8bae1dSRodney W. Grimes } 680