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