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