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