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