1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3 * The Regents of the University of California. 4 * Copyright (c) 2008 Robert N. M. Watson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 4. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_ipfw.h" 38 #include "opt_inet6.h" 39 #include "opt_ipsec.h" 40 #include "opt_mac.h" 41 42 #include <sys/param.h> 43 #include <sys/domain.h> 44 #include <sys/eventhandler.h> 45 #include <sys/jail.h> 46 #include <sys/kernel.h> 47 #include <sys/lock.h> 48 #include <sys/malloc.h> 49 #include <sys/mbuf.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/protosw.h> 53 #include <sys/signalvar.h> 54 #include <sys/socket.h> 55 #include <sys/socketvar.h> 56 #include <sys/sx.h> 57 #include <sys/sysctl.h> 58 #include <sys/syslog.h> 59 #include <sys/systm.h> 60 #include <sys/vimage.h> 61 62 #include <vm/uma.h> 63 64 #include <net/if.h> 65 #include <net/route.h> 66 67 #include <netinet/in.h> 68 #include <netinet/in_pcb.h> 69 #include <netinet/in_systm.h> 70 #include <netinet/in_var.h> 71 #include <netinet/ip.h> 72 #ifdef INET6 73 #include <netinet/ip6.h> 74 #endif 75 #include <netinet/ip_icmp.h> 76 #include <netinet/icmp_var.h> 77 #include <netinet/ip_var.h> 78 #include <netinet/ip_options.h> 79 #ifdef INET6 80 #include <netinet6/ip6_var.h> 81 #endif 82 #include <netinet/udp.h> 83 #include <netinet/udp_var.h> 84 #include <netinet/vinet.h> 85 86 #ifdef IPSEC 87 #include <netipsec/ipsec.h> 88 #endif 89 90 #include <machine/in_cksum.h> 91 92 #include <security/mac/mac_framework.h> 93 94 /* 95 * UDP protocol implementation. 96 * Per RFC 768, August, 1980. 97 */ 98 99 #ifdef VIMAGE_GLOBALS 100 int udp_blackhole; 101 #endif 102 103 /* 104 * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums 105 * removes the only data integrity mechanism for packets and malformed 106 * packets that would otherwise be discarded due to bad checksums, and may 107 * cause problems (especially for NFS data blocks). 108 */ 109 static int udp_cksum = 1; 110 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum, 111 0, "compute udp checksum"); 112 113 int udp_log_in_vain = 0; 114 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 115 &udp_log_in_vain, 0, "Log all incoming UDP packets"); 116 117 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_udp, OID_AUTO, blackhole, 118 CTLFLAG_RW, udp_blackhole, 0, 119 "Do not send port unreachables for refused connects"); 120 121 u_long udp_sendspace = 9216; /* really max datagram size */ 122 /* 40 1K datagrams */ 123 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 124 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 125 126 u_long udp_recvspace = 40 * (1024 + 127 #ifdef INET6 128 sizeof(struct sockaddr_in6) 129 #else 130 sizeof(struct sockaddr_in) 131 #endif 132 ); 133 134 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 135 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 136 137 #ifdef VIMAGE_GLOBALS 138 struct inpcbhead udb; /* from udp_var.h */ 139 struct inpcbinfo udbinfo; 140 struct udpstat udpstat; /* from udp_var.h */ 141 #endif 142 143 #ifndef UDBHASHSIZE 144 #define UDBHASHSIZE 128 145 #endif 146 147 SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_udp, UDPCTL_STATS, stats, 148 CTLFLAG_RW, udpstat, udpstat, 149 "UDP statistics (struct udpstat, netinet/udp_var.h)"); 150 151 static void udp_detach(struct socket *so); 152 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 153 struct mbuf *, struct thread *); 154 155 static void 156 udp_zone_change(void *tag) 157 { 158 159 uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 160 } 161 162 static int 163 udp_inpcb_init(void *mem, int size, int flags) 164 { 165 struct inpcb *inp; 166 167 inp = mem; 168 INP_LOCK_INIT(inp, "inp", "udpinp"); 169 return (0); 170 } 171 172 void 173 udp_init(void) 174 { 175 INIT_VNET_INET(curvnet); 176 177 V_udp_blackhole = 0; 178 179 INP_INFO_LOCK_INIT(&V_udbinfo, "udp"); 180 LIST_INIT(&V_udb); 181 V_udbinfo.ipi_listhead = &V_udb; 182 V_udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB, 183 &V_udbinfo.ipi_hashmask); 184 V_udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB, 185 &V_udbinfo.ipi_porthashmask); 186 V_udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL, 187 NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 188 uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 189 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL, 190 EVENTHANDLER_PRI_ANY); 191 } 192 193 /* 194 * Subroutine of udp_input(), which appends the provided mbuf chain to the 195 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 196 * contains the source address. If the socket ends up being an IPv6 socket, 197 * udp_append() will convert to a sockaddr_in6 before passing the address 198 * into the socket code. 199 */ 200 static void 201 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off, 202 struct sockaddr_in *udp_in) 203 { 204 struct sockaddr *append_sa; 205 struct socket *so; 206 struct mbuf *opts = 0; 207 #ifdef INET6 208 struct sockaddr_in6 udp_in6; 209 #endif 210 211 INP_RLOCK_ASSERT(inp); 212 213 #ifdef IPSEC 214 /* Check AH/ESP integrity. */ 215 if (ipsec4_in_reject(n, inp)) { 216 INIT_VNET_IPSEC(curvnet); 217 m_freem(n); 218 V_ipsec4stat.in_polvio++; 219 return; 220 } 221 #endif /* IPSEC */ 222 #ifdef MAC 223 if (mac_inpcb_check_deliver(inp, n) != 0) { 224 m_freem(n); 225 return; 226 } 227 #endif 228 if (inp->inp_flags & INP_CONTROLOPTS || 229 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 230 #ifdef INET6 231 if (inp->inp_vflag & INP_IPV6) 232 (void)ip6_savecontrol_v4(inp, n, &opts, NULL); 233 else 234 #endif 235 ip_savecontrol(inp, &opts, ip, n); 236 } 237 #ifdef INET6 238 if (inp->inp_vflag & INP_IPV6) { 239 bzero(&udp_in6, sizeof(udp_in6)); 240 udp_in6.sin6_len = sizeof(udp_in6); 241 udp_in6.sin6_family = AF_INET6; 242 in6_sin_2_v4mapsin6(udp_in, &udp_in6); 243 append_sa = (struct sockaddr *)&udp_in6; 244 } else 245 #endif 246 append_sa = (struct sockaddr *)udp_in; 247 m_adj(n, off); 248 249 so = inp->inp_socket; 250 SOCKBUF_LOCK(&so->so_rcv); 251 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 252 INIT_VNET_INET(so->so_vnet); 253 SOCKBUF_UNLOCK(&so->so_rcv); 254 m_freem(n); 255 if (opts) 256 m_freem(opts); 257 UDPSTAT_INC(udps_fullsock); 258 } else 259 sorwakeup_locked(so); 260 } 261 262 void 263 udp_input(struct mbuf *m, int off) 264 { 265 INIT_VNET_INET(curvnet); 266 int iphlen = off; 267 struct ip *ip; 268 struct udphdr *uh; 269 struct ifnet *ifp; 270 struct inpcb *inp; 271 int len; 272 struct ip save_ip; 273 struct sockaddr_in udp_in; 274 #ifdef IPFIREWALL_FORWARD 275 struct m_tag *fwd_tag; 276 #endif 277 278 ifp = m->m_pkthdr.rcvif; 279 UDPSTAT_INC(udps_ipackets); 280 281 /* 282 * Strip IP options, if any; should skip this, make available to 283 * user, and use on returned packets, but we don't yet have a way to 284 * check the checksum with options still present. 285 */ 286 if (iphlen > sizeof (struct ip)) { 287 ip_stripoptions(m, (struct mbuf *)0); 288 iphlen = sizeof(struct ip); 289 } 290 291 /* 292 * Get IP and UDP header together in first mbuf. 293 */ 294 ip = mtod(m, struct ip *); 295 if (m->m_len < iphlen + sizeof(struct udphdr)) { 296 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 297 UDPSTAT_INC(udps_hdrops); 298 return; 299 } 300 ip = mtod(m, struct ip *); 301 } 302 uh = (struct udphdr *)((caddr_t)ip + iphlen); 303 304 /* 305 * Destination port of 0 is illegal, based on RFC768. 306 */ 307 if (uh->uh_dport == 0) 308 goto badunlocked; 309 310 /* 311 * Construct sockaddr format source address. Stuff source address 312 * and datagram in user buffer. 313 */ 314 bzero(&udp_in, sizeof(udp_in)); 315 udp_in.sin_len = sizeof(udp_in); 316 udp_in.sin_family = AF_INET; 317 udp_in.sin_port = uh->uh_sport; 318 udp_in.sin_addr = ip->ip_src; 319 320 /* 321 * Make mbuf data length reflect UDP length. If not enough data to 322 * reflect UDP length, drop. 323 */ 324 len = ntohs((u_short)uh->uh_ulen); 325 if (ip->ip_len != len) { 326 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 327 UDPSTAT_INC(udps_badlen); 328 goto badunlocked; 329 } 330 m_adj(m, len - ip->ip_len); 331 /* ip->ip_len = len; */ 332 } 333 334 /* 335 * Save a copy of the IP header in case we want restore it for 336 * sending an ICMP error message in response. 337 */ 338 if (!V_udp_blackhole) 339 save_ip = *ip; 340 else 341 memset(&save_ip, 0, sizeof(save_ip)); 342 343 /* 344 * Checksum extended UDP header and data. 345 */ 346 if (uh->uh_sum) { 347 u_short uh_sum; 348 349 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 350 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 351 uh_sum = m->m_pkthdr.csum_data; 352 else 353 uh_sum = in_pseudo(ip->ip_src.s_addr, 354 ip->ip_dst.s_addr, htonl((u_short)len + 355 m->m_pkthdr.csum_data + IPPROTO_UDP)); 356 uh_sum ^= 0xffff; 357 } else { 358 char b[9]; 359 360 bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 361 bzero(((struct ipovly *)ip)->ih_x1, 9); 362 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 363 uh_sum = in_cksum(m, len + sizeof (struct ip)); 364 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 365 } 366 if (uh_sum) { 367 UDPSTAT_INC(udps_badsum); 368 m_freem(m); 369 return; 370 } 371 } else 372 UDPSTAT_INC(udps_nosum); 373 374 #ifdef IPFIREWALL_FORWARD 375 /* 376 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 377 */ 378 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 379 if (fwd_tag != NULL) { 380 struct sockaddr_in *next_hop; 381 382 /* 383 * Do the hack. 384 */ 385 next_hop = (struct sockaddr_in *)(fwd_tag + 1); 386 ip->ip_dst = next_hop->sin_addr; 387 uh->uh_dport = ntohs(next_hop->sin_port); 388 389 /* 390 * Remove the tag from the packet. We don't need it anymore. 391 */ 392 m_tag_delete(m, fwd_tag); 393 } 394 #endif 395 396 INP_INFO_RLOCK(&V_udbinfo); 397 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 398 in_broadcast(ip->ip_dst, ifp)) { 399 struct inpcb *last; 400 struct ip_moptions *imo; 401 402 last = NULL; 403 LIST_FOREACH(inp, &V_udb, inp_list) { 404 if (inp->inp_lport != uh->uh_dport) 405 continue; 406 #ifdef INET6 407 if ((inp->inp_vflag & INP_IPV4) == 0) 408 continue; 409 #endif 410 if (inp->inp_laddr.s_addr != INADDR_ANY && 411 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 412 continue; 413 if (inp->inp_faddr.s_addr != INADDR_ANY && 414 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 415 continue; 416 if (inp->inp_fport != 0 && 417 inp->inp_fport != uh->uh_sport) 418 continue; 419 420 INP_RLOCK(inp); 421 422 /* 423 * Handle socket delivery policy for any-source 424 * and source-specific multicast. [RFC3678] 425 */ 426 imo = inp->inp_moptions; 427 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 428 imo != NULL) { 429 struct sockaddr_in group; 430 int blocked; 431 432 bzero(&group, sizeof(struct sockaddr_in)); 433 group.sin_len = sizeof(struct sockaddr_in); 434 group.sin_family = AF_INET; 435 group.sin_addr = ip->ip_dst; 436 437 blocked = imo_multi_filter(imo, ifp, 438 (struct sockaddr *)&group, 439 (struct sockaddr *)&udp_in); 440 if (blocked != MCAST_PASS) { 441 if (blocked == MCAST_NOTGMEMBER) 442 IPSTAT_INC(ips_notmember); 443 if (blocked == MCAST_NOTSMEMBER || 444 blocked == MCAST_MUTED) 445 UDPSTAT_INC(udps_filtermcast); 446 INP_RUNLOCK(inp); 447 continue; 448 } 449 } 450 if (last != NULL) { 451 struct mbuf *n; 452 453 n = m_copy(m, 0, M_COPYALL); 454 if (last->inp_ppcb == NULL) { 455 if (n != NULL) 456 udp_append(last, 457 ip, n, 458 iphlen + 459 sizeof(struct udphdr), 460 &udp_in); 461 INP_RUNLOCK(last); 462 } else { 463 /* 464 * Engage the tunneling protocol we 465 * will have to leave the info_lock 466 * up, since we are hunting through 467 * multiple UDP's. 468 * 469 */ 470 udp_tun_func_t tunnel_func; 471 472 tunnel_func = (udp_tun_func_t)last->inp_ppcb; 473 tunnel_func(n, iphlen, last); 474 INP_RUNLOCK(last); 475 } 476 } 477 last = inp; 478 /* 479 * Don't look for additional matches if this one does 480 * not have either the SO_REUSEPORT or SO_REUSEADDR 481 * socket options set. This heuristic avoids 482 * searching through all pcbs in the common case of a 483 * non-shared port. It assumes that an application 484 * will never clear these options after setting them. 485 */ 486 if ((last->inp_socket->so_options & 487 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 488 break; 489 } 490 491 if (last == NULL) { 492 /* 493 * No matching pcb found; discard datagram. (No need 494 * to send an ICMP Port Unreachable for a broadcast 495 * or multicast datgram.) 496 */ 497 UDPSTAT_INC(udps_noportbcast); 498 goto badheadlocked; 499 } 500 if (last->inp_ppcb == NULL) { 501 udp_append(last, ip, m, iphlen + sizeof(struct udphdr), 502 &udp_in); 503 INP_RUNLOCK(last); 504 INP_INFO_RUNLOCK(&V_udbinfo); 505 } else { 506 /* 507 * Engage the tunneling protocol. 508 */ 509 udp_tun_func_t tunnel_func; 510 511 tunnel_func = (udp_tun_func_t)last->inp_ppcb; 512 tunnel_func(m, iphlen, last); 513 INP_RUNLOCK(last); 514 INP_INFO_RUNLOCK(&V_udbinfo); 515 } 516 return; 517 } 518 519 /* 520 * Locate pcb for datagram. 521 */ 522 inp = in_pcblookup_hash(&V_udbinfo, ip->ip_src, uh->uh_sport, 523 ip->ip_dst, uh->uh_dport, 1, ifp); 524 if (inp == NULL) { 525 if (udp_log_in_vain) { 526 char buf[4*sizeof "123"]; 527 528 strcpy(buf, inet_ntoa(ip->ip_dst)); 529 log(LOG_INFO, 530 "Connection attempt to UDP %s:%d from %s:%d\n", 531 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 532 ntohs(uh->uh_sport)); 533 } 534 UDPSTAT_INC(udps_noport); 535 if (m->m_flags & (M_BCAST | M_MCAST)) { 536 UDPSTAT_INC(udps_noportbcast); 537 goto badheadlocked; 538 } 539 if (V_udp_blackhole) 540 goto badheadlocked; 541 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 542 goto badheadlocked; 543 *ip = save_ip; 544 ip->ip_len += iphlen; 545 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 546 INP_INFO_RUNLOCK(&V_udbinfo); 547 return; 548 } 549 550 /* 551 * Check the minimum TTL for socket. 552 */ 553 INP_RLOCK(inp); 554 INP_INFO_RUNLOCK(&V_udbinfo); 555 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) { 556 INP_RUNLOCK(inp); 557 goto badunlocked; 558 } 559 if (inp->inp_ppcb != NULL) { 560 /* 561 * Engage the tunneling protocol. 562 */ 563 udp_tun_func_t tunnel_func; 564 565 tunnel_func = (udp_tun_func_t)inp->inp_ppcb; 566 tunnel_func(m, iphlen, inp); 567 INP_RUNLOCK(inp); 568 return; 569 } 570 udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in); 571 INP_RUNLOCK(inp); 572 return; 573 574 badheadlocked: 575 if (inp) 576 INP_RUNLOCK(inp); 577 INP_INFO_RUNLOCK(&V_udbinfo); 578 badunlocked: 579 m_freem(m); 580 } 581 582 /* 583 * Notify a udp user of an asynchronous error; just wake up so that they can 584 * collect error status. 585 */ 586 struct inpcb * 587 udp_notify(struct inpcb *inp, int errno) 588 { 589 590 /* 591 * While udp_ctlinput() always calls udp_notify() with a read lock 592 * when invoking it directly, in_pcbnotifyall() currently uses write 593 * locks due to sharing code with TCP. For now, accept either a read 594 * or a write lock, but a read lock is sufficient. 595 */ 596 INP_LOCK_ASSERT(inp); 597 598 inp->inp_socket->so_error = errno; 599 sorwakeup(inp->inp_socket); 600 sowwakeup(inp->inp_socket); 601 return (inp); 602 } 603 604 void 605 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 606 { 607 INIT_VNET_INET(curvnet); 608 struct ip *ip = vip; 609 struct udphdr *uh; 610 struct in_addr faddr; 611 struct inpcb *inp; 612 613 faddr = ((struct sockaddr_in *)sa)->sin_addr; 614 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 615 return; 616 617 /* 618 * Redirects don't need to be handled up here. 619 */ 620 if (PRC_IS_REDIRECT(cmd)) 621 return; 622 623 /* 624 * Hostdead is ugly because it goes linearly through all PCBs. 625 * 626 * XXX: We never get this from ICMP, otherwise it makes an excellent 627 * DoS attack on machines with many connections. 628 */ 629 if (cmd == PRC_HOSTDEAD) 630 ip = NULL; 631 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 632 return; 633 if (ip != NULL) { 634 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 635 INP_INFO_RLOCK(&V_udbinfo); 636 inp = in_pcblookup_hash(&V_udbinfo, faddr, uh->uh_dport, 637 ip->ip_src, uh->uh_sport, 0, NULL); 638 if (inp != NULL) { 639 INP_RLOCK(inp); 640 if (inp->inp_socket != NULL) { 641 udp_notify(inp, inetctlerrmap[cmd]); 642 } 643 INP_RUNLOCK(inp); 644 } 645 INP_INFO_RUNLOCK(&V_udbinfo); 646 } else 647 in_pcbnotifyall(&V_udbinfo, faddr, inetctlerrmap[cmd], 648 udp_notify); 649 } 650 651 static int 652 udp_pcblist(SYSCTL_HANDLER_ARGS) 653 { 654 INIT_VNET_INET(curvnet); 655 int error, i, n; 656 struct inpcb *inp, **inp_list; 657 inp_gen_t gencnt; 658 struct xinpgen xig; 659 660 /* 661 * The process of preparing the PCB list is too time-consuming and 662 * resource-intensive to repeat twice on every request. 663 */ 664 if (req->oldptr == 0) { 665 n = V_udbinfo.ipi_count; 666 req->oldidx = 2 * (sizeof xig) 667 + (n + n/8) * sizeof(struct xinpcb); 668 return (0); 669 } 670 671 if (req->newptr != 0) 672 return (EPERM); 673 674 /* 675 * OK, now we're committed to doing something. 676 */ 677 INP_INFO_RLOCK(&V_udbinfo); 678 gencnt = V_udbinfo.ipi_gencnt; 679 n = V_udbinfo.ipi_count; 680 INP_INFO_RUNLOCK(&V_udbinfo); 681 682 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 683 + n * sizeof(struct xinpcb)); 684 if (error != 0) 685 return (error); 686 687 xig.xig_len = sizeof xig; 688 xig.xig_count = n; 689 xig.xig_gen = gencnt; 690 xig.xig_sogen = so_gencnt; 691 error = SYSCTL_OUT(req, &xig, sizeof xig); 692 if (error) 693 return (error); 694 695 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 696 if (inp_list == 0) 697 return (ENOMEM); 698 699 INP_INFO_RLOCK(&V_udbinfo); 700 for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n; 701 inp = LIST_NEXT(inp, inp_list)) { 702 INP_RLOCK(inp); 703 if (inp->inp_gencnt <= gencnt && 704 cr_canseeinpcb(req->td->td_ucred, inp) == 0) 705 inp_list[i++] = inp; 706 INP_RUNLOCK(inp); 707 } 708 INP_INFO_RUNLOCK(&V_udbinfo); 709 n = i; 710 711 error = 0; 712 for (i = 0; i < n; i++) { 713 inp = inp_list[i]; 714 INP_RLOCK(inp); 715 if (inp->inp_gencnt <= gencnt) { 716 struct xinpcb xi; 717 bzero(&xi, sizeof(xi)); 718 xi.xi_len = sizeof xi; 719 /* XXX should avoid extra copy */ 720 bcopy(inp, &xi.xi_inp, sizeof *inp); 721 if (inp->inp_socket) 722 sotoxsocket(inp->inp_socket, &xi.xi_socket); 723 xi.xi_inp.inp_gencnt = inp->inp_gencnt; 724 INP_RUNLOCK(inp); 725 error = SYSCTL_OUT(req, &xi, sizeof xi); 726 } else 727 INP_RUNLOCK(inp); 728 } 729 if (!error) { 730 /* 731 * Give the user an updated idea of our state. If the 732 * generation differs from what we told her before, she knows 733 * that something happened while we were processing this 734 * request, and it might be necessary to retry. 735 */ 736 INP_INFO_RLOCK(&V_udbinfo); 737 xig.xig_gen = V_udbinfo.ipi_gencnt; 738 xig.xig_sogen = so_gencnt; 739 xig.xig_count = V_udbinfo.ipi_count; 740 INP_INFO_RUNLOCK(&V_udbinfo); 741 error = SYSCTL_OUT(req, &xig, sizeof xig); 742 } 743 free(inp_list, M_TEMP); 744 return (error); 745 } 746 747 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 748 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 749 750 static int 751 udp_getcred(SYSCTL_HANDLER_ARGS) 752 { 753 INIT_VNET_INET(curvnet); 754 struct xucred xuc; 755 struct sockaddr_in addrs[2]; 756 struct inpcb *inp; 757 int error; 758 759 error = priv_check(req->td, PRIV_NETINET_GETCRED); 760 if (error) 761 return (error); 762 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 763 if (error) 764 return (error); 765 INP_INFO_RLOCK(&V_udbinfo); 766 inp = in_pcblookup_hash(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 767 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 768 if (inp != NULL) { 769 INP_RLOCK(inp); 770 INP_INFO_RUNLOCK(&V_udbinfo); 771 if (inp->inp_socket == NULL) 772 error = ENOENT; 773 if (error == 0) 774 error = cr_canseeinpcb(req->td->td_ucred, inp); 775 if (error == 0) 776 cru2x(inp->inp_cred, &xuc); 777 INP_RUNLOCK(inp); 778 } else { 779 INP_INFO_RUNLOCK(&V_udbinfo); 780 error = ENOENT; 781 } 782 if (error == 0) 783 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 784 return (error); 785 } 786 787 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 788 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 789 udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 790 791 static int 792 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, 793 struct mbuf *control, struct thread *td) 794 { 795 INIT_VNET_INET(inp->inp_vnet); 796 struct udpiphdr *ui; 797 int len = m->m_pkthdr.len; 798 struct in_addr faddr, laddr; 799 struct cmsghdr *cm; 800 struct sockaddr_in *sin, src; 801 int error = 0; 802 int ipflags; 803 u_short fport, lport; 804 int unlock_udbinfo; 805 806 /* 807 * udp_output() may need to temporarily bind or connect the current 808 * inpcb. As such, we don't know up front whether we will need the 809 * pcbinfo lock or not. Do any work to decide what is needed up 810 * front before acquiring any locks. 811 */ 812 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 813 if (control) 814 m_freem(control); 815 m_freem(m); 816 return (EMSGSIZE); 817 } 818 819 src.sin_family = 0; 820 if (control != NULL) { 821 /* 822 * XXX: Currently, we assume all the optional information is 823 * stored in a single mbuf. 824 */ 825 if (control->m_next) { 826 m_freem(control); 827 m_freem(m); 828 return (EINVAL); 829 } 830 for (; control->m_len > 0; 831 control->m_data += CMSG_ALIGN(cm->cmsg_len), 832 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 833 cm = mtod(control, struct cmsghdr *); 834 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 835 || cm->cmsg_len > control->m_len) { 836 error = EINVAL; 837 break; 838 } 839 if (cm->cmsg_level != IPPROTO_IP) 840 continue; 841 842 switch (cm->cmsg_type) { 843 case IP_SENDSRCADDR: 844 if (cm->cmsg_len != 845 CMSG_LEN(sizeof(struct in_addr))) { 846 error = EINVAL; 847 break; 848 } 849 bzero(&src, sizeof(src)); 850 src.sin_family = AF_INET; 851 src.sin_len = sizeof(src); 852 src.sin_port = inp->inp_lport; 853 src.sin_addr = 854 *(struct in_addr *)CMSG_DATA(cm); 855 break; 856 857 default: 858 error = ENOPROTOOPT; 859 break; 860 } 861 if (error) 862 break; 863 } 864 m_freem(control); 865 } 866 if (error) { 867 m_freem(m); 868 return (error); 869 } 870 871 /* 872 * Depending on whether or not the application has bound or connected 873 * the socket, we may have to do varying levels of work. The optimal 874 * case is for a connected UDP socket, as a global lock isn't 875 * required at all. 876 * 877 * In order to decide which we need, we require stability of the 878 * inpcb binding, which we ensure by acquiring a read lock on the 879 * inpcb. This doesn't strictly follow the lock order, so we play 880 * the trylock and retry game; note that we may end up with more 881 * conservative locks than required the second time around, so later 882 * assertions have to accept that. Further analysis of the number of 883 * misses under contention is required. 884 */ 885 sin = (struct sockaddr_in *)addr; 886 INP_RLOCK(inp); 887 if (sin != NULL && 888 (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 889 INP_RUNLOCK(inp); 890 INP_INFO_WLOCK(&V_udbinfo); 891 INP_WLOCK(inp); 892 unlock_udbinfo = 2; 893 } else if ((sin != NULL && ( 894 (sin->sin_addr.s_addr == INADDR_ANY) || 895 (sin->sin_addr.s_addr == INADDR_BROADCAST) || 896 (inp->inp_laddr.s_addr == INADDR_ANY) || 897 (inp->inp_lport == 0))) || 898 (src.sin_family == AF_INET)) { 899 if (!INP_INFO_TRY_RLOCK(&V_udbinfo)) { 900 INP_RUNLOCK(inp); 901 INP_INFO_RLOCK(&V_udbinfo); 902 INP_RLOCK(inp); 903 } 904 unlock_udbinfo = 1; 905 } else 906 unlock_udbinfo = 0; 907 908 /* 909 * If the IP_SENDSRCADDR control message was specified, override the 910 * source address for this datagram. Its use is invalidated if the 911 * address thus specified is incomplete or clobbers other inpcbs. 912 */ 913 laddr = inp->inp_laddr; 914 lport = inp->inp_lport; 915 if (src.sin_family == AF_INET) { 916 INP_INFO_LOCK_ASSERT(&V_udbinfo); 917 if ((lport == 0) || 918 (laddr.s_addr == INADDR_ANY && 919 src.sin_addr.s_addr == INADDR_ANY)) { 920 error = EINVAL; 921 goto release; 922 } 923 error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 924 &laddr.s_addr, &lport, td->td_ucred); 925 if (error) 926 goto release; 927 } 928 929 /* 930 * If a UDP socket has been connected, then a local address/port will 931 * have been selected and bound. 932 * 933 * If a UDP socket has not been connected to, then an explicit 934 * destination address must be used, in which case a local 935 * address/port may not have been selected and bound. 936 */ 937 if (sin != NULL) { 938 INP_LOCK_ASSERT(inp); 939 if (inp->inp_faddr.s_addr != INADDR_ANY) { 940 error = EISCONN; 941 goto release; 942 } 943 944 /* 945 * Jail may rewrite the destination address, so let it do 946 * that before we use it. 947 */ 948 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 949 if (error) 950 goto release; 951 952 /* 953 * If a local address or port hasn't yet been selected, or if 954 * the destination address needs to be rewritten due to using 955 * a special INADDR_ constant, invoke in_pcbconnect_setup() 956 * to do the heavy lifting. Once a port is selected, we 957 * commit the binding back to the socket; we also commit the 958 * binding of the address if in jail. 959 * 960 * If we already have a valid binding and we're not 961 * requesting a destination address rewrite, use a fast path. 962 */ 963 if (inp->inp_laddr.s_addr == INADDR_ANY || 964 inp->inp_lport == 0 || 965 sin->sin_addr.s_addr == INADDR_ANY || 966 sin->sin_addr.s_addr == INADDR_BROADCAST) { 967 INP_INFO_LOCK_ASSERT(&V_udbinfo); 968 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, 969 &lport, &faddr.s_addr, &fport, NULL, 970 td->td_ucred); 971 if (error) 972 goto release; 973 974 /* 975 * XXXRW: Why not commit the port if the address is 976 * !INADDR_ANY? 977 */ 978 /* Commit the local port if newly assigned. */ 979 if (inp->inp_laddr.s_addr == INADDR_ANY && 980 inp->inp_lport == 0) { 981 INP_INFO_WLOCK_ASSERT(&V_udbinfo); 982 INP_WLOCK_ASSERT(inp); 983 /* 984 * Remember addr if jailed, to prevent 985 * rebinding. 986 */ 987 if (jailed(td->td_ucred)) 988 inp->inp_laddr = laddr; 989 inp->inp_lport = lport; 990 if (in_pcbinshash(inp) != 0) { 991 inp->inp_lport = 0; 992 error = EAGAIN; 993 goto release; 994 } 995 inp->inp_flags |= INP_ANONPORT; 996 } 997 } else { 998 faddr = sin->sin_addr; 999 fport = sin->sin_port; 1000 } 1001 } else { 1002 INP_LOCK_ASSERT(inp); 1003 faddr = inp->inp_faddr; 1004 fport = inp->inp_fport; 1005 if (faddr.s_addr == INADDR_ANY) { 1006 error = ENOTCONN; 1007 goto release; 1008 } 1009 } 1010 1011 /* 1012 * Calculate data length and get a mbuf for UDP, IP, and possible 1013 * link-layer headers. Immediate slide the data pointer back forward 1014 * since we won't use that space at this layer. 1015 */ 1016 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT); 1017 if (m == NULL) { 1018 error = ENOBUFS; 1019 goto release; 1020 } 1021 m->m_data += max_linkhdr; 1022 m->m_len -= max_linkhdr; 1023 m->m_pkthdr.len -= max_linkhdr; 1024 1025 /* 1026 * Fill in mbuf with extended UDP header and addresses and length put 1027 * into network format. 1028 */ 1029 ui = mtod(m, struct udpiphdr *); 1030 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 1031 ui->ui_pr = IPPROTO_UDP; 1032 ui->ui_src = laddr; 1033 ui->ui_dst = faddr; 1034 ui->ui_sport = lport; 1035 ui->ui_dport = fport; 1036 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 1037 1038 /* 1039 * Set the Don't Fragment bit in the IP header. 1040 */ 1041 if (inp->inp_flags & INP_DONTFRAG) { 1042 struct ip *ip; 1043 1044 ip = (struct ip *)&ui->ui_i; 1045 ip->ip_off |= IP_DF; 1046 } 1047 1048 ipflags = 0; 1049 if (inp->inp_socket->so_options & SO_DONTROUTE) 1050 ipflags |= IP_ROUTETOIF; 1051 if (inp->inp_socket->so_options & SO_BROADCAST) 1052 ipflags |= IP_ALLOWBROADCAST; 1053 if (inp->inp_flags & INP_ONESBCAST) 1054 ipflags |= IP_SENDONES; 1055 1056 #ifdef MAC 1057 mac_inpcb_create_mbuf(inp, m); 1058 #endif 1059 1060 /* 1061 * Set up checksum and output datagram. 1062 */ 1063 if (udp_cksum) { 1064 if (inp->inp_flags & INP_ONESBCAST) 1065 faddr.s_addr = INADDR_BROADCAST; 1066 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 1067 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 1068 m->m_pkthdr.csum_flags = CSUM_UDP; 1069 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 1070 } else 1071 ui->ui_sum = 0; 1072 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 1073 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 1074 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 1075 UDPSTAT_INC(udps_opackets); 1076 1077 if (unlock_udbinfo == 2) 1078 INP_INFO_WUNLOCK(&V_udbinfo); 1079 else if (unlock_udbinfo == 1) 1080 INP_INFO_RUNLOCK(&V_udbinfo); 1081 error = ip_output(m, inp->inp_options, NULL, ipflags, 1082 inp->inp_moptions, inp); 1083 if (unlock_udbinfo == 2) 1084 INP_WUNLOCK(inp); 1085 else 1086 INP_RUNLOCK(inp); 1087 return (error); 1088 1089 release: 1090 if (unlock_udbinfo == 2) { 1091 INP_WUNLOCK(inp); 1092 INP_INFO_WUNLOCK(&V_udbinfo); 1093 } else if (unlock_udbinfo == 1) { 1094 INP_RUNLOCK(inp); 1095 INP_INFO_RUNLOCK(&V_udbinfo); 1096 } else 1097 INP_RUNLOCK(inp); 1098 m_freem(m); 1099 return (error); 1100 } 1101 1102 static void 1103 udp_abort(struct socket *so) 1104 { 1105 INIT_VNET_INET(so->so_vnet); 1106 struct inpcb *inp; 1107 1108 inp = sotoinpcb(so); 1109 KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 1110 INP_INFO_WLOCK(&V_udbinfo); 1111 INP_WLOCK(inp); 1112 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1113 in_pcbdisconnect(inp); 1114 inp->inp_laddr.s_addr = INADDR_ANY; 1115 soisdisconnected(so); 1116 } 1117 INP_WUNLOCK(inp); 1118 INP_INFO_WUNLOCK(&V_udbinfo); 1119 } 1120 1121 static int 1122 udp_attach(struct socket *so, int proto, struct thread *td) 1123 { 1124 INIT_VNET_INET(so->so_vnet); 1125 struct inpcb *inp; 1126 int error; 1127 1128 inp = sotoinpcb(so); 1129 KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 1130 error = soreserve(so, udp_sendspace, udp_recvspace); 1131 if (error) 1132 return (error); 1133 INP_INFO_WLOCK(&V_udbinfo); 1134 error = in_pcballoc(so, &V_udbinfo); 1135 if (error) { 1136 INP_INFO_WUNLOCK(&V_udbinfo); 1137 return (error); 1138 } 1139 1140 inp = (struct inpcb *)so->so_pcb; 1141 INP_INFO_WUNLOCK(&V_udbinfo); 1142 inp->inp_vflag |= INP_IPV4; 1143 inp->inp_ip_ttl = V_ip_defttl; 1144 /* 1145 * UDP does not have a per-protocol pcb (inp->inp_ppcb). 1146 * We use this pointer for kernel tunneling pointer. 1147 * If we ever need to have a protocol block we will 1148 * need to move this function pointer there. Null 1149 * in this pointer means "do the normal thing". 1150 */ 1151 inp->inp_ppcb = NULL; 1152 INP_WUNLOCK(inp); 1153 return (0); 1154 } 1155 1156 int 1157 udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f) 1158 { 1159 struct inpcb *inp; 1160 1161 inp = (struct inpcb *)so->so_pcb; 1162 KASSERT(so->so_type == SOCK_DGRAM, ("udp_set_kernel_tunneling: !dgram")); 1163 KASSERT(so->so_pcb != NULL, ("udp_set_kernel_tunneling: NULL inp")); 1164 if (so->so_type != SOCK_DGRAM) { 1165 /* Not UDP socket... sorry! */ 1166 return (ENOTSUP); 1167 } 1168 if (inp == NULL) { 1169 /* NULL INP? */ 1170 return (EINVAL); 1171 } 1172 INP_WLOCK(inp); 1173 if (inp->inp_ppcb != NULL) { 1174 INP_WUNLOCK(inp); 1175 return (EBUSY); 1176 } 1177 inp->inp_ppcb = f; 1178 INP_WUNLOCK(inp); 1179 return (0); 1180 } 1181 1182 static int 1183 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1184 { 1185 INIT_VNET_INET(so->so_vnet); 1186 struct inpcb *inp; 1187 int error; 1188 1189 inp = sotoinpcb(so); 1190 KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 1191 INP_INFO_WLOCK(&V_udbinfo); 1192 INP_WLOCK(inp); 1193 error = in_pcbbind(inp, nam, td->td_ucred); 1194 INP_WUNLOCK(inp); 1195 INP_INFO_WUNLOCK(&V_udbinfo); 1196 return (error); 1197 } 1198 1199 static void 1200 udp_close(struct socket *so) 1201 { 1202 INIT_VNET_INET(so->so_vnet); 1203 struct inpcb *inp; 1204 1205 inp = sotoinpcb(so); 1206 KASSERT(inp != NULL, ("udp_close: inp == NULL")); 1207 INP_INFO_WLOCK(&V_udbinfo); 1208 INP_WLOCK(inp); 1209 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1210 in_pcbdisconnect(inp); 1211 inp->inp_laddr.s_addr = INADDR_ANY; 1212 soisdisconnected(so); 1213 } 1214 INP_WUNLOCK(inp); 1215 INP_INFO_WUNLOCK(&V_udbinfo); 1216 } 1217 1218 static int 1219 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1220 { 1221 INIT_VNET_INET(so->so_vnet); 1222 struct inpcb *inp; 1223 int error; 1224 struct sockaddr_in *sin; 1225 1226 inp = sotoinpcb(so); 1227 KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 1228 INP_INFO_WLOCK(&V_udbinfo); 1229 INP_WLOCK(inp); 1230 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1231 INP_WUNLOCK(inp); 1232 INP_INFO_WUNLOCK(&V_udbinfo); 1233 return (EISCONN); 1234 } 1235 sin = (struct sockaddr_in *)nam; 1236 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1237 if (error != 0) { 1238 INP_WUNLOCK(inp); 1239 INP_INFO_WUNLOCK(&V_udbinfo); 1240 return (error); 1241 } 1242 error = in_pcbconnect(inp, nam, td->td_ucred); 1243 if (error == 0) 1244 soisconnected(so); 1245 INP_WUNLOCK(inp); 1246 INP_INFO_WUNLOCK(&V_udbinfo); 1247 return (error); 1248 } 1249 1250 static void 1251 udp_detach(struct socket *so) 1252 { 1253 INIT_VNET_INET(so->so_vnet); 1254 struct inpcb *inp; 1255 1256 inp = sotoinpcb(so); 1257 KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1258 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1259 ("udp_detach: not disconnected")); 1260 INP_INFO_WLOCK(&V_udbinfo); 1261 INP_WLOCK(inp); 1262 in_pcbdetach(inp); 1263 in_pcbfree(inp); 1264 INP_INFO_WUNLOCK(&V_udbinfo); 1265 } 1266 1267 static int 1268 udp_disconnect(struct socket *so) 1269 { 1270 INIT_VNET_INET(so->so_vnet); 1271 struct inpcb *inp; 1272 1273 inp = sotoinpcb(so); 1274 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 1275 INP_INFO_WLOCK(&V_udbinfo); 1276 INP_WLOCK(inp); 1277 if (inp->inp_faddr.s_addr == INADDR_ANY) { 1278 INP_WUNLOCK(inp); 1279 INP_INFO_WUNLOCK(&V_udbinfo); 1280 return (ENOTCONN); 1281 } 1282 1283 in_pcbdisconnect(inp); 1284 inp->inp_laddr.s_addr = INADDR_ANY; 1285 SOCK_LOCK(so); 1286 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1287 SOCK_UNLOCK(so); 1288 INP_WUNLOCK(inp); 1289 INP_INFO_WUNLOCK(&V_udbinfo); 1290 return (0); 1291 } 1292 1293 static int 1294 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1295 struct mbuf *control, struct thread *td) 1296 { 1297 struct inpcb *inp; 1298 1299 inp = sotoinpcb(so); 1300 KASSERT(inp != NULL, ("udp_send: inp == NULL")); 1301 return (udp_output(inp, m, addr, control, td)); 1302 } 1303 1304 int 1305 udp_shutdown(struct socket *so) 1306 { 1307 struct inpcb *inp; 1308 1309 inp = sotoinpcb(so); 1310 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 1311 INP_WLOCK(inp); 1312 socantsendmore(so); 1313 INP_WUNLOCK(inp); 1314 return (0); 1315 } 1316 1317 struct pr_usrreqs udp_usrreqs = { 1318 .pru_abort = udp_abort, 1319 .pru_attach = udp_attach, 1320 .pru_bind = udp_bind, 1321 .pru_connect = udp_connect, 1322 .pru_control = in_control, 1323 .pru_detach = udp_detach, 1324 .pru_disconnect = udp_disconnect, 1325 .pru_peeraddr = in_getpeeraddr, 1326 .pru_send = udp_send, 1327 .pru_soreceive = soreceive_dgram, 1328 .pru_sosend = sosend_dgram, 1329 .pru_shutdown = udp_shutdown, 1330 .pru_sockaddr = in_getsockaddr, 1331 .pru_sosetlabel = in_pcbsosetlabel, 1332 .pru_close = udp_close, 1333 }; 1334