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