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