1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 34 * $Id: udp_usrreq.c,v 1.43 1998/01/25 17:25:41 steve Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/protosw.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 #include <sys/sysctl.h> 46 #include <sys/syslog.h> 47 48 #include <net/if.h> 49 #include <net/route.h> 50 51 #include <netinet/in.h> 52 #include <netinet/in_systm.h> 53 #include <netinet/ip.h> 54 #include <netinet/in_pcb.h> 55 #include <netinet/in_var.h> 56 #include <netinet/ip_var.h> 57 #include <netinet/ip_icmp.h> 58 #include <netinet/udp.h> 59 #include <netinet/udp_var.h> 60 61 /* 62 * UDP protocol implementation. 63 * Per RFC 768, August, 1980. 64 */ 65 #ifndef COMPAT_42 66 static int udpcksum = 1; 67 #else 68 static int udpcksum = 0; /* XXX */ 69 #endif 70 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 71 &udpcksum, 0, ""); 72 73 static int log_in_vain = 0; 74 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 75 &log_in_vain, 0, ""); 76 77 static struct inpcbhead udb; /* from udp_var.h */ 78 static struct inpcbinfo udbinfo; 79 80 #ifndef UDBHASHSIZE 81 #define UDBHASHSIZE 16 82 #endif 83 84 static struct udpstat udpstat; /* from udp_var.h */ 85 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, 86 &udpstat, udpstat, ""); 87 88 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 89 90 static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *, 91 struct mbuf *, struct proc *)); 92 static void udp_notify __P((struct inpcb *, int)); 93 94 void 95 udp_init() 96 { 97 LIST_INIT(&udb); 98 udbinfo.listhead = &udb; 99 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 100 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.porthashmask); 101 } 102 103 void 104 udp_input(m, iphlen) 105 register struct mbuf *m; 106 int iphlen; 107 { 108 register struct ip *ip; 109 register struct udphdr *uh; 110 register struct inpcb *inp; 111 struct mbuf *opts = 0; 112 int len; 113 struct ip save_ip; 114 115 udpstat.udps_ipackets++; 116 117 /* 118 * Strip IP options, if any; should skip this, 119 * make available to user, and use on returned packets, 120 * but we don't yet have a way to check the checksum 121 * with options still present. 122 */ 123 if (iphlen > sizeof (struct ip)) { 124 ip_stripoptions(m, (struct mbuf *)0); 125 iphlen = sizeof(struct ip); 126 } 127 128 /* 129 * Get IP and UDP header together in first mbuf. 130 */ 131 ip = mtod(m, struct ip *); 132 if (m->m_len < iphlen + sizeof(struct udphdr)) { 133 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 134 udpstat.udps_hdrops++; 135 return; 136 } 137 ip = mtod(m, struct ip *); 138 } 139 uh = (struct udphdr *)((caddr_t)ip + iphlen); 140 141 /* 142 * Make mbuf data length reflect UDP length. 143 * If not enough data to reflect UDP length, drop. 144 */ 145 len = ntohs((u_short)uh->uh_ulen); 146 if (ip->ip_len != len) { 147 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 148 udpstat.udps_badlen++; 149 goto bad; 150 } 151 m_adj(m, len - ip->ip_len); 152 /* ip->ip_len = len; */ 153 } 154 /* 155 * Save a copy of the IP header in case we want restore it 156 * for sending an ICMP error message in response. 157 */ 158 save_ip = *ip; 159 160 /* 161 * Checksum extended UDP header and data. 162 */ 163 if (uh->uh_sum) { 164 ((struct ipovly *)ip)->ih_next = 0; 165 ((struct ipovly *)ip)->ih_prev = 0; 166 ((struct ipovly *)ip)->ih_x1 = 0; 167 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 168 uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 169 if (uh->uh_sum) { 170 udpstat.udps_badsum++; 171 m_freem(m); 172 return; 173 } 174 } 175 176 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 177 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 178 struct inpcb *last; 179 /* 180 * Deliver a multicast or broadcast datagram to *all* sockets 181 * for which the local and remote addresses and ports match 182 * those of the incoming datagram. This allows more than 183 * one process to receive multi/broadcasts on the same port. 184 * (This really ought to be done for unicast datagrams as 185 * well, but that would cause problems with existing 186 * applications that open both address-specific sockets and 187 * a wildcard socket listening to the same port -- they would 188 * end up receiving duplicates of every unicast datagram. 189 * Those applications open the multiple sockets to overcome an 190 * inadequacy of the UDP socket interface, but for backwards 191 * compatibility we avoid the problem here rather than 192 * fixing the interface. Maybe 4.5BSD will remedy this?) 193 */ 194 195 /* 196 * Construct sockaddr format source address. 197 */ 198 udp_in.sin_port = uh->uh_sport; 199 udp_in.sin_addr = ip->ip_src; 200 m->m_len -= sizeof (struct udpiphdr); 201 m->m_data += sizeof (struct udpiphdr); 202 /* 203 * Locate pcb(s) for datagram. 204 * (Algorithm copied from raw_intr().) 205 */ 206 last = NULL; 207 for (inp = udb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 208 if (inp->inp_lport != uh->uh_dport) 209 continue; 210 if (inp->inp_laddr.s_addr != INADDR_ANY) { 211 if (inp->inp_laddr.s_addr != 212 ip->ip_dst.s_addr) 213 continue; 214 } 215 if (inp->inp_faddr.s_addr != INADDR_ANY) { 216 if (inp->inp_faddr.s_addr != 217 ip->ip_src.s_addr || 218 inp->inp_fport != uh->uh_sport) 219 continue; 220 } 221 222 if (last != NULL) { 223 struct mbuf *n; 224 225 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 226 if (last->inp_flags & INP_CONTROLOPTS 227 || last->inp_socket->so_options & SO_TIMESTAMP) 228 ip_savecontrol(last, &opts, ip, n); 229 if (sbappendaddr(&last->inp_socket->so_rcv, 230 (struct sockaddr *)&udp_in, 231 n, opts) == 0) { 232 m_freem(n); 233 if (opts) 234 m_freem(opts); 235 udpstat.udps_fullsock++; 236 } else 237 sorwakeup(last->inp_socket); 238 opts = 0; 239 } 240 } 241 last = inp; 242 /* 243 * Don't look for additional matches if this one does 244 * not have either the SO_REUSEPORT or SO_REUSEADDR 245 * socket options set. This heuristic avoids searching 246 * through all pcbs in the common case of a non-shared 247 * port. It * assumes that an application will never 248 * clear these options after setting them. 249 */ 250 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 251 break; 252 } 253 254 if (last == NULL) { 255 /* 256 * No matching pcb found; discard datagram. 257 * (No need to send an ICMP Port Unreachable 258 * for a broadcast or multicast datgram.) 259 */ 260 udpstat.udps_noportbcast++; 261 goto bad; 262 } 263 if (last->inp_flags & INP_CONTROLOPTS 264 || last->inp_socket->so_options & SO_TIMESTAMP) 265 ip_savecontrol(last, &opts, ip, m); 266 if (sbappendaddr(&last->inp_socket->so_rcv, 267 (struct sockaddr *)&udp_in, 268 m, opts) == 0) { 269 udpstat.udps_fullsock++; 270 goto bad; 271 } 272 sorwakeup(last->inp_socket); 273 return; 274 } 275 /* 276 * Locate pcb for datagram. 277 */ 278 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 279 ip->ip_dst, uh->uh_dport, 1); 280 if (inp == NULL) { 281 if (log_in_vain) { 282 char buf[4*sizeof "123"]; 283 284 strcpy(buf, inet_ntoa(ip->ip_dst)); 285 log(LOG_INFO, 286 "Connection attempt to UDP %s:%d from %s:%d\n", 287 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 288 ntohs(uh->uh_sport)); 289 } 290 udpstat.udps_noport++; 291 if (m->m_flags & (M_BCAST | M_MCAST)) { 292 udpstat.udps_noportbcast++; 293 goto bad; 294 } 295 *ip = save_ip; 296 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 297 return; 298 } 299 300 /* 301 * Construct sockaddr format source address. 302 * Stuff source address and datagram in user buffer. 303 */ 304 udp_in.sin_port = uh->uh_sport; 305 udp_in.sin_addr = ip->ip_src; 306 if (inp->inp_flags & INP_CONTROLOPTS 307 || inp->inp_socket->so_options & SO_TIMESTAMP) 308 ip_savecontrol(inp, &opts, ip, m); 309 iphlen += sizeof(struct udphdr); 310 m->m_len -= iphlen; 311 m->m_pkthdr.len -= iphlen; 312 m->m_data += iphlen; 313 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, 314 m, opts) == 0) { 315 udpstat.udps_fullsock++; 316 goto bad; 317 } 318 sorwakeup(inp->inp_socket); 319 return; 320 bad: 321 m_freem(m); 322 if (opts) 323 m_freem(opts); 324 } 325 326 /* 327 * Notify a udp user of an asynchronous error; 328 * just wake up so that he can collect error status. 329 */ 330 static void 331 udp_notify(inp, errno) 332 register struct inpcb *inp; 333 int errno; 334 { 335 inp->inp_socket->so_error = errno; 336 sorwakeup(inp->inp_socket); 337 sowwakeup(inp->inp_socket); 338 } 339 340 void 341 udp_ctlinput(cmd, sa, vip) 342 int cmd; 343 struct sockaddr *sa; 344 void *vip; 345 { 346 register struct ip *ip = vip; 347 register struct udphdr *uh; 348 349 if (!PRC_IS_REDIRECT(cmd) && 350 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) 351 return; 352 if (ip) { 353 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 354 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, 355 cmd, udp_notify); 356 } else 357 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); 358 } 359 360 static int 361 udp_output(inp, m, addr, control, p) 362 register struct inpcb *inp; 363 register struct mbuf *m; 364 struct sockaddr *addr; 365 struct mbuf *control; 366 struct proc *p; 367 { 368 register struct udpiphdr *ui; 369 register int len = m->m_pkthdr.len; 370 struct in_addr laddr; 371 int s = 0, error = 0; 372 373 if (control) 374 m_freem(control); /* XXX */ 375 376 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 377 error = EMSGSIZE; 378 goto release; 379 } 380 381 if (addr) { 382 laddr = inp->inp_laddr; 383 if (inp->inp_faddr.s_addr != INADDR_ANY) { 384 error = EISCONN; 385 goto release; 386 } 387 /* 388 * Must block input while temporarily connected. 389 */ 390 s = splnet(); 391 error = in_pcbconnect(inp, addr, p); 392 if (error) { 393 splx(s); 394 goto release; 395 } 396 } else { 397 if (inp->inp_faddr.s_addr == INADDR_ANY) { 398 error = ENOTCONN; 399 goto release; 400 } 401 } 402 /* 403 * Calculate data length and get a mbuf 404 * for UDP and IP headers. 405 */ 406 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 407 if (m == 0) { 408 error = ENOBUFS; 409 if (addr) 410 splx(s); 411 goto release; 412 } 413 414 /* 415 * Fill in mbuf with extended UDP header 416 * and addresses and length put into network format. 417 */ 418 ui = mtod(m, struct udpiphdr *); 419 ui->ui_next = ui->ui_prev = 0; 420 ui->ui_x1 = 0; 421 ui->ui_pr = IPPROTO_UDP; 422 ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); 423 ui->ui_src = inp->inp_laddr; 424 ui->ui_dst = inp->inp_faddr; 425 ui->ui_sport = inp->inp_lport; 426 ui->ui_dport = inp->inp_fport; 427 ui->ui_ulen = ui->ui_len; 428 429 /* 430 * Stuff checksum and output datagram. 431 */ 432 ui->ui_sum = 0; 433 if (udpcksum) { 434 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 435 ui->ui_sum = 0xffff; 436 } 437 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 438 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 439 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 440 udpstat.udps_opackets++; 441 error = ip_output(m, inp->inp_options, &inp->inp_route, 442 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 443 inp->inp_moptions); 444 445 if (addr) { 446 in_pcbdisconnect(inp); 447 inp->inp_laddr = laddr; /* XXX rehash? */ 448 splx(s); 449 } 450 return (error); 451 452 release: 453 m_freem(m); 454 return (error); 455 } 456 457 static u_long udp_sendspace = 9216; /* really max datagram size */ 458 /* 40 1K datagrams */ 459 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 460 &udp_sendspace, 0, ""); 461 462 static u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 463 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 464 &udp_recvspace, 0, ""); 465 466 static int 467 udp_abort(struct socket *so) 468 { 469 struct inpcb *inp; 470 int s; 471 472 inp = sotoinpcb(so); 473 if (inp == 0) 474 return EINVAL; /* ??? possible? panic instead? */ 475 soisdisconnected(so); 476 s = splnet(); 477 in_pcbdetach(inp); 478 splx(s); 479 return 0; 480 } 481 482 static int 483 udp_attach(struct socket *so, int proto, struct proc *p) 484 { 485 struct inpcb *inp; 486 int s, error; 487 488 inp = sotoinpcb(so); 489 if (inp != 0) 490 return EINVAL; 491 492 s = splnet(); 493 error = in_pcballoc(so, &udbinfo, p); 494 splx(s); 495 if (error) 496 return error; 497 error = soreserve(so, udp_sendspace, udp_recvspace); 498 if (error) 499 return error; 500 ((struct inpcb *) so->so_pcb)->inp_ip_ttl = ip_defttl; 501 return 0; 502 } 503 504 static int 505 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 506 { 507 struct inpcb *inp; 508 int s, error; 509 510 inp = sotoinpcb(so); 511 if (inp == 0) 512 return EINVAL; 513 s = splnet(); 514 error = in_pcbbind(inp, nam, p); 515 splx(s); 516 return error; 517 } 518 519 static int 520 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 521 { 522 struct inpcb *inp; 523 int s, error; 524 525 inp = sotoinpcb(so); 526 if (inp == 0) 527 return EINVAL; 528 if (inp->inp_faddr.s_addr != INADDR_ANY) 529 return EISCONN; 530 s = splnet(); 531 error = in_pcbconnect(inp, nam, p); 532 splx(s); 533 if (error == 0) 534 soisconnected(so); 535 return error; 536 } 537 538 static int 539 udp_detach(struct socket *so) 540 { 541 struct inpcb *inp; 542 int s; 543 544 inp = sotoinpcb(so); 545 if (inp == 0) 546 return EINVAL; 547 s = splnet(); 548 in_pcbdetach(inp); 549 splx(s); 550 return 0; 551 } 552 553 static int 554 udp_disconnect(struct socket *so) 555 { 556 struct inpcb *inp; 557 int s; 558 559 inp = sotoinpcb(so); 560 if (inp == 0) 561 return EINVAL; 562 if (inp->inp_faddr.s_addr == INADDR_ANY) 563 return ENOTCONN; 564 565 s = splnet(); 566 in_pcbdisconnect(inp); 567 inp->inp_laddr.s_addr = INADDR_ANY; 568 splx(s); 569 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 570 return 0; 571 } 572 573 static int 574 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 575 struct mbuf *control, struct proc *p) 576 { 577 struct inpcb *inp; 578 579 inp = sotoinpcb(so); 580 if (inp == 0) { 581 m_freem(m); 582 return EINVAL; 583 } 584 return udp_output(inp, m, addr, control, p); 585 } 586 587 static int 588 udp_shutdown(struct socket *so) 589 { 590 struct inpcb *inp; 591 592 inp = sotoinpcb(so); 593 if (inp == 0) 594 return EINVAL; 595 socantsendmore(so); 596 return 0; 597 } 598 599 struct pr_usrreqs udp_usrreqs = { 600 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 601 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 602 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 603 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 604 in_setsockaddr, sosend, soreceive, sopoll 605 }; 606