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