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