1 /* $NetBSD: udp_usrreq.c,v 1.45 1999/01/11 22:35:07 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 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.6 (Berkeley) 5/23/95 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 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, 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 struct sockaddr_in udpsrc; 110 111 va_start(ap, m); 112 iphlen = va_arg(ap, int); 113 va_end(ap); 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_int16_t)uh->uh_ulen); 146 if (ip->ip_len != len) { 147 if (len > ip->ip_len) { 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 bzero(((struct ipovly *)ip)->ih_x1, 165 sizeof ((struct ipovly *)ip)->ih_x1); 166 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 167 if ((uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) != 0) { 168 udpstat.udps_badsum++; 169 m_freem(m); 170 return; 171 } 172 } 173 174 if (IN_MULTICAST(ip->ip_dst.s_addr) || 175 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 176 struct inpcb *last; 177 /* 178 * Deliver a multicast or broadcast datagram to *all* sockets 179 * for which the local and remote addresses and ports match 180 * those of the incoming datagram. This allows more than 181 * one process to receive multi/broadcasts on the same port. 182 * (This really ought to be done for unicast datagrams as 183 * well, but that would cause problems with existing 184 * applications that open both address-specific sockets and 185 * a wildcard socket listening to the same port -- they would 186 * end up receiving duplicates of every unicast datagram. 187 * Those applications open the multiple sockets to overcome an 188 * inadequacy of the UDP socket interface, but for backwards 189 * compatibility we avoid the problem here rather than 190 * fixing the interface. Maybe 4.5BSD will remedy this?) 191 */ 192 193 /* 194 * Construct sockaddr format source address. 195 */ 196 udpsrc.sin_family = AF_INET; 197 udpsrc.sin_len = sizeof(struct sockaddr_in); 198 udpsrc.sin_addr = ip->ip_src; 199 udpsrc.sin_port = uh->uh_sport; 200 bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero)); 201 202 iphlen += sizeof(struct udphdr); 203 m->m_len -= iphlen; 204 m->m_pkthdr.len -= iphlen; 205 m->m_data += iphlen; 206 /* 207 * Locate pcb(s) for datagram. 208 * (Algorithm copied from raw_intr().) 209 */ 210 last = NULL; 211 for (inp = udbtable.inpt_queue.cqh_first; 212 inp != (struct inpcb *)&udbtable.inpt_queue; 213 inp = inp->inp_queue.cqe_next) { 214 if (inp->inp_lport != uh->uh_dport) 215 continue; 216 if (!in_nullhost(inp->inp_laddr)) { 217 if (!in_hosteq(inp->inp_laddr, ip->ip_dst)) 218 continue; 219 } 220 if (!in_nullhost(inp->inp_faddr)) { 221 if (!in_hosteq(inp->inp_faddr, ip->ip_src) || 222 inp->inp_fport != uh->uh_sport) 223 continue; 224 } 225 226 if (last != NULL) { 227 struct mbuf *n; 228 229 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 230 if (last->inp_flags & INP_CONTROLOPTS 231 || last->inp_socket->so_options & 232 SO_TIMESTAMP) { 233 ip_savecontrol(last, &opts, 234 ip, n); 235 } 236 if (sbappendaddr( 237 &last->inp_socket->so_rcv, 238 sintosa(&udpsrc), n, opts) == 0) { 239 m_freem(n); 240 if (opts) 241 m_freem(opts); 242 } else 243 sorwakeup(last->inp_socket); 244 opts = 0; 245 } 246 } 247 last = inp; 248 /* 249 * Don't look for additional matches if this one does 250 * not have either the SO_REUSEPORT or SO_REUSEADDR 251 * socket options set. This heuristic avoids searching 252 * through all pcbs in the common case of a non-shared 253 * port. It * assumes that an application will never 254 * clear these options after setting them. 255 */ 256 if ((last->inp_socket->so_options & 257 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 258 break; 259 } 260 261 if (last == NULL) { 262 /* 263 * No matching pcb found; discard datagram. 264 * (No need to send an ICMP Port Unreachable 265 * for a broadcast or multicast datgram.) 266 */ 267 udpstat.udps_noportbcast++; 268 goto bad; 269 } 270 if (last->inp_flags & INP_CONTROLOPTS || 271 last->inp_socket->so_options & SO_TIMESTAMP) 272 ip_savecontrol(last, &opts, ip, m); 273 if (sbappendaddr(&last->inp_socket->so_rcv, 274 sintosa(&udpsrc), m, opts) == 0) { 275 udpstat.udps_fullsock++; 276 goto bad; 277 } 278 sorwakeup(last->inp_socket); 279 return; 280 } 281 /* 282 * Locate pcb for datagram. 283 */ 284 inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport, 285 ip->ip_dst, uh->uh_dport); 286 if (inp == 0) { 287 ++udpstat.udps_pcbhashmiss; 288 inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport); 289 if (inp == 0) { 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 #if NIPKDB > 0 297 if (checkipkdb(&ip->ip_src, 298 uh->uh_sport, 299 uh->uh_dport, 300 m, 301 iphlen + sizeof(struct udphdr), 302 len - sizeof(struct udphdr))) 303 /* It was a debugger connect packet, just drop it now */ 304 goto bad; 305 #endif 306 ip->ip_len += iphlen; 307 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 308 return; 309 } 310 } 311 312 /* 313 * Construct sockaddr format source address. 314 * Stuff source address and datagram in user buffer. 315 */ 316 udpsrc.sin_family = AF_INET; 317 udpsrc.sin_len = sizeof(struct sockaddr_in); 318 udpsrc.sin_addr = ip->ip_src; 319 udpsrc.sin_port = uh->uh_sport; 320 bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero)); 321 322 if (inp->inp_flags & INP_CONTROLOPTS || 323 inp->inp_socket->so_options & SO_TIMESTAMP) 324 ip_savecontrol(inp, &opts, ip, m); 325 iphlen += sizeof(struct udphdr); 326 m->m_len -= iphlen; 327 m->m_pkthdr.len -= iphlen; 328 m->m_data += iphlen; 329 if (sbappendaddr(&inp->inp_socket->so_rcv, sintosa(&udpsrc), m, 330 opts) == 0) { 331 udpstat.udps_fullsock++; 332 goto bad; 333 } 334 sorwakeup(inp->inp_socket); 335 return; 336 bad: 337 m_freem(m); 338 if (opts) 339 m_freem(opts); 340 } 341 342 /* 343 * Notify a udp user of an asynchronous error; 344 * just wake up so that he can collect error status. 345 */ 346 static void 347 udp_notify(inp, errno) 348 register struct inpcb *inp; 349 int errno; 350 { 351 352 inp->inp_socket->so_error = errno; 353 sorwakeup(inp->inp_socket); 354 sowwakeup(inp->inp_socket); 355 } 356 357 void * 358 udp_ctlinput(cmd, sa, v) 359 int cmd; 360 struct sockaddr *sa; 361 void *v; 362 { 363 register struct ip *ip = v; 364 register struct udphdr *uh; 365 extern int inetctlerrmap[]; 366 void (*notify) __P((struct inpcb *, int)) = udp_notify; 367 int errno; 368 369 if ((unsigned)cmd >= PRC_NCMDS) 370 return NULL; 371 errno = inetctlerrmap[cmd]; 372 if (PRC_IS_REDIRECT(cmd)) 373 notify = in_rtchange, ip = 0; 374 else if (cmd == PRC_HOSTDEAD) 375 ip = 0; 376 else if (errno == 0) 377 return NULL; 378 if (ip) { 379 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 380 in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport, 381 ip->ip_src, uh->uh_sport, errno, notify); 382 } else 383 in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno, 384 notify); 385 return NULL; 386 } 387 388 int 389 #if __STDC__ 390 udp_output(struct mbuf *m, ...) 391 #else 392 udp_output(m, va_alist) 393 struct mbuf *m; 394 va_dcl 395 #endif 396 { 397 register struct inpcb *inp; 398 register struct udpiphdr *ui; 399 register int len = m->m_pkthdr.len; 400 int error = 0; 401 va_list ap; 402 403 va_start(ap, m); 404 inp = va_arg(ap, struct inpcb *); 405 va_end(ap); 406 407 /* 408 * Calculate data length and get a mbuf 409 * for UDP and IP headers. 410 */ 411 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 412 if (m == 0) { 413 error = ENOBUFS; 414 goto release; 415 } 416 417 /* 418 * Compute the packet length of the IP header, and 419 * punt if the length looks bogus. 420 */ 421 if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) { 422 error = EMSGSIZE; 423 goto release; 424 } 425 426 /* 427 * Fill in mbuf with extended UDP header 428 * and addresses and length put into network format. 429 */ 430 ui = mtod(m, struct udpiphdr *); 431 bzero(ui->ui_x1, sizeof ui->ui_x1); 432 ui->ui_pr = IPPROTO_UDP; 433 ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr)); 434 ui->ui_src = inp->inp_laddr; 435 ui->ui_dst = inp->inp_faddr; 436 ui->ui_sport = inp->inp_lport; 437 ui->ui_dport = inp->inp_fport; 438 ui->ui_ulen = ui->ui_len; 439 440 /* 441 * Stuff checksum and output datagram. 442 */ 443 ui->ui_sum = 0; 444 if (udpcksum) { 445 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 446 ui->ui_sum = 0xffff; 447 } 448 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 449 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ 450 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ 451 udpstat.udps_opackets++; 452 return (ip_output(m, inp->inp_options, &inp->inp_route, 453 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 454 inp->inp_moptions)); 455 456 release: 457 m_freem(m); 458 return (error); 459 } 460 461 int udp_sendspace = 9216; /* really max datagram size */ 462 int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 463 /* 40 1K datagrams */ 464 465 /*ARGSUSED*/ 466 int 467 udp_usrreq(so, req, m, nam, control, p) 468 struct socket *so; 469 int req; 470 struct mbuf *m, *nam, *control; 471 struct proc *p; 472 { 473 register struct inpcb *inp; 474 int s; 475 register int error = 0; 476 477 if (req == PRU_CONTROL) 478 return (in_control(so, (long)m, (caddr_t)nam, 479 (struct ifnet *)control, p)); 480 481 s = splsoftnet(); 482 inp = sotoinpcb(so); 483 #ifdef DIAGNOSTIC 484 if (req != PRU_SEND && req != PRU_SENDOOB && control) 485 panic("udp_usrreq: unexpected control mbuf"); 486 #endif 487 if (inp == 0 && req != PRU_ATTACH) { 488 error = EINVAL; 489 goto release; 490 } 491 492 /* 493 * Note: need to block udp_input while changing 494 * the udp pcb queue and/or pcb addresses. 495 */ 496 switch (req) { 497 498 case PRU_ATTACH: 499 if (inp != 0) { 500 error = EISCONN; 501 break; 502 } 503 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 504 error = soreserve(so, udp_sendspace, udp_recvspace); 505 if (error) 506 break; 507 } 508 error = in_pcballoc(so, &udbtable); 509 if (error) 510 break; 511 inp = sotoinpcb(so); 512 inp->inp_ip.ip_ttl = ip_defttl; 513 break; 514 515 case PRU_DETACH: 516 in_pcbdetach(inp); 517 break; 518 519 case PRU_BIND: 520 error = in_pcbbind(inp, nam, p); 521 break; 522 523 case PRU_LISTEN: 524 error = EOPNOTSUPP; 525 break; 526 527 case PRU_CONNECT: 528 error = in_pcbconnect(inp, nam); 529 if (error) 530 break; 531 soisconnected(so); 532 break; 533 534 case PRU_CONNECT2: 535 error = EOPNOTSUPP; 536 break; 537 538 case PRU_DISCONNECT: 539 /*soisdisconnected(so);*/ 540 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 541 in_pcbdisconnect(inp); 542 inp->inp_laddr = zeroin_addr; /* XXX */ 543 in_pcbstate(inp, INP_BOUND); /* XXX */ 544 break; 545 546 case PRU_SHUTDOWN: 547 socantsendmore(so); 548 break; 549 550 case PRU_RCVD: 551 error = EOPNOTSUPP; 552 break; 553 554 case PRU_SEND: 555 if (control && control->m_len) { 556 m_freem(control); 557 m_freem(m); 558 error = EINVAL; 559 break; 560 } 561 { 562 struct in_addr laddr; /* XXX */ 563 564 if (nam) { 565 laddr = inp->inp_laddr; /* XXX */ 566 if ((so->so_state & SS_ISCONNECTED) != 0) { 567 error = EISCONN; 568 goto die; 569 } 570 error = in_pcbconnect(inp, nam); 571 if (error) { 572 die: 573 m_freem(m); 574 break; 575 } 576 } else { 577 if ((so->so_state & SS_ISCONNECTED) == 0) { 578 error = ENOTCONN; 579 goto die; 580 } 581 } 582 error = udp_output(m, inp); 583 if (nam) { 584 in_pcbdisconnect(inp); 585 inp->inp_laddr = laddr; /* XXX */ 586 in_pcbstate(inp, INP_BOUND); /* XXX */ 587 } 588 } 589 break; 590 591 case PRU_SENSE: 592 /* 593 * stat: don't bother with a blocksize. 594 */ 595 splx(s); 596 return (0); 597 598 case PRU_RCVOOB: 599 error = EOPNOTSUPP; 600 break; 601 602 case PRU_SENDOOB: 603 m_freem(control); 604 m_freem(m); 605 error = EOPNOTSUPP; 606 break; 607 608 case PRU_SOCKADDR: 609 in_setsockaddr(inp, nam); 610 break; 611 612 case PRU_PEERADDR: 613 in_setpeeraddr(inp, nam); 614 break; 615 616 default: 617 panic("udp_usrreq"); 618 } 619 620 release: 621 splx(s); 622 return (error); 623 } 624 625 /* 626 * Sysctl for udp variables. 627 */ 628 int 629 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 630 int *name; 631 u_int namelen; 632 void *oldp; 633 size_t *oldlenp; 634 void *newp; 635 size_t newlen; 636 { 637 /* All sysctl names at this level are terminal. */ 638 if (namelen != 1) 639 return (ENOTDIR); 640 641 switch (name[0]) { 642 case UDPCTL_CHECKSUM: 643 return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum)); 644 case UDPCTL_SENDSPACE: 645 return (sysctl_int(oldp, oldlenp, newp, newlen, 646 &udp_sendspace)); 647 case UDPCTL_RECVSPACE: 648 return (sysctl_int(oldp, oldlenp, newp, newlen, 649 &udp_recvspace)); 650 default: 651 return (ENOPROTOOPT); 652 } 653 /* NOTREACHED */ 654 } 655