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 * Copyright (c) 2010-2011 Juniper Networks, Inc. 6 * Copyright (c) 2014 Kevin Lo 7 * All rights reserved. 8 * 9 * Portions of this software were developed by Robert N. M. Watson under 10 * contract to Juniper Networks, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include "opt_ipfw.h" 43 #include "opt_inet.h" 44 #include "opt_inet6.h" 45 #include "opt_ipsec.h" 46 #include "opt_rss.h" 47 48 #include <sys/param.h> 49 #include <sys/domain.h> 50 #include <sys/eventhandler.h> 51 #include <sys/jail.h> 52 #include <sys/kernel.h> 53 #include <sys/lock.h> 54 #include <sys/malloc.h> 55 #include <sys/mbuf.h> 56 #include <sys/priv.h> 57 #include <sys/proc.h> 58 #include <sys/protosw.h> 59 #include <sys/sdt.h> 60 #include <sys/signalvar.h> 61 #include <sys/socket.h> 62 #include <sys/socketvar.h> 63 #include <sys/sx.h> 64 #include <sys/sysctl.h> 65 #include <sys/syslog.h> 66 #include <sys/systm.h> 67 68 #include <vm/uma.h> 69 70 #include <net/if.h> 71 #include <net/if_var.h> 72 #include <net/route.h> 73 #include <net/rss_config.h> 74 75 #include <netinet/in.h> 76 #include <netinet/in_kdtrace.h> 77 #include <netinet/in_pcb.h> 78 #include <netinet/in_systm.h> 79 #include <netinet/in_var.h> 80 #include <netinet/ip.h> 81 #ifdef INET6 82 #include <netinet/ip6.h> 83 #endif 84 #include <netinet/ip_icmp.h> 85 #include <netinet/icmp_var.h> 86 #include <netinet/ip_var.h> 87 #include <netinet/ip_options.h> 88 #ifdef INET6 89 #include <netinet6/ip6_var.h> 90 #endif 91 #include <netinet/udp.h> 92 #include <netinet/udp_var.h> 93 #include <netinet/udplite.h> 94 #include <netinet/in_rss.h> 95 96 #ifdef IPSEC 97 #include <netipsec/ipsec.h> 98 #include <netipsec/esp.h> 99 #endif 100 101 #include <machine/in_cksum.h> 102 103 #include <security/mac/mac_framework.h> 104 105 /* 106 * UDP and UDP-Lite protocols implementation. 107 * Per RFC 768, August, 1980. 108 * Per RFC 3828, July, 2004. 109 */ 110 111 /* 112 * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums 113 * removes the only data integrity mechanism for packets and malformed 114 * packets that would otherwise be discarded due to bad checksums, and may 115 * cause problems (especially for NFS data blocks). 116 */ 117 VNET_DEFINE(int, udp_cksum) = 1; 118 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW, 119 &VNET_NAME(udp_cksum), 0, "compute udp checksum"); 120 121 int udp_log_in_vain = 0; 122 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 123 &udp_log_in_vain, 0, "Log all incoming UDP packets"); 124 125 VNET_DEFINE(int, udp_blackhole) = 0; 126 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW, 127 &VNET_NAME(udp_blackhole), 0, 128 "Do not send port unreachables for refused connects"); 129 130 u_long udp_sendspace = 9216; /* really max datagram size */ 131 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 132 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 133 134 u_long udp_recvspace = 40 * (1024 + 135 #ifdef INET6 136 sizeof(struct sockaddr_in6) 137 #else 138 sizeof(struct sockaddr_in) 139 #endif 140 ); /* 40 1K datagrams */ 141 142 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 143 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams"); 144 145 VNET_DEFINE(struct inpcbhead, udb); /* from udp_var.h */ 146 VNET_DEFINE(struct inpcbinfo, udbinfo); 147 VNET_DEFINE(struct inpcbhead, ulitecb); 148 VNET_DEFINE(struct inpcbinfo, ulitecbinfo); 149 static VNET_DEFINE(uma_zone_t, udpcb_zone); 150 #define V_udpcb_zone VNET(udpcb_zone) 151 152 #ifndef UDBHASHSIZE 153 #define UDBHASHSIZE 128 154 #endif 155 156 VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat); /* from udp_var.h */ 157 VNET_PCPUSTAT_SYSINIT(udpstat); 158 SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat, 159 udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 160 161 #ifdef VIMAGE 162 VNET_PCPUSTAT_SYSUNINIT(udpstat); 163 #endif /* VIMAGE */ 164 #ifdef INET 165 static void udp_detach(struct socket *so); 166 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, 167 struct mbuf *, struct thread *); 168 #endif 169 170 #ifdef IPSEC 171 #ifdef IPSEC_NAT_T 172 #define UF_ESPINUDP_ALL (UF_ESPINUDP_NON_IKE|UF_ESPINUDP) 173 #ifdef INET 174 static struct mbuf *udp4_espdecap(struct inpcb *, struct mbuf *, int); 175 #endif 176 #endif /* IPSEC_NAT_T */ 177 #endif /* IPSEC */ 178 179 static void 180 udp_zone_change(void *tag) 181 { 182 183 uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets); 184 uma_zone_set_max(V_udpcb_zone, maxsockets); 185 } 186 187 static int 188 udp_inpcb_init(void *mem, int size, int flags) 189 { 190 struct inpcb *inp; 191 192 inp = mem; 193 INP_LOCK_INIT(inp, "inp", "udpinp"); 194 return (0); 195 } 196 197 static int 198 udplite_inpcb_init(void *mem, int size, int flags) 199 { 200 struct inpcb *inp; 201 202 inp = mem; 203 INP_LOCK_INIT(inp, "inp", "udpliteinp"); 204 return (0); 205 } 206 207 void 208 udp_init(void) 209 { 210 211 /* 212 * For now default to 2-tuple UDP hashing - until the fragment 213 * reassembly code can also update the flowid. 214 * 215 * Once we can calculate the flowid that way and re-establish 216 * a 4-tuple, flip this to 4-tuple. 217 */ 218 in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE, 219 "udp_inpcb", udp_inpcb_init, NULL, 0, 220 IPI_HASHFIELDS_2TUPLE); 221 V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb), 222 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 223 uma_zone_set_max(V_udpcb_zone, maxsockets); 224 uma_zone_set_warning(V_udpcb_zone, "kern.ipc.maxsockets limit reached"); 225 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL, 226 EVENTHANDLER_PRI_ANY); 227 } 228 229 void 230 udplite_init(void) 231 { 232 233 in_pcbinfo_init(&V_ulitecbinfo, "udplite", &V_ulitecb, UDBHASHSIZE, 234 UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init, NULL, 235 0, IPI_HASHFIELDS_2TUPLE); 236 } 237 238 /* 239 * Kernel module interface for updating udpstat. The argument is an index 240 * into udpstat treated as an array of u_long. While this encodes the 241 * general layout of udpstat into the caller, it doesn't encode its location, 242 * so that future changes to add, for example, per-CPU stats support won't 243 * cause binary compatibility problems for kernel modules. 244 */ 245 void 246 kmod_udpstat_inc(int statnum) 247 { 248 249 counter_u64_add(VNET(udpstat)[statnum], 1); 250 } 251 252 int 253 udp_newudpcb(struct inpcb *inp) 254 { 255 struct udpcb *up; 256 257 up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO); 258 if (up == NULL) 259 return (ENOBUFS); 260 inp->inp_ppcb = up; 261 return (0); 262 } 263 264 void 265 udp_discardcb(struct udpcb *up) 266 { 267 268 uma_zfree(V_udpcb_zone, up); 269 } 270 271 #ifdef VIMAGE 272 static void 273 udp_destroy(void *unused __unused) 274 { 275 276 in_pcbinfo_destroy(&V_udbinfo); 277 uma_zdestroy(V_udpcb_zone); 278 } 279 VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL); 280 281 static void 282 udplite_destroy(void *unused __unused) 283 { 284 285 in_pcbinfo_destroy(&V_ulitecbinfo); 286 } 287 VNET_SYSUNINIT(udplite, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udplite_destroy, 288 NULL); 289 #endif 290 291 #ifdef INET 292 /* 293 * Subroutine of udp_input(), which appends the provided mbuf chain to the 294 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that 295 * contains the source address. If the socket ends up being an IPv6 socket, 296 * udp_append() will convert to a sockaddr_in6 before passing the address 297 * into the socket code. 298 * 299 * In the normal case udp_append() will return 0, indicating that you 300 * must unlock the inp. However if a tunneling protocol is in place we increment 301 * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we 302 * then decrement the reference count. If the inp_rele returns 1, indicating the 303 * inp is gone, we return that to the caller to tell them *not* to unlock 304 * the inp. In the case of multi-cast this will cause the distribution 305 * to stop (though most tunneling protocols known currently do *not* use 306 * multicast). 307 */ 308 static int 309 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off, 310 struct sockaddr_in *udp_in) 311 { 312 struct sockaddr *append_sa; 313 struct socket *so; 314 struct mbuf *opts = NULL; 315 #ifdef INET6 316 struct sockaddr_in6 udp_in6; 317 #endif 318 struct udpcb *up; 319 320 INP_LOCK_ASSERT(inp); 321 322 /* 323 * Engage the tunneling protocol. 324 */ 325 up = intoudpcb(inp); 326 if (up->u_tun_func != NULL) { 327 in_pcbref(inp); 328 INP_RUNLOCK(inp); 329 (*up->u_tun_func)(n, off, inp, (struct sockaddr *)udp_in, 330 up->u_tun_ctx); 331 INP_RLOCK(inp); 332 return (in_pcbrele_rlocked(inp)); 333 } 334 335 off += sizeof(struct udphdr); 336 337 #ifdef IPSEC 338 /* Check AH/ESP integrity. */ 339 if (ipsec4_in_reject(n, inp)) { 340 m_freem(n); 341 return (0); 342 } 343 #ifdef IPSEC_NAT_T 344 up = intoudpcb(inp); 345 KASSERT(up != NULL, ("%s: udpcb NULL", __func__)); 346 if (up->u_flags & UF_ESPINUDP_ALL) { /* IPSec UDP encaps. */ 347 n = udp4_espdecap(inp, n, off); 348 if (n == NULL) /* Consumed. */ 349 return (0); 350 } 351 #endif /* IPSEC_NAT_T */ 352 #endif /* IPSEC */ 353 #ifdef MAC 354 if (mac_inpcb_check_deliver(inp, n) != 0) { 355 m_freem(n); 356 return (0); 357 } 358 #endif /* MAC */ 359 if (inp->inp_flags & INP_CONTROLOPTS || 360 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) { 361 #ifdef INET6 362 if (inp->inp_vflag & INP_IPV6) 363 (void)ip6_savecontrol_v4(inp, n, &opts, NULL); 364 else 365 #endif /* INET6 */ 366 ip_savecontrol(inp, &opts, ip, n); 367 } 368 #ifdef INET6 369 if (inp->inp_vflag & INP_IPV6) { 370 bzero(&udp_in6, sizeof(udp_in6)); 371 udp_in6.sin6_len = sizeof(udp_in6); 372 udp_in6.sin6_family = AF_INET6; 373 in6_sin_2_v4mapsin6(udp_in, &udp_in6); 374 append_sa = (struct sockaddr *)&udp_in6; 375 } else 376 #endif /* INET6 */ 377 append_sa = (struct sockaddr *)udp_in; 378 m_adj(n, off); 379 380 so = inp->inp_socket; 381 SOCKBUF_LOCK(&so->so_rcv); 382 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) { 383 SOCKBUF_UNLOCK(&so->so_rcv); 384 m_freem(n); 385 if (opts) 386 m_freem(opts); 387 UDPSTAT_INC(udps_fullsock); 388 } else 389 sorwakeup_locked(so); 390 return (0); 391 } 392 393 int 394 udp_input(struct mbuf **mp, int *offp, int proto) 395 { 396 struct ip *ip; 397 struct udphdr *uh; 398 struct ifnet *ifp; 399 struct inpcb *inp; 400 uint16_t len, ip_len; 401 struct inpcbinfo *pcbinfo; 402 struct ip save_ip; 403 struct sockaddr_in udp_in; 404 struct mbuf *m; 405 struct m_tag *fwd_tag; 406 int cscov_partial, iphlen; 407 408 m = *mp; 409 iphlen = *offp; 410 ifp = m->m_pkthdr.rcvif; 411 *mp = NULL; 412 UDPSTAT_INC(udps_ipackets); 413 414 /* 415 * Strip IP options, if any; should skip this, make available to 416 * user, and use on returned packets, but we don't yet have a way to 417 * check the checksum with options still present. 418 */ 419 if (iphlen > sizeof (struct ip)) { 420 ip_stripoptions(m); 421 iphlen = sizeof(struct ip); 422 } 423 424 /* 425 * Get IP and UDP header together in first mbuf. 426 */ 427 ip = mtod(m, struct ip *); 428 if (m->m_len < iphlen + sizeof(struct udphdr)) { 429 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) { 430 UDPSTAT_INC(udps_hdrops); 431 return (IPPROTO_DONE); 432 } 433 ip = mtod(m, struct ip *); 434 } 435 uh = (struct udphdr *)((caddr_t)ip + iphlen); 436 cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0; 437 438 /* 439 * Destination port of 0 is illegal, based on RFC768. 440 */ 441 if (uh->uh_dport == 0) 442 goto badunlocked; 443 444 /* 445 * Construct sockaddr format source address. Stuff source address 446 * and datagram in user buffer. 447 */ 448 bzero(&udp_in, sizeof(udp_in)); 449 udp_in.sin_len = sizeof(udp_in); 450 udp_in.sin_family = AF_INET; 451 udp_in.sin_port = uh->uh_sport; 452 udp_in.sin_addr = ip->ip_src; 453 454 /* 455 * Make mbuf data length reflect UDP length. If not enough data to 456 * reflect UDP length, drop. 457 */ 458 len = ntohs((u_short)uh->uh_ulen); 459 ip_len = ntohs(ip->ip_len) - iphlen; 460 if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) { 461 /* Zero means checksum over the complete packet. */ 462 if (len == 0) 463 len = ip_len; 464 cscov_partial = 0; 465 } 466 if (ip_len != len) { 467 if (len > ip_len || len < sizeof(struct udphdr)) { 468 UDPSTAT_INC(udps_badlen); 469 goto badunlocked; 470 } 471 if (proto == IPPROTO_UDP) 472 m_adj(m, len - ip_len); 473 } 474 475 /* 476 * Save a copy of the IP header in case we want restore it for 477 * sending an ICMP error message in response. 478 */ 479 if (!V_udp_blackhole) 480 save_ip = *ip; 481 else 482 memset(&save_ip, 0, sizeof(save_ip)); 483 484 /* 485 * Checksum extended UDP header and data. 486 */ 487 if (uh->uh_sum) { 488 u_short uh_sum; 489 490 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) && 491 !cscov_partial) { 492 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 493 uh_sum = m->m_pkthdr.csum_data; 494 else 495 uh_sum = in_pseudo(ip->ip_src.s_addr, 496 ip->ip_dst.s_addr, htonl((u_short)len + 497 m->m_pkthdr.csum_data + proto)); 498 uh_sum ^= 0xffff; 499 } else { 500 char b[9]; 501 502 bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 503 bzero(((struct ipovly *)ip)->ih_x1, 9); 504 ((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ? 505 uh->uh_ulen : htons(ip_len); 506 uh_sum = in_cksum(m, len + sizeof (struct ip)); 507 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 508 } 509 if (uh_sum) { 510 UDPSTAT_INC(udps_badsum); 511 m_freem(m); 512 return (IPPROTO_DONE); 513 } 514 } else { 515 if (proto == IPPROTO_UDP) { 516 UDPSTAT_INC(udps_nosum); 517 } else { 518 /* UDPLite requires a checksum */ 519 /* XXX: What is the right UDPLite MIB counter here? */ 520 m_freem(m); 521 return (IPPROTO_DONE); 522 } 523 } 524 525 pcbinfo = udp_get_inpcbinfo(proto); 526 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 527 in_broadcast(ip->ip_dst, ifp)) { 528 struct inpcb *last; 529 struct inpcbhead *pcblist; 530 struct ip_moptions *imo; 531 532 INP_INFO_RLOCK(pcbinfo); 533 pcblist = udp_get_pcblist(proto); 534 last = NULL; 535 LIST_FOREACH(inp, pcblist, inp_list) { 536 if (inp->inp_lport != uh->uh_dport) 537 continue; 538 #ifdef INET6 539 if ((inp->inp_vflag & INP_IPV4) == 0) 540 continue; 541 #endif 542 if (inp->inp_laddr.s_addr != INADDR_ANY && 543 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 544 continue; 545 if (inp->inp_faddr.s_addr != INADDR_ANY && 546 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 547 continue; 548 if (inp->inp_fport != 0 && 549 inp->inp_fport != uh->uh_sport) 550 continue; 551 552 INP_RLOCK(inp); 553 554 /* 555 * XXXRW: Because we weren't holding either the inpcb 556 * or the hash lock when we checked for a match 557 * before, we should probably recheck now that the 558 * inpcb lock is held. 559 */ 560 561 /* 562 * Handle socket delivery policy for any-source 563 * and source-specific multicast. [RFC3678] 564 */ 565 imo = inp->inp_moptions; 566 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 567 struct sockaddr_in group; 568 int blocked; 569 if (imo == NULL) { 570 INP_RUNLOCK(inp); 571 continue; 572 } 573 bzero(&group, sizeof(struct sockaddr_in)); 574 group.sin_len = sizeof(struct sockaddr_in); 575 group.sin_family = AF_INET; 576 group.sin_addr = ip->ip_dst; 577 578 blocked = imo_multi_filter(imo, ifp, 579 (struct sockaddr *)&group, 580 (struct sockaddr *)&udp_in); 581 if (blocked != MCAST_PASS) { 582 if (blocked == MCAST_NOTGMEMBER) 583 IPSTAT_INC(ips_notmember); 584 if (blocked == MCAST_NOTSMEMBER || 585 blocked == MCAST_MUTED) 586 UDPSTAT_INC(udps_filtermcast); 587 INP_RUNLOCK(inp); 588 continue; 589 } 590 } 591 if (last != NULL) { 592 struct mbuf *n; 593 594 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 595 UDP_PROBE(receive, NULL, last, ip, 596 last, uh); 597 if (udp_append(last, ip, n, iphlen, 598 &udp_in)) { 599 goto inp_lost; 600 } 601 } 602 INP_RUNLOCK(last); 603 } 604 last = inp; 605 /* 606 * Don't look for additional matches if this one does 607 * not have either the SO_REUSEPORT or SO_REUSEADDR 608 * socket options set. This heuristic avoids 609 * searching through all pcbs in the common case of a 610 * non-shared port. It assumes that an application 611 * will never clear these options after setting them. 612 */ 613 if ((last->inp_socket->so_options & 614 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 615 break; 616 } 617 618 if (last == NULL) { 619 /* 620 * No matching pcb found; discard datagram. (No need 621 * to send an ICMP Port Unreachable for a broadcast 622 * or multicast datgram.) 623 */ 624 UDPSTAT_INC(udps_noportbcast); 625 if (inp) 626 INP_RUNLOCK(inp); 627 INP_INFO_RUNLOCK(pcbinfo); 628 goto badunlocked; 629 } 630 UDP_PROBE(receive, NULL, last, ip, last, uh); 631 if (udp_append(last, ip, m, iphlen, &udp_in) == 0) 632 INP_RUNLOCK(last); 633 inp_lost: 634 INP_INFO_RUNLOCK(pcbinfo); 635 return (IPPROTO_DONE); 636 } 637 638 /* 639 * Locate pcb for datagram. 640 */ 641 642 /* 643 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 644 */ 645 if ((m->m_flags & M_IP_NEXTHOP) && 646 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 647 struct sockaddr_in *next_hop; 648 649 next_hop = (struct sockaddr_in *)(fwd_tag + 1); 650 651 /* 652 * Transparently forwarded. Pretend to be the destination. 653 * Already got one like this? 654 */ 655 inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 656 ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m); 657 if (!inp) { 658 /* 659 * It's new. Try to find the ambushing socket. 660 * Because we've rewritten the destination address, 661 * any hardware-generated hash is ignored. 662 */ 663 inp = in_pcblookup(pcbinfo, ip->ip_src, 664 uh->uh_sport, next_hop->sin_addr, 665 next_hop->sin_port ? htons(next_hop->sin_port) : 666 uh->uh_dport, INPLOOKUP_WILDCARD | 667 INPLOOKUP_RLOCKPCB, ifp); 668 } 669 /* Remove the tag from the packet. We don't need it anymore. */ 670 m_tag_delete(m, fwd_tag); 671 m->m_flags &= ~M_IP_NEXTHOP; 672 } else 673 inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport, 674 ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD | 675 INPLOOKUP_RLOCKPCB, ifp, m); 676 if (inp == NULL) { 677 if (udp_log_in_vain) { 678 char buf[4*sizeof "123"]; 679 680 strcpy(buf, inet_ntoa(ip->ip_dst)); 681 log(LOG_INFO, 682 "Connection attempt to UDP %s:%d from %s:%d\n", 683 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 684 ntohs(uh->uh_sport)); 685 } 686 UDPSTAT_INC(udps_noport); 687 if (m->m_flags & (M_BCAST | M_MCAST)) { 688 UDPSTAT_INC(udps_noportbcast); 689 goto badunlocked; 690 } 691 if (V_udp_blackhole) 692 goto badunlocked; 693 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 694 goto badunlocked; 695 *ip = save_ip; 696 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 697 return (IPPROTO_DONE); 698 } 699 700 /* 701 * Check the minimum TTL for socket. 702 */ 703 INP_RLOCK_ASSERT(inp); 704 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) { 705 INP_RUNLOCK(inp); 706 m_freem(m); 707 return (IPPROTO_DONE); 708 } 709 if (cscov_partial) { 710 struct udpcb *up; 711 712 up = intoudpcb(inp); 713 if (up->u_rxcslen == 0 || up->u_rxcslen > len) { 714 INP_RUNLOCK(inp); 715 m_freem(m); 716 return (IPPROTO_DONE); 717 } 718 } 719 720 UDP_PROBE(receive, NULL, inp, ip, inp, uh); 721 if (udp_append(inp, ip, m, iphlen, &udp_in) == 0) 722 INP_RUNLOCK(inp); 723 return (IPPROTO_DONE); 724 725 badunlocked: 726 m_freem(m); 727 return (IPPROTO_DONE); 728 } 729 #endif /* INET */ 730 731 /* 732 * Notify a udp user of an asynchronous error; just wake up so that they can 733 * collect error status. 734 */ 735 struct inpcb * 736 udp_notify(struct inpcb *inp, int errno) 737 { 738 739 /* 740 * While udp_ctlinput() always calls udp_notify() with a read lock 741 * when invoking it directly, in_pcbnotifyall() currently uses write 742 * locks due to sharing code with TCP. For now, accept either a read 743 * or a write lock, but a read lock is sufficient. 744 */ 745 INP_LOCK_ASSERT(inp); 746 if ((errno == EHOSTUNREACH || errno == ENETUNREACH || 747 errno == EHOSTDOWN) && inp->inp_route.ro_rt) { 748 RTFREE(inp->inp_route.ro_rt); 749 inp->inp_route.ro_rt = (struct rtentry *)NULL; 750 } 751 752 inp->inp_socket->so_error = errno; 753 sorwakeup(inp->inp_socket); 754 sowwakeup(inp->inp_socket); 755 return (inp); 756 } 757 758 #ifdef INET 759 static void 760 udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip, 761 struct inpcbinfo *pcbinfo) 762 { 763 struct ip *ip = vip; 764 struct udphdr *uh; 765 struct in_addr faddr; 766 struct inpcb *inp; 767 768 faddr = ((struct sockaddr_in *)sa)->sin_addr; 769 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 770 return; 771 772 if (PRC_IS_REDIRECT(cmd)) { 773 /* signal EHOSTDOWN, as it flushes the cached route */ 774 in_pcbnotifyall(&V_udbinfo, faddr, EHOSTDOWN, udp_notify); 775 return; 776 } 777 778 /* 779 * Hostdead is ugly because it goes linearly through all PCBs. 780 * 781 * XXX: We never get this from ICMP, otherwise it makes an excellent 782 * DoS attack on machines with many connections. 783 */ 784 if (cmd == PRC_HOSTDEAD) 785 ip = NULL; 786 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 787 return; 788 if (ip != NULL) { 789 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 790 inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 791 ip->ip_src, uh->uh_sport, INPLOOKUP_RLOCKPCB, NULL); 792 if (inp != NULL) { 793 INP_RLOCK_ASSERT(inp); 794 if (inp->inp_socket != NULL) { 795 udp_notify(inp, inetctlerrmap[cmd]); 796 } 797 INP_RUNLOCK(inp); 798 } else { 799 inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport, 800 ip->ip_src, uh->uh_sport, 801 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 802 if (inp != NULL) { 803 struct udpcb *up; 804 805 up = intoudpcb(inp); 806 if (up->u_icmp_func != NULL) { 807 INP_RUNLOCK(inp); 808 (*up->u_icmp_func)(cmd, sa, vip, up->u_tun_ctx); 809 } else { 810 INP_RUNLOCK(inp); 811 } 812 } 813 } 814 } else 815 in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd], 816 udp_notify); 817 } 818 void 819 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 820 { 821 822 return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo)); 823 } 824 825 void 826 udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip) 827 { 828 829 return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo)); 830 } 831 #endif /* INET */ 832 833 static int 834 udp_pcblist(SYSCTL_HANDLER_ARGS) 835 { 836 int error, i, n; 837 struct inpcb *inp, **inp_list; 838 inp_gen_t gencnt; 839 struct xinpgen xig; 840 841 /* 842 * The process of preparing the PCB list is too time-consuming and 843 * resource-intensive to repeat twice on every request. 844 */ 845 if (req->oldptr == 0) { 846 n = V_udbinfo.ipi_count; 847 n += imax(n / 8, 10); 848 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb); 849 return (0); 850 } 851 852 if (req->newptr != 0) 853 return (EPERM); 854 855 /* 856 * OK, now we're committed to doing something. 857 */ 858 INP_INFO_RLOCK(&V_udbinfo); 859 gencnt = V_udbinfo.ipi_gencnt; 860 n = V_udbinfo.ipi_count; 861 INP_INFO_RUNLOCK(&V_udbinfo); 862 863 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 864 + n * sizeof(struct xinpcb)); 865 if (error != 0) 866 return (error); 867 868 xig.xig_len = sizeof xig; 869 xig.xig_count = n; 870 xig.xig_gen = gencnt; 871 xig.xig_sogen = so_gencnt; 872 error = SYSCTL_OUT(req, &xig, sizeof xig); 873 if (error) 874 return (error); 875 876 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 877 if (inp_list == NULL) 878 return (ENOMEM); 879 880 INP_INFO_RLOCK(&V_udbinfo); 881 for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n; 882 inp = LIST_NEXT(inp, inp_list)) { 883 INP_WLOCK(inp); 884 if (inp->inp_gencnt <= gencnt && 885 cr_canseeinpcb(req->td->td_ucred, inp) == 0) { 886 in_pcbref(inp); 887 inp_list[i++] = inp; 888 } 889 INP_WUNLOCK(inp); 890 } 891 INP_INFO_RUNLOCK(&V_udbinfo); 892 n = i; 893 894 error = 0; 895 for (i = 0; i < n; i++) { 896 inp = inp_list[i]; 897 INP_RLOCK(inp); 898 if (inp->inp_gencnt <= gencnt) { 899 struct xinpcb xi; 900 901 bzero(&xi, sizeof(xi)); 902 xi.xi_len = sizeof xi; 903 /* XXX should avoid extra copy */ 904 bcopy(inp, &xi.xi_inp, sizeof *inp); 905 if (inp->inp_socket) 906 sotoxsocket(inp->inp_socket, &xi.xi_socket); 907 xi.xi_inp.inp_gencnt = inp->inp_gencnt; 908 INP_RUNLOCK(inp); 909 error = SYSCTL_OUT(req, &xi, sizeof xi); 910 } else 911 INP_RUNLOCK(inp); 912 } 913 INP_INFO_WLOCK(&V_udbinfo); 914 for (i = 0; i < n; i++) { 915 inp = inp_list[i]; 916 INP_RLOCK(inp); 917 if (!in_pcbrele_rlocked(inp)) 918 INP_RUNLOCK(inp); 919 } 920 INP_INFO_WUNLOCK(&V_udbinfo); 921 922 if (!error) { 923 /* 924 * Give the user an updated idea of our state. If the 925 * generation differs from what we told her before, she knows 926 * that something happened while we were processing this 927 * request, and it might be necessary to retry. 928 */ 929 INP_INFO_RLOCK(&V_udbinfo); 930 xig.xig_gen = V_udbinfo.ipi_gencnt; 931 xig.xig_sogen = so_gencnt; 932 xig.xig_count = V_udbinfo.ipi_count; 933 INP_INFO_RUNLOCK(&V_udbinfo); 934 error = SYSCTL_OUT(req, &xig, sizeof xig); 935 } 936 free(inp_list, M_TEMP); 937 return (error); 938 } 939 940 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, 941 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0, 942 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 943 944 #ifdef INET 945 static int 946 udp_getcred(SYSCTL_HANDLER_ARGS) 947 { 948 struct xucred xuc; 949 struct sockaddr_in addrs[2]; 950 struct inpcb *inp; 951 int error; 952 953 error = priv_check(req->td, PRIV_NETINET_GETCRED); 954 if (error) 955 return (error); 956 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 957 if (error) 958 return (error); 959 inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 960 addrs[0].sin_addr, addrs[0].sin_port, 961 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 962 if (inp != NULL) { 963 INP_RLOCK_ASSERT(inp); 964 if (inp->inp_socket == NULL) 965 error = ENOENT; 966 if (error == 0) 967 error = cr_canseeinpcb(req->td->td_ucred, inp); 968 if (error == 0) 969 cru2x(inp->inp_cred, &xuc); 970 INP_RUNLOCK(inp); 971 } else 972 error = ENOENT; 973 if (error == 0) 974 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 975 return (error); 976 } 977 978 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, 979 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 980 udp_getcred, "S,xucred", "Get the xucred of a UDP connection"); 981 #endif /* INET */ 982 983 int 984 udp_ctloutput(struct socket *so, struct sockopt *sopt) 985 { 986 struct inpcb *inp; 987 struct udpcb *up; 988 int isudplite, error, optval; 989 990 error = 0; 991 isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0; 992 inp = sotoinpcb(so); 993 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 994 INP_WLOCK(inp); 995 if (sopt->sopt_level != so->so_proto->pr_protocol) { 996 #ifdef INET6 997 if (INP_CHECK_SOCKAF(so, AF_INET6)) { 998 INP_WUNLOCK(inp); 999 error = ip6_ctloutput(so, sopt); 1000 } 1001 #endif 1002 #if defined(INET) && defined(INET6) 1003 else 1004 #endif 1005 #ifdef INET 1006 { 1007 INP_WUNLOCK(inp); 1008 error = ip_ctloutput(so, sopt); 1009 } 1010 #endif 1011 return (error); 1012 } 1013 1014 switch (sopt->sopt_dir) { 1015 case SOPT_SET: 1016 switch (sopt->sopt_name) { 1017 case UDP_ENCAP: 1018 INP_WUNLOCK(inp); 1019 error = sooptcopyin(sopt, &optval, sizeof optval, 1020 sizeof optval); 1021 if (error) 1022 break; 1023 inp = sotoinpcb(so); 1024 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1025 INP_WLOCK(inp); 1026 #ifdef IPSEC_NAT_T 1027 up = intoudpcb(inp); 1028 KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1029 #endif 1030 switch (optval) { 1031 case 0: 1032 /* Clear all UDP encap. */ 1033 #ifdef IPSEC_NAT_T 1034 up->u_flags &= ~UF_ESPINUDP_ALL; 1035 #endif 1036 break; 1037 #ifdef IPSEC_NAT_T 1038 case UDP_ENCAP_ESPINUDP: 1039 case UDP_ENCAP_ESPINUDP_NON_IKE: 1040 up->u_flags &= ~UF_ESPINUDP_ALL; 1041 if (optval == UDP_ENCAP_ESPINUDP) 1042 up->u_flags |= UF_ESPINUDP; 1043 else if (optval == UDP_ENCAP_ESPINUDP_NON_IKE) 1044 up->u_flags |= UF_ESPINUDP_NON_IKE; 1045 break; 1046 #endif 1047 default: 1048 error = EINVAL; 1049 break; 1050 } 1051 INP_WUNLOCK(inp); 1052 break; 1053 case UDPLITE_SEND_CSCOV: 1054 case UDPLITE_RECV_CSCOV: 1055 if (!isudplite) { 1056 INP_WUNLOCK(inp); 1057 error = ENOPROTOOPT; 1058 break; 1059 } 1060 INP_WUNLOCK(inp); 1061 error = sooptcopyin(sopt, &optval, sizeof(optval), 1062 sizeof(optval)); 1063 if (error != 0) 1064 break; 1065 inp = sotoinpcb(so); 1066 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 1067 INP_WLOCK(inp); 1068 up = intoudpcb(inp); 1069 KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1070 if ((optval != 0 && optval < 8) || (optval > 65535)) { 1071 INP_WUNLOCK(inp); 1072 error = EINVAL; 1073 break; 1074 } 1075 if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1076 up->u_txcslen = optval; 1077 else 1078 up->u_rxcslen = optval; 1079 INP_WUNLOCK(inp); 1080 break; 1081 default: 1082 INP_WUNLOCK(inp); 1083 error = ENOPROTOOPT; 1084 break; 1085 } 1086 break; 1087 case SOPT_GET: 1088 switch (sopt->sopt_name) { 1089 #ifdef IPSEC_NAT_T 1090 case UDP_ENCAP: 1091 up = intoudpcb(inp); 1092 KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1093 optval = up->u_flags & UF_ESPINUDP_ALL; 1094 INP_WUNLOCK(inp); 1095 error = sooptcopyout(sopt, &optval, sizeof optval); 1096 break; 1097 #endif 1098 case UDPLITE_SEND_CSCOV: 1099 case UDPLITE_RECV_CSCOV: 1100 if (!isudplite) { 1101 INP_WUNLOCK(inp); 1102 error = ENOPROTOOPT; 1103 break; 1104 } 1105 up = intoudpcb(inp); 1106 KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1107 if (sopt->sopt_name == UDPLITE_SEND_CSCOV) 1108 optval = up->u_txcslen; 1109 else 1110 optval = up->u_rxcslen; 1111 INP_WUNLOCK(inp); 1112 error = sooptcopyout(sopt, &optval, sizeof(optval)); 1113 break; 1114 default: 1115 INP_WUNLOCK(inp); 1116 error = ENOPROTOOPT; 1117 break; 1118 } 1119 break; 1120 } 1121 return (error); 1122 } 1123 1124 #ifdef INET 1125 #define UH_WLOCKED 2 1126 #define UH_RLOCKED 1 1127 #define UH_UNLOCKED 0 1128 static int 1129 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, 1130 struct mbuf *control, struct thread *td) 1131 { 1132 struct udpiphdr *ui; 1133 int len = m->m_pkthdr.len; 1134 struct in_addr faddr, laddr; 1135 struct cmsghdr *cm; 1136 struct inpcbinfo *pcbinfo; 1137 struct sockaddr_in *sin, src; 1138 int cscov_partial = 0; 1139 int error = 0; 1140 int ipflags; 1141 u_short fport, lport; 1142 int unlock_udbinfo, unlock_inp; 1143 u_char tos; 1144 uint8_t pr; 1145 uint16_t cscov = 0; 1146 uint32_t flowid = 0; 1147 uint8_t flowtype = M_HASHTYPE_NONE; 1148 1149 /* 1150 * udp_output() may need to temporarily bind or connect the current 1151 * inpcb. As such, we don't know up front whether we will need the 1152 * pcbinfo lock or not. Do any work to decide what is needed up 1153 * front before acquiring any locks. 1154 */ 1155 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 1156 if (control) 1157 m_freem(control); 1158 m_freem(m); 1159 return (EMSGSIZE); 1160 } 1161 1162 src.sin_family = 0; 1163 sin = (struct sockaddr_in *)addr; 1164 if (sin == NULL || 1165 (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 1166 INP_WLOCK(inp); 1167 unlock_inp = UH_WLOCKED; 1168 } else { 1169 INP_RLOCK(inp); 1170 unlock_inp = UH_RLOCKED; 1171 } 1172 tos = inp->inp_ip_tos; 1173 if (control != NULL) { 1174 /* 1175 * XXX: Currently, we assume all the optional information is 1176 * stored in a single mbuf. 1177 */ 1178 if (control->m_next) { 1179 if (unlock_inp == UH_WLOCKED) 1180 INP_WUNLOCK(inp); 1181 else 1182 INP_RUNLOCK(inp); 1183 m_freem(control); 1184 m_freem(m); 1185 return (EINVAL); 1186 } 1187 for (; control->m_len > 0; 1188 control->m_data += CMSG_ALIGN(cm->cmsg_len), 1189 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 1190 cm = mtod(control, struct cmsghdr *); 1191 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 1192 || cm->cmsg_len > control->m_len) { 1193 error = EINVAL; 1194 break; 1195 } 1196 if (cm->cmsg_level != IPPROTO_IP) 1197 continue; 1198 1199 switch (cm->cmsg_type) { 1200 case IP_SENDSRCADDR: 1201 if (cm->cmsg_len != 1202 CMSG_LEN(sizeof(struct in_addr))) { 1203 error = EINVAL; 1204 break; 1205 } 1206 bzero(&src, sizeof(src)); 1207 src.sin_family = AF_INET; 1208 src.sin_len = sizeof(src); 1209 src.sin_port = inp->inp_lport; 1210 src.sin_addr = 1211 *(struct in_addr *)CMSG_DATA(cm); 1212 break; 1213 1214 case IP_TOS: 1215 if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) { 1216 error = EINVAL; 1217 break; 1218 } 1219 tos = *(u_char *)CMSG_DATA(cm); 1220 break; 1221 1222 case IP_FLOWID: 1223 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 1224 error = EINVAL; 1225 break; 1226 } 1227 flowid = *(uint32_t *) CMSG_DATA(cm); 1228 break; 1229 1230 case IP_FLOWTYPE: 1231 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 1232 error = EINVAL; 1233 break; 1234 } 1235 flowtype = *(uint32_t *) CMSG_DATA(cm); 1236 break; 1237 1238 #ifdef RSS 1239 case IP_RSSBUCKETID: 1240 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) { 1241 error = EINVAL; 1242 break; 1243 } 1244 /* This is just a placeholder for now */ 1245 break; 1246 #endif /* RSS */ 1247 default: 1248 error = ENOPROTOOPT; 1249 break; 1250 } 1251 if (error) 1252 break; 1253 } 1254 m_freem(control); 1255 } 1256 if (error) { 1257 if (unlock_inp == UH_WLOCKED) 1258 INP_WUNLOCK(inp); 1259 else 1260 INP_RUNLOCK(inp); 1261 m_freem(m); 1262 return (error); 1263 } 1264 1265 /* 1266 * Depending on whether or not the application has bound or connected 1267 * the socket, we may have to do varying levels of work. The optimal 1268 * case is for a connected UDP socket, as a global lock isn't 1269 * required at all. 1270 * 1271 * In order to decide which we need, we require stability of the 1272 * inpcb binding, which we ensure by acquiring a read lock on the 1273 * inpcb. This doesn't strictly follow the lock order, so we play 1274 * the trylock and retry game; note that we may end up with more 1275 * conservative locks than required the second time around, so later 1276 * assertions have to accept that. Further analysis of the number of 1277 * misses under contention is required. 1278 * 1279 * XXXRW: Check that hash locking update here is correct. 1280 */ 1281 pr = inp->inp_socket->so_proto->pr_protocol; 1282 pcbinfo = udp_get_inpcbinfo(pr); 1283 sin = (struct sockaddr_in *)addr; 1284 if (sin != NULL && 1285 (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) { 1286 INP_HASH_WLOCK(pcbinfo); 1287 unlock_udbinfo = UH_WLOCKED; 1288 } else if ((sin != NULL && ( 1289 (sin->sin_addr.s_addr == INADDR_ANY) || 1290 (sin->sin_addr.s_addr == INADDR_BROADCAST) || 1291 (inp->inp_laddr.s_addr == INADDR_ANY) || 1292 (inp->inp_lport == 0))) || 1293 (src.sin_family == AF_INET)) { 1294 INP_HASH_RLOCK(pcbinfo); 1295 unlock_udbinfo = UH_RLOCKED; 1296 } else 1297 unlock_udbinfo = UH_UNLOCKED; 1298 1299 /* 1300 * If the IP_SENDSRCADDR control message was specified, override the 1301 * source address for this datagram. Its use is invalidated if the 1302 * address thus specified is incomplete or clobbers other inpcbs. 1303 */ 1304 laddr = inp->inp_laddr; 1305 lport = inp->inp_lport; 1306 if (src.sin_family == AF_INET) { 1307 INP_HASH_LOCK_ASSERT(pcbinfo); 1308 if ((lport == 0) || 1309 (laddr.s_addr == INADDR_ANY && 1310 src.sin_addr.s_addr == INADDR_ANY)) { 1311 error = EINVAL; 1312 goto release; 1313 } 1314 error = in_pcbbind_setup(inp, (struct sockaddr *)&src, 1315 &laddr.s_addr, &lport, td->td_ucred); 1316 if (error) 1317 goto release; 1318 } 1319 1320 /* 1321 * If a UDP socket has been connected, then a local address/port will 1322 * have been selected and bound. 1323 * 1324 * If a UDP socket has not been connected to, then an explicit 1325 * destination address must be used, in which case a local 1326 * address/port may not have been selected and bound. 1327 */ 1328 if (sin != NULL) { 1329 INP_LOCK_ASSERT(inp); 1330 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1331 error = EISCONN; 1332 goto release; 1333 } 1334 1335 /* 1336 * Jail may rewrite the destination address, so let it do 1337 * that before we use it. 1338 */ 1339 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1340 if (error) 1341 goto release; 1342 1343 /* 1344 * If a local address or port hasn't yet been selected, or if 1345 * the destination address needs to be rewritten due to using 1346 * a special INADDR_ constant, invoke in_pcbconnect_setup() 1347 * to do the heavy lifting. Once a port is selected, we 1348 * commit the binding back to the socket; we also commit the 1349 * binding of the address if in jail. 1350 * 1351 * If we already have a valid binding and we're not 1352 * requesting a destination address rewrite, use a fast path. 1353 */ 1354 if (inp->inp_laddr.s_addr == INADDR_ANY || 1355 inp->inp_lport == 0 || 1356 sin->sin_addr.s_addr == INADDR_ANY || 1357 sin->sin_addr.s_addr == INADDR_BROADCAST) { 1358 INP_HASH_LOCK_ASSERT(pcbinfo); 1359 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, 1360 &lport, &faddr.s_addr, &fport, NULL, 1361 td->td_ucred); 1362 if (error) 1363 goto release; 1364 1365 /* 1366 * XXXRW: Why not commit the port if the address is 1367 * !INADDR_ANY? 1368 */ 1369 /* Commit the local port if newly assigned. */ 1370 if (inp->inp_laddr.s_addr == INADDR_ANY && 1371 inp->inp_lport == 0) { 1372 INP_WLOCK_ASSERT(inp); 1373 INP_HASH_WLOCK_ASSERT(pcbinfo); 1374 /* 1375 * Remember addr if jailed, to prevent 1376 * rebinding. 1377 */ 1378 if (prison_flag(td->td_ucred, PR_IP4)) 1379 inp->inp_laddr = laddr; 1380 inp->inp_lport = lport; 1381 if (in_pcbinshash(inp) != 0) { 1382 inp->inp_lport = 0; 1383 error = EAGAIN; 1384 goto release; 1385 } 1386 inp->inp_flags |= INP_ANONPORT; 1387 } 1388 } else { 1389 faddr = sin->sin_addr; 1390 fport = sin->sin_port; 1391 } 1392 } else { 1393 INP_LOCK_ASSERT(inp); 1394 faddr = inp->inp_faddr; 1395 fport = inp->inp_fport; 1396 if (faddr.s_addr == INADDR_ANY) { 1397 error = ENOTCONN; 1398 goto release; 1399 } 1400 } 1401 1402 /* 1403 * Calculate data length and get a mbuf for UDP, IP, and possible 1404 * link-layer headers. Immediate slide the data pointer back forward 1405 * since we won't use that space at this layer. 1406 */ 1407 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT); 1408 if (m == NULL) { 1409 error = ENOBUFS; 1410 goto release; 1411 } 1412 m->m_data += max_linkhdr; 1413 m->m_len -= max_linkhdr; 1414 m->m_pkthdr.len -= max_linkhdr; 1415 1416 /* 1417 * Fill in mbuf with extended UDP header and addresses and length put 1418 * into network format. 1419 */ 1420 ui = mtod(m, struct udpiphdr *); 1421 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ 1422 ui->ui_pr = pr; 1423 ui->ui_src = laddr; 1424 ui->ui_dst = faddr; 1425 ui->ui_sport = lport; 1426 ui->ui_dport = fport; 1427 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 1428 if (pr == IPPROTO_UDPLITE) { 1429 struct udpcb *up; 1430 uint16_t plen; 1431 1432 up = intoudpcb(inp); 1433 cscov = up->u_txcslen; 1434 plen = (u_short)len + sizeof(struct udphdr); 1435 if (cscov >= plen) 1436 cscov = 0; 1437 ui->ui_len = htons(plen); 1438 ui->ui_ulen = htons(cscov); 1439 /* 1440 * For UDP-Lite, checksum coverage length of zero means 1441 * the entire UDPLite packet is covered by the checksum. 1442 */ 1443 cscov_partial = (cscov == 0) ? 0 : 1; 1444 } else 1445 ui->ui_v = IPVERSION << 4; 1446 1447 /* 1448 * Set the Don't Fragment bit in the IP header. 1449 */ 1450 if (inp->inp_flags & INP_DONTFRAG) { 1451 struct ip *ip; 1452 1453 ip = (struct ip *)&ui->ui_i; 1454 ip->ip_off |= htons(IP_DF); 1455 } 1456 1457 ipflags = 0; 1458 if (inp->inp_socket->so_options & SO_DONTROUTE) 1459 ipflags |= IP_ROUTETOIF; 1460 if (inp->inp_socket->so_options & SO_BROADCAST) 1461 ipflags |= IP_ALLOWBROADCAST; 1462 if (inp->inp_flags & INP_ONESBCAST) 1463 ipflags |= IP_SENDONES; 1464 1465 #ifdef MAC 1466 mac_inpcb_create_mbuf(inp, m); 1467 #endif 1468 1469 /* 1470 * Set up checksum and output datagram. 1471 */ 1472 ui->ui_sum = 0; 1473 if (pr == IPPROTO_UDPLITE) { 1474 if (inp->inp_flags & INP_ONESBCAST) 1475 faddr.s_addr = INADDR_BROADCAST; 1476 if (cscov_partial) { 1477 if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0) 1478 ui->ui_sum = 0xffff; 1479 } else { 1480 if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0) 1481 ui->ui_sum = 0xffff; 1482 } 1483 } else if (V_udp_cksum) { 1484 if (inp->inp_flags & INP_ONESBCAST) 1485 faddr.s_addr = INADDR_BROADCAST; 1486 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr, 1487 htons((u_short)len + sizeof(struct udphdr) + pr)); 1488 m->m_pkthdr.csum_flags = CSUM_UDP; 1489 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 1490 } 1491 ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len); 1492 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 1493 ((struct ip *)ui)->ip_tos = tos; /* XXX */ 1494 UDPSTAT_INC(udps_opackets); 1495 1496 /* 1497 * Setup flowid / RSS information for outbound socket. 1498 * 1499 * Once the UDP code decides to set a flowid some other way, 1500 * this allows the flowid to be overridden by userland. 1501 */ 1502 if (flowtype != M_HASHTYPE_NONE) { 1503 m->m_pkthdr.flowid = flowid; 1504 M_HASHTYPE_SET(m, flowtype); 1505 #ifdef RSS 1506 } else { 1507 uint32_t hash_val, hash_type; 1508 /* 1509 * Calculate an appropriate RSS hash for UDP and 1510 * UDP Lite. 1511 * 1512 * The called function will take care of figuring out 1513 * whether a 2-tuple or 4-tuple hash is required based 1514 * on the currently configured scheme. 1515 * 1516 * Later later on connected socket values should be 1517 * cached in the inpcb and reused, rather than constantly 1518 * re-calculating it. 1519 * 1520 * UDP Lite is a different protocol number and will 1521 * likely end up being hashed as a 2-tuple until 1522 * RSS / NICs grow UDP Lite protocol awareness. 1523 */ 1524 if (rss_proto_software_hash_v4(faddr, laddr, fport, lport, 1525 pr, &hash_val, &hash_type) == 0) { 1526 m->m_pkthdr.flowid = hash_val; 1527 M_HASHTYPE_SET(m, hash_type); 1528 } 1529 #endif 1530 } 1531 1532 #ifdef RSS 1533 /* 1534 * Don't override with the inp cached flowid value. 1535 * 1536 * Depending upon the kind of send being done, the inp 1537 * flowid/flowtype values may actually not be appropriate 1538 * for this particular socket send. 1539 * 1540 * We should either leave the flowid at zero (which is what is 1541 * currently done) or set it to some software generated 1542 * hash value based on the packet contents. 1543 */ 1544 ipflags |= IP_NODEFAULTFLOWID; 1545 #endif /* RSS */ 1546 1547 if (unlock_udbinfo == UH_WLOCKED) 1548 INP_HASH_WUNLOCK(pcbinfo); 1549 else if (unlock_udbinfo == UH_RLOCKED) 1550 INP_HASH_RUNLOCK(pcbinfo); 1551 UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u); 1552 error = ip_output(m, inp->inp_options, 1553 (unlock_inp == UH_WLOCKED ? &inp->inp_route : NULL), ipflags, 1554 inp->inp_moptions, inp); 1555 if (unlock_inp == UH_WLOCKED) 1556 INP_WUNLOCK(inp); 1557 else 1558 INP_RUNLOCK(inp); 1559 return (error); 1560 1561 release: 1562 if (unlock_udbinfo == UH_WLOCKED) { 1563 INP_HASH_WUNLOCK(pcbinfo); 1564 INP_WUNLOCK(inp); 1565 } else if (unlock_udbinfo == UH_RLOCKED) { 1566 INP_HASH_RUNLOCK(pcbinfo); 1567 INP_RUNLOCK(inp); 1568 } else 1569 INP_RUNLOCK(inp); 1570 m_freem(m); 1571 return (error); 1572 } 1573 1574 1575 #if defined(IPSEC) && defined(IPSEC_NAT_T) 1576 /* 1577 * Potentially decap ESP in UDP frame. Check for an ESP header 1578 * and optional marker; if present, strip the UDP header and 1579 * push the result through IPSec. 1580 * 1581 * Returns mbuf to be processed (potentially re-allocated) or 1582 * NULL if consumed and/or processed. 1583 */ 1584 static struct mbuf * 1585 udp4_espdecap(struct inpcb *inp, struct mbuf *m, int off) 1586 { 1587 size_t minlen, payload, skip, iphlen; 1588 caddr_t data; 1589 struct udpcb *up; 1590 struct m_tag *tag; 1591 struct udphdr *udphdr; 1592 struct ip *ip; 1593 1594 INP_RLOCK_ASSERT(inp); 1595 1596 /* 1597 * Pull up data so the longest case is contiguous: 1598 * IP/UDP hdr + non ESP marker + ESP hdr. 1599 */ 1600 minlen = off + sizeof(uint64_t) + sizeof(struct esp); 1601 if (minlen > m->m_pkthdr.len) 1602 minlen = m->m_pkthdr.len; 1603 if ((m = m_pullup(m, minlen)) == NULL) { 1604 IPSECSTAT_INC(ips_in_inval); 1605 return (NULL); /* Bypass caller processing. */ 1606 } 1607 data = mtod(m, caddr_t); /* Points to ip header. */ 1608 payload = m->m_len - off; /* Size of payload. */ 1609 1610 if (payload == 1 && data[off] == '\xff') 1611 return (m); /* NB: keepalive packet, no decap. */ 1612 1613 up = intoudpcb(inp); 1614 KASSERT(up != NULL, ("%s: udpcb NULL", __func__)); 1615 KASSERT((up->u_flags & UF_ESPINUDP_ALL) != 0, 1616 ("u_flags 0x%x", up->u_flags)); 1617 1618 /* 1619 * Check that the payload is large enough to hold an 1620 * ESP header and compute the amount of data to remove. 1621 * 1622 * NB: the caller has already done a pullup for us. 1623 * XXX can we assume alignment and eliminate bcopys? 1624 */ 1625 if (up->u_flags & UF_ESPINUDP_NON_IKE) { 1626 /* 1627 * draft-ietf-ipsec-nat-t-ike-0[01].txt and 1628 * draft-ietf-ipsec-udp-encaps-(00/)01.txt, ignoring 1629 * possible AH mode non-IKE marker+non-ESP marker 1630 * from draft-ietf-ipsec-udp-encaps-00.txt. 1631 */ 1632 uint64_t marker; 1633 1634 if (payload <= sizeof(uint64_t) + sizeof(struct esp)) 1635 return (m); /* NB: no decap. */ 1636 bcopy(data + off, &marker, sizeof(uint64_t)); 1637 if (marker != 0) /* Non-IKE marker. */ 1638 return (m); /* NB: no decap. */ 1639 skip = sizeof(uint64_t) + sizeof(struct udphdr); 1640 } else { 1641 uint32_t spi; 1642 1643 if (payload <= sizeof(struct esp)) { 1644 IPSECSTAT_INC(ips_in_inval); 1645 m_freem(m); 1646 return (NULL); /* Discard. */ 1647 } 1648 bcopy(data + off, &spi, sizeof(uint32_t)); 1649 if (spi == 0) /* Non-ESP marker. */ 1650 return (m); /* NB: no decap. */ 1651 skip = sizeof(struct udphdr); 1652 } 1653 1654 /* 1655 * Setup a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember 1656 * the UDP ports. This is required if we want to select 1657 * the right SPD for multiple hosts behind same NAT. 1658 * 1659 * NB: ports are maintained in network byte order everywhere 1660 * in the NAT-T code. 1661 */ 1662 tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS, 1663 2 * sizeof(uint16_t), M_NOWAIT); 1664 if (tag == NULL) { 1665 IPSECSTAT_INC(ips_in_nomem); 1666 m_freem(m); 1667 return (NULL); /* Discard. */ 1668 } 1669 iphlen = off - sizeof(struct udphdr); 1670 udphdr = (struct udphdr *)(data + iphlen); 1671 ((uint16_t *)(tag + 1))[0] = udphdr->uh_sport; 1672 ((uint16_t *)(tag + 1))[1] = udphdr->uh_dport; 1673 m_tag_prepend(m, tag); 1674 1675 /* 1676 * Remove the UDP header (and possibly the non ESP marker) 1677 * IP header length is iphlen 1678 * Before: 1679 * <--- off ---> 1680 * +----+------+-----+ 1681 * | IP | UDP | ESP | 1682 * +----+------+-----+ 1683 * <-skip-> 1684 * After: 1685 * +----+-----+ 1686 * | IP | ESP | 1687 * +----+-----+ 1688 * <-skip-> 1689 */ 1690 ovbcopy(data, data + skip, iphlen); 1691 m_adj(m, skip); 1692 1693 ip = mtod(m, struct ip *); 1694 ip->ip_len = htons(ntohs(ip->ip_len) - skip); 1695 ip->ip_p = IPPROTO_ESP; 1696 1697 /* 1698 * We cannot yet update the cksums so clear any 1699 * h/w cksum flags as they are no longer valid. 1700 */ 1701 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) 1702 m->m_pkthdr.csum_flags &= ~(CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 1703 1704 (void) ipsec_common_input(m, iphlen, offsetof(struct ip, ip_p), 1705 AF_INET, ip->ip_p); 1706 return (NULL); /* NB: consumed, bypass processing. */ 1707 } 1708 #endif /* defined(IPSEC) && defined(IPSEC_NAT_T) */ 1709 1710 static void 1711 udp_abort(struct socket *so) 1712 { 1713 struct inpcb *inp; 1714 struct inpcbinfo *pcbinfo; 1715 1716 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1717 inp = sotoinpcb(so); 1718 KASSERT(inp != NULL, ("udp_abort: inp == NULL")); 1719 INP_WLOCK(inp); 1720 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1721 INP_HASH_WLOCK(pcbinfo); 1722 in_pcbdisconnect(inp); 1723 inp->inp_laddr.s_addr = INADDR_ANY; 1724 INP_HASH_WUNLOCK(pcbinfo); 1725 soisdisconnected(so); 1726 } 1727 INP_WUNLOCK(inp); 1728 } 1729 1730 static int 1731 udp_attach(struct socket *so, int proto, struct thread *td) 1732 { 1733 struct inpcb *inp; 1734 struct inpcbinfo *pcbinfo; 1735 int error; 1736 1737 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1738 inp = sotoinpcb(so); 1739 KASSERT(inp == NULL, ("udp_attach: inp != NULL")); 1740 error = soreserve(so, udp_sendspace, udp_recvspace); 1741 if (error) 1742 return (error); 1743 INP_INFO_WLOCK(pcbinfo); 1744 error = in_pcballoc(so, pcbinfo); 1745 if (error) { 1746 INP_INFO_WUNLOCK(pcbinfo); 1747 return (error); 1748 } 1749 1750 inp = sotoinpcb(so); 1751 inp->inp_vflag |= INP_IPV4; 1752 inp->inp_ip_ttl = V_ip_defttl; 1753 1754 error = udp_newudpcb(inp); 1755 if (error) { 1756 in_pcbdetach(inp); 1757 in_pcbfree(inp); 1758 INP_INFO_WUNLOCK(pcbinfo); 1759 return (error); 1760 } 1761 1762 INP_WUNLOCK(inp); 1763 INP_INFO_WUNLOCK(pcbinfo); 1764 return (0); 1765 } 1766 #endif /* INET */ 1767 1768 int 1769 udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx) 1770 { 1771 struct inpcb *inp; 1772 struct udpcb *up; 1773 1774 KASSERT(so->so_type == SOCK_DGRAM, 1775 ("udp_set_kernel_tunneling: !dgram")); 1776 inp = sotoinpcb(so); 1777 KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL")); 1778 INP_WLOCK(inp); 1779 up = intoudpcb(inp); 1780 if ((up->u_tun_func != NULL) || 1781 (up->u_icmp_func != NULL)) { 1782 INP_WUNLOCK(inp); 1783 return (EBUSY); 1784 } 1785 up->u_tun_func = f; 1786 up->u_icmp_func = i; 1787 up->u_tun_ctx = ctx; 1788 INP_WUNLOCK(inp); 1789 return (0); 1790 } 1791 1792 #ifdef INET 1793 static int 1794 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1795 { 1796 struct inpcb *inp; 1797 struct inpcbinfo *pcbinfo; 1798 int error; 1799 1800 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1801 inp = sotoinpcb(so); 1802 KASSERT(inp != NULL, ("udp_bind: inp == NULL")); 1803 INP_WLOCK(inp); 1804 INP_HASH_WLOCK(pcbinfo); 1805 error = in_pcbbind(inp, nam, td->td_ucred); 1806 INP_HASH_WUNLOCK(pcbinfo); 1807 INP_WUNLOCK(inp); 1808 return (error); 1809 } 1810 1811 static void 1812 udp_close(struct socket *so) 1813 { 1814 struct inpcb *inp; 1815 struct inpcbinfo *pcbinfo; 1816 1817 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1818 inp = sotoinpcb(so); 1819 KASSERT(inp != NULL, ("udp_close: inp == NULL")); 1820 INP_WLOCK(inp); 1821 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1822 INP_HASH_WLOCK(pcbinfo); 1823 in_pcbdisconnect(inp); 1824 inp->inp_laddr.s_addr = INADDR_ANY; 1825 INP_HASH_WUNLOCK(pcbinfo); 1826 soisdisconnected(so); 1827 } 1828 INP_WUNLOCK(inp); 1829 } 1830 1831 static int 1832 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1833 { 1834 struct inpcb *inp; 1835 struct inpcbinfo *pcbinfo; 1836 struct sockaddr_in *sin; 1837 int error; 1838 1839 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1840 inp = sotoinpcb(so); 1841 KASSERT(inp != NULL, ("udp_connect: inp == NULL")); 1842 INP_WLOCK(inp); 1843 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1844 INP_WUNLOCK(inp); 1845 return (EISCONN); 1846 } 1847 sin = (struct sockaddr_in *)nam; 1848 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr); 1849 if (error != 0) { 1850 INP_WUNLOCK(inp); 1851 return (error); 1852 } 1853 INP_HASH_WLOCK(pcbinfo); 1854 error = in_pcbconnect(inp, nam, td->td_ucred); 1855 INP_HASH_WUNLOCK(pcbinfo); 1856 if (error == 0) 1857 soisconnected(so); 1858 INP_WUNLOCK(inp); 1859 return (error); 1860 } 1861 1862 static void 1863 udp_detach(struct socket *so) 1864 { 1865 struct inpcb *inp; 1866 struct inpcbinfo *pcbinfo; 1867 struct udpcb *up; 1868 1869 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1870 inp = sotoinpcb(so); 1871 KASSERT(inp != NULL, ("udp_detach: inp == NULL")); 1872 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 1873 ("udp_detach: not disconnected")); 1874 INP_INFO_WLOCK(pcbinfo); 1875 INP_WLOCK(inp); 1876 up = intoudpcb(inp); 1877 KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1878 inp->inp_ppcb = NULL; 1879 in_pcbdetach(inp); 1880 in_pcbfree(inp); 1881 INP_INFO_WUNLOCK(pcbinfo); 1882 udp_discardcb(up); 1883 } 1884 1885 static int 1886 udp_disconnect(struct socket *so) 1887 { 1888 struct inpcb *inp; 1889 struct inpcbinfo *pcbinfo; 1890 1891 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1892 inp = sotoinpcb(so); 1893 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL")); 1894 INP_WLOCK(inp); 1895 if (inp->inp_faddr.s_addr == INADDR_ANY) { 1896 INP_WUNLOCK(inp); 1897 return (ENOTCONN); 1898 } 1899 INP_HASH_WLOCK(pcbinfo); 1900 in_pcbdisconnect(inp); 1901 inp->inp_laddr.s_addr = INADDR_ANY; 1902 INP_HASH_WUNLOCK(pcbinfo); 1903 SOCK_LOCK(so); 1904 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1905 SOCK_UNLOCK(so); 1906 INP_WUNLOCK(inp); 1907 return (0); 1908 } 1909 1910 static int 1911 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1912 struct mbuf *control, struct thread *td) 1913 { 1914 struct inpcb *inp; 1915 1916 inp = sotoinpcb(so); 1917 KASSERT(inp != NULL, ("udp_send: inp == NULL")); 1918 return (udp_output(inp, m, addr, control, td)); 1919 } 1920 #endif /* INET */ 1921 1922 int 1923 udp_shutdown(struct socket *so) 1924 { 1925 struct inpcb *inp; 1926 1927 inp = sotoinpcb(so); 1928 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL")); 1929 INP_WLOCK(inp); 1930 socantsendmore(so); 1931 INP_WUNLOCK(inp); 1932 return (0); 1933 } 1934 1935 #ifdef INET 1936 struct pr_usrreqs udp_usrreqs = { 1937 .pru_abort = udp_abort, 1938 .pru_attach = udp_attach, 1939 .pru_bind = udp_bind, 1940 .pru_connect = udp_connect, 1941 .pru_control = in_control, 1942 .pru_detach = udp_detach, 1943 .pru_disconnect = udp_disconnect, 1944 .pru_peeraddr = in_getpeeraddr, 1945 .pru_send = udp_send, 1946 .pru_soreceive = soreceive_dgram, 1947 .pru_sosend = sosend_dgram, 1948 .pru_shutdown = udp_shutdown, 1949 .pru_sockaddr = in_getsockaddr, 1950 .pru_sosetlabel = in_pcbsosetlabel, 1951 .pru_close = udp_close, 1952 }; 1953 #endif /* INET */ 1954