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