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