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