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