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