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