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