1 /* $NetBSD: udp_usrreq.c,v 1.36 1996/09/16 17:45:19 mycroft 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 38 #include <sys/param.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/protosw.h> 42 #include <sys/socket.h> 43 #include <sys/socketvar.h> 44 #include <sys/errno.h> 45 #include <sys/stat.h> 46 #include <sys/systm.h> 47 #include <sys/proc.h> 48 49 #include <vm/vm.h> 50 #include <sys/sysctl.h> 51 52 #include <net/if.h> 53 #include <net/route.h> 54 55 #include <netinet/in.h> 56 #include <netinet/in_systm.h> 57 #include <netinet/in_var.h> 58 #include <netinet/ip.h> 59 #include <netinet/in_pcb.h> 60 #include <netinet/ip_var.h> 61 #include <netinet/ip_icmp.h> 62 #include <netinet/udp.h> 63 #include <netinet/udp_var.h> 64 65 #include <machine/stdarg.h> 66 67 /* 68 * UDP protocol implementation. 69 * Per RFC 768, August, 1980. 70 */ 71 #ifndef COMPAT_42 72 int udpcksum = 1; 73 #else 74 int udpcksum = 0; /* XXX */ 75 #endif 76 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, 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 socket *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 m->m_len -= sizeof (struct udpiphdr); 203 m->m_data += sizeof (struct udpiphdr); 204 /* 205 * Locate pcb(s) for datagram. 206 * (Algorithm copied from raw_intr().) 207 */ 208 last = NULL; 209 for (inp = udbtable.inpt_queue.cqh_first; 210 inp != (struct inpcb *)&udbtable.inpt_queue; 211 inp = inp->inp_queue.cqe_next) { 212 if (inp->inp_lport != uh->uh_dport) 213 continue; 214 if (!in_nullhost(inp->inp_laddr)) { 215 if (!in_hosteq(inp->inp_laddr, ip->ip_dst)) 216 continue; 217 } 218 if (!in_nullhost(inp->inp_faddr)) { 219 if (!in_hosteq(inp->inp_faddr, ip->ip_src) || 220 inp->inp_fport != uh->uh_sport) 221 continue; 222 } 223 224 if (last != NULL) { 225 struct mbuf *n; 226 227 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 228 if (sbappendaddr(&last->so_rcv, 229 sintosa(&udpsrc), n, 230 (struct mbuf *)0) == 0) { 231 m_freem(n); 232 udpstat.udps_fullsock++; 233 } else 234 sorwakeup(last); 235 } 236 } 237 last = inp->inp_socket; 238 /* 239 * Don't look for additional matches if this one does 240 * not have either the SO_REUSEPORT or SO_REUSEADDR 241 * socket options set. This heuristic avoids searching 242 * through all pcbs in the common case of a non-shared 243 * port. It * assumes that an application will never 244 * clear these options after setting them. 245 */ 246 if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 247 break; 248 } 249 250 if (last == NULL) { 251 /* 252 * No matching pcb found; discard datagram. 253 * (No need to send an ICMP Port Unreachable 254 * for a broadcast or multicast datgram.) 255 */ 256 udpstat.udps_noportbcast++; 257 goto bad; 258 } 259 if (sbappendaddr(&last->so_rcv, sintosa(&udpsrc), m, 260 (struct mbuf *)0) == 0) { 261 udpstat.udps_fullsock++; 262 goto bad; 263 } 264 sorwakeup(last); 265 return; 266 } 267 /* 268 * Locate pcb for datagram. 269 */ 270 inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport, 271 ip->ip_dst, uh->uh_dport); 272 if (inp == 0) { 273 ++udpstat.udps_pcbhashmiss; 274 inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport); 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 udpsrc.sin_family = AF_INET; 293 udpsrc.sin_len = sizeof(struct sockaddr_in); 294 udpsrc.sin_addr = ip->ip_src; 295 udpsrc.sin_port = uh->uh_sport; 296 bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero)); 297 298 if (inp->inp_flags & INP_CONTROLOPTS) { 299 struct mbuf **mp = &opts; 300 301 if (inp->inp_flags & INP_RECVDSTADDR) { 302 *mp = udp_saveopt((caddr_t) &ip->ip_dst, 303 sizeof(struct in_addr), IP_RECVDSTADDR); 304 if (*mp) 305 mp = &(*mp)->m_next; 306 } 307 #ifdef notyet 308 /* options were tossed above */ 309 if (inp->inp_flags & INP_RECVOPTS) { 310 *mp = udp_saveopt((caddr_t) opts_deleted_above, 311 sizeof(struct in_addr), IP_RECVOPTS); 312 if (*mp) 313 mp = &(*mp)->m_next; 314 } 315 /* ip_srcroute doesn't do what we want here, need to fix */ 316 if (inp->inp_flags & INP_RECVRETOPTS) { 317 *mp = udp_saveopt((caddr_t) ip_srcroute(), 318 sizeof(struct in_addr), IP_RECVRETOPTS); 319 if (*mp) 320 mp = &(*mp)->m_next; 321 } 322 #endif 323 } 324 iphlen += sizeof(struct udphdr); 325 m->m_len -= iphlen; 326 m->m_pkthdr.len -= iphlen; 327 m->m_data += iphlen; 328 if (sbappendaddr(&inp->inp_socket->so_rcv, sintosa(&udpsrc), m, 329 opts) == 0) { 330 udpstat.udps_fullsock++; 331 goto bad; 332 } 333 sorwakeup(inp->inp_socket); 334 return; 335 bad: 336 m_freem(m); 337 if (opts) 338 m_freem(opts); 339 } 340 341 /* 342 * Create a "control" mbuf containing the specified data 343 * with the specified type for presentation with a datagram. 344 */ 345 struct mbuf * 346 udp_saveopt(p, size, type) 347 caddr_t p; 348 register int size; 349 int type; 350 { 351 register struct cmsghdr *cp; 352 struct mbuf *m; 353 354 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 355 return ((struct mbuf *) NULL); 356 cp = (struct cmsghdr *) mtod(m, struct cmsghdr *); 357 bcopy(p, CMSG_DATA(cp), size); 358 size += sizeof(*cp); 359 m->m_len = size; 360 cp->cmsg_len = size; 361 cp->cmsg_level = IPPROTO_IP; 362 cp->cmsg_type = type; 363 return (m); 364 } 365 366 /* 367 * Notify a udp user of an asynchronous error; 368 * just wake up so that he can collect error status. 369 */ 370 static void 371 udp_notify(inp, errno) 372 register struct inpcb *inp; 373 int errno; 374 { 375 376 inp->inp_socket->so_error = errno; 377 sorwakeup(inp->inp_socket); 378 sowwakeup(inp->inp_socket); 379 } 380 381 void * 382 udp_ctlinput(cmd, sa, v) 383 int cmd; 384 struct sockaddr *sa; 385 void *v; 386 { 387 register struct ip *ip = v; 388 register struct udphdr *uh; 389 extern int inetctlerrmap[]; 390 void (*notify) __P((struct inpcb *, int)) = udp_notify; 391 int errno; 392 393 if ((unsigned)cmd >= PRC_NCMDS) 394 return NULL; 395 errno = inetctlerrmap[cmd]; 396 if (PRC_IS_REDIRECT(cmd)) 397 notify = in_rtchange, ip = 0; 398 else if (cmd == PRC_HOSTDEAD) 399 ip = 0; 400 else if (errno == 0) 401 return NULL; 402 if (ip) { 403 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 404 in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport, 405 ip->ip_src, uh->uh_sport, errno, notify); 406 } else 407 in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno, 408 notify); 409 return NULL; 410 } 411 412 int 413 #if __STDC__ 414 udp_output(struct mbuf *m, ...) 415 #else 416 udp_output(m, va_alist) 417 struct mbuf *m; 418 va_dcl 419 #endif 420 { 421 register struct inpcb *inp; 422 register struct udpiphdr *ui; 423 register int len = m->m_pkthdr.len; 424 int error = 0; 425 va_list ap; 426 427 va_start(ap, m); 428 inp = va_arg(ap, struct inpcb *); 429 va_end(ap); 430 431 /* 432 * Calculate data length and get a mbuf 433 * for UDP and IP headers. 434 */ 435 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 436 if (m == 0) { 437 error = ENOBUFS; 438 goto release; 439 } 440 441 /* 442 * Fill in mbuf with extended UDP header 443 * and addresses and length put into network format. 444 */ 445 ui = mtod(m, struct udpiphdr *); 446 bzero(ui->ui_x1, sizeof ui->ui_x1); 447 ui->ui_pr = IPPROTO_UDP; 448 ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr)); 449 ui->ui_src = inp->inp_laddr; 450 ui->ui_dst = inp->inp_faddr; 451 ui->ui_sport = inp->inp_lport; 452 ui->ui_dport = inp->inp_fport; 453 ui->ui_ulen = ui->ui_len; 454 455 /* 456 * Stuff checksum and output datagram. 457 */ 458 ui->ui_sum = 0; 459 if (udpcksum) { 460 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 461 ui->ui_sum = 0xffff; 462 } 463 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 464 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ 465 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ 466 udpstat.udps_opackets++; 467 return (ip_output(m, inp->inp_options, &inp->inp_route, 468 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 469 inp->inp_moptions)); 470 471 release: 472 m_freem(m); 473 return (error); 474 } 475 476 u_long udp_sendspace = 9216; /* really max datagram size */ 477 u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 478 /* 40 1K datagrams */ 479 480 /*ARGSUSED*/ 481 int 482 udp_usrreq(so, req, m, nam, control, p) 483 struct socket *so; 484 int req; 485 struct mbuf *m, *nam, *control; 486 struct proc *p; 487 { 488 register struct inpcb *inp; 489 int s; 490 register int error = 0; 491 492 if (req == PRU_CONTROL) 493 return (in_control(so, (long)m, (caddr_t)nam, 494 (struct ifnet *)control, p)); 495 496 s = splsoftnet(); 497 inp = sotoinpcb(so); 498 #ifdef DIAGNOSTIC 499 if (req != PRU_SEND && req != PRU_SENDOOB && control) 500 panic("udp_usrreq: unexpected control mbuf"); 501 #endif 502 if (inp == 0 && req != PRU_ATTACH) { 503 error = EINVAL; 504 goto release; 505 } 506 507 /* 508 * Note: need to block udp_input while changing 509 * the udp pcb queue and/or pcb addresses. 510 */ 511 switch (req) { 512 513 case PRU_ATTACH: 514 if (inp != 0) { 515 error = EISCONN; 516 break; 517 } 518 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 519 error = soreserve(so, udp_sendspace, udp_recvspace); 520 if (error) 521 break; 522 } 523 error = in_pcballoc(so, &udbtable); 524 if (error) 525 break; 526 inp = sotoinpcb(so); 527 inp->inp_ip.ip_ttl = ip_defttl; 528 break; 529 530 case PRU_DETACH: 531 in_pcbdetach(inp); 532 break; 533 534 case PRU_BIND: 535 error = in_pcbbind(inp, nam, p); 536 break; 537 538 case PRU_LISTEN: 539 error = EOPNOTSUPP; 540 break; 541 542 case PRU_CONNECT: 543 error = in_pcbconnect(inp, nam); 544 if (error) 545 break; 546 soisconnected(so); 547 break; 548 549 case PRU_CONNECT2: 550 error = EOPNOTSUPP; 551 break; 552 553 case PRU_DISCONNECT: 554 /*soisdisconnected(so);*/ 555 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 556 in_pcbdisconnect(inp); 557 inp->inp_laddr = zeroin_addr; /* XXX */ 558 in_pcbstate(inp, INP_BOUND); /* XXX */ 559 break; 560 561 case PRU_SHUTDOWN: 562 socantsendmore(so); 563 break; 564 565 case PRU_RCVD: 566 error = EOPNOTSUPP; 567 break; 568 569 case PRU_SEND: 570 if (control && control->m_len) { 571 m_freem(control); 572 m_freem(m); 573 error = EINVAL; 574 break; 575 } 576 { 577 struct in_addr laddr; /* XXX */ 578 579 if (nam) { 580 laddr = inp->inp_laddr; /* XXX */ 581 if ((so->so_state & SS_ISCONNECTED) != 0) { 582 error = EISCONN; 583 goto die; 584 } 585 error = in_pcbconnect(inp, nam); 586 if (error) { 587 die: 588 m_freem(m); 589 break; 590 } 591 } else { 592 if ((so->so_state & SS_ISCONNECTED) == 0) { 593 error = ENOTCONN; 594 goto die; 595 } 596 } 597 error = udp_output(m, inp); 598 if (nam) { 599 in_pcbdisconnect(inp); 600 inp->inp_laddr = laddr; /* XXX */ 601 in_pcbstate(inp, INP_BOUND); /* XXX */ 602 } 603 } 604 break; 605 606 case PRU_SENSE: 607 /* 608 * stat: don't bother with a blocksize. 609 */ 610 splx(s); 611 return (0); 612 613 case PRU_RCVOOB: 614 error = EOPNOTSUPP; 615 break; 616 617 case PRU_SENDOOB: 618 m_freem(control); 619 m_freem(m); 620 error = EOPNOTSUPP; 621 break; 622 623 case PRU_SOCKADDR: 624 in_setsockaddr(inp, nam); 625 break; 626 627 case PRU_PEERADDR: 628 in_setpeeraddr(inp, nam); 629 break; 630 631 default: 632 panic("udp_usrreq"); 633 } 634 635 release: 636 splx(s); 637 return (error); 638 } 639 640 /* 641 * Sysctl for udp variables. 642 */ 643 int 644 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 645 int *name; 646 u_int namelen; 647 void *oldp; 648 size_t *oldlenp; 649 void *newp; 650 size_t newlen; 651 { 652 /* All sysctl names at this level are terminal. */ 653 if (namelen != 1) 654 return (ENOTDIR); 655 656 switch (name[0]) { 657 case UDPCTL_CHECKSUM: 658 return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum)); 659 default: 660 return (ENOPROTOOPT); 661 } 662 /* NOTREACHED */ 663 } 664