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