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.27 1996/06/05 17:20:35 wollman Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/queue.h> 39 #include <sys/systm.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/errno.h> 46 #include <sys/stat.h> 47 #include <sys/kernel.h> 48 #include <sys/sysctl.h> 49 #include <sys/syslog.h> 50 51 #include <net/if.h> 52 #include <net/route.h> 53 54 #include <netinet/in.h> 55 #include <netinet/in_systm.h> 56 #include <netinet/ip.h> 57 #include <netinet/in_pcb.h> 58 #include <netinet/in_var.h> 59 #include <netinet/ip_var.h> 60 #include <netinet/ip_icmp.h> 61 #include <netinet/udp.h> 62 #include <netinet/udp_var.h> 63 64 /* 65 * UDP protocol implementation. 66 * Per RFC 768, August, 1980. 67 */ 68 #ifndef COMPAT_42 69 static int udpcksum = 1; 70 #else 71 static int udpcksum = 0; /* XXX */ 72 #endif 73 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 74 &udpcksum, 0, ""); 75 76 static int log_in_vain = 0; 77 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 78 &log_in_vain, 0, ""); 79 80 static struct inpcbhead udb; /* from udp_var.h */ 81 static struct inpcbinfo udbinfo; 82 83 #ifndef UDBHASHSIZE 84 #define UDBHASHSIZE 64 85 #endif 86 87 static struct udpstat udpstat; /* from udp_var.h */ 88 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, 89 &udpstat, udpstat, ""); 90 91 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 92 93 static void udp_detach __P((struct inpcb *)); 94 static int udp_output __P((struct inpcb *, struct mbuf *, struct mbuf *, 95 struct mbuf *)); 96 static void udp_notify __P((struct inpcb *, int)); 97 static struct mbuf *udp_saveopt __P((caddr_t, int, int)); 98 static struct mbuf *udp_timestamp __P((void)); 99 100 void 101 udp_init() 102 { 103 LIST_INIT(&udb); 104 udbinfo.listhead = &udb; 105 udbinfo.hashbase = phashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashsize); 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 ((struct ipovly *)ip)->ih_next = 0; 170 ((struct ipovly *)ip)->ih_prev = 0; 171 ((struct ipovly *)ip)->ih_x1 = 0; 172 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 173 uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 174 if (uh->uh_sum) { 175 udpstat.udps_badsum++; 176 m_freem(m); 177 return; 178 } 179 } 180 181 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 182 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 183 struct socket *last; 184 /* 185 * Deliver a multicast or broadcast datagram to *all* sockets 186 * for which the local and remote addresses and ports match 187 * those of the incoming datagram. This allows more than 188 * one process to receive multi/broadcasts on the same port. 189 * (This really ought to be done for unicast datagrams as 190 * well, but that would cause problems with existing 191 * applications that open both address-specific sockets and 192 * a wildcard socket listening to the same port -- they would 193 * end up receiving duplicates of every unicast datagram. 194 * Those applications open the multiple sockets to overcome an 195 * inadequacy of the UDP socket interface, but for backwards 196 * compatibility we avoid the problem here rather than 197 * fixing the interface. Maybe 4.5BSD will remedy this?) 198 */ 199 200 /* 201 * Construct sockaddr format source address. 202 */ 203 udp_in.sin_port = uh->uh_sport; 204 udp_in.sin_addr = ip->ip_src; 205 m->m_len -= sizeof (struct udpiphdr); 206 m->m_data += sizeof (struct udpiphdr); 207 /* 208 * Locate pcb(s) for datagram. 209 * (Algorithm copied from raw_intr().) 210 */ 211 last = NULL; 212 for (inp = udb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 213 if (inp->inp_lport != uh->uh_dport) 214 continue; 215 if (inp->inp_laddr.s_addr != INADDR_ANY) { 216 if (inp->inp_laddr.s_addr != 217 ip->ip_dst.s_addr) 218 continue; 219 } 220 if (inp->inp_faddr.s_addr != INADDR_ANY) { 221 if (inp->inp_faddr.s_addr != 222 ip->ip_src.s_addr || 223 inp->inp_fport != uh->uh_sport) 224 continue; 225 } 226 227 if (last != NULL) { 228 struct mbuf *n; 229 230 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 231 if (sbappendaddr(&last->so_rcv, 232 (struct sockaddr *)&udp_in, 233 n, (struct mbuf *)0) == 0) { 234 m_freem(n); 235 udpstat.udps_fullsock++; 236 } else 237 sorwakeup(last); 238 } 239 } 240 last = inp->inp_socket; 241 /* 242 * Don't look for additional matches if this one does 243 * not have either the SO_REUSEPORT or SO_REUSEADDR 244 * socket options set. This heuristic avoids searching 245 * through all pcbs in the common case of a non-shared 246 * port. It * assumes that an application will never 247 * clear these options after setting them. 248 */ 249 if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR) == 0)) 250 break; 251 } 252 253 if (last == NULL) { 254 /* 255 * No matching pcb found; discard datagram. 256 * (No need to send an ICMP Port Unreachable 257 * for a broadcast or multicast datgram.) 258 */ 259 udpstat.udps_noportbcast++; 260 goto bad; 261 } 262 if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&udp_in, 263 m, (struct mbuf *)0) == 0) { 264 udpstat.udps_fullsock++; 265 goto bad; 266 } 267 sorwakeup(last); 268 return; 269 } 270 /* 271 * Locate pcb for datagram. First look for an exact match. 272 */ 273 inp = in_pcblookuphash(&udbinfo, ip->ip_src, uh->uh_sport, 274 ip->ip_dst, uh->uh_dport); 275 /* 276 * ...and if that fails, do a wildcard search. 277 */ 278 if (inp == NULL) { 279 udpstat.udpps_pcbhashmiss++; 280 inp = in_pcblookup(&udb, ip->ip_src, uh->uh_sport, ip->ip_dst, 281 uh->uh_dport, INPLOOKUP_WILDCARD); 282 } 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, "Connection attempt to UDP %s:%d" 289 " from %s:%d\n", 290 buf, ntohs(uh->uh_dport), 291 inet_ntoa(ip->ip_src), 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 struct mbuf **mp = &opts; 312 313 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 314 if (*mp = udp_timestamp()) 315 mp = &(*mp)->m_next; 316 } 317 if (inp->inp_flags & INP_RECVDSTADDR) { 318 *mp = udp_saveopt((caddr_t) &ip->ip_dst, 319 sizeof(struct in_addr), IP_RECVDSTADDR); 320 if (*mp) 321 mp = &(*mp)->m_next; 322 } 323 #ifdef notyet 324 /* options were tossed above */ 325 if (inp->inp_flags & INP_RECVOPTS) { 326 *mp = udp_saveopt((caddr_t) opts_deleted_above, 327 sizeof(struct in_addr), IP_RECVOPTS); 328 if (*mp) 329 mp = &(*mp)->m_next; 330 } 331 /* ip_srcroute doesn't do what we want here, need to fix */ 332 if (inp->inp_flags & INP_RECVRETOPTS) { 333 *mp = udp_saveopt((caddr_t) ip_srcroute(), 334 sizeof(struct in_addr), IP_RECVRETOPTS); 335 if (*mp) 336 mp = &(*mp)->m_next; 337 } 338 #endif 339 } 340 iphlen += sizeof(struct udphdr); 341 m->m_len -= iphlen; 342 m->m_pkthdr.len -= iphlen; 343 m->m_data += iphlen; 344 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, 345 m, opts) == 0) { 346 udpstat.udps_fullsock++; 347 goto bad; 348 } 349 sorwakeup(inp->inp_socket); 350 return; 351 bad: 352 m_freem(m); 353 if (opts) 354 m_freem(opts); 355 } 356 357 /* 358 * Create a "control" mbuf containing the specified data 359 * with the specified type for presentation with a datagram. 360 */ 361 struct mbuf * 362 udp_saveopt(p, size, type) 363 caddr_t p; 364 register int size; 365 int type; 366 { 367 register struct cmsghdr *cp; 368 struct mbuf *m; 369 370 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 371 return ((struct mbuf *) NULL); 372 cp = (struct cmsghdr *) mtod(m, struct cmsghdr *); 373 bcopy(p, CMSG_DATA(cp), size); 374 size += sizeof(*cp); 375 m->m_len = size; 376 cp->cmsg_len = size; 377 cp->cmsg_level = IPPROTO_IP; 378 cp->cmsg_type = type; 379 return (m); 380 } 381 382 /* 383 * Create an mbuf with the SCM_TIMESTAMP socket option data (struct timeval) 384 * inside. This really isn't UDP specific; but there's not really a better 385 * place for it yet.. 386 */ 387 static struct mbuf * 388 udp_timestamp() 389 { 390 register struct cmsghdr *cp; 391 struct mbuf *m; 392 struct timeval tv; 393 394 MGET(m, M_DONTWAIT, MT_CONTROL); 395 if (m == 0) 396 return (struct mbuf *) 0; 397 398 microtime(&tv); 399 cp = (struct cmsghdr *) mtod(m, struct cmsghdr *); 400 cp->cmsg_len = 401 m->m_len = sizeof(*cp) + sizeof(struct timeval); 402 cp->cmsg_level = SOL_SOCKET; 403 cp->cmsg_type = SCM_TIMESTAMP; 404 (void) memcpy(CMSG_DATA(cp), &tv, sizeof(struct timeval)); 405 return (m); 406 } 407 408 /* 409 * Notify a udp user of an asynchronous error; 410 * just wake up so that he can collect error status. 411 */ 412 static void 413 udp_notify(inp, errno) 414 register struct inpcb *inp; 415 int errno; 416 { 417 inp->inp_socket->so_error = errno; 418 sorwakeup(inp->inp_socket); 419 sowwakeup(inp->inp_socket); 420 } 421 422 void 423 udp_ctlinput(cmd, sa, vip) 424 int cmd; 425 struct sockaddr *sa; 426 void *vip; 427 { 428 register struct ip *ip = vip; 429 register struct udphdr *uh; 430 431 if (!PRC_IS_REDIRECT(cmd) && 432 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) 433 return; 434 if (ip) { 435 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 436 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, 437 cmd, udp_notify); 438 } else 439 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); 440 } 441 442 static int 443 udp_output(inp, m, addr, control) 444 register struct inpcb *inp; 445 register struct mbuf *m; 446 struct mbuf *addr, *control; 447 { 448 register struct udpiphdr *ui; 449 register int len = m->m_pkthdr.len; 450 struct in_addr laddr; 451 int s = 0, error = 0; 452 453 if (control) 454 m_freem(control); /* XXX */ 455 456 if (addr) { 457 laddr = inp->inp_laddr; 458 if (inp->inp_faddr.s_addr != INADDR_ANY) { 459 error = EISCONN; 460 goto release; 461 } 462 /* 463 * Must block input while temporarily connected. 464 */ 465 s = splnet(); 466 error = in_pcbconnect(inp, addr); 467 if (error) { 468 splx(s); 469 goto release; 470 } 471 } else { 472 if (inp->inp_faddr.s_addr == INADDR_ANY) { 473 error = ENOTCONN; 474 goto release; 475 } 476 } 477 /* 478 * Calculate data length and get a mbuf 479 * for UDP and IP headers. 480 */ 481 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 482 if (m == 0) { 483 error = ENOBUFS; 484 if (addr) 485 splx(s); 486 goto release; 487 } 488 489 /* 490 * Fill in mbuf with extended UDP header 491 * and addresses and length put into network format. 492 */ 493 ui = mtod(m, struct udpiphdr *); 494 ui->ui_next = ui->ui_prev = 0; 495 ui->ui_x1 = 0; 496 ui->ui_pr = IPPROTO_UDP; 497 ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); 498 ui->ui_src = inp->inp_laddr; 499 ui->ui_dst = inp->inp_faddr; 500 ui->ui_sport = inp->inp_lport; 501 ui->ui_dport = inp->inp_fport; 502 ui->ui_ulen = ui->ui_len; 503 504 /* 505 * Stuff checksum and output datagram. 506 */ 507 ui->ui_sum = 0; 508 if (udpcksum) { 509 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 510 ui->ui_sum = 0xffff; 511 } 512 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 513 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ 514 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ 515 udpstat.udps_opackets++; 516 error = ip_output(m, inp->inp_options, &inp->inp_route, 517 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 518 inp->inp_moptions); 519 520 if (addr) { 521 in_pcbdisconnect(inp); 522 inp->inp_laddr = laddr; 523 splx(s); 524 } 525 return (error); 526 527 release: 528 m_freem(m); 529 return (error); 530 } 531 532 static u_long udp_sendspace = 9216; /* really max datagram size */ 533 /* 40 1K datagrams */ 534 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 535 &udp_sendspace, 0, ""); 536 537 static u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 538 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 539 &udp_recvspace, 0, ""); 540 541 /*ARGSUSED*/ 542 int 543 udp_usrreq(so, req, m, addr, control) 544 struct socket *so; 545 int req; 546 struct mbuf *m, *addr, *control; 547 { 548 struct inpcb *inp = sotoinpcb(so); 549 int error = 0; 550 int s; 551 552 if (req == PRU_CONTROL) 553 return (in_control(so, (u_long)m, (caddr_t)addr, 554 (struct ifnet *)control)); 555 if (inp == NULL && req != PRU_ATTACH) { 556 error = EINVAL; 557 goto release; 558 } 559 /* 560 * Note: need to block udp_input while changing 561 * the udp pcb queue and/or pcb addresses. 562 */ 563 switch (req) { 564 565 case PRU_ATTACH: 566 if (inp != NULL) { 567 error = EINVAL; 568 break; 569 } 570 s = splnet(); 571 error = in_pcballoc(so, &udbinfo); 572 splx(s); 573 if (error) 574 break; 575 error = soreserve(so, udp_sendspace, udp_recvspace); 576 if (error) 577 break; 578 ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl; 579 break; 580 581 case PRU_DETACH: 582 udp_detach(inp); 583 break; 584 585 case PRU_BIND: 586 s = splnet(); 587 error = in_pcbbind(inp, addr); 588 splx(s); 589 break; 590 591 case PRU_LISTEN: 592 error = EOPNOTSUPP; 593 break; 594 595 case PRU_CONNECT: 596 if (inp->inp_faddr.s_addr != INADDR_ANY) { 597 error = EISCONN; 598 break; 599 } 600 s = splnet(); 601 error = in_pcbconnect(inp, addr); 602 splx(s); 603 if (error == 0) 604 soisconnected(so); 605 break; 606 607 case PRU_CONNECT2: 608 error = EOPNOTSUPP; 609 break; 610 611 case PRU_ACCEPT: 612 error = EOPNOTSUPP; 613 break; 614 615 case PRU_DISCONNECT: 616 if (inp->inp_faddr.s_addr == INADDR_ANY) { 617 error = ENOTCONN; 618 break; 619 } 620 s = splnet(); 621 in_pcbdisconnect(inp); 622 inp->inp_laddr.s_addr = INADDR_ANY; 623 splx(s); 624 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 625 break; 626 627 case PRU_SHUTDOWN: 628 socantsendmore(so); 629 break; 630 631 case PRU_SEND: 632 return (udp_output(inp, m, addr, control)); 633 634 case PRU_ABORT: 635 soisdisconnected(so); 636 udp_detach(inp); 637 break; 638 639 case PRU_SOCKADDR: 640 in_setsockaddr(inp, addr); 641 break; 642 643 case PRU_PEERADDR: 644 in_setpeeraddr(inp, addr); 645 break; 646 647 case PRU_SENSE: 648 /* 649 * stat: don't bother with a blocksize. 650 */ 651 return (0); 652 653 case PRU_SENDOOB: 654 case PRU_FASTTIMO: 655 case PRU_SLOWTIMO: 656 case PRU_PROTORCV: 657 case PRU_PROTOSEND: 658 error = EOPNOTSUPP; 659 break; 660 661 case PRU_RCVD: 662 case PRU_RCVOOB: 663 return (EOPNOTSUPP); /* do not free mbuf's */ 664 665 default: 666 panic("udp_usrreq"); 667 } 668 669 release: 670 if (control) { 671 printf("udp control data unexpectedly retained\n"); 672 m_freem(control); 673 } 674 if (m) 675 m_freem(m); 676 return (error); 677 } 678 679 static void 680 udp_detach(inp) 681 struct inpcb *inp; 682 { 683 int s = splnet(); 684 685 in_pcbdetach(inp); 686 splx(s); 687 } 688