1 /* $OpenBSD: udp_usrreq.c,v 1.7 1996/07/05 20:42:18 deraadt Exp $ */ 2 /* $NetBSD: udp_usrreq.c,v 1.28 1996/03/16 23:54:03 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94 37 */ 38 39 #include <sys/param.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/systm.h> 48 #include <sys/proc.h> 49 50 #include <vm/vm.h> 51 #include <sys/sysctl.h> 52 53 #include <net/if.h> 54 #include <net/route.h> 55 56 #include <netinet/in.h> 57 #include <netinet/in_systm.h> 58 #include <netinet/in_var.h> 59 #include <netinet/ip.h> 60 #include <netinet/in_pcb.h> 61 #include <netinet/ip_var.h> 62 #include <netinet/ip_icmp.h> 63 #include <netinet/udp.h> 64 #include <netinet/udp_var.h> 65 66 #include <machine/stdarg.h> 67 68 /* 69 * UDP protocol implementation. 70 * Per RFC 768, August, 1980. 71 */ 72 int udpcksum = 1; 73 74 struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 75 76 static void udp_detach __P((struct inpcb *)); 77 static void udp_notify __P((struct inpcb *, int)); 78 static struct mbuf *udp_saveopt __P((caddr_t, int, int)); 79 80 #ifndef UDBHASHSIZE 81 #define UDBHASHSIZE 128 82 #endif 83 int udbhashsize = UDBHASHSIZE; 84 85 void 86 udp_init() 87 { 88 89 in_pcbinit(&udbtable, udbhashsize); 90 } 91 92 void 93 #if __STDC__ 94 udp_input(struct mbuf *m, ...) 95 #else 96 udp_input(m, va_alist) 97 struct mbuf *m; 98 va_dcl 99 #endif 100 { 101 register struct ip *ip; 102 register struct udphdr *uh; 103 register struct inpcb *inp; 104 struct mbuf *opts = 0; 105 int len; 106 struct ip save_ip; 107 int iphlen; 108 va_list ap; 109 110 va_start(ap, m); 111 iphlen = va_arg(ap, int); 112 va_end(ap); 113 114 udpstat.udps_ipackets++; 115 116 /* 117 * Strip IP options, if any; should skip this, 118 * make available to user, and use on returned packets, 119 * but we don't yet have a way to check the checksum 120 * with options still present. 121 */ 122 if (iphlen > sizeof (struct ip)) { 123 ip_stripoptions(m, (struct mbuf *)0); 124 iphlen = sizeof(struct ip); 125 } 126 127 /* 128 * Get IP and UDP header together in first mbuf. 129 */ 130 ip = mtod(m, struct ip *); 131 if (m->m_len < iphlen + sizeof(struct udphdr)) { 132 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 133 udpstat.udps_hdrops++; 134 return; 135 } 136 ip = mtod(m, struct ip *); 137 } 138 uh = (struct udphdr *)((caddr_t)ip + iphlen); 139 140 /* 141 * Make mbuf data length reflect UDP length. 142 * If not enough data to reflect UDP length, drop. 143 */ 144 len = ntohs((u_int16_t)uh->uh_ulen); 145 if (ip->ip_len != len) { 146 if (len > ip->ip_len) { 147 udpstat.udps_badlen++; 148 goto bad; 149 } 150 m_adj(m, len - ip->ip_len); 151 /* ip->ip_len = len; */ 152 } 153 /* 154 * Save a copy of the IP header in case we want restore it 155 * for sending an ICMP error message in response. 156 */ 157 save_ip = *ip; 158 159 /* 160 * Checksum extended UDP header and data. 161 * from W.R.Stevens: check incoming udp cksums even if 162 * udpcksum is not set. 163 */ 164 if (uh->uh_sum) { 165 bzero(((struct ipovly *)ip)->ih_x1, 166 sizeof ((struct ipovly *)ip)->ih_x1); 167 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 168 if ((uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) != 0) { 169 udpstat.udps_badsum++; 170 m_freem(m); 171 return; 172 } 173 } 174 175 if (IN_MULTICAST(ip->ip_dst.s_addr) || 176 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 177 struct socket *last; 178 /* 179 * Deliver a multicast or broadcast datagram to *all* sockets 180 * for which the local and remote addresses and ports match 181 * those of the incoming datagram. This allows more than 182 * one process to receive multi/broadcasts on the same port. 183 * (This really ought to be done for unicast datagrams as 184 * well, but that would cause problems with existing 185 * applications that open both address-specific sockets and 186 * a wildcard socket listening to the same port -- they would 187 * end up receiving duplicates of every unicast datagram. 188 * Those applications open the multiple sockets to overcome an 189 * inadequacy of the UDP socket interface, but for backwards 190 * compatibility we avoid the problem here rather than 191 * fixing the interface. Maybe 4.5BSD will remedy this?) 192 */ 193 194 /* 195 * Construct sockaddr format source address. 196 */ 197 udp_in.sin_port = uh->uh_sport; 198 udp_in.sin_addr = ip->ip_src; 199 m->m_len -= sizeof (struct udpiphdr); 200 m->m_data += sizeof (struct udpiphdr); 201 /* 202 * Locate pcb(s) for datagram. 203 * (Algorithm copied from raw_intr().) 204 */ 205 last = NULL; 206 for (inp = udbtable.inpt_queue.cqh_first; 207 inp != (struct inpcb *)&udbtable.inpt_queue; 208 inp = inp->inp_queue.cqe_next) { 209 if (inp->inp_lport != uh->uh_dport) 210 continue; 211 if (inp->inp_laddr.s_addr != INADDR_ANY) { 212 if (inp->inp_laddr.s_addr != 213 ip->ip_dst.s_addr) 214 continue; 215 } 216 if (inp->inp_faddr.s_addr != INADDR_ANY) { 217 if (inp->inp_faddr.s_addr != 218 ip->ip_src.s_addr || 219 inp->inp_fport != uh->uh_sport) 220 continue; 221 } 222 223 if (last != NULL) { 224 struct mbuf *n; 225 226 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 227 if (sbappendaddr(&last->so_rcv, 228 sintosa(&udp_in), n, 229 (struct mbuf *)0) == 0) { 230 m_freem(n); 231 udpstat.udps_fullsock++; 232 } else 233 sorwakeup(last); 234 } 235 } 236 last = inp->inp_socket; 237 /* 238 * Don't look for additional matches if this one does 239 * not have either the SO_REUSEPORT or SO_REUSEADDR 240 * socket options set. This heuristic avoids searching 241 * through all pcbs in the common case of a non-shared 242 * port. It * assumes that an application will never 243 * clear these options after setting them. 244 */ 245 if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 246 break; 247 } 248 249 if (last == NULL) { 250 /* 251 * No matching pcb found; discard datagram. 252 * (No need to send an ICMP Port Unreachable 253 * for a broadcast or multicast datgram.) 254 */ 255 udpstat.udps_noportbcast++; 256 goto bad; 257 } 258 if (sbappendaddr(&last->so_rcv, sintosa(&udp_in), m, 259 (struct mbuf *)0) == 0) { 260 udpstat.udps_fullsock++; 261 goto bad; 262 } 263 sorwakeup(last); 264 return; 265 } 266 /* 267 * Locate pcb for datagram. 268 */ 269 inp = in_pcbhashlookup(&udbtable, ip->ip_src, uh->uh_sport, 270 ip->ip_dst, uh->uh_dport); 271 if (inp == 0) { 272 ++udpstat.udps_pcbhashmiss; 273 inp = in_pcblookup(&udbtable, ip->ip_src, uh->uh_sport, 274 ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD); 275 if (inp == 0) { 276 udpstat.udps_noport++; 277 if (m->m_flags & (M_BCAST | M_MCAST)) { 278 udpstat.udps_noportbcast++; 279 goto bad; 280 } 281 *ip = save_ip; 282 ip->ip_len += iphlen; 283 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 284 return; 285 } 286 } 287 288 /* 289 * Construct sockaddr format source address. 290 * Stuff source address and datagram in user buffer. 291 */ 292 udp_in.sin_port = uh->uh_sport; 293 udp_in.sin_addr = ip->ip_src; 294 if (inp->inp_flags & INP_CONTROLOPTS) { 295 struct mbuf **mp = &opts; 296 297 if (inp->inp_flags & INP_RECVDSTADDR) { 298 *mp = udp_saveopt((caddr_t) &ip->ip_dst, 299 sizeof(struct in_addr), IP_RECVDSTADDR); 300 if (*mp) 301 mp = &(*mp)->m_next; 302 } 303 #ifdef notyet 304 /* options were tossed above */ 305 if (inp->inp_flags & INP_RECVOPTS) { 306 *mp = udp_saveopt((caddr_t) opts_deleted_above, 307 sizeof(struct in_addr), IP_RECVOPTS); 308 if (*mp) 309 mp = &(*mp)->m_next; 310 } 311 /* ip_srcroute doesn't do what we want here, need to fix */ 312 if (inp->inp_flags & INP_RECVRETOPTS) { 313 *mp = udp_saveopt((caddr_t) ip_srcroute(), 314 sizeof(struct in_addr), IP_RECVRETOPTS); 315 if (*mp) 316 mp = &(*mp)->m_next; 317 } 318 #endif 319 } 320 iphlen += sizeof(struct udphdr); 321 m->m_len -= iphlen; 322 m->m_pkthdr.len -= iphlen; 323 m->m_data += iphlen; 324 if (sbappendaddr(&inp->inp_socket->so_rcv, sintosa(&udp_in), m, 325 opts) == 0) { 326 udpstat.udps_fullsock++; 327 goto bad; 328 } 329 sorwakeup(inp->inp_socket); 330 return; 331 bad: 332 m_freem(m); 333 if (opts) 334 m_freem(opts); 335 } 336 337 /* 338 * Create a "control" mbuf containing the specified data 339 * with the specified type for presentation with a datagram. 340 */ 341 struct mbuf * 342 udp_saveopt(p, size, type) 343 caddr_t p; 344 register int size; 345 int type; 346 { 347 register struct cmsghdr *cp; 348 struct mbuf *m; 349 350 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 351 return ((struct mbuf *) NULL); 352 cp = (struct cmsghdr *) mtod(m, struct cmsghdr *); 353 bcopy(p, CMSG_DATA(cp), size); 354 size += sizeof(*cp); 355 m->m_len = size; 356 cp->cmsg_len = size; 357 cp->cmsg_level = IPPROTO_IP; 358 cp->cmsg_type = type; 359 return (m); 360 } 361 362 /* 363 * Notify a udp user of an asynchronous error; 364 * just wake up so that he can collect error status. 365 */ 366 static void 367 udp_notify(inp, errno) 368 register struct inpcb *inp; 369 int errno; 370 { 371 inp->inp_socket->so_error = errno; 372 sorwakeup(inp->inp_socket); 373 sowwakeup(inp->inp_socket); 374 } 375 376 void * 377 udp_ctlinput(cmd, sa, v) 378 int cmd; 379 struct sockaddr *sa; 380 void *v; 381 { 382 register struct ip *ip = v; 383 register struct udphdr *uh; 384 extern int inetctlerrmap[]; 385 void (*notify) __P((struct inpcb *, int)) = udp_notify; 386 int errno; 387 388 if ((unsigned)cmd >= PRC_NCMDS) 389 return NULL; 390 errno = inetctlerrmap[cmd]; 391 if (PRC_IS_REDIRECT(cmd)) 392 notify = in_rtchange, ip = 0; 393 else if (cmd == PRC_HOSTDEAD) 394 ip = 0; 395 else if (errno == 0) 396 return NULL; 397 if (ip) { 398 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 399 in_pcbnotify(&udbtable, sa, uh->uh_dport, ip->ip_src, 400 uh->uh_sport, errno, notify); 401 } else 402 in_pcbnotifyall(&udbtable, sa, errno, notify); 403 return NULL; 404 } 405 406 int 407 #if __STDC__ 408 udp_output(struct mbuf *m, ...) 409 #else 410 udp_output(m, va_alist) 411 struct mbuf *m; 412 va_dcl 413 #endif 414 { 415 register struct inpcb *inp; 416 struct mbuf *addr, *control; 417 register struct udpiphdr *ui; 418 register int len = m->m_pkthdr.len; 419 struct in_addr laddr; 420 int s = 0, error = 0; 421 va_list ap; 422 423 va_start(ap, m); 424 inp = va_arg(ap, struct inpcb *); 425 addr = va_arg(ap, struct mbuf *); 426 control = va_arg(ap, struct mbuf *); 427 va_end(ap); 428 429 if (control) 430 m_freem(control); /* XXX */ 431 432 if (addr) { 433 laddr = inp->inp_laddr; 434 if (inp->inp_faddr.s_addr != INADDR_ANY) { 435 error = EISCONN; 436 goto release; 437 } 438 /* 439 * Must block input while temporarily connected. 440 */ 441 s = splsoftnet(); 442 error = in_pcbconnect(inp, addr); 443 if (error) { 444 splx(s); 445 goto release; 446 } 447 } else { 448 if (inp->inp_faddr.s_addr == INADDR_ANY) { 449 error = ENOTCONN; 450 goto release; 451 } 452 } 453 /* 454 * Calculate data length and get a mbuf 455 * for UDP and IP headers. 456 */ 457 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 458 if (m == 0) { 459 error = ENOBUFS; 460 goto bail; 461 } 462 463 /* 464 * Fill in mbuf with extended UDP header 465 * and addresses and length put into network format. 466 */ 467 ui = mtod(m, struct udpiphdr *); 468 bzero(ui->ui_x1, sizeof ui->ui_x1); 469 ui->ui_pr = IPPROTO_UDP; 470 ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr)); 471 ui->ui_src = inp->inp_laddr; 472 ui->ui_dst = inp->inp_faddr; 473 ui->ui_sport = inp->inp_lport; 474 ui->ui_dport = inp->inp_fport; 475 ui->ui_ulen = ui->ui_len; 476 477 /* 478 * Stuff checksum and output datagram. 479 */ 480 ui->ui_sum = 0; 481 if (udpcksum) { 482 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 483 ui->ui_sum = 0xffff; 484 } 485 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 486 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ 487 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ 488 udpstat.udps_opackets++; 489 error = ip_output(m, inp->inp_options, &inp->inp_route, 490 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 491 inp->inp_moptions); 492 493 bail: 494 if (addr) { 495 in_pcbdisconnect(inp); 496 inp->inp_laddr = laddr; 497 splx(s); 498 } 499 return (error); 500 501 release: 502 m_freem(m); 503 return (error); 504 } 505 506 u_long udp_sendspace = 9216; /* really max datagram size */ 507 u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 508 /* 40 1K datagrams */ 509 510 /*ARGSUSED*/ 511 int 512 udp_usrreq(so, req, m, addr, control) 513 struct socket *so; 514 int req; 515 struct mbuf *m, *addr, *control; 516 { 517 struct inpcb *inp = sotoinpcb(so); 518 int error = 0; 519 int s; 520 521 if (req == PRU_CONTROL) 522 return (in_control(so, (long)m, (caddr_t)addr, 523 (struct ifnet *)control)); 524 if (inp == NULL && req != PRU_ATTACH) { 525 error = EINVAL; 526 goto release; 527 } 528 /* 529 * Note: need to block udp_input while changing 530 * the udp pcb queue and/or pcb addresses. 531 */ 532 switch (req) { 533 534 case PRU_ATTACH: 535 if (inp != NULL) { 536 error = EINVAL; 537 break; 538 } 539 s = splsoftnet(); 540 error = in_pcballoc(so, &udbtable); 541 splx(s); 542 if (error) 543 break; 544 error = soreserve(so, udp_sendspace, udp_recvspace); 545 if (error) 546 break; 547 ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl; 548 break; 549 550 case PRU_DETACH: 551 udp_detach(inp); 552 break; 553 554 case PRU_BIND: 555 s = splsoftnet(); 556 error = in_pcbbind(inp, addr); 557 splx(s); 558 break; 559 560 case PRU_LISTEN: 561 error = EOPNOTSUPP; 562 break; 563 564 case PRU_CONNECT: 565 if (inp->inp_faddr.s_addr != INADDR_ANY) { 566 error = EISCONN; 567 break; 568 } 569 s = splsoftnet(); 570 error = in_pcbconnect(inp, addr); 571 splx(s); 572 if (error == 0) 573 soisconnected(so); 574 break; 575 576 case PRU_CONNECT2: 577 error = EOPNOTSUPP; 578 break; 579 580 case PRU_ACCEPT: 581 error = EOPNOTSUPP; 582 break; 583 584 case PRU_DISCONNECT: 585 if (inp->inp_faddr.s_addr == INADDR_ANY) { 586 error = ENOTCONN; 587 break; 588 } 589 s = splsoftnet(); 590 in_pcbdisconnect(inp); 591 inp->inp_laddr.s_addr = INADDR_ANY; 592 splx(s); 593 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 594 break; 595 596 case PRU_SHUTDOWN: 597 socantsendmore(so); 598 break; 599 600 case PRU_SEND: 601 return (udp_output(m, inp, addr, control)); 602 603 case PRU_ABORT: 604 soisdisconnected(so); 605 udp_detach(inp); 606 break; 607 608 case PRU_SOCKADDR: 609 in_setsockaddr(inp, addr); 610 break; 611 612 case PRU_PEERADDR: 613 in_setpeeraddr(inp, addr); 614 break; 615 616 case PRU_SENSE: 617 /* 618 * stat: don't bother with a blocksize. 619 */ 620 return (0); 621 622 case PRU_SENDOOB: 623 case PRU_FASTTIMO: 624 case PRU_SLOWTIMO: 625 case PRU_PROTORCV: 626 case PRU_PROTOSEND: 627 error = EOPNOTSUPP; 628 break; 629 630 case PRU_RCVD: 631 case PRU_RCVOOB: 632 return (EOPNOTSUPP); /* do not free mbuf's */ 633 634 default: 635 panic("udp_usrreq"); 636 } 637 638 release: 639 if (control) { 640 printf("udp control data unexpectedly retained\n"); 641 m_freem(control); 642 } 643 if (m) 644 m_freem(m); 645 return (error); 646 } 647 648 static void 649 udp_detach(inp) 650 struct inpcb *inp; 651 { 652 int s = splsoftnet(); 653 654 in_pcbdetach(inp); 655 splx(s); 656 } 657 658 /* 659 * Sysctl for udp variables. 660 */ 661 int 662 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 663 int *name; 664 u_int namelen; 665 void *oldp; 666 size_t *oldlenp; 667 void *newp; 668 size_t newlen; 669 { 670 /* All sysctl names at this level are terminal. */ 671 if (namelen != 1) 672 return (ENOTDIR); 673 674 switch (name[0]) { 675 case UDPCTL_CHECKSUM: 676 return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum)); 677 default: 678 return (ENOPROTOOPT); 679 } 680 /* NOTREACHED */ 681 } 682