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