root/net/sctp/socket.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sctp_enter_memory_pressure
  2. sctp_wspace
  3. sctp_set_owner_w
  4. sctp_clear_owner_w
  5. sctp_for_each_tx_datachunk
  6. sctp_for_each_rx_skb
  7. sctp_verify_addr
  8. sctp_id2assoc
  9. sctp_addr_id2transport
  10. sctp_bind
  11. sctp_sockaddr_af
  12. sctp_do_bind
  13. sctp_send_asconf
  14. sctp_bindx_add
  15. sctp_send_asconf_add_ip
  16. sctp_bindx_rem
  17. sctp_send_asconf_del_ip
  18. sctp_asconf_mgmt
  19. sctp_setsockopt_bindx
  20. sctp_connect_new_asoc
  21. sctp_connect_add_peer
  22. __sctp_connect
  23. __sctp_setsockopt_connectx
  24. sctp_setsockopt_connectx_old
  25. sctp_setsockopt_connectx
  26. sctp_getsockopt_connectx3
  27. sctp_close
  28. sctp_error
  29. sctp_sendmsg_parse
  30. sctp_sendmsg_new_asoc
  31. sctp_sendmsg_check_sflags
  32. sctp_sendmsg_to_asoc
  33. sctp_sendmsg_get_daddr
  34. sctp_sendmsg_update_sinfo
  35. sctp_sendmsg
  36. sctp_skb_pull
  37. sctp_recvmsg
  38. sctp_setsockopt_disable_fragments
  39. sctp_setsockopt_events
  40. sctp_setsockopt_autoclose
  41. sctp_apply_peer_addr_params
  42. sctp_setsockopt_peer_addr_params
  43. sctp_spp_sackdelay_enable
  44. sctp_spp_sackdelay_disable
  45. sctp_apply_asoc_delayed_ack
  46. sctp_setsockopt_delayed_ack
  47. sctp_setsockopt_initmsg
  48. sctp_setsockopt_default_send_param
  49. sctp_setsockopt_default_sndinfo
  50. sctp_setsockopt_primary_addr
  51. sctp_setsockopt_nodelay
  52. sctp_setsockopt_rtoinfo
  53. sctp_setsockopt_associnfo
  54. sctp_setsockopt_mappedv4
  55. sctp_setsockopt_maxseg
  56. sctp_setsockopt_peer_primary_addr
  57. sctp_setsockopt_adaptation_layer
  58. sctp_setsockopt_context
  59. sctp_setsockopt_fragment_interleave
  60. sctp_setsockopt_partial_delivery_point
  61. sctp_setsockopt_maxburst
  62. sctp_setsockopt_auth_chunk
  63. sctp_setsockopt_hmac_ident
  64. sctp_setsockopt_auth_key
  65. sctp_setsockopt_active_key
  66. sctp_setsockopt_del_key
  67. sctp_setsockopt_deactivate_key
  68. sctp_setsockopt_auto_asconf
  69. sctp_setsockopt_paddr_thresholds
  70. sctp_setsockopt_recvrcvinfo
  71. sctp_setsockopt_recvnxtinfo
  72. sctp_setsockopt_pr_supported
  73. sctp_setsockopt_default_prinfo
  74. sctp_setsockopt_reconfig_supported
  75. sctp_setsockopt_enable_strreset
  76. sctp_setsockopt_reset_streams
  77. sctp_setsockopt_reset_assoc
  78. sctp_setsockopt_add_streams
  79. sctp_setsockopt_scheduler
  80. sctp_setsockopt_scheduler_value
  81. sctp_setsockopt_interleaving_supported
  82. sctp_setsockopt_reuse_port
  83. sctp_assoc_ulpevent_type_set
  84. sctp_setsockopt_event
  85. sctp_setsockopt_asconf_supported
  86. sctp_setsockopt_auth_supported
  87. sctp_setsockopt_ecn_supported
  88. sctp_setsockopt
  89. sctp_connect
  90. sctp_inet_connect
  91. sctp_disconnect
  92. sctp_accept
  93. sctp_ioctl
  94. sctp_init_sock
  95. sctp_destroy_sock
  96. sctp_destruct_sock
  97. sctp_shutdown
  98. sctp_get_sctp_info
  99. sctp_transport_walk_start
  100. sctp_transport_walk_stop
  101. sctp_transport_get_next
  102. sctp_transport_get_idx
  103. sctp_for_each_endpoint
  104. sctp_transport_lookup_process
  105. sctp_for_each_transport
  106. sctp_getsockopt_sctp_status
  107. sctp_getsockopt_peer_addr_info
  108. sctp_getsockopt_disable_fragments
  109. sctp_getsockopt_events
  110. sctp_getsockopt_autoclose
  111. sctp_do_peeloff
  112. sctp_getsockopt_peeloff_common
  113. sctp_getsockopt_peeloff
  114. sctp_getsockopt_peeloff_flags
  115. sctp_getsockopt_peer_addr_params
  116. sctp_getsockopt_delayed_ack
  117. sctp_getsockopt_initmsg
  118. sctp_getsockopt_peer_addrs
  119. sctp_copy_laddrs
  120. sctp_getsockopt_local_addrs
  121. sctp_getsockopt_primary_addr
  122. sctp_getsockopt_adaptation_layer
  123. sctp_getsockopt_default_send_param
  124. sctp_getsockopt_default_sndinfo
  125. sctp_getsockopt_nodelay
  126. sctp_getsockopt_rtoinfo
  127. sctp_getsockopt_associnfo
  128. sctp_getsockopt_mappedv4
  129. sctp_getsockopt_context
  130. sctp_getsockopt_maxseg
  131. sctp_getsockopt_fragment_interleave
  132. sctp_getsockopt_partial_delivery_point
  133. sctp_getsockopt_maxburst
  134. sctp_getsockopt_hmac_ident
  135. sctp_getsockopt_active_key
  136. sctp_getsockopt_peer_auth_chunks
  137. sctp_getsockopt_local_auth_chunks
  138. sctp_getsockopt_assoc_number
  139. sctp_getsockopt_auto_asconf
  140. sctp_getsockopt_assoc_ids
  141. sctp_getsockopt_paddr_thresholds
  142. sctp_getsockopt_assoc_stats
  143. sctp_getsockopt_recvrcvinfo
  144. sctp_getsockopt_recvnxtinfo
  145. sctp_getsockopt_pr_supported
  146. sctp_getsockopt_default_prinfo
  147. sctp_getsockopt_pr_assocstatus
  148. sctp_getsockopt_pr_streamstatus
  149. sctp_getsockopt_reconfig_supported
  150. sctp_getsockopt_enable_strreset
  151. sctp_getsockopt_scheduler
  152. sctp_getsockopt_scheduler_value
  153. sctp_getsockopt_interleaving_supported
  154. sctp_getsockopt_reuse_port
  155. sctp_getsockopt_event
  156. sctp_getsockopt_asconf_supported
  157. sctp_getsockopt_auth_supported
  158. sctp_getsockopt_ecn_supported
  159. sctp_getsockopt
  160. sctp_hash
  161. sctp_unhash
  162. sctp_get_port_local
  163. sctp_get_port
  164. sctp_listen_start
  165. sctp_inet_listen
  166. sctp_poll
  167. sctp_bucket_create
  168. sctp_bucket_destroy
  169. __sctp_put_port
  170. sctp_put_port
  171. sctp_autobind
  172. sctp_msghdr_parse
  173. sctp_wait_for_packet
  174. sctp_skb_recv_datagram
  175. __sctp_write_space
  176. sctp_wake_up_waiters
  177. sctp_wfree
  178. sctp_sock_rfree
  179. sctp_wait_for_sndbuf
  180. sctp_data_ready
  181. sctp_write_space
  182. sctp_writeable
  183. sctp_wait_for_connect
  184. sctp_wait_for_accept
  185. sctp_wait_for_close
  186. sctp_skb_set_owner_r_frag
  187. sctp_copy_sock
  188. sctp_copy_descendant
  189. sctp_sock_migrate
  190. sctp_v6_destroy_sock

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

/* [<][>][^][v][top][bottom][index][help] */