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 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/mbuf.h> 45 #include <sys/domain.h> 46 #include <sys/proc.h> 47 #include <sys/protosw.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/sysctl.h> 51 #include <sys/syslog.h> 52 #include <sys/jail.h> 53 54 #include <vm/vm_zone.h> 55 56 #include <net/if.h> 57 #include <net/route.h> 58 59 #include <netinet/in.h> 60 #include <netinet/in_systm.h> 61 #include <netinet/ip.h> 62 #ifdef INET6 63 #include <netinet/ip6.h> 64 #endif 65 #include <netinet/in_pcb.h> 66 #include <netinet/in_var.h> 67 #include <netinet/ip_var.h> 68 #ifdef INET6 69 #include <netinet6/ip6_var.h> 70 #endif 71 #include <netinet/ip_icmp.h> 72 #include <netinet/icmp_var.h> 73 #include <netinet/udp.h> 74 #include <netinet/udp_var.h> 75 76 #ifdef IPSEC 77 #include <netinet6/ipsec.h> 78 #endif /*IPSEC*/ 79 80 #include <machine/in_cksum.h> 81 82 /* 83 * UDP protocol implementation. 84 * Per RFC 768, August, 1980. 85 */ 86 #ifndef COMPAT_42 87 static int udpcksum = 1; 88 #else 89 static int udpcksum = 0; /* XXX */ 90 #endif 91 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 92 &udpcksum, 0, ""); 93 94 int log_in_vain = 0; 95 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 96 &log_in_vain, 0, "Log all incoming UDP packets"); 97 98 static int blackhole = 0; 99 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, 100 &blackhole, 0, "Do not send port unreachables for refused connects"); 101 102 struct inpcbhead udb; /* from udp_var.h */ 103 #define udb6 udb /* for KAME src sync over BSD*'s */ 104 struct inpcbinfo udbinfo; 105 106 #ifndef UDBHASHSIZE 107 #define UDBHASHSIZE 16 108 #endif 109 110 struct udpstat udpstat; /* from udp_var.h */ 111 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, 112 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 113 114 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 115 #ifdef INET6 116 struct udp_in6 { 117 struct sockaddr_in6 uin6_sin; 118 u_char uin6_init_done : 1; 119 } udp_in6 = { 120 { sizeof(udp_in6.uin6_sin), AF_INET6 }, 121 0 122 }; 123 struct udp_ip6 { 124 struct ip6_hdr uip6_ip6; 125 u_char uip6_init_done : 1; 126 } udp_ip6; 127 #endif /* INET6 */ 128 129 static void udp_append __P((struct inpcb *last, struct ip *ip, 130 struct mbuf *n, int off)); 131 #ifdef INET6 132 static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip)); 133 #endif 134 135 static int udp_detach __P((struct socket *so)); 136 static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *, 137 struct mbuf *, struct thread *)); 138 139 void 140 udp_init() 141 { 142 LIST_INIT(&udb); 143 udbinfo.listhead = &udb; 144 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 145 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 146 &udbinfo.porthashmask); 147 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets, 148 ZONE_INTERRUPT, 0); 149 } 150 151 void 152 udp_input(m, off) 153 register struct mbuf *m; 154 int off; 155 { 156 int iphlen = off; 157 register struct ip *ip; 158 register struct udphdr *uh; 159 register struct inpcb *inp; 160 struct mbuf *opts = 0; 161 int len; 162 struct ip save_ip; 163 struct sockaddr *append_sa; 164 165 udpstat.udps_ipackets++; 166 167 /* 168 * Strip IP options, if any; should skip this, 169 * make available to user, and use on returned packets, 170 * but we don't yet have a way to check the checksum 171 * with options still present. 172 */ 173 if (iphlen > sizeof (struct ip)) { 174 ip_stripoptions(m, (struct mbuf *)0); 175 iphlen = sizeof(struct ip); 176 } 177 178 /* 179 * Get IP and UDP header together in first mbuf. 180 */ 181 ip = mtod(m, struct ip *); 182 if (m->m_len < iphlen + sizeof(struct udphdr)) { 183 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 184 udpstat.udps_hdrops++; 185 return; 186 } 187 ip = mtod(m, struct ip *); 188 } 189 uh = (struct udphdr *)((caddr_t)ip + iphlen); 190 191 /* destination port of 0 is illegal, based on RFC768. */ 192 if (uh->uh_dport == 0) 193 goto bad; 194 195 /* 196 * Make mbuf data length reflect UDP length. 197 * If not enough data to reflect UDP length, drop. 198 */ 199 len = ntohs((u_short)uh->uh_ulen); 200 if (ip->ip_len != len) { 201 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 202 udpstat.udps_badlen++; 203 goto bad; 204 } 205 m_adj(m, len - ip->ip_len); 206 /* ip->ip_len = len; */ 207 } 208 /* 209 * Save a copy of the IP header in case we want restore it 210 * for sending an ICMP error message in response. 211 */ 212 if (!blackhole) 213 save_ip = *ip; 214 215 /* 216 * Checksum extended UDP header and data. 217 */ 218 if (uh->uh_sum) { 219 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 220 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 221 uh->uh_sum = m->m_pkthdr.csum_data; 222 else 223 uh->uh_sum = in_pseudo(ip->ip_src.s_addr, 224 ip->ip_dst.s_addr, htonl((u_short)len + 225 m->m_pkthdr.csum_data + IPPROTO_UDP)); 226 uh->uh_sum ^= 0xffff; 227 } else { 228 char b[9]; 229 bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 230 bzero(((struct ipovly *)ip)->ih_x1, 9); 231 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 232 uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 233 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 234 } 235 if (uh->uh_sum) { 236 udpstat.udps_badsum++; 237 m_freem(m); 238 return; 239 } 240 } else 241 udpstat.udps_nosum++; 242 243 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 244 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 245 struct inpcb *last; 246 /* 247 * Deliver a multicast or broadcast datagram to *all* sockets 248 * for which the local and remote addresses and ports match 249 * those of the incoming datagram. This allows more than 250 * one process to receive multi/broadcasts on the same port. 251 * (This really ought to be done for unicast datagrams as 252 * well, but that would cause problems with existing 253 * applications that open both address-specific sockets and 254 * a wildcard socket listening to the same port -- they would 255 * end up receiving duplicates of every unicast datagram. 256 * Those applications open the multiple sockets to overcome an 257 * inadequacy of the UDP socket interface, but for backwards 258 * compatibility we avoid the problem here rather than 259 * fixing the interface. Maybe 4.5BSD will remedy this?) 260 */ 261 262 /* 263 * Construct sockaddr format source address. 264 */ 265 udp_in.sin_port = uh->uh_sport; 266 udp_in.sin_addr = ip->ip_src; 267 /* 268 * Locate pcb(s) for datagram. 269 * (Algorithm copied from raw_intr().) 270 */ 271 last = NULL; 272 #ifdef INET6 273 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; 274 #endif 275 LIST_FOREACH(inp, &udb, inp_list) { 276 #ifdef INET6 277 if ((inp->inp_vflag & INP_IPV4) == 0) 278 continue; 279 #endif 280 if (inp->inp_lport != uh->uh_dport) 281 continue; 282 if (inp->inp_laddr.s_addr != INADDR_ANY) { 283 if (inp->inp_laddr.s_addr != 284 ip->ip_dst.s_addr) 285 continue; 286 } 287 if (inp->inp_faddr.s_addr != INADDR_ANY) { 288 if (inp->inp_faddr.s_addr != 289 ip->ip_src.s_addr || 290 inp->inp_fport != uh->uh_sport) 291 continue; 292 } 293 294 if (last != NULL) { 295 struct mbuf *n; 296 297 #ifdef IPSEC 298 /* check AH/ESP integrity. */ 299 if (ipsec4_in_reject_so(m, last->inp_socket)) 300 ipsecstat.in_polvio++; 301 /* do not inject data to pcb */ 302 else 303 #endif /*IPSEC*/ 304 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) 305 udp_append(last, ip, n, 306 iphlen + 307 sizeof(struct udphdr)); 308 } 309 last = inp; 310 /* 311 * Don't look for additional matches if this one does 312 * not have either the SO_REUSEPORT or SO_REUSEADDR 313 * socket options set. This heuristic avoids searching 314 * through all pcbs in the common case of a non-shared 315 * port. It * assumes that an application will never 316 * clear these options after setting them. 317 */ 318 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 319 break; 320 } 321 322 if (last == NULL) { 323 /* 324 * No matching pcb found; discard datagram. 325 * (No need to send an ICMP Port Unreachable 326 * for a broadcast or multicast datgram.) 327 */ 328 udpstat.udps_noportbcast++; 329 goto bad; 330 } 331 #ifdef IPSEC 332 /* check AH/ESP integrity. */ 333 if (ipsec4_in_reject_so(m, last->inp_socket)) { 334 ipsecstat.in_polvio++; 335 goto bad; 336 } 337 #endif /*IPSEC*/ 338 udp_append(last, ip, m, iphlen + sizeof(struct udphdr)); 339 return; 340 } 341 /* 342 * Locate pcb for datagram. 343 */ 344 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 345 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 346 if (inp == NULL) { 347 if (log_in_vain) { 348 char buf[4*sizeof "123"]; 349 350 strcpy(buf, inet_ntoa(ip->ip_dst)); 351 log(LOG_INFO, 352 "Connection attempt to UDP %s:%d from %s:%d\n", 353 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 354 ntohs(uh->uh_sport)); 355 } 356 udpstat.udps_noport++; 357 if (m->m_flags & (M_BCAST | M_MCAST)) { 358 udpstat.udps_noportbcast++; 359 goto bad; 360 } 361 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 362 goto bad; 363 if (blackhole) 364 goto bad; 365 *ip = save_ip; 366 ip->ip_len += iphlen; 367 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 368 return; 369 } 370 #ifdef IPSEC 371 if (ipsec4_in_reject_so(m, inp->inp_socket)) { 372 ipsecstat.in_polvio++; 373 goto bad; 374 } 375 #endif /*IPSEC*/ 376 377 /* 378 * Construct sockaddr format source address. 379 * Stuff source address and datagram in user buffer. 380 */ 381 udp_in.sin_port = uh->uh_sport; 382 udp_in.sin_addr = ip->ip_src; 383 if (inp->inp_flags & INP_CONTROLOPTS 384 || inp->inp_socket->so_options & SO_TIMESTAMP) { 385 #ifdef INET6 386 if (inp->inp_vflag & INP_IPV6) { 387 int savedflags; 388 389 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 390 savedflags = inp->inp_flags; 391 inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 392 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m); 393 inp->inp_flags = savedflags; 394 } else 395 #endif 396 ip_savecontrol(inp, &opts, ip, m); 397 } 398 m_adj(m, iphlen + sizeof(struct udphdr)); 399 #ifdef INET6 400 if (inp->inp_vflag & INP_IPV6) { 401 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 402 append_sa = (struct sockaddr *)&udp_in6; 403 } else 404 #endif 405 append_sa = (struct sockaddr *)&udp_in; 406 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) { 407 udpstat.udps_fullsock++; 408 goto bad; 409 } 410 sorwakeup(inp->inp_socket); 411 return; 412 bad: 413 m_freem(m); 414 if (opts) 415 m_freem(opts); 416 return; 417 } 418 419 #ifdef INET6 420 static void 421 ip_2_ip6_hdr(ip6, ip) 422 struct ip6_hdr *ip6; 423 struct ip *ip; 424 { 425 bzero(ip6, sizeof(*ip6)); 426 427 ip6->ip6_vfc = IPV6_VERSION; 428 ip6->ip6_plen = ip->ip_len; 429 ip6->ip6_nxt = ip->ip_p; 430 ip6->ip6_hlim = ip->ip_ttl; 431 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = 432 IPV6_ADDR_INT32_SMP; 433 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; 434 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; 435 } 436 #endif 437 438 /* 439 * subroutine of udp_input(), mainly for source code readability. 440 * caller must properly init udp_ip6 and udp_in6 beforehand. 441 */ 442 static void 443 udp_append(last, ip, n, off) 444 struct inpcb *last; 445 struct ip *ip; 446 struct mbuf *n; 447 int off; 448 { 449 struct sockaddr *append_sa; 450 struct mbuf *opts = 0; 451 452 if (last->inp_flags & INP_CONTROLOPTS || 453 last->inp_socket->so_options & SO_TIMESTAMP) { 454 #ifdef INET6 455 if (last->inp_vflag & INP_IPV6) { 456 int savedflags; 457 458 if (udp_ip6.uip6_init_done == 0) { 459 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 460 udp_ip6.uip6_init_done = 1; 461 } 462 savedflags = last->inp_flags; 463 last->inp_flags &= ~INP_UNMAPPABLEOPTS; 464 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n); 465 last->inp_flags = savedflags; 466 } else 467 #endif 468 ip_savecontrol(last, &opts, ip, n); 469 } 470 #ifdef INET6 471 if (last->inp_vflag & INP_IPV6) { 472 if (udp_in6.uin6_init_done == 0) { 473 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 474 udp_in6.uin6_init_done = 1; 475 } 476 append_sa = (struct sockaddr *)&udp_in6.uin6_sin; 477 } else 478 #endif 479 append_sa = (struct sockaddr *)&udp_in; 480 m_adj(n, off); 481 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) { 482 m_freem(n); 483 if (opts) 484 m_freem(opts); 485 udpstat.udps_fullsock++; 486 } else 487 sorwakeup(last->inp_socket); 488 } 489 490 /* 491 * Notify a udp user of an asynchronous error; 492 * just wake up so that he can collect error status. 493 */ 494 void 495 udp_notify(inp, errno) 496 register struct inpcb *inp; 497 int errno; 498 { 499 inp->inp_socket->so_error = errno; 500 sorwakeup(inp->inp_socket); 501 sowwakeup(inp->inp_socket); 502 } 503 504 void 505 udp_ctlinput(cmd, sa, vip) 506 int cmd; 507 struct sockaddr *sa; 508 void *vip; 509 { 510 struct ip *ip = vip; 511 struct udphdr *uh; 512 void (*notify) __P((struct inpcb *, int)) = udp_notify; 513 struct in_addr faddr; 514 struct inpcb *inp; 515 int s; 516 517 faddr = ((struct sockaddr_in *)sa)->sin_addr; 518 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 519 return; 520 521 if (PRC_IS_REDIRECT(cmd)) { 522 ip = 0; 523 notify = in_rtchange; 524 } else if (cmd == PRC_HOSTDEAD) 525 ip = 0; 526 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 527 return; 528 if (ip) { 529 s = splnet(); 530 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 531 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, 532 ip->ip_src, uh->uh_sport, 0, NULL); 533 if (inp != NULL && inp->inp_socket != NULL) 534 (*notify)(inp, inetctlerrmap[cmd]); 535 splx(s); 536 } else 537 in_pcbnotifyall(&udb, faddr, inetctlerrmap[cmd], notify); 538 } 539 540 static int 541 udp_pcblist(SYSCTL_HANDLER_ARGS) 542 { 543 int error, i, n, s; 544 struct inpcb *inp, **inp_list; 545 inp_gen_t gencnt; 546 struct xinpgen xig; 547 548 /* 549 * The process of preparing the TCB list is too time-consuming and 550 * resource-intensive to repeat twice on every request. 551 */ 552 if (req->oldptr == 0) { 553 n = udbinfo.ipi_count; 554 req->oldidx = 2 * (sizeof xig) 555 + (n + n/8) * sizeof(struct xinpcb); 556 return 0; 557 } 558 559 if (req->newptr != 0) 560 return EPERM; 561 562 /* 563 * OK, now we're committed to doing something. 564 */ 565 s = splnet(); 566 gencnt = udbinfo.ipi_gencnt; 567 n = udbinfo.ipi_count; 568 splx(s); 569 570 xig.xig_len = sizeof xig; 571 xig.xig_count = n; 572 xig.xig_gen = gencnt; 573 xig.xig_sogen = so_gencnt; 574 error = SYSCTL_OUT(req, &xig, sizeof xig); 575 if (error) 576 return error; 577 578 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 579 if (inp_list == 0) 580 return ENOMEM; 581 582 s = splnet(); 583 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n; 584 inp = LIST_NEXT(inp, inp_list)) { 585 if (inp->inp_gencnt <= gencnt) { 586 if (cr_cansee(req->td->td_proc->p_ucred, 587 inp->inp_socket->so_cred)) 588 continue; 589 inp_list[i++] = inp; 590 } 591 } 592 splx(s); 593 n = i; 594 595 error = 0; 596 for (i = 0; i < n; i++) { 597 inp = inp_list[i]; 598 if (inp->inp_gencnt <= gencnt) { 599 struct xinpcb xi; 600 xi.xi_len = sizeof xi; 601 /* XXX should avoid extra copy */ 602 bcopy(inp, &xi.xi_inp, sizeof *inp); 603 if (inp->inp_socket) 604 sotoxsocket(inp->inp_socket, &xi.xi_socket); 605 error = SYSCTL_OUT(req, &xi, sizeof xi); 606 } 607 } 608 if (!error) { 609 /* 610 * Give the user an updated idea of our state. 611 * If the generation differs from what we told 612 * her before, she knows that something happened 613 * while we were processing this request, and it 614 * might be necessary to retry. 615 */ 616 s = splnet(); 617 xig.xig_gen = udbinfo.ipi_gencnt; 618 xig.xig_sogen = so_gencnt; 619 xig.xig_count = udbinfo.ipi_count; 620 splx(s); 621 error = SYSCTL_OUT(req, &xig, sizeof xig); 622 } 623 free(inp_list, M_TEMP); 624 return error; 625 } 626 627 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 628 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 629 630 static int 631 udp_getcred(SYSCTL_HANDLER_ARGS) 632 { 633 struct xucred xuc; 634 struct sockaddr_in addrs[2]; 635 struct inpcb *inp; 636 int error, s; 637 638 error = suser_xxx(0, req->td->td_proc, PRISON_ROOT); 639 if (error) 640 return (error); 641 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 642 if (error) 643 return (error); 644 s = splnet(); 645 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 646 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 647 if (inp == NULL || inp->inp_socket == NULL) { 648 error = ENOENT; 649 goto out; 650 } 651 error = cr_cansee(req->td->td_proc->p_ucred, inp->inp_socket->so_cred); 652 if (error) 653 goto out; 654 bzero(&xuc, sizeof(xuc)); 655 xuc.cr_uid = inp->inp_socket->so_cred->cr_uid; 656 xuc.cr_ngroups = inp->inp_socket->so_cred->cr_ngroups; 657 bcopy(inp->inp_socket->so_cred->cr_groups, xuc.cr_groups, 658 sizeof(xuc.cr_groups)); 659 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 660 out: 661 splx(s); 662 return (error); 663 } 664 665 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 666 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 667 udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 668 669 static int 670 udp_output(inp, m, addr, control, td) 671 register struct inpcb *inp; 672 struct mbuf *m; 673 struct sockaddr *addr; 674 struct mbuf *control; 675 struct thread *td; 676 { 677 register struct udpiphdr *ui; 678 register int len = m->m_pkthdr.len; 679 struct in_addr laddr; 680 struct sockaddr_in *sin; 681 int s = 0, error = 0; 682 683 if (control) 684 m_freem(control); /* XXX */ 685 686 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 687 error = EMSGSIZE; 688 goto release; 689 } 690 691 if (addr) { 692 sin = (struct sockaddr_in *)addr; 693 if (td && jailed(td->td_proc->p_ucred)) 694 prison_remote_ip(td->td_proc->p_ucred, 0, &sin->sin_addr.s_addr); 695 laddr = inp->inp_laddr; 696 if (inp->inp_faddr.s_addr != INADDR_ANY) { 697 error = EISCONN; 698 goto release; 699 } 700 /* 701 * Must block input while temporarily connected. 702 */ 703 s = splnet(); 704 error = in_pcbconnect(inp, addr, td); 705 if (error) { 706 splx(s); 707 goto release; 708 } 709 } else { 710 if (inp->inp_faddr.s_addr == INADDR_ANY) { 711 error = ENOTCONN; 712 goto release; 713 } 714 } 715 /* 716 * Calculate data length and get a mbuf 717 * for UDP and IP headers. 718 */ 719 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 720 if (m == 0) { 721 error = ENOBUFS; 722 if (addr) 723 splx(s); 724 goto release; 725 } 726 727 /* 728 * Fill in mbuf with extended UDP header 729 * and addresses and length put into network format. 730 */ 731 ui = mtod(m, struct udpiphdr *); 732 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 733 ui->ui_pr = IPPROTO_UDP; 734 ui->ui_src = inp->inp_laddr; 735 ui->ui_dst = inp->inp_faddr; 736 ui->ui_sport = inp->inp_lport; 737 ui->ui_dport = inp->inp_fport; 738 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 739 740 /* 741 * Set up checksum and output datagram. 742 */ 743 if (udpcksum) { 744 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, 745 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 746 m->m_pkthdr.csum_flags = CSUM_UDP; 747 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 748 } else { 749 ui->ui_sum = 0; 750 } 751 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 752 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 753 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 754 udpstat.udps_opackets++; 755 756 #ifdef IPSEC 757 if (ipsec_setsocket(m, inp->inp_socket) != 0) { 758 error = ENOBUFS; 759 goto release; 760 } 761 #endif /*IPSEC*/ 762 error = ip_output(m, inp->inp_options, &inp->inp_route, 763 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)), 764 inp->inp_moptions); 765 766 if (addr) { 767 in_pcbdisconnect(inp); 768 inp->inp_laddr = laddr; /* XXX rehash? */ 769 splx(s); 770 } 771 return (error); 772 773 release: 774 m_freem(m); 775 return (error); 776 } 777 778 u_long udp_sendspace = 9216; /* really max datagram size */ 779 /* 40 1K datagrams */ 780 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 781 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 782 783 u_long udp_recvspace = 40 * (1024 + 784 #ifdef INET6 785 sizeof(struct sockaddr_in6) 786 #else 787 sizeof(struct sockaddr_in) 788 #endif 789 ); 790 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 791 &udp_recvspace, 0, "Maximum incoming UDP datagram size"); 792 793 static int 794 udp_abort(struct socket *so) 795 { 796 struct inpcb *inp; 797 int s; 798 799 inp = sotoinpcb(so); 800 if (inp == 0) 801 return EINVAL; /* ??? possible? panic instead? */ 802 soisdisconnected(so); 803 s = splnet(); 804 in_pcbdetach(inp); 805 splx(s); 806 return 0; 807 } 808 809 static int 810 udp_attach(struct socket *so, int proto, struct thread *td) 811 { 812 struct inpcb *inp; 813 int s, error; 814 815 inp = sotoinpcb(so); 816 if (inp != 0) 817 return EINVAL; 818 819 error = soreserve(so, udp_sendspace, udp_recvspace); 820 if (error) 821 return error; 822 s = splnet(); 823 error = in_pcballoc(so, &udbinfo, td); 824 splx(s); 825 if (error) 826 return error; 827 828 inp = (struct inpcb *)so->so_pcb; 829 inp->inp_vflag |= INP_IPV4; 830 inp->inp_ip_ttl = ip_defttl; 831 return 0; 832 } 833 834 static int 835 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 836 { 837 struct inpcb *inp; 838 int s, error; 839 840 inp = sotoinpcb(so); 841 if (inp == 0) 842 return EINVAL; 843 s = splnet(); 844 error = in_pcbbind(inp, nam, td); 845 splx(s); 846 return error; 847 } 848 849 static int 850 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 851 { 852 struct inpcb *inp; 853 int s, error; 854 struct sockaddr_in *sin; 855 856 inp = sotoinpcb(so); 857 if (inp == 0) 858 return EINVAL; 859 if (inp->inp_faddr.s_addr != INADDR_ANY) 860 return EISCONN; 861 s = splnet(); 862 sin = (struct sockaddr_in *)nam; 863 if (td && jailed(td->td_proc->p_ucred)) 864 prison_remote_ip(td->td_proc->p_ucred, 0, &sin->sin_addr.s_addr); 865 error = in_pcbconnect(inp, nam, td); 866 splx(s); 867 if (error == 0) 868 soisconnected(so); 869 return error; 870 } 871 872 static int 873 udp_detach(struct socket *so) 874 { 875 struct inpcb *inp; 876 int s; 877 878 inp = sotoinpcb(so); 879 if (inp == 0) 880 return EINVAL; 881 s = splnet(); 882 in_pcbdetach(inp); 883 splx(s); 884 return 0; 885 } 886 887 static int 888 udp_disconnect(struct socket *so) 889 { 890 struct inpcb *inp; 891 int s; 892 893 inp = sotoinpcb(so); 894 if (inp == 0) 895 return EINVAL; 896 if (inp->inp_faddr.s_addr == INADDR_ANY) 897 return ENOTCONN; 898 899 s = splnet(); 900 in_pcbdisconnect(inp); 901 inp->inp_laddr.s_addr = INADDR_ANY; 902 splx(s); 903 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 904 return 0; 905 } 906 907 static int 908 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 909 struct mbuf *control, struct thread *td) 910 { 911 struct inpcb *inp; 912 913 inp = sotoinpcb(so); 914 if (inp == 0) { 915 m_freem(m); 916 return EINVAL; 917 } 918 return udp_output(inp, m, addr, control, td); 919 } 920 921 int 922 udp_shutdown(struct socket *so) 923 { 924 struct inpcb *inp; 925 926 inp = sotoinpcb(so); 927 if (inp == 0) 928 return EINVAL; 929 socantsendmore(so); 930 return 0; 931 } 932 933 struct pr_usrreqs udp_usrreqs = { 934 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 935 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 936 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 937 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 938 in_setsockaddr, sosend, soreceive, sopoll 939 }; 940