sctp6_usrreq.c revision 163954
1/*- 2 * Copyright (c) 2001-2006, Cisco Systems, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * a) Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * 10 * b) Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the distribution. 13 * 14 * c) Neither the name of Cisco Systems, Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 * THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30/* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */ 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 163954 2006-11-03 17:21:53Z rrs $"); 33#include "opt_inet.h" 34#include "opt_inet6.h" 35#include "opt_inet.h" 36#include "opt_ipsec.h" 37#include "opt_sctp.h" 38 39#include <sys/param.h> 40#include <sys/kernel.h> 41#include <sys/mbuf.h> 42#include <sys/domain.h> 43#include <sys/protosw.h> 44#include <sys/socket.h> 45#include <sys/malloc.h> 46#include <sys/socketvar.h> 47#include <sys/sysctl.h> 48#include <sys/errno.h> 49#include <sys/stat.h> 50#include <sys/systm.h> 51#include <sys/syslog.h> 52#include <sys/proc.h> 53#include <net/if.h> 54#include <net/route.h> 55#include <net/if_types.h> 56#include <netinet/in.h> 57#include <netinet/in_systm.h> 58#include <netinet/ip.h> 59#include <netinet/in_pcb.h> 60#include <netinet/in_var.h> 61#include <netinet/ip_var.h> 62#include <netinet/sctp_os.h> 63#include <netinet/sctp_pcb.h> 64#include <netinet/sctp_header.h> 65#include <netinet/sctp_var.h> 66#include <netinet/sctputil.h> 67#include <netinet/sctp_output.h> 68#include <netinet/sctp_bsd_addr.h> 69#include <netinet/sctp_input.h> 70#include <netinet/sctp_asconf.h> 71#include <netinet6/ip6_var.h> 72#include <netinet6/scope6_var.h> 73#include <netinet/sctp_bsd_addr.h> 74#include <netinet/ip6.h> 75#include <netinet6/in6_pcb.h> 76#include <netinet/icmp6.h> 77#include <netinet6/sctp6_var.h> 78#include <netinet6/ip6protosw.h> 79#include <netinet6/nd6.h> 80 81#ifdef IPSEC 82#include <netinet6/ipsec.h> 83#endif /* IPSEC */ 84 85#if defined(NFAITH) && NFAITH > 0 86#include <net/if_faith.h> 87#endif 88 89 90 91extern struct protosw inetsw[]; 92 93 94#ifndef in6pcb 95#define in6pcb inpcb 96#endif 97#ifndef sotoin6pcb 98#define sotoin6pcb sotoinpcb 99#endif 100 101 102#ifdef SCTP_DEBUG 103extern u_int32_t sctp_debug_on; 104 105#endif 106 107 108 109extern int sctp_no_csum_on_loopback; 110 111int 112sctp6_input(mp, offp, proto) 113 struct mbuf **mp; 114 int *offp; 115 116 int proto; 117 118{ 119 struct mbuf *m = *mp; 120 struct ip6_hdr *ip6; 121 struct sctphdr *sh; 122 struct sctp_inpcb *in6p = NULL; 123 struct sctp_nets *net; 124 int refcount_up = 0; 125 u_int32_t check, calc_check; 126 struct inpcb *in6p_ip; 127 struct sctp_chunkhdr *ch; 128 int length, mlen, offset, iphlen; 129 u_int8_t ecn_bits; 130 struct sctp_tcb *stcb = NULL; 131 int off = *offp; 132 int s; 133 134 ip6 = mtod(m, struct ip6_hdr *); 135#ifndef PULLDOWN_TEST 136 /* If PULLDOWN_TEST off, must be in a single mbuf. */ 137 IP6_EXTHDR_CHECK(m, off, (int)(sizeof(*sh) + sizeof(*ch)), IPPROTO_DONE); 138 sh = (struct sctphdr *)((caddr_t)ip6 + off); 139 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh)); 140#else 141 /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */ 142 IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, sizeof(*sh) + sizeof(*ch)); 143 if (sh == NULL) { 144 SCTP_STAT_INCR(sctps_hdrops); 145 return IPPROTO_DONE; 146 } 147 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); 148#endif 149 150 iphlen = off; 151 offset = iphlen + sizeof(*sh) + sizeof(*ch); 152 153#if defined(NFAITH) && NFAITH > 0 154 155 if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) { 156 /* XXX send icmp6 host/port unreach? */ 157 goto bad; 158 } 159#endif /* NFAITH defined and > 0 */ 160 SCTP_STAT_INCR(sctps_recvpackets); 161 SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 162#ifdef SCTP_DEBUG 163 if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 164 printf("V6 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len); 165 } 166#endif 167 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 168 /* No multi-cast support in SCTP */ 169 goto bad; 170 } 171 /* destination port of 0 is illegal, based on RFC2960. */ 172 if (sh->dest_port == 0) 173 goto bad; 174 if ((sctp_no_csum_on_loopback == 0) || 175 (m->m_pkthdr.rcvif == NULL) || 176 (m->m_pkthdr.rcvif->if_type != IFT_LOOP)) { 177 /* 178 * we do NOT validate things from the loopback if the sysctl 179 * is set to 1. 180 */ 181 check = sh->checksum; /* save incoming checksum */ 182 if ((check == 0) && (sctp_no_csum_on_loopback)) { 183 /* 184 * special hook for where we got a local address 185 * somehow routed across a non IFT_LOOP type 186 * interface 187 */ 188 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst)) 189 goto sctp_skip_csum; 190 } 191 sh->checksum = 0; /* prepare for calc */ 192 calc_check = sctp_calculate_sum(m, &mlen, iphlen); 193 if (calc_check != check) { 194#ifdef SCTP_DEBUG 195 if (sctp_debug_on & SCTP_DEBUG_INPUT1) { 196 printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%x mlen:%d iphlen:%d\n", 197 calc_check, check, (u_int)m, 198 mlen, iphlen); 199 } 200#endif 201 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 202 sh, ch, &in6p, &net); 203 /* in6p's ref-count increased && stcb locked */ 204 if ((in6p) && (stcb)) { 205 sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 206 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2); 207 } else if ((in6p != NULL) && (stcb == NULL)) { 208 refcount_up = 1; 209 } 210 SCTP_STAT_INCR(sctps_badsum); 211 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 212 goto bad; 213 } 214 sh->checksum = calc_check; 215 } else { 216sctp_skip_csum: 217 mlen = m->m_pkthdr.len; 218 } 219 net = NULL; 220 /* 221 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 222 * IP/SCTP/first chunk header... 223 */ 224 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 225 sh, ch, &in6p, &net); 226 /* in6p's ref-count increased */ 227 if (in6p == NULL) { 228 struct sctp_init_chunk *init_chk, chunk_buf; 229 230 SCTP_STAT_INCR(sctps_noport); 231 if (ch->chunk_type == SCTP_INITIATION) { 232 /* 233 * we do a trick here to get the INIT tag, dig in 234 * and get the tag from the INIT and put it in the 235 * common header. 236 */ 237 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 238 iphlen + sizeof(*sh), sizeof(*init_chk), 239 (u_int8_t *) & chunk_buf); 240 sh->v_tag = init_chk->init.initiate_tag; 241 } 242 sctp_send_abort(m, iphlen, sh, 0, NULL); 243 goto bad; 244 } else if (stcb == NULL) { 245 refcount_up = 1; 246 } 247 in6p_ip = (struct inpcb *)in6p; 248#ifdef IPSEC 249 /* 250 * Check AH/ESP integrity. 251 */ 252 if (in6p->sctp_socket && (ipsec6_in_reject_so(m, in6p->sctp_socket)) { 253/* XXX */ 254 ipsec6stat.in_polvio++; 255 goto bad; 256 } 257#endif /* IPSEC */ 258 259 260 /* 261 * CONTROL chunk processing 262 */ 263 length = ntohs(ip6->ip6_plen) + iphlen; 264 offset -= sizeof(*ch); 265 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff); 266 s = splnet(); 267 (void)sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 268 in6p, stcb, net, ecn_bits); 269 /* inp's ref-count reduced && stcb unlocked */ 270 splx(s); 271 /* XXX this stuff below gets moved to appropriate parts later... */ 272 if (m) 273 m_freem(m); 274 if ((in6p) && refcount_up) { 275 /* reduce ref-count */ 276 SCTP_INP_WLOCK(in6p); 277 SCTP_INP_DECR_REF(in6p); 278 SCTP_INP_WUNLOCK(in6p); 279 } 280 return IPPROTO_DONE; 281 282bad: 283 if (stcb) 284 SCTP_TCB_UNLOCK(stcb); 285 286 if ((in6p) && refcount_up) { 287 /* reduce ref-count */ 288 SCTP_INP_WLOCK(in6p); 289 SCTP_INP_DECR_REF(in6p); 290 SCTP_INP_WUNLOCK(in6p); 291 } 292 if (m) 293 m_freem(m); 294 return IPPROTO_DONE; 295} 296 297 298static void 299sctp6_notify_mbuf(struct sctp_inpcb *inp, 300 struct icmp6_hdr *icmp6, 301 struct sctphdr *sh, 302 struct sctp_tcb *stcb, 303 struct sctp_nets *net) 304{ 305 u_int32_t nxtsz; 306 307 if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 308 (icmp6 == NULL) || (sh == NULL)) { 309 goto out; 310 } 311 /* First do we even look at it? */ 312 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) 313 goto out; 314 315 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) { 316 /* not PACKET TO BIG */ 317 goto out; 318 } 319 /* 320 * ok we need to look closely. We could even get smarter and look at 321 * anyone that we sent to in case we get a different ICMP that tells 322 * us there is no way to reach a host, but for this impl, all we 323 * care about is MTU discovery. 324 */ 325 nxtsz = ntohl(icmp6->icmp6_mtu); 326 /* Stop any PMTU timer */ 327 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL); 328 329 /* Adjust destination size limit */ 330 if (net->mtu > nxtsz) { 331 net->mtu = nxtsz; 332 } 333 /* now what about the ep? */ 334 if (stcb->asoc.smallest_mtu > nxtsz) { 335 struct sctp_tmit_chunk *chk; 336 337 /* Adjust that too */ 338 stcb->asoc.smallest_mtu = nxtsz; 339 /* now off to subtract IP_DF flag if needed */ 340 341 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) { 342 if ((u_int32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { 343 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 344 } 345 } 346 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 347 if ((u_int32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { 348 /* 349 * For this guy we also mark for immediate 350 * resend since we sent to big of chunk 351 */ 352 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 353 if (chk->sent != SCTP_DATAGRAM_RESEND) 354 stcb->asoc.sent_queue_retran_cnt++; 355 chk->sent = SCTP_DATAGRAM_RESEND; 356 chk->rec.data.doing_fast_retransmit = 0; 357 358 chk->sent = SCTP_DATAGRAM_RESEND; 359 /* Clear any time so NO RTT is being done */ 360 chk->sent_rcv_time.tv_sec = 0; 361 chk->sent_rcv_time.tv_usec = 0; 362 stcb->asoc.total_flight -= chk->send_size; 363 net->flight_size -= chk->send_size; 364 } 365 } 366 } 367 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL); 368out: 369 if (stcb) 370 SCTP_TCB_UNLOCK(stcb); 371} 372 373 374void 375sctp6_ctlinput(cmd, pktdst, d) 376 int cmd; 377 struct sockaddr *pktdst; 378 void *d; 379{ 380 struct sctphdr sh; 381 struct ip6ctlparam *ip6cp = NULL; 382 int s, cm; 383 384 if (pktdst->sa_family != AF_INET6 || 385 pktdst->sa_len != sizeof(struct sockaddr_in6)) 386 return; 387 388 if ((unsigned)cmd >= PRC_NCMDS) 389 return; 390 if (PRC_IS_REDIRECT(cmd)) { 391 d = NULL; 392 } else if (inet6ctlerrmap[cmd] == 0) { 393 return; 394 } 395 /* if the parameter is from icmp6, decode it. */ 396 if (d != NULL) { 397 ip6cp = (struct ip6ctlparam *)d; 398 } else { 399 ip6cp = (struct ip6ctlparam *)NULL; 400 } 401 402 if (ip6cp) { 403 /* 404 * XXX: We assume that when IPV6 is non NULL, M and OFF are 405 * valid. 406 */ 407 /* check if we can safely examine src and dst ports */ 408 struct sctp_inpcb *inp = NULL; 409 struct sctp_tcb *stcb = NULL; 410 struct sctp_nets *net = NULL; 411 struct sockaddr_in6 final; 412 413 if (ip6cp->ip6c_m == NULL || 414 (size_t)ip6cp->ip6c_m->m_pkthdr.len < (ip6cp->ip6c_off + sizeof(sh))) 415 return; 416 417 bzero(&sh, sizeof(sh)); 418 bzero(&final, sizeof(final)); 419 inp = NULL; 420 net = NULL; 421 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh), 422 (caddr_t)&sh); 423 ip6cp->ip6c_src->sin6_port = sh.src_port; 424 final.sin6_len = sizeof(final); 425 final.sin6_family = AF_INET6; 426 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr; 427 final.sin6_port = sh.dest_port; 428 s = splnet(); 429 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src, 430 (struct sockaddr *)&final, 431 &inp, &net, 1); 432 /* inp's ref-count increased && stcb locked */ 433 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) { 434 if (cmd == PRC_MSGSIZE) { 435 sctp6_notify_mbuf(inp, 436 ip6cp->ip6c_icmp6, 437 &sh, 438 stcb, 439 net); 440 /* inp's ref-count reduced && stcb unlocked */ 441 } else { 442 if (cmd == PRC_HOSTDEAD) { 443 cm = EHOSTUNREACH; 444 } else { 445 cm = inet6ctlerrmap[cmd]; 446 } 447 sctp_notify(inp, cm, &sh, 448 (struct sockaddr *)&final, 449 stcb, net); 450 /* inp's ref-count reduced && stcb unlocked */ 451 } 452 } else { 453 if (PRC_IS_REDIRECT(cmd) && inp) { 454 in6_rtchange((struct in6pcb *)inp, 455 inet6ctlerrmap[cmd]); 456 } 457 if (inp) { 458 /* reduce inp's ref-count */ 459 SCTP_INP_WLOCK(inp); 460 SCTP_INP_DECR_REF(inp); 461 SCTP_INP_WUNLOCK(inp); 462 } 463 if (stcb) 464 SCTP_TCB_UNLOCK(stcb); 465 } 466 splx(s); 467 } 468} 469 470/* 471 * this routine can probably be collasped into the one in sctp_userreq.c 472 * since they do the same thing and now we lookup with a sockaddr 473 */ 474static int 475sctp6_getcred(SYSCTL_HANDLER_ARGS) 476{ 477 struct sockaddr_in6 addrs[2]; 478 struct sctp_inpcb *inp; 479 struct sctp_nets *net; 480 struct sctp_tcb *stcb; 481 int error, s; 482 483 error = suser(req->td); 484 if (error) 485 return (error); 486 487 if (req->newlen != sizeof(addrs)) 488 return (EINVAL); 489 if (req->oldlen != sizeof(struct ucred)) 490 return (EINVAL); 491 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 492 if (error) 493 return (error); 494 s = splnet(); 495 496 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]), 497 sin6tosa(&addrs[1]), 498 &inp, &net, 1); 499 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { 500 error = ENOENT; 501 if (inp) { 502 SCTP_INP_WLOCK(inp); 503 SCTP_INP_DECR_REF(inp); 504 SCTP_INP_WUNLOCK(inp); 505 } 506 goto out; 507 } 508 error = SYSCTL_OUT(req, inp->sctp_socket->so_cred, 509 sizeof(struct ucred)); 510 511 SCTP_TCB_UNLOCK(stcb); 512out: 513 splx(s); 514 return (error); 515} 516 517SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW, 518 0, 0, 519 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection"); 520 521 522/* This is the same as the sctp_abort() could be made common */ 523static void 524sctp6_abort(struct socket *so) 525{ 526 struct sctp_inpcb *inp; 527 int s; 528 uint32_t flags; 529 530 inp = (struct sctp_inpcb *)so->so_pcb; 531 if (inp == 0) 532 return; 533 s = splnet(); 534sctp_must_try_again: 535 flags = inp->sctp_flags; 536#ifdef SCTP_LOG_CLOSING 537 sctp_log_closing(inp, NULL, 17); 538#endif 539 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 540 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 541#ifdef SCTP_LOG_CLOSING 542 sctp_log_closing(inp, NULL, 16); 543#endif 544 sctp_inpcb_free(inp, 1, 0); 545 SOCK_LOCK(so); 546 so->so_snd.sb_cc = 0; 547 so->so_snd.sb_mb = NULL; 548 so->so_snd.sb_mbcnt = 0; 549 550 /* 551 * same for the rcv ones, they are only here for the 552 * accounting/select. 553 */ 554 so->so_rcv.sb_cc = 0; 555 so->so_rcv.sb_mb = NULL; 556 so->so_rcv.sb_mbcnt = 0; 557 /* 558 * Now null out the reference, we are completely detached. 559 */ 560 so->so_pcb = NULL; 561 SOCK_UNLOCK(so); 562 } else { 563 flags = inp->sctp_flags; 564 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 565 goto sctp_must_try_again; 566 } 567 } 568 splx(s); 569 return; 570} 571 572static int 573sctp6_attach(struct socket *so, int proto, struct thread *p) 574{ 575 struct in6pcb *inp6; 576 int s, error; 577 struct sctp_inpcb *inp; 578 579 inp = (struct sctp_inpcb *)so->so_pcb; 580 if (inp != NULL) 581 return EINVAL; 582 583 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 584 error = soreserve(so, sctp_sendspace, sctp_recvspace); 585 if (error) 586 return error; 587 } 588 s = splnet(); 589 error = sctp_inpcb_alloc(so); 590 splx(s); 591 if (error) 592 return error; 593 inp = (struct sctp_inpcb *)so->so_pcb; 594 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */ 595 inp6 = (struct in6pcb *)inp; 596 597 inp6->inp_vflag |= INP_IPV6; 598 inp6->in6p_hops = -1; /* use kernel default */ 599 inp6->in6p_cksum = -1; /* just to be sure */ 600#ifdef INET 601 /* 602 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6 603 * socket as well, because the socket may be bound to an IPv6 604 * wildcard address, which may match an IPv4-mapped IPv6 address. 605 */ 606 inp6->inp_ip_ttl = ip_defttl; 607#endif 608 /* 609 * Hmm what about the IPSEC stuff that is missing here but in 610 * sctp_attach()? 611 */ 612 return 0; 613} 614 615static int 616sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p) 617{ 618 struct sctp_inpcb *inp; 619 struct in6pcb *inp6; 620 int s, error; 621 622 inp = (struct sctp_inpcb *)so->so_pcb; 623 if (inp == 0) 624 return EINVAL; 625 626 inp6 = (struct in6pcb *)inp; 627 inp6->inp_vflag &= ~INP_IPV4; 628 inp6->inp_vflag |= INP_IPV6; 629 if (addr != NULL && 630 (inp6->inp_flags & IN6P_IPV6_V6ONLY) 631 == 0) { 632 if (addr->sa_family == AF_INET) { 633 /* binding v4 addr to v6 socket, so reset flags */ 634 inp6->inp_vflag |= INP_IPV4; 635 inp6->inp_vflag &= ~INP_IPV6; 636 } else { 637 struct sockaddr_in6 *sin6_p; 638 639 sin6_p = (struct sockaddr_in6 *)addr; 640 641 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { 642 inp6->inp_vflag |= INP_IPV4; 643 } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 644 struct sockaddr_in sin; 645 646 in6_sin6_2_sin(&sin, sin6_p); 647 inp6->inp_vflag |= INP_IPV4; 648 inp6->inp_vflag &= ~INP_IPV6; 649 s = splnet(); 650 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, p); 651 splx(s); 652 return error; 653 } 654 } 655 } else if (addr != NULL) { 656 /* IPV6_V6ONLY socket */ 657 if (addr->sa_family == AF_INET) { 658 /* can't bind v4 addr to v6 only socket! */ 659 return EINVAL; 660 } else { 661 struct sockaddr_in6 *sin6_p; 662 663 sin6_p = (struct sockaddr_in6 *)addr; 664 665 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) 666 /* can't bind v4-mapped addrs either! */ 667 /* NOTE: we don't support SIIT */ 668 return EINVAL; 669 } 670 } 671 s = splnet(); 672 error = sctp_inpcb_bind(so, addr, p); 673 splx(s); 674 return error; 675} 676 677 678static void 679sctp6_close(struct socket *so) 680{ 681 struct sctp_inpcb *inp; 682 uint32_t flags; 683 684 inp = (struct sctp_inpcb *)so->so_pcb; 685 if (inp == 0) 686 return; 687 688 /* 689 * Inform all the lower layer assoc that we are done. 690 */ 691sctp_must_try_again: 692 flags = inp->sctp_flags; 693#ifdef SCTP_LOG_CLOSING 694 sctp_log_closing(inp, NULL, 17); 695#endif 696 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 697 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 698 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) || 699 (so->so_rcv.sb_cc > 0)) { 700#ifdef SCTP_LOG_CLOSING 701 sctp_log_closing(inp, NULL, 13); 702#endif 703 sctp_inpcb_free(inp, 1, 1); 704 } else { 705#ifdef SCTP_LOG_CLOSING 706 sctp_log_closing(inp, NULL, 14); 707#endif 708 sctp_inpcb_free(inp, 0, 1); 709 } 710 /* 711 * The socket is now detached, no matter what the state of 712 * the SCTP association. 713 */ 714 SOCK_LOCK(so); 715 so->so_snd.sb_cc = 0; 716 so->so_snd.sb_mb = NULL; 717 so->so_snd.sb_mbcnt = 0; 718 719 /* 720 * same for the rcv ones, they are only here for the 721 * accounting/select. 722 */ 723 so->so_rcv.sb_cc = 0; 724 so->so_rcv.sb_mb = NULL; 725 so->so_rcv.sb_mbcnt = 0; 726 /* 727 * Now null out the reference, we are completely detached. 728 */ 729 so->so_pcb = NULL; 730 SOCK_UNLOCK(so); 731 } else { 732 flags = inp->sctp_flags; 733 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 734 goto sctp_must_try_again; 735 } 736 } 737 return; 738 739} 740 741 742static int 743sctp6_disconnect(struct socket *so) 744{ 745 struct sctp_inpcb *inp; 746 int s; 747 748 s = splnet(); /* XXX */ 749 inp = (struct sctp_inpcb *)so->so_pcb; 750 if (inp == NULL) { 751 splx(s); 752 return (ENOTCONN); 753 } 754 SCTP_INP_RLOCK(inp); 755 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 756 if (LIST_EMPTY(&inp->sctp_asoc_list)) { 757 /* No connection */ 758 splx(s); 759 SCTP_INP_RUNLOCK(inp); 760 return (ENOTCONN); 761 } else { 762 int some_on_streamwheel = 0; 763 struct sctp_association *asoc; 764 struct sctp_tcb *stcb; 765 766 stcb = LIST_FIRST(&inp->sctp_asoc_list); 767 if (stcb == NULL) { 768 splx(s); 769 SCTP_INP_RUNLOCK(inp); 770 return (EINVAL); 771 } 772 SCTP_TCB_LOCK(stcb); 773 asoc = &stcb->asoc; 774 if (((so->so_options & SO_LINGER) && 775 (so->so_linger == 0)) || 776 (so->so_rcv.sb_cc > 0)) { 777 if (SCTP_GET_STATE(asoc) != 778 SCTP_STATE_COOKIE_WAIT) { 779 /* Left with Data unread */ 780 struct mbuf *err; 781 782 err = NULL; 783 MGET(err, M_DONTWAIT, MT_DATA); 784 if (err) { 785 /* 786 * Fill in the user 787 * initiated abort 788 */ 789 struct sctp_paramhdr *ph; 790 791 ph = mtod(err, struct sctp_paramhdr *); 792 err->m_len = sizeof(struct sctp_paramhdr); 793 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); 794 ph->param_length = htons(err->m_len); 795 } 796 sctp_send_abort_tcb(stcb, err); 797 SCTP_STAT_INCR_COUNTER32(sctps_aborted); 798 } 799 SCTP_INP_RUNLOCK(inp); 800 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 801 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 802 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 803 } 804 sctp_free_assoc(inp, stcb, 0); 805 /* No unlock tcb assoc is gone */ 806 splx(s); 807 return (0); 808 } 809 if (!TAILQ_EMPTY(&asoc->out_wheel)) { 810 /* Check to see if some data queued */ 811 struct sctp_stream_out *outs; 812 813 TAILQ_FOREACH(outs, &asoc->out_wheel, 814 next_spoke) { 815 if (!TAILQ_EMPTY(&outs->outqueue)) { 816 some_on_streamwheel = 1; 817 break; 818 } 819 } 820 } 821 if (TAILQ_EMPTY(&asoc->send_queue) && 822 TAILQ_EMPTY(&asoc->sent_queue) && 823 (some_on_streamwheel == 0)) { 824 /* nothing queued to send, so I'm done... */ 825 if ((SCTP_GET_STATE(asoc) != 826 SCTP_STATE_SHUTDOWN_SENT) && 827 (SCTP_GET_STATE(asoc) != 828 SCTP_STATE_SHUTDOWN_ACK_SENT)) { 829 /* only send SHUTDOWN the first time */ 830 sctp_send_shutdown(stcb, stcb->asoc.primary_destination); 831 sctp_chunk_output(stcb->sctp_ep, stcb, 1); 832 asoc->state = SCTP_STATE_SHUTDOWN_SENT; 833 SCTP_STAT_DECR_GAUGE32(sctps_currestab); 834 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 835 stcb->sctp_ep, stcb, 836 asoc->primary_destination); 837 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 838 stcb->sctp_ep, stcb, 839 asoc->primary_destination); 840 } 841 } else { 842 /* 843 * we still got (or just got) data to send, 844 * so set SHUTDOWN_PENDING 845 */ 846 /* 847 * XXX sockets draft says that MSG_EOF 848 * should be sent with no data. currently, 849 * we will allow user data to be sent first 850 * and move to SHUTDOWN-PENDING 851 */ 852 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; 853 } 854 SCTP_TCB_UNLOCK(stcb); 855 SCTP_INP_RUNLOCK(inp); 856 splx(s); 857 return (0); 858 } 859 } else { 860 /* UDP model does not support this */ 861 SCTP_INP_RUNLOCK(inp); 862 splx(s); 863 return EOPNOTSUPP; 864 } 865} 866 867int 868sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 869 struct mbuf *control, struct thread *p); 870 871 872 873static int 874sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 875 struct mbuf *control, struct thread *p) 876{ 877 struct sctp_inpcb *inp; 878 struct inpcb *in_inp; 879 struct in6pcb *inp6; 880 881#ifdef INET 882 struct sockaddr_in6 *sin6; 883 884#endif /* INET */ 885 /* No SPL needed since sctp_output does this */ 886 887 inp = (struct sctp_inpcb *)so->so_pcb; 888 if (inp == NULL) { 889 if (control) { 890 m_freem(control); 891 control = NULL; 892 } 893 m_freem(m); 894 return EINVAL; 895 } 896 in_inp = (struct inpcb *)inp; 897 inp6 = (struct in6pcb *)inp; 898 /* 899 * For the TCP model we may get a NULL addr, if we are a connected 900 * socket thats ok. 901 */ 902 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) && 903 (addr == NULL)) { 904 goto connected_type; 905 } 906 if (addr == NULL) { 907 m_freem(m); 908 if (control) { 909 m_freem(control); 910 control = NULL; 911 } 912 return (EDESTADDRREQ); 913 } 914#ifdef INET 915 sin6 = (struct sockaddr_in6 *)addr; 916 if ( 917 918 (inp6->inp_flags & IN6P_IPV6_V6ONLY) 919 ) { 920 /* 921 * if IPV6_V6ONLY flag, we discard datagrams destined to a 922 * v4 addr or v4-mapped addr 923 */ 924 if (addr->sa_family == AF_INET) { 925 return EINVAL; 926 } 927 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 928 return EINVAL; 929 } 930 } 931 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 932 if (!ip6_v6only) { 933 struct sockaddr_in sin; 934 935 /* convert v4-mapped into v4 addr and send */ 936 in6_sin6_2_sin(&sin, sin6); 937 return sctp_sendm(so, flags, m, (struct sockaddr *)&sin, 938 control, p); 939 } else { 940 /* mapped addresses aren't enabled */ 941 return EINVAL; 942 } 943 } 944#endif /* INET */ 945connected_type: 946 /* now what about control */ 947 if (control) { 948 if (inp->control) { 949 printf("huh? control set?\n"); 950 m_freem(inp->control); 951 inp->control = NULL; 952 } 953 inp->control = control; 954 } 955 /* add it in possibly */ 956 if ((inp->pkt) && 957 (inp->pkt->m_flags & M_PKTHDR)) { 958 struct mbuf *x; 959 int c_len; 960 961 c_len = 0; 962 /* How big is it */ 963 for (x = m; x; x = x->m_next) { 964 c_len += x->m_len; 965 } 966 inp->pkt->m_pkthdr.len += c_len; 967 } 968 /* Place the data */ 969 if (inp->pkt) { 970 inp->pkt_last->m_next = m; 971 inp->pkt_last = m; 972 } else { 973 inp->pkt_last = inp->pkt = m; 974 } 975 if ( 976 /* FreeBSD and MacOSX uses a flag passed */ 977 ((flags & PRUS_MORETOCOME) == 0) 978 ) { 979 /* 980 * note with the current version this code will only be used 981 * by OpenBSD, NetBSD and FreeBSD have methods for 982 * re-defining sosend() to use sctp_sosend(). One can 983 * optionaly switch back to this code (by changing back the 984 * defininitions but this is not advisable. 985 */ 986 int ret; 987 988 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); 989 inp->pkt = NULL; 990 inp->control = NULL; 991 return (ret); 992 } else { 993 return (0); 994 } 995} 996 997static int 998sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p) 999{ 1000 int s = splnet(); 1001 1002 int error = 0; 1003 struct sctp_inpcb *inp; 1004 struct in6pcb *inp6; 1005 struct sctp_tcb *stcb; 1006 1007#ifdef INET 1008 struct sockaddr_in6 *sin6; 1009 struct sockaddr_storage ss; 1010 1011#endif /* INET */ 1012 1013 inp6 = (struct in6pcb *)so->so_pcb; 1014 inp = (struct sctp_inpcb *)so->so_pcb; 1015 if (inp == 0) { 1016 splx(s); 1017 return (ECONNRESET); /* I made the same as TCP since we are 1018 * not setup? */ 1019 } 1020 SCTP_ASOC_CREATE_LOCK(inp); 1021 SCTP_INP_RLOCK(inp); 1022 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 1023 SCTP_PCB_FLAGS_UNBOUND) { 1024 /* Bind a ephemeral port */ 1025 SCTP_INP_RUNLOCK(inp); 1026 error = sctp6_bind(so, NULL, p); 1027 if (error) { 1028 splx(s); 1029 SCTP_ASOC_CREATE_UNLOCK(inp); 1030 1031 return (error); 1032 } 1033 SCTP_INP_RLOCK(inp); 1034 } 1035 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1036 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 1037 /* We are already connected AND the TCP model */ 1038 splx(s); 1039 SCTP_INP_RUNLOCK(inp); 1040 SCTP_ASOC_CREATE_UNLOCK(inp); 1041 return (EADDRINUSE); 1042 } 1043#ifdef INET 1044 sin6 = (struct sockaddr_in6 *)addr; 1045 if ( 1046 (inp6->inp_flags & IN6P_IPV6_V6ONLY) 1047 ) { 1048 /* 1049 * if IPV6_V6ONLY flag, ignore connections destined to a v4 1050 * addr or v4-mapped addr 1051 */ 1052 if (addr->sa_family == AF_INET) { 1053 splx(s); 1054 SCTP_INP_RUNLOCK(inp); 1055 SCTP_ASOC_CREATE_UNLOCK(inp); 1056 return EINVAL; 1057 } 1058 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1059 splx(s); 1060 SCTP_INP_RUNLOCK(inp); 1061 SCTP_ASOC_CREATE_UNLOCK(inp); 1062 return EINVAL; 1063 } 1064 } 1065 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1066 if (!ip6_v6only) { 1067 /* convert v4-mapped into v4 addr */ 1068 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6); 1069 addr = (struct sockaddr *)&ss; 1070 } else { 1071 /* mapped addresses aren't enabled */ 1072 splx(s); 1073 SCTP_INP_RUNLOCK(inp); 1074 SCTP_ASOC_CREATE_UNLOCK(inp); 1075 return EINVAL; 1076 } 1077 } else 1078#endif /* INET */ 1079 addr = addr; /* for true v6 address case */ 1080 1081 /* Now do we connect? */ 1082 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1083 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1084 if (stcb) 1085 SCTP_TCB_UNLOCK(stcb); 1086 SCTP_INP_RUNLOCK(inp); 1087 } else { 1088 SCTP_INP_RUNLOCK(inp); 1089 SCTP_INP_WLOCK(inp); 1090 SCTP_INP_INCR_REF(inp); 1091 SCTP_INP_WUNLOCK(inp); 1092 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); 1093 if (stcb == NULL) { 1094 SCTP_INP_WLOCK(inp); 1095 SCTP_INP_DECR_REF(inp); 1096 SCTP_INP_WUNLOCK(inp); 1097 } 1098 } 1099 1100 if (stcb != NULL) { 1101 /* Already have or am bring up an association */ 1102 SCTP_ASOC_CREATE_UNLOCK(inp); 1103 SCTP_TCB_UNLOCK(stcb); 1104 splx(s); 1105 return (EALREADY); 1106 } 1107 /* We are GOOD to go */ 1108 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0); 1109 SCTP_ASOC_CREATE_UNLOCK(inp); 1110 if (stcb == NULL) { 1111 /* Gak! no memory */ 1112 splx(s); 1113 return (error); 1114 } 1115 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1116 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1117 /* Set the connected flag so we can queue data */ 1118 soisconnecting(so); 1119 } 1120 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 1121 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1122 1123 /* initialize authentication parameters for the assoc */ 1124 sctp_initialize_auth_params(inp, stcb); 1125 1126 sctp_send_initiate(inp, stcb); 1127 SCTP_TCB_UNLOCK(stcb); 1128 splx(s); 1129 return error; 1130} 1131 1132static int 1133sctp6_getaddr(struct socket *so, struct sockaddr **addr) 1134{ 1135 struct sockaddr_in6 *sin6; 1136 1137 struct sctp_inpcb *inp; 1138 1139 int error; 1140 1141 1142 /* 1143 * Do the malloc first in case it blocks. 1144 */ 1145 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1146 sin6->sin6_family = AF_INET6; 1147 sin6->sin6_len = sizeof(*sin6); 1148 1149 inp = (struct sctp_inpcb *)so->so_pcb; 1150 if (inp == NULL) { 1151 SCTP_FREE_SONAME(sin6); 1152 return ECONNRESET; 1153 } 1154 SCTP_INP_RLOCK(inp); 1155 sin6->sin6_port = inp->sctp_lport; 1156 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1157 /* For the bound all case you get back 0 */ 1158 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1159 struct sctp_tcb *stcb; 1160 struct sockaddr_in6 *sin_a6; 1161 struct sctp_nets *net; 1162 int fnd; 1163 1164 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1165 if (stcb == NULL) { 1166 goto notConn6; 1167 } 1168 fnd = 0; 1169 sin_a6 = NULL; 1170 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1171 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1172 if (sin_a6->sin6_family == AF_INET6) { 1173 fnd = 1; 1174 break; 1175 } 1176 } 1177 if ((!fnd) || (sin_a6 == NULL)) { 1178 /* punt */ 1179 goto notConn6; 1180 } 1181 sin6->sin6_addr = sctp_ipv6_source_address_selection( 1182 inp, stcb, (struct route *)&net->ro, net, 0); 1183 1184 } else { 1185 /* For the bound all case you get back 0 */ 1186 notConn6: 1187 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr)); 1188 } 1189 } else { 1190 /* Take the first IPv6 address in the list */ 1191 struct sctp_laddr *laddr; 1192 int fnd = 0; 1193 1194 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1195 if (laddr->ifa->ifa_addr->sa_family == AF_INET6) { 1196 struct sockaddr_in6 *sin_a; 1197 1198 sin_a = (struct sockaddr_in6 *)laddr->ifa->ifa_addr; 1199 sin6->sin6_addr = sin_a->sin6_addr; 1200 fnd = 1; 1201 break; 1202 } 1203 } 1204 if (!fnd) { 1205 SCTP_FREE_SONAME(sin6); 1206 SCTP_INP_RUNLOCK(inp); 1207 return ENOENT; 1208 } 1209 } 1210 SCTP_INP_RUNLOCK(inp); 1211 /* Scoping things for v6 */ 1212 if ((error = sa6_recoverscope(sin6)) != 0) 1213 return (error); 1214 (*addr) = (struct sockaddr *)sin6; 1215 return (0); 1216} 1217 1218static int 1219sctp6_peeraddr(struct socket *so, struct sockaddr **addr) 1220{ 1221 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr; 1222 1223 int fnd; 1224 struct sockaddr_in6 *sin_a6; 1225 struct sctp_inpcb *inp; 1226 struct sctp_tcb *stcb; 1227 struct sctp_nets *net; 1228 1229 int error; 1230 1231 1232 /* 1233 * Do the malloc first in case it blocks. 1234 */ 1235 inp = (struct sctp_inpcb *)so->so_pcb; 1236 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) { 1237 /* UDP type and listeners will drop out here */ 1238 return (ENOTCONN); 1239 } 1240 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1241 sin6->sin6_family = AF_INET6; 1242 sin6->sin6_len = sizeof(*sin6); 1243 1244 /* We must recapture incase we blocked */ 1245 inp = (struct sctp_inpcb *)so->so_pcb; 1246 if (inp == NULL) { 1247 SCTP_FREE_SONAME(sin6); 1248 return ECONNRESET; 1249 } 1250 SCTP_INP_RLOCK(inp); 1251 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1252 if (stcb) 1253 SCTP_TCB_LOCK(stcb); 1254 SCTP_INP_RUNLOCK(inp); 1255 if (stcb == NULL) { 1256 SCTP_FREE_SONAME(sin6); 1257 return ECONNRESET; 1258 } 1259 fnd = 0; 1260 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1261 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1262 if (sin_a6->sin6_family == AF_INET6) { 1263 fnd = 1; 1264 sin6->sin6_port = stcb->rport; 1265 sin6->sin6_addr = sin_a6->sin6_addr; 1266 break; 1267 } 1268 } 1269 SCTP_TCB_UNLOCK(stcb); 1270 if (!fnd) { 1271 /* No IPv4 address */ 1272 SCTP_FREE_SONAME(sin6); 1273 return ENOENT; 1274 } 1275 if ((error = sa6_recoverscope(sin6)) != 0) 1276 return (error); 1277 *addr = (struct sockaddr *)sin6; 1278 return (0); 1279} 1280 1281static int 1282sctp6_in6getaddr(struct socket *so, struct sockaddr **nam) 1283{ 1284 struct sockaddr *addr; 1285 1286 struct in6pcb *inp6 = sotoin6pcb(so); 1287 int error, s; 1288 1289 if (inp6 == NULL) 1290 return EINVAL; 1291 1292 s = splnet(); 1293 /* allow v6 addresses precedence */ 1294 error = sctp6_getaddr(so, nam); 1295 if (error) { 1296 /* try v4 next if v6 failed */ 1297 error = sctp_ingetaddr(so, nam); 1298 if (error) { 1299 splx(s); 1300 return (error); 1301 } 1302 addr = *nam; 1303 /* if I'm V6ONLY, convert it to v4-mapped */ 1304 if ( 1305 (inp6->inp_flags & IN6P_IPV6_V6ONLY) 1306 ) { 1307 struct sockaddr_in6 sin6; 1308 1309 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1310 memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1311 } 1312 } 1313 splx(s); 1314 return (error); 1315} 1316 1317 1318static int 1319sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam) 1320{ 1321 struct sockaddr *addr = *nam; 1322 1323 struct in6pcb *inp6 = sotoin6pcb(so); 1324 int error, s; 1325 1326 if (inp6 == NULL) 1327 return EINVAL; 1328 1329 s = splnet(); 1330 /* allow v6 addresses precedence */ 1331 error = sctp6_peeraddr(so, nam); 1332 if (error) { 1333 /* try v4 next if v6 failed */ 1334 error = sctp_peeraddr(so, nam); 1335 if (error) { 1336 splx(s); 1337 return (error); 1338 } 1339 /* if I'm V6ONLY, convert it to v4-mapped */ 1340 if ( 1341 (inp6->inp_flags & IN6P_IPV6_V6ONLY) 1342 ) { 1343 struct sockaddr_in6 sin6; 1344 1345 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1346 memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1347 } 1348 } 1349 splx(s); 1350 return error; 1351} 1352 1353struct pr_usrreqs sctp6_usrreqs = { 1354 .pru_abort = sctp6_abort, 1355 .pru_accept = sctp_accept, 1356 .pru_attach = sctp6_attach, 1357 .pru_bind = sctp6_bind, 1358 .pru_connect = sctp6_connect, 1359 .pru_control = in6_control, 1360 .pru_close = sctp6_close, 1361 .pru_detach = sctp6_close, 1362 .pru_sopoll = sopoll_generic, 1363 .pru_disconnect = sctp6_disconnect, 1364 .pru_listen = sctp_listen, 1365 .pru_peeraddr = sctp6_getpeeraddr, 1366 .pru_send = sctp6_send, 1367 .pru_shutdown = sctp_shutdown, 1368 .pru_sockaddr = sctp6_in6getaddr, 1369 .pru_sosend = sctp_sosend, 1370 .pru_soreceive = sctp_soreceive 1371}; 1372