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