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