sctp_usrreq.c revision 171531
1163953Srrs/*- 2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 3163953Srrs * 4163953Srrs * Redistribution and use in source and binary forms, with or without 5163953Srrs * modification, are permitted provided that the following conditions are met: 6163953Srrs * 7163953Srrs * a) Redistributions of source code must retain the above copyright notice, 8163953Srrs * this list of conditions and the following disclaimer. 9163953Srrs * 10163953Srrs * b) Redistributions in binary form must reproduce the above copyright 11163953Srrs * notice, this list of conditions and the following disclaimer in 12163953Srrs * the documentation and/or other materials provided with the distribution. 13163953Srrs * 14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its 15163953Srrs * contributors may be used to endorse or promote products derived 16163953Srrs * from this software without specific prior written permission. 17163953Srrs * 18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE. 29163953Srrs */ 30163953Srrs 31163953Srrs/* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $ */ 32163953Srrs 33163953Srrs#include <sys/cdefs.h> 34163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_usrreq.c 171531 2007-07-21 21:41:32Z rrs $"); 35166086Srrs#include <netinet/sctp_os.h> 36163953Srrs#include <sys/proc.h> 37163953Srrs#include <netinet/sctp_pcb.h> 38163953Srrs#include <netinet/sctp_header.h> 39163953Srrs#include <netinet/sctp_var.h> 40167695Srrs#if defined(INET6) 41167695Srrs#include <netinet6/sctp6_var.h> 42167695Srrs#endif 43167598Srrs#include <netinet/sctp_sysctl.h> 44163953Srrs#include <netinet/sctp_output.h> 45163953Srrs#include <netinet/sctp_uio.h> 46163953Srrs#include <netinet/sctp_asconf.h> 47163953Srrs#include <netinet/sctputil.h> 48163953Srrs#include <netinet/sctp_indata.h> 49163953Srrs#include <netinet/sctp_timer.h> 50163953Srrs#include <netinet/sctp_auth.h> 51170091Srrs#include <netinet/sctp_bsd_addr.h> 52171440Srrs#include <netinet/sctp_cc_functions.h> 53164085Srrs 54163953Srrs 55163953Srrs 56170091Srrs 57163953Srrsvoid 58163953Srrssctp_init(void) 59163953Srrs{ 60163953Srrs /* Init the SCTP pcb in sctp_pcb.c */ 61163953Srrs u_long sb_max_adj; 62163953Srrs 63163953Srrs sctp_pcb_init(); 64163953Srrs 65171440Srrs 66163953Srrs if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE) 67163953Srrs sctp_max_chunks_on_queue = (nmbclusters / 8); 68163953Srrs /* 69163953Srrs * Allow a user to take no more than 1/2 the number of clusters or 70163953Srrs * the SB_MAX whichever is smaller for the send window. 71163953Srrs */ 72163953Srrs sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES)); 73163953Srrs sctp_sendspace = min((min(SB_MAX, sb_max_adj)), 74170056Srrs (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT)); 75163953Srrs /* 76163953Srrs * Now for the recv window, should we take the same amount? or 77163953Srrs * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For 78163953Srrs * now I will just copy. 79163953Srrs */ 80163953Srrs sctp_recvspace = sctp_sendspace; 81163953Srrs 82163953Srrs} 83163953Srrs 84163953Srrs 85166023Srrs 86166023Srrs/* 87166023Srrs * cleanup of the sctppcbinfo structure. 88166023Srrs * Assumes that the sctppcbinfo lock is held. 89166023Srrs */ 90166023Srrsvoid 91166023Srrssctp_pcbinfo_cleanup(void) 92166023Srrs{ 93166023Srrs /* free the hash tables */ 94166023Srrs if (sctppcbinfo.sctp_asochash != NULL) 95166023Srrs SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark); 96166023Srrs if (sctppcbinfo.sctp_ephash != NULL) 97166023Srrs SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark); 98166023Srrs if (sctppcbinfo.sctp_tcpephash != NULL) 99166023Srrs SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark); 100166023Srrs if (sctppcbinfo.sctp_restarthash != NULL) 101166023Srrs SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark); 102166023Srrs} 103166023Srrs 104163953Srrs 105163953Srrsstatic void 106167695Srrssctp_pathmtu_adjustment(struct sctp_inpcb *inp, 107163953Srrs struct sctp_tcb *stcb, 108163953Srrs struct sctp_nets *net, 109163953Srrs uint16_t nxtsz) 110163953Srrs{ 111163953Srrs struct sctp_tmit_chunk *chk; 112163953Srrs 113163953Srrs /* Adjust that too */ 114163953Srrs stcb->asoc.smallest_mtu = nxtsz; 115163953Srrs /* now off to subtract IP_DF flag if needed */ 116169352Srrs#ifdef SCTP_PRINT_FOR_B_AND_M 117169420Srrs SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n", 118169352Srrs inp, stcb, net, nxtsz); 119169352Srrs#endif 120163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) { 121163953Srrs if ((chk->send_size + IP_HDR_SIZE) > nxtsz) { 122163953Srrs chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 123163953Srrs } 124163953Srrs } 125163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 126163953Srrs if ((chk->send_size + IP_HDR_SIZE) > nxtsz) { 127163953Srrs /* 128163953Srrs * For this guy we also mark for immediate resend 129163953Srrs * since we sent to big of chunk 130163953Srrs */ 131163953Srrs chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 132163953Srrs if (chk->sent != SCTP_DATAGRAM_RESEND) { 133163953Srrs sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 134163953Srrs } 135163953Srrs chk->sent = SCTP_DATAGRAM_RESEND; 136163953Srrs chk->rec.data.doing_fast_retransmit = 0; 137170744Srrs if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) { 138170744Srrs sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU, 139170744Srrs chk->whoTo->flight_size, 140170744Srrs chk->book_size, 141170744Srrs (uintptr_t) chk->whoTo, 142170744Srrs chk->rec.data.TSN_seq); 143170744Srrs } 144163953Srrs /* Clear any time so NO RTT is being done */ 145163953Srrs chk->do_rtt = 0; 146168709Srrs sctp_flight_size_decrease(chk); 147168709Srrs sctp_total_flight_decrease(stcb, chk); 148163953Srrs } 149163953Srrs } 150163953Srrs} 151163953Srrs 152163953Srrsstatic void 153163953Srrssctp_notify_mbuf(struct sctp_inpcb *inp, 154163953Srrs struct sctp_tcb *stcb, 155163953Srrs struct sctp_nets *net, 156163953Srrs struct ip *ip, 157163953Srrs struct sctphdr *sh) 158163953Srrs{ 159163953Srrs struct icmp *icmph; 160163953Srrs int totsz, tmr_stopped = 0; 161163953Srrs uint16_t nxtsz; 162163953Srrs 163163953Srrs /* protection */ 164163953Srrs if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 165163953Srrs (ip == NULL) || (sh == NULL)) { 166169420Srrs if (stcb != NULL) { 167163953Srrs SCTP_TCB_UNLOCK(stcb); 168169420Srrs } 169163953Srrs return; 170163953Srrs } 171163953Srrs /* First job is to verify the vtag matches what I would send */ 172163953Srrs if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) { 173163953Srrs SCTP_TCB_UNLOCK(stcb); 174163953Srrs return; 175163953Srrs } 176163953Srrs icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) - 177163953Srrs sizeof(struct ip))); 178163953Srrs if (icmph->icmp_type != ICMP_UNREACH) { 179163953Srrs /* We only care about unreachable */ 180163953Srrs SCTP_TCB_UNLOCK(stcb); 181163953Srrs return; 182163953Srrs } 183163953Srrs if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) { 184163953Srrs /* not a unreachable message due to frag. */ 185163953Srrs SCTP_TCB_UNLOCK(stcb); 186163953Srrs return; 187163953Srrs } 188163953Srrs totsz = ip->ip_len; 189163953Srrs 190163953Srrs nxtsz = ntohs(icmph->icmp_seq); 191163953Srrs if (nxtsz == 0) { 192163953Srrs /* 193163953Srrs * old type router that does not tell us what the next size 194163953Srrs * mtu is. Rats we will have to guess (in a educated fashion 195163953Srrs * of course) 196163953Srrs */ 197163953Srrs nxtsz = find_next_best_mtu(totsz); 198163953Srrs } 199163953Srrs /* Stop any PMTU timer */ 200165647Srrs if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 201163953Srrs tmr_stopped = 1; 202165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, 203165220Srrs SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1); 204163953Srrs } 205163953Srrs /* Adjust destination size limit */ 206163953Srrs if (net->mtu > nxtsz) { 207163953Srrs net->mtu = nxtsz; 208163953Srrs } 209163953Srrs /* now what about the ep? */ 210163953Srrs if (stcb->asoc.smallest_mtu > nxtsz) { 211169352Srrs#ifdef SCTP_PRINT_FOR_B_AND_M 212169420Srrs SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n", 213169352Srrs nxtsz); 214169352Srrs#endif 215167695Srrs sctp_pathmtu_adjustment(inp, stcb, net, nxtsz); 216163953Srrs } 217163953Srrs if (tmr_stopped) 218163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net); 219163953Srrs 220163953Srrs SCTP_TCB_UNLOCK(stcb); 221163953Srrs} 222163953Srrs 223163953Srrs 224163953Srrsvoid 225163953Srrssctp_notify(struct sctp_inpcb *inp, 226167695Srrs int error, 227163953Srrs struct sctphdr *sh, 228163953Srrs struct sockaddr *to, 229163953Srrs struct sctp_tcb *stcb, 230163953Srrs struct sctp_nets *net) 231163953Srrs{ 232163953Srrs /* protection */ 233163953Srrs if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 234163953Srrs (sh == NULL) || (to == NULL)) { 235163953Srrs return; 236163953Srrs } 237163953Srrs /* First job is to verify the vtag matches what I would send */ 238163953Srrs if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) { 239163953Srrs return; 240163953Srrs } 241163953Srrs /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */ 242163953Srrs 243167695Srrs if ((error == EHOSTUNREACH) || /* Host is not reachable */ 244167695Srrs (error == EHOSTDOWN) || /* Host is down */ 245167695Srrs (error == ECONNREFUSED) || /* Host refused the connection, (not 246163953Srrs * an abort?) */ 247167695Srrs (error == ENOPROTOOPT) /* SCTP is not present on host */ 248163953Srrs ) { 249163953Srrs /* 250163953Srrs * Hmm reachablity problems we must examine closely. If its 251163953Srrs * not reachable, we may have lost a network. Or if there is 252163953Srrs * NO protocol at the other end named SCTP. well we consider 253163953Srrs * it a OOTB abort. 254163953Srrs */ 255167695Srrs if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) { 256163953Srrs if (net->dest_state & SCTP_ADDR_REACHABLE) { 257163953Srrs /* Ok that destination is NOT reachable */ 258169420Srrs SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n", 259167598Srrs net->error_count, 260167598Srrs net->failure_threshold, 261167598Srrs net); 262167598Srrs 263163953Srrs net->dest_state &= ~SCTP_ADDR_REACHABLE; 264163953Srrs net->dest_state |= SCTP_ADDR_NOT_REACHABLE; 265171440Srrs /* 266171440Srrs * JRS 5/14/07 - If a destination is 267171440Srrs * unreachable, the PF bit is turned off. 268171440Srrs * This allows an unambiguous use of the PF 269171440Srrs * bit for destinations that are reachable 270171440Srrs * but potentially failed. If the 271171440Srrs * destination is set to the unreachable 272171440Srrs * state, also set the destination to the PF 273171440Srrs * state. 274171440Srrs */ 275171440Srrs /* 276171440Srrs * Add debug message here if destination is 277171440Srrs * not in PF state. 278171440Srrs */ 279171440Srrs /* Stop any running T3 timers here? */ 280171477Srrs if (sctp_cmt_on_off && sctp_cmt_pf) { 281171440Srrs net->dest_state &= ~SCTP_ADDR_PF; 282171440Srrs SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n", 283171440Srrs net); 284171440Srrs } 285163953Srrs net->error_count = net->failure_threshold + 1; 286163953Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 287163953Srrs stcb, SCTP_FAILED_THRESHOLD, 288163953Srrs (void *)net); 289163953Srrs } 290169420Srrs if (stcb) { 291163953Srrs SCTP_TCB_UNLOCK(stcb); 292169420Srrs } 293163953Srrs } else { 294163953Srrs /* 295163953Srrs * Here the peer is either playing tricks on us, 296163953Srrs * including an address that belongs to someone who 297163953Srrs * does not support SCTP OR was a userland 298163953Srrs * implementation that shutdown and now is dead. In 299163953Srrs * either case treat it like a OOTB abort with no 300163953Srrs * TCB 301163953Srrs */ 302163953Srrs sctp_abort_notification(stcb, SCTP_PEER_FAULTY); 303165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2); 304163953Srrs /* no need to unlock here, since the TCB is gone */ 305163953Srrs } 306163953Srrs } else { 307163953Srrs /* Send all others to the app */ 308169420Srrs if (stcb) { 309163953Srrs SCTP_TCB_UNLOCK(stcb); 310169420Srrs } 311163953Srrs if (inp->sctp_socket) { 312163953Srrs#ifdef SCTP_LOCK_LOGGING 313170744Srrs if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) { 314170744Srrs sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCK); 315170744Srrs } 316163953Srrs#endif 317163953Srrs SOCK_LOCK(inp->sctp_socket); 318167695Srrs inp->sctp_socket->so_error = error; 319163953Srrs sctp_sowwakeup(inp, inp->sctp_socket); 320163953Srrs SOCK_UNLOCK(inp->sctp_socket); 321163953Srrs } 322163953Srrs } 323163953Srrs} 324163953Srrs 325163953Srrsvoid 326163953Srrssctp_ctlinput(cmd, sa, vip) 327163953Srrs int cmd; 328163953Srrs struct sockaddr *sa; 329163953Srrs void *vip; 330163953Srrs{ 331163953Srrs struct ip *ip = vip; 332163953Srrs struct sctphdr *sh; 333167598Srrs uint32_t vrf_id; 334163953Srrs 335168299Srrs /* FIX, for non-bsd is this right? */ 336167598Srrs vrf_id = SCTP_DEFAULT_VRFID; 337163953Srrs if (sa->sa_family != AF_INET || 338163953Srrs ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) { 339163953Srrs return; 340163953Srrs } 341163953Srrs if (PRC_IS_REDIRECT(cmd)) { 342163953Srrs ip = 0; 343163953Srrs } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) { 344163953Srrs return; 345163953Srrs } 346163953Srrs if (ip) { 347163953Srrs struct sctp_inpcb *inp = NULL; 348163953Srrs struct sctp_tcb *stcb = NULL; 349163953Srrs struct sctp_nets *net = NULL; 350163953Srrs struct sockaddr_in to, from; 351163953Srrs 352163953Srrs sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 353163953Srrs bzero(&to, sizeof(to)); 354163953Srrs bzero(&from, sizeof(from)); 355163953Srrs from.sin_family = to.sin_family = AF_INET; 356163953Srrs from.sin_len = to.sin_len = sizeof(to); 357163953Srrs from.sin_port = sh->src_port; 358163953Srrs from.sin_addr = ip->ip_src; 359163953Srrs to.sin_port = sh->dest_port; 360163953Srrs to.sin_addr = ip->ip_dst; 361163953Srrs 362163953Srrs /* 363163953Srrs * 'to' holds the dest of the packet that failed to be sent. 364163953Srrs * 'from' holds our local endpoint address. Thus we reverse 365163953Srrs * the to and the from in the lookup. 366163953Srrs */ 367163953Srrs stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from, 368163953Srrs (struct sockaddr *)&to, 369167598Srrs &inp, &net, 1, vrf_id); 370163953Srrs if (stcb != NULL && inp && (inp->sctp_socket != NULL)) { 371163953Srrs if (cmd != PRC_MSGSIZE) { 372163953Srrs int cm; 373163953Srrs 374163953Srrs if (cmd == PRC_HOSTDEAD) { 375163953Srrs cm = EHOSTUNREACH; 376163953Srrs } else { 377163953Srrs cm = inetctlerrmap[cmd]; 378163953Srrs } 379163953Srrs sctp_notify(inp, cm, sh, 380163953Srrs (struct sockaddr *)&to, stcb, 381163953Srrs net); 382163953Srrs } else { 383163953Srrs /* handle possible ICMP size messages */ 384163953Srrs sctp_notify_mbuf(inp, stcb, net, ip, sh); 385163953Srrs } 386163953Srrs } else { 387163953Srrs if ((stcb == NULL) && (inp != NULL)) { 388163953Srrs /* reduce ref-count */ 389163953Srrs SCTP_INP_WLOCK(inp); 390163953Srrs SCTP_INP_DECR_REF(inp); 391163953Srrs SCTP_INP_WUNLOCK(inp); 392163953Srrs } 393163953Srrs } 394163953Srrs } 395163953Srrs return; 396163953Srrs} 397163953Srrs 398163953Srrsstatic int 399163953Srrssctp_getcred(SYSCTL_HANDLER_ARGS) 400163953Srrs{ 401164085Srrs struct xucred xuc; 402163953Srrs struct sockaddr_in addrs[2]; 403163953Srrs struct sctp_inpcb *inp; 404163953Srrs struct sctp_nets *net; 405163953Srrs struct sctp_tcb *stcb; 406164085Srrs int error; 407167598Srrs uint32_t vrf_id; 408163953Srrs 409168299Srrs /* FIX, for non-bsd is this right? */ 410167598Srrs vrf_id = SCTP_DEFAULT_VRFID; 411168299Srrs 412170587Srwatson error = priv_check(req->td, PRIV_NETINET_GETCRED); 413170587Srwatson 414163953Srrs if (error) 415163953Srrs return (error); 416164039Srwatson 417163953Srrs error = SYSCTL_IN(req, addrs, sizeof(addrs)); 418163953Srrs if (error) 419163953Srrs return (error); 420163953Srrs 421163953Srrs stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]), 422163953Srrs sintosa(&addrs[1]), 423167598Srrs &inp, &net, 1, vrf_id); 424163953Srrs if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { 425163953Srrs if ((inp != NULL) && (stcb == NULL)) { 426163953Srrs /* reduce ref-count */ 427163953Srrs SCTP_INP_WLOCK(inp); 428163953Srrs SCTP_INP_DECR_REF(inp); 429164085Srrs goto cred_can_cont; 430163953Srrs } 431163953Srrs error = ENOENT; 432163953Srrs goto out; 433163953Srrs } 434163953Srrs SCTP_TCB_UNLOCK(stcb); 435164085Srrs /* 436164085Srrs * We use the write lock here, only since in the error leg we need 437164085Srrs * it. If we used RLOCK, then we would have to 438164085Srrs * wlock/decr/unlock/rlock. Which in theory could create a hole. 439164085Srrs * Better to use higher wlock. 440164085Srrs */ 441164085Srrs SCTP_INP_WLOCK(inp); 442164085Srrscred_can_cont: 443164085Srrs error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket); 444164085Srrs if (error) { 445164085Srrs SCTP_INP_WUNLOCK(inp); 446164085Srrs goto out; 447164085Srrs } 448164085Srrs cru2x(inp->sctp_socket->so_cred, &xuc); 449164085Srrs SCTP_INP_WUNLOCK(inp); 450164085Srrs error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 451163953Srrsout: 452163953Srrs return (error); 453163953Srrs} 454163953Srrs 455163953SrrsSYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW, 456163953Srrs 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection"); 457163953Srrs 458163953Srrs 459163953Srrsstatic void 460163953Srrssctp_abort(struct socket *so) 461163953Srrs{ 462163953Srrs struct sctp_inpcb *inp; 463163953Srrs uint32_t flags; 464163953Srrs 465163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 466163953Srrs if (inp == 0) 467163953Srrs return; 468163953Srrs 469163953Srrssctp_must_try_again: 470163953Srrs flags = inp->sctp_flags; 471163953Srrs#ifdef SCTP_LOG_CLOSING 472163953Srrs sctp_log_closing(inp, NULL, 17); 473163953Srrs#endif 474163953Srrs if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 475163953Srrs (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 476163953Srrs#ifdef SCTP_LOG_CLOSING 477163953Srrs sctp_log_closing(inp, NULL, 16); 478163953Srrs#endif 479169380Srrs sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 480169380Srrs SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 481163953Srrs SOCK_LOCK(so); 482167695Srrs SCTP_SB_CLEAR(so->so_snd); 483163953Srrs /* 484163953Srrs * same for the rcv ones, they are only here for the 485163953Srrs * accounting/select. 486163953Srrs */ 487167695Srrs SCTP_SB_CLEAR(so->so_rcv); 488167695Srrs 489167695Srrs /* Now null out the reference, we are completely detached. */ 490163953Srrs so->so_pcb = NULL; 491163953Srrs SOCK_UNLOCK(so); 492163953Srrs } else { 493163953Srrs flags = inp->sctp_flags; 494163953Srrs if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 495163953Srrs goto sctp_must_try_again; 496163953Srrs } 497163953Srrs } 498163953Srrs return; 499163953Srrs} 500163953Srrs 501163953Srrsstatic int 502163953Srrssctp_attach(struct socket *so, int proto, struct thread *p) 503163953Srrs{ 504163953Srrs struct sctp_inpcb *inp; 505163953Srrs struct inpcb *ip_inp; 506166086Srrs int error; 507170205Srrs uint32_t vrf_id = SCTP_DEFAULT_VRFID; 508163953Srrs 509171167Sgnn#ifdef IPSEC 510163953Srrs uint32_t flags; 511171440Srrs 512163953Srrs#endif 513163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 514163953Srrs if (inp != 0) { 515163953Srrs return EINVAL; 516163953Srrs } 517167695Srrs error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace); 518163953Srrs if (error) { 519163953Srrs return error; 520163953Srrs } 521170205Srrs error = sctp_inpcb_alloc(so, vrf_id); 522163953Srrs if (error) { 523163953Srrs return error; 524163953Srrs } 525163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 526163953Srrs SCTP_INP_WLOCK(inp); 527163953Srrs 528163953Srrs inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */ 529163953Srrs ip_inp = &inp->ip_inp.inp; 530163953Srrs ip_inp->inp_vflag |= INP_IPV4; 531163953Srrs ip_inp->inp_ip_ttl = ip_defttl; 532163953Srrs 533171167Sgnn#ifdef IPSEC 534171133Sgnn error = ipsec_init_policy(so, &ip_inp->inp_sp); 535163953Srrs#ifdef SCTP_LOG_CLOSING 536163953Srrs sctp_log_closing(inp, NULL, 17); 537163953Srrs#endif 538163953Srrs if (error != 0) { 539163953Srrs flags = inp->sctp_flags; 540163953Srrs if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 541163953Srrs (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 542163953Srrs#ifdef SCTP_LOG_CLOSING 543163953Srrs sctp_log_closing(inp, NULL, 15); 544163953Srrs#endif 545169352Srrs SCTP_INP_WUNLOCK(inp); 546169380Srrs sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 547169380Srrs SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 548169254Srrs } else { 549169352Srrs SCTP_INP_WUNLOCK(inp); 550163953Srrs } 551163953Srrs return error; 552163953Srrs } 553171167Sgnn#endif /* IPSEC */ 554163953Srrs SCTP_INP_WUNLOCK(inp); 555163953Srrs return 0; 556163953Srrs} 557163953Srrs 558163953Srrsstatic int 559163953Srrssctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p) 560163953Srrs{ 561163953Srrs struct sctp_inpcb *inp; 562166086Srrs int error; 563163953Srrs 564163953Srrs#ifdef INET6 565163953Srrs if (addr && addr->sa_family != AF_INET) 566163953Srrs /* must be a v4 address! */ 567163953Srrs return EINVAL; 568163953Srrs#endif /* INET6 */ 569170056Srrs if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) { 570170056Srrs return EINVAL; 571170056Srrs } 572163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 573163953Srrs if (inp == 0) 574163953Srrs return EINVAL; 575163953Srrs 576171477Srrs error = sctp_inpcb_bind(so, addr, p); 577163953Srrs return error; 578163953Srrs} 579163953Srrs 580163953Srrsstatic void 581163953Srrssctp_close(struct socket *so) 582163953Srrs{ 583163953Srrs struct sctp_inpcb *inp; 584163953Srrs uint32_t flags; 585163953Srrs 586163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 587163953Srrs if (inp == 0) 588163953Srrs return; 589163953Srrs 590163953Srrs /* 591163953Srrs * Inform all the lower layer assoc that we are done. 592163953Srrs */ 593163953Srrssctp_must_try_again: 594163953Srrs flags = inp->sctp_flags; 595163953Srrs#ifdef SCTP_LOG_CLOSING 596163953Srrs sctp_log_closing(inp, NULL, 17); 597163953Srrs#endif 598163953Srrs if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 599163953Srrs (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 600163953Srrs if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) || 601163953Srrs (so->so_rcv.sb_cc > 0)) { 602163953Srrs#ifdef SCTP_LOG_CLOSING 603163953Srrs sctp_log_closing(inp, NULL, 13); 604163953Srrs#endif 605169380Srrs sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 606169380Srrs SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 607163953Srrs } else { 608163953Srrs#ifdef SCTP_LOG_CLOSING 609163953Srrs sctp_log_closing(inp, NULL, 14); 610163953Srrs#endif 611169380Srrs sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, 612169380Srrs SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 613163953Srrs } 614163953Srrs /* 615163953Srrs * The socket is now detached, no matter what the state of 616163953Srrs * the SCTP association. 617163953Srrs */ 618163953Srrs SOCK_LOCK(so); 619167695Srrs SCTP_SB_CLEAR(so->so_snd); 620163953Srrs /* 621163953Srrs * same for the rcv ones, they are only here for the 622163953Srrs * accounting/select. 623163953Srrs */ 624167695Srrs SCTP_SB_CLEAR(so->so_rcv); 625167695Srrs 626167695Srrs /* Now null out the reference, we are completely detached. */ 627163953Srrs so->so_pcb = NULL; 628163953Srrs SOCK_UNLOCK(so); 629163953Srrs } else { 630163953Srrs flags = inp->sctp_flags; 631163953Srrs if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 632163953Srrs goto sctp_must_try_again; 633163953Srrs } 634163953Srrs } 635163953Srrs return; 636163953Srrs} 637163953Srrs 638163953Srrs 639163953Srrsint 640163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 641163953Srrs struct mbuf *control, struct thread *p); 642163953Srrs 643163953Srrs 644163953Srrsint 645163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 646163953Srrs struct mbuf *control, struct thread *p) 647163953Srrs{ 648163953Srrs struct sctp_inpcb *inp; 649163953Srrs int error; 650163953Srrs 651163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 652163953Srrs if (inp == 0) { 653163953Srrs if (control) { 654163953Srrs sctp_m_freem(control); 655163953Srrs control = NULL; 656163953Srrs } 657163953Srrs sctp_m_freem(m); 658163953Srrs return EINVAL; 659163953Srrs } 660163953Srrs /* Got to have an to address if we are NOT a connected socket */ 661163953Srrs if ((addr == NULL) && 662163953Srrs ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) || 663163953Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) 664163953Srrs ) { 665163953Srrs goto connected_type; 666163953Srrs } else if (addr == NULL) { 667163953Srrs error = EDESTADDRREQ; 668163953Srrs sctp_m_freem(m); 669163953Srrs if (control) { 670163953Srrs sctp_m_freem(control); 671163953Srrs control = NULL; 672163953Srrs } 673163953Srrs return (error); 674163953Srrs } 675163953Srrs#ifdef INET6 676163953Srrs if (addr->sa_family != AF_INET) { 677163953Srrs /* must be a v4 address! */ 678163953Srrs sctp_m_freem(m); 679163953Srrs if (control) { 680163953Srrs sctp_m_freem(control); 681163953Srrs control = NULL; 682163953Srrs } 683163953Srrs error = EDESTADDRREQ; 684163953Srrs return EINVAL; 685163953Srrs } 686163953Srrs#endif /* INET6 */ 687163953Srrsconnected_type: 688163953Srrs /* now what about control */ 689163953Srrs if (control) { 690163953Srrs if (inp->control) { 691169420Srrs SCTP_PRINTF("huh? control set?\n"); 692163953Srrs sctp_m_freem(inp->control); 693163953Srrs inp->control = NULL; 694163953Srrs } 695163953Srrs inp->control = control; 696163953Srrs } 697163953Srrs /* Place the data */ 698163953Srrs if (inp->pkt) { 699165647Srrs SCTP_BUF_NEXT(inp->pkt_last) = m; 700163953Srrs inp->pkt_last = m; 701163953Srrs } else { 702163953Srrs inp->pkt_last = inp->pkt = m; 703163953Srrs } 704163953Srrs if ( 705163953Srrs /* FreeBSD uses a flag passed */ 706163953Srrs ((flags & PRUS_MORETOCOME) == 0) 707163953Srrs ) { 708163953Srrs /* 709163953Srrs * note with the current version this code will only be used 710163953Srrs * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for 711163953Srrs * re-defining sosend to use the sctp_sosend. One can 712163953Srrs * optionally switch back to this code (by changing back the 713163953Srrs * definitions) but this is not advisable. This code is used 714163953Srrs * by FreeBSD when sending a file with sendfile() though. 715163953Srrs */ 716163953Srrs int ret; 717163953Srrs 718163953Srrs ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); 719163953Srrs inp->pkt = NULL; 720163953Srrs inp->control = NULL; 721163953Srrs return (ret); 722163953Srrs } else { 723163953Srrs return (0); 724163953Srrs } 725163953Srrs} 726163953Srrs 727163953Srrsstatic int 728163953Srrssctp_disconnect(struct socket *so) 729163953Srrs{ 730163953Srrs struct sctp_inpcb *inp; 731163953Srrs 732163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 733163953Srrs if (inp == NULL) { 734163953Srrs return (ENOTCONN); 735163953Srrs } 736163953Srrs SCTP_INP_RLOCK(inp); 737163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 738166675Srrs if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) { 739163953Srrs /* No connection */ 740163953Srrs SCTP_INP_RUNLOCK(inp); 741163953Srrs return (0); 742163953Srrs } else { 743163953Srrs struct sctp_association *asoc; 744163953Srrs struct sctp_tcb *stcb; 745163953Srrs 746163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 747163953Srrs if (stcb == NULL) { 748163953Srrs SCTP_INP_RUNLOCK(inp); 749163953Srrs return (EINVAL); 750163953Srrs } 751163953Srrs SCTP_TCB_LOCK(stcb); 752163953Srrs asoc = &stcb->asoc; 753163953Srrs if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 754163953Srrs /* We are about to be freed, out of here */ 755163953Srrs SCTP_TCB_UNLOCK(stcb); 756163953Srrs SCTP_INP_RUNLOCK(inp); 757163953Srrs return (0); 758163953Srrs } 759163953Srrs if (((so->so_options & SO_LINGER) && 760163953Srrs (so->so_linger == 0)) || 761163953Srrs (so->so_rcv.sb_cc > 0)) { 762163953Srrs if (SCTP_GET_STATE(asoc) != 763163953Srrs SCTP_STATE_COOKIE_WAIT) { 764163953Srrs /* Left with Data unread */ 765163953Srrs struct mbuf *err; 766163953Srrs 767163953Srrs err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA); 768163953Srrs if (err) { 769163953Srrs /* 770163953Srrs * Fill in the user 771163953Srrs * initiated abort 772163953Srrs */ 773163953Srrs struct sctp_paramhdr *ph; 774163953Srrs 775163953Srrs ph = mtod(err, struct sctp_paramhdr *); 776165647Srrs SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr); 777163953Srrs ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); 778165647Srrs ph->param_length = htons(SCTP_BUF_LEN(err)); 779163953Srrs } 780163953Srrs sctp_send_abort_tcb(stcb, err); 781163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 782163953Srrs } 783163953Srrs SCTP_INP_RUNLOCK(inp); 784163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 785163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 786163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 787163953Srrs } 788165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3); 789163953Srrs /* No unlock tcb assoc is gone */ 790163953Srrs return (0); 791163953Srrs } 792163953Srrs if (TAILQ_EMPTY(&asoc->send_queue) && 793163953Srrs TAILQ_EMPTY(&asoc->sent_queue) && 794163953Srrs (asoc->stream_queue_cnt == 0)) { 795163953Srrs /* there is nothing queued to send, so done */ 796163953Srrs if (asoc->locked_on_sending) { 797163953Srrs goto abort_anyway; 798163953Srrs } 799166675Srrs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && 800166675Srrs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { 801163953Srrs /* only send SHUTDOWN 1st time thru */ 802163953Srrs sctp_stop_timers_for_shutdown(stcb); 803163953Srrs sctp_send_shutdown(stcb, 804163953Srrs stcb->asoc.primary_destination); 805163953Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3); 806166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 807166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 808166675Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 809166675Srrs } 810163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_SENT; 811163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 812163953Srrs stcb->sctp_ep, stcb, 813163953Srrs asoc->primary_destination); 814163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 815163953Srrs stcb->sctp_ep, stcb, 816163953Srrs asoc->primary_destination); 817163953Srrs } 818163953Srrs } else { 819163953Srrs /* 820163953Srrs * we still got (or just got) data to send, 821163953Srrs * so set SHUTDOWN_PENDING 822163953Srrs */ 823163953Srrs /* 824163953Srrs * XXX sockets draft says that SCTP_EOF 825163953Srrs * should be sent with no data. currently, 826163953Srrs * we will allow user data to be sent first 827163953Srrs * and move to SHUTDOWN-PENDING 828163953Srrs */ 829163953Srrs asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; 830163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, 831163953Srrs asoc->primary_destination); 832163953Srrs if (asoc->locked_on_sending) { 833163953Srrs /* Locked to send out the data */ 834163953Srrs struct sctp_stream_queue_pending *sp; 835163953Srrs 836163953Srrs sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead); 837163953Srrs if (sp == NULL) { 838169420Srrs SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n", 839163953Srrs asoc->locked_on_sending->stream_no); 840163953Srrs } else { 841163953Srrs if ((sp->length == 0) && (sp->msg_is_complete == 0)) 842163953Srrs asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT; 843163953Srrs } 844163953Srrs } 845163953Srrs if (TAILQ_EMPTY(&asoc->send_queue) && 846163953Srrs TAILQ_EMPTY(&asoc->sent_queue) && 847163953Srrs (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { 848163953Srrs struct mbuf *op_err; 849163953Srrs 850163953Srrs abort_anyway: 851163953Srrs op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)), 852163953Srrs 0, M_DONTWAIT, 1, MT_DATA); 853163953Srrs if (op_err) { 854163953Srrs /* 855163953Srrs * Fill in the user 856163953Srrs * initiated abort 857163953Srrs */ 858163953Srrs struct sctp_paramhdr *ph; 859163953Srrs uint32_t *ippp; 860163953Srrs 861165647Srrs SCTP_BUF_LEN(op_err) = 862163953Srrs (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)); 863163953Srrs ph = mtod(op_err, 864163953Srrs struct sctp_paramhdr *); 865163953Srrs ph->param_type = htons( 866163953Srrs SCTP_CAUSE_USER_INITIATED_ABT); 867165647Srrs ph->param_length = htons(SCTP_BUF_LEN(op_err)); 868163953Srrs ippp = (uint32_t *) (ph + 1); 869165220Srrs *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4); 870163953Srrs } 871165220Srrs stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4; 872163953Srrs sctp_send_abort_tcb(stcb, op_err); 873163953Srrs SCTP_STAT_INCR_COUNTER32(sctps_aborted); 874163953Srrs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) || 875163953Srrs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 876163953Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 877163953Srrs } 878163953Srrs SCTP_INP_RUNLOCK(inp); 879165220Srrs sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5); 880163953Srrs return (0); 881163953Srrs } 882163953Srrs } 883163953Srrs SCTP_TCB_UNLOCK(stcb); 884163953Srrs SCTP_INP_RUNLOCK(inp); 885163953Srrs return (0); 886163953Srrs } 887163953Srrs /* not reached */ 888163953Srrs } else { 889163953Srrs /* UDP model does not support this */ 890163953Srrs SCTP_INP_RUNLOCK(inp); 891163953Srrs return EOPNOTSUPP; 892163953Srrs } 893163953Srrs} 894163953Srrs 895163953Srrsint 896163953Srrssctp_shutdown(struct socket *so) 897163953Srrs{ 898163953Srrs struct sctp_inpcb *inp; 899163953Srrs 900163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 901163953Srrs if (inp == 0) { 902163953Srrs return EINVAL; 903163953Srrs } 904163953Srrs SCTP_INP_RLOCK(inp); 905163953Srrs /* For UDP model this is a invalid call */ 906163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 907163953Srrs /* Restore the flags that the soshutdown took away. */ 908163953Srrs so->so_rcv.sb_state &= ~SBS_CANTRCVMORE; 909163953Srrs /* This proc will wakeup for read and do nothing (I hope) */ 910163953Srrs SCTP_INP_RUNLOCK(inp); 911163953Srrs return (EOPNOTSUPP); 912163953Srrs } 913163953Srrs /* 914163953Srrs * Ok if we reach here its the TCP model and it is either a SHUT_WR 915163953Srrs * or SHUT_RDWR. This means we put the shutdown flag against it. 916163953Srrs */ 917163953Srrs { 918163953Srrs struct sctp_tcb *stcb; 919163953Srrs struct sctp_association *asoc; 920163953Srrs 921163953Srrs socantsendmore(so); 922163953Srrs 923163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 924163953Srrs if (stcb == NULL) { 925163953Srrs /* 926163953Srrs * Ok we hit the case that the shutdown call was 927163953Srrs * made after an abort or something. Nothing to do 928163953Srrs * now. 929163953Srrs */ 930168299Srrs SCTP_INP_RUNLOCK(inp); 931163953Srrs return (0); 932163953Srrs } 933163953Srrs SCTP_TCB_LOCK(stcb); 934163953Srrs asoc = &stcb->asoc; 935163953Srrs if (TAILQ_EMPTY(&asoc->send_queue) && 936163953Srrs TAILQ_EMPTY(&asoc->sent_queue) && 937163953Srrs (asoc->stream_queue_cnt == 0)) { 938163953Srrs if (asoc->locked_on_sending) { 939163953Srrs goto abort_anyway; 940163953Srrs } 941163953Srrs /* there is nothing queued to send, so I'm done... */ 942163953Srrs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) { 943163953Srrs /* only send SHUTDOWN the first time through */ 944163953Srrs sctp_stop_timers_for_shutdown(stcb); 945163953Srrs sctp_send_shutdown(stcb, 946163953Srrs stcb->asoc.primary_destination); 947163953Srrs sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3); 948166675Srrs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 949166675Srrs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 950166675Srrs SCTP_STAT_DECR_GAUGE32(sctps_currestab); 951166675Srrs } 952163953Srrs asoc->state = SCTP_STATE_SHUTDOWN_SENT; 953163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 954163953Srrs stcb->sctp_ep, stcb, 955163953Srrs asoc->primary_destination); 956163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 957163953Srrs stcb->sctp_ep, stcb, 958163953Srrs asoc->primary_destination); 959163953Srrs } 960163953Srrs } else { 961163953Srrs /* 962163953Srrs * we still got (or just got) data to send, so set 963163953Srrs * SHUTDOWN_PENDING 964163953Srrs */ 965163953Srrs asoc->state |= SCTP_STATE_SHUTDOWN_PENDING; 966163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, 967163953Srrs asoc->primary_destination); 968163953Srrs 969163953Srrs if (asoc->locked_on_sending) { 970163953Srrs /* Locked to send out the data */ 971163953Srrs struct sctp_stream_queue_pending *sp; 972163953Srrs 973163953Srrs sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead); 974163953Srrs if (sp == NULL) { 975169420Srrs SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n", 976163953Srrs asoc->locked_on_sending->stream_no); 977163953Srrs } else { 978163953Srrs if ((sp->length == 0) && (sp->msg_is_complete == 0)) { 979163953Srrs asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT; 980163953Srrs } 981163953Srrs } 982163953Srrs } 983163953Srrs if (TAILQ_EMPTY(&asoc->send_queue) && 984163953Srrs TAILQ_EMPTY(&asoc->sent_queue) && 985163953Srrs (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) { 986163953Srrs struct mbuf *op_err; 987163953Srrs 988163953Srrs abort_anyway: 989163953Srrs op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)), 990163953Srrs 0, M_DONTWAIT, 1, MT_DATA); 991163953Srrs if (op_err) { 992163953Srrs /* Fill in the user initiated abort */ 993163953Srrs struct sctp_paramhdr *ph; 994163953Srrs uint32_t *ippp; 995163953Srrs 996165647Srrs SCTP_BUF_LEN(op_err) = 997163953Srrs sizeof(struct sctp_paramhdr) + sizeof(uint32_t); 998163953Srrs ph = mtod(op_err, 999163953Srrs struct sctp_paramhdr *); 1000163953Srrs ph->param_type = htons( 1001163953Srrs SCTP_CAUSE_USER_INITIATED_ABT); 1002165647Srrs ph->param_length = htons(SCTP_BUF_LEN(op_err)); 1003163953Srrs ippp = (uint32_t *) (ph + 1); 1004165220Srrs *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6); 1005163953Srrs } 1006165220Srrs stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6; 1007163953Srrs sctp_abort_an_association(stcb->sctp_ep, stcb, 1008163953Srrs SCTP_RESPONSE_TO_USER_REQ, 1009163953Srrs op_err); 1010163953Srrs goto skip_unlock; 1011163953Srrs } 1012163953Srrs } 1013163953Srrs SCTP_TCB_UNLOCK(stcb); 1014163953Srrs } 1015163953Srrsskip_unlock: 1016163953Srrs SCTP_INP_RUNLOCK(inp); 1017163953Srrs return 0; 1018163953Srrs} 1019163953Srrs 1020163953Srrs/* 1021163953Srrs * copies a "user" presentable address and removes embedded scope, etc. 1022163953Srrs * returns 0 on success, 1 on error 1023163953Srrs */ 1024163953Srrsstatic uint32_t 1025163953Srrssctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa) 1026163953Srrs{ 1027163953Srrs struct sockaddr_in6 lsa6; 1028163953Srrs 1029163953Srrs sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa, 1030163953Srrs &lsa6); 1031163953Srrs memcpy(ss, sa, sa->sa_len); 1032163953Srrs return (0); 1033163953Srrs} 1034163953Srrs 1035163953Srrs 1036163953Srrs 1037166675Srrsstatic size_t 1038168124Srrssctp_fill_up_addresses_vrf(struct sctp_inpcb *inp, 1039163953Srrs struct sctp_tcb *stcb, 1040166675Srrs size_t limit, 1041167598Srrs struct sockaddr_storage *sas, 1042167598Srrs uint32_t vrf_id) 1043163953Srrs{ 1044167598Srrs struct sctp_ifn *sctp_ifn; 1045167598Srrs struct sctp_ifa *sctp_ifa; 1046166675Srrs int loopback_scope, ipv4_local_scope, local_scope, site_scope; 1047166675Srrs size_t actual; 1048163953Srrs int ipv4_addr_legal, ipv6_addr_legal; 1049167598Srrs struct sctp_vrf *vrf; 1050163953Srrs 1051163953Srrs actual = 0; 1052163953Srrs if (limit <= 0) 1053163953Srrs return (actual); 1054163953Srrs 1055163953Srrs if (stcb) { 1056163953Srrs /* Turn on all the appropriate scope */ 1057163953Srrs loopback_scope = stcb->asoc.loopback_scope; 1058163953Srrs ipv4_local_scope = stcb->asoc.ipv4_local_scope; 1059163953Srrs local_scope = stcb->asoc.local_scope; 1060163953Srrs site_scope = stcb->asoc.site_scope; 1061163953Srrs } else { 1062163953Srrs /* Turn on ALL scope, since we look at the EP */ 1063163953Srrs loopback_scope = ipv4_local_scope = local_scope = 1064163953Srrs site_scope = 1; 1065163953Srrs } 1066163953Srrs ipv4_addr_legal = ipv6_addr_legal = 0; 1067163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 1068163953Srrs ipv6_addr_legal = 1; 1069166023Srrs if (SCTP_IPV6_V6ONLY(inp) == 0) { 1070163953Srrs ipv4_addr_legal = 1; 1071163953Srrs } 1072163953Srrs } else { 1073163953Srrs ipv4_addr_legal = 1; 1074163953Srrs } 1075167598Srrs vrf = sctp_find_vrf(vrf_id); 1076167598Srrs if (vrf == NULL) { 1077167598Srrs return (0); 1078167598Srrs } 1079163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1080167598Srrs LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { 1081163953Srrs if ((loopback_scope == 0) && 1082167598Srrs SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { 1083163953Srrs /* Skip loopback if loopback_scope not set */ 1084163953Srrs continue; 1085163953Srrs } 1086167598Srrs LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 1087163953Srrs if (stcb) { 1088163953Srrs /* 1089163953Srrs * For the BOUND-ALL case, the list 1090163953Srrs * associated with a TCB is Always 1091163953Srrs * considered a reverse list.. i.e. 1092163953Srrs * it lists addresses that are NOT 1093163953Srrs * part of the association. If this 1094163953Srrs * is one of those we must skip it. 1095163953Srrs */ 1096163953Srrs if (sctp_is_addr_restricted(stcb, 1097167598Srrs sctp_ifa)) { 1098163953Srrs continue; 1099163953Srrs } 1100163953Srrs } 1101167598Srrs if ((sctp_ifa->address.sa.sa_family == AF_INET) && 1102163953Srrs (ipv4_addr_legal)) { 1103163953Srrs struct sockaddr_in *sin; 1104163953Srrs 1105167598Srrs sin = (struct sockaddr_in *)&sctp_ifa->address.sa; 1106163953Srrs if (sin->sin_addr.s_addr == 0) { 1107163953Srrs /* 1108163953Srrs * we skip unspecifed 1109163953Srrs * addresses 1110163953Srrs */ 1111163953Srrs continue; 1112163953Srrs } 1113163953Srrs if ((ipv4_local_scope == 0) && 1114163953Srrs (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { 1115163953Srrs continue; 1116163953Srrs } 1117163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) { 1118163953Srrs in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas); 1119163953Srrs ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 1120163953Srrs sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6)); 1121163953Srrs actual += sizeof(sizeof(struct sockaddr_in6)); 1122163953Srrs } else { 1123163953Srrs memcpy(sas, sin, sizeof(*sin)); 1124163953Srrs ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport; 1125163953Srrs sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin)); 1126163953Srrs actual += sizeof(*sin); 1127163953Srrs } 1128163953Srrs if (actual >= limit) { 1129163953Srrs return (actual); 1130163953Srrs } 1131167598Srrs } else if ((sctp_ifa->address.sa.sa_family == AF_INET6) && 1132163953Srrs (ipv6_addr_legal)) { 1133163953Srrs struct sockaddr_in6 *sin6; 1134163953Srrs 1135167598Srrs sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa; 1136163953Srrs if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1137163953Srrs /* 1138163953Srrs * we skip unspecifed 1139163953Srrs * addresses 1140163953Srrs */ 1141163953Srrs continue; 1142163953Srrs } 1143163953Srrs if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 1144163953Srrs if (local_scope == 0) 1145163953Srrs continue; 1146163953Srrs if (sin6->sin6_scope_id == 0) { 1147163953Srrs if (sa6_recoverscope(sin6) != 0) 1148163953Srrs /* 1149163953Srrs * bad link 1150163953Srrs * local 1151163953Srrs * address 1152163953Srrs */ 1153163953Srrs continue; 1154163953Srrs } 1155163953Srrs } 1156163953Srrs if ((site_scope == 0) && 1157163953Srrs (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { 1158163953Srrs continue; 1159163953Srrs } 1160163953Srrs memcpy(sas, sin6, sizeof(*sin6)); 1161163953Srrs ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 1162163953Srrs sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6)); 1163163953Srrs actual += sizeof(*sin6); 1164163953Srrs if (actual >= limit) { 1165163953Srrs return (actual); 1166163953Srrs } 1167163953Srrs } 1168163953Srrs } 1169163953Srrs } 1170163953Srrs } else { 1171163953Srrs struct sctp_laddr *laddr; 1172163953Srrs 1173167598Srrs /* The list is a NEGATIVE list */ 1174167598Srrs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1175167598Srrs if (stcb) { 1176167598Srrs if (sctp_is_addr_restricted(stcb, laddr->ifa)) { 1177163953Srrs continue; 1178163953Srrs } 1179163953Srrs } 1180167598Srrs if (sctp_fill_user_address(sas, &laddr->ifa->address.sa)) 1181167598Srrs continue; 1182167598Srrs 1183167598Srrs ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport; 1184167598Srrs sas = (struct sockaddr_storage *)((caddr_t)sas + 1185167598Srrs laddr->ifa->address.sa.sa_len); 1186167598Srrs actual += laddr->ifa->address.sa.sa_len; 1187167598Srrs if (actual >= limit) { 1188167598Srrs return (actual); 1189163953Srrs } 1190163953Srrs } 1191163953Srrs } 1192163953Srrs return (actual); 1193163953Srrs} 1194163953Srrs 1195168124Srrsstatic size_t 1196168124Srrssctp_fill_up_addresses(struct sctp_inpcb *inp, 1197168124Srrs struct sctp_tcb *stcb, 1198168124Srrs size_t limit, 1199168124Srrs struct sockaddr_storage *sas) 1200168124Srrs{ 1201168124Srrs size_t size = 0; 1202168124Srrs 1203168124Srrs /* fill up addresses for the endpoint's default vrf */ 1204168124Srrs size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas, 1205168124Srrs inp->def_vrf_id); 1206168124Srrs return (size); 1207168124Srrs} 1208168124Srrs 1209163953Srrsstatic int 1210168124Srrssctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id) 1211163953Srrs{ 1212163953Srrs int cnt = 0; 1213167598Srrs struct sctp_vrf *vrf = NULL; 1214163953Srrs 1215163953Srrs /* 1216163953Srrs * In both sub-set bound an bound_all cases we return the MAXIMUM 1217163953Srrs * number of addresses that you COULD get. In reality the sub-set 1218163953Srrs * bound may have an exclusion list for a given TCB OR in the 1219163953Srrs * bound-all case a TCB may NOT include the loopback or other 1220163953Srrs * addresses as well. 1221163953Srrs */ 1222167598Srrs vrf = sctp_find_vrf(vrf_id); 1223167598Srrs if (vrf == NULL) { 1224167598Srrs return (0); 1225167598Srrs } 1226163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1227167598Srrs struct sctp_ifn *sctp_ifn; 1228167598Srrs struct sctp_ifa *sctp_ifa; 1229163953Srrs 1230167598Srrs LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { 1231167598Srrs LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { 1232163953Srrs /* Count them if they are the right type */ 1233167598Srrs if (sctp_ifa->address.sa.sa_family == AF_INET) { 1234163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) 1235163953Srrs cnt += sizeof(struct sockaddr_in6); 1236163953Srrs else 1237163953Srrs cnt += sizeof(struct sockaddr_in); 1238163953Srrs 1239167598Srrs } else if (sctp_ifa->address.sa.sa_family == AF_INET6) 1240163953Srrs cnt += sizeof(struct sockaddr_in6); 1241163953Srrs } 1242163953Srrs } 1243163953Srrs } else { 1244163953Srrs struct sctp_laddr *laddr; 1245163953Srrs 1246163953Srrs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1247167598Srrs if (laddr->ifa->address.sa.sa_family == AF_INET) { 1248163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) 1249163953Srrs cnt += sizeof(struct sockaddr_in6); 1250163953Srrs else 1251163953Srrs cnt += sizeof(struct sockaddr_in); 1252163953Srrs 1253167598Srrs } else if (laddr->ifa->address.sa.sa_family == AF_INET6) 1254163953Srrs cnt += sizeof(struct sockaddr_in6); 1255163953Srrs } 1256163953Srrs } 1257163953Srrs return (cnt); 1258163953Srrs} 1259163953Srrs 1260168124Srrsstatic int 1261168124Srrssctp_count_max_addresses(struct sctp_inpcb *inp) 1262168124Srrs{ 1263168124Srrs int cnt = 0; 1264166675Srrs 1265168124Srrs /* count addresses for the endpoint's default VRF */ 1266168124Srrs cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id); 1267168124Srrs return (cnt); 1268168124Srrs} 1269168124Srrs 1270163953Srrsstatic int 1271166675Srrssctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval, 1272166675Srrs size_t optsize, void *p, int delay) 1273163953Srrs{ 1274163953Srrs int error = 0; 1275163953Srrs int creat_lock_on = 0; 1276163953Srrs struct sctp_tcb *stcb = NULL; 1277163953Srrs struct sockaddr *sa; 1278169352Srrs int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr; 1279169352Srrs int added = 0; 1280167598Srrs uint32_t vrf_id; 1281170056Srrs int bad_addresses = 0; 1282167598Srrs sctp_assoc_t *a_id; 1283163953Srrs 1284169420Srrs SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n"); 1285163953Srrs 1286163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 1287163953Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 1288163953Srrs /* We are already connected AND the TCP model */ 1289163953Srrs return (EADDRINUSE); 1290163953Srrs } 1291163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { 1292163953Srrs return (EINVAL); 1293163953Srrs } 1294163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1295163953Srrs SCTP_INP_RLOCK(inp); 1296163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1297163953Srrs SCTP_INP_RUNLOCK(inp); 1298163953Srrs } 1299163953Srrs if (stcb) { 1300163953Srrs return (EALREADY); 1301163953Srrs } 1302163953Srrs SCTP_INP_INCR_REF(inp); 1303163953Srrs SCTP_ASOC_CREATE_LOCK(inp); 1304163953Srrs creat_lock_on = 1; 1305163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 1306163953Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { 1307163953Srrs error = EFAULT; 1308163953Srrs goto out_now; 1309163953Srrs } 1310166675Srrs totaddrp = (int *)optval; 1311163953Srrs totaddr = *totaddrp; 1312163953Srrs sa = (struct sockaddr *)(totaddrp + 1); 1313170056Srrs stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses); 1314170056Srrs if ((stcb != NULL) || bad_addresses) { 1315169352Srrs /* Already have or am bring up an association */ 1316169352Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1317169352Srrs creat_lock_on = 0; 1318170931Srrs if (stcb) 1319170931Srrs SCTP_TCB_UNLOCK(stcb); 1320170056Srrs if (bad_addresses == 0) 1321170056Srrs error = EALREADY; 1322169352Srrs goto out_now; 1323163953Srrs } 1324163953Srrs#ifdef INET6 1325163953Srrs if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) && 1326163953Srrs (num_v6 > 0)) { 1327163953Srrs error = EINVAL; 1328163953Srrs goto out_now; 1329163953Srrs } 1330163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && 1331163953Srrs (num_v4 > 0)) { 1332163953Srrs struct in6pcb *inp6; 1333163953Srrs 1334163953Srrs inp6 = (struct in6pcb *)inp; 1335166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 1336163953Srrs /* 1337163953Srrs * if IPV6_V6ONLY flag, ignore connections destined 1338163953Srrs * to a v4 addr or v4-mapped addr 1339163953Srrs */ 1340163953Srrs error = EINVAL; 1341163953Srrs goto out_now; 1342163953Srrs } 1343163953Srrs } 1344163953Srrs#endif /* INET6 */ 1345163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 1346163953Srrs SCTP_PCB_FLAGS_UNBOUND) { 1347163953Srrs /* Bind a ephemeral port */ 1348171477Srrs error = sctp_inpcb_bind(so, NULL, p); 1349163953Srrs if (error) { 1350163953Srrs goto out_now; 1351163953Srrs } 1352163953Srrs } 1353167695Srrs /* FIX ME: do we want to pass in a vrf on the connect call? */ 1354167695Srrs vrf_id = inp->def_vrf_id; 1355167695Srrs 1356163953Srrs /* We are GOOD to go */ 1357171531Srrs stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id, 1358171531Srrs (struct thread *)p 1359171531Srrs ); 1360163953Srrs if (stcb == NULL) { 1361163953Srrs /* Gak! no memory */ 1362163953Srrs goto out_now; 1363163953Srrs } 1364169352Srrs stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 1365163953Srrs /* move to second address */ 1366163953Srrs if (sa->sa_family == AF_INET) 1367163953Srrs sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in)); 1368163953Srrs else 1369163953Srrs sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6)); 1370163953Srrs 1371170056Srrs error = 0; 1372169352Srrs added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error); 1373167598Srrs /* Fill in the return id */ 1374170056Srrs if (error) { 1375170056Srrs sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12); 1376170056Srrs goto out_now; 1377170056Srrs } 1378167598Srrs a_id = (sctp_assoc_t *) optval; 1379167598Srrs *a_id = sctp_get_associd(stcb); 1380163953Srrs 1381163953Srrs /* initialize authentication parameters for the assoc */ 1382163953Srrs sctp_initialize_auth_params(inp, stcb); 1383163953Srrs 1384163953Srrs if (delay) { 1385163953Srrs /* doing delayed connection */ 1386163953Srrs stcb->asoc.delayed_connection = 1; 1387163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination); 1388163953Srrs } else { 1389169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1390163953Srrs sctp_send_initiate(inp, stcb); 1391163953Srrs } 1392163953Srrs SCTP_TCB_UNLOCK(stcb); 1393163953Srrs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1394163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1395163953Srrs /* Set the connected flag so we can queue data */ 1396163953Srrs soisconnecting(so); 1397163953Srrs } 1398163953Srrsout_now: 1399169655Srrs if (creat_lock_on) { 1400163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1401169655Srrs } 1402163953Srrs SCTP_INP_DECR_REF(inp); 1403163953Srrs return error; 1404163953Srrs} 1405163953Srrs 1406169420Srrs#define SCTP_FIND_STCB(inp, stcb, assoc_id) { \ 1407169655Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\ 1408169655Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \ 1409166675Srrs SCTP_INP_RLOCK(inp); \ 1410166675Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); \ 1411169655Srrs if (stcb) { \ 1412166675Srrs SCTP_TCB_LOCK(stcb); \ 1413169655Srrs } \ 1414166675Srrs SCTP_INP_RUNLOCK(inp); \ 1415166675Srrs } else if (assoc_id != 0) { \ 1416166675Srrs stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \ 1417166675Srrs if (stcb == NULL) { \ 1418166675Srrs error = ENOENT; \ 1419166675Srrs break; \ 1420166675Srrs } \ 1421166675Srrs } else { \ 1422166675Srrs stcb = NULL; \ 1423169420Srrs } \ 1424169420Srrs } 1425163953Srrs 1426169420Srrs 1427169420Srrs#define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\ 1428166675Srrs if (size < sizeof(type)) { \ 1429166675Srrs error = EINVAL; \ 1430166675Srrs break; \ 1431166675Srrs } else { \ 1432166675Srrs destp = (type *)srcp; \ 1433169420Srrs } \ 1434169420Srrs } 1435163953Srrs 1436163953Srrsstatic int 1437166675Srrssctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize, 1438166675Srrs void *p) 1439163953Srrs{ 1440163953Srrs struct sctp_inpcb *inp; 1441166675Srrs int error, val = 0; 1442163953Srrs struct sctp_tcb *stcb = NULL; 1443163953Srrs 1444166675Srrs if (optval == NULL) { 1445166675Srrs return (EINVAL); 1446166675Srrs } 1447163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1448163953Srrs if (inp == 0) 1449163953Srrs return EINVAL; 1450163953Srrs error = 0; 1451163953Srrs 1452166675Srrs switch (optname) { 1453163953Srrs case SCTP_NODELAY: 1454163953Srrs case SCTP_AUTOCLOSE: 1455163953Srrs case SCTP_EXPLICIT_EOR: 1456163953Srrs case SCTP_AUTO_ASCONF: 1457163953Srrs case SCTP_DISABLE_FRAGMENTS: 1458163953Srrs case SCTP_I_WANT_MAPPED_V4_ADDR: 1459163953Srrs case SCTP_USE_EXT_RCVINFO: 1460163953Srrs SCTP_INP_RLOCK(inp); 1461166675Srrs switch (optname) { 1462163953Srrs case SCTP_DISABLE_FRAGMENTS: 1463166675Srrs val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT); 1464163953Srrs break; 1465163953Srrs case SCTP_I_WANT_MAPPED_V4_ADDR: 1466166675Srrs val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4); 1467163953Srrs break; 1468163953Srrs case SCTP_AUTO_ASCONF: 1469166675Srrs val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); 1470163953Srrs break; 1471163953Srrs case SCTP_EXPLICIT_EOR: 1472166675Srrs val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR); 1473163953Srrs break; 1474163953Srrs case SCTP_NODELAY: 1475166675Srrs val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY); 1476163953Srrs break; 1477163953Srrs case SCTP_USE_EXT_RCVINFO: 1478166675Srrs val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO); 1479163953Srrs break; 1480163953Srrs case SCTP_AUTOCLOSE: 1481163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) 1482166675Srrs val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time); 1483163953Srrs else 1484166675Srrs val = 0; 1485163953Srrs break; 1486163953Srrs 1487163953Srrs default: 1488163953Srrs error = ENOPROTOOPT; 1489163953Srrs } /* end switch (sopt->sopt_name) */ 1490166675Srrs if (optname != SCTP_AUTOCLOSE) { 1491163953Srrs /* make it an "on/off" value */ 1492166675Srrs val = (val != 0); 1493163953Srrs } 1494166675Srrs if (*optsize < sizeof(val)) { 1495163953Srrs error = EINVAL; 1496163953Srrs } 1497163953Srrs SCTP_INP_RUNLOCK(inp); 1498163953Srrs if (error == 0) { 1499163953Srrs /* return the option value */ 1500166675Srrs *(int *)optval = val; 1501166675Srrs *optsize = sizeof(val); 1502163953Srrs } 1503163953Srrs break; 1504170091Srrs case SCTP_GET_PACKET_LOG: 1505170091Srrs { 1506170091Srrs#ifdef SCTP_PACKET_LOGGING 1507170091Srrs uint8_t *target; 1508170091Srrs int ret; 1509167598Srrs 1510170091Srrs SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize); 1511170091Srrs ret = sctp_copy_out_packet_log(target, (int)*optsize); 1512170091Srrs *optsize = ret; 1513170091Srrs#else 1514170091Srrs error = EOPNOTSUPP; 1515170091Srrs#endif 1516170091Srrs break; 1517170091Srrs } 1518163953Srrs case SCTP_PARTIAL_DELIVERY_POINT: 1519163953Srrs { 1520166675Srrs uint32_t *value; 1521166675Srrs 1522166675Srrs SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1523166675Srrs *value = inp->partial_delivery_point; 1524166675Srrs *optsize = sizeof(uint32_t); 1525163953Srrs } 1526163953Srrs break; 1527163953Srrs case SCTP_FRAGMENT_INTERLEAVE: 1528163953Srrs { 1529166675Srrs uint32_t *value; 1530166675Srrs 1531166675Srrs SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1532168943Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) { 1533168943Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) { 1534168943Srrs *value = SCTP_FRAG_LEVEL_2; 1535168943Srrs } else { 1536168943Srrs *value = SCTP_FRAG_LEVEL_1; 1537168943Srrs } 1538168943Srrs } else { 1539168943Srrs *value = SCTP_FRAG_LEVEL_0; 1540168943Srrs } 1541166675Srrs *optsize = sizeof(uint32_t); 1542163953Srrs } 1543163953Srrs break; 1544163953Srrs case SCTP_CMT_ON_OFF: 1545163953Srrs { 1546166675Srrs struct sctp_assoc_value *av; 1547166675Srrs 1548166675Srrs SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 1549166675Srrs if (sctp_cmt_on_off) { 1550166675Srrs SCTP_FIND_STCB(inp, stcb, av->assoc_id); 1551166675Srrs if (stcb) { 1552166675Srrs av->assoc_value = stcb->asoc.sctp_cmt_on_off; 1553166675Srrs SCTP_TCB_UNLOCK(stcb); 1554166675Srrs 1555166675Srrs } else { 1556166675Srrs error = ENOTCONN; 1557166675Srrs } 1558166675Srrs } else { 1559166675Srrs error = ENOPROTOOPT; 1560163953Srrs } 1561166675Srrs *optsize = sizeof(*av); 1562163953Srrs } 1563163953Srrs break; 1564171440Srrs /* JRS - Get socket option for pluggable congestion control */ 1565171440Srrs case SCTP_PLUGGABLE_CC: 1566171440Srrs { 1567171440Srrs struct sctp_assoc_value *av; 1568171440Srrs 1569171440Srrs SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 1570171440Srrs SCTP_FIND_STCB(inp, stcb, av->assoc_id); 1571171440Srrs if (stcb) { 1572171440Srrs av->assoc_value = stcb->asoc.congestion_control_module; 1573171440Srrs SCTP_TCB_UNLOCK(stcb); 1574171440Srrs } else { 1575171440Srrs av->assoc_value = inp->sctp_ep.sctp_default_cc_module; 1576171440Srrs } 1577171440Srrs *optsize = sizeof(*av); 1578171440Srrs } 1579171440Srrs break; 1580163953Srrs case SCTP_GET_ADDR_LEN: 1581163953Srrs { 1582163953Srrs struct sctp_assoc_value *av; 1583163953Srrs 1584166675Srrs SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 1585163953Srrs error = EINVAL; 1586167598Srrs#ifdef INET 1587163953Srrs if (av->assoc_value == AF_INET) { 1588163953Srrs av->assoc_value = sizeof(struct sockaddr_in); 1589163953Srrs error = 0; 1590163953Srrs } 1591163953Srrs#endif 1592167598Srrs#ifdef INET6 1593163953Srrs if (av->assoc_value == AF_INET6) { 1594163953Srrs av->assoc_value = sizeof(struct sockaddr_in6); 1595163953Srrs error = 0; 1596163953Srrs } 1597163953Srrs#endif 1598166675Srrs *optsize = sizeof(*av); 1599163953Srrs } 1600163953Srrs break; 1601169655Srrs case SCTP_GET_ASSOC_NUMBER: 1602163953Srrs { 1603169655Srrs uint32_t *value, cnt; 1604163953Srrs 1605169655Srrs SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1606163953Srrs cnt = 0; 1607163953Srrs SCTP_INP_RLOCK(inp); 1608169655Srrs LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 1609169655Srrs cnt++; 1610163953Srrs } 1611169655Srrs SCTP_INP_RUNLOCK(inp); 1612169655Srrs *value = cnt; 1613169655Srrs *optsize = sizeof(uint32_t); 1614169655Srrs } 1615169655Srrs break; 1616163953Srrs 1617169655Srrs case SCTP_GET_ASSOC_ID_LIST: 1618169655Srrs { 1619169655Srrs struct sctp_assoc_ids *ids; 1620169655Srrs unsigned int at, limit; 1621169655Srrs 1622169655Srrs SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize); 1623163953Srrs at = 0; 1624169655Srrs limit = *optsize / sizeof(sctp_assoc_t); 1625169655Srrs SCTP_INP_RLOCK(inp); 1626169655Srrs LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 1627169655Srrs if (at < limit) { 1628169655Srrs ids->gaids_assoc_id[at++] = sctp_get_associd(stcb); 1629169655Srrs } else { 1630169655Srrs error = EINVAL; 1631163953Srrs break; 1632163953Srrs } 1633163953Srrs } 1634163953Srrs SCTP_INP_RUNLOCK(inp); 1635169655Srrs *optsize = at * sizeof(sctp_assoc_t); 1636163953Srrs } 1637163953Srrs break; 1638163953Srrs case SCTP_CONTEXT: 1639163953Srrs { 1640163953Srrs struct sctp_assoc_value *av; 1641163953Srrs 1642166675Srrs SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 1643166675Srrs SCTP_FIND_STCB(inp, stcb, av->assoc_id); 1644166675Srrs 1645166675Srrs if (stcb) { 1646166675Srrs av->assoc_value = stcb->asoc.context; 1647166675Srrs SCTP_TCB_UNLOCK(stcb); 1648163953Srrs } else { 1649166675Srrs SCTP_INP_RLOCK(inp); 1650163953Srrs av->assoc_value = inp->sctp_context; 1651166675Srrs SCTP_INP_RUNLOCK(inp); 1652163953Srrs } 1653166675Srrs *optsize = sizeof(*av); 1654163953Srrs } 1655163953Srrs break; 1656167598Srrs case SCTP_VRF_ID: 1657167598Srrs { 1658170056Srrs uint32_t *default_vrfid; 1659167598Srrs 1660170056Srrs SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize); 1661170056Srrs *default_vrfid = inp->def_vrf_id; 1662167598Srrs break; 1663167598Srrs } 1664167598Srrs case SCTP_GET_ASOC_VRF: 1665167598Srrs { 1666167598Srrs struct sctp_assoc_value *id; 1667167598Srrs 1668167598Srrs SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize); 1669167598Srrs SCTP_FIND_STCB(inp, stcb, id->assoc_id); 1670167598Srrs if (stcb == NULL) { 1671167598Srrs error = EINVAL; 1672167598Srrs break; 1673167598Srrs } 1674167598Srrs id->assoc_value = stcb->asoc.vrf_id; 1675167598Srrs break; 1676167598Srrs } 1677167598Srrs case SCTP_GET_VRF_IDS: 1678167598Srrs { 1679167598Srrs error = EOPNOTSUPP; 1680167598Srrs break; 1681167598Srrs } 1682163953Srrs case SCTP_GET_NONCE_VALUES: 1683163953Srrs { 1684163953Srrs struct sctp_get_nonce_values *gnv; 1685163953Srrs 1686166675Srrs SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize); 1687166675Srrs SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id); 1688166675Srrs 1689166675Srrs if (stcb) { 1690163953Srrs gnv->gn_peers_tag = stcb->asoc.peer_vtag; 1691163953Srrs gnv->gn_local_tag = stcb->asoc.my_vtag; 1692163953Srrs SCTP_TCB_UNLOCK(stcb); 1693166675Srrs } else { 1694166675Srrs error = ENOTCONN; 1695163953Srrs } 1696166675Srrs *optsize = sizeof(*gnv); 1697163953Srrs } 1698163953Srrs break; 1699170056Srrs case SCTP_DELAYED_SACK: 1700163953Srrs { 1701170056Srrs struct sctp_sack_info *sack; 1702163953Srrs 1703170056Srrs SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize); 1704170056Srrs SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id); 1705166675Srrs if (stcb) { 1706170056Srrs sack->sack_delay = stcb->asoc.delayed_ack; 1707170056Srrs sack->sack_freq = stcb->asoc.sack_freq; 1708166675Srrs SCTP_TCB_UNLOCK(stcb); 1709166675Srrs } else { 1710163953Srrs SCTP_INP_RLOCK(inp); 1711170056Srrs sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]); 1712170056Srrs sack->sack_freq = inp->sctp_ep.sctp_sack_freq; 1713163953Srrs SCTP_INP_RUNLOCK(inp); 1714163953Srrs } 1715170056Srrs *optsize = sizeof(*sack); 1716163953Srrs } 1717163953Srrs break; 1718163953Srrs 1719163953Srrs case SCTP_GET_SNDBUF_USE: 1720166675Srrs { 1721163953Srrs struct sctp_sockstat *ss; 1722163953Srrs 1723166675Srrs SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize); 1724166675Srrs SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id); 1725166675Srrs 1726166675Srrs if (stcb) { 1727166675Srrs ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size; 1728166675Srrs ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue + 1729166675Srrs stcb->asoc.size_on_all_streams); 1730166675Srrs SCTP_TCB_UNLOCK(stcb); 1731166675Srrs } else { 1732163953Srrs error = ENOTCONN; 1733163953Srrs } 1734166675Srrs *optsize = sizeof(struct sctp_sockstat); 1735163953Srrs } 1736163953Srrs break; 1737170056Srrs case SCTP_MAX_BURST: 1738163953Srrs { 1739166675Srrs uint8_t *value; 1740163953Srrs 1741166675Srrs SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize); 1742166675Srrs 1743163953Srrs SCTP_INP_RLOCK(inp); 1744166675Srrs *value = inp->sctp_ep.max_burst; 1745163953Srrs SCTP_INP_RUNLOCK(inp); 1746166675Srrs *optsize = sizeof(uint8_t); 1747163953Srrs } 1748163953Srrs break; 1749163953Srrs case SCTP_MAXSEG: 1750163953Srrs { 1751167598Srrs struct sctp_assoc_value *av; 1752163953Srrs int ovh; 1753163953Srrs 1754167598Srrs SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize); 1755170056Srrs SCTP_FIND_STCB(inp, stcb, av->assoc_id); 1756163953Srrs 1757167598Srrs if (stcb) { 1758167598Srrs av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc); 1759167598Srrs SCTP_TCB_UNLOCK(stcb); 1760163953Srrs } else { 1761167598Srrs SCTP_INP_RLOCK(inp); 1762167598Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 1763167598Srrs ovh = SCTP_MED_OVERHEAD; 1764167598Srrs } else { 1765167598Srrs ovh = SCTP_MED_V4_OVERHEAD; 1766167598Srrs } 1767170056Srrs if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT) 1768170056Srrs av->assoc_value = 0; 1769170056Srrs else 1770170056Srrs av->assoc_value = inp->sctp_frag_point - ovh; 1771167598Srrs SCTP_INP_RUNLOCK(inp); 1772163953Srrs } 1773167598Srrs *optsize = sizeof(struct sctp_assoc_value); 1774163953Srrs } 1775163953Srrs break; 1776163953Srrs case SCTP_GET_STAT_LOG: 1777167598Srrs error = sctp_fill_stat_log(optval, optsize); 1778163953Srrs break; 1779163953Srrs case SCTP_EVENTS: 1780163953Srrs { 1781163953Srrs struct sctp_event_subscribe *events; 1782163953Srrs 1783166675Srrs SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize); 1784163953Srrs memset(events, 0, sizeof(*events)); 1785163953Srrs SCTP_INP_RLOCK(inp); 1786163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) 1787163953Srrs events->sctp_data_io_event = 1; 1788163953Srrs 1789163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT)) 1790163953Srrs events->sctp_association_event = 1; 1791163953Srrs 1792163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT)) 1793163953Srrs events->sctp_address_event = 1; 1794163953Srrs 1795163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT)) 1796163953Srrs events->sctp_send_failure_event = 1; 1797163953Srrs 1798163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR)) 1799163953Srrs events->sctp_peer_error_event = 1; 1800163953Srrs 1801163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) 1802163953Srrs events->sctp_shutdown_event = 1; 1803163953Srrs 1804163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT)) 1805163953Srrs events->sctp_partial_delivery_event = 1; 1806163953Srrs 1807163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) 1808163953Srrs events->sctp_adaptation_layer_event = 1; 1809163953Srrs 1810163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT)) 1811163953Srrs events->sctp_authentication_event = 1; 1812163953Srrs 1813163953Srrs if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) 1814163953Srrs events->sctp_stream_reset_events = 1; 1815163953Srrs SCTP_INP_RUNLOCK(inp); 1816166675Srrs *optsize = sizeof(struct sctp_event_subscribe); 1817163953Srrs } 1818163953Srrs break; 1819163953Srrs 1820163953Srrs case SCTP_ADAPTATION_LAYER: 1821166675Srrs { 1822166675Srrs uint32_t *value; 1823166675Srrs 1824166675Srrs SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1825166675Srrs 1826166675Srrs SCTP_INP_RLOCK(inp); 1827166675Srrs *value = inp->sctp_ep.adaptation_layer_indicator; 1828166675Srrs SCTP_INP_RUNLOCK(inp); 1829166675Srrs *optsize = sizeof(uint32_t); 1830163953Srrs } 1831163953Srrs break; 1832163953Srrs case SCTP_SET_INITIAL_DBG_SEQ: 1833166675Srrs { 1834166675Srrs uint32_t *value; 1835166675Srrs 1836166675Srrs SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1837166675Srrs SCTP_INP_RLOCK(inp); 1838166675Srrs *value = inp->sctp_ep.initial_sequence_debug; 1839166675Srrs SCTP_INP_RUNLOCK(inp); 1840166675Srrs *optsize = sizeof(uint32_t); 1841163953Srrs } 1842163953Srrs break; 1843163953Srrs case SCTP_GET_LOCAL_ADDR_SIZE: 1844166675Srrs { 1845166675Srrs uint32_t *value; 1846166675Srrs 1847166675Srrs SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1848166675Srrs SCTP_INP_RLOCK(inp); 1849168124Srrs *value = sctp_count_max_addresses(inp); 1850166675Srrs SCTP_INP_RUNLOCK(inp); 1851166675Srrs *optsize = sizeof(uint32_t); 1852163953Srrs } 1853163953Srrs break; 1854163953Srrs case SCTP_GET_REMOTE_ADDR_SIZE: 1855163953Srrs { 1856166675Srrs uint32_t *value; 1857166675Srrs size_t size; 1858163953Srrs struct sctp_nets *net; 1859163953Srrs 1860166675Srrs SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize); 1861166675Srrs /* FIXME MT: change to sctp_assoc_value? */ 1862166675Srrs SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value); 1863166675Srrs 1864166675Srrs if (stcb) { 1865166675Srrs size = 0; 1866166675Srrs /* Count the sizes */ 1867166675Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1868166675Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) || 1869166675Srrs (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) { 1870166675Srrs size += sizeof(struct sockaddr_in6); 1871166675Srrs } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) { 1872166675Srrs size += sizeof(struct sockaddr_in); 1873166675Srrs } else { 1874166675Srrs /* huh */ 1875166675Srrs break; 1876166675Srrs } 1877163953Srrs } 1878166675Srrs SCTP_TCB_UNLOCK(stcb); 1879166675Srrs *value = (uint32_t) size; 1880166675Srrs } else { 1881166675Srrs error = ENOTCONN; 1882163953Srrs } 1883166675Srrs *optsize = sizeof(uint32_t); 1884163953Srrs } 1885163953Srrs break; 1886163953Srrs case SCTP_GET_PEER_ADDRESSES: 1887163953Srrs /* 1888163953Srrs * Get the address information, an array is passed in to 1889163953Srrs * fill up we pack it. 1890163953Srrs */ 1891163953Srrs { 1892166675Srrs size_t cpsz, left; 1893163953Srrs struct sockaddr_storage *sas; 1894163953Srrs struct sctp_nets *net; 1895163953Srrs struct sctp_getaddresses *saddr; 1896163953Srrs 1897166675Srrs SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize); 1898166675Srrs SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id); 1899163953Srrs 1900166675Srrs if (stcb) { 1901166675Srrs left = (*optsize) - sizeof(struct sctp_getaddresses); 1902166675Srrs *optsize = sizeof(struct sctp_getaddresses); 1903166675Srrs sas = (struct sockaddr_storage *)&saddr->addr[0]; 1904166675Srrs 1905166675Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1906166675Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) || 1907166675Srrs (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) { 1908166675Srrs cpsz = sizeof(struct sockaddr_in6); 1909166675Srrs } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) { 1910166675Srrs cpsz = sizeof(struct sockaddr_in); 1911166675Srrs } else { 1912166675Srrs /* huh */ 1913166675Srrs break; 1914166675Srrs } 1915166675Srrs if (left < cpsz) { 1916166675Srrs /* not enough room. */ 1917166675Srrs break; 1918166675Srrs } 1919166675Srrs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) && 1920166675Srrs (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) { 1921166675Srrs /* Must map the address */ 1922166675Srrs in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr, 1923166675Srrs (struct sockaddr_in6 *)sas); 1924166675Srrs } else { 1925166675Srrs memcpy(sas, &net->ro._l_addr, cpsz); 1926166675Srrs } 1927166675Srrs ((struct sockaddr_in *)sas)->sin_port = stcb->rport; 1928166675Srrs 1929166675Srrs sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz); 1930166675Srrs left -= cpsz; 1931166675Srrs *optsize += cpsz; 1932163953Srrs } 1933166675Srrs SCTP_TCB_UNLOCK(stcb); 1934166675Srrs } else { 1935166675Srrs error = ENOENT; 1936163953Srrs } 1937163953Srrs } 1938163953Srrs break; 1939163953Srrs case SCTP_GET_LOCAL_ADDRESSES: 1940163953Srrs { 1941166675Srrs size_t limit, actual; 1942163953Srrs struct sockaddr_storage *sas; 1943163953Srrs struct sctp_getaddresses *saddr; 1944163953Srrs 1945166675Srrs SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize); 1946166675Srrs SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id); 1947163953Srrs 1948163953Srrs sas = (struct sockaddr_storage *)&saddr->addr[0]; 1949166675Srrs limit = *optsize - sizeof(sctp_assoc_t); 1950168124Srrs actual = sctp_fill_up_addresses(inp, stcb, limit, sas); 1951169655Srrs if (stcb) { 1952163953Srrs SCTP_TCB_UNLOCK(stcb); 1953169655Srrs } 1954166675Srrs *optsize = sizeof(struct sockaddr_storage) + actual; 1955163953Srrs } 1956163953Srrs break; 1957163953Srrs case SCTP_PEER_ADDR_PARAMS: 1958163953Srrs { 1959163953Srrs struct sctp_paddrparams *paddrp; 1960163953Srrs struct sctp_nets *net; 1961163953Srrs 1962166675Srrs SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize); 1963166675Srrs SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id); 1964163953Srrs 1965163953Srrs net = NULL; 1966166675Srrs if (stcb) { 1967166675Srrs net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address); 1968166675Srrs } else { 1969166675Srrs /* 1970166675Srrs * We increment here since 1971166675Srrs * sctp_findassociation_ep_addr() wil do a 1972166675Srrs * decrement if it finds the stcb as long as 1973166675Srrs * the locked tcb (last argument) is NOT a 1974166675Srrs * TCB.. aka NULL. 1975166675Srrs */ 1976166675Srrs SCTP_INP_INCR_REF(inp); 1977166675Srrs stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL); 1978163953Srrs if (stcb == NULL) { 1979166675Srrs SCTP_INP_DECR_REF(inp); 1980163953Srrs } 1981163953Srrs } 1982163953Srrs 1983163953Srrs if (stcb) { 1984163953Srrs /* Applys to the specific association */ 1985163953Srrs paddrp->spp_flags = 0; 1986163953Srrs if (net) { 1987170056Srrs int ovh; 1988170056Srrs 1989170056Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 1990170056Srrs ovh = SCTP_MED_OVERHEAD; 1991170056Srrs } else { 1992170056Srrs ovh = SCTP_MED_V4_OVERHEAD; 1993170056Srrs } 1994170056Srrs 1995170056Srrs 1996163953Srrs paddrp->spp_pathmaxrxt = net->failure_threshold; 1997170056Srrs paddrp->spp_pathmtu = net->mtu - ovh; 1998163953Srrs /* get flags for HB */ 1999163953Srrs if (net->dest_state & SCTP_ADDR_NOHB) 2000163953Srrs paddrp->spp_flags |= SPP_HB_DISABLE; 2001163953Srrs else 2002163953Srrs paddrp->spp_flags |= SPP_HB_ENABLE; 2003163953Srrs /* get flags for PMTU */ 2004165647Srrs if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 2005163953Srrs paddrp->spp_flags |= SPP_PMTUD_ENABLE; 2006163953Srrs } else { 2007163953Srrs paddrp->spp_flags |= SPP_PMTUD_DISABLE; 2008163953Srrs } 2009167598Srrs#ifdef INET 2010163953Srrs if (net->ro._l_addr.sin.sin_family == AF_INET) { 2011163953Srrs paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc; 2012163953Srrs paddrp->spp_flags |= SPP_IPV4_TOS; 2013163953Srrs } 2014163953Srrs#endif 2015167598Srrs#ifdef INET6 2016163953Srrs if (net->ro._l_addr.sin6.sin6_family == AF_INET6) { 2017163953Srrs paddrp->spp_ipv6_flowlabel = net->tos_flowlabel; 2018163953Srrs paddrp->spp_flags |= SPP_IPV6_FLOWLABEL; 2019163953Srrs } 2020163953Srrs#endif 2021163953Srrs } else { 2022163953Srrs /* 2023163953Srrs * No destination so return default 2024163953Srrs * value 2025163953Srrs */ 2026170056Srrs int cnt = 0; 2027170056Srrs 2028163953Srrs paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure; 2029163953Srrs paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc); 2030167598Srrs#ifdef INET 2031163953Srrs paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc; 2032163953Srrs paddrp->spp_flags |= SPP_IPV4_TOS; 2033163953Srrs#endif 2034167598Srrs#ifdef INET6 2035163953Srrs paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel; 2036163953Srrs paddrp->spp_flags |= SPP_IPV6_FLOWLABEL; 2037163953Srrs#endif 2038163953Srrs /* default settings should be these */ 2039170056Srrs if (stcb->asoc.hb_is_disabled == 0) { 2040163953Srrs paddrp->spp_flags |= SPP_HB_ENABLE; 2041170056Srrs } else { 2042170056Srrs paddrp->spp_flags |= SPP_HB_DISABLE; 2043163953Srrs } 2044170056Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 2045170056Srrs if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 2046170056Srrs cnt++; 2047170056Srrs } 2048170056Srrs } 2049170056Srrs if (cnt) { 2050170056Srrs paddrp->spp_flags |= SPP_PMTUD_ENABLE; 2051170056Srrs } 2052163953Srrs } 2053163953Srrs paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay; 2054163953Srrs paddrp->spp_assoc_id = sctp_get_associd(stcb); 2055163953Srrs SCTP_TCB_UNLOCK(stcb); 2056163953Srrs } else { 2057163953Srrs /* Use endpoint defaults */ 2058163953Srrs SCTP_INP_RLOCK(inp); 2059163953Srrs paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure; 2060163953Srrs paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]); 2061163953Srrs paddrp->spp_assoc_id = (sctp_assoc_t) 0; 2062163953Srrs /* get inp's default */ 2063167598Srrs#ifdef INET 2064163953Srrs paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos; 2065163953Srrs paddrp->spp_flags |= SPP_IPV4_TOS; 2066163953Srrs#endif 2067167598Srrs#ifdef INET6 2068163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 2069163953Srrs paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo; 2070163953Srrs paddrp->spp_flags |= SPP_IPV6_FLOWLABEL; 2071163953Srrs } 2072163953Srrs#endif 2073163953Srrs /* can't return this */ 2074163953Srrs paddrp->spp_pathmtu = 0; 2075170056Srrs 2076163953Srrs /* default behavior, no stcb */ 2077170056Srrs paddrp->spp_flags = SPP_PMTUD_ENABLE; 2078163953Srrs 2079170056Srrs if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) { 2080170056Srrs paddrp->spp_flags |= SPP_HB_ENABLE; 2081170056Srrs } else { 2082170056Srrs paddrp->spp_flags |= SPP_HB_DISABLE; 2083170056Srrs } 2084163953Srrs SCTP_INP_RUNLOCK(inp); 2085163953Srrs } 2086166675Srrs *optsize = sizeof(struct sctp_paddrparams); 2087163953Srrs } 2088163953Srrs break; 2089163953Srrs case SCTP_GET_PEER_ADDR_INFO: 2090163953Srrs { 2091163953Srrs struct sctp_paddrinfo *paddri; 2092163953Srrs struct sctp_nets *net; 2093163953Srrs 2094166675Srrs SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize); 2095166675Srrs SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id); 2096166675Srrs 2097163953Srrs net = NULL; 2098166675Srrs if (stcb) { 2099166675Srrs net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address); 2100166675Srrs } else { 2101166675Srrs /* 2102166675Srrs * We increment here since 2103166675Srrs * sctp_findassociation_ep_addr() wil do a 2104166675Srrs * decrement if it finds the stcb as long as 2105166675Srrs * the locked tcb (last argument) is NOT a 2106166675Srrs * TCB.. aka NULL. 2107166675Srrs */ 2108166675Srrs SCTP_INP_INCR_REF(inp); 2109166675Srrs stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL); 2110166675Srrs if (stcb == NULL) { 2111166675Srrs SCTP_INP_DECR_REF(inp); 2112163953Srrs } 2113166675Srrs } 2114163953Srrs 2115166675Srrs if ((stcb) && (net)) { 2116166675Srrs paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB); 2117166675Srrs paddri->spinfo_cwnd = net->cwnd; 2118166675Srrs paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1; 2119166675Srrs paddri->spinfo_rto = net->RTO; 2120166675Srrs paddri->spinfo_assoc_id = sctp_get_associd(stcb); 2121166675Srrs SCTP_TCB_UNLOCK(stcb); 2122163953Srrs } else { 2123163953Srrs if (stcb) { 2124163953Srrs SCTP_TCB_UNLOCK(stcb); 2125163953Srrs } 2126163953Srrs error = ENOENT; 2127163953Srrs } 2128166675Srrs *optsize = sizeof(struct sctp_paddrinfo); 2129163953Srrs } 2130163953Srrs break; 2131163953Srrs case SCTP_PCB_STATUS: 2132163953Srrs { 2133163953Srrs struct sctp_pcbinfo *spcb; 2134163953Srrs 2135166675Srrs SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize); 2136163953Srrs sctp_fill_pcbinfo(spcb); 2137166675Srrs *optsize = sizeof(struct sctp_pcbinfo); 2138163953Srrs } 2139163953Srrs break; 2140167598Srrs 2141163953Srrs case SCTP_STATUS: 2142163953Srrs { 2143163953Srrs struct sctp_nets *net; 2144163953Srrs struct sctp_status *sstat; 2145163953Srrs 2146166675Srrs SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize); 2147166675Srrs SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id); 2148163953Srrs 2149163953Srrs if (stcb == NULL) { 2150163953Srrs error = EINVAL; 2151163953Srrs break; 2152163953Srrs } 2153163953Srrs /* 2154163953Srrs * I think passing the state is fine since 2155163953Srrs * sctp_constants.h will be available to the user 2156163953Srrs * land. 2157163953Srrs */ 2158163953Srrs sstat->sstat_state = stcb->asoc.state; 2159163953Srrs sstat->sstat_rwnd = stcb->asoc.peers_rwnd; 2160163953Srrs sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt; 2161163953Srrs /* 2162163953Srrs * We can't include chunks that have been passed to 2163163953Srrs * the socket layer. Only things in queue. 2164163953Srrs */ 2165163953Srrs sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue + 2166163953Srrs stcb->asoc.cnt_on_all_streams); 2167163953Srrs 2168163953Srrs 2169163953Srrs sstat->sstat_instrms = stcb->asoc.streamincnt; 2170163953Srrs sstat->sstat_outstrms = stcb->asoc.streamoutcnt; 2171163953Srrs sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc); 2172163953Srrs memcpy(&sstat->sstat_primary.spinfo_address, 2173163953Srrs &stcb->asoc.primary_destination->ro._l_addr, 2174163953Srrs ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len); 2175163953Srrs net = stcb->asoc.primary_destination; 2176163953Srrs ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport; 2177163953Srrs /* 2178163953Srrs * Again the user can get info from sctp_constants.h 2179163953Srrs * for what the state of the network is. 2180163953Srrs */ 2181163953Srrs sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK; 2182163953Srrs sstat->sstat_primary.spinfo_cwnd = net->cwnd; 2183163953Srrs sstat->sstat_primary.spinfo_srtt = net->lastsa; 2184163953Srrs sstat->sstat_primary.spinfo_rto = net->RTO; 2185163953Srrs sstat->sstat_primary.spinfo_mtu = net->mtu; 2186163953Srrs sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb); 2187163953Srrs SCTP_TCB_UNLOCK(stcb); 2188166675Srrs *optsize = sizeof(*sstat); 2189163953Srrs } 2190163953Srrs break; 2191163953Srrs case SCTP_RTOINFO: 2192163953Srrs { 2193163953Srrs struct sctp_rtoinfo *srto; 2194163953Srrs 2195166675Srrs SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize); 2196166675Srrs SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id); 2197166675Srrs 2198166675Srrs if (stcb) { 2199166675Srrs srto->srto_initial = stcb->asoc.initial_rto; 2200166675Srrs srto->srto_max = stcb->asoc.maxrto; 2201166675Srrs srto->srto_min = stcb->asoc.minrto; 2202166675Srrs SCTP_TCB_UNLOCK(stcb); 2203166675Srrs } else { 2204163953Srrs SCTP_INP_RLOCK(inp); 2205163953Srrs srto->srto_initial = inp->sctp_ep.initial_rto; 2206163953Srrs srto->srto_max = inp->sctp_ep.sctp_maxrto; 2207163953Srrs srto->srto_min = inp->sctp_ep.sctp_minrto; 2208163953Srrs SCTP_INP_RUNLOCK(inp); 2209163953Srrs } 2210166675Srrs *optsize = sizeof(*srto); 2211163953Srrs } 2212163953Srrs break; 2213163953Srrs case SCTP_ASSOCINFO: 2214163953Srrs { 2215163953Srrs struct sctp_assocparams *sasoc; 2216171477Srrs uint32_t oldval; 2217163953Srrs 2218166675Srrs SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize); 2219166675Srrs SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id); 2220163953Srrs 2221163953Srrs if (stcb) { 2222171477Srrs oldval = sasoc->sasoc_cookie_life; 2223171477Srrs sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life); 2224163953Srrs sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times; 2225163953Srrs sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets; 2226163953Srrs sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd; 2227163953Srrs sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd; 2228163953Srrs SCTP_TCB_UNLOCK(stcb); 2229163953Srrs } else { 2230163953Srrs SCTP_INP_RLOCK(inp); 2231171477Srrs sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life); 2232163953Srrs sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times; 2233163953Srrs sasoc->sasoc_number_peer_destinations = 0; 2234163953Srrs sasoc->sasoc_peer_rwnd = 0; 2235163953Srrs sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv); 2236163953Srrs SCTP_INP_RUNLOCK(inp); 2237163953Srrs } 2238166675Srrs *optsize = sizeof(*sasoc); 2239163953Srrs } 2240163953Srrs break; 2241163953Srrs case SCTP_DEFAULT_SEND_PARAM: 2242163953Srrs { 2243163953Srrs struct sctp_sndrcvinfo *s_info; 2244163953Srrs 2245166675Srrs SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize); 2246166675Srrs SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id); 2247166675Srrs 2248166675Srrs if (stcb) { 2249170056Srrs memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send)); 2250166675Srrs SCTP_TCB_UNLOCK(stcb); 2251166675Srrs } else { 2252163953Srrs SCTP_INP_RLOCK(inp); 2253170056Srrs memcpy(s_info, &inp->def_send, sizeof(inp->def_send)); 2254163953Srrs SCTP_INP_RUNLOCK(inp); 2255163953Srrs } 2256166675Srrs *optsize = sizeof(*s_info); 2257163953Srrs } 2258163953Srrs break; 2259163953Srrs case SCTP_INITMSG: 2260163953Srrs { 2261163953Srrs struct sctp_initmsg *sinit; 2262163953Srrs 2263166675Srrs SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize); 2264163953Srrs SCTP_INP_RLOCK(inp); 2265163953Srrs sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count; 2266163953Srrs sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome; 2267163953Srrs sinit->sinit_max_attempts = inp->sctp_ep.max_init_times; 2268163953Srrs sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max; 2269163953Srrs SCTP_INP_RUNLOCK(inp); 2270166675Srrs *optsize = sizeof(*sinit); 2271163953Srrs } 2272163953Srrs break; 2273163953Srrs case SCTP_PRIMARY_ADDR: 2274163953Srrs /* we allow a "get" operation on this */ 2275163953Srrs { 2276163953Srrs struct sctp_setprim *ssp; 2277163953Srrs 2278166675Srrs SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize); 2279166675Srrs SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id); 2280166675Srrs 2281166675Srrs if (stcb) { 2282166675Srrs /* simply copy out the sockaddr_storage... */ 2283170056Srrs int len; 2284170056Srrs 2285170056Srrs len = *optsize; 2286170056Srrs if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len) 2287170056Srrs len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len; 2288170056Srrs 2289170056Srrs memcpy(&ssp->ssp_addr, 2290170056Srrs &stcb->asoc.primary_destination->ro._l_addr, 2291170056Srrs len); 2292166675Srrs SCTP_TCB_UNLOCK(stcb); 2293166675Srrs } else { 2294163953Srrs error = EINVAL; 2295163953Srrs } 2296166675Srrs *optsize = sizeof(*ssp); 2297163953Srrs } 2298163953Srrs break; 2299163953Srrs 2300163953Srrs case SCTP_HMAC_IDENT: 2301163953Srrs { 2302163953Srrs struct sctp_hmacalgo *shmac; 2303163953Srrs sctp_hmaclist_t *hmaclist; 2304163953Srrs uint32_t size; 2305163953Srrs int i; 2306163953Srrs 2307166675Srrs SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize); 2308166675Srrs 2309163953Srrs SCTP_INP_RLOCK(inp); 2310163953Srrs hmaclist = inp->sctp_ep.local_hmacs; 2311163953Srrs if (hmaclist == NULL) { 2312163953Srrs /* no HMACs to return */ 2313166675Srrs *optsize = sizeof(*shmac); 2314168299Srrs SCTP_INP_RUNLOCK(inp); 2315163953Srrs break; 2316163953Srrs } 2317163953Srrs /* is there room for all of the hmac ids? */ 2318163953Srrs size = sizeof(*shmac) + (hmaclist->num_algo * 2319163953Srrs sizeof(shmac->shmac_idents[0])); 2320166675Srrs if ((size_t)(*optsize) < size) { 2321163953Srrs error = EINVAL; 2322163953Srrs SCTP_INP_RUNLOCK(inp); 2323163953Srrs break; 2324163953Srrs } 2325163953Srrs /* copy in the list */ 2326163953Srrs for (i = 0; i < hmaclist->num_algo; i++) 2327163953Srrs shmac->shmac_idents[i] = hmaclist->hmac[i]; 2328163953Srrs SCTP_INP_RUNLOCK(inp); 2329166675Srrs *optsize = size; 2330163953Srrs break; 2331163953Srrs } 2332163953Srrs case SCTP_AUTH_ACTIVE_KEY: 2333163953Srrs { 2334163953Srrs struct sctp_authkeyid *scact; 2335163953Srrs 2336166675Srrs SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize); 2337166675Srrs SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id); 2338166675Srrs 2339166675Srrs if (stcb) { 2340163953Srrs /* get the active key on the assoc */ 2341163953Srrs scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid; 2342163953Srrs SCTP_TCB_UNLOCK(stcb); 2343163953Srrs } else { 2344163953Srrs /* get the endpoint active key */ 2345163953Srrs SCTP_INP_RLOCK(inp); 2346163953Srrs scact->scact_keynumber = inp->sctp_ep.default_keyid; 2347163953Srrs SCTP_INP_RUNLOCK(inp); 2348163953Srrs } 2349166675Srrs *optsize = sizeof(*scact); 2350163953Srrs break; 2351163953Srrs } 2352163953Srrs case SCTP_LOCAL_AUTH_CHUNKS: 2353163953Srrs { 2354163953Srrs struct sctp_authchunks *sac; 2355163953Srrs sctp_auth_chklist_t *chklist = NULL; 2356166675Srrs size_t size = 0; 2357163953Srrs 2358166675Srrs SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize); 2359166675Srrs SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id); 2360166675Srrs 2361166675Srrs if (stcb) { 2362163953Srrs /* get off the assoc */ 2363163953Srrs chklist = stcb->asoc.local_auth_chunks; 2364163953Srrs /* is there enough space? */ 2365163953Srrs size = sctp_auth_get_chklist_size(chklist); 2366166675Srrs if (*optsize < (sizeof(struct sctp_authchunks) + size)) { 2367163953Srrs error = EINVAL; 2368166675Srrs } else { 2369166675Srrs /* copy in the chunks */ 2370169420Srrs (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks); 2371163953Srrs } 2372163953Srrs SCTP_TCB_UNLOCK(stcb); 2373163953Srrs } else { 2374163953Srrs /* get off the endpoint */ 2375163953Srrs SCTP_INP_RLOCK(inp); 2376163953Srrs chklist = inp->sctp_ep.local_auth_chunks; 2377163953Srrs /* is there enough space? */ 2378163953Srrs size = sctp_auth_get_chklist_size(chklist); 2379166675Srrs if (*optsize < (sizeof(struct sctp_authchunks) + size)) { 2380163953Srrs error = EINVAL; 2381166675Srrs } else { 2382166675Srrs /* copy in the chunks */ 2383169420Srrs (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks); 2384163953Srrs } 2385163953Srrs SCTP_INP_RUNLOCK(inp); 2386163953Srrs } 2387166675Srrs *optsize = sizeof(struct sctp_authchunks) + size; 2388163953Srrs break; 2389163953Srrs } 2390163953Srrs case SCTP_PEER_AUTH_CHUNKS: 2391163953Srrs { 2392163953Srrs struct sctp_authchunks *sac; 2393163953Srrs sctp_auth_chklist_t *chklist = NULL; 2394166675Srrs size_t size = 0; 2395163953Srrs 2396166675Srrs SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize); 2397166675Srrs SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id); 2398166675Srrs 2399166675Srrs if (stcb) { 2400166675Srrs /* get off the assoc */ 2401166675Srrs chklist = stcb->asoc.peer_auth_chunks; 2402166675Srrs /* is there enough space? */ 2403166675Srrs size = sctp_auth_get_chklist_size(chklist); 2404166675Srrs if (*optsize < (sizeof(struct sctp_authchunks) + size)) { 2405166675Srrs error = EINVAL; 2406166675Srrs } else { 2407166675Srrs /* copy in the chunks */ 2408169420Srrs (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks); 2409166675Srrs } 2410166675Srrs SCTP_TCB_UNLOCK(stcb); 2411166675Srrs } else { 2412163953Srrs error = ENOENT; 2413163953Srrs } 2414166675Srrs *optsize = sizeof(struct sctp_authchunks) + size; 2415163953Srrs break; 2416163953Srrs } 2417163953Srrs 2418163953Srrs 2419163953Srrs default: 2420163953Srrs error = ENOPROTOOPT; 2421166675Srrs *optsize = 0; 2422163953Srrs break; 2423163953Srrs } /* end switch (sopt->sopt_name) */ 2424163953Srrs return (error); 2425163953Srrs} 2426163953Srrs 2427163953Srrsstatic int 2428166675Srrssctp_setopt(struct socket *so, int optname, void *optval, size_t optsize, 2429166675Srrs void *p) 2430163953Srrs{ 2431166675Srrs int error, set_opt; 2432166675Srrs uint32_t *mopt; 2433163953Srrs struct sctp_tcb *stcb = NULL; 2434163953Srrs struct sctp_inpcb *inp; 2435167598Srrs uint32_t vrf_id; 2436163953Srrs 2437166675Srrs if (optval == NULL) { 2438169420Srrs SCTP_PRINTF("optval is NULL\n"); 2439163953Srrs return (EINVAL); 2440163953Srrs } 2441163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 2442167598Srrs if (inp == 0) { 2443169420Srrs SCTP_PRINTF("inp is NULL?\n"); 2444163953Srrs return EINVAL; 2445167598Srrs } 2446168299Srrs vrf_id = inp->def_vrf_id; 2447163953Srrs 2448163953Srrs error = 0; 2449166675Srrs switch (optname) { 2450163953Srrs case SCTP_NODELAY: 2451163953Srrs case SCTP_AUTOCLOSE: 2452163953Srrs case SCTP_AUTO_ASCONF: 2453163953Srrs case SCTP_EXPLICIT_EOR: 2454163953Srrs case SCTP_DISABLE_FRAGMENTS: 2455163953Srrs case SCTP_USE_EXT_RCVINFO: 2456163953Srrs case SCTP_I_WANT_MAPPED_V4_ADDR: 2457163953Srrs /* copy in the option value */ 2458166675Srrs SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize); 2459163953Srrs set_opt = 0; 2460163953Srrs if (error) 2461163953Srrs break; 2462166675Srrs switch (optname) { 2463163953Srrs case SCTP_DISABLE_FRAGMENTS: 2464163953Srrs set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT; 2465163953Srrs break; 2466163953Srrs case SCTP_AUTO_ASCONF: 2467163953Srrs set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF; 2468163953Srrs break; 2469163953Srrs case SCTP_EXPLICIT_EOR: 2470163953Srrs set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR; 2471163953Srrs break; 2472163953Srrs case SCTP_USE_EXT_RCVINFO: 2473163953Srrs set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO; 2474163953Srrs break; 2475163953Srrs case SCTP_I_WANT_MAPPED_V4_ADDR: 2476163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 2477163953Srrs set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4; 2478163953Srrs } else { 2479163953Srrs return (EINVAL); 2480163953Srrs } 2481163953Srrs break; 2482163953Srrs case SCTP_NODELAY: 2483163953Srrs set_opt = SCTP_PCB_FLAGS_NODELAY; 2484163953Srrs break; 2485163953Srrs case SCTP_AUTOCLOSE: 2486170056Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || 2487170056Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { 2488170056Srrs return (EINVAL); 2489170056Srrs } 2490163953Srrs set_opt = SCTP_PCB_FLAGS_AUTOCLOSE; 2491163953Srrs /* 2492163953Srrs * The value is in ticks. Note this does not effect 2493163953Srrs * old associations, only new ones. 2494163953Srrs */ 2495163953Srrs inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt); 2496163953Srrs break; 2497163953Srrs } 2498163953Srrs SCTP_INP_WLOCK(inp); 2499163953Srrs if (*mopt != 0) { 2500163953Srrs sctp_feature_on(inp, set_opt); 2501163953Srrs } else { 2502163953Srrs sctp_feature_off(inp, set_opt); 2503163953Srrs } 2504163953Srrs SCTP_INP_WUNLOCK(inp); 2505163953Srrs break; 2506163953Srrs case SCTP_PARTIAL_DELIVERY_POINT: 2507163953Srrs { 2508166675Srrs uint32_t *value; 2509166675Srrs 2510166675Srrs SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize); 2511167736Srrs if (*value > SCTP_SB_LIMIT_RCV(so)) { 2512167736Srrs error = EINVAL; 2513167736Srrs break; 2514167736Srrs } 2515166675Srrs inp->partial_delivery_point = *value; 2516163953Srrs } 2517163953Srrs break; 2518163953Srrs case SCTP_FRAGMENT_INTERLEAVE: 2519163953Srrs /* not yet until we re-write sctp_recvmsg() */ 2520163953Srrs { 2521168943Srrs uint32_t *level; 2522163953Srrs 2523168943Srrs SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize); 2524168943Srrs if (*level == SCTP_FRAG_LEVEL_2) { 2525163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 2526168943Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 2527168943Srrs } else if (*level == SCTP_FRAG_LEVEL_1) { 2528168943Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 2529168943Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 2530168943Srrs } else if (*level == SCTP_FRAG_LEVEL_0) { 2531170056Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); 2532168943Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); 2533168943Srrs 2534163953Srrs } else { 2535168943Srrs error = EINVAL; 2536163953Srrs } 2537163953Srrs } 2538163953Srrs break; 2539163953Srrs case SCTP_CMT_ON_OFF: 2540163953Srrs { 2541163953Srrs struct sctp_assoc_value *av; 2542163953Srrs 2543166675Srrs SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 2544166675Srrs if (sctp_cmt_on_off) { 2545166675Srrs SCTP_FIND_STCB(inp, stcb, av->assoc_id); 2546166675Srrs if (stcb) { 2547163953Srrs stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value; 2548166675Srrs SCTP_TCB_UNLOCK(stcb); 2549163953Srrs } else { 2550166675Srrs error = ENOTCONN; 2551163953Srrs } 2552166675Srrs } else { 2553166675Srrs error = ENOPROTOOPT; 2554163953Srrs } 2555163953Srrs } 2556163953Srrs break; 2557171440Srrs /* JRS - Set socket option for pluggable congestion control */ 2558171440Srrs case SCTP_PLUGGABLE_CC: 2559171440Srrs { 2560171440Srrs struct sctp_assoc_value *av; 2561171440Srrs 2562171440Srrs SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 2563171440Srrs SCTP_FIND_STCB(inp, stcb, av->assoc_id); 2564171440Srrs if (stcb) { 2565171440Srrs switch (av->assoc_value) { 2566171440Srrs /* 2567171440Srrs * JRS - Standard TCP congestion 2568171440Srrs * control 2569171440Srrs */ 2570171440Srrs case SCTP_CC_RFC2581: 2571171440Srrs { 2572171440Srrs stcb->asoc.congestion_control_module = SCTP_CC_RFC2581; 2573171440Srrs stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param; 2574171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack; 2575171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr; 2576171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout; 2577171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo; 2578171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped; 2579171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output; 2580171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer; 2581171440Srrs SCTP_TCB_UNLOCK(stcb); 2582171440Srrs break; 2583171440Srrs } 2584171440Srrs /* 2585171440Srrs * JRS - High Speed TCP congestion 2586171440Srrs * control (Floyd) 2587171440Srrs */ 2588171440Srrs case SCTP_CC_HSTCP: 2589171440Srrs { 2590171440Srrs stcb->asoc.congestion_control_module = SCTP_CC_HSTCP; 2591171440Srrs stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param; 2592171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack; 2593171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr; 2594171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout; 2595171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo; 2596171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped; 2597171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output; 2598171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer; 2599171440Srrs SCTP_TCB_UNLOCK(stcb); 2600171440Srrs break; 2601171440Srrs } 2602171440Srrs /* JRS - HTCP congestion control */ 2603171440Srrs case SCTP_CC_HTCP: 2604171440Srrs { 2605171440Srrs stcb->asoc.congestion_control_module = SCTP_CC_HTCP; 2606171440Srrs stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param; 2607171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack; 2608171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr; 2609171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout; 2610171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo; 2611171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped; 2612171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output; 2613171440Srrs stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer; 2614171440Srrs SCTP_TCB_UNLOCK(stcb); 2615171440Srrs break; 2616171440Srrs } 2617171440Srrs /* 2618171440Srrs * JRS - All other values are 2619171440Srrs * invalid 2620171440Srrs */ 2621171440Srrs default: 2622171440Srrs { 2623171440Srrs error = EINVAL; 2624171440Srrs SCTP_TCB_UNLOCK(stcb); 2625171440Srrs break; 2626171440Srrs } 2627171440Srrs } 2628171440Srrs } else { 2629171440Srrs switch (av->assoc_value) { 2630171440Srrs case SCTP_CC_RFC2581: 2631171440Srrs case SCTP_CC_HSTCP: 2632171440Srrs case SCTP_CC_HTCP: 2633171440Srrs inp->sctp_ep.sctp_default_cc_module = av->assoc_value; 2634171440Srrs break; 2635171440Srrs default: 2636171440Srrs error = EINVAL; 2637171440Srrs break; 2638171440Srrs }; 2639171440Srrs } 2640171440Srrs } 2641171440Srrs break; 2642163953Srrs case SCTP_CLR_STAT_LOG: 2643163953Srrs error = EOPNOTSUPP; 2644163953Srrs break; 2645163953Srrs case SCTP_CONTEXT: 2646163953Srrs { 2647163953Srrs struct sctp_assoc_value *av; 2648163953Srrs 2649166675Srrs SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 2650166675Srrs SCTP_FIND_STCB(inp, stcb, av->assoc_id); 2651166675Srrs 2652166675Srrs if (stcb) { 2653166675Srrs stcb->asoc.context = av->assoc_value; 2654166675Srrs SCTP_TCB_UNLOCK(stcb); 2655163953Srrs } else { 2656166675Srrs SCTP_INP_WLOCK(inp); 2657163953Srrs inp->sctp_context = av->assoc_value; 2658166675Srrs SCTP_INP_WUNLOCK(inp); 2659163953Srrs } 2660163953Srrs } 2661163953Srrs break; 2662167598Srrs case SCTP_VRF_ID: 2663167598Srrs { 2664170056Srrs uint32_t *default_vrfid; 2665167598Srrs 2666170056Srrs SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize); 2667170056Srrs if (*default_vrfid > SCTP_MAX_VRF_ID) { 2668167598Srrs error = EINVAL; 2669167598Srrs break; 2670167598Srrs } 2671170056Srrs inp->def_vrf_id = *default_vrfid; 2672167598Srrs break; 2673167598Srrs } 2674167598Srrs case SCTP_DEL_VRF_ID: 2675167598Srrs { 2676167598Srrs error = EOPNOTSUPP; 2677167598Srrs break; 2678167598Srrs } 2679167598Srrs case SCTP_ADD_VRF_ID: 2680167598Srrs { 2681167598Srrs error = EOPNOTSUPP; 2682167598Srrs break; 2683167598Srrs } 2684170056Srrs case SCTP_DELAYED_SACK: 2685163953Srrs { 2686170056Srrs struct sctp_sack_info *sack; 2687163953Srrs 2688170056Srrs SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize); 2689170056Srrs SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id); 2690171477Srrs if (sack->sack_delay) { 2691171477Srrs if (sack->sack_delay > SCTP_MAX_SACK_DELAY) 2692171477Srrs sack->sack_delay = SCTP_MAX_SACK_DELAY; 2693171477Srrs } 2694166675Srrs if (stcb) { 2695170056Srrs if (sack->sack_delay) { 2696170056Srrs if (MSEC_TO_TICKS(sack->sack_delay) < 1) { 2697170056Srrs sack->sack_delay = TICKS_TO_MSEC(1); 2698170056Srrs } 2699170056Srrs stcb->asoc.delayed_ack = sack->sack_delay; 2700170056Srrs } 2701170056Srrs if (sack->sack_freq) { 2702170056Srrs stcb->asoc.sack_freq = sack->sack_freq; 2703170056Srrs } 2704166675Srrs SCTP_TCB_UNLOCK(stcb); 2705166675Srrs } else { 2706163953Srrs SCTP_INP_WLOCK(inp); 2707170056Srrs if (sack->sack_delay) { 2708170056Srrs if (MSEC_TO_TICKS(sack->sack_delay) < 1) { 2709170056Srrs sack->sack_delay = TICKS_TO_MSEC(1); 2710170056Srrs } 2711170056Srrs inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay); 2712170056Srrs } 2713170056Srrs if (sack->sack_freq) { 2714170056Srrs inp->sctp_ep.sctp_sack_freq = sack->sack_freq; 2715170056Srrs } 2716163953Srrs SCTP_INP_WUNLOCK(inp); 2717163953Srrs } 2718166675Srrs break; 2719163953Srrs } 2720163953Srrs case SCTP_AUTH_CHUNK: 2721163953Srrs { 2722163953Srrs struct sctp_authchunk *sauth; 2723163953Srrs 2724166675Srrs SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize); 2725166675Srrs 2726166675Srrs SCTP_INP_WLOCK(inp); 2727166675Srrs if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) 2728163953Srrs error = EINVAL; 2729166675Srrs SCTP_INP_WUNLOCK(inp); 2730163953Srrs break; 2731163953Srrs } 2732163953Srrs case SCTP_AUTH_KEY: 2733163953Srrs { 2734163953Srrs struct sctp_authkey *sca; 2735163953Srrs struct sctp_keyhead *shared_keys; 2736163953Srrs sctp_sharedkey_t *shared_key; 2737163953Srrs sctp_key_t *key = NULL; 2738166675Srrs size_t size; 2739163953Srrs 2740166675Srrs SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize); 2741169420Srrs SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id); 2742169420Srrs size = optsize - sizeof(*sca); 2743166675Srrs 2744166675Srrs if (stcb) { 2745163953Srrs /* set it on the assoc */ 2746163953Srrs shared_keys = &stcb->asoc.shared_keys; 2747163953Srrs /* clear the cached keys for this key id */ 2748163953Srrs sctp_clear_cachedkeys(stcb, sca->sca_keynumber); 2749163953Srrs /* 2750163953Srrs * create the new shared key and 2751163953Srrs * insert/replace it 2752163953Srrs */ 2753163953Srrs if (size > 0) { 2754163953Srrs key = sctp_set_key(sca->sca_key, (uint32_t) size); 2755163953Srrs if (key == NULL) { 2756163953Srrs error = ENOMEM; 2757163953Srrs SCTP_TCB_UNLOCK(stcb); 2758163953Srrs break; 2759163953Srrs } 2760163953Srrs } 2761163953Srrs shared_key = sctp_alloc_sharedkey(); 2762163953Srrs if (shared_key == NULL) { 2763163953Srrs sctp_free_key(key); 2764163953Srrs error = ENOMEM; 2765163953Srrs SCTP_TCB_UNLOCK(stcb); 2766163953Srrs break; 2767163953Srrs } 2768163953Srrs shared_key->key = key; 2769163953Srrs shared_key->keyid = sca->sca_keynumber; 2770163953Srrs sctp_insert_sharedkey(shared_keys, shared_key); 2771163953Srrs SCTP_TCB_UNLOCK(stcb); 2772163953Srrs } else { 2773166675Srrs /* set it on the endpoint */ 2774163953Srrs SCTP_INP_WLOCK(inp); 2775163953Srrs shared_keys = &inp->sctp_ep.shared_keys; 2776163953Srrs /* 2777163953Srrs * clear the cached keys on all assocs for 2778163953Srrs * this key id 2779163953Srrs */ 2780163953Srrs sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber); 2781163953Srrs /* 2782163953Srrs * create the new shared key and 2783163953Srrs * insert/replace it 2784163953Srrs */ 2785163953Srrs if (size > 0) { 2786163953Srrs key = sctp_set_key(sca->sca_key, (uint32_t) size); 2787163953Srrs if (key == NULL) { 2788163953Srrs error = ENOMEM; 2789163953Srrs SCTP_INP_WUNLOCK(inp); 2790163953Srrs break; 2791163953Srrs } 2792163953Srrs } 2793163953Srrs shared_key = sctp_alloc_sharedkey(); 2794163953Srrs if (shared_key == NULL) { 2795163953Srrs sctp_free_key(key); 2796163953Srrs error = ENOMEM; 2797163953Srrs SCTP_INP_WUNLOCK(inp); 2798163953Srrs break; 2799163953Srrs } 2800163953Srrs shared_key->key = key; 2801163953Srrs shared_key->keyid = sca->sca_keynumber; 2802163953Srrs sctp_insert_sharedkey(shared_keys, shared_key); 2803163953Srrs SCTP_INP_WUNLOCK(inp); 2804163953Srrs } 2805163953Srrs break; 2806163953Srrs } 2807163953Srrs case SCTP_HMAC_IDENT: 2808163953Srrs { 2809163953Srrs struct sctp_hmacalgo *shmac; 2810163953Srrs sctp_hmaclist_t *hmaclist; 2811163953Srrs uint32_t hmacid; 2812170056Srrs size_t size, i, found; 2813163953Srrs 2814166675Srrs SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize); 2815166675Srrs size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]); 2816163953Srrs hmaclist = sctp_alloc_hmaclist(size); 2817163953Srrs if (hmaclist == NULL) { 2818163953Srrs error = ENOMEM; 2819163953Srrs break; 2820163953Srrs } 2821163953Srrs for (i = 0; i < size; i++) { 2822163953Srrs hmacid = shmac->shmac_idents[i]; 2823163953Srrs if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) { 2824163953Srrs /* invalid HMACs were found */ ; 2825163953Srrs error = EINVAL; 2826164085Srrs sctp_free_hmaclist(hmaclist); 2827163953Srrs goto sctp_set_hmac_done; 2828163953Srrs } 2829163953Srrs } 2830170056Srrs found = 0; 2831170056Srrs for (i = 0; i < hmaclist->num_algo; i++) { 2832170056Srrs if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) { 2833170056Srrs /* already in list */ 2834170056Srrs found = 1; 2835170056Srrs } 2836170056Srrs } 2837170056Srrs if (!found) { 2838170056Srrs sctp_free_hmaclist(hmaclist); 2839170056Srrs error = EINVAL; 2840170056Srrs break; 2841170056Srrs } 2842163953Srrs /* set it on the endpoint */ 2843163953Srrs SCTP_INP_WLOCK(inp); 2844163953Srrs if (inp->sctp_ep.local_hmacs) 2845163953Srrs sctp_free_hmaclist(inp->sctp_ep.local_hmacs); 2846163953Srrs inp->sctp_ep.local_hmacs = hmaclist; 2847163953Srrs SCTP_INP_WUNLOCK(inp); 2848163953Srrs sctp_set_hmac_done: 2849163953Srrs break; 2850163953Srrs } 2851163953Srrs case SCTP_AUTH_ACTIVE_KEY: 2852163953Srrs { 2853163953Srrs struct sctp_authkeyid *scact; 2854163953Srrs 2855166675Srrs SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize); 2856166675Srrs SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id); 2857166675Srrs 2858163953Srrs /* set the active key on the right place */ 2859166675Srrs if (stcb) { 2860163953Srrs /* set the active key on the assoc */ 2861163953Srrs if (sctp_auth_setactivekey(stcb, scact->scact_keynumber)) 2862163953Srrs error = EINVAL; 2863163953Srrs SCTP_TCB_UNLOCK(stcb); 2864163953Srrs } else { 2865163953Srrs /* set the active key on the endpoint */ 2866163953Srrs SCTP_INP_WLOCK(inp); 2867163953Srrs if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) 2868163953Srrs error = EINVAL; 2869163953Srrs SCTP_INP_WUNLOCK(inp); 2870163953Srrs } 2871163953Srrs break; 2872163953Srrs } 2873163953Srrs case SCTP_AUTH_DELETE_KEY: 2874163953Srrs { 2875163953Srrs struct sctp_authkeyid *scdel; 2876163953Srrs 2877166675Srrs SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize); 2878166675Srrs SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id); 2879166675Srrs 2880163953Srrs /* delete the key from the right place */ 2881166675Srrs if (stcb) { 2882163953Srrs if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) 2883163953Srrs error = EINVAL; 2884163953Srrs SCTP_TCB_UNLOCK(stcb); 2885163953Srrs } else { 2886163953Srrs SCTP_INP_WLOCK(inp); 2887163953Srrs if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) 2888163953Srrs error = EINVAL; 2889163953Srrs SCTP_INP_WUNLOCK(inp); 2890163953Srrs } 2891163953Srrs break; 2892163953Srrs } 2893163953Srrs 2894163953Srrs case SCTP_RESET_STREAMS: 2895163953Srrs { 2896163953Srrs struct sctp_stream_reset *strrst; 2897163953Srrs uint8_t send_in = 0, send_tsn = 0, send_out = 0; 2898163953Srrs int i; 2899163953Srrs 2900166675Srrs SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize); 2901166675Srrs SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id); 2902163953Srrs 2903163953Srrs if (stcb == NULL) { 2904163953Srrs error = ENOENT; 2905163953Srrs break; 2906163953Srrs } 2907163953Srrs if (stcb->asoc.peer_supports_strreset == 0) { 2908163953Srrs /* 2909163953Srrs * Peer does not support it, we return 2910163953Srrs * protocol not supported since this is true 2911163953Srrs * for this feature and this peer, not the 2912163953Srrs * socket request in general. 2913163953Srrs */ 2914163953Srrs error = EPROTONOSUPPORT; 2915163953Srrs SCTP_TCB_UNLOCK(stcb); 2916163953Srrs break; 2917163953Srrs } 2918163953Srrs if (stcb->asoc.stream_reset_outstanding) { 2919163953Srrs error = EALREADY; 2920163953Srrs SCTP_TCB_UNLOCK(stcb); 2921163953Srrs break; 2922163953Srrs } 2923163953Srrs if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) { 2924163953Srrs send_in = 1; 2925163953Srrs } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) { 2926163953Srrs send_out = 1; 2927163953Srrs } else if (strrst->strrst_flags == SCTP_RESET_BOTH) { 2928163953Srrs send_in = 1; 2929163953Srrs send_out = 1; 2930163953Srrs } else if (strrst->strrst_flags == SCTP_RESET_TSN) { 2931163953Srrs send_tsn = 1; 2932163953Srrs } else { 2933163953Srrs error = EINVAL; 2934163953Srrs SCTP_TCB_UNLOCK(stcb); 2935163953Srrs break; 2936163953Srrs } 2937163953Srrs for (i = 0; i < strrst->strrst_num_streams; i++) { 2938163953Srrs if ((send_in) && 2939163953Srrs 2940163953Srrs (strrst->strrst_list[i] > stcb->asoc.streamincnt)) { 2941163953Srrs error = EINVAL; 2942163953Srrs goto get_out; 2943163953Srrs } 2944163953Srrs if ((send_out) && 2945163953Srrs (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) { 2946163953Srrs error = EINVAL; 2947163953Srrs goto get_out; 2948163953Srrs } 2949163953Srrs } 2950163953Srrs if (error) { 2951163953Srrs get_out: 2952163953Srrs SCTP_TCB_UNLOCK(stcb); 2953163953Srrs break; 2954163953Srrs } 2955163953Srrs error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams, 2956163953Srrs strrst->strrst_list, 2957163953Srrs send_out, (stcb->asoc.str_reset_seq_in - 3), 2958163953Srrs send_in, send_tsn); 2959163953Srrs 2960163953Srrs sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ); 2961163953Srrs SCTP_TCB_UNLOCK(stcb); 2962163953Srrs } 2963163953Srrs break; 2964166675Srrs 2965163953Srrs case SCTP_CONNECT_X: 2966166675Srrs if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) { 2967163953Srrs error = EINVAL; 2968163953Srrs break; 2969163953Srrs } 2970166675Srrs error = sctp_do_connect_x(so, inp, optval, optsize, p, 0); 2971163953Srrs break; 2972163953Srrs 2973163953Srrs case SCTP_CONNECT_X_DELAYED: 2974166675Srrs if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) { 2975163953Srrs error = EINVAL; 2976163953Srrs break; 2977163953Srrs } 2978166675Srrs error = sctp_do_connect_x(so, inp, optval, optsize, p, 1); 2979163953Srrs break; 2980163953Srrs 2981163953Srrs case SCTP_CONNECT_X_COMPLETE: 2982163953Srrs { 2983163953Srrs struct sockaddr *sa; 2984163953Srrs struct sctp_nets *net; 2985163953Srrs 2986166675Srrs /* FIXME MT: check correct? */ 2987166675Srrs SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize); 2988166675Srrs 2989163953Srrs /* find tcb */ 2990163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 2991163953Srrs SCTP_INP_RLOCK(inp); 2992163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 2993163953Srrs if (stcb) { 2994163953Srrs SCTP_TCB_LOCK(stcb); 2995163953Srrs net = sctp_findnet(stcb, sa); 2996163953Srrs } 2997163953Srrs SCTP_INP_RUNLOCK(inp); 2998163953Srrs } else { 2999166675Srrs /* 3000166675Srrs * We increment here since 3001166675Srrs * sctp_findassociation_ep_addr() wil do a 3002166675Srrs * decrement if it finds the stcb as long as 3003166675Srrs * the locked tcb (last argument) is NOT a 3004166675Srrs * TCB.. aka NULL. 3005166675Srrs */ 3006163953Srrs SCTP_INP_INCR_REF(inp); 3007163953Srrs stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL); 3008163953Srrs if (stcb == NULL) { 3009163953Srrs SCTP_INP_DECR_REF(inp); 3010163953Srrs } 3011163953Srrs } 3012163953Srrs 3013163953Srrs if (stcb == NULL) { 3014163953Srrs error = ENOENT; 3015163953Srrs break; 3016163953Srrs } 3017163953Srrs if (stcb->asoc.delayed_connection == 1) { 3018163953Srrs stcb->asoc.delayed_connection = 0; 3019169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 3020165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, 3021165220Srrs stcb->asoc.primary_destination, 3022165220Srrs SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9); 3023163953Srrs sctp_send_initiate(inp, stcb); 3024163953Srrs } else { 3025163953Srrs /* 3026163953Srrs * already expired or did not use delayed 3027163953Srrs * connectx 3028163953Srrs */ 3029163953Srrs error = EALREADY; 3030163953Srrs } 3031163953Srrs SCTP_TCB_UNLOCK(stcb); 3032163953Srrs } 3033163953Srrs break; 3034170056Srrs case SCTP_MAX_BURST: 3035163953Srrs { 3036163953Srrs uint8_t *burst; 3037163953Srrs 3038166675Srrs SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize); 3039166675Srrs 3040163953Srrs SCTP_INP_WLOCK(inp); 3041163953Srrs if (*burst) { 3042163953Srrs inp->sctp_ep.max_burst = *burst; 3043163953Srrs } 3044163953Srrs SCTP_INP_WUNLOCK(inp); 3045163953Srrs } 3046163953Srrs break; 3047163953Srrs case SCTP_MAXSEG: 3048163953Srrs { 3049167598Srrs struct sctp_assoc_value *av; 3050163953Srrs int ovh; 3051163953Srrs 3052167598Srrs SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize); 3053167598Srrs SCTP_FIND_STCB(inp, stcb, av->assoc_id); 3054166675Srrs 3055170056Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 3056170056Srrs ovh = SCTP_MED_OVERHEAD; 3057170056Srrs } else { 3058170056Srrs ovh = SCTP_MED_V4_OVERHEAD; 3059170056Srrs } 3060167598Srrs if (stcb) { 3061170056Srrs if (av->assoc_value) { 3062170056Srrs stcb->asoc.sctp_frag_point = (av->assoc_value + ovh); 3063170056Srrs } else { 3064170056Srrs stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT; 3065170056Srrs } 3066167598Srrs SCTP_TCB_UNLOCK(stcb); 3067163953Srrs } else { 3068167598Srrs SCTP_INP_WLOCK(inp); 3069167598Srrs /* 3070167598Srrs * FIXME MT: I think this is not in tune 3071167598Srrs * with the API ID 3072167598Srrs */ 3073167598Srrs if (av->assoc_value) { 3074167598Srrs inp->sctp_frag_point = (av->assoc_value + ovh); 3075167598Srrs } else { 3076170056Srrs inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT; 3077167598Srrs } 3078167598Srrs SCTP_INP_WUNLOCK(inp); 3079163953Srrs } 3080163953Srrs } 3081163953Srrs break; 3082163953Srrs case SCTP_EVENTS: 3083163953Srrs { 3084163953Srrs struct sctp_event_subscribe *events; 3085163953Srrs 3086166675Srrs SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize); 3087166675Srrs 3088163953Srrs SCTP_INP_WLOCK(inp); 3089163953Srrs if (events->sctp_data_io_event) { 3090163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT); 3091163953Srrs } else { 3092163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT); 3093163953Srrs } 3094163953Srrs 3095163953Srrs if (events->sctp_association_event) { 3096163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT); 3097163953Srrs } else { 3098163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT); 3099163953Srrs } 3100163953Srrs 3101163953Srrs if (events->sctp_address_event) { 3102163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT); 3103163953Srrs } else { 3104163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT); 3105163953Srrs } 3106163953Srrs 3107163953Srrs if (events->sctp_send_failure_event) { 3108163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT); 3109163953Srrs } else { 3110163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT); 3111163953Srrs } 3112163953Srrs 3113163953Srrs if (events->sctp_peer_error_event) { 3114163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR); 3115163953Srrs } else { 3116163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR); 3117163953Srrs } 3118163953Srrs 3119163953Srrs if (events->sctp_shutdown_event) { 3120163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT); 3121163953Srrs } else { 3122163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT); 3123163953Srrs } 3124163953Srrs 3125163953Srrs if (events->sctp_partial_delivery_event) { 3126163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT); 3127163953Srrs } else { 3128163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT); 3129163953Srrs } 3130163953Srrs 3131163953Srrs if (events->sctp_adaptation_layer_event) { 3132163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT); 3133163953Srrs } else { 3134163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT); 3135163953Srrs } 3136163953Srrs 3137163953Srrs if (events->sctp_authentication_event) { 3138163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT); 3139163953Srrs } else { 3140163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT); 3141163953Srrs } 3142163953Srrs 3143163953Srrs if (events->sctp_stream_reset_events) { 3144163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT); 3145163953Srrs } else { 3146163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT); 3147163953Srrs } 3148163953Srrs SCTP_INP_WUNLOCK(inp); 3149163953Srrs } 3150163953Srrs break; 3151163953Srrs 3152163953Srrs case SCTP_ADAPTATION_LAYER: 3153163953Srrs { 3154163953Srrs struct sctp_setadaptation *adap_bits; 3155163953Srrs 3156166675Srrs SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize); 3157163953Srrs SCTP_INP_WLOCK(inp); 3158163953Srrs inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind; 3159163953Srrs SCTP_INP_WUNLOCK(inp); 3160163953Srrs } 3161163953Srrs break; 3162166675Srrs#ifdef SCTP_DEBUG 3163163953Srrs case SCTP_SET_INITIAL_DBG_SEQ: 3164163953Srrs { 3165163953Srrs uint32_t *vvv; 3166163953Srrs 3167166675Srrs SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize); 3168163953Srrs SCTP_INP_WLOCK(inp); 3169163953Srrs inp->sctp_ep.initial_sequence_debug = *vvv; 3170163953Srrs SCTP_INP_WUNLOCK(inp); 3171163953Srrs } 3172163953Srrs break; 3173166675Srrs#endif 3174163953Srrs case SCTP_DEFAULT_SEND_PARAM: 3175163953Srrs { 3176163953Srrs struct sctp_sndrcvinfo *s_info; 3177163953Srrs 3178166675Srrs SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize); 3179166675Srrs SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id); 3180163953Srrs 3181166675Srrs if (stcb) { 3182166675Srrs if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) { 3183170056Srrs memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send))); 3184163953Srrs } else { 3185166675Srrs error = EINVAL; 3186163953Srrs } 3187166675Srrs SCTP_TCB_UNLOCK(stcb); 3188166675Srrs } else { 3189166675Srrs SCTP_INP_WLOCK(inp); 3190170056Srrs memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send))); 3191166675Srrs SCTP_INP_WUNLOCK(inp); 3192163953Srrs } 3193163953Srrs } 3194163953Srrs break; 3195163953Srrs case SCTP_PEER_ADDR_PARAMS: 3196163953Srrs /* Applys to the specific association */ 3197163953Srrs { 3198163953Srrs struct sctp_paddrparams *paddrp; 3199163953Srrs struct sctp_nets *net; 3200163953Srrs 3201166675Srrs SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize); 3202166675Srrs SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id); 3203163953Srrs net = NULL; 3204166675Srrs if (stcb) { 3205166675Srrs net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address); 3206166675Srrs } else { 3207166675Srrs /* 3208166675Srrs * We increment here since 3209166675Srrs * sctp_findassociation_ep_addr() wil do a 3210166675Srrs * decrement if it finds the stcb as long as 3211166675Srrs * the locked tcb (last argument) is NOT a 3212166675Srrs * TCB.. aka NULL. 3213166675Srrs */ 3214166675Srrs SCTP_INP_INCR_REF(inp); 3215166675Srrs stcb = sctp_findassociation_ep_addr(&inp, 3216166675Srrs (struct sockaddr *)&paddrp->spp_address, 3217166675Srrs &net, NULL, NULL); 3218163953Srrs if (stcb == NULL) { 3219166675Srrs SCTP_INP_DECR_REF(inp); 3220163953Srrs } 3221163953Srrs } 3222170056Srrs /* sanity checks */ 3223170056Srrs if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) { 3224170056Srrs if (stcb) 3225170056Srrs SCTP_TCB_UNLOCK(stcb); 3226170056Srrs return (EINVAL); 3227170056Srrs } 3228170056Srrs if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) { 3229170056Srrs if (stcb) 3230170056Srrs SCTP_TCB_UNLOCK(stcb); 3231170056Srrs return (EINVAL); 3232170056Srrs } 3233163953Srrs if (stcb) { 3234163953Srrs /************************TCB SPECIFIC SET ******************/ 3235163953Srrs /* 3236163953Srrs * do we change the timer for HB, we run 3237163953Srrs * only one? 3238163953Srrs */ 3239170056Srrs int ovh = 0; 3240170056Srrs 3241170056Srrs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { 3242170056Srrs ovh = SCTP_MED_OVERHEAD; 3243170056Srrs } else { 3244170056Srrs ovh = SCTP_MED_V4_OVERHEAD; 3245170056Srrs } 3246170056Srrs 3247163953Srrs if (paddrp->spp_hbinterval) 3248163953Srrs stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval; 3249163953Srrs else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) 3250163953Srrs stcb->asoc.heart_beat_delay = 0; 3251163953Srrs 3252163953Srrs /* network sets ? */ 3253163953Srrs if (net) { 3254163953Srrs /************************NET SPECIFIC SET ******************/ 3255163953Srrs if (paddrp->spp_flags & SPP_HB_DEMAND) { 3256163953Srrs /* on demand HB */ 3257171440Srrs if (sctp_send_hb(stcb, 1, net) < 0) { 3258171440Srrs /* asoc destroyed */ 3259171440Srrs error = EINVAL; 3260171440Srrs break; 3261171440Srrs } 3262163953Srrs } 3263163953Srrs if (paddrp->spp_flags & SPP_HB_DISABLE) { 3264163953Srrs net->dest_state |= SCTP_ADDR_NOHB; 3265163953Srrs } 3266163953Srrs if (paddrp->spp_flags & SPP_HB_ENABLE) { 3267163953Srrs net->dest_state &= ~SCTP_ADDR_NOHB; 3268163953Srrs } 3269170056Srrs if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) { 3270165647Srrs if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 3271165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, 3272165220Srrs SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10); 3273163953Srrs } 3274163953Srrs if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) { 3275170056Srrs net->mtu = paddrp->spp_pathmtu + ovh; 3276169352Srrs if (net->mtu < stcb->asoc.smallest_mtu) { 3277169352Srrs#ifdef SCTP_PRINT_FOR_B_AND_M 3278169420Srrs SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n", 3279169352Srrs net->mtu); 3280169352Srrs#endif 3281167695Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu); 3282169352Srrs } 3283163953Srrs } 3284163953Srrs } 3285163953Srrs if (paddrp->spp_flags & SPP_PMTUD_ENABLE) { 3286165647Srrs if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 3287163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net); 3288163953Srrs } 3289163953Srrs } 3290163953Srrs if (paddrp->spp_pathmaxrxt) 3291163953Srrs net->failure_threshold = paddrp->spp_pathmaxrxt; 3292167598Srrs#ifdef INET 3293163953Srrs if (paddrp->spp_flags & SPP_IPV4_TOS) { 3294163953Srrs if (net->ro._l_addr.sin.sin_family == AF_INET) { 3295163953Srrs net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc; 3296163953Srrs } 3297163953Srrs } 3298163953Srrs#endif 3299167598Srrs#ifdef INET6 3300163953Srrs if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) { 3301163953Srrs if (net->ro._l_addr.sin6.sin6_family == AF_INET6) { 3302163953Srrs net->tos_flowlabel = paddrp->spp_ipv6_flowlabel; 3303163953Srrs } 3304163953Srrs } 3305163953Srrs#endif 3306163953Srrs } else { 3307163953Srrs /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/ 3308163953Srrs if (paddrp->spp_pathmaxrxt) 3309163953Srrs stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt; 3310163953Srrs 3311163953Srrs if (paddrp->spp_flags & SPP_HB_ENABLE) { 3312163953Srrs /* Turn back on the timer */ 3313163953Srrs stcb->asoc.hb_is_disabled = 0; 3314163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net); 3315163953Srrs } 3316170056Srrs if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) { 3317170056Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3318170056Srrs if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 3319170056Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, 3320170056Srrs SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10); 3321170056Srrs } 3322170056Srrs if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) { 3323170056Srrs net->mtu = paddrp->spp_pathmtu + ovh; 3324170056Srrs if (net->mtu < stcb->asoc.smallest_mtu) { 3325170056Srrs#ifdef SCTP_PRINT_FOR_B_AND_M 3326170056Srrs SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n", 3327170056Srrs net->mtu); 3328170056Srrs#endif 3329170056Srrs sctp_pathmtu_adjustment(inp, stcb, net, net->mtu); 3330170056Srrs } 3331170056Srrs } 3332170056Srrs } 3333170056Srrs } 3334170056Srrs if (paddrp->spp_flags & SPP_PMTUD_ENABLE) { 3335170056Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3336170056Srrs if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 3337170056Srrs sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net); 3338170056Srrs } 3339170056Srrs } 3340170056Srrs } 3341163953Srrs if (paddrp->spp_flags & SPP_HB_DISABLE) { 3342163953Srrs int cnt_of_unconf = 0; 3343163953Srrs struct sctp_nets *lnet; 3344163953Srrs 3345163953Srrs stcb->asoc.hb_is_disabled = 1; 3346163953Srrs TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) { 3347163953Srrs if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) { 3348163953Srrs cnt_of_unconf++; 3349163953Srrs } 3350163953Srrs } 3351163953Srrs /* 3352163953Srrs * stop the timer ONLY if we 3353163953Srrs * have no unconfirmed 3354163953Srrs * addresses 3355163953Srrs */ 3356163953Srrs if (cnt_of_unconf == 0) { 3357170056Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3358170056Srrs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, 3359170056Srrs SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11); 3360170056Srrs } 3361163953Srrs } 3362163953Srrs } 3363163953Srrs if (paddrp->spp_flags & SPP_HB_ENABLE) { 3364163953Srrs /* start up the timer. */ 3365170056Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 3366170056Srrs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net); 3367170056Srrs } 3368163953Srrs } 3369167598Srrs#ifdef INET 3370163953Srrs if (paddrp->spp_flags & SPP_IPV4_TOS) 3371163953Srrs stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc; 3372163953Srrs#endif 3373167598Srrs#ifdef INET6 3374163953Srrs if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) 3375163953Srrs stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel; 3376163953Srrs#endif 3377163953Srrs 3378163953Srrs } 3379163953Srrs SCTP_TCB_UNLOCK(stcb); 3380163953Srrs } else { 3381163953Srrs /************************NO TCB, SET TO default stuff ******************/ 3382163953Srrs SCTP_INP_WLOCK(inp); 3383163953Srrs /* 3384163953Srrs * For the TOS/FLOWLABEL stuff you set it 3385163953Srrs * with the options on the socket 3386163953Srrs */ 3387163953Srrs if (paddrp->spp_pathmaxrxt) { 3388163953Srrs inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt; 3389163953Srrs } 3390170056Srrs if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) 3391170056Srrs inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0; 3392171477Srrs else if (paddrp->spp_hbinterval) { 3393171477Srrs if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL) 3394171477Srrs paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL; 3395170056Srrs inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval); 3396171477Srrs } 3397163953Srrs if (paddrp->spp_flags & SPP_HB_ENABLE) { 3398163953Srrs sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT); 3399170056Srrs 3400163953Srrs } else if (paddrp->spp_flags & SPP_HB_DISABLE) { 3401163953Srrs sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT); 3402163953Srrs } 3403163953Srrs SCTP_INP_WUNLOCK(inp); 3404163953Srrs } 3405163953Srrs } 3406163953Srrs break; 3407163953Srrs case SCTP_RTOINFO: 3408163953Srrs { 3409163953Srrs struct sctp_rtoinfo *srto; 3410169655Srrs uint32_t new_init, new_min, new_max; 3411163953Srrs 3412166675Srrs SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize); 3413166675Srrs SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id); 3414166675Srrs 3415166675Srrs if (stcb) { 3416167598Srrs if (srto->srto_initial) 3417169655Srrs new_init = srto->srto_initial; 3418169655Srrs else 3419169655Srrs new_init = stcb->asoc.initial_rto; 3420167598Srrs if (srto->srto_max) 3421169655Srrs new_max = srto->srto_max; 3422169655Srrs else 3423169655Srrs new_max = stcb->asoc.maxrto; 3424167598Srrs if (srto->srto_min) 3425169655Srrs new_min = srto->srto_min; 3426169655Srrs else 3427169655Srrs new_min = stcb->asoc.minrto; 3428169655Srrs if ((new_min <= new_init) && (new_init <= new_max)) { 3429169655Srrs stcb->asoc.initial_rto = new_init; 3430169655Srrs stcb->asoc.maxrto = new_max; 3431169655Srrs stcb->asoc.minrto = new_min; 3432169655Srrs } else { 3433169655Srrs error = EDOM; 3434169655Srrs } 3435166675Srrs SCTP_TCB_UNLOCK(stcb); 3436166675Srrs } else { 3437163953Srrs SCTP_INP_WLOCK(inp); 3438167598Srrs if (srto->srto_initial) 3439169655Srrs new_init = srto->srto_initial; 3440169655Srrs else 3441169655Srrs new_init = inp->sctp_ep.initial_rto; 3442167598Srrs if (srto->srto_max) 3443169655Srrs new_max = srto->srto_max; 3444169655Srrs else 3445169655Srrs new_max = inp->sctp_ep.sctp_maxrto; 3446167598Srrs if (srto->srto_min) 3447169655Srrs new_min = srto->srto_min; 3448169655Srrs else 3449169655Srrs new_min = inp->sctp_ep.sctp_minrto; 3450169655Srrs if ((new_min <= new_init) && (new_init <= new_max)) { 3451169655Srrs inp->sctp_ep.initial_rto = new_init; 3452169655Srrs inp->sctp_ep.sctp_maxrto = new_max; 3453169655Srrs inp->sctp_ep.sctp_minrto = new_min; 3454169655Srrs } else { 3455169655Srrs error = EDOM; 3456169655Srrs } 3457163953Srrs SCTP_INP_WUNLOCK(inp); 3458163953Srrs } 3459163953Srrs } 3460163953Srrs break; 3461163953Srrs case SCTP_ASSOCINFO: 3462163953Srrs { 3463163953Srrs struct sctp_assocparams *sasoc; 3464163953Srrs 3465166675Srrs SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize); 3466166675Srrs SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id); 3467171477Srrs if (sasoc->sasoc_cookie_life) { 3468171477Srrs /* boundary check the cookie life */ 3469171477Srrs if (sasoc->sasoc_cookie_life < 1000) 3470171477Srrs sasoc->sasoc_cookie_life = 1000; 3471171477Srrs if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) { 3472171477Srrs sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE; 3473171477Srrs } 3474171477Srrs } 3475163953Srrs if (stcb) { 3476163953Srrs if (sasoc->sasoc_asocmaxrxt) 3477163953Srrs stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt; 3478163953Srrs sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets; 3479163953Srrs sasoc->sasoc_peer_rwnd = 0; 3480163953Srrs sasoc->sasoc_local_rwnd = 0; 3481170056Srrs if (sasoc->sasoc_cookie_life) { 3482171477Srrs stcb->asoc.cookie_life = sasoc->sasoc_cookie_life; 3483171477Srrs 3484167598Srrs } 3485163953Srrs SCTP_TCB_UNLOCK(stcb); 3486163953Srrs } else { 3487163953Srrs SCTP_INP_WLOCK(inp); 3488163953Srrs if (sasoc->sasoc_asocmaxrxt) 3489163953Srrs inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt; 3490163953Srrs sasoc->sasoc_number_peer_destinations = 0; 3491163953Srrs sasoc->sasoc_peer_rwnd = 0; 3492163953Srrs sasoc->sasoc_local_rwnd = 0; 3493170056Srrs if (sasoc->sasoc_cookie_life) { 3494169655Srrs inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life); 3495167598Srrs } 3496163953Srrs SCTP_INP_WUNLOCK(inp); 3497163953Srrs } 3498163953Srrs } 3499163953Srrs break; 3500163953Srrs case SCTP_INITMSG: 3501163953Srrs { 3502163953Srrs struct sctp_initmsg *sinit; 3503163953Srrs 3504166675Srrs SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize); 3505163953Srrs SCTP_INP_WLOCK(inp); 3506163953Srrs if (sinit->sinit_num_ostreams) 3507163953Srrs inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams; 3508163953Srrs 3509163953Srrs if (sinit->sinit_max_instreams) 3510163953Srrs inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams; 3511163953Srrs 3512163953Srrs if (sinit->sinit_max_attempts) 3513163953Srrs inp->sctp_ep.max_init_times = sinit->sinit_max_attempts; 3514163953Srrs 3515167598Srrs if (sinit->sinit_max_init_timeo) 3516163953Srrs inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo; 3517163953Srrs SCTP_INP_WUNLOCK(inp); 3518163953Srrs } 3519163953Srrs break; 3520163953Srrs case SCTP_PRIMARY_ADDR: 3521163953Srrs { 3522163953Srrs struct sctp_setprim *spa; 3523163953Srrs struct sctp_nets *net, *lnet; 3524163953Srrs 3525166675Srrs SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize); 3526166675Srrs SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id); 3527163953Srrs 3528166675Srrs net = NULL; 3529166675Srrs if (stcb) { 3530166675Srrs net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr); 3531166675Srrs } else { 3532166675Srrs /* 3533166675Srrs * We increment here since 3534166675Srrs * sctp_findassociation_ep_addr() wil do a 3535166675Srrs * decrement if it finds the stcb as long as 3536166675Srrs * the locked tcb (last argument) is NOT a 3537166675Srrs * TCB.. aka NULL. 3538166675Srrs */ 3539163953Srrs SCTP_INP_INCR_REF(inp); 3540163953Srrs stcb = sctp_findassociation_ep_addr(&inp, 3541163953Srrs (struct sockaddr *)&spa->ssp_addr, 3542163953Srrs &net, NULL, NULL); 3543163953Srrs if (stcb == NULL) { 3544163953Srrs SCTP_INP_DECR_REF(inp); 3545163953Srrs } 3546163953Srrs } 3547166675Srrs 3548166675Srrs if ((stcb) && (net)) { 3549166675Srrs if ((net != stcb->asoc.primary_destination) && 3550166675Srrs (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) { 3551166675Srrs /* Ok we need to set it */ 3552166675Srrs lnet = stcb->asoc.primary_destination; 3553166675Srrs if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) { 3554166675Srrs if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) { 3555166675Srrs net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH; 3556166675Srrs } 3557166675Srrs net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY; 3558163953Srrs } 3559163953Srrs } 3560166675Srrs } else { 3561166675Srrs error = EINVAL; 3562163953Srrs } 3563166675Srrs if (stcb) { 3564166675Srrs SCTP_TCB_UNLOCK(stcb); 3565166675Srrs } 3566163953Srrs } 3567163953Srrs break; 3568167598Srrs case SCTP_SET_DYNAMIC_PRIMARY: 3569167598Srrs { 3570167598Srrs union sctp_sockstore *ss; 3571163953Srrs 3572170587Srwatson error = priv_check(curthread, 3573170587Srwatson PRIV_NETINET_RESERVEDPORT); 3574167598Srrs if (error) 3575167598Srrs break; 3576167598Srrs 3577167598Srrs SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize); 3578167598Srrs /* SUPER USER CHECK? */ 3579167598Srrs error = sctp_dynamic_set_primary(&ss->sa, vrf_id); 3580167598Srrs } 3581167598Srrs break; 3582163953Srrs case SCTP_SET_PEER_PRIMARY_ADDR: 3583163953Srrs { 3584163953Srrs struct sctp_setpeerprim *sspp; 3585163953Srrs 3586166675Srrs SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize); 3587166675Srrs SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id); 3588169208Srrs if (stcb != NULL) { 3589170056Srrs struct sctp_ifa *ifa; 3590170056Srrs 3591170056Srrs ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr, 3592170056Srrs stcb->asoc.vrf_id, 0); 3593170056Srrs if (ifa == NULL) { 3594166675Srrs error = EINVAL; 3595170056Srrs goto out_of_it; 3596166675Srrs } 3597170056Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { 3598170056Srrs /* 3599170056Srrs * Must validate the ifa found is in 3600170056Srrs * our ep 3601170056Srrs */ 3602170056Srrs struct sctp_laddr *laddr; 3603170056Srrs int found = 0; 3604170056Srrs 3605170056Srrs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 3606170056Srrs if (laddr->ifa == NULL) { 3607170056Srrs SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n", 3608170056Srrs __FUNCTION__); 3609170056Srrs continue; 3610170056Srrs } 3611170056Srrs if (laddr->ifa == ifa) { 3612170056Srrs found = 1; 3613170056Srrs break; 3614170056Srrs } 3615170056Srrs } 3616170056Srrs if (!found) { 3617170056Srrs error = EINVAL; 3618170056Srrs goto out_of_it; 3619170056Srrs } 3620170056Srrs } 3621170056Srrs if (sctp_set_primary_ip_address_sa(stcb, 3622170056Srrs (struct sockaddr *)&sspp->sspp_addr) != 0) { 3623170056Srrs error = EINVAL; 3624170056Srrs } 3625170056Srrs out_of_it: 3626169208Srrs SCTP_TCB_UNLOCK(stcb); 3627166675Srrs } else { 3628163953Srrs error = EINVAL; 3629163953Srrs } 3630169208Srrs 3631163953Srrs } 3632163953Srrs break; 3633163953Srrs case SCTP_BINDX_ADD_ADDR: 3634163953Srrs { 3635163953Srrs struct sctp_getaddresses *addrs; 3636171531Srrs size_t sz; 3637171477Srrs struct thread *td; 3638171477Srrs int prison = 0; 3639163953Srrs 3640171477Srrs td = (struct thread *)p; 3641171477Srrs if (jailed(td->td_ucred)) { 3642171477Srrs prison = 1; 3643171477Srrs } 3644170606Srrs SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, 3645170606Srrs optsize); 3646171477Srrs if (addrs->addr->sa_family == AF_INET) { 3647171477Srrs sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in); 3648171477Srrs if (optsize < sz) { 3649171477Srrs error = EINVAL; 3650171477Srrs break; 3651171477Srrs } 3652171477Srrs if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) { 3653171477Srrs error = EADDRNOTAVAIL; 3654171477Srrs } 3655171477Srrs } else if (addrs->addr->sa_family == AF_INET6) { 3656171477Srrs sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6); 3657171477Srrs if (optsize < sz) { 3658171477Srrs error = EINVAL; 3659171477Srrs break; 3660171477Srrs } 3661171477Srrs /* JAIL XXXX Add else here for V6 */ 3662171477Srrs } 3663170606Srrs sctp_bindx_add_address(so, inp, addrs->addr, 3664170606Srrs addrs->sget_assoc_id, vrf_id, 3665170606Srrs &error, p); 3666163953Srrs } 3667163953Srrs break; 3668163953Srrs case SCTP_BINDX_REM_ADDR: 3669163953Srrs { 3670163953Srrs struct sctp_getaddresses *addrs; 3671171531Srrs size_t sz; 3672171477Srrs struct thread *td; 3673171477Srrs int prison = 0; 3674163953Srrs 3675171477Srrs td = (struct thread *)p; 3676171477Srrs if (jailed(td->td_ucred)) { 3677171477Srrs prison = 1; 3678171477Srrs } 3679166675Srrs SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize); 3680171477Srrs if (addrs->addr->sa_family == AF_INET) { 3681171477Srrs sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in); 3682171477Srrs if (optsize < sz) { 3683171477Srrs error = EINVAL; 3684171477Srrs break; 3685171477Srrs } 3686171477Srrs if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) { 3687171477Srrs error = EADDRNOTAVAIL; 3688171477Srrs } 3689171477Srrs } else if (addrs->addr->sa_family == AF_INET6) { 3690171477Srrs sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6); 3691171477Srrs if (optsize < sz) { 3692171477Srrs error = EINVAL; 3693171477Srrs break; 3694171477Srrs } 3695171477Srrs /* JAIL XXXX Add else here for V6 */ 3696171477Srrs } 3697170606Srrs sctp_bindx_delete_address(so, inp, addrs->addr, 3698170606Srrs addrs->sget_assoc_id, vrf_id, 3699170606Srrs &error); 3700163953Srrs } 3701163953Srrs break; 3702163953Srrs default: 3703163953Srrs error = ENOPROTOOPT; 3704163953Srrs break; 3705163953Srrs } /* end switch (opt) */ 3706163953Srrs return (error); 3707163953Srrs} 3708163953Srrs 3709163953Srrs 3710163953Srrsint 3711163953Srrssctp_ctloutput(struct socket *so, struct sockopt *sopt) 3712163953Srrs{ 3713166675Srrs void *optval = NULL; 3714166675Srrs size_t optsize = 0; 3715163953Srrs struct sctp_inpcb *inp; 3716166675Srrs void *p; 3717166675Srrs int error = 0; 3718163953Srrs 3719163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 3720163953Srrs if (inp == 0) { 3721163953Srrs /* I made the same as TCP since we are not setup? */ 3722163953Srrs return (ECONNRESET); 3723163953Srrs } 3724163953Srrs if (sopt->sopt_level != IPPROTO_SCTP) { 3725163953Srrs /* wrong proto level... send back up to IP */ 3726163953Srrs#ifdef INET6 3727163953Srrs if (INP_CHECK_SOCKAF(so, AF_INET6)) 3728163953Srrs error = ip6_ctloutput(so, sopt); 3729163953Srrs else 3730163953Srrs#endif /* INET6 */ 3731163953Srrs error = ip_ctloutput(so, sopt); 3732163953Srrs return (error); 3733163953Srrs } 3734166675Srrs optsize = sopt->sopt_valsize; 3735166675Srrs if (optsize) { 3736170091Srrs SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT); 3737166675Srrs if (optval == NULL) { 3738163953Srrs return (ENOBUFS); 3739163953Srrs } 3740166675Srrs error = sooptcopyin(sopt, optval, optsize, optsize); 3741163953Srrs if (error) { 3742170091Srrs SCTP_FREE(optval, SCTP_M_SOCKOPT); 3743163953Srrs goto out; 3744163953Srrs } 3745163953Srrs } 3746166675Srrs p = (void *)sopt->sopt_td; 3747163953Srrs if (sopt->sopt_dir == SOPT_SET) { 3748166675Srrs error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p); 3749163953Srrs } else if (sopt->sopt_dir == SOPT_GET) { 3750166675Srrs error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p); 3751163953Srrs } else { 3752163953Srrs error = EINVAL; 3753163953Srrs } 3754166675Srrs if ((error == 0) && (optval != NULL)) { 3755166675Srrs error = sooptcopyout(sopt, optval, optsize); 3756170091Srrs SCTP_FREE(optval, SCTP_M_SOCKOPT); 3757166675Srrs } else if (optval != NULL) { 3758170091Srrs SCTP_FREE(optval, SCTP_M_SOCKOPT); 3759163953Srrs } 3760163953Srrsout: 3761163953Srrs return (error); 3762163953Srrs} 3763163953Srrs 3764163953Srrs 3765163953Srrsstatic int 3766163953Srrssctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p) 3767163953Srrs{ 3768163953Srrs int error = 0; 3769163953Srrs int create_lock_on = 0; 3770167598Srrs uint32_t vrf_id; 3771163953Srrs struct sctp_inpcb *inp; 3772163953Srrs struct sctp_tcb *stcb = NULL; 3773163953Srrs 3774163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 3775163953Srrs if (inp == 0) { 3776163953Srrs /* I made the same as TCP since we are not setup? */ 3777163953Srrs return (ECONNRESET); 3778163953Srrs } 3779170056Srrs if (addr == NULL) 3780170056Srrs return EINVAL; 3781170056Srrs 3782170056Srrs if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) { 3783170056Srrs return (EINVAL); 3784170056Srrs } 3785170056Srrs if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) { 3786170056Srrs return (EINVAL); 3787170056Srrs } 3788163953Srrs SCTP_ASOC_CREATE_LOCK(inp); 3789163953Srrs create_lock_on = 1; 3790163953Srrs 3791163953Srrs SCTP_INP_INCR_REF(inp); 3792163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 3793163953Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { 3794163953Srrs /* Should I really unlock ? */ 3795163953Srrs error = EFAULT; 3796163953Srrs goto out_now; 3797163953Srrs } 3798163953Srrs#ifdef INET6 3799163953Srrs if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) && 3800163953Srrs (addr->sa_family == AF_INET6)) { 3801163953Srrs error = EINVAL; 3802163953Srrs goto out_now; 3803163953Srrs } 3804163953Srrs#endif /* INET6 */ 3805163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 3806163953Srrs SCTP_PCB_FLAGS_UNBOUND) { 3807163953Srrs /* Bind a ephemeral port */ 3808171477Srrs error = sctp_inpcb_bind(so, NULL, p); 3809163953Srrs if (error) { 3810163953Srrs goto out_now; 3811163953Srrs } 3812163953Srrs } 3813163953Srrs /* Now do we connect? */ 3814163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { 3815163953Srrs error = EINVAL; 3816163953Srrs goto out_now; 3817163953Srrs } 3818163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 3819163953Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 3820163953Srrs /* We are already connected AND the TCP model */ 3821163953Srrs error = EADDRINUSE; 3822163953Srrs goto out_now; 3823163953Srrs } 3824163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 3825163953Srrs SCTP_INP_RLOCK(inp); 3826163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 3827163953Srrs SCTP_INP_RUNLOCK(inp); 3828163953Srrs } else { 3829163953Srrs /* 3830166675Srrs * We increment here since sctp_findassociation_ep_addr() 3831166675Srrs * wil do a decrement if it finds the stcb as long as the 3832166675Srrs * locked tcb (last argument) is NOT a TCB.. aka NULL. 3833163953Srrs */ 3834163953Srrs SCTP_INP_INCR_REF(inp); 3835163953Srrs stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); 3836163953Srrs if (stcb == NULL) { 3837163953Srrs SCTP_INP_DECR_REF(inp); 3838168299Srrs } else { 3839168299Srrs SCTP_TCB_LOCK(stcb); 3840163953Srrs } 3841163953Srrs } 3842163953Srrs if (stcb != NULL) { 3843163953Srrs /* Already have or am bring up an association */ 3844163953Srrs error = EALREADY; 3845163953Srrs goto out_now; 3846163953Srrs } 3847168299Srrs vrf_id = inp->def_vrf_id; 3848163953Srrs /* We are GOOD to go */ 3849171531Srrs stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p); 3850163953Srrs if (stcb == NULL) { 3851163953Srrs /* Gak! no memory */ 3852167598Srrs goto out_now; 3853163953Srrs } 3854163953Srrs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 3855163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 3856163953Srrs /* Set the connected flag so we can queue data */ 3857163953Srrs soisconnecting(so); 3858163953Srrs } 3859163953Srrs stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 3860169378Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 3861163953Srrs 3862163953Srrs /* initialize authentication parameters for the assoc */ 3863163953Srrs sctp_initialize_auth_params(inp, stcb); 3864163953Srrs 3865163953Srrs sctp_send_initiate(inp, stcb); 3866168299Srrs SCTP_TCB_UNLOCK(stcb); 3867163953Srrsout_now: 3868169420Srrs if (create_lock_on) { 3869163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 3870169420Srrs } 3871163953Srrs SCTP_INP_DECR_REF(inp); 3872163953Srrs return error; 3873163953Srrs} 3874163953Srrs 3875163953Srrsint 3876163953Srrssctp_listen(struct socket *so, int backlog, struct thread *p) 3877163953Srrs{ 3878163953Srrs /* 3879163953Srrs * Note this module depends on the protocol processing being called 3880163953Srrs * AFTER any socket level flags and backlog are applied to the 3881163953Srrs * socket. The traditional way that the socket flags are applied is 3882163953Srrs * AFTER protocol processing. We have made a change to the 3883163953Srrs * sys/kern/uipc_socket.c module to reverse this but this MUST be in 3884163953Srrs * place if the socket API for SCTP is to work properly. 3885163953Srrs */ 3886163953Srrs 3887163953Srrs int error = 0; 3888163953Srrs struct sctp_inpcb *inp; 3889163953Srrs 3890163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 3891163953Srrs if (inp == 0) { 3892163953Srrs /* I made the same as TCP since we are not setup? */ 3893163953Srrs return (ECONNRESET); 3894163953Srrs } 3895163953Srrs SCTP_INP_RLOCK(inp); 3896163953Srrs#ifdef SCTP_LOCK_LOGGING 3897170744Srrs if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) { 3898170744Srrs sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK); 3899170744Srrs } 3900163953Srrs#endif 3901163953Srrs SOCK_LOCK(so); 3902163953Srrs error = solisten_proto_check(so); 3903163953Srrs if (error) { 3904163953Srrs SOCK_UNLOCK(so); 3905169208Srrs SCTP_INP_RUNLOCK(inp); 3906163953Srrs return (error); 3907163953Srrs } 3908163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 3909163953Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 3910163953Srrs /* We are already connected AND the TCP model */ 3911163953Srrs SCTP_INP_RUNLOCK(inp); 3912163953Srrs SOCK_UNLOCK(so); 3913163953Srrs return (EADDRINUSE); 3914163953Srrs } 3915163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { 3916163953Srrs /* We must do a bind. */ 3917166675Srrs SOCK_UNLOCK(so); 3918163953Srrs SCTP_INP_RUNLOCK(inp); 3919171477Srrs if ((error = sctp_inpcb_bind(so, NULL, p))) { 3920163953Srrs /* bind error, probably perm */ 3921163953Srrs return (error); 3922163953Srrs } 3923166675Srrs SOCK_LOCK(so); 3924163953Srrs } else { 3925163953Srrs SCTP_INP_RUNLOCK(inp); 3926163953Srrs } 3927163953Srrs /* It appears for 7.0 and on, we must always call this. */ 3928163953Srrs solisten_proto(so, backlog); 3929163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 3930163953Srrs /* remove the ACCEPTCONN flag for one-to-many sockets */ 3931163953Srrs so->so_options &= ~SO_ACCEPTCONN; 3932163953Srrs } 3933163953Srrs if (backlog == 0) { 3934163953Srrs /* turning off listen */ 3935163953Srrs so->so_options &= ~SO_ACCEPTCONN; 3936163953Srrs } 3937163953Srrs SOCK_UNLOCK(so); 3938163953Srrs return (error); 3939163953Srrs} 3940163953Srrs 3941163953Srrsstatic int sctp_defered_wakeup_cnt = 0; 3942163953Srrs 3943163953Srrsint 3944163953Srrssctp_accept(struct socket *so, struct sockaddr **addr) 3945163953Srrs{ 3946163953Srrs struct sctp_tcb *stcb; 3947163953Srrs struct sctp_inpcb *inp; 3948163953Srrs union sctp_sockstore store; 3949163953Srrs 3950163953Srrs int error; 3951163953Srrs 3952163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 3953163953Srrs 3954163953Srrs if (inp == 0) { 3955163953Srrs return (ECONNRESET); 3956163953Srrs } 3957163953Srrs SCTP_INP_RLOCK(inp); 3958163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) { 3959168299Srrs SCTP_INP_RUNLOCK(inp); 3960163953Srrs return (ENOTSUP); 3961163953Srrs } 3962163953Srrs if (so->so_state & SS_ISDISCONNECTED) { 3963163953Srrs SCTP_INP_RUNLOCK(inp); 3964163953Srrs return (ECONNABORTED); 3965163953Srrs } 3966163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 3967163953Srrs if (stcb == NULL) { 3968163953Srrs SCTP_INP_RUNLOCK(inp); 3969163953Srrs return (ECONNRESET); 3970163953Srrs } 3971163953Srrs SCTP_TCB_LOCK(stcb); 3972163953Srrs SCTP_INP_RUNLOCK(inp); 3973163953Srrs store = stcb->asoc.primary_destination->ro._l_addr; 3974163953Srrs SCTP_TCB_UNLOCK(stcb); 3975163953Srrs if (store.sa.sa_family == AF_INET) { 3976163953Srrs struct sockaddr_in *sin; 3977163953Srrs 3978163953Srrs SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin); 3979163953Srrs sin->sin_family = AF_INET; 3980163953Srrs sin->sin_len = sizeof(*sin); 3981163953Srrs sin->sin_port = ((struct sockaddr_in *)&store)->sin_port; 3982163953Srrs sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr; 3983163953Srrs *addr = (struct sockaddr *)sin; 3984163953Srrs } else { 3985163953Srrs struct sockaddr_in6 *sin6; 3986163953Srrs 3987163953Srrs SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 3988163953Srrs sin6->sin6_family = AF_INET6; 3989163953Srrs sin6->sin6_len = sizeof(*sin6); 3990163953Srrs sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port; 3991163953Srrs 3992163953Srrs sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr; 3993164085Srrs if ((error = sa6_recoverscope(sin6)) != 0) { 3994164085Srrs SCTP_FREE_SONAME(sin6); 3995163953Srrs return (error); 3996164085Srrs } 3997163953Srrs *addr = (struct sockaddr *)sin6; 3998163953Srrs } 3999163953Srrs /* Wake any delayed sleep action */ 4000163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) { 4001166086Srrs SCTP_INP_WLOCK(inp); 4002163953Srrs inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE; 4003163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) { 4004163953Srrs inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT; 4005166086Srrs SCTP_INP_WUNLOCK(inp); 4006163953Srrs SOCKBUF_LOCK(&inp->sctp_socket->so_snd); 4007163953Srrs if (sowriteable(inp->sctp_socket)) { 4008163953Srrs sowwakeup_locked(inp->sctp_socket); 4009163953Srrs } else { 4010163953Srrs SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd); 4011163953Srrs } 4012166086Srrs SCTP_INP_WLOCK(inp); 4013163953Srrs } 4014163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) { 4015163953Srrs inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT; 4016166086Srrs SCTP_INP_WUNLOCK(inp); 4017163953Srrs SOCKBUF_LOCK(&inp->sctp_socket->so_rcv); 4018163953Srrs if (soreadable(inp->sctp_socket)) { 4019163953Srrs sctp_defered_wakeup_cnt++; 4020163953Srrs sorwakeup_locked(inp->sctp_socket); 4021163953Srrs } else { 4022163953Srrs SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv); 4023163953Srrs } 4024166086Srrs SCTP_INP_WLOCK(inp); 4025163953Srrs } 4026166086Srrs SCTP_INP_WUNLOCK(inp); 4027163953Srrs } 4028163953Srrs return (0); 4029163953Srrs} 4030163953Srrs 4031163953Srrsint 4032163953Srrssctp_ingetaddr(struct socket *so, struct sockaddr **addr) 4033163953Srrs{ 4034163953Srrs struct sockaddr_in *sin; 4035167598Srrs uint32_t vrf_id; 4036163953Srrs struct sctp_inpcb *inp; 4037167695Srrs struct sctp_ifa *sctp_ifa; 4038163953Srrs 4039163953Srrs /* 4040163953Srrs * Do the malloc first in case it blocks. 4041163953Srrs */ 4042163953Srrs SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin); 4043163953Srrs sin->sin_family = AF_INET; 4044163953Srrs sin->sin_len = sizeof(*sin); 4045163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 4046163953Srrs if (!inp) { 4047163953Srrs SCTP_FREE_SONAME(sin); 4048163953Srrs return ECONNRESET; 4049163953Srrs } 4050163953Srrs SCTP_INP_RLOCK(inp); 4051163953Srrs sin->sin_port = inp->sctp_lport; 4052163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 4053163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 4054163953Srrs struct sctp_tcb *stcb; 4055163953Srrs struct sockaddr_in *sin_a; 4056163953Srrs struct sctp_nets *net; 4057163953Srrs int fnd; 4058163953Srrs 4059163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 4060163953Srrs if (stcb == NULL) { 4061163953Srrs goto notConn; 4062163953Srrs } 4063163953Srrs fnd = 0; 4064163953Srrs sin_a = NULL; 4065163953Srrs SCTP_TCB_LOCK(stcb); 4066163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4067163953Srrs sin_a = (struct sockaddr_in *)&net->ro._l_addr; 4068164085Srrs if (sin_a == NULL) 4069164085Srrs /* this will make coverity happy */ 4070164085Srrs continue; 4071164085Srrs 4072163953Srrs if (sin_a->sin_family == AF_INET) { 4073163953Srrs fnd = 1; 4074163953Srrs break; 4075163953Srrs } 4076163953Srrs } 4077163953Srrs if ((!fnd) || (sin_a == NULL)) { 4078163953Srrs /* punt */ 4079163953Srrs SCTP_TCB_UNLOCK(stcb); 4080163953Srrs goto notConn; 4081163953Srrs } 4082168299Srrs vrf_id = inp->def_vrf_id; 4083167598Srrs sctp_ifa = sctp_source_address_selection(inp, 4084167598Srrs stcb, 4085168299Srrs (sctp_route_t *) & net->ro, 4086167598Srrs net, 0, vrf_id); 4087167598Srrs if (sctp_ifa) { 4088167598Srrs sin->sin_addr = sctp_ifa->address.sin.sin_addr; 4089167598Srrs sctp_free_ifa(sctp_ifa); 4090167598Srrs } 4091163953Srrs SCTP_TCB_UNLOCK(stcb); 4092163953Srrs } else { 4093163953Srrs /* For the bound all case you get back 0 */ 4094163953Srrs notConn: 4095163953Srrs sin->sin_addr.s_addr = 0; 4096163953Srrs } 4097163953Srrs 4098163953Srrs } else { 4099163953Srrs /* Take the first IPv4 address in the list */ 4100163953Srrs struct sctp_laddr *laddr; 4101163953Srrs int fnd = 0; 4102163953Srrs 4103163953Srrs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 4104167598Srrs if (laddr->ifa->address.sa.sa_family == AF_INET) { 4105163953Srrs struct sockaddr_in *sin_a; 4106163953Srrs 4107167598Srrs sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa; 4108163953Srrs sin->sin_addr = sin_a->sin_addr; 4109163953Srrs fnd = 1; 4110163953Srrs break; 4111163953Srrs } 4112163953Srrs } 4113163953Srrs if (!fnd) { 4114163953Srrs SCTP_FREE_SONAME(sin); 4115163953Srrs SCTP_INP_RUNLOCK(inp); 4116163953Srrs return ENOENT; 4117163953Srrs } 4118163953Srrs } 4119163953Srrs SCTP_INP_RUNLOCK(inp); 4120163953Srrs (*addr) = (struct sockaddr *)sin; 4121163953Srrs return (0); 4122163953Srrs} 4123163953Srrs 4124163953Srrsint 4125163953Srrssctp_peeraddr(struct socket *so, struct sockaddr **addr) 4126163953Srrs{ 4127163953Srrs struct sockaddr_in *sin = (struct sockaddr_in *)*addr; 4128166086Srrs int fnd; 4129163953Srrs struct sockaddr_in *sin_a; 4130163953Srrs struct sctp_inpcb *inp; 4131163953Srrs struct sctp_tcb *stcb; 4132163953Srrs struct sctp_nets *net; 4133163953Srrs 4134163953Srrs /* Do the malloc first in case it blocks. */ 4135163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 4136163953Srrs if ((inp == NULL) || 4137163953Srrs ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 4138163953Srrs /* UDP type and listeners will drop out here */ 4139163953Srrs return (ENOTCONN); 4140163953Srrs } 4141163953Srrs SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin); 4142163953Srrs sin->sin_family = AF_INET; 4143163953Srrs sin->sin_len = sizeof(*sin); 4144163953Srrs 4145163953Srrs /* We must recapture incase we blocked */ 4146163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 4147163953Srrs if (!inp) { 4148163953Srrs SCTP_FREE_SONAME(sin); 4149163953Srrs return ECONNRESET; 4150163953Srrs } 4151163953Srrs SCTP_INP_RLOCK(inp); 4152163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 4153169420Srrs if (stcb) { 4154163953Srrs SCTP_TCB_LOCK(stcb); 4155169420Srrs } 4156163953Srrs SCTP_INP_RUNLOCK(inp); 4157163953Srrs if (stcb == NULL) { 4158163953Srrs SCTP_FREE_SONAME(sin); 4159163953Srrs return ECONNRESET; 4160163953Srrs } 4161163953Srrs fnd = 0; 4162163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 4163163953Srrs sin_a = (struct sockaddr_in *)&net->ro._l_addr; 4164163953Srrs if (sin_a->sin_family == AF_INET) { 4165163953Srrs fnd = 1; 4166163953Srrs sin->sin_port = stcb->rport; 4167163953Srrs sin->sin_addr = sin_a->sin_addr; 4168163953Srrs break; 4169163953Srrs } 4170163953Srrs } 4171163953Srrs SCTP_TCB_UNLOCK(stcb); 4172163953Srrs if (!fnd) { 4173163953Srrs /* No IPv4 address */ 4174163953Srrs SCTP_FREE_SONAME(sin); 4175163953Srrs return ENOENT; 4176163953Srrs } 4177163953Srrs (*addr) = (struct sockaddr *)sin; 4178163953Srrs return (0); 4179163953Srrs} 4180163953Srrs 4181163953Srrsstruct pr_usrreqs sctp_usrreqs = { 4182163953Srrs .pru_abort = sctp_abort, 4183163953Srrs .pru_accept = sctp_accept, 4184163953Srrs .pru_attach = sctp_attach, 4185163953Srrs .pru_bind = sctp_bind, 4186163953Srrs .pru_connect = sctp_connect, 4187163953Srrs .pru_control = in_control, 4188163953Srrs .pru_close = sctp_close, 4189163953Srrs .pru_detach = sctp_close, 4190163953Srrs .pru_sopoll = sopoll_generic, 4191163953Srrs .pru_disconnect = sctp_disconnect, 4192163953Srrs .pru_listen = sctp_listen, 4193163953Srrs .pru_peeraddr = sctp_peeraddr, 4194163953Srrs .pru_send = sctp_sendm, 4195163953Srrs .pru_shutdown = sctp_shutdown, 4196163953Srrs .pru_sockaddr = sctp_ingetaddr, 4197163953Srrs .pru_sosend = sctp_sosend, 4198163953Srrs .pru_soreceive = sctp_soreceive 4199163953Srrs}; 4200