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