1/* SCTP kernel reference 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 reference 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 * The SCTP reference 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 * The SCTP reference 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, write to 32 * the Free Software Foundation, 59 Temple Place - Suite 330, 33 * Boston, MA 02111-1307, USA. 34 * 35 * Please send any bug reports or fixes you make to the 36 * email address(es): 37 * lksctp developers <lksctp-developers@lists.sourceforge.net> 38 * 39 * Or submit a bug report through the following website: 40 * http://www.sf.net/projects/lksctp 41 * 42 * Written or modified by: 43 * La Monte H.P. Yarroll <piggy@acm.org> 44 * Narasimha Budihal <narsi@refcode.org> 45 * Karl Knutson <karl@athena.chicago.il.us> 46 * Jon Grimm <jgrimm@us.ibm.com> 47 * Xingang Guo <xingang.guo@intel.com> 48 * Daisy Chang <daisyc@us.ibm.com> 49 * Sridhar Samudrala <samudrala@us.ibm.com> 50 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com> 51 * Ardelle Fan <ardelle.fan@intel.com> 52 * Ryan Layer <rmlayer@us.ibm.com> 53 * Anup Pemmaiah <pemmaiah@cc.usu.edu> 54 * Kevin Gao <kevin.gao@intel.com> 55 * 56 * Any bugs reported given to us we will try to fix... any fixes shared will 57 * be incorporated into the next SCTP release. 58 */ 59 60#include <linux/types.h> 61#include <linux/kernel.h> 62#include <linux/wait.h> 63#include <linux/time.h> 64#include <linux/ip.h> 65#include <linux/capability.h> 66#include <linux/fcntl.h> 67#include <linux/poll.h> 68#include <linux/init.h> 69#include <linux/crypto.h> 70 71#include <net/ip.h> 72#include <net/icmp.h> 73#include <net/route.h> 74#include <net/ipv6.h> 75#include <net/inet_common.h> 76 77#include <linux/socket.h> /* for sa_family_t */ 78#include <net/sock.h> 79#include <net/sctp/sctp.h> 80#include <net/sctp/sm.h> 81 82/* WARNING: Please do not remove the SCTP_STATIC attribute to 83 * any of the functions below as they are used to export functions 84 * used by a project regression testsuite. 85 */ 86 87/* Forward declarations for internal helper functions. */ 88static int sctp_writeable(struct sock *sk); 89static void sctp_wfree(struct sk_buff *skb); 90static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p, 91 size_t msg_len); 92static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p); 93static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p); 94static int sctp_wait_for_accept(struct sock *sk, long timeo); 95static void sctp_wait_for_close(struct sock *sk, long timeo); 96static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, 97 union sctp_addr *addr, int len); 98static int sctp_bindx_add(struct sock *, struct sockaddr *, int); 99static int sctp_bindx_rem(struct sock *, struct sockaddr *, int); 100static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int); 101static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int); 102static int sctp_send_asconf(struct sctp_association *asoc, 103 struct sctp_chunk *chunk); 104static int sctp_do_bind(struct sock *, union sctp_addr *, int); 105static int sctp_autobind(struct sock *sk); 106static void sctp_sock_migrate(struct sock *, struct sock *, 107 struct sctp_association *, sctp_socket_type_t); 108static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG; 109 110extern struct kmem_cache *sctp_bucket_cachep; 111 112/* Get the sndbuf space available at the time on the association. */ 113static inline int sctp_wspace(struct sctp_association *asoc) 114{ 115 struct sock *sk = asoc->base.sk; 116 int amt = 0; 117 118 if (asoc->ep->sndbuf_policy) { 119 /* make sure that no association uses more than sk_sndbuf */ 120 amt = sk->sk_sndbuf - asoc->sndbuf_used; 121 } else { 122 /* do socket level accounting */ 123 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc); 124 } 125 126 if (amt < 0) 127 amt = 0; 128 129 return amt; 130} 131 132/* Increment the used sndbuf space count of the corresponding association by 133 * the size of the outgoing data chunk. 134 * Also, set the skb destructor for sndbuf accounting later. 135 * 136 * Since it is always 1-1 between chunk and skb, and also a new skb is always 137 * allocated for chunk bundling in sctp_packet_transmit(), we can use the 138 * destructor in the data chunk skb for the purpose of the sndbuf space 139 * tracking. 140 */ 141static inline void sctp_set_owner_w(struct sctp_chunk *chunk) 142{ 143 struct sctp_association *asoc = chunk->asoc; 144 struct sock *sk = asoc->base.sk; 145 146 /* The sndbuf space is tracked per association. */ 147 sctp_association_hold(asoc); 148 149 skb_set_owner_w(chunk->skb, sk); 150 151 chunk->skb->destructor = sctp_wfree; 152 /* Save the chunk pointer in skb for sctp_wfree to use later. */ 153 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk; 154 155 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) + 156 sizeof(struct sk_buff) + 157 sizeof(struct sctp_chunk); 158 159 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); 160} 161 162/* Verify that this is a valid address. */ 163static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr, 164 int len) 165{ 166 struct sctp_af *af; 167 168 /* Verify basic sockaddr. */ 169 af = sctp_sockaddr_af(sctp_sk(sk), addr, len); 170 if (!af) 171 return -EINVAL; 172 173 /* Is this a valid SCTP address? */ 174 if (!af->addr_valid(addr, sctp_sk(sk), NULL)) 175 return -EINVAL; 176 177 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr))) 178 return -EINVAL; 179 180 return 0; 181} 182 183/* Look up the association by its id. If this is not a UDP-style 184 * socket, the ID field is always ignored. 185 */ 186struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id) 187{ 188 struct sctp_association *asoc = NULL; 189 190 /* If this is not a UDP-style socket, assoc id should be ignored. */ 191 if (!sctp_style(sk, UDP)) { 192 /* Return NULL if the socket state is not ESTABLISHED. It 193 * could be a TCP-style listening socket or a socket which 194 * hasn't yet called connect() to establish an association. 195 */ 196 if (!sctp_sstate(sk, ESTABLISHED)) 197 return NULL; 198 199 /* Get the first and the only association from the list. */ 200 if (!list_empty(&sctp_sk(sk)->ep->asocs)) 201 asoc = list_entry(sctp_sk(sk)->ep->asocs.next, 202 struct sctp_association, asocs); 203 return asoc; 204 } 205 206 /* Otherwise this is a UDP-style socket. */ 207 if (!id || (id == (sctp_assoc_t)-1)) 208 return NULL; 209 210 spin_lock_bh(&sctp_assocs_id_lock); 211 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); 212 spin_unlock_bh(&sctp_assocs_id_lock); 213 214 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead) 215 return NULL; 216 217 return asoc; 218} 219 220/* Look up the transport from an address and an assoc id. If both address and 221 * id are specified, the associations matching the address and the id should be 222 * the same. 223 */ 224static struct sctp_transport *sctp_addr_id2transport(struct sock *sk, 225 struct sockaddr_storage *addr, 226 sctp_assoc_t id) 227{ 228 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL; 229 struct sctp_transport *transport; 230 union sctp_addr *laddr = (union sctp_addr *)addr; 231 232 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep, 233 laddr, 234 &transport); 235 236 if (!addr_asoc) 237 return NULL; 238 239 id_asoc = sctp_id2assoc(sk, id); 240 if (id_asoc && (id_asoc != addr_asoc)) 241 return NULL; 242 243 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), 244 (union sctp_addr *)addr); 245 246 return transport; 247} 248 249/* API 3.1.2 bind() - UDP Style Syntax 250 * The syntax of bind() is, 251 * 252 * ret = bind(int sd, struct sockaddr *addr, int addrlen); 253 * 254 * sd - the socket descriptor returned by socket(). 255 * addr - the address structure (struct sockaddr_in or struct 256 * sockaddr_in6 [RFC 2553]), 257 * addr_len - the size of the address structure. 258 */ 259SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len) 260{ 261 int retval = 0; 262 263 sctp_lock_sock(sk); 264 265 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n", 266 sk, addr, addr_len); 267 268 /* Disallow binding twice. */ 269 if (!sctp_sk(sk)->ep->base.bind_addr.port) 270 retval = sctp_do_bind(sk, (union sctp_addr *)addr, 271 addr_len); 272 else 273 retval = -EINVAL; 274 275 sctp_release_sock(sk); 276 277 return retval; 278} 279 280static long sctp_get_port_local(struct sock *, union sctp_addr *); 281 282/* Verify this is a valid sockaddr. */ 283static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, 284 union sctp_addr *addr, int len) 285{ 286 struct sctp_af *af; 287 288 /* Check minimum size. */ 289 if (len < sizeof (struct sockaddr)) 290 return NULL; 291 292 /* Does this PF support this AF? */ 293 if (!opt->pf->af_supported(addr->sa.sa_family, opt)) 294 return NULL; 295 296 /* If we get this far, af is valid. */ 297 af = sctp_get_af_specific(addr->sa.sa_family); 298 299 if (len < af->sockaddr_len) 300 return NULL; 301 302 return af; 303} 304 305/* Bind a local address either to an endpoint or to an association. */ 306SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) 307{ 308 struct sctp_sock *sp = sctp_sk(sk); 309 struct sctp_endpoint *ep = sp->ep; 310 struct sctp_bind_addr *bp = &ep->base.bind_addr; 311 struct sctp_af *af; 312 unsigned short snum; 313 int ret = 0; 314 315 /* Common sockaddr verification. */ 316 af = sctp_sockaddr_af(sp, addr, len); 317 if (!af) { 318 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n", 319 sk, addr, len); 320 return -EINVAL; 321 } 322 323 snum = ntohs(addr->v4.sin_port); 324 325 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ", 326 ", port: %d, new port: %d, len: %d)\n", 327 sk, 328 addr, 329 bp->port, snum, 330 len); 331 332 /* PF specific bind() address verification. */ 333 if (!sp->pf->bind_verify(sp, addr)) 334 return -EADDRNOTAVAIL; 335 336 /* We must either be unbound, or bind to the same port. 337 * It's OK to allow 0 ports if we are already bound. 338 * We'll just inhert an already bound port in this case 339 */ 340 if (bp->port) { 341 if (!snum) 342 snum = bp->port; 343 else if (snum != bp->port) { 344 SCTP_DEBUG_PRINTK("sctp_do_bind:" 345 " New port %d does not match existing port " 346 "%d.\n", snum, bp->port); 347 return -EINVAL; 348 } 349 } 350 351 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) 352 return -EACCES; 353 354 /* Make sure we are allowed to bind here. 355 * The function sctp_get_port_local() does duplicate address 356 * detection. 357 */ 358 if ((ret = sctp_get_port_local(sk, addr))) { 359 if (ret == (long) sk) { 360 /* This endpoint has a conflicting address. */ 361 return -EINVAL; 362 } else { 363 return -EADDRINUSE; 364 } 365 } 366 367 /* Refresh ephemeral port. */ 368 if (!bp->port) 369 bp->port = inet_sk(sk)->num; 370 371 /* Add the address to the bind address list. */ 372 sctp_local_bh_disable(); 373 sctp_write_lock(&ep->base.addr_lock); 374 375 /* Use GFP_ATOMIC since BHs are disabled. */ 376 ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC); 377 sctp_write_unlock(&ep->base.addr_lock); 378 sctp_local_bh_enable(); 379 380 /* Copy back into socket for getsockname() use. */ 381 if (!ret) { 382 inet_sk(sk)->sport = htons(inet_sk(sk)->num); 383 af->to_sk_saddr(addr, sk); 384 } 385 386 return ret; 387} 388 389 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks 390 * 391 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged 392 * at any one time. If a sender, after sending an ASCONF chunk, decides 393 * it needs to transfer another ASCONF Chunk, it MUST wait until the 394 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a 395 * subsequent ASCONF. Note this restriction binds each side, so at any 396 * time two ASCONF may be in-transit on any given association (one sent 397 * from each endpoint). 398 */ 399static int sctp_send_asconf(struct sctp_association *asoc, 400 struct sctp_chunk *chunk) 401{ 402 int retval = 0; 403 404 /* If there is an outstanding ASCONF chunk, queue it for later 405 * transmission. 406 */ 407 if (asoc->addip_last_asconf) { 408 list_add_tail(&chunk->list, &asoc->addip_chunk_list); 409 goto out; 410 } 411 412 /* Hold the chunk until an ASCONF_ACK is received. */ 413 sctp_chunk_hold(chunk); 414 retval = sctp_primitive_ASCONF(asoc, chunk); 415 if (retval) 416 sctp_chunk_free(chunk); 417 else 418 asoc->addip_last_asconf = chunk; 419 420out: 421 return retval; 422} 423 424/* Add a list of addresses as bind addresses to local endpoint or 425 * association. 426 * 427 * Basically run through each address specified in the addrs/addrcnt 428 * array/length pair, determine if it is IPv6 or IPv4 and call 429 * sctp_do_bind() on it. 430 * 431 * If any of them fails, then the operation will be reversed and the 432 * ones that were added will be removed. 433 * 434 * Only sctp_setsockopt_bindx() is supposed to call this function. 435 */ 436int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt) 437{ 438 int cnt; 439 int retval = 0; 440 void *addr_buf; 441 struct sockaddr *sa_addr; 442 struct sctp_af *af; 443 444 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n", 445 sk, addrs, addrcnt); 446 447 addr_buf = addrs; 448 for (cnt = 0; cnt < addrcnt; cnt++) { 449 /* The list may contain either IPv4 or IPv6 address; 450 * determine the address length for walking thru the list. 451 */ 452 sa_addr = (struct sockaddr *)addr_buf; 453 af = sctp_get_af_specific(sa_addr->sa_family); 454 if (!af) { 455 retval = -EINVAL; 456 goto err_bindx_add; 457 } 458 459 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, 460 af->sockaddr_len); 461 462 addr_buf += af->sockaddr_len; 463 464err_bindx_add: 465 if (retval < 0) { 466 /* Failed. Cleanup the ones that have been added */ 467 if (cnt > 0) 468 sctp_bindx_rem(sk, addrs, cnt); 469 return retval; 470 } 471 } 472 473 return retval; 474} 475 476/* Send an ASCONF chunk with Add IP address parameters to all the peers of the 477 * associations that are part of the endpoint indicating that a list of local 478 * addresses are added to the endpoint. 479 * 480 * If any of the addresses is already in the bind address list of the 481 * association, we do not send the chunk for that association. But it will not 482 * affect other associations. 483 * 484 * Only sctp_setsockopt_bindx() is supposed to call this function. 485 */ 486static int sctp_send_asconf_add_ip(struct sock *sk, 487 struct sockaddr *addrs, 488 int addrcnt) 489{ 490 struct sctp_sock *sp; 491 struct sctp_endpoint *ep; 492 struct sctp_association *asoc; 493 struct sctp_bind_addr *bp; 494 struct sctp_chunk *chunk; 495 struct sctp_sockaddr_entry *laddr; 496 union sctp_addr *addr; 497 union sctp_addr saveaddr; 498 void *addr_buf; 499 struct sctp_af *af; 500 struct list_head *pos; 501 struct list_head *p; 502 int i; 503 int retval = 0; 504 505 if (!sctp_addip_enable) 506 return retval; 507 508 sp = sctp_sk(sk); 509 ep = sp->ep; 510 511 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n", 512 __FUNCTION__, sk, addrs, addrcnt); 513 514 list_for_each(pos, &ep->asocs) { 515 asoc = list_entry(pos, struct sctp_association, asocs); 516 517 if (!asoc->peer.asconf_capable) 518 continue; 519 520 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP) 521 continue; 522 523 if (!sctp_state(asoc, ESTABLISHED)) 524 continue; 525 526 /* Check if any address in the packed array of addresses is 527 * in the bind address list of the association. If so, 528 * do not send the asconf chunk to its peer, but continue with 529 * other associations. 530 */ 531 addr_buf = addrs; 532 for (i = 0; i < addrcnt; i++) { 533 addr = (union sctp_addr *)addr_buf; 534 af = sctp_get_af_specific(addr->v4.sin_family); 535 if (!af) { 536 retval = -EINVAL; 537 goto out; 538 } 539 540 if (sctp_assoc_lookup_laddr(asoc, addr)) 541 break; 542 543 addr_buf += af->sockaddr_len; 544 } 545 if (i < addrcnt) 546 continue; 547 548 /* Use the first address in bind addr list of association as 549 * Address Parameter of ASCONF CHUNK. 550 */ 551 sctp_read_lock(&asoc->base.addr_lock); 552 bp = &asoc->base.bind_addr; 553 p = bp->address_list.next; 554 laddr = list_entry(p, struct sctp_sockaddr_entry, list); 555 sctp_read_unlock(&asoc->base.addr_lock); 556 557 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs, 558 addrcnt, SCTP_PARAM_ADD_IP); 559 if (!chunk) { 560 retval = -ENOMEM; 561 goto out; 562 } 563 564 retval = sctp_send_asconf(asoc, chunk); 565 if (retval) 566 goto out; 567 568 /* Add the new addresses to the bind address list with 569 * use_as_src set to 0. 570 */ 571 sctp_local_bh_disable(); 572 sctp_write_lock(&asoc->base.addr_lock); 573 addr_buf = addrs; 574 for (i = 0; i < addrcnt; i++) { 575 addr = (union sctp_addr *)addr_buf; 576 af = sctp_get_af_specific(addr->v4.sin_family); 577 memcpy(&saveaddr, addr, af->sockaddr_len); 578 retval = sctp_add_bind_addr(bp, &saveaddr, 0, 579 GFP_ATOMIC); 580 addr_buf += af->sockaddr_len; 581 } 582 sctp_write_unlock(&asoc->base.addr_lock); 583 sctp_local_bh_enable(); 584 } 585 586out: 587 return retval; 588} 589 590/* Remove a list of addresses from bind addresses list. Do not remove the 591 * last address. 592 * 593 * Basically run through each address specified in the addrs/addrcnt 594 * array/length pair, determine if it is IPv6 or IPv4 and call 595 * sctp_del_bind() on it. 596 * 597 * If any of them fails, then the operation will be reversed and the 598 * ones that were removed will be added back. 599 * 600 * At least one address has to be left; if only one address is 601 * available, the operation will return -EBUSY. 602 * 603 * Only sctp_setsockopt_bindx() is supposed to call this function. 604 */ 605int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt) 606{ 607 struct sctp_sock *sp = sctp_sk(sk); 608 struct sctp_endpoint *ep = sp->ep; 609 int cnt; 610 struct sctp_bind_addr *bp = &ep->base.bind_addr; 611 int retval = 0; 612 void *addr_buf; 613 union sctp_addr *sa_addr; 614 struct sctp_af *af; 615 616 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n", 617 sk, addrs, addrcnt); 618 619 addr_buf = addrs; 620 for (cnt = 0; cnt < addrcnt; cnt++) { 621 /* If the bind address list is empty or if there is only one 622 * bind address, there is nothing more to be removed (we need 623 * at least one address here). 624 */ 625 if (list_empty(&bp->address_list) || 626 (sctp_list_single_entry(&bp->address_list))) { 627 retval = -EBUSY; 628 goto err_bindx_rem; 629 } 630 631 sa_addr = (union sctp_addr *)addr_buf; 632 af = sctp_get_af_specific(sa_addr->sa.sa_family); 633 if (!af) { 634 retval = -EINVAL; 635 goto err_bindx_rem; 636 } 637 638 if (!af->addr_valid(sa_addr, sp, NULL)) { 639 retval = -EADDRNOTAVAIL; 640 goto err_bindx_rem; 641 } 642 643 if (sa_addr->v4.sin_port != htons(bp->port)) { 644 retval = -EINVAL; 645 goto err_bindx_rem; 646 } 647 648 sctp_local_bh_disable(); 649 sctp_write_lock(&ep->base.addr_lock); 650 651 retval = sctp_del_bind_addr(bp, sa_addr); 652 653 sctp_write_unlock(&ep->base.addr_lock); 654 sctp_local_bh_enable(); 655 656 addr_buf += af->sockaddr_len; 657err_bindx_rem: 658 if (retval < 0) { 659 /* Failed. Add the ones that has been removed back */ 660 if (cnt > 0) 661 sctp_bindx_add(sk, addrs, cnt); 662 return retval; 663 } 664 } 665 666 return retval; 667} 668 669/* Send an ASCONF chunk with Delete IP address parameters to all the peers of 670 * the associations that are part of the endpoint indicating that a list of 671 * local addresses are removed from the endpoint. 672 * 673 * If any of the addresses is already in the bind address list of the 674 * association, we do not send the chunk for that association. But it will not 675 * affect other associations. 676 * 677 * Only sctp_setsockopt_bindx() is supposed to call this function. 678 */ 679static int sctp_send_asconf_del_ip(struct sock *sk, 680 struct sockaddr *addrs, 681 int addrcnt) 682{ 683 struct sctp_sock *sp; 684 struct sctp_endpoint *ep; 685 struct sctp_association *asoc; 686 struct sctp_transport *transport; 687 struct sctp_bind_addr *bp; 688 struct sctp_chunk *chunk; 689 union sctp_addr *laddr; 690 void *addr_buf; 691 struct sctp_af *af; 692 struct list_head *pos, *pos1; 693 struct sctp_sockaddr_entry *saddr; 694 int i; 695 int retval = 0; 696 697 if (!sctp_addip_enable) 698 return retval; 699 700 sp = sctp_sk(sk); 701 ep = sp->ep; 702 703 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n", 704 __FUNCTION__, sk, addrs, addrcnt); 705 706 list_for_each(pos, &ep->asocs) { 707 asoc = list_entry(pos, struct sctp_association, asocs); 708 709 if (!asoc->peer.asconf_capable) 710 continue; 711 712 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP) 713 continue; 714 715 if (!sctp_state(asoc, ESTABLISHED)) 716 continue; 717 718 /* Check if any address in the packed array of addresses is 719 * not present in the bind address list of the association. 720 * If so, do not send the asconf chunk to its peer, but 721 * continue with other associations. 722 */ 723 addr_buf = addrs; 724 for (i = 0; i < addrcnt; i++) { 725 laddr = (union sctp_addr *)addr_buf; 726 af = sctp_get_af_specific(laddr->v4.sin_family); 727 if (!af) { 728 retval = -EINVAL; 729 goto out; 730 } 731 732 if (!sctp_assoc_lookup_laddr(asoc, laddr)) 733 break; 734 735 addr_buf += af->sockaddr_len; 736 } 737 if (i < addrcnt) 738 continue; 739 740 /* Find one address in the association's bind address list 741 * that is not in the packed array of addresses. This is to 742 * make sure that we do not delete all the addresses in the 743 * association. 744 */ 745 sctp_read_lock(&asoc->base.addr_lock); 746 bp = &asoc->base.bind_addr; 747 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs, 748 addrcnt, sp); 749 sctp_read_unlock(&asoc->base.addr_lock); 750 if (!laddr) 751 continue; 752 753 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, 754 SCTP_PARAM_DEL_IP); 755 if (!chunk) { 756 retval = -ENOMEM; 757 goto out; 758 } 759 760 /* Reset use_as_src flag for the addresses in the bind address 761 * list that are to be deleted. 762 */ 763 sctp_local_bh_disable(); 764 sctp_write_lock(&asoc->base.addr_lock); 765 addr_buf = addrs; 766 for (i = 0; i < addrcnt; i++) { 767 laddr = (union sctp_addr *)addr_buf; 768 af = sctp_get_af_specific(laddr->v4.sin_family); 769 list_for_each(pos1, &bp->address_list) { 770 saddr = list_entry(pos1, 771 struct sctp_sockaddr_entry, 772 list); 773 if (sctp_cmp_addr_exact(&saddr->a, laddr)) 774 saddr->use_as_src = 0; 775 } 776 addr_buf += af->sockaddr_len; 777 } 778 sctp_write_unlock(&asoc->base.addr_lock); 779 sctp_local_bh_enable(); 780 781 /* Update the route and saddr entries for all the transports 782 * as some of the addresses in the bind address list are 783 * about to be deleted and cannot be used as source addresses. 784 */ 785 list_for_each(pos1, &asoc->peer.transport_addr_list) { 786 transport = list_entry(pos1, struct sctp_transport, 787 transports); 788 dst_release(transport->dst); 789 sctp_transport_route(transport, NULL, 790 sctp_sk(asoc->base.sk)); 791 } 792 793 retval = sctp_send_asconf(asoc, chunk); 794 } 795out: 796 return retval; 797} 798 799/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt() 800 * 801 * API 8.1 802 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, 803 * int flags); 804 * 805 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. 806 * If the sd is an IPv6 socket, the addresses passed can either be IPv4 807 * or IPv6 addresses. 808 * 809 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see 810 * Section 3.1.2 for this usage. 811 * 812 * addrs is a pointer to an array of one or more socket addresses. Each 813 * address is contained in its appropriate structure (i.e. struct 814 * sockaddr_in or struct sockaddr_in6) the family of the address type 815 * must be used to distinguish the address length (note that this 816 * representation is termed a "packed array" of addresses). The caller 817 * specifies the number of addresses in the array with addrcnt. 818 * 819 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns 820 * -1, and sets errno to the appropriate error code. 821 * 822 * For SCTP, the port given in each socket address must be the same, or 823 * sctp_bindx() will fail, setting errno to EINVAL. 824 * 825 * The flags parameter is formed from the bitwise OR of zero or more of 826 * the following currently defined flags: 827 * 828 * SCTP_BINDX_ADD_ADDR 829 * 830 * SCTP_BINDX_REM_ADDR 831 * 832 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the 833 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given 834 * addresses from the association. The two flags are mutually exclusive; 835 * if both are given, sctp_bindx() will fail with EINVAL. A caller may 836 * not remove all addresses from an association; sctp_bindx() will 837 * reject such an attempt with EINVAL. 838 * 839 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate 840 * additional addresses with an endpoint after calling bind(). Or use 841 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening 842 * socket is associated with so that no new association accepted will be 843 * associated with those addresses. If the endpoint supports dynamic 844 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a 845 * endpoint to send the appropriate message to the peer to change the 846 * peers address lists. 847 * 848 * Adding and removing addresses from a connected association is 849 * optional functionality. Implementations that do not support this 850 * functionality should return EOPNOTSUPP. 851 * 852 * Basically do nothing but copying the addresses from user to kernel 853 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk. 854 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() 855 * from userspace. 856 * 857 * We don't use copy_from_user() for optimization: we first do the 858 * sanity checks (buffer size -fast- and access check-healthy 859 * pointer); if all of those succeed, then we can alloc the memory 860 * (expensive operation) needed to copy the data to kernel. Then we do 861 * the copying without checking the user space area 862 * (__copy_from_user()). 863 * 864 * On exit there is no need to do sockfd_put(), sys_setsockopt() does 865 * it. 866 * 867 * sk The sk of the socket 868 * addrs The pointer to the addresses in user land 869 * addrssize Size of the addrs buffer 870 * op Operation to perform (add or remove, see the flags of 871 * sctp_bindx) 872 * 873 * Returns 0 if ok, <0 errno code on error. 874 */ 875SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk, 876 struct sockaddr __user *addrs, 877 int addrs_size, int op) 878{ 879 struct sockaddr *kaddrs; 880 int err; 881 int addrcnt = 0; 882 int walk_size = 0; 883 struct sockaddr *sa_addr; 884 void *addr_buf; 885 struct sctp_af *af; 886 887 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p" 888 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op); 889 890 if (unlikely(addrs_size <= 0)) 891 return -EINVAL; 892 893 /* Check the user passed a healthy pointer. */ 894 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) 895 return -EFAULT; 896 897 /* Alloc space for the address array in kernel memory. */ 898 kaddrs = kmalloc(addrs_size, GFP_KERNEL); 899 if (unlikely(!kaddrs)) 900 return -ENOMEM; 901 902 if (__copy_from_user(kaddrs, addrs, addrs_size)) { 903 kfree(kaddrs); 904 return -EFAULT; 905 } 906 907 /* Walk through the addrs buffer and count the number of addresses. */ 908 addr_buf = kaddrs; 909 while (walk_size < addrs_size) { 910 sa_addr = (struct sockaddr *)addr_buf; 911 af = sctp_get_af_specific(sa_addr->sa_family); 912 913 /* If the address family is not supported or if this address 914 * causes the address buffer to overflow return EINVAL. 915 */ 916 if (!af || (walk_size + af->sockaddr_len) > addrs_size) { 917 kfree(kaddrs); 918 return -EINVAL; 919 } 920 addrcnt++; 921 addr_buf += af->sockaddr_len; 922 walk_size += af->sockaddr_len; 923 } 924 925 /* Do the work. */ 926 switch (op) { 927 case SCTP_BINDX_ADD_ADDR: 928 err = sctp_bindx_add(sk, kaddrs, addrcnt); 929 if (err) 930 goto out; 931 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt); 932 break; 933 934 case SCTP_BINDX_REM_ADDR: 935 err = sctp_bindx_rem(sk, kaddrs, addrcnt); 936 if (err) 937 goto out; 938 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt); 939 break; 940 941 default: 942 err = -EINVAL; 943 break; 944 } 945 946out: 947 kfree(kaddrs); 948 949 return err; 950} 951 952/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) 953 * 954 * Common routine for handling connect() and sctp_connectx(). 955 * Connect will come in with just a single address. 956 */ 957static int __sctp_connect(struct sock* sk, 958 struct sockaddr *kaddrs, 959 int addrs_size) 960{ 961 struct sctp_sock *sp; 962 struct sctp_endpoint *ep; 963 struct sctp_association *asoc = NULL; 964 struct sctp_association *asoc2; 965 struct sctp_transport *transport; 966 union sctp_addr to; 967 struct sctp_af *af; 968 sctp_scope_t scope; 969 long timeo; 970 int err = 0; 971 int addrcnt = 0; 972 int walk_size = 0; 973 union sctp_addr *sa_addr; 974 void *addr_buf; 975 unsigned short port; 976 unsigned int f_flags = 0; 977 978 sp = sctp_sk(sk); 979 ep = sp->ep; 980 981 /* connect() cannot be done on a socket that is already in ESTABLISHED 982 * state - UDP-style peeled off socket or a TCP-style socket that 983 * is already connected. 984 * It cannot be done even on a TCP-style listening socket. 985 */ 986 if (sctp_sstate(sk, ESTABLISHED) || 987 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) { 988 err = -EISCONN; 989 goto out_free; 990 } 991 992 /* Walk through the addrs buffer and count the number of addresses. */ 993 addr_buf = kaddrs; 994 while (walk_size < addrs_size) { 995 sa_addr = (union sctp_addr *)addr_buf; 996 af = sctp_get_af_specific(sa_addr->sa.sa_family); 997 port = ntohs(sa_addr->v4.sin_port); 998 999 /* If the address family is not supported or if this address 1000 * causes the address buffer to overflow return EINVAL. 1001 */ 1002 if (!af || (walk_size + af->sockaddr_len) > addrs_size) { 1003 err = -EINVAL; 1004 goto out_free; 1005 } 1006 1007 err = sctp_verify_addr(sk, sa_addr, af->sockaddr_len); 1008 if (err) 1009 goto out_free; 1010 1011 /* Make sure the destination port is correctly set 1012 * in all addresses. 1013 */ 1014 if (asoc && asoc->peer.port && asoc->peer.port != port) 1015 goto out_free; 1016 1017 memcpy(&to, sa_addr, af->sockaddr_len); 1018 1019 /* Check if there already is a matching association on the 1020 * endpoint (other than the one created here). 1021 */ 1022 asoc2 = sctp_endpoint_lookup_assoc(ep, sa_addr, &transport); 1023 if (asoc2 && asoc2 != asoc) { 1024 if (asoc2->state >= SCTP_STATE_ESTABLISHED) 1025 err = -EISCONN; 1026 else 1027 err = -EALREADY; 1028 goto out_free; 1029 } 1030 1031 /* If we could not find a matching association on the endpoint, 1032 * make sure that there is no peeled-off association matching 1033 * the peer address even on another socket. 1034 */ 1035 if (sctp_endpoint_is_peeled_off(ep, sa_addr)) { 1036 err = -EADDRNOTAVAIL; 1037 goto out_free; 1038 } 1039 1040 if (!asoc) { 1041 /* If a bind() or sctp_bindx() is not called prior to 1042 * an sctp_connectx() call, the system picks an 1043 * ephemeral port and will choose an address set 1044 * equivalent to binding with a wildcard address. 1045 */ 1046 if (!ep->base.bind_addr.port) { 1047 if (sctp_autobind(sk)) { 1048 err = -EAGAIN; 1049 goto out_free; 1050 } 1051 } else { 1052 /* 1053 * If an unprivileged user inherits a 1-many 1054 * style socket with open associations on a 1055 * privileged port, it MAY be permitted to 1056 * accept new associations, but it SHOULD NOT 1057 * be permitted to open new associations. 1058 */ 1059 if (ep->base.bind_addr.port < PROT_SOCK && 1060 !capable(CAP_NET_BIND_SERVICE)) { 1061 err = -EACCES; 1062 goto out_free; 1063 } 1064 } 1065 1066 scope = sctp_scope(sa_addr); 1067 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); 1068 if (!asoc) { 1069 err = -ENOMEM; 1070 goto out_free; 1071 } 1072 } 1073 1074 /* Prime the peer's transport structures. */ 1075 transport = sctp_assoc_add_peer(asoc, sa_addr, GFP_KERNEL, 1076 SCTP_UNKNOWN); 1077 if (!transport) { 1078 err = -ENOMEM; 1079 goto out_free; 1080 } 1081 1082 addrcnt++; 1083 addr_buf += af->sockaddr_len; 1084 walk_size += af->sockaddr_len; 1085 } 1086 1087 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL); 1088 if (err < 0) { 1089 goto out_free; 1090 } 1091 1092 err = sctp_primitive_ASSOCIATE(asoc, NULL); 1093 if (err < 0) { 1094 goto out_free; 1095 } 1096 1097 /* Initialize sk's dport and daddr for getpeername() */ 1098 inet_sk(sk)->dport = htons(asoc->peer.port); 1099 af = sctp_get_af_specific(to.sa.sa_family); 1100 af->to_sk_daddr(&to, sk); 1101 sk->sk_err = 0; 1102 1103 /* in-kernel sockets don't generally have a file allocated to them 1104 * if all they do is call sock_create_kern(). 1105 */ 1106 if (sk->sk_socket->file) 1107 f_flags = sk->sk_socket->file->f_flags; 1108 1109 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK); 1110 1111 err = sctp_wait_for_connect(asoc, &timeo); 1112 1113 /* Don't free association on exit. */ 1114 asoc = NULL; 1115 1116out_free: 1117 1118 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p" 1119 " kaddrs: %p err: %d\n", 1120 asoc, kaddrs, err); 1121 if (asoc) 1122 sctp_association_free(asoc); 1123 return err; 1124} 1125 1126/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt() 1127 * 1128 * API 8.9 1129 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt); 1130 * 1131 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. 1132 * If the sd is an IPv6 socket, the addresses passed can either be IPv4 1133 * or IPv6 addresses. 1134 * 1135 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see 1136 * Section 3.1.2 for this usage. 1137 * 1138 * addrs is a pointer to an array of one or more socket addresses. Each 1139 * address is contained in its appropriate structure (i.e. struct 1140 * sockaddr_in or struct sockaddr_in6) the family of the address type 1141 * must be used to distengish the address length (note that this 1142 * representation is termed a "packed array" of addresses). The caller 1143 * specifies the number of addresses in the array with addrcnt. 1144 * 1145 * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns 1146 * -1, and sets errno to the appropriate error code. 1147 * 1148 * For SCTP, the port given in each socket address must be the same, or 1149 * sctp_connectx() will fail, setting errno to EINVAL. 1150 * 1151 * An application can use sctp_connectx to initiate an association with 1152 * an endpoint that is multi-homed. Much like sctp_bindx() this call 1153 * allows a caller to specify multiple addresses at which a peer can be 1154 * reached. The way the SCTP stack uses the list of addresses to set up 1155 * the association is implementation dependant. This function only 1156 * specifies that the stack will try to make use of all the addresses in 1157 * the list when needed. 1158 * 1159 * Note that the list of addresses passed in is only used for setting up 1160 * the association. It does not necessarily equal the set of addresses 1161 * the peer uses for the resulting association. If the caller wants to 1162 * find out the set of peer addresses, it must use sctp_getpaddrs() to 1163 * retrieve them after the association has been set up. 1164 * 1165 * Basically do nothing but copying the addresses from user to kernel 1166 * land and invoking either sctp_connectx(). This is used for tunneling 1167 * the sctp_connectx() request through sctp_setsockopt() from userspace. 1168 * 1169 * We don't use copy_from_user() for optimization: we first do the 1170 * sanity checks (buffer size -fast- and access check-healthy 1171 * pointer); if all of those succeed, then we can alloc the memory 1172 * (expensive operation) needed to copy the data to kernel. Then we do 1173 * the copying without checking the user space area 1174 * (__copy_from_user()). 1175 * 1176 * On exit there is no need to do sockfd_put(), sys_setsockopt() does 1177 * it. 1178 * 1179 * sk The sk of the socket 1180 * addrs The pointer to the addresses in user land 1181 * addrssize Size of the addrs buffer 1182 * 1183 * Returns 0 if ok, <0 errno code on error. 1184 */ 1185SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk, 1186 struct sockaddr __user *addrs, 1187 int addrs_size) 1188{ 1189 int err = 0; 1190 struct sockaddr *kaddrs; 1191 1192 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n", 1193 __FUNCTION__, sk, addrs, addrs_size); 1194 1195 if (unlikely(addrs_size <= 0)) 1196 return -EINVAL; 1197 1198 /* Check the user passed a healthy pointer. */ 1199 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) 1200 return -EFAULT; 1201 1202 /* Alloc space for the address array in kernel memory. */ 1203 kaddrs = kmalloc(addrs_size, GFP_KERNEL); 1204 if (unlikely(!kaddrs)) 1205 return -ENOMEM; 1206 1207 if (__copy_from_user(kaddrs, addrs, addrs_size)) { 1208 err = -EFAULT; 1209 } else { 1210 err = __sctp_connect(sk, kaddrs, addrs_size); 1211 } 1212 1213 kfree(kaddrs); 1214 return err; 1215} 1216 1217SCTP_STATIC void sctp_close(struct sock *sk, long timeout) 1218{ 1219 struct sctp_endpoint *ep; 1220 struct sctp_association *asoc; 1221 struct list_head *pos, *temp; 1222 1223 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout); 1224 1225 sctp_lock_sock(sk); 1226 sk->sk_shutdown = SHUTDOWN_MASK; 1227 1228 ep = sctp_sk(sk)->ep; 1229 1230 /* Walk all associations on an endpoint. */ 1231 list_for_each_safe(pos, temp, &ep->asocs) { 1232 asoc = list_entry(pos, struct sctp_association, asocs); 1233 1234 if (sctp_style(sk, TCP)) { 1235 /* A closed association can still be in the list if 1236 * it belongs to a TCP-style listening socket that is 1237 * not yet accepted. If so, free it. If not, send an 1238 * ABORT or SHUTDOWN based on the linger options. 1239 */ 1240 if (sctp_state(asoc, CLOSED)) { 1241 sctp_unhash_established(asoc); 1242 sctp_association_free(asoc); 1243 continue; 1244 } 1245 } 1246 1247 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { 1248 struct sctp_chunk *chunk; 1249 1250 chunk = sctp_make_abort_user(asoc, NULL, 0); 1251 if (chunk) 1252 sctp_primitive_ABORT(asoc, chunk); 1253 } else 1254 sctp_primitive_SHUTDOWN(asoc, NULL); 1255 } 1256 1257 /* Clean up any skbs sitting on the receive queue. */ 1258 sctp_queue_purge_ulpevents(&sk->sk_receive_queue); 1259 sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby); 1260 1261 /* On a TCP-style socket, block for at most linger_time if set. */ 1262 if (sctp_style(sk, TCP) && timeout) 1263 sctp_wait_for_close(sk, timeout); 1264 1265 /* This will run the backlog queue. */ 1266 sctp_release_sock(sk); 1267 1268 /* Supposedly, no process has access to the socket, but 1269 * the net layers still may. 1270 */ 1271 sctp_local_bh_disable(); 1272 sctp_bh_lock_sock(sk); 1273 1274 /* Hold the sock, since sk_common_release() will put sock_put() 1275 * and we have just a little more cleanup. 1276 */ 1277 sock_hold(sk); 1278 sk_common_release(sk); 1279 1280 sctp_bh_unlock_sock(sk); 1281 sctp_local_bh_enable(); 1282 1283 sock_put(sk); 1284 1285 SCTP_DBG_OBJCNT_DEC(sock); 1286} 1287 1288/* Handle EPIPE error. */ 1289static int sctp_error(struct sock *sk, int flags, int err) 1290{ 1291 if (err == -EPIPE) 1292 err = sock_error(sk) ? : -EPIPE; 1293 if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) 1294 send_sig(SIGPIPE, current, 0); 1295 return err; 1296} 1297 1298/* API 3.1.3 sendmsg() - UDP Style Syntax 1299 * 1300 * An application uses sendmsg() and recvmsg() calls to transmit data to 1301 * and receive data from its peer. 1302 * 1303 * ssize_t sendmsg(int socket, const struct msghdr *message, 1304 * int flags); 1305 * 1306 * socket - the socket descriptor of the endpoint. 1307 * message - pointer to the msghdr structure which contains a single 1308 * user message and possibly some ancillary data. 1309 * 1310 * See Section 5 for complete description of the data 1311 * structures. 1312 * 1313 * flags - flags sent or received with the user message, see Section 1314 * 5 for complete description of the flags. 1315 * 1316 * Note: This function could use a rewrite especially when explicit 1317 * connect support comes in. 1318 */ 1319/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */ 1320 1321SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *); 1322 1323SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk, 1324 struct msghdr *msg, size_t msg_len) 1325{ 1326 struct sctp_sock *sp; 1327 struct sctp_endpoint *ep; 1328 struct sctp_association *new_asoc=NULL, *asoc=NULL; 1329 struct sctp_transport *transport, *chunk_tp; 1330 struct sctp_chunk *chunk; 1331 union sctp_addr to; 1332 struct sockaddr *msg_name = NULL; 1333 struct sctp_sndrcvinfo default_sinfo = { 0 }; 1334 struct sctp_sndrcvinfo *sinfo; 1335 struct sctp_initmsg *sinit; 1336 sctp_assoc_t associd = 0; 1337 sctp_cmsgs_t cmsgs = { NULL }; 1338 int err; 1339 sctp_scope_t scope; 1340 long timeo; 1341 __u16 sinfo_flags = 0; 1342 struct sctp_datamsg *datamsg; 1343 struct list_head *pos; 1344 int msg_flags = msg->msg_flags; 1345 1346 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n", 1347 sk, msg, msg_len); 1348 1349 err = 0; 1350 sp = sctp_sk(sk); 1351 ep = sp->ep; 1352 1353 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep); 1354 1355 /* We cannot send a message over a TCP-style listening socket. */ 1356 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) { 1357 err = -EPIPE; 1358 goto out_nounlock; 1359 } 1360 1361 /* Parse out the SCTP CMSGs. */ 1362 err = sctp_msghdr_parse(msg, &cmsgs); 1363 1364 if (err) { 1365 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err); 1366 goto out_nounlock; 1367 } 1368 1369 /* Fetch the destination address for this packet. This 1370 * address only selects the association--it is not necessarily 1371 * the address we will send to. 1372 * For a peeled-off socket, msg_name is ignored. 1373 */ 1374 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { 1375 int msg_namelen = msg->msg_namelen; 1376 1377 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name, 1378 msg_namelen); 1379 if (err) 1380 return err; 1381 1382 if (msg_namelen > sizeof(to)) 1383 msg_namelen = sizeof(to); 1384 memcpy(&to, msg->msg_name, msg_namelen); 1385 msg_name = msg->msg_name; 1386 } 1387 1388 sinfo = cmsgs.info; 1389 sinit = cmsgs.init; 1390 1391 /* Did the user specify SNDRCVINFO? */ 1392 if (sinfo) { 1393 sinfo_flags = sinfo->sinfo_flags; 1394 associd = sinfo->sinfo_assoc_id; 1395 } 1396 1397 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n", 1398 msg_len, sinfo_flags); 1399 1400 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */ 1401 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) { 1402 err = -EINVAL; 1403 goto out_nounlock; 1404 } 1405 1406 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero 1407 * length messages when SCTP_EOF|SCTP_ABORT is not set. 1408 * If SCTP_ABORT is set, the message length could be non zero with 1409 * the msg_iov set to the user abort reason. 1410 */ 1411 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) || 1412 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) { 1413 err = -EINVAL; 1414 goto out_nounlock; 1415 } 1416 1417 /* If SCTP_ADDR_OVER is set, there must be an address 1418 * specified in msg_name. 1419 */ 1420 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) { 1421 err = -EINVAL; 1422 goto out_nounlock; 1423 } 1424 1425 transport = NULL; 1426 1427 SCTP_DEBUG_PRINTK("About to look up association.\n"); 1428 1429 sctp_lock_sock(sk); 1430 1431 /* If a msg_name has been specified, assume this is to be used. */ 1432 if (msg_name) { 1433 /* Look for a matching association on the endpoint. */ 1434 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport); 1435 if (!asoc) { 1436 /* If we could not find a matching association on the 1437 * endpoint, make sure that it is not a TCP-style 1438 * socket that already has an association or there is 1439 * no peeled-off association on another socket. 1440 */ 1441 if ((sctp_style(sk, TCP) && 1442 sctp_sstate(sk, ESTABLISHED)) || 1443 sctp_endpoint_is_peeled_off(ep, &to)) { 1444 err = -EADDRNOTAVAIL; 1445 goto out_unlock; 1446 } 1447 } 1448 } else { 1449 asoc = sctp_id2assoc(sk, associd); 1450 if (!asoc) { 1451 err = -EPIPE; 1452 goto out_unlock; 1453 } 1454 } 1455 1456 if (asoc) { 1457 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc); 1458 1459 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED 1460 * socket that has an association in CLOSED state. This can 1461 * happen when an accepted socket has an association that is 1462 * already CLOSED. 1463 */ 1464 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) { 1465 err = -EPIPE; 1466 goto out_unlock; 1467 } 1468 1469 if (sinfo_flags & SCTP_EOF) { 1470 SCTP_DEBUG_PRINTK("Shutting down association: %p\n", 1471 asoc); 1472 sctp_primitive_SHUTDOWN(asoc, NULL); 1473 err = 0; 1474 goto out_unlock; 1475 } 1476 if (sinfo_flags & SCTP_ABORT) { 1477 struct sctp_chunk *chunk; 1478 1479 chunk = sctp_make_abort_user(asoc, msg, msg_len); 1480 if (!chunk) { 1481 err = -ENOMEM; 1482 goto out_unlock; 1483 } 1484 1485 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc); 1486 sctp_primitive_ABORT(asoc, chunk); 1487 err = 0; 1488 goto out_unlock; 1489 } 1490 } 1491 1492 /* Do we need to create the association? */ 1493 if (!asoc) { 1494 SCTP_DEBUG_PRINTK("There is no association yet.\n"); 1495 1496 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) { 1497 err = -EINVAL; 1498 goto out_unlock; 1499 } 1500 1501 /* Check for invalid stream against the stream counts, 1502 * either the default or the user specified stream counts. 1503 */ 1504 if (sinfo) { 1505 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) { 1506 /* Check against the defaults. */ 1507 if (sinfo->sinfo_stream >= 1508 sp->initmsg.sinit_num_ostreams) { 1509 err = -EINVAL; 1510 goto out_unlock; 1511 } 1512 } else { 1513 /* Check against the requested. */ 1514 if (sinfo->sinfo_stream >= 1515 sinit->sinit_num_ostreams) { 1516 err = -EINVAL; 1517 goto out_unlock; 1518 } 1519 } 1520 } 1521 1522 /* 1523 * API 3.1.2 bind() - UDP Style Syntax 1524 * If a bind() or sctp_bindx() is not called prior to a 1525 * sendmsg() call that initiates a new association, the 1526 * system picks an ephemeral port and will choose an address 1527 * set equivalent to binding with a wildcard address. 1528 */ 1529 if (!ep->base.bind_addr.port) { 1530 if (sctp_autobind(sk)) { 1531 err = -EAGAIN; 1532 goto out_unlock; 1533 } 1534 } else { 1535 /* 1536 * If an unprivileged user inherits a one-to-many 1537 * style socket with open associations on a privileged 1538 * port, it MAY be permitted to accept new associations, 1539 * but it SHOULD NOT be permitted to open new 1540 * associations. 1541 */ 1542 if (ep->base.bind_addr.port < PROT_SOCK && 1543 !capable(CAP_NET_BIND_SERVICE)) { 1544 err = -EACCES; 1545 goto out_unlock; 1546 } 1547 } 1548 1549 scope = sctp_scope(&to); 1550 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); 1551 if (!new_asoc) { 1552 err = -ENOMEM; 1553 goto out_unlock; 1554 } 1555 asoc = new_asoc; 1556 1557 /* If the SCTP_INIT ancillary data is specified, set all 1558 * the association init values accordingly. 1559 */ 1560 if (sinit) { 1561 if (sinit->sinit_num_ostreams) { 1562 asoc->c.sinit_num_ostreams = 1563 sinit->sinit_num_ostreams; 1564 } 1565 if (sinit->sinit_max_instreams) { 1566 asoc->c.sinit_max_instreams = 1567 sinit->sinit_max_instreams; 1568 } 1569 if (sinit->sinit_max_attempts) { 1570 asoc->max_init_attempts 1571 = sinit->sinit_max_attempts; 1572 } 1573 if (sinit->sinit_max_init_timeo) { 1574 asoc->max_init_timeo = 1575 msecs_to_jiffies(sinit->sinit_max_init_timeo); 1576 } 1577 } 1578 1579 /* Prime the peer's transport structures. */ 1580 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN); 1581 if (!transport) { 1582 err = -ENOMEM; 1583 goto out_free; 1584 } 1585 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL); 1586 if (err < 0) { 1587 err = -ENOMEM; 1588 goto out_free; 1589 } 1590 } 1591 1592 /* ASSERT: we have a valid association at this point. */ 1593 SCTP_DEBUG_PRINTK("We have a valid association.\n"); 1594 1595 if (!sinfo) { 1596 /* If the user didn't specify SNDRCVINFO, make up one with 1597 * some defaults. 1598 */ 1599 default_sinfo.sinfo_stream = asoc->default_stream; 1600 default_sinfo.sinfo_flags = asoc->default_flags; 1601 default_sinfo.sinfo_ppid = asoc->default_ppid; 1602 default_sinfo.sinfo_context = asoc->default_context; 1603 default_sinfo.sinfo_timetolive = asoc->default_timetolive; 1604 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc); 1605 sinfo = &default_sinfo; 1606 } 1607 1608 /* API 7.1.7, the sndbuf size per association bounds the 1609 * maximum size of data that can be sent in a single send call. 1610 */ 1611 if (msg_len > sk->sk_sndbuf) { 1612 err = -EMSGSIZE; 1613 goto out_free; 1614 } 1615 1616 if (asoc->pmtu_pending) 1617 sctp_assoc_pending_pmtu(asoc); 1618 1619 /* If fragmentation is disabled and the message length exceeds the 1620 * association fragmentation point, return EMSGSIZE. The I-D 1621 * does not specify what this error is, but this looks like 1622 * a great fit. 1623 */ 1624 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) { 1625 err = -EMSGSIZE; 1626 goto out_free; 1627 } 1628 1629 if (sinfo) { 1630 /* Check for invalid stream. */ 1631 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) { 1632 err = -EINVAL; 1633 goto out_free; 1634 } 1635 } 1636 1637 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 1638 if (!sctp_wspace(asoc)) { 1639 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len); 1640 if (err) 1641 goto out_free; 1642 } 1643 1644 /* If an address is passed with the sendto/sendmsg call, it is used 1645 * to override the primary destination address in the TCP model, or 1646 * when SCTP_ADDR_OVER flag is set in the UDP model. 1647 */ 1648 if ((sctp_style(sk, TCP) && msg_name) || 1649 (sinfo_flags & SCTP_ADDR_OVER)) { 1650 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to); 1651 if (!chunk_tp) { 1652 err = -EINVAL; 1653 goto out_free; 1654 } 1655 } else 1656 chunk_tp = NULL; 1657 1658 /* Auto-connect, if we aren't connected already. */ 1659 if (sctp_state(asoc, CLOSED)) { 1660 err = sctp_primitive_ASSOCIATE(asoc, NULL); 1661 if (err < 0) 1662 goto out_free; 1663 SCTP_DEBUG_PRINTK("We associated primitively.\n"); 1664 } 1665 1666 /* Break the message into multiple chunks of maximum size. */ 1667 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len); 1668 if (!datamsg) { 1669 err = -ENOMEM; 1670 goto out_free; 1671 } 1672 1673 /* Now send the (possibly) fragmented message. */ 1674 list_for_each(pos, &datamsg->chunks) { 1675 chunk = list_entry(pos, struct sctp_chunk, frag_list); 1676 sctp_datamsg_track(chunk); 1677 1678 /* Do accounting for the write space. */ 1679 sctp_set_owner_w(chunk); 1680 1681 chunk->transport = chunk_tp; 1682 1683 /* Send it to the lower layers. Note: all chunks 1684 * must either fail or succeed. The lower layer 1685 * works that way today. Keep it that way or this 1686 * breaks. 1687 */ 1688 err = sctp_primitive_SEND(asoc, chunk); 1689 /* Did the lower layer accept the chunk? */ 1690 if (err) 1691 sctp_chunk_free(chunk); 1692 SCTP_DEBUG_PRINTK("We sent primitively.\n"); 1693 } 1694 1695 sctp_datamsg_free(datamsg); 1696 if (err) 1697 goto out_free; 1698 else 1699 err = msg_len; 1700 1701 /* If we are already past ASSOCIATE, the lower 1702 * layers are responsible for association cleanup. 1703 */ 1704 goto out_unlock; 1705 1706out_free: 1707 if (new_asoc) 1708 sctp_association_free(asoc); 1709out_unlock: 1710 sctp_release_sock(sk); 1711 1712out_nounlock: 1713 return sctp_error(sk, msg_flags, err); 1714 1715} 1716 1717/* This is an extended version of skb_pull() that removes the data from the 1718 * start of a skb even when data is spread across the list of skb's in the 1719 * frag_list. len specifies the total amount of data that needs to be removed. 1720 * when 'len' bytes could be removed from the skb, it returns 0. 1721 * If 'len' exceeds the total skb length, it returns the no. of bytes that 1722 * could not be removed. 1723 */ 1724static int sctp_skb_pull(struct sk_buff *skb, int len) 1725{ 1726 struct sk_buff *list; 1727 int skb_len = skb_headlen(skb); 1728 int rlen; 1729 1730 if (len <= skb_len) { 1731 __skb_pull(skb, len); 1732 return 0; 1733 } 1734 len -= skb_len; 1735 __skb_pull(skb, skb_len); 1736 1737 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) { 1738 rlen = sctp_skb_pull(list, len); 1739 skb->len -= (len-rlen); 1740 skb->data_len -= (len-rlen); 1741 1742 if (!rlen) 1743 return 0; 1744 1745 len = rlen; 1746 } 1747 1748 return len; 1749} 1750 1751/* API 3.1.3 recvmsg() - UDP Style Syntax 1752 * 1753 * ssize_t recvmsg(int socket, struct msghdr *message, 1754 * int flags); 1755 * 1756 * socket - the socket descriptor of the endpoint. 1757 * message - pointer to the msghdr structure which contains a single 1758 * user message and possibly some ancillary data. 1759 * 1760 * See Section 5 for complete description of the data 1761 * structures. 1762 * 1763 * flags - flags sent or received with the user message, see Section 1764 * 5 for complete description of the flags. 1765 */ 1766static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *); 1767 1768SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk, 1769 struct msghdr *msg, size_t len, int noblock, 1770 int flags, int *addr_len) 1771{ 1772 struct sctp_ulpevent *event = NULL; 1773 struct sctp_sock *sp = sctp_sk(sk); 1774 struct sk_buff *skb; 1775 int copied; 1776 int err = 0; 1777 int skb_len; 1778 1779 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: " 1780 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg, 1781 "len", len, "knoblauch", noblock, 1782 "flags", flags, "addr_len", addr_len); 1783 1784 sctp_lock_sock(sk); 1785 1786 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) { 1787 err = -ENOTCONN; 1788 goto out; 1789 } 1790 1791 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err); 1792 if (!skb) 1793 goto out; 1794 1795 /* Get the total length of the skb including any skb's in the 1796 * frag_list. 1797 */ 1798 skb_len = skb->len; 1799 1800 copied = skb_len; 1801 if (copied > len) 1802 copied = len; 1803 1804 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 1805 1806 event = sctp_skb2event(skb); 1807 1808 if (err) 1809 goto out_free; 1810 1811 sock_recv_timestamp(msg, sk, skb); 1812 if (sctp_ulpevent_is_notification(event)) { 1813 msg->msg_flags |= MSG_NOTIFICATION; 1814 sp->pf->event_msgname(event, msg->msg_name, addr_len); 1815 } else { 1816 sp->pf->skb_msgname(skb, msg->msg_name, addr_len); 1817 } 1818 1819 /* Check if we allow SCTP_SNDRCVINFO. */ 1820 if (sp->subscribe.sctp_data_io_event) 1821 sctp_ulpevent_read_sndrcvinfo(event, msg); 1822 1823 err = copied; 1824 1825 /* If skb's length exceeds the user's buffer, update the skb and 1826 * push it back to the receive_queue so that the next call to 1827 * recvmsg() will return the remaining data. Don't set MSG_EOR. 1828 */ 1829 if (skb_len > copied) { 1830 msg->msg_flags &= ~MSG_EOR; 1831 if (flags & MSG_PEEK) 1832 goto out_free; 1833 sctp_skb_pull(skb, copied); 1834 skb_queue_head(&sk->sk_receive_queue, skb); 1835 1836 /* When only partial message is copied to the user, increase 1837 * rwnd by that amount. If all the data in the skb is read, 1838 * rwnd is updated when the event is freed. 1839 */ 1840 sctp_assoc_rwnd_increase(event->asoc, copied); 1841 goto out; 1842 } else if ((event->msg_flags & MSG_NOTIFICATION) || 1843 (event->msg_flags & MSG_EOR)) 1844 msg->msg_flags |= MSG_EOR; 1845 else 1846 msg->msg_flags &= ~MSG_EOR; 1847 1848out_free: 1849 if (flags & MSG_PEEK) { 1850 /* Release the skb reference acquired after peeking the skb in 1851 * sctp_skb_recv_datagram(). 1852 */ 1853 kfree_skb(skb); 1854 } else { 1855 /* Free the event which includes releasing the reference to 1856 * the owner of the skb, freeing the skb and updating the 1857 * rwnd. 1858 */ 1859 sctp_ulpevent_free(event); 1860 } 1861out: 1862 sctp_release_sock(sk); 1863 return err; 1864} 1865 1866/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) 1867 * 1868 * This option is a on/off flag. If enabled no SCTP message 1869 * fragmentation will be performed. Instead if a message being sent 1870 * exceeds the current PMTU size, the message will NOT be sent and 1871 * instead a error will be indicated to the user. 1872 */ 1873static int sctp_setsockopt_disable_fragments(struct sock *sk, 1874 char __user *optval, int optlen) 1875{ 1876 int val; 1877 1878 if (optlen < sizeof(int)) 1879 return -EINVAL; 1880 1881 if (get_user(val, (int __user *)optval)) 1882 return -EFAULT; 1883 1884 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1; 1885 1886 return 0; 1887} 1888 1889static int sctp_setsockopt_events(struct sock *sk, char __user *optval, 1890 int optlen) 1891{ 1892 if (optlen != sizeof(struct sctp_event_subscribe)) 1893 return -EINVAL; 1894 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen)) 1895 return -EFAULT; 1896 return 0; 1897} 1898 1899/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) 1900 * 1901 * This socket option is applicable to the UDP-style socket only. When 1902 * set it will cause associations that are idle for more than the 1903 * specified number of seconds to automatically close. An association 1904 * being idle is defined an association that has NOT sent or received 1905 * user data. The special value of '0' indicates that no automatic 1906 * close of any associations should be performed. The option expects an 1907 * integer defining the number of seconds of idle time before an 1908 * association is closed. 1909 */ 1910static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval, 1911 int optlen) 1912{ 1913 struct sctp_sock *sp = sctp_sk(sk); 1914 1915 /* Applicable to UDP-style socket only */ 1916 if (sctp_style(sk, TCP)) 1917 return -EOPNOTSUPP; 1918 if (optlen != sizeof(int)) 1919 return -EINVAL; 1920 if (copy_from_user(&sp->autoclose, optval, optlen)) 1921 return -EFAULT; 1922 1923 return 0; 1924} 1925 1926/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) 1927 * 1928 * Applications can enable or disable heartbeats for any peer address of 1929 * an association, modify an address's heartbeat interval, force a 1930 * heartbeat to be sent immediately, and adjust the address's maximum 1931 * number of retransmissions sent before an address is considered 1932 * unreachable. The following structure is used to access and modify an 1933 * address's parameters: 1934 * 1935 * struct sctp_paddrparams { 1936 * sctp_assoc_t spp_assoc_id; 1937 * struct sockaddr_storage spp_address; 1938 * uint32_t spp_hbinterval; 1939 * uint16_t spp_pathmaxrxt; 1940 * uint32_t spp_pathmtu; 1941 * uint32_t spp_sackdelay; 1942 * uint32_t spp_flags; 1943 * }; 1944 * 1945 * spp_assoc_id - (one-to-many style socket) This is filled in the 1946 * application, and identifies the association for 1947 * this query. 1948 * spp_address - This specifies which address is of interest. 1949 * spp_hbinterval - This contains the value of the heartbeat interval, 1950 * in milliseconds. If a value of zero 1951 * is present in this field then no changes are to 1952 * be made to this parameter. 1953 * spp_pathmaxrxt - This contains the maximum number of 1954 * retransmissions before this address shall be 1955 * considered unreachable. If a value of zero 1956 * is present in this field then no changes are to 1957 * be made to this parameter. 1958 * spp_pathmtu - When Path MTU discovery is disabled the value 1959 * specified here will be the "fixed" path mtu. 1960 * Note that if the spp_address field is empty 1961 * then all associations on this address will 1962 * have this fixed path mtu set upon them. 1963 * 1964 * spp_sackdelay - When delayed sack is enabled, this value specifies 1965 * the number of milliseconds that sacks will be delayed 1966 * for. This value will apply to all addresses of an 1967 * association if the spp_address field is empty. Note 1968 * also, that if delayed sack is enabled and this 1969 * value is set to 0, no change is made to the last 1970 * recorded delayed sack timer value. 1971 * 1972 * spp_flags - These flags are used to control various features 1973 * on an association. The flag field may contain 1974 * zero or more of the following options. 1975 * 1976 * SPP_HB_ENABLE - Enable heartbeats on the 1977 * specified address. Note that if the address 1978 * field is empty all addresses for the association 1979 * have heartbeats enabled upon them. 1980 * 1981 * SPP_HB_DISABLE - Disable heartbeats on the 1982 * speicifed address. Note that if the address 1983 * field is empty all addresses for the association 1984 * will have their heartbeats disabled. Note also 1985 * that SPP_HB_ENABLE and SPP_HB_DISABLE are 1986 * mutually exclusive, only one of these two should 1987 * be specified. Enabling both fields will have 1988 * undetermined results. 1989 * 1990 * SPP_HB_DEMAND - Request a user initiated heartbeat 1991 * to be made immediately. 1992 * 1993 * SPP_HB_TIME_IS_ZERO - Specify's that the time for 1994 * heartbeat delayis to be set to the value of 0 1995 * milliseconds. 1996 * 1997 * SPP_PMTUD_ENABLE - This field will enable PMTU 1998 * discovery upon the specified address. Note that 1999 * if the address feild is empty then all addresses 2000 * on the association are effected. 2001 * 2002 * SPP_PMTUD_DISABLE - This field will disable PMTU 2003 * discovery upon the specified address. Note that 2004 * if the address feild is empty then all addresses 2005 * on the association are effected. Not also that 2006 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually 2007 * exclusive. Enabling both will have undetermined 2008 * results. 2009 * 2010 * SPP_SACKDELAY_ENABLE - Setting this flag turns 2011 * on delayed sack. The time specified in spp_sackdelay 2012 * is used to specify the sack delay for this address. Note 2013 * that if spp_address is empty then all addresses will 2014 * enable delayed sack and take on the sack delay 2015 * value specified in spp_sackdelay. 2016 * SPP_SACKDELAY_DISABLE - Setting this flag turns 2017 * off delayed sack. If the spp_address field is blank then 2018 * delayed sack is disabled for the entire association. Note 2019 * also that this field is mutually exclusive to 2020 * SPP_SACKDELAY_ENABLE, setting both will have undefined 2021 * results. 2022 */ 2023static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params, 2024 struct sctp_transport *trans, 2025 struct sctp_association *asoc, 2026 struct sctp_sock *sp, 2027 int hb_change, 2028 int pmtud_change, 2029 int sackdelay_change) 2030{ 2031 int error; 2032 2033 if (params->spp_flags & SPP_HB_DEMAND && trans) { 2034 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans); 2035 if (error) 2036 return error; 2037 } 2038 2039 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of 2040 * this field is ignored. Note also that a value of zero indicates 2041 * the current setting should be left unchanged. 2042 */ 2043 if (params->spp_flags & SPP_HB_ENABLE) { 2044 2045 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is 2046 * set. This lets us use 0 value when this flag 2047 * is set. 2048 */ 2049 if (params->spp_flags & SPP_HB_TIME_IS_ZERO) 2050 params->spp_hbinterval = 0; 2051 2052 if (params->spp_hbinterval || 2053 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) { 2054 if (trans) { 2055 trans->hbinterval = 2056 msecs_to_jiffies(params->spp_hbinterval); 2057 } else if (asoc) { 2058 asoc->hbinterval = 2059 msecs_to_jiffies(params->spp_hbinterval); 2060 } else { 2061 sp->hbinterval = params->spp_hbinterval; 2062 } 2063 } 2064 } 2065 2066 if (hb_change) { 2067 if (trans) { 2068 trans->param_flags = 2069 (trans->param_flags & ~SPP_HB) | hb_change; 2070 } else if (asoc) { 2071 asoc->param_flags = 2072 (asoc->param_flags & ~SPP_HB) | hb_change; 2073 } else { 2074 sp->param_flags = 2075 (sp->param_flags & ~SPP_HB) | hb_change; 2076 } 2077 } 2078 2079 /* When Path MTU discovery is disabled the value specified here will 2080 * be the "fixed" path mtu (i.e. the value of the spp_flags field must 2081 * include the flag SPP_PMTUD_DISABLE for this field to have any 2082 * effect). 2083 */ 2084 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) { 2085 if (trans) { 2086 trans->pathmtu = params->spp_pathmtu; 2087 sctp_assoc_sync_pmtu(asoc); 2088 } else if (asoc) { 2089 asoc->pathmtu = params->spp_pathmtu; 2090 sctp_frag_point(sp, params->spp_pathmtu); 2091 } else { 2092 sp->pathmtu = params->spp_pathmtu; 2093 } 2094 } 2095 2096 if (pmtud_change) { 2097 if (trans) { 2098 int update = (trans->param_flags & SPP_PMTUD_DISABLE) && 2099 (params->spp_flags & SPP_PMTUD_ENABLE); 2100 trans->param_flags = 2101 (trans->param_flags & ~SPP_PMTUD) | pmtud_change; 2102 if (update) { 2103 sctp_transport_pmtu(trans); 2104 sctp_assoc_sync_pmtu(asoc); 2105 } 2106 } else if (asoc) { 2107 asoc->param_flags = 2108 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change; 2109 } else { 2110 sp->param_flags = 2111 (sp->param_flags & ~SPP_PMTUD) | pmtud_change; 2112 } 2113 } 2114 2115 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the 2116 * value of this field is ignored. Note also that a value of zero 2117 * indicates the current setting should be left unchanged. 2118 */ 2119 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) { 2120 if (trans) { 2121 trans->sackdelay = 2122 msecs_to_jiffies(params->spp_sackdelay); 2123 } else if (asoc) { 2124 asoc->sackdelay = 2125 msecs_to_jiffies(params->spp_sackdelay); 2126 } else { 2127 sp->sackdelay = params->spp_sackdelay; 2128 } 2129 } 2130 2131 if (sackdelay_change) { 2132 if (trans) { 2133 trans->param_flags = 2134 (trans->param_flags & ~SPP_SACKDELAY) | 2135 sackdelay_change; 2136 } else if (asoc) { 2137 asoc->param_flags = 2138 (asoc->param_flags & ~SPP_SACKDELAY) | 2139 sackdelay_change; 2140 } else { 2141 sp->param_flags = 2142 (sp->param_flags & ~SPP_SACKDELAY) | 2143 sackdelay_change; 2144 } 2145 } 2146 2147 /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value 2148 * of this field is ignored. Note also that a value of zero 2149 * indicates the current setting should be left unchanged. 2150 */ 2151 if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) { 2152 if (trans) { 2153 trans->pathmaxrxt = params->spp_pathmaxrxt; 2154 } else if (asoc) { 2155 asoc->pathmaxrxt = params->spp_pathmaxrxt; 2156 } else { 2157 sp->pathmaxrxt = params->spp_pathmaxrxt; 2158 } 2159 } 2160 2161 return 0; 2162} 2163 2164static int sctp_setsockopt_peer_addr_params(struct sock *sk, 2165 char __user *optval, int optlen) 2166{ 2167 struct sctp_paddrparams params; 2168 struct sctp_transport *trans = NULL; 2169 struct sctp_association *asoc = NULL; 2170 struct sctp_sock *sp = sctp_sk(sk); 2171 int error; 2172 int hb_change, pmtud_change, sackdelay_change; 2173 2174 if (optlen != sizeof(struct sctp_paddrparams)) 2175 return - EINVAL; 2176 2177 if (copy_from_user(¶ms, optval, optlen)) 2178 return -EFAULT; 2179 2180 /* Validate flags and value parameters. */ 2181 hb_change = params.spp_flags & SPP_HB; 2182 pmtud_change = params.spp_flags & SPP_PMTUD; 2183 sackdelay_change = params.spp_flags & SPP_SACKDELAY; 2184 2185 if (hb_change == SPP_HB || 2186 pmtud_change == SPP_PMTUD || 2187 sackdelay_change == SPP_SACKDELAY || 2188 params.spp_sackdelay > 500 || 2189 (params.spp_pathmtu 2190 && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT)) 2191 return -EINVAL; 2192 2193 /* If an address other than INADDR_ANY is specified, and 2194 * no transport is found, then the request is invalid. 2195 */ 2196 if (!sctp_is_any(( union sctp_addr *)¶ms.spp_address)) { 2197 trans = sctp_addr_id2transport(sk, ¶ms.spp_address, 2198 params.spp_assoc_id); 2199 if (!trans) 2200 return -EINVAL; 2201 } 2202 2203 /* Get association, if assoc_id != 0 and the socket is a one 2204 * to many style socket, and an association was not found, then 2205 * the id was invalid. 2206 */ 2207 asoc = sctp_id2assoc(sk, params.spp_assoc_id); 2208 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) 2209 return -EINVAL; 2210 2211 /* Heartbeat demand can only be sent on a transport or 2212 * association, but not a socket. 2213 */ 2214 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc) 2215 return -EINVAL; 2216 2217 /* Process parameters. */ 2218 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, 2219 hb_change, pmtud_change, 2220 sackdelay_change); 2221 2222 if (error) 2223 return error; 2224 2225 /* If changes are for association, also apply parameters to each 2226 * transport. 2227 */ 2228 if (!trans && asoc) { 2229 struct list_head *pos; 2230 2231 list_for_each(pos, &asoc->peer.transport_addr_list) { 2232 trans = list_entry(pos, struct sctp_transport, 2233 transports); 2234 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, 2235 hb_change, pmtud_change, 2236 sackdelay_change); 2237 } 2238 } 2239 2240 return 0; 2241} 2242 2243/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME) 2244 * 2245 * This options will get or set the delayed ack timer. The time is set 2246 * in milliseconds. If the assoc_id is 0, then this sets or gets the 2247 * endpoints default delayed ack timer value. If the assoc_id field is 2248 * non-zero, then the set or get effects the specified association. 2249 * 2250 * struct sctp_assoc_value { 2251 * sctp_assoc_t assoc_id; 2252 * uint32_t assoc_value; 2253 * }; 2254 * 2255 * assoc_id - This parameter, indicates which association the 2256 * user is preforming an action upon. Note that if 2257 * this field's value is zero then the endpoints 2258 * default value is changed (effecting future 2259 * associations only). 2260 * 2261 * assoc_value - This parameter contains the number of milliseconds 2262 * that the user is requesting the delayed ACK timer 2263 * be set to. Note that this value is defined in 2264 * the standard to be between 200 and 500 milliseconds. 2265 * 2266 * Note: a value of zero will leave the value alone, 2267 * but disable SACK delay. A non-zero value will also 2268 * enable SACK delay. 2269 */ 2270 2271static int sctp_setsockopt_delayed_ack_time(struct sock *sk, 2272 char __user *optval, int optlen) 2273{ 2274 struct sctp_assoc_value params; 2275 struct sctp_transport *trans = NULL; 2276 struct sctp_association *asoc = NULL; 2277 struct sctp_sock *sp = sctp_sk(sk); 2278 2279 if (optlen != sizeof(struct sctp_assoc_value)) 2280 return - EINVAL; 2281 2282 if (copy_from_user(¶ms, optval, optlen)) 2283 return -EFAULT; 2284 2285 /* Validate value parameter. */ 2286 if (params.assoc_value > 500) 2287 return -EINVAL; 2288 2289 /* Get association, if assoc_id != 0 and the socket is a one 2290 * to many style socket, and an association was not found, then 2291 * the id was invalid. 2292 */ 2293 asoc = sctp_id2assoc(sk, params.assoc_id); 2294 if (!asoc && params.assoc_id && sctp_style(sk, UDP)) 2295 return -EINVAL; 2296 2297 if (params.assoc_value) { 2298 if (asoc) { 2299 asoc->sackdelay = 2300 msecs_to_jiffies(params.assoc_value); 2301 asoc->param_flags = 2302 (asoc->param_flags & ~SPP_SACKDELAY) | 2303 SPP_SACKDELAY_ENABLE; 2304 } else { 2305 sp->sackdelay = params.assoc_value; 2306 sp->param_flags = 2307 (sp->param_flags & ~SPP_SACKDELAY) | 2308 SPP_SACKDELAY_ENABLE; 2309 } 2310 } else { 2311 if (asoc) { 2312 asoc->param_flags = 2313 (asoc->param_flags & ~SPP_SACKDELAY) | 2314 SPP_SACKDELAY_DISABLE; 2315 } else { 2316 sp->param_flags = 2317 (sp->param_flags & ~SPP_SACKDELAY) | 2318 SPP_SACKDELAY_DISABLE; 2319 } 2320 } 2321 2322 /* If change is for association, also apply to each transport. */ 2323 if (asoc) { 2324 struct list_head *pos; 2325 2326 list_for_each(pos, &asoc->peer.transport_addr_list) { 2327 trans = list_entry(pos, struct sctp_transport, 2328 transports); 2329 if (params.assoc_value) { 2330 trans->sackdelay = 2331 msecs_to_jiffies(params.assoc_value); 2332 trans->param_flags = 2333 (trans->param_flags & ~SPP_SACKDELAY) | 2334 SPP_SACKDELAY_ENABLE; 2335 } else { 2336 trans->param_flags = 2337 (trans->param_flags & ~SPP_SACKDELAY) | 2338 SPP_SACKDELAY_DISABLE; 2339 } 2340 } 2341 } 2342 2343 return 0; 2344} 2345 2346/* 7.1.3 Initialization Parameters (SCTP_INITMSG) 2347 * 2348 * Applications can specify protocol parameters for the default association 2349 * initialization. The option name argument to setsockopt() and getsockopt() 2350 * is SCTP_INITMSG. 2351 * 2352 * Setting initialization parameters is effective only on an unconnected 2353 * socket (for UDP-style sockets only future associations are effected 2354 * by the change). With TCP-style sockets, this option is inherited by 2355 * sockets derived from a listener socket. 2356 */ 2357static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen) 2358{ 2359 struct sctp_initmsg sinit; 2360 struct sctp_sock *sp = sctp_sk(sk); 2361 2362 if (optlen != sizeof(struct sctp_initmsg)) 2363 return -EINVAL; 2364 if (copy_from_user(&sinit, optval, optlen)) 2365 return -EFAULT; 2366 2367 if (sinit.sinit_num_ostreams) 2368 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams; 2369 if (sinit.sinit_max_instreams) 2370 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams; 2371 if (sinit.sinit_max_attempts) 2372 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts; 2373 if (sinit.sinit_max_init_timeo) 2374 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo; 2375 2376 return 0; 2377} 2378 2379/* 2380 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) 2381 * 2382 * Applications that wish to use the sendto() system call may wish to 2383 * specify a default set of parameters that would normally be supplied 2384 * through the inclusion of ancillary data. This socket option allows 2385 * such an application to set the default sctp_sndrcvinfo structure. 2386 * The application that wishes to use this socket option simply passes 2387 * in to this call the sctp_sndrcvinfo structure defined in Section 2388 * 5.2.2) The input parameters accepted by this call include 2389 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, 2390 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in 2391 * to this call if the caller is using the UDP model. 2392 */ 2393static int sctp_setsockopt_default_send_param(struct sock *sk, 2394 char __user *optval, int optlen) 2395{ 2396 struct sctp_sndrcvinfo info; 2397 struct sctp_association *asoc; 2398 struct sctp_sock *sp = sctp_sk(sk); 2399 2400 if (optlen != sizeof(struct sctp_sndrcvinfo)) 2401 return -EINVAL; 2402 if (copy_from_user(&info, optval, optlen)) 2403 return -EFAULT; 2404 2405 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); 2406 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) 2407 return -EINVAL; 2408 2409 if (asoc) { 2410 asoc->default_stream = info.sinfo_stream; 2411 asoc->default_flags = info.sinfo_flags; 2412 asoc->default_ppid = info.sinfo_ppid; 2413 asoc->default_context = info.sinfo_context; 2414 asoc->default_timetolive = info.sinfo_timetolive; 2415 } else { 2416 sp->default_stream = info.sinfo_stream; 2417 sp->default_flags = info.sinfo_flags; 2418 sp->default_ppid = info.sinfo_ppid; 2419 sp->default_context = info.sinfo_context; 2420 sp->default_timetolive = info.sinfo_timetolive; 2421 } 2422 2423 return 0; 2424} 2425 2426/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) 2427 * 2428 * Requests that the local SCTP stack use the enclosed peer address as 2429 * the association primary. The enclosed address must be one of the 2430 * association peer's addresses. 2431 */ 2432static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval, 2433 int optlen) 2434{ 2435 struct sctp_prim prim; 2436 struct sctp_transport *trans; 2437 2438 if (optlen != sizeof(struct sctp_prim)) 2439 return -EINVAL; 2440 2441 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim))) 2442 return -EFAULT; 2443 2444 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id); 2445 if (!trans) 2446 return -EINVAL; 2447 2448 sctp_assoc_set_primary(trans->asoc, trans); 2449 2450 return 0; 2451} 2452 2453/* 2454 * 7.1.5 SCTP_NODELAY 2455 * 2456 * Turn on/off any Nagle-like algorithm. This means that packets are 2457 * generally sent as soon as possible and no unnecessary delays are 2458 * introduced, at the cost of more packets in the network. Expects an 2459 * integer boolean flag. 2460 */ 2461static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval, 2462 int optlen) 2463{ 2464 int val; 2465 2466 if (optlen < sizeof(int)) 2467 return -EINVAL; 2468 if (get_user(val, (int __user *)optval)) 2469 return -EFAULT; 2470 2471 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1; 2472 return 0; 2473} 2474 2475/* 2476 * 2477 * 7.1.1 SCTP_RTOINFO 2478 * 2479 * The protocol parameters used to initialize and bound retransmission 2480 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access 2481 * and modify these parameters. 2482 * All parameters are time values, in milliseconds. A value of 0, when 2483 * modifying the parameters, indicates that the current value should not 2484 * be changed. 2485 * 2486 */ 2487static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) { 2488 struct sctp_rtoinfo rtoinfo; 2489 struct sctp_association *asoc; 2490 2491 if (optlen != sizeof (struct sctp_rtoinfo)) 2492 return -EINVAL; 2493 2494 if (copy_from_user(&rtoinfo, optval, optlen)) 2495 return -EFAULT; 2496 2497 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); 2498 2499 /* Set the values to the specific association */ 2500 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) 2501 return -EINVAL; 2502 2503 if (asoc) { 2504 if (rtoinfo.srto_initial != 0) 2505 asoc->rto_initial = 2506 msecs_to_jiffies(rtoinfo.srto_initial); 2507 if (rtoinfo.srto_max != 0) 2508 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max); 2509 if (rtoinfo.srto_min != 0) 2510 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min); 2511 } else { 2512 /* If there is no association or the association-id = 0 2513 * set the values to the endpoint. 2514 */ 2515 struct sctp_sock *sp = sctp_sk(sk); 2516 2517 if (rtoinfo.srto_initial != 0) 2518 sp->rtoinfo.srto_initial = rtoinfo.srto_initial; 2519 if (rtoinfo.srto_max != 0) 2520 sp->rtoinfo.srto_max = rtoinfo.srto_max; 2521 if (rtoinfo.srto_min != 0) 2522 sp->rtoinfo.srto_min = rtoinfo.srto_min; 2523 } 2524 2525 return 0; 2526} 2527 2528/* 2529 * 2530 * 7.1.2 SCTP_ASSOCINFO 2531 * 2532 * This option is used to tune the maximum retransmission attempts 2533 * of the association. 2534 * Returns an error if the new association retransmission value is 2535 * greater than the sum of the retransmission value of the peer. 2536 * See [SCTP] for more information. 2537 * 2538 */ 2539static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen) 2540{ 2541 2542 struct sctp_assocparams assocparams; 2543 struct sctp_association *asoc; 2544 2545 if (optlen != sizeof(struct sctp_assocparams)) 2546 return -EINVAL; 2547 if (copy_from_user(&assocparams, optval, optlen)) 2548 return -EFAULT; 2549 2550 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); 2551 2552 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) 2553 return -EINVAL; 2554 2555 /* Set the values to the specific association */ 2556 if (asoc) { 2557 if (assocparams.sasoc_asocmaxrxt != 0) { 2558 __u32 path_sum = 0; 2559 int paths = 0; 2560 struct list_head *pos; 2561 struct sctp_transport *peer_addr; 2562 2563 list_for_each(pos, &asoc->peer.transport_addr_list) { 2564 peer_addr = list_entry(pos, 2565 struct sctp_transport, 2566 transports); 2567 path_sum += peer_addr->pathmaxrxt; 2568 paths++; 2569 } 2570 2571 /* Only validate asocmaxrxt if we have more then 2572 * one path/transport. We do this because path 2573 * retransmissions are only counted when we have more 2574 * then one path. 2575 */ 2576 if (paths > 1 && 2577 assocparams.sasoc_asocmaxrxt > path_sum) 2578 return -EINVAL; 2579 2580 asoc->max_retrans = assocparams.sasoc_asocmaxrxt; 2581 } 2582 2583 if (assocparams.sasoc_cookie_life != 0) { 2584 asoc->cookie_life.tv_sec = 2585 assocparams.sasoc_cookie_life / 1000; 2586 asoc->cookie_life.tv_usec = 2587 (assocparams.sasoc_cookie_life % 1000) 2588 * 1000; 2589 } 2590 } else { 2591 /* Set the values to the endpoint */ 2592 struct sctp_sock *sp = sctp_sk(sk); 2593 2594 if (assocparams.sasoc_asocmaxrxt != 0) 2595 sp->assocparams.sasoc_asocmaxrxt = 2596 assocparams.sasoc_asocmaxrxt; 2597 if (assocparams.sasoc_cookie_life != 0) 2598 sp->assocparams.sasoc_cookie_life = 2599 assocparams.sasoc_cookie_life; 2600 } 2601 return 0; 2602} 2603 2604/* 2605 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) 2606 * 2607 * This socket option is a boolean flag which turns on or off mapped V4 2608 * addresses. If this option is turned on and the socket is type 2609 * PF_INET6, then IPv4 addresses will be mapped to V6 representation. 2610 * If this option is turned off, then no mapping will be done of V4 2611 * addresses and a user will receive both PF_INET6 and PF_INET type 2612 * addresses on the socket. 2613 */ 2614static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen) 2615{ 2616 int val; 2617 struct sctp_sock *sp = sctp_sk(sk); 2618 2619 if (optlen < sizeof(int)) 2620 return -EINVAL; 2621 if (get_user(val, (int __user *)optval)) 2622 return -EFAULT; 2623 if (val) 2624 sp->v4mapped = 1; 2625 else 2626 sp->v4mapped = 0; 2627 2628 return 0; 2629} 2630 2631/* 2632 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG) 2633 * 2634 * This socket option specifies the maximum size to put in any outgoing 2635 * SCTP chunk. If a message is larger than this size it will be 2636 * fragmented by SCTP into the specified size. Note that the underlying 2637 * SCTP implementation may fragment into smaller sized chunks when the 2638 * PMTU of the underlying association is smaller than the value set by 2639 * the user. 2640 */ 2641static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen) 2642{ 2643 struct sctp_association *asoc; 2644 struct list_head *pos; 2645 struct sctp_sock *sp = sctp_sk(sk); 2646 int val; 2647 2648 if (optlen < sizeof(int)) 2649 return -EINVAL; 2650 if (get_user(val, (int __user *)optval)) 2651 return -EFAULT; 2652 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))) 2653 return -EINVAL; 2654 sp->user_frag = val; 2655 2656 /* Update the frag_point of the existing associations. */ 2657 list_for_each(pos, &(sp->ep->asocs)) { 2658 asoc = list_entry(pos, struct sctp_association, asocs); 2659 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu); 2660 } 2661 2662 return 0; 2663} 2664 2665 2666/* 2667 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) 2668 * 2669 * Requests that the peer mark the enclosed address as the association 2670 * primary. The enclosed address must be one of the association's 2671 * locally bound addresses. The following structure is used to make a 2672 * set primary request: 2673 */ 2674static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval, 2675 int optlen) 2676{ 2677 struct sctp_sock *sp; 2678 struct sctp_endpoint *ep; 2679 struct sctp_association *asoc = NULL; 2680 struct sctp_setpeerprim prim; 2681 struct sctp_chunk *chunk; 2682 int err; 2683 2684 sp = sctp_sk(sk); 2685 ep = sp->ep; 2686 2687 if (!sctp_addip_enable) 2688 return -EPERM; 2689 2690 if (optlen != sizeof(struct sctp_setpeerprim)) 2691 return -EINVAL; 2692 2693 if (copy_from_user(&prim, optval, optlen)) 2694 return -EFAULT; 2695 2696 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id); 2697 if (!asoc) 2698 return -EINVAL; 2699 2700 if (!asoc->peer.asconf_capable) 2701 return -EPERM; 2702 2703 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY) 2704 return -EPERM; 2705 2706 if (!sctp_state(asoc, ESTABLISHED)) 2707 return -ENOTCONN; 2708 2709 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr)) 2710 return -EADDRNOTAVAIL; 2711 2712 /* Create an ASCONF chunk with SET_PRIMARY parameter */ 2713 chunk = sctp_make_asconf_set_prim(asoc, 2714 (union sctp_addr *)&prim.sspp_addr); 2715 if (!chunk) 2716 return -ENOMEM; 2717 2718 err = sctp_send_asconf(asoc, chunk); 2719 2720 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n"); 2721 2722 return err; 2723} 2724 2725static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval, 2726 int optlen) 2727{ 2728 struct sctp_setadaptation adaptation; 2729 2730 if (optlen != sizeof(struct sctp_setadaptation)) 2731 return -EINVAL; 2732 if (copy_from_user(&adaptation, optval, optlen)) 2733 return -EFAULT; 2734 2735 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind; 2736 2737 return 0; 2738} 2739 2740/* 2741 * 7.1.29. Set or Get the default context (SCTP_CONTEXT) 2742 * 2743 * The context field in the sctp_sndrcvinfo structure is normally only 2744 * used when a failed message is retrieved holding the value that was 2745 * sent down on the actual send call. This option allows the setting of 2746 * a default context on an association basis that will be received on 2747 * reading messages from the peer. This is especially helpful in the 2748 * one-2-many model for an application to keep some reference to an 2749 * internal state machine that is processing messages on the 2750 * association. Note that the setting of this value only effects 2751 * received messages from the peer and does not effect the value that is 2752 * saved with outbound messages. 2753 */ 2754static int sctp_setsockopt_context(struct sock *sk, char __user *optval, 2755 int optlen) 2756{ 2757 struct sctp_assoc_value params; 2758 struct sctp_sock *sp; 2759 struct sctp_association *asoc; 2760 2761 if (optlen != sizeof(struct sctp_assoc_value)) 2762 return -EINVAL; 2763 if (copy_from_user(¶ms, optval, optlen)) 2764 return -EFAULT; 2765 2766 sp = sctp_sk(sk); 2767 2768 if (params.assoc_id != 0) { 2769 asoc = sctp_id2assoc(sk, params.assoc_id); 2770 if (!asoc) 2771 return -EINVAL; 2772 asoc->default_rcv_context = params.assoc_value; 2773 } else { 2774 sp->default_rcv_context = params.assoc_value; 2775 } 2776 2777 return 0; 2778} 2779 2780/* 2781 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) 2782 * 2783 * This options will at a minimum specify if the implementation is doing 2784 * fragmented interleave. Fragmented interleave, for a one to many 2785 * socket, is when subsequent calls to receive a message may return 2786 * parts of messages from different associations. Some implementations 2787 * may allow you to turn this value on or off. If so, when turned off, 2788 * no fragment interleave will occur (which will cause a head of line 2789 * blocking amongst multiple associations sharing the same one to many 2790 * socket). When this option is turned on, then each receive call may 2791 * come from a different association (thus the user must receive data 2792 * with the extended calls (e.g. sctp_recvmsg) to keep track of which 2793 * association each receive belongs to. 2794 * 2795 * This option takes a boolean value. A non-zero value indicates that 2796 * fragmented interleave is on. A value of zero indicates that 2797 * fragmented interleave is off. 2798 * 2799 * Note that it is important that an implementation that allows this 2800 * option to be turned on, have it off by default. Otherwise an unaware 2801 * application using the one to many model may become confused and act 2802 * incorrectly. 2803 */ 2804static int sctp_setsockopt_fragment_interleave(struct sock *sk, 2805 char __user *optval, 2806 int optlen) 2807{ 2808 int val; 2809 2810 if (optlen != sizeof(int)) 2811 return -EINVAL; 2812 if (get_user(val, (int __user *)optval)) 2813 return -EFAULT; 2814 2815 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1; 2816 2817 return 0; 2818} 2819 2820/* 2821 * 7.1.25. Set or Get the sctp partial delivery point 2822 * (SCTP_PARTIAL_DELIVERY_POINT) 2823 * This option will set or get the SCTP partial delivery point. This 2824 * point is the size of a message where the partial delivery API will be 2825 * invoked to help free up rwnd space for the peer. Setting this to a 2826 * lower value will cause partial delivery's to happen more often. The 2827 * calls argument is an integer that sets or gets the partial delivery 2828 * point. 2829 */ 2830static int sctp_setsockopt_partial_delivery_point(struct sock *sk, 2831 char __user *optval, 2832 int optlen) 2833{ 2834 u32 val; 2835 2836 if (optlen != sizeof(u32)) 2837 return -EINVAL; 2838 if (get_user(val, (int __user *)optval)) 2839 return -EFAULT; 2840 2841 sctp_sk(sk)->pd_point = val; 2842 2843 return 0; /* is this the right error code? */ 2844} 2845 2846/* 2847 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) 2848 * 2849 * This option will allow a user to change the maximum burst of packets 2850 * that can be emitted by this association. Note that the default value 2851 * is 4, and some implementations may restrict this setting so that it 2852 * can only be lowered. 2853 * 2854 * NOTE: This text doesn't seem right. Do this on a socket basis with 2855 * future associations inheriting the socket value. 2856 */ 2857static int sctp_setsockopt_maxburst(struct sock *sk, 2858 char __user *optval, 2859 int optlen) 2860{ 2861 int val; 2862 2863 if (optlen != sizeof(int)) 2864 return -EINVAL; 2865 if (get_user(val, (int __user *)optval)) 2866 return -EFAULT; 2867 2868 if (val < 0) 2869 return -EINVAL; 2870 2871 sctp_sk(sk)->max_burst = val; 2872 2873 return 0; 2874} 2875 2876/* API 6.2 setsockopt(), getsockopt() 2877 * 2878 * Applications use setsockopt() and getsockopt() to set or retrieve 2879 * socket options. Socket options are used to change the default 2880 * behavior of sockets calls. They are described in Section 7. 2881 * 2882 * The syntax is: 2883 * 2884 * ret = getsockopt(int sd, int level, int optname, void __user *optval, 2885 * int __user *optlen); 2886 * ret = setsockopt(int sd, int level, int optname, const void __user *optval, 2887 * int optlen); 2888 * 2889 * sd - the socket descript. 2890 * level - set to IPPROTO_SCTP for all SCTP options. 2891 * optname - the option name. 2892 * optval - the buffer to store the value of the option. 2893 * optlen - the size of the buffer. 2894 */ 2895SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname, 2896 char __user *optval, int optlen) 2897{ 2898 int retval = 0; 2899 2900 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n", 2901 sk, optname); 2902 2903 /* I can hardly begin to describe how wrong this is. This is 2904 * so broken as to be worse than useless. The API draft 2905 * REALLY is NOT helpful here... I am not convinced that the 2906 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP 2907 * are at all well-founded. 2908 */ 2909 if (level != SOL_SCTP) { 2910 struct sctp_af *af = sctp_sk(sk)->pf->af; 2911 retval = af->setsockopt(sk, level, optname, optval, optlen); 2912 goto out_nounlock; 2913 } 2914 2915 sctp_lock_sock(sk); 2916 2917 switch (optname) { 2918 case SCTP_SOCKOPT_BINDX_ADD: 2919 /* 'optlen' is the size of the addresses buffer. */ 2920 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, 2921 optlen, SCTP_BINDX_ADD_ADDR); 2922 break; 2923 2924 case SCTP_SOCKOPT_BINDX_REM: 2925 /* 'optlen' is the size of the addresses buffer. */ 2926 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, 2927 optlen, SCTP_BINDX_REM_ADDR); 2928 break; 2929 2930 case SCTP_SOCKOPT_CONNECTX: 2931 /* 'optlen' is the size of the addresses buffer. */ 2932 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval, 2933 optlen); 2934 break; 2935 2936 case SCTP_DISABLE_FRAGMENTS: 2937 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen); 2938 break; 2939 2940 case SCTP_EVENTS: 2941 retval = sctp_setsockopt_events(sk, optval, optlen); 2942 break; 2943 2944 case SCTP_AUTOCLOSE: 2945 retval = sctp_setsockopt_autoclose(sk, optval, optlen); 2946 break; 2947 2948 case SCTP_PEER_ADDR_PARAMS: 2949 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen); 2950 break; 2951 2952 case SCTP_DELAYED_ACK_TIME: 2953 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen); 2954 break; 2955 case SCTP_PARTIAL_DELIVERY_POINT: 2956 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen); 2957 break; 2958 2959 case SCTP_INITMSG: 2960 retval = sctp_setsockopt_initmsg(sk, optval, optlen); 2961 break; 2962 case SCTP_DEFAULT_SEND_PARAM: 2963 retval = sctp_setsockopt_default_send_param(sk, optval, 2964 optlen); 2965 break; 2966 case SCTP_PRIMARY_ADDR: 2967 retval = sctp_setsockopt_primary_addr(sk, optval, optlen); 2968 break; 2969 case SCTP_SET_PEER_PRIMARY_ADDR: 2970 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen); 2971 break; 2972 case SCTP_NODELAY: 2973 retval = sctp_setsockopt_nodelay(sk, optval, optlen); 2974 break; 2975 case SCTP_RTOINFO: 2976 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen); 2977 break; 2978 case SCTP_ASSOCINFO: 2979 retval = sctp_setsockopt_associnfo(sk, optval, optlen); 2980 break; 2981 case SCTP_I_WANT_MAPPED_V4_ADDR: 2982 retval = sctp_setsockopt_mappedv4(sk, optval, optlen); 2983 break; 2984 case SCTP_MAXSEG: 2985 retval = sctp_setsockopt_maxseg(sk, optval, optlen); 2986 break; 2987 case SCTP_ADAPTATION_LAYER: 2988 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen); 2989 break; 2990 case SCTP_CONTEXT: 2991 retval = sctp_setsockopt_context(sk, optval, optlen); 2992 break; 2993 case SCTP_FRAGMENT_INTERLEAVE: 2994 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen); 2995 break; 2996 case SCTP_MAX_BURST: 2997 retval = sctp_setsockopt_maxburst(sk, optval, optlen); 2998 break; 2999 default: 3000 retval = -ENOPROTOOPT; 3001 break; 3002 } 3003 3004 sctp_release_sock(sk); 3005 3006out_nounlock: 3007 return retval; 3008} 3009 3010/* API 3.1.6 connect() - UDP Style Syntax 3011 * 3012 * An application may use the connect() call in the UDP model to initiate an 3013 * association without sending data. 3014 * 3015 * The syntax is: 3016 * 3017 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len); 3018 * 3019 * sd: the socket descriptor to have a new association added to. 3020 * 3021 * nam: the address structure (either struct sockaddr_in or struct 3022 * sockaddr_in6 defined in RFC2553 [7]). 3023 * 3024 * len: the size of the address. 3025 */ 3026SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr, 3027 int addr_len) 3028{ 3029 int err = 0; 3030 struct sctp_af *af; 3031 3032 sctp_lock_sock(sk); 3033 3034 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n", 3035 __FUNCTION__, sk, addr, addr_len); 3036 3037 /* Validate addr_len before calling common connect/connectx routine. */ 3038 af = sctp_get_af_specific(addr->sa_family); 3039 if (!af || addr_len < af->sockaddr_len) { 3040 err = -EINVAL; 3041 } else { 3042 /* Pass correct addr len to common routine (so it knows there 3043 * is only one address being passed. 3044 */ 3045 err = __sctp_connect(sk, addr, af->sockaddr_len); 3046 } 3047 3048 sctp_release_sock(sk); 3049 return err; 3050} 3051 3052SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags) 3053{ 3054 return -EOPNOTSUPP; /* STUB */ 3055} 3056 3057/* 4.1.4 accept() - TCP Style Syntax 3058 * 3059 * Applications use accept() call to remove an established SCTP 3060 * association from the accept queue of the endpoint. A new socket 3061 * descriptor will be returned from accept() to represent the newly 3062 * formed association. 3063 */ 3064SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err) 3065{ 3066 struct sctp_sock *sp; 3067 struct sctp_endpoint *ep; 3068 struct sock *newsk = NULL; 3069 struct sctp_association *asoc; 3070 long timeo; 3071 int error = 0; 3072 3073 sctp_lock_sock(sk); 3074 3075 sp = sctp_sk(sk); 3076 ep = sp->ep; 3077 3078 if (!sctp_style(sk, TCP)) { 3079 error = -EOPNOTSUPP; 3080 goto out; 3081 } 3082 3083 if (!sctp_sstate(sk, LISTENING)) { 3084 error = -EINVAL; 3085 goto out; 3086 } 3087 3088 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); 3089 3090 error = sctp_wait_for_accept(sk, timeo); 3091 if (error) 3092 goto out; 3093 3094 /* We treat the list of associations on the endpoint as the accept 3095 * queue and pick the first association on the list. 3096 */ 3097 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs); 3098 3099 newsk = sp->pf->create_accept_sk(sk, asoc); 3100 if (!newsk) { 3101 error = -ENOMEM; 3102 goto out; 3103 } 3104 3105 /* Populate the fields of the newsk from the oldsk and migrate the 3106 * asoc to the newsk. 3107 */ 3108 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP); 3109 3110out: 3111 sctp_release_sock(sk); 3112 *err = error; 3113 return newsk; 3114} 3115 3116/* The SCTP ioctl handler. */ 3117SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg) 3118{ 3119 return -ENOIOCTLCMD; 3120} 3121 3122/* This is the function which gets called during socket creation to 3123 * initialized the SCTP-specific portion of the sock. 3124 * The sock structure should already be zero-filled memory. 3125 */ 3126SCTP_STATIC int sctp_init_sock(struct sock *sk) 3127{ 3128 struct sctp_endpoint *ep; 3129 struct sctp_sock *sp; 3130 3131 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk); 3132 3133 sp = sctp_sk(sk); 3134 3135 /* Initialize the SCTP per socket area. */ 3136 switch (sk->sk_type) { 3137 case SOCK_SEQPACKET: 3138 sp->type = SCTP_SOCKET_UDP; 3139 break; 3140 case SOCK_STREAM: 3141 sp->type = SCTP_SOCKET_TCP; 3142 break; 3143 default: 3144 return -ESOCKTNOSUPPORT; 3145 } 3146 3147 /* Initialize default send parameters. These parameters can be 3148 * modified with the SCTP_DEFAULT_SEND_PARAM socket option. 3149 */ 3150 sp->default_stream = 0; 3151 sp->default_ppid = 0; 3152 sp->default_flags = 0; 3153 sp->default_context = 0; 3154 sp->default_timetolive = 0; 3155 3156 sp->default_rcv_context = 0; 3157 sp->max_burst = sctp_max_burst; 3158 3159 /* Initialize default setup parameters. These parameters 3160 * can be modified with the SCTP_INITMSG socket option or 3161 * overridden by the SCTP_INIT CMSG. 3162 */ 3163 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams; 3164 sp->initmsg.sinit_max_instreams = sctp_max_instreams; 3165 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init; 3166 sp->initmsg.sinit_max_init_timeo = sctp_rto_max; 3167 3168 /* Initialize default RTO related parameters. These parameters can 3169 * be modified for with the SCTP_RTOINFO socket option. 3170 */ 3171 sp->rtoinfo.srto_initial = sctp_rto_initial; 3172 sp->rtoinfo.srto_max = sctp_rto_max; 3173 sp->rtoinfo.srto_min = sctp_rto_min; 3174 3175 /* Initialize default association related parameters. These parameters 3176 * can be modified with the SCTP_ASSOCINFO socket option. 3177 */ 3178 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association; 3179 sp->assocparams.sasoc_number_peer_destinations = 0; 3180 sp->assocparams.sasoc_peer_rwnd = 0; 3181 sp->assocparams.sasoc_local_rwnd = 0; 3182 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life; 3183 3184 /* Initialize default event subscriptions. By default, all the 3185 * options are off. 3186 */ 3187 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe)); 3188 3189 /* Default Peer Address Parameters. These defaults can 3190 * be modified via SCTP_PEER_ADDR_PARAMS 3191 */ 3192 sp->hbinterval = sctp_hb_interval; 3193 sp->pathmaxrxt = sctp_max_retrans_path; 3194 sp->pathmtu = 0; // allow default discovery 3195 sp->sackdelay = sctp_sack_timeout; 3196 sp->param_flags = SPP_HB_ENABLE | 3197 SPP_PMTUD_ENABLE | 3198 SPP_SACKDELAY_ENABLE; 3199 3200 /* If enabled no SCTP message fragmentation will be performed. 3201 * Configure through SCTP_DISABLE_FRAGMENTS socket option. 3202 */ 3203 sp->disable_fragments = 0; 3204 3205 /* Enable Nagle algorithm by default. */ 3206 sp->nodelay = 0; 3207 3208 /* Enable by default. */ 3209 sp->v4mapped = 1; 3210 3211 /* Auto-close idle associations after the configured 3212 * number of seconds. A value of 0 disables this 3213 * feature. Configure through the SCTP_AUTOCLOSE socket option, 3214 * for UDP-style sockets only. 3215 */ 3216 sp->autoclose = 0; 3217 3218 /* User specified fragmentation limit. */ 3219 sp->user_frag = 0; 3220 3221 sp->adaptation_ind = 0; 3222 3223 sp->pf = sctp_get_pf_specific(sk->sk_family); 3224 3225 /* Control variables for partial data delivery. */ 3226 atomic_set(&sp->pd_mode, 0); 3227 skb_queue_head_init(&sp->pd_lobby); 3228 sp->frag_interleave = 0; 3229 3230 /* Create a per socket endpoint structure. Even if we 3231 * change the data structure relationships, this may still 3232 * be useful for storing pre-connect address information. 3233 */ 3234 ep = sctp_endpoint_new(sk, GFP_KERNEL); 3235 if (!ep) 3236 return -ENOMEM; 3237 3238 sp->ep = ep; 3239 sp->hmac = NULL; 3240 3241 SCTP_DBG_OBJCNT_INC(sock); 3242 return 0; 3243} 3244 3245/* Cleanup any SCTP per socket resources. */ 3246SCTP_STATIC int sctp_destroy_sock(struct sock *sk) 3247{ 3248 struct sctp_endpoint *ep; 3249 3250 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk); 3251 3252 /* Release our hold on the endpoint. */ 3253 ep = sctp_sk(sk)->ep; 3254 sctp_endpoint_free(ep); 3255 3256 return 0; 3257} 3258 3259/* API 4.1.7 shutdown() - TCP Style Syntax 3260 * int shutdown(int socket, int how); 3261 * 3262 * sd - the socket descriptor of the association to be closed. 3263 * how - Specifies the type of shutdown. The values are 3264 * as follows: 3265 * SHUT_RD 3266 * Disables further receive operations. No SCTP 3267 * protocol action is taken. 3268 * SHUT_WR 3269 * Disables further send operations, and initiates 3270 * the SCTP shutdown sequence. 3271 * SHUT_RDWR 3272 * Disables further send and receive operations 3273 * and initiates the SCTP shutdown sequence. 3274 */ 3275SCTP_STATIC void sctp_shutdown(struct sock *sk, int how) 3276{ 3277 struct sctp_endpoint *ep; 3278 struct sctp_association *asoc; 3279 3280 if (!sctp_style(sk, TCP)) 3281 return; 3282 3283 if (how & SEND_SHUTDOWN) { 3284 ep = sctp_sk(sk)->ep; 3285 if (!list_empty(&ep->asocs)) { 3286 asoc = list_entry(ep->asocs.next, 3287 struct sctp_association, asocs); 3288 sctp_primitive_SHUTDOWN(asoc, NULL); 3289 } 3290 } 3291} 3292 3293/* 7.2.1 Association Status (SCTP_STATUS) 3294 3295 * Applications can retrieve current status information about an 3296 * association, including association state, peer receiver window size, 3297 * number of unacked data chunks, and number of data chunks pending 3298 * receipt. This information is read-only. 3299 */ 3300static int sctp_getsockopt_sctp_status(struct sock *sk, int len, 3301 char __user *optval, 3302 int __user *optlen) 3303{ 3304 struct sctp_status status; 3305 struct sctp_association *asoc = NULL; 3306 struct sctp_transport *transport; 3307 sctp_assoc_t associd; 3308 int retval = 0; 3309 3310 if (len < sizeof(status)) { 3311 retval = -EINVAL; 3312 goto out; 3313 } 3314 3315 len = sizeof(status); 3316 if (copy_from_user(&status, optval, len)) { 3317 retval = -EFAULT; 3318 goto out; 3319 } 3320 3321 associd = status.sstat_assoc_id; 3322 asoc = sctp_id2assoc(sk, associd); 3323 if (!asoc) { 3324 retval = -EINVAL; 3325 goto out; 3326 } 3327 3328 transport = asoc->peer.primary_path; 3329 3330 status.sstat_assoc_id = sctp_assoc2id(asoc); 3331 status.sstat_state = asoc->state; 3332 status.sstat_rwnd = asoc->peer.rwnd; 3333 status.sstat_unackdata = asoc->unack_data; 3334 3335 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); 3336 status.sstat_instrms = asoc->c.sinit_max_instreams; 3337 status.sstat_outstrms = asoc->c.sinit_num_ostreams; 3338 status.sstat_fragmentation_point = asoc->frag_point; 3339 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); 3340 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, 3341 transport->af_specific->sockaddr_len); 3342 /* Map ipv4 address into v4-mapped-on-v6 address. */ 3343 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), 3344 (union sctp_addr *)&status.sstat_primary.spinfo_address); 3345 status.sstat_primary.spinfo_state = transport->state; 3346 status.sstat_primary.spinfo_cwnd = transport->cwnd; 3347 status.sstat_primary.spinfo_srtt = transport->srtt; 3348 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); 3349 status.sstat_primary.spinfo_mtu = transport->pathmtu; 3350 3351 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) 3352 status.sstat_primary.spinfo_state = SCTP_ACTIVE; 3353 3354 if (put_user(len, optlen)) { 3355 retval = -EFAULT; 3356 goto out; 3357 } 3358 3359 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n", 3360 len, status.sstat_state, status.sstat_rwnd, 3361 status.sstat_assoc_id); 3362 3363 if (copy_to_user(optval, &status, len)) { 3364 retval = -EFAULT; 3365 goto out; 3366 } 3367 3368out: 3369 return (retval); 3370} 3371 3372 3373/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) 3374 * 3375 * Applications can retrieve information about a specific peer address 3376 * of an association, including its reachability state, congestion 3377 * window, and retransmission timer values. This information is 3378 * read-only. 3379 */ 3380static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, 3381 char __user *optval, 3382 int __user *optlen) 3383{ 3384 struct sctp_paddrinfo pinfo; 3385 struct sctp_transport *transport; 3386 int retval = 0; 3387 3388 if (len < sizeof(pinfo)) { 3389 retval = -EINVAL; 3390 goto out; 3391 } 3392 3393 len = sizeof(pinfo); 3394 if (copy_from_user(&pinfo, optval, len)) { 3395 retval = -EFAULT; 3396 goto out; 3397 } 3398 3399 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, 3400 pinfo.spinfo_assoc_id); 3401 if (!transport) 3402 return -EINVAL; 3403 3404 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); 3405 pinfo.spinfo_state = transport->state; 3406 pinfo.spinfo_cwnd = transport->cwnd; 3407 pinfo.spinfo_srtt = transport->srtt; 3408 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); 3409 pinfo.spinfo_mtu = transport->pathmtu; 3410 3411 if (pinfo.spinfo_state == SCTP_UNKNOWN) 3412 pinfo.spinfo_state = SCTP_ACTIVE; 3413 3414 if (put_user(len, optlen)) { 3415 retval = -EFAULT; 3416 goto out; 3417 } 3418 3419 if (copy_to_user(optval, &pinfo, len)) { 3420 retval = -EFAULT; 3421 goto out; 3422 } 3423 3424out: 3425 return (retval); 3426} 3427 3428/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) 3429 * 3430 * This option is a on/off flag. If enabled no SCTP message 3431 * fragmentation will be performed. Instead if a message being sent 3432 * exceeds the current PMTU size, the message will NOT be sent and 3433 * instead a error will be indicated to the user. 3434 */ 3435static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, 3436 char __user *optval, int __user *optlen) 3437{ 3438 int val; 3439 3440 if (len < sizeof(int)) 3441 return -EINVAL; 3442 3443 len = sizeof(int); 3444 val = (sctp_sk(sk)->disable_fragments == 1); 3445 if (put_user(len, optlen)) 3446 return -EFAULT; 3447 if (copy_to_user(optval, &val, len)) 3448 return -EFAULT; 3449 return 0; 3450} 3451 3452/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) 3453 * 3454 * This socket option is used to specify various notifications and 3455 * ancillary data the user wishes to receive. 3456 */ 3457static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, 3458 int __user *optlen) 3459{ 3460 if (len < sizeof(struct sctp_event_subscribe)) 3461 return -EINVAL; 3462 len = sizeof(struct sctp_event_subscribe); 3463 if (put_user(len, optlen)) 3464 return -EFAULT; 3465 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len)) 3466 return -EFAULT; 3467 return 0; 3468} 3469 3470/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) 3471 * 3472 * This socket option is applicable to the UDP-style socket only. When 3473 * set it will cause associations that are idle for more than the 3474 * specified number of seconds to automatically close. An association 3475 * being idle is defined an association that has NOT sent or received 3476 * user data. The special value of '0' indicates that no automatic 3477 * close of any associations should be performed. The option expects an 3478 * integer defining the number of seconds of idle time before an 3479 * association is closed. 3480 */ 3481static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) 3482{ 3483 /* Applicable to UDP-style socket only */ 3484 if (sctp_style(sk, TCP)) 3485 return -EOPNOTSUPP; 3486 if (len < sizeof(int)) 3487 return -EINVAL; 3488 len = sizeof(int); 3489 if (put_user(len, optlen)) 3490 return -EFAULT; 3491 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int))) 3492 return -EFAULT; 3493 return 0; 3494} 3495 3496/* Helper routine to branch off an association to a new socket. */ 3497SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc, 3498 struct socket **sockp) 3499{ 3500 struct sock *sk = asoc->base.sk; 3501 struct socket *sock; 3502 struct inet_sock *inetsk; 3503 struct sctp_af *af; 3504 int err = 0; 3505 3506 /* An association cannot be branched off from an already peeled-off 3507 * socket, nor is this supported for tcp style sockets. 3508 */ 3509 if (!sctp_style(sk, UDP)) 3510 return -EINVAL; 3511 3512 /* Create a new socket. */ 3513 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); 3514 if (err < 0) 3515 return err; 3516 3517 /* Populate the fields of the newsk from the oldsk and migrate the 3518 * asoc to the newsk. 3519 */ 3520 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH); 3521 3522 /* Make peeled-off sockets more like 1-1 accepted sockets. 3523 * Set the daddr and initialize id to something more random 3524 */ 3525 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family); 3526 af->to_sk_daddr(&asoc->peer.primary_addr, sk); 3527 inetsk = inet_sk(sock->sk); 3528 inetsk->id = asoc->next_tsn ^ jiffies; 3529 3530 *sockp = sock; 3531 3532 return err; 3533} 3534 3535static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) 3536{ 3537 sctp_peeloff_arg_t peeloff; 3538 struct socket *newsock; 3539 int retval = 0; 3540 struct sctp_association *asoc; 3541 3542 if (len < sizeof(sctp_peeloff_arg_t)) 3543 return -EINVAL; 3544 len = sizeof(sctp_peeloff_arg_t); 3545 if (copy_from_user(&peeloff, optval, len)) 3546 return -EFAULT; 3547 3548 asoc = sctp_id2assoc(sk, peeloff.associd); 3549 if (!asoc) { 3550 retval = -EINVAL; 3551 goto out; 3552 } 3553 3554 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc); 3555 3556 retval = sctp_do_peeloff(asoc, &newsock); 3557 if (retval < 0) 3558 goto out; 3559 3560 /* Map the socket to an unused fd that can be returned to the user. */ 3561 retval = sock_map_fd(newsock); 3562 if (retval < 0) { 3563 sock_release(newsock); 3564 goto out; 3565 } 3566 3567 SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n", 3568 __FUNCTION__, sk, asoc, newsock->sk, retval); 3569 3570 /* Return the fd mapped to the new socket. */ 3571 peeloff.sd = retval; 3572 if (put_user(len, optlen)) 3573 return -EFAULT; 3574 if (copy_to_user(optval, &peeloff, len)) 3575 retval = -EFAULT; 3576 3577out: 3578 return retval; 3579} 3580 3581/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) 3582 * 3583 * Applications can enable or disable heartbeats for any peer address of 3584 * an association, modify an address's heartbeat interval, force a 3585 * heartbeat to be sent immediately, and adjust the address's maximum 3586 * number of retransmissions sent before an address is considered 3587 * unreachable. The following structure is used to access and modify an 3588 * address's parameters: 3589 * 3590 * struct sctp_paddrparams { 3591 * sctp_assoc_t spp_assoc_id; 3592 * struct sockaddr_storage spp_address; 3593 * uint32_t spp_hbinterval; 3594 * uint16_t spp_pathmaxrxt; 3595 * uint32_t spp_pathmtu; 3596 * uint32_t spp_sackdelay; 3597 * uint32_t spp_flags; 3598 * }; 3599 * 3600 * spp_assoc_id - (one-to-many style socket) This is filled in the 3601 * application, and identifies the association for 3602 * this query. 3603 * spp_address - This specifies which address is of interest. 3604 * spp_hbinterval - This contains the value of the heartbeat interval, 3605 * in milliseconds. If a value of zero 3606 * is present in this field then no changes are to 3607 * be made to this parameter. 3608 * spp_pathmaxrxt - This contains the maximum number of 3609 * retransmissions before this address shall be 3610 * considered unreachable. If a value of zero 3611 * is present in this field then no changes are to 3612 * be made to this parameter. 3613 * spp_pathmtu - When Path MTU discovery is disabled the value 3614 * specified here will be the "fixed" path mtu. 3615 * Note that if the spp_address field is empty 3616 * then all associations on this address will 3617 * have this fixed path mtu set upon them. 3618 * 3619 * spp_sackdelay - When delayed sack is enabled, this value specifies 3620 * the number of milliseconds that sacks will be delayed 3621 * for. This value will apply to all addresses of an 3622 * association if the spp_address field is empty. Note 3623 * also, that if delayed sack is enabled and this 3624 * value is set to 0, no change is made to the last 3625 * recorded delayed sack timer value. 3626 * 3627 * spp_flags - These flags are used to control various features 3628 * on an association. The flag field may contain 3629 * zero or more of the following options. 3630 * 3631 * SPP_HB_ENABLE - Enable heartbeats on the 3632 * specified address. Note that if the address 3633 * field is empty all addresses for the association 3634 * have heartbeats enabled upon them. 3635 * 3636 * SPP_HB_DISABLE - Disable heartbeats on the 3637 * speicifed address. Note that if the address 3638 * field is empty all addresses for the association 3639 * will have their heartbeats disabled. Note also 3640 * that SPP_HB_ENABLE and SPP_HB_DISABLE are 3641 * mutually exclusive, only one of these two should 3642 * be specified. Enabling both fields will have 3643 * undetermined results. 3644 * 3645 * SPP_HB_DEMAND - Request a user initiated heartbeat 3646 * to be made immediately. 3647 * 3648 * SPP_PMTUD_ENABLE - This field will enable PMTU 3649 * discovery upon the specified address. Note that 3650 * if the address feild is empty then all addresses 3651 * on the association are effected. 3652 * 3653 * SPP_PMTUD_DISABLE - This field will disable PMTU 3654 * discovery upon the specified address. Note that 3655 * if the address feild is empty then all addresses 3656 * on the association are effected. Not also that 3657 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually 3658 * exclusive. Enabling both will have undetermined 3659 * results. 3660 * 3661 * SPP_SACKDELAY_ENABLE - Setting this flag turns 3662 * on delayed sack. The time specified in spp_sackdelay 3663 * is used to specify the sack delay for this address. Note 3664 * that if spp_address is empty then all addresses will 3665 * enable delayed sack and take on the sack delay 3666 * value specified in spp_sackdelay. 3667 * SPP_SACKDELAY_DISABLE - Setting this flag turns 3668 * off delayed sack. If the spp_address field is blank then 3669 * delayed sack is disabled for the entire association. Note 3670 * also that this field is mutually exclusive to 3671 * SPP_SACKDELAY_ENABLE, setting both will have undefined 3672 * results. 3673 */ 3674static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, 3675 char __user *optval, int __user *optlen) 3676{ 3677 struct sctp_paddrparams params; 3678 struct sctp_transport *trans = NULL; 3679 struct sctp_association *asoc = NULL; 3680 struct sctp_sock *sp = sctp_sk(sk); 3681 3682 if (len < sizeof(struct sctp_paddrparams)) 3683 return -EINVAL; 3684 len = sizeof(struct sctp_paddrparams); 3685 if (copy_from_user(¶ms, optval, len)) 3686 return -EFAULT; 3687 3688 /* If an address other than INADDR_ANY is specified, and 3689 * no transport is found, then the request is invalid. 3690 */ 3691 if (!sctp_is_any(( union sctp_addr *)¶ms.spp_address)) { 3692 trans = sctp_addr_id2transport(sk, ¶ms.spp_address, 3693 params.spp_assoc_id); 3694 if (!trans) { 3695 SCTP_DEBUG_PRINTK("Failed no transport\n"); 3696 return -EINVAL; 3697 } 3698 } 3699 3700 /* Get association, if assoc_id != 0 and the socket is a one 3701 * to many style socket, and an association was not found, then 3702 * the id was invalid. 3703 */ 3704 asoc = sctp_id2assoc(sk, params.spp_assoc_id); 3705 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) { 3706 SCTP_DEBUG_PRINTK("Failed no association\n"); 3707 return -EINVAL; 3708 } 3709 3710 if (trans) { 3711 /* Fetch transport values. */ 3712 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); 3713 params.spp_pathmtu = trans->pathmtu; 3714 params.spp_pathmaxrxt = trans->pathmaxrxt; 3715 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay); 3716 3717 /*draft-11 doesn't say what to return in spp_flags*/ 3718 params.spp_flags = trans->param_flags; 3719 } else if (asoc) { 3720 /* Fetch association values. */ 3721 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); 3722 params.spp_pathmtu = asoc->pathmtu; 3723 params.spp_pathmaxrxt = asoc->pathmaxrxt; 3724 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay); 3725 3726 /*draft-11 doesn't say what to return in spp_flags*/ 3727 params.spp_flags = asoc->param_flags; 3728 } else { 3729 /* Fetch socket values. */ 3730 params.spp_hbinterval = sp->hbinterval; 3731 params.spp_pathmtu = sp->pathmtu; 3732 params.spp_sackdelay = sp->sackdelay; 3733 params.spp_pathmaxrxt = sp->pathmaxrxt; 3734 3735 /*draft-11 doesn't say what to return in spp_flags*/ 3736 params.spp_flags = sp->param_flags; 3737 } 3738 3739 if (copy_to_user(optval, ¶ms, len)) 3740 return -EFAULT; 3741 3742 if (put_user(len, optlen)) 3743 return -EFAULT; 3744 3745 return 0; 3746} 3747 3748/* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME) 3749 * 3750 * This options will get or set the delayed ack timer. The time is set 3751 * in milliseconds. If the assoc_id is 0, then this sets or gets the 3752 * endpoints default delayed ack timer value. If the assoc_id field is 3753 * non-zero, then the set or get effects the specified association. 3754 * 3755 * struct sctp_assoc_value { 3756 * sctp_assoc_t assoc_id; 3757 * uint32_t assoc_value; 3758 * }; 3759 * 3760 * assoc_id - This parameter, indicates which association the 3761 * user is preforming an action upon. Note that if 3762 * this field's value is zero then the endpoints 3763 * default value is changed (effecting future 3764 * associations only). 3765 * 3766 * assoc_value - This parameter contains the number of milliseconds 3767 * that the user is requesting the delayed ACK timer 3768 * be set to. Note that this value is defined in 3769 * the standard to be between 200 and 500 milliseconds. 3770 * 3771 * Note: a value of zero will leave the value alone, 3772 * but disable SACK delay. A non-zero value will also 3773 * enable SACK delay. 3774 */ 3775static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len, 3776 char __user *optval, 3777 int __user *optlen) 3778{ 3779 struct sctp_assoc_value params; 3780 struct sctp_association *asoc = NULL; 3781 struct sctp_sock *sp = sctp_sk(sk); 3782 3783 if (len < sizeof(struct sctp_assoc_value)) 3784 return - EINVAL; 3785 3786 len = sizeof(struct sctp_assoc_value); 3787 3788 if (copy_from_user(¶ms, optval, len)) 3789 return -EFAULT; 3790 3791 /* Get association, if assoc_id != 0 and the socket is a one 3792 * to many style socket, and an association was not found, then 3793 * the id was invalid. 3794 */ 3795 asoc = sctp_id2assoc(sk, params.assoc_id); 3796 if (!asoc && params.assoc_id && sctp_style(sk, UDP)) 3797 return -EINVAL; 3798 3799 if (asoc) { 3800 /* Fetch association values. */ 3801 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) 3802 params.assoc_value = jiffies_to_msecs( 3803 asoc->sackdelay); 3804 else 3805 params.assoc_value = 0; 3806 } else { 3807 /* Fetch socket values. */ 3808 if (sp->param_flags & SPP_SACKDELAY_ENABLE) 3809 params.assoc_value = sp->sackdelay; 3810 else 3811 params.assoc_value = 0; 3812 } 3813 3814 if (copy_to_user(optval, ¶ms, len)) 3815 return -EFAULT; 3816 3817 if (put_user(len, optlen)) 3818 return -EFAULT; 3819 3820 return 0; 3821} 3822 3823/* 7.1.3 Initialization Parameters (SCTP_INITMSG) 3824 * 3825 * Applications can specify protocol parameters for the default association 3826 * initialization. The option name argument to setsockopt() and getsockopt() 3827 * is SCTP_INITMSG. 3828 * 3829 * Setting initialization parameters is effective only on an unconnected 3830 * socket (for UDP-style sockets only future associations are effected 3831 * by the change). With TCP-style sockets, this option is inherited by 3832 * sockets derived from a listener socket. 3833 */ 3834static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) 3835{ 3836 if (len < sizeof(struct sctp_initmsg)) 3837 return -EINVAL; 3838 len = sizeof(struct sctp_initmsg); 3839 if (put_user(len, optlen)) 3840 return -EFAULT; 3841 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) 3842 return -EFAULT; 3843 return 0; 3844} 3845 3846static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len, 3847 char __user *optval, 3848 int __user *optlen) 3849{ 3850 sctp_assoc_t id; 3851 struct sctp_association *asoc; 3852 struct list_head *pos; 3853 int cnt = 0; 3854 3855 if (len < sizeof(sctp_assoc_t)) 3856 return -EINVAL; 3857 3858 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t))) 3859 return -EFAULT; 3860 3861 /* For UDP-style sockets, id specifies the association to query. */ 3862 asoc = sctp_id2assoc(sk, id); 3863 if (!asoc) 3864 return -EINVAL; 3865 3866 list_for_each(pos, &asoc->peer.transport_addr_list) { 3867 cnt ++; 3868 } 3869 3870 return cnt; 3871} 3872 3873/* 3874 * Old API for getting list of peer addresses. Does not work for 32-bit 3875 * programs running on a 64-bit kernel 3876 */ 3877static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len, 3878 char __user *optval, 3879 int __user *optlen) 3880{ 3881 struct sctp_association *asoc; 3882 struct list_head *pos; 3883 int cnt = 0; 3884 struct sctp_getaddrs_old getaddrs; 3885 struct sctp_transport *from; 3886 void __user *to; 3887 union sctp_addr temp; 3888 struct sctp_sock *sp = sctp_sk(sk); 3889 int addrlen; 3890 3891 if (len < sizeof(struct sctp_getaddrs_old)) 3892 return -EINVAL; 3893 3894 len = sizeof(struct sctp_getaddrs_old); 3895 3896 if (copy_from_user(&getaddrs, optval, len)) 3897 return -EFAULT; 3898 3899 if (getaddrs.addr_num <= 0) return -EINVAL; 3900 3901 /* For UDP-style sockets, id specifies the association to query. */ 3902 asoc = sctp_id2assoc(sk, getaddrs.assoc_id); 3903 if (!asoc) 3904 return -EINVAL; 3905 3906 to = (void __user *)getaddrs.addrs; 3907 list_for_each(pos, &asoc->peer.transport_addr_list) { 3908 from = list_entry(pos, struct sctp_transport, transports); 3909 memcpy(&temp, &from->ipaddr, sizeof(temp)); 3910 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); 3911 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len; 3912 if (copy_to_user(to, &temp, addrlen)) 3913 return -EFAULT; 3914 to += addrlen ; 3915 cnt ++; 3916 if (cnt >= getaddrs.addr_num) break; 3917 } 3918 getaddrs.addr_num = cnt; 3919 if (put_user(len, optlen)) 3920 return -EFAULT; 3921 if (copy_to_user(optval, &getaddrs, len)) 3922 return -EFAULT; 3923 3924 return 0; 3925} 3926 3927static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, 3928 char __user *optval, int __user *optlen) 3929{ 3930 struct sctp_association *asoc; 3931 struct list_head *pos; 3932 int cnt = 0; 3933 struct sctp_getaddrs getaddrs; 3934 struct sctp_transport *from; 3935 void __user *to; 3936 union sctp_addr temp; 3937 struct sctp_sock *sp = sctp_sk(sk); 3938 int addrlen; 3939 size_t space_left; 3940 int bytes_copied; 3941 3942 if (len < sizeof(struct sctp_getaddrs)) 3943 return -EINVAL; 3944 3945 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) 3946 return -EFAULT; 3947 3948 /* For UDP-style sockets, id specifies the association to query. */ 3949 asoc = sctp_id2assoc(sk, getaddrs.assoc_id); 3950 if (!asoc) 3951 return -EINVAL; 3952 3953 to = optval + offsetof(struct sctp_getaddrs,addrs); 3954 space_left = len - offsetof(struct sctp_getaddrs,addrs); 3955 3956 list_for_each(pos, &asoc->peer.transport_addr_list) { 3957 from = list_entry(pos, struct sctp_transport, transports); 3958 memcpy(&temp, &from->ipaddr, sizeof(temp)); 3959 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); 3960 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len; 3961 if (space_left < addrlen) 3962 return -ENOMEM; 3963 if (copy_to_user(to, &temp, addrlen)) 3964 return -EFAULT; 3965 to += addrlen; 3966 cnt++; 3967 space_left -= addrlen; 3968 } 3969 3970 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) 3971 return -EFAULT; 3972 bytes_copied = ((char __user *)to) - optval; 3973 if (put_user(bytes_copied, optlen)) 3974 return -EFAULT; 3975 3976 return 0; 3977} 3978 3979static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len, 3980 char __user *optval, 3981 int __user *optlen) 3982{ 3983 sctp_assoc_t id; 3984 struct sctp_bind_addr *bp; 3985 struct sctp_association *asoc; 3986 struct list_head *pos, *temp; 3987 struct sctp_sockaddr_entry *addr; 3988 rwlock_t *addr_lock; 3989 int cnt = 0; 3990 3991 if (len < sizeof(sctp_assoc_t)) 3992 return -EINVAL; 3993 3994 if (copy_from_user(&id, optval, sizeof(sctp_assoc_t))) 3995 return -EFAULT; 3996 3997 /* 3998 * For UDP-style sockets, id specifies the association to query. 3999 * If the id field is set to the value '0' then the locally bound 4000 * addresses are returned without regard to any particular 4001 * association. 4002 */ 4003 if (0 == id) { 4004 bp = &sctp_sk(sk)->ep->base.bind_addr; 4005 addr_lock = &sctp_sk(sk)->ep->base.addr_lock; 4006 } else { 4007 asoc = sctp_id2assoc(sk, id); 4008 if (!asoc) 4009 return -EINVAL; 4010 bp = &asoc->base.bind_addr; 4011 addr_lock = &asoc->base.addr_lock; 4012 } 4013 4014 sctp_read_lock(addr_lock); 4015 4016 /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid 4017 * addresses from the global local address list. 4018 */ 4019 if (sctp_list_single_entry(&bp->address_list)) { 4020 addr = list_entry(bp->address_list.next, 4021 struct sctp_sockaddr_entry, list); 4022 if (sctp_is_any(&addr->a)) { 4023 list_for_each_safe(pos, temp, &sctp_local_addr_list) { 4024 addr = list_entry(pos, 4025 struct sctp_sockaddr_entry, 4026 list); 4027 if ((PF_INET == sk->sk_family) && 4028 (AF_INET6 == addr->a.sa.sa_family)) 4029 continue; 4030 cnt++; 4031 } 4032 } else { 4033 cnt = 1; 4034 } 4035 goto done; 4036 } 4037 4038 list_for_each(pos, &bp->address_list) { 4039 cnt ++; 4040 } 4041 4042done: 4043 sctp_read_unlock(addr_lock); 4044 return cnt; 4045} 4046 4047/* Helper function that copies local addresses to user and returns the number 4048 * of addresses copied. 4049 */ 4050static int sctp_copy_laddrs_old(struct sock *sk, __u16 port, 4051 int max_addrs, void *to, 4052 int *bytes_copied) 4053{ 4054 struct list_head *pos, *next; 4055 struct sctp_sockaddr_entry *addr; 4056 union sctp_addr temp; 4057 int cnt = 0; 4058 int addrlen; 4059 4060 list_for_each_safe(pos, next, &sctp_local_addr_list) { 4061 addr = list_entry(pos, struct sctp_sockaddr_entry, list); 4062 if ((PF_INET == sk->sk_family) && 4063 (AF_INET6 == addr->a.sa.sa_family)) 4064 continue; 4065 memcpy(&temp, &addr->a, sizeof(temp)); 4066 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), 4067 &temp); 4068 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; 4069 memcpy(to, &temp, addrlen); 4070 4071 to += addrlen; 4072 *bytes_copied += addrlen; 4073 cnt ++; 4074 if (cnt >= max_addrs) break; 4075 } 4076 4077 return cnt; 4078} 4079 4080static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, 4081 size_t space_left, int *bytes_copied) 4082{ 4083 struct list_head *pos, *next; 4084 struct sctp_sockaddr_entry *addr; 4085 union sctp_addr temp; 4086 int cnt = 0; 4087 int addrlen; 4088 4089 list_for_each_safe(pos, next, &sctp_local_addr_list) { 4090 addr = list_entry(pos, struct sctp_sockaddr_entry, list); 4091 if ((PF_INET == sk->sk_family) && 4092 (AF_INET6 == addr->a.sa.sa_family)) 4093 continue; 4094 memcpy(&temp, &addr->a, sizeof(temp)); 4095 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), 4096 &temp); 4097 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; 4098 if (space_left < addrlen) 4099 return -ENOMEM; 4100 memcpy(to, &temp, addrlen); 4101 4102 to += addrlen; 4103 cnt ++; 4104 space_left -= addrlen; 4105 *bytes_copied += addrlen; 4106 } 4107 4108 return cnt; 4109} 4110 4111/* Old API for getting list of local addresses. Does not work for 32-bit 4112 * programs running on a 64-bit kernel 4113 */ 4114static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len, 4115 char __user *optval, int __user *optlen) 4116{ 4117 struct sctp_bind_addr *bp; 4118 struct sctp_association *asoc; 4119 struct list_head *pos; 4120 int cnt = 0; 4121 struct sctp_getaddrs_old getaddrs; 4122 struct sctp_sockaddr_entry *addr; 4123 void __user *to; 4124 union sctp_addr temp; 4125 struct sctp_sock *sp = sctp_sk(sk); 4126 int addrlen; 4127 rwlock_t *addr_lock; 4128 int err = 0; 4129 void *addrs; 4130 void *buf; 4131 int bytes_copied = 0; 4132 4133 if (len < sizeof(struct sctp_getaddrs_old)) 4134 return -EINVAL; 4135 4136 len = sizeof(struct sctp_getaddrs_old); 4137 if (copy_from_user(&getaddrs, optval, len)) 4138 return -EFAULT; 4139 4140 if (getaddrs.addr_num <= 0) return -EINVAL; 4141 /* 4142 * For UDP-style sockets, id specifies the association to query. 4143 * If the id field is set to the value '0' then the locally bound 4144 * addresses are returned without regard to any particular 4145 * association. 4146 */ 4147 if (0 == getaddrs.assoc_id) { 4148 bp = &sctp_sk(sk)->ep->base.bind_addr; 4149 addr_lock = &sctp_sk(sk)->ep->base.addr_lock; 4150 } else { 4151 asoc = sctp_id2assoc(sk, getaddrs.assoc_id); 4152 if (!asoc) 4153 return -EINVAL; 4154 bp = &asoc->base.bind_addr; 4155 addr_lock = &asoc->base.addr_lock; 4156 } 4157 4158 to = getaddrs.addrs; 4159 4160 /* Allocate space for a local instance of packed array to hold all 4161 * the data. We store addresses here first and then put write them 4162 * to the user in one shot. 4163 */ 4164 addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num, 4165 GFP_KERNEL); 4166 if (!addrs) 4167 return -ENOMEM; 4168 4169 sctp_read_lock(addr_lock); 4170 4171 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid 4172 * addresses from the global local address list. 4173 */ 4174 if (sctp_list_single_entry(&bp->address_list)) { 4175 addr = list_entry(bp->address_list.next, 4176 struct sctp_sockaddr_entry, list); 4177 if (sctp_is_any(&addr->a)) { 4178 cnt = sctp_copy_laddrs_old(sk, bp->port, 4179 getaddrs.addr_num, 4180 addrs, &bytes_copied); 4181 goto copy_getaddrs; 4182 } 4183 } 4184 4185 buf = addrs; 4186 list_for_each(pos, &bp->address_list) { 4187 addr = list_entry(pos, struct sctp_sockaddr_entry, list); 4188 memcpy(&temp, &addr->a, sizeof(temp)); 4189 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); 4190 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; 4191 memcpy(buf, &temp, addrlen); 4192 buf += addrlen; 4193 bytes_copied += addrlen; 4194 cnt ++; 4195 if (cnt >= getaddrs.addr_num) break; 4196 } 4197 4198copy_getaddrs: 4199 sctp_read_unlock(addr_lock); 4200 4201 /* copy the entire address list into the user provided space */ 4202 if (copy_to_user(to, addrs, bytes_copied)) { 4203 err = -EFAULT; 4204 goto error; 4205 } 4206 4207 /* copy the leading structure back to user */ 4208 getaddrs.addr_num = cnt; 4209 if (copy_to_user(optval, &getaddrs, len)) 4210 err = -EFAULT; 4211 4212error: 4213 kfree(addrs); 4214 return err; 4215} 4216 4217static int sctp_getsockopt_local_addrs(struct sock *sk, int len, 4218 char __user *optval, int __user *optlen) 4219{ 4220 struct sctp_bind_addr *bp; 4221 struct sctp_association *asoc; 4222 struct list_head *pos; 4223 int cnt = 0; 4224 struct sctp_getaddrs getaddrs; 4225 struct sctp_sockaddr_entry *addr; 4226 void __user *to; 4227 union sctp_addr temp; 4228 struct sctp_sock *sp = sctp_sk(sk); 4229 int addrlen; 4230 rwlock_t *addr_lock; 4231 int err = 0; 4232 size_t space_left; 4233 int bytes_copied = 0; 4234 void *addrs; 4235 void *buf; 4236 4237 if (len < sizeof(struct sctp_getaddrs)) 4238 return -EINVAL; 4239 4240 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) 4241 return -EFAULT; 4242 4243 /* 4244 * For UDP-style sockets, id specifies the association to query. 4245 * If the id field is set to the value '0' then the locally bound 4246 * addresses are returned without regard to any particular 4247 * association. 4248 */ 4249 if (0 == getaddrs.assoc_id) { 4250 bp = &sctp_sk(sk)->ep->base.bind_addr; 4251 addr_lock = &sctp_sk(sk)->ep->base.addr_lock; 4252 } else { 4253 asoc = sctp_id2assoc(sk, getaddrs.assoc_id); 4254 if (!asoc) 4255 return -EINVAL; 4256 bp = &asoc->base.bind_addr; 4257 addr_lock = &asoc->base.addr_lock; 4258 } 4259 4260 to = optval + offsetof(struct sctp_getaddrs,addrs); 4261 space_left = len - offsetof(struct sctp_getaddrs,addrs); 4262 4263 addrs = kmalloc(space_left, GFP_KERNEL); 4264 if (!addrs) 4265 return -ENOMEM; 4266 4267 sctp_read_lock(addr_lock); 4268 4269 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid 4270 * addresses from the global local address list. 4271 */ 4272 if (sctp_list_single_entry(&bp->address_list)) { 4273 addr = list_entry(bp->address_list.next, 4274 struct sctp_sockaddr_entry, list); 4275 if (sctp_is_any(&addr->a)) { 4276 cnt = sctp_copy_laddrs(sk, bp->port, addrs, 4277 space_left, &bytes_copied); 4278 if (cnt < 0) { 4279 err = cnt; 4280 goto error; 4281 } 4282 goto copy_getaddrs; 4283 } 4284 } 4285 4286 buf = addrs; 4287 list_for_each(pos, &bp->address_list) { 4288 addr = list_entry(pos, struct sctp_sockaddr_entry, list); 4289 memcpy(&temp, &addr->a, sizeof(temp)); 4290 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); 4291 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; 4292 if (space_left < addrlen) { 4293 err = -ENOMEM; 4294 goto error; 4295 } 4296 memcpy(buf, &temp, addrlen); 4297 buf += addrlen; 4298 bytes_copied += addrlen; 4299 cnt ++; 4300 space_left -= addrlen; 4301 } 4302 4303copy_getaddrs: 4304 sctp_read_unlock(addr_lock); 4305 4306 if (copy_to_user(to, addrs, bytes_copied)) { 4307 err = -EFAULT; 4308 goto error; 4309 } 4310 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) { 4311 err = -EFAULT; 4312 goto error; 4313 } 4314 if (put_user(bytes_copied, optlen)) 4315 err = -EFAULT; 4316error: 4317 kfree(addrs); 4318 return err; 4319} 4320 4321/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) 4322 * 4323 * Requests that the local SCTP stack use the enclosed peer address as 4324 * the association primary. The enclosed address must be one of the 4325 * association peer's addresses. 4326 */ 4327static int sctp_getsockopt_primary_addr(struct sock *sk, int len, 4328 char __user *optval, int __user *optlen) 4329{ 4330 struct sctp_prim prim; 4331 struct sctp_association *asoc; 4332 struct sctp_sock *sp = sctp_sk(sk); 4333 4334 if (len < sizeof(struct sctp_prim)) 4335 return -EINVAL; 4336 4337 len = sizeof(struct sctp_prim); 4338 4339 if (copy_from_user(&prim, optval, len)) 4340 return -EFAULT; 4341 4342 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); 4343 if (!asoc) 4344 return -EINVAL; 4345 4346 if (!asoc->peer.primary_path) 4347 return -ENOTCONN; 4348 4349 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, 4350 asoc->peer.primary_path->af_specific->sockaddr_len); 4351 4352 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, 4353 (union sctp_addr *)&prim.ssp_addr); 4354 4355 if (put_user(len, optlen)) 4356 return -EFAULT; 4357 if (copy_to_user(optval, &prim, len)) 4358 return -EFAULT; 4359 4360 return 0; 4361} 4362 4363/* 4364 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) 4365 * 4366 * Requests that the local endpoint set the specified Adaptation Layer 4367 * Indication parameter for all future INIT and INIT-ACK exchanges. 4368 */ 4369static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, 4370 char __user *optval, int __user *optlen) 4371{ 4372 struct sctp_setadaptation adaptation; 4373 4374 if (len < sizeof(struct sctp_setadaptation)) 4375 return -EINVAL; 4376 4377 len = sizeof(struct sctp_setadaptation); 4378 4379 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; 4380 4381 if (put_user(len, optlen)) 4382 return -EFAULT; 4383 if (copy_to_user(optval, &adaptation, len)) 4384 return -EFAULT; 4385 4386 return 0; 4387} 4388 4389/* 4390 * 4391 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) 4392 * 4393 * Applications that wish to use the sendto() system call may wish to 4394 * specify a default set of parameters that would normally be supplied 4395 * through the inclusion of ancillary data. This socket option allows 4396 * such an application to set the default sctp_sndrcvinfo structure. 4397 4398 4399 * The application that wishes to use this socket option simply passes 4400 * in to this call the sctp_sndrcvinfo structure defined in Section 4401 * 5.2.2) The input parameters accepted by this call include 4402 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, 4403 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in 4404 * to this call if the caller is using the UDP model. 4405 * 4406 * For getsockopt, it get the default sctp_sndrcvinfo structure. 4407 */ 4408static int sctp_getsockopt_default_send_param(struct sock *sk, 4409 int len, char __user *optval, 4410 int __user *optlen) 4411{ 4412 struct sctp_sndrcvinfo info; 4413 struct sctp_association *asoc; 4414 struct sctp_sock *sp = sctp_sk(sk); 4415 4416 if (len < sizeof(struct sctp_sndrcvinfo)) 4417 return -EINVAL; 4418 4419 len = sizeof(struct sctp_sndrcvinfo); 4420 4421 if (copy_from_user(&info, optval, len)) 4422 return -EFAULT; 4423 4424 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); 4425 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) 4426 return -EINVAL; 4427 4428 if (asoc) { 4429 info.sinfo_stream = asoc->default_stream; 4430 info.sinfo_flags = asoc->default_flags; 4431 info.sinfo_ppid = asoc->default_ppid; 4432 info.sinfo_context = asoc->default_context; 4433 info.sinfo_timetolive = asoc->default_timetolive; 4434 } else { 4435 info.sinfo_stream = sp->default_stream; 4436 info.sinfo_flags = sp->default_flags; 4437 info.sinfo_ppid = sp->default_ppid; 4438 info.sinfo_context = sp->default_context; 4439 info.sinfo_timetolive = sp->default_timetolive; 4440 } 4441 4442 if (put_user(len, optlen)) 4443 return -EFAULT; 4444 if (copy_to_user(optval, &info, len)) 4445 return -EFAULT; 4446 4447 return 0; 4448} 4449 4450/* 4451 * 4452 * 7.1.5 SCTP_NODELAY 4453 * 4454 * Turn on/off any Nagle-like algorithm. This means that packets are 4455 * generally sent as soon as possible and no unnecessary delays are 4456 * introduced, at the cost of more packets in the network. Expects an 4457 * integer boolean flag. 4458 */ 4459 4460static int sctp_getsockopt_nodelay(struct sock *sk, int len, 4461 char __user *optval, int __user *optlen) 4462{ 4463 int val; 4464 4465 if (len < sizeof(int)) 4466 return -EINVAL; 4467 4468 len = sizeof(int); 4469 val = (sctp_sk(sk)->nodelay == 1); 4470 if (put_user(len, optlen)) 4471 return -EFAULT; 4472 if (copy_to_user(optval, &val, len)) 4473 return -EFAULT; 4474 return 0; 4475} 4476 4477/* 4478 * 4479 * 7.1.1 SCTP_RTOINFO 4480 * 4481 * The protocol parameters used to initialize and bound retransmission 4482 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access 4483 * and modify these parameters. 4484 * All parameters are time values, in milliseconds. A value of 0, when 4485 * modifying the parameters, indicates that the current value should not 4486 * be changed. 4487 * 4488 */ 4489static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, 4490 char __user *optval, 4491 int __user *optlen) { 4492 struct sctp_rtoinfo rtoinfo; 4493 struct sctp_association *asoc; 4494 4495 if (len < sizeof (struct sctp_rtoinfo)) 4496 return -EINVAL; 4497 4498 len = sizeof(struct sctp_rtoinfo); 4499 4500 if (copy_from_user(&rtoinfo, optval, len)) 4501 return -EFAULT; 4502 4503 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); 4504 4505 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) 4506 return -EINVAL; 4507 4508 /* Values corresponding to the specific association. */ 4509 if (asoc) { 4510 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); 4511 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); 4512 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); 4513 } else { 4514 /* Values corresponding to the endpoint. */ 4515 struct sctp_sock *sp = sctp_sk(sk); 4516 4517 rtoinfo.srto_initial = sp->rtoinfo.srto_initial; 4518 rtoinfo.srto_max = sp->rtoinfo.srto_max; 4519 rtoinfo.srto_min = sp->rtoinfo.srto_min; 4520 } 4521 4522 if (put_user(len, optlen)) 4523 return -EFAULT; 4524 4525 if (copy_to_user(optval, &rtoinfo, len)) 4526 return -EFAULT; 4527 4528 return 0; 4529} 4530 4531/* 4532 * 4533 * 7.1.2 SCTP_ASSOCINFO 4534 * 4535 * This option is used to tune the maximum retransmission attempts 4536 * of the association. 4537 * Returns an error if the new association retransmission value is 4538 * greater than the sum of the retransmission value of the peer. 4539 * See [SCTP] for more information. 4540 * 4541 */ 4542static int sctp_getsockopt_associnfo(struct sock *sk, int len, 4543 char __user *optval, 4544 int __user *optlen) 4545{ 4546 4547 struct sctp_assocparams assocparams; 4548 struct sctp_association *asoc; 4549 struct list_head *pos; 4550 int cnt = 0; 4551 4552 if (len < sizeof (struct sctp_assocparams)) 4553 return -EINVAL; 4554 4555 len = sizeof(struct sctp_assocparams); 4556 4557 if (copy_from_user(&assocparams, optval, len)) 4558 return -EFAULT; 4559 4560 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); 4561 4562 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) 4563 return -EINVAL; 4564 4565 /* Values correspoinding to the specific association */ 4566 if (asoc) { 4567 assocparams.sasoc_asocmaxrxt = asoc->max_retrans; 4568 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; 4569 assocparams.sasoc_local_rwnd = asoc->a_rwnd; 4570 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec 4571 * 1000) + 4572 (asoc->cookie_life.tv_usec 4573 / 1000); 4574 4575 list_for_each(pos, &asoc->peer.transport_addr_list) { 4576 cnt ++; 4577 } 4578 4579 assocparams.sasoc_number_peer_destinations = cnt; 4580 } else { 4581 /* Values corresponding to the endpoint */ 4582 struct sctp_sock *sp = sctp_sk(sk); 4583 4584 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; 4585 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; 4586 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; 4587 assocparams.sasoc_cookie_life = 4588 sp->assocparams.sasoc_cookie_life; 4589 assocparams.sasoc_number_peer_destinations = 4590 sp->assocparams. 4591 sasoc_number_peer_destinations; 4592 } 4593 4594 if (put_user(len, optlen)) 4595 return -EFAULT; 4596 4597 if (copy_to_user(optval, &assocparams, len)) 4598 return -EFAULT; 4599 4600 return 0; 4601} 4602 4603/* 4604 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) 4605 * 4606 * This socket option is a boolean flag which turns on or off mapped V4 4607 * addresses. If this option is turned on and the socket is type 4608 * PF_INET6, then IPv4 addresses will be mapped to V6 representation. 4609 * If this option is turned off, then no mapping will be done of V4 4610 * addresses and a user will receive both PF_INET6 and PF_INET type 4611 * addresses on the socket. 4612 */ 4613static int sctp_getsockopt_mappedv4(struct sock *sk, int len, 4614 char __user *optval, int __user *optlen) 4615{ 4616 int val; 4617 struct sctp_sock *sp = sctp_sk(sk); 4618 4619 if (len < sizeof(int)) 4620 return -EINVAL; 4621 4622 len = sizeof(int); 4623 val = sp->v4mapped; 4624 if (put_user(len, optlen)) 4625 return -EFAULT; 4626 if (copy_to_user(optval, &val, len)) 4627 return -EFAULT; 4628 4629 return 0; 4630} 4631 4632/* 4633 * 7.1.29. Set or Get the default context (SCTP_CONTEXT) 4634 * (chapter and verse is quoted at sctp_setsockopt_context()) 4635 */ 4636static int sctp_getsockopt_context(struct sock *sk, int len, 4637 char __user *optval, int __user *optlen) 4638{ 4639 struct sctp_assoc_value params; 4640 struct sctp_sock *sp; 4641 struct sctp_association *asoc; 4642 4643 if (len < sizeof(struct sctp_assoc_value)) 4644 return -EINVAL; 4645 4646 len = sizeof(struct sctp_assoc_value); 4647 4648 if (copy_from_user(¶ms, optval, len)) 4649 return -EFAULT; 4650 4651 sp = sctp_sk(sk); 4652 4653 if (params.assoc_id != 0) { 4654 asoc = sctp_id2assoc(sk, params.assoc_id); 4655 if (!asoc) 4656 return -EINVAL; 4657 params.assoc_value = asoc->default_rcv_context; 4658 } else { 4659 params.assoc_value = sp->default_rcv_context; 4660 } 4661 4662 if (put_user(len, optlen)) 4663 return -EFAULT; 4664 if (copy_to_user(optval, ¶ms, len)) 4665 return -EFAULT; 4666 4667 return 0; 4668} 4669 4670/* 4671 * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG) 4672 * 4673 * This socket option specifies the maximum size to put in any outgoing 4674 * SCTP chunk. If a message is larger than this size it will be 4675 * fragmented by SCTP into the specified size. Note that the underlying 4676 * SCTP implementation may fragment into smaller sized chunks when the 4677 * PMTU of the underlying association is smaller than the value set by 4678 * the user. 4679 */ 4680static int sctp_getsockopt_maxseg(struct sock *sk, int len, 4681 char __user *optval, int __user *optlen) 4682{ 4683 int val; 4684 4685 if (len < sizeof(int)) 4686 return -EINVAL; 4687 4688 len = sizeof(int); 4689 4690 val = sctp_sk(sk)->user_frag; 4691 if (put_user(len, optlen)) 4692 return -EFAULT; 4693 if (copy_to_user(optval, &val, len)) 4694 return -EFAULT; 4695 4696 return 0; 4697} 4698 4699/* 4700 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) 4701 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) 4702 */ 4703static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, 4704 char __user *optval, int __user *optlen) 4705{ 4706 int val; 4707 4708 if (len < sizeof(int)) 4709 return -EINVAL; 4710 4711 len = sizeof(int); 4712 4713 val = sctp_sk(sk)->frag_interleave; 4714 if (put_user(len, optlen)) 4715 return -EFAULT; 4716 if (copy_to_user(optval, &val, len)) 4717 return -EFAULT; 4718 4719 return 0; 4720} 4721 4722/* 4723 * 7.1.25. Set or Get the sctp partial delivery point 4724 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) 4725 */ 4726static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, 4727 char __user *optval, 4728 int __user *optlen) 4729{ 4730 u32 val; 4731 4732 if (len < sizeof(u32)) 4733 return -EINVAL; 4734 4735 len = sizeof(u32); 4736 4737 val = sctp_sk(sk)->pd_point; 4738 if (put_user(len, optlen)) 4739 return -EFAULT; 4740 if (copy_to_user(optval, &val, len)) 4741 return -EFAULT; 4742 4743 return -ENOTSUPP; 4744} 4745 4746/* 4747 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) 4748 * (chapter and verse is quoted at sctp_setsockopt_maxburst()) 4749 */ 4750static int sctp_getsockopt_maxburst(struct sock *sk, int len, 4751 char __user *optval, 4752 int __user *optlen) 4753{ 4754 int val; 4755 4756 if (len < sizeof(int)) 4757 return -EINVAL; 4758 4759 len = sizeof(int); 4760 4761 val = sctp_sk(sk)->max_burst; 4762 if (put_user(len, optlen)) 4763 return -EFAULT; 4764 if (copy_to_user(optval, &val, len)) 4765 return -EFAULT; 4766 4767 return -ENOTSUPP; 4768} 4769 4770SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname, 4771 char __user *optval, int __user *optlen) 4772{ 4773 int retval = 0; 4774 int len; 4775 4776 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n", 4777 sk, optname); 4778 4779 /* I can hardly begin to describe how wrong this is. This is 4780 * so broken as to be worse than useless. The API draft 4781 * REALLY is NOT helpful here... I am not convinced that the 4782 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP 4783 * are at all well-founded. 4784 */ 4785 if (level != SOL_SCTP) { 4786 struct sctp_af *af = sctp_sk(sk)->pf->af; 4787 4788 retval = af->getsockopt(sk, level, optname, optval, optlen); 4789 return retval; 4790 } 4791 4792 if (get_user(len, optlen)) 4793 return -EFAULT; 4794 4795 sctp_lock_sock(sk); 4796 4797 switch (optname) { 4798 case SCTP_STATUS: 4799 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); 4800 break; 4801 case SCTP_DISABLE_FRAGMENTS: 4802 retval = sctp_getsockopt_disable_fragments(sk, len, optval, 4803 optlen); 4804 break; 4805 case SCTP_EVENTS: 4806 retval = sctp_getsockopt_events(sk, len, optval, optlen); 4807 break; 4808 case SCTP_AUTOCLOSE: 4809 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); 4810 break; 4811 case SCTP_SOCKOPT_PEELOFF: 4812 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); 4813 break; 4814 case SCTP_PEER_ADDR_PARAMS: 4815 retval = sctp_getsockopt_peer_addr_params(sk, len, optval, 4816 optlen); 4817 break; 4818 case SCTP_DELAYED_ACK_TIME: 4819 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval, 4820 optlen); 4821 break; 4822 case SCTP_INITMSG: 4823 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); 4824 break; 4825 case SCTP_GET_PEER_ADDRS_NUM_OLD: 4826 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval, 4827 optlen); 4828 break; 4829 case SCTP_GET_LOCAL_ADDRS_NUM_OLD: 4830 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval, 4831 optlen); 4832 break; 4833 case SCTP_GET_PEER_ADDRS_OLD: 4834 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval, 4835 optlen); 4836 break; 4837 case SCTP_GET_LOCAL_ADDRS_OLD: 4838 retval = sctp_getsockopt_local_addrs_old(sk, len, optval, 4839 optlen); 4840 break; 4841 case SCTP_GET_PEER_ADDRS: 4842 retval = sctp_getsockopt_peer_addrs(sk, len, optval, 4843 optlen); 4844 break; 4845 case SCTP_GET_LOCAL_ADDRS: 4846 retval = sctp_getsockopt_local_addrs(sk, len, optval, 4847 optlen); 4848 break; 4849 case SCTP_DEFAULT_SEND_PARAM: 4850 retval = sctp_getsockopt_default_send_param(sk, len, 4851 optval, optlen); 4852 break; 4853 case SCTP_PRIMARY_ADDR: 4854 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); 4855 break; 4856 case SCTP_NODELAY: 4857 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); 4858 break; 4859 case SCTP_RTOINFO: 4860 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); 4861 break; 4862 case SCTP_ASSOCINFO: 4863 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); 4864 break; 4865 case SCTP_I_WANT_MAPPED_V4_ADDR: 4866 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); 4867 break; 4868 case SCTP_MAXSEG: 4869 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); 4870 break; 4871 case SCTP_GET_PEER_ADDR_INFO: 4872 retval = sctp_getsockopt_peer_addr_info(sk, len, optval, 4873 optlen); 4874 break; 4875 case SCTP_ADAPTATION_LAYER: 4876 retval = sctp_getsockopt_adaptation_layer(sk, len, optval, 4877 optlen); 4878 break; 4879 case SCTP_CONTEXT: 4880 retval = sctp_getsockopt_context(sk, len, optval, optlen); 4881 break; 4882 case SCTP_FRAGMENT_INTERLEAVE: 4883 retval = sctp_getsockopt_fragment_interleave(sk, len, optval, 4884 optlen); 4885 break; 4886 case SCTP_PARTIAL_DELIVERY_POINT: 4887 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, 4888 optlen); 4889 break; 4890 case SCTP_MAX_BURST: 4891 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); 4892 break; 4893 default: 4894 retval = -ENOPROTOOPT; 4895 break; 4896 } 4897 4898 sctp_release_sock(sk); 4899 return retval; 4900} 4901 4902static void sctp_hash(struct sock *sk) 4903{ 4904 /* STUB */ 4905} 4906 4907static void sctp_unhash(struct sock *sk) 4908{ 4909 /* STUB */ 4910} 4911 4912static struct sctp_bind_bucket *sctp_bucket_create( 4913 struct sctp_bind_hashbucket *head, unsigned short snum); 4914 4915static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) 4916{ 4917 struct sctp_bind_hashbucket *head; /* hash list */ 4918 struct sctp_bind_bucket *pp; /* hash list port iterator */ 4919 unsigned short snum; 4920 int ret; 4921 4922 snum = ntohs(addr->v4.sin_port); 4923 4924 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum); 4925 sctp_local_bh_disable(); 4926 4927 if (snum == 0) { 4928 /* Search for an available port. 4929 * 4930 * 'sctp_port_rover' was the last port assigned, so 4931 * we start to search from 'sctp_port_rover + 4932 * 1'. What we do is first check if port 'rover' is 4933 * already in the hash table; if not, we use that; if 4934 * it is, we try next. 4935 */ 4936 int low = sysctl_local_port_range[0]; 4937 int high = sysctl_local_port_range[1]; 4938 int remaining = (high - low) + 1; 4939 int rover; 4940 int index; 4941 4942 sctp_spin_lock(&sctp_port_alloc_lock); 4943 rover = sctp_port_rover; 4944 do { 4945 rover++; 4946 if ((rover < low) || (rover > high)) 4947 rover = low; 4948 index = sctp_phashfn(rover); 4949 head = &sctp_port_hashtable[index]; 4950 sctp_spin_lock(&head->lock); 4951 for (pp = head->chain; pp; pp = pp->next) 4952 if (pp->port == rover) 4953 goto next; 4954 break; 4955 next: 4956 sctp_spin_unlock(&head->lock); 4957 } while (--remaining > 0); 4958 sctp_port_rover = rover; 4959 sctp_spin_unlock(&sctp_port_alloc_lock); 4960 4961 /* Exhausted local port range during search? */ 4962 ret = 1; 4963 if (remaining <= 0) 4964 goto fail; 4965 4966 /* OK, here is the one we will use. HEAD (the port 4967 * hash table list entry) is non-NULL and we hold it's 4968 * mutex. 4969 */ 4970 snum = rover; 4971 } else { 4972 /* We are given an specific port number; we verify 4973 * that it is not being used. If it is used, we will 4974 * exahust the search in the hash list corresponding 4975 * to the port number (snum) - we detect that with the 4976 * port iterator, pp being NULL. 4977 */ 4978 head = &sctp_port_hashtable[sctp_phashfn(snum)]; 4979 sctp_spin_lock(&head->lock); 4980 for (pp = head->chain; pp; pp = pp->next) { 4981 if (pp->port == snum) 4982 goto pp_found; 4983 } 4984 } 4985 pp = NULL; 4986 goto pp_not_found; 4987pp_found: 4988 if (!hlist_empty(&pp->owner)) { 4989 /* We had a port hash table hit - there is an 4990 * available port (pp != NULL) and it is being 4991 * used by other socket (pp->owner not empty); that other 4992 * socket is going to be sk2. 4993 */ 4994 int reuse = sk->sk_reuse; 4995 struct sock *sk2; 4996 struct hlist_node *node; 4997 4998 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n"); 4999 if (pp->fastreuse && sk->sk_reuse && 5000 sk->sk_state != SCTP_SS_LISTENING) 5001 goto success; 5002 5003 /* Run through the list of sockets bound to the port 5004 * (pp->port) [via the pointers bind_next and 5005 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, 5006 * we get the endpoint they describe and run through 5007 * the endpoint's list of IP (v4 or v6) addresses, 5008 * comparing each of the addresses with the address of 5009 * the socket sk. If we find a match, then that means 5010 * that this port/socket (sk) combination are already 5011 * in an endpoint. 5012 */ 5013 sk_for_each_bound(sk2, node, &pp->owner) { 5014 struct sctp_endpoint *ep2; 5015 ep2 = sctp_sk(sk2)->ep; 5016 5017 if (reuse && sk2->sk_reuse && 5018 sk2->sk_state != SCTP_SS_LISTENING) 5019 continue; 5020 5021 if (sctp_bind_addr_match(&ep2->base.bind_addr, addr, 5022 sctp_sk(sk))) { 5023 ret = (long)sk2; 5024 goto fail_unlock; 5025 } 5026 } 5027 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n"); 5028 } 5029pp_not_found: 5030 /* If there was a hash table miss, create a new port. */ 5031 ret = 1; 5032 if (!pp && !(pp = sctp_bucket_create(head, snum))) 5033 goto fail_unlock; 5034 5035 /* In either case (hit or miss), make sure fastreuse is 1 only 5036 * if sk->sk_reuse is too (that is, if the caller requested 5037 * SO_REUSEADDR on this socket -sk-). 5038 */ 5039 if (hlist_empty(&pp->owner)) { 5040 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING) 5041 pp->fastreuse = 1; 5042 else 5043 pp->fastreuse = 0; 5044 } else if (pp->fastreuse && 5045 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING)) 5046 pp->fastreuse = 0; 5047 5048success: 5049 if (!sctp_sk(sk)->bind_hash) { 5050 inet_sk(sk)->num = snum; 5051 sk_add_bind_node(sk, &pp->owner); 5052 sctp_sk(sk)->bind_hash = pp; 5053 } 5054 ret = 0; 5055 5056fail_unlock: 5057 sctp_spin_unlock(&head->lock); 5058 5059fail: 5060 sctp_local_bh_enable(); 5061 return ret; 5062} 5063 5064/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral 5065 * port is requested. 5066 */ 5067static int sctp_get_port(struct sock *sk, unsigned short snum) 5068{ 5069 long ret; 5070 union sctp_addr addr; 5071 struct sctp_af *af = sctp_sk(sk)->pf->af; 5072 5073 /* Set up a dummy address struct from the sk. */ 5074 af->from_sk(&addr, sk); 5075 addr.v4.sin_port = htons(snum); 5076 5077 /* Note: sk->sk_num gets filled in if ephemeral port request. */ 5078 ret = sctp_get_port_local(sk, &addr); 5079 5080 return (ret ? 1 : 0); 5081} 5082 5083/* 5084 * 3.1.3 listen() - UDP Style Syntax 5085 * 5086 * By default, new associations are not accepted for UDP style sockets. 5087 * An application uses listen() to mark a socket as being able to 5088 * accept new associations. 5089 */ 5090SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog) 5091{ 5092 struct sctp_sock *sp = sctp_sk(sk); 5093 struct sctp_endpoint *ep = sp->ep; 5094 5095 /* Only UDP style sockets that are not peeled off are allowed to 5096 * listen(). 5097 */ 5098 if (!sctp_style(sk, UDP)) 5099 return -EINVAL; 5100 5101 /* If backlog is zero, disable listening. */ 5102 if (!backlog) { 5103 if (sctp_sstate(sk, CLOSED)) 5104 return 0; 5105 5106 sctp_unhash_endpoint(ep); 5107 sk->sk_state = SCTP_SS_CLOSED; 5108 } 5109 5110 /* Return if we are already listening. */ 5111 if (sctp_sstate(sk, LISTENING)) 5112 return 0; 5113 5114 /* 5115 * If a bind() or sctp_bindx() is not called prior to a listen() 5116 * call that allows new associations to be accepted, the system 5117 * picks an ephemeral port and will choose an address set equivalent 5118 * to binding with a wildcard address. 5119 * 5120 * This is not currently spelled out in the SCTP sockets 5121 * extensions draft, but follows the practice as seen in TCP 5122 * sockets. 5123 * 5124 * Additionally, turn off fastreuse flag since we are not listening 5125 */ 5126 sk->sk_state = SCTP_SS_LISTENING; 5127 if (!ep->base.bind_addr.port) { 5128 if (sctp_autobind(sk)) 5129 return -EAGAIN; 5130 } else 5131 sctp_sk(sk)->bind_hash->fastreuse = 0; 5132 5133 sctp_hash_endpoint(ep); 5134 return 0; 5135} 5136 5137/* 5138 * 4.1.3 listen() - TCP Style Syntax 5139 * 5140 * Applications uses listen() to ready the SCTP endpoint for accepting 5141 * inbound associations. 5142 */ 5143SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog) 5144{ 5145 struct sctp_sock *sp = sctp_sk(sk); 5146 struct sctp_endpoint *ep = sp->ep; 5147 5148 /* If backlog is zero, disable listening. */ 5149 if (!backlog) { 5150 if (sctp_sstate(sk, CLOSED)) 5151 return 0; 5152 5153 sctp_unhash_endpoint(ep); 5154 sk->sk_state = SCTP_SS_CLOSED; 5155 } 5156 5157 if (sctp_sstate(sk, LISTENING)) 5158 return 0; 5159 5160 /* 5161 * If a bind() or sctp_bindx() is not called prior to a listen() 5162 * call that allows new associations to be accepted, the system 5163 * picks an ephemeral port and will choose an address set equivalent 5164 * to binding with a wildcard address. 5165 * 5166 * This is not currently spelled out in the SCTP sockets 5167 * extensions draft, but follows the practice as seen in TCP 5168 * sockets. 5169 */ 5170 sk->sk_state = SCTP_SS_LISTENING; 5171 if (!ep->base.bind_addr.port) { 5172 if (sctp_autobind(sk)) 5173 return -EAGAIN; 5174 } else 5175 sctp_sk(sk)->bind_hash->fastreuse = 0; 5176 5177 sk->sk_max_ack_backlog = backlog; 5178 sctp_hash_endpoint(ep); 5179 return 0; 5180} 5181 5182/* 5183 * Move a socket to LISTENING state. 5184 */ 5185int sctp_inet_listen(struct socket *sock, int backlog) 5186{ 5187 struct sock *sk = sock->sk; 5188 struct crypto_hash *tfm = NULL; 5189 int err = -EINVAL; 5190 5191 if (unlikely(backlog < 0)) 5192 goto out; 5193 5194 sctp_lock_sock(sk); 5195 5196 if (sock->state != SS_UNCONNECTED) 5197 goto out; 5198 5199 /* Allocate HMAC for generating cookie. */ 5200 if (sctp_hmac_alg) { 5201 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC); 5202 if (IS_ERR(tfm)) { 5203 if (net_ratelimit()) { 5204 printk(KERN_INFO 5205 "SCTP: failed to load transform for %s: %ld\n", 5206 sctp_hmac_alg, PTR_ERR(tfm)); 5207 } 5208 err = -ENOSYS; 5209 goto out; 5210 } 5211 } 5212 5213 switch (sock->type) { 5214 case SOCK_SEQPACKET: 5215 err = sctp_seqpacket_listen(sk, backlog); 5216 break; 5217 case SOCK_STREAM: 5218 err = sctp_stream_listen(sk, backlog); 5219 break; 5220 default: 5221 break; 5222 } 5223 5224 if (err) 5225 goto cleanup; 5226 5227 /* Store away the transform reference. */ 5228 sctp_sk(sk)->hmac = tfm; 5229out: 5230 sctp_release_sock(sk); 5231 return err; 5232cleanup: 5233 crypto_free_hash(tfm); 5234 goto out; 5235} 5236 5237/* 5238 * This function is done by modeling the current datagram_poll() and the 5239 * tcp_poll(). Note that, based on these implementations, we don't 5240 * lock the socket in this function, even though it seems that, 5241 * ideally, locking or some other mechanisms can be used to ensure 5242 * the integrity of the counters (sndbuf and wmem_alloc) used 5243 * in this place. We assume that we don't need locks either until proven 5244 * otherwise. 5245 * 5246 * Another thing to note is that we include the Async I/O support 5247 * here, again, by modeling the current TCP/UDP code. We don't have 5248 * a good way to test with it yet. 5249 */ 5250unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait) 5251{ 5252 struct sock *sk = sock->sk; 5253 struct sctp_sock *sp = sctp_sk(sk); 5254 unsigned int mask; 5255 5256 poll_wait(file, sk->sk_sleep, wait); 5257 5258 /* A TCP-style listening socket becomes readable when the accept queue 5259 * is not empty. 5260 */ 5261 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) 5262 return (!list_empty(&sp->ep->asocs)) ? 5263 (POLLIN | POLLRDNORM) : 0; 5264 5265 mask = 0; 5266 5267 /* Is there any exceptional events? */ 5268 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) 5269 mask |= POLLERR; 5270 if (sk->sk_shutdown & RCV_SHUTDOWN) 5271 mask |= POLLRDHUP; 5272 if (sk->sk_shutdown == SHUTDOWN_MASK) 5273 mask |= POLLHUP; 5274 5275 /* Is it readable? Reconsider this code with TCP-style support. */ 5276 if (!skb_queue_empty(&sk->sk_receive_queue) || 5277 (sk->sk_shutdown & RCV_SHUTDOWN)) 5278 mask |= POLLIN | POLLRDNORM; 5279 5280 /* The association is either gone or not ready. */ 5281 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) 5282 return mask; 5283 5284 /* Is it writable? */ 5285 if (sctp_writeable(sk)) { 5286 mask |= POLLOUT | POLLWRNORM; 5287 } else { 5288 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); 5289 /* 5290 * Since the socket is not locked, the buffer 5291 * might be made available after the writeable check and 5292 * before the bit is set. This could cause a lost I/O 5293 * signal. tcp_poll() has a race breaker for this race 5294 * condition. Based on their implementation, we put 5295 * in the following code to cover it as well. 5296 */ 5297 if (sctp_writeable(sk)) 5298 mask |= POLLOUT | POLLWRNORM; 5299 } 5300 return mask; 5301} 5302 5303/******************************************************************** 5304 * 2nd Level Abstractions 5305 ********************************************************************/ 5306 5307static struct sctp_bind_bucket *sctp_bucket_create( 5308 struct sctp_bind_hashbucket *head, unsigned short snum) 5309{ 5310 struct sctp_bind_bucket *pp; 5311 5312 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); 5313 SCTP_DBG_OBJCNT_INC(bind_bucket); 5314 if (pp) { 5315 pp->port = snum; 5316 pp->fastreuse = 0; 5317 INIT_HLIST_HEAD(&pp->owner); 5318 if ((pp->next = head->chain) != NULL) 5319 pp->next->pprev = &pp->next; 5320 head->chain = pp; 5321 pp->pprev = &head->chain; 5322 } 5323 return pp; 5324} 5325 5326/* Caller must hold hashbucket lock for this tb with local BH disabled */ 5327static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) 5328{ 5329 if (pp && hlist_empty(&pp->owner)) { 5330 if (pp->next) 5331 pp->next->pprev = pp->pprev; 5332 *(pp->pprev) = pp->next; 5333 kmem_cache_free(sctp_bucket_cachep, pp); 5334 SCTP_DBG_OBJCNT_DEC(bind_bucket); 5335 } 5336} 5337 5338/* Release this socket's reference to a local port. */ 5339static inline void __sctp_put_port(struct sock *sk) 5340{ 5341 struct sctp_bind_hashbucket *head = 5342 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)]; 5343 struct sctp_bind_bucket *pp; 5344 5345 sctp_spin_lock(&head->lock); 5346 pp = sctp_sk(sk)->bind_hash; 5347 __sk_del_bind_node(sk); 5348 sctp_sk(sk)->bind_hash = NULL; 5349 inet_sk(sk)->num = 0; 5350 sctp_bucket_destroy(pp); 5351 sctp_spin_unlock(&head->lock); 5352} 5353 5354void sctp_put_port(struct sock *sk) 5355{ 5356 sctp_local_bh_disable(); 5357 __sctp_put_port(sk); 5358 sctp_local_bh_enable(); 5359} 5360 5361/* 5362 * The system picks an ephemeral port and choose an address set equivalent 5363 * to binding with a wildcard address. 5364 * One of those addresses will be the primary address for the association. 5365 * This automatically enables the multihoming capability of SCTP. 5366 */ 5367static int sctp_autobind(struct sock *sk) 5368{ 5369 union sctp_addr autoaddr; 5370 struct sctp_af *af; 5371 __be16 port; 5372 5373 /* Initialize a local sockaddr structure to INADDR_ANY. */ 5374 af = sctp_sk(sk)->pf->af; 5375 5376 port = htons(inet_sk(sk)->num); 5377 af->inaddr_any(&autoaddr, port); 5378 5379 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); 5380} 5381 5382/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation. 5383 * 5384 * From RFC 2292 5385 * 4.2 The cmsghdr Structure * 5386 * 5387 * When ancillary data is sent or received, any number of ancillary data 5388 * objects can be specified by the msg_control and msg_controllen members of 5389 * the msghdr structure, because each object is preceded by 5390 * a cmsghdr structure defining the object's length (the cmsg_len member). 5391 * Historically Berkeley-derived implementations have passed only one object 5392 * at a time, but this API allows multiple objects to be 5393 * passed in a single call to sendmsg() or recvmsg(). The following example 5394 * shows two ancillary data objects in a control buffer. 5395 * 5396 * |<--------------------------- msg_controllen -------------------------->| 5397 * | | 5398 * 5399 * |<----- ancillary data object ----->|<----- ancillary data object ----->| 5400 * 5401 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| 5402 * | | | 5403 * 5404 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| | 5405 * 5406 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| | 5407 * | | | | | 5408 * 5409 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ 5410 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX| 5411 * 5412 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX| 5413 * 5414 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ 5415 * ^ 5416 * | 5417 * 5418 * msg_control 5419 * points here 5420 */ 5421SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg, 5422 sctp_cmsgs_t *cmsgs) 5423{ 5424 struct cmsghdr *cmsg; 5425 5426 for (cmsg = CMSG_FIRSTHDR(msg); 5427 cmsg != NULL; 5428 cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) { 5429 if (!CMSG_OK(msg, cmsg)) 5430 return -EINVAL; 5431 5432 /* Should we parse this header or ignore? */ 5433 if (cmsg->cmsg_level != IPPROTO_SCTP) 5434 continue; 5435 5436 /* Strictly check lengths following example in SCM code. */ 5437 switch (cmsg->cmsg_type) { 5438 case SCTP_INIT: 5439 /* SCTP Socket API Extension 5440 * 5.2.1 SCTP Initiation Structure (SCTP_INIT) 5441 * 5442 * This cmsghdr structure provides information for 5443 * initializing new SCTP associations with sendmsg(). 5444 * The SCTP_INITMSG socket option uses this same data 5445 * structure. This structure is not used for 5446 * recvmsg(). 5447 * 5448 * cmsg_level cmsg_type cmsg_data[] 5449 * ------------ ------------ ---------------------- 5450 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg 5451 */ 5452 if (cmsg->cmsg_len != 5453 CMSG_LEN(sizeof(struct sctp_initmsg))) 5454 return -EINVAL; 5455 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg); 5456 break; 5457 5458 case SCTP_SNDRCV: 5459 /* SCTP Socket API Extension 5460 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV) 5461 * 5462 * This cmsghdr structure specifies SCTP options for 5463 * sendmsg() and describes SCTP header information 5464 * about a received message through recvmsg(). 5465 * 5466 * cmsg_level cmsg_type cmsg_data[] 5467 * ------------ ------------ ---------------------- 5468 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo 5469 */ 5470 if (cmsg->cmsg_len != 5471 CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) 5472 return -EINVAL; 5473 5474 cmsgs->info = 5475 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); 5476 5477 /* Minimally, validate the sinfo_flags. */ 5478 if (cmsgs->info->sinfo_flags & 5479 ~(SCTP_UNORDERED | SCTP_ADDR_OVER | 5480 SCTP_ABORT | SCTP_EOF)) 5481 return -EINVAL; 5482 break; 5483 5484 default: 5485 return -EINVAL; 5486 } 5487 } 5488 return 0; 5489} 5490 5491/* 5492 * Wait for a packet.. 5493 * Note: This function is the same function as in core/datagram.c 5494 * with a few modifications to make lksctp work. 5495 */ 5496static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p) 5497{ 5498 int error; 5499 DEFINE_WAIT(wait); 5500 5501 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); 5502 5503 /* Socket errors? */ 5504 error = sock_error(sk); 5505 if (error) 5506 goto out; 5507 5508 if (!skb_queue_empty(&sk->sk_receive_queue)) 5509 goto ready; 5510 5511 /* Socket shut down? */ 5512 if (sk->sk_shutdown & RCV_SHUTDOWN) 5513 goto out; 5514 5515 /* Sequenced packets can come disconnected. If so we report the 5516 * problem. 5517 */ 5518 error = -ENOTCONN; 5519 5520 /* Is there a good reason to think that we may receive some data? */ 5521 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) 5522 goto out; 5523 5524 /* Handle signals. */ 5525 if (signal_pending(current)) 5526 goto interrupted; 5527 5528 /* Let another process have a go. Since we are going to sleep 5529 * anyway. Note: This may cause odd behaviors if the message 5530 * does not fit in the user's buffer, but this seems to be the 5531 * only way to honor MSG_DONTWAIT realistically. 5532 */ 5533 sctp_release_sock(sk); 5534 *timeo_p = schedule_timeout(*timeo_p); 5535 sctp_lock_sock(sk); 5536 5537ready: 5538 finish_wait(sk->sk_sleep, &wait); 5539 return 0; 5540 5541interrupted: 5542 error = sock_intr_errno(*timeo_p); 5543 5544out: 5545 finish_wait(sk->sk_sleep, &wait); 5546 *err = error; 5547 return error; 5548} 5549 5550/* Receive a datagram. 5551 * Note: This is pretty much the same routine as in core/datagram.c 5552 * with a few changes to make lksctp work. 5553 */ 5554static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, 5555 int noblock, int *err) 5556{ 5557 int error; 5558 struct sk_buff *skb; 5559 long timeo; 5560 5561 timeo = sock_rcvtimeo(sk, noblock); 5562 5563 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n", 5564 timeo, MAX_SCHEDULE_TIMEOUT); 5565 5566 do { 5567 /* Again only user level code calls this function, 5568 * so nothing interrupt level 5569 * will suddenly eat the receive_queue. 5570 * 5571 * Look at current nfs client by the way... 5572 * However, this function was corrent in any case. 8) 5573 */ 5574 if (flags & MSG_PEEK) { 5575 spin_lock_bh(&sk->sk_receive_queue.lock); 5576 skb = skb_peek(&sk->sk_receive_queue); 5577 if (skb) 5578 atomic_inc(&skb->users); 5579 spin_unlock_bh(&sk->sk_receive_queue.lock); 5580 } else { 5581 skb = skb_dequeue(&sk->sk_receive_queue); 5582 } 5583 5584 if (skb) 5585 return skb; 5586 5587 /* Caller is allowed not to check sk->sk_err before calling. */ 5588 error = sock_error(sk); 5589 if (error) 5590 goto no_packet; 5591 5592 if (sk->sk_shutdown & RCV_SHUTDOWN) 5593 break; 5594 5595 /* User doesn't want to wait. */ 5596 error = -EAGAIN; 5597 if (!timeo) 5598 goto no_packet; 5599 } while (sctp_wait_for_packet(sk, err, &timeo) == 0); 5600 5601 return NULL; 5602 5603no_packet: 5604 *err = error; 5605 return NULL; 5606} 5607 5608/* If sndbuf has changed, wake up per association sndbuf waiters. */ 5609static void __sctp_write_space(struct sctp_association *asoc) 5610{ 5611 struct sock *sk = asoc->base.sk; 5612 struct socket *sock = sk->sk_socket; 5613 5614 if ((sctp_wspace(asoc) > 0) && sock) { 5615 if (waitqueue_active(&asoc->wait)) 5616 wake_up_interruptible(&asoc->wait); 5617 5618 if (sctp_writeable(sk)) { 5619 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) 5620 wake_up_interruptible(sk->sk_sleep); 5621 5622 /* Note that we try to include the Async I/O support 5623 * here by modeling from the current TCP/UDP code. 5624 * We have not tested with it yet. 5625 */ 5626 if (sock->fasync_list && 5627 !(sk->sk_shutdown & SEND_SHUTDOWN)) 5628 sock_wake_async(sock, 2, POLL_OUT); 5629 } 5630 } 5631} 5632 5633/* Do accounting for the sndbuf space. 5634 * Decrement the used sndbuf space of the corresponding association by the 5635 * data size which was just transmitted(freed). 5636 */ 5637static void sctp_wfree(struct sk_buff *skb) 5638{ 5639 struct sctp_association *asoc; 5640 struct sctp_chunk *chunk; 5641 struct sock *sk; 5642 5643 /* Get the saved chunk pointer. */ 5644 chunk = *((struct sctp_chunk **)(skb->cb)); 5645 asoc = chunk->asoc; 5646 sk = asoc->base.sk; 5647 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) + 5648 sizeof(struct sk_buff) + 5649 sizeof(struct sctp_chunk); 5650 5651 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); 5652 5653 sock_wfree(skb); 5654 __sctp_write_space(asoc); 5655 5656 sctp_association_put(asoc); 5657} 5658 5659/* Do accounting for the receive space on the socket. 5660 * Accounting for the association is done in ulpevent.c 5661 * We set this as a destructor for the cloned data skbs so that 5662 * accounting is done at the correct time. 5663 */ 5664void sctp_sock_rfree(struct sk_buff *skb) 5665{ 5666 struct sock *sk = skb->sk; 5667 struct sctp_ulpevent *event = sctp_skb2event(skb); 5668 5669 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); 5670} 5671 5672 5673/* Helper function to wait for space in the sndbuf. */ 5674static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, 5675 size_t msg_len) 5676{ 5677 struct sock *sk = asoc->base.sk; 5678 int err = 0; 5679 long current_timeo = *timeo_p; 5680 DEFINE_WAIT(wait); 5681 5682 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n", 5683 asoc, (long)(*timeo_p), msg_len); 5684 5685 /* Increment the association's refcnt. */ 5686 sctp_association_hold(asoc); 5687 5688 /* Wait on the association specific sndbuf space. */ 5689 for (;;) { 5690 prepare_to_wait_exclusive(&asoc->wait, &wait, 5691 TASK_INTERRUPTIBLE); 5692 if (!*timeo_p) 5693 goto do_nonblock; 5694 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || 5695 asoc->base.dead) 5696 goto do_error; 5697 if (signal_pending(current)) 5698 goto do_interrupted; 5699 if (msg_len <= sctp_wspace(asoc)) 5700 break; 5701 5702 /* Let another process have a go. Since we are going 5703 * to sleep anyway. 5704 */ 5705 sctp_release_sock(sk); 5706 current_timeo = schedule_timeout(current_timeo); 5707 BUG_ON(sk != asoc->base.sk); 5708 sctp_lock_sock(sk); 5709 5710 *timeo_p = current_timeo; 5711 } 5712 5713out: 5714 finish_wait(&asoc->wait, &wait); 5715 5716 /* Release the association's refcnt. */ 5717 sctp_association_put(asoc); 5718 5719 return err; 5720 5721do_error: 5722 err = -EPIPE; 5723 goto out; 5724 5725do_interrupted: 5726 err = sock_intr_errno(*timeo_p); 5727 goto out; 5728 5729do_nonblock: 5730 err = -EAGAIN; 5731 goto out; 5732} 5733 5734/* If socket sndbuf has changed, wake up all per association waiters. */ 5735void sctp_write_space(struct sock *sk) 5736{ 5737 struct sctp_association *asoc; 5738 struct list_head *pos; 5739 5740 /* Wake up the tasks in each wait queue. */ 5741 list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) { 5742 asoc = list_entry(pos, struct sctp_association, asocs); 5743 __sctp_write_space(asoc); 5744 } 5745} 5746 5747/* Is there any sndbuf space available on the socket? 5748 * 5749 * Note that sk_wmem_alloc is the sum of the send buffers on all of the 5750 * associations on the same socket. For a UDP-style socket with 5751 * multiple associations, it is possible for it to be "unwriteable" 5752 * prematurely. I assume that this is acceptable because 5753 * a premature "unwriteable" is better than an accidental "writeable" which 5754 * would cause an unwanted block under certain circumstances. For the 1-1 5755 * UDP-style sockets or TCP-style sockets, this code should work. 5756 * - Daisy 5757 */ 5758static int sctp_writeable(struct sock *sk) 5759{ 5760 int amt = 0; 5761 5762 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc); 5763 if (amt < 0) 5764 amt = 0; 5765 return amt; 5766} 5767 5768/* Wait for an association to go into ESTABLISHED state. If timeout is 0, 5769 * returns immediately with EINPROGRESS. 5770 */ 5771static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) 5772{ 5773 struct sock *sk = asoc->base.sk; 5774 int err = 0; 5775 long current_timeo = *timeo_p; 5776 DEFINE_WAIT(wait); 5777 5778 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc, 5779 (long)(*timeo_p)); 5780 5781 /* Increment the association's refcnt. */ 5782 sctp_association_hold(asoc); 5783 5784 for (;;) { 5785 prepare_to_wait_exclusive(&asoc->wait, &wait, 5786 TASK_INTERRUPTIBLE); 5787 if (!*timeo_p) 5788 goto do_nonblock; 5789 if (sk->sk_shutdown & RCV_SHUTDOWN) 5790 break; 5791 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || 5792 asoc->base.dead) 5793 goto do_error; 5794 if (signal_pending(current)) 5795 goto do_interrupted; 5796 5797 if (sctp_state(asoc, ESTABLISHED)) 5798 break; 5799 5800 /* Let another process have a go. Since we are going 5801 * to sleep anyway. 5802 */ 5803 sctp_release_sock(sk); 5804 current_timeo = schedule_timeout(current_timeo); 5805 sctp_lock_sock(sk); 5806 5807 *timeo_p = current_timeo; 5808 } 5809 5810out: 5811 finish_wait(&asoc->wait, &wait); 5812 5813 /* Release the association's refcnt. */ 5814 sctp_association_put(asoc); 5815 5816 return err; 5817 5818do_error: 5819 if (asoc->init_err_counter + 1 > asoc->max_init_attempts) 5820 err = -ETIMEDOUT; 5821 else 5822 err = -ECONNREFUSED; 5823 goto out; 5824 5825do_interrupted: 5826 err = sock_intr_errno(*timeo_p); 5827 goto out; 5828 5829do_nonblock: 5830 err = -EINPROGRESS; 5831 goto out; 5832} 5833 5834static int sctp_wait_for_accept(struct sock *sk, long timeo) 5835{ 5836 struct sctp_endpoint *ep; 5837 int err = 0; 5838 DEFINE_WAIT(wait); 5839 5840 ep = sctp_sk(sk)->ep; 5841 5842 5843 for (;;) { 5844 prepare_to_wait_exclusive(sk->sk_sleep, &wait, 5845 TASK_INTERRUPTIBLE); 5846 5847 if (list_empty(&ep->asocs)) { 5848 sctp_release_sock(sk); 5849 timeo = schedule_timeout(timeo); 5850 sctp_lock_sock(sk); 5851 } 5852 5853 err = -EINVAL; 5854 if (!sctp_sstate(sk, LISTENING)) 5855 break; 5856 5857 err = 0; 5858 if (!list_empty(&ep->asocs)) 5859 break; 5860 5861 err = sock_intr_errno(timeo); 5862 if (signal_pending(current)) 5863 break; 5864 5865 err = -EAGAIN; 5866 if (!timeo) 5867 break; 5868 } 5869 5870 finish_wait(sk->sk_sleep, &wait); 5871 5872 return err; 5873} 5874 5875void sctp_wait_for_close(struct sock *sk, long timeout) 5876{ 5877 DEFINE_WAIT(wait); 5878 5879 do { 5880 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); 5881 if (list_empty(&sctp_sk(sk)->ep->asocs)) 5882 break; 5883 sctp_release_sock(sk); 5884 timeout = schedule_timeout(timeout); 5885 sctp_lock_sock(sk); 5886 } while (!signal_pending(current) && timeout); 5887 5888 finish_wait(sk->sk_sleep, &wait); 5889} 5890 5891static void sctp_sock_rfree_frag(struct sk_buff *skb) 5892{ 5893 struct sk_buff *frag; 5894 5895 if (!skb->data_len) 5896 goto done; 5897 5898 /* Don't forget the fragments. */ 5899 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) 5900 sctp_sock_rfree_frag(frag); 5901 5902done: 5903 sctp_sock_rfree(skb); 5904} 5905 5906static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) 5907{ 5908 struct sk_buff *frag; 5909 5910 if (!skb->data_len) 5911 goto done; 5912 5913 /* Don't forget the fragments. */ 5914 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) 5915 sctp_skb_set_owner_r_frag(frag, sk); 5916 5917done: 5918 sctp_skb_set_owner_r(skb, sk); 5919} 5920 5921/* Populate the fields of the newsk from the oldsk and migrate the assoc 5922 * and its messages to the newsk. 5923 */ 5924static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, 5925 struct sctp_association *assoc, 5926 sctp_socket_type_t type) 5927{ 5928 struct sctp_sock *oldsp = sctp_sk(oldsk); 5929 struct sctp_sock *newsp = sctp_sk(newsk); 5930 struct sctp_bind_bucket *pp; /* hash list port iterator */ 5931 struct sctp_endpoint *newep = newsp->ep; 5932 struct sk_buff *skb, *tmp; 5933 struct sctp_ulpevent *event; 5934 int flags = 0; 5935 5936 /* Migrate socket buffer sizes and all the socket level options to the 5937 * new socket. 5938 */ 5939 newsk->sk_sndbuf = oldsk->sk_sndbuf; 5940 newsk->sk_rcvbuf = oldsk->sk_rcvbuf; 5941 /* Brute force copy old sctp opt. */ 5942 inet_sk_copy_descendant(newsk, oldsk); 5943 5944 /* Restore the ep value that was overwritten with the above structure 5945 * copy. 5946 */ 5947 newsp->ep = newep; 5948 newsp->hmac = NULL; 5949 5950 /* Hook this new socket in to the bind_hash list. */ 5951 pp = sctp_sk(oldsk)->bind_hash; 5952 sk_add_bind_node(newsk, &pp->owner); 5953 sctp_sk(newsk)->bind_hash = pp; 5954 inet_sk(newsk)->num = inet_sk(oldsk)->num; 5955 5956 /* Copy the bind_addr list from the original endpoint to the new 5957 * endpoint so that we can handle restarts properly 5958 */ 5959 if (PF_INET6 == assoc->base.sk->sk_family) 5960 flags = SCTP_ADDR6_ALLOWED; 5961 if (assoc->peer.ipv4_address) 5962 flags |= SCTP_ADDR4_PEERSUPP; 5963 if (assoc->peer.ipv6_address) 5964 flags |= SCTP_ADDR6_PEERSUPP; 5965 sctp_bind_addr_copy(&newsp->ep->base.bind_addr, 5966 &oldsp->ep->base.bind_addr, 5967 SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags); 5968 5969 /* Move any messages in the old socket's receive queue that are for the 5970 * peeled off association to the new socket's receive queue. 5971 */ 5972 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { 5973 event = sctp_skb2event(skb); 5974 if (event->asoc == assoc) { 5975 sctp_sock_rfree_frag(skb); 5976 __skb_unlink(skb, &oldsk->sk_receive_queue); 5977 __skb_queue_tail(&newsk->sk_receive_queue, skb); 5978 sctp_skb_set_owner_r_frag(skb, newsk); 5979 } 5980 } 5981 5982 /* Clean up any messages pending delivery due to partial 5983 * delivery. Three cases: 5984 * 1) No partial deliver; no work. 5985 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. 5986 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. 5987 */ 5988 skb_queue_head_init(&newsp->pd_lobby); 5989 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); 5990 5991 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { 5992 struct sk_buff_head *queue; 5993 5994 /* Decide which queue to move pd_lobby skbs to. */ 5995 if (assoc->ulpq.pd_mode) { 5996 queue = &newsp->pd_lobby; 5997 } else 5998 queue = &newsk->sk_receive_queue; 5999 6000 /* Walk through the pd_lobby, looking for skbs that 6001 * need moved to the new socket. 6002 */ 6003 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { 6004 event = sctp_skb2event(skb); 6005 if (event->asoc == assoc) { 6006 sctp_sock_rfree_frag(skb); 6007 __skb_unlink(skb, &oldsp->pd_lobby); 6008 __skb_queue_tail(queue, skb); 6009 sctp_skb_set_owner_r_frag(skb, newsk); 6010 } 6011 } 6012 6013 /* Clear up any skbs waiting for the partial 6014 * delivery to finish. 6015 */ 6016 if (assoc->ulpq.pd_mode) 6017 sctp_clear_pd(oldsk, NULL); 6018 6019 } 6020 6021 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) { 6022 sctp_sock_rfree_frag(skb); 6023 sctp_skb_set_owner_r_frag(skb, newsk); 6024 } 6025 6026 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) { 6027 sctp_sock_rfree_frag(skb); 6028 sctp_skb_set_owner_r_frag(skb, newsk); 6029 } 6030 6031 /* Set the type of socket to indicate that it is peeled off from the 6032 * original UDP-style socket or created with the accept() call on a 6033 * TCP-style socket.. 6034 */ 6035 newsp->type = type; 6036 6037 /* Mark the new socket "in-use" by the user so that any packets 6038 * that may arrive on the association after we've moved it are 6039 * queued to the backlog. This prevents a potential race between 6040 * backlog processing on the old socket and new-packet processing 6041 * on the new socket. 6042 * 6043 * The caller has just allocated newsk so we can guarantee that other 6044 * paths won't try to lock it and then oldsk. 6045 */ 6046 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); 6047 sctp_assoc_migrate(assoc, newsk); 6048 6049 /* If the association on the newsk is already closed before accept() 6050 * is called, set RCV_SHUTDOWN flag. 6051 */ 6052 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) 6053 newsk->sk_shutdown |= RCV_SHUTDOWN; 6054 6055 newsk->sk_state = SCTP_SS_ESTABLISHED; 6056 sctp_release_sock(newsk); 6057} 6058 6059/* This proto struct describes the ULP interface for SCTP. */ 6060struct proto sctp_prot = { 6061 .name = "SCTP", 6062 .owner = THIS_MODULE, 6063 .close = sctp_close, 6064 .connect = sctp_connect, 6065 .disconnect = sctp_disconnect, 6066 .accept = sctp_accept, 6067 .ioctl = sctp_ioctl, 6068 .init = sctp_init_sock, 6069 .destroy = sctp_destroy_sock, 6070 .shutdown = sctp_shutdown, 6071 .setsockopt = sctp_setsockopt, 6072 .getsockopt = sctp_getsockopt, 6073 .sendmsg = sctp_sendmsg, 6074 .recvmsg = sctp_recvmsg, 6075 .bind = sctp_bind, 6076 .backlog_rcv = sctp_backlog_rcv, 6077 .hash = sctp_hash, 6078 .unhash = sctp_unhash, 6079 .get_port = sctp_get_port, 6080 .obj_size = sizeof(struct sctp_sock), 6081}; 6082 6083#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 6084struct proto sctpv6_prot = { 6085 .name = "SCTPv6", 6086 .owner = THIS_MODULE, 6087 .close = sctp_close, 6088 .connect = sctp_connect, 6089 .disconnect = sctp_disconnect, 6090 .accept = sctp_accept, 6091 .ioctl = sctp_ioctl, 6092 .init = sctp_init_sock, 6093 .destroy = sctp_destroy_sock, 6094 .shutdown = sctp_shutdown, 6095 .setsockopt = sctp_setsockopt, 6096 .getsockopt = sctp_getsockopt, 6097 .sendmsg = sctp_sendmsg, 6098 .recvmsg = sctp_recvmsg, 6099 .bind = sctp_bind, 6100 .backlog_rcv = sctp_backlog_rcv, 6101 .hash = sctp_hash, 6102 .unhash = sctp_unhash, 6103 .get_port = sctp_get_port, 6104 .obj_size = sizeof(struct sctp6_sock), 6105}; 6106#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */ 6107