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