]> git.codelabs.ch Git - muen/linux.git/blob - net/sctp/socket.c
Merge branch 'work.namei' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[muen/linux.git] / net / sctp / socket.c
1 /* SCTP kernel implementation
2  * (C) Copyright IBM Corp. 2001, 2004
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001-2003 Intel Corp.
6  * Copyright (c) 2001-2002 Nokia, Inc.
7  * Copyright (c) 2001 La Monte H.P. Yarroll
8  *
9  * This file is part of the SCTP kernel implementation
10  *
11  * These functions interface with the sockets layer to implement the
12  * SCTP Extensions for the Sockets API.
13  *
14  * Note that the descriptions from the specification are USER level
15  * functions--this file is the functions which populate the struct proto
16  * for SCTP which is the BOTTOM of the sockets interface.
17  *
18  * This SCTP implementation is free software;
19  * you can redistribute it and/or modify it under the terms of
20  * the GNU General Public License as published by
21  * the Free Software Foundation; either version 2, or (at your option)
22  * any later version.
23  *
24  * This SCTP implementation is distributed in the hope that it
25  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26  *                 ************************
27  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28  * See the GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with GNU CC; see the file COPYING.  If not, see
32  * <http://www.gnu.org/licenses/>.
33  *
34  * Please send any bug reports or fixes you make to the
35  * email address(es):
36  *    lksctp developers <linux-sctp@vger.kernel.org>
37  *
38  * Written or modified by:
39  *    La Monte H.P. Yarroll <piggy@acm.org>
40  *    Narasimha Budihal     <narsi@refcode.org>
41  *    Karl Knutson          <karl@athena.chicago.il.us>
42  *    Jon Grimm             <jgrimm@us.ibm.com>
43  *    Xingang Guo           <xingang.guo@intel.com>
44  *    Daisy Chang           <daisyc@us.ibm.com>
45  *    Sridhar Samudrala     <samudrala@us.ibm.com>
46  *    Inaky Perez-Gonzalez  <inaky.gonzalez@intel.com>
47  *    Ardelle Fan           <ardelle.fan@intel.com>
48  *    Ryan Layer            <rmlayer@us.ibm.com>
49  *    Anup Pemmaiah         <pemmaiah@cc.usu.edu>
50  *    Kevin Gao             <kevin.gao@intel.com>
51  */
52
53 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
55 #include <crypto/hash.h>
56 #include <linux/types.h>
57 #include <linux/kernel.h>
58 #include <linux/wait.h>
59 #include <linux/time.h>
60 #include <linux/sched/signal.h>
61 #include <linux/ip.h>
62 #include <linux/capability.h>
63 #include <linux/fcntl.h>
64 #include <linux/poll.h>
65 #include <linux/init.h>
66 #include <linux/slab.h>
67 #include <linux/file.h>
68 #include <linux/compat.h>
69
70 #include <net/ip.h>
71 #include <net/icmp.h>
72 #include <net/route.h>
73 #include <net/ipv6.h>
74 #include <net/inet_common.h>
75 #include <net/busy_poll.h>
76
77 #include <linux/socket.h> /* for sa_family_t */
78 #include <linux/export.h>
79 #include <net/sock.h>
80 #include <net/sctp/sctp.h>
81 #include <net/sctp/sm.h>
82 #include <net/sctp/stream_sched.h>
83
84 /* Forward declarations for internal helper functions. */
85 static int sctp_writeable(struct sock *sk);
86 static void sctp_wfree(struct sk_buff *skb);
87 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
88                                 size_t msg_len);
89 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
90 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
91 static int sctp_wait_for_accept(struct sock *sk, long timeo);
92 static void sctp_wait_for_close(struct sock *sk, long timeo);
93 static void sctp_destruct_sock(struct sock *sk);
94 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
95                                         union sctp_addr *addr, int len);
96 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
97 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
98 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
99 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf(struct sctp_association *asoc,
101                             struct sctp_chunk *chunk);
102 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
103 static int sctp_autobind(struct sock *sk);
104 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
105                               struct sctp_association *assoc,
106                               enum sctp_socket_type type);
107
108 static unsigned long sctp_memory_pressure;
109 static atomic_long_t sctp_memory_allocated;
110 struct percpu_counter sctp_sockets_allocated;
111
112 static void sctp_enter_memory_pressure(struct sock *sk)
113 {
114         sctp_memory_pressure = 1;
115 }
116
117
118 /* Get the sndbuf space available at the time on the association.  */
119 static inline int sctp_wspace(struct sctp_association *asoc)
120 {
121         int amt;
122
123         if (asoc->ep->sndbuf_policy)
124                 amt = asoc->sndbuf_used;
125         else
126                 amt = sk_wmem_alloc_get(asoc->base.sk);
127
128         if (amt >= asoc->base.sk->sk_sndbuf) {
129                 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
130                         amt = 0;
131                 else {
132                         amt = sk_stream_wspace(asoc->base.sk);
133                         if (amt < 0)
134                                 amt = 0;
135                 }
136         } else {
137                 amt = asoc->base.sk->sk_sndbuf - amt;
138         }
139         return amt;
140 }
141
142 /* Increment the used sndbuf space count of the corresponding association by
143  * the size of the outgoing data chunk.
144  * Also, set the skb destructor for sndbuf accounting later.
145  *
146  * Since it is always 1-1 between chunk and skb, and also a new skb is always
147  * allocated for chunk bundling in sctp_packet_transmit(), we can use the
148  * destructor in the data chunk skb for the purpose of the sndbuf space
149  * tracking.
150  */
151 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
152 {
153         struct sctp_association *asoc = chunk->asoc;
154         struct sock *sk = asoc->base.sk;
155
156         /* The sndbuf space is tracked per association.  */
157         sctp_association_hold(asoc);
158
159         if (chunk->shkey)
160                 sctp_auth_shkey_hold(chunk->shkey);
161
162         skb_set_owner_w(chunk->skb, sk);
163
164         chunk->skb->destructor = sctp_wfree;
165         /* Save the chunk pointer in skb for sctp_wfree to use later.  */
166         skb_shinfo(chunk->skb)->destructor_arg = chunk;
167
168         asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
169                                 sizeof(struct sk_buff) +
170                                 sizeof(struct sctp_chunk);
171
172         refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
173         sk->sk_wmem_queued += chunk->skb->truesize;
174         sk_mem_charge(sk, chunk->skb->truesize);
175 }
176
177 static void sctp_clear_owner_w(struct sctp_chunk *chunk)
178 {
179         skb_orphan(chunk->skb);
180 }
181
182 static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
183                                        void (*cb)(struct sctp_chunk *))
184
185 {
186         struct sctp_outq *q = &asoc->outqueue;
187         struct sctp_transport *t;
188         struct sctp_chunk *chunk;
189
190         list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
191                 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
192                         cb(chunk);
193
194         list_for_each_entry(chunk, &q->retransmit, transmitted_list)
195                 cb(chunk);
196
197         list_for_each_entry(chunk, &q->sacked, transmitted_list)
198                 cb(chunk);
199
200         list_for_each_entry(chunk, &q->abandoned, transmitted_list)
201                 cb(chunk);
202
203         list_for_each_entry(chunk, &q->out_chunk_list, list)
204                 cb(chunk);
205 }
206
207 static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
208                                  void (*cb)(struct sk_buff *, struct sock *))
209
210 {
211         struct sk_buff *skb, *tmp;
212
213         sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
214                 cb(skb, sk);
215
216         sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
217                 cb(skb, sk);
218
219         sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
220                 cb(skb, sk);
221 }
222
223 /* Verify that this is a valid address. */
224 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
225                                    int len)
226 {
227         struct sctp_af *af;
228
229         /* Verify basic sockaddr. */
230         af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
231         if (!af)
232                 return -EINVAL;
233
234         /* Is this a valid SCTP address?  */
235         if (!af->addr_valid(addr, sctp_sk(sk), NULL))
236                 return -EINVAL;
237
238         if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
239                 return -EINVAL;
240
241         return 0;
242 }
243
244 /* Look up the association by its id.  If this is not a UDP-style
245  * socket, the ID field is always ignored.
246  */
247 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
248 {
249         struct sctp_association *asoc = NULL;
250
251         /* If this is not a UDP-style socket, assoc id should be ignored. */
252         if (!sctp_style(sk, UDP)) {
253                 /* Return NULL if the socket state is not ESTABLISHED. It
254                  * could be a TCP-style listening socket or a socket which
255                  * hasn't yet called connect() to establish an association.
256                  */
257                 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
258                         return NULL;
259
260                 /* Get the first and the only association from the list. */
261                 if (!list_empty(&sctp_sk(sk)->ep->asocs))
262                         asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
263                                           struct sctp_association, asocs);
264                 return asoc;
265         }
266
267         /* Otherwise this is a UDP-style socket. */
268         if (!id || (id == (sctp_assoc_t)-1))
269                 return NULL;
270
271         spin_lock_bh(&sctp_assocs_id_lock);
272         asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
273         spin_unlock_bh(&sctp_assocs_id_lock);
274
275         if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
276                 return NULL;
277
278         return asoc;
279 }
280
281 /* Look up the transport from an address and an assoc id. If both address and
282  * id are specified, the associations matching the address and the id should be
283  * the same.
284  */
285 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
286                                               struct sockaddr_storage *addr,
287                                               sctp_assoc_t id)
288 {
289         struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
290         struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
291         union sctp_addr *laddr = (union sctp_addr *)addr;
292         struct sctp_transport *transport;
293
294         if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
295                 return NULL;
296
297         addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
298                                                laddr,
299                                                &transport);
300
301         if (!addr_asoc)
302                 return NULL;
303
304         id_asoc = sctp_id2assoc(sk, id);
305         if (id_asoc && (id_asoc != addr_asoc))
306                 return NULL;
307
308         sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
309                                                 (union sctp_addr *)addr);
310
311         return transport;
312 }
313
314 /* API 3.1.2 bind() - UDP Style Syntax
315  * The syntax of bind() is,
316  *
317  *   ret = bind(int sd, struct sockaddr *addr, int addrlen);
318  *
319  *   sd      - the socket descriptor returned by socket().
320  *   addr    - the address structure (struct sockaddr_in or struct
321  *             sockaddr_in6 [RFC 2553]),
322  *   addr_len - the size of the address structure.
323  */
324 static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
325 {
326         int retval = 0;
327
328         lock_sock(sk);
329
330         pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
331                  addr, addr_len);
332
333         /* Disallow binding twice. */
334         if (!sctp_sk(sk)->ep->base.bind_addr.port)
335                 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
336                                       addr_len);
337         else
338                 retval = -EINVAL;
339
340         release_sock(sk);
341
342         return retval;
343 }
344
345 static long sctp_get_port_local(struct sock *, union sctp_addr *);
346
347 /* Verify this is a valid sockaddr. */
348 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
349                                         union sctp_addr *addr, int len)
350 {
351         struct sctp_af *af;
352
353         /* Check minimum size.  */
354         if (len < sizeof (struct sockaddr))
355                 return NULL;
356
357         if (!opt->pf->af_supported(addr->sa.sa_family, opt))
358                 return NULL;
359
360         /* V4 mapped address are really of AF_INET family */
361         if (addr->sa.sa_family == AF_INET6 &&
362             ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
363             !opt->pf->af_supported(AF_INET, opt))
364                 return NULL;
365
366         /* If we get this far, af is valid. */
367         af = sctp_get_af_specific(addr->sa.sa_family);
368
369         if (len < af->sockaddr_len)
370                 return NULL;
371
372         return af;
373 }
374
375 /* Bind a local address either to an endpoint or to an association.  */
376 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
377 {
378         struct net *net = sock_net(sk);
379         struct sctp_sock *sp = sctp_sk(sk);
380         struct sctp_endpoint *ep = sp->ep;
381         struct sctp_bind_addr *bp = &ep->base.bind_addr;
382         struct sctp_af *af;
383         unsigned short snum;
384         int ret = 0;
385
386         /* Common sockaddr verification. */
387         af = sctp_sockaddr_af(sp, addr, len);
388         if (!af) {
389                 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
390                          __func__, sk, addr, len);
391                 return -EINVAL;
392         }
393
394         snum = ntohs(addr->v4.sin_port);
395
396         pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
397                  __func__, sk, &addr->sa, bp->port, snum, len);
398
399         /* PF specific bind() address verification. */
400         if (!sp->pf->bind_verify(sp, addr))
401                 return -EADDRNOTAVAIL;
402
403         /* We must either be unbound, or bind to the same port.
404          * It's OK to allow 0 ports if we are already bound.
405          * We'll just inhert an already bound port in this case
406          */
407         if (bp->port) {
408                 if (!snum)
409                         snum = bp->port;
410                 else if (snum != bp->port) {
411                         pr_debug("%s: new port %d doesn't match existing port "
412                                  "%d\n", __func__, snum, bp->port);
413                         return -EINVAL;
414                 }
415         }
416
417         if (snum && snum < inet_prot_sock(net) &&
418             !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
419                 return -EACCES;
420
421         /* See if the address matches any of the addresses we may have
422          * already bound before checking against other endpoints.
423          */
424         if (sctp_bind_addr_match(bp, addr, sp))
425                 return -EINVAL;
426
427         /* Make sure we are allowed to bind here.
428          * The function sctp_get_port_local() does duplicate address
429          * detection.
430          */
431         addr->v4.sin_port = htons(snum);
432         if ((ret = sctp_get_port_local(sk, addr))) {
433                 return -EADDRINUSE;
434         }
435
436         /* Refresh ephemeral port.  */
437         if (!bp->port)
438                 bp->port = inet_sk(sk)->inet_num;
439
440         /* Add the address to the bind address list.
441          * Use GFP_ATOMIC since BHs will be disabled.
442          */
443         ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
444                                  SCTP_ADDR_SRC, GFP_ATOMIC);
445
446         /* Copy back into socket for getsockname() use. */
447         if (!ret) {
448                 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
449                 sp->pf->to_sk_saddr(addr, sk);
450         }
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 net *net = sock_net(sk);
558         struct sctp_sock                *sp;
559         struct sctp_endpoint            *ep;
560         struct sctp_association         *asoc;
561         struct sctp_bind_addr           *bp;
562         struct sctp_chunk               *chunk;
563         struct sctp_sockaddr_entry      *laddr;
564         union sctp_addr                 *addr;
565         union sctp_addr                 saveaddr;
566         void                            *addr_buf;
567         struct sctp_af                  *af;
568         struct list_head                *p;
569         int                             i;
570         int                             retval = 0;
571
572         if (!net->sctp.addip_enable)
573                 return retval;
574
575         sp = sctp_sk(sk);
576         ep = sp->ep;
577
578         pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
579                  __func__, sk, addrs, addrcnt);
580
581         list_for_each_entry(asoc, &ep->asocs, asocs) {
582                 if (!asoc->peer.asconf_capable)
583                         continue;
584
585                 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
586                         continue;
587
588                 if (!sctp_state(asoc, ESTABLISHED))
589                         continue;
590
591                 /* Check if any address in the packed array of addresses is
592                  * in the bind address list of the association. If so,
593                  * do not send the asconf chunk to its peer, but continue with
594                  * other associations.
595                  */
596                 addr_buf = addrs;
597                 for (i = 0; i < addrcnt; i++) {
598                         addr = addr_buf;
599                         af = sctp_get_af_specific(addr->v4.sin_family);
600                         if (!af) {
601                                 retval = -EINVAL;
602                                 goto out;
603                         }
604
605                         if (sctp_assoc_lookup_laddr(asoc, addr))
606                                 break;
607
608                         addr_buf += af->sockaddr_len;
609                 }
610                 if (i < addrcnt)
611                         continue;
612
613                 /* Use the first valid address in bind addr list of
614                  * association as Address Parameter of ASCONF CHUNK.
615                  */
616                 bp = &asoc->base.bind_addr;
617                 p = bp->address_list.next;
618                 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
619                 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
620                                                    addrcnt, SCTP_PARAM_ADD_IP);
621                 if (!chunk) {
622                         retval = -ENOMEM;
623                         goto out;
624                 }
625
626                 /* Add the new addresses to the bind address list with
627                  * use_as_src set to 0.
628                  */
629                 addr_buf = addrs;
630                 for (i = 0; i < addrcnt; i++) {
631                         addr = addr_buf;
632                         af = sctp_get_af_specific(addr->v4.sin_family);
633                         memcpy(&saveaddr, addr, af->sockaddr_len);
634                         retval = sctp_add_bind_addr(bp, &saveaddr,
635                                                     sizeof(saveaddr),
636                                                     SCTP_ADDR_NEW, GFP_ATOMIC);
637                         addr_buf += af->sockaddr_len;
638                 }
639                 if (asoc->src_out_of_asoc_ok) {
640                         struct sctp_transport *trans;
641
642                         list_for_each_entry(trans,
643                             &asoc->peer.transport_addr_list, transports) {
644                                 /* Clear the source and route cache */
645                                 sctp_transport_dst_release(trans);
646                                 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
647                                     2*asoc->pathmtu, 4380));
648                                 trans->ssthresh = asoc->peer.i.a_rwnd;
649                                 trans->rto = asoc->rto_initial;
650                                 sctp_max_rto(asoc, trans);
651                                 trans->rtt = trans->srtt = trans->rttvar = 0;
652                                 sctp_transport_route(trans, NULL,
653                                     sctp_sk(asoc->base.sk));
654                         }
655                 }
656                 retval = sctp_send_asconf(asoc, chunk);
657         }
658
659 out:
660         return retval;
661 }
662
663 /* Remove a list of addresses from bind addresses list.  Do not remove the
664  * last address.
665  *
666  * Basically run through each address specified in the addrs/addrcnt
667  * array/length pair, determine if it is IPv6 or IPv4 and call
668  * sctp_del_bind() on it.
669  *
670  * If any of them fails, then the operation will be reversed and the
671  * ones that were removed will be added back.
672  *
673  * At least one address has to be left; if only one address is
674  * available, the operation will return -EBUSY.
675  *
676  * Only sctp_setsockopt_bindx() is supposed to call this function.
677  */
678 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
679 {
680         struct sctp_sock *sp = sctp_sk(sk);
681         struct sctp_endpoint *ep = sp->ep;
682         int cnt;
683         struct sctp_bind_addr *bp = &ep->base.bind_addr;
684         int retval = 0;
685         void *addr_buf;
686         union sctp_addr *sa_addr;
687         struct sctp_af *af;
688
689         pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
690                  __func__, sk, addrs, addrcnt);
691
692         addr_buf = addrs;
693         for (cnt = 0; cnt < addrcnt; cnt++) {
694                 /* If the bind address list is empty or if there is only one
695                  * bind address, there is nothing more to be removed (we need
696                  * at least one address here).
697                  */
698                 if (list_empty(&bp->address_list) ||
699                     (sctp_list_single_entry(&bp->address_list))) {
700                         retval = -EBUSY;
701                         goto err_bindx_rem;
702                 }
703
704                 sa_addr = addr_buf;
705                 af = sctp_get_af_specific(sa_addr->sa.sa_family);
706                 if (!af) {
707                         retval = -EINVAL;
708                         goto err_bindx_rem;
709                 }
710
711                 if (!af->addr_valid(sa_addr, sp, NULL)) {
712                         retval = -EADDRNOTAVAIL;
713                         goto err_bindx_rem;
714                 }
715
716                 if (sa_addr->v4.sin_port &&
717                     sa_addr->v4.sin_port != htons(bp->port)) {
718                         retval = -EINVAL;
719                         goto err_bindx_rem;
720                 }
721
722                 if (!sa_addr->v4.sin_port)
723                         sa_addr->v4.sin_port = htons(bp->port);
724
725                 /* FIXME - There is probably a need to check if sk->sk_saddr and
726                  * sk->sk_rcv_addr are currently set to one of the addresses to
727                  * be removed. This is something which needs to be looked into
728                  * when we are fixing the outstanding issues with multi-homing
729                  * socket routing and failover schemes. Refer to comments in
730                  * sctp_do_bind(). -daisy
731                  */
732                 retval = sctp_del_bind_addr(bp, sa_addr);
733
734                 addr_buf += af->sockaddr_len;
735 err_bindx_rem:
736                 if (retval < 0) {
737                         /* Failed. Add the ones that has been removed back */
738                         if (cnt > 0)
739                                 sctp_bindx_add(sk, addrs, cnt);
740                         return retval;
741                 }
742         }
743
744         return retval;
745 }
746
747 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
748  * the associations that are part of the endpoint indicating that a list of
749  * local addresses are removed from the endpoint.
750  *
751  * If any of the addresses is already in the bind address list of the
752  * association, we do not send the chunk for that association.  But it will not
753  * affect other associations.
754  *
755  * Only sctp_setsockopt_bindx() is supposed to call this function.
756  */
757 static int sctp_send_asconf_del_ip(struct sock          *sk,
758                                    struct sockaddr      *addrs,
759                                    int                  addrcnt)
760 {
761         struct net *net = sock_net(sk);
762         struct sctp_sock        *sp;
763         struct sctp_endpoint    *ep;
764         struct sctp_association *asoc;
765         struct sctp_transport   *transport;
766         struct sctp_bind_addr   *bp;
767         struct sctp_chunk       *chunk;
768         union sctp_addr         *laddr;
769         void                    *addr_buf;
770         struct sctp_af          *af;
771         struct sctp_sockaddr_entry *saddr;
772         int                     i;
773         int                     retval = 0;
774         int                     stored = 0;
775
776         chunk = NULL;
777         if (!net->sctp.addip_enable)
778                 return retval;
779
780         sp = sctp_sk(sk);
781         ep = sp->ep;
782
783         pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
784                  __func__, sk, addrs, addrcnt);
785
786         list_for_each_entry(asoc, &ep->asocs, asocs) {
787
788                 if (!asoc->peer.asconf_capable)
789                         continue;
790
791                 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
792                         continue;
793
794                 if (!sctp_state(asoc, ESTABLISHED))
795                         continue;
796
797                 /* Check if any address in the packed array of addresses is
798                  * not present in the bind address list of the association.
799                  * If so, do not send the asconf chunk to its peer, but
800                  * continue with other associations.
801                  */
802                 addr_buf = addrs;
803                 for (i = 0; i < addrcnt; i++) {
804                         laddr = addr_buf;
805                         af = sctp_get_af_specific(laddr->v4.sin_family);
806                         if (!af) {
807                                 retval = -EINVAL;
808                                 goto out;
809                         }
810
811                         if (!sctp_assoc_lookup_laddr(asoc, laddr))
812                                 break;
813
814                         addr_buf += af->sockaddr_len;
815                 }
816                 if (i < addrcnt)
817                         continue;
818
819                 /* Find one address in the association's bind address list
820                  * that is not in the packed array of addresses. This is to
821                  * make sure that we do not delete all the addresses in the
822                  * association.
823                  */
824                 bp = &asoc->base.bind_addr;
825                 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
826                                                addrcnt, sp);
827                 if ((laddr == NULL) && (addrcnt == 1)) {
828                         if (asoc->asconf_addr_del_pending)
829                                 continue;
830                         asoc->asconf_addr_del_pending =
831                             kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
832                         if (asoc->asconf_addr_del_pending == NULL) {
833                                 retval = -ENOMEM;
834                                 goto out;
835                         }
836                         asoc->asconf_addr_del_pending->sa.sa_family =
837                                     addrs->sa_family;
838                         asoc->asconf_addr_del_pending->v4.sin_port =
839                                     htons(bp->port);
840                         if (addrs->sa_family == AF_INET) {
841                                 struct sockaddr_in *sin;
842
843                                 sin = (struct sockaddr_in *)addrs;
844                                 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
845                         } else if (addrs->sa_family == AF_INET6) {
846                                 struct sockaddr_in6 *sin6;
847
848                                 sin6 = (struct sockaddr_in6 *)addrs;
849                                 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
850                         }
851
852                         pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
853                                  __func__, asoc, &asoc->asconf_addr_del_pending->sa,
854                                  asoc->asconf_addr_del_pending);
855
856                         asoc->src_out_of_asoc_ok = 1;
857                         stored = 1;
858                         goto skip_mkasconf;
859                 }
860
861                 if (laddr == NULL)
862                         return -EINVAL;
863
864                 /* We do not need RCU protection throughout this loop
865                  * because this is done under a socket lock from the
866                  * setsockopt call.
867                  */
868                 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
869                                                    SCTP_PARAM_DEL_IP);
870                 if (!chunk) {
871                         retval = -ENOMEM;
872                         goto out;
873                 }
874
875 skip_mkasconf:
876                 /* Reset use_as_src flag for the addresses in the bind address
877                  * list that are to be deleted.
878                  */
879                 addr_buf = addrs;
880                 for (i = 0; i < addrcnt; i++) {
881                         laddr = addr_buf;
882                         af = sctp_get_af_specific(laddr->v4.sin_family);
883                         list_for_each_entry(saddr, &bp->address_list, list) {
884                                 if (sctp_cmp_addr_exact(&saddr->a, laddr))
885                                         saddr->state = SCTP_ADDR_DEL;
886                         }
887                         addr_buf += af->sockaddr_len;
888                 }
889
890                 /* Update the route and saddr entries for all the transports
891                  * as some of the addresses in the bind address list are
892                  * about to be deleted and cannot be used as source addresses.
893                  */
894                 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
895                                         transports) {
896                         sctp_transport_dst_release(transport);
897                         sctp_transport_route(transport, NULL,
898                                              sctp_sk(asoc->base.sk));
899                 }
900
901                 if (stored)
902                         /* We don't need to transmit ASCONF */
903                         continue;
904                 retval = sctp_send_asconf(asoc, chunk);
905         }
906 out:
907         return retval;
908 }
909
910 /* set addr events to assocs in the endpoint.  ep and addr_wq must be locked */
911 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
912 {
913         struct sock *sk = sctp_opt2sk(sp);
914         union sctp_addr *addr;
915         struct sctp_af *af;
916
917         /* It is safe to write port space in caller. */
918         addr = &addrw->a;
919         addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
920         af = sctp_get_af_specific(addr->sa.sa_family);
921         if (!af)
922                 return -EINVAL;
923         if (sctp_verify_addr(sk, addr, af->sockaddr_len))
924                 return -EINVAL;
925
926         if (addrw->state == SCTP_ADDR_NEW)
927                 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
928         else
929                 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
930 }
931
932 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
933  *
934  * API 8.1
935  * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
936  *                int flags);
937  *
938  * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
939  * If the sd is an IPv6 socket, the addresses passed can either be IPv4
940  * or IPv6 addresses.
941  *
942  * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
943  * Section 3.1.2 for this usage.
944  *
945  * addrs is a pointer to an array of one or more socket addresses. Each
946  * address is contained in its appropriate structure (i.e. struct
947  * sockaddr_in or struct sockaddr_in6) the family of the address type
948  * must be used to distinguish the address length (note that this
949  * representation is termed a "packed array" of addresses). The caller
950  * specifies the number of addresses in the array with addrcnt.
951  *
952  * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
953  * -1, and sets errno to the appropriate error code.
954  *
955  * For SCTP, the port given in each socket address must be the same, or
956  * sctp_bindx() will fail, setting errno to EINVAL.
957  *
958  * The flags parameter is formed from the bitwise OR of zero or more of
959  * the following currently defined flags:
960  *
961  * SCTP_BINDX_ADD_ADDR
962  *
963  * SCTP_BINDX_REM_ADDR
964  *
965  * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
966  * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
967  * addresses from the association. The two flags are mutually exclusive;
968  * if both are given, sctp_bindx() will fail with EINVAL. A caller may
969  * not remove all addresses from an association; sctp_bindx() will
970  * reject such an attempt with EINVAL.
971  *
972  * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
973  * additional addresses with an endpoint after calling bind().  Or use
974  * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
975  * socket is associated with so that no new association accepted will be
976  * associated with those addresses. If the endpoint supports dynamic
977  * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
978  * endpoint to send the appropriate message to the peer to change the
979  * peers address lists.
980  *
981  * Adding and removing addresses from a connected association is
982  * optional functionality. Implementations that do not support this
983  * functionality should return EOPNOTSUPP.
984  *
985  * Basically do nothing but copying the addresses from user to kernel
986  * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
987  * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
988  * from userspace.
989  *
990  * On exit there is no need to do sockfd_put(), sys_setsockopt() does
991  * it.
992  *
993  * sk        The sk of the socket
994  * addrs     The pointer to the addresses in user land
995  * addrssize Size of the addrs buffer
996  * op        Operation to perform (add or remove, see the flags of
997  *           sctp_bindx)
998  *
999  * Returns 0 if ok, <0 errno code on error.
1000  */
1001 static int sctp_setsockopt_bindx(struct sock *sk,
1002                                  struct sockaddr __user *addrs,
1003                                  int addrs_size, int op)
1004 {
1005         struct sockaddr *kaddrs;
1006         int err;
1007         int addrcnt = 0;
1008         int walk_size = 0;
1009         struct sockaddr *sa_addr;
1010         void *addr_buf;
1011         struct sctp_af *af;
1012
1013         pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1014                  __func__, sk, addrs, addrs_size, op);
1015
1016         if (unlikely(addrs_size <= 0))
1017                 return -EINVAL;
1018
1019         kaddrs = vmemdup_user(addrs, addrs_size);
1020         if (unlikely(IS_ERR(kaddrs)))
1021                 return PTR_ERR(kaddrs);
1022
1023         /* Walk through the addrs buffer and count the number of addresses. */
1024         addr_buf = kaddrs;
1025         while (walk_size < addrs_size) {
1026                 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1027                         kvfree(kaddrs);
1028                         return -EINVAL;
1029                 }
1030
1031                 sa_addr = addr_buf;
1032                 af = sctp_get_af_specific(sa_addr->sa_family);
1033
1034                 /* If the address family is not supported or if this address
1035                  * causes the address buffer to overflow return EINVAL.
1036                  */
1037                 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1038                         kvfree(kaddrs);
1039                         return -EINVAL;
1040                 }
1041                 addrcnt++;
1042                 addr_buf += af->sockaddr_len;
1043                 walk_size += af->sockaddr_len;
1044         }
1045
1046         /* Do the work. */
1047         switch (op) {
1048         case SCTP_BINDX_ADD_ADDR:
1049                 /* Allow security module to validate bindx addresses. */
1050                 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1051                                                  (struct sockaddr *)kaddrs,
1052                                                  addrs_size);
1053                 if (err)
1054                         goto out;
1055                 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1056                 if (err)
1057                         goto out;
1058                 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1059                 break;
1060
1061         case SCTP_BINDX_REM_ADDR:
1062                 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1063                 if (err)
1064                         goto out;
1065                 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1066                 break;
1067
1068         default:
1069                 err = -EINVAL;
1070                 break;
1071         }
1072
1073 out:
1074         kvfree(kaddrs);
1075
1076         return err;
1077 }
1078
1079 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1080  *
1081  * Common routine for handling connect() and sctp_connectx().
1082  * Connect will come in with just a single address.
1083  */
1084 static int __sctp_connect(struct sock *sk,
1085                           struct sockaddr *kaddrs,
1086                           int addrs_size,
1087                           sctp_assoc_t *assoc_id)
1088 {
1089         struct net *net = sock_net(sk);
1090         struct sctp_sock *sp;
1091         struct sctp_endpoint *ep;
1092         struct sctp_association *asoc = NULL;
1093         struct sctp_association *asoc2;
1094         struct sctp_transport *transport;
1095         union sctp_addr to;
1096         enum sctp_scope scope;
1097         long timeo;
1098         int err = 0;
1099         int addrcnt = 0;
1100         int walk_size = 0;
1101         union sctp_addr *sa_addr = NULL;
1102         void *addr_buf;
1103         unsigned short port;
1104         unsigned int f_flags = 0;
1105
1106         sp = sctp_sk(sk);
1107         ep = sp->ep;
1108
1109         /* connect() cannot be done on a socket that is already in ESTABLISHED
1110          * state - UDP-style peeled off socket or a TCP-style socket that
1111          * is already connected.
1112          * It cannot be done even on a TCP-style listening socket.
1113          */
1114         if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1115             (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1116                 err = -EISCONN;
1117                 goto out_free;
1118         }
1119
1120         /* Walk through the addrs buffer and count the number of addresses. */
1121         addr_buf = kaddrs;
1122         while (walk_size < addrs_size) {
1123                 struct sctp_af *af;
1124
1125                 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1126                         err = -EINVAL;
1127                         goto out_free;
1128                 }
1129
1130                 sa_addr = addr_buf;
1131                 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1132
1133                 /* If the address family is not supported or if this address
1134                  * causes the address buffer to overflow return EINVAL.
1135                  */
1136                 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1137                         err = -EINVAL;
1138                         goto out_free;
1139                 }
1140
1141                 port = ntohs(sa_addr->v4.sin_port);
1142
1143                 /* Save current address so we can work with it */
1144                 memcpy(&to, sa_addr, af->sockaddr_len);
1145
1146                 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
1147                 if (err)
1148                         goto out_free;
1149
1150                 /* Make sure the destination port is correctly set
1151                  * in all addresses.
1152                  */
1153                 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1154                         err = -EINVAL;
1155                         goto out_free;
1156                 }
1157
1158                 /* Check if there already is a matching association on the
1159                  * endpoint (other than the one created here).
1160                  */
1161                 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1162                 if (asoc2 && asoc2 != asoc) {
1163                         if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1164                                 err = -EISCONN;
1165                         else
1166                                 err = -EALREADY;
1167                         goto out_free;
1168                 }
1169
1170                 /* If we could not find a matching association on the endpoint,
1171                  * make sure that there is no peeled-off association matching
1172                  * the peer address even on another socket.
1173                  */
1174                 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1175                         err = -EADDRNOTAVAIL;
1176                         goto out_free;
1177                 }
1178
1179                 if (!asoc) {
1180                         /* If a bind() or sctp_bindx() is not called prior to
1181                          * an sctp_connectx() call, the system picks an
1182                          * ephemeral port and will choose an address set
1183                          * equivalent to binding with a wildcard address.
1184                          */
1185                         if (!ep->base.bind_addr.port) {
1186                                 if (sctp_autobind(sk)) {
1187                                         err = -EAGAIN;
1188                                         goto out_free;
1189                                 }
1190                         } else {
1191                                 /*
1192                                  * If an unprivileged user inherits a 1-many
1193                                  * style socket with open associations on a
1194                                  * privileged port, it MAY be permitted to
1195                                  * accept new associations, but it SHOULD NOT
1196                                  * be permitted to open new associations.
1197                                  */
1198                                 if (ep->base.bind_addr.port <
1199                                     inet_prot_sock(net) &&
1200                                     !ns_capable(net->user_ns,
1201                                     CAP_NET_BIND_SERVICE)) {
1202                                         err = -EACCES;
1203                                         goto out_free;
1204                                 }
1205                         }
1206
1207                         scope = sctp_scope(&to);
1208                         asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1209                         if (!asoc) {
1210                                 err = -ENOMEM;
1211                                 goto out_free;
1212                         }
1213
1214                         err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1215                                                               GFP_KERNEL);
1216                         if (err < 0) {
1217                                 goto out_free;
1218                         }
1219
1220                 }
1221
1222                 /* Prime the peer's transport structures.  */
1223                 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1224                                                 SCTP_UNKNOWN);
1225                 if (!transport) {
1226                         err = -ENOMEM;
1227                         goto out_free;
1228                 }
1229
1230                 addrcnt++;
1231                 addr_buf += af->sockaddr_len;
1232                 walk_size += af->sockaddr_len;
1233         }
1234
1235         /* In case the user of sctp_connectx() wants an association
1236          * id back, assign one now.
1237          */
1238         if (assoc_id) {
1239                 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1240                 if (err < 0)
1241                         goto out_free;
1242         }
1243
1244         err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1245         if (err < 0) {
1246                 goto out_free;
1247         }
1248
1249         /* Initialize sk's dport and daddr for getpeername() */
1250         inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1251         sp->pf->to_sk_daddr(sa_addr, sk);
1252         sk->sk_err = 0;
1253
1254         /* in-kernel sockets don't generally have a file allocated to them
1255          * if all they do is call sock_create_kern().
1256          */
1257         if (sk->sk_socket->file)
1258                 f_flags = sk->sk_socket->file->f_flags;
1259
1260         timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1261
1262         if (assoc_id)
1263                 *assoc_id = asoc->assoc_id;
1264
1265         err = sctp_wait_for_connect(asoc, &timeo);
1266         /* Note: the asoc may be freed after the return of
1267          * sctp_wait_for_connect.
1268          */
1269
1270         /* Don't free association on exit. */
1271         asoc = NULL;
1272
1273 out_free:
1274         pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1275                  __func__, asoc, kaddrs, err);
1276
1277         if (asoc) {
1278                 /* sctp_primitive_ASSOCIATE may have added this association
1279                  * To the hash table, try to unhash it, just in case, its a noop
1280                  * if it wasn't hashed so we're safe
1281                  */
1282                 sctp_association_free(asoc);
1283         }
1284         return err;
1285 }
1286
1287 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1288  *
1289  * API 8.9
1290  * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1291  *                      sctp_assoc_t *asoc);
1292  *
1293  * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1294  * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1295  * or IPv6 addresses.
1296  *
1297  * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1298  * Section 3.1.2 for this usage.
1299  *
1300  * addrs is a pointer to an array of one or more socket addresses. Each
1301  * address is contained in its appropriate structure (i.e. struct
1302  * sockaddr_in or struct sockaddr_in6) the family of the address type
1303  * must be used to distengish the address length (note that this
1304  * representation is termed a "packed array" of addresses). The caller
1305  * specifies the number of addresses in the array with addrcnt.
1306  *
1307  * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1308  * the association id of the new association.  On failure, sctp_connectx()
1309  * returns -1, and sets errno to the appropriate error code.  The assoc_id
1310  * is not touched by the kernel.
1311  *
1312  * For SCTP, the port given in each socket address must be the same, or
1313  * sctp_connectx() will fail, setting errno to EINVAL.
1314  *
1315  * An application can use sctp_connectx to initiate an association with
1316  * an endpoint that is multi-homed.  Much like sctp_bindx() this call
1317  * allows a caller to specify multiple addresses at which a peer can be
1318  * reached.  The way the SCTP stack uses the list of addresses to set up
1319  * the association is implementation dependent.  This function only
1320  * specifies that the stack will try to make use of all the addresses in
1321  * the list when needed.
1322  *
1323  * Note that the list of addresses passed in is only used for setting up
1324  * the association.  It does not necessarily equal the set of addresses
1325  * the peer uses for the resulting association.  If the caller wants to
1326  * find out the set of peer addresses, it must use sctp_getpaddrs() to
1327  * retrieve them after the association has been set up.
1328  *
1329  * Basically do nothing but copying the addresses from user to kernel
1330  * land and invoking either sctp_connectx(). This is used for tunneling
1331  * the sctp_connectx() request through sctp_setsockopt() from userspace.
1332  *
1333  * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1334  * it.
1335  *
1336  * sk        The sk of the socket
1337  * addrs     The pointer to the addresses in user land
1338  * addrssize Size of the addrs buffer
1339  *
1340  * Returns >=0 if ok, <0 errno code on error.
1341  */
1342 static int __sctp_setsockopt_connectx(struct sock *sk,
1343                                       struct sockaddr __user *addrs,
1344                                       int addrs_size,
1345                                       sctp_assoc_t *assoc_id)
1346 {
1347         struct sockaddr *kaddrs;
1348         int err = 0;
1349
1350         pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1351                  __func__, sk, addrs, addrs_size);
1352
1353         if (unlikely(addrs_size <= 0))
1354                 return -EINVAL;
1355
1356         kaddrs = vmemdup_user(addrs, addrs_size);
1357         if (unlikely(IS_ERR(kaddrs)))
1358                 return PTR_ERR(kaddrs);
1359
1360         /* Allow security module to validate connectx addresses. */
1361         err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1362                                          (struct sockaddr *)kaddrs,
1363                                           addrs_size);
1364         if (err)
1365                 goto out_free;
1366
1367         err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
1368
1369 out_free:
1370         kvfree(kaddrs);
1371
1372         return err;
1373 }
1374
1375 /*
1376  * This is an older interface.  It's kept for backward compatibility
1377  * to the option that doesn't provide association id.
1378  */
1379 static int sctp_setsockopt_connectx_old(struct sock *sk,
1380                                         struct sockaddr __user *addrs,
1381                                         int addrs_size)
1382 {
1383         return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1384 }
1385
1386 /*
1387  * New interface for the API.  The since the API is done with a socket
1388  * option, to make it simple we feed back the association id is as a return
1389  * indication to the call.  Error is always negative and association id is
1390  * always positive.
1391  */
1392 static int sctp_setsockopt_connectx(struct sock *sk,
1393                                     struct sockaddr __user *addrs,
1394                                     int addrs_size)
1395 {
1396         sctp_assoc_t assoc_id = 0;
1397         int err = 0;
1398
1399         err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1400
1401         if (err)
1402                 return err;
1403         else
1404                 return assoc_id;
1405 }
1406
1407 /*
1408  * New (hopefully final) interface for the API.
1409  * We use the sctp_getaddrs_old structure so that use-space library
1410  * can avoid any unnecessary allocations. The only different part
1411  * is that we store the actual length of the address buffer into the
1412  * addrs_num structure member. That way we can re-use the existing
1413  * code.
1414  */
1415 #ifdef CONFIG_COMPAT
1416 struct compat_sctp_getaddrs_old {
1417         sctp_assoc_t    assoc_id;
1418         s32             addr_num;
1419         compat_uptr_t   addrs;          /* struct sockaddr * */
1420 };
1421 #endif
1422
1423 static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1424                                      char __user *optval,
1425                                      int __user *optlen)
1426 {
1427         struct sctp_getaddrs_old param;
1428         sctp_assoc_t assoc_id = 0;
1429         int err = 0;
1430
1431 #ifdef CONFIG_COMPAT
1432         if (in_compat_syscall()) {
1433                 struct compat_sctp_getaddrs_old param32;
1434
1435                 if (len < sizeof(param32))
1436                         return -EINVAL;
1437                 if (copy_from_user(&param32, optval, sizeof(param32)))
1438                         return -EFAULT;
1439
1440                 param.assoc_id = param32.assoc_id;
1441                 param.addr_num = param32.addr_num;
1442                 param.addrs = compat_ptr(param32.addrs);
1443         } else
1444 #endif
1445         {
1446                 if (len < sizeof(param))
1447                         return -EINVAL;
1448                 if (copy_from_user(&param, optval, sizeof(param)))
1449                         return -EFAULT;
1450         }
1451
1452         err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1453                                          param.addrs, param.addr_num,
1454                                          &assoc_id);
1455         if (err == 0 || err == -EINPROGRESS) {
1456                 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1457                         return -EFAULT;
1458                 if (put_user(sizeof(assoc_id), optlen))
1459                         return -EFAULT;
1460         }
1461
1462         return err;
1463 }
1464
1465 /* API 3.1.4 close() - UDP Style Syntax
1466  * Applications use close() to perform graceful shutdown (as described in
1467  * Section 10.1 of [SCTP]) on ALL the associations currently represented
1468  * by a UDP-style socket.
1469  *
1470  * The syntax is
1471  *
1472  *   ret = close(int sd);
1473  *
1474  *   sd      - the socket descriptor of the associations to be closed.
1475  *
1476  * To gracefully shutdown a specific association represented by the
1477  * UDP-style socket, an application should use the sendmsg() call,
1478  * passing no user data, but including the appropriate flag in the
1479  * ancillary data (see Section xxxx).
1480  *
1481  * If sd in the close() call is a branched-off socket representing only
1482  * one association, the shutdown is performed on that association only.
1483  *
1484  * 4.1.6 close() - TCP Style Syntax
1485  *
1486  * Applications use close() to gracefully close down an association.
1487  *
1488  * The syntax is:
1489  *
1490  *    int close(int sd);
1491  *
1492  *      sd      - the socket descriptor of the association to be closed.
1493  *
1494  * After an application calls close() on a socket descriptor, no further
1495  * socket operations will succeed on that descriptor.
1496  *
1497  * API 7.1.4 SO_LINGER
1498  *
1499  * An application using the TCP-style socket can use this option to
1500  * perform the SCTP ABORT primitive.  The linger option structure is:
1501  *
1502  *  struct  linger {
1503  *     int     l_onoff;                // option on/off
1504  *     int     l_linger;               // linger time
1505  * };
1506  *
1507  * To enable the option, set l_onoff to 1.  If the l_linger value is set
1508  * to 0, calling close() is the same as the ABORT primitive.  If the
1509  * value is set to a negative value, the setsockopt() call will return
1510  * an error.  If the value is set to a positive value linger_time, the
1511  * close() can be blocked for at most linger_time ms.  If the graceful
1512  * shutdown phase does not finish during this period, close() will
1513  * return but the graceful shutdown phase continues in the system.
1514  */
1515 static void sctp_close(struct sock *sk, long timeout)
1516 {
1517         struct net *net = sock_net(sk);
1518         struct sctp_endpoint *ep;
1519         struct sctp_association *asoc;
1520         struct list_head *pos, *temp;
1521         unsigned int data_was_unread;
1522
1523         pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1524
1525         lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1526         sk->sk_shutdown = SHUTDOWN_MASK;
1527         inet_sk_set_state(sk, SCTP_SS_CLOSING);
1528
1529         ep = sctp_sk(sk)->ep;
1530
1531         /* Clean up any skbs sitting on the receive queue.  */
1532         data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1533         data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1534
1535         /* Walk all associations on an endpoint.  */
1536         list_for_each_safe(pos, temp, &ep->asocs) {
1537                 asoc = list_entry(pos, struct sctp_association, asocs);
1538
1539                 if (sctp_style(sk, TCP)) {
1540                         /* A closed association can still be in the list if
1541                          * it belongs to a TCP-style listening socket that is
1542                          * not yet accepted. If so, free it. If not, send an
1543                          * ABORT or SHUTDOWN based on the linger options.
1544                          */
1545                         if (sctp_state(asoc, CLOSED)) {
1546                                 sctp_association_free(asoc);
1547                                 continue;
1548                         }
1549                 }
1550
1551                 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1552                     !skb_queue_empty(&asoc->ulpq.reasm) ||
1553                     !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1554                     (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1555                         struct sctp_chunk *chunk;
1556
1557                         chunk = sctp_make_abort_user(asoc, NULL, 0);
1558                         sctp_primitive_ABORT(net, asoc, chunk);
1559                 } else
1560                         sctp_primitive_SHUTDOWN(net, asoc, NULL);
1561         }
1562
1563         /* On a TCP-style socket, block for at most linger_time if set. */
1564         if (sctp_style(sk, TCP) && timeout)
1565                 sctp_wait_for_close(sk, timeout);
1566
1567         /* This will run the backlog queue.  */
1568         release_sock(sk);
1569
1570         /* Supposedly, no process has access to the socket, but
1571          * the net layers still may.
1572          * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1573          * held and that should be grabbed before socket lock.
1574          */
1575         spin_lock_bh(&net->sctp.addr_wq_lock);
1576         bh_lock_sock_nested(sk);
1577
1578         /* Hold the sock, since sk_common_release() will put sock_put()
1579          * and we have just a little more cleanup.
1580          */
1581         sock_hold(sk);
1582         sk_common_release(sk);
1583
1584         bh_unlock_sock(sk);
1585         spin_unlock_bh(&net->sctp.addr_wq_lock);
1586
1587         sock_put(sk);
1588
1589         SCTP_DBG_OBJCNT_DEC(sock);
1590 }
1591
1592 /* Handle EPIPE error. */
1593 static int sctp_error(struct sock *sk, int flags, int err)
1594 {
1595         if (err == -EPIPE)
1596                 err = sock_error(sk) ? : -EPIPE;
1597         if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1598                 send_sig(SIGPIPE, current, 0);
1599         return err;
1600 }
1601
1602 /* API 3.1.3 sendmsg() - UDP Style Syntax
1603  *
1604  * An application uses sendmsg() and recvmsg() calls to transmit data to
1605  * and receive data from its peer.
1606  *
1607  *  ssize_t sendmsg(int socket, const struct msghdr *message,
1608  *                  int flags);
1609  *
1610  *  socket  - the socket descriptor of the endpoint.
1611  *  message - pointer to the msghdr structure which contains a single
1612  *            user message and possibly some ancillary data.
1613  *
1614  *            See Section 5 for complete description of the data
1615  *            structures.
1616  *
1617  *  flags   - flags sent or received with the user message, see Section
1618  *            5 for complete description of the flags.
1619  *
1620  * Note:  This function could use a rewrite especially when explicit
1621  * connect support comes in.
1622  */
1623 /* BUG:  We do not implement the equivalent of sk_stream_wait_memory(). */
1624
1625 static int sctp_msghdr_parse(const struct msghdr *msg,
1626                              struct sctp_cmsgs *cmsgs);
1627
1628 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1629                               struct sctp_sndrcvinfo *srinfo,
1630                               const struct msghdr *msg, size_t msg_len)
1631 {
1632         __u16 sflags;
1633         int err;
1634
1635         if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1636                 return -EPIPE;
1637
1638         if (msg_len > sk->sk_sndbuf)
1639                 return -EMSGSIZE;
1640
1641         memset(cmsgs, 0, sizeof(*cmsgs));
1642         err = sctp_msghdr_parse(msg, cmsgs);
1643         if (err) {
1644                 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1645                 return err;
1646         }
1647
1648         memset(srinfo, 0, sizeof(*srinfo));
1649         if (cmsgs->srinfo) {
1650                 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1651                 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1652                 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1653                 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1654                 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1655                 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1656         }
1657
1658         if (cmsgs->sinfo) {
1659                 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1660                 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1661                 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1662                 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1663                 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1664         }
1665
1666         if (cmsgs->prinfo) {
1667                 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1668                 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1669                                    cmsgs->prinfo->pr_policy);
1670         }
1671
1672         sflags = srinfo->sinfo_flags;
1673         if (!sflags && msg_len)
1674                 return 0;
1675
1676         if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1677                 return -EINVAL;
1678
1679         if (((sflags & SCTP_EOF) && msg_len > 0) ||
1680             (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1681                 return -EINVAL;
1682
1683         if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1684                 return -EINVAL;
1685
1686         return 0;
1687 }
1688
1689 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1690                                  struct sctp_cmsgs *cmsgs,
1691                                  union sctp_addr *daddr,
1692                                  struct sctp_transport **tp)
1693 {
1694         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1695         struct net *net = sock_net(sk);
1696         struct sctp_association *asoc;
1697         enum sctp_scope scope;
1698         struct cmsghdr *cmsg;
1699         struct sctp_af *af;
1700         int err;
1701
1702         *tp = NULL;
1703
1704         if (sflags & (SCTP_EOF | SCTP_ABORT))
1705                 return -EINVAL;
1706
1707         if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1708                                     sctp_sstate(sk, CLOSING)))
1709                 return -EADDRNOTAVAIL;
1710
1711         if (sctp_endpoint_is_peeled_off(ep, daddr))
1712                 return -EADDRNOTAVAIL;
1713
1714         if (!ep->base.bind_addr.port) {
1715                 if (sctp_autobind(sk))
1716                         return -EAGAIN;
1717         } else {
1718                 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1719                     !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1720                         return -EACCES;
1721         }
1722
1723         scope = sctp_scope(daddr);
1724
1725         /* Label connection socket for first association 1-to-many
1726          * style for client sequence socket()->sendmsg(). This
1727          * needs to be done before sctp_assoc_add_peer() as that will
1728          * set up the initial packet that needs to account for any
1729          * security ip options (CIPSO/CALIPSO) added to the packet.
1730          */
1731         af = sctp_get_af_specific(daddr->sa.sa_family);
1732         if (!af)
1733                 return -EINVAL;
1734         err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1735                                          (struct sockaddr *)daddr,
1736                                          af->sockaddr_len);
1737         if (err < 0)
1738                 return err;
1739
1740         asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1741         if (!asoc)
1742                 return -ENOMEM;
1743
1744         if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1745                 err = -ENOMEM;
1746                 goto free;
1747         }
1748
1749         if (cmsgs->init) {
1750                 struct sctp_initmsg *init = cmsgs->init;
1751
1752                 if (init->sinit_num_ostreams) {
1753                         __u16 outcnt = init->sinit_num_ostreams;
1754
1755                         asoc->c.sinit_num_ostreams = outcnt;
1756                         /* outcnt has been changed, need to re-init stream */
1757                         err = sctp_stream_init(&asoc->stream, outcnt, 0,
1758                                                GFP_KERNEL);
1759                         if (err)
1760                                 goto free;
1761                 }
1762
1763                 if (init->sinit_max_instreams)
1764                         asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1765
1766                 if (init->sinit_max_attempts)
1767                         asoc->max_init_attempts = init->sinit_max_attempts;
1768
1769                 if (init->sinit_max_init_timeo)
1770                         asoc->max_init_timeo =
1771                                 msecs_to_jiffies(init->sinit_max_init_timeo);
1772         }
1773
1774         *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1775         if (!*tp) {
1776                 err = -ENOMEM;
1777                 goto free;
1778         }
1779
1780         if (!cmsgs->addrs_msg)
1781                 return 0;
1782
1783         /* sendv addr list parse */
1784         for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1785                 struct sctp_transport *transport;
1786                 struct sctp_association *old;
1787                 union sctp_addr _daddr;
1788                 int dlen;
1789
1790                 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1791                     (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1792                      cmsg->cmsg_type != SCTP_DSTADDRV6))
1793                         continue;
1794
1795                 daddr = &_daddr;
1796                 memset(daddr, 0, sizeof(*daddr));
1797                 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1798                 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1799                         if (dlen < sizeof(struct in_addr)) {
1800                                 err = -EINVAL;
1801                                 goto free;
1802                         }
1803
1804                         dlen = sizeof(struct in_addr);
1805                         daddr->v4.sin_family = AF_INET;
1806                         daddr->v4.sin_port = htons(asoc->peer.port);
1807                         memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1808                 } else {
1809                         if (dlen < sizeof(struct in6_addr)) {
1810                                 err = -EINVAL;
1811                                 goto free;
1812                         }
1813
1814                         dlen = sizeof(struct in6_addr);
1815                         daddr->v6.sin6_family = AF_INET6;
1816                         daddr->v6.sin6_port = htons(asoc->peer.port);
1817                         memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1818                 }
1819                 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1820                 if (err)
1821                         goto free;
1822
1823                 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1824                 if (old && old != asoc) {
1825                         if (old->state >= SCTP_STATE_ESTABLISHED)
1826                                 err = -EISCONN;
1827                         else
1828                                 err = -EALREADY;
1829                         goto free;
1830                 }
1831
1832                 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1833                         err = -EADDRNOTAVAIL;
1834                         goto free;
1835                 }
1836
1837                 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1838                                                 SCTP_UNKNOWN);
1839                 if (!transport) {
1840                         err = -ENOMEM;
1841                         goto free;
1842                 }
1843         }
1844
1845         return 0;
1846
1847 free:
1848         sctp_association_free(asoc);
1849         return err;
1850 }
1851
1852 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1853                                      __u16 sflags, struct msghdr *msg,
1854                                      size_t msg_len)
1855 {
1856         struct sock *sk = asoc->base.sk;
1857         struct net *net = sock_net(sk);
1858
1859         if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1860                 return -EPIPE;
1861
1862         if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1863             !sctp_state(asoc, ESTABLISHED))
1864                 return 0;
1865
1866         if (sflags & SCTP_EOF) {
1867                 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1868                 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1869
1870                 return 0;
1871         }
1872
1873         if (sflags & SCTP_ABORT) {
1874                 struct sctp_chunk *chunk;
1875
1876                 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1877                 if (!chunk)
1878                         return -ENOMEM;
1879
1880                 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1881                 sctp_primitive_ABORT(net, asoc, chunk);
1882
1883                 return 0;
1884         }
1885
1886         return 1;
1887 }
1888
1889 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1890                                 struct msghdr *msg, size_t msg_len,
1891                                 struct sctp_transport *transport,
1892                                 struct sctp_sndrcvinfo *sinfo)
1893 {
1894         struct sock *sk = asoc->base.sk;
1895         struct net *net = sock_net(sk);
1896         struct sctp_datamsg *datamsg;
1897         bool wait_connect = false;
1898         struct sctp_chunk *chunk;
1899         long timeo;
1900         int err;
1901
1902         if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1903                 err = -EINVAL;
1904                 goto err;
1905         }
1906
1907         if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1908                 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1909                 if (err)
1910                         goto err;
1911         }
1912
1913         if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) {
1914                 err = -EMSGSIZE;
1915                 goto err;
1916         }
1917
1918         if (asoc->pmtu_pending)
1919                 sctp_assoc_pending_pmtu(asoc);
1920
1921         if (sctp_wspace(asoc) < msg_len)
1922                 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1923
1924         if (!sctp_wspace(asoc)) {
1925                 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1926                 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1927                 if (err)
1928                         goto err;
1929         }
1930
1931         if (sctp_state(asoc, CLOSED)) {
1932                 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1933                 if (err)
1934                         goto err;
1935
1936                 if (sctp_sk(sk)->strm_interleave) {
1937                         timeo = sock_sndtimeo(sk, 0);
1938                         err = sctp_wait_for_connect(asoc, &timeo);
1939                         if (err)
1940                                 goto err;
1941                 } else {
1942                         wait_connect = true;
1943                 }
1944
1945                 pr_debug("%s: we associated primitively\n", __func__);
1946         }
1947
1948         datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1949         if (IS_ERR(datamsg)) {
1950                 err = PTR_ERR(datamsg);
1951                 goto err;
1952         }
1953
1954         asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1955
1956         list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1957                 sctp_chunk_hold(chunk);
1958                 sctp_set_owner_w(chunk);
1959                 chunk->transport = transport;
1960         }
1961
1962         err = sctp_primitive_SEND(net, asoc, datamsg);
1963         if (err) {
1964                 sctp_datamsg_free(datamsg);
1965                 goto err;
1966         }
1967
1968         pr_debug("%s: we sent primitively\n", __func__);
1969
1970         sctp_datamsg_put(datamsg);
1971
1972         if (unlikely(wait_connect)) {
1973                 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1974                 sctp_wait_for_connect(asoc, &timeo);
1975         }
1976
1977         err = msg_len;
1978
1979 err:
1980         return err;
1981 }
1982
1983 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1984                                                const struct msghdr *msg,
1985                                                struct sctp_cmsgs *cmsgs)
1986 {
1987         union sctp_addr *daddr = NULL;
1988         int err;
1989
1990         if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1991                 int len = msg->msg_namelen;
1992
1993                 if (len > sizeof(*daddr))
1994                         len = sizeof(*daddr);
1995
1996                 daddr = (union sctp_addr *)msg->msg_name;
1997
1998                 err = sctp_verify_addr(sk, daddr, len);
1999                 if (err)
2000                         return ERR_PTR(err);
2001         }
2002
2003         return daddr;
2004 }
2005
2006 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
2007                                       struct sctp_sndrcvinfo *sinfo,
2008                                       struct sctp_cmsgs *cmsgs)
2009 {
2010         if (!cmsgs->srinfo && !cmsgs->sinfo) {
2011                 sinfo->sinfo_stream = asoc->default_stream;
2012                 sinfo->sinfo_ppid = asoc->default_ppid;
2013                 sinfo->sinfo_context = asoc->default_context;
2014                 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
2015
2016                 if (!cmsgs->prinfo)
2017                         sinfo->sinfo_flags = asoc->default_flags;
2018         }
2019
2020         if (!cmsgs->srinfo && !cmsgs->prinfo)
2021                 sinfo->sinfo_timetolive = asoc->default_timetolive;
2022
2023         if (cmsgs->authinfo) {
2024                 /* Reuse sinfo_tsn to indicate that authinfo was set and
2025                  * sinfo_ssn to save the keyid on tx path.
2026                  */
2027                 sinfo->sinfo_tsn = 1;
2028                 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2029         }
2030 }
2031
2032 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
2033 {
2034         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
2035         struct sctp_transport *transport = NULL;
2036         struct sctp_sndrcvinfo _sinfo, *sinfo;
2037         struct sctp_association *asoc;
2038         struct sctp_cmsgs cmsgs;
2039         union sctp_addr *daddr;
2040         bool new = false;
2041         __u16 sflags;
2042         int err;
2043
2044         /* Parse and get snd_info */
2045         err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2046         if (err)
2047                 goto out;
2048
2049         sinfo  = &_sinfo;
2050         sflags = sinfo->sinfo_flags;
2051
2052         /* Get daddr from msg */
2053         daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2054         if (IS_ERR(daddr)) {
2055                 err = PTR_ERR(daddr);
2056                 goto out;
2057         }
2058
2059         lock_sock(sk);
2060
2061         /* SCTP_SENDALL process */
2062         if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2063                 list_for_each_entry(asoc, &ep->asocs, asocs) {
2064                         err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2065                                                         msg_len);
2066                         if (err == 0)
2067                                 continue;
2068                         if (err < 0)
2069                                 goto out_unlock;
2070
2071                         sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2072
2073                         err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2074                                                    NULL, sinfo);
2075                         if (err < 0)
2076                                 goto out_unlock;
2077
2078                         iov_iter_revert(&msg->msg_iter, err);
2079                 }
2080
2081                 goto out_unlock;
2082         }
2083
2084         /* Get and check or create asoc */
2085         if (daddr) {
2086                 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
2087                 if (asoc) {
2088                         err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2089                                                         msg_len);
2090                         if (err <= 0)
2091                                 goto out_unlock;
2092                 } else {
2093                         err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2094                                                     &transport);
2095                         if (err)
2096                                 goto out_unlock;
2097
2098                         asoc = transport->asoc;
2099                         new = true;
2100                 }
2101
2102                 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2103                         transport = NULL;
2104         } else {
2105                 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2106                 if (!asoc) {
2107                         err = -EPIPE;
2108                         goto out_unlock;
2109                 }
2110
2111                 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2112                 if (err <= 0)
2113                         goto out_unlock;
2114         }
2115
2116         /* Update snd_info with the asoc */
2117         sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2118
2119         /* Send msg to the asoc */
2120         err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2121         if (err < 0 && err != -ESRCH && new)
2122                 sctp_association_free(asoc);
2123
2124 out_unlock:
2125         release_sock(sk);
2126 out:
2127         return sctp_error(sk, msg->msg_flags, err);
2128 }
2129
2130 /* This is an extended version of skb_pull() that removes the data from the
2131  * start of a skb even when data is spread across the list of skb's in the
2132  * frag_list. len specifies the total amount of data that needs to be removed.
2133  * when 'len' bytes could be removed from the skb, it returns 0.
2134  * If 'len' exceeds the total skb length,  it returns the no. of bytes that
2135  * could not be removed.
2136  */
2137 static int sctp_skb_pull(struct sk_buff *skb, int len)
2138 {
2139         struct sk_buff *list;
2140         int skb_len = skb_headlen(skb);
2141         int rlen;
2142
2143         if (len <= skb_len) {
2144                 __skb_pull(skb, len);
2145                 return 0;
2146         }
2147         len -= skb_len;
2148         __skb_pull(skb, skb_len);
2149
2150         skb_walk_frags(skb, list) {
2151                 rlen = sctp_skb_pull(list, len);
2152                 skb->len -= (len-rlen);
2153                 skb->data_len -= (len-rlen);
2154
2155                 if (!rlen)
2156                         return 0;
2157
2158                 len = rlen;
2159         }
2160
2161         return len;
2162 }
2163
2164 /* API 3.1.3  recvmsg() - UDP Style Syntax
2165  *
2166  *  ssize_t recvmsg(int socket, struct msghdr *message,
2167  *                    int flags);
2168  *
2169  *  socket  - the socket descriptor of the endpoint.
2170  *  message - pointer to the msghdr structure which contains a single
2171  *            user message and possibly some ancillary data.
2172  *
2173  *            See Section 5 for complete description of the data
2174  *            structures.
2175  *
2176  *  flags   - flags sent or received with the user message, see Section
2177  *            5 for complete description of the flags.
2178  */
2179 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2180                         int noblock, int flags, int *addr_len)
2181 {
2182         struct sctp_ulpevent *event = NULL;
2183         struct sctp_sock *sp = sctp_sk(sk);
2184         struct sk_buff *skb, *head_skb;
2185         int copied;
2186         int err = 0;
2187         int skb_len;
2188
2189         pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2190                  "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2191                  addr_len);
2192
2193         lock_sock(sk);
2194
2195         if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2196             !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2197                 err = -ENOTCONN;
2198                 goto out;
2199         }
2200
2201         skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2202         if (!skb)
2203                 goto out;
2204
2205         /* Get the total length of the skb including any skb's in the
2206          * frag_list.
2207          */
2208         skb_len = skb->len;
2209
2210         copied = skb_len;
2211         if (copied > len)
2212                 copied = len;
2213
2214         err = skb_copy_datagram_msg(skb, 0, msg, copied);
2215
2216         event = sctp_skb2event(skb);
2217
2218         if (err)
2219                 goto out_free;
2220
2221         if (event->chunk && event->chunk->head_skb)
2222                 head_skb = event->chunk->head_skb;
2223         else
2224                 head_skb = skb;
2225         sock_recv_ts_and_drops(msg, sk, head_skb);
2226         if (sctp_ulpevent_is_notification(event)) {
2227                 msg->msg_flags |= MSG_NOTIFICATION;
2228                 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2229         } else {
2230                 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2231         }
2232
2233         /* Check if we allow SCTP_NXTINFO. */
2234         if (sp->recvnxtinfo)
2235                 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2236         /* Check if we allow SCTP_RCVINFO. */
2237         if (sp->recvrcvinfo)
2238                 sctp_ulpevent_read_rcvinfo(event, msg);
2239         /* Check if we allow SCTP_SNDRCVINFO. */
2240         if (sp->subscribe.sctp_data_io_event)
2241                 sctp_ulpevent_read_sndrcvinfo(event, msg);
2242
2243         err = copied;
2244
2245         /* If skb's length exceeds the user's buffer, update the skb and
2246          * push it back to the receive_queue so that the next call to
2247          * recvmsg() will return the remaining data. Don't set MSG_EOR.
2248          */
2249         if (skb_len > copied) {
2250                 msg->msg_flags &= ~MSG_EOR;
2251                 if (flags & MSG_PEEK)
2252                         goto out_free;
2253                 sctp_skb_pull(skb, copied);
2254                 skb_queue_head(&sk->sk_receive_queue, skb);
2255
2256                 /* When only partial message is copied to the user, increase
2257                  * rwnd by that amount. If all the data in the skb is read,
2258                  * rwnd is updated when the event is freed.
2259                  */
2260                 if (!sctp_ulpevent_is_notification(event))
2261                         sctp_assoc_rwnd_increase(event->asoc, copied);
2262                 goto out;
2263         } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2264                    (event->msg_flags & MSG_EOR))
2265                 msg->msg_flags |= MSG_EOR;
2266         else
2267                 msg->msg_flags &= ~MSG_EOR;
2268
2269 out_free:
2270         if (flags & MSG_PEEK) {
2271                 /* Release the skb reference acquired after peeking the skb in
2272                  * sctp_skb_recv_datagram().
2273                  */
2274                 kfree_skb(skb);
2275         } else {
2276                 /* Free the event which includes releasing the reference to
2277                  * the owner of the skb, freeing the skb and updating the
2278                  * rwnd.
2279                  */
2280                 sctp_ulpevent_free(event);
2281         }
2282 out:
2283         release_sock(sk);
2284         return err;
2285 }
2286
2287 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2288  *
2289  * This option is a on/off flag.  If enabled no SCTP message
2290  * fragmentation will be performed.  Instead if a message being sent
2291  * exceeds the current PMTU size, the message will NOT be sent and
2292  * instead a error will be indicated to the user.
2293  */
2294 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2295                                              char __user *optval,
2296                                              unsigned int optlen)
2297 {
2298         int val;
2299
2300         if (optlen < sizeof(int))
2301                 return -EINVAL;
2302
2303         if (get_user(val, (int __user *)optval))
2304                 return -EFAULT;
2305
2306         sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2307
2308         return 0;
2309 }
2310
2311 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2312                                   unsigned int optlen)
2313 {
2314         struct sctp_association *asoc;
2315         struct sctp_ulpevent *event;
2316
2317         if (optlen > sizeof(struct sctp_event_subscribe))
2318                 return -EINVAL;
2319         if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2320                 return -EFAULT;
2321
2322         /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2323          * if there is no data to be sent or retransmit, the stack will
2324          * immediately send up this notification.
2325          */
2326         if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2327                                        &sctp_sk(sk)->subscribe)) {
2328                 asoc = sctp_id2assoc(sk, 0);
2329
2330                 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2331                         event = sctp_ulpevent_make_sender_dry_event(asoc,
2332                                         GFP_USER | __GFP_NOWARN);
2333                         if (!event)
2334                                 return -ENOMEM;
2335
2336                         asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2337                 }
2338         }
2339
2340         return 0;
2341 }
2342
2343 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2344  *
2345  * This socket option is applicable to the UDP-style socket only.  When
2346  * set it will cause associations that are idle for more than the
2347  * specified number of seconds to automatically close.  An association
2348  * being idle is defined an association that has NOT sent or received
2349  * user data.  The special value of '0' indicates that no automatic
2350  * close of any associations should be performed.  The option expects an
2351  * integer defining the number of seconds of idle time before an
2352  * association is closed.
2353  */
2354 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2355                                      unsigned int optlen)
2356 {
2357         struct sctp_sock *sp = sctp_sk(sk);
2358         struct net *net = sock_net(sk);
2359
2360         /* Applicable to UDP-style socket only */
2361         if (sctp_style(sk, TCP))
2362                 return -EOPNOTSUPP;
2363         if (optlen != sizeof(int))
2364                 return -EINVAL;
2365         if (copy_from_user(&sp->autoclose, optval, optlen))
2366                 return -EFAULT;
2367
2368         if (sp->autoclose > net->sctp.max_autoclose)
2369                 sp->autoclose = net->sctp.max_autoclose;
2370
2371         return 0;
2372 }
2373
2374 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2375  *
2376  * Applications can enable or disable heartbeats for any peer address of
2377  * an association, modify an address's heartbeat interval, force a
2378  * heartbeat to be sent immediately, and adjust the address's maximum
2379  * number of retransmissions sent before an address is considered
2380  * unreachable.  The following structure is used to access and modify an
2381  * address's parameters:
2382  *
2383  *  struct sctp_paddrparams {
2384  *     sctp_assoc_t            spp_assoc_id;
2385  *     struct sockaddr_storage spp_address;
2386  *     uint32_t                spp_hbinterval;
2387  *     uint16_t                spp_pathmaxrxt;
2388  *     uint32_t                spp_pathmtu;
2389  *     uint32_t                spp_sackdelay;
2390  *     uint32_t                spp_flags;
2391  * };
2392  *
2393  *   spp_assoc_id    - (one-to-many style socket) This is filled in the
2394  *                     application, and identifies the association for
2395  *                     this query.
2396  *   spp_address     - This specifies which address is of interest.
2397  *   spp_hbinterval  - This contains the value of the heartbeat interval,
2398  *                     in milliseconds.  If a  value of zero
2399  *                     is present in this field then no changes are to
2400  *                     be made to this parameter.
2401  *   spp_pathmaxrxt  - This contains the maximum number of
2402  *                     retransmissions before this address shall be
2403  *                     considered unreachable. If a  value of zero
2404  *                     is present in this field then no changes are to
2405  *                     be made to this parameter.
2406  *   spp_pathmtu     - When Path MTU discovery is disabled the value
2407  *                     specified here will be the "fixed" path mtu.
2408  *                     Note that if the spp_address field is empty
2409  *                     then all associations on this address will
2410  *                     have this fixed path mtu set upon them.
2411  *
2412  *   spp_sackdelay   - When delayed sack is enabled, this value specifies
2413  *                     the number of milliseconds that sacks will be delayed
2414  *                     for. This value will apply to all addresses of an
2415  *                     association if the spp_address field is empty. Note
2416  *                     also, that if delayed sack is enabled and this
2417  *                     value is set to 0, no change is made to the last
2418  *                     recorded delayed sack timer value.
2419  *
2420  *   spp_flags       - These flags are used to control various features
2421  *                     on an association. The flag field may contain
2422  *                     zero or more of the following options.
2423  *
2424  *                     SPP_HB_ENABLE  - Enable heartbeats on the
2425  *                     specified address. Note that if the address
2426  *                     field is empty all addresses for the association
2427  *                     have heartbeats enabled upon them.
2428  *
2429  *                     SPP_HB_DISABLE - Disable heartbeats on the
2430  *                     speicifed address. Note that if the address
2431  *                     field is empty all addresses for the association
2432  *                     will have their heartbeats disabled. Note also
2433  *                     that SPP_HB_ENABLE and SPP_HB_DISABLE are
2434  *                     mutually exclusive, only one of these two should
2435  *                     be specified. Enabling both fields will have
2436  *                     undetermined results.
2437  *
2438  *                     SPP_HB_DEMAND - Request a user initiated heartbeat
2439  *                     to be made immediately.
2440  *
2441  *                     SPP_HB_TIME_IS_ZERO - Specify's that the time for
2442  *                     heartbeat delayis to be set to the value of 0
2443  *                     milliseconds.
2444  *
2445  *                     SPP_PMTUD_ENABLE - This field will enable PMTU
2446  *                     discovery upon the specified address. Note that
2447  *                     if the address feild is empty then all addresses
2448  *                     on the association are effected.
2449  *
2450  *                     SPP_PMTUD_DISABLE - This field will disable PMTU
2451  *                     discovery upon the specified address. Note that
2452  *                     if the address feild is empty then all addresses
2453  *                     on the association are effected. Not also that
2454  *                     SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2455  *                     exclusive. Enabling both will have undetermined
2456  *                     results.
2457  *
2458  *                     SPP_SACKDELAY_ENABLE - Setting this flag turns
2459  *                     on delayed sack. The time specified in spp_sackdelay
2460  *                     is used to specify the sack delay for this address. Note
2461  *                     that if spp_address is empty then all addresses will
2462  *                     enable delayed sack and take on the sack delay
2463  *                     value specified in spp_sackdelay.
2464  *                     SPP_SACKDELAY_DISABLE - Setting this flag turns
2465  *                     off delayed sack. If the spp_address field is blank then
2466  *                     delayed sack is disabled for the entire association. Note
2467  *                     also that this field is mutually exclusive to
2468  *                     SPP_SACKDELAY_ENABLE, setting both will have undefined
2469  *                     results.
2470  */
2471 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2472                                        struct sctp_transport   *trans,
2473                                        struct sctp_association *asoc,
2474                                        struct sctp_sock        *sp,
2475                                        int                      hb_change,
2476                                        int                      pmtud_change,
2477                                        int                      sackdelay_change)
2478 {
2479         int error;
2480
2481         if (params->spp_flags & SPP_HB_DEMAND && trans) {
2482                 struct net *net = sock_net(trans->asoc->base.sk);
2483
2484                 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2485                 if (error)
2486                         return error;
2487         }
2488
2489         /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2490          * this field is ignored.  Note also that a value of zero indicates
2491          * the current setting should be left unchanged.
2492          */
2493         if (params->spp_flags & SPP_HB_ENABLE) {
2494
2495                 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2496                  * set.  This lets us use 0 value when this flag
2497                  * is set.
2498                  */
2499                 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2500                         params->spp_hbinterval = 0;
2501
2502                 if (params->spp_hbinterval ||
2503                     (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2504                         if (trans) {
2505                                 trans->hbinterval =
2506                                     msecs_to_jiffies(params->spp_hbinterval);
2507                         } else if (asoc) {
2508                                 asoc->hbinterval =
2509                                     msecs_to_jiffies(params->spp_hbinterval);
2510                         } else {
2511                                 sp->hbinterval = params->spp_hbinterval;
2512                         }
2513                 }
2514         }
2515
2516         if (hb_change) {
2517                 if (trans) {
2518                         trans->param_flags =
2519                                 (trans->param_flags & ~SPP_HB) | hb_change;
2520                 } else if (asoc) {
2521                         asoc->param_flags =
2522                                 (asoc->param_flags & ~SPP_HB) | hb_change;
2523                 } else {
2524                         sp->param_flags =
2525                                 (sp->param_flags & ~SPP_HB) | hb_change;
2526                 }
2527         }
2528
2529         /* When Path MTU discovery is disabled the value specified here will
2530          * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2531          * include the flag SPP_PMTUD_DISABLE for this field to have any
2532          * effect).
2533          */
2534         if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2535                 if (trans) {
2536                         trans->pathmtu = params->spp_pathmtu;
2537                         sctp_assoc_sync_pmtu(asoc);
2538                 } else if (asoc) {
2539                         asoc->pathmtu = params->spp_pathmtu;
2540                 } else {
2541                         sp->pathmtu = params->spp_pathmtu;
2542                 }
2543         }
2544
2545         if (pmtud_change) {
2546                 if (trans) {
2547                         int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2548                                 (params->spp_flags & SPP_PMTUD_ENABLE);
2549                         trans->param_flags =
2550                                 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2551                         if (update) {
2552                                 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2553                                 sctp_assoc_sync_pmtu(asoc);
2554                         }
2555                 } else if (asoc) {
2556                         asoc->param_flags =
2557                                 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2558                 } else {
2559                         sp->param_flags =
2560                                 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2561                 }
2562         }
2563
2564         /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2565          * value of this field is ignored.  Note also that a value of zero
2566          * indicates the current setting should be left unchanged.
2567          */
2568         if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2569                 if (trans) {
2570                         trans->sackdelay =
2571                                 msecs_to_jiffies(params->spp_sackdelay);
2572                 } else if (asoc) {
2573                         asoc->sackdelay =
2574                                 msecs_to_jiffies(params->spp_sackdelay);
2575                 } else {
2576                         sp->sackdelay = params->spp_sackdelay;
2577                 }
2578         }
2579
2580         if (sackdelay_change) {
2581                 if (trans) {
2582                         trans->param_flags =
2583                                 (trans->param_flags & ~SPP_SACKDELAY) |
2584                                 sackdelay_change;
2585                 } else if (asoc) {
2586                         asoc->param_flags =
2587                                 (asoc->param_flags & ~SPP_SACKDELAY) |
2588                                 sackdelay_change;
2589                 } else {
2590                         sp->param_flags =
2591                                 (sp->param_flags & ~SPP_SACKDELAY) |
2592                                 sackdelay_change;
2593                 }
2594         }
2595
2596         /* Note that a value of zero indicates the current setting should be
2597            left unchanged.
2598          */
2599         if (params->spp_pathmaxrxt) {
2600                 if (trans) {
2601                         trans->pathmaxrxt = params->spp_pathmaxrxt;
2602                 } else if (asoc) {
2603                         asoc->pathmaxrxt = params->spp_pathmaxrxt;
2604                 } else {
2605                         sp->pathmaxrxt = params->spp_pathmaxrxt;
2606                 }
2607         }
2608
2609         return 0;
2610 }
2611
2612 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2613                                             char __user *optval,
2614                                             unsigned int optlen)
2615 {
2616         struct sctp_paddrparams  params;
2617         struct sctp_transport   *trans = NULL;
2618         struct sctp_association *asoc = NULL;
2619         struct sctp_sock        *sp = sctp_sk(sk);
2620         int error;
2621         int hb_change, pmtud_change, sackdelay_change;
2622
2623         if (optlen != sizeof(struct sctp_paddrparams))
2624                 return -EINVAL;
2625
2626         if (copy_from_user(&params, optval, optlen))
2627                 return -EFAULT;
2628
2629         /* Validate flags and value parameters. */
2630         hb_change        = params.spp_flags & SPP_HB;
2631         pmtud_change     = params.spp_flags & SPP_PMTUD;
2632         sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2633
2634         if (hb_change        == SPP_HB ||
2635             pmtud_change     == SPP_PMTUD ||
2636             sackdelay_change == SPP_SACKDELAY ||
2637             params.spp_sackdelay > 500 ||
2638             (params.spp_pathmtu &&
2639              params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2640                 return -EINVAL;
2641
2642         /* If an address other than INADDR_ANY is specified, and
2643          * no transport is found, then the request is invalid.
2644          */
2645         if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
2646                 trans = sctp_addr_id2transport(sk, &params.spp_address,
2647                                                params.spp_assoc_id);
2648                 if (!trans)
2649                         return -EINVAL;
2650         }
2651
2652         /* Get association, if assoc_id != 0 and the socket is a one
2653          * to many style socket, and an association was not found, then
2654          * the id was invalid.
2655          */
2656         asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2657         if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2658                 return -EINVAL;
2659
2660         /* Heartbeat demand can only be sent on a transport or
2661          * association, but not a socket.
2662          */
2663         if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2664                 return -EINVAL;
2665
2666         /* Process parameters. */
2667         error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2668                                             hb_change, pmtud_change,
2669                                             sackdelay_change);
2670
2671         if (error)
2672                 return error;
2673
2674         /* If changes are for association, also apply parameters to each
2675          * transport.
2676          */
2677         if (!trans && asoc) {
2678                 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2679                                 transports) {
2680                         sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2681                                                     hb_change, pmtud_change,
2682                                                     sackdelay_change);
2683                 }
2684         }
2685
2686         return 0;
2687 }
2688
2689 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2690 {
2691         return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2692 }
2693
2694 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2695 {
2696         return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2697 }
2698
2699 /*
2700  * 7.1.23.  Get or set delayed ack timer (SCTP_DELAYED_SACK)
2701  *
2702  * This option will effect the way delayed acks are performed.  This
2703  * option allows you to get or set the delayed ack time, in
2704  * milliseconds.  It also allows changing the delayed ack frequency.
2705  * Changing the frequency to 1 disables the delayed sack algorithm.  If
2706  * the assoc_id is 0, then this sets or gets the endpoints default
2707  * values.  If the assoc_id field is non-zero, then the set or get
2708  * effects the specified association for the one to many model (the
2709  * assoc_id field is ignored by the one to one model).  Note that if
2710  * sack_delay or sack_freq are 0 when setting this option, then the
2711  * current values will remain unchanged.
2712  *
2713  * struct sctp_sack_info {
2714  *     sctp_assoc_t            sack_assoc_id;
2715  *     uint32_t                sack_delay;
2716  *     uint32_t                sack_freq;
2717  * };
2718  *
2719  * sack_assoc_id -  This parameter, indicates which association the user
2720  *    is performing an action upon.  Note that if this field's value is
2721  *    zero then the endpoints default value is changed (effecting future
2722  *    associations only).
2723  *
2724  * sack_delay -  This parameter contains the number of milliseconds that
2725  *    the user is requesting the delayed ACK timer be set to.  Note that
2726  *    this value is defined in the standard to be between 200 and 500
2727  *    milliseconds.
2728  *
2729  * sack_freq -  This parameter contains the number of packets that must
2730  *    be received before a sack is sent without waiting for the delay
2731  *    timer to expire.  The default value for this is 2, setting this
2732  *    value to 1 will disable the delayed sack algorithm.
2733  */
2734
2735 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2736                                        char __user *optval, unsigned int optlen)
2737 {
2738         struct sctp_sack_info    params;
2739         struct sctp_transport   *trans = NULL;
2740         struct sctp_association *asoc = NULL;
2741         struct sctp_sock        *sp = sctp_sk(sk);
2742
2743         if (optlen == sizeof(struct sctp_sack_info)) {
2744                 if (copy_from_user(&params, optval, optlen))
2745                         return -EFAULT;
2746
2747                 if (params.sack_delay == 0 && params.sack_freq == 0)
2748                         return 0;
2749         } else if (optlen == sizeof(struct sctp_assoc_value)) {
2750                 pr_warn_ratelimited(DEPRECATED
2751                                     "%s (pid %d) "
2752                                     "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2753                                     "Use struct sctp_sack_info instead\n",
2754                                     current->comm, task_pid_nr(current));
2755                 if (copy_from_user(&params, optval, optlen))
2756                         return -EFAULT;
2757
2758                 if (params.sack_delay == 0)
2759                         params.sack_freq = 1;
2760                 else
2761                         params.sack_freq = 0;
2762         } else
2763                 return -EINVAL;
2764
2765         /* Validate value parameter. */
2766         if (params.sack_delay > 500)
2767                 return -EINVAL;
2768
2769         /* Get association, if sack_assoc_id != 0 and the socket is a one
2770          * to many style socket, and an association was not found, then
2771          * the id was invalid.
2772          */
2773         asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2774         if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
2775                 return -EINVAL;
2776
2777         if (params.sack_delay) {
2778                 if (asoc) {
2779                         asoc->sackdelay =
2780                                 msecs_to_jiffies(params.sack_delay);
2781                         asoc->param_flags =
2782                                 sctp_spp_sackdelay_enable(asoc->param_flags);
2783                 } else {
2784                         sp->sackdelay = params.sack_delay;
2785                         sp->param_flags =
2786                                 sctp_spp_sackdelay_enable(sp->param_flags);
2787                 }
2788         }
2789
2790         if (params.sack_freq == 1) {
2791                 if (asoc) {
2792                         asoc->param_flags =
2793                                 sctp_spp_sackdelay_disable(asoc->param_flags);
2794                 } else {
2795                         sp->param_flags =
2796                                 sctp_spp_sackdelay_disable(sp->param_flags);
2797                 }
2798         } else if (params.sack_freq > 1) {
2799                 if (asoc) {
2800                         asoc->sackfreq = params.sack_freq;
2801                         asoc->param_flags =
2802                                 sctp_spp_sackdelay_enable(asoc->param_flags);
2803                 } else {
2804                         sp->sackfreq = params.sack_freq;
2805                         sp->param_flags =
2806                                 sctp_spp_sackdelay_enable(sp->param_flags);
2807                 }
2808         }
2809
2810         /* If change is for association, also apply to each transport. */
2811         if (asoc) {
2812                 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2813                                 transports) {
2814                         if (params.sack_delay) {
2815                                 trans->sackdelay =
2816                                         msecs_to_jiffies(params.sack_delay);
2817                                 trans->param_flags =
2818                                         sctp_spp_sackdelay_enable(trans->param_flags);
2819                         }
2820                         if (params.sack_freq == 1) {
2821                                 trans->param_flags =
2822                                         sctp_spp_sackdelay_disable(trans->param_flags);
2823                         } else if (params.sack_freq > 1) {
2824                                 trans->sackfreq = params.sack_freq;
2825                                 trans->param_flags =
2826                                         sctp_spp_sackdelay_enable(trans->param_flags);
2827                         }
2828                 }
2829         }
2830
2831         return 0;
2832 }
2833
2834 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2835  *
2836  * Applications can specify protocol parameters for the default association
2837  * initialization.  The option name argument to setsockopt() and getsockopt()
2838  * is SCTP_INITMSG.
2839  *
2840  * Setting initialization parameters is effective only on an unconnected
2841  * socket (for UDP-style sockets only future associations are effected
2842  * by the change).  With TCP-style sockets, this option is inherited by
2843  * sockets derived from a listener socket.
2844  */
2845 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2846 {
2847         struct sctp_initmsg sinit;
2848         struct sctp_sock *sp = sctp_sk(sk);
2849
2850         if (optlen != sizeof(struct sctp_initmsg))
2851                 return -EINVAL;
2852         if (copy_from_user(&sinit, optval, optlen))
2853                 return -EFAULT;
2854
2855         if (sinit.sinit_num_ostreams)
2856                 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2857         if (sinit.sinit_max_instreams)
2858                 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2859         if (sinit.sinit_max_attempts)
2860                 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2861         if (sinit.sinit_max_init_timeo)
2862                 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2863
2864         return 0;
2865 }
2866
2867 /*
2868  * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2869  *
2870  *   Applications that wish to use the sendto() system call may wish to
2871  *   specify a default set of parameters that would normally be supplied
2872  *   through the inclusion of ancillary data.  This socket option allows
2873  *   such an application to set the default sctp_sndrcvinfo structure.
2874  *   The application that wishes to use this socket option simply passes
2875  *   in to this call the sctp_sndrcvinfo structure defined in Section
2876  *   5.2.2) The input parameters accepted by this call include
2877  *   sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2878  *   sinfo_timetolive.  The user must provide the sinfo_assoc_id field in
2879  *   to this call if the caller is using the UDP model.
2880  */
2881 static int sctp_setsockopt_default_send_param(struct sock *sk,
2882                                               char __user *optval,
2883                                               unsigned int optlen)
2884 {
2885         struct sctp_sock *sp = sctp_sk(sk);
2886         struct sctp_association *asoc;
2887         struct sctp_sndrcvinfo info;
2888
2889         if (optlen != sizeof(info))
2890                 return -EINVAL;
2891         if (copy_from_user(&info, optval, optlen))
2892                 return -EFAULT;
2893         if (info.sinfo_flags &
2894             ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2895               SCTP_ABORT | SCTP_EOF))
2896                 return -EINVAL;
2897
2898         asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2899         if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2900                 return -EINVAL;
2901         if (asoc) {
2902                 asoc->default_stream = info.sinfo_stream;
2903                 asoc->default_flags = info.sinfo_flags;
2904                 asoc->default_ppid = info.sinfo_ppid;
2905                 asoc->default_context = info.sinfo_context;
2906                 asoc->default_timetolive = info.sinfo_timetolive;
2907         } else {
2908                 sp->default_stream = info.sinfo_stream;
2909                 sp->default_flags = info.sinfo_flags;
2910                 sp->default_ppid = info.sinfo_ppid;
2911                 sp->default_context = info.sinfo_context;
2912                 sp->default_timetolive = info.sinfo_timetolive;
2913         }
2914
2915         return 0;
2916 }
2917
2918 /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2919  * (SCTP_DEFAULT_SNDINFO)
2920  */
2921 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2922                                            char __user *optval,
2923                                            unsigned int optlen)
2924 {
2925         struct sctp_sock *sp = sctp_sk(sk);
2926         struct sctp_association *asoc;
2927         struct sctp_sndinfo info;
2928
2929         if (optlen != sizeof(info))
2930                 return -EINVAL;
2931         if (copy_from_user(&info, optval, optlen))
2932                 return -EFAULT;
2933         if (info.snd_flags &
2934             ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2935               SCTP_ABORT | SCTP_EOF))
2936                 return -EINVAL;
2937
2938         asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2939         if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2940                 return -EINVAL;
2941         if (asoc) {
2942                 asoc->default_stream = info.snd_sid;
2943                 asoc->default_flags = info.snd_flags;
2944                 asoc->default_ppid = info.snd_ppid;
2945                 asoc->default_context = info.snd_context;
2946         } else {
2947                 sp->default_stream = info.snd_sid;
2948                 sp->default_flags = info.snd_flags;
2949                 sp->default_ppid = info.snd_ppid;
2950                 sp->default_context = info.snd_context;
2951         }
2952
2953         return 0;
2954 }
2955
2956 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2957  *
2958  * Requests that the local SCTP stack use the enclosed peer address as
2959  * the association primary.  The enclosed address must be one of the
2960  * association peer's addresses.
2961  */
2962 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2963                                         unsigned int optlen)
2964 {
2965         struct sctp_prim prim;
2966         struct sctp_transport *trans;
2967         struct sctp_af *af;
2968         int err;
2969
2970         if (optlen != sizeof(struct sctp_prim))
2971                 return -EINVAL;
2972
2973         if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2974                 return -EFAULT;
2975
2976         /* Allow security module to validate address but need address len. */
2977         af = sctp_get_af_specific(prim.ssp_addr.ss_family);
2978         if (!af)
2979                 return -EINVAL;
2980
2981         err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
2982                                          (struct sockaddr *)&prim.ssp_addr,
2983                                          af->sockaddr_len);
2984         if (err)
2985                 return err;
2986
2987         trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2988         if (!trans)
2989                 return -EINVAL;
2990
2991         sctp_assoc_set_primary(trans->asoc, trans);
2992
2993         return 0;
2994 }
2995
2996 /*
2997  * 7.1.5 SCTP_NODELAY
2998  *
2999  * Turn on/off any Nagle-like algorithm.  This means that packets are
3000  * generally sent as soon as possible and no unnecessary delays are
3001  * introduced, at the cost of more packets in the network.  Expects an
3002  *  integer boolean flag.
3003  */
3004 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
3005                                    unsigned int optlen)
3006 {
3007         int val;
3008
3009         if (optlen < sizeof(int))
3010                 return -EINVAL;
3011         if (get_user(val, (int __user *)optval))
3012                 return -EFAULT;
3013
3014         sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3015         return 0;
3016 }
3017
3018 /*
3019  *
3020  * 7.1.1 SCTP_RTOINFO
3021  *
3022  * The protocol parameters used to initialize and bound retransmission
3023  * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3024  * and modify these parameters.
3025  * All parameters are time values, in milliseconds.  A value of 0, when
3026  * modifying the parameters, indicates that the current value should not
3027  * be changed.
3028  *
3029  */
3030 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3031 {
3032         struct sctp_rtoinfo rtoinfo;
3033         struct sctp_association *asoc;
3034         unsigned long rto_min, rto_max;
3035         struct sctp_sock *sp = sctp_sk(sk);
3036
3037         if (optlen != sizeof (struct sctp_rtoinfo))
3038                 return -EINVAL;
3039
3040         if (copy_from_user(&rtoinfo, optval, optlen))
3041                 return -EFAULT;
3042
3043         asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3044
3045         /* Set the values to the specific association */
3046         if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3047                 return -EINVAL;
3048
3049         rto_max = rtoinfo.srto_max;
3050         rto_min = rtoinfo.srto_min;
3051
3052         if (rto_max)
3053                 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3054         else
3055                 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3056
3057         if (rto_min)
3058                 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3059         else
3060                 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3061
3062         if (rto_min > rto_max)
3063                 return -EINVAL;
3064
3065         if (asoc) {
3066                 if (rtoinfo.srto_initial != 0)
3067                         asoc->rto_initial =
3068                                 msecs_to_jiffies(rtoinfo.srto_initial);
3069                 asoc->rto_max = rto_max;
3070                 asoc->rto_min = rto_min;
3071         } else {
3072                 /* If there is no association or the association-id = 0
3073                  * set the values to the endpoint.
3074                  */
3075                 if (rtoinfo.srto_initial != 0)
3076                         sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3077                 sp->rtoinfo.srto_max = rto_max;
3078                 sp->rtoinfo.srto_min = rto_min;
3079         }
3080
3081         return 0;
3082 }
3083
3084 /*
3085  *
3086  * 7.1.2 SCTP_ASSOCINFO
3087  *
3088  * This option is used to tune the maximum retransmission attempts
3089  * of the association.
3090  * Returns an error if the new association retransmission value is
3091  * greater than the sum of the retransmission value  of the peer.
3092  * See [SCTP] for more information.
3093  *
3094  */
3095 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3096 {
3097
3098         struct sctp_assocparams assocparams;
3099         struct sctp_association *asoc;
3100
3101         if (optlen != sizeof(struct sctp_assocparams))
3102                 return -EINVAL;
3103         if (copy_from_user(&assocparams, optval, optlen))
3104                 return -EFAULT;
3105
3106         asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3107
3108         if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3109                 return -EINVAL;
3110
3111         /* Set the values to the specific association */
3112         if (asoc) {
3113                 if (assocparams.sasoc_asocmaxrxt != 0) {
3114                         __u32 path_sum = 0;
3115                         int   paths = 0;
3116                         struct sctp_transport *peer_addr;
3117
3118                         list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3119                                         transports) {
3120                                 path_sum += peer_addr->pathmaxrxt;
3121                                 paths++;
3122                         }
3123
3124                         /* Only validate asocmaxrxt if we have more than
3125                          * one path/transport.  We do this because path
3126                          * retransmissions are only counted when we have more
3127                          * then one path.
3128                          */
3129                         if (paths > 1 &&
3130                             assocparams.sasoc_asocmaxrxt > path_sum)
3131                                 return -EINVAL;
3132
3133                         asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3134                 }
3135
3136                 if (assocparams.sasoc_cookie_life != 0)
3137                         asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3138         } else {
3139                 /* Set the values to the endpoint */
3140                 struct sctp_sock *sp = sctp_sk(sk);
3141
3142                 if (assocparams.sasoc_asocmaxrxt != 0)
3143                         sp->assocparams.sasoc_asocmaxrxt =
3144                                                 assocparams.sasoc_asocmaxrxt;
3145                 if (assocparams.sasoc_cookie_life != 0)
3146                         sp->assocparams.sasoc_cookie_life =
3147                                                 assocparams.sasoc_cookie_life;
3148         }
3149         return 0;
3150 }
3151
3152 /*
3153  * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3154  *
3155  * This socket option is a boolean flag which turns on or off mapped V4
3156  * addresses.  If this option is turned on and the socket is type
3157  * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3158  * If this option is turned off, then no mapping will be done of V4
3159  * addresses and a user will receive both PF_INET6 and PF_INET type
3160  * addresses on the socket.
3161  */
3162 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3163 {
3164         int val;
3165         struct sctp_sock *sp = sctp_sk(sk);
3166
3167         if (optlen < sizeof(int))
3168                 return -EINVAL;
3169         if (get_user(val, (int __user *)optval))
3170                 return -EFAULT;
3171         if (val)
3172                 sp->v4mapped = 1;
3173         else
3174                 sp->v4mapped = 0;
3175
3176         return 0;
3177 }
3178
3179 /*
3180  * 8.1.16.  Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3181  * This option will get or set the maximum size to put in any outgoing
3182  * SCTP DATA chunk.  If a message is larger than this size it will be
3183  * fragmented by SCTP into the specified size.  Note that the underlying
3184  * SCTP implementation may fragment into smaller sized chunks when the
3185  * PMTU of the underlying association is smaller than the value set by
3186  * the user.  The default value for this option is '0' which indicates
3187  * the user is NOT limiting fragmentation and only the PMTU will effect
3188  * SCTP's choice of DATA chunk size.  Note also that values set larger
3189  * than the maximum size of an IP datagram will effectively let SCTP
3190  * control fragmentation (i.e. the same as setting this option to 0).
3191  *
3192  * The following structure is used to access and modify this parameter:
3193  *
3194  * struct sctp_assoc_value {
3195  *   sctp_assoc_t assoc_id;
3196  *   uint32_t assoc_value;
3197  * };
3198  *
3199  * assoc_id:  This parameter is ignored for one-to-one style sockets.
3200  *    For one-to-many style sockets this parameter indicates which
3201  *    association the user is performing an action upon.  Note that if
3202  *    this field's value is zero then the endpoints default value is
3203  *    changed (effecting future associations only).
3204  * assoc_value:  This parameter specifies the maximum size in bytes.
3205  */
3206 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3207 {
3208         struct sctp_sock *sp = sctp_sk(sk);
3209         struct sctp_af *af = sp->pf->af;
3210         struct sctp_assoc_value params;
3211         struct sctp_association *asoc;
3212         int val;
3213
3214         if (optlen == sizeof(int)) {
3215                 pr_warn_ratelimited(DEPRECATED
3216                                     "%s (pid %d) "
3217                                     "Use of int in maxseg socket option.\n"
3218                                     "Use struct sctp_assoc_value instead\n",
3219                                     current->comm, task_pid_nr(current));
3220                 if (copy_from_user(&val, optval, optlen))
3221                         return -EFAULT;
3222                 params.assoc_id = 0;
3223         } else if (optlen == sizeof(struct sctp_assoc_value)) {
3224                 if (copy_from_user(&params, optval, optlen))
3225                         return -EFAULT;
3226                 val = params.assoc_value;
3227         } else {
3228                 return -EINVAL;
3229         }
3230
3231         if (val) {
3232                 int min_len, max_len;
3233
3234                 min_len = SCTP_DEFAULT_MINSEGMENT - af->net_header_len;
3235                 min_len -= af->ip_options_len(sk);
3236                 min_len -= sizeof(struct sctphdr) +
3237                            sizeof(struct sctp_data_chunk);
3238
3239                 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3240
3241                 if (val < min_len || val > max_len)
3242                         return -EINVAL;
3243         }
3244
3245         asoc = sctp_id2assoc(sk, params.assoc_id);
3246         if (asoc) {
3247                 if (val == 0) {
3248                         val = asoc->pathmtu - af->net_header_len;
3249                         val -= af->ip_options_len(sk);
3250                         val -= sizeof(struct sctphdr) +
3251                                sctp_datachk_len(&asoc->stream);
3252                 }
3253                 asoc->user_frag = val;
3254                 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
3255         } else {
3256                 if (params.assoc_id && sctp_style(sk, UDP))
3257                         return -EINVAL;
3258                 sp->user_frag = val;
3259         }
3260
3261         return 0;
3262 }
3263
3264
3265 /*
3266  *  7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3267  *
3268  *   Requests that the peer mark the enclosed address as the association
3269  *   primary. The enclosed address must be one of the association's
3270  *   locally bound addresses. The following structure is used to make a
3271  *   set primary request:
3272  */
3273 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3274                                              unsigned int optlen)
3275 {
3276         struct net *net = sock_net(sk);
3277         struct sctp_sock        *sp;
3278         struct sctp_association *asoc = NULL;
3279         struct sctp_setpeerprim prim;
3280         struct sctp_chunk       *chunk;
3281         struct sctp_af          *af;
3282         int                     err;
3283
3284         sp = sctp_sk(sk);
3285
3286         if (!net->sctp.addip_enable)
3287                 return -EPERM;
3288
3289         if (optlen != sizeof(struct sctp_setpeerprim))
3290                 return -EINVAL;
3291
3292         if (copy_from_user(&prim, optval, optlen))
3293                 return -EFAULT;
3294
3295         asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3296         if (!asoc)
3297                 return -EINVAL;
3298
3299         if (!asoc->peer.asconf_capable)
3300                 return -EPERM;
3301
3302         if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3303                 return -EPERM;
3304
3305         if (!sctp_state(asoc, ESTABLISHED))
3306                 return -ENOTCONN;
3307
3308         af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3309         if (!af)
3310                 return -EINVAL;
3311
3312         if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3313                 return -EADDRNOTAVAIL;
3314
3315         if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3316                 return -EADDRNOTAVAIL;
3317
3318         /* Allow security module to validate address. */
3319         err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3320                                          (struct sockaddr *)&prim.sspp_addr,
3321                                          af->sockaddr_len);
3322         if (err)
3323                 return err;
3324
3325         /* Create an ASCONF chunk with SET_PRIMARY parameter    */
3326         chunk = sctp_make_asconf_set_prim(asoc,
3327                                           (union sctp_addr *)&prim.sspp_addr);
3328         if (!chunk)
3329                 return -ENOMEM;
3330
3331         err = sctp_send_asconf(asoc, chunk);
3332 </