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