1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * a) Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 14 * b) Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the distribution. 17 * 18 * c) Neither the name of Cisco Systems, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <netinet/sctp_os.h> 36 #ifdef INET6 37 #include <sys/proc.h> 38 #include <netinet/sctp_pcb.h> 39 #include <netinet/sctp_header.h> 40 #include <netinet/sctp_var.h> 41 #include <netinet6/sctp6_var.h> 42 #include <netinet/sctp_sysctl.h> 43 #include <netinet/sctp_output.h> 44 #include <netinet/sctp_uio.h> 45 #include <netinet/sctp_asconf.h> 46 #include <netinet/sctputil.h> 47 #include <netinet/sctp_indata.h> 48 #include <netinet/sctp_timer.h> 49 #include <netinet/sctp_auth.h> 50 #include <netinet/sctp_input.h> 51 #include <netinet/sctp_output.h> 52 #include <netinet/sctp_bsd_addr.h> 53 #include <netinet/sctp_crc32.h> 54 #include <netinet/icmp6.h> 55 #include <netinet/udp.h> 56 57 int 58 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port) 59 { 60 struct mbuf *m; 61 int iphlen; 62 uint32_t vrf_id; 63 uint8_t ecn_bits; 64 struct sockaddr_in6 src, dst; 65 struct ip6_hdr *ip6; 66 struct sctphdr *sh; 67 struct sctp_chunkhdr *ch; 68 int length, offset; 69 uint8_t compute_crc; 70 uint32_t mflowid; 71 uint8_t mflowtype; 72 uint16_t fibnum; 73 74 iphlen = *offp; 75 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) { 76 SCTP_RELEASE_PKT(*i_pak); 77 return (IPPROTO_DONE); 78 } 79 m = SCTP_HEADER_TO_CHAIN(*i_pak); 80 #ifdef SCTP_MBUF_LOGGING 81 /* Log in any input mbufs */ 82 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 83 sctp_log_mbc(m, SCTP_MBUF_INPUT); 84 } 85 #endif 86 #ifdef SCTP_PACKET_LOGGING 87 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { 88 sctp_packet_log(m); 89 } 90 #endif 91 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 92 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n", 93 m->m_pkthdr.len, 94 if_name(m->m_pkthdr.rcvif), 95 (int)m->m_pkthdr.csum_flags, CSUM_BITS); 96 mflowid = m->m_pkthdr.flowid; 97 mflowtype = M_HASHTYPE_GET(m); 98 fibnum = M_GETFIB(m); 99 SCTP_STAT_INCR(sctps_recvpackets); 100 SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 101 /* Get IP, SCTP, and first chunk header together in the first mbuf. */ 102 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); 103 if (m->m_len < offset) { 104 m = m_pullup(m, offset); 105 if (m == NULL) { 106 SCTP_STAT_INCR(sctps_hdrops); 107 return (IPPROTO_DONE); 108 } 109 } 110 ip6 = mtod(m, struct ip6_hdr *); 111 sh = (struct sctphdr *)(mtod(m, caddr_t)+iphlen); 112 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); 113 offset -= sizeof(struct sctp_chunkhdr); 114 memset(&src, 0, sizeof(struct sockaddr_in6)); 115 src.sin6_family = AF_INET6; 116 src.sin6_len = sizeof(struct sockaddr_in6); 117 src.sin6_port = sh->src_port; 118 src.sin6_addr = ip6->ip6_src; 119 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) { 120 goto out; 121 } 122 memset(&dst, 0, sizeof(struct sockaddr_in6)); 123 dst.sin6_family = AF_INET6; 124 dst.sin6_len = sizeof(struct sockaddr_in6); 125 dst.sin6_port = sh->dest_port; 126 dst.sin6_addr = ip6->ip6_dst; 127 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) { 128 goto out; 129 } 130 length = ntohs(ip6->ip6_plen) + iphlen; 131 /* Validate mbuf chain length with IP payload length. */ 132 if (SCTP_HEADER_LEN(m) != length) { 133 SCTPDBG(SCTP_DEBUG_INPUT1, 134 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m)); 135 SCTP_STAT_INCR(sctps_hdrops); 136 goto out; 137 } 138 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 139 goto out; 140 } 141 ecn_bits = IPV6_TRAFFIC_CLASS(ip6); 142 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 143 SCTP_STAT_INCR(sctps_recvhwcrc); 144 compute_crc = 0; 145 } else { 146 SCTP_STAT_INCR(sctps_recvswcrc); 147 compute_crc = 1; 148 } 149 sctp_common_input_processing(&m, iphlen, offset, length, 150 (struct sockaddr *)&src, 151 (struct sockaddr *)&dst, 152 sh, ch, 153 compute_crc, 154 ecn_bits, 155 mflowtype, mflowid, fibnum, 156 vrf_id, port); 157 out: 158 if (m) { 159 sctp_m_freem(m); 160 } 161 return (IPPROTO_DONE); 162 } 163 164 int 165 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED) 166 { 167 return (sctp6_input_with_port(i_pak, offp, 0)); 168 } 169 170 void 171 sctp6_notify(struct sctp_inpcb *inp, 172 struct sctp_tcb *stcb, 173 struct sctp_nets *net, 174 uint8_t icmp6_type, 175 uint8_t icmp6_code, 176 uint32_t next_mtu) 177 { 178 int timer_stopped; 179 180 switch (icmp6_type) { 181 case ICMP6_DST_UNREACH: 182 if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) || 183 (icmp6_code == ICMP6_DST_UNREACH_ADMIN) || 184 (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) || 185 (icmp6_code == ICMP6_DST_UNREACH_ADDR)) { 186 /* Mark the net unreachable. */ 187 if (net->dest_state & SCTP_ADDR_REACHABLE) { 188 /* Ok that destination is not reachable */ 189 net->dest_state &= ~SCTP_ADDR_REACHABLE; 190 net->dest_state &= ~SCTP_ADDR_PF; 191 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 192 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED); 193 } 194 } 195 SCTP_TCB_UNLOCK(stcb); 196 break; 197 case ICMP6_PARAM_PROB: 198 /* Treat it like an ABORT. */ 199 if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) { 200 sctp_abort_notification(stcb, true, false, 0, NULL, SCTP_SO_NOT_LOCKED); 201 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 202 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2); 203 } else { 204 SCTP_TCB_UNLOCK(stcb); 205 } 206 break; 207 case ICMP6_PACKET_TOO_BIG: 208 if (net->dest_state & SCTP_ADDR_NO_PMTUD) { 209 SCTP_TCB_UNLOCK(stcb); 210 break; 211 } 212 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 213 timer_stopped = 1; 214 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, 215 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1); 216 } else { 217 timer_stopped = 0; 218 } 219 /* Update the path MTU. */ 220 if (net->port) { 221 next_mtu -= sizeof(struct udphdr); 222 } 223 if (net->mtu > next_mtu) { 224 net->mtu = next_mtu; 225 if (net->port) { 226 sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu + sizeof(struct udphdr)); 227 } else { 228 sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu); 229 } 230 } 231 /* Update the association MTU */ 232 if (stcb->asoc.smallest_mtu > next_mtu) { 233 sctp_pathmtu_adjustment(stcb, next_mtu, true); 234 } 235 /* Finally, start the PMTU timer if it was running before. */ 236 if (timer_stopped) { 237 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net); 238 } 239 SCTP_TCB_UNLOCK(stcb); 240 break; 241 default: 242 SCTP_TCB_UNLOCK(stcb); 243 break; 244 } 245 } 246 247 void 248 sctp6_ctlinput(struct ip6ctlparam *ip6cp) 249 { 250 struct sctp_inpcb *inp; 251 struct sctp_tcb *stcb; 252 struct sctp_nets *net; 253 struct sctphdr sh; 254 struct sockaddr_in6 src, dst; 255 256 if (icmp6_errmap(ip6cp->ip6c_icmp6) == 0) { 257 return; 258 } 259 260 /* 261 * Check if we can safely examine the ports and the verification tag 262 * of the SCTP common header. 263 */ 264 if (ip6cp->ip6c_m->m_pkthdr.len < 265 (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) { 266 return; 267 } 268 269 /* Copy out the port numbers and the verification tag. */ 270 memset(&sh, 0, sizeof(sh)); 271 m_copydata(ip6cp->ip6c_m, 272 ip6cp->ip6c_off, 273 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t), 274 (caddr_t)&sh); 275 memset(&src, 0, sizeof(struct sockaddr_in6)); 276 src.sin6_family = AF_INET6; 277 src.sin6_len = sizeof(struct sockaddr_in6); 278 src.sin6_port = sh.src_port; 279 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src; 280 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) { 281 return; 282 } 283 memset(&dst, 0, sizeof(struct sockaddr_in6)); 284 dst.sin6_family = AF_INET6; 285 dst.sin6_len = sizeof(struct sockaddr_in6); 286 dst.sin6_port = sh.dest_port; 287 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst; 288 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) { 289 return; 290 } 291 inp = NULL; 292 net = NULL; 293 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst, 294 (struct sockaddr *)&src, 295 &inp, &net, 1, SCTP_DEFAULT_VRFID); 296 if ((stcb != NULL) && 297 (net != NULL) && 298 (inp != NULL)) { 299 /* Check the verification tag */ 300 if (ntohl(sh.v_tag) != 0) { 301 /* 302 * This must be the verification tag used for 303 * sending out packets. We don't consider packets 304 * reflecting the verification tag. 305 */ 306 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) { 307 SCTP_TCB_UNLOCK(stcb); 308 return; 309 } 310 } else { 311 if (ip6cp->ip6c_m->m_pkthdr.len >= 312 ip6cp->ip6c_off + sizeof(struct sctphdr) + 313 sizeof(struct sctp_chunkhdr) + 314 offsetof(struct sctp_init, a_rwnd)) { 315 /* 316 * In this case we can check if we got an 317 * INIT chunk and if the initiate tag 318 * matches. 319 */ 320 uint32_t initiate_tag; 321 uint8_t chunk_type; 322 323 m_copydata(ip6cp->ip6c_m, 324 ip6cp->ip6c_off + 325 sizeof(struct sctphdr), 326 sizeof(uint8_t), 327 (caddr_t)&chunk_type); 328 m_copydata(ip6cp->ip6c_m, 329 ip6cp->ip6c_off + 330 sizeof(struct sctphdr) + 331 sizeof(struct sctp_chunkhdr), 332 sizeof(uint32_t), 333 (caddr_t)&initiate_tag); 334 if ((chunk_type != SCTP_INITIATION) || 335 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) { 336 SCTP_TCB_UNLOCK(stcb); 337 return; 338 } 339 } else { 340 SCTP_TCB_UNLOCK(stcb); 341 return; 342 } 343 } 344 sctp6_notify(inp, stcb, net, 345 ip6cp->ip6c_icmp6->icmp6_type, 346 ip6cp->ip6c_icmp6->icmp6_code, 347 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu)); 348 } else { 349 if ((stcb == NULL) && (inp != NULL)) { 350 /* reduce inp's ref-count */ 351 SCTP_INP_WLOCK(inp); 352 SCTP_INP_DECR_REF(inp); 353 SCTP_INP_WUNLOCK(inp); 354 } 355 if (stcb) { 356 SCTP_TCB_UNLOCK(stcb); 357 } 358 } 359 } 360 361 /* 362 * this routine can probably be collapsed into the one in sctp_userreq.c 363 * since they do the same thing and now we lookup with a sockaddr 364 */ 365 static int 366 sctp6_getcred(SYSCTL_HANDLER_ARGS) 367 { 368 struct xucred xuc; 369 struct sockaddr_in6 addrs[2]; 370 struct sctp_inpcb *inp; 371 struct sctp_nets *net; 372 struct sctp_tcb *stcb; 373 int error; 374 uint32_t vrf_id; 375 376 vrf_id = SCTP_DEFAULT_VRFID; 377 378 if (req->newptr == NULL) 379 return (EINVAL); 380 error = priv_check(req->td, PRIV_NETINET_GETCRED); 381 if (error) 382 return (error); 383 384 if (req->newlen != sizeof(addrs)) { 385 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 386 return (EINVAL); 387 } 388 if (req->oldlen != sizeof(struct ucred)) { 389 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 390 return (EINVAL); 391 } 392 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 393 if (error) 394 return (error); 395 396 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]), 397 sin6tosa(&addrs[0]), 398 &inp, &net, 1, vrf_id); 399 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { 400 if ((inp != NULL) && (stcb == NULL)) { 401 /* reduce ref-count */ 402 SCTP_INP_WLOCK(inp); 403 SCTP_INP_DECR_REF(inp); 404 goto cred_can_cont; 405 } 406 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 407 error = ENOENT; 408 goto out; 409 } 410 SCTP_TCB_UNLOCK(stcb); 411 /* 412 * We use the write lock here, only since in the error leg we need 413 * it. If we used RLOCK, then we would have to 414 * wlock/decr/unlock/rlock. Which in theory could create a hole. 415 * Better to use higher wlock. 416 */ 417 SCTP_INP_WLOCK(inp); 418 cred_can_cont: 419 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket); 420 if (error) { 421 SCTP_INP_WUNLOCK(inp); 422 goto out; 423 } 424 cru2x(inp->sctp_socket->so_cred, &xuc); 425 SCTP_INP_WUNLOCK(inp); 426 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 427 out: 428 return (error); 429 } 430 431 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, 432 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 433 0, 0, sctp6_getcred, "S,ucred", 434 "Get the ucred of a SCTP6 connection"); 435 436 static int 437 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED) 438 { 439 int error; 440 struct sctp_inpcb *inp; 441 uint32_t vrf_id = SCTP_DEFAULT_VRFID; 442 443 inp = (struct sctp_inpcb *)so->so_pcb; 444 if (inp != NULL) { 445 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 446 return (EINVAL); 447 } 448 449 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 450 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace)); 451 if (error) 452 return (error); 453 } 454 error = sctp_inpcb_alloc(so, vrf_id); 455 if (error) 456 return (error); 457 inp = (struct sctp_inpcb *)so->so_pcb; 458 SCTP_INP_WLOCK(inp); 459 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */ 460 461 inp->ip_inp.inp.inp_vflag |= INP_IPV6; 462 inp->ip_inp.inp.in6p_hops = -1; /* use kernel default */ 463 inp->ip_inp.inp.in6p_cksum = -1; /* just to be sure */ 464 #ifdef INET 465 /* 466 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6 467 * socket as well, because the socket may be bound to an IPv6 468 * wildcard address, which may match an IPv4-mapped IPv6 address. 469 */ 470 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl); 471 #endif 472 SCTP_INP_WUNLOCK(inp); 473 return (0); 474 } 475 476 static int 477 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p) 478 { 479 struct sctp_inpcb *inp; 480 int error; 481 u_char vflagsav; 482 483 inp = (struct sctp_inpcb *)so->so_pcb; 484 if (inp == NULL) { 485 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 486 return (EINVAL); 487 } 488 489 if (addr) { 490 switch (addr->sa_family) { 491 #ifdef INET 492 case AF_INET: 493 if (addr->sa_len != sizeof(struct sockaddr_in)) { 494 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 495 return (EINVAL); 496 } 497 break; 498 #endif 499 #ifdef INET6 500 case AF_INET6: 501 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 502 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 503 return (EINVAL); 504 } 505 break; 506 #endif 507 default: 508 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 509 return (EINVAL); 510 } 511 } 512 vflagsav = inp->ip_inp.inp.inp_vflag; 513 inp->ip_inp.inp.inp_vflag &= ~INP_IPV4; 514 inp->ip_inp.inp.inp_vflag |= INP_IPV6; 515 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) { 516 switch (addr->sa_family) { 517 #ifdef INET 518 case AF_INET: 519 /* binding v4 addr to v6 socket, so reset flags */ 520 inp->ip_inp.inp.inp_vflag |= INP_IPV4; 521 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6; 522 break; 523 #endif 524 #ifdef INET6 525 case AF_INET6: 526 { 527 struct sockaddr_in6 *sin6_p; 528 529 sin6_p = (struct sockaddr_in6 *)addr; 530 531 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { 532 inp->ip_inp.inp.inp_vflag |= INP_IPV4; 533 } 534 #ifdef INET 535 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 536 struct sockaddr_in sin; 537 538 in6_sin6_2_sin(&sin, sin6_p); 539 inp->ip_inp.inp.inp_vflag |= INP_IPV4; 540 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6; 541 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p); 542 goto out; 543 } 544 #endif 545 break; 546 } 547 #endif 548 default: 549 break; 550 } 551 } else if (addr != NULL) { 552 struct sockaddr_in6 *sin6_p; 553 554 /* IPV6_V6ONLY socket */ 555 #ifdef INET 556 if (addr->sa_family == AF_INET) { 557 /* can't bind v4 addr to v6 only socket! */ 558 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 559 error = EINVAL; 560 goto out; 561 } 562 #endif 563 sin6_p = (struct sockaddr_in6 *)addr; 564 565 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 566 /* can't bind v4-mapped addrs either! */ 567 /* NOTE: we don't support SIIT */ 568 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 569 error = EINVAL; 570 goto out; 571 } 572 } 573 error = sctp_inpcb_bind(so, addr, NULL, p); 574 out: 575 if (error != 0) 576 inp->ip_inp.inp.inp_vflag = vflagsav; 577 return (error); 578 } 579 580 static void 581 sctp6_close(struct socket *so) 582 { 583 sctp_close(so); 584 } 585 586 /* This could be made common with sctp_detach() since they are identical */ 587 588 int 589 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 590 struct mbuf *control, struct thread *p); 591 592 static int 593 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 594 struct mbuf *control, struct thread *p) 595 { 596 struct sctp_inpcb *inp; 597 598 #ifdef INET 599 struct sockaddr_in6 *sin6; 600 #endif /* INET */ 601 /* No SPL needed since sctp_output does this */ 602 603 inp = (struct sctp_inpcb *)so->so_pcb; 604 if (inp == NULL) { 605 if (control) { 606 SCTP_RELEASE_PKT(control); 607 control = NULL; 608 } 609 SCTP_RELEASE_PKT(m); 610 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 611 return (EINVAL); 612 } 613 /* 614 * For the TCP model we may get a NULL addr, if we are a connected 615 * socket thats ok. 616 */ 617 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) && 618 (addr == NULL)) { 619 goto connected_type; 620 } 621 if (addr == NULL) { 622 SCTP_RELEASE_PKT(m); 623 if (control) { 624 SCTP_RELEASE_PKT(control); 625 control = NULL; 626 } 627 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ); 628 return (EDESTADDRREQ); 629 } 630 switch (addr->sa_family) { 631 #ifdef INET 632 case AF_INET: 633 if (addr->sa_len != sizeof(struct sockaddr_in)) { 634 if (control) { 635 SCTP_RELEASE_PKT(control); 636 control = NULL; 637 } 638 SCTP_RELEASE_PKT(m); 639 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 640 return (EINVAL); 641 } 642 break; 643 #endif 644 #ifdef INET6 645 case AF_INET6: 646 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 647 if (control) { 648 SCTP_RELEASE_PKT(control); 649 control = NULL; 650 } 651 SCTP_RELEASE_PKT(m); 652 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 653 return (EINVAL); 654 } 655 break; 656 #endif 657 default: 658 if (control) { 659 SCTP_RELEASE_PKT(control); 660 control = NULL; 661 } 662 SCTP_RELEASE_PKT(m); 663 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 664 return (EINVAL); 665 } 666 #ifdef INET 667 sin6 = (struct sockaddr_in6 *)addr; 668 if (SCTP_IPV6_V6ONLY(inp)) { 669 /* 670 * if IPV6_V6ONLY flag, we discard datagrams destined to a 671 * v4 addr or v4-mapped addr 672 */ 673 if (addr->sa_family == AF_INET) { 674 if (control) { 675 SCTP_RELEASE_PKT(control); 676 control = NULL; 677 } 678 SCTP_RELEASE_PKT(m); 679 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 680 return (EINVAL); 681 } 682 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 683 if (control) { 684 SCTP_RELEASE_PKT(control); 685 control = NULL; 686 } 687 SCTP_RELEASE_PKT(m); 688 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 689 return (EINVAL); 690 } 691 } 692 if ((addr->sa_family == AF_INET6) && 693 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 694 struct sockaddr_in sin; 695 696 /* convert v4-mapped into v4 addr and send */ 697 in6_sin6_2_sin(&sin, sin6); 698 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p)); 699 } 700 #endif /* INET */ 701 connected_type: 702 /* now what about control */ 703 if (control) { 704 if (inp->control) { 705 SCTP_PRINTF("huh? control set?\n"); 706 SCTP_RELEASE_PKT(inp->control); 707 inp->control = NULL; 708 } 709 inp->control = control; 710 } 711 /* Place the data */ 712 if (inp->pkt) { 713 SCTP_BUF_NEXT(inp->pkt_last) = m; 714 inp->pkt_last = m; 715 } else { 716 inp->pkt_last = inp->pkt = m; 717 } 718 if ( 719 /* FreeBSD and MacOSX uses a flag passed */ 720 ((flags & PRUS_MORETOCOME) == 0) 721 ) { 722 /* 723 * note with the current version this code will only be used 724 * by OpenBSD, NetBSD and FreeBSD have methods for 725 * re-defining sosend() to use sctp_sosend(). One can 726 * optionally switch back to this code (by changing back the 727 * definitions but this is not advisable. 728 */ 729 struct epoch_tracker et; 730 int ret; 731 732 NET_EPOCH_ENTER(et); 733 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); 734 NET_EPOCH_EXIT(et); 735 inp->pkt = NULL; 736 inp->control = NULL; 737 return (ret); 738 } else { 739 return (0); 740 } 741 } 742 743 static int 744 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p) 745 { 746 struct epoch_tracker et; 747 uint32_t vrf_id; 748 int error = 0; 749 struct sctp_inpcb *inp; 750 struct sctp_tcb *stcb; 751 #ifdef INET 752 struct sockaddr_in6 *sin6; 753 union sctp_sockstore store; 754 #endif 755 756 inp = (struct sctp_inpcb *)so->so_pcb; 757 if (inp == NULL) { 758 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 759 return (ECONNRESET); /* I made the same as TCP since we are 760 * not setup? */ 761 } 762 if (addr == NULL) { 763 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 764 return (EINVAL); 765 } 766 switch (addr->sa_family) { 767 #ifdef INET 768 case AF_INET: 769 if (addr->sa_len != sizeof(struct sockaddr_in)) { 770 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 771 return (EINVAL); 772 } 773 break; 774 #endif 775 #ifdef INET6 776 case AF_INET6: 777 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 778 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 779 return (EINVAL); 780 } 781 break; 782 #endif 783 default: 784 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 785 return (EINVAL); 786 } 787 788 vrf_id = inp->def_vrf_id; 789 SCTP_ASOC_CREATE_LOCK(inp); 790 SCTP_INP_RLOCK(inp); 791 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 792 SCTP_PCB_FLAGS_UNBOUND) { 793 /* Bind a ephemeral port */ 794 SCTP_INP_RUNLOCK(inp); 795 error = sctp6_bind(so, NULL, p); 796 if (error) { 797 SCTP_ASOC_CREATE_UNLOCK(inp); 798 799 return (error); 800 } 801 SCTP_INP_RLOCK(inp); 802 } 803 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 804 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 805 /* We are already connected AND the TCP model */ 806 SCTP_INP_RUNLOCK(inp); 807 SCTP_ASOC_CREATE_UNLOCK(inp); 808 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE); 809 return (EADDRINUSE); 810 } 811 #ifdef INET 812 sin6 = (struct sockaddr_in6 *)addr; 813 if (SCTP_IPV6_V6ONLY(inp)) { 814 /* 815 * if IPV6_V6ONLY flag, ignore connections destined to a v4 816 * addr or v4-mapped addr 817 */ 818 if (addr->sa_family == AF_INET) { 819 SCTP_INP_RUNLOCK(inp); 820 SCTP_ASOC_CREATE_UNLOCK(inp); 821 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 822 return (EINVAL); 823 } 824 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 825 SCTP_INP_RUNLOCK(inp); 826 SCTP_ASOC_CREATE_UNLOCK(inp); 827 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 828 return (EINVAL); 829 } 830 } 831 if ((addr->sa_family == AF_INET6) && 832 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 833 /* convert v4-mapped into v4 addr */ 834 in6_sin6_2_sin(&store.sin, sin6); 835 addr = &store.sa; 836 } 837 #endif /* INET */ 838 /* Now do we connect? */ 839 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 840 stcb = LIST_FIRST(&inp->sctp_asoc_list); 841 if (stcb) { 842 SCTP_TCB_LOCK(stcb); 843 } 844 SCTP_INP_RUNLOCK(inp); 845 } else { 846 SCTP_INP_RUNLOCK(inp); 847 SCTP_INP_WLOCK(inp); 848 SCTP_INP_INCR_REF(inp); 849 SCTP_INP_WUNLOCK(inp); 850 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); 851 if (stcb == NULL) { 852 SCTP_INP_WLOCK(inp); 853 SCTP_INP_DECR_REF(inp); 854 SCTP_INP_WUNLOCK(inp); 855 } 856 } 857 858 if (stcb != NULL) { 859 /* Already have or am bring up an association */ 860 SCTP_ASOC_CREATE_UNLOCK(inp); 861 SCTP_TCB_UNLOCK(stcb); 862 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY); 863 return (EALREADY); 864 } 865 /* We are GOOD to go */ 866 stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id, 867 inp->sctp_ep.pre_open_stream_count, 868 inp->sctp_ep.port, p, 869 SCTP_INITIALIZE_AUTH_PARAMS); 870 SCTP_ASOC_CREATE_UNLOCK(inp); 871 if (stcb == NULL) { 872 /* Gak! no memory */ 873 return (error); 874 } 875 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT); 876 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 877 NET_EPOCH_ENTER(et); 878 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 879 SCTP_TCB_UNLOCK(stcb); 880 NET_EPOCH_EXIT(et); 881 return (error); 882 } 883 884 static int 885 sctp6_getaddr(struct socket *so, struct sockaddr *sa) 886 { 887 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; 888 struct sctp_inpcb *inp; 889 uint32_t vrf_id; 890 struct sctp_ifa *sctp_ifa; 891 int error; 892 893 *sin6 = (struct sockaddr_in6 ){ 894 .sin6_len = sizeof(struct sockaddr_in6), 895 .sin6_family = AF_INET6, 896 }; 897 898 inp = (struct sctp_inpcb *)so->so_pcb; 899 if (inp == NULL) { 900 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 901 return (ECONNRESET); 902 } 903 SCTP_INP_RLOCK(inp); 904 sin6->sin6_port = inp->sctp_lport; 905 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 906 /* For the bound all case you get back 0 */ 907 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 908 struct sctp_tcb *stcb; 909 struct sockaddr_in6 *sin_a6; 910 struct sctp_nets *net; 911 int fnd; 912 913 stcb = LIST_FIRST(&inp->sctp_asoc_list); 914 if (stcb == NULL) { 915 SCTP_INP_RUNLOCK(inp); 916 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 917 return (ENOENT); 918 } 919 fnd = 0; 920 sin_a6 = NULL; 921 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 922 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 923 if (sin_a6 == NULL) 924 /* this will make coverity happy */ 925 continue; 926 927 if (sin_a6->sin6_family == AF_INET6) { 928 fnd = 1; 929 break; 930 } 931 } 932 if ((!fnd) || (sin_a6 == NULL)) { 933 /* punt */ 934 SCTP_INP_RUNLOCK(inp); 935 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 936 return (ENOENT); 937 } 938 vrf_id = inp->def_vrf_id; 939 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id); 940 if (sctp_ifa) { 941 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr; 942 } 943 } else { 944 /* For the bound all case you get back 0 */ 945 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr)); 946 } 947 } else { 948 /* Take the first IPv6 address in the list */ 949 struct sctp_laddr *laddr; 950 int fnd = 0; 951 952 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 953 if (laddr->ifa->address.sa.sa_family == AF_INET6) { 954 struct sockaddr_in6 *sin_a; 955 956 sin_a = &laddr->ifa->address.sin6; 957 sin6->sin6_addr = sin_a->sin6_addr; 958 fnd = 1; 959 break; 960 } 961 } 962 if (!fnd) { 963 SCTP_INP_RUNLOCK(inp); 964 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 965 return (ENOENT); 966 } 967 } 968 SCTP_INP_RUNLOCK(inp); 969 /* Scoping things for v6 */ 970 if ((error = sa6_recoverscope(sin6)) != 0) { 971 return (error); 972 } 973 974 return (0); 975 } 976 977 static int 978 sctp6_peeraddr(struct socket *so, struct sockaddr *sa) 979 { 980 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; 981 int fnd; 982 struct sockaddr_in6 *sin_a6; 983 struct sctp_inpcb *inp; 984 struct sctp_tcb *stcb; 985 struct sctp_nets *net; 986 int error; 987 988 *sin6 = (struct sockaddr_in6 ){ 989 .sin6_len = sizeof(struct sockaddr_in6), 990 .sin6_family = AF_INET6, 991 }; 992 993 inp = (struct sctp_inpcb *)so->so_pcb; 994 if ((inp == NULL) || 995 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 996 /* UDP type and listeners will drop out here */ 997 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN); 998 return (ENOTCONN); 999 } 1000 SCTP_INP_RLOCK(inp); 1001 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1002 if (stcb) { 1003 SCTP_TCB_LOCK(stcb); 1004 } 1005 SCTP_INP_RUNLOCK(inp); 1006 if (stcb == NULL) { 1007 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1008 return (ECONNRESET); 1009 } 1010 fnd = 0; 1011 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1012 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1013 if (sin_a6->sin6_family == AF_INET6) { 1014 fnd = 1; 1015 sin6->sin6_port = stcb->rport; 1016 sin6->sin6_addr = sin_a6->sin6_addr; 1017 break; 1018 } 1019 } 1020 SCTP_TCB_UNLOCK(stcb); 1021 if (!fnd) { 1022 /* No IPv4 address */ 1023 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1024 return (ENOENT); 1025 } 1026 if ((error = sa6_recoverscope(sin6)) != 0) { 1027 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error); 1028 return (error); 1029 } 1030 1031 return (0); 1032 } 1033 1034 static int 1035 sctp6_in6getaddr(struct socket *so, struct sockaddr *sa) 1036 { 1037 struct inpcb *inp = sotoinpcb(so); 1038 int error; 1039 1040 if (inp == NULL) { 1041 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1042 return (EINVAL); 1043 } 1044 1045 /* allow v6 addresses precedence */ 1046 error = sctp6_getaddr(so, sa); 1047 #ifdef INET 1048 if (error) { 1049 struct sockaddr_in sin; 1050 1051 /* try v4 next if v6 failed */ 1052 error = sctp_ingetaddr(so, (struct sockaddr *)&sin); 1053 if (error) 1054 return (error); 1055 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa); 1056 } 1057 #endif 1058 return (error); 1059 } 1060 1061 static int 1062 sctp6_getpeeraddr(struct socket *so, struct sockaddr *sa) 1063 { 1064 struct inpcb *inp = sotoinpcb(so); 1065 int error; 1066 1067 if (inp == NULL) { 1068 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1069 return (EINVAL); 1070 } 1071 1072 /* allow v6 addresses precedence */ 1073 error = sctp6_peeraddr(so, sa); 1074 #ifdef INET 1075 if (error) { 1076 struct sockaddr_in sin; 1077 1078 /* try v4 next if v6 failed */ 1079 error = sctp_peeraddr(so, (struct sockaddr *)&sin); 1080 if (error) 1081 return (error); 1082 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa); 1083 } 1084 #endif 1085 return (error); 1086 } 1087 1088 #define SCTP6_PROTOSW \ 1089 .pr_protocol = IPPROTO_SCTP, \ 1090 .pr_ctloutput = sctp_ctloutput, \ 1091 .pr_abort = sctp_abort, \ 1092 .pr_accept = sctp_accept, \ 1093 .pr_attach = sctp6_attach, \ 1094 .pr_bind = sctp6_bind, \ 1095 .pr_connect = sctp6_connect, \ 1096 .pr_control = in6_control, \ 1097 .pr_close = sctp6_close, \ 1098 .pr_detach = sctp6_close, \ 1099 .pr_disconnect = sctp_disconnect, \ 1100 .pr_listen = sctp_listen, \ 1101 .pr_peeraddr = sctp6_getpeeraddr, \ 1102 .pr_send = sctp6_send, \ 1103 .pr_shutdown = sctp_shutdown, \ 1104 .pr_sockaddr = sctp6_in6getaddr, \ 1105 .pr_sosend = sctp_sosend, \ 1106 .pr_soreceive = sctp_soreceive 1107 1108 struct protosw sctp6_seqpacket_protosw = { 1109 .pr_type = SOCK_SEQPACKET, 1110 .pr_flags = PR_WANTRCVD, 1111 SCTP6_PROTOSW 1112 }; 1113 1114 struct protosw sctp6_stream_protosw = { 1115 .pr_type = SOCK_STREAM, 1116 .pr_flags = PR_CONNREQUIRED | PR_WANTRCVD, 1117 SCTP6_PROTOSW 1118 }; 1119 #endif 1120