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.47 1998/05/15 20:11:35 wollman 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 <vm/vm_zone.h> 49 50 #include <net/if.h> 51 #include <net/route.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 #include <netinet/in_pcb.h> 57 #include <netinet/in_var.h> 58 #include <netinet/ip_var.h> 59 #include <netinet/ip_icmp.h> 60 #include <netinet/udp.h> 61 #include <netinet/udp_var.h> 62 63 /* 64 * UDP protocol implementation. 65 * Per RFC 768, August, 1980. 66 */ 67 #ifndef COMPAT_42 68 static int udpcksum = 1; 69 #else 70 static int udpcksum = 0; /* XXX */ 71 #endif 72 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 73 &udpcksum, 0, ""); 74 75 static int log_in_vain = 0; 76 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 77 &log_in_vain, 0, ""); 78 79 static struct inpcbhead udb; /* from udp_var.h */ 80 static struct inpcbinfo udbinfo; 81 82 #ifndef UDBHASHSIZE 83 #define UDBHASHSIZE 16 84 #endif 85 86 static struct udpstat udpstat; /* from udp_var.h */ 87 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, 88 &udpstat, udpstat, ""); 89 90 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 91 92 static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *, 93 struct mbuf *, struct proc *)); 94 static void udp_notify __P((struct inpcb *, int)); 95 96 void 97 udp_init() 98 { 99 LIST_INIT(&udb); 100 udbinfo.listhead = &udb; 101 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 102 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 103 &udbinfo.porthashmask); 104 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets, 105 ZONE_INTERRUPT, 0); 106 } 107 108 void 109 udp_input(m, iphlen) 110 register struct mbuf *m; 111 int iphlen; 112 { 113 register struct ip *ip; 114 register struct udphdr *uh; 115 register struct inpcb *inp; 116 struct mbuf *opts = 0; 117 int len; 118 struct ip save_ip; 119 120 udpstat.udps_ipackets++; 121 122 /* 123 * Strip IP options, if any; should skip this, 124 * make available to user, and use on returned packets, 125 * but we don't yet have a way to check the checksum 126 * with options still present. 127 */ 128 if (iphlen > sizeof (struct ip)) { 129 ip_stripoptions(m, (struct mbuf *)0); 130 iphlen = sizeof(struct ip); 131 } 132 133 /* 134 * Get IP and UDP header together in first mbuf. 135 */ 136 ip = mtod(m, struct ip *); 137 if (m->m_len < iphlen + sizeof(struct udphdr)) { 138 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 139 udpstat.udps_hdrops++; 140 return; 141 } 142 ip = mtod(m, struct ip *); 143 } 144 uh = (struct udphdr *)((caddr_t)ip + iphlen); 145 146 /* 147 * Make mbuf data length reflect UDP length. 148 * If not enough data to reflect UDP length, drop. 149 */ 150 len = ntohs((u_short)uh->uh_ulen); 151 if (ip->ip_len != len) { 152 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 153 udpstat.udps_badlen++; 154 goto bad; 155 } 156 m_adj(m, len - ip->ip_len); 157 /* ip->ip_len = len; */ 158 } 159 /* 160 * Save a copy of the IP header in case we want restore it 161 * for sending an ICMP error message in response. 162 */ 163 save_ip = *ip; 164 165 /* 166 * Checksum extended UDP header and data. 167 */ 168 if (uh->uh_sum) { 169 bzero(((struct ipovly *)ip)->ih_x1, 9); 170 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 171 uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 172 if (uh->uh_sum) { 173 udpstat.udps_badsum++; 174 m_freem(m); 175 return; 176 } 177 } 178 179 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 180 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 181 struct inpcb *last; 182 /* 183 * Deliver a multicast or broadcast datagram to *all* sockets 184 * for which the local and remote addresses and ports match 185 * those of the incoming datagram. This allows more than 186 * one process to receive multi/broadcasts on the same port. 187 * (This really ought to be done for unicast datagrams as 188 * well, but that would cause problems with existing 189 * applications that open both address-specific sockets and 190 * a wildcard socket listening to the same port -- they would 191 * end up receiving duplicates of every unicast datagram. 192 * Those applications open the multiple sockets to overcome an 193 * inadequacy of the UDP socket interface, but for backwards 194 * compatibility we avoid the problem here rather than 195 * fixing the interface. Maybe 4.5BSD will remedy this?) 196 */ 197 198 /* 199 * Construct sockaddr format source address. 200 */ 201 udp_in.sin_port = uh->uh_sport; 202 udp_in.sin_addr = ip->ip_src; 203 m->m_len -= sizeof (struct udpiphdr); 204 m->m_data += sizeof (struct udpiphdr); 205 /* 206 * Locate pcb(s) for datagram. 207 * (Algorithm copied from raw_intr().) 208 */ 209 last = NULL; 210 for (inp = udb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 211 if (inp->inp_lport != uh->uh_dport) 212 continue; 213 if (inp->inp_laddr.s_addr != INADDR_ANY) { 214 if (inp->inp_laddr.s_addr != 215 ip->ip_dst.s_addr) 216 continue; 217 } 218 if (inp->inp_faddr.s_addr != INADDR_ANY) { 219 if (inp->inp_faddr.s_addr != 220 ip->ip_src.s_addr || 221 inp->inp_fport != uh->uh_sport) 222 continue; 223 } 224 225 if (last != NULL) { 226 struct mbuf *n; 227 228 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 229 if (last->inp_flags & INP_CONTROLOPTS 230 || last->inp_socket->so_options & SO_TIMESTAMP) 231 ip_savecontrol(last, &opts, ip, n); 232 if (sbappendaddr(&last->inp_socket->so_rcv, 233 (struct sockaddr *)&udp_in, 234 n, opts) == 0) { 235 m_freem(n); 236 if (opts) 237 m_freem(opts); 238 udpstat.udps_fullsock++; 239 } else 240 sorwakeup(last->inp_socket); 241 opts = 0; 242 } 243 } 244 last = inp; 245 /* 246 * Don't look for additional matches if this one does 247 * not have either the SO_REUSEPORT or SO_REUSEADDR 248 * socket options set. This heuristic avoids searching 249 * through all pcbs in the common case of a non-shared 250 * port. It * assumes that an application will never 251 * clear these options after setting them. 252 */ 253 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 254 break; 255 } 256 257 if (last == NULL) { 258 /* 259 * No matching pcb found; discard datagram. 260 * (No need to send an ICMP Port Unreachable 261 * for a broadcast or multicast datgram.) 262 */ 263 udpstat.udps_noportbcast++; 264 goto bad; 265 } 266 if (last->inp_flags & INP_CONTROLOPTS 267 || last->inp_socket->so_options & SO_TIMESTAMP) 268 ip_savecontrol(last, &opts, ip, m); 269 if (sbappendaddr(&last->inp_socket->so_rcv, 270 (struct sockaddr *)&udp_in, 271 m, opts) == 0) { 272 udpstat.udps_fullsock++; 273 goto bad; 274 } 275 sorwakeup(last->inp_socket); 276 return; 277 } 278 /* 279 * Locate pcb for datagram. 280 */ 281 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 282 ip->ip_dst, uh->uh_dport, 1); 283 if (inp == NULL) { 284 if (log_in_vain) { 285 char buf[4*sizeof "123"]; 286 287 strcpy(buf, inet_ntoa(ip->ip_dst)); 288 log(LOG_INFO, 289 "Connection attempt to UDP %s:%d from %s:%d\n", 290 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 291 ntohs(uh->uh_sport)); 292 } 293 udpstat.udps_noport++; 294 if (m->m_flags & (M_BCAST | M_MCAST)) { 295 udpstat.udps_noportbcast++; 296 goto bad; 297 } 298 *ip = save_ip; 299 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 300 return; 301 } 302 303 /* 304 * Construct sockaddr format source address. 305 * Stuff source address and datagram in user buffer. 306 */ 307 udp_in.sin_port = uh->uh_sport; 308 udp_in.sin_addr = ip->ip_src; 309 if (inp->inp_flags & INP_CONTROLOPTS 310 || inp->inp_socket->so_options & SO_TIMESTAMP) 311 ip_savecontrol(inp, &opts, ip, m); 312 iphlen += sizeof(struct udphdr); 313 m->m_len -= iphlen; 314 m->m_pkthdr.len -= iphlen; 315 m->m_data += iphlen; 316 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, 317 m, opts) == 0) { 318 udpstat.udps_fullsock++; 319 goto bad; 320 } 321 sorwakeup(inp->inp_socket); 322 return; 323 bad: 324 m_freem(m); 325 if (opts) 326 m_freem(opts); 327 } 328 329 /* 330 * Notify a udp user of an asynchronous error; 331 * just wake up so that he can collect error status. 332 */ 333 static void 334 udp_notify(inp, errno) 335 register struct inpcb *inp; 336 int errno; 337 { 338 inp->inp_socket->so_error = errno; 339 sorwakeup(inp->inp_socket); 340 sowwakeup(inp->inp_socket); 341 } 342 343 void 344 udp_ctlinput(cmd, sa, vip) 345 int cmd; 346 struct sockaddr *sa; 347 void *vip; 348 { 349 register struct ip *ip = vip; 350 register struct udphdr *uh; 351 352 if (!PRC_IS_REDIRECT(cmd) && 353 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) 354 return; 355 if (ip) { 356 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 357 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, 358 cmd, udp_notify); 359 } else 360 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); 361 } 362 363 static int 364 udp_pcblist SYSCTL_HANDLER_ARGS 365 { 366 int error, i, n, s; 367 struct inpcb *inp, **inp_list; 368 inp_gen_t gencnt; 369 struct xinpgen xig; 370 371 /* 372 * The process of preparing the TCB list is too time-consuming and 373 * resource-intensive to repeat twice on every request. 374 */ 375 if (req->oldptr == 0) { 376 n = udbinfo.ipi_count; 377 req->oldidx = 2 * (sizeof xig) 378 + (n + n/8) * sizeof(struct xinpcb); 379 return 0; 380 } 381 382 if (req->newptr != 0) 383 return EPERM; 384 385 /* 386 * OK, now we're committed to doing something. 387 */ 388 s = splnet(); 389 gencnt = udbinfo.ipi_gencnt; 390 n = udbinfo.ipi_count; 391 splx(s); 392 393 xig.xig_len = sizeof xig; 394 xig.xig_count = n; 395 xig.xig_gen = gencnt; 396 xig.xig_sogen = so_gencnt; 397 error = SYSCTL_OUT(req, &xig, sizeof xig); 398 if (error) 399 return error; 400 401 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 402 if (inp_list == 0) 403 return ENOMEM; 404 405 s = splnet(); 406 for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n; 407 inp = inp->inp_list.le_next) { 408 if (inp->inp_gencnt <= gencnt) 409 inp_list[i++] = inp; 410 } 411 splx(s); 412 n = i; 413 414 error = 0; 415 for (i = 0; i < n; i++) { 416 inp = inp_list[i]; 417 if (inp->inp_gencnt <= gencnt) { 418 struct xinpcb xi; 419 xi.xi_len = sizeof xi; 420 /* XXX should avoid extra copy */ 421 bcopy(inp, &xi.xi_inp, sizeof *inp); 422 if (inp->inp_socket) 423 sotoxsocket(inp->inp_socket, &xi.xi_socket); 424 error = SYSCTL_OUT(req, &xi, sizeof xi); 425 } 426 } 427 if (!error) { 428 /* 429 * Give the user an updated idea of our state. 430 * If the generation differs from what we told 431 * her before, she knows that something happened 432 * while we were processing this request, and it 433 * might be necessary to retry. 434 */ 435 s = splnet(); 436 xig.xig_gen = udbinfo.ipi_gencnt; 437 xig.xig_sogen = so_gencnt; 438 xig.xig_count = udbinfo.ipi_count; 439 splx(s); 440 error = SYSCTL_OUT(req, &xig, sizeof xig); 441 } 442 free(inp_list, M_TEMP); 443 return error; 444 } 445 446 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 447 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 448 449 static int 450 udp_output(inp, m, addr, control, p) 451 register struct inpcb *inp; 452 register struct mbuf *m; 453 struct sockaddr *addr; 454 struct mbuf *control; 455 struct proc *p; 456 { 457 register struct udpiphdr *ui; 458 register int len = m->m_pkthdr.len; 459 struct in_addr laddr; 460 int s = 0, error = 0; 461 462 if (control) 463 m_freem(control); /* XXX */ 464 465 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 466 error = EMSGSIZE; 467 goto release; 468 } 469 470 if (addr) { 471 laddr = inp->inp_laddr; 472 if (inp->inp_faddr.s_addr != INADDR_ANY) { 473 error = EISCONN; 474 goto release; 475 } 476 /* 477 * Must block input while temporarily connected. 478 */ 479 s = splnet(); 480 error = in_pcbconnect(inp, addr, p); 481 if (error) { 482 splx(s); 483 goto release; 484 } 485 } else { 486 if (inp->inp_faddr.s_addr == INADDR_ANY) { 487 error = ENOTCONN; 488 goto release; 489 } 490 } 491 /* 492 * Calculate data length and get a mbuf 493 * for UDP and IP headers. 494 */ 495 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 496 if (m == 0) { 497 error = ENOBUFS; 498 if (addr) 499 splx(s); 500 goto release; 501 } 502 503 /* 504 * Fill in mbuf with extended UDP header 505 * and addresses and length put into network format. 506 */ 507 ui = mtod(m, struct udpiphdr *); 508 bzero(ui->ui_x1, sizeof(ui->ui_x1)); 509 ui->ui_pr = IPPROTO_UDP; 510 ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); 511 ui->ui_src = inp->inp_laddr; 512 ui->ui_dst = inp->inp_faddr; 513 ui->ui_sport = inp->inp_lport; 514 ui->ui_dport = inp->inp_fport; 515 ui->ui_ulen = ui->ui_len; 516 517 /* 518 * Stuff checksum and output datagram. 519 */ 520 ui->ui_sum = 0; 521 if (udpcksum) { 522 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 523 ui->ui_sum = 0xffff; 524 } 525 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 526 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 527 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 528 udpstat.udps_opackets++; 529 error = ip_output(m, inp->inp_options, &inp->inp_route, 530 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 531 inp->inp_moptions); 532 533 if (addr) { 534 in_pcbdisconnect(inp); 535 inp->inp_laddr = laddr; /* XXX rehash? */ 536 splx(s); 537 } 538 return (error); 539 540 release: 541 m_freem(m); 542 return (error); 543 } 544 545 static u_long udp_sendspace = 9216; /* really max datagram size */ 546 /* 40 1K datagrams */ 547 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 548 &udp_sendspace, 0, ""); 549 550 static u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 551 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 552 &udp_recvspace, 0, ""); 553 554 static int 555 udp_abort(struct socket *so) 556 { 557 struct inpcb *inp; 558 int s; 559 560 inp = sotoinpcb(so); 561 if (inp == 0) 562 return EINVAL; /* ??? possible? panic instead? */ 563 soisdisconnected(so); 564 s = splnet(); 565 in_pcbdetach(inp); 566 splx(s); 567 return 0; 568 } 569 570 static int 571 udp_attach(struct socket *so, int proto, struct proc *p) 572 { 573 struct inpcb *inp; 574 int s, error; 575 576 inp = sotoinpcb(so); 577 if (inp != 0) 578 return EINVAL; 579 580 s = splnet(); 581 error = in_pcballoc(so, &udbinfo, p); 582 splx(s); 583 if (error) 584 return error; 585 error = soreserve(so, udp_sendspace, udp_recvspace); 586 if (error) 587 return error; 588 ((struct inpcb *) so->so_pcb)->inp_ip_ttl = ip_defttl; 589 return 0; 590 } 591 592 static int 593 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 594 { 595 struct inpcb *inp; 596 int s, error; 597 598 inp = sotoinpcb(so); 599 if (inp == 0) 600 return EINVAL; 601 s = splnet(); 602 error = in_pcbbind(inp, nam, p); 603 splx(s); 604 return error; 605 } 606 607 static int 608 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 609 { 610 struct inpcb *inp; 611 int s, error; 612 613 inp = sotoinpcb(so); 614 if (inp == 0) 615 return EINVAL; 616 if (inp->inp_faddr.s_addr != INADDR_ANY) 617 return EISCONN; 618 s = splnet(); 619 error = in_pcbconnect(inp, nam, p); 620 splx(s); 621 if (error == 0) 622 soisconnected(so); 623 return error; 624 } 625 626 static int 627 udp_detach(struct socket *so) 628 { 629 struct inpcb *inp; 630 int s; 631 632 inp = sotoinpcb(so); 633 if (inp == 0) 634 return EINVAL; 635 s = splnet(); 636 in_pcbdetach(inp); 637 splx(s); 638 return 0; 639 } 640 641 static int 642 udp_disconnect(struct socket *so) 643 { 644 struct inpcb *inp; 645 int s; 646 647 inp = sotoinpcb(so); 648 if (inp == 0) 649 return EINVAL; 650 if (inp->inp_faddr.s_addr == INADDR_ANY) 651 return ENOTCONN; 652 653 s = splnet(); 654 in_pcbdisconnect(inp); 655 inp->inp_laddr.s_addr = INADDR_ANY; 656 splx(s); 657 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 658 return 0; 659 } 660 661 static int 662 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 663 struct mbuf *control, struct proc *p) 664 { 665 struct inpcb *inp; 666 667 inp = sotoinpcb(so); 668 if (inp == 0) { 669 m_freem(m); 670 return EINVAL; 671 } 672 return udp_output(inp, m, addr, control, p); 673 } 674 675 static int 676 udp_shutdown(struct socket *so) 677 { 678 struct inpcb *inp; 679 680 inp = sotoinpcb(so); 681 if (inp == 0) 682 return EINVAL; 683 socantsendmore(so); 684 return 0; 685 } 686 687 struct pr_usrreqs udp_usrreqs = { 688 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 689 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 690 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 691 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 692 in_setsockaddr, sosend, soreceive, sopoll 693 }; 694