~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/sctp/socket.c

Version: ~ [ linux-5.8-rc4 ] ~ [ linux-5.7.7 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.50 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.131 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.187 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.229 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.229 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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