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