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