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