91/* 92 * UDP protocol implementation. 93 * Per RFC 768, August, 1980. 94 */ 95#ifndef COMPAT_42 96static int udpcksum = 1; 97#else 98static int udpcksum = 0; /* XXX */ 99#endif 100SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 101 &udpcksum, 0, ""); 102 103int log_in_vain = 0; 104SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 105 &log_in_vain, 0, "Log all incoming UDP packets"); 106 107static int blackhole = 0; 108SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, 109 &blackhole, 0, "Do not send port unreachables for refused connects"); 110 111static int strict_mcast_mship = 0; 112SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW, 113 &strict_mcast_mship, 0, "Only send multicast to member sockets"); 114 115struct inpcbhead udb; /* from udp_var.h */ 116#define udb6 udb /* for KAME src sync over BSD*'s */ 117struct inpcbinfo udbinfo; 118 119#ifndef UDBHASHSIZE 120#define UDBHASHSIZE 16 121#endif 122 123struct udpstat udpstat; /* from udp_var.h */ 124SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, 125 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 126 127static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, 128 int off, struct sockaddr_in *udp_in); 129 130static void udp_detach(struct socket *so); 131static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 132 struct mbuf *, struct thread *); 133 134static void 135udp_zone_change(void *tag) 136{ 137 138 uma_zone_set_max(udbinfo.ipi_zone, maxsockets); 139} 140 141static int 142udp_inpcb_init(void *mem, int size, int flags) 143{ 144 struct inpcb *inp = (struct inpcb *) mem; 145 INP_LOCK_INIT(inp, "inp", "udpinp"); 146 return (0); 147} 148 149void 150udp_init() 151{ 152 INP_INFO_LOCK_INIT(&udbinfo, "udp"); 153 LIST_INIT(&udb); 154 udbinfo.listhead = &udb; 155 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 156 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 157 &udbinfo.porthashmask); 158 udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL, 159 NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 160 uma_zone_set_max(udbinfo.ipi_zone, maxsockets); 161 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL, 162 EVENTHANDLER_PRI_ANY); 163} 164 165void 166udp_input(m, off) 167 register struct mbuf *m; 168 int off; 169{ 170 int iphlen = off; 171 register struct ip *ip; 172 register struct udphdr *uh; 173 register struct inpcb *inp; 174 int len; 175 struct ip save_ip; 176 struct sockaddr_in udp_in; 177#ifdef IPFIREWALL_FORWARD 178 struct m_tag *fwd_tag; 179#endif 180 181 udpstat.udps_ipackets++; 182 183 /* 184 * Strip IP options, if any; should skip this, 185 * make available to user, and use on returned packets, 186 * but we don't yet have a way to check the checksum 187 * with options still present. 188 */ 189 if (iphlen > sizeof (struct ip)) { 190 ip_stripoptions(m, (struct mbuf *)0); 191 iphlen = sizeof(struct ip); 192 } 193 194 /* 195 * Get IP and UDP header together in first mbuf. 196 */ 197 ip = mtod(m, struct ip *); 198 if (m->m_len < iphlen + sizeof(struct udphdr)) { 199 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 200 udpstat.udps_hdrops++; 201 return; 202 } 203 ip = mtod(m, struct ip *); 204 } 205 uh = (struct udphdr *)((caddr_t)ip + iphlen); 206 207 /* destination port of 0 is illegal, based on RFC768. */ 208 if (uh->uh_dport == 0) 209 goto badunlocked; 210 211 /* 212 * Construct sockaddr format source address. 213 * Stuff source address and datagram in user buffer. 214 */ 215 bzero(&udp_in, sizeof(udp_in)); 216 udp_in.sin_len = sizeof(udp_in); 217 udp_in.sin_family = AF_INET; 218 udp_in.sin_port = uh->uh_sport; 219 udp_in.sin_addr = ip->ip_src; 220 221 /* 222 * Make mbuf data length reflect UDP length. 223 * If not enough data to reflect UDP length, drop. 224 */ 225 len = ntohs((u_short)uh->uh_ulen); 226 if (ip->ip_len != len) { 227 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 228 udpstat.udps_badlen++; 229 goto badunlocked; 230 } 231 m_adj(m, len - ip->ip_len); 232 /* ip->ip_len = len; */ 233 } 234 /* 235 * Save a copy of the IP header in case we want restore it 236 * for sending an ICMP error message in response. 237 */ 238 if (!blackhole) 239 save_ip = *ip; 240 241 /* 242 * Checksum extended UDP header and data. 243 */ 244 if (uh->uh_sum) { 245 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 246 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 247 uh->uh_sum = m->m_pkthdr.csum_data; 248 else 249 uh->uh_sum = in_pseudo(ip->ip_src.s_addr, 250 ip->ip_dst.s_addr, htonl((u_short)len + 251 m->m_pkthdr.csum_data + IPPROTO_UDP)); 252 uh->uh_sum ^= 0xffff; 253 } else { 254 char b[9]; 255 bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 256 bzero(((struct ipovly *)ip)->ih_x1, 9); 257 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 258 uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 259 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 260 } 261 if (uh->uh_sum) { 262 udpstat.udps_badsum++; 263 m_freem(m); 264 return; 265 } 266 } else 267 udpstat.udps_nosum++; 268 269#ifdef IPFIREWALL_FORWARD 270 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */ 271 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 272 273 if (fwd_tag != NULL) { 274 struct sockaddr_in *next_hop; 275 276 /* Do the hack. */ 277 next_hop = (struct sockaddr_in *)(fwd_tag + 1); 278 ip->ip_dst = next_hop->sin_addr; 279 uh->uh_dport = ntohs(next_hop->sin_port); 280 /* Remove the tag from the packet. We don't need it anymore. */ 281 m_tag_delete(m, fwd_tag); 282 } 283#endif 284 285 INP_INFO_RLOCK(&udbinfo); 286 287 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 288 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 289 struct inpcb *last; 290 /* 291 * Deliver a multicast or broadcast datagram to *all* sockets 292 * for which the local and remote addresses and ports match 293 * those of the incoming datagram. This allows more than 294 * one process to receive multi/broadcasts on the same port. 295 * (This really ought to be done for unicast datagrams as 296 * well, but that would cause problems with existing 297 * applications that open both address-specific sockets and 298 * a wildcard socket listening to the same port -- they would 299 * end up receiving duplicates of every unicast datagram. 300 * Those applications open the multiple sockets to overcome an 301 * inadequacy of the UDP socket interface, but for backwards 302 * compatibility we avoid the problem here rather than 303 * fixing the interface. Maybe 4.5BSD will remedy this?) 304 */ 305 306 /* 307 * Locate pcb(s) for datagram. 308 * (Algorithm copied from raw_intr().) 309 */ 310 last = NULL; 311 LIST_FOREACH(inp, &udb, inp_list) { 312 if (inp->inp_lport != uh->uh_dport) 313 continue; 314#ifdef INET6 315 if ((inp->inp_vflag & INP_IPV4) == 0) 316 continue; 317#endif 318 if (inp->inp_laddr.s_addr != INADDR_ANY) { 319 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 320 continue; 321 } 322 if (inp->inp_faddr.s_addr != INADDR_ANY) { 323 if (inp->inp_faddr.s_addr != 324 ip->ip_src.s_addr || 325 inp->inp_fport != uh->uh_sport) 326 continue; 327 } 328 INP_LOCK(inp); 329 330 /* 331 * Check multicast packets to make sure they are only 332 * sent to sockets with multicast memberships for the 333 * packet's destination address and arrival interface 334 */ 335#define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)]) 336#define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships) 337 if (strict_mcast_mship && inp->inp_moptions != NULL) { 338 int mship, foundmship = 0; 339 340 for (mship = 0; mship < NMSHIPS(inp); mship++) { 341 if (MSHIP(inp, mship)->inm_addr.s_addr 342 == ip->ip_dst.s_addr && 343 MSHIP(inp, mship)->inm_ifp 344 == m->m_pkthdr.rcvif) { 345 foundmship = 1; 346 break; 347 } 348 } 349 if (foundmship == 0) { 350 INP_UNLOCK(inp); 351 continue; 352 } 353 } 354#undef NMSHIPS 355#undef MSHIP 356 if (last != NULL) { 357 struct mbuf *n; 358 359 n = m_copy(m, 0, M_COPYALL); 360 if (n != NULL) 361 udp_append(last, ip, n, 362 iphlen + 363 sizeof(struct udphdr), 364 &udp_in); 365 INP_UNLOCK(last); 366 } 367 last = inp; 368 /* 369 * Don't look for additional matches if this one does 370 * not have either the SO_REUSEPORT or SO_REUSEADDR 371 * socket options set. This heuristic avoids searching 372 * through all pcbs in the common case of a non-shared 373 * port. It assumes that an application will never 374 * clear these options after setting them. 375 */ 376 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 377 break; 378 } 379 380 if (last == NULL) { 381 /* 382 * No matching pcb found; discard datagram. 383 * (No need to send an ICMP Port Unreachable 384 * for a broadcast or multicast datgram.) 385 */ 386 udpstat.udps_noportbcast++; 387 goto badheadlocked; 388 } 389 udp_append(last, ip, m, iphlen + sizeof(struct udphdr), 390 &udp_in); 391 INP_UNLOCK(last); 392 INP_INFO_RUNLOCK(&udbinfo); 393 return; 394 } 395 /* 396 * Locate pcb for datagram. 397 */ 398 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 399 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 400 if (inp == NULL) { 401 if (log_in_vain) { 402 char buf[4*sizeof "123"]; 403 404 strcpy(buf, inet_ntoa(ip->ip_dst)); 405 log(LOG_INFO, 406 "Connection attempt to UDP %s:%d from %s:%d\n", 407 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 408 ntohs(uh->uh_sport)); 409 } 410 udpstat.udps_noport++; 411 if (m->m_flags & (M_BCAST | M_MCAST)) { 412 udpstat.udps_noportbcast++; 413 goto badheadlocked; 414 } 415 if (blackhole) 416 goto badheadlocked; 417 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 418 goto badheadlocked; 419 *ip = save_ip; 420 ip->ip_len += iphlen; 421 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 422 INP_INFO_RUNLOCK(&udbinfo); 423 return; 424 } 425 INP_LOCK(inp); 426 /* Check the minimum TTL for socket. */ 427 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) 428 goto badheadlocked; 429 udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in); 430 INP_UNLOCK(inp); 431 INP_INFO_RUNLOCK(&udbinfo); 432 return; 433 434badheadlocked: 435 if (inp) 436 INP_UNLOCK(inp); 437 INP_INFO_RUNLOCK(&udbinfo); 438badunlocked: 439 m_freem(m); 440 return; 441} 442 443/* 444 * Subroutine of udp_input(), which appends the provided mbuf chain to the 445 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 446 * contains the source address. If the socket ends up being an IPv6 socket, 447 * udp_append() will convert to a sockaddr_in6 before passing the address 448 * into the socket code. 449 */ 450static void 451udp_append(inp, ip, n, off, udp_in) 452 struct inpcb *inp; 453 struct ip *ip; 454 struct mbuf *n; 455 int off; 456 struct sockaddr_in *udp_in; 457{ 458 struct sockaddr *append_sa; 459 struct socket *so; 460 struct mbuf *opts = 0; 461#ifdef INET6 462 struct sockaddr_in6 udp_in6; 463#endif 464 465 INP_LOCK_ASSERT(inp); 466 467#if defined(IPSEC) || defined(FAST_IPSEC) 468 /* check AH/ESP integrity. */ 469 if (ipsec4_in_reject(n, inp)) { 470#ifdef IPSEC 471 ipsecstat.in_polvio++; 472#endif /*IPSEC*/ 473 m_freem(n); 474 return; 475 } 476#endif /*IPSEC || FAST_IPSEC*/ 477#ifdef MAC 478 if (mac_check_inpcb_deliver(inp, n) != 0) { 479 m_freem(n); 480 return; 481 } 482#endif 483 if (inp->inp_flags & INP_CONTROLOPTS || 484 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 485#ifdef INET6 486 if (inp->inp_vflag & INP_IPV6) { 487 int savedflags; 488 489 savedflags = inp->inp_flags; 490 inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 491 ip6_savecontrol(inp, n, &opts); 492 inp->inp_flags = savedflags; 493 } else 494#endif 495 ip_savecontrol(inp, &opts, ip, n); 496 } 497#ifdef INET6 498 if (inp->inp_vflag & INP_IPV6) { 499 bzero(&udp_in6, sizeof(udp_in6)); 500 udp_in6.sin6_len = sizeof(udp_in6); 501 udp_in6.sin6_family = AF_INET6; 502 in6_sin_2_v4mapsin6(udp_in, &udp_in6); 503 append_sa = (struct sockaddr *)&udp_in6; 504 } else 505#endif 506 append_sa = (struct sockaddr *)udp_in; 507 m_adj(n, off); 508 509 so = inp->inp_socket; 510 SOCKBUF_LOCK(&so->so_rcv); 511 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 512 m_freem(n); 513 if (opts) 514 m_freem(opts); 515 udpstat.udps_fullsock++; 516 SOCKBUF_UNLOCK(&so->so_rcv); 517 } else 518 sorwakeup_locked(so); 519} 520 521/* 522 * Notify a udp user of an asynchronous error; 523 * just wake up so that he can collect error status. 524 */ 525struct inpcb * 526udp_notify(inp, errno) 527 register struct inpcb *inp; 528 int errno; 529{ 530 inp->inp_socket->so_error = errno; 531 sorwakeup(inp->inp_socket); 532 sowwakeup(inp->inp_socket); 533 return inp; 534} 535 536void 537udp_ctlinput(cmd, sa, vip) 538 int cmd; 539 struct sockaddr *sa; 540 void *vip; 541{ 542 struct ip *ip = vip; 543 struct udphdr *uh; 544 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify; 545 struct in_addr faddr; 546 struct inpcb *inp; 547 548 faddr = ((struct sockaddr_in *)sa)->sin_addr; 549 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 550 return; 551 552 /* 553 * Redirects don't need to be handled up here. 554 */ 555 if (PRC_IS_REDIRECT(cmd)) 556 return; 557 /* 558 * Hostdead is ugly because it goes linearly through all PCBs. 559 * XXX: We never get this from ICMP, otherwise it makes an 560 * excellent DoS attack on machines with many connections. 561 */ 562 if (cmd == PRC_HOSTDEAD) 563 ip = 0; 564 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 565 return; 566 if (ip) { 567 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 568 INP_INFO_RLOCK(&udbinfo); 569 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, 570 ip->ip_src, uh->uh_sport, 0, NULL); 571 if (inp != NULL) { 572 INP_LOCK(inp); 573 if (inp->inp_socket != NULL) { 574 (*notify)(inp, inetctlerrmap[cmd]); 575 } 576 INP_UNLOCK(inp); 577 } 578 INP_INFO_RUNLOCK(&udbinfo); 579 } else 580 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify); 581} 582 583static int 584udp_pcblist(SYSCTL_HANDLER_ARGS) 585{ 586 int error, i, n; 587 struct inpcb *inp, **inp_list; 588 inp_gen_t gencnt; 589 struct xinpgen xig; 590 591 /* 592 * The process of preparing the TCB list is too time-consuming and 593 * resource-intensive to repeat twice on every request. 594 */ 595 if (req->oldptr == 0) { 596 n = udbinfo.ipi_count; 597 req->oldidx = 2 * (sizeof xig) 598 + (n + n/8) * sizeof(struct xinpcb); 599 return 0; 600 } 601 602 if (req->newptr != 0) 603 return EPERM; 604 605 /* 606 * OK, now we're committed to doing something. 607 */ 608 INP_INFO_RLOCK(&udbinfo); 609 gencnt = udbinfo.ipi_gencnt; 610 n = udbinfo.ipi_count; 611 INP_INFO_RUNLOCK(&udbinfo); 612 613 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 614 + n * sizeof(struct xinpcb)); 615 if (error != 0) 616 return (error); 617 618 xig.xig_len = sizeof xig; 619 xig.xig_count = n; 620 xig.xig_gen = gencnt; 621 xig.xig_sogen = so_gencnt; 622 error = SYSCTL_OUT(req, &xig, sizeof xig); 623 if (error) 624 return error; 625 626 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 627 if (inp_list == 0) 628 return ENOMEM; 629 630 INP_INFO_RLOCK(&udbinfo); 631 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n; 632 inp = LIST_NEXT(inp, inp_list)) { 633 INP_LOCK(inp); 634 if (inp->inp_gencnt <= gencnt && 635 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 636 inp_list[i++] = inp; 637 INP_UNLOCK(inp); 638 } 639 INP_INFO_RUNLOCK(&udbinfo); 640 n = i; 641 642 error = 0; 643 for (i = 0; i < n; i++) { 644 inp = inp_list[i]; 645 INP_LOCK(inp); 646 if (inp->inp_gencnt <= gencnt) { 647 struct xinpcb xi; 648 bzero(&xi, sizeof(xi)); 649 xi.xi_len = sizeof xi; 650 /* XXX should avoid extra copy */ 651 bcopy(inp, &xi.xi_inp, sizeof *inp); 652 if (inp->inp_socket) 653 sotoxsocket(inp->inp_socket, &xi.xi_socket); 654 xi.xi_inp.inp_gencnt = inp->inp_gencnt; 655 INP_UNLOCK(inp); 656 error = SYSCTL_OUT(req, &xi, sizeof xi); 657 } else 658 INP_UNLOCK(inp); 659 } 660 if (!error) { 661 /* 662 * Give the user an updated idea of our state. 663 * If the generation differs from what we told 664 * her before, she knows that something happened 665 * while we were processing this request, and it 666 * might be necessary to retry. 667 */ 668 INP_INFO_RLOCK(&udbinfo); 669 xig.xig_gen = udbinfo.ipi_gencnt; 670 xig.xig_sogen = so_gencnt; 671 xig.xig_count = udbinfo.ipi_count; 672 INP_INFO_RUNLOCK(&udbinfo); 673 error = SYSCTL_OUT(req, &xig, sizeof xig); 674 } 675 free(inp_list, M_TEMP); 676 return error; 677} 678 679SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 680 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 681 682static int 683udp_getcred(SYSCTL_HANDLER_ARGS) 684{ 685 struct xucred xuc; 686 struct sockaddr_in addrs[2]; 687 struct inpcb *inp; 688 int error; 689 690 error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL); 691 if (error) 692 return (error); 693 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 694 if (error) 695 return (error); 696 INP_INFO_RLOCK(&udbinfo); 697 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 698 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 699 if (inp == NULL || inp->inp_socket == NULL) { 700 error = ENOENT; 701 goto out; 702 } 703 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket); 704 if (error) 705 goto out; 706 cru2x(inp->inp_socket->so_cred, &xuc); 707out: 708 INP_INFO_RUNLOCK(&udbinfo); 709 if (error == 0) 710 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 711 return (error); 712} 713 714SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 715 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 716 udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 717 718static int 719udp_output(inp, m, addr, control, td) 720 register struct inpcb *inp; 721 struct mbuf *m; 722 struct sockaddr *addr; 723 struct mbuf *control; 724 struct thread *td; 725{ 726 register struct udpiphdr *ui; 727 register int len = m->m_pkthdr.len; 728 struct in_addr faddr, laddr; 729 struct cmsghdr *cm; 730 struct sockaddr_in *sin, src; 731 int error = 0; 732 int ipflags; 733 u_short fport, lport; 734 int unlock_udbinfo; 735 736 /* 737 * udp_output() may need to temporarily bind or connect the current 738 * inpcb. As such, we don't know up front what inpcb locks we will 739 * need. Do any work to decide what is needed up front before 740 * acquiring locks. 741 */ 742 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 743 if (control) 744 m_freem(control); 745 m_freem(m); 746 return EMSGSIZE; 747 } 748 749 src.sin_addr.s_addr = INADDR_ANY; 750 if (control != NULL) { 751 /* 752 * XXX: Currently, we assume all the optional information 753 * is stored in a single mbuf. 754 */ 755 if (control->m_next) { 756 m_freem(control); 757 m_freem(m); 758 return EINVAL; 759 } 760 for (; control->m_len > 0; 761 control->m_data += CMSG_ALIGN(cm->cmsg_len), 762 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 763 cm = mtod(control, struct cmsghdr *); 764 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 || 765 cm->cmsg_len > control->m_len) { 766 error = EINVAL; 767 break; 768 } 769 if (cm->cmsg_level != IPPROTO_IP) 770 continue; 771 772 switch (cm->cmsg_type) { 773 case IP_SENDSRCADDR: 774 if (cm->cmsg_len != 775 CMSG_LEN(sizeof(struct in_addr))) { 776 error = EINVAL; 777 break; 778 } 779 bzero(&src, sizeof(src)); 780 src.sin_family = AF_INET; 781 src.sin_len = sizeof(src); 782 src.sin_port = inp->inp_lport; 783 src.sin_addr = *(struct in_addr *)CMSG_DATA(cm); 784 break; 785 default: 786 error = ENOPROTOOPT; 787 break; 788 } 789 if (error) 790 break; 791 } 792 m_freem(control); 793 } 794 if (error) { 795 m_freem(m); 796 return error; 797 } 798 799 if (src.sin_addr.s_addr != INADDR_ANY || 800 addr != NULL) { 801 INP_INFO_WLOCK(&udbinfo); 802 unlock_udbinfo = 1; 803 } else 804 unlock_udbinfo = 0; 805 INP_LOCK(inp); 806 807#ifdef MAC 808 mac_create_mbuf_from_inpcb(inp, m); 809#endif 810 811 laddr = inp->inp_laddr; 812 lport = inp->inp_lport; 813 if (src.sin_addr.s_addr != INADDR_ANY) { 814 if (lport == 0) { 815 error = EINVAL; 816 goto release; 817 } 818 error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 819 &laddr.s_addr, &lport, td->td_ucred); 820 if (error) 821 goto release; 822 } 823 824 if (addr) { 825 sin = (struct sockaddr_in *)addr; 826 if (jailed(td->td_ucred)) 827 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr); 828 if (inp->inp_faddr.s_addr != INADDR_ANY) { 829 error = EISCONN; 830 goto release; 831 } 832 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport, 833 &faddr.s_addr, &fport, NULL, td->td_ucred); 834 if (error) 835 goto release; 836 837 /* Commit the local port if newly assigned. */ 838 if (inp->inp_laddr.s_addr == INADDR_ANY && 839 inp->inp_lport == 0) { 840 /* 841 * Remember addr if jailed, to prevent rebinding. 842 */ 843 if (jailed(td->td_ucred)) 844 inp->inp_laddr = laddr; 845 inp->inp_lport = lport; 846 if (in_pcbinshash(inp) != 0) { 847 inp->inp_lport = 0; 848 error = EAGAIN; 849 goto release; 850 } 851 inp->inp_flags |= INP_ANONPORT; 852 } 853 } else { 854 faddr = inp->inp_faddr; 855 fport = inp->inp_fport; 856 if (faddr.s_addr == INADDR_ANY) { 857 error = ENOTCONN; 858 goto release; 859 } 860 } 861 862 /* 863 * Calculate data length and get a mbuf for UDP, IP, and possible 864 * link-layer headers. Immediate slide the data pointer back forward 865 * since we won't use that space at this layer. 866 */ 867 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT); 868 if (m == NULL) { 869 error = ENOBUFS; 870 goto release; 871 } 872 m->m_data += max_linkhdr; 873 m->m_len -= max_linkhdr; 874 m->m_pkthdr.len -= max_linkhdr; 875 876 /* 877 * Fill in mbuf with extended UDP header 878 * and addresses and length put into network format. 879 */ 880 ui = mtod(m, struct udpiphdr *); 881 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 882 ui->ui_pr = IPPROTO_UDP; 883 ui->ui_src = laddr; 884 ui->ui_dst = faddr; 885 ui->ui_sport = lport; 886 ui->ui_dport = fport; 887 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 888 889 /* 890 * Set the Don't Fragment bit in the IP header. 891 */ 892 if (inp->inp_flags & INP_DONTFRAG) { 893 struct ip *ip; 894 ip = (struct ip *)&ui->ui_i; 895 ip->ip_off |= IP_DF; 896 } 897 898 ipflags = 0; 899 if (inp->inp_socket->so_options & SO_DONTROUTE) 900 ipflags |= IP_ROUTETOIF; 901 if (inp->inp_socket->so_options & SO_BROADCAST) 902 ipflags |= IP_ALLOWBROADCAST; 903 if (inp->inp_flags & INP_ONESBCAST) 904 ipflags |= IP_SENDONES; 905 906 /* 907 * Set up checksum and output datagram. 908 */ 909 if (udpcksum) { 910 if (inp->inp_flags & INP_ONESBCAST) 911 faddr.s_addr = INADDR_BROADCAST; 912 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 913 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 914 m->m_pkthdr.csum_flags = CSUM_UDP; 915 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 916 } else { 917 ui->ui_sum = 0; 918 } 919 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 920 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 921 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 922 udpstat.udps_opackets++; 923 924 if (unlock_udbinfo) 925 INP_INFO_WUNLOCK(&udbinfo); 926 error = ip_output(m, inp->inp_options, NULL, ipflags, 927 inp->inp_moptions, inp); 928 INP_UNLOCK(inp); 929 return (error); 930 931release: 932 INP_UNLOCK(inp); 933 if (unlock_udbinfo) 934 INP_INFO_WUNLOCK(&udbinfo); 935 m_freem(m); 936 return (error); 937} 938 939u_long udp_sendspace = 9216; /* really max datagram size */ 940 /* 40 1K datagrams */ 941SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 942 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 943 944u_long udp_recvspace = 40 * (1024 + 945#ifdef INET6 946 sizeof(struct sockaddr_in6) 947#else 948 sizeof(struct sockaddr_in) 949#endif 950 ); 951SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 952 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 953 954static void 955udp_abort(struct socket *so) 956{ 957 struct inpcb *inp; 958 959 inp = sotoinpcb(so); 960 KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 961 INP_INFO_WLOCK(&udbinfo); 962 INP_LOCK(inp); 963 if (inp->inp_faddr.s_addr != INADDR_ANY) { 964 in_pcbdisconnect(inp); 965 inp->inp_laddr.s_addr = INADDR_ANY; 966 soisdisconnected(so); 967 } 968 INP_UNLOCK(inp); 969 INP_INFO_WUNLOCK(&udbinfo); 970} 971 972static int 973udp_attach(struct socket *so, int proto, struct thread *td) 974{ 975 struct inpcb *inp; 976 int error; 977 978 inp = sotoinpcb(so); 979 KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 980 error = soreserve(so, udp_sendspace, udp_recvspace); 981 if (error) 982 return error; 983 INP_INFO_WLOCK(&udbinfo); 984 error = in_pcballoc(so, &udbinfo); 985 if (error) { 986 INP_INFO_WUNLOCK(&udbinfo); 987 return error; 988 } 989 990 inp = (struct inpcb *)so->so_pcb; 991 INP_INFO_WUNLOCK(&udbinfo); 992 inp->inp_vflag |= INP_IPV4; 993 inp->inp_ip_ttl = ip_defttl; 994 INP_UNLOCK(inp); 995 return 0; 996} 997 998static int 999udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1000{ 1001 struct inpcb *inp; 1002 int error; 1003 1004 inp = sotoinpcb(so); 1005 KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 1006 INP_INFO_WLOCK(&udbinfo); 1007 INP_LOCK(inp); 1008 error = in_pcbbind(inp, nam, td->td_ucred); 1009 INP_UNLOCK(inp); 1010 INP_INFO_WUNLOCK(&udbinfo); 1011 return error; 1012} 1013 1014static void 1015udp_close(struct socket *so) 1016{ 1017 struct inpcb *inp; 1018 1019 inp = sotoinpcb(so); 1020 KASSERT(inp != NULL, ("udp_close: inp == NULL")); 1021 INP_INFO_WLOCK(&udbinfo); 1022 INP_LOCK(inp); 1023 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1024 in_pcbdisconnect(inp); 1025 inp->inp_laddr.s_addr = INADDR_ANY; 1026 soisdisconnected(so); 1027 } 1028 INP_UNLOCK(inp); 1029 INP_INFO_WUNLOCK(&udbinfo); 1030} 1031 1032static int 1033udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1034{ 1035 struct inpcb *inp; 1036 int error; 1037 struct sockaddr_in *sin; 1038 1039 inp = sotoinpcb(so); 1040 KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 1041 INP_INFO_WLOCK(&udbinfo); 1042 INP_LOCK(inp); 1043 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1044 INP_UNLOCK(inp); 1045 INP_INFO_WUNLOCK(&udbinfo); 1046 return EISCONN; 1047 } 1048 sin = (struct sockaddr_in *)nam; 1049 if (jailed(td->td_ucred)) 1050 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr); 1051 error = in_pcbconnect(inp, nam, td->td_ucred); 1052 if (error == 0) 1053 soisconnected(so); 1054 INP_UNLOCK(inp); 1055 INP_INFO_WUNLOCK(&udbinfo); 1056 return error; 1057} 1058 1059static void 1060udp_detach(struct socket *so) 1061{ 1062 struct inpcb *inp; 1063 1064 inp = sotoinpcb(so); 1065 KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1066 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1067 ("udp_detach: not disconnected")); 1068 INP_INFO_WLOCK(&udbinfo); 1069 INP_LOCK(inp); 1070 in_pcbdetach(inp); 1071 in_pcbfree(inp); 1072 INP_INFO_WUNLOCK(&udbinfo); 1073} 1074 1075static int 1076udp_disconnect(struct socket *so) 1077{ 1078 struct inpcb *inp; 1079 1080 inp = sotoinpcb(so); 1081 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 1082 INP_INFO_WLOCK(&udbinfo); 1083 INP_LOCK(inp); 1084 if (inp->inp_faddr.s_addr == INADDR_ANY) { 1085 INP_INFO_WUNLOCK(&udbinfo); 1086 INP_UNLOCK(inp); 1087 return ENOTCONN; 1088 } 1089 1090 in_pcbdisconnect(inp); 1091 inp->inp_laddr.s_addr = INADDR_ANY; 1092 SOCK_LOCK(so); 1093 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1094 SOCK_UNLOCK(so); 1095 INP_UNLOCK(inp); 1096 INP_INFO_WUNLOCK(&udbinfo); 1097 return 0; 1098} 1099 1100static int 1101udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1102 struct mbuf *control, struct thread *td) 1103{ 1104 struct inpcb *inp; 1105 1106 inp = sotoinpcb(so); 1107 KASSERT(inp != NULL, ("udp_send: inp == NULL")); 1108 return udp_output(inp, m, addr, control, td); 1109} 1110 1111int 1112udp_shutdown(struct socket *so) 1113{ 1114 struct inpcb *inp; 1115 1116 inp = sotoinpcb(so); 1117 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 1118 INP_LOCK(inp); 1119 socantsendmore(so); 1120 INP_UNLOCK(inp); 1121 return 0; 1122} 1123 1124/* 1125 * This is the wrapper function for in_setsockaddr. We just pass down 1126 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 1127 * here because in_setsockaddr will call malloc and might block. 1128 */ 1129static int 1130udp_sockaddr(struct socket *so, struct sockaddr **nam) 1131{ 1132 return (in_setsockaddr(so, nam, &udbinfo)); 1133} 1134 1135/* 1136 * This is the wrapper function for in_setpeeraddr. We just pass down 1137 * the pcbinfo for in_setpeeraddr to lock. 1138 */ 1139static int 1140udp_peeraddr(struct socket *so, struct sockaddr **nam) 1141{ 1142 return (in_setpeeraddr(so, nam, &udbinfo)); 1143} 1144 1145struct pr_usrreqs udp_usrreqs = { 1146 .pru_abort = udp_abort, 1147 .pru_attach = udp_attach, 1148 .pru_bind = udp_bind, 1149 .pru_connect = udp_connect, 1150 .pru_control = in_control, 1151 .pru_detach = udp_detach, 1152 .pru_disconnect = udp_disconnect, 1153 .pru_peeraddr = udp_peeraddr, 1154 .pru_send = udp_send, 1155 .pru_sosend = sosend_dgram, 1156 .pru_shutdown = udp_shutdown, 1157 .pru_sockaddr = udp_sockaddr, 1158 .pru_sosetlabel = in_pcbsosetlabel, 1159 .pru_close = udp_close, 1160};
|