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