1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* SCTP kernel implementation 3 * (C) Copyright IBM Corp. 2001, 2004 4 * Copyright (c) 1999-2000 Cisco, Inc. 5 * Copyright (c) 1999-2001 Motorola, Inc. 6 * Copyright (c) 2001-2003 Intel Corp. 7 * Copyright (c) 2001-2002 Nokia, Inc. 8 * Copyright (c) 2001 La Monte H.P. Yarroll 9 * 10 * This file is part of the SCTP kernel implementation 11 * 12 * These functions interface with the sockets layer to implement the 13 * SCTP Extensions for the Sockets API. 14 * 15 * Note that the descriptions from the specification are USER level 16 * functions--this file is the functions which populate the struct proto 17 * for SCTP which is the BOTTOM of the sockets interface. 18 * 19 * Please send any bug reports or fixes you make to the 20 * email address(es): 21 * lksctp developers <linux-sctp@vger.kernel.org> 22 * 23 * Written or modified by: 24 * La Monte H.P. Yarroll <piggy@acm.org> 25 * Narasimha Budihal <narsi@refcode.org> 26 * Karl Knutson <karl@athena.chicago.il.us> 27 * Jon Grimm <jgrimm@us.ibm.com> 28 * Xingang Guo <xingang.guo@intel.com> 29 * Daisy Chang <daisyc@us.ibm.com> 30 * Sridhar Samudrala <samudrala@us.ibm.com> 31 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com> 32 * Ardelle Fan <ardelle.fan@intel.com> 33 * Ryan Layer <rmlayer@us.ibm.com> 34 * Anup Pemmaiah <pemmaiah@cc.usu.edu> 35 * Kevin Gao <kevin.gao@intel.com> 36 */ 37 38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 39 40 #include <linux/types.h> 41 #include <linux/kernel.h> 42 #include <linux/wait.h> 43 #include <linux/time.h> 44 #include <linux/sched/signal.h> 45 #include <linux/ip.h> 46 #include <linux/capability.h> 47 #include <linux/fcntl.h> 48 #include <linux/poll.h> 49 #include <linux/init.h> 50 #include <linux/slab.h> 51 #include <linux/file.h> 52 #include <linux/compat.h> 53 #include <linux/rhashtable.h> 54 55 #include <net/ip.h> 56 #include <net/icmp.h> 57 #include <net/route.h> 58 #include <net/ipv6.h> 59 #include <net/inet_common.h> 60 #include <net/busy_poll.h> 61 #include <trace/events/sock.h> 62 63 #include <linux/socket.h> /* for sa_family_t */ 64 #include <linux/export.h> 65 #include <net/sock.h> 66 #include <net/sctp/sctp.h> 67 #include <net/sctp/sm.h> 68 #include <net/sctp/stream_sched.h> 69 #include <net/rps.h> 70 71 /* Forward declarations for internal helper functions. */ 72 static bool sctp_writeable(const struct sock *sk); 73 static void sctp_wfree(struct sk_buff *skb); 74 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, 75 struct sctp_transport *transport, 76 long *timeo_p, size_t msg_len); 77 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p); 78 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p); 79 static int sctp_wait_for_accept(struct sock *sk, long timeo); 80 static void sctp_wait_for_close(struct sock *sk, long timeo); 81 static void sctp_destruct_sock(struct sock *sk); 82 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, 83 union sctp_addr *addr, int len); 84 static int sctp_bindx_add(struct sock *, struct sockaddr *, int); 85 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int); 86 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int); 87 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int); 88 static int sctp_send_asconf(struct sctp_association *asoc, 89 struct sctp_chunk *chunk); 90 static int sctp_do_bind(struct sock *, union sctp_addr *, int); 91 static int sctp_autobind(struct sock *sk); 92 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, 93 struct sctp_association *assoc, 94 enum sctp_socket_type type); 95 96 static unsigned long sctp_memory_pressure; 97 static atomic_long_t sctp_memory_allocated; 98 static DEFINE_PER_CPU(int, sctp_memory_per_cpu_fw_alloc); 99 struct percpu_counter sctp_sockets_allocated; 100 101 static void sctp_enter_memory_pressure(struct sock *sk) 102 { 103 WRITE_ONCE(sctp_memory_pressure, 1); 104 } 105 106 107 /* Get the sndbuf space available at the time on the association. */ 108 static inline int sctp_wspace(struct sctp_association *asoc) 109 { 110 struct sock *sk = asoc->base.sk; 111 112 return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used 113 : sk_stream_wspace(sk); 114 } 115 116 /* Increment the used sndbuf space count of the corresponding association by 117 * the size of the outgoing data chunk. 118 * Also, set the skb destructor for sndbuf accounting later. 119 * 120 * Since it is always 1-1 between chunk and skb, and also a new skb is always 121 * allocated for chunk bundling in sctp_packet_transmit(), we can use the 122 * destructor in the data chunk skb for the purpose of the sndbuf space 123 * tracking. 124 */ 125 static inline void sctp_set_owner_w(struct sctp_chunk *chunk) 126 { 127 struct sctp_association *asoc = chunk->asoc; 128 struct sock *sk = asoc->base.sk; 129 130 /* The sndbuf space is tracked per association. */ 131 sctp_association_hold(asoc); 132 133 if (chunk->shkey) 134 sctp_auth_shkey_hold(chunk->shkey); 135 136 skb_set_owner_w(chunk->skb, sk); 137 138 chunk->skb->destructor = sctp_wfree; 139 /* Save the chunk pointer in skb for sctp_wfree to use later. */ 140 skb_shinfo(chunk->skb)->destructor_arg = chunk; 141 142 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); 143 asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk); 144 sk_wmem_queued_add(sk, chunk->skb->truesize + sizeof(struct sctp_chunk)); 145 sk_mem_charge(sk, chunk->skb->truesize); 146 } 147 148 static void sctp_clear_owner_w(struct sctp_chunk *chunk) 149 { 150 skb_orphan(chunk->skb); 151 } 152 153 #define traverse_and_process() \ 154 do { \ 155 msg = chunk->msg; \ 156 if (msg == prev_msg) \ 157 continue; \ 158 list_for_each_entry(c, &msg->chunks, frag_list) { \ 159 if ((clear && asoc->base.sk == c->skb->sk) || \ 160 (!clear && asoc->base.sk != c->skb->sk)) \ 161 cb(c); \ 162 } \ 163 prev_msg = msg; \ 164 } while (0) 165 166 static void sctp_for_each_tx_datachunk(struct sctp_association *asoc, 167 bool clear, 168 void (*cb)(struct sctp_chunk *)) 169 170 { 171 struct sctp_datamsg *msg, *prev_msg = NULL; 172 struct sctp_outq *q = &asoc->outqueue; 173 struct sctp_chunk *chunk, *c; 174 struct sctp_transport *t; 175 176 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) 177 list_for_each_entry(chunk, &t->transmitted, transmitted_list) 178 traverse_and_process(); 179 180 list_for_each_entry(chunk, &q->retransmit, transmitted_list) 181 traverse_and_process(); 182 183 list_for_each_entry(chunk, &q->sacked, transmitted_list) 184 traverse_and_process(); 185 186 list_for_each_entry(chunk, &q->abandoned, transmitted_list) 187 traverse_and_process(); 188 189 list_for_each_entry(chunk, &q->out_chunk_list, list) 190 traverse_and_process(); 191 } 192 193 static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk, 194 void (*cb)(struct sk_buff *, struct sock *)) 195 196 { 197 struct sk_buff *skb, *tmp; 198 199 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp) 200 cb(skb, sk); 201 202 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp) 203 cb(skb, sk); 204 205 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp) 206 cb(skb, sk); 207 } 208 209 /* Verify that this is a valid address. */ 210 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr, 211 int len) 212 { 213 struct sctp_af *af; 214 215 /* Verify basic sockaddr. */ 216 af = sctp_sockaddr_af(sctp_sk(sk), addr, len); 217 if (!af) 218 return -EINVAL; 219 220 /* Is this a valid SCTP address? */ 221 if (!af->addr_valid(addr, sctp_sk(sk), NULL)) 222 return -EINVAL; 223 224 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr))) 225 return -EINVAL; 226 227 return 0; 228 } 229 230 /* Look up the association by its id. If this is not a UDP-style 231 * socket, the ID field is always ignored. 232 */ 233 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id) 234 { 235 struct sctp_association *asoc = NULL; 236 237 /* If this is not a UDP-style socket, assoc id should be ignored. */ 238 if (!sctp_style(sk, UDP)) { 239 /* Return NULL if the socket state is not ESTABLISHED. It 240 * could be a TCP-style listening socket or a socket which 241 * hasn't yet called connect() to establish an association. 242 */ 243 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING)) 244 return NULL; 245 246 /* Get the first and the only association from the list. */ 247 if (!list_empty(&sctp_sk(sk)->ep->asocs)) 248 asoc = list_entry(sctp_sk(sk)->ep->asocs.next, 249 struct sctp_association, asocs); 250 return asoc; 251 } 252 253 /* Otherwise this is a UDP-style socket. */ 254 if (id <= SCTP_ALL_ASSOC) 255 return NULL; 256 257 spin_lock_bh(&sctp_assocs_id_lock); 258 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); 259 if (asoc && (asoc->base.sk != sk || asoc->base.dead)) 260 asoc = NULL; 261 spin_unlock_bh(&sctp_assocs_id_lock); 262 263 return asoc; 264 } 265 266 /* Look up the transport from an address and an assoc id. If both address and 267 * id are specified, the associations matching the address and the id should be 268 * the same. 269 */ 270 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk, 271 struct sockaddr_storage *addr, 272 sctp_assoc_t id) 273 { 274 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL; 275 struct sctp_af *af = sctp_get_af_specific(addr->ss_family); 276 union sctp_addr *laddr = (union sctp_addr *)addr; 277 struct sctp_transport *transport; 278 279 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len)) 280 return NULL; 281 282 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep, 283 laddr, 284 &transport); 285 286 if (!addr_asoc) 287 return NULL; 288 289 id_asoc = sctp_id2assoc(sk, id); 290 if (id_asoc && (id_asoc != addr_asoc)) 291 return NULL; 292 293 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk), 294 (union sctp_addr *)addr); 295 296 return transport; 297 } 298 299 /* API 3.1.2 bind() - UDP Style Syntax 300 * The syntax of bind() is, 301 * 302 * ret = bind(int sd, struct sockaddr *addr, int addrlen); 303 * 304 * sd - the socket descriptor returned by socket(). 305 * addr - the address structure (struct sockaddr_in or struct 306 * sockaddr_in6 [RFC 2553]), 307 * addr_len - the size of the address structure. 308 */ 309 static int sctp_bind(struct sock *sk, struct sockaddr_unsized *addr, 310 int addr_len) 311 { 312 int retval = 0; 313 314 lock_sock(sk); 315 316 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk, 317 addr, addr_len); 318 319 /* Disallow binding twice. */ 320 if (!sctp_sk(sk)->ep->base.bind_addr.port) 321 retval = sctp_do_bind(sk, (union sctp_addr *)addr, 322 addr_len); 323 else 324 retval = -EINVAL; 325 326 release_sock(sk); 327 328 return retval; 329 } 330 331 static int sctp_get_port_local(struct sock *, union sctp_addr *); 332 333 /* Verify this is a valid sockaddr. */ 334 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, 335 union sctp_addr *addr, int len) 336 { 337 struct sctp_af *af; 338 339 /* Check minimum size. */ 340 if (len < sizeof (struct sockaddr)) 341 return NULL; 342 343 if (!opt->pf->af_supported(addr->sa.sa_family, opt)) 344 return NULL; 345 346 if (addr->sa.sa_family == AF_INET6) { 347 if (len < SIN6_LEN_RFC2133) 348 return NULL; 349 /* V4 mapped address are really of AF_INET family */ 350 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) && 351 !opt->pf->af_supported(AF_INET, opt)) 352 return NULL; 353 } 354 355 /* If we get this far, af is valid. */ 356 af = sctp_get_af_specific(addr->sa.sa_family); 357 358 if (len < af->sockaddr_len) 359 return NULL; 360 361 return af; 362 } 363 364 static void sctp_auto_asconf_init(struct sctp_sock *sp) 365 { 366 struct net *net = sock_net(&sp->inet.sk); 367 368 if (net->sctp.default_auto_asconf) { 369 spin_lock_bh(&net->sctp.addr_wq_lock); 370 list_add_tail(&sp->auto_asconf_list, &net->sctp.auto_asconf_splist); 371 spin_unlock_bh(&net->sctp.addr_wq_lock); 372 sp->do_auto_asconf = 1; 373 } 374 } 375 376 /* Bind a local address either to an endpoint or to an association. */ 377 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) 378 { 379 struct net *net = sock_net(sk); 380 struct sctp_sock *sp = sctp_sk(sk); 381 struct sctp_endpoint *ep = sp->ep; 382 struct sctp_bind_addr *bp = &ep->base.bind_addr; 383 struct sctp_af *af; 384 unsigned short snum; 385 int ret = 0; 386 387 /* Common sockaddr verification. */ 388 af = sctp_sockaddr_af(sp, addr, len); 389 if (!af) { 390 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n", 391 __func__, sk, addr, len); 392 return -EINVAL; 393 } 394 395 snum = ntohs(addr->v4.sin_port); 396 397 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n", 398 __func__, sk, &addr->sa, bp->port, snum, len); 399 400 /* PF specific bind() address verification. */ 401 if (!sp->pf->bind_verify(sp, addr)) 402 return -EADDRNOTAVAIL; 403 404 /* We must either be unbound, or bind to the same port. 405 * It's OK to allow 0 ports if we are already bound. 406 * We'll just inhert an already bound port in this case 407 */ 408 if (bp->port) { 409 if (!snum) 410 snum = bp->port; 411 else if (snum != bp->port) { 412 pr_debug("%s: new port %d doesn't match existing port " 413 "%d\n", __func__, snum, bp->port); 414 return -EINVAL; 415 } 416 } 417 418 if (snum && inet_port_requires_bind_service(net, snum) && 419 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) 420 return -EACCES; 421 422 /* See if the address matches any of the addresses we may have 423 * already bound before checking against other endpoints. 424 */ 425 if (sctp_bind_addr_match(bp, addr, sp)) 426 return -EINVAL; 427 428 /* Make sure we are allowed to bind here. 429 * The function sctp_get_port_local() does duplicate address 430 * detection. 431 */ 432 addr->v4.sin_port = htons(snum); 433 if (sctp_get_port_local(sk, addr)) 434 return -EADDRINUSE; 435 436 /* Refresh ephemeral port. */ 437 if (!bp->port) { 438 bp->port = inet_sk(sk)->inet_num; 439 sctp_auto_asconf_init(sp); 440 } 441 442 /* Add the address to the bind address list. 443 * Use GFP_ATOMIC since BHs will be disabled. 444 */ 445 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len, 446 SCTP_ADDR_SRC, GFP_ATOMIC); 447 448 if (ret) { 449 sctp_put_port(sk); 450 return ret; 451 } 452 /* Copy back into socket for getsockname() use. */ 453 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num); 454 sp->pf->to_sk_saddr(addr, sk); 455 456 return ret; 457 } 458 459 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks 460 * 461 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged 462 * at any one time. If a sender, after sending an ASCONF chunk, decides 463 * it needs to transfer another ASCONF Chunk, it MUST wait until the 464 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a 465 * subsequent ASCONF. Note this restriction binds each side, so at any 466 * time two ASCONF may be in-transit on any given association (one sent 467 * from each endpoint). 468 */ 469 static int sctp_send_asconf(struct sctp_association *asoc, 470 struct sctp_chunk *chunk) 471 { 472 int retval = 0; 473 474 /* If there is an outstanding ASCONF chunk, queue it for later 475 * transmission. 476 */ 477 if (asoc->addip_last_asconf) { 478 list_add_tail(&chunk->list, &asoc->addip_chunk_list); 479 goto out; 480 } 481 482 /* Hold the chunk until an ASCONF_ACK is received. */ 483 sctp_chunk_hold(chunk); 484 retval = sctp_primitive_ASCONF(asoc->base.net, asoc, chunk); 485 if (retval) 486 sctp_chunk_free(chunk); 487 else 488 asoc->addip_last_asconf = chunk; 489 490 out: 491 return retval; 492 } 493 494 /* Add a list of addresses as bind addresses to local endpoint or 495 * association. 496 * 497 * Basically run through each address specified in the addrs/addrcnt 498 * array/length pair, determine if it is IPv6 or IPv4 and call 499 * sctp_do_bind() on it. 500 * 501 * If any of them fails, then the operation will be reversed and the 502 * ones that were added will be removed. 503 * 504 * Only sctp_setsockopt_bindx() is supposed to call this function. 505 */ 506 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt) 507 { 508 int cnt; 509 int retval = 0; 510 void *addr_buf; 511 struct sockaddr *sa_addr; 512 struct sctp_af *af; 513 514 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk, 515 addrs, addrcnt); 516 517 addr_buf = addrs; 518 for (cnt = 0; cnt < addrcnt; cnt++) { 519 /* The list may contain either IPv4 or IPv6 address; 520 * determine the address length for walking thru the list. 521 */ 522 sa_addr = addr_buf; 523 af = sctp_get_af_specific(sa_addr->sa_family); 524 if (!af) { 525 retval = -EINVAL; 526 goto err_bindx_add; 527 } 528 529 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, 530 af->sockaddr_len); 531 532 addr_buf += af->sockaddr_len; 533 534 err_bindx_add: 535 if (retval < 0) { 536 /* Failed. Cleanup the ones that have been added */ 537 if (cnt > 0) 538 sctp_bindx_rem(sk, addrs, cnt); 539 return retval; 540 } 541 } 542 543 return retval; 544 } 545 546 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the 547 * associations that are part of the endpoint indicating that a list of local 548 * addresses are added to the endpoint. 549 * 550 * If any of the addresses is already in the bind address list of the 551 * association, we do not send the chunk for that association. But it will not 552 * affect other associations. 553 * 554 * Only sctp_setsockopt_bindx() is supposed to call this function. 555 */ 556 static int sctp_send_asconf_add_ip(struct sock *sk, 557 struct sockaddr *addrs, 558 int addrcnt) 559 { 560 struct sctp_sock *sp; 561 struct sctp_endpoint *ep; 562 struct sctp_association *asoc; 563 struct sctp_bind_addr *bp; 564 struct sctp_chunk *chunk; 565 struct sctp_sockaddr_entry *laddr; 566 union sctp_addr *addr; 567 union sctp_addr saveaddr; 568 void *addr_buf; 569 struct sctp_af *af; 570 struct list_head *p; 571 int i; 572 int retval = 0; 573 574 sp = sctp_sk(sk); 575 ep = sp->ep; 576 577 if (!ep->asconf_enable) 578 return retval; 579 580 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", 581 __func__, sk, addrs, addrcnt); 582 583 list_for_each_entry(asoc, &ep->asocs, asocs) { 584 if (!asoc->peer.asconf_capable) 585 continue; 586 587 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP) 588 continue; 589 590 if (!sctp_state(asoc, ESTABLISHED)) 591 continue; 592 593 /* Check if any address in the packed array of addresses is 594 * in the bind address list of the association. If so, 595 * do not send the asconf chunk to its peer, but continue with 596 * other associations. 597 */ 598 addr_buf = addrs; 599 for (i = 0; i < addrcnt; i++) { 600 addr = addr_buf; 601 af = sctp_get_af_specific(addr->v4.sin_family); 602 if (!af) { 603 retval = -EINVAL; 604 goto out; 605 } 606 607 if (sctp_assoc_lookup_laddr(asoc, addr)) 608 break; 609 610 addr_buf += af->sockaddr_len; 611 } 612 if (i < addrcnt) 613 continue; 614 615 /* Use the first valid address in bind addr list of 616 * association as Address Parameter of ASCONF CHUNK. 617 */ 618 bp = &asoc->base.bind_addr; 619 p = bp->address_list.next; 620 laddr = list_entry(p, struct sctp_sockaddr_entry, list); 621 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs, 622 addrcnt, SCTP_PARAM_ADD_IP); 623 if (!chunk) { 624 retval = -ENOMEM; 625 goto out; 626 } 627 628 /* Add the new addresses to the bind address list with 629 * use_as_src set to 0. 630 */ 631 addr_buf = addrs; 632 for (i = 0; i < addrcnt; i++) { 633 addr = addr_buf; 634 af = sctp_get_af_specific(addr->v4.sin_family); 635 memcpy(&saveaddr, addr, af->sockaddr_len); 636 retval = sctp_add_bind_addr(bp, &saveaddr, 637 sizeof(saveaddr), 638 SCTP_ADDR_NEW, GFP_ATOMIC); 639 addr_buf += af->sockaddr_len; 640 } 641 if (asoc->src_out_of_asoc_ok) { 642 struct sctp_transport *trans; 643 644 list_for_each_entry(trans, 645 &asoc->peer.transport_addr_list, transports) { 646 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32, 647 2*asoc->pathmtu, 4380)); 648 trans->ssthresh = asoc->peer.i.a_rwnd; 649 trans->rto = asoc->rto_initial; 650 sctp_max_rto(asoc, trans); 651 trans->rtt = trans->srtt = trans->rttvar = 0; 652 /* Clear the source and route cache */ 653 sctp_transport_route(trans, NULL, 654 sctp_sk(asoc->base.sk)); 655 } 656 } 657 retval = sctp_send_asconf(asoc, chunk); 658 } 659 660 out: 661 return retval; 662 } 663 664 /* Remove a list of addresses from bind addresses list. Do not remove the 665 * last address. 666 * 667 * Basically run through each address specified in the addrs/addrcnt 668 * array/length pair, determine if it is IPv6 or IPv4 and call 669 * sctp_del_bind() on it. 670 * 671 * If any of them fails, then the operation will be reversed and the 672 * ones that were removed will be added back. 673 * 674 * At least one address has to be left; if only one address is 675 * available, the operation will return -EBUSY. 676 * 677 * Only sctp_setsockopt_bindx() is supposed to call this function. 678 */ 679 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt) 680 { 681 struct sctp_sock *sp = sctp_sk(sk); 682 struct sctp_endpoint *ep = sp->ep; 683 int cnt; 684 struct sctp_bind_addr *bp = &ep->base.bind_addr; 685 int retval = 0; 686 void *addr_buf; 687 union sctp_addr *sa_addr; 688 struct sctp_af *af; 689 690 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", 691 __func__, sk, addrs, addrcnt); 692 693 addr_buf = addrs; 694 for (cnt = 0; cnt < addrcnt; cnt++) { 695 /* If the bind address list is empty or if there is only one 696 * bind address, there is nothing more to be removed (we need 697 * at least one address here). 698 */ 699 if (list_empty(&bp->address_list) || 700 (sctp_list_single_entry(&bp->address_list))) { 701 retval = -EBUSY; 702 goto err_bindx_rem; 703 } 704 705 sa_addr = addr_buf; 706 af = sctp_get_af_specific(sa_addr->sa.sa_family); 707 if (!af) { 708 retval = -EINVAL; 709 goto err_bindx_rem; 710 } 711 712 if (!af->addr_valid(sa_addr, sp, NULL)) { 713 retval = -EADDRNOTAVAIL; 714 goto err_bindx_rem; 715 } 716 717 if (sa_addr->v4.sin_port && 718 sa_addr->v4.sin_port != htons(bp->port)) { 719 retval = -EINVAL; 720 goto err_bindx_rem; 721 } 722 723 if (!sa_addr->v4.sin_port) 724 sa_addr->v4.sin_port = htons(bp->port); 725 726 /* FIXME - There is probably a need to check if sk->sk_saddr and 727 * sk->sk_rcv_addr are currently set to one of the addresses to 728 * be removed. This is something which needs to be looked into 729 * when we are fixing the outstanding issues with multi-homing 730 * socket routing and failover schemes. Refer to comments in 731 * sctp_do_bind(). -daisy 732 */ 733 retval = sctp_del_bind_addr(bp, sa_addr); 734 735 addr_buf += af->sockaddr_len; 736 err_bindx_rem: 737 if (retval < 0) { 738 /* Failed. Add the ones that has been removed back */ 739 if (cnt > 0) 740 sctp_bindx_add(sk, addrs, cnt); 741 return retval; 742 } 743 } 744 745 return retval; 746 } 747 748 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of 749 * the associations that are part of the endpoint indicating that a list of 750 * local addresses are removed from the endpoint. 751 * 752 * If any of the addresses is already in the bind address list of the 753 * association, we do not send the chunk for that association. But it will not 754 * affect other associations. 755 * 756 * Only sctp_setsockopt_bindx() is supposed to call this function. 757 */ 758 static int sctp_send_asconf_del_ip(struct sock *sk, 759 struct sockaddr *addrs, 760 int addrcnt) 761 { 762 struct sctp_sock *sp; 763 struct sctp_endpoint *ep; 764 struct sctp_association *asoc; 765 struct sctp_transport *transport; 766 struct sctp_bind_addr *bp; 767 struct sctp_chunk *chunk; 768 union sctp_addr *laddr; 769 void *addr_buf; 770 struct sctp_af *af; 771 struct sctp_sockaddr_entry *saddr; 772 int i; 773 int retval = 0; 774 int stored = 0; 775 776 chunk = NULL; 777 sp = sctp_sk(sk); 778 ep = sp->ep; 779 780 if (!ep->asconf_enable) 781 return retval; 782 783 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", 784 __func__, sk, addrs, addrcnt); 785 786 list_for_each_entry(asoc, &ep->asocs, asocs) { 787 788 if (!asoc->peer.asconf_capable) 789 continue; 790 791 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP) 792 continue; 793 794 if (!sctp_state(asoc, ESTABLISHED)) 795 continue; 796 797 /* Check if any address in the packed array of addresses is 798 * not present in the bind address list of the association. 799 * If so, do not send the asconf chunk to its peer, but 800 * continue with other associations. 801 */ 802 addr_buf = addrs; 803 for (i = 0; i < addrcnt; i++) { 804 laddr = addr_buf; 805 af = sctp_get_af_specific(laddr->v4.sin_family); 806 if (!af) { 807 retval = -EINVAL; 808 goto out; 809 } 810 811 if (!sctp_assoc_lookup_laddr(asoc, laddr)) 812 break; 813 814 addr_buf += af->sockaddr_len; 815 } 816 if (i < addrcnt) 817 continue; 818 819 /* Find one address in the association's bind address list 820 * that is not in the packed array of addresses. This is to 821 * make sure that we do not delete all the addresses in the 822 * association. 823 */ 824 bp = &asoc->base.bind_addr; 825 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs, 826 addrcnt, sp); 827 if ((laddr == NULL) && (addrcnt == 1)) { 828 if (asoc->asconf_addr_del_pending) 829 continue; 830 asoc->asconf_addr_del_pending = 831 kzalloc_obj(union sctp_addr, GFP_ATOMIC); 832 if (asoc->asconf_addr_del_pending == NULL) { 833 retval = -ENOMEM; 834 goto out; 835 } 836 asoc->asconf_addr_del_pending->sa.sa_family = 837 addrs->sa_family; 838 asoc->asconf_addr_del_pending->v4.sin_port = 839 htons(bp->port); 840 if (addrs->sa_family == AF_INET) { 841 struct sockaddr_in *sin; 842 843 sin = (struct sockaddr_in *)addrs; 844 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr; 845 } else if (addrs->sa_family == AF_INET6) { 846 struct sockaddr_in6 *sin6; 847 848 sin6 = (struct sockaddr_in6 *)addrs; 849 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr; 850 } 851 852 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n", 853 __func__, asoc, &asoc->asconf_addr_del_pending->sa, 854 asoc->asconf_addr_del_pending); 855 856 asoc->src_out_of_asoc_ok = 1; 857 stored = 1; 858 goto skip_mkasconf; 859 } 860 861 if (laddr == NULL) 862 return -EINVAL; 863 864 /* We do not need RCU protection throughout this loop 865 * because this is done under a socket lock from the 866 * setsockopt call. 867 */ 868 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, 869 SCTP_PARAM_DEL_IP); 870 if (!chunk) { 871 retval = -ENOMEM; 872 goto out; 873 } 874 875 skip_mkasconf: 876 /* Reset use_as_src flag for the addresses in the bind address 877 * list that are to be deleted. 878 */ 879 addr_buf = addrs; 880 for (i = 0; i < addrcnt; i++) { 881 laddr = addr_buf; 882 af = sctp_get_af_specific(laddr->v4.sin_family); 883 list_for_each_entry(saddr, &bp->address_list, list) { 884 if (sctp_cmp_addr_exact(&saddr->a, laddr)) 885 saddr->state = SCTP_ADDR_DEL; 886 } 887 addr_buf += af->sockaddr_len; 888 } 889 890 /* Update the route and saddr entries for all the transports 891 * as some of the addresses in the bind address list are 892 * about to be deleted and cannot be used as source addresses. 893 */ 894 list_for_each_entry(transport, &asoc->peer.transport_addr_list, 895 transports) { 896 sctp_transport_route(transport, NULL, 897 sctp_sk(asoc->base.sk)); 898 } 899 900 if (stored) 901 /* We don't need to transmit ASCONF */ 902 continue; 903 retval = sctp_send_asconf(asoc, chunk); 904 } 905 out: 906 return retval; 907 } 908 909 /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */ 910 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw) 911 { 912 struct sock *sk = sctp_opt2sk(sp); 913 union sctp_addr *addr; 914 struct sctp_af *af; 915 916 /* It is safe to write port space in caller. */ 917 addr = &addrw->a; 918 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port); 919 af = sctp_get_af_specific(addr->sa.sa_family); 920 if (!af) 921 return -EINVAL; 922 if (sctp_verify_addr(sk, addr, af->sockaddr_len)) 923 return -EINVAL; 924 925 if (addrw->state == SCTP_ADDR_NEW) 926 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1); 927 else 928 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1); 929 } 930 931 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt() 932 * 933 * API 8.1 934 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, 935 * int flags); 936 * 937 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. 938 * If the sd is an IPv6 socket, the addresses passed can either be IPv4 939 * or IPv6 addresses. 940 * 941 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see 942 * Section 3.1.2 for this usage. 943 * 944 * addrs is a pointer to an array of one or more socket addresses. Each 945 * address is contained in its appropriate structure (i.e. struct 946 * sockaddr_in or struct sockaddr_in6) the family of the address type 947 * must be used to distinguish the address length (note that this 948 * representation is termed a "packed array" of addresses). The caller 949 * specifies the number of addresses in the array with addrcnt. 950 * 951 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns 952 * -1, and sets errno to the appropriate error code. 953 * 954 * For SCTP, the port given in each socket address must be the same, or 955 * sctp_bindx() will fail, setting errno to EINVAL. 956 * 957 * The flags parameter is formed from the bitwise OR of zero or more of 958 * the following currently defined flags: 959 * 960 * SCTP_BINDX_ADD_ADDR 961 * 962 * SCTP_BINDX_REM_ADDR 963 * 964 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the 965 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given 966 * addresses from the association. The two flags are mutually exclusive; 967 * if both are given, sctp_bindx() will fail with EINVAL. A caller may 968 * not remove all addresses from an association; sctp_bindx() will 969 * reject such an attempt with EINVAL. 970 * 971 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate 972 * additional addresses with an endpoint after calling bind(). Or use 973 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening 974 * socket is associated with so that no new association accepted will be 975 * associated with those addresses. If the endpoint supports dynamic 976 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a 977 * endpoint to send the appropriate message to the peer to change the 978 * peers address lists. 979 * 980 * Adding and removing addresses from a connected association is 981 * optional functionality. Implementations that do not support this 982 * functionality should return EOPNOTSUPP. 983 * 984 * Basically do nothing but copying the addresses from user to kernel 985 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk. 986 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() 987 * from userspace. 988 * 989 * On exit there is no need to do sockfd_put(), sys_setsockopt() does 990 * it. 991 * 992 * sk The sk of the socket 993 * addrs The pointer to the addresses 994 * addrssize Size of the addrs buffer 995 * op Operation to perform (add or remove, see the flags of 996 * sctp_bindx) 997 * 998 * Returns 0 if ok, <0 errno code on error. 999 */ 1000 static int sctp_setsockopt_bindx(struct sock *sk, struct sockaddr *addrs, 1001 int addrs_size, int op) 1002 { 1003 int err; 1004 int addrcnt = 0; 1005 int walk_size = 0; 1006 struct sockaddr *sa_addr; 1007 void *addr_buf = addrs; 1008 struct sctp_af *af; 1009 1010 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n", 1011 __func__, sk, addr_buf, addrs_size, op); 1012 1013 if (unlikely(addrs_size <= 0)) 1014 return -EINVAL; 1015 1016 /* Walk through the addrs buffer and count the number of addresses. */ 1017 while (walk_size < addrs_size) { 1018 if (walk_size + sizeof(sa_family_t) > addrs_size) 1019 return -EINVAL; 1020 1021 sa_addr = addr_buf; 1022 af = sctp_get_af_specific(sa_addr->sa_family); 1023 1024 /* If the address family is not supported or if this address 1025 * causes the address buffer to overflow return EINVAL. 1026 */ 1027 if (!af || (walk_size + af->sockaddr_len) > addrs_size) 1028 return -EINVAL; 1029 addrcnt++; 1030 addr_buf += af->sockaddr_len; 1031 walk_size += af->sockaddr_len; 1032 } 1033 1034 /* Do the work. */ 1035 switch (op) { 1036 case SCTP_BINDX_ADD_ADDR: 1037 /* Allow security module to validate bindx addresses. */ 1038 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD, 1039 addrs, addrs_size); 1040 if (err) 1041 return err; 1042 err = sctp_bindx_add(sk, addrs, addrcnt); 1043 if (err) 1044 return err; 1045 return sctp_send_asconf_add_ip(sk, addrs, addrcnt); 1046 case SCTP_BINDX_REM_ADDR: 1047 err = sctp_bindx_rem(sk, addrs, addrcnt); 1048 if (err) 1049 return err; 1050 return sctp_send_asconf_del_ip(sk, addrs, addrcnt); 1051 1052 default: 1053 return -EINVAL; 1054 } 1055 } 1056 1057 static int sctp_bind_add(struct sock *sk, struct sockaddr_unsized *addrs, 1058 int addrlen) 1059 { 1060 int err; 1061 1062 lock_sock(sk); 1063 err = sctp_setsockopt_bindx(sk, (struct sockaddr *)addrs, addrlen, SCTP_BINDX_ADD_ADDR); 1064 release_sock(sk); 1065 return err; 1066 } 1067 1068 static int sctp_connect_new_asoc(struct sctp_endpoint *ep, 1069 const union sctp_addr *daddr, 1070 const struct sctp_initmsg *init, 1071 struct sctp_transport **tp) 1072 { 1073 struct sctp_association *asoc; 1074 struct sock *sk = ep->base.sk; 1075 struct net *net = sock_net(sk); 1076 enum sctp_scope scope; 1077 int err; 1078 1079 if (sctp_endpoint_is_peeled_off(ep, daddr)) 1080 return -EADDRNOTAVAIL; 1081 1082 if (!ep->base.bind_addr.port) { 1083 if (sctp_autobind(sk)) 1084 return -EAGAIN; 1085 } else { 1086 if (inet_port_requires_bind_service(net, ep->base.bind_addr.port) && 1087 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) 1088 return -EACCES; 1089 } 1090 1091 scope = sctp_scope(daddr); 1092 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); 1093 if (!asoc) 1094 return -ENOMEM; 1095 1096 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL); 1097 if (err < 0) 1098 goto free; 1099 1100 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN); 1101 if (!*tp) { 1102 err = -ENOMEM; 1103 goto free; 1104 } 1105 1106 if (!init) 1107 return 0; 1108 1109 if (init->sinit_num_ostreams) { 1110 __u16 outcnt = init->sinit_num_ostreams; 1111 1112 asoc->c.sinit_num_ostreams = outcnt; 1113 /* outcnt has been changed, need to re-init stream */ 1114 err = sctp_stream_init(&asoc->stream, outcnt, 0, GFP_KERNEL); 1115 if (err) 1116 goto free; 1117 } 1118 1119 if (init->sinit_max_instreams) 1120 asoc->c.sinit_max_instreams = init->sinit_max_instreams; 1121 1122 if (init->sinit_max_attempts) 1123 asoc->max_init_attempts = init->sinit_max_attempts; 1124 1125 if (init->sinit_max_init_timeo) 1126 asoc->max_init_timeo = 1127 msecs_to_jiffies(init->sinit_max_init_timeo); 1128 1129 return 0; 1130 free: 1131 sctp_association_free(asoc); 1132 return err; 1133 } 1134 1135 static int sctp_connect_add_peer(struct sctp_association *asoc, 1136 union sctp_addr *daddr, int addr_len) 1137 { 1138 struct sctp_endpoint *ep = asoc->ep; 1139 struct sctp_association *old; 1140 struct sctp_transport *t; 1141 int err; 1142 1143 err = sctp_verify_addr(ep->base.sk, daddr, addr_len); 1144 if (err) 1145 return err; 1146 1147 old = sctp_endpoint_lookup_assoc(ep, daddr, &t); 1148 if (old && old != asoc) 1149 return old->state >= SCTP_STATE_ESTABLISHED ? -EISCONN 1150 : -EALREADY; 1151 1152 if (sctp_endpoint_is_peeled_off(ep, daddr)) 1153 return -EADDRNOTAVAIL; 1154 1155 t = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN); 1156 if (!t) 1157 return -ENOMEM; 1158 1159 return 0; 1160 } 1161 1162 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) 1163 * 1164 * Common routine for handling connect() and sctp_connectx(). 1165 * Connect will come in with just a single address. 1166 */ 1167 static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs, 1168 int addrs_size, int flags, sctp_assoc_t *assoc_id) 1169 { 1170 struct sctp_sock *sp = sctp_sk(sk); 1171 struct sctp_endpoint *ep = sp->ep; 1172 struct sctp_transport *transport; 1173 struct sctp_association *asoc; 1174 void *addr_buf = kaddrs; 1175 union sctp_addr *daddr; 1176 struct sctp_af *af; 1177 int walk_size, err; 1178 long timeo; 1179 1180 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) || 1181 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) 1182 return -EISCONN; 1183 1184 daddr = addr_buf; 1185 af = sctp_get_af_specific(daddr->sa.sa_family); 1186 if (!af || af->sockaddr_len > addrs_size) 1187 return -EINVAL; 1188 1189 err = sctp_verify_addr(sk, daddr, af->sockaddr_len); 1190 if (err) 1191 return err; 1192 1193 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport); 1194 if (asoc) 1195 return asoc->state >= SCTP_STATE_ESTABLISHED ? -EISCONN 1196 : -EALREADY; 1197 1198 err = sctp_connect_new_asoc(ep, daddr, NULL, &transport); 1199 if (err) 1200 return err; 1201 asoc = transport->asoc; 1202 1203 addr_buf += af->sockaddr_len; 1204 walk_size = af->sockaddr_len; 1205 while (walk_size < addrs_size) { 1206 err = -EINVAL; 1207 if (walk_size + sizeof(sa_family_t) > addrs_size) 1208 goto out_free; 1209 1210 daddr = addr_buf; 1211 af = sctp_get_af_specific(daddr->sa.sa_family); 1212 if (!af || af->sockaddr_len + walk_size > addrs_size) 1213 goto out_free; 1214 1215 if (asoc->peer.port != ntohs(daddr->v4.sin_port)) 1216 goto out_free; 1217 1218 err = sctp_connect_add_peer(asoc, daddr, af->sockaddr_len); 1219 if (err) 1220 goto out_free; 1221 1222 addr_buf += af->sockaddr_len; 1223 walk_size += af->sockaddr_len; 1224 } 1225 1226 /* In case the user of sctp_connectx() wants an association 1227 * id back, assign one now. 1228 */ 1229 if (assoc_id) { 1230 err = sctp_assoc_set_id(asoc, GFP_KERNEL); 1231 if (err < 0) 1232 goto out_free; 1233 } 1234 1235 err = sctp_primitive_ASSOCIATE(sock_net(sk), asoc, NULL); 1236 if (err < 0) 1237 goto out_free; 1238 1239 /* Initialize sk's dport and daddr for getpeername() */ 1240 inet_sk(sk)->inet_dport = htons(asoc->peer.port); 1241 sp->pf->to_sk_daddr(daddr, sk); 1242 sk->sk_err = 0; 1243 1244 if (assoc_id) 1245 *assoc_id = asoc->assoc_id; 1246 1247 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); 1248 return sctp_wait_for_connect(asoc, &timeo); 1249 1250 out_free: 1251 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n", 1252 __func__, asoc, kaddrs, err); 1253 sctp_association_free(asoc); 1254 return err; 1255 } 1256 1257 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt() 1258 * 1259 * API 8.9 1260 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt, 1261 * sctp_assoc_t *asoc); 1262 * 1263 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. 1264 * If the sd is an IPv6 socket, the addresses passed can either be IPv4 1265 * or IPv6 addresses. 1266 * 1267 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see 1268 * Section 3.1.2 for this usage. 1269 * 1270 * addrs is a pointer to an array of one or more socket addresses. Each 1271 * address is contained in its appropriate structure (i.e. struct 1272 * sockaddr_in or struct sockaddr_in6) the family of the address type 1273 * must be used to distengish the address length (note that this 1274 * representation is termed a "packed array" of addresses). The caller 1275 * specifies the number of addresses in the array with addrcnt. 1276 * 1277 * On success, sctp_connectx() returns 0. It also sets the assoc_id to 1278 * the association id of the new association. On failure, sctp_connectx() 1279 * returns -1, and sets errno to the appropriate error code. The assoc_id 1280 * is not touched by the kernel. 1281 * 1282 * For SCTP, the port given in each socket address must be the same, or 1283 * sctp_connectx() will fail, setting errno to EINVAL. 1284 * 1285 * An application can use sctp_connectx to initiate an association with 1286 * an endpoint that is multi-homed. Much like sctp_bindx() this call 1287 * allows a caller to specify multiple addresses at which a peer can be 1288 * reached. The way the SCTP stack uses the list of addresses to set up 1289 * the association is implementation dependent. This function only 1290 * specifies that the stack will try to make use of all the addresses in 1291 * the list when needed. 1292 * 1293 * Note that the list of addresses passed in is only used for setting up 1294 * the association. It does not necessarily equal the set of addresses 1295 * the peer uses for the resulting association. If the caller wants to 1296 * find out the set of peer addresses, it must use sctp_getpaddrs() to 1297 * retrieve them after the association has been set up. 1298 * 1299 * Basically do nothing but copying the addresses from user to kernel 1300 * land and invoking either sctp_connectx(). This is used for tunneling 1301 * the sctp_connectx() request through sctp_setsockopt() from userspace. 1302 * 1303 * On exit there is no need to do sockfd_put(), sys_setsockopt() does 1304 * it. 1305 * 1306 * sk The sk of the socket 1307 * addrs The pointer to the addresses 1308 * addrssize Size of the addrs buffer 1309 * 1310 * Returns >=0 if ok, <0 errno code on error. 1311 */ 1312 static int __sctp_setsockopt_connectx(struct sock *sk, struct sockaddr *kaddrs, 1313 int addrs_size, sctp_assoc_t *assoc_id) 1314 { 1315 int err = 0, flags = 0; 1316 1317 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n", 1318 __func__, sk, kaddrs, addrs_size); 1319 1320 /* make sure the 1st addr's sa_family is accessible later */ 1321 if (unlikely(addrs_size < sizeof(sa_family_t))) 1322 return -EINVAL; 1323 1324 /* Allow security module to validate connectx addresses. */ 1325 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX, 1326 (struct sockaddr *)kaddrs, 1327 addrs_size); 1328 if (err) 1329 return err; 1330 1331 /* in-kernel sockets don't generally have a file allocated to them 1332 * if all they do is call sock_create_kern(). 1333 */ 1334 if (sk->sk_socket->file) 1335 flags = sk->sk_socket->file->f_flags; 1336 1337 return __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id); 1338 } 1339 1340 /* 1341 * This is an older interface. It's kept for backward compatibility 1342 * to the option that doesn't provide association id. 1343 */ 1344 static int sctp_setsockopt_connectx_old(struct sock *sk, 1345 struct sockaddr *kaddrs, 1346 int addrs_size) 1347 { 1348 return __sctp_setsockopt_connectx(sk, kaddrs, addrs_size, NULL); 1349 } 1350 1351 /* 1352 * New interface for the API. The since the API is done with a socket 1353 * option, to make it simple we feed back the association id is as a return 1354 * indication to the call. Error is always negative and association id is 1355 * always positive. 1356 */ 1357 static int sctp_setsockopt_connectx(struct sock *sk, 1358 struct sockaddr *kaddrs, 1359 int addrs_size) 1360 { 1361 sctp_assoc_t assoc_id = 0; 1362 int err = 0; 1363 1364 err = __sctp_setsockopt_connectx(sk, kaddrs, addrs_size, &assoc_id); 1365 1366 if (err) 1367 return err; 1368 else 1369 return assoc_id; 1370 } 1371 1372 /* 1373 * New (hopefully final) interface for the API. 1374 * We use the sctp_getaddrs_old structure so that use-space library 1375 * can avoid any unnecessary allocations. The only different part 1376 * is that we store the actual length of the address buffer into the 1377 * addrs_num structure member. That way we can re-use the existing 1378 * code. 1379 */ 1380 #ifdef CONFIG_COMPAT 1381 struct compat_sctp_getaddrs_old { 1382 sctp_assoc_t assoc_id; 1383 s32 addr_num; 1384 compat_uptr_t addrs; /* struct sockaddr * */ 1385 }; 1386 #endif 1387 1388 static int sctp_getsockopt_connectx3(struct sock *sk, int len, 1389 char __user *optval, 1390 int __user *optlen) 1391 { 1392 struct sctp_getaddrs_old param; 1393 sctp_assoc_t assoc_id = 0; 1394 struct sockaddr *kaddrs; 1395 int err = 0; 1396 1397 #ifdef CONFIG_COMPAT 1398 if (in_compat_syscall()) { 1399 struct compat_sctp_getaddrs_old param32; 1400 1401 if (len < sizeof(param32)) 1402 return -EINVAL; 1403 if (copy_from_user(¶m32, optval, sizeof(param32))) 1404 return -EFAULT; 1405 1406 param.assoc_id = param32.assoc_id; 1407 param.addr_num = param32.addr_num; 1408 param.addrs = compat_ptr(param32.addrs); 1409 } else 1410 #endif 1411 { 1412 if (len < sizeof(param)) 1413 return -EINVAL; 1414 if (copy_from_user(¶m, optval, sizeof(param))) 1415 return -EFAULT; 1416 } 1417 1418 kaddrs = memdup_user(param.addrs, param.addr_num); 1419 if (IS_ERR(kaddrs)) 1420 return PTR_ERR(kaddrs); 1421 1422 err = __sctp_setsockopt_connectx(sk, kaddrs, param.addr_num, &assoc_id); 1423 kfree(kaddrs); 1424 if (err == 0 || err == -EINPROGRESS) { 1425 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id))) 1426 return -EFAULT; 1427 if (put_user(sizeof(assoc_id), optlen)) 1428 return -EFAULT; 1429 } 1430 1431 return err; 1432 } 1433 1434 /* API 3.1.4 close() - UDP Style Syntax 1435 * Applications use close() to perform graceful shutdown (as described in 1436 * Section 10.1 of [SCTP]) on ALL the associations currently represented 1437 * by a UDP-style socket. 1438 * 1439 * The syntax is 1440 * 1441 * ret = close(int sd); 1442 * 1443 * sd - the socket descriptor of the associations to be closed. 1444 * 1445 * To gracefully shutdown a specific association represented by the 1446 * UDP-style socket, an application should use the sendmsg() call, 1447 * passing no user data, but including the appropriate flag in the 1448 * ancillary data (see Section xxxx). 1449 * 1450 * If sd in the close() call is a branched-off socket representing only 1451 * one association, the shutdown is performed on that association only. 1452 * 1453 * 4.1.6 close() - TCP Style Syntax 1454 * 1455 * Applications use close() to gracefully close down an association. 1456 * 1457 * The syntax is: 1458 * 1459 * int close(int sd); 1460 * 1461 * sd - the socket descriptor of the association to be closed. 1462 * 1463 * After an application calls close() on a socket descriptor, no further 1464 * socket operations will succeed on that descriptor. 1465 * 1466 * API 7.1.4 SO_LINGER 1467 * 1468 * An application using the TCP-style socket can use this option to 1469 * perform the SCTP ABORT primitive. The linger option structure is: 1470 * 1471 * struct linger { 1472 * int l_onoff; // option on/off 1473 * int l_linger; // linger time 1474 * }; 1475 * 1476 * To enable the option, set l_onoff to 1. If the l_linger value is set 1477 * to 0, calling close() is the same as the ABORT primitive. If the 1478 * value is set to a negative value, the setsockopt() call will return 1479 * an error. If the value is set to a positive value linger_time, the 1480 * close() can be blocked for at most linger_time ms. If the graceful 1481 * shutdown phase does not finish during this period, close() will 1482 * return but the graceful shutdown phase continues in the system. 1483 */ 1484 static void sctp_close(struct sock *sk, long timeout) 1485 { 1486 struct net *net = sock_net(sk); 1487 struct sctp_endpoint *ep; 1488 struct sctp_association *asoc; 1489 struct list_head *pos, *temp; 1490 unsigned int data_was_unread; 1491 1492 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout); 1493 1494 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 1495 sk->sk_shutdown = SHUTDOWN_MASK; 1496 inet_sk_set_state(sk, SCTP_SS_CLOSING); 1497 1498 ep = sctp_sk(sk)->ep; 1499 1500 /* Clean up any skbs sitting on the receive queue. */ 1501 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue); 1502 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby); 1503 1504 /* Walk all associations on an endpoint. */ 1505 list_for_each_safe(pos, temp, &ep->asocs) { 1506 asoc = list_entry(pos, struct sctp_association, asocs); 1507 1508 if (sctp_style(sk, TCP)) { 1509 /* A closed association can still be in the list if 1510 * it belongs to a TCP-style listening socket that is 1511 * not yet accepted. If so, free it. If not, send an 1512 * ABORT or SHUTDOWN based on the linger options. 1513 */ 1514 if (sctp_state(asoc, CLOSED)) { 1515 sctp_association_free(asoc); 1516 continue; 1517 } 1518 } 1519 1520 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) || 1521 !skb_queue_empty(&asoc->ulpq.reasm) || 1522 !skb_queue_empty(&asoc->ulpq.reasm_uo) || 1523 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) { 1524 struct sctp_chunk *chunk; 1525 1526 chunk = sctp_make_abort_user(asoc, NULL, 0); 1527 sctp_primitive_ABORT(net, asoc, chunk); 1528 } else 1529 sctp_primitive_SHUTDOWN(net, asoc, NULL); 1530 } 1531 1532 /* On a TCP-style socket, block for at most linger_time if set. */ 1533 if (sctp_style(sk, TCP) && timeout) 1534 sctp_wait_for_close(sk, timeout); 1535 1536 /* This will run the backlog queue. */ 1537 release_sock(sk); 1538 1539 /* Supposedly, no process has access to the socket, but 1540 * the net layers still may. 1541 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock 1542 * held and that should be grabbed before socket lock. 1543 */ 1544 spin_lock_bh(&net->sctp.addr_wq_lock); 1545 bh_lock_sock_nested(sk); 1546 1547 /* Hold the sock, since sk_common_release() will put sock_put() 1548 * and we have just a little more cleanup. 1549 */ 1550 sock_hold(sk); 1551 sk_common_release(sk); 1552 1553 bh_unlock_sock(sk); 1554 spin_unlock_bh(&net->sctp.addr_wq_lock); 1555 1556 sock_put(sk); 1557 } 1558 1559 /* Handle EPIPE error. */ 1560 static int sctp_error(struct sock *sk, int flags, int err) 1561 { 1562 if (err == -EPIPE) 1563 err = sock_error(sk) ? : -EPIPE; 1564 if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) 1565 send_sig(SIGPIPE, current, 0); 1566 return err; 1567 } 1568 1569 /* API 3.1.3 sendmsg() - UDP Style Syntax 1570 * 1571 * An application uses sendmsg() and recvmsg() calls to transmit data to 1572 * and receive data from its peer. 1573 * 1574 * ssize_t sendmsg(int socket, const struct msghdr *message, 1575 * int flags); 1576 * 1577 * socket - the socket descriptor of the endpoint. 1578 * message - pointer to the msghdr structure which contains a single 1579 * user message and possibly some ancillary data. 1580 * 1581 * See Section 5 for complete description of the data 1582 * structures. 1583 * 1584 * flags - flags sent or received with the user message, see Section 1585 * 5 for complete description of the flags. 1586 * 1587 * Note: This function could use a rewrite especially when explicit 1588 * connect support comes in. 1589 */ 1590 /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */ 1591 1592 static int sctp_msghdr_parse(const struct msghdr *msg, 1593 struct sctp_cmsgs *cmsgs); 1594 1595 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs, 1596 struct sctp_sndrcvinfo *srinfo, 1597 const struct msghdr *msg, size_t msg_len) 1598 { 1599 __u16 sflags; 1600 int err; 1601 1602 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP)) 1603 return -EPIPE; 1604 1605 if (msg_len > sk->sk_sndbuf) 1606 return -EMSGSIZE; 1607 1608 memset(cmsgs, 0, sizeof(*cmsgs)); 1609 err = sctp_msghdr_parse(msg, cmsgs); 1610 if (err) { 1611 pr_debug("%s: msghdr parse err:%x\n", __func__, err); 1612 return err; 1613 } 1614 1615 memset(srinfo, 0, sizeof(*srinfo)); 1616 if (cmsgs->srinfo) { 1617 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream; 1618 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags; 1619 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid; 1620 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context; 1621 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id; 1622 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive; 1623 } 1624 1625 if (cmsgs->sinfo) { 1626 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid; 1627 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags; 1628 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid; 1629 srinfo->sinfo_context = cmsgs->sinfo->snd_context; 1630 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id; 1631 } 1632 1633 if (cmsgs->prinfo) { 1634 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value; 1635 SCTP_PR_SET_POLICY(srinfo->sinfo_flags, 1636 cmsgs->prinfo->pr_policy); 1637 } 1638 1639 sflags = srinfo->sinfo_flags; 1640 if (!sflags && msg_len) 1641 return 0; 1642 1643 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT))) 1644 return -EINVAL; 1645 1646 if (((sflags & SCTP_EOF) && msg_len > 0) || 1647 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0)) 1648 return -EINVAL; 1649 1650 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name) 1651 return -EINVAL; 1652 1653 return 0; 1654 } 1655 1656 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags, 1657 struct sctp_cmsgs *cmsgs, 1658 union sctp_addr *daddr, 1659 struct sctp_transport **tp) 1660 { 1661 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 1662 struct sctp_association *asoc; 1663 struct cmsghdr *cmsg; 1664 __be32 flowinfo = 0; 1665 struct sctp_af *af; 1666 int err; 1667 1668 *tp = NULL; 1669 1670 if (sflags & (SCTP_EOF | SCTP_ABORT)) 1671 return -EINVAL; 1672 1673 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) || 1674 sctp_sstate(sk, CLOSING))) 1675 return -EADDRNOTAVAIL; 1676 1677 /* Label connection socket for first association 1-to-many 1678 * style for client sequence socket()->sendmsg(). This 1679 * needs to be done before sctp_assoc_add_peer() as that will 1680 * set up the initial packet that needs to account for any 1681 * security ip options (CIPSO/CALIPSO) added to the packet. 1682 */ 1683 af = sctp_get_af_specific(daddr->sa.sa_family); 1684 if (!af) 1685 return -EINVAL; 1686 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT, 1687 (struct sockaddr *)daddr, 1688 af->sockaddr_len); 1689 if (err < 0) 1690 return err; 1691 1692 err = sctp_connect_new_asoc(ep, daddr, cmsgs->init, tp); 1693 if (err) 1694 return err; 1695 asoc = (*tp)->asoc; 1696 1697 if (!cmsgs->addrs_msg) 1698 return 0; 1699 1700 if (daddr->sa.sa_family == AF_INET6) 1701 flowinfo = daddr->v6.sin6_flowinfo; 1702 1703 /* sendv addr list parse */ 1704 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) { 1705 union sctp_addr _daddr; 1706 int dlen; 1707 1708 if (cmsg->cmsg_level != IPPROTO_SCTP || 1709 (cmsg->cmsg_type != SCTP_DSTADDRV4 && 1710 cmsg->cmsg_type != SCTP_DSTADDRV6)) 1711 continue; 1712 1713 daddr = &_daddr; 1714 memset(daddr, 0, sizeof(*daddr)); 1715 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr); 1716 if (cmsg->cmsg_type == SCTP_DSTADDRV4) { 1717 if (dlen < sizeof(struct in_addr)) { 1718 err = -EINVAL; 1719 goto free; 1720 } 1721 1722 dlen = sizeof(struct in_addr); 1723 daddr->v4.sin_family = AF_INET; 1724 daddr->v4.sin_port = htons(asoc->peer.port); 1725 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen); 1726 } else { 1727 if (dlen < sizeof(struct in6_addr)) { 1728 err = -EINVAL; 1729 goto free; 1730 } 1731 1732 dlen = sizeof(struct in6_addr); 1733 daddr->v6.sin6_flowinfo = flowinfo; 1734 daddr->v6.sin6_family = AF_INET6; 1735 daddr->v6.sin6_port = htons(asoc->peer.port); 1736 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen); 1737 } 1738 1739 err = sctp_connect_add_peer(asoc, daddr, sizeof(*daddr)); 1740 if (err) 1741 goto free; 1742 } 1743 1744 return 0; 1745 1746 free: 1747 sctp_association_free(asoc); 1748 return err; 1749 } 1750 1751 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc, 1752 __u16 sflags, struct msghdr *msg, 1753 size_t msg_len) 1754 { 1755 struct sock *sk = asoc->base.sk; 1756 struct net *net = sock_net(sk); 1757 1758 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) 1759 return -EPIPE; 1760 1761 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) && 1762 !sctp_state(asoc, ESTABLISHED)) 1763 return 0; 1764 1765 if (sflags & SCTP_EOF) { 1766 pr_debug("%s: shutting down association:%p\n", __func__, asoc); 1767 sctp_primitive_SHUTDOWN(net, asoc, NULL); 1768 1769 return 0; 1770 } 1771 1772 if (sflags & SCTP_ABORT) { 1773 struct sctp_chunk *chunk; 1774 1775 chunk = sctp_make_abort_user(asoc, msg, msg_len); 1776 if (!chunk) 1777 return -ENOMEM; 1778 1779 pr_debug("%s: aborting association:%p\n", __func__, asoc); 1780 sctp_primitive_ABORT(net, asoc, chunk); 1781 iov_iter_revert(&msg->msg_iter, msg_len); 1782 1783 return 0; 1784 } 1785 1786 return 1; 1787 } 1788 1789 static int sctp_sendmsg_check_stream(struct sctp_association *asoc, 1790 struct sctp_sndrcvinfo *sinfo) 1791 { 1792 if (unlikely(sinfo->sinfo_stream >= asoc->stream.outcnt)) 1793 return -EINVAL; 1794 1795 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) 1796 return sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream); 1797 1798 return 0; 1799 } 1800 1801 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc, 1802 struct msghdr *msg, size_t msg_len, 1803 struct sctp_transport *transport, 1804 struct sctp_sndrcvinfo *sinfo) 1805 { 1806 struct sock *sk = asoc->base.sk; 1807 struct sctp_sock *sp = sctp_sk(sk); 1808 struct net *net = sock_net(sk); 1809 struct sctp_datamsg *datamsg; 1810 bool wait_connect = false; 1811 struct sctp_chunk *chunk; 1812 long timeo; 1813 int err; 1814 1815 err = sctp_sendmsg_check_stream(asoc, sinfo); 1816 if (err) 1817 goto err; 1818 1819 if (sp->disable_fragments && msg_len > asoc->frag_point) { 1820 err = -EMSGSIZE; 1821 goto err; 1822 } 1823 1824 if (asoc->pmtu_pending) { 1825 if (sp->param_flags & SPP_PMTUD_ENABLE) 1826 sctp_assoc_sync_pmtu(asoc); 1827 asoc->pmtu_pending = 0; 1828 } 1829 1830 if (sctp_wspace(asoc) < (int)msg_len) 1831 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc)); 1832 1833 if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) { 1834 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 1835 err = sctp_wait_for_sndbuf(asoc, transport, &timeo, msg_len); 1836 if (err) 1837 goto err; 1838 err = sctp_sendmsg_check_stream(asoc, sinfo); 1839 if (err) 1840 goto err; 1841 } 1842 1843 if (sctp_state(asoc, CLOSED)) { 1844 err = sctp_primitive_ASSOCIATE(net, asoc, NULL); 1845 if (err) 1846 goto err; 1847 1848 if (asoc->ep->intl_enable) { 1849 timeo = sock_sndtimeo(sk, 0); 1850 err = sctp_wait_for_connect(asoc, &timeo); 1851 if (err || sk->sk_shutdown & RCV_SHUTDOWN) { 1852 err = -ESRCH; 1853 goto err; 1854 } 1855 err = sctp_sendmsg_check_stream(asoc, sinfo); 1856 if (err) { 1857 err = -ESRCH; 1858 goto err; 1859 } 1860 } else { 1861 wait_connect = true; 1862 } 1863 1864 pr_debug("%s: we associated primitively\n", __func__); 1865 } 1866 1867 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter); 1868 if (IS_ERR(datamsg)) { 1869 err = PTR_ERR(datamsg); 1870 goto err; 1871 } 1872 1873 asoc->force_delay = !!(msg->msg_flags & MSG_MORE); 1874 1875 list_for_each_entry(chunk, &datamsg->chunks, frag_list) { 1876 sctp_chunk_hold(chunk); 1877 sctp_set_owner_w(chunk); 1878 chunk->transport = transport; 1879 } 1880 1881 err = sctp_primitive_SEND(net, asoc, datamsg); 1882 if (err) { 1883 sctp_datamsg_free(datamsg); 1884 goto err; 1885 } 1886 1887 pr_debug("%s: we sent primitively\n", __func__); 1888 1889 sctp_datamsg_put(datamsg); 1890 1891 if (unlikely(wait_connect)) { 1892 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 1893 sctp_wait_for_connect(asoc, &timeo); 1894 } 1895 1896 err = msg_len; 1897 1898 err: 1899 return err; 1900 } 1901 1902 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk, 1903 const struct msghdr *msg, 1904 struct sctp_cmsgs *cmsgs) 1905 { 1906 union sctp_addr *daddr = NULL; 1907 int err; 1908 1909 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { 1910 int len = msg->msg_namelen; 1911 1912 if (len > sizeof(*daddr)) 1913 len = sizeof(*daddr); 1914 1915 daddr = (union sctp_addr *)msg->msg_name; 1916 1917 err = sctp_verify_addr(sk, daddr, len); 1918 if (err) 1919 return ERR_PTR(err); 1920 } 1921 1922 return daddr; 1923 } 1924 1925 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc, 1926 struct sctp_sndrcvinfo *sinfo, 1927 struct sctp_cmsgs *cmsgs) 1928 { 1929 if (!cmsgs->srinfo && !cmsgs->sinfo) { 1930 sinfo->sinfo_stream = asoc->default_stream; 1931 sinfo->sinfo_ppid = asoc->default_ppid; 1932 sinfo->sinfo_context = asoc->default_context; 1933 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc); 1934 1935 if (!cmsgs->prinfo) 1936 sinfo->sinfo_flags = asoc->default_flags; 1937 } 1938 1939 if (!cmsgs->srinfo && !cmsgs->prinfo) 1940 sinfo->sinfo_timetolive = asoc->default_timetolive; 1941 1942 if (cmsgs->authinfo) { 1943 /* Reuse sinfo_tsn to indicate that authinfo was set and 1944 * sinfo_ssn to save the keyid on tx path. 1945 */ 1946 sinfo->sinfo_tsn = 1; 1947 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber; 1948 } 1949 } 1950 1951 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len) 1952 { 1953 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 1954 struct sctp_transport *transport = NULL; 1955 struct sctp_sndrcvinfo _sinfo, *sinfo; 1956 struct sctp_association *asoc, *tmp; 1957 struct sctp_cmsgs cmsgs; 1958 union sctp_addr *daddr; 1959 bool new = false; 1960 __u16 sflags; 1961 int err; 1962 1963 /* Parse and get snd_info */ 1964 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len); 1965 if (err) 1966 goto out; 1967 1968 sinfo = &_sinfo; 1969 sflags = sinfo->sinfo_flags; 1970 1971 /* Get daddr from msg */ 1972 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs); 1973 if (IS_ERR(daddr)) { 1974 err = PTR_ERR(daddr); 1975 goto out; 1976 } 1977 1978 lock_sock(sk); 1979 1980 /* SCTP_SENDALL process */ 1981 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) { 1982 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) { 1983 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, 1984 msg_len); 1985 if (err == 0) 1986 continue; 1987 if (err < 0) 1988 goto out_unlock; 1989 1990 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs); 1991 1992 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, 1993 NULL, sinfo); 1994 if (err < 0) 1995 goto out_unlock; 1996 1997 iov_iter_revert(&msg->msg_iter, err); 1998 1999 /* sctp_sendmsg_to_asoc() may have released the socket 2000 * lock (sctp_wait_for_sndbuf), during which other 2001 * associations on ep->asocs could have been peeled 2002 * off or freed. @asoc itself is revalidated by the 2003 * base.dead and base.sk checks in sctp_wait_for_sndbuf, 2004 * so re-derive the cached cursor from it. 2005 */ 2006 tmp = list_next_entry(asoc, asocs); 2007 } 2008 2009 goto out_unlock; 2010 } 2011 2012 /* Get and check or create asoc */ 2013 if (daddr) { 2014 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport); 2015 if (asoc) { 2016 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, 2017 msg_len); 2018 if (err <= 0) 2019 goto out_unlock; 2020 } else { 2021 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr, 2022 &transport); 2023 if (err) 2024 goto out_unlock; 2025 2026 asoc = transport->asoc; 2027 new = true; 2028 } 2029 2030 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER)) 2031 transport = NULL; 2032 } else { 2033 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id); 2034 if (!asoc) { 2035 err = -EPIPE; 2036 goto out_unlock; 2037 } 2038 2039 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len); 2040 if (err <= 0) 2041 goto out_unlock; 2042 } 2043 2044 /* Update snd_info with the asoc */ 2045 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs); 2046 2047 /* Send msg to the asoc */ 2048 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo); 2049 if (err < 0 && err != -ESRCH && new) 2050 sctp_association_free(asoc); 2051 2052 out_unlock: 2053 release_sock(sk); 2054 out: 2055 return sctp_error(sk, msg->msg_flags, err); 2056 } 2057 2058 /* This is an extended version of skb_pull() that removes the data from the 2059 * start of a skb even when data is spread across the list of skb's in the 2060 * frag_list. len specifies the total amount of data that needs to be removed. 2061 * when 'len' bytes could be removed from the skb, it returns 0. 2062 * If 'len' exceeds the total skb length, it returns the no. of bytes that 2063 * could not be removed. 2064 */ 2065 static int sctp_skb_pull(struct sk_buff *skb, int len) 2066 { 2067 struct sk_buff *list; 2068 int skb_len = skb_headlen(skb); 2069 int rlen; 2070 2071 if (len <= skb_len) { 2072 __skb_pull(skb, len); 2073 return 0; 2074 } 2075 len -= skb_len; 2076 __skb_pull(skb, skb_len); 2077 2078 skb_walk_frags(skb, list) { 2079 rlen = sctp_skb_pull(list, len); 2080 skb->len -= (len-rlen); 2081 skb->data_len -= (len-rlen); 2082 2083 if (!rlen) 2084 return 0; 2085 2086 len = rlen; 2087 } 2088 2089 return len; 2090 } 2091 2092 /* API 3.1.3 recvmsg() - UDP Style Syntax 2093 * 2094 * ssize_t recvmsg(int socket, struct msghdr *message, 2095 * int flags); 2096 * 2097 * socket - the socket descriptor of the endpoint. 2098 * message - pointer to the msghdr structure which contains a single 2099 * user message and possibly some ancillary data. 2100 * 2101 * See Section 5 for complete description of the data 2102 * structures. 2103 * 2104 * flags - flags sent or received with the user message, see Section 2105 * 5 for complete description of the flags. 2106 */ 2107 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 2108 int flags) 2109 { 2110 struct sctp_ulpevent *event = NULL; 2111 struct sctp_sock *sp = sctp_sk(sk); 2112 struct sk_buff *skb, *head_skb; 2113 int copied; 2114 int err = 0; 2115 int skb_len; 2116 2117 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, flags:0x%x)\n", 2118 __func__, sk, msg, len, flags); 2119 2120 if (unlikely(flags & MSG_ERRQUEUE)) 2121 return inet_recv_error(sk, msg, len); 2122 2123 if (sk_can_busy_loop(sk) && 2124 skb_queue_empty_lockless(&sk->sk_receive_queue)) 2125 sk_busy_loop(sk, flags & MSG_DONTWAIT); 2126 2127 lock_sock(sk); 2128 2129 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) && 2130 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) { 2131 err = -ENOTCONN; 2132 goto out; 2133 } 2134 2135 skb = sctp_skb_recv_datagram(sk, flags, &err); 2136 if (!skb) 2137 goto out; 2138 2139 /* Get the total length of the skb including any skb's in the 2140 * frag_list. 2141 */ 2142 skb_len = skb->len; 2143 2144 copied = skb_len; 2145 if (copied > len) 2146 copied = len; 2147 2148 err = skb_copy_datagram_msg(skb, 0, msg, copied); 2149 2150 event = sctp_skb2event(skb); 2151 2152 if (err) 2153 goto out_free; 2154 2155 if (event->chunk && event->chunk->head_skb) 2156 head_skb = event->chunk->head_skb; 2157 else 2158 head_skb = skb; 2159 sock_recv_cmsgs(msg, sk, head_skb); 2160 if (sctp_ulpevent_is_notification(event)) { 2161 msg->msg_flags |= MSG_NOTIFICATION; 2162 sp->pf->event_msgname(event, msg->msg_name, &msg->msg_namelen); 2163 } else { 2164 sp->pf->skb_msgname(head_skb, msg->msg_name, &msg->msg_namelen); 2165 } 2166 2167 /* Check if we allow SCTP_NXTINFO. */ 2168 if (sp->recvnxtinfo) 2169 sctp_ulpevent_read_nxtinfo(event, msg, sk); 2170 /* Check if we allow SCTP_RCVINFO. */ 2171 if (sp->recvrcvinfo) 2172 sctp_ulpevent_read_rcvinfo(event, msg); 2173 /* Check if we allow SCTP_SNDRCVINFO. */ 2174 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT)) 2175 sctp_ulpevent_read_sndrcvinfo(event, msg); 2176 2177 err = copied; 2178 2179 /* If skb's length exceeds the user's buffer, update the skb and 2180 * push it back to the receive_queue so that the next call to 2181 * recvmsg() will return the remaining data. Don't set MSG_EOR. 2182 */ 2183 if (skb_len > copied) { 2184 msg->msg_flags &= ~MSG_EOR; 2185 if (flags & MSG_PEEK) 2186 goto out_free; 2187 sctp_skb_pull(skb, copied); 2188 skb_queue_head(&sk->sk_receive_queue, skb); 2189 2190 /* When only partial message is copied to the user, increase 2191 * rwnd by that amount. If all the data in the skb is read, 2192 * rwnd is updated when the event is freed. 2193 */ 2194 if (!sctp_ulpevent_is_notification(event)) 2195 sctp_assoc_rwnd_increase(event->asoc, copied); 2196 goto out; 2197 } else if ((event->msg_flags & MSG_NOTIFICATION) || 2198 (event->msg_flags & MSG_EOR)) 2199 msg->msg_flags |= MSG_EOR; 2200 else 2201 msg->msg_flags &= ~MSG_EOR; 2202 2203 out_free: 2204 if (flags & MSG_PEEK) { 2205 /* Release the skb reference acquired after peeking the skb in 2206 * sctp_skb_recv_datagram(). 2207 */ 2208 kfree_skb(skb); 2209 } else { 2210 /* Free the event which includes releasing the reference to 2211 * the owner of the skb, freeing the skb and updating the 2212 * rwnd. 2213 */ 2214 sctp_ulpevent_free(event); 2215 } 2216 out: 2217 release_sock(sk); 2218 return err; 2219 } 2220 2221 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) 2222 * 2223 * This option is a on/off flag. If enabled no SCTP message 2224 * fragmentation will be performed. Instead if a message being sent 2225 * exceeds the current PMTU size, the message will NOT be sent and 2226 * instead a error will be indicated to the user. 2227 */ 2228 static int sctp_setsockopt_disable_fragments(struct sock *sk, int *val, 2229 unsigned int optlen) 2230 { 2231 if (optlen < sizeof(int)) 2232 return -EINVAL; 2233 sctp_sk(sk)->disable_fragments = (*val == 0) ? 0 : 1; 2234 return 0; 2235 } 2236 2237 static int sctp_setsockopt_events(struct sock *sk, __u8 *sn_type, 2238 unsigned int optlen) 2239 { 2240 struct sctp_sock *sp = sctp_sk(sk); 2241 struct sctp_association *asoc; 2242 int i; 2243 2244 if (optlen > sizeof(struct sctp_event_subscribe)) 2245 return -EINVAL; 2246 2247 for (i = 0; i < optlen; i++) 2248 sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i, 2249 sn_type[i]); 2250 2251 list_for_each_entry(asoc, &sp->ep->asocs, asocs) 2252 asoc->subscribe = sctp_sk(sk)->subscribe; 2253 2254 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT, 2255 * if there is no data to be sent or retransmit, the stack will 2256 * immediately send up this notification. 2257 */ 2258 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) { 2259 struct sctp_ulpevent *event; 2260 2261 asoc = sctp_id2assoc(sk, 0); 2262 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) { 2263 event = sctp_ulpevent_make_sender_dry_event(asoc, 2264 GFP_USER | __GFP_NOWARN); 2265 if (!event) 2266 return -ENOMEM; 2267 2268 asoc->stream.si->enqueue_event(&asoc->ulpq, event); 2269 } 2270 } 2271 2272 return 0; 2273 } 2274 2275 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) 2276 * 2277 * This socket option is applicable to the UDP-style socket only. When 2278 * set it will cause associations that are idle for more than the 2279 * specified number of seconds to automatically close. An association 2280 * being idle is defined an association that has NOT sent or received 2281 * user data. The special value of '0' indicates that no automatic 2282 * close of any associations should be performed. The option expects an 2283 * integer defining the number of seconds of idle time before an 2284 * association is closed. 2285 */ 2286 static int sctp_setsockopt_autoclose(struct sock *sk, u32 *optval, 2287 unsigned int optlen) 2288 { 2289 struct sctp_sock *sp = sctp_sk(sk); 2290 struct net *net = sock_net(sk); 2291 2292 /* Applicable to UDP-style socket only */ 2293 if (sctp_style(sk, TCP)) 2294 return -EOPNOTSUPP; 2295 if (optlen != sizeof(int)) 2296 return -EINVAL; 2297 2298 sp->autoclose = *optval; 2299 if (sp->autoclose > net->sctp.max_autoclose) 2300 sp->autoclose = net->sctp.max_autoclose; 2301 2302 return 0; 2303 } 2304 2305 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) 2306 * 2307 * Applications can enable or disable heartbeats for any peer address of 2308 * an association, modify an address's heartbeat interval, force a 2309 * heartbeat to be sent immediately, and adjust the address's maximum 2310 * number of retransmissions sent before an address is considered 2311 * unreachable. The following structure is used to access and modify an 2312 * address's parameters: 2313 * 2314 * struct sctp_paddrparams { 2315 * sctp_assoc_t spp_assoc_id; 2316 * struct sockaddr_storage spp_address; 2317 * uint32_t spp_hbinterval; 2318 * uint16_t spp_pathmaxrxt; 2319 * uint32_t spp_pathmtu; 2320 * uint32_t spp_sackdelay; 2321 * uint32_t spp_flags; 2322 * uint32_t spp_ipv6_flowlabel; 2323 * uint8_t spp_dscp; 2324 * }; 2325 * 2326 * spp_assoc_id - (one-to-many style socket) This is filled in the 2327 * application, and identifies the association for 2328 * this query. 2329 * spp_address - This specifies which address is of interest. 2330 * spp_hbinterval - This contains the value of the heartbeat interval, 2331 * in milliseconds. If a value of zero 2332 * is present in this field then no changes are to 2333 * be made to this parameter. 2334 * spp_pathmaxrxt - This contains the maximum number of 2335 * retransmissions before this address shall be 2336 * considered unreachable. If a value of zero 2337 * is present in this field then no changes are to 2338 * be made to this parameter. 2339 * spp_pathmtu - When Path MTU discovery is disabled the value 2340 * specified here will be the "fixed" path mtu. 2341 * Note that if the spp_address field is empty 2342 * then all associations on this address will 2343 * have this fixed path mtu set upon them. 2344 * 2345 * spp_sackdelay - When delayed sack is enabled, this value specifies 2346 * the number of milliseconds that sacks will be delayed 2347 * for. This value will apply to all addresses of an 2348 * association if the spp_address field is empty. Note 2349 * also, that if delayed sack is enabled and this 2350 * value is set to 0, no change is made to the last 2351 * recorded delayed sack timer value. 2352 * 2353 * spp_flags - These flags are used to control various features 2354 * on an association. The flag field may contain 2355 * zero or more of the following options. 2356 * 2357 * SPP_HB_ENABLE - Enable heartbeats on the 2358 * specified address. Note that if the address 2359 * field is empty all addresses for the association 2360 * have heartbeats enabled upon them. 2361 * 2362 * SPP_HB_DISABLE - Disable heartbeats on the 2363 * speicifed address. Note that if the address 2364 * field is empty all addresses for the association 2365 * will have their heartbeats disabled. Note also 2366 * that SPP_HB_ENABLE and SPP_HB_DISABLE are 2367 * mutually exclusive, only one of these two should 2368 * be specified. Enabling both fields will have 2369 * undetermined results. 2370 * 2371 * SPP_HB_DEMAND - Request a user initiated heartbeat 2372 * to be made immediately. 2373 * 2374 * SPP_HB_TIME_IS_ZERO - Specify's that the time for 2375 * heartbeat delayis to be set to the value of 0 2376 * milliseconds. 2377 * 2378 * SPP_PMTUD_ENABLE - This field will enable PMTU 2379 * discovery upon the specified address. Note that 2380 * if the address feild is empty then all addresses 2381 * on the association are effected. 2382 * 2383 * SPP_PMTUD_DISABLE - This field will disable PMTU 2384 * discovery upon the specified address. Note that 2385 * if the address feild is empty then all addresses 2386 * on the association are effected. Not also that 2387 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually 2388 * exclusive. Enabling both will have undetermined 2389 * results. 2390 * 2391 * SPP_SACKDELAY_ENABLE - Setting this flag turns 2392 * on delayed sack. The time specified in spp_sackdelay 2393 * is used to specify the sack delay for this address. Note 2394 * that if spp_address is empty then all addresses will 2395 * enable delayed sack and take on the sack delay 2396 * value specified in spp_sackdelay. 2397 * SPP_SACKDELAY_DISABLE - Setting this flag turns 2398 * off delayed sack. If the spp_address field is blank then 2399 * delayed sack is disabled for the entire association. Note 2400 * also that this field is mutually exclusive to 2401 * SPP_SACKDELAY_ENABLE, setting both will have undefined 2402 * results. 2403 * 2404 * SPP_IPV6_FLOWLABEL: Setting this flag enables the 2405 * setting of the IPV6 flow label value. The value is 2406 * contained in the spp_ipv6_flowlabel field. 2407 * Upon retrieval, this flag will be set to indicate that 2408 * the spp_ipv6_flowlabel field has a valid value returned. 2409 * If a specific destination address is set (in the 2410 * spp_address field), then the value returned is that of 2411 * the address. If just an association is specified (and 2412 * no address), then the association's default flow label 2413 * is returned. If neither an association nor a destination 2414 * is specified, then the socket's default flow label is 2415 * returned. For non-IPv6 sockets, this flag will be left 2416 * cleared. 2417 * 2418 * SPP_DSCP: Setting this flag enables the setting of the 2419 * Differentiated Services Code Point (DSCP) value 2420 * associated with either the association or a specific 2421 * address. The value is obtained in the spp_dscp field. 2422 * Upon retrieval, this flag will be set to indicate that 2423 * the spp_dscp field has a valid value returned. If a 2424 * specific destination address is set when called (in the 2425 * spp_address field), then that specific destination 2426 * address's DSCP value is returned. If just an association 2427 * is specified, then the association's default DSCP is 2428 * returned. If neither an association nor a destination is 2429 * specified, then the socket's default DSCP is returned. 2430 * 2431 * spp_ipv6_flowlabel 2432 * - This field is used in conjunction with the 2433 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label. 2434 * The 20 least significant bits are used for the flow 2435 * label. This setting has precedence over any IPv6-layer 2436 * setting. 2437 * 2438 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag 2439 * and contains the DSCP. The 6 most significant bits are 2440 * used for the DSCP. This setting has precedence over any 2441 * IPv4- or IPv6- layer setting. 2442 */ 2443 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params, 2444 struct sctp_transport *trans, 2445 struct sctp_association *asoc, 2446 struct sctp_sock *sp, 2447 int hb_change, 2448 int pmtud_change, 2449 int sackdelay_change) 2450 { 2451 int error; 2452 2453 if (params->spp_flags & SPP_HB_DEMAND && trans) { 2454 error = sctp_primitive_REQUESTHEARTBEAT(trans->asoc->base.net, 2455 trans->asoc, trans); 2456 if (error) 2457 return error; 2458 } 2459 2460 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of 2461 * this field is ignored. Note also that a value of zero indicates 2462 * the current setting should be left unchanged. 2463 */ 2464 if (params->spp_flags & SPP_HB_ENABLE) { 2465 2466 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is 2467 * set. This lets us use 0 value when this flag 2468 * is set. 2469 */ 2470 if (params->spp_flags & SPP_HB_TIME_IS_ZERO) 2471 params->spp_hbinterval = 0; 2472 2473 if (params->spp_hbinterval || 2474 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) { 2475 if (trans) { 2476 trans->hbinterval = 2477 msecs_to_jiffies(params->spp_hbinterval); 2478 sctp_transport_reset_hb_timer(trans); 2479 } else if (asoc) { 2480 asoc->hbinterval = 2481 msecs_to_jiffies(params->spp_hbinterval); 2482 } else { 2483 sp->hbinterval = params->spp_hbinterval; 2484 } 2485 } 2486 } 2487 2488 if (hb_change) { 2489 if (trans) { 2490 trans->param_flags = 2491 (trans->param_flags & ~SPP_HB) | hb_change; 2492 } else if (asoc) { 2493 asoc->param_flags = 2494 (asoc->param_flags & ~SPP_HB) | hb_change; 2495 } else { 2496 sp->param_flags = 2497 (sp->param_flags & ~SPP_HB) | hb_change; 2498 } 2499 } 2500 2501 /* When Path MTU discovery is disabled the value specified here will 2502 * be the "fixed" path mtu (i.e. the value of the spp_flags field must 2503 * include the flag SPP_PMTUD_DISABLE for this field to have any 2504 * effect). 2505 */ 2506 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) { 2507 if (trans) { 2508 trans->pathmtu = params->spp_pathmtu; 2509 sctp_assoc_sync_pmtu(asoc); 2510 } else if (asoc) { 2511 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu); 2512 } else { 2513 sp->pathmtu = params->spp_pathmtu; 2514 } 2515 } 2516 2517 if (pmtud_change) { 2518 if (trans) { 2519 int update = (trans->param_flags & SPP_PMTUD_DISABLE) && 2520 (params->spp_flags & SPP_PMTUD_ENABLE); 2521 trans->param_flags = 2522 (trans->param_flags & ~SPP_PMTUD) | pmtud_change; 2523 if (update) { 2524 sctp_transport_pmtu(trans, sctp_opt2sk(sp)); 2525 sctp_assoc_sync_pmtu(asoc); 2526 } 2527 sctp_transport_pl_reset(trans); 2528 } else if (asoc) { 2529 asoc->param_flags = 2530 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change; 2531 } else { 2532 sp->param_flags = 2533 (sp->param_flags & ~SPP_PMTUD) | pmtud_change; 2534 } 2535 } 2536 2537 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the 2538 * value of this field is ignored. Note also that a value of zero 2539 * indicates the current setting should be left unchanged. 2540 */ 2541 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) { 2542 if (trans) { 2543 trans->sackdelay = 2544 msecs_to_jiffies(params->spp_sackdelay); 2545 } else if (asoc) { 2546 asoc->sackdelay = 2547 msecs_to_jiffies(params->spp_sackdelay); 2548 } else { 2549 sp->sackdelay = params->spp_sackdelay; 2550 } 2551 } 2552 2553 if (sackdelay_change) { 2554 if (trans) { 2555 trans->param_flags = 2556 (trans->param_flags & ~SPP_SACKDELAY) | 2557 sackdelay_change; 2558 } else if (asoc) { 2559 asoc->param_flags = 2560 (asoc->param_flags & ~SPP_SACKDELAY) | 2561 sackdelay_change; 2562 } else { 2563 sp->param_flags = 2564 (sp->param_flags & ~SPP_SACKDELAY) | 2565 sackdelay_change; 2566 } 2567 } 2568 2569 /* Note that a value of zero indicates the current setting should be 2570 left unchanged. 2571 */ 2572 if (params->spp_pathmaxrxt) { 2573 if (trans) { 2574 trans->pathmaxrxt = params->spp_pathmaxrxt; 2575 } else if (asoc) { 2576 asoc->pathmaxrxt = params->spp_pathmaxrxt; 2577 } else { 2578 sp->pathmaxrxt = params->spp_pathmaxrxt; 2579 } 2580 } 2581 2582 if (params->spp_flags & SPP_IPV6_FLOWLABEL) { 2583 if (trans) { 2584 if (trans->ipaddr.sa.sa_family == AF_INET6) { 2585 trans->flowlabel = params->spp_ipv6_flowlabel & 2586 SCTP_FLOWLABEL_VAL_MASK; 2587 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK; 2588 } 2589 } else if (asoc) { 2590 struct sctp_transport *t; 2591 2592 list_for_each_entry(t, &asoc->peer.transport_addr_list, 2593 transports) { 2594 if (t->ipaddr.sa.sa_family != AF_INET6) 2595 continue; 2596 t->flowlabel = params->spp_ipv6_flowlabel & 2597 SCTP_FLOWLABEL_VAL_MASK; 2598 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK; 2599 } 2600 asoc->flowlabel = params->spp_ipv6_flowlabel & 2601 SCTP_FLOWLABEL_VAL_MASK; 2602 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK; 2603 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) { 2604 sp->flowlabel = params->spp_ipv6_flowlabel & 2605 SCTP_FLOWLABEL_VAL_MASK; 2606 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK; 2607 } 2608 } 2609 2610 if (params->spp_flags & SPP_DSCP) { 2611 if (trans) { 2612 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK; 2613 trans->dscp |= SCTP_DSCP_SET_MASK; 2614 } else if (asoc) { 2615 struct sctp_transport *t; 2616 2617 list_for_each_entry(t, &asoc->peer.transport_addr_list, 2618 transports) { 2619 t->dscp = params->spp_dscp & 2620 SCTP_DSCP_VAL_MASK; 2621 t->dscp |= SCTP_DSCP_SET_MASK; 2622 } 2623 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK; 2624 asoc->dscp |= SCTP_DSCP_SET_MASK; 2625 } else { 2626 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK; 2627 sp->dscp |= SCTP_DSCP_SET_MASK; 2628 } 2629 } 2630 2631 return 0; 2632 } 2633 2634 static int sctp_setsockopt_peer_addr_params(struct sock *sk, 2635 struct sctp_paddrparams *params, 2636 unsigned int optlen) 2637 { 2638 struct sctp_transport *trans = NULL; 2639 struct sctp_association *asoc = NULL; 2640 struct sctp_sock *sp = sctp_sk(sk); 2641 int error; 2642 int hb_change, pmtud_change, sackdelay_change; 2643 2644 if (optlen == ALIGN(offsetof(struct sctp_paddrparams, 2645 spp_ipv6_flowlabel), 4)) { 2646 if (params->spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL)) 2647 return -EINVAL; 2648 } else if (optlen != sizeof(*params)) { 2649 return -EINVAL; 2650 } 2651 2652 /* Validate flags and value parameters. */ 2653 hb_change = params->spp_flags & SPP_HB; 2654 pmtud_change = params->spp_flags & SPP_PMTUD; 2655 sackdelay_change = params->spp_flags & SPP_SACKDELAY; 2656 2657 if (hb_change == SPP_HB || 2658 pmtud_change == SPP_PMTUD || 2659 sackdelay_change == SPP_SACKDELAY || 2660 params->spp_sackdelay > 500 || 2661 (params->spp_pathmtu && 2662 params->spp_pathmtu < SCTP_DEFAULT_MINSEGMENT)) 2663 return -EINVAL; 2664 2665 /* If an address other than INADDR_ANY is specified, and 2666 * no transport is found, then the request is invalid. 2667 */ 2668 if (!sctp_is_any(sk, (union sctp_addr *)¶ms->spp_address)) { 2669 trans = sctp_addr_id2transport(sk, ¶ms->spp_address, 2670 params->spp_assoc_id); 2671 if (!trans) 2672 return -EINVAL; 2673 } 2674 2675 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the 2676 * socket is a one to many style socket, and an association 2677 * was not found, then the id was invalid. 2678 */ 2679 asoc = sctp_id2assoc(sk, params->spp_assoc_id); 2680 if (!asoc && params->spp_assoc_id != SCTP_FUTURE_ASSOC && 2681 sctp_style(sk, UDP)) 2682 return -EINVAL; 2683 2684 /* Heartbeat demand can only be sent on a transport or 2685 * association, but not a socket. 2686 */ 2687 if (params->spp_flags & SPP_HB_DEMAND && !trans && !asoc) 2688 return -EINVAL; 2689 2690 /* Process parameters. */ 2691 error = sctp_apply_peer_addr_params(params, trans, asoc, sp, 2692 hb_change, pmtud_change, 2693 sackdelay_change); 2694 2695 if (error) 2696 return error; 2697 2698 /* If changes are for association, also apply parameters to each 2699 * transport. 2700 */ 2701 if (!trans && asoc) { 2702 list_for_each_entry(trans, &asoc->peer.transport_addr_list, 2703 transports) { 2704 sctp_apply_peer_addr_params(params, trans, asoc, sp, 2705 hb_change, pmtud_change, 2706 sackdelay_change); 2707 } 2708 } 2709 2710 return 0; 2711 } 2712 2713 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags) 2714 { 2715 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE; 2716 } 2717 2718 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags) 2719 { 2720 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE; 2721 } 2722 2723 static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params, 2724 struct sctp_association *asoc) 2725 { 2726 struct sctp_transport *trans; 2727 2728 if (params->sack_delay) { 2729 asoc->sackdelay = msecs_to_jiffies(params->sack_delay); 2730 asoc->param_flags = 2731 sctp_spp_sackdelay_enable(asoc->param_flags); 2732 } 2733 if (params->sack_freq == 1) { 2734 asoc->param_flags = 2735 sctp_spp_sackdelay_disable(asoc->param_flags); 2736 } else if (params->sack_freq > 1) { 2737 asoc->sackfreq = params->sack_freq; 2738 asoc->param_flags = 2739 sctp_spp_sackdelay_enable(asoc->param_flags); 2740 } 2741 2742 list_for_each_entry(trans, &asoc->peer.transport_addr_list, 2743 transports) { 2744 if (params->sack_delay) { 2745 trans->sackdelay = msecs_to_jiffies(params->sack_delay); 2746 trans->param_flags = 2747 sctp_spp_sackdelay_enable(trans->param_flags); 2748 } 2749 if (params->sack_freq == 1) { 2750 trans->param_flags = 2751 sctp_spp_sackdelay_disable(trans->param_flags); 2752 } else if (params->sack_freq > 1) { 2753 trans->sackfreq = params->sack_freq; 2754 trans->param_flags = 2755 sctp_spp_sackdelay_enable(trans->param_flags); 2756 } 2757 } 2758 } 2759 2760 /* 2761 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) 2762 * 2763 * This option will effect the way delayed acks are performed. This 2764 * option allows you to get or set the delayed ack time, in 2765 * milliseconds. It also allows changing the delayed ack frequency. 2766 * Changing the frequency to 1 disables the delayed sack algorithm. If 2767 * the assoc_id is 0, then this sets or gets the endpoints default 2768 * values. If the assoc_id field is non-zero, then the set or get 2769 * effects the specified association for the one to many model (the 2770 * assoc_id field is ignored by the one to one model). Note that if 2771 * sack_delay or sack_freq are 0 when setting this option, then the 2772 * current values will remain unchanged. 2773 * 2774 * struct sctp_sack_info { 2775 * sctp_assoc_t sack_assoc_id; 2776 * uint32_t sack_delay; 2777 * uint32_t sack_freq; 2778 * }; 2779 * 2780 * sack_assoc_id - This parameter, indicates which association the user 2781 * is performing an action upon. Note that if this field's value is 2782 * zero then the endpoints default value is changed (effecting future 2783 * associations only). 2784 * 2785 * sack_delay - This parameter contains the number of milliseconds that 2786 * the user is requesting the delayed ACK timer be set to. Note that 2787 * this value is defined in the standard to be between 200 and 500 2788 * milliseconds. 2789 * 2790 * sack_freq - This parameter contains the number of packets that must 2791 * be received before a sack is sent without waiting for the delay 2792 * timer to expire. The default value for this is 2, setting this 2793 * value to 1 will disable the delayed sack algorithm. 2794 */ 2795 static int __sctp_setsockopt_delayed_ack(struct sock *sk, 2796 struct sctp_sack_info *params) 2797 { 2798 struct sctp_sock *sp = sctp_sk(sk); 2799 struct sctp_association *asoc; 2800 2801 /* Validate value parameter. */ 2802 if (params->sack_delay > 500) 2803 return -EINVAL; 2804 2805 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the 2806 * socket is a one to many style socket, and an association 2807 * was not found, then the id was invalid. 2808 */ 2809 asoc = sctp_id2assoc(sk, params->sack_assoc_id); 2810 if (!asoc && params->sack_assoc_id > SCTP_ALL_ASSOC && 2811 sctp_style(sk, UDP)) 2812 return -EINVAL; 2813 2814 if (asoc) { 2815 sctp_apply_asoc_delayed_ack(params, asoc); 2816 2817 return 0; 2818 } 2819 2820 if (sctp_style(sk, TCP)) 2821 params->sack_assoc_id = SCTP_FUTURE_ASSOC; 2822 2823 if (params->sack_assoc_id == SCTP_FUTURE_ASSOC || 2824 params->sack_assoc_id == SCTP_ALL_ASSOC) { 2825 if (params->sack_delay) { 2826 sp->sackdelay = params->sack_delay; 2827 sp->param_flags = 2828 sctp_spp_sackdelay_enable(sp->param_flags); 2829 } 2830 if (params->sack_freq == 1) { 2831 sp->param_flags = 2832 sctp_spp_sackdelay_disable(sp->param_flags); 2833 } else if (params->sack_freq > 1) { 2834 sp->sackfreq = params->sack_freq; 2835 sp->param_flags = 2836 sctp_spp_sackdelay_enable(sp->param_flags); 2837 } 2838 } 2839 2840 if (params->sack_assoc_id == SCTP_CURRENT_ASSOC || 2841 params->sack_assoc_id == SCTP_ALL_ASSOC) 2842 list_for_each_entry(asoc, &sp->ep->asocs, asocs) 2843 sctp_apply_asoc_delayed_ack(params, asoc); 2844 2845 return 0; 2846 } 2847 2848 static int sctp_setsockopt_delayed_ack(struct sock *sk, 2849 struct sctp_sack_info *params, 2850 unsigned int optlen) 2851 { 2852 if (optlen == sizeof(struct sctp_assoc_value)) { 2853 struct sctp_assoc_value *v = (struct sctp_assoc_value *)params; 2854 struct sctp_sack_info p; 2855 2856 pr_warn_ratelimited(DEPRECATED 2857 "%s (pid %d) " 2858 "Use of struct sctp_assoc_value in delayed_ack socket option.\n" 2859 "Use struct sctp_sack_info instead\n", 2860 current->comm, task_pid_nr(current)); 2861 2862 p.sack_assoc_id = v->assoc_id; 2863 p.sack_delay = v->assoc_value; 2864 p.sack_freq = v->assoc_value ? 0 : 1; 2865 return __sctp_setsockopt_delayed_ack(sk, &p); 2866 } 2867 2868 if (optlen != sizeof(struct sctp_sack_info)) 2869 return -EINVAL; 2870 if (params->sack_delay == 0 && params->sack_freq == 0) 2871 return 0; 2872 return __sctp_setsockopt_delayed_ack(sk, params); 2873 } 2874 2875 /* 7.1.3 Initialization Parameters (SCTP_INITMSG) 2876 * 2877 * Applications can specify protocol parameters for the default association 2878 * initialization. The option name argument to setsockopt() and getsockopt() 2879 * is SCTP_INITMSG. 2880 * 2881 * Setting initialization parameters is effective only on an unconnected 2882 * socket (for UDP-style sockets only future associations are effected 2883 * by the change). With TCP-style sockets, this option is inherited by 2884 * sockets derived from a listener socket. 2885 */ 2886 static int sctp_setsockopt_initmsg(struct sock *sk, struct sctp_initmsg *sinit, 2887 unsigned int optlen) 2888 { 2889 struct sctp_sock *sp = sctp_sk(sk); 2890 2891 if (optlen != sizeof(struct sctp_initmsg)) 2892 return -EINVAL; 2893 2894 if (sinit->sinit_num_ostreams) 2895 sp->initmsg.sinit_num_ostreams = sinit->sinit_num_ostreams; 2896 if (sinit->sinit_max_instreams) 2897 sp->initmsg.sinit_max_instreams = sinit->sinit_max_instreams; 2898 if (sinit->sinit_max_attempts) 2899 sp->initmsg.sinit_max_attempts = sinit->sinit_max_attempts; 2900 if (sinit->sinit_max_init_timeo) 2901 sp->initmsg.sinit_max_init_timeo = sinit->sinit_max_init_timeo; 2902 2903 return 0; 2904 } 2905 2906 /* 2907 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) 2908 * 2909 * Applications that wish to use the sendto() system call may wish to 2910 * specify a default set of parameters that would normally be supplied 2911 * through the inclusion of ancillary data. This socket option allows 2912 * such an application to set the default sctp_sndrcvinfo structure. 2913 * The application that wishes to use this socket option simply passes 2914 * in to this call the sctp_sndrcvinfo structure defined in Section 2915 * 5.2.2) The input parameters accepted by this call include 2916 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, 2917 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in 2918 * to this call if the caller is using the UDP model. 2919 */ 2920 static int sctp_setsockopt_default_send_param(struct sock *sk, 2921 struct sctp_sndrcvinfo *info, 2922 unsigned int optlen) 2923 { 2924 struct sctp_sock *sp = sctp_sk(sk); 2925 struct sctp_association *asoc; 2926 2927 if (optlen != sizeof(*info)) 2928 return -EINVAL; 2929 if (info->sinfo_flags & 2930 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 2931 SCTP_ABORT | SCTP_EOF)) 2932 return -EINVAL; 2933 2934 asoc = sctp_id2assoc(sk, info->sinfo_assoc_id); 2935 if (!asoc && info->sinfo_assoc_id > SCTP_ALL_ASSOC && 2936 sctp_style(sk, UDP)) 2937 return -EINVAL; 2938 2939 if (asoc) { 2940 asoc->default_stream = info->sinfo_stream; 2941 asoc->default_flags = info->sinfo_flags; 2942 asoc->default_ppid = info->sinfo_ppid; 2943 asoc->default_context = info->sinfo_context; 2944 asoc->default_timetolive = info->sinfo_timetolive; 2945 2946 return 0; 2947 } 2948 2949 if (sctp_style(sk, TCP)) 2950 info->sinfo_assoc_id = SCTP_FUTURE_ASSOC; 2951 2952 if (info->sinfo_assoc_id == SCTP_FUTURE_ASSOC || 2953 info->sinfo_assoc_id == SCTP_ALL_ASSOC) { 2954 sp->default_stream = info->sinfo_stream; 2955 sp->default_flags = info->sinfo_flags; 2956 sp->default_ppid = info->sinfo_ppid; 2957 sp->default_context = info->sinfo_context; 2958 sp->default_timetolive = info->sinfo_timetolive; 2959 } 2960 2961 if (info->sinfo_assoc_id == SCTP_CURRENT_ASSOC || 2962 info->sinfo_assoc_id == SCTP_ALL_ASSOC) { 2963 list_for_each_entry(asoc, &sp->ep->asocs, asocs) { 2964 asoc->default_stream = info->sinfo_stream; 2965 asoc->default_flags = info->sinfo_flags; 2966 asoc->default_ppid = info->sinfo_ppid; 2967 asoc->default_context = info->sinfo_context; 2968 asoc->default_timetolive = info->sinfo_timetolive; 2969 } 2970 } 2971 2972 return 0; 2973 } 2974 2975 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters 2976 * (SCTP_DEFAULT_SNDINFO) 2977 */ 2978 static int sctp_setsockopt_default_sndinfo(struct sock *sk, 2979 struct sctp_sndinfo *info, 2980 unsigned int optlen) 2981 { 2982 struct sctp_sock *sp = sctp_sk(sk); 2983 struct sctp_association *asoc; 2984 2985 if (optlen != sizeof(*info)) 2986 return -EINVAL; 2987 if (info->snd_flags & 2988 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 2989 SCTP_ABORT | SCTP_EOF)) 2990 return -EINVAL; 2991 2992 asoc = sctp_id2assoc(sk, info->snd_assoc_id); 2993 if (!asoc && info->snd_assoc_id > SCTP_ALL_ASSOC && 2994 sctp_style(sk, UDP)) 2995 return -EINVAL; 2996 2997 if (asoc) { 2998 asoc->default_stream = info->snd_sid; 2999 asoc->default_flags = info->snd_flags; 3000 asoc->default_ppid = info->snd_ppid; 3001 asoc->default_context = info->snd_context; 3002 3003 return 0; 3004 } 3005 3006 if (sctp_style(sk, TCP)) 3007 info->snd_assoc_id = SCTP_FUTURE_ASSOC; 3008 3009 if (info->snd_assoc_id == SCTP_FUTURE_ASSOC || 3010 info->snd_assoc_id == SCTP_ALL_ASSOC) { 3011 sp->default_stream = info->snd_sid; 3012 sp->default_flags = info->snd_flags; 3013 sp->default_ppid = info->snd_ppid; 3014 sp->default_context = info->snd_context; 3015 } 3016 3017 if (info->snd_assoc_id == SCTP_CURRENT_ASSOC || 3018 info->snd_assoc_id == SCTP_ALL_ASSOC) { 3019 list_for_each_entry(asoc, &sp->ep->asocs, asocs) { 3020 asoc->default_stream = info->snd_sid; 3021 asoc->default_flags = info->snd_flags; 3022 asoc->default_ppid = info->snd_ppid; 3023 asoc->default_context = info->snd_context; 3024 } 3025 } 3026 3027 return 0; 3028 } 3029 3030 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) 3031 * 3032 * Requests that the local SCTP stack use the enclosed peer address as 3033 * the association primary. The enclosed address must be one of the 3034 * association peer's addresses. 3035 */ 3036 static int sctp_setsockopt_primary_addr(struct sock *sk, struct sctp_prim *prim, 3037 unsigned int optlen) 3038 { 3039 struct sctp_transport *trans; 3040 struct sctp_af *af; 3041 int err; 3042 3043 if (optlen != sizeof(struct sctp_prim)) 3044 return -EINVAL; 3045 3046 /* Allow security module to validate address but need address len. */ 3047 af = sctp_get_af_specific(prim->ssp_addr.ss_family); 3048 if (!af) 3049 return -EINVAL; 3050 3051 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR, 3052 (struct sockaddr *)&prim->ssp_addr, 3053 af->sockaddr_len); 3054 if (err) 3055 return err; 3056 3057 trans = sctp_addr_id2transport(sk, &prim->ssp_addr, prim->ssp_assoc_id); 3058 if (!trans) 3059 return -EINVAL; 3060 3061 sctp_assoc_set_primary(trans->asoc, trans); 3062 3063 return 0; 3064 } 3065 3066 /* 3067 * 7.1.5 SCTP_NODELAY 3068 * 3069 * Turn on/off any Nagle-like algorithm. This means that packets are 3070 * generally sent as soon as possible and no unnecessary delays are 3071 * introduced, at the cost of more packets in the network. Expects an 3072 * integer boolean flag. 3073 */ 3074 static int sctp_setsockopt_nodelay(struct sock *sk, int *val, 3075 unsigned int optlen) 3076 { 3077 if (optlen < sizeof(int)) 3078 return -EINVAL; 3079 sctp_sk(sk)->nodelay = (*val == 0) ? 0 : 1; 3080 return 0; 3081 } 3082 3083 /* 3084 * 3085 * 7.1.1 SCTP_RTOINFO 3086 * 3087 * The protocol parameters used to initialize and bound retransmission 3088 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access 3089 * and modify these parameters. 3090 * All parameters are time values, in milliseconds. A value of 0, when 3091 * modifying the parameters, indicates that the current value should not 3092 * be changed. 3093 * 3094 */ 3095 static int sctp_setsockopt_rtoinfo(struct sock *sk, 3096 struct sctp_rtoinfo *rtoinfo, 3097 unsigned int optlen) 3098 { 3099 struct sctp_association *asoc; 3100 unsigned long rto_min, rto_max; 3101 struct sctp_sock *sp = sctp_sk(sk); 3102 3103 if (optlen != sizeof (struct sctp_rtoinfo)) 3104 return -EINVAL; 3105 3106 asoc = sctp_id2assoc(sk, rtoinfo->srto_assoc_id); 3107 3108 /* Set the values to the specific association */ 3109 if (!asoc && rtoinfo->srto_assoc_id != SCTP_FUTURE_ASSOC && 3110 sctp_style(sk, UDP)) 3111 return -EINVAL; 3112 3113 rto_max = rtoinfo->srto_max; 3114 rto_min = rtoinfo->srto_min; 3115 3116 if (rto_max) 3117 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max; 3118 else 3119 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max; 3120 3121 if (rto_min) 3122 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min; 3123 else 3124 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min; 3125 3126 if (rto_min > rto_max) 3127 return -EINVAL; 3128 3129 if (asoc) { 3130 if (rtoinfo->srto_initial != 0) 3131 asoc->rto_initial = 3132 msecs_to_jiffies(rtoinfo->srto_initial); 3133 asoc->rto_max = rto_max; 3134 asoc->rto_min = rto_min; 3135 } else { 3136 /* If there is no association or the association-id = 0 3137 * set the values to the endpoint. 3138 */ 3139 if (rtoinfo->srto_initial != 0) 3140 sp->rtoinfo.srto_initial = rtoinfo->srto_initial; 3141 sp->rtoinfo.srto_max = rto_max; 3142 sp->rtoinfo.srto_min = rto_min; 3143 } 3144 3145 return 0; 3146 } 3147 3148 /* 3149 * 3150 * 7.1.2 SCTP_ASSOCINFO 3151 * 3152 * This option is used to tune the maximum retransmission attempts 3153 * of the association. 3154 * Returns an error if the new association retransmission value is 3155 * greater than the sum of the retransmission value of the peer. 3156 * See [SCTP] for more information. 3157 * 3158 */ 3159 static int sctp_setsockopt_associnfo(struct sock *sk, 3160 struct sctp_assocparams *assocparams, 3161 unsigned int optlen) 3162 { 3163 3164 struct sctp_association *asoc; 3165 3166 if (optlen != sizeof(struct sctp_assocparams)) 3167 return -EINVAL; 3168 3169 asoc = sctp_id2assoc(sk, assocparams->sasoc_assoc_id); 3170 3171 if (!asoc && assocparams->sasoc_assoc_id != SCTP_FUTURE_ASSOC && 3172 sctp_style(sk, UDP)) 3173 return -EINVAL; 3174 3175 /* Set the values to the specific association */ 3176 if (asoc) { 3177 if (assocparams->sasoc_asocmaxrxt != 0) { 3178 __u32 path_sum = 0; 3179 int paths = 0; 3180 struct sctp_transport *peer_addr; 3181 3182 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list, 3183 transports) { 3184 path_sum += peer_addr->pathmaxrxt; 3185 paths++; 3186 } 3187 3188 /* Only validate asocmaxrxt if we have more than 3189 * one path/transport. We do this because path 3190 * retransmissions are only counted when we have more 3191 * then one path. 3192 */ 3193 if (paths > 1 && 3194 assocparams->sasoc_asocmaxrxt > path_sum) 3195 return -EINVAL; 3196 3197 asoc->max_retrans = assocparams->sasoc_asocmaxrxt; 3198 } 3199 3200 if (assocparams->sasoc_cookie_life != 0) 3201 asoc->cookie_life = 3202 ms_to_ktime(assocparams->sasoc_cookie_life); 3203 } else { 3204 /* Set the values to the endpoint */ 3205 struct sctp_sock *sp = sctp_sk(sk); 3206 3207 if (assocparams->sasoc_asocmaxrxt != 0) 3208 sp->assocparams.sasoc_asocmaxrxt = 3209 assocparams->sasoc_asocmaxrxt; 3210 if (assocparams->sasoc_cookie_life != 0) 3211 sp->assocparams.sasoc_cookie_life = 3212 assocparams->sasoc_cookie_life; 3213 } 3214 return 0; 3215 } 3216 3217 /* 3218 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) 3219 * 3220 * This socket option is a boolean flag which turns on or off mapped V4 3221 * addresses. If this option is turned on and the socket is type 3222 * PF_INET6, then IPv4 addresses will be mapped to V6 representation. 3223 * If this option is turned off, then no mapping will be done of V4 3224 * addresses and a user will receive both PF_INET6 and PF_INET type 3225 * addresses on the socket. 3226 */ 3227 static int sctp_setsockopt_mappedv4(struct sock *sk, int *val, 3228 unsigned int optlen) 3229 { 3230 struct sctp_sock *sp = sctp_sk(sk); 3231 3232 if (optlen < sizeof(int)) 3233 return -EINVAL; 3234 if (*val) 3235 sp->v4mapped = 1; 3236 else 3237 sp->v4mapped = 0; 3238 3239 return 0; 3240 } 3241 3242 /* 3243 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) 3244 * This option will get or set the maximum size to put in any outgoing 3245 * SCTP DATA chunk. If a message is larger than this size it will be 3246 * fragmented by SCTP into the specified size. Note that the underlying 3247 * SCTP implementation may fragment into smaller sized chunks when the 3248 * PMTU of the underlying association is smaller than the value set by 3249 * the user. The default value for this option is '0' which indicates 3250 * the user is NOT limiting fragmentation and only the PMTU will effect 3251 * SCTP's choice of DATA chunk size. Note also that values set larger 3252 * than the maximum size of an IP datagram will effectively let SCTP 3253 * control fragmentation (i.e. the same as setting this option to 0). 3254 * 3255 * The following structure is used to access and modify this parameter: 3256 * 3257 * struct sctp_assoc_value { 3258 * sctp_assoc_t assoc_id; 3259 * uint32_t assoc_value; 3260 * }; 3261 * 3262 * assoc_id: This parameter is ignored for one-to-one style sockets. 3263 * For one-to-many style sockets this parameter indicates which 3264 * association the user is performing an action upon. Note that if 3265 * this field's value is zero then the endpoints default value is 3266 * changed (effecting future associations only). 3267 * assoc_value: This parameter specifies the maximum size in bytes. 3268 */ 3269 static int sctp_setsockopt_maxseg(struct sock *sk, 3270 struct sctp_assoc_value *params, 3271 unsigned int optlen) 3272 { 3273 struct sctp_sock *sp = sctp_sk(sk); 3274 struct sctp_association *asoc; 3275 sctp_assoc_t assoc_id; 3276 int val; 3277 3278 if (optlen == sizeof(int)) { 3279 pr_warn_ratelimited(DEPRECATED 3280 "%s (pid %d) " 3281 "Use of int in maxseg socket option.\n" 3282 "Use struct sctp_assoc_value instead\n", 3283 current->comm, task_pid_nr(current)); 3284 assoc_id = SCTP_FUTURE_ASSOC; 3285 val = *(int *)params; 3286 } else if (optlen == sizeof(struct sctp_assoc_value)) { 3287 assoc_id = params->assoc_id; 3288 val = params->assoc_value; 3289 } else { 3290 return -EINVAL; 3291 } 3292 3293 asoc = sctp_id2assoc(sk, assoc_id); 3294 if (!asoc && assoc_id != SCTP_FUTURE_ASSOC && 3295 sctp_style(sk, UDP)) 3296 return -EINVAL; 3297 3298 if (val) { 3299 int min_len, max_len; 3300 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) : 3301 sizeof(struct sctp_data_chunk); 3302 3303 min_len = sctp_min_frag_point(sp, datasize); 3304 max_len = SCTP_MAX_CHUNK_LEN - datasize; 3305 3306 if (val < min_len || val > max_len) 3307 return -EINVAL; 3308 } 3309 3310 if (asoc) { 3311 asoc->user_frag = val; 3312 sctp_assoc_update_frag_point(asoc); 3313 } else { 3314 sp->user_frag = val; 3315 } 3316 3317 return 0; 3318 } 3319 3320 3321 /* 3322 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) 3323 * 3324 * Requests that the peer mark the enclosed address as the association 3325 * primary. The enclosed address must be one of the association's 3326 * locally bound addresses. The following structure is used to make a 3327 * set primary request: 3328 */ 3329 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, 3330 struct sctp_setpeerprim *prim, 3331 unsigned int optlen) 3332 { 3333 struct sctp_sock *sp; 3334 struct sctp_association *asoc = NULL; 3335 struct sctp_chunk *chunk; 3336 struct sctp_af *af; 3337 int err; 3338 3339 sp = sctp_sk(sk); 3340 3341 if (!sp->ep->asconf_enable) 3342 return -EPERM; 3343 3344 if (optlen != sizeof(struct sctp_setpeerprim)) 3345 return -EINVAL; 3346 3347 asoc = sctp_id2assoc(sk, prim->sspp_assoc_id); 3348 if (!asoc) 3349 return -EINVAL; 3350 3351 if (!asoc->peer.asconf_capable) 3352 return -EPERM; 3353 3354 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY) 3355 return -EPERM; 3356 3357 if (!sctp_state(asoc, ESTABLISHED)) 3358 return -ENOTCONN; 3359 3360 af = sctp_get_af_specific(prim->sspp_addr.ss_family); 3361 if (!af) 3362 return -EINVAL; 3363 3364 if (!af->addr_valid((union sctp_addr *)&prim->sspp_addr, sp, NULL)) 3365 return -EADDRNOTAVAIL; 3366 3367 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim->sspp_addr)) 3368 return -EADDRNOTAVAIL; 3369 3370 /* Allow security module to validate address. */ 3371 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR, 3372 (struct sockaddr *)&prim->sspp_addr, 3373 af->sockaddr_len); 3374 if (err) 3375 return err; 3376 3377 /* Create an ASCONF chunk with SET_PRIMARY parameter */ 3378 chunk = sctp_make_asconf_set_prim(asoc, 3379 (union sctp_addr *)&prim->sspp_addr); 3380 if (!chunk) 3381 return -ENOMEM; 3382 3383 err = sctp_send_asconf(asoc, chunk); 3384 3385 pr_debug("%s: we set peer primary addr primitively\n", __func__); 3386 3387 return err; 3388 } 3389 3390 static int sctp_setsockopt_adaptation_layer(struct sock *sk, 3391 struct sctp_setadaptation *adapt, 3392 unsigned int optlen) 3393 { 3394 if (optlen != sizeof(struct sctp_setadaptation)) 3395 return -EINVAL; 3396 3397 sctp_sk(sk)->adaptation_ind = adapt->ssb_adaptation_ind; 3398 3399 return 0; 3400 } 3401 3402 /* 3403 * 7.1.29. Set or Get the default context (SCTP_CONTEXT) 3404 * 3405 * The context field in the sctp_sndrcvinfo structure is normally only 3406 * used when a failed message is retrieved holding the value that was 3407 * sent down on the actual send call. This option allows the setting of 3408 * a default context on an association basis that will be received on 3409 * reading messages from the peer. This is especially helpful in the 3410 * one-2-many model for an application to keep some reference to an 3411 * internal state machine that is processing messages on the 3412 * association. Note that the setting of this value only effects 3413 * received messages from the peer and does not effect the value that is 3414 * saved with outbound messages. 3415 */ 3416 static int sctp_setsockopt_context(struct sock *sk, 3417 struct sctp_assoc_value *params, 3418 unsigned int optlen) 3419 { 3420 struct sctp_sock *sp = sctp_sk(sk); 3421 struct sctp_association *asoc; 3422 3423 if (optlen != sizeof(struct sctp_assoc_value)) 3424 return -EINVAL; 3425 3426 asoc = sctp_id2assoc(sk, params->assoc_id); 3427 if (!asoc && params->assoc_id > SCTP_ALL_ASSOC && 3428 sctp_style(sk, UDP)) 3429 return -EINVAL; 3430 3431 if (asoc) { 3432 asoc->default_rcv_context = params->assoc_value; 3433 3434 return 0; 3435 } 3436 3437 if (sctp_style(sk, TCP)) 3438 params->assoc_id = SCTP_FUTURE_ASSOC; 3439 3440 if (params->assoc_id == SCTP_FUTURE_ASSOC || 3441 params->assoc_id == SCTP_ALL_ASSOC) 3442 sp->default_rcv_context = params->assoc_value; 3443 3444 if (params->assoc_id == SCTP_CURRENT_ASSOC || 3445 params->assoc_id == SCTP_ALL_ASSOC) 3446 list_for_each_entry(asoc, &sp->ep->asocs, asocs) 3447 asoc->default_rcv_context = params->assoc_value; 3448 3449 return 0; 3450 } 3451 3452 /* 3453 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) 3454 * 3455 * This options will at a minimum specify if the implementation is doing 3456 * fragmented interleave. Fragmented interleave, for a one to many 3457 * socket, is when subsequent calls to receive a message may return 3458 * parts of messages from different associations. Some implementations 3459 * may allow you to turn this value on or off. If so, when turned off, 3460 * no fragment interleave will occur (which will cause a head of line 3461 * blocking amongst multiple associations sharing the same one to many 3462 * socket). When this option is turned on, then each receive call may 3463 * come from a different association (thus the user must receive data 3464 * with the extended calls (e.g. sctp_recvmsg) to keep track of which 3465 * association each receive belongs to. 3466 * 3467 * This option takes a boolean value. A non-zero value indicates that 3468 * fragmented interleave is on. A value of zero indicates that 3469 * fragmented interleave is off. 3470 * 3471 * Note that it is important that an implementation that allows this 3472 * option to be turned on, have it off by default. Otherwise an unaware 3473 * application using the one to many model may become confused and act 3474 * incorrectly. 3475 */ 3476 static int sctp_setsockopt_fragment_interleave(struct sock *sk, int *val, 3477 unsigned int optlen) 3478 { 3479 if (optlen != sizeof(int)) 3480 return -EINVAL; 3481 3482 sctp_sk(sk)->frag_interleave = !!*val; 3483 3484 if (!sctp_sk(sk)->frag_interleave) 3485 sctp_sk(sk)->ep->intl_enable = 0; 3486 3487 return 0; 3488 } 3489 3490 /* 3491 * 8.1.21. Set or Get the SCTP Partial Delivery Point 3492 * (SCTP_PARTIAL_DELIVERY_POINT) 3493 * 3494 * This option will set or get the SCTP partial delivery point. This 3495 * point is the size of a message where the partial delivery API will be 3496 * invoked to help free up rwnd space for the peer. Setting this to a 3497 * lower value will cause partial deliveries to happen more often. The 3498 * calls argument is an integer that sets or gets the partial delivery 3499 * point. Note also that the call will fail if the user attempts to set 3500 * this value larger than the socket receive buffer size. 3501 * 3502 * Note that any single message having a length smaller than or equal to 3503 * the SCTP partial delivery point will be delivered in one single read 3504 * call as long as the user provided buffer is large enough to hold the 3505 * message. 3506 */ 3507 static int sctp_setsockopt_partial_delivery_point(struct sock *sk, u32 *val, 3508 unsigned int optlen) 3509 { 3510 if (optlen != sizeof(u32)) 3511 return -EINVAL; 3512 3513 /* Note: We double the receive buffer from what the user sets 3514 * it to be, also initial rwnd is based on rcvbuf/2. 3515 */ 3516 if (*val > (sk->sk_rcvbuf >> 1)) 3517 return -EINVAL; 3518 3519 sctp_sk(sk)->pd_point = *val; 3520 3521 return 0; /* is this the right error code? */ 3522 } 3523 3524 /* 3525 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) 3526 * 3527 * This option will allow a user to change the maximum burst of packets 3528 * that can be emitted by this association. Note that the default value 3529 * is 4, and some implementations may restrict this setting so that it 3530 * can only be lowered. 3531 * 3532 * NOTE: This text doesn't seem right. Do this on a socket basis with 3533 * future associations inheriting the socket value. 3534 */ 3535 static int sctp_setsockopt_maxburst(struct sock *sk, 3536 struct sctp_assoc_value *params, 3537 unsigned int optlen) 3538 { 3539 struct sctp_sock *sp = sctp_sk(sk); 3540 struct sctp_association *asoc; 3541 sctp_assoc_t assoc_id; 3542 u32 assoc_value; 3543 3544 if (optlen == sizeof(int)) { 3545 pr_warn_ratelimited(DEPRECATED 3546 "%s (pid %d) " 3547 "Use of int in max_burst socket option deprecated.\n" 3548 "Use struct sctp_assoc_value instead\n", 3549 current->comm, task_pid_nr(current)); 3550 assoc_id = SCTP_FUTURE_ASSOC; 3551 assoc_value = *((int *)params); 3552 } else if (optlen == sizeof(struct sctp_assoc_value)) { 3553 assoc_id = params->assoc_id; 3554 assoc_value = params->assoc_value; 3555 } else 3556 return -EINVAL; 3557 3558 asoc = sctp_id2assoc(sk, assoc_id); 3559 if (!asoc && assoc_id > SCTP_ALL_ASSOC && sctp_style(sk, UDP)) 3560 return -EINVAL; 3561 3562 if (asoc) { 3563 asoc->max_burst = assoc_value; 3564 3565 return 0; 3566 } 3567 3568 if (sctp_style(sk, TCP)) 3569 assoc_id = SCTP_FUTURE_ASSOC; 3570 3571 if (assoc_id == SCTP_FUTURE_ASSOC || assoc_id == SCTP_ALL_ASSOC) 3572 sp->max_burst = assoc_value; 3573 3574 if (assoc_id == SCTP_CURRENT_ASSOC || assoc_id == SCTP_ALL_ASSOC) 3575 list_for_each_entry(asoc, &sp->ep->asocs, asocs) 3576 asoc->max_burst = assoc_value; 3577 3578 return 0; 3579 } 3580 3581 /* 3582 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK) 3583 * 3584 * This set option adds a chunk type that the user is requesting to be 3585 * received only in an authenticated way. Changes to the list of chunks 3586 * will only effect future associations on the socket. 3587 */ 3588 static int sctp_setsockopt_auth_chunk(struct sock *sk, 3589 struct sctp_authchunk *val, 3590 unsigned int optlen) 3591 { 3592 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3593 3594 if (!ep->auth_enable) 3595 return -EACCES; 3596 3597 if (optlen != sizeof(struct sctp_authchunk)) 3598 return -EINVAL; 3599 3600 switch (val->sauth_chunk) { 3601 case SCTP_CID_INIT: 3602 case SCTP_CID_INIT_ACK: 3603 case SCTP_CID_SHUTDOWN_COMPLETE: 3604 case SCTP_CID_AUTH: 3605 return -EINVAL; 3606 } 3607 3608 /* add this chunk id to the endpoint */ 3609 return sctp_auth_ep_add_chunkid(ep, val->sauth_chunk); 3610 } 3611 3612 /* 3613 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT) 3614 * 3615 * This option gets or sets the list of HMAC algorithms that the local 3616 * endpoint requires the peer to use. 3617 */ 3618 static int sctp_setsockopt_hmac_ident(struct sock *sk, 3619 struct sctp_hmacalgo *hmacs, 3620 unsigned int optlen) 3621 { 3622 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3623 u32 idents; 3624 3625 if (!ep->auth_enable) 3626 return -EACCES; 3627 3628 if (optlen < sizeof(struct sctp_hmacalgo)) 3629 return -EINVAL; 3630 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) + 3631 SCTP_AUTH_NUM_HMACS * sizeof(u16)); 3632 3633 idents = hmacs->shmac_num_idents; 3634 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS || 3635 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) 3636 return -EINVAL; 3637 3638 return sctp_auth_ep_set_hmacs(ep, hmacs); 3639 } 3640 3641 /* 3642 * 7.1.20. Set a shared key (SCTP_AUTH_KEY) 3643 * 3644 * This option will set a shared secret key which is used to build an 3645 * association shared key. 3646 */ 3647 static int sctp_setsockopt_auth_key(struct sock *sk, 3648 struct sctp_authkey *authkey, 3649 unsigned int optlen) 3650 { 3651 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3652 struct sctp_association *asoc; 3653 int ret = -EINVAL; 3654 3655 if (optlen <= sizeof(struct sctp_authkey)) 3656 return -EINVAL; 3657 /* authkey->sca_keylength is u16, so optlen can't be bigger than 3658 * this. 3659 */ 3660 optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey)); 3661 3662 if (authkey->sca_keylength > optlen - sizeof(*authkey)) 3663 goto out; 3664 3665 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id); 3666 if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC && 3667 sctp_style(sk, UDP)) 3668 goto out; 3669 3670 if (asoc) { 3671 ret = sctp_auth_set_key(ep, asoc, authkey); 3672 goto out; 3673 } 3674 3675 if (sctp_style(sk, TCP)) 3676 authkey->sca_assoc_id = SCTP_FUTURE_ASSOC; 3677 3678 if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC || 3679 authkey->sca_assoc_id == SCTP_ALL_ASSOC) { 3680 ret = sctp_auth_set_key(ep, asoc, authkey); 3681 if (ret) 3682 goto out; 3683 } 3684 3685 ret = 0; 3686 3687 if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC || 3688 authkey->sca_assoc_id == SCTP_ALL_ASSOC) { 3689 list_for_each_entry(asoc, &ep->asocs, asocs) { 3690 int res = sctp_auth_set_key(ep, asoc, authkey); 3691 3692 if (res && !ret) 3693 ret = res; 3694 } 3695 } 3696 3697 out: 3698 memzero_explicit(authkey, optlen); 3699 return ret; 3700 } 3701 3702 /* 3703 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY) 3704 * 3705 * This option will get or set the active shared key to be used to build 3706 * the association shared key. 3707 */ 3708 static int sctp_setsockopt_active_key(struct sock *sk, 3709 struct sctp_authkeyid *val, 3710 unsigned int optlen) 3711 { 3712 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3713 struct sctp_association *asoc; 3714 int ret = 0; 3715 3716 if (optlen != sizeof(struct sctp_authkeyid)) 3717 return -EINVAL; 3718 3719 asoc = sctp_id2assoc(sk, val->scact_assoc_id); 3720 if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC && 3721 sctp_style(sk, UDP)) 3722 return -EINVAL; 3723 3724 if (asoc) 3725 return sctp_auth_set_active_key(ep, asoc, val->scact_keynumber); 3726 3727 if (sctp_style(sk, TCP)) 3728 val->scact_assoc_id = SCTP_FUTURE_ASSOC; 3729 3730 if (val->scact_assoc_id == SCTP_FUTURE_ASSOC || 3731 val->scact_assoc_id == SCTP_ALL_ASSOC) { 3732 ret = sctp_auth_set_active_key(ep, asoc, val->scact_keynumber); 3733 if (ret) 3734 return ret; 3735 } 3736 3737 if (val->scact_assoc_id == SCTP_CURRENT_ASSOC || 3738 val->scact_assoc_id == SCTP_ALL_ASSOC) { 3739 list_for_each_entry(asoc, &ep->asocs, asocs) { 3740 int res = sctp_auth_set_active_key(ep, asoc, 3741 val->scact_keynumber); 3742 3743 if (res && !ret) 3744 ret = res; 3745 } 3746 } 3747 3748 return ret; 3749 } 3750 3751 /* 3752 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY) 3753 * 3754 * This set option will delete a shared secret key from use. 3755 */ 3756 static int sctp_setsockopt_del_key(struct sock *sk, 3757 struct sctp_authkeyid *val, 3758 unsigned int optlen) 3759 { 3760 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3761 struct sctp_association *asoc; 3762 int ret = 0; 3763 3764 if (optlen != sizeof(struct sctp_authkeyid)) 3765 return -EINVAL; 3766 3767 asoc = sctp_id2assoc(sk, val->scact_assoc_id); 3768 if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC && 3769 sctp_style(sk, UDP)) 3770 return -EINVAL; 3771 3772 if (asoc) 3773 return sctp_auth_del_key_id(ep, asoc, val->scact_keynumber); 3774 3775 if (sctp_style(sk, TCP)) 3776 val->scact_assoc_id = SCTP_FUTURE_ASSOC; 3777 3778 if (val->scact_assoc_id == SCTP_FUTURE_ASSOC || 3779 val->scact_assoc_id == SCTP_ALL_ASSOC) { 3780 ret = sctp_auth_del_key_id(ep, asoc, val->scact_keynumber); 3781 if (ret) 3782 return ret; 3783 } 3784 3785 if (val->scact_assoc_id == SCTP_CURRENT_ASSOC || 3786 val->scact_assoc_id == SCTP_ALL_ASSOC) { 3787 list_for_each_entry(asoc, &ep->asocs, asocs) { 3788 int res = sctp_auth_del_key_id(ep, asoc, 3789 val->scact_keynumber); 3790 3791 if (res && !ret) 3792 ret = res; 3793 } 3794 } 3795 3796 return ret; 3797 } 3798 3799 /* 3800 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY) 3801 * 3802 * This set option will deactivate a shared secret key. 3803 */ 3804 static int sctp_setsockopt_deactivate_key(struct sock *sk, 3805 struct sctp_authkeyid *val, 3806 unsigned int optlen) 3807 { 3808 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 3809 struct sctp_association *asoc; 3810 int ret = 0; 3811 3812 if (optlen != sizeof(struct sctp_authkeyid)) 3813 return -EINVAL; 3814 3815 asoc = sctp_id2assoc(sk, val->scact_assoc_id); 3816 if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC && 3817 sctp_style(sk, UDP)) 3818 return -EINVAL; 3819 3820 if (asoc) 3821 return sctp_auth_deact_key_id(ep, asoc, val->scact_keynumber); 3822 3823 if (sctp_style(sk, TCP)) 3824 val->scact_assoc_id = SCTP_FUTURE_ASSOC; 3825 3826 if (val->scact_assoc_id == SCTP_FUTURE_ASSOC || 3827 val->scact_assoc_id == SCTP_ALL_ASSOC) { 3828 ret = sctp_auth_deact_key_id(ep, asoc, val->scact_keynumber); 3829 if (ret) 3830 return ret; 3831 } 3832 3833 if (val->scact_assoc_id == SCTP_CURRENT_ASSOC || 3834 val->scact_assoc_id == SCTP_ALL_ASSOC) { 3835 list_for_each_entry(asoc, &ep->asocs, asocs) { 3836 int res = sctp_auth_deact_key_id(ep, asoc, 3837 val->scact_keynumber); 3838 3839 if (res && !ret) 3840 ret = res; 3841 } 3842 } 3843 3844 return ret; 3845 } 3846 3847 /* 3848 * 8.1.23 SCTP_AUTO_ASCONF 3849 * 3850 * This option will enable or disable the use of the automatic generation of 3851 * ASCONF chunks to add and delete addresses to an existing association. Note 3852 * that this option has two caveats namely: a) it only affects sockets that 3853 * are bound to all addresses available to the SCTP stack, and b) the system 3854 * administrator may have an overriding control that turns the ASCONF feature 3855 * off no matter what setting the socket option may have. 3856 * This option expects an integer boolean flag, where a non-zero value turns on 3857 * the option, and a zero value turns off the option. 3858 * Note. In this implementation, socket operation overrides default parameter 3859 * being set by sysctl as well as FreeBSD implementation 3860 */ 3861 static int sctp_setsockopt_auto_asconf(struct sock *sk, int *val, 3862 unsigned int optlen) 3863 { 3864 struct sctp_sock *sp = sctp_sk(sk); 3865 3866 if (optlen < sizeof(int)) 3867 return -EINVAL; 3868 if (!sctp_is_ep_boundall(sk) && *val) 3869 return -EINVAL; 3870 if ((*val && sp->do_auto_asconf) || (!*val && !sp->do_auto_asconf)) 3871 return 0; 3872 3873 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock); 3874 if (*val == 0 && sp->do_auto_asconf) { 3875 list_del(&sp->auto_asconf_list); 3876 sp->do_auto_asconf = 0; 3877 } else if (*val && !sp->do_auto_asconf) { 3878 list_add_tail(&sp->auto_asconf_list, 3879 &sock_net(sk)->sctp.auto_asconf_splist); 3880 sp->do_auto_asconf = 1; 3881 } 3882 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock); 3883 return 0; 3884 } 3885 3886 /* 3887 * SCTP_PEER_ADDR_THLDS 3888 * 3889 * This option allows us to alter the partially failed threshold for one or all 3890 * transports in an association. See Section 6.1 of: 3891 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt 3892 */ 3893 static int sctp_setsockopt_paddr_thresholds(struct sock *sk, 3894 struct sctp_paddrthlds_v2 *val, 3895 unsigned int optlen, bool v2) 3896 { 3897 struct sctp_transport *trans; 3898 struct sctp_association *asoc; 3899 int len; 3900 3901 len = v2 ? sizeof(*val) : sizeof(struct sctp_paddrthlds); 3902 if (optlen < len) 3903 return -EINVAL; 3904 3905 if (v2 && val->spt_pathpfthld > val->spt_pathcpthld) 3906 return -EINVAL; 3907 3908 if (!sctp_is_any(sk, (const union sctp_addr *)&val->spt_address)) { 3909 trans = sctp_addr_id2transport(sk, &val->spt_address, 3910 val->spt_assoc_id); 3911 if (!trans) 3912 return -ENOENT; 3913 3914 if (val->spt_pathmaxrxt) 3915 trans->pathmaxrxt = val->spt_pathmaxrxt; 3916 if (v2) 3917 trans->ps_retrans = val->spt_pathcpthld; 3918 trans->pf_retrans = val->spt_pathpfthld; 3919 3920 return 0; 3921 } 3922 3923 asoc = sctp_id2assoc(sk, val->spt_assoc_id); 3924 if (!asoc && val->spt_assoc_id != SCTP_FUTURE_ASSOC && 3925 sctp_style(sk, UDP)) 3926 return -EINVAL; 3927 3928 if (asoc) { 3929 list_for_each_entry(trans, &asoc->peer.transport_addr_list, 3930 transports) { 3931 if (val->spt_pathmaxrxt) 3932 trans->pathmaxrxt = val->spt_pathmaxrxt; 3933 if (v2) 3934 trans->ps_retrans = val->spt_pathcpthld; 3935 trans->pf_retrans = val->spt_pathpfthld; 3936 } 3937 3938 if (val->spt_pathmaxrxt) 3939 asoc->pathmaxrxt = val->spt_pathmaxrxt; 3940 if (v2) 3941 asoc->ps_retrans = val->spt_pathcpthld; 3942 asoc->pf_retrans = val->spt_pathpfthld; 3943 } else { 3944 struct sctp_sock *sp = sctp_sk(sk); 3945 3946 if (val->spt_pathmaxrxt) 3947 sp->pathmaxrxt = val->spt_pathmaxrxt; 3948 if (v2) 3949 sp->ps_retrans = val->spt_pathcpthld; 3950 sp->pf_retrans = val->spt_pathpfthld; 3951 } 3952 3953 return 0; 3954 } 3955 3956 static int sctp_setsockopt_recvrcvinfo(struct sock *sk, int *val, 3957 unsigned int optlen) 3958 { 3959 if (optlen < sizeof(int)) 3960 return -EINVAL; 3961 3962 sctp_sk(sk)->recvrcvinfo = (*val == 0) ? 0 : 1; 3963 3964 return 0; 3965 } 3966 3967 static int sctp_setsockopt_recvnxtinfo(struct sock *sk, int *val, 3968 unsigned int optlen) 3969 { 3970 if (optlen < sizeof(int)) 3971 return -EINVAL; 3972 3973 sctp_sk(sk)->recvnxtinfo = (*val == 0) ? 0 : 1; 3974 3975 return 0; 3976 } 3977 3978 static int sctp_setsockopt_pr_supported(struct sock *sk, 3979 struct sctp_assoc_value *params, 3980 unsigned int optlen) 3981 { 3982 struct sctp_association *asoc; 3983 3984 if (optlen != sizeof(*params)) 3985 return -EINVAL; 3986 3987 asoc = sctp_id2assoc(sk, params->assoc_id); 3988 if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC && 3989 sctp_style(sk, UDP)) 3990 return -EINVAL; 3991 3992 sctp_sk(sk)->ep->prsctp_enable = !!params->assoc_value; 3993 3994 return 0; 3995 } 3996 3997 static int sctp_setsockopt_default_prinfo(struct sock *sk, 3998 struct sctp_default_prinfo *info, 3999 unsigned int optlen) 4000 { 4001 struct sctp_sock *sp = sctp_sk(sk); 4002 struct sctp_association *asoc; 4003 int retval = -EINVAL; 4004 4005 if (optlen != sizeof(*info)) 4006 goto out; 4007 4008 if (info->pr_policy & ~SCTP_PR_SCTP_MASK) 4009 goto out; 4010 4011 if (info->pr_policy == SCTP_PR_SCTP_NONE) 4012 info->pr_value = 0; 4013 4014 asoc = sctp_id2assoc(sk, info->pr_assoc_id); 4015 if (!asoc && info->pr_assoc_id > SCTP_ALL_ASSOC && 4016 sctp_style(sk, UDP)) 4017 goto out; 4018 4019 retval = 0; 4020 4021 if (asoc) { 4022 SCTP_PR_SET_POLICY(asoc->default_flags, info->pr_policy); 4023 asoc->default_timetolive = info->pr_value; 4024 goto out; 4025 } 4026 4027 if (sctp_style(sk, TCP)) 4028 info->pr_assoc_id = SCTP_FUTURE_ASSOC; 4029 4030 if (info->pr_assoc_id == SCTP_FUTURE_ASSOC || 4031 info->pr_assoc_id == SCTP_ALL_ASSOC) { 4032 SCTP_PR_SET_POLICY(sp->default_flags, info->pr_policy); 4033 sp->default_timetolive = info->pr_value; 4034 } 4035 4036 if (info->pr_assoc_id == SCTP_CURRENT_ASSOC || 4037 info->pr_assoc_id == SCTP_ALL_ASSOC) { 4038 list_for_each_entry(asoc, &sp->ep->asocs, asocs) { 4039 SCTP_PR_SET_POLICY(asoc->default_flags, 4040 info->pr_policy); 4041 asoc->default_timetolive = info->pr_value; 4042 } 4043 } 4044 4045 out: 4046 return retval; 4047 } 4048 4049 static int sctp_setsockopt_reconfig_supported(struct sock *sk, 4050 struct sctp_assoc_value *params, 4051 unsigned int optlen) 4052 { 4053 struct sctp_association *asoc; 4054 int retval = -EINVAL; 4055 4056 if (optlen != sizeof(*params)) 4057 goto out; 4058 4059 asoc = sctp_id2assoc(sk, params->assoc_id); 4060 if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC && 4061 sctp_style(sk, UDP)) 4062 goto out; 4063 4064 sctp_sk(sk)->ep->reconf_enable = !!params->assoc_value; 4065 4066 retval = 0; 4067 4068 out: 4069 return retval; 4070 } 4071 4072 static int sctp_setsockopt_enable_strreset(struct sock *sk, 4073 struct sctp_assoc_value *params, 4074 unsigned int optlen) 4075 { 4076 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 4077 struct sctp_association *asoc; 4078 int retval = -EINVAL; 4079 4080 if (optlen != sizeof(*params)) 4081 goto out; 4082 4083 if (params->assoc_value & (~SCTP_ENABLE_STRRESET_MASK)) 4084 goto out; 4085 4086 asoc = sctp_id2assoc(sk, params->assoc_id); 4087 if (!asoc && params->assoc_id > SCTP_ALL_ASSOC && 4088 sctp_style(sk, UDP)) 4089 goto out; 4090 4091 retval = 0; 4092 4093 if (asoc) { 4094 asoc->strreset_enable = params->assoc_value; 4095 goto out; 4096 } 4097 4098 if (sctp_style(sk, TCP)) 4099 params->assoc_id = SCTP_FUTURE_ASSOC; 4100 4101 if (params->assoc_id == SCTP_FUTURE_ASSOC || 4102 params->assoc_id == SCTP_ALL_ASSOC) 4103 ep->strreset_enable = params->assoc_value; 4104 4105 if (params->assoc_id == SCTP_CURRENT_ASSOC || 4106 params->assoc_id == SCTP_ALL_ASSOC) 4107 list_for_each_entry(asoc, &ep->asocs, asocs) 4108 asoc->strreset_enable = params->assoc_value; 4109 4110 out: 4111 return retval; 4112 } 4113 4114 static int sctp_setsockopt_reset_streams(struct sock *sk, 4115 struct sctp_reset_streams *params, 4116 unsigned int optlen) 4117 { 4118 struct sctp_association *asoc; 4119 4120 if (optlen < sizeof(*params)) 4121 return -EINVAL; 4122 /* srs_number_streams is u16, so optlen can't be bigger than this. */ 4123 optlen = min_t(unsigned int, optlen, 4124 struct_size_t(struct sctp_reset_streams, srs_stream_list, 4125 USHRT_MAX)); 4126 4127 if (params->srs_number_streams * sizeof(__u16) > 4128 optlen - sizeof(*params)) 4129 return -EINVAL; 4130 4131 asoc = sctp_id2assoc(sk, params->srs_assoc_id); 4132 if (!asoc) 4133 return -EINVAL; 4134 4135 return sctp_send_reset_streams(asoc, params); 4136 } 4137 4138 static int sctp_setsockopt_reset_assoc(struct sock *sk, sctp_assoc_t *associd, 4139 unsigned int optlen) 4140 { 4141 struct sctp_association *asoc; 4142 4143 if (optlen != sizeof(*associd)) 4144 return -EINVAL; 4145 4146 asoc = sctp_id2assoc(sk, *associd); 4147 if (!asoc) 4148 return -EINVAL; 4149 4150 return sctp_send_reset_assoc(asoc); 4151 } 4152 4153 static int sctp_setsockopt_add_streams(struct sock *sk, 4154 struct sctp_add_streams *params, 4155 unsigned int optlen) 4156 { 4157 struct sctp_association *asoc; 4158 4159 if (optlen != sizeof(*params)) 4160 return -EINVAL; 4161 4162 asoc = sctp_id2assoc(sk, params->sas_assoc_id); 4163 if (!asoc) 4164 return -EINVAL; 4165 4166 return sctp_send_add_streams(asoc, params); 4167 } 4168 4169 static int sctp_setsockopt_scheduler(struct sock *sk, 4170 struct sctp_assoc_value *params, 4171 unsigned int optlen) 4172 { 4173 struct sctp_sock *sp = sctp_sk(sk); 4174 struct sctp_association *asoc; 4175 int retval = 0; 4176 4177 if (optlen < sizeof(*params)) 4178 return -EINVAL; 4179 4180 if (params->assoc_value > SCTP_SS_MAX) 4181 return -EINVAL; 4182 4183 asoc = sctp_id2assoc(sk, params->assoc_id); 4184 if (!asoc && params->assoc_id > SCTP_ALL_ASSOC && 4185 sctp_style(sk, UDP)) 4186 return -EINVAL; 4187 4188 if (asoc) 4189 return sctp_sched_set_sched(asoc, params->assoc_value); 4190 4191 if (sctp_style(sk, TCP)) 4192 params->assoc_id = SCTP_FUTURE_ASSOC; 4193 4194 if (params->assoc_id == SCTP_FUTURE_ASSOC || 4195 params->assoc_id == SCTP_ALL_ASSOC) 4196 sp->default_ss = params->assoc_value; 4197 4198 if (params->assoc_id == SCTP_CURRENT_ASSOC || 4199 params->assoc_id == SCTP_ALL_ASSOC) { 4200 list_for_each_entry(asoc, &sp->ep->asocs, asocs) { 4201 int ret = sctp_sched_set_sched(asoc, 4202 params->assoc_value); 4203 4204 if (ret && !retval) 4205 retval = ret; 4206 } 4207 } 4208 4209 return retval; 4210 } 4211 4212 static int sctp_setsockopt_scheduler_value(struct sock *sk, 4213 struct sctp_stream_value *params, 4214 unsigned int optlen) 4215 { 4216 struct sctp_association *asoc; 4217 int retval = -EINVAL; 4218 4219 if (optlen < sizeof(*params)) 4220 goto out; 4221 4222 asoc = sctp_id2assoc(sk, params->assoc_id); 4223 if (!asoc && params->assoc_id != SCTP_CURRENT_ASSOC && 4224 sctp_style(sk, UDP)) 4225 goto out; 4226 4227 if (asoc) { 4228 retval = sctp_sched_set_value(asoc, params->stream_id, 4229 params->stream_value, GFP_KERNEL); 4230 goto out; 4231 } 4232 4233 retval = 0; 4234 4235 list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) { 4236 int ret = sctp_sched_set_value(asoc, params->stream_id, 4237 params->stream_value, 4238 GFP_KERNEL); 4239 if (ret && !retval) /* try to return the 1st error. */ 4240 retval = ret; 4241 } 4242 4243 out: 4244 return retval; 4245 } 4246 4247 static int sctp_setsockopt_interleaving_supported(struct sock *sk, 4248 struct sctp_assoc_value *p, 4249 unsigned int optlen) 4250 { 4251 struct sctp_sock *sp = sctp_sk(sk); 4252 struct sctp_association *asoc; 4253 4254 if (optlen < sizeof(*p)) 4255 return -EINVAL; 4256 4257 asoc = sctp_id2assoc(sk, p->assoc_id); 4258 if (!asoc && p->assoc_id != SCTP_FUTURE_ASSOC && sctp_style(sk, UDP)) 4259 return -EINVAL; 4260 4261 if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) { 4262 return -EPERM; 4263 } 4264 4265 sp->ep->intl_enable = !!p->assoc_value; 4266 return 0; 4267 } 4268 4269 static int sctp_setsockopt_reuse_port(struct sock *sk, int *val, 4270 unsigned int optlen) 4271 { 4272 if (!sctp_style(sk, TCP)) 4273 return -EOPNOTSUPP; 4274 4275 if (sctp_sk(sk)->ep->base.bind_addr.port) 4276 return -EFAULT; 4277 4278 if (optlen < sizeof(int)) 4279 return -EINVAL; 4280 4281 sctp_sk(sk)->reuse = !!*val; 4282 4283 return 0; 4284 } 4285 4286 static int sctp_assoc_ulpevent_type_set(struct sctp_event *param, 4287 struct sctp_association *asoc) 4288 { 4289 struct sctp_ulpevent *event; 4290 4291 sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on); 4292 4293 if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) { 4294 if (sctp_outq_is_empty(&asoc->outqueue)) { 4295 event = sctp_ulpevent_make_sender_dry_event(asoc, 4296 GFP_USER | __GFP_NOWARN); 4297 if (!event) 4298 return -ENOMEM; 4299 4300 asoc->stream.si->enqueue_event(&asoc->ulpq, event); 4301 } 4302 } 4303 4304 return 0; 4305 } 4306 4307 static int sctp_setsockopt_event(struct sock *sk, struct sctp_event *param, 4308 unsigned int optlen) 4309 { 4310 struct sctp_sock *sp = sctp_sk(sk); 4311 struct sctp_association *asoc; 4312 int retval = 0; 4313 4314 if (optlen < sizeof(*param)) 4315 return -EINVAL; 4316 4317 if (param->se_type < SCTP_SN_TYPE_BASE || 4318 param->se_type > SCTP_SN_TYPE_MAX) 4319 return -EINVAL; 4320 4321 asoc = sctp_id2assoc(sk, param->se_assoc_id); 4322 if (!asoc && param->se_assoc_id > SCTP_ALL_ASSOC && 4323 sctp_style(sk, UDP)) 4324 return -EINVAL; 4325 4326 if (asoc) 4327 return sctp_assoc_ulpevent_type_set(param, asoc); 4328 4329 if (sctp_style(sk, TCP)) 4330 param->se_assoc_id = SCTP_FUTURE_ASSOC; 4331 4332 if (param->se_assoc_id == SCTP_FUTURE_ASSOC || 4333 param->se_assoc_id == SCTP_ALL_ASSOC) 4334 sctp_ulpevent_type_set(&sp->subscribe, 4335 param->se_type, param->se_on); 4336 4337 if (param->se_assoc_id == SCTP_CURRENT_ASSOC || 4338 param->se_assoc_id == SCTP_ALL_ASSOC) { 4339 list_for_each_entry(asoc, &sp->ep->asocs, asocs) { 4340 int ret = sctp_assoc_ulpevent_type_set(param, asoc); 4341 4342 if (ret && !retval) 4343 retval = ret; 4344 } 4345 } 4346 4347 return retval; 4348 } 4349 4350 static int sctp_setsockopt_asconf_supported(struct sock *sk, 4351 struct sctp_assoc_value *params, 4352 unsigned int optlen) 4353 { 4354 struct sctp_association *asoc; 4355 struct sctp_endpoint *ep; 4356 int retval = -EINVAL; 4357 4358 if (optlen != sizeof(*params)) 4359 goto out; 4360 4361 asoc = sctp_id2assoc(sk, params->assoc_id); 4362 if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC && 4363 sctp_style(sk, UDP)) 4364 goto out; 4365 4366 ep = sctp_sk(sk)->ep; 4367 ep->asconf_enable = !!params->assoc_value; 4368 4369 if (ep->asconf_enable && ep->auth_enable) { 4370 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF); 4371 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK); 4372 } 4373 4374 retval = 0; 4375 4376 out: 4377 return retval; 4378 } 4379 4380 static int sctp_setsockopt_auth_supported(struct sock *sk, 4381 struct sctp_assoc_value *params, 4382 unsigned int optlen) 4383 { 4384 struct sctp_association *asoc; 4385 struct sctp_endpoint *ep; 4386 int retval = -EINVAL; 4387 4388 if (optlen != sizeof(*params)) 4389 goto out; 4390 4391 asoc = sctp_id2assoc(sk, params->assoc_id); 4392 if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC && 4393 sctp_style(sk, UDP)) 4394 goto out; 4395 4396 ep = sctp_sk(sk)->ep; 4397 if (params->assoc_value) { 4398 retval = sctp_auth_init(ep, GFP_KERNEL); 4399 if (retval) 4400 goto out; 4401 if (ep->asconf_enable) { 4402 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF); 4403 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK); 4404 } 4405 } 4406 4407 ep->auth_enable = !!params->assoc_value; 4408 retval = 0; 4409 4410 out: 4411 return retval; 4412 } 4413 4414 static int sctp_setsockopt_ecn_supported(struct sock *sk, 4415 struct sctp_assoc_value *params, 4416 unsigned int optlen) 4417 { 4418 struct sctp_association *asoc; 4419 int retval = -EINVAL; 4420 4421 if (optlen != sizeof(*params)) 4422 goto out; 4423 4424 asoc = sctp_id2assoc(sk, params->assoc_id); 4425 if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC && 4426 sctp_style(sk, UDP)) 4427 goto out; 4428 4429 sctp_sk(sk)->ep->ecn_enable = !!params->assoc_value; 4430 retval = 0; 4431 4432 out: 4433 return retval; 4434 } 4435 4436 static int sctp_setsockopt_pf_expose(struct sock *sk, 4437 struct sctp_assoc_value *params, 4438 unsigned int optlen) 4439 { 4440 struct sctp_association *asoc; 4441 int retval = -EINVAL; 4442 4443 if (optlen != sizeof(*params)) 4444 goto out; 4445 4446 if (params->assoc_value > SCTP_PF_EXPOSE_MAX) 4447 goto out; 4448 4449 asoc = sctp_id2assoc(sk, params->assoc_id); 4450 if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC && 4451 sctp_style(sk, UDP)) 4452 goto out; 4453 4454 if (asoc) 4455 asoc->pf_expose = params->assoc_value; 4456 else 4457 sctp_sk(sk)->pf_expose = params->assoc_value; 4458 retval = 0; 4459 4460 out: 4461 return retval; 4462 } 4463 4464 static int sctp_setsockopt_encap_port(struct sock *sk, 4465 struct sctp_udpencaps *encap, 4466 unsigned int optlen) 4467 { 4468 struct sctp_association *asoc; 4469 struct sctp_transport *t; 4470 __be16 encap_port; 4471 4472 if (optlen != sizeof(*encap)) 4473 return -EINVAL; 4474 4475 /* If an address other than INADDR_ANY is specified, and 4476 * no transport is found, then the request is invalid. 4477 */ 4478 encap_port = (__force __be16)encap->sue_port; 4479 if (!sctp_is_any(sk, (union sctp_addr *)&encap->sue_address)) { 4480 t = sctp_addr_id2transport(sk, &encap->sue_address, 4481 encap->sue_assoc_id); 4482 if (!t) 4483 return -EINVAL; 4484 4485 t->encap_port = encap_port; 4486 return 0; 4487 } 4488 4489 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the 4490 * socket is a one to many style socket, and an association 4491 * was not found, then the id was invalid. 4492 */ 4493 asoc = sctp_id2assoc(sk, encap->sue_assoc_id); 4494 if (!asoc && encap->sue_assoc_id != SCTP_FUTURE_ASSOC && 4495 sctp_style(sk, UDP)) 4496 return -EINVAL; 4497 4498 /* If changes are for association, also apply encap_port to 4499 * each transport. 4500 */ 4501 if (asoc) { 4502 list_for_each_entry(t, &asoc->peer.transport_addr_list, 4503 transports) 4504 t->encap_port = encap_port; 4505 4506 asoc->encap_port = encap_port; 4507 return 0; 4508 } 4509 4510 sctp_sk(sk)->encap_port = encap_port; 4511 return 0; 4512 } 4513 4514 static int sctp_setsockopt_probe_interval(struct sock *sk, 4515 struct sctp_probeinterval *params, 4516 unsigned int optlen) 4517 { 4518 struct sctp_association *asoc; 4519 struct sctp_transport *t; 4520 __u32 probe_interval; 4521 4522 if (optlen != sizeof(*params)) 4523 return -EINVAL; 4524 4525 probe_interval = params->spi_interval; 4526 if (probe_interval && probe_interval < SCTP_PROBE_TIMER_MIN) 4527 return -EINVAL; 4528 4529 /* If an address other than INADDR_ANY is specified, and 4530 * no transport is found, then the request is invalid. 4531 */ 4532 if (!sctp_is_any(sk, (union sctp_addr *)¶ms->spi_address)) { 4533 t = sctp_addr_id2transport(sk, ¶ms->spi_address, 4534 params->spi_assoc_id); 4535 if (!t) 4536 return -EINVAL; 4537 4538 t->probe_interval = msecs_to_jiffies(probe_interval); 4539 sctp_transport_pl_reset(t); 4540 return 0; 4541 } 4542 4543 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the 4544 * socket is a one to many style socket, and an association 4545 * was not found, then the id was invalid. 4546 */ 4547 asoc = sctp_id2assoc(sk, params->spi_assoc_id); 4548 if (!asoc && params->spi_assoc_id != SCTP_FUTURE_ASSOC && 4549 sctp_style(sk, UDP)) 4550 return -EINVAL; 4551 4552 /* If changes are for association, also apply probe_interval to 4553 * each transport. 4554 */ 4555 if (asoc) { 4556 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) { 4557 t->probe_interval = msecs_to_jiffies(probe_interval); 4558 sctp_transport_pl_reset(t); 4559 } 4560 4561 asoc->probe_interval = msecs_to_jiffies(probe_interval); 4562 return 0; 4563 } 4564 4565 sctp_sk(sk)->probe_interval = probe_interval; 4566 return 0; 4567 } 4568 4569 /* API 6.2 setsockopt(), getsockopt() 4570 * 4571 * Applications use setsockopt() and getsockopt() to set or retrieve 4572 * socket options. Socket options are used to change the default 4573 * behavior of sockets calls. They are described in Section 7. 4574 * 4575 * The syntax is: 4576 * 4577 * ret = getsockopt(int sd, int level, int optname, void __user *optval, 4578 * int __user *optlen); 4579 * ret = setsockopt(int sd, int level, int optname, const void __user *optval, 4580 * int optlen); 4581 * 4582 * sd - the socket descript. 4583 * level - set to IPPROTO_SCTP for all SCTP options. 4584 * optname - the option name. 4585 * optval - the buffer to store the value of the option. 4586 * optlen - the size of the buffer. 4587 */ 4588 static int sctp_setsockopt(struct sock *sk, int level, int optname, 4589 sockptr_t optval, unsigned int optlen) 4590 { 4591 void *kopt = NULL; 4592 int retval = 0; 4593 4594 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); 4595 4596 /* I can hardly begin to describe how wrong this is. This is 4597 * so broken as to be worse than useless. The API draft 4598 * REALLY is NOT helpful here... I am not convinced that the 4599 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP 4600 * are at all well-founded. 4601 */ 4602 if (level != SOL_SCTP) { 4603 struct sctp_af *af = sctp_sk(sk)->pf->af; 4604 4605 return af->setsockopt(sk, level, optname, optval, optlen); 4606 } 4607 4608 if (optlen > 0) { 4609 /* Trim it to the biggest size sctp sockopt may need if necessary */ 4610 optlen = min_t(unsigned int, optlen, 4611 PAGE_ALIGN(struct_size_t(struct sctp_reset_streams, 4612 srs_stream_list, USHRT_MAX))); 4613 kopt = memdup_sockptr(optval, optlen); 4614 if (IS_ERR(kopt)) 4615 return PTR_ERR(kopt); 4616 } 4617 4618 lock_sock(sk); 4619 4620 switch (optname) { 4621 case SCTP_SOCKOPT_BINDX_ADD: 4622 /* 'optlen' is the size of the addresses buffer. */ 4623 retval = sctp_setsockopt_bindx(sk, kopt, optlen, 4624 SCTP_BINDX_ADD_ADDR); 4625 break; 4626 4627 case SCTP_SOCKOPT_BINDX_REM: 4628 /* 'optlen' is the size of the addresses buffer. */ 4629 retval = sctp_setsockopt_bindx(sk, kopt, optlen, 4630 SCTP_BINDX_REM_ADDR); 4631 break; 4632 4633 case SCTP_SOCKOPT_CONNECTX_OLD: 4634 /* 'optlen' is the size of the addresses buffer. */ 4635 retval = sctp_setsockopt_connectx_old(sk, kopt, optlen); 4636 break; 4637 4638 case SCTP_SOCKOPT_CONNECTX: 4639 /* 'optlen' is the size of the addresses buffer. */ 4640 retval = sctp_setsockopt_connectx(sk, kopt, optlen); 4641 break; 4642 4643 case SCTP_DISABLE_FRAGMENTS: 4644 retval = sctp_setsockopt_disable_fragments(sk, kopt, optlen); 4645 break; 4646 4647 case SCTP_EVENTS: 4648 retval = sctp_setsockopt_events(sk, kopt, optlen); 4649 break; 4650 4651 case SCTP_AUTOCLOSE: 4652 retval = sctp_setsockopt_autoclose(sk, kopt, optlen); 4653 break; 4654 4655 case SCTP_PEER_ADDR_PARAMS: 4656 retval = sctp_setsockopt_peer_addr_params(sk, kopt, optlen); 4657 break; 4658 4659 case SCTP_DELAYED_SACK: 4660 retval = sctp_setsockopt_delayed_ack(sk, kopt, optlen); 4661 break; 4662 case SCTP_PARTIAL_DELIVERY_POINT: 4663 retval = sctp_setsockopt_partial_delivery_point(sk, kopt, optlen); 4664 break; 4665 4666 case SCTP_INITMSG: 4667 retval = sctp_setsockopt_initmsg(sk, kopt, optlen); 4668 break; 4669 case SCTP_DEFAULT_SEND_PARAM: 4670 retval = sctp_setsockopt_default_send_param(sk, kopt, optlen); 4671 break; 4672 case SCTP_DEFAULT_SNDINFO: 4673 retval = sctp_setsockopt_default_sndinfo(sk, kopt, optlen); 4674 break; 4675 case SCTP_PRIMARY_ADDR: 4676 retval = sctp_setsockopt_primary_addr(sk, kopt, optlen); 4677 break; 4678 case SCTP_SET_PEER_PRIMARY_ADDR: 4679 retval = sctp_setsockopt_peer_primary_addr(sk, kopt, optlen); 4680 break; 4681 case SCTP_NODELAY: 4682 retval = sctp_setsockopt_nodelay(sk, kopt, optlen); 4683 break; 4684 case SCTP_RTOINFO: 4685 retval = sctp_setsockopt_rtoinfo(sk, kopt, optlen); 4686 break; 4687 case SCTP_ASSOCINFO: 4688 retval = sctp_setsockopt_associnfo(sk, kopt, optlen); 4689 break; 4690 case SCTP_I_WANT_MAPPED_V4_ADDR: 4691 retval = sctp_setsockopt_mappedv4(sk, kopt, optlen); 4692 break; 4693 case SCTP_MAXSEG: 4694 retval = sctp_setsockopt_maxseg(sk, kopt, optlen); 4695 break; 4696 case SCTP_ADAPTATION_LAYER: 4697 retval = sctp_setsockopt_adaptation_layer(sk, kopt, optlen); 4698 break; 4699 case SCTP_CONTEXT: 4700 retval = sctp_setsockopt_context(sk, kopt, optlen); 4701 break; 4702 case SCTP_FRAGMENT_INTERLEAVE: 4703 retval = sctp_setsockopt_fragment_interleave(sk, kopt, optlen); 4704 break; 4705 case SCTP_MAX_BURST: 4706 retval = sctp_setsockopt_maxburst(sk, kopt, optlen); 4707 break; 4708 case SCTP_AUTH_CHUNK: 4709 retval = sctp_setsockopt_auth_chunk(sk, kopt, optlen); 4710 break; 4711 case SCTP_HMAC_IDENT: 4712 retval = sctp_setsockopt_hmac_ident(sk, kopt, optlen); 4713 break; 4714 case SCTP_AUTH_KEY: 4715 retval = sctp_setsockopt_auth_key(sk, kopt, optlen); 4716 break; 4717 case SCTP_AUTH_ACTIVE_KEY: 4718 retval = sctp_setsockopt_active_key(sk, kopt, optlen); 4719 break; 4720 case SCTP_AUTH_DELETE_KEY: 4721 retval = sctp_setsockopt_del_key(sk, kopt, optlen); 4722 break; 4723 case SCTP_AUTH_DEACTIVATE_KEY: 4724 retval = sctp_setsockopt_deactivate_key(sk, kopt, optlen); 4725 break; 4726 case SCTP_AUTO_ASCONF: 4727 retval = sctp_setsockopt_auto_asconf(sk, kopt, optlen); 4728 break; 4729 case SCTP_PEER_ADDR_THLDS: 4730 retval = sctp_setsockopt_paddr_thresholds(sk, kopt, optlen, 4731 false); 4732 break; 4733 case SCTP_PEER_ADDR_THLDS_V2: 4734 retval = sctp_setsockopt_paddr_thresholds(sk, kopt, optlen, 4735 true); 4736 break; 4737 case SCTP_RECVRCVINFO: 4738 retval = sctp_setsockopt_recvrcvinfo(sk, kopt, optlen); 4739 break; 4740 case SCTP_RECVNXTINFO: 4741 retval = sctp_setsockopt_recvnxtinfo(sk, kopt, optlen); 4742 break; 4743 case SCTP_PR_SUPPORTED: 4744 retval = sctp_setsockopt_pr_supported(sk, kopt, optlen); 4745 break; 4746 case SCTP_DEFAULT_PRINFO: 4747 retval = sctp_setsockopt_default_prinfo(sk, kopt, optlen); 4748 break; 4749 case SCTP_RECONFIG_SUPPORTED: 4750 retval = sctp_setsockopt_reconfig_supported(sk, kopt, optlen); 4751 break; 4752 case SCTP_ENABLE_STREAM_RESET: 4753 retval = sctp_setsockopt_enable_strreset(sk, kopt, optlen); 4754 break; 4755 case SCTP_RESET_STREAMS: 4756 retval = sctp_setsockopt_reset_streams(sk, kopt, optlen); 4757 break; 4758 case SCTP_RESET_ASSOC: 4759 retval = sctp_setsockopt_reset_assoc(sk, kopt, optlen); 4760 break; 4761 case SCTP_ADD_STREAMS: 4762 retval = sctp_setsockopt_add_streams(sk, kopt, optlen); 4763 break; 4764 case SCTP_STREAM_SCHEDULER: 4765 retval = sctp_setsockopt_scheduler(sk, kopt, optlen); 4766 break; 4767 case SCTP_STREAM_SCHEDULER_VALUE: 4768 retval = sctp_setsockopt_scheduler_value(sk, kopt, optlen); 4769 break; 4770 case SCTP_INTERLEAVING_SUPPORTED: 4771 retval = sctp_setsockopt_interleaving_supported(sk, kopt, 4772 optlen); 4773 break; 4774 case SCTP_REUSE_PORT: 4775 retval = sctp_setsockopt_reuse_port(sk, kopt, optlen); 4776 break; 4777 case SCTP_EVENT: 4778 retval = sctp_setsockopt_event(sk, kopt, optlen); 4779 break; 4780 case SCTP_ASCONF_SUPPORTED: 4781 retval = sctp_setsockopt_asconf_supported(sk, kopt, optlen); 4782 break; 4783 case SCTP_AUTH_SUPPORTED: 4784 retval = sctp_setsockopt_auth_supported(sk, kopt, optlen); 4785 break; 4786 case SCTP_ECN_SUPPORTED: 4787 retval = sctp_setsockopt_ecn_supported(sk, kopt, optlen); 4788 break; 4789 case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE: 4790 retval = sctp_setsockopt_pf_expose(sk, kopt, optlen); 4791 break; 4792 case SCTP_REMOTE_UDP_ENCAPS_PORT: 4793 retval = sctp_setsockopt_encap_port(sk, kopt, optlen); 4794 break; 4795 case SCTP_PLPMTUD_PROBE_INTERVAL: 4796 retval = sctp_setsockopt_probe_interval(sk, kopt, optlen); 4797 break; 4798 default: 4799 retval = -ENOPROTOOPT; 4800 break; 4801 } 4802 4803 release_sock(sk); 4804 kfree(kopt); 4805 return retval; 4806 } 4807 4808 /* API 3.1.6 connect() - UDP Style Syntax 4809 * 4810 * An application may use the connect() call in the UDP model to initiate an 4811 * association without sending data. 4812 * 4813 * The syntax is: 4814 * 4815 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len); 4816 * 4817 * sd: the socket descriptor to have a new association added to. 4818 * 4819 * nam: the address structure (either struct sockaddr_in or struct 4820 * sockaddr_in6 defined in RFC2553 [7]). 4821 * 4822 * len: the size of the address. 4823 */ 4824 static int sctp_connect(struct sock *sk, struct sockaddr *addr, 4825 int addr_len, int flags) 4826 { 4827 struct sctp_af *af; 4828 int err = -EINVAL; 4829 4830 lock_sock(sk); 4831 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk, 4832 addr, addr_len); 4833 4834 /* Validate addr_len before calling common connect/connectx routine. */ 4835 af = sctp_get_af_specific(addr->sa_family); 4836 if (af && addr_len >= af->sockaddr_len) 4837 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL); 4838 4839 release_sock(sk); 4840 return err; 4841 } 4842 4843 int sctp_inet_connect(struct socket *sock, struct sockaddr_unsized *uaddr, 4844 int addr_len, int flags) 4845 { 4846 if (addr_len < sizeof(uaddr->sa_family)) 4847 return -EINVAL; 4848 4849 if (uaddr->sa_family == AF_UNSPEC) 4850 return -EOPNOTSUPP; 4851 4852 return sctp_connect(sock->sk, (struct sockaddr *)uaddr, addr_len, flags); 4853 } 4854 4855 /* Only called when shutdown a listening SCTP socket. */ 4856 static int sctp_disconnect(struct sock *sk, int flags) 4857 { 4858 if (!sctp_style(sk, TCP)) 4859 return -EOPNOTSUPP; 4860 4861 sk->sk_shutdown |= RCV_SHUTDOWN; 4862 return 0; 4863 } 4864 4865 static struct sock *sctp_clone_sock(struct sock *sk, 4866 struct sctp_association *asoc, 4867 enum sctp_socket_type type) 4868 { 4869 struct sock *newsk = sk_clone(sk, GFP_KERNEL, false); 4870 struct inet_sock *newinet; 4871 struct sctp_sock *newsp; 4872 int err = -ENOMEM; 4873 4874 if (!newsk) 4875 return ERR_PTR(err); 4876 4877 /* sk_clone() sets refcnt to 2 and increments sockets_allocated */ 4878 sock_put(newsk); 4879 sk_sockets_allocated_dec(newsk); 4880 4881 newinet = inet_sk(newsk); 4882 newsp = sctp_sk(newsk); 4883 4884 newsp->pf->to_sk_daddr(&asoc->peer.primary_addr, newsk); 4885 newinet->inet_dport = htons(asoc->peer.port); 4886 atomic_set(&newinet->inet_id, get_random_u16()); 4887 4888 inet_set_bit(MC_LOOP, newsk); 4889 newinet->mc_ttl = 1; 4890 newinet->mc_index = 0; 4891 newinet->mc_list = NULL; 4892 4893 #if IS_ENABLED(CONFIG_IPV6) 4894 if (sk->sk_family == AF_INET6) { 4895 struct ipv6_pinfo *newnp; 4896 4897 newinet->pinet6 = &((struct sctp6_sock *)newsk)->inet6; 4898 newinet->ipv6_fl_list = NULL; 4899 4900 newnp = inet6_sk(newsk); 4901 memcpy(newnp, inet6_sk(sk), sizeof(struct ipv6_pinfo)); 4902 newnp->ipv6_mc_list = NULL; 4903 newnp->ipv6_ac_list = NULL; 4904 } 4905 #endif 4906 4907 newsp->pf->copy_ip_options(sk, newsk); 4908 4909 newsp->do_auto_asconf = 0; 4910 skb_queue_head_init(&newsp->pd_lobby); 4911 4912 newsp->ep = sctp_endpoint_new(newsk, GFP_KERNEL); 4913 if (!newsp->ep) 4914 goto out_release; 4915 4916 SCTP_DBG_OBJCNT_INC(sock); 4917 sk_sockets_allocated_inc(newsk); 4918 sock_prot_inuse_add(sock_net(sk), newsk->sk_prot, 1); 4919 4920 err = sctp_sock_migrate(sk, newsk, asoc, type); 4921 if (err) 4922 goto out_release; 4923 4924 /* Set newsk security attributes from original sk and connection 4925 * security attribute from asoc. 4926 */ 4927 security_sctp_sk_clone(asoc, sk, newsk); 4928 4929 return newsk; 4930 4931 out_release: 4932 sk_common_release(newsk); 4933 return ERR_PTR(err); 4934 } 4935 4936 /* 4.1.4 accept() - TCP Style Syntax 4937 * 4938 * Applications use accept() call to remove an established SCTP 4939 * association from the accept queue of the endpoint. A new socket 4940 * descriptor will be returned from accept() to represent the newly 4941 * formed association. 4942 */ 4943 static struct sock *sctp_accept(struct sock *sk, struct proto_accept_arg *arg) 4944 { 4945 struct sctp_association *asoc; 4946 struct sock *newsk = NULL; 4947 int error = 0; 4948 long timeo; 4949 4950 lock_sock(sk); 4951 4952 if (!sctp_style(sk, TCP)) { 4953 error = -EOPNOTSUPP; 4954 goto out; 4955 } 4956 4957 if (!sctp_sstate(sk, LISTENING) || 4958 (sk->sk_shutdown & RCV_SHUTDOWN)) { 4959 error = -EINVAL; 4960 goto out; 4961 } 4962 4963 timeo = sock_rcvtimeo(sk, arg->flags & O_NONBLOCK); 4964 4965 error = sctp_wait_for_accept(sk, timeo); 4966 if (error) 4967 goto out; 4968 4969 /* We treat the list of associations on the endpoint as the accept 4970 * queue and pick the first association on the list. 4971 */ 4972 asoc = list_entry(sctp_sk(sk)->ep->asocs.next, 4973 struct sctp_association, asocs); 4974 4975 newsk = sctp_clone_sock(sk, asoc, SCTP_SOCKET_TCP); 4976 if (IS_ERR(newsk)) { 4977 error = PTR_ERR(newsk); 4978 newsk = NULL; 4979 } 4980 4981 out: 4982 release_sock(sk); 4983 arg->err = error; 4984 return newsk; 4985 } 4986 4987 /* The SCTP ioctl handler. */ 4988 static int sctp_ioctl(struct sock *sk, int cmd, int *karg) 4989 { 4990 int rc = -ENOTCONN; 4991 4992 lock_sock(sk); 4993 4994 /* 4995 * SEQPACKET-style sockets in LISTENING state are valid, for 4996 * SCTP, so only discard TCP-style sockets in LISTENING state. 4997 */ 4998 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) 4999 goto out; 5000 5001 switch (cmd) { 5002 case SIOCINQ: { 5003 struct sk_buff *skb; 5004 *karg = 0; 5005 5006 skb = skb_peek(&sk->sk_receive_queue); 5007 if (skb != NULL) { 5008 /* 5009 * We will only return the amount of this packet since 5010 * that is all that will be read. 5011 */ 5012 *karg = skb->len; 5013 } 5014 rc = 0; 5015 break; 5016 } 5017 default: 5018 rc = -ENOIOCTLCMD; 5019 break; 5020 } 5021 out: 5022 release_sock(sk); 5023 return rc; 5024 } 5025 5026 /* This is the function which gets called during socket creation to 5027 * initialized the SCTP-specific portion of the sock. 5028 * The sock structure should already be zero-filled memory. 5029 */ 5030 static int sctp_init_sock(struct sock *sk) 5031 { 5032 struct net *net = sock_net(sk); 5033 struct sctp_sock *sp; 5034 5035 pr_debug("%s: sk:%p\n", __func__, sk); 5036 5037 sp = sctp_sk(sk); 5038 5039 /* Initialize the SCTP per socket area. */ 5040 switch (sk->sk_type) { 5041 case SOCK_SEQPACKET: 5042 sp->type = SCTP_SOCKET_UDP; 5043 break; 5044 case SOCK_STREAM: 5045 sp->type = SCTP_SOCKET_TCP; 5046 break; 5047 default: 5048 return -ESOCKTNOSUPPORT; 5049 } 5050 5051 sk->sk_gso_type = SKB_GSO_SCTP; 5052 5053 /* Initialize default send parameters. These parameters can be 5054 * modified with the SCTP_DEFAULT_SEND_PARAM socket option. 5055 */ 5056 sp->default_stream = 0; 5057 sp->default_ppid = 0; 5058 sp->default_flags = 0; 5059 sp->default_context = 0; 5060 sp->default_timetolive = 0; 5061 5062 sp->default_rcv_context = 0; 5063 sp->max_burst = net->sctp.max_burst; 5064 5065 sp->cookie_auth_enable = net->sctp.cookie_auth_enable; 5066 5067 /* Initialize default setup parameters. These parameters 5068 * can be modified with the SCTP_INITMSG socket option or 5069 * overridden by the SCTP_INIT CMSG. 5070 */ 5071 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams; 5072 sp->initmsg.sinit_max_instreams = sctp_max_instreams; 5073 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init; 5074 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max; 5075 5076 /* Initialize default RTO related parameters. These parameters can 5077 * be modified for with the SCTP_RTOINFO socket option. 5078 */ 5079 sp->rtoinfo.srto_initial = net->sctp.rto_initial; 5080 sp->rtoinfo.srto_max = net->sctp.rto_max; 5081 sp->rtoinfo.srto_min = net->sctp.rto_min; 5082 5083 /* Initialize default association related parameters. These parameters 5084 * can be modified with the SCTP_ASSOCINFO socket option. 5085 */ 5086 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association; 5087 sp->assocparams.sasoc_number_peer_destinations = 0; 5088 sp->assocparams.sasoc_peer_rwnd = 0; 5089 sp->assocparams.sasoc_local_rwnd = 0; 5090 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life; 5091 5092 /* Initialize default event subscriptions. By default, all the 5093 * options are off. 5094 */ 5095 sp->subscribe = 0; 5096 5097 /* Default Peer Address Parameters. These defaults can 5098 * be modified via SCTP_PEER_ADDR_PARAMS 5099 */ 5100 sp->hbinterval = net->sctp.hb_interval; 5101 sp->udp_port = htons(net->sctp.udp_port); 5102 sp->encap_port = htons(net->sctp.encap_port); 5103 sp->pathmaxrxt = net->sctp.max_retrans_path; 5104 sp->pf_retrans = net->sctp.pf_retrans; 5105 sp->ps_retrans = net->sctp.ps_retrans; 5106 sp->pf_expose = net->sctp.pf_expose; 5107 sp->pathmtu = 0; /* allow default discovery */ 5108 sp->sackdelay = net->sctp.sack_timeout; 5109 sp->sackfreq = 2; 5110 sp->param_flags = SPP_HB_ENABLE | 5111 SPP_PMTUD_ENABLE | 5112 SPP_SACKDELAY_ENABLE; 5113 sp->default_ss = SCTP_SS_DEFAULT; 5114 5115 /* If enabled no SCTP message fragmentation will be performed. 5116 * Configure through SCTP_DISABLE_FRAGMENTS socket option. 5117 */ 5118 sp->disable_fragments = 0; 5119 5120 /* Enable Nagle algorithm by default. */ 5121 sp->nodelay = 0; 5122 5123 sp->recvrcvinfo = 0; 5124 sp->recvnxtinfo = 0; 5125 5126 /* Enable by default. */ 5127 sp->v4mapped = 1; 5128 5129 /* Auto-close idle associations after the configured 5130 * number of seconds. A value of 0 disables this 5131 * feature. Configure through the SCTP_AUTOCLOSE socket option, 5132 * for UDP-style sockets only. 5133 */ 5134 sp->autoclose = 0; 5135 5136 /* User specified fragmentation limit. */ 5137 sp->user_frag = 0; 5138 5139 sp->adaptation_ind = 0; 5140 5141 sp->pf = sctp_get_pf_specific(sk->sk_family); 5142 5143 /* Control variables for partial data delivery. */ 5144 atomic_set(&sp->pd_mode, 0); 5145 skb_queue_head_init(&sp->pd_lobby); 5146 sp->frag_interleave = 0; 5147 sp->probe_interval = net->sctp.probe_interval; 5148 5149 /* Create a per socket endpoint structure. Even if we 5150 * change the data structure relationships, this may still 5151 * be useful for storing pre-connect address information. 5152 */ 5153 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL); 5154 if (!sp->ep) 5155 return -ENOMEM; 5156 5157 sk->sk_destruct = sctp_destruct_sock; 5158 5159 SCTP_DBG_OBJCNT_INC(sock); 5160 5161 sk_sockets_allocated_inc(sk); 5162 sock_prot_inuse_add(net, sk->sk_prot, 1); 5163 5164 return 0; 5165 } 5166 5167 /* Cleanup any SCTP per socket resources. Must be called with 5168 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true 5169 */ 5170 static void sctp_destroy_sock(struct sock *sk) 5171 { 5172 struct sctp_sock *sp; 5173 5174 pr_debug("%s: sk:%p\n", __func__, sk); 5175 5176 /* Release our hold on the endpoint. */ 5177 sp = sctp_sk(sk); 5178 /* This could happen during socket init, thus we bail out 5179 * early, since the rest of the below is not setup either. 5180 */ 5181 if (sp->ep == NULL) 5182 return; 5183 5184 if (sp->do_auto_asconf) { 5185 sp->do_auto_asconf = 0; 5186 list_del(&sp->auto_asconf_list); 5187 } 5188 5189 sctp_endpoint_free(sp->ep); 5190 5191 sk_sockets_allocated_dec(sk); 5192 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 5193 SCTP_DBG_OBJCNT_DEC(sock); 5194 } 5195 5196 static void sctp_destruct_sock(struct sock *sk) 5197 { 5198 inet_sock_destruct(sk); 5199 } 5200 5201 /* API 4.1.7 shutdown() - TCP Style Syntax 5202 * int shutdown(int socket, int how); 5203 * 5204 * sd - the socket descriptor of the association to be closed. 5205 * how - Specifies the type of shutdown. The values are 5206 * as follows: 5207 * SHUT_RD 5208 * Disables further receive operations. No SCTP 5209 * protocol action is taken. 5210 * SHUT_WR 5211 * Disables further send operations, and initiates 5212 * the SCTP shutdown sequence. 5213 * SHUT_RDWR 5214 * Disables further send and receive operations 5215 * and initiates the SCTP shutdown sequence. 5216 */ 5217 static void sctp_shutdown(struct sock *sk, int how) 5218 { 5219 struct net *net = sock_net(sk); 5220 struct sctp_endpoint *ep; 5221 5222 if (!sctp_style(sk, TCP)) 5223 return; 5224 5225 ep = sctp_sk(sk)->ep; 5226 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) { 5227 struct sctp_association *asoc; 5228 5229 inet_sk_set_state(sk, SCTP_SS_CLOSING); 5230 asoc = list_entry(ep->asocs.next, 5231 struct sctp_association, asocs); 5232 sctp_primitive_SHUTDOWN(net, asoc, NULL); 5233 } 5234 } 5235 5236 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc, 5237 struct sctp_info *info) 5238 { 5239 struct sctp_transport *prim; 5240 struct list_head *pos; 5241 int mask; 5242 5243 memset(info, 0, sizeof(*info)); 5244 if (!asoc) { 5245 struct sctp_sock *sp = sctp_sk(sk); 5246 5247 info->sctpi_s_autoclose = sp->autoclose; 5248 info->sctpi_s_adaptation_ind = sp->adaptation_ind; 5249 info->sctpi_s_pd_point = sp->pd_point; 5250 info->sctpi_s_nodelay = sp->nodelay; 5251 info->sctpi_s_disable_fragments = sp->disable_fragments; 5252 info->sctpi_s_v4mapped = sp->v4mapped; 5253 info->sctpi_s_frag_interleave = sp->frag_interleave; 5254 info->sctpi_s_type = sp->type; 5255 5256 return 0; 5257 } 5258 5259 info->sctpi_tag = asoc->c.my_vtag; 5260 info->sctpi_state = asoc->state; 5261 info->sctpi_rwnd = asoc->a_rwnd; 5262 info->sctpi_unackdata = asoc->unack_data; 5263 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); 5264 info->sctpi_instrms = asoc->stream.incnt; 5265 info->sctpi_outstrms = asoc->stream.outcnt; 5266 list_for_each(pos, &asoc->base.inqueue.in_chunk_list) 5267 info->sctpi_inqueue++; 5268 list_for_each(pos, &asoc->outqueue.out_chunk_list) 5269 info->sctpi_outqueue++; 5270 info->sctpi_overall_error = asoc->overall_error_count; 5271 info->sctpi_max_burst = asoc->max_burst; 5272 info->sctpi_maxseg = asoc->frag_point; 5273 info->sctpi_peer_rwnd = asoc->peer.rwnd; 5274 info->sctpi_peer_tag = asoc->c.peer_vtag; 5275 5276 mask = asoc->peer.intl_capable << 1; 5277 mask = (mask | asoc->peer.ecn_capable) << 1; 5278 mask = (mask | asoc->peer.ipv4_address) << 1; 5279 mask = (mask | asoc->peer.ipv6_address) << 1; 5280 mask = (mask | asoc->peer.reconf_capable) << 1; 5281 mask = (mask | asoc->peer.asconf_capable) << 1; 5282 mask = (mask | asoc->peer.prsctp_capable) << 1; 5283 mask = (mask | asoc->peer.auth_capable); 5284 info->sctpi_peer_capable = mask; 5285 mask = asoc->peer.sack_needed << 1; 5286 mask = (mask | asoc->peer.sack_generation) << 1; 5287 mask = (mask | asoc->peer.zero_window_announced); 5288 info->sctpi_peer_sack = mask; 5289 5290 info->sctpi_isacks = asoc->stats.isacks; 5291 info->sctpi_osacks = asoc->stats.osacks; 5292 info->sctpi_opackets = asoc->stats.opackets; 5293 info->sctpi_ipackets = asoc->stats.ipackets; 5294 info->sctpi_rtxchunks = asoc->stats.rtxchunks; 5295 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns; 5296 info->sctpi_idupchunks = asoc->stats.idupchunks; 5297 info->sctpi_gapcnt = asoc->stats.gapcnt; 5298 info->sctpi_ouodchunks = asoc->stats.ouodchunks; 5299 info->sctpi_iuodchunks = asoc->stats.iuodchunks; 5300 info->sctpi_oodchunks = asoc->stats.oodchunks; 5301 info->sctpi_iodchunks = asoc->stats.iodchunks; 5302 info->sctpi_octrlchunks = asoc->stats.octrlchunks; 5303 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks; 5304 5305 prim = asoc->peer.primary_path; 5306 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr)); 5307 info->sctpi_p_state = prim->state; 5308 info->sctpi_p_cwnd = prim->cwnd; 5309 info->sctpi_p_srtt = prim->srtt; 5310 info->sctpi_p_rto = jiffies_to_msecs(prim->rto); 5311 info->sctpi_p_hbinterval = prim->hbinterval; 5312 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt; 5313 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay); 5314 info->sctpi_p_ssthresh = prim->ssthresh; 5315 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked; 5316 info->sctpi_p_flight_size = prim->flight_size; 5317 info->sctpi_p_error = prim->error_count; 5318 5319 return 0; 5320 } 5321 EXPORT_SYMBOL_GPL(sctp_get_sctp_info); 5322 5323 /* use callback to avoid exporting the core structure */ 5324 void sctp_transport_walk_start(struct rhashtable_iter *iter) __acquires(RCU) 5325 { 5326 rhltable_walk_enter(&sctp_transport_hashtable, iter); 5327 5328 rhashtable_walk_start(iter); 5329 } 5330 5331 void sctp_transport_walk_stop(struct rhashtable_iter *iter) __releases(RCU) 5332 { 5333 rhashtable_walk_stop(iter); 5334 rhashtable_walk_exit(iter); 5335 } 5336 5337 struct sctp_transport *sctp_transport_get_next(struct net *net, 5338 struct rhashtable_iter *iter) 5339 { 5340 struct sctp_transport *t; 5341 5342 t = rhashtable_walk_next(iter); 5343 for (; t; t = rhashtable_walk_next(iter)) { 5344 if (IS_ERR(t)) { 5345 if (PTR_ERR(t) == -EAGAIN) 5346 continue; 5347 break; 5348 } 5349 5350 if (!sctp_transport_hold(t)) 5351 continue; 5352 5353 if (net_eq(t->asoc->base.net, net) && 5354 t->asoc->peer.primary_path == t) 5355 break; 5356 5357 sctp_transport_put(t); 5358 } 5359 5360 return t; 5361 } 5362 5363 struct sctp_transport *sctp_transport_get_idx(struct net *net, 5364 struct rhashtable_iter *iter, 5365 int pos) 5366 { 5367 struct sctp_transport *t; 5368 5369 if (!pos) 5370 return SEQ_START_TOKEN; 5371 5372 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) { 5373 if (!--pos) 5374 break; 5375 sctp_transport_put(t); 5376 } 5377 5378 return t; 5379 } 5380 5381 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *), 5382 struct net *net, int *pos, void *p) { 5383 int err, hash = 0, idx = 0, start; 5384 struct sctp_hashbucket *head; 5385 struct sctp_endpoint *ep; 5386 5387 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize; 5388 hash++, head++) { 5389 start = idx; 5390 again: 5391 read_lock_bh(&head->lock); 5392 sctp_for_each_hentry(ep, &head->chain) { 5393 if (sock_net(ep->base.sk) != net) 5394 continue; 5395 if (idx++ >= *pos) { 5396 sctp_endpoint_hold(ep); 5397 break; 5398 } 5399 } 5400 read_unlock_bh(&head->lock); 5401 5402 if (ep) { 5403 err = cb(ep, p); 5404 sctp_endpoint_put(ep); 5405 if (err) 5406 return err; 5407 (*pos)++; 5408 5409 idx = start; 5410 goto again; 5411 } 5412 } 5413 5414 return 0; 5415 } 5416 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint); 5417 5418 int sctp_transport_lookup_process(sctp_callback_t cb, struct net *net, 5419 const union sctp_addr *laddr, 5420 const union sctp_addr *paddr, void *p, int dif) 5421 { 5422 struct sctp_transport *transport; 5423 struct sctp_endpoint *ep; 5424 int err = -ENOENT; 5425 5426 rcu_read_lock(); 5427 transport = sctp_addrs_lookup_transport(net, laddr, paddr, dif, dif); 5428 if (!transport) { 5429 rcu_read_unlock(); 5430 return err; 5431 } 5432 ep = transport->asoc->ep; 5433 if (!sctp_endpoint_hold(ep)) { /* asoc can be peeled off */ 5434 sctp_transport_put(transport); 5435 rcu_read_unlock(); 5436 return err; 5437 } 5438 rcu_read_unlock(); 5439 5440 err = cb(ep, transport, p); 5441 sctp_endpoint_put(ep); 5442 sctp_transport_put(transport); 5443 return err; 5444 } 5445 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process); 5446 5447 int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done, 5448 struct net *net, int *pos, void *p) 5449 { 5450 struct rhashtable_iter hti; 5451 struct sctp_transport *tsp; 5452 struct sctp_endpoint *ep; 5453 int ret; 5454 5455 again: 5456 ret = 0; 5457 sctp_transport_walk_start(&hti); 5458 5459 tsp = sctp_transport_get_idx(net, &hti, *pos + 1); 5460 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) { 5461 ep = tsp->asoc->ep; 5462 if (sctp_endpoint_hold(ep)) { /* asoc can be peeled off */ 5463 ret = cb(ep, tsp, p); 5464 if (ret) 5465 break; 5466 sctp_endpoint_put(ep); 5467 } 5468 (*pos)++; 5469 sctp_transport_put(tsp); 5470 } 5471 sctp_transport_walk_stop(&hti); 5472 5473 if (ret) { 5474 if (cb_done && !cb_done(ep, tsp, p)) { 5475 (*pos)++; 5476 sctp_endpoint_put(ep); 5477 sctp_transport_put(tsp); 5478 goto again; 5479 } 5480 sctp_endpoint_put(ep); 5481 sctp_transport_put(tsp); 5482 } 5483 5484 return ret; 5485 } 5486 EXPORT_SYMBOL_GPL(sctp_transport_traverse_process); 5487 5488 /* 7.2.1 Association Status (SCTP_STATUS) 5489 5490 * Applications can retrieve current status information about an 5491 * association, including association state, peer receiver window size, 5492 * number of unacked data chunks, and number of data chunks pending 5493 * receipt. This information is read-only. 5494 */ 5495 static int sctp_getsockopt_sctp_status(struct sock *sk, int len, 5496 char __user *optval, 5497 int __user *optlen) 5498 { 5499 struct sctp_status status; 5500 struct sctp_association *asoc = NULL; 5501 struct sctp_transport *transport; 5502 sctp_assoc_t associd; 5503 int retval = 0; 5504 5505 if (len < sizeof(status)) { 5506 retval = -EINVAL; 5507 goto out; 5508 } 5509 5510 len = sizeof(status); 5511 if (copy_from_user(&status, optval, len)) { 5512 retval = -EFAULT; 5513 goto out; 5514 } 5515 5516 associd = status.sstat_assoc_id; 5517 asoc = sctp_id2assoc(sk, associd); 5518 if (!asoc) { 5519 retval = -EINVAL; 5520 goto out; 5521 } 5522 5523 transport = asoc->peer.primary_path; 5524 5525 status.sstat_assoc_id = sctp_assoc2id(asoc); 5526 status.sstat_state = sctp_assoc_to_state(asoc); 5527 status.sstat_rwnd = asoc->peer.rwnd; 5528 status.sstat_unackdata = asoc->unack_data; 5529 5530 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); 5531 status.sstat_instrms = asoc->stream.incnt; 5532 status.sstat_outstrms = asoc->stream.outcnt; 5533 status.sstat_fragmentation_point = asoc->frag_point; 5534 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); 5535 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, 5536 transport->af_specific->sockaddr_len); 5537 /* Map ipv4 address into v4-mapped-on-v6 address. */ 5538 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk), 5539 (union sctp_addr *)&status.sstat_primary.spinfo_address); 5540 status.sstat_primary.spinfo_state = transport->state; 5541 status.sstat_primary.spinfo_cwnd = transport->cwnd; 5542 status.sstat_primary.spinfo_srtt = transport->srtt; 5543 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); 5544 status.sstat_primary.spinfo_mtu = transport->pathmtu; 5545 5546 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) 5547 status.sstat_primary.spinfo_state = SCTP_ACTIVE; 5548 5549 if (put_user(len, optlen)) { 5550 retval = -EFAULT; 5551 goto out; 5552 } 5553 5554 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n", 5555 __func__, len, status.sstat_state, status.sstat_rwnd, 5556 status.sstat_assoc_id); 5557 5558 if (copy_to_user(optval, &status, len)) { 5559 retval = -EFAULT; 5560 goto out; 5561 } 5562 5563 out: 5564 return retval; 5565 } 5566 5567 5568 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) 5569 * 5570 * Applications can retrieve information about a specific peer address 5571 * of an association, including its reachability state, congestion 5572 * window, and retransmission timer values. This information is 5573 * read-only. 5574 */ 5575 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, 5576 char __user *optval, 5577 int __user *optlen) 5578 { 5579 struct sctp_paddrinfo pinfo; 5580 struct sctp_transport *transport; 5581 int retval = 0; 5582 5583 if (len < sizeof(pinfo)) { 5584 retval = -EINVAL; 5585 goto out; 5586 } 5587 5588 len = sizeof(pinfo); 5589 if (copy_from_user(&pinfo, optval, len)) { 5590 retval = -EFAULT; 5591 goto out; 5592 } 5593 5594 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, 5595 pinfo.spinfo_assoc_id); 5596 if (!transport) { 5597 retval = -EINVAL; 5598 goto out; 5599 } 5600 5601 if (transport->state == SCTP_PF && 5602 transport->asoc->pf_expose == SCTP_PF_EXPOSE_DISABLE) { 5603 retval = -EACCES; 5604 goto out; 5605 } 5606 5607 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); 5608 pinfo.spinfo_state = transport->state; 5609 pinfo.spinfo_cwnd = transport->cwnd; 5610 pinfo.spinfo_srtt = transport->srtt; 5611 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); 5612 pinfo.spinfo_mtu = transport->pathmtu; 5613 5614 if (pinfo.spinfo_state == SCTP_UNKNOWN) 5615 pinfo.spinfo_state = SCTP_ACTIVE; 5616 5617 if (put_user(len, optlen)) { 5618 retval = -EFAULT; 5619 goto out; 5620 } 5621 5622 if (copy_to_user(optval, &pinfo, len)) { 5623 retval = -EFAULT; 5624 goto out; 5625 } 5626 5627 out: 5628 return retval; 5629 } 5630 5631 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) 5632 * 5633 * This option is a on/off flag. If enabled no SCTP message 5634 * fragmentation will be performed. Instead if a message being sent 5635 * exceeds the current PMTU size, the message will NOT be sent and 5636 * instead a error will be indicated to the user. 5637 */ 5638 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, 5639 char __user *optval, int __user *optlen) 5640 { 5641 int val; 5642 5643 if (len < sizeof(int)) 5644 return -EINVAL; 5645 5646 len = sizeof(int); 5647 val = (sctp_sk(sk)->disable_fragments == 1); 5648 if (put_user(len, optlen)) 5649 return -EFAULT; 5650 if (copy_to_user(optval, &val, len)) 5651 return -EFAULT; 5652 return 0; 5653 } 5654 5655 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) 5656 * 5657 * This socket option is used to specify various notifications and 5658 * ancillary data the user wishes to receive. 5659 */ 5660 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, 5661 int __user *optlen) 5662 { 5663 struct sctp_event_subscribe subscribe; 5664 __u8 *sn_type = (__u8 *)&subscribe; 5665 int i; 5666 5667 if (len == 0) 5668 return -EINVAL; 5669 if (len > sizeof(struct sctp_event_subscribe)) 5670 len = sizeof(struct sctp_event_subscribe); 5671 if (put_user(len, optlen)) 5672 return -EFAULT; 5673 5674 for (i = 0; i < len; i++) 5675 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe, 5676 SCTP_SN_TYPE_BASE + i); 5677 5678 if (copy_to_user(optval, &subscribe, len)) 5679 return -EFAULT; 5680 5681 return 0; 5682 } 5683 5684 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) 5685 * 5686 * This socket option is applicable to the UDP-style socket only. When 5687 * set it will cause associations that are idle for more than the 5688 * specified number of seconds to automatically close. An association 5689 * being idle is defined an association that has NOT sent or received 5690 * user data. The special value of '0' indicates that no automatic 5691 * close of any associations should be performed. The option expects an 5692 * integer defining the number of seconds of idle time before an 5693 * association is closed. 5694 */ 5695 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) 5696 { 5697 /* Applicable to UDP-style socket only */ 5698 if (sctp_style(sk, TCP)) 5699 return -EOPNOTSUPP; 5700 if (len < sizeof(int)) 5701 return -EINVAL; 5702 len = sizeof(int); 5703 if (put_user(len, optlen)) 5704 return -EFAULT; 5705 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval)) 5706 return -EFAULT; 5707 return 0; 5708 } 5709 5710 /* Helper routine to branch off an association to a new socket. */ 5711 static int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, 5712 struct socket **sockp) 5713 { 5714 struct sctp_association *asoc = sctp_id2assoc(sk, id); 5715 struct socket *sock; 5716 struct sock *newsk; 5717 int err = 0; 5718 5719 /* Do not peel off from one netns to another one. */ 5720 if (!net_eq(current->nsproxy->net_ns, sock_net(sk))) 5721 return -EINVAL; 5722 5723 if (!asoc) 5724 return -EINVAL; 5725 5726 /* An association cannot be branched off from an already peeled-off 5727 * socket, nor is this supported for tcp style sockets. 5728 */ 5729 if (!sctp_style(sk, UDP)) 5730 return -EINVAL; 5731 5732 err = sock_create_lite(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); 5733 if (err) 5734 return err; 5735 5736 newsk = sctp_clone_sock(sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH); 5737 if (IS_ERR(newsk)) { 5738 sock_release(sock); 5739 *sockp = NULL; 5740 return PTR_ERR(newsk); 5741 } 5742 5743 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); 5744 __inet_accept(sk->sk_socket, sock, newsk); 5745 release_sock(newsk); 5746 5747 sock->ops = sk->sk_socket->ops; 5748 __module_get(sock->ops->owner); 5749 5750 *sockp = sock; 5751 5752 return err; 5753 } 5754 5755 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff, 5756 struct file **newfile, unsigned flags) 5757 { 5758 struct socket *newsock; 5759 int retval; 5760 5761 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock); 5762 if (retval < 0) 5763 goto out; 5764 5765 /* Map the socket to an unused fd that can be returned to the user. */ 5766 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC); 5767 if (retval < 0) { 5768 sock_release(newsock); 5769 goto out; 5770 } 5771 5772 *newfile = sock_alloc_file(newsock, 0, NULL); 5773 if (IS_ERR(*newfile)) { 5774 put_unused_fd(retval); 5775 retval = PTR_ERR(*newfile); 5776 *newfile = NULL; 5777 return retval; 5778 } 5779 5780 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk, 5781 retval); 5782 5783 peeloff->sd = retval; 5784 5785 if (flags & SOCK_NONBLOCK) 5786 (*newfile)->f_flags |= O_NONBLOCK; 5787 out: 5788 return retval; 5789 } 5790 5791 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) 5792 { 5793 sctp_peeloff_arg_t peeloff; 5794 struct file *newfile = NULL; 5795 int retval = 0; 5796 5797 if (len < sizeof(sctp_peeloff_arg_t)) 5798 return -EINVAL; 5799 len = sizeof(sctp_peeloff_arg_t); 5800 if (copy_from_user(&peeloff, optval, len)) 5801 return -EFAULT; 5802 5803 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0); 5804 if (retval < 0) 5805 goto out; 5806 5807 /* Return the fd mapped to the new socket. */ 5808 if (put_user(len, optlen)) { 5809 fput(newfile); 5810 put_unused_fd(retval); 5811 return -EFAULT; 5812 } 5813 5814 if (copy_to_user(optval, &peeloff, len)) { 5815 fput(newfile); 5816 put_unused_fd(retval); 5817 return -EFAULT; 5818 } 5819 fd_install(retval, newfile); 5820 out: 5821 return retval; 5822 } 5823 5824 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len, 5825 char __user *optval, int __user *optlen) 5826 { 5827 sctp_peeloff_flags_arg_t peeloff; 5828 struct file *newfile = NULL; 5829 int retval = 0; 5830 5831 if (len < sizeof(sctp_peeloff_flags_arg_t)) 5832 return -EINVAL; 5833 len = sizeof(sctp_peeloff_flags_arg_t); 5834 if (copy_from_user(&peeloff, optval, len)) 5835 return -EFAULT; 5836 5837 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg, 5838 &newfile, peeloff.flags); 5839 if (retval < 0) 5840 goto out; 5841 5842 /* Return the fd mapped to the new socket. */ 5843 if (put_user(len, optlen)) { 5844 fput(newfile); 5845 put_unused_fd(retval); 5846 return -EFAULT; 5847 } 5848 5849 if (copy_to_user(optval, &peeloff, len)) { 5850 fput(newfile); 5851 put_unused_fd(retval); 5852 return -EFAULT; 5853 } 5854 fd_install(retval, newfile); 5855 out: 5856 return retval; 5857 } 5858 5859 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) 5860 * 5861 * Applications can enable or disable heartbeats for any peer address of 5862 * an association, modify an address's heartbeat interval, force a 5863 * heartbeat to be sent immediately, and adjust the address's maximum 5864 * number of retransmissions sent before an address is considered 5865 * unreachable. The following structure is used to access and modify an 5866 * address's parameters: 5867 * 5868 * struct sctp_paddrparams { 5869 * sctp_assoc_t spp_assoc_id; 5870 * struct sockaddr_storage spp_address; 5871 * uint32_t spp_hbinterval; 5872 * uint16_t spp_pathmaxrxt; 5873 * uint32_t spp_pathmtu; 5874 * uint32_t spp_sackdelay; 5875 * uint32_t spp_flags; 5876 * }; 5877 * 5878 * spp_assoc_id - (one-to-many style socket) This is filled in the 5879 * application, and identifies the association for 5880 * this query. 5881 * spp_address - This specifies which address is of interest. 5882 * spp_hbinterval - This contains the value of the heartbeat interval, 5883 * in milliseconds. If a value of zero 5884 * is present in this field then no changes are to 5885 * be made to this parameter. 5886 * spp_pathmaxrxt - This contains the maximum number of 5887 * retransmissions before this address shall be 5888 * considered unreachable. If a value of zero 5889 * is present in this field then no changes are to 5890 * be made to this parameter. 5891 * spp_pathmtu - When Path MTU discovery is disabled the value 5892 * specified here will be the "fixed" path mtu. 5893 * Note that if the spp_address field is empty 5894 * then all associations on this address will 5895 * have this fixed path mtu set upon them. 5896 * 5897 * spp_sackdelay - When delayed sack is enabled, this value specifies 5898 * the number of milliseconds that sacks will be delayed 5899 * for. This value will apply to all addresses of an 5900 * association if the spp_address field is empty. Note 5901 * also, that if delayed sack is enabled and this 5902 * value is set to 0, no change is made to the last 5903 * recorded delayed sack timer value. 5904 * 5905 * spp_flags - These flags are used to control various features 5906 * on an association. The flag field may contain 5907 * zero or more of the following options. 5908 * 5909 * SPP_HB_ENABLE - Enable heartbeats on the 5910 * specified address. Note that if the address 5911 * field is empty all addresses for the association 5912 * have heartbeats enabled upon them. 5913 * 5914 * SPP_HB_DISABLE - Disable heartbeats on the 5915 * speicifed address. Note that if the address 5916 * field is empty all addresses for the association 5917 * will have their heartbeats disabled. Note also 5918 * that SPP_HB_ENABLE and SPP_HB_DISABLE are 5919 * mutually exclusive, only one of these two should 5920 * be specified. Enabling both fields will have 5921 * undetermined results. 5922 * 5923 * SPP_HB_DEMAND - Request a user initiated heartbeat 5924 * to be made immediately. 5925 * 5926 * SPP_PMTUD_ENABLE - This field will enable PMTU 5927 * discovery upon the specified address. Note that 5928 * if the address feild is empty then all addresses 5929 * on the association are effected. 5930 * 5931 * SPP_PMTUD_DISABLE - This field will disable PMTU 5932 * discovery upon the specified address. Note that 5933 * if the address feild is empty then all addresses 5934 * on the association are effected. Not also that 5935 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually 5936 * exclusive. Enabling both will have undetermined 5937 * results. 5938 * 5939 * SPP_SACKDELAY_ENABLE - Setting this flag turns 5940 * on delayed sack. The time specified in spp_sackdelay 5941 * is used to specify the sack delay for this address. Note 5942 * that if spp_address is empty then all addresses will 5943 * enable delayed sack and take on the sack delay 5944 * value specified in spp_sackdelay. 5945 * SPP_SACKDELAY_DISABLE - Setting this flag turns 5946 * off delayed sack. If the spp_address field is blank then 5947 * delayed sack is disabled for the entire association. Note 5948 * also that this field is mutually exclusive to 5949 * SPP_SACKDELAY_ENABLE, setting both will have undefined 5950 * results. 5951 * 5952 * SPP_IPV6_FLOWLABEL: Setting this flag enables the 5953 * setting of the IPV6 flow label value. The value is 5954 * contained in the spp_ipv6_flowlabel field. 5955 * Upon retrieval, this flag will be set to indicate that 5956 * the spp_ipv6_flowlabel field has a valid value returned. 5957 * If a specific destination address is set (in the 5958 * spp_address field), then the value returned is that of 5959 * the address. If just an association is specified (and 5960 * no address), then the association's default flow label 5961 * is returned. If neither an association nor a destination 5962 * is specified, then the socket's default flow label is 5963 * returned. For non-IPv6 sockets, this flag will be left 5964 * cleared. 5965 * 5966 * SPP_DSCP: Setting this flag enables the setting of the 5967 * Differentiated Services Code Point (DSCP) value 5968 * associated with either the association or a specific 5969 * address. The value is obtained in the spp_dscp field. 5970 * Upon retrieval, this flag will be set to indicate that 5971 * the spp_dscp field has a valid value returned. If a 5972 * specific destination address is set when called (in the 5973 * spp_address field), then that specific destination 5974 * address's DSCP value is returned. If just an association 5975 * is specified, then the association's default DSCP is 5976 * returned. If neither an association nor a destination is 5977 * specified, then the socket's default DSCP is returned. 5978 * 5979 * spp_ipv6_flowlabel 5980 * - This field is used in conjunction with the 5981 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label. 5982 * The 20 least significant bits are used for the flow 5983 * label. This setting has precedence over any IPv6-layer 5984 * setting. 5985 * 5986 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag 5987 * and contains the DSCP. The 6 most significant bits are 5988 * used for the DSCP. This setting has precedence over any 5989 * IPv4- or IPv6- layer setting. 5990 */ 5991 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, 5992 char __user *optval, int __user *optlen) 5993 { 5994 struct sctp_paddrparams params; 5995 struct sctp_transport *trans = NULL; 5996 struct sctp_association *asoc = NULL; 5997 struct sctp_sock *sp = sctp_sk(sk); 5998 5999 if (len >= sizeof(params)) 6000 len = sizeof(params); 6001 else if (len >= ALIGN(offsetof(struct sctp_paddrparams, 6002 spp_ipv6_flowlabel), 4)) 6003 len = ALIGN(offsetof(struct sctp_paddrparams, 6004 spp_ipv6_flowlabel), 4); 6005 else 6006 return -EINVAL; 6007 6008 if (copy_from_user(¶ms, optval, len)) 6009 return -EFAULT; 6010 6011 /* If an address other than INADDR_ANY is specified, and 6012 * no transport is found, then the request is invalid. 6013 */ 6014 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) { 6015 trans = sctp_addr_id2transport(sk, ¶ms.spp_address, 6016 params.spp_assoc_id); 6017 if (!trans) { 6018 pr_debug("%s: failed no transport\n", __func__); 6019 return -EINVAL; 6020 } 6021 } 6022 6023 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the 6024 * socket is a one to many style socket, and an association 6025 * was not found, then the id was invalid. 6026 */ 6027 asoc = sctp_id2assoc(sk, params.spp_assoc_id); 6028 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC && 6029 sctp_style(sk, UDP)) { 6030 pr_debug("%s: failed no association\n", __func__); 6031 return -EINVAL; 6032 } 6033 6034 if (trans) { 6035 /* Fetch transport values. */ 6036 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); 6037 params.spp_pathmtu = trans->pathmtu; 6038 params.spp_pathmaxrxt = trans->pathmaxrxt; 6039 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay); 6040 6041 /*draft-11 doesn't say what to return in spp_flags*/ 6042 params.spp_flags = trans->param_flags; 6043 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) { 6044 params.spp_ipv6_flowlabel = trans->flowlabel & 6045 SCTP_FLOWLABEL_VAL_MASK; 6046 params.spp_flags |= SPP_IPV6_FLOWLABEL; 6047 } 6048 if (trans->dscp & SCTP_DSCP_SET_MASK) { 6049 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK; 6050 params.spp_flags |= SPP_DSCP; 6051 } 6052 } else if (asoc) { 6053 /* Fetch association values. */ 6054 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); 6055 params.spp_pathmtu = asoc->pathmtu; 6056 params.spp_pathmaxrxt = asoc->pathmaxrxt; 6057 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay); 6058 6059 /*draft-11 doesn't say what to return in spp_flags*/ 6060 params.spp_flags = asoc->param_flags; 6061 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) { 6062 params.spp_ipv6_flowlabel = asoc->flowlabel & 6063 SCTP_FLOWLABEL_VAL_MASK; 6064 params.spp_flags |= SPP_IPV6_FLOWLABEL; 6065 } 6066 if (asoc->dscp & SCTP_DSCP_SET_MASK) { 6067 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK; 6068 params.spp_flags |= SPP_DSCP; 6069 } 6070 } else { 6071 /* Fetch socket values. */ 6072 params.spp_hbinterval = sp->hbinterval; 6073 params.spp_pathmtu = sp->pathmtu; 6074 params.spp_sackdelay = sp->sackdelay; 6075 params.spp_pathmaxrxt = sp->pathmaxrxt; 6076 6077 /*draft-11 doesn't say what to return in spp_flags*/ 6078 params.spp_flags = sp->param_flags; 6079 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) { 6080 params.spp_ipv6_flowlabel = sp->flowlabel & 6081 SCTP_FLOWLABEL_VAL_MASK; 6082 params.spp_flags |= SPP_IPV6_FLOWLABEL; 6083 } 6084 if (sp->dscp & SCTP_DSCP_SET_MASK) { 6085 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK; 6086 params.spp_flags |= SPP_DSCP; 6087 } 6088 } 6089 6090 if (copy_to_user(optval, ¶ms, len)) 6091 return -EFAULT; 6092 6093 if (put_user(len, optlen)) 6094 return -EFAULT; 6095 6096 return 0; 6097 } 6098 6099 /* 6100 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) 6101 * 6102 * This option will effect the way delayed acks are performed. This 6103 * option allows you to get or set the delayed ack time, in 6104 * milliseconds. It also allows changing the delayed ack frequency. 6105 * Changing the frequency to 1 disables the delayed sack algorithm. If 6106 * the assoc_id is 0, then this sets or gets the endpoints default 6107 * values. If the assoc_id field is non-zero, then the set or get 6108 * effects the specified association for the one to many model (the 6109 * assoc_id field is ignored by the one to one model). Note that if 6110 * sack_delay or sack_freq are 0 when setting this option, then the 6111 * current values will remain unchanged. 6112 * 6113 * struct sctp_sack_info { 6114 * sctp_assoc_t sack_assoc_id; 6115 * uint32_t sack_delay; 6116 * uint32_t sack_freq; 6117 * }; 6118 * 6119 * sack_assoc_id - This parameter, indicates which association the user 6120 * is performing an action upon. Note that if this field's value is 6121 * zero then the endpoints default value is changed (effecting future 6122 * associations only). 6123 * 6124 * sack_delay - This parameter contains the number of milliseconds that 6125 * the user is requesting the delayed ACK timer be set to. Note that 6126 * this value is defined in the standard to be between 200 and 500 6127 * milliseconds. 6128 * 6129 * sack_freq - This parameter contains the number of packets that must 6130 * be received before a sack is sent without waiting for the delay 6131 * timer to expire. The default value for this is 2, setting this 6132 * value to 1 will disable the delayed sack algorithm. 6133 */ 6134 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len, 6135 char __user *optval, 6136 int __user *optlen) 6137 { 6138 struct sctp_sack_info params; 6139 struct sctp_association *asoc = NULL; 6140 struct sctp_sock *sp = sctp_sk(sk); 6141 6142 if (len >= sizeof(struct sctp_sack_info)) { 6143 len = sizeof(struct sctp_sack_info); 6144 6145 if (copy_from_user(¶ms, optval, len)) 6146 return -EFAULT; 6147 } else if (len == sizeof(struct sctp_assoc_value)) { 6148 pr_warn_ratelimited(DEPRECATED 6149 "%s (pid %d) " 6150 "Use of struct sctp_assoc_value in delayed_ack socket option.\n" 6151 "Use struct sctp_sack_info instead\n", 6152 current->comm, task_pid_nr(current)); 6153 if (copy_from_user(¶ms, optval, len)) 6154 return -EFAULT; 6155 } else 6156 return -EINVAL; 6157 6158 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the 6159 * socket is a one to many style socket, and an association 6160 * was not found, then the id was invalid. 6161 */ 6162 asoc = sctp_id2assoc(sk, params.sack_assoc_id); 6163 if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC && 6164 sctp_style(sk, UDP)) 6165 return -EINVAL; 6166 6167 if (asoc) { 6168 /* Fetch association values. */ 6169 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) { 6170 params.sack_delay = jiffies_to_msecs(asoc->sackdelay); 6171 params.sack_freq = asoc->sackfreq; 6172 6173 } else { 6174 params.sack_delay = 0; 6175 params.sack_freq = 1; 6176 } 6177 } else { 6178 /* Fetch socket values. */ 6179 if (sp->param_flags & SPP_SACKDELAY_ENABLE) { 6180 params.sack_delay = sp->sackdelay; 6181 params.sack_freq = sp->sackfreq; 6182 } else { 6183 params.sack_delay = 0; 6184 params.sack_freq = 1; 6185 } 6186 } 6187 6188 if (copy_to_user(optval, ¶ms, len)) 6189 return -EFAULT; 6190 6191 if (put_user(len, optlen)) 6192 return -EFAULT; 6193 6194 return 0; 6195 } 6196 6197 /* 7.1.3 Initialization Parameters (SCTP_INITMSG) 6198 * 6199 * Applications can specify protocol parameters for the default association 6200 * initialization. The option name argument to setsockopt() and getsockopt() 6201 * is SCTP_INITMSG. 6202 * 6203 * Setting initialization parameters is effective only on an unconnected 6204 * socket (for UDP-style sockets only future associations are effected 6205 * by the change). With TCP-style sockets, this option is inherited by 6206 * sockets derived from a listener socket. 6207 */ 6208 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) 6209 { 6210 if (len < sizeof(struct sctp_initmsg)) 6211 return -EINVAL; 6212 len = sizeof(struct sctp_initmsg); 6213 if (put_user(len, optlen)) 6214 return -EFAULT; 6215 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) 6216 return -EFAULT; 6217 return 0; 6218 } 6219 6220 6221 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, 6222 char __user *optval, int __user *optlen) 6223 { 6224 struct sctp_association *asoc; 6225 int cnt = 0; 6226 struct sctp_getaddrs getaddrs; 6227 struct sctp_transport *from; 6228 void __user *to; 6229 union sctp_addr temp; 6230 struct sctp_sock *sp = sctp_sk(sk); 6231 int addrlen; 6232 size_t space_left; 6233 int bytes_copied; 6234 6235 if (len < sizeof(struct sctp_getaddrs)) 6236 return -EINVAL; 6237 6238 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) 6239 return -EFAULT; 6240 6241 /* For UDP-style sockets, id specifies the association to query. */ 6242 asoc = sctp_id2assoc(sk, getaddrs.assoc_id); 6243 if (!asoc) 6244 return -EINVAL; 6245 6246 to = optval + offsetof(struct sctp_getaddrs, addrs); 6247 space_left = len - offsetof(struct sctp_getaddrs, addrs); 6248 6249 list_for_each_entry(from, &asoc->peer.transport_addr_list, 6250 transports) { 6251 memcpy(&temp, &from->ipaddr, sizeof(temp)); 6252 addrlen = sctp_get_pf_specific(sk->sk_family) 6253 ->addr_to_user(sp, &temp); 6254 if (space_left < addrlen) 6255 return -ENOMEM; 6256 if (copy_to_user(to, &temp, addrlen)) 6257 return -EFAULT; 6258 to += addrlen; 6259 cnt++; 6260 space_left -= addrlen; 6261 } 6262 6263 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) 6264 return -EFAULT; 6265 bytes_copied = ((char __user *)to) - optval; 6266 if (put_user(bytes_copied, optlen)) 6267 return -EFAULT; 6268 6269 return 0; 6270 } 6271 6272 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, 6273 size_t space_left, int *bytes_copied) 6274 { 6275 struct sctp_sockaddr_entry *addr; 6276 union sctp_addr temp; 6277 int cnt = 0; 6278 int addrlen; 6279 struct net *net = sock_net(sk); 6280 6281 rcu_read_lock(); 6282 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) { 6283 if (!addr->valid) 6284 continue; 6285 6286 if ((PF_INET == sk->sk_family) && 6287 (AF_INET6 == addr->a.sa.sa_family)) 6288 continue; 6289 if ((PF_INET6 == sk->sk_family) && 6290 inet_v6_ipv6only(sk) && 6291 (AF_INET == addr->a.sa.sa_family)) 6292 continue; 6293 memcpy(&temp, &addr->a, sizeof(temp)); 6294 if (!temp.v4.sin_port) 6295 temp.v4.sin_port = htons(port); 6296 6297 addrlen = sctp_get_pf_specific(sk->sk_family) 6298 ->addr_to_user(sctp_sk(sk), &temp); 6299 6300 if (space_left < addrlen) { 6301 cnt = -ENOMEM; 6302 break; 6303 } 6304 memcpy(to, &temp, addrlen); 6305 6306 to += addrlen; 6307 cnt++; 6308 space_left -= addrlen; 6309 *bytes_copied += addrlen; 6310 } 6311 rcu_read_unlock(); 6312 6313 return cnt; 6314 } 6315 6316 6317 static int sctp_getsockopt_local_addrs(struct sock *sk, int len, 6318 char __user *optval, int __user *optlen) 6319 { 6320 struct sctp_bind_addr *bp; 6321 struct sctp_association *asoc; 6322 int cnt = 0; 6323 struct sctp_getaddrs getaddrs; 6324 struct sctp_sockaddr_entry *addr; 6325 void __user *to; 6326 union sctp_addr temp; 6327 struct sctp_sock *sp = sctp_sk(sk); 6328 int addrlen; 6329 int err = 0; 6330 size_t space_left; 6331 int bytes_copied = 0; 6332 void *addrs; 6333 void *buf; 6334 6335 if (len < sizeof(struct sctp_getaddrs)) 6336 return -EINVAL; 6337 6338 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) 6339 return -EFAULT; 6340 6341 /* 6342 * For UDP-style sockets, id specifies the association to query. 6343 * If the id field is set to the value '0' then the locally bound 6344 * addresses are returned without regard to any particular 6345 * association. 6346 */ 6347 if (0 == getaddrs.assoc_id) { 6348 bp = &sctp_sk(sk)->ep->base.bind_addr; 6349 } else { 6350 asoc = sctp_id2assoc(sk, getaddrs.assoc_id); 6351 if (!asoc) 6352 return -EINVAL; 6353 bp = &asoc->base.bind_addr; 6354 } 6355 6356 to = optval + offsetof(struct sctp_getaddrs, addrs); 6357 space_left = len - offsetof(struct sctp_getaddrs, addrs); 6358 6359 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN); 6360 if (!addrs) 6361 return -ENOMEM; 6362 6363 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid 6364 * addresses from the global local address list. 6365 */ 6366 if (sctp_list_single_entry(&bp->address_list)) { 6367 addr = list_entry(bp->address_list.next, 6368 struct sctp_sockaddr_entry, list); 6369 if (sctp_is_any(sk, &addr->a)) { 6370 cnt = sctp_copy_laddrs(sk, bp->port, addrs, 6371 space_left, &bytes_copied); 6372 if (cnt < 0) { 6373 err = cnt; 6374 goto out; 6375 } 6376 goto copy_getaddrs; 6377 } 6378 } 6379 6380 buf = addrs; 6381 /* Protection on the bound address list is not needed since 6382 * in the socket option context we hold a socket lock and 6383 * thus the bound address list can't change. 6384 */ 6385 list_for_each_entry(addr, &bp->address_list, list) { 6386 memcpy(&temp, &addr->a, sizeof(temp)); 6387 addrlen = sctp_get_pf_specific(sk->sk_family) 6388 ->addr_to_user(sp, &temp); 6389 if (space_left < addrlen) { 6390 err = -ENOMEM; /*fixme: right error?*/ 6391 goto out; 6392 } 6393 memcpy(buf, &temp, addrlen); 6394 buf += addrlen; 6395 bytes_copied += addrlen; 6396 cnt++; 6397 space_left -= addrlen; 6398 } 6399 6400 copy_getaddrs: 6401 if (copy_to_user(to, addrs, bytes_copied)) { 6402 err = -EFAULT; 6403 goto out; 6404 } 6405 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) { 6406 err = -EFAULT; 6407 goto out; 6408 } 6409 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too, 6410 * but we can't change it anymore. 6411 */ 6412 if (put_user(bytes_copied, optlen)) 6413 err = -EFAULT; 6414 out: 6415 kfree(addrs); 6416 return err; 6417 } 6418 6419 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) 6420 * 6421 * Requests that the local SCTP stack use the enclosed peer address as 6422 * the association primary. The enclosed address must be one of the 6423 * association peer's addresses. 6424 */ 6425 static int sctp_getsockopt_primary_addr(struct sock *sk, int len, 6426 char __user *optval, int __user *optlen) 6427 { 6428 struct sctp_prim prim; 6429 struct sctp_association *asoc; 6430 struct sctp_sock *sp = sctp_sk(sk); 6431 6432 if (len < sizeof(struct sctp_prim)) 6433 return -EINVAL; 6434 6435 len = sizeof(struct sctp_prim); 6436 6437 if (copy_from_user(&prim, optval, len)) 6438 return -EFAULT; 6439 6440 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); 6441 if (!asoc) 6442 return -EINVAL; 6443 6444 if (!asoc->peer.primary_path) 6445 return -ENOTCONN; 6446 6447 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, 6448 asoc->peer.primary_path->af_specific->sockaddr_len); 6449 6450 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp, 6451 (union sctp_addr *)&prim.ssp_addr); 6452 6453 if (put_user(len, optlen)) 6454 return -EFAULT; 6455 if (copy_to_user(optval, &prim, len)) 6456 return -EFAULT; 6457 6458 return 0; 6459 } 6460 6461 /* 6462 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) 6463 * 6464 * Requests that the local endpoint set the specified Adaptation Layer 6465 * Indication parameter for all future INIT and INIT-ACK exchanges. 6466 */ 6467 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, 6468 char __user *optval, int __user *optlen) 6469 { 6470 struct sctp_setadaptation adaptation; 6471 6472 if (len < sizeof(struct sctp_setadaptation)) 6473 return -EINVAL; 6474 6475 len = sizeof(struct sctp_setadaptation); 6476 6477 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; 6478 6479 if (put_user(len, optlen)) 6480 return -EFAULT; 6481 if (copy_to_user(optval, &adaptation, len)) 6482 return -EFAULT; 6483 6484 return 0; 6485 } 6486 6487 /* 6488 * 6489 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) 6490 * 6491 * Applications that wish to use the sendto() system call may wish to 6492 * specify a default set of parameters that would normally be supplied 6493 * through the inclusion of ancillary data. This socket option allows 6494 * such an application to set the default sctp_sndrcvinfo structure. 6495 6496 6497 * The application that wishes to use this socket option simply passes 6498 * in to this call the sctp_sndrcvinfo structure defined in Section 6499 * 5.2.2) The input parameters accepted by this call include 6500 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, 6501 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in 6502 * to this call if the caller is using the UDP model. 6503 * 6504 * For getsockopt, it get the default sctp_sndrcvinfo structure. 6505 */ 6506 static int sctp_getsockopt_default_send_param(struct sock *sk, 6507 int len, char __user *optval, 6508 int __user *optlen) 6509 { 6510 struct sctp_sock *sp = sctp_sk(sk); 6511 struct sctp_association *asoc; 6512 struct sctp_sndrcvinfo info; 6513 6514 if (len < sizeof(info)) 6515 return -EINVAL; 6516 6517 len = sizeof(info); 6518 6519 if (copy_from_user(&info, optval, len)) 6520 return -EFAULT; 6521 6522 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); 6523 if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC && 6524 sctp_style(sk, UDP)) 6525 return -EINVAL; 6526 6527 if (asoc) { 6528 info.sinfo_stream = asoc->default_stream; 6529 info.sinfo_flags = asoc->default_flags; 6530 info.sinfo_ppid = asoc->default_ppid; 6531 info.sinfo_context = asoc->default_context; 6532 info.sinfo_timetolive = asoc->default_timetolive; 6533 } else { 6534 info.sinfo_stream = sp->default_stream; 6535 info.sinfo_flags = sp->default_flags; 6536 info.sinfo_ppid = sp->default_ppid; 6537 info.sinfo_context = sp->default_context; 6538 info.sinfo_timetolive = sp->default_timetolive; 6539 } 6540 6541 if (put_user(len, optlen)) 6542 return -EFAULT; 6543 if (copy_to_user(optval, &info, len)) 6544 return -EFAULT; 6545 6546 return 0; 6547 } 6548 6549 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters 6550 * (SCTP_DEFAULT_SNDINFO) 6551 */ 6552 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len, 6553 char __user *optval, 6554 int __user *optlen) 6555 { 6556 struct sctp_sock *sp = sctp_sk(sk); 6557 struct sctp_association *asoc; 6558 struct sctp_sndinfo info; 6559 6560 if (len < sizeof(info)) 6561 return -EINVAL; 6562 6563 len = sizeof(info); 6564 6565 if (copy_from_user(&info, optval, len)) 6566 return -EFAULT; 6567 6568 asoc = sctp_id2assoc(sk, info.snd_assoc_id); 6569 if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC && 6570 sctp_style(sk, UDP)) 6571 return -EINVAL; 6572 6573 if (asoc) { 6574 info.snd_sid = asoc->default_stream; 6575 info.snd_flags = asoc->default_flags; 6576 info.snd_ppid = asoc->default_ppid; 6577 info.snd_context = asoc->default_context; 6578 } else { 6579 info.snd_sid = sp->default_stream; 6580 info.snd_flags = sp->default_flags; 6581 info.snd_ppid = sp->default_ppid; 6582 info.snd_context = sp->default_context; 6583 } 6584 6585 if (put_user(len, optlen)) 6586 return -EFAULT; 6587 if (copy_to_user(optval, &info, len)) 6588 return -EFAULT; 6589 6590 return 0; 6591 } 6592 6593 /* 6594 * 6595 * 7.1.5 SCTP_NODELAY 6596 * 6597 * Turn on/off any Nagle-like algorithm. This means that packets are 6598 * generally sent as soon as possible and no unnecessary delays are 6599 * introduced, at the cost of more packets in the network. Expects an 6600 * integer boolean flag. 6601 */ 6602 6603 static int sctp_getsockopt_nodelay(struct sock *sk, int len, 6604 char __user *optval, int __user *optlen) 6605 { 6606 int val; 6607 6608 if (len < sizeof(int)) 6609 return -EINVAL; 6610 6611 len = sizeof(int); 6612 val = (sctp_sk(sk)->nodelay == 1); 6613 if (put_user(len, optlen)) 6614 return -EFAULT; 6615 if (copy_to_user(optval, &val, len)) 6616 return -EFAULT; 6617 return 0; 6618 } 6619 6620 /* 6621 * 6622 * 7.1.1 SCTP_RTOINFO 6623 * 6624 * The protocol parameters used to initialize and bound retransmission 6625 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access 6626 * and modify these parameters. 6627 * All parameters are time values, in milliseconds. A value of 0, when 6628 * modifying the parameters, indicates that the current value should not 6629 * be changed. 6630 * 6631 */ 6632 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, 6633 char __user *optval, 6634 int __user *optlen) { 6635 struct sctp_rtoinfo rtoinfo; 6636 struct sctp_association *asoc; 6637 6638 if (len < sizeof (struct sctp_rtoinfo)) 6639 return -EINVAL; 6640 6641 len = sizeof(struct sctp_rtoinfo); 6642 6643 if (copy_from_user(&rtoinfo, optval, len)) 6644 return -EFAULT; 6645 6646 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); 6647 6648 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC && 6649 sctp_style(sk, UDP)) 6650 return -EINVAL; 6651 6652 /* Values corresponding to the specific association. */ 6653 if (asoc) { 6654 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); 6655 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); 6656 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); 6657 } else { 6658 /* Values corresponding to the endpoint. */ 6659 struct sctp_sock *sp = sctp_sk(sk); 6660 6661 rtoinfo.srto_initial = sp->rtoinfo.srto_initial; 6662 rtoinfo.srto_max = sp->rtoinfo.srto_max; 6663 rtoinfo.srto_min = sp->rtoinfo.srto_min; 6664 } 6665 6666 if (put_user(len, optlen)) 6667 return -EFAULT; 6668 6669 if (copy_to_user(optval, &rtoinfo, len)) 6670 return -EFAULT; 6671 6672 return 0; 6673 } 6674 6675 /* 6676 * 6677 * 7.1.2 SCTP_ASSOCINFO 6678 * 6679 * This option is used to tune the maximum retransmission attempts 6680 * of the association. 6681 * Returns an error if the new association retransmission value is 6682 * greater than the sum of the retransmission value of the peer. 6683 * See [SCTP] for more information. 6684 * 6685 */ 6686 static int sctp_getsockopt_associnfo(struct sock *sk, int len, 6687 char __user *optval, 6688 int __user *optlen) 6689 { 6690 6691 struct sctp_assocparams assocparams; 6692 struct sctp_association *asoc; 6693 struct list_head *pos; 6694 int cnt = 0; 6695 6696 if (len < sizeof (struct sctp_assocparams)) 6697 return -EINVAL; 6698 6699 len = sizeof(struct sctp_assocparams); 6700 6701 if (copy_from_user(&assocparams, optval, len)) 6702 return -EFAULT; 6703 6704 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); 6705 6706 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC && 6707 sctp_style(sk, UDP)) 6708 return -EINVAL; 6709 6710 /* Values correspoinding to the specific association */ 6711 if (asoc) { 6712 assocparams.sasoc_asocmaxrxt = asoc->max_retrans; 6713 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; 6714 assocparams.sasoc_local_rwnd = asoc->a_rwnd; 6715 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life); 6716 6717 list_for_each(pos, &asoc->peer.transport_addr_list) { 6718 cnt++; 6719 } 6720 6721 assocparams.sasoc_number_peer_destinations = cnt; 6722 } else { 6723 /* Values corresponding to the endpoint */ 6724 struct sctp_sock *sp = sctp_sk(sk); 6725 6726 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; 6727 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; 6728 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; 6729 assocparams.sasoc_cookie_life = 6730 sp->assocparams.sasoc_cookie_life; 6731 assocparams.sasoc_number_peer_destinations = 6732 sp->assocparams. 6733 sasoc_number_peer_destinations; 6734 } 6735 6736 if (put_user(len, optlen)) 6737 return -EFAULT; 6738 6739 if (copy_to_user(optval, &assocparams, len)) 6740 return -EFAULT; 6741 6742 return 0; 6743 } 6744 6745 /* 6746 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) 6747 * 6748 * This socket option is a boolean flag which turns on or off mapped V4 6749 * addresses. If this option is turned on and the socket is type 6750 * PF_INET6, then IPv4 addresses will be mapped to V6 representation. 6751 * If this option is turned off, then no mapping will be done of V4 6752 * addresses and a user will receive both PF_INET6 and PF_INET type 6753 * addresses on the socket. 6754 */ 6755 static int sctp_getsockopt_mappedv4(struct sock *sk, int len, 6756 char __user *optval, int __user *optlen) 6757 { 6758 int val; 6759 struct sctp_sock *sp = sctp_sk(sk); 6760 6761 if (len < sizeof(int)) 6762 return -EINVAL; 6763 6764 len = sizeof(int); 6765 val = sp->v4mapped; 6766 if (put_user(len, optlen)) 6767 return -EFAULT; 6768 if (copy_to_user(optval, &val, len)) 6769 return -EFAULT; 6770 6771 return 0; 6772 } 6773 6774 /* 6775 * 7.1.29. Set or Get the default context (SCTP_CONTEXT) 6776 * (chapter and verse is quoted at sctp_setsockopt_context()) 6777 */ 6778 static int sctp_getsockopt_context(struct sock *sk, int len, 6779 char __user *optval, int __user *optlen) 6780 { 6781 struct sctp_assoc_value params; 6782 struct sctp_association *asoc; 6783 6784 if (len < sizeof(struct sctp_assoc_value)) 6785 return -EINVAL; 6786 6787 len = sizeof(struct sctp_assoc_value); 6788 6789 if (copy_from_user(¶ms, optval, len)) 6790 return -EFAULT; 6791 6792 asoc = sctp_id2assoc(sk, params.assoc_id); 6793 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 6794 sctp_style(sk, UDP)) 6795 return -EINVAL; 6796 6797 params.assoc_value = asoc ? asoc->default_rcv_context 6798 : sctp_sk(sk)->default_rcv_context; 6799 6800 if (put_user(len, optlen)) 6801 return -EFAULT; 6802 if (copy_to_user(optval, ¶ms, len)) 6803 return -EFAULT; 6804 6805 return 0; 6806 } 6807 6808 /* 6809 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) 6810 * This option will get or set the maximum size to put in any outgoing 6811 * SCTP DATA chunk. If a message is larger than this size it will be 6812 * fragmented by SCTP into the specified size. Note that the underlying 6813 * SCTP implementation may fragment into smaller sized chunks when the 6814 * PMTU of the underlying association is smaller than the value set by 6815 * the user. The default value for this option is '0' which indicates 6816 * the user is NOT limiting fragmentation and only the PMTU will effect 6817 * SCTP's choice of DATA chunk size. Note also that values set larger 6818 * than the maximum size of an IP datagram will effectively let SCTP 6819 * control fragmentation (i.e. the same as setting this option to 0). 6820 * 6821 * The following structure is used to access and modify this parameter: 6822 * 6823 * struct sctp_assoc_value { 6824 * sctp_assoc_t assoc_id; 6825 * uint32_t assoc_value; 6826 * }; 6827 * 6828 * assoc_id: This parameter is ignored for one-to-one style sockets. 6829 * For one-to-many style sockets this parameter indicates which 6830 * association the user is performing an action upon. Note that if 6831 * this field's value is zero then the endpoints default value is 6832 * changed (effecting future associations only). 6833 * assoc_value: This parameter specifies the maximum size in bytes. 6834 */ 6835 static int sctp_getsockopt_maxseg(struct sock *sk, int len, 6836 char __user *optval, int __user *optlen) 6837 { 6838 struct sctp_assoc_value params; 6839 struct sctp_association *asoc; 6840 6841 if (len == sizeof(int)) { 6842 pr_warn_ratelimited(DEPRECATED 6843 "%s (pid %d) " 6844 "Use of int in maxseg socket option.\n" 6845 "Use struct sctp_assoc_value instead\n", 6846 current->comm, task_pid_nr(current)); 6847 params.assoc_id = SCTP_FUTURE_ASSOC; 6848 } else if (len >= sizeof(struct sctp_assoc_value)) { 6849 len = sizeof(struct sctp_assoc_value); 6850 if (copy_from_user(¶ms, optval, len)) 6851 return -EFAULT; 6852 } else 6853 return -EINVAL; 6854 6855 asoc = sctp_id2assoc(sk, params.assoc_id); 6856 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 6857 sctp_style(sk, UDP)) 6858 return -EINVAL; 6859 6860 if (asoc) 6861 params.assoc_value = asoc->frag_point; 6862 else 6863 params.assoc_value = sctp_sk(sk)->user_frag; 6864 6865 if (put_user(len, optlen)) 6866 return -EFAULT; 6867 if (len == sizeof(int)) { 6868 if (copy_to_user(optval, ¶ms.assoc_value, len)) 6869 return -EFAULT; 6870 } else { 6871 if (copy_to_user(optval, ¶ms, len)) 6872 return -EFAULT; 6873 } 6874 6875 return 0; 6876 } 6877 6878 /* 6879 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) 6880 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) 6881 */ 6882 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, 6883 char __user *optval, int __user *optlen) 6884 { 6885 int val; 6886 6887 if (len < sizeof(int)) 6888 return -EINVAL; 6889 6890 len = sizeof(int); 6891 6892 val = sctp_sk(sk)->frag_interleave; 6893 if (put_user(len, optlen)) 6894 return -EFAULT; 6895 if (copy_to_user(optval, &val, len)) 6896 return -EFAULT; 6897 6898 return 0; 6899 } 6900 6901 /* 6902 * 7.1.25. Set or Get the sctp partial delivery point 6903 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) 6904 */ 6905 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, 6906 char __user *optval, 6907 int __user *optlen) 6908 { 6909 u32 val; 6910 6911 if (len < sizeof(u32)) 6912 return -EINVAL; 6913 6914 len = sizeof(u32); 6915 6916 val = sctp_sk(sk)->pd_point; 6917 if (put_user(len, optlen)) 6918 return -EFAULT; 6919 if (copy_to_user(optval, &val, len)) 6920 return -EFAULT; 6921 6922 return 0; 6923 } 6924 6925 /* 6926 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) 6927 * (chapter and verse is quoted at sctp_setsockopt_maxburst()) 6928 */ 6929 static int sctp_getsockopt_maxburst(struct sock *sk, int len, 6930 char __user *optval, 6931 int __user *optlen) 6932 { 6933 struct sctp_assoc_value params; 6934 struct sctp_association *asoc; 6935 6936 if (len == sizeof(int)) { 6937 pr_warn_ratelimited(DEPRECATED 6938 "%s (pid %d) " 6939 "Use of int in max_burst socket option.\n" 6940 "Use struct sctp_assoc_value instead\n", 6941 current->comm, task_pid_nr(current)); 6942 params.assoc_id = SCTP_FUTURE_ASSOC; 6943 } else if (len >= sizeof(struct sctp_assoc_value)) { 6944 len = sizeof(struct sctp_assoc_value); 6945 if (copy_from_user(¶ms, optval, len)) 6946 return -EFAULT; 6947 } else 6948 return -EINVAL; 6949 6950 asoc = sctp_id2assoc(sk, params.assoc_id); 6951 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 6952 sctp_style(sk, UDP)) 6953 return -EINVAL; 6954 6955 params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst; 6956 6957 if (len == sizeof(int)) { 6958 if (copy_to_user(optval, ¶ms.assoc_value, len)) 6959 return -EFAULT; 6960 } else { 6961 if (copy_to_user(optval, ¶ms, len)) 6962 return -EFAULT; 6963 } 6964 6965 return 0; 6966 6967 } 6968 6969 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len, 6970 char __user *optval, int __user *optlen) 6971 { 6972 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 6973 struct sctp_hmacalgo __user *p = (void __user *)optval; 6974 struct sctp_hmac_algo_param *hmacs; 6975 __u16 data_len = 0; 6976 u32 num_idents; 6977 int i; 6978 6979 if (!ep->auth_enable) 6980 return -EACCES; 6981 6982 hmacs = ep->auth_hmacs_list; 6983 data_len = ntohs(hmacs->param_hdr.length) - 6984 sizeof(struct sctp_paramhdr); 6985 6986 if (len < sizeof(struct sctp_hmacalgo) + data_len) 6987 return -EINVAL; 6988 6989 len = sizeof(struct sctp_hmacalgo) + data_len; 6990 num_idents = data_len / sizeof(u16); 6991 6992 if (put_user(len, optlen)) 6993 return -EFAULT; 6994 if (put_user(num_idents, &p->shmac_num_idents)) 6995 return -EFAULT; 6996 for (i = 0; i < num_idents; i++) { 6997 __u16 hmacid = ntohs(hmacs->hmac_ids[i]); 6998 6999 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16))) 7000 return -EFAULT; 7001 } 7002 return 0; 7003 } 7004 7005 static int sctp_getsockopt_active_key(struct sock *sk, int len, 7006 char __user *optval, int __user *optlen) 7007 { 7008 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 7009 struct sctp_authkeyid val; 7010 struct sctp_association *asoc; 7011 7012 if (len < sizeof(struct sctp_authkeyid)) 7013 return -EINVAL; 7014 7015 len = sizeof(struct sctp_authkeyid); 7016 if (copy_from_user(&val, optval, len)) 7017 return -EFAULT; 7018 7019 asoc = sctp_id2assoc(sk, val.scact_assoc_id); 7020 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) 7021 return -EINVAL; 7022 7023 if (asoc) { 7024 if (!asoc->peer.auth_capable) 7025 return -EACCES; 7026 val.scact_keynumber = asoc->active_key_id; 7027 } else { 7028 if (!ep->auth_enable) 7029 return -EACCES; 7030 val.scact_keynumber = ep->active_key_id; 7031 } 7032 7033 if (put_user(len, optlen)) 7034 return -EFAULT; 7035 if (copy_to_user(optval, &val, len)) 7036 return -EFAULT; 7037 7038 return 0; 7039 } 7040 7041 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len, 7042 char __user *optval, int __user *optlen) 7043 { 7044 struct sctp_authchunks __user *p = (void __user *)optval; 7045 struct sctp_authchunks val; 7046 struct sctp_association *asoc; 7047 struct sctp_chunks_param *ch; 7048 u32 num_chunks = 0; 7049 char __user *to; 7050 7051 if (len < sizeof(struct sctp_authchunks)) 7052 return -EINVAL; 7053 7054 if (copy_from_user(&val, optval, sizeof(val))) 7055 return -EFAULT; 7056 7057 to = p->gauth_chunks; 7058 asoc = sctp_id2assoc(sk, val.gauth_assoc_id); 7059 if (!asoc) 7060 return -EINVAL; 7061 7062 if (!asoc->peer.auth_capable) 7063 return -EACCES; 7064 7065 ch = asoc->peer.peer_chunks; 7066 if (!ch) 7067 goto num; 7068 7069 /* See if the user provided enough room for all the data */ 7070 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); 7071 if (len < sizeof(struct sctp_authchunks) + num_chunks) 7072 return -EINVAL; 7073 7074 if (copy_to_user(to, ch->chunks, num_chunks)) 7075 return -EFAULT; 7076 num: 7077 len = sizeof(struct sctp_authchunks) + num_chunks; 7078 if (put_user(len, optlen)) 7079 return -EFAULT; 7080 if (put_user(num_chunks, &p->gauth_number_of_chunks)) 7081 return -EFAULT; 7082 return 0; 7083 } 7084 7085 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len, 7086 char __user *optval, int __user *optlen) 7087 { 7088 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 7089 struct sctp_authchunks __user *p = (void __user *)optval; 7090 struct sctp_authchunks val; 7091 struct sctp_association *asoc; 7092 struct sctp_chunks_param *ch; 7093 u32 num_chunks = 0; 7094 char __user *to; 7095 7096 if (len < sizeof(struct sctp_authchunks)) 7097 return -EINVAL; 7098 7099 if (copy_from_user(&val, optval, sizeof(val))) 7100 return -EFAULT; 7101 7102 to = p->gauth_chunks; 7103 asoc = sctp_id2assoc(sk, val.gauth_assoc_id); 7104 if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC && 7105 sctp_style(sk, UDP)) 7106 return -EINVAL; 7107 7108 if (asoc) { 7109 if (!asoc->peer.auth_capable) 7110 return -EACCES; 7111 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks; 7112 } else { 7113 if (!ep->auth_enable) 7114 return -EACCES; 7115 ch = ep->auth_chunk_list; 7116 } 7117 if (!ch) 7118 goto num; 7119 7120 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); 7121 if (len < sizeof(struct sctp_authchunks) + num_chunks) 7122 return -EINVAL; 7123 7124 if (copy_to_user(to, ch->chunks, num_chunks)) 7125 return -EFAULT; 7126 num: 7127 len = sizeof(struct sctp_authchunks) + num_chunks; 7128 if (put_user(len, optlen)) 7129 return -EFAULT; 7130 if (put_user(num_chunks, &p->gauth_number_of_chunks)) 7131 return -EFAULT; 7132 7133 return 0; 7134 } 7135 7136 /* 7137 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER) 7138 * This option gets the current number of associations that are attached 7139 * to a one-to-many style socket. The option value is an uint32_t. 7140 */ 7141 static int sctp_getsockopt_assoc_number(struct sock *sk, int len, 7142 char __user *optval, int __user *optlen) 7143 { 7144 struct sctp_sock *sp = sctp_sk(sk); 7145 struct sctp_association *asoc; 7146 u32 val = 0; 7147 7148 if (sctp_style(sk, TCP)) 7149 return -EOPNOTSUPP; 7150 7151 if (len < sizeof(u32)) 7152 return -EINVAL; 7153 7154 len = sizeof(u32); 7155 7156 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { 7157 val++; 7158 } 7159 7160 if (put_user(len, optlen)) 7161 return -EFAULT; 7162 if (copy_to_user(optval, &val, len)) 7163 return -EFAULT; 7164 7165 return 0; 7166 } 7167 7168 /* 7169 * 8.1.23 SCTP_AUTO_ASCONF 7170 * See the corresponding setsockopt entry as description 7171 */ 7172 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len, 7173 char __user *optval, int __user *optlen) 7174 { 7175 int val = 0; 7176 7177 if (len < sizeof(int)) 7178 return -EINVAL; 7179 7180 len = sizeof(int); 7181 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk)) 7182 val = 1; 7183 if (put_user(len, optlen)) 7184 return -EFAULT; 7185 if (copy_to_user(optval, &val, len)) 7186 return -EFAULT; 7187 return 0; 7188 } 7189 7190 /* 7191 * 8.2.6. Get the Current Identifiers of Associations 7192 * (SCTP_GET_ASSOC_ID_LIST) 7193 * 7194 * This option gets the current list of SCTP association identifiers of 7195 * the SCTP associations handled by a one-to-many style socket. 7196 */ 7197 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len, 7198 char __user *optval, int __user *optlen) 7199 { 7200 struct sctp_sock *sp = sctp_sk(sk); 7201 struct sctp_association *asoc; 7202 struct sctp_assoc_ids *ids; 7203 size_t ids_size; 7204 u32 num = 0; 7205 7206 if (sctp_style(sk, TCP)) 7207 return -EOPNOTSUPP; 7208 7209 if (len < sizeof(struct sctp_assoc_ids)) 7210 return -EINVAL; 7211 7212 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { 7213 num++; 7214 } 7215 7216 ids_size = struct_size(ids, gaids_assoc_id, num); 7217 if (len < ids_size) 7218 return -EINVAL; 7219 7220 len = ids_size; 7221 ids = kmalloc(len, GFP_USER | __GFP_NOWARN); 7222 if (unlikely(!ids)) 7223 return -ENOMEM; 7224 7225 ids->gaids_number_of_ids = num; 7226 num = 0; 7227 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { 7228 ids->gaids_assoc_id[num++] = asoc->assoc_id; 7229 } 7230 7231 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) { 7232 kfree(ids); 7233 return -EFAULT; 7234 } 7235 7236 kfree(ids); 7237 return 0; 7238 } 7239 7240 /* 7241 * SCTP_PEER_ADDR_THLDS 7242 * 7243 * This option allows us to fetch the partially failed threshold for one or all 7244 * transports in an association. See Section 6.1 of: 7245 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt 7246 */ 7247 static int sctp_getsockopt_paddr_thresholds(struct sock *sk, 7248 char __user *optval, int len, 7249 int __user *optlen, bool v2) 7250 { 7251 struct sctp_paddrthlds_v2 val; 7252 struct sctp_transport *trans; 7253 struct sctp_association *asoc; 7254 int min; 7255 7256 min = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds); 7257 if (len < min) 7258 return -EINVAL; 7259 len = min; 7260 if (copy_from_user(&val, optval, len)) 7261 return -EFAULT; 7262 7263 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { 7264 trans = sctp_addr_id2transport(sk, &val.spt_address, 7265 val.spt_assoc_id); 7266 if (!trans) 7267 return -ENOENT; 7268 7269 val.spt_pathmaxrxt = trans->pathmaxrxt; 7270 val.spt_pathpfthld = trans->pf_retrans; 7271 val.spt_pathcpthld = trans->ps_retrans; 7272 7273 goto out; 7274 } 7275 7276 asoc = sctp_id2assoc(sk, val.spt_assoc_id); 7277 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC && 7278 sctp_style(sk, UDP)) 7279 return -EINVAL; 7280 7281 if (asoc) { 7282 val.spt_pathpfthld = asoc->pf_retrans; 7283 val.spt_pathmaxrxt = asoc->pathmaxrxt; 7284 val.spt_pathcpthld = asoc->ps_retrans; 7285 } else { 7286 struct sctp_sock *sp = sctp_sk(sk); 7287 7288 val.spt_pathpfthld = sp->pf_retrans; 7289 val.spt_pathmaxrxt = sp->pathmaxrxt; 7290 val.spt_pathcpthld = sp->ps_retrans; 7291 } 7292 7293 out: 7294 if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 7295 return -EFAULT; 7296 7297 return 0; 7298 } 7299 7300 /* 7301 * SCTP_GET_ASSOC_STATS 7302 * 7303 * This option retrieves local per endpoint statistics. It is modeled 7304 * after OpenSolaris' implementation 7305 */ 7306 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len, 7307 char __user *optval, 7308 int __user *optlen) 7309 { 7310 struct sctp_assoc_stats sas; 7311 struct sctp_association *asoc = NULL; 7312 7313 /* User must provide at least the assoc id */ 7314 if (len < sizeof(sctp_assoc_t)) 7315 return -EINVAL; 7316 7317 /* Allow the struct to grow and fill in as much as possible */ 7318 len = min_t(size_t, len, sizeof(sas)); 7319 7320 if (copy_from_user(&sas, optval, len)) 7321 return -EFAULT; 7322 7323 asoc = sctp_id2assoc(sk, sas.sas_assoc_id); 7324 if (!asoc) 7325 return -EINVAL; 7326 7327 sas.sas_rtxchunks = asoc->stats.rtxchunks; 7328 sas.sas_gapcnt = asoc->stats.gapcnt; 7329 sas.sas_outofseqtsns = asoc->stats.outofseqtsns; 7330 sas.sas_osacks = asoc->stats.osacks; 7331 sas.sas_isacks = asoc->stats.isacks; 7332 sas.sas_octrlchunks = asoc->stats.octrlchunks; 7333 sas.sas_ictrlchunks = asoc->stats.ictrlchunks; 7334 sas.sas_oodchunks = asoc->stats.oodchunks; 7335 sas.sas_iodchunks = asoc->stats.iodchunks; 7336 sas.sas_ouodchunks = asoc->stats.ouodchunks; 7337 sas.sas_iuodchunks = asoc->stats.iuodchunks; 7338 sas.sas_idupchunks = asoc->stats.idupchunks; 7339 sas.sas_opackets = asoc->stats.opackets; 7340 sas.sas_ipackets = asoc->stats.ipackets; 7341 7342 /* New high max rto observed, will return 0 if not a single 7343 * RTO update took place. obs_rto_ipaddr will be bogus 7344 * in such a case 7345 */ 7346 sas.sas_maxrto = asoc->stats.max_obs_rto; 7347 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr, 7348 sizeof(struct sockaddr_storage)); 7349 7350 /* Mark beginning of a new observation period */ 7351 asoc->stats.max_obs_rto = asoc->rto_min; 7352 7353 if (put_user(len, optlen)) 7354 return -EFAULT; 7355 7356 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id); 7357 7358 if (copy_to_user(optval, &sas, len)) 7359 return -EFAULT; 7360 7361 return 0; 7362 } 7363 7364 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len, 7365 char __user *optval, 7366 int __user *optlen) 7367 { 7368 int val = 0; 7369 7370 if (len < sizeof(int)) 7371 return -EINVAL; 7372 7373 len = sizeof(int); 7374 if (sctp_sk(sk)->recvrcvinfo) 7375 val = 1; 7376 if (put_user(len, optlen)) 7377 return -EFAULT; 7378 if (copy_to_user(optval, &val, len)) 7379 return -EFAULT; 7380 7381 return 0; 7382 } 7383 7384 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len, 7385 char __user *optval, 7386 int __user *optlen) 7387 { 7388 int val = 0; 7389 7390 if (len < sizeof(int)) 7391 return -EINVAL; 7392 7393 len = sizeof(int); 7394 if (sctp_sk(sk)->recvnxtinfo) 7395 val = 1; 7396 if (put_user(len, optlen)) 7397 return -EFAULT; 7398 if (copy_to_user(optval, &val, len)) 7399 return -EFAULT; 7400 7401 return 0; 7402 } 7403 7404 static int sctp_getsockopt_pr_supported(struct sock *sk, int len, 7405 char __user *optval, 7406 int __user *optlen) 7407 { 7408 struct sctp_assoc_value params; 7409 struct sctp_association *asoc; 7410 int retval = -EFAULT; 7411 7412 if (len < sizeof(params)) { 7413 retval = -EINVAL; 7414 goto out; 7415 } 7416 7417 len = sizeof(params); 7418 if (copy_from_user(¶ms, optval, len)) 7419 goto out; 7420 7421 asoc = sctp_id2assoc(sk, params.assoc_id); 7422 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7423 sctp_style(sk, UDP)) { 7424 retval = -EINVAL; 7425 goto out; 7426 } 7427 7428 params.assoc_value = asoc ? asoc->peer.prsctp_capable 7429 : sctp_sk(sk)->ep->prsctp_enable; 7430 7431 if (put_user(len, optlen)) 7432 goto out; 7433 7434 if (copy_to_user(optval, ¶ms, len)) 7435 goto out; 7436 7437 retval = 0; 7438 7439 out: 7440 return retval; 7441 } 7442 7443 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len, 7444 char __user *optval, 7445 int __user *optlen) 7446 { 7447 struct sctp_default_prinfo info; 7448 struct sctp_association *asoc; 7449 int retval = -EFAULT; 7450 7451 if (len < sizeof(info)) { 7452 retval = -EINVAL; 7453 goto out; 7454 } 7455 7456 len = sizeof(info); 7457 if (copy_from_user(&info, optval, len)) 7458 goto out; 7459 7460 asoc = sctp_id2assoc(sk, info.pr_assoc_id); 7461 if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC && 7462 sctp_style(sk, UDP)) { 7463 retval = -EINVAL; 7464 goto out; 7465 } 7466 7467 if (asoc) { 7468 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags); 7469 info.pr_value = asoc->default_timetolive; 7470 } else { 7471 struct sctp_sock *sp = sctp_sk(sk); 7472 7473 info.pr_policy = SCTP_PR_POLICY(sp->default_flags); 7474 info.pr_value = sp->default_timetolive; 7475 } 7476 7477 if (put_user(len, optlen)) 7478 goto out; 7479 7480 if (copy_to_user(optval, &info, len)) 7481 goto out; 7482 7483 retval = 0; 7484 7485 out: 7486 return retval; 7487 } 7488 7489 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len, 7490 char __user *optval, 7491 int __user *optlen) 7492 { 7493 struct sctp_prstatus params; 7494 struct sctp_association *asoc; 7495 int policy; 7496 int retval = -EINVAL; 7497 7498 if (len < sizeof(params)) 7499 goto out; 7500 7501 len = sizeof(params); 7502 if (copy_from_user(¶ms, optval, len)) { 7503 retval = -EFAULT; 7504 goto out; 7505 } 7506 7507 policy = params.sprstat_policy; 7508 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) || 7509 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK))) 7510 goto out; 7511 7512 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); 7513 if (!asoc) 7514 goto out; 7515 7516 if (policy == SCTP_PR_SCTP_ALL) { 7517 params.sprstat_abandoned_unsent = 0; 7518 params.sprstat_abandoned_sent = 0; 7519 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { 7520 params.sprstat_abandoned_unsent += 7521 asoc->abandoned_unsent[policy]; 7522 params.sprstat_abandoned_sent += 7523 asoc->abandoned_sent[policy]; 7524 } 7525 } else { 7526 params.sprstat_abandoned_unsent = 7527 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)]; 7528 params.sprstat_abandoned_sent = 7529 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)]; 7530 } 7531 7532 if (put_user(len, optlen)) { 7533 retval = -EFAULT; 7534 goto out; 7535 } 7536 7537 if (copy_to_user(optval, ¶ms, len)) { 7538 retval = -EFAULT; 7539 goto out; 7540 } 7541 7542 retval = 0; 7543 7544 out: 7545 return retval; 7546 } 7547 7548 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len, 7549 char __user *optval, 7550 int __user *optlen) 7551 { 7552 struct sctp_stream_out_ext *streamoute; 7553 struct sctp_association *asoc; 7554 struct sctp_prstatus params; 7555 int retval = -EINVAL; 7556 int policy; 7557 7558 if (len < sizeof(params)) 7559 goto out; 7560 7561 len = sizeof(params); 7562 if (copy_from_user(¶ms, optval, len)) { 7563 retval = -EFAULT; 7564 goto out; 7565 } 7566 7567 policy = params.sprstat_policy; 7568 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) || 7569 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK))) 7570 goto out; 7571 7572 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); 7573 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt) 7574 goto out; 7575 7576 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext; 7577 if (!streamoute) { 7578 /* Not allocated yet, means all stats are 0 */ 7579 params.sprstat_abandoned_unsent = 0; 7580 params.sprstat_abandoned_sent = 0; 7581 } else if (policy == SCTP_PR_SCTP_ALL) { 7582 params.sprstat_abandoned_unsent = 0; 7583 params.sprstat_abandoned_sent = 0; 7584 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { 7585 params.sprstat_abandoned_unsent += 7586 streamoute->abandoned_unsent[policy]; 7587 params.sprstat_abandoned_sent += 7588 streamoute->abandoned_sent[policy]; 7589 } 7590 } else { 7591 params.sprstat_abandoned_unsent = 7592 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)]; 7593 params.sprstat_abandoned_sent = 7594 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)]; 7595 } 7596 7597 if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) { 7598 retval = -EFAULT; 7599 goto out; 7600 } 7601 7602 retval = 0; 7603 7604 out: 7605 return retval; 7606 } 7607 7608 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len, 7609 char __user *optval, 7610 int __user *optlen) 7611 { 7612 struct sctp_assoc_value params; 7613 struct sctp_association *asoc; 7614 int retval = -EFAULT; 7615 7616 if (len < sizeof(params)) { 7617 retval = -EINVAL; 7618 goto out; 7619 } 7620 7621 len = sizeof(params); 7622 if (copy_from_user(¶ms, optval, len)) 7623 goto out; 7624 7625 asoc = sctp_id2assoc(sk, params.assoc_id); 7626 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7627 sctp_style(sk, UDP)) { 7628 retval = -EINVAL; 7629 goto out; 7630 } 7631 7632 params.assoc_value = asoc ? asoc->peer.reconf_capable 7633 : sctp_sk(sk)->ep->reconf_enable; 7634 7635 if (put_user(len, optlen)) 7636 goto out; 7637 7638 if (copy_to_user(optval, ¶ms, len)) 7639 goto out; 7640 7641 retval = 0; 7642 7643 out: 7644 return retval; 7645 } 7646 7647 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len, 7648 char __user *optval, 7649 int __user *optlen) 7650 { 7651 struct sctp_assoc_value params; 7652 struct sctp_association *asoc; 7653 int retval = -EFAULT; 7654 7655 if (len < sizeof(params)) { 7656 retval = -EINVAL; 7657 goto out; 7658 } 7659 7660 len = sizeof(params); 7661 if (copy_from_user(¶ms, optval, len)) 7662 goto out; 7663 7664 asoc = sctp_id2assoc(sk, params.assoc_id); 7665 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7666 sctp_style(sk, UDP)) { 7667 retval = -EINVAL; 7668 goto out; 7669 } 7670 7671 params.assoc_value = asoc ? asoc->strreset_enable 7672 : sctp_sk(sk)->ep->strreset_enable; 7673 7674 if (put_user(len, optlen)) 7675 goto out; 7676 7677 if (copy_to_user(optval, ¶ms, len)) 7678 goto out; 7679 7680 retval = 0; 7681 7682 out: 7683 return retval; 7684 } 7685 7686 static int sctp_getsockopt_scheduler(struct sock *sk, int len, 7687 char __user *optval, 7688 int __user *optlen) 7689 { 7690 struct sctp_assoc_value params; 7691 struct sctp_association *asoc; 7692 int retval = -EFAULT; 7693 7694 if (len < sizeof(params)) { 7695 retval = -EINVAL; 7696 goto out; 7697 } 7698 7699 len = sizeof(params); 7700 if (copy_from_user(¶ms, optval, len)) 7701 goto out; 7702 7703 asoc = sctp_id2assoc(sk, params.assoc_id); 7704 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7705 sctp_style(sk, UDP)) { 7706 retval = -EINVAL; 7707 goto out; 7708 } 7709 7710 params.assoc_value = asoc ? sctp_sched_get_sched(asoc) 7711 : sctp_sk(sk)->default_ss; 7712 7713 if (put_user(len, optlen)) 7714 goto out; 7715 7716 if (copy_to_user(optval, ¶ms, len)) 7717 goto out; 7718 7719 retval = 0; 7720 7721 out: 7722 return retval; 7723 } 7724 7725 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len, 7726 char __user *optval, 7727 int __user *optlen) 7728 { 7729 struct sctp_stream_value params; 7730 struct sctp_association *asoc; 7731 int retval = -EFAULT; 7732 7733 if (len < sizeof(params)) { 7734 retval = -EINVAL; 7735 goto out; 7736 } 7737 7738 len = sizeof(params); 7739 if (copy_from_user(¶ms, optval, len)) 7740 goto out; 7741 7742 asoc = sctp_id2assoc(sk, params.assoc_id); 7743 if (!asoc) { 7744 retval = -EINVAL; 7745 goto out; 7746 } 7747 7748 retval = sctp_sched_get_value(asoc, params.stream_id, 7749 ¶ms.stream_value); 7750 if (retval) 7751 goto out; 7752 7753 if (put_user(len, optlen)) { 7754 retval = -EFAULT; 7755 goto out; 7756 } 7757 7758 if (copy_to_user(optval, ¶ms, len)) { 7759 retval = -EFAULT; 7760 goto out; 7761 } 7762 7763 out: 7764 return retval; 7765 } 7766 7767 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len, 7768 char __user *optval, 7769 int __user *optlen) 7770 { 7771 struct sctp_assoc_value params; 7772 struct sctp_association *asoc; 7773 int retval = -EFAULT; 7774 7775 if (len < sizeof(params)) { 7776 retval = -EINVAL; 7777 goto out; 7778 } 7779 7780 len = sizeof(params); 7781 if (copy_from_user(¶ms, optval, len)) 7782 goto out; 7783 7784 asoc = sctp_id2assoc(sk, params.assoc_id); 7785 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7786 sctp_style(sk, UDP)) { 7787 retval = -EINVAL; 7788 goto out; 7789 } 7790 7791 params.assoc_value = asoc ? asoc->peer.intl_capable 7792 : sctp_sk(sk)->ep->intl_enable; 7793 7794 if (put_user(len, optlen)) 7795 goto out; 7796 7797 if (copy_to_user(optval, ¶ms, len)) 7798 goto out; 7799 7800 retval = 0; 7801 7802 out: 7803 return retval; 7804 } 7805 7806 static int sctp_getsockopt_reuse_port(struct sock *sk, int len, 7807 char __user *optval, 7808 int __user *optlen) 7809 { 7810 int val; 7811 7812 if (len < sizeof(int)) 7813 return -EINVAL; 7814 7815 len = sizeof(int); 7816 val = sctp_sk(sk)->reuse; 7817 if (put_user(len, optlen)) 7818 return -EFAULT; 7819 7820 if (copy_to_user(optval, &val, len)) 7821 return -EFAULT; 7822 7823 return 0; 7824 } 7825 7826 static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval, 7827 int __user *optlen) 7828 { 7829 struct sctp_association *asoc; 7830 struct sctp_event param; 7831 __u16 subscribe; 7832 7833 if (len < sizeof(param)) 7834 return -EINVAL; 7835 7836 len = sizeof(param); 7837 if (copy_from_user(¶m, optval, len)) 7838 return -EFAULT; 7839 7840 if (param.se_type < SCTP_SN_TYPE_BASE || 7841 param.se_type > SCTP_SN_TYPE_MAX) 7842 return -EINVAL; 7843 7844 asoc = sctp_id2assoc(sk, param.se_assoc_id); 7845 if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC && 7846 sctp_style(sk, UDP)) 7847 return -EINVAL; 7848 7849 subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe; 7850 param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type); 7851 7852 if (put_user(len, optlen)) 7853 return -EFAULT; 7854 7855 if (copy_to_user(optval, ¶m, len)) 7856 return -EFAULT; 7857 7858 return 0; 7859 } 7860 7861 static int sctp_getsockopt_asconf_supported(struct sock *sk, int len, 7862 char __user *optval, 7863 int __user *optlen) 7864 { 7865 struct sctp_assoc_value params; 7866 struct sctp_association *asoc; 7867 int retval = -EFAULT; 7868 7869 if (len < sizeof(params)) { 7870 retval = -EINVAL; 7871 goto out; 7872 } 7873 7874 len = sizeof(params); 7875 if (copy_from_user(¶ms, optval, len)) 7876 goto out; 7877 7878 asoc = sctp_id2assoc(sk, params.assoc_id); 7879 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7880 sctp_style(sk, UDP)) { 7881 retval = -EINVAL; 7882 goto out; 7883 } 7884 7885 params.assoc_value = asoc ? asoc->peer.asconf_capable 7886 : sctp_sk(sk)->ep->asconf_enable; 7887 7888 if (put_user(len, optlen)) 7889 goto out; 7890 7891 if (copy_to_user(optval, ¶ms, len)) 7892 goto out; 7893 7894 retval = 0; 7895 7896 out: 7897 return retval; 7898 } 7899 7900 static int sctp_getsockopt_auth_supported(struct sock *sk, int len, 7901 char __user *optval, 7902 int __user *optlen) 7903 { 7904 struct sctp_assoc_value params; 7905 struct sctp_association *asoc; 7906 int retval = -EFAULT; 7907 7908 if (len < sizeof(params)) { 7909 retval = -EINVAL; 7910 goto out; 7911 } 7912 7913 len = sizeof(params); 7914 if (copy_from_user(¶ms, optval, len)) 7915 goto out; 7916 7917 asoc = sctp_id2assoc(sk, params.assoc_id); 7918 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7919 sctp_style(sk, UDP)) { 7920 retval = -EINVAL; 7921 goto out; 7922 } 7923 7924 params.assoc_value = asoc ? asoc->peer.auth_capable 7925 : sctp_sk(sk)->ep->auth_enable; 7926 7927 if (put_user(len, optlen)) 7928 goto out; 7929 7930 if (copy_to_user(optval, ¶ms, len)) 7931 goto out; 7932 7933 retval = 0; 7934 7935 out: 7936 return retval; 7937 } 7938 7939 static int sctp_getsockopt_ecn_supported(struct sock *sk, int len, 7940 char __user *optval, 7941 int __user *optlen) 7942 { 7943 struct sctp_assoc_value params; 7944 struct sctp_association *asoc; 7945 int retval = -EFAULT; 7946 7947 if (len < sizeof(params)) { 7948 retval = -EINVAL; 7949 goto out; 7950 } 7951 7952 len = sizeof(params); 7953 if (copy_from_user(¶ms, optval, len)) 7954 goto out; 7955 7956 asoc = sctp_id2assoc(sk, params.assoc_id); 7957 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7958 sctp_style(sk, UDP)) { 7959 retval = -EINVAL; 7960 goto out; 7961 } 7962 7963 params.assoc_value = asoc ? asoc->peer.ecn_capable 7964 : sctp_sk(sk)->ep->ecn_enable; 7965 7966 if (put_user(len, optlen)) 7967 goto out; 7968 7969 if (copy_to_user(optval, ¶ms, len)) 7970 goto out; 7971 7972 retval = 0; 7973 7974 out: 7975 return retval; 7976 } 7977 7978 static int sctp_getsockopt_pf_expose(struct sock *sk, int len, 7979 char __user *optval, 7980 int __user *optlen) 7981 { 7982 struct sctp_assoc_value params; 7983 struct sctp_association *asoc; 7984 int retval = -EFAULT; 7985 7986 if (len < sizeof(params)) { 7987 retval = -EINVAL; 7988 goto out; 7989 } 7990 7991 len = sizeof(params); 7992 if (copy_from_user(¶ms, optval, len)) 7993 goto out; 7994 7995 asoc = sctp_id2assoc(sk, params.assoc_id); 7996 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && 7997 sctp_style(sk, UDP)) { 7998 retval = -EINVAL; 7999 goto out; 8000 } 8001 8002 params.assoc_value = asoc ? asoc->pf_expose 8003 : sctp_sk(sk)->pf_expose; 8004 8005 if (put_user(len, optlen)) 8006 goto out; 8007 8008 if (copy_to_user(optval, ¶ms, len)) 8009 goto out; 8010 8011 retval = 0; 8012 8013 out: 8014 return retval; 8015 } 8016 8017 static int sctp_getsockopt_encap_port(struct sock *sk, int len, 8018 char __user *optval, int __user *optlen) 8019 { 8020 struct sctp_association *asoc; 8021 struct sctp_udpencaps encap; 8022 struct sctp_transport *t; 8023 __be16 encap_port; 8024 8025 if (len < sizeof(encap)) 8026 return -EINVAL; 8027 8028 len = sizeof(encap); 8029 if (copy_from_user(&encap, optval, len)) 8030 return -EFAULT; 8031 8032 /* If an address other than INADDR_ANY is specified, and 8033 * no transport is found, then the request is invalid. 8034 */ 8035 if (!sctp_is_any(sk, (union sctp_addr *)&encap.sue_address)) { 8036 t = sctp_addr_id2transport(sk, &encap.sue_address, 8037 encap.sue_assoc_id); 8038 if (!t) { 8039 pr_debug("%s: failed no transport\n", __func__); 8040 return -EINVAL; 8041 } 8042 8043 encap_port = t->encap_port; 8044 goto out; 8045 } 8046 8047 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the 8048 * socket is a one to many style socket, and an association 8049 * was not found, then the id was invalid. 8050 */ 8051 asoc = sctp_id2assoc(sk, encap.sue_assoc_id); 8052 if (!asoc && encap.sue_assoc_id != SCTP_FUTURE_ASSOC && 8053 sctp_style(sk, UDP)) { 8054 pr_debug("%s: failed no association\n", __func__); 8055 return -EINVAL; 8056 } 8057 8058 if (asoc) { 8059 encap_port = asoc->encap_port; 8060 goto out; 8061 } 8062 8063 encap_port = sctp_sk(sk)->encap_port; 8064 8065 out: 8066 encap.sue_port = (__force uint16_t)encap_port; 8067 if (copy_to_user(optval, &encap, len)) 8068 return -EFAULT; 8069 8070 if (put_user(len, optlen)) 8071 return -EFAULT; 8072 8073 return 0; 8074 } 8075 8076 static int sctp_getsockopt_probe_interval(struct sock *sk, int len, 8077 char __user *optval, 8078 int __user *optlen) 8079 { 8080 struct sctp_probeinterval params; 8081 struct sctp_association *asoc; 8082 struct sctp_transport *t; 8083 __u32 probe_interval; 8084 8085 if (len < sizeof(params)) 8086 return -EINVAL; 8087 8088 len = sizeof(params); 8089 if (copy_from_user(¶ms, optval, len)) 8090 return -EFAULT; 8091 8092 /* If an address other than INADDR_ANY is specified, and 8093 * no transport is found, then the request is invalid. 8094 */ 8095 if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spi_address)) { 8096 t = sctp_addr_id2transport(sk, ¶ms.spi_address, 8097 params.spi_assoc_id); 8098 if (!t) { 8099 pr_debug("%s: failed no transport\n", __func__); 8100 return -EINVAL; 8101 } 8102 8103 probe_interval = jiffies_to_msecs(t->probe_interval); 8104 goto out; 8105 } 8106 8107 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the 8108 * socket is a one to many style socket, and an association 8109 * was not found, then the id was invalid. 8110 */ 8111 asoc = sctp_id2assoc(sk, params.spi_assoc_id); 8112 if (!asoc && params.spi_assoc_id != SCTP_FUTURE_ASSOC && 8113 sctp_style(sk, UDP)) { 8114 pr_debug("%s: failed no association\n", __func__); 8115 return -EINVAL; 8116 } 8117 8118 if (asoc) { 8119 probe_interval = jiffies_to_msecs(asoc->probe_interval); 8120 goto out; 8121 } 8122 8123 probe_interval = sctp_sk(sk)->probe_interval; 8124 8125 out: 8126 params.spi_interval = probe_interval; 8127 if (copy_to_user(optval, ¶ms, len)) 8128 return -EFAULT; 8129 8130 if (put_user(len, optlen)) 8131 return -EFAULT; 8132 8133 return 0; 8134 } 8135 8136 static int sctp_getsockopt(struct sock *sk, int level, int optname, 8137 char __user *optval, int __user *optlen) 8138 { 8139 int retval = 0; 8140 int len; 8141 8142 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); 8143 8144 /* I can hardly begin to describe how wrong this is. This is 8145 * so broken as to be worse than useless. The API draft 8146 * REALLY is NOT helpful here... I am not convinced that the 8147 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP 8148 * are at all well-founded. 8149 */ 8150 if (level != SOL_SCTP) { 8151 struct sctp_af *af = sctp_sk(sk)->pf->af; 8152 8153 retval = af->getsockopt(sk, level, optname, optval, optlen); 8154 return retval; 8155 } 8156 8157 if (get_user(len, optlen)) 8158 return -EFAULT; 8159 8160 if (len < 0) 8161 return -EINVAL; 8162 8163 lock_sock(sk); 8164 8165 switch (optname) { 8166 case SCTP_STATUS: 8167 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); 8168 break; 8169 case SCTP_DISABLE_FRAGMENTS: 8170 retval = sctp_getsockopt_disable_fragments(sk, len, optval, 8171 optlen); 8172 break; 8173 case SCTP_EVENTS: 8174 retval = sctp_getsockopt_events(sk, len, optval, optlen); 8175 break; 8176 case SCTP_AUTOCLOSE: 8177 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); 8178 break; 8179 case SCTP_SOCKOPT_PEELOFF: 8180 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); 8181 break; 8182 case SCTP_SOCKOPT_PEELOFF_FLAGS: 8183 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen); 8184 break; 8185 case SCTP_PEER_ADDR_PARAMS: 8186 retval = sctp_getsockopt_peer_addr_params(sk, len, optval, 8187 optlen); 8188 break; 8189 case SCTP_DELAYED_SACK: 8190 retval = sctp_getsockopt_delayed_ack(sk, len, optval, 8191 optlen); 8192 break; 8193 case SCTP_INITMSG: 8194 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); 8195 break; 8196 case SCTP_GET_PEER_ADDRS: 8197 retval = sctp_getsockopt_peer_addrs(sk, len, optval, 8198 optlen); 8199 break; 8200 case SCTP_GET_LOCAL_ADDRS: 8201 retval = sctp_getsockopt_local_addrs(sk, len, optval, 8202 optlen); 8203 break; 8204 case SCTP_SOCKOPT_CONNECTX3: 8205 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen); 8206 break; 8207 case SCTP_DEFAULT_SEND_PARAM: 8208 retval = sctp_getsockopt_default_send_param(sk, len, 8209 optval, optlen); 8210 break; 8211 case SCTP_DEFAULT_SNDINFO: 8212 retval = sctp_getsockopt_default_sndinfo(sk, len, 8213 optval, optlen); 8214 break; 8215 case SCTP_PRIMARY_ADDR: 8216 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); 8217 break; 8218 case SCTP_NODELAY: 8219 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); 8220 break; 8221 case SCTP_RTOINFO: 8222 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); 8223 break; 8224 case SCTP_ASSOCINFO: 8225 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); 8226 break; 8227 case SCTP_I_WANT_MAPPED_V4_ADDR: 8228 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); 8229 break; 8230 case SCTP_MAXSEG: 8231 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); 8232 break; 8233 case SCTP_GET_PEER_ADDR_INFO: 8234 retval = sctp_getsockopt_peer_addr_info(sk, len, optval, 8235 optlen); 8236 break; 8237 case SCTP_ADAPTATION_LAYER: 8238 retval = sctp_getsockopt_adaptation_layer(sk, len, optval, 8239 optlen); 8240 break; 8241 case SCTP_CONTEXT: 8242 retval = sctp_getsockopt_context(sk, len, optval, optlen); 8243 break; 8244 case SCTP_FRAGMENT_INTERLEAVE: 8245 retval = sctp_getsockopt_fragment_interleave(sk, len, optval, 8246 optlen); 8247 break; 8248 case SCTP_PARTIAL_DELIVERY_POINT: 8249 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, 8250 optlen); 8251 break; 8252 case SCTP_MAX_BURST: 8253 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); 8254 break; 8255 case SCTP_AUTH_KEY: 8256 case SCTP_AUTH_CHUNK: 8257 case SCTP_AUTH_DELETE_KEY: 8258 case SCTP_AUTH_DEACTIVATE_KEY: 8259 retval = -EOPNOTSUPP; 8260 break; 8261 case SCTP_HMAC_IDENT: 8262 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen); 8263 break; 8264 case SCTP_AUTH_ACTIVE_KEY: 8265 retval = sctp_getsockopt_active_key(sk, len, optval, optlen); 8266 break; 8267 case SCTP_PEER_AUTH_CHUNKS: 8268 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval, 8269 optlen); 8270 break; 8271 case SCTP_LOCAL_AUTH_CHUNKS: 8272 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval, 8273 optlen); 8274 break; 8275 case SCTP_GET_ASSOC_NUMBER: 8276 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen); 8277 break; 8278 case SCTP_GET_ASSOC_ID_LIST: 8279 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen); 8280 break; 8281 case SCTP_AUTO_ASCONF: 8282 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen); 8283 break; 8284 case SCTP_PEER_ADDR_THLDS: 8285 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, 8286 optlen, false); 8287 break; 8288 case SCTP_PEER_ADDR_THLDS_V2: 8289 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, 8290 optlen, true); 8291 break; 8292 case SCTP_GET_ASSOC_STATS: 8293 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen); 8294 break; 8295 case SCTP_RECVRCVINFO: 8296 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen); 8297 break; 8298 case SCTP_RECVNXTINFO: 8299 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen); 8300 break; 8301 case SCTP_PR_SUPPORTED: 8302 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen); 8303 break; 8304 case SCTP_DEFAULT_PRINFO: 8305 retval = sctp_getsockopt_default_prinfo(sk, len, optval, 8306 optlen); 8307 break; 8308 case SCTP_PR_ASSOC_STATUS: 8309 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval, 8310 optlen); 8311 break; 8312 case SCTP_PR_STREAM_STATUS: 8313 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval, 8314 optlen); 8315 break; 8316 case SCTP_RECONFIG_SUPPORTED: 8317 retval = sctp_getsockopt_reconfig_supported(sk, len, optval, 8318 optlen); 8319 break; 8320 case SCTP_ENABLE_STREAM_RESET: 8321 retval = sctp_getsockopt_enable_strreset(sk, len, optval, 8322 optlen); 8323 break; 8324 case SCTP_STREAM_SCHEDULER: 8325 retval = sctp_getsockopt_scheduler(sk, len, optval, 8326 optlen); 8327 break; 8328 case SCTP_STREAM_SCHEDULER_VALUE: 8329 retval = sctp_getsockopt_scheduler_value(sk, len, optval, 8330 optlen); 8331 break; 8332 case SCTP_INTERLEAVING_SUPPORTED: 8333 retval = sctp_getsockopt_interleaving_supported(sk, len, optval, 8334 optlen); 8335 break; 8336 case SCTP_REUSE_PORT: 8337 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen); 8338 break; 8339 case SCTP_EVENT: 8340 retval = sctp_getsockopt_event(sk, len, optval, optlen); 8341 break; 8342 case SCTP_ASCONF_SUPPORTED: 8343 retval = sctp_getsockopt_asconf_supported(sk, len, optval, 8344 optlen); 8345 break; 8346 case SCTP_AUTH_SUPPORTED: 8347 retval = sctp_getsockopt_auth_supported(sk, len, optval, 8348 optlen); 8349 break; 8350 case SCTP_ECN_SUPPORTED: 8351 retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen); 8352 break; 8353 case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE: 8354 retval = sctp_getsockopt_pf_expose(sk, len, optval, optlen); 8355 break; 8356 case SCTP_REMOTE_UDP_ENCAPS_PORT: 8357 retval = sctp_getsockopt_encap_port(sk, len, optval, optlen); 8358 break; 8359 case SCTP_PLPMTUD_PROBE_INTERVAL: 8360 retval = sctp_getsockopt_probe_interval(sk, len, optval, optlen); 8361 break; 8362 default: 8363 retval = -ENOPROTOOPT; 8364 break; 8365 } 8366 8367 release_sock(sk); 8368 return retval; 8369 } 8370 8371 static bool sctp_bpf_bypass_getsockopt(int level, int optname) 8372 { 8373 if (level == SOL_SCTP) { 8374 switch (optname) { 8375 case SCTP_SOCKOPT_PEELOFF: 8376 case SCTP_SOCKOPT_PEELOFF_FLAGS: 8377 case SCTP_SOCKOPT_CONNECTX3: 8378 return true; 8379 default: 8380 return false; 8381 } 8382 } 8383 8384 return false; 8385 } 8386 8387 static int sctp_hash(struct sock *sk) 8388 { 8389 /* STUB */ 8390 return 0; 8391 } 8392 8393 static void sctp_unhash(struct sock *sk) 8394 { 8395 sock_rps_delete_flow(sk); 8396 } 8397 8398 /* Check if port is acceptable. Possibly find first available port. 8399 * 8400 * The port hash table (contained in the 'global' SCTP protocol storage 8401 * returned by struct sctp_protocol *sctp_get_protocol()). The hash 8402 * table is an array of 4096 lists (sctp_bind_hashbucket). Each 8403 * list (the list number is the port number hashed out, so as you 8404 * would expect from a hash function, all the ports in a given list have 8405 * such a number that hashes out to the same list number; you were 8406 * expecting that, right?); so each list has a set of ports, with a 8407 * link to the socket (struct sock) that uses it, the port number and 8408 * a fastreuse flag (FIXME: NPI ipg). 8409 */ 8410 static struct sctp_bind_bucket *sctp_bucket_create( 8411 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum); 8412 8413 static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr) 8414 { 8415 struct sctp_sock *sp = sctp_sk(sk); 8416 bool reuse = (sk->sk_reuse || sp->reuse); 8417 struct sctp_bind_hashbucket *head; /* hash list */ 8418 struct net *net = sock_net(sk); 8419 struct sctp_bind_bucket *pp; 8420 kuid_t uid = sk_uid(sk); 8421 unsigned short snum; 8422 int ret; 8423 8424 snum = ntohs(addr->v4.sin_port); 8425 8426 pr_debug("%s: begins, snum:%d\n", __func__, snum); 8427 8428 if (snum == 0) { 8429 /* Search for an available port. */ 8430 int low, high, remaining, index; 8431 unsigned int rover; 8432 8433 inet_sk_get_local_port_range(sk, &low, &high); 8434 remaining = (high - low) + 1; 8435 rover = get_random_u32_below(remaining) + low; 8436 8437 do { 8438 rover++; 8439 if ((rover < low) || (rover > high)) 8440 rover = low; 8441 if (inet_is_local_reserved_port(net, rover)) 8442 continue; 8443 index = sctp_phashfn(net, rover); 8444 head = &sctp_port_hashtable[index]; 8445 spin_lock_bh(&head->lock); 8446 sctp_for_each_hentry(pp, &head->chain) 8447 if ((pp->port == rover) && 8448 net_eq(net, pp->net)) 8449 goto next; 8450 break; 8451 next: 8452 spin_unlock_bh(&head->lock); 8453 cond_resched(); 8454 } while (--remaining > 0); 8455 8456 /* Exhausted local port range during search? */ 8457 ret = 1; 8458 if (remaining <= 0) 8459 return ret; 8460 8461 /* OK, here is the one we will use. HEAD (the port 8462 * hash table list entry) is non-NULL and we hold it's 8463 * mutex. 8464 */ 8465 snum = rover; 8466 } else { 8467 /* We are given an specific port number; we verify 8468 * that it is not being used. If it is used, we will 8469 * exahust the search in the hash list corresponding 8470 * to the port number (snum) - we detect that with the 8471 * port iterator, pp being NULL. 8472 */ 8473 head = &sctp_port_hashtable[sctp_phashfn(net, snum)]; 8474 spin_lock_bh(&head->lock); 8475 sctp_for_each_hentry(pp, &head->chain) { 8476 if ((pp->port == snum) && net_eq(pp->net, net)) 8477 goto pp_found; 8478 } 8479 } 8480 pp = NULL; 8481 goto pp_not_found; 8482 pp_found: 8483 if (!hlist_empty(&pp->owner)) { 8484 /* We had a port hash table hit - there is an 8485 * available port (pp != NULL) and it is being 8486 * used by other socket (pp->owner not empty); that other 8487 * socket is going to be sk2. 8488 */ 8489 struct sock *sk2; 8490 8491 pr_debug("%s: found a possible match\n", __func__); 8492 8493 if ((pp->fastreuse && reuse && 8494 sk->sk_state != SCTP_SS_LISTENING) || 8495 (pp->fastreuseport && sk->sk_reuseport && 8496 uid_eq(pp->fastuid, uid))) 8497 goto success; 8498 8499 /* Run through the list of sockets bound to the port 8500 * (pp->port) [via the pointers bind_next and 8501 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, 8502 * we get the endpoint they describe and run through 8503 * the endpoint's list of IP (v4 or v6) addresses, 8504 * comparing each of the addresses with the address of 8505 * the socket sk. If we find a match, then that means 8506 * that this port/socket (sk) combination are already 8507 * in an endpoint. 8508 */ 8509 sk_for_each_bound(sk2, &pp->owner) { 8510 int bound_dev_if2 = READ_ONCE(sk2->sk_bound_dev_if); 8511 struct sctp_sock *sp2 = sctp_sk(sk2); 8512 struct sctp_endpoint *ep2 = sp2->ep; 8513 8514 if (sk == sk2 || 8515 (reuse && (sk2->sk_reuse || sp2->reuse) && 8516 sk2->sk_state != SCTP_SS_LISTENING) || 8517 (sk->sk_reuseport && sk2->sk_reuseport && 8518 uid_eq(uid, sk_uid(sk2)))) 8519 continue; 8520 8521 if ((!sk->sk_bound_dev_if || !bound_dev_if2 || 8522 sk->sk_bound_dev_if == bound_dev_if2) && 8523 sctp_bind_addr_conflict(&ep2->base.bind_addr, 8524 addr, sp2, sp)) { 8525 ret = 1; 8526 goto fail_unlock; 8527 } 8528 } 8529 8530 pr_debug("%s: found a match\n", __func__); 8531 } 8532 pp_not_found: 8533 /* If there was a hash table miss, create a new port. */ 8534 ret = 1; 8535 if (!pp && !(pp = sctp_bucket_create(head, net, snum))) 8536 goto fail_unlock; 8537 8538 /* In either case (hit or miss), make sure fastreuse is 1 only 8539 * if sk->sk_reuse is too (that is, if the caller requested 8540 * SO_REUSEADDR on this socket -sk-). 8541 */ 8542 if (hlist_empty(&pp->owner)) { 8543 if (reuse && sk->sk_state != SCTP_SS_LISTENING) 8544 pp->fastreuse = 1; 8545 else 8546 pp->fastreuse = 0; 8547 8548 if (sk->sk_reuseport) { 8549 pp->fastreuseport = 1; 8550 pp->fastuid = uid; 8551 } else { 8552 pp->fastreuseport = 0; 8553 } 8554 } else { 8555 if (pp->fastreuse && 8556 (!reuse || sk->sk_state == SCTP_SS_LISTENING)) 8557 pp->fastreuse = 0; 8558 8559 if (pp->fastreuseport && 8560 (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid))) 8561 pp->fastreuseport = 0; 8562 } 8563 8564 /* We are set, so fill up all the data in the hash table 8565 * entry, tie the socket list information with the rest of the 8566 * sockets FIXME: Blurry, NPI (ipg). 8567 */ 8568 success: 8569 if (!sp->bind_hash) { 8570 inet_sk(sk)->inet_num = snum; 8571 sk_add_bind_node(sk, &pp->owner); 8572 sp->bind_hash = pp; 8573 } 8574 ret = 0; 8575 8576 fail_unlock: 8577 spin_unlock_bh(&head->lock); 8578 return ret; 8579 } 8580 8581 /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral 8582 * port is requested. 8583 */ 8584 static int sctp_get_port(struct sock *sk, unsigned short snum) 8585 { 8586 union sctp_addr addr; 8587 struct sctp_af *af = sctp_sk(sk)->pf->af; 8588 8589 /* Set up a dummy address struct from the sk. */ 8590 af->from_sk(&addr, sk); 8591 addr.v4.sin_port = htons(snum); 8592 8593 /* Note: sk->sk_num gets filled in if ephemeral port request. */ 8594 return sctp_get_port_local(sk, &addr); 8595 } 8596 8597 /* 8598 * Move a socket to LISTENING state. 8599 */ 8600 static int sctp_listen_start(struct sock *sk, int backlog) 8601 { 8602 struct sctp_sock *sp = sctp_sk(sk); 8603 struct sctp_endpoint *ep = sp->ep; 8604 int err; 8605 8606 /* 8607 * If a bind() or sctp_bindx() is not called prior to a listen() 8608 * call that allows new associations to be accepted, the system 8609 * picks an ephemeral port and will choose an address set equivalent 8610 * to binding with a wildcard address. 8611 * 8612 * This is not currently spelled out in the SCTP sockets 8613 * extensions draft, but follows the practice as seen in TCP 8614 * sockets. 8615 * 8616 */ 8617 inet_sk_set_state(sk, SCTP_SS_LISTENING); 8618 if (!ep->base.bind_addr.port) { 8619 if (sctp_autobind(sk)) { 8620 err = -EAGAIN; 8621 goto err; 8622 } 8623 } else { 8624 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) { 8625 err = -EADDRINUSE; 8626 goto err; 8627 } 8628 } 8629 8630 WRITE_ONCE(sk->sk_max_ack_backlog, backlog); 8631 err = sctp_hash_endpoint(ep); 8632 if (err) 8633 goto err; 8634 8635 return 0; 8636 err: 8637 inet_sk_set_state(sk, SCTP_SS_CLOSED); 8638 return err; 8639 } 8640 8641 /* 8642 * 4.1.3 / 5.1.3 listen() 8643 * 8644 * By default, new associations are not accepted for UDP style sockets. 8645 * An application uses listen() to mark a socket as being able to 8646 * accept new associations. 8647 * 8648 * On TCP style sockets, applications use listen() to ready the SCTP 8649 * endpoint for accepting inbound associations. 8650 * 8651 * On both types of endpoints a backlog of '0' disables listening. 8652 * 8653 * Move a socket to LISTENING state. 8654 */ 8655 int sctp_inet_listen(struct socket *sock, int backlog) 8656 { 8657 struct sock *sk = sock->sk; 8658 struct sctp_endpoint *ep = sctp_sk(sk)->ep; 8659 int err = -EINVAL; 8660 8661 if (unlikely(backlog < 0)) 8662 return err; 8663 8664 lock_sock(sk); 8665 8666 /* Peeled-off sockets are not allowed to listen(). */ 8667 if (sctp_style(sk, UDP_HIGH_BANDWIDTH)) 8668 goto out; 8669 8670 if (sock->state != SS_UNCONNECTED) 8671 goto out; 8672 8673 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED)) 8674 goto out; 8675 8676 /* If backlog is zero, disable listening. */ 8677 if (!backlog) { 8678 if (sctp_sstate(sk, CLOSED)) 8679 goto out; 8680 8681 err = 0; 8682 sctp_unhash_endpoint(ep); 8683 sk->sk_state = SCTP_SS_CLOSED; 8684 if (sk->sk_reuse || sctp_sk(sk)->reuse) 8685 sctp_sk(sk)->bind_hash->fastreuse = 1; 8686 goto out; 8687 } 8688 8689 /* If we are already listening, just update the backlog */ 8690 if (sctp_sstate(sk, LISTENING)) 8691 WRITE_ONCE(sk->sk_max_ack_backlog, backlog); 8692 else { 8693 err = sctp_listen_start(sk, backlog); 8694 if (err) 8695 goto out; 8696 } 8697 8698 err = 0; 8699 out: 8700 release_sock(sk); 8701 return err; 8702 } 8703 8704 /* 8705 * This function is done by modeling the current datagram_poll() and the 8706 * tcp_poll(). Note that, based on these implementations, we don't 8707 * lock the socket in this function, even though it seems that, 8708 * ideally, locking or some other mechanisms can be used to ensure 8709 * the integrity of the counters (sndbuf and wmem_alloc) used 8710 * in this place. We assume that we don't need locks either until proven 8711 * otherwise. 8712 * 8713 * Another thing to note is that we include the Async I/O support 8714 * here, again, by modeling the current TCP/UDP code. We don't have 8715 * a good way to test with it yet. 8716 */ 8717 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait) 8718 { 8719 struct sock *sk = sock->sk; 8720 struct sctp_sock *sp = sctp_sk(sk); 8721 __poll_t mask; 8722 8723 poll_wait(file, sk_sleep(sk), wait); 8724 8725 sock_rps_record_flow(sk); 8726 8727 /* A TCP-style listening socket becomes readable when the accept queue 8728 * is not empty. 8729 */ 8730 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) 8731 return (!list_empty(&sp->ep->asocs)) ? 8732 (EPOLLIN | EPOLLRDNORM) : 0; 8733 8734 mask = 0; 8735 8736 /* Is there any exceptional events? */ 8737 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue)) 8738 mask |= EPOLLERR | 8739 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); 8740 if (sk->sk_shutdown & RCV_SHUTDOWN) 8741 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; 8742 if (sk->sk_shutdown == SHUTDOWN_MASK) 8743 mask |= EPOLLHUP; 8744 8745 /* Is it readable? Reconsider this code with TCP-style support. */ 8746 if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) 8747 mask |= EPOLLIN | EPOLLRDNORM; 8748 8749 /* The association is either gone or not ready. */ 8750 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) 8751 return mask; 8752 8753 /* Is it writable? */ 8754 if (sctp_writeable(sk)) { 8755 mask |= EPOLLOUT | EPOLLWRNORM; 8756 } else { 8757 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 8758 /* 8759 * Since the socket is not locked, the buffer 8760 * might be made available after the writeable check and 8761 * before the bit is set. This could cause a lost I/O 8762 * signal. tcp_poll() has a race breaker for this race 8763 * condition. Based on their implementation, we put 8764 * in the following code to cover it as well. 8765 */ 8766 if (sctp_writeable(sk)) 8767 mask |= EPOLLOUT | EPOLLWRNORM; 8768 } 8769 return mask; 8770 } 8771 8772 /******************************************************************** 8773 * 2nd Level Abstractions 8774 ********************************************************************/ 8775 8776 static struct sctp_bind_bucket *sctp_bucket_create( 8777 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum) 8778 { 8779 struct sctp_bind_bucket *pp; 8780 8781 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); 8782 if (pp) { 8783 SCTP_DBG_OBJCNT_INC(bind_bucket); 8784 pp->port = snum; 8785 pp->fastreuse = 0; 8786 INIT_HLIST_HEAD(&pp->owner); 8787 pp->net = net; 8788 hlist_add_head(&pp->node, &head->chain); 8789 } 8790 return pp; 8791 } 8792 8793 /* Caller must hold hashbucket lock for this tb with local BH disabled */ 8794 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) 8795 { 8796 if (pp && hlist_empty(&pp->owner)) { 8797 __hlist_del(&pp->node); 8798 kmem_cache_free(sctp_bucket_cachep, pp); 8799 SCTP_DBG_OBJCNT_DEC(bind_bucket); 8800 } 8801 } 8802 8803 /* Release this socket's reference to a local port. */ 8804 static inline void __sctp_put_port(struct sock *sk) 8805 { 8806 struct sctp_bind_hashbucket *head = 8807 &sctp_port_hashtable[sctp_phashfn(sock_net(sk), 8808 inet_sk(sk)->inet_num)]; 8809 struct sctp_bind_bucket *pp; 8810 8811 spin_lock(&head->lock); 8812 pp = sctp_sk(sk)->bind_hash; 8813 __sk_del_bind_node(sk); 8814 sctp_sk(sk)->bind_hash = NULL; 8815 inet_sk(sk)->inet_num = 0; 8816 sctp_bucket_destroy(pp); 8817 spin_unlock(&head->lock); 8818 } 8819 8820 void sctp_put_port(struct sock *sk) 8821 { 8822 local_bh_disable(); 8823 __sctp_put_port(sk); 8824 local_bh_enable(); 8825 } 8826 8827 /* 8828 * The system picks an ephemeral port and choose an address set equivalent 8829 * to binding with a wildcard address. 8830 * One of those addresses will be the primary address for the association. 8831 * This automatically enables the multihoming capability of SCTP. 8832 */ 8833 static int sctp_autobind(struct sock *sk) 8834 { 8835 union sctp_addr autoaddr; 8836 struct sctp_af *af; 8837 __be16 port; 8838 8839 /* Initialize a local sockaddr structure to INADDR_ANY. */ 8840 af = sctp_sk(sk)->pf->af; 8841 8842 port = htons(inet_sk(sk)->inet_num); 8843 af->inaddr_any(&autoaddr, port); 8844 8845 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); 8846 } 8847 8848 /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation. 8849 * 8850 * From RFC 2292 8851 * 4.2 The cmsghdr Structure * 8852 * 8853 * When ancillary data is sent or received, any number of ancillary data 8854 * objects can be specified by the msg_control and msg_controllen members of 8855 * the msghdr structure, because each object is preceded by 8856 * a cmsghdr structure defining the object's length (the cmsg_len member). 8857 * Historically Berkeley-derived implementations have passed only one object 8858 * at a time, but this API allows multiple objects to be 8859 * passed in a single call to sendmsg() or recvmsg(). The following example 8860 * shows two ancillary data objects in a control buffer. 8861 * 8862 * |<--------------------------- msg_controllen -------------------------->| 8863 * | | 8864 * 8865 * |<----- ancillary data object ----->|<----- ancillary data object ----->| 8866 * 8867 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| 8868 * | | | 8869 * 8870 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| | 8871 * 8872 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| | 8873 * | | | | | 8874 * 8875 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ 8876 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX| 8877 * 8878 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX| 8879 * 8880 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ 8881 * ^ 8882 * | 8883 * 8884 * msg_control 8885 * points here 8886 */ 8887 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs) 8888 { 8889 struct msghdr *my_msg = (struct msghdr *)msg; 8890 struct cmsghdr *cmsg; 8891 8892 for_each_cmsghdr(cmsg, my_msg) { 8893 if (!CMSG_OK(my_msg, cmsg)) 8894 return -EINVAL; 8895 8896 /* Should we parse this header or ignore? */ 8897 if (cmsg->cmsg_level != IPPROTO_SCTP) 8898 continue; 8899 8900 /* Strictly check lengths following example in SCM code. */ 8901 switch (cmsg->cmsg_type) { 8902 case SCTP_INIT: 8903 /* SCTP Socket API Extension 8904 * 5.3.1 SCTP Initiation Structure (SCTP_INIT) 8905 * 8906 * This cmsghdr structure provides information for 8907 * initializing new SCTP associations with sendmsg(). 8908 * The SCTP_INITMSG socket option uses this same data 8909 * structure. This structure is not used for 8910 * recvmsg(). 8911 * 8912 * cmsg_level cmsg_type cmsg_data[] 8913 * ------------ ------------ ---------------------- 8914 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg 8915 */ 8916 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg))) 8917 return -EINVAL; 8918 8919 cmsgs->init = CMSG_DATA(cmsg); 8920 break; 8921 8922 case SCTP_SNDRCV: 8923 /* SCTP Socket API Extension 8924 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV) 8925 * 8926 * This cmsghdr structure specifies SCTP options for 8927 * sendmsg() and describes SCTP header information 8928 * about a received message through recvmsg(). 8929 * 8930 * cmsg_level cmsg_type cmsg_data[] 8931 * ------------ ------------ ---------------------- 8932 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo 8933 */ 8934 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) 8935 return -EINVAL; 8936 8937 cmsgs->srinfo = CMSG_DATA(cmsg); 8938 8939 if (cmsgs->srinfo->sinfo_flags & 8940 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 8941 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL | 8942 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF)) 8943 return -EINVAL; 8944 break; 8945 8946 case SCTP_SNDINFO: 8947 /* SCTP Socket API Extension 8948 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO) 8949 * 8950 * This cmsghdr structure specifies SCTP options for 8951 * sendmsg(). This structure and SCTP_RCVINFO replaces 8952 * SCTP_SNDRCV which has been deprecated. 8953 * 8954 * cmsg_level cmsg_type cmsg_data[] 8955 * ------------ ------------ --------------------- 8956 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo 8957 */ 8958 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo))) 8959 return -EINVAL; 8960 8961 cmsgs->sinfo = CMSG_DATA(cmsg); 8962 8963 if (cmsgs->sinfo->snd_flags & 8964 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 8965 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL | 8966 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF)) 8967 return -EINVAL; 8968 break; 8969 case SCTP_PRINFO: 8970 /* SCTP Socket API Extension 8971 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO) 8972 * 8973 * This cmsghdr structure specifies SCTP options for sendmsg(). 8974 * 8975 * cmsg_level cmsg_type cmsg_data[] 8976 * ------------ ------------ --------------------- 8977 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo 8978 */ 8979 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo))) 8980 return -EINVAL; 8981 8982 cmsgs->prinfo = CMSG_DATA(cmsg); 8983 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK) 8984 return -EINVAL; 8985 8986 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE) 8987 cmsgs->prinfo->pr_value = 0; 8988 break; 8989 case SCTP_AUTHINFO: 8990 /* SCTP Socket API Extension 8991 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO) 8992 * 8993 * This cmsghdr structure specifies SCTP options for sendmsg(). 8994 * 8995 * cmsg_level cmsg_type cmsg_data[] 8996 * ------------ ------------ --------------------- 8997 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo 8998 */ 8999 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo))) 9000 return -EINVAL; 9001 9002 cmsgs->authinfo = CMSG_DATA(cmsg); 9003 break; 9004 case SCTP_DSTADDRV4: 9005 case SCTP_DSTADDRV6: 9006 /* SCTP Socket API Extension 9007 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6) 9008 * 9009 * This cmsghdr structure specifies SCTP options for sendmsg(). 9010 * 9011 * cmsg_level cmsg_type cmsg_data[] 9012 * ------------ ------------ --------------------- 9013 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr 9014 * ------------ ------------ --------------------- 9015 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr 9016 */ 9017 cmsgs->addrs_msg = my_msg; 9018 break; 9019 default: 9020 return -EINVAL; 9021 } 9022 } 9023 9024 return 0; 9025 } 9026 9027 /* 9028 * Wait for a packet.. 9029 * Note: This function is the same function as in core/datagram.c 9030 * with a few modifications to make lksctp work. 9031 */ 9032 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p) 9033 { 9034 int error; 9035 DEFINE_WAIT(wait); 9036 9037 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 9038 9039 /* Socket errors? */ 9040 error = sock_error(sk); 9041 if (error) 9042 goto out; 9043 9044 if (!skb_queue_empty(&sk->sk_receive_queue)) 9045 goto ready; 9046 9047 /* Socket shut down? */ 9048 if (sk->sk_shutdown & RCV_SHUTDOWN) 9049 goto out; 9050 9051 /* Sequenced packets can come disconnected. If so we report the 9052 * problem. 9053 */ 9054 error = -ENOTCONN; 9055 9056 /* Is there a good reason to think that we may receive some data? */ 9057 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) 9058 goto out; 9059 9060 /* Handle signals. */ 9061 if (signal_pending(current)) 9062 goto interrupted; 9063 9064 /* Let another process have a go. Since we are going to sleep 9065 * anyway. Note: This may cause odd behaviors if the message 9066 * does not fit in the user's buffer, but this seems to be the 9067 * only way to honor MSG_DONTWAIT realistically. 9068 */ 9069 release_sock(sk); 9070 *timeo_p = schedule_timeout(*timeo_p); 9071 lock_sock(sk); 9072 9073 ready: 9074 finish_wait(sk_sleep(sk), &wait); 9075 return 0; 9076 9077 interrupted: 9078 error = sock_intr_errno(*timeo_p); 9079 9080 out: 9081 finish_wait(sk_sleep(sk), &wait); 9082 *err = error; 9083 return error; 9084 } 9085 9086 /* Receive a datagram. 9087 * Note: This is pretty much the same routine as in core/datagram.c 9088 * with a few changes to make lksctp work. 9089 */ 9090 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, int *err) 9091 { 9092 int error; 9093 struct sk_buff *skb; 9094 long timeo; 9095 9096 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 9097 9098 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo, 9099 MAX_SCHEDULE_TIMEOUT); 9100 9101 do { 9102 /* Again only user level code calls this function, 9103 * so nothing interrupt level 9104 * will suddenly eat the receive_queue. 9105 * 9106 * Look at current nfs client by the way... 9107 * However, this function was correct in any case. 8) 9108 */ 9109 if (flags & MSG_PEEK) { 9110 skb = skb_peek(&sk->sk_receive_queue); 9111 if (skb) 9112 refcount_inc(&skb->users); 9113 } else { 9114 skb = __skb_dequeue(&sk->sk_receive_queue); 9115 } 9116 9117 if (skb) 9118 return skb; 9119 9120 /* Caller is allowed not to check sk->sk_err before calling. */ 9121 error = sock_error(sk); 9122 if (error) 9123 goto no_packet; 9124 9125 if (sk->sk_shutdown & RCV_SHUTDOWN) 9126 break; 9127 9128 9129 /* User doesn't want to wait. */ 9130 error = -EAGAIN; 9131 if (!timeo) 9132 goto no_packet; 9133 } while (sctp_wait_for_packet(sk, err, &timeo) == 0); 9134 9135 return NULL; 9136 9137 no_packet: 9138 *err = error; 9139 return NULL; 9140 } 9141 9142 /* If sndbuf has changed, wake up per association sndbuf waiters. */ 9143 static void __sctp_write_space(struct sctp_association *asoc) 9144 { 9145 struct sock *sk = asoc->base.sk; 9146 9147 if (sctp_wspace(asoc) <= 0) 9148 return; 9149 9150 if (waitqueue_active(&asoc->wait)) 9151 wake_up_interruptible(&asoc->wait); 9152 9153 if (sctp_writeable(sk)) { 9154 struct socket_wq *wq; 9155 9156 rcu_read_lock(); 9157 wq = rcu_dereference(sk->sk_wq); 9158 if (wq) { 9159 if (waitqueue_active(&wq->wait)) 9160 wake_up_interruptible_poll(&wq->wait, EPOLLOUT | 9161 EPOLLWRNORM | EPOLLWRBAND); 9162 9163 /* Note that we try to include the Async I/O support 9164 * here by modeling from the current TCP/UDP code. 9165 * We have not tested with it yet. 9166 */ 9167 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) 9168 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT); 9169 } 9170 rcu_read_unlock(); 9171 } 9172 } 9173 9174 static void sctp_wake_up_waiters(struct sock *sk, 9175 struct sctp_association *asoc) 9176 { 9177 struct sctp_association *tmp = asoc; 9178 9179 /* We do accounting for the sndbuf space per association, 9180 * so we only need to wake our own association. 9181 */ 9182 if (asoc->ep->sndbuf_policy) 9183 return __sctp_write_space(asoc); 9184 9185 /* If association goes down and is just flushing its 9186 * outq, then just normally notify others. 9187 */ 9188 if (asoc->base.dead) 9189 return sctp_write_space(sk); 9190 9191 /* Accounting for the sndbuf space is per socket, so we 9192 * need to wake up others, try to be fair and in case of 9193 * other associations, let them have a go first instead 9194 * of just doing a sctp_write_space() call. 9195 * 9196 * Note that we reach sctp_wake_up_waiters() only when 9197 * associations free up queued chunks, thus we are under 9198 * lock and the list of associations on a socket is 9199 * guaranteed not to change. 9200 */ 9201 for (tmp = list_next_entry(tmp, asocs); 1; 9202 tmp = list_next_entry(tmp, asocs)) { 9203 /* Manually skip the head element. */ 9204 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs)) 9205 continue; 9206 /* Wake up association. */ 9207 __sctp_write_space(tmp); 9208 /* We've reached the end. */ 9209 if (tmp == asoc) 9210 break; 9211 } 9212 } 9213 9214 /* Do accounting for the sndbuf space. 9215 * Decrement the used sndbuf space of the corresponding association by the 9216 * data size which was just transmitted(freed). 9217 */ 9218 static void sctp_wfree(struct sk_buff *skb) 9219 { 9220 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg; 9221 struct sctp_association *asoc = chunk->asoc; 9222 struct sock *sk = asoc->base.sk; 9223 9224 sk_mem_uncharge(sk, skb->truesize); 9225 sk_wmem_queued_add(sk, -(skb->truesize + sizeof(struct sctp_chunk))); 9226 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk); 9227 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), 9228 &sk->sk_wmem_alloc)); 9229 9230 if (chunk->shkey) { 9231 struct sctp_shared_key *shkey = chunk->shkey; 9232 9233 /* refcnt == 2 and !list_empty mean after this release, it's 9234 * not being used anywhere, and it's time to notify userland 9235 * that this shkey can be freed if it's been deactivated. 9236 */ 9237 if (shkey->deactivated && !list_empty(&shkey->key_list) && 9238 refcount_read(&shkey->refcnt) == 2) { 9239 struct sctp_ulpevent *ev; 9240 9241 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id, 9242 SCTP_AUTH_FREE_KEY, 9243 GFP_KERNEL); 9244 if (ev) 9245 asoc->stream.si->enqueue_event(&asoc->ulpq, ev); 9246 } 9247 sctp_auth_shkey_release(chunk->shkey); 9248 } 9249 9250 sock_wfree(skb); 9251 sctp_wake_up_waiters(sk, asoc); 9252 9253 sctp_association_put(asoc); 9254 } 9255 9256 /* Do accounting for the receive space on the socket. 9257 * Accounting for the association is done in ulpevent.c 9258 * We set this as a destructor for the cloned data skbs so that 9259 * accounting is done at the correct time. 9260 */ 9261 void sctp_sock_rfree(struct sk_buff *skb) 9262 { 9263 struct sock *sk = skb->sk; 9264 struct sctp_ulpevent *event = sctp_skb2event(skb); 9265 9266 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); 9267 9268 /* 9269 * Mimic the behavior of sock_rfree 9270 */ 9271 sk_mem_uncharge(sk, event->rmem_len); 9272 } 9273 9274 9275 /* Helper function to wait for space in the sndbuf. */ 9276 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, 9277 struct sctp_transport *transport, 9278 long *timeo_p, size_t msg_len) 9279 { 9280 struct sock *sk = asoc->base.sk; 9281 long current_timeo = *timeo_p; 9282 DEFINE_WAIT(wait); 9283 int err = 0; 9284 9285 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc, 9286 *timeo_p, msg_len); 9287 9288 /* Increment the transport and association's refcnt. */ 9289 if (transport) 9290 sctp_transport_hold(transport); 9291 sctp_association_hold(asoc); 9292 9293 /* Wait on the association specific sndbuf space. */ 9294 for (;;) { 9295 prepare_to_wait_exclusive(&asoc->wait, &wait, 9296 TASK_INTERRUPTIBLE); 9297 if (asoc->base.dead) 9298 goto do_dead; 9299 if ((!*timeo_p) || (transport && transport->dead)) 9300 goto do_nonblock; 9301 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING) 9302 goto do_error; 9303 if (signal_pending(current)) 9304 goto do_interrupted; 9305 if ((int)msg_len <= sctp_wspace(asoc) && 9306 sk_wmem_schedule(sk, msg_len)) 9307 break; 9308 9309 /* Let another process have a go. Since we are going 9310 * to sleep anyway. 9311 */ 9312 release_sock(sk); 9313 current_timeo = schedule_timeout(current_timeo); 9314 lock_sock(sk); 9315 if (sk != asoc->base.sk) 9316 goto do_error; 9317 9318 *timeo_p = current_timeo; 9319 } 9320 9321 out: 9322 finish_wait(&asoc->wait, &wait); 9323 9324 /* Release the transport and association's refcnt. */ 9325 if (transport) 9326 sctp_transport_put(transport); 9327 sctp_association_put(asoc); 9328 9329 return err; 9330 9331 do_dead: 9332 err = -ESRCH; 9333 goto out; 9334 9335 do_error: 9336 err = -EPIPE; 9337 goto out; 9338 9339 do_interrupted: 9340 err = sock_intr_errno(*timeo_p); 9341 goto out; 9342 9343 do_nonblock: 9344 err = -EAGAIN; 9345 goto out; 9346 } 9347 9348 void sctp_data_ready(struct sock *sk) 9349 { 9350 struct socket_wq *wq; 9351 9352 trace_sk_data_ready(sk); 9353 9354 rcu_read_lock(); 9355 wq = rcu_dereference(sk->sk_wq); 9356 if (skwq_has_sleeper(wq)) 9357 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | 9358 EPOLLRDNORM | EPOLLRDBAND); 9359 sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN); 9360 rcu_read_unlock(); 9361 } 9362 9363 /* If socket sndbuf has changed, wake up all per association waiters. */ 9364 void sctp_write_space(struct sock *sk) 9365 { 9366 struct sctp_association *asoc; 9367 9368 /* Wake up the tasks in each wait queue. */ 9369 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) { 9370 __sctp_write_space(asoc); 9371 } 9372 } 9373 9374 /* Is there any sndbuf space available on the socket? 9375 * 9376 * Note that sk_wmem_alloc is the sum of the send buffers on all of the 9377 * associations on the same socket. For a UDP-style socket with 9378 * multiple associations, it is possible for it to be "unwriteable" 9379 * prematurely. I assume that this is acceptable because 9380 * a premature "unwriteable" is better than an accidental "writeable" which 9381 * would cause an unwanted block under certain circumstances. For the 1-1 9382 * UDP-style sockets or TCP-style sockets, this code should work. 9383 * - Daisy 9384 */ 9385 static bool sctp_writeable(const struct sock *sk) 9386 { 9387 return READ_ONCE(sk->sk_sndbuf) > READ_ONCE(sk->sk_wmem_queued); 9388 } 9389 9390 /* Wait for an association to go into ESTABLISHED state. If timeout is 0, 9391 * returns immediately with EINPROGRESS. 9392 */ 9393 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) 9394 { 9395 struct sock *sk = asoc->base.sk; 9396 int err = 0; 9397 long current_timeo = *timeo_p; 9398 DEFINE_WAIT(wait); 9399 9400 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p); 9401 9402 /* Increment the association's refcnt. */ 9403 sctp_association_hold(asoc); 9404 9405 for (;;) { 9406 prepare_to_wait_exclusive(&asoc->wait, &wait, 9407 TASK_INTERRUPTIBLE); 9408 if (!*timeo_p) 9409 goto do_nonblock; 9410 if (sk->sk_shutdown & RCV_SHUTDOWN) 9411 break; 9412 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || 9413 asoc->base.dead) 9414 goto do_error; 9415 if (signal_pending(current)) 9416 goto do_interrupted; 9417 9418 if (sctp_state(asoc, ESTABLISHED)) 9419 break; 9420 9421 /* Let another process have a go. Since we are going 9422 * to sleep anyway. 9423 */ 9424 release_sock(sk); 9425 current_timeo = schedule_timeout(current_timeo); 9426 lock_sock(sk); 9427 if (sk != asoc->base.sk) 9428 goto do_error; 9429 9430 *timeo_p = current_timeo; 9431 } 9432 9433 out: 9434 finish_wait(&asoc->wait, &wait); 9435 9436 /* Release the association's refcnt. */ 9437 sctp_association_put(asoc); 9438 9439 return err; 9440 9441 do_error: 9442 if (asoc->init_err_counter + 1 > asoc->max_init_attempts) 9443 err = -ETIMEDOUT; 9444 else 9445 err = -ECONNREFUSED; 9446 goto out; 9447 9448 do_interrupted: 9449 err = sock_intr_errno(*timeo_p); 9450 goto out; 9451 9452 do_nonblock: 9453 err = -EINPROGRESS; 9454 goto out; 9455 } 9456 9457 static int sctp_wait_for_accept(struct sock *sk, long timeo) 9458 { 9459 struct sctp_endpoint *ep; 9460 int err = 0; 9461 DEFINE_WAIT(wait); 9462 9463 ep = sctp_sk(sk)->ep; 9464 9465 9466 for (;;) { 9467 prepare_to_wait_exclusive(sk_sleep(sk), &wait, 9468 TASK_INTERRUPTIBLE); 9469 9470 if (list_empty(&ep->asocs)) { 9471 release_sock(sk); 9472 timeo = schedule_timeout(timeo); 9473 lock_sock(sk); 9474 } 9475 9476 err = -EINVAL; 9477 if (!sctp_sstate(sk, LISTENING) || 9478 (sk->sk_shutdown & RCV_SHUTDOWN)) 9479 break; 9480 9481 err = 0; 9482 if (!list_empty(&ep->asocs)) 9483 break; 9484 9485 err = sock_intr_errno(timeo); 9486 if (signal_pending(current)) 9487 break; 9488 9489 err = -EAGAIN; 9490 if (!timeo) 9491 break; 9492 } 9493 9494 finish_wait(sk_sleep(sk), &wait); 9495 9496 return err; 9497 } 9498 9499 static void sctp_wait_for_close(struct sock *sk, long timeout) 9500 { 9501 DEFINE_WAIT(wait); 9502 9503 do { 9504 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 9505 if (list_empty(&sctp_sk(sk)->ep->asocs)) 9506 break; 9507 release_sock(sk); 9508 timeout = schedule_timeout(timeout); 9509 lock_sock(sk); 9510 } while (!signal_pending(current) && timeout); 9511 9512 finish_wait(sk_sleep(sk), &wait); 9513 } 9514 9515 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) 9516 { 9517 struct sk_buff *frag; 9518 9519 if (!skb->data_len) 9520 goto done; 9521 9522 /* Don't forget the fragments. */ 9523 skb_walk_frags(skb, frag) 9524 sctp_skb_set_owner_r_frag(frag, sk); 9525 9526 done: 9527 sctp_skb_set_owner_r(skb, sk); 9528 } 9529 9530 /* Populate the fields of the newsk from the oldsk and migrate the assoc 9531 * and its messages to the newsk. 9532 */ 9533 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, 9534 struct sctp_association *assoc, 9535 enum sctp_socket_type type) 9536 { 9537 struct sctp_sock *oldsp = sctp_sk(oldsk); 9538 struct sctp_sock *newsp = sctp_sk(newsk); 9539 struct sctp_bind_bucket *pp; /* hash list port iterator */ 9540 struct sctp_endpoint *newep = newsp->ep; 9541 struct sk_buff *skb, *tmp; 9542 struct sctp_ulpevent *event; 9543 struct sctp_bind_hashbucket *head; 9544 int err; 9545 9546 /* Restore the ep value that was overwritten with the above structure 9547 * copy. 9548 */ 9549 newsp->ep = newep; 9550 9551 /* Hook this new socket in to the bind_hash list. */ 9552 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk), 9553 inet_sk(oldsk)->inet_num)]; 9554 spin_lock_bh(&head->lock); 9555 pp = sctp_sk(oldsk)->bind_hash; 9556 sk_add_bind_node(newsk, &pp->owner); 9557 sctp_sk(newsk)->bind_hash = pp; 9558 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num; 9559 spin_unlock_bh(&head->lock); 9560 9561 /* Copy the bind_addr list from the original endpoint to the new 9562 * endpoint so that we can handle restarts properly 9563 */ 9564 err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr, 9565 &oldsp->ep->base.bind_addr, GFP_KERNEL); 9566 if (err) 9567 return err; 9568 9569 sctp_auto_asconf_init(newsp); 9570 9571 /* Move any messages in the old socket's receive queue that are for the 9572 * peeled off association to the new socket's receive queue. 9573 */ 9574 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { 9575 event = sctp_skb2event(skb); 9576 if (event->asoc == assoc) { 9577 __skb_unlink(skb, &oldsk->sk_receive_queue); 9578 __skb_queue_tail(&newsk->sk_receive_queue, skb); 9579 sctp_skb_set_owner_r_frag(skb, newsk); 9580 } 9581 } 9582 9583 /* Clean up any messages pending delivery due to partial 9584 * delivery. Three cases: 9585 * 1) No partial deliver; no work. 9586 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. 9587 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. 9588 */ 9589 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); 9590 9591 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { 9592 struct sk_buff_head *queue; 9593 9594 /* Decide which queue to move pd_lobby skbs to. */ 9595 if (assoc->ulpq.pd_mode) { 9596 queue = &newsp->pd_lobby; 9597 } else 9598 queue = &newsk->sk_receive_queue; 9599 9600 /* Walk through the pd_lobby, looking for skbs that 9601 * need moved to the new socket. 9602 */ 9603 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { 9604 event = sctp_skb2event(skb); 9605 if (event->asoc == assoc) { 9606 __skb_unlink(skb, &oldsp->pd_lobby); 9607 __skb_queue_tail(queue, skb); 9608 sctp_skb_set_owner_r_frag(skb, newsk); 9609 } 9610 } 9611 9612 /* Clear up any skbs waiting for the partial 9613 * delivery to finish. 9614 */ 9615 if (assoc->ulpq.pd_mode) 9616 sctp_clear_pd(oldsk, NULL); 9617 9618 } 9619 9620 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag); 9621 9622 /* Set the type of socket to indicate that it is peeled off from the 9623 * original UDP-style socket or created with the accept() call on a 9624 * TCP-style socket.. 9625 */ 9626 newsp->type = type; 9627 9628 /* Mark the new socket "in-use" by the user so that any packets 9629 * that may arrive on the association after we've moved it are 9630 * queued to the backlog. This prevents a potential race between 9631 * backlog processing on the old socket and new-packet processing 9632 * on the new socket. 9633 * 9634 * The caller has just allocated newsk so we can guarantee that other 9635 * paths won't try to lock it and then oldsk. 9636 */ 9637 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); 9638 sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w); 9639 sctp_assoc_migrate(assoc, newsk); 9640 sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w); 9641 9642 /* If the association on the newsk is already closed before accept() 9643 * is called, set RCV_SHUTDOWN flag. 9644 */ 9645 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) { 9646 inet_sk_set_state(newsk, SCTP_SS_CLOSED); 9647 newsk->sk_shutdown |= RCV_SHUTDOWN; 9648 } else { 9649 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED); 9650 } 9651 9652 release_sock(newsk); 9653 9654 return 0; 9655 } 9656 9657 9658 /* This proto struct describes the ULP interface for SCTP. */ 9659 struct proto sctp_prot = { 9660 .name = "SCTP", 9661 .owner = THIS_MODULE, 9662 .close = sctp_close, 9663 .disconnect = sctp_disconnect, 9664 .accept = sctp_accept, 9665 .ioctl = sctp_ioctl, 9666 .init = sctp_init_sock, 9667 .destroy = sctp_destroy_sock, 9668 .shutdown = sctp_shutdown, 9669 .setsockopt = sctp_setsockopt, 9670 .getsockopt = sctp_getsockopt, 9671 .bpf_bypass_getsockopt = sctp_bpf_bypass_getsockopt, 9672 .sendmsg = sctp_sendmsg, 9673 .recvmsg = sctp_recvmsg, 9674 .bind = sctp_bind, 9675 .bind_add = sctp_bind_add, 9676 .backlog_rcv = sctp_backlog_rcv, 9677 .hash = sctp_hash, 9678 .unhash = sctp_unhash, 9679 .no_autobind = true, 9680 .obj_size = sizeof(struct sctp_sock), 9681 .useroffset = offsetof(struct sctp_sock, subscribe), 9682 .usersize = offsetof(struct sctp_sock, initmsg) - 9683 offsetof(struct sctp_sock, subscribe) + 9684 sizeof_field(struct sctp_sock, initmsg), 9685 .sysctl_mem = sysctl_sctp_mem, 9686 .sysctl_rmem = sysctl_sctp_rmem, 9687 .sysctl_wmem = sysctl_sctp_wmem, 9688 .memory_pressure = &sctp_memory_pressure, 9689 .enter_memory_pressure = sctp_enter_memory_pressure, 9690 9691 .memory_allocated = &sctp_memory_allocated, 9692 .per_cpu_fw_alloc = &sctp_memory_per_cpu_fw_alloc, 9693 9694 .sockets_allocated = &sctp_sockets_allocated, 9695 }; 9696 9697 #if IS_ENABLED(CONFIG_IPV6) 9698 9699 static void sctp_v6_destruct_sock(struct sock *sk) 9700 { 9701 inet6_sock_destruct(sk); 9702 } 9703 9704 static int sctp_v6_init_sock(struct sock *sk) 9705 { 9706 int ret = sctp_init_sock(sk); 9707 9708 if (!ret) 9709 sk->sk_destruct = sctp_v6_destruct_sock; 9710 9711 return ret; 9712 } 9713 9714 struct proto sctpv6_prot = { 9715 .name = "SCTPv6", 9716 .owner = THIS_MODULE, 9717 .close = sctp_close, 9718 .disconnect = sctp_disconnect, 9719 .accept = sctp_accept, 9720 .ioctl = sctp_ioctl, 9721 .init = sctp_v6_init_sock, 9722 .destroy = sctp_destroy_sock, 9723 .shutdown = sctp_shutdown, 9724 .setsockopt = sctp_setsockopt, 9725 .getsockopt = sctp_getsockopt, 9726 .bpf_bypass_getsockopt = sctp_bpf_bypass_getsockopt, 9727 .sendmsg = sctp_sendmsg, 9728 .recvmsg = sctp_recvmsg, 9729 .bind = sctp_bind, 9730 .bind_add = sctp_bind_add, 9731 .backlog_rcv = sctp_backlog_rcv, 9732 .hash = sctp_hash, 9733 .unhash = sctp_unhash, 9734 .no_autobind = true, 9735 .obj_size = sizeof(struct sctp6_sock), 9736 .ipv6_pinfo_offset = offsetof(struct sctp6_sock, inet6), 9737 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe), 9738 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) - 9739 offsetof(struct sctp6_sock, sctp.subscribe) + 9740 sizeof_field(struct sctp6_sock, sctp.initmsg), 9741 .sysctl_mem = sysctl_sctp_mem, 9742 .sysctl_rmem = sysctl_sctp_rmem, 9743 .sysctl_wmem = sysctl_sctp_wmem, 9744 .memory_pressure = &sctp_memory_pressure, 9745 .enter_memory_pressure = sctp_enter_memory_pressure, 9746 9747 .memory_allocated = &sctp_memory_allocated, 9748 .per_cpu_fw_alloc = &sctp_memory_per_cpu_fw_alloc, 9749 9750 .sockets_allocated = &sctp_sockets_allocated, 9751 }; 9752 #endif /* IS_ENABLED(CONFIG_IPV6) */ 9753