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