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