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_RD, 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 proc *)); 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, proto) 153 register struct mbuf *m; 154 int off, proto; 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 bzero(((struct ipovly *)ip)->ih_x1, 9); 229 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 230 uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 231 } 232 if (uh->uh_sum) { 233 udpstat.udps_badsum++; 234 m_freem(m); 235 return; 236 } 237 } else 238 udpstat.udps_nosum++; 239 240 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 241 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 242 struct inpcb *last; 243 /* 244 * Deliver a multicast or broadcast datagram to *all* sockets 245 * for which the local and remote addresses and ports match 246 * those of the incoming datagram. This allows more than 247 * one process to receive multi/broadcasts on the same port. 248 * (This really ought to be done for unicast datagrams as 249 * well, but that would cause problems with existing 250 * applications that open both address-specific sockets and 251 * a wildcard socket listening to the same port -- they would 252 * end up receiving duplicates of every unicast datagram. 253 * Those applications open the multiple sockets to overcome an 254 * inadequacy of the UDP socket interface, but for backwards 255 * compatibility we avoid the problem here rather than 256 * fixing the interface. Maybe 4.5BSD will remedy this?) 257 */ 258 259 /* 260 * Construct sockaddr format source address. 261 */ 262 udp_in.sin_port = uh->uh_sport; 263 udp_in.sin_addr = ip->ip_src; 264 /* 265 * Locate pcb(s) for datagram. 266 * (Algorithm copied from raw_intr().) 267 */ 268 last = NULL; 269 #ifdef INET6 270 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; 271 #endif 272 LIST_FOREACH(inp, &udb, inp_list) { 273 #ifdef INET6 274 if ((inp->inp_vflag & INP_IPV4) == 0) 275 continue; 276 #endif 277 if (inp->inp_lport != uh->uh_dport) 278 continue; 279 if (inp->inp_laddr.s_addr != INADDR_ANY) { 280 if (inp->inp_laddr.s_addr != 281 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 291 if (last != NULL) { 292 struct mbuf *n; 293 294 #ifdef IPSEC 295 /* check AH/ESP integrity. */ 296 if (ipsec4_in_reject_so(m, last->inp_socket)) 297 ipsecstat.in_polvio++; 298 /* do not inject data to pcb */ 299 else 300 #endif /*IPSEC*/ 301 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) 302 udp_append(last, ip, n, 303 iphlen + 304 sizeof(struct udphdr)); 305 } 306 last = inp; 307 /* 308 * Don't look for additional matches if this one does 309 * not have either the SO_REUSEPORT or SO_REUSEADDR 310 * socket options set. This heuristic avoids searching 311 * through all pcbs in the common case of a non-shared 312 * port. It * assumes that an application will never 313 * clear these options after setting them. 314 */ 315 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 316 break; 317 } 318 319 if (last == NULL) { 320 /* 321 * No matching pcb found; discard datagram. 322 * (No need to send an ICMP Port Unreachable 323 * for a broadcast or multicast datgram.) 324 */ 325 udpstat.udps_noportbcast++; 326 goto bad; 327 } 328 #ifdef IPSEC 329 /* check AH/ESP integrity. */ 330 if (ipsec4_in_reject_so(m, last->inp_socket)) { 331 ipsecstat.in_polvio++; 332 goto bad; 333 } 334 #endif /*IPSEC*/ 335 udp_append(last, ip, m, iphlen + sizeof(struct udphdr)); 336 return; 337 } 338 /* 339 * Locate pcb for datagram. 340 */ 341 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 342 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 343 if (inp == NULL) { 344 if (log_in_vain) { 345 char buf[4*sizeof "123"]; 346 347 strcpy(buf, inet_ntoa(ip->ip_dst)); 348 log(LOG_INFO, 349 "Connection attempt to UDP %s:%d from %s:%d\n", 350 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 351 ntohs(uh->uh_sport)); 352 } 353 udpstat.udps_noport++; 354 if (m->m_flags & (M_BCAST | M_MCAST)) { 355 udpstat.udps_noportbcast++; 356 goto bad; 357 } 358 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 359 goto bad; 360 if (blackhole) 361 goto bad; 362 *ip = save_ip; 363 ip->ip_len += iphlen; 364 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 365 return; 366 } 367 #ifdef IPSEC 368 if (ipsec4_in_reject_so(m, inp->inp_socket)) { 369 ipsecstat.in_polvio++; 370 goto bad; 371 } 372 #endif /*IPSEC*/ 373 374 /* 375 * Construct sockaddr format source address. 376 * Stuff source address and datagram in user buffer. 377 */ 378 udp_in.sin_port = uh->uh_sport; 379 udp_in.sin_addr = ip->ip_src; 380 if (inp->inp_flags & INP_CONTROLOPTS 381 || inp->inp_socket->so_options & SO_TIMESTAMP) { 382 #ifdef INET6 383 if (inp->inp_vflag & INP_IPV6) { 384 int savedflags; 385 386 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 387 savedflags = inp->inp_flags; 388 inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 389 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m); 390 inp->inp_flags = savedflags; 391 } else 392 #endif 393 ip_savecontrol(inp, &opts, ip, m); 394 } 395 iphlen += sizeof(struct udphdr); 396 m_adj(m, iphlen); 397 #ifdef INET6 398 if (inp->inp_vflag & INP_IPV6) { 399 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 400 append_sa = (struct sockaddr *)&udp_in6; 401 } else 402 #endif 403 append_sa = (struct sockaddr *)&udp_in; 404 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) { 405 udpstat.udps_fullsock++; 406 goto bad; 407 } 408 sorwakeup(inp->inp_socket); 409 return; 410 bad: 411 m_freem(m); 412 if (opts) 413 m_freem(opts); 414 return; 415 } 416 417 #ifdef INET6 418 static void 419 ip_2_ip6_hdr(ip6, ip) 420 struct ip6_hdr *ip6; 421 struct ip *ip; 422 { 423 bzero(ip6, sizeof(*ip6)); 424 425 ip6->ip6_vfc = IPV6_VERSION; 426 ip6->ip6_plen = ip->ip_len; 427 ip6->ip6_nxt = ip->ip_p; 428 ip6->ip6_hlim = ip->ip_ttl; 429 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = 430 IPV6_ADDR_INT32_SMP; 431 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; 432 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; 433 } 434 #endif 435 436 /* 437 * subroutine of udp_input(), mainly for source code readability. 438 * caller must properly init udp_ip6 and udp_in6 beforehand. 439 */ 440 static void 441 udp_append(last, ip, n, off) 442 struct inpcb *last; 443 struct ip *ip; 444 struct mbuf *n; 445 int off; 446 { 447 struct sockaddr *append_sa; 448 struct mbuf *opts = 0; 449 450 if (last->inp_flags & INP_CONTROLOPTS || 451 last->inp_socket->so_options & SO_TIMESTAMP) { 452 #ifdef INET6 453 if (last->inp_vflag & INP_IPV6) { 454 int savedflags; 455 456 if (udp_ip6.uip6_init_done == 0) { 457 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 458 udp_ip6.uip6_init_done = 1; 459 } 460 savedflags = last->inp_flags; 461 last->inp_flags &= ~INP_UNMAPPABLEOPTS; 462 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n); 463 last->inp_flags = savedflags; 464 } else 465 #endif 466 ip_savecontrol(last, &opts, ip, n); 467 } 468 #ifdef INET6 469 if (last->inp_vflag & INP_IPV6) { 470 if (udp_in6.uin6_init_done == 0) { 471 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 472 udp_in6.uin6_init_done = 1; 473 } 474 append_sa = (struct sockaddr *)&udp_in6.uin6_sin; 475 } else 476 #endif 477 append_sa = (struct sockaddr *)&udp_in; 478 m_adj(n, off); 479 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) { 480 m_freem(n); 481 if (opts) 482 m_freem(opts); 483 udpstat.udps_fullsock++; 484 } else 485 sorwakeup(last->inp_socket); 486 } 487 488 /* 489 * Notify a udp user of an asynchronous error; 490 * just wake up so that he can collect error status. 491 */ 492 void 493 udp_notify(inp, errno) 494 register struct inpcb *inp; 495 int errno; 496 { 497 inp->inp_socket->so_error = errno; 498 sorwakeup(inp->inp_socket); 499 sowwakeup(inp->inp_socket); 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 void (*notify) __P((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 = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, 530 ip->ip_src, uh->uh_sport, 0, NULL); 531 if (inp != NULL && inp->inp_socket != NULL) 532 (*notify)(inp, inetctlerrmap[cmd]); 533 splx(s); 534 } else 535 in_pcbnotifyall(&udb, faddr, inetctlerrmap[cmd], notify); 536 } 537 538 static int 539 udp_pcblist(SYSCTL_HANDLER_ARGS) 540 { 541 int error, i, n, s; 542 struct inpcb *inp, **inp_list; 543 inp_gen_t gencnt; 544 struct xinpgen xig; 545 546 /* 547 * The process of preparing the TCB list is too time-consuming and 548 * resource-intensive to repeat twice on every request. 549 */ 550 if (req->oldptr == 0) { 551 n = udbinfo.ipi_count; 552 req->oldidx = 2 * (sizeof xig) 553 + (n + n/8) * sizeof(struct xinpcb); 554 return 0; 555 } 556 557 if (req->newptr != 0) 558 return EPERM; 559 560 /* 561 * OK, now we're committed to doing something. 562 */ 563 s = splnet(); 564 gencnt = udbinfo.ipi_gencnt; 565 n = udbinfo.ipi_count; 566 splx(s); 567 568 xig.xig_len = sizeof xig; 569 xig.xig_count = n; 570 xig.xig_gen = gencnt; 571 xig.xig_sogen = so_gencnt; 572 error = SYSCTL_OUT(req, &xig, sizeof xig); 573 if (error) 574 return error; 575 576 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 577 if (inp_list == 0) 578 return ENOMEM; 579 580 s = splnet(); 581 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n; 582 inp = LIST_NEXT(inp, inp_list)) { 583 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp)) 584 inp_list[i++] = inp; 585 } 586 splx(s); 587 n = i; 588 589 error = 0; 590 for (i = 0; i < n; i++) { 591 inp = inp_list[i]; 592 if (inp->inp_gencnt <= gencnt) { 593 struct xinpcb xi; 594 xi.xi_len = sizeof xi; 595 /* XXX should avoid extra copy */ 596 bcopy(inp, &xi.xi_inp, sizeof *inp); 597 if (inp->inp_socket) 598 sotoxsocket(inp->inp_socket, &xi.xi_socket); 599 error = SYSCTL_OUT(req, &xi, sizeof xi); 600 } 601 } 602 if (!error) { 603 /* 604 * Give the user an updated idea of our state. 605 * If the generation differs from what we told 606 * her before, she knows that something happened 607 * while we were processing this request, and it 608 * might be necessary to retry. 609 */ 610 s = splnet(); 611 xig.xig_gen = udbinfo.ipi_gencnt; 612 xig.xig_sogen = so_gencnt; 613 xig.xig_count = udbinfo.ipi_count; 614 splx(s); 615 error = SYSCTL_OUT(req, &xig, sizeof xig); 616 } 617 free(inp_list, M_TEMP); 618 return error; 619 } 620 621 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 622 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 623 624 static int 625 udp_getcred(SYSCTL_HANDLER_ARGS) 626 { 627 struct xucred xuc; 628 struct sockaddr_in addrs[2]; 629 struct inpcb *inp; 630 int error, s; 631 632 error = suser(req->p); 633 if (error) 634 return (error); 635 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 636 if (error) 637 return (error); 638 s = splnet(); 639 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 640 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 641 if (inp == NULL || inp->inp_socket == NULL) { 642 error = ENOENT; 643 goto out; 644 } 645 bzero(&xuc, sizeof(xuc)); 646 xuc.cr_uid = inp->inp_socket->so_cred->cr_uid; 647 xuc.cr_ngroups = inp->inp_socket->so_cred->cr_ngroups; 648 bcopy(inp->inp_socket->so_cred->cr_groups, xuc.cr_groups, 649 sizeof(xuc.cr_groups)); 650 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 651 out: 652 splx(s); 653 return (error); 654 } 655 656 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 657 0, 0, udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 658 659 static int 660 udp_output(inp, m, addr, control, p) 661 register struct inpcb *inp; 662 struct mbuf *m; 663 struct sockaddr *addr; 664 struct mbuf *control; 665 struct proc *p; 666 { 667 register struct udpiphdr *ui; 668 register int len = m->m_pkthdr.len; 669 struct in_addr laddr; 670 struct sockaddr_in *sin; 671 int s = 0, error = 0; 672 673 if (control) 674 m_freem(control); /* XXX */ 675 676 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 677 error = EMSGSIZE; 678 goto release; 679 } 680 681 if (addr) { 682 sin = (struct sockaddr_in *)addr; 683 if (p && jailed(p->p_ucred)) 684 prison_remote_ip(p->p_ucred, 0, &sin->sin_addr.s_addr); 685 laddr = inp->inp_laddr; 686 if (inp->inp_faddr.s_addr != INADDR_ANY) { 687 error = EISCONN; 688 goto release; 689 } 690 /* 691 * Must block input while temporarily connected. 692 */ 693 s = splnet(); 694 error = in_pcbconnect(inp, addr, p); 695 if (error) { 696 splx(s); 697 goto release; 698 } 699 } else { 700 if (inp->inp_faddr.s_addr == INADDR_ANY) { 701 error = ENOTCONN; 702 goto release; 703 } 704 } 705 /* 706 * Calculate data length and get a mbuf 707 * for UDP and IP headers. 708 */ 709 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 710 if (m == 0) { 711 error = ENOBUFS; 712 if (addr) 713 splx(s); 714 goto release; 715 } 716 717 /* 718 * Fill in mbuf with extended UDP header 719 * and addresses and length put into network format. 720 */ 721 ui = mtod(m, struct udpiphdr *); 722 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 723 ui->ui_pr = IPPROTO_UDP; 724 ui->ui_src = inp->inp_laddr; 725 ui->ui_dst = inp->inp_faddr; 726 ui->ui_sport = inp->inp_lport; 727 ui->ui_dport = inp->inp_fport; 728 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 729 730 /* 731 * Set up checksum and output datagram. 732 */ 733 if (udpcksum) { 734 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, 735 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 736 m->m_pkthdr.csum_flags = CSUM_UDP; 737 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 738 } else { 739 ui->ui_sum = 0; 740 } 741 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 742 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 743 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 744 udpstat.udps_opackets++; 745 746 #ifdef IPSEC 747 ipsec_setsocket(m, inp->inp_socket); 748 #endif /*IPSEC*/ 749 error = ip_output(m, inp->inp_options, &inp->inp_route, 750 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)), 751 inp->inp_moptions); 752 753 if (addr) { 754 in_pcbdisconnect(inp); 755 inp->inp_laddr = laddr; /* XXX rehash? */ 756 splx(s); 757 } 758 return (error); 759 760 release: 761 m_freem(m); 762 return (error); 763 } 764 765 u_long udp_sendspace = 9216; /* really max datagram size */ 766 /* 40 1K datagrams */ 767 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 768 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 769 770 u_long udp_recvspace = 40 * (1024 + 771 #ifdef INET6 772 sizeof(struct sockaddr_in6) 773 #else 774 sizeof(struct sockaddr_in) 775 #endif 776 ); 777 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 778 &udp_recvspace, 0, "Maximum incoming UDP datagram size"); 779 780 static int 781 udp_abort(struct socket *so) 782 { 783 struct inpcb *inp; 784 int s; 785 786 inp = sotoinpcb(so); 787 if (inp == 0) 788 return EINVAL; /* ??? possible? panic instead? */ 789 soisdisconnected(so); 790 s = splnet(); 791 in_pcbdetach(inp); 792 splx(s); 793 return 0; 794 } 795 796 static int 797 udp_attach(struct socket *so, int proto, struct proc *p) 798 { 799 struct inpcb *inp; 800 int s, error; 801 802 inp = sotoinpcb(so); 803 if (inp != 0) 804 return EINVAL; 805 806 error = soreserve(so, udp_sendspace, udp_recvspace); 807 if (error) 808 return error; 809 s = splnet(); 810 error = in_pcballoc(so, &udbinfo, p); 811 splx(s); 812 if (error) 813 return error; 814 815 inp = (struct inpcb *)so->so_pcb; 816 inp->inp_vflag |= INP_IPV4; 817 inp->inp_ip_ttl = ip_defttl; 818 #ifdef IPSEC 819 error = ipsec_init_policy(so, &inp->inp_sp); 820 if (error != 0) { 821 in_pcbdetach(inp); 822 return error; 823 } 824 #endif /*IPSEC*/ 825 return 0; 826 } 827 828 static int 829 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 830 { 831 struct inpcb *inp; 832 int s, error; 833 834 inp = sotoinpcb(so); 835 if (inp == 0) 836 return EINVAL; 837 s = splnet(); 838 error = in_pcbbind(inp, nam, p); 839 splx(s); 840 return error; 841 } 842 843 static int 844 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 845 { 846 struct inpcb *inp; 847 int s, error; 848 struct sockaddr_in *sin; 849 850 inp = sotoinpcb(so); 851 if (inp == 0) 852 return EINVAL; 853 if (inp->inp_faddr.s_addr != INADDR_ANY) 854 return EISCONN; 855 s = splnet(); 856 sin = (struct sockaddr_in *)nam; 857 if (p && jailed(p->p_ucred)) 858 prison_remote_ip(p->p_ucred, 0, &sin->sin_addr.s_addr); 859 error = in_pcbconnect(inp, nam, p); 860 splx(s); 861 if (error == 0) 862 soisconnected(so); 863 return error; 864 } 865 866 static int 867 udp_detach(struct socket *so) 868 { 869 struct inpcb *inp; 870 int s; 871 872 inp = sotoinpcb(so); 873 if (inp == 0) 874 return EINVAL; 875 s = splnet(); 876 in_pcbdetach(inp); 877 splx(s); 878 return 0; 879 } 880 881 static int 882 udp_disconnect(struct socket *so) 883 { 884 struct inpcb *inp; 885 int s; 886 887 inp = sotoinpcb(so); 888 if (inp == 0) 889 return EINVAL; 890 if (inp->inp_faddr.s_addr == INADDR_ANY) 891 return ENOTCONN; 892 893 s = splnet(); 894 in_pcbdisconnect(inp); 895 inp->inp_laddr.s_addr = INADDR_ANY; 896 splx(s); 897 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 898 return 0; 899 } 900 901 static int 902 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 903 struct mbuf *control, struct proc *p) 904 { 905 struct inpcb *inp; 906 907 inp = sotoinpcb(so); 908 if (inp == 0) { 909 m_freem(m); 910 return EINVAL; 911 } 912 return udp_output(inp, m, addr, control, p); 913 } 914 915 int 916 udp_shutdown(struct socket *so) 917 { 918 struct inpcb *inp; 919 920 inp = sotoinpcb(so); 921 if (inp == 0) 922 return EINVAL; 923 socantsendmore(so); 924 return 0; 925 } 926 927 struct pr_usrreqs udp_usrreqs = { 928 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 929 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 930 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 931 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 932 in_setsockaddr, sosend, soreceive, sopoll 933 }; 934