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