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