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