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