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