1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 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 * 3. Neither the name of the project 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 PROJECT 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 PROJECT 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 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ 37 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $ 38 */ 39 40 /*- 41 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 42 * The Regents of the University of California. 43 * All rights reserved. 44 * 45 * Redistribution and use in source and binary forms, with or without 46 * modification, are permitted provided that the following conditions 47 * are met: 48 * 1. Redistributions of source code must retain the above copyright 49 * notice, this list of conditions and the following disclaimer. 50 * 2. Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 3. Neither the name of the University nor the names of its contributors 54 * may be used to endorse or promote products derived from this software 55 * without specific prior written permission. 56 * 57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 67 * SUCH DAMAGE. 68 * 69 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 70 */ 71 72 #include <sys/cdefs.h> 73 __FBSDID("$FreeBSD$"); 74 75 #include "opt_inet.h" 76 #include "opt_inet6.h" 77 #include "opt_ipsec.h" 78 #include "opt_rss.h" 79 80 #include <sys/param.h> 81 #include <sys/jail.h> 82 #include <sys/kernel.h> 83 #include <sys/lock.h> 84 #include <sys/mbuf.h> 85 #include <sys/priv.h> 86 #include <sys/proc.h> 87 #include <sys/protosw.h> 88 #include <sys/sdt.h> 89 #include <sys/signalvar.h> 90 #include <sys/socket.h> 91 #include <sys/socketvar.h> 92 #include <sys/sx.h> 93 #include <sys/sysctl.h> 94 #include <sys/syslog.h> 95 #include <sys/systm.h> 96 97 #include <net/if.h> 98 #include <net/if_var.h> 99 #include <net/if_types.h> 100 #include <net/route.h> 101 #include <net/rss_config.h> 102 103 #include <netinet/in.h> 104 #include <netinet/in_kdtrace.h> 105 #include <netinet/in_pcb.h> 106 #include <netinet/in_systm.h> 107 #include <netinet/in_var.h> 108 #include <netinet/ip.h> 109 #include <netinet/ip6.h> 110 #include <netinet/icmp6.h> 111 #include <netinet/ip_var.h> 112 #include <netinet/udp.h> 113 #include <netinet/udp_var.h> 114 #include <netinet/udplite.h> 115 116 #include <netinet6/ip6protosw.h> 117 #include <netinet6/ip6_var.h> 118 #include <netinet6/in6_pcb.h> 119 #include <netinet6/in6_rss.h> 120 #include <netinet6/udp6_var.h> 121 #include <netinet6/scope6_var.h> 122 123 #include <netipsec/ipsec_support.h> 124 125 #include <security/mac/mac_framework.h> 126 127 /* 128 * UDP protocol implementation. 129 * Per RFC 768, August, 1980. 130 */ 131 132 extern struct protosw inetsw[]; 133 static void udp6_detach(struct socket *so); 134 135 static int 136 udp6_append(struct inpcb *inp, struct mbuf *n, int off, 137 struct sockaddr_in6 *fromsa) 138 { 139 struct socket *so; 140 struct mbuf *opts = NULL, *tmp_opts; 141 struct udpcb *up; 142 143 INP_LOCK_ASSERT(inp); 144 145 /* 146 * Engage the tunneling protocol. 147 */ 148 up = intoudpcb(inp); 149 if (up->u_tun_func != NULL) { 150 in_pcbref(inp); 151 INP_RUNLOCK(inp); 152 (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&fromsa[0], 153 up->u_tun_ctx); 154 INP_RLOCK(inp); 155 return (in_pcbrele_rlocked(inp)); 156 } 157 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 158 /* Check AH/ESP integrity. */ 159 if (IPSEC_ENABLED(ipv6)) { 160 if (IPSEC_CHECK_POLICY(ipv6, n, inp) != 0) { 161 m_freem(n); 162 return (0); 163 } 164 } 165 #endif /* IPSEC */ 166 #ifdef MAC 167 if (mac_inpcb_check_deliver(inp, n) != 0) { 168 m_freem(n); 169 return (0); 170 } 171 #endif 172 opts = NULL; 173 if (inp->inp_flags & INP_CONTROLOPTS || 174 inp->inp_socket->so_options & SO_TIMESTAMP) 175 ip6_savecontrol(inp, n, &opts); 176 if ((inp->inp_vflag & INP_IPV6) && (inp->inp_flags2 & INP_ORIGDSTADDR)) { 177 tmp_opts = sbcreatecontrol((caddr_t)&fromsa[1], 178 sizeof(struct sockaddr_in6), IPV6_ORIGDSTADDR, IPPROTO_IPV6); 179 if (tmp_opts) { 180 if (opts) { 181 tmp_opts->m_next = opts; 182 opts = tmp_opts; 183 } else 184 opts = tmp_opts; 185 } 186 187 } 188 m_adj(n, off + sizeof(struct udphdr)); 189 190 so = inp->inp_socket; 191 SOCKBUF_LOCK(&so->so_rcv); 192 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&fromsa[0], n, 193 opts) == 0) { 194 SOCKBUF_UNLOCK(&so->so_rcv); 195 m_freem(n); 196 if (opts) 197 m_freem(opts); 198 UDPSTAT_INC(udps_fullsock); 199 } else 200 sorwakeup_locked(so); 201 return (0); 202 } 203 204 int 205 udp6_input(struct mbuf **mp, int *offp, int proto) 206 { 207 struct mbuf *m = *mp; 208 struct ifnet *ifp; 209 struct ip6_hdr *ip6; 210 struct udphdr *uh; 211 struct inpcb *inp; 212 struct inpcbinfo *pcbinfo; 213 struct udpcb *up; 214 int off = *offp; 215 int cscov_partial; 216 int plen, ulen; 217 struct epoch_tracker et; 218 struct sockaddr_in6 fromsa[2]; 219 struct m_tag *fwd_tag; 220 uint16_t uh_sum; 221 uint8_t nxt; 222 223 ifp = m->m_pkthdr.rcvif; 224 225 #ifndef PULLDOWN_TEST 226 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE); 227 ip6 = mtod(m, struct ip6_hdr *); 228 uh = (struct udphdr *)((caddr_t)ip6 + off); 229 #else 230 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh)); 231 if (!uh) 232 return (IPPROTO_DONE); 233 ip6 = mtod(m, struct ip6_hdr *); 234 #endif 235 236 UDPSTAT_INC(udps_ipackets); 237 238 /* 239 * Destination port of 0 is illegal, based on RFC768. 240 */ 241 if (uh->uh_dport == 0) 242 goto badunlocked; 243 244 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6); 245 ulen = ntohs((u_short)uh->uh_ulen); 246 247 nxt = proto; 248 cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0; 249 if (nxt == IPPROTO_UDPLITE) { 250 /* Zero means checksum over the complete packet. */ 251 if (ulen == 0) 252 ulen = plen; 253 if (ulen == plen) 254 cscov_partial = 0; 255 if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) { 256 /* XXX: What is the right UDPLite MIB counter? */ 257 goto badunlocked; 258 } 259 if (uh->uh_sum == 0) { 260 /* XXX: What is the right UDPLite MIB counter? */ 261 goto badunlocked; 262 } 263 } else { 264 if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) { 265 UDPSTAT_INC(udps_badlen); 266 goto badunlocked; 267 } 268 if (uh->uh_sum == 0) { 269 UDPSTAT_INC(udps_nosum); 270 goto badunlocked; 271 } 272 } 273 274 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) && 275 !cscov_partial) { 276 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 277 uh_sum = m->m_pkthdr.csum_data; 278 else 279 uh_sum = in6_cksum_pseudo(ip6, ulen, nxt, 280 m->m_pkthdr.csum_data); 281 uh_sum ^= 0xffff; 282 } else 283 uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen); 284 285 if (uh_sum != 0) { 286 UDPSTAT_INC(udps_badsum); 287 goto badunlocked; 288 } 289 290 /* 291 * Construct sockaddr format source address. 292 */ 293 init_sin6(&fromsa[0], m, 0); 294 fromsa[0].sin6_port = uh->uh_sport; 295 init_sin6(&fromsa[1], m, 1); 296 fromsa[1].sin6_port = uh->uh_dport; 297 298 pcbinfo = udp_get_inpcbinfo(nxt); 299 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 300 struct inpcb *last; 301 struct inpcbhead *pcblist; 302 struct ip6_moptions *imo; 303 304 INP_INFO_RLOCK_ET(pcbinfo, et); 305 /* 306 * In the event that laddr should be set to the link-local 307 * address (this happens in RIPng), the multicast address 308 * specified in the received packet will not match laddr. To 309 * handle this situation, matching is relaxed if the 310 * receiving interface is the same as one specified in the 311 * socket and if the destination multicast address matches 312 * one of the multicast groups specified in the socket. 313 */ 314 315 /* 316 * KAME note: traditionally we dropped udpiphdr from mbuf 317 * here. We need udphdr for IPsec processing so we do that 318 * later. 319 */ 320 pcblist = udp_get_pcblist(nxt); 321 last = NULL; 322 CK_LIST_FOREACH(inp, pcblist, inp_list) { 323 if ((inp->inp_vflag & INP_IPV6) == 0) 324 continue; 325 if (inp->inp_lport != uh->uh_dport) 326 continue; 327 if (inp->inp_fport != 0 && 328 inp->inp_fport != uh->uh_sport) 329 continue; 330 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 331 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 332 &ip6->ip6_dst)) 333 continue; 334 } 335 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 336 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 337 &ip6->ip6_src) || 338 inp->inp_fport != uh->uh_sport) 339 continue; 340 } 341 342 /* 343 * XXXRW: Because we weren't holding either the inpcb 344 * or the hash lock when we checked for a match 345 * before, we should probably recheck now that the 346 * inpcb lock is (supposed to be) held. 347 */ 348 349 /* 350 * Handle socket delivery policy for any-source 351 * and source-specific multicast. [RFC3678] 352 */ 353 imo = inp->in6p_moptions; 354 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 355 struct sockaddr_in6 mcaddr; 356 int blocked; 357 358 INP_RLOCK(inp); 359 if (__predict_false(inp->inp_flags2 & INP_FREED)) { 360 INP_RUNLOCK(inp); 361 continue; 362 } 363 364 bzero(&mcaddr, sizeof(struct sockaddr_in6)); 365 mcaddr.sin6_len = sizeof(struct sockaddr_in6); 366 mcaddr.sin6_family = AF_INET6; 367 mcaddr.sin6_addr = ip6->ip6_dst; 368 369 blocked = im6o_mc_filter(imo, ifp, 370 (struct sockaddr *)&mcaddr, 371 (struct sockaddr *)&fromsa[0]); 372 if (blocked != MCAST_PASS) { 373 if (blocked == MCAST_NOTGMEMBER) 374 IP6STAT_INC(ip6s_notmember); 375 if (blocked == MCAST_NOTSMEMBER || 376 blocked == MCAST_MUTED) 377 UDPSTAT_INC(udps_filtermcast); 378 INP_RUNLOCK(inp); /* XXX */ 379 continue; 380 } 381 382 INP_RUNLOCK(inp); 383 } 384 if (last != NULL) { 385 struct mbuf *n; 386 387 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != 388 NULL) { 389 INP_RLOCK(last); 390 if (__predict_true(last->inp_flags2 & INP_FREED) == 0) { 391 if (nxt == IPPROTO_UDPLITE) 392 UDPLITE_PROBE(receive, NULL, last, 393 ip6, last, uh); 394 else 395 UDP_PROBE(receive, NULL, last, 396 ip6, last, uh); 397 if (udp6_append(last, n, off, fromsa)) 398 goto inp_lost; 399 } 400 INP_RUNLOCK(last); 401 } 402 } 403 last = inp; 404 /* 405 * Don't look for additional matches if this one does 406 * not have either the SO_REUSEPORT or SO_REUSEADDR 407 * socket options set. This heuristic avoids 408 * searching through all pcbs in the common case of a 409 * non-shared port. It assumes that an application 410 * will never clear these options after setting them. 411 */ 412 if ((last->inp_socket->so_options & 413 (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) 414 break; 415 } 416 417 if (last == NULL) { 418 /* 419 * No matching pcb found; discard datagram. (No need 420 * to send an ICMP Port Unreachable for a broadcast 421 * or multicast datgram.) 422 */ 423 UDPSTAT_INC(udps_noport); 424 UDPSTAT_INC(udps_noportmcast); 425 goto badheadlocked; 426 } 427 INP_RLOCK(last); 428 if (__predict_true(last->inp_flags2 & INP_FREED) == 0) { 429 if (nxt == IPPROTO_UDPLITE) 430 UDPLITE_PROBE(receive, NULL, last, ip6, last, uh); 431 else 432 UDP_PROBE(receive, NULL, last, ip6, last, uh); 433 if (udp6_append(last, m, off, fromsa) == 0) 434 INP_RUNLOCK(last); 435 } else 436 INP_RUNLOCK(last); 437 inp_lost: 438 INP_INFO_RUNLOCK_ET(pcbinfo, et); 439 return (IPPROTO_DONE); 440 } 441 /* 442 * Locate pcb for datagram. 443 */ 444 445 /* 446 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 447 */ 448 if ((m->m_flags & M_IP6_NEXTHOP) && 449 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 450 struct sockaddr_in6 *next_hop6; 451 452 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 453 454 /* 455 * Transparently forwarded. Pretend to be the destination. 456 * Already got one like this? 457 */ 458 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src, 459 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport, 460 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m); 461 if (!inp) { 462 /* 463 * It's new. Try to find the ambushing socket. 464 * Because we've rewritten the destination address, 465 * any hardware-generated hash is ignored. 466 */ 467 inp = in6_pcblookup(pcbinfo, &ip6->ip6_src, 468 uh->uh_sport, &next_hop6->sin6_addr, 469 next_hop6->sin6_port ? htons(next_hop6->sin6_port) : 470 uh->uh_dport, INPLOOKUP_WILDCARD | 471 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif); 472 } 473 /* Remove the tag from the packet. We don't need it anymore. */ 474 m_tag_delete(m, fwd_tag); 475 m->m_flags &= ~M_IP6_NEXTHOP; 476 } else 477 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src, 478 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport, 479 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, 480 m->m_pkthdr.rcvif, m); 481 if (inp == NULL) { 482 if (udp_log_in_vain) { 483 char ip6bufs[INET6_ADDRSTRLEN]; 484 char ip6bufd[INET6_ADDRSTRLEN]; 485 486 log(LOG_INFO, 487 "Connection attempt to UDP [%s]:%d from [%s]:%d\n", 488 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 489 ntohs(uh->uh_dport), 490 ip6_sprintf(ip6bufs, &ip6->ip6_src), 491 ntohs(uh->uh_sport)); 492 } 493 if (nxt == IPPROTO_UDPLITE) 494 UDPLITE_PROBE(receive, NULL, NULL, ip6, NULL, uh); 495 else 496 UDP_PROBE(receive, NULL, NULL, ip6, NULL, uh); 497 UDPSTAT_INC(udps_noport); 498 if (m->m_flags & M_MCAST) { 499 printf("UDP6: M_MCAST is set in a unicast packet.\n"); 500 UDPSTAT_INC(udps_noportmcast); 501 goto badunlocked; 502 } 503 if (V_udp_blackhole) 504 goto badunlocked; 505 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 506 return (IPPROTO_DONE); 507 } 508 INP_RLOCK_ASSERT(inp); 509 up = intoudpcb(inp); 510 if (cscov_partial) { 511 if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) { 512 INP_RUNLOCK(inp); 513 m_freem(m); 514 return (IPPROTO_DONE); 515 } 516 } 517 if (nxt == IPPROTO_UDPLITE) 518 UDPLITE_PROBE(receive, NULL, inp, ip6, inp, uh); 519 else 520 UDP_PROBE(receive, NULL, inp, ip6, inp, uh); 521 if (udp6_append(inp, m, off, fromsa) == 0) 522 INP_RUNLOCK(inp); 523 return (IPPROTO_DONE); 524 525 badheadlocked: 526 INP_INFO_RUNLOCK_ET(pcbinfo, et); 527 badunlocked: 528 if (m) 529 m_freem(m); 530 return (IPPROTO_DONE); 531 } 532 533 static void 534 udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d, 535 struct inpcbinfo *pcbinfo) 536 { 537 struct udphdr uh; 538 struct ip6_hdr *ip6; 539 struct mbuf *m; 540 int off = 0; 541 struct ip6ctlparam *ip6cp = NULL; 542 const struct sockaddr_in6 *sa6_src = NULL; 543 void *cmdarg; 544 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify; 545 struct udp_portonly { 546 u_int16_t uh_sport; 547 u_int16_t uh_dport; 548 } *uhp; 549 550 if (sa->sa_family != AF_INET6 || 551 sa->sa_len != sizeof(struct sockaddr_in6)) 552 return; 553 554 if ((unsigned)cmd >= PRC_NCMDS) 555 return; 556 if (PRC_IS_REDIRECT(cmd)) 557 notify = in6_rtchange, d = NULL; 558 else if (cmd == PRC_HOSTDEAD) 559 d = NULL; 560 else if (inet6ctlerrmap[cmd] == 0) 561 return; 562 563 /* if the parameter is from icmp6, decode it. */ 564 if (d != NULL) { 565 ip6cp = (struct ip6ctlparam *)d; 566 m = ip6cp->ip6c_m; 567 ip6 = ip6cp->ip6c_ip6; 568 off = ip6cp->ip6c_off; 569 cmdarg = ip6cp->ip6c_cmdarg; 570 sa6_src = ip6cp->ip6c_src; 571 } else { 572 m = NULL; 573 ip6 = NULL; 574 cmdarg = NULL; 575 sa6_src = &sa6_any; 576 } 577 578 if (ip6) { 579 /* 580 * XXX: We assume that when IPV6 is non NULL, 581 * M and OFF are valid. 582 */ 583 584 /* Check if we can safely examine src and dst ports. */ 585 if (m->m_pkthdr.len < off + sizeof(*uhp)) 586 return; 587 588 bzero(&uh, sizeof(uh)); 589 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh); 590 591 if (!PRC_IS_REDIRECT(cmd)) { 592 /* Check to see if its tunneled */ 593 struct inpcb *inp; 594 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst, 595 uh.uh_dport, &ip6->ip6_src, uh.uh_sport, 596 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, 597 m->m_pkthdr.rcvif, m); 598 if (inp != NULL) { 599 struct udpcb *up; 600 601 up = intoudpcb(inp); 602 if (up->u_icmp_func) { 603 /* Yes it is. */ 604 INP_RUNLOCK(inp); 605 (*up->u_icmp_func)(cmd, (struct sockaddr *)ip6cp->ip6c_src, 606 d, up->u_tun_ctx); 607 return; 608 } else { 609 /* Can't find it. */ 610 INP_RUNLOCK(inp); 611 } 612 } 613 } 614 (void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport, 615 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd, 616 cmdarg, notify); 617 } else 618 (void)in6_pcbnotify(pcbinfo, sa, 0, 619 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 620 } 621 622 void 623 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d) 624 { 625 626 return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo)); 627 } 628 629 void 630 udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d) 631 { 632 633 return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo)); 634 } 635 636 static int 637 udp6_getcred(SYSCTL_HANDLER_ARGS) 638 { 639 struct xucred xuc; 640 struct sockaddr_in6 addrs[2]; 641 struct inpcb *inp; 642 int error; 643 644 error = priv_check(req->td, PRIV_NETINET_GETCRED); 645 if (error) 646 return (error); 647 648 if (req->newlen != sizeof(addrs)) 649 return (EINVAL); 650 if (req->oldlen != sizeof(struct xucred)) 651 return (EINVAL); 652 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 653 if (error) 654 return (error); 655 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 || 656 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) { 657 return (error); 658 } 659 inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr, 660 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 661 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL); 662 if (inp != NULL) { 663 INP_RLOCK_ASSERT(inp); 664 if (inp->inp_socket == NULL) 665 error = ENOENT; 666 if (error == 0) 667 error = cr_canseesocket(req->td->td_ucred, 668 inp->inp_socket); 669 if (error == 0) 670 cru2x(inp->inp_cred, &xuc); 671 INP_RUNLOCK(inp); 672 } else 673 error = ENOENT; 674 if (error == 0) 675 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 676 return (error); 677 } 678 679 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0, 680 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection"); 681 682 #define UH_WLOCKED 2 683 #define UH_RLOCKED 1 684 #define UH_UNLOCKED 0 685 static int 686 udp6_output(struct socket *so, int flags_arg, struct mbuf *m, 687 struct sockaddr *addr6, struct mbuf *control, struct thread *td) 688 { 689 struct inpcbinfo *pcbinfo; 690 struct inpcb *inp; 691 struct ip6_hdr *ip6; 692 struct udphdr *udp6; 693 struct in6_addr *laddr, *faddr, in6a; 694 struct ip6_pktopts *optp, opt; 695 struct sockaddr_in6 *sin6, tmp; 696 struct epoch_tracker et; 697 int cscov_partial, error, flags, hlen, scope_ambiguous; 698 u_int32_t ulen, plen; 699 uint16_t cscov; 700 u_short fport; 701 uint8_t nxt, unlock_inp, unlock_udbinfo; 702 703 /* addr6 has been validated in udp6_send(). */ 704 sin6 = (struct sockaddr_in6 *)addr6; 705 706 /* 707 * In contrast to to IPv4 we do not validate the max. packet length 708 * here due to IPv6 Jumbograms (RFC2675). 709 */ 710 711 scope_ambiguous = 0; 712 if (sin6) { 713 /* Protect *addr6 from overwrites. */ 714 tmp = *sin6; 715 sin6 = &tmp; 716 717 /* 718 * Application should provide a proper zone ID or the use of 719 * default zone IDs should be enabled. Unfortunately, some 720 * applications do not behave as it should, so we need a 721 * workaround. Even if an appropriate ID is not determined, 722 * we'll see if we can determine the outgoing interface. If we 723 * can, determine the zone ID based on the interface below. 724 */ 725 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone) 726 scope_ambiguous = 1; 727 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) { 728 if (control) 729 m_freem(control); 730 m_freem(m); 731 return (error); 732 } 733 } 734 735 inp = sotoinpcb(so); 736 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 737 /* 738 * In the following cases we want a write lock on the inp for either 739 * local operations or for possible route cache updates in the IPv6 740 * output path: 741 * - on connected sockets (sin6 is NULL) for route cache updates, 742 * - when we are not bound to an address and source port (it is 743 * in6_pcbsetport() which will require the write lock). 744 */ 745 retry: 746 if (sin6 == NULL || (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && 747 inp->inp_lport == 0)) { 748 INP_WLOCK(inp); 749 /* 750 * In case we lost a race and another thread bound addr/port 751 * on the inp we cannot keep the wlock (which still would be 752 * fine) as further down, based on these values we make 753 * decisions for the pcbinfo lock. If the locks are not in 754 * synch the assertions on unlock will fire, hence we go for 755 * one retry loop. 756 */ 757 if (sin6 != NULL && 758 (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) || 759 inp->inp_lport != 0)) { 760 INP_WUNLOCK(inp); 761 goto retry; 762 } 763 unlock_inp = UH_WLOCKED; 764 } else { 765 INP_RLOCK(inp); 766 unlock_inp = UH_RLOCKED; 767 } 768 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ? 769 IPPROTO_UDP : IPPROTO_UDPLITE; 770 771 #ifdef INET 772 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 773 int hasv4addr; 774 775 if (sin6 == NULL) 776 hasv4addr = (inp->inp_vflag & INP_IPV4); 777 else 778 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) 779 ? 1 : 0; 780 if (hasv4addr) { 781 struct pr_usrreqs *pru; 782 783 /* 784 * XXXRW: We release UDP-layer locks before calling 785 * udp_send() in order to avoid recursion. However, 786 * this does mean there is a short window where inp's 787 * fields are unstable. Could this lead to a 788 * potential race in which the factors causing us to 789 * select the UDPv4 output routine are invalidated? 790 */ 791 if (unlock_inp == UH_WLOCKED) 792 INP_WUNLOCK(inp); 793 else 794 INP_RUNLOCK(inp); 795 if (sin6) 796 in6_sin6_2_sin_in_sock((struct sockaddr *)sin6); 797 pru = inetsw[ip_protox[nxt]].pr_usrreqs; 798 /* addr will just be freed in sendit(). */ 799 return ((*pru->pru_send)(so, flags_arg, m, 800 (struct sockaddr *)sin6, control, td)); 801 } 802 } else 803 #endif 804 if (sin6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 805 /* 806 * Given this is either an IPv6-only socket or no INET is 807 * supported we will fail the send if the given destination 808 * address is a v4mapped address. 809 */ 810 if (unlock_inp == UH_WLOCKED) 811 INP_WUNLOCK(inp); 812 else 813 INP_RUNLOCK(inp); 814 return (EINVAL); 815 } 816 817 if (control) { 818 if ((error = ip6_setpktopts(control, &opt, 819 inp->in6p_outputopts, td->td_ucred, nxt)) != 0) { 820 if (unlock_inp == UH_WLOCKED) 821 INP_WUNLOCK(inp); 822 else 823 INP_RUNLOCK(inp); 824 ip6_clearpktopts(&opt, -1); 825 if (control) 826 m_freem(control); 827 m_freem(m); 828 return (error); 829 } 830 optp = &opt; 831 } else 832 optp = inp->in6p_outputopts; 833 834 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 835 if (sin6 != NULL && 836 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && inp->inp_lport == 0) { 837 INP_HASH_WLOCK(pcbinfo); 838 unlock_udbinfo = UH_WLOCKED; 839 } else if (sin6 != NULL && 840 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 841 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) || 842 inp->inp_lport == 0)) { 843 INP_HASH_RLOCK_ET(pcbinfo, et); 844 unlock_udbinfo = UH_RLOCKED; 845 } else 846 unlock_udbinfo = UH_UNLOCKED; 847 848 if (sin6) { 849 850 /* 851 * Since we saw no essential reason for calling in_pcbconnect, 852 * we get rid of such kind of logic, and call in6_selectsrc 853 * and in6_pcbsetport in order to fill in the local address 854 * and the local port. 855 */ 856 if (sin6->sin6_port == 0) { 857 error = EADDRNOTAVAIL; 858 goto release; 859 } 860 861 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 862 /* how about ::ffff:0.0.0.0 case? */ 863 error = EISCONN; 864 goto release; 865 } 866 867 /* 868 * Given we handle the v4mapped case in the INET block above 869 * assert here that it must not happen anymore. 870 */ 871 KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr), 872 ("%s: sin6(%p)->sin6_addr is v4mapped which we " 873 "should have handled.", __func__, sin6)); 874 875 /* This only requires read-locking. */ 876 error = in6_selectsrc_socket(sin6, optp, inp, 877 td->td_ucred, scope_ambiguous, &in6a, NULL); 878 if (error) 879 goto release; 880 laddr = &in6a; 881 882 if (inp->inp_lport == 0) { 883 884 INP_WLOCK_ASSERT(inp); 885 error = in6_pcbsetport(laddr, inp, td->td_ucred); 886 if (error != 0) { 887 /* Undo an address bind that may have occurred. */ 888 inp->in6p_laddr = in6addr_any; 889 goto release; 890 } 891 } 892 faddr = &sin6->sin6_addr; 893 fport = sin6->sin6_port; /* allow 0 port */ 894 895 } else { 896 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 897 error = ENOTCONN; 898 goto release; 899 } 900 laddr = &inp->in6p_laddr; 901 faddr = &inp->in6p_faddr; 902 fport = inp->inp_fport; 903 } 904 905 ulen = m->m_pkthdr.len; 906 plen = sizeof(struct udphdr) + ulen; 907 hlen = sizeof(struct ip6_hdr); 908 909 /* 910 * Calculate data length and get a mbuf 911 * for UDP and IP6 headers. 912 */ 913 M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT); 914 if (m == NULL) { 915 error = ENOBUFS; 916 goto release; 917 } 918 919 /* 920 * Stuff checksum and output datagram. 921 */ 922 cscov = cscov_partial = 0; 923 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); 924 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */ 925 udp6->uh_dport = fport; 926 if (nxt == IPPROTO_UDPLITE) { 927 struct udpcb *up; 928 929 up = intoudpcb(inp); 930 cscov = up->u_txcslen; 931 if (cscov >= plen) 932 cscov = 0; 933 udp6->uh_ulen = htons(cscov); 934 /* 935 * For UDP-Lite, checksum coverage length of zero means 936 * the entire UDPLite packet is covered by the checksum. 937 */ 938 cscov_partial = (cscov == 0) ? 0 : 1; 939 } else if (plen <= 0xffff) 940 udp6->uh_ulen = htons((u_short)plen); 941 else 942 udp6->uh_ulen = 0; 943 udp6->uh_sum = 0; 944 945 ip6 = mtod(m, struct ip6_hdr *); 946 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK; 947 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 948 ip6->ip6_vfc |= IPV6_VERSION; 949 ip6->ip6_plen = htons((u_short)plen); 950 ip6->ip6_nxt = nxt; 951 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 952 ip6->ip6_src = *laddr; 953 ip6->ip6_dst = *faddr; 954 955 #ifdef MAC 956 mac_inpcb_create_mbuf(inp, m); 957 #endif 958 959 if (cscov_partial) { 960 if ((udp6->uh_sum = in6_cksum_partial(m, nxt, 961 sizeof(struct ip6_hdr), plen, cscov)) == 0) 962 udp6->uh_sum = 0xffff; 963 } else { 964 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0); 965 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 966 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 967 } 968 969 flags = 0; 970 #ifdef RSS 971 { 972 uint32_t hash_val, hash_type; 973 uint8_t pr; 974 975 pr = inp->inp_socket->so_proto->pr_protocol; 976 /* 977 * Calculate an appropriate RSS hash for UDP and 978 * UDP Lite. 979 * 980 * The called function will take care of figuring out 981 * whether a 2-tuple or 4-tuple hash is required based 982 * on the currently configured scheme. 983 * 984 * Later later on connected socket values should be 985 * cached in the inpcb and reused, rather than constantly 986 * re-calculating it. 987 * 988 * UDP Lite is a different protocol number and will 989 * likely end up being hashed as a 2-tuple until 990 * RSS / NICs grow UDP Lite protocol awareness. 991 */ 992 if (rss_proto_software_hash_v6(faddr, laddr, fport, 993 inp->inp_lport, pr, &hash_val, &hash_type) == 0) { 994 m->m_pkthdr.flowid = hash_val; 995 M_HASHTYPE_SET(m, hash_type); 996 } 997 998 /* 999 * Don't override with the inp cached flowid. 1000 * 1001 * Until the whole UDP path is vetted, it may actually 1002 * be incorrect. 1003 */ 1004 flags |= IP_NODEFAULTFLOWID; 1005 } 1006 #endif 1007 1008 UDPSTAT_INC(udps_opackets); 1009 if (unlock_udbinfo == UH_WLOCKED) 1010 INP_HASH_WUNLOCK(pcbinfo); 1011 else if (unlock_udbinfo == UH_RLOCKED) 1012 INP_HASH_RUNLOCK_ET(pcbinfo, et); 1013 if (nxt == IPPROTO_UDPLITE) 1014 UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6); 1015 else 1016 UDP_PROBE(send, NULL, inp, ip6, inp, udp6); 1017 error = ip6_output(m, optp, 1018 (unlock_inp == UH_WLOCKED) ? &inp->inp_route6 : NULL, flags, 1019 inp->in6p_moptions, NULL, inp); 1020 if (unlock_inp == UH_WLOCKED) 1021 INP_WUNLOCK(inp); 1022 else 1023 INP_RUNLOCK(inp); 1024 1025 if (control) { 1026 ip6_clearpktopts(&opt, -1); 1027 m_freem(control); 1028 } 1029 return (error); 1030 1031 release: 1032 if (unlock_udbinfo == UH_WLOCKED) { 1033 KASSERT(unlock_inp == UH_WLOCKED, ("%s: excl udbinfo lock, " 1034 "non-excl inp lock: pcbinfo %p %#x inp %p %#x", 1035 __func__, pcbinfo, unlock_udbinfo, inp, unlock_inp)); 1036 INP_HASH_WUNLOCK(pcbinfo); 1037 INP_WUNLOCK(inp); 1038 } else if (unlock_udbinfo == UH_RLOCKED) { 1039 KASSERT(unlock_inp == UH_RLOCKED, ("%s: non-excl udbinfo lock, " 1040 "excl inp lock: pcbinfo %p %#x inp %p %#x", 1041 __func__, pcbinfo, unlock_udbinfo, inp, unlock_inp)); 1042 INP_HASH_RUNLOCK_ET(pcbinfo, et); 1043 INP_RUNLOCK(inp); 1044 } else if (unlock_inp == UH_WLOCKED) 1045 INP_WUNLOCK(inp); 1046 else 1047 INP_RUNLOCK(inp); 1048 if (control) { 1049 ip6_clearpktopts(&opt, -1); 1050 m_freem(control); 1051 } 1052 m_freem(m); 1053 1054 return (error); 1055 } 1056 1057 static void 1058 udp6_abort(struct socket *so) 1059 { 1060 struct inpcb *inp; 1061 struct inpcbinfo *pcbinfo; 1062 1063 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1064 inp = sotoinpcb(so); 1065 KASSERT(inp != NULL, ("udp6_abort: inp == NULL")); 1066 1067 INP_WLOCK(inp); 1068 #ifdef INET 1069 if (inp->inp_vflag & INP_IPV4) { 1070 struct pr_usrreqs *pru; 1071 uint8_t nxt; 1072 1073 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ? 1074 IPPROTO_UDP : IPPROTO_UDPLITE; 1075 INP_WUNLOCK(inp); 1076 pru = inetsw[ip_protox[nxt]].pr_usrreqs; 1077 (*pru->pru_abort)(so); 1078 return; 1079 } 1080 #endif 1081 1082 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 1083 INP_HASH_WLOCK(pcbinfo); 1084 in6_pcbdisconnect(inp); 1085 inp->in6p_laddr = in6addr_any; 1086 INP_HASH_WUNLOCK(pcbinfo); 1087 soisdisconnected(so); 1088 } 1089 INP_WUNLOCK(inp); 1090 } 1091 1092 static int 1093 udp6_attach(struct socket *so, int proto, struct thread *td) 1094 { 1095 struct inpcb *inp; 1096 struct inpcbinfo *pcbinfo; 1097 int error; 1098 1099 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1100 inp = sotoinpcb(so); 1101 KASSERT(inp == NULL, ("udp6_attach: inp != NULL")); 1102 1103 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1104 error = soreserve(so, udp_sendspace, udp_recvspace); 1105 if (error) 1106 return (error); 1107 } 1108 INP_INFO_WLOCK(pcbinfo); 1109 error = in_pcballoc(so, pcbinfo); 1110 if (error) { 1111 INP_INFO_WUNLOCK(pcbinfo); 1112 return (error); 1113 } 1114 inp = (struct inpcb *)so->so_pcb; 1115 inp->inp_vflag |= INP_IPV6; 1116 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 1117 inp->inp_vflag |= INP_IPV4; 1118 inp->in6p_hops = -1; /* use kernel default */ 1119 inp->in6p_cksum = -1; /* just to be sure */ 1120 /* 1121 * XXX: ugly!! 1122 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 1123 * because the socket may be bound to an IPv6 wildcard address, 1124 * which may match an IPv4-mapped IPv6 address. 1125 */ 1126 inp->inp_ip_ttl = V_ip_defttl; 1127 1128 error = udp_newudpcb(inp); 1129 if (error) { 1130 in_pcbdetach(inp); 1131 in_pcbfree(inp); 1132 INP_INFO_WUNLOCK(pcbinfo); 1133 return (error); 1134 } 1135 INP_WUNLOCK(inp); 1136 INP_INFO_WUNLOCK(pcbinfo); 1137 return (0); 1138 } 1139 1140 static int 1141 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 1142 { 1143 struct inpcb *inp; 1144 struct inpcbinfo *pcbinfo; 1145 int error; 1146 1147 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1148 inp = sotoinpcb(so); 1149 KASSERT(inp != NULL, ("udp6_bind: inp == NULL")); 1150 1151 INP_WLOCK(inp); 1152 INP_HASH_WLOCK(pcbinfo); 1153 inp->inp_vflag &= ~INP_IPV4; 1154 inp->inp_vflag |= INP_IPV6; 1155 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 1156 struct sockaddr_in6 *sin6_p; 1157 1158 sin6_p = (struct sockaddr_in6 *)nam; 1159 1160 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) 1161 inp->inp_vflag |= INP_IPV4; 1162 #ifdef INET 1163 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 1164 struct sockaddr_in sin; 1165 1166 in6_sin6_2_sin(&sin, sin6_p); 1167 inp->inp_vflag |= INP_IPV4; 1168 inp->inp_vflag &= ~INP_IPV6; 1169 error = in_pcbbind(inp, (struct sockaddr *)&sin, 1170 td->td_ucred); 1171 goto out; 1172 } 1173 #endif 1174 } 1175 1176 error = in6_pcbbind(inp, nam, td->td_ucred); 1177 #ifdef INET 1178 out: 1179 #endif 1180 INP_HASH_WUNLOCK(pcbinfo); 1181 INP_WUNLOCK(inp); 1182 return (error); 1183 } 1184 1185 static void 1186 udp6_close(struct socket *so) 1187 { 1188 struct inpcb *inp; 1189 struct inpcbinfo *pcbinfo; 1190 1191 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1192 inp = sotoinpcb(so); 1193 KASSERT(inp != NULL, ("udp6_close: inp == NULL")); 1194 1195 INP_WLOCK(inp); 1196 #ifdef INET 1197 if (inp->inp_vflag & INP_IPV4) { 1198 struct pr_usrreqs *pru; 1199 uint8_t nxt; 1200 1201 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ? 1202 IPPROTO_UDP : IPPROTO_UDPLITE; 1203 INP_WUNLOCK(inp); 1204 pru = inetsw[ip_protox[nxt]].pr_usrreqs; 1205 (*pru->pru_disconnect)(so); 1206 return; 1207 } 1208 #endif 1209 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 1210 INP_HASH_WLOCK(pcbinfo); 1211 in6_pcbdisconnect(inp); 1212 inp->in6p_laddr = in6addr_any; 1213 INP_HASH_WUNLOCK(pcbinfo); 1214 soisdisconnected(so); 1215 } 1216 INP_WUNLOCK(inp); 1217 } 1218 1219 static int 1220 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 1221 { 1222 struct inpcb *inp; 1223 struct inpcbinfo *pcbinfo; 1224 struct sockaddr_in6 *sin6; 1225 int error; 1226 1227 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1228 inp = sotoinpcb(so); 1229 sin6 = (struct sockaddr_in6 *)nam; 1230 KASSERT(inp != NULL, ("udp6_connect: inp == NULL")); 1231 1232 /* 1233 * XXXRW: Need to clarify locking of v4/v6 flags. 1234 */ 1235 INP_WLOCK(inp); 1236 #ifdef INET 1237 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1238 struct sockaddr_in sin; 1239 1240 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 1241 error = EINVAL; 1242 goto out; 1243 } 1244 if ((inp->inp_vflag & INP_IPV4) == 0) { 1245 error = EAFNOSUPPORT; 1246 goto out; 1247 } 1248 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1249 error = EISCONN; 1250 goto out; 1251 } 1252 in6_sin6_2_sin(&sin, sin6); 1253 inp->inp_vflag |= INP_IPV4; 1254 inp->inp_vflag &= ~INP_IPV6; 1255 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr); 1256 if (error != 0) 1257 goto out; 1258 INP_HASH_WLOCK(pcbinfo); 1259 error = in_pcbconnect(inp, (struct sockaddr *)&sin, 1260 td->td_ucred); 1261 INP_HASH_WUNLOCK(pcbinfo); 1262 if (error == 0) 1263 soisconnected(so); 1264 goto out; 1265 } else { 1266 if ((inp->inp_vflag & INP_IPV6) == 0) { 1267 error = EAFNOSUPPORT; 1268 goto out; 1269 } 1270 } 1271 #endif 1272 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 1273 error = EISCONN; 1274 goto out; 1275 } 1276 inp->inp_vflag &= ~INP_IPV4; 1277 inp->inp_vflag |= INP_IPV6; 1278 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr); 1279 if (error != 0) 1280 goto out; 1281 INP_HASH_WLOCK(pcbinfo); 1282 error = in6_pcbconnect(inp, nam, td->td_ucred); 1283 INP_HASH_WUNLOCK(pcbinfo); 1284 if (error == 0) 1285 soisconnected(so); 1286 out: 1287 INP_WUNLOCK(inp); 1288 return (error); 1289 } 1290 1291 static void 1292 udp6_detach(struct socket *so) 1293 { 1294 struct inpcb *inp; 1295 struct inpcbinfo *pcbinfo; 1296 struct udpcb *up; 1297 1298 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1299 inp = sotoinpcb(so); 1300 KASSERT(inp != NULL, ("udp6_detach: inp == NULL")); 1301 1302 INP_INFO_WLOCK(pcbinfo); 1303 INP_WLOCK(inp); 1304 up = intoudpcb(inp); 1305 KASSERT(up != NULL, ("%s: up == NULL", __func__)); 1306 in_pcbdetach(inp); 1307 in_pcbfree(inp); 1308 INP_INFO_WUNLOCK(pcbinfo); 1309 udp_discardcb(up); 1310 } 1311 1312 static int 1313 udp6_disconnect(struct socket *so) 1314 { 1315 struct inpcb *inp; 1316 struct inpcbinfo *pcbinfo; 1317 1318 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol); 1319 inp = sotoinpcb(so); 1320 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL")); 1321 1322 INP_WLOCK(inp); 1323 #ifdef INET 1324 if (inp->inp_vflag & INP_IPV4) { 1325 struct pr_usrreqs *pru; 1326 uint8_t nxt; 1327 1328 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ? 1329 IPPROTO_UDP : IPPROTO_UDPLITE; 1330 INP_WUNLOCK(inp); 1331 pru = inetsw[ip_protox[nxt]].pr_usrreqs; 1332 (void)(*pru->pru_disconnect)(so); 1333 return (0); 1334 } 1335 #endif 1336 1337 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 1338 INP_WUNLOCK(inp); 1339 return (ENOTCONN); 1340 } 1341 1342 INP_HASH_WLOCK(pcbinfo); 1343 in6_pcbdisconnect(inp); 1344 inp->in6p_laddr = in6addr_any; 1345 INP_HASH_WUNLOCK(pcbinfo); 1346 SOCK_LOCK(so); 1347 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1348 SOCK_UNLOCK(so); 1349 INP_WUNLOCK(inp); 1350 return (0); 1351 } 1352 1353 static int 1354 udp6_send(struct socket *so, int flags, struct mbuf *m, 1355 struct sockaddr *addr, struct mbuf *control, struct thread *td) 1356 { 1357 int error; 1358 1359 if (addr) { 1360 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 1361 error = EINVAL; 1362 goto bad; 1363 } 1364 if (addr->sa_family != AF_INET6) { 1365 error = EAFNOSUPPORT; 1366 goto bad; 1367 } 1368 } 1369 1370 return (udp6_output(so, flags, m, addr, control, td)); 1371 1372 bad: 1373 if (control) 1374 m_freem(control); 1375 m_freem(m); 1376 return (error); 1377 } 1378 1379 struct pr_usrreqs udp6_usrreqs = { 1380 .pru_abort = udp6_abort, 1381 .pru_attach = udp6_attach, 1382 .pru_bind = udp6_bind, 1383 .pru_connect = udp6_connect, 1384 .pru_control = in6_control, 1385 .pru_detach = udp6_detach, 1386 .pru_disconnect = udp6_disconnect, 1387 .pru_peeraddr = in6_mapped_peeraddr, 1388 .pru_send = udp6_send, 1389 .pru_shutdown = udp_shutdown, 1390 .pru_sockaddr = in6_mapped_sockaddr, 1391 .pru_soreceive = soreceive_dgram, 1392 .pru_sosend = sosend_dgram, 1393 .pru_sosetlabel = in_pcbsosetlabel, 1394 .pru_close = udp6_close 1395 }; 1396