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