Merge to Fedora kernel-2.6.17-1.2187_FC5 patched with stable patch-2.6.17.13-vs2...
[linux-2.6.git] / net / sctp / socket.c
1 /* SCTP kernel reference 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 reference 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  * The SCTP reference 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  * The SCTP reference 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 <lksctp-developers@lists.sourceforge.net>
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 <piggy@acm.org>
44  *    Narasimha Budihal     <narsi@refcode.org>
45  *    Karl Knutson          <karl@athena.chicago.il.us>
46  *    Jon Grimm             <jgrimm@us.ibm.com>
47  *    Xingang Guo           <xingang.guo@intel.com>
48  *    Daisy Chang           <daisyc@us.ibm.com>
49  *    Sridhar Samudrala     <samudrala@us.ibm.com>
50  *    Inaky Perez-Gonzalez  <inaky.gonzalez@intel.com>
51  *    Ardelle Fan           <ardelle.fan@intel.com>
52  *    Ryan Layer            <rmlayer@us.ibm.com>
53  *    Anup Pemmaiah         <pemmaiah@cc.usu.edu>
54  *    Kevin Gao             <kevin.gao@intel.com>
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 #include <linux/config.h>
61 #include <linux/types.h>
62 #include <linux/kernel.h>
63 #include <linux/wait.h>
64 #include <linux/time.h>
65 #include <linux/ip.h>
66 #include <linux/capability.h>
67 #include <linux/fcntl.h>
68 #include <linux/poll.h>
69 #include <linux/init.h>
70 #include <linux/crypto.h>
71
72 #include <net/ip.h>
73 #include <net/icmp.h>
74 #include <net/route.h>
75 #include <net/ipv6.h>
76 #include <net/inet_common.h>
77
78 #include <linux/socket.h> /* for sa_family_t */
79 #include <net/sock.h>
80 #include <net/sctp/sctp.h>
81 #include <net/sctp/sm.h>
82
83 /* WARNING:  Please do not remove the SCTP_STATIC attribute to
84  * any of the functions below as they are used to export functions
85  * used by a project regression testsuite.
86  */
87
88 /* Forward declarations for internal helper functions. */
89 static int sctp_writeable(struct sock *sk);
90 static void sctp_wfree(struct sk_buff *skb);
91 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
92                                 size_t msg_len);
93 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
94 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
95 static int sctp_wait_for_accept(struct sock *sk, long timeo);
96 static void sctp_wait_for_close(struct sock *sk, long timeo);
97 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
98                                         union sctp_addr *addr, int len);
99 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
100 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
103 static int sctp_send_asconf(struct sctp_association *asoc,
104                             struct sctp_chunk *chunk);
105 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
106 static int sctp_autobind(struct sock *sk);
107 static void sctp_sock_migrate(struct sock *, struct sock *,
108                               struct sctp_association *, sctp_socket_type_t);
109 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
110
111 extern kmem_cache_t *sctp_bucket_cachep;
112
113 /* Get the sndbuf space available at the time on the association.  */
114 static inline int sctp_wspace(struct sctp_association *asoc)
115 {
116         struct sock *sk = asoc->base.sk;
117         int amt = 0;
118
119         if (asoc->ep->sndbuf_policy) {
120                 /* make sure that no association uses more than sk_sndbuf */
121                 amt = sk->sk_sndbuf - asoc->sndbuf_used;
122         } else {
123                 /* do socket level accounting */
124                 amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
125         }
126
127         if (amt < 0)
128                 amt = 0;
129
130         return amt;
131 }
132
133 /* Increment the used sndbuf space count of the corresponding association by
134  * the size of the outgoing data chunk.
135  * Also, set the skb destructor for sndbuf accounting later.
136  *
137  * Since it is always 1-1 between chunk and skb, and also a new skb is always
138  * allocated for chunk bundling in sctp_packet_transmit(), we can use the
139  * destructor in the data chunk skb for the purpose of the sndbuf space
140  * tracking.
141  */
142 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
143 {
144         struct sctp_association *asoc = chunk->asoc;
145         struct sock *sk = asoc->base.sk;
146
147         /* The sndbuf space is tracked per association.  */
148         sctp_association_hold(asoc);
149
150         skb_set_owner_w(chunk->skb, sk);
151
152         chunk->skb->destructor = sctp_wfree;
153         /* Save the chunk pointer in skb for sctp_wfree to use later.  */
154         *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
155
156         asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
157                                 sizeof(struct sk_buff) +
158                                 sizeof(struct sctp_chunk);
159
160         atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
161 }
162
163 /* Verify that this is a valid address. */
164 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
165                                    int len)
166 {
167         struct sctp_af *af;
168
169         /* Verify basic sockaddr. */
170         af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
171         if (!af)
172                 return -EINVAL;
173
174         /* Is this a valid SCTP address?  */
175         if (!af->addr_valid(addr, sctp_sk(sk), NULL))
176                 return -EINVAL;
177
178         if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
179                 return -EINVAL;
180
181         return 0;
182 }
183
184 /* Look up the association by its id.  If this is not a UDP-style
185  * socket, the ID field is always ignored.
186  */
187 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
188 {
189         struct sctp_association *asoc = NULL;
190
191         /* If this is not a UDP-style socket, assoc id should be ignored. */
192         if (!sctp_style(sk, UDP)) {
193                 /* Return NULL if the socket state is not ESTABLISHED. It
194                  * could be a TCP-style listening socket or a socket which
195                  * hasn't yet called connect() to establish an association.
196                  */
197                 if (!sctp_sstate(sk, ESTABLISHED))
198                         return NULL;
199
200                 /* Get the first and the only association from the list. */
201                 if (!list_empty(&sctp_sk(sk)->ep->asocs))
202                         asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
203                                           struct sctp_association, asocs);
204                 return asoc;
205         }
206
207         /* Otherwise this is a UDP-style socket. */
208         if (!id || (id == (sctp_assoc_t)-1))
209                 return NULL;
210
211         spin_lock_bh(&sctp_assocs_id_lock);
212         asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
213         spin_unlock_bh(&sctp_assocs_id_lock);
214
215         if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
216                 return NULL;
217
218         return asoc;
219 }
220
221 /* Look up the transport from an address and an assoc id. If both address and
222  * id are specified, the associations matching the address and the id should be
223  * the same.
224  */
225 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
226                                               struct sockaddr_storage *addr,
227                                               sctp_assoc_t id)
228 {
229         struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
230         struct sctp_transport *transport;
231         union sctp_addr *laddr = (union sctp_addr *)addr;
232
233         laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
234         addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
235                                                (union sctp_addr *)addr,
236                                                &transport);
237         laddr->v4.sin_port = htons(laddr->v4.sin_port);
238
239         if (!addr_asoc)
240                 return NULL;
241
242         id_asoc = sctp_id2assoc(sk, id);
243         if (id_asoc && (id_asoc != addr_asoc))
244                 return NULL;
245
246         sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
247                                                 (union sctp_addr *)addr);
248
249         return transport;
250 }
251
252 /* API 3.1.2 bind() - UDP Style Syntax
253  * The syntax of bind() is,
254  *
255  *   ret = bind(int sd, struct sockaddr *addr, int addrlen);
256  *
257  *   sd      - the socket descriptor returned by socket().
258  *   addr    - the address structure (struct sockaddr_in or struct
259  *             sockaddr_in6 [RFC 2553]),
260  *   addr_len - the size of the address structure.
261  */
262 SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
263 {
264         int retval = 0;
265
266         sctp_lock_sock(sk);
267
268         SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
269                           sk, addr, addr_len);
270
271         /* Disallow binding twice. */
272         if (!sctp_sk(sk)->ep->base.bind_addr.port)
273                 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
274                                       addr_len);
275         else
276                 retval = -EINVAL;
277
278         sctp_release_sock(sk);
279
280         return retval;
281 }
282
283 static long sctp_get_port_local(struct sock *, union sctp_addr *);
284
285 /* Verify this is a valid sockaddr. */
286 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
287                                         union sctp_addr *addr, int len)
288 {
289         struct sctp_af *af;
290
291         /* Check minimum size.  */
292         if (len < sizeof (struct sockaddr))
293                 return NULL;
294
295         /* Does this PF support this AF? */
296         if (!opt->pf->af_supported(addr->sa.sa_family, opt))
297                 return NULL;
298
299         /* If we get this far, af is valid. */
300         af = sctp_get_af_specific(addr->sa.sa_family);
301
302         if (len < af->sockaddr_len)
303                 return NULL;
304
305         return af;
306 }
307
308 /* Bind a local address either to an endpoint or to an association.  */
309 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
310 {
311         struct sctp_sock *sp = sctp_sk(sk);
312         struct sctp_endpoint *ep = sp->ep;
313         struct sctp_bind_addr *bp = &ep->base.bind_addr;
314         struct sctp_af *af;
315         unsigned short snum;
316         int ret = 0;
317
318         /* Common sockaddr verification. */
319         af = sctp_sockaddr_af(sp, addr, len);
320         if (!af) {
321                 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
322                                   sk, addr, len);
323                 return -EINVAL;
324         }
325
326         snum = ntohs(addr->v4.sin_port);
327
328         SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
329                                  ", port: %d, new port: %d, len: %d)\n",
330                                  sk,
331                                  addr,
332                                  bp->port, snum,
333                                  len);
334
335         /* PF specific bind() address verification. */
336         if (!sp->pf->bind_verify(sp, addr))
337                 return -EADDRNOTAVAIL;
338
339         /* We must either be unbound, or bind to the same port.  */
340         if (bp->port && (snum != bp->port)) {
341                 SCTP_DEBUG_PRINTK("sctp_do_bind:"
342                                   " New port %d does not match existing port "
343                                   "%d.\n", snum, bp->port);
344                 return -EINVAL;
345         }
346
347         if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
348                 return -EACCES;
349
350         /* Make sure we are allowed to bind here.
351          * The function sctp_get_port_local() does duplicate address
352          * detection.
353          */
354         if ((ret = sctp_get_port_local(sk, addr))) {
355                 if (ret == (long) sk) {
356                         /* This endpoint has a conflicting address. */
357                         return -EINVAL;
358                 } else {
359                         return -EADDRINUSE;
360                 }
361         }
362
363         /* Refresh ephemeral port.  */
364         if (!bp->port)
365                 bp->port = inet_sk(sk)->num;
366
367         /* Add the address to the bind address list.  */
368         sctp_local_bh_disable();
369         sctp_write_lock(&ep->base.addr_lock);
370
371         /* Use GFP_ATOMIC since BHs are disabled.  */
372         addr->v4.sin_port = ntohs(addr->v4.sin_port);
373         ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
374         addr->v4.sin_port = htons(addr->v4.sin_port);
375         sctp_write_unlock(&ep->base.addr_lock);
376         sctp_local_bh_enable();
377
378         /* Copy back into socket for getsockname() use. */
379         if (!ret) {
380                 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
381                 af->to_sk_saddr(addr, sk);
382         }
383
384         return ret;
385 }
386
387  /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
388  *
389  * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged 
390  * at any one time.  If a sender, after sending an ASCONF chunk, decides
391  * it needs to transfer another ASCONF Chunk, it MUST wait until the 
392  * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
393  * subsequent ASCONF. Note this restriction binds each side, so at any 
394  * time two ASCONF may be in-transit on any given association (one sent 
395  * from each endpoint).
396  */
397 static int sctp_send_asconf(struct sctp_association *asoc,
398                             struct sctp_chunk *chunk)
399 {
400         int             retval = 0;
401
402         /* If there is an outstanding ASCONF chunk, queue it for later
403          * transmission.
404          */     
405         if (asoc->addip_last_asconf) {
406                 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
407                 goto out;       
408         }
409
410         /* Hold the chunk until an ASCONF_ACK is received. */
411         sctp_chunk_hold(chunk);
412         retval = sctp_primitive_ASCONF(asoc, chunk);
413         if (retval)
414                 sctp_chunk_free(chunk);
415         else
416                 asoc->addip_last_asconf = chunk;
417
418 out:
419         return retval;
420 }
421
422 /* Add a list of addresses as bind addresses to local endpoint or
423  * association.
424  *
425  * Basically run through each address specified in the addrs/addrcnt
426  * array/length pair, determine if it is IPv6 or IPv4 and call
427  * sctp_do_bind() on it.
428  *
429  * If any of them fails, then the operation will be reversed and the
430  * ones that were added will be removed.
431  *
432  * Only sctp_setsockopt_bindx() is supposed to call this function.
433  */
434 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
435 {
436         int cnt;
437         int retval = 0;
438         void *addr_buf;
439         struct sockaddr *sa_addr;
440         struct sctp_af *af;
441
442         SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
443                           sk, addrs, addrcnt);
444
445         addr_buf = addrs;
446         for (cnt = 0; cnt < addrcnt; cnt++) {
447                 /* The list may contain either IPv4 or IPv6 address;
448                  * determine the address length for walking thru the list.
449                  */
450                 sa_addr = (struct sockaddr *)addr_buf;
451                 af = sctp_get_af_specific(sa_addr->sa_family);
452                 if (!af) {
453                         retval = -EINVAL;
454                         goto err_bindx_add;
455                 }
456
457                 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, 
458                                       af->sockaddr_len);
459
460                 addr_buf += af->sockaddr_len;
461
462 err_bindx_add:
463                 if (retval < 0) {
464                         /* Failed. Cleanup the ones that have been added */
465                         if (cnt > 0)
466                                 sctp_bindx_rem(sk, addrs, cnt);
467                         return retval;
468                 }
469         }
470
471         return retval;
472 }
473
474 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
475  * associations that are part of the endpoint indicating that a list of local
476  * addresses are added to the endpoint.
477  *
478  * If any of the addresses is already in the bind address list of the 
479  * association, we do not send the chunk for that association.  But it will not
480  * affect other associations.
481  *
482  * Only sctp_setsockopt_bindx() is supposed to call this function.
483  */
484 static int sctp_send_asconf_add_ip(struct sock          *sk, 
485                                    struct sockaddr      *addrs,
486                                    int                  addrcnt)
487 {
488         struct sctp_sock                *sp;
489         struct sctp_endpoint            *ep;
490         struct sctp_association         *asoc;
491         struct sctp_bind_addr           *bp;
492         struct sctp_chunk               *chunk;
493         struct sctp_sockaddr_entry      *laddr;
494         union sctp_addr                 *addr;
495         void                            *addr_buf;
496         struct sctp_af                  *af;
497         struct list_head                *pos;
498         struct list_head                *p;
499         int                             i;
500         int                             retval = 0;
501
502         if (!sctp_addip_enable)
503                 return retval;
504
505         sp = sctp_sk(sk);
506         ep = sp->ep;
507
508         SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
509                           __FUNCTION__, sk, addrs, addrcnt);
510
511         list_for_each(pos, &ep->asocs) {
512                 asoc = list_entry(pos, struct sctp_association, asocs);
513
514                 if (!asoc->peer.asconf_capable)
515                         continue;
516
517                 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
518                         continue;
519
520                 if (!sctp_state(asoc, ESTABLISHED))
521                         continue;
522
523                 /* Check if any address in the packed array of addresses is
524                  * in the bind address list of the association. If so, 
525                  * do not send the asconf chunk to its peer, but continue with 
526                  * other associations.
527                  */
528                 addr_buf = addrs;
529                 for (i = 0; i < addrcnt; i++) {
530                         addr = (union sctp_addr *)addr_buf;
531                         af = sctp_get_af_specific(addr->v4.sin_family);
532                         if (!af) {
533                                 retval = -EINVAL;
534                                 goto out;
535                         }
536
537                         if (sctp_assoc_lookup_laddr(asoc, addr))
538                                 break;
539
540                         addr_buf += af->sockaddr_len;
541                 }
542                 if (i < addrcnt)
543                         continue;
544
545                 /* Use the first address in bind addr list of association as
546                  * Address Parameter of ASCONF CHUNK.
547                  */
548                 sctp_read_lock(&asoc->base.addr_lock);
549                 bp = &asoc->base.bind_addr;
550                 p = bp->address_list.next;
551                 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
552                 sctp_read_unlock(&asoc->base.addr_lock);
553
554                 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
555                                                    addrcnt, SCTP_PARAM_ADD_IP);
556                 if (!chunk) {
557                         retval = -ENOMEM;
558                         goto out;
559                 }
560
561                 retval = sctp_send_asconf(asoc, chunk);
562
563                 /* FIXME: After sending the add address ASCONF chunk, we
564                  * cannot append the address to the association's binding
565                  * address list, because the new address may be used as the
566                  * source of a message sent to the peer before the ASCONF
567                  * chunk is received by the peer.  So we should wait until
568                  * ASCONF_ACK is received.
569                  */
570         }
571
572 out:
573         return retval;
574 }
575
576 /* Remove a list of addresses from bind addresses list.  Do not remove the
577  * last address.
578  *
579  * Basically run through each address specified in the addrs/addrcnt
580  * array/length pair, determine if it is IPv6 or IPv4 and call
581  * sctp_del_bind() on it.
582  *
583  * If any of them fails, then the operation will be reversed and the
584  * ones that were removed will be added back.
585  *
586  * At least one address has to be left; if only one address is
587  * available, the operation will return -EBUSY.
588  *
589  * Only sctp_setsockopt_bindx() is supposed to call this function.
590  */
591 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
592 {
593         struct sctp_sock *sp = sctp_sk(sk);
594         struct sctp_endpoint *ep = sp->ep;
595         int cnt;
596         struct sctp_bind_addr *bp = &ep->base.bind_addr;
597         int retval = 0;
598         union sctp_addr saveaddr;
599         void *addr_buf;
600         struct sockaddr *sa_addr;
601         struct sctp_af *af;
602
603         SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
604                           sk, addrs, addrcnt);
605
606         addr_buf = addrs;
607         for (cnt = 0; cnt < addrcnt; cnt++) {
608                 /* If the bind address list is empty or if there is only one
609                  * bind address, there is nothing more to be removed (we need
610                  * at least one address here).
611                  */
612                 if (list_empty(&bp->address_list) ||
613                     (sctp_list_single_entry(&bp->address_list))) {
614                         retval = -EBUSY;
615                         goto err_bindx_rem;
616                 }
617
618                 /* The list may contain either IPv4 or IPv6 address;
619                  * determine the address length to copy the address to
620                  * saveaddr. 
621                  */
622                 sa_addr = (struct sockaddr *)addr_buf;
623                 af = sctp_get_af_specific(sa_addr->sa_family);
624                 if (!af) {
625                         retval = -EINVAL;
626                         goto err_bindx_rem;
627                 }
628                 memcpy(&saveaddr, sa_addr, af->sockaddr_len); 
629                 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
630                 if (saveaddr.v4.sin_port != bp->port) {
631                         retval = -EINVAL;
632                         goto err_bindx_rem;
633                 }
634
635                 /* FIXME - There is probably a need to check if sk->sk_saddr and
636                  * sk->sk_rcv_addr are currently set to one of the addresses to
637                  * be removed. This is something which needs to be looked into
638                  * when we are fixing the outstanding issues with multi-homing
639                  * socket routing and failover schemes. Refer to comments in
640                  * sctp_do_bind(). -daisy
641                  */
642                 sctp_local_bh_disable();
643                 sctp_write_lock(&ep->base.addr_lock);
644
645                 retval = sctp_del_bind_addr(bp, &saveaddr);
646
647                 sctp_write_unlock(&ep->base.addr_lock);
648                 sctp_local_bh_enable();
649
650                 addr_buf += af->sockaddr_len;
651 err_bindx_rem:
652                 if (retval < 0) {
653                         /* Failed. Add the ones that has been removed back */
654                         if (cnt > 0)
655                                 sctp_bindx_add(sk, addrs, cnt);
656                         return retval;
657                 }
658         }
659
660         return retval;
661 }
662
663 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
664  * the associations that are part of the endpoint indicating that a list of
665  * local addresses are removed from the endpoint.
666  *
667  * If any of the addresses is already in the bind address list of the 
668  * association, we do not send the chunk for that association.  But it will not
669  * affect other associations.
670  *
671  * Only sctp_setsockopt_bindx() is supposed to call this function.
672  */
673 static int sctp_send_asconf_del_ip(struct sock          *sk,
674                                    struct sockaddr      *addrs,
675                                    int                  addrcnt)
676 {
677         struct sctp_sock        *sp;
678         struct sctp_endpoint    *ep;
679         struct sctp_association *asoc;
680         struct sctp_bind_addr   *bp;
681         struct sctp_chunk       *chunk;
682         union sctp_addr         *laddr;
683         void                    *addr_buf;
684         struct sctp_af          *af;
685         struct list_head        *pos;
686         int                     i;
687         int                     retval = 0;
688
689         if (!sctp_addip_enable)
690                 return retval;
691
692         sp = sctp_sk(sk);
693         ep = sp->ep;
694
695         SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
696                           __FUNCTION__, sk, addrs, addrcnt);
697
698         list_for_each(pos, &ep->asocs) {
699                 asoc = list_entry(pos, struct sctp_association, asocs);
700
701                 if (!asoc->peer.asconf_capable)
702                         continue;
703
704                 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
705                         continue;
706
707                 if (!sctp_state(asoc, ESTABLISHED))
708                         continue;
709
710                 /* Check if any address in the packed array of addresses is
711                  * not present in the bind address list of the association.
712                  * If so, do not send the asconf chunk to its peer, but
713                  * continue with other associations.
714                  */
715                 addr_buf = addrs;
716                 for (i = 0; i < addrcnt; i++) {
717                         laddr = (union sctp_addr *)addr_buf;
718                         af = sctp_get_af_specific(laddr->v4.sin_family);
719                         if (!af) {
720                                 retval = -EINVAL;
721                                 goto out;
722                         }
723
724                         if (!sctp_assoc_lookup_laddr(asoc, laddr))
725                                 break;
726
727                         addr_buf += af->sockaddr_len;
728                 }
729                 if (i < addrcnt)
730                         continue;
731
732                 /* Find one address in the association's bind address list
733                  * that is not in the packed array of addresses. This is to
734                  * make sure that we do not delete all the addresses in the
735                  * association.
736                  */
737                 sctp_read_lock(&asoc->base.addr_lock);
738                 bp = &asoc->base.bind_addr;
739                 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
740                                                addrcnt, sp);
741                 sctp_read_unlock(&asoc->base.addr_lock);
742                 if (!laddr)
743                         continue;
744
745                 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
746                                                    SCTP_PARAM_DEL_IP);
747                 if (!chunk) {
748                         retval = -ENOMEM;
749                         goto out;
750                 }
751
752                 retval = sctp_send_asconf(asoc, chunk);
753
754                 /* FIXME: After sending the delete address ASCONF chunk, we
755                  * cannot remove the addresses from the association's bind
756                  * address list, because there maybe some packet send to
757                  * the delete addresses, so we should wait until ASCONF_ACK
758                  * packet is received.
759                  */
760         }
761 out:
762         return retval;
763 }
764
765 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
766  *
767  * API 8.1
768  * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
769  *                int flags);
770  *
771  * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
772  * If the sd is an IPv6 socket, the addresses passed can either be IPv4
773  * or IPv6 addresses.
774  *
775  * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
776  * Section 3.1.2 for this usage.
777  *
778  * addrs is a pointer to an array of one or more socket addresses. Each
779  * address is contained in its appropriate structure (i.e. struct
780  * sockaddr_in or struct sockaddr_in6) the family of the address type
781  * must be used to distengish the address length (note that this
782  * representation is termed a "packed array" of addresses). The caller
783  * specifies the number of addresses in the array with addrcnt.
784  *
785  * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
786  * -1, and sets errno to the appropriate error code.
787  *
788  * For SCTP, the port given in each socket address must be the same, or
789  * sctp_bindx() will fail, setting errno to EINVAL.
790  *
791  * The flags parameter is formed from the bitwise OR of zero or more of
792  * the following currently defined flags:
793  *
794  * SCTP_BINDX_ADD_ADDR
795  *
796  * SCTP_BINDX_REM_ADDR
797  *
798  * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
799  * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
800  * addresses from the association. The two flags are mutually exclusive;
801  * if both are given, sctp_bindx() will fail with EINVAL. A caller may
802  * not remove all addresses from an association; sctp_bindx() will
803  * reject such an attempt with EINVAL.
804  *
805  * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
806  * additional addresses with an endpoint after calling bind().  Or use
807  * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
808  * socket is associated with so that no new association accepted will be
809  * associated with those addresses. If the endpoint supports dynamic
810  * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
811  * endpoint to send the appropriate message to the peer to change the
812  * peers address lists.
813  *
814  * Adding and removing addresses from a connected association is
815  * optional functionality. Implementations that do not support this
816  * functionality should return EOPNOTSUPP.
817  *
818  * Basically do nothing but copying the addresses from user to kernel
819  * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
820  * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
821  * from userspace.
822  *
823  * We don't use copy_from_user() for optimization: we first do the
824  * sanity checks (buffer size -fast- and access check-healthy
825  * pointer); if all of those succeed, then we can alloc the memory
826  * (expensive operation) needed to copy the data to kernel. Then we do
827  * the copying without checking the user space area
828  * (__copy_from_user()).
829  *
830  * On exit there is no need to do sockfd_put(), sys_setsockopt() does
831  * it.
832  *
833  * sk        The sk of the socket
834  * addrs     The pointer to the addresses in user land
835  * addrssize Size of the addrs buffer
836  * op        Operation to perform (add or remove, see the flags of
837  *           sctp_bindx)
838  *
839  * Returns 0 if ok, <0 errno code on error.
840  */
841 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
842                                       struct sockaddr __user *addrs,
843                                       int addrs_size, int op)
844 {
845         struct sockaddr *kaddrs;
846         int err;
847         int addrcnt = 0;
848         int walk_size = 0;
849         struct sockaddr *sa_addr;
850         void *addr_buf;
851         struct sctp_af *af;
852
853         SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
854                           " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
855
856         if (unlikely(addrs_size <= 0))
857                 return -EINVAL;
858
859         /* Check the user passed a healthy pointer.  */
860         if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
861                 return -EFAULT;
862
863         /* Alloc space for the address array in kernel memory.  */
864         kaddrs = kmalloc(addrs_size, GFP_KERNEL);
865         if (unlikely(!kaddrs))
866                 return -ENOMEM;
867
868         if (__copy_from_user(kaddrs, addrs, addrs_size)) {
869                 kfree(kaddrs);
870                 return -EFAULT;
871         }
872
873         /* Walk through the addrs buffer and count the number of addresses. */ 
874         addr_buf = kaddrs;
875         while (walk_size < addrs_size) {
876                 sa_addr = (struct sockaddr *)addr_buf;
877                 af = sctp_get_af_specific(sa_addr->sa_family);
878
879                 /* If the address family is not supported or if this address
880                  * causes the address buffer to overflow return EINVAL.
881                  */ 
882                 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
883                         kfree(kaddrs);
884                         return -EINVAL;
885                 }
886                 addrcnt++;
887                 addr_buf += af->sockaddr_len;
888                 walk_size += af->sockaddr_len;
889         }
890
891         /* Do the work. */
892         switch (op) {
893         case SCTP_BINDX_ADD_ADDR:
894                 err = sctp_bindx_add(sk, kaddrs, addrcnt);
895                 if (err)
896                         goto out;
897                 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
898                 break;
899
900         case SCTP_BINDX_REM_ADDR:
901                 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
902                 if (err)
903                         goto out;
904                 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
905                 break;
906
907         default:
908                 err = -EINVAL;
909                 break;
910         };
911
912 out:
913         kfree(kaddrs);
914
915         return err;
916 }
917
918 /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
919  *
920  * Common routine for handling connect() and sctp_connectx().
921  * Connect will come in with just a single address.
922  */
923 static int __sctp_connect(struct sock* sk,
924                           struct sockaddr *kaddrs,
925                           int addrs_size)
926 {
927         struct sctp_sock *sp;
928         struct sctp_endpoint *ep;
929         struct sctp_association *asoc = NULL;
930         struct sctp_association *asoc2;
931         struct sctp_transport *transport;
932         union sctp_addr to;
933         struct sctp_af *af;
934         sctp_scope_t scope;
935         long timeo;
936         int err = 0;
937         int addrcnt = 0;
938         int walk_size = 0;
939         struct sockaddr *sa_addr;
940         void *addr_buf;
941
942         sp = sctp_sk(sk);
943         ep = sp->ep;
944
945         /* connect() cannot be done on a socket that is already in ESTABLISHED
946          * state - UDP-style peeled off socket or a TCP-style socket that
947          * is already connected.
948          * It cannot be done even on a TCP-style listening socket.
949          */
950         if (sctp_sstate(sk, ESTABLISHED) ||
951             (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
952                 err = -EISCONN;
953                 goto out_free;
954         }
955
956         /* Walk through the addrs buffer and count the number of addresses. */
957         addr_buf = kaddrs;
958         while (walk_size < addrs_size) {
959                 sa_addr = (struct sockaddr *)addr_buf;
960                 af = sctp_get_af_specific(sa_addr->sa_family);
961
962                 /* If the address family is not supported or if this address
963                  * causes the address buffer to overflow return EINVAL.
964                  */
965                 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
966                         err = -EINVAL;
967                         goto out_free;
968                 }
969
970                 err = sctp_verify_addr(sk, (union sctp_addr *)sa_addr,
971                                        af->sockaddr_len);
972                 if (err)
973                         goto out_free;
974
975                 memcpy(&to, sa_addr, af->sockaddr_len);
976                 to.v4.sin_port = ntohs(to.v4.sin_port);
977
978                 /* Check if there already is a matching association on the
979                  * endpoint (other than the one created here).
980                  */
981                 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
982                 if (asoc2 && asoc2 != asoc) {
983                         if (asoc2->state >= SCTP_STATE_ESTABLISHED)
984                                 err = -EISCONN;
985                         else
986                                 err = -EALREADY;
987                         goto out_free;
988                 }
989
990                 /* If we could not find a matching association on the endpoint,
991                  * make sure that there is no peeled-off association matching
992                  * the peer address even on another socket.
993                  */
994                 if (sctp_endpoint_is_peeled_off(ep, &to)) {
995                         err = -EADDRNOTAVAIL;
996                         goto out_free;
997                 }
998
999                 if (!asoc) {
1000                         /* If a bind() or sctp_bindx() is not called prior to
1001                          * an sctp_connectx() call, the system picks an
1002                          * ephemeral port and will choose an address set
1003                          * equivalent to binding with a wildcard address.
1004                          */
1005                         if (!ep->base.bind_addr.port) {
1006                                 if (sctp_autobind(sk)) {
1007                                         err = -EAGAIN;
1008                                         goto out_free;
1009                                 }
1010                         } else {
1011                                 /*
1012                                  * If an unprivileged user inherits a 1-many 
1013                                  * style socket with open associations on a 
1014                                  * privileged port, it MAY be permitted to 
1015                                  * accept new associations, but it SHOULD NOT 
1016                                  * be permitted to open new associations.
1017                                  */
1018                                 if (ep->base.bind_addr.port < PROT_SOCK &&
1019                                     !capable(CAP_NET_BIND_SERVICE)) {
1020                                         err = -EACCES;
1021                                         goto out_free;
1022                                 }
1023                         }
1024
1025                         scope = sctp_scope(&to);
1026                         asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1027                         if (!asoc) {
1028                                 err = -ENOMEM;
1029                                 goto out_free;
1030                         }
1031                 }
1032
1033                 /* Prime the peer's transport structures.  */
1034                 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1035                                                 SCTP_UNKNOWN);
1036                 if (!transport) {
1037                         err = -ENOMEM;
1038                         goto out_free;
1039                 }
1040
1041                 addrcnt++;
1042                 addr_buf += af->sockaddr_len;
1043                 walk_size += af->sockaddr_len;
1044         }
1045
1046         err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1047         if (err < 0) {
1048                 goto out_free;
1049         }
1050
1051         err = sctp_primitive_ASSOCIATE(asoc, NULL);
1052         if (err < 0) {
1053                 goto out_free;
1054         }
1055
1056         /* Initialize sk's dport and daddr for getpeername() */
1057         inet_sk(sk)->dport = htons(asoc->peer.port);
1058         af = sctp_get_af_specific(to.sa.sa_family);
1059         af->to_sk_daddr(&to, sk);
1060         sk->sk_err = 0;
1061
1062         timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
1063         err = sctp_wait_for_connect(asoc, &timeo);
1064
1065         /* Don't free association on exit. */
1066         asoc = NULL;
1067
1068 out_free:
1069
1070         SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
1071                           " kaddrs: %p err: %d\n",
1072                           asoc, kaddrs, err);
1073         if (asoc)
1074                 sctp_association_free(asoc);
1075         return err;
1076 }
1077
1078 /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1079  *
1080  * API 8.9
1081  * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
1082  *
1083  * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1084  * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1085  * or IPv6 addresses.
1086  *
1087  * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1088  * Section 3.1.2 for this usage.
1089  *
1090  * addrs is a pointer to an array of one or more socket addresses. Each
1091  * address is contained in its appropriate structure (i.e. struct
1092  * sockaddr_in or struct sockaddr_in6) the family of the address type
1093  * must be used to distengish the address length (note that this
1094  * representation is termed a "packed array" of addresses). The caller
1095  * specifies the number of addresses in the array with addrcnt.
1096  *
1097  * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
1098  * -1, and sets errno to the appropriate error code.
1099  *
1100  * For SCTP, the port given in each socket address must be the same, or
1101  * sctp_connectx() will fail, setting errno to EINVAL.
1102  *
1103  * An application can use sctp_connectx to initiate an association with
1104  * an endpoint that is multi-homed.  Much like sctp_bindx() this call
1105  * allows a caller to specify multiple addresses at which a peer can be
1106  * reached.  The way the SCTP stack uses the list of addresses to set up
1107  * the association is implementation dependant.  This function only
1108  * specifies that the stack will try to make use of all the addresses in
1109  * the list when needed.
1110  *
1111  * Note that the list of addresses passed in is only used for setting up
1112  * the association.  It does not necessarily equal the set of addresses
1113  * the peer uses for the resulting association.  If the caller wants to
1114  * find out the set of peer addresses, it must use sctp_getpaddrs() to
1115  * retrieve them after the association has been set up.
1116  *
1117  * Basically do nothing but copying the addresses from user to kernel
1118  * land and invoking either sctp_connectx(). This is used for tunneling
1119  * the sctp_connectx() request through sctp_setsockopt() from userspace.
1120  *
1121  * We don't use copy_from_user() for optimization: we first do the
1122  * sanity checks (buffer size -fast- and access check-healthy
1123  * pointer); if all of those succeed, then we can alloc the memory
1124  * (expensive operation) needed to copy the data to kernel. Then we do
1125  * the copying without checking the user space area
1126  * (__copy_from_user()).
1127  *
1128  * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1129  * it.
1130  *
1131  * sk        The sk of the socket
1132  * addrs     The pointer to the addresses in user land
1133  * addrssize Size of the addrs buffer
1134  *
1135  * Returns 0 if ok, <0 errno code on error.
1136  */
1137 SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1138                                       struct sockaddr __user *addrs,
1139                                       int addrs_size)
1140 {
1141         int err = 0;
1142         struct sockaddr *kaddrs;
1143
1144         SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
1145                           __FUNCTION__, sk, addrs, addrs_size);
1146
1147         if (unlikely(addrs_size <= 0))
1148                 return -EINVAL;
1149
1150         /* Check the user passed a healthy pointer.  */
1151         if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1152                 return -EFAULT;
1153
1154         /* Alloc space for the address array in kernel memory.  */
1155         kaddrs = kmalloc(addrs_size, GFP_KERNEL);
1156         if (unlikely(!kaddrs))
1157                 return -ENOMEM;
1158
1159         if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1160                 err = -EFAULT;
1161         } else {
1162                 err = __sctp_connect(sk, kaddrs, addrs_size);
1163         }
1164
1165         kfree(kaddrs);
1166         return err;
1167 }
1168
1169 /* API 3.1.4 close() - UDP Style Syntax
1170  * Applications use close() to perform graceful shutdown (as described in
1171  * Section 10.1 of [SCTP]) on ALL the associations currently represented
1172  * by a UDP-style socket.
1173  *
1174  * The syntax is
1175  *
1176  *   ret = close(int sd);
1177  *
1178  *   sd      - the socket descriptor of the associations to be closed.
1179  *
1180  * To gracefully shutdown a specific association represented by the
1181  * UDP-style socket, an application should use the sendmsg() call,
1182  * passing no user data, but including the appropriate flag in the
1183  * ancillary data (see Section xxxx).
1184  *
1185  * If sd in the close() call is a branched-off socket representing only
1186  * one association, the shutdown is performed on that association only.
1187  *
1188  * 4.1.6 close() - TCP Style Syntax
1189  *
1190  * Applications use close() to gracefully close down an association.
1191  *
1192  * The syntax is:
1193  *
1194  *    int close(int sd);
1195  *
1196  *      sd      - the socket descriptor of the association to be closed.
1197  *
1198  * After an application calls close() on a socket descriptor, no further
1199  * socket operations will succeed on that descriptor.
1200  *
1201  * API 7.1.4 SO_LINGER
1202  *
1203  * An application using the TCP-style socket can use this option to
1204  * perform the SCTP ABORT primitive.  The linger option structure is:
1205  *
1206  *  struct  linger {
1207  *     int     l_onoff;                // option on/off
1208  *     int     l_linger;               // linger time
1209  * };
1210  *
1211  * To enable the option, set l_onoff to 1.  If the l_linger value is set
1212  * to 0, calling close() is the same as the ABORT primitive.  If the
1213  * value is set to a negative value, the setsockopt() call will return
1214  * an error.  If the value is set to a positive value linger_time, the
1215  * close() can be blocked for at most linger_time ms.  If the graceful
1216  * shutdown phase does not finish during this period, close() will
1217  * return but the graceful shutdown phase continues in the system.
1218  */
1219 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1220 {
1221         struct sctp_endpoint *ep;
1222         struct sctp_association *asoc;
1223         struct list_head *pos, *temp;
1224
1225         SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1226
1227         sctp_lock_sock(sk);
1228         sk->sk_shutdown = SHUTDOWN_MASK;
1229
1230         ep = sctp_sk(sk)->ep;
1231
1232         /* Walk all associations on an endpoint.  */
1233         list_for_each_safe(pos, temp, &ep->asocs) {
1234                 asoc = list_entry(pos, struct sctp_association, asocs);
1235
1236                 if (sctp_style(sk, TCP)) {
1237                         /* A closed association can still be in the list if
1238                          * it belongs to a TCP-style listening socket that is
1239                          * not yet accepted. If so, free it. If not, send an
1240                          * ABORT or SHUTDOWN based on the linger options.
1241                          */
1242                         if (sctp_state(asoc, CLOSED)) {
1243                                 sctp_unhash_established(asoc);
1244                                 sctp_association_free(asoc);
1245                                 continue;
1246                         }
1247                 }
1248
1249                 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1250                         struct sctp_chunk *chunk;
1251
1252                         chunk = sctp_make_abort_user(asoc, NULL, 0);
1253                         if (chunk)
1254                                 sctp_primitive_ABORT(asoc, chunk);
1255                 } else
1256                         sctp_primitive_SHUTDOWN(asoc, NULL);
1257         }
1258
1259         /* Clean up any skbs sitting on the receive queue.  */
1260         sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1261         sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1262
1263         /* On a TCP-style socket, block for at most linger_time if set. */
1264         if (sctp_style(sk, TCP) && timeout)
1265                 sctp_wait_for_close(sk, timeout);
1266
1267         /* This will run the backlog queue.  */
1268         sctp_release_sock(sk);
1269
1270         /* Supposedly, no process has access to the socket, but
1271          * the net layers still may.
1272          */
1273         sctp_local_bh_disable();
1274         sctp_bh_lock_sock(sk);
1275
1276         /* Hold the sock, since sk_common_release() will put sock_put()
1277          * and we have just a little more cleanup.
1278          */
1279         sock_hold(sk);
1280         sk_common_release(sk);
1281
1282         sctp_bh_unlock_sock(sk);
1283         sctp_local_bh_enable();
1284
1285         sock_put(sk);
1286
1287         SCTP_DBG_OBJCNT_DEC(sock);
1288 }
1289
1290 /* Handle EPIPE error. */
1291 static int sctp_error(struct sock *sk, int flags, int err)
1292 {
1293         if (err == -EPIPE)
1294                 err = sock_error(sk) ? : -EPIPE;
1295         if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1296                 send_sig(SIGPIPE, current, 0);
1297         return err;
1298 }
1299
1300 /* API 3.1.3 sendmsg() - UDP Style Syntax
1301  *
1302  * An application uses sendmsg() and recvmsg() calls to transmit data to
1303  * and receive data from its peer.
1304  *
1305  *  ssize_t sendmsg(int socket, const struct msghdr *message,
1306  *                  int flags);
1307  *
1308  *  socket  - the socket descriptor of the endpoint.
1309  *  message - pointer to the msghdr structure which contains a single
1310  *            user message and possibly some ancillary data.
1311  *
1312  *            See Section 5 for complete description of the data
1313  *            structures.
1314  *
1315  *  flags   - flags sent or received with the user message, see Section
1316  *            5 for complete description of the flags.
1317  *
1318  * Note:  This function could use a rewrite especially when explicit
1319  * connect support comes in.
1320  */
1321 /* BUG:  We do not implement the equivalent of sk_stream_wait_memory(). */
1322
1323 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1324
1325 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1326                              struct msghdr *msg, size_t msg_len)
1327 {
1328         struct sctp_sock *sp;
1329         struct sctp_endpoint *ep;
1330         struct sctp_association *new_asoc=NULL, *asoc=NULL;
1331         struct sctp_transport *transport, *chunk_tp;
1332         struct sctp_chunk *chunk;
1333         union sctp_addr to;
1334         struct sockaddr *msg_name = NULL;
1335         struct sctp_sndrcvinfo default_sinfo = { 0 };
1336         struct sctp_sndrcvinfo *sinfo;
1337         struct sctp_initmsg *sinit;
1338         sctp_assoc_t associd = 0;
1339         sctp_cmsgs_t cmsgs = { NULL };
1340         int err;
1341         sctp_scope_t scope;
1342         long timeo;
1343         __u16 sinfo_flags = 0;
1344         struct sctp_datamsg *datamsg;
1345         struct list_head *pos;
1346         int msg_flags = msg->msg_flags;
1347
1348         SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1349                           sk, msg, msg_len);
1350
1351         err = 0;
1352         sp = sctp_sk(sk);
1353         ep = sp->ep;
1354
1355         SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
1356
1357         /* We cannot send a message over a TCP-style listening socket. */
1358         if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1359                 err = -EPIPE;
1360                 goto out_nounlock;
1361         }
1362
1363         /* Parse out the SCTP CMSGs.  */
1364         err = sctp_msghdr_parse(msg, &cmsgs);
1365
1366         if (err) {
1367                 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1368                 goto out_nounlock;
1369         }
1370
1371         /* Fetch the destination address for this packet.  This
1372          * address only selects the association--it is not necessarily
1373          * the address we will send to.
1374          * For a peeled-off socket, msg_name is ignored.
1375          */
1376         if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1377                 int msg_namelen = msg->msg_namelen;
1378
1379                 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1380                                        msg_namelen);
1381                 if (err)
1382                         return err;
1383
1384                 if (msg_namelen > sizeof(to))
1385                         msg_namelen = sizeof(to);
1386                 memcpy(&to, msg->msg_name, msg_namelen);
1387                 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1388                                   "0x%x:%u.\n",
1389                                   to.v4.sin_addr.s_addr, to.v4.sin_port);
1390
1391                 to.v4.sin_port = ntohs(to.v4.sin_port);
1392                 msg_name = msg->msg_name;
1393         }
1394
1395         sinfo = cmsgs.info;
1396         sinit = cmsgs.init;
1397
1398         /* Did the user specify SNDRCVINFO?  */
1399         if (sinfo) {
1400                 sinfo_flags = sinfo->sinfo_flags;
1401                 associd = sinfo->sinfo_assoc_id;
1402         }
1403
1404         SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1405                           msg_len, sinfo_flags);
1406
1407         /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1408         if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
1409                 err = -EINVAL;
1410                 goto out_nounlock;
1411         }
1412
1413         /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1414          * length messages when SCTP_EOF|SCTP_ABORT is not set.
1415          * If SCTP_ABORT is set, the message length could be non zero with
1416          * the msg_iov set to the user abort reason.
1417          */
1418         if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1419             (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
1420                 err = -EINVAL;
1421                 goto out_nounlock;
1422         }
1423
1424         /* If SCTP_ADDR_OVER is set, there must be an address
1425          * specified in msg_name.
1426          */
1427         if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
1428                 err = -EINVAL;
1429                 goto out_nounlock;
1430         }
1431
1432         transport = NULL;
1433
1434         SCTP_DEBUG_PRINTK("About to look up association.\n");
1435
1436         sctp_lock_sock(sk);
1437
1438         /* If a msg_name has been specified, assume this is to be used.  */
1439         if (msg_name) {
1440                 /* Look for a matching association on the endpoint. */
1441                 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1442                 if (!asoc) {
1443                         /* If we could not find a matching association on the
1444                          * endpoint, make sure that it is not a TCP-style
1445                          * socket that already has an association or there is
1446                          * no peeled-off association on another socket.
1447                          */
1448                         if ((sctp_style(sk, TCP) &&
1449                              sctp_sstate(sk, ESTABLISHED)) ||
1450                             sctp_endpoint_is_peeled_off(ep, &to)) {
1451                                 err = -EADDRNOTAVAIL;
1452                                 goto out_unlock;
1453                         }
1454                 }
1455         } else {
1456                 asoc = sctp_id2assoc(sk, associd);
1457                 if (!asoc) {
1458                         err = -EPIPE;
1459                         goto out_unlock;
1460                 }
1461         }
1462
1463         if (asoc) {
1464                 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1465
1466                 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1467                  * socket that has an association in CLOSED state. This can
1468                  * happen when an accepted socket has an association that is
1469                  * already CLOSED.
1470                  */
1471                 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1472                         err = -EPIPE;
1473                         goto out_unlock;
1474                 }
1475
1476                 if (sinfo_flags & SCTP_EOF) {
1477                         SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1478                                           asoc);
1479                         sctp_primitive_SHUTDOWN(asoc, NULL);
1480                         err = 0;
1481                         goto out_unlock;
1482                 }
1483                 if (sinfo_flags & SCTP_ABORT) {
1484                         struct sctp_chunk *chunk;
1485
1486                         chunk = sctp_make_abort_user(asoc, msg, msg_len);
1487                         if (!chunk) {
1488                                 err = -ENOMEM;
1489                                 goto out_unlock;
1490                         }
1491
1492                         SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1493                         sctp_primitive_ABORT(asoc, chunk);
1494                         err = 0;
1495                         goto out_unlock;
1496                 }
1497         }
1498
1499         /* Do we need to create the association?  */
1500         if (!asoc) {
1501                 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1502
1503                 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
1504                         err = -EINVAL;
1505                         goto out_unlock;
1506                 }
1507
1508                 /* Check for invalid stream against the stream counts,
1509                  * either the default or the user specified stream counts.
1510                  */
1511                 if (sinfo) {
1512                         if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1513                                 /* Check against the defaults. */
1514                                 if (sinfo->sinfo_stream >=
1515                                     sp->initmsg.sinit_num_ostreams) {
1516                                         err = -EINVAL;
1517                                         goto out_unlock;
1518                                 }
1519                         } else {
1520                                 /* Check against the requested.  */
1521                                 if (sinfo->sinfo_stream >=
1522                                     sinit->sinit_num_ostreams) {
1523                                         err = -EINVAL;
1524                                         goto out_unlock;
1525                                 }
1526                         }
1527                 }
1528
1529                 /*
1530                  * API 3.1.2 bind() - UDP Style Syntax
1531                  * If a bind() or sctp_bindx() is not called prior to a
1532                  * sendmsg() call that initiates a new association, the
1533                  * system picks an ephemeral port and will choose an address
1534                  * set equivalent to binding with a wildcard address.
1535                  */
1536                 if (!ep->base.bind_addr.port) {
1537                         if (sctp_autobind(sk)) {
1538                                 err = -EAGAIN;
1539                                 goto out_unlock;
1540                         }
1541                 } else {
1542                         /*
1543                          * If an unprivileged user inherits a one-to-many
1544                          * style socket with open associations on a privileged
1545                          * port, it MAY be permitted to accept new associations,
1546                          * but it SHOULD NOT be permitted to open new
1547                          * associations.
1548                          */
1549                         if (ep->base.bind_addr.port < PROT_SOCK &&
1550                             !capable(CAP_NET_BIND_SERVICE)) {
1551                                 err = -EACCES;
1552                                 goto out_unlock;
1553                         }
1554                 }
1555
1556                 scope = sctp_scope(&to);
1557                 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1558                 if (!new_asoc) {
1559                         err = -ENOMEM;
1560                         goto out_unlock;
1561                 }
1562                 asoc = new_asoc;
1563
1564                 /* If the SCTP_INIT ancillary data is specified, set all
1565                  * the association init values accordingly.
1566                  */
1567                 if (sinit) {
1568                         if (sinit->sinit_num_ostreams) {
1569                                 asoc->c.sinit_num_ostreams =
1570                                         sinit->sinit_num_ostreams;
1571                         }
1572                         if (sinit->sinit_max_instreams) {
1573                                 asoc->c.sinit_max_instreams =
1574                                         sinit->sinit_max_instreams;
1575                         }
1576                         if (sinit->sinit_max_attempts) {
1577                                 asoc->max_init_attempts
1578                                         = sinit->sinit_max_attempts;
1579                         }
1580                         if (sinit->sinit_max_init_timeo) {
1581                                 asoc->max_init_timeo = 
1582                                  msecs_to_jiffies(sinit->sinit_max_init_timeo);
1583                         }
1584                 }
1585
1586                 /* Prime the peer's transport structures.  */
1587                 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
1588                 if (!transport) {
1589                         err = -ENOMEM;
1590                         goto out_free;
1591                 }
1592                 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1593                 if (err < 0) {
1594                         err = -ENOMEM;
1595                         goto out_free;
1596                 }
1597         }
1598
1599         /* ASSERT: we have a valid association at this point.  */
1600         SCTP_DEBUG_PRINTK("We have a valid association.\n");
1601
1602         if (!sinfo) {
1603                 /* If the user didn't specify SNDRCVINFO, make up one with
1604                  * some defaults.
1605                  */
1606                 default_sinfo.sinfo_stream = asoc->default_stream;
1607                 default_sinfo.sinfo_flags = asoc->default_flags;
1608                 default_sinfo.sinfo_ppid = asoc->default_ppid;
1609                 default_sinfo.sinfo_context = asoc->default_context;
1610                 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1611                 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1612                 sinfo = &default_sinfo;
1613         }
1614
1615         /* API 7.1.7, the sndbuf size per association bounds the
1616          * maximum size of data that can be sent in a single send call.
1617          */
1618         if (msg_len > sk->sk_sndbuf) {
1619                 err = -EMSGSIZE;
1620                 goto out_free;
1621         }
1622
1623         /* If fragmentation is disabled and the message length exceeds the
1624          * association fragmentation point, return EMSGSIZE.  The I-D
1625          * does not specify what this error is, but this looks like
1626          * a great fit.
1627          */
1628         if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1629                 err = -EMSGSIZE;
1630                 goto out_free;
1631         }
1632
1633         if (sinfo) {
1634                 /* Check for invalid stream. */
1635                 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1636                         err = -EINVAL;
1637                         goto out_free;
1638                 }
1639         }
1640
1641         timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1642         if (!sctp_wspace(asoc)) {
1643                 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1644                 if (err)
1645                         goto out_free;
1646         }
1647
1648         /* If an address is passed with the sendto/sendmsg call, it is used
1649          * to override the primary destination address in the TCP model, or
1650          * when SCTP_ADDR_OVER flag is set in the UDP model.
1651          */
1652         if ((sctp_style(sk, TCP) && msg_name) ||
1653             (sinfo_flags & SCTP_ADDR_OVER)) {
1654                 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1655                 if (!chunk_tp) {
1656                         err = -EINVAL;
1657                         goto out_free;
1658                 }
1659         } else
1660                 chunk_tp = NULL;
1661
1662         /* Auto-connect, if we aren't connected already. */
1663         if (sctp_state(asoc, CLOSED)) {
1664                 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1665                 if (err < 0)
1666                         goto out_free;
1667                 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1668         }
1669
1670         /* Break the message into multiple chunks of maximum size. */
1671         datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1672         if (!datamsg) {
1673                 err = -ENOMEM;
1674                 goto out_free;
1675         }
1676
1677         /* Now send the (possibly) fragmented message. */
1678         list_for_each(pos, &datamsg->chunks) {
1679                 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1680                 sctp_datamsg_track(chunk);
1681
1682                 /* Do accounting for the write space.  */
1683                 sctp_set_owner_w(chunk);
1684
1685                 chunk->transport = chunk_tp;
1686
1687                 /* Send it to the lower layers.  Note:  all chunks
1688                  * must either fail or succeed.   The lower layer
1689                  * works that way today.  Keep it that way or this
1690                  * breaks.
1691                  */
1692                 err = sctp_primitive_SEND(asoc, chunk);
1693                 /* Did the lower layer accept the chunk? */
1694                 if (err)
1695                         sctp_chunk_free(chunk);
1696                 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1697         }
1698
1699         sctp_datamsg_free(datamsg);
1700         if (err)
1701                 goto out_free;
1702         else
1703                 err = msg_len;
1704
1705         /* If we are already past ASSOCIATE, the lower
1706          * layers are responsible for association cleanup.
1707          */
1708         goto out_unlock;
1709
1710 out_free:
1711         if (new_asoc)
1712                 sctp_association_free(asoc);
1713 out_unlock:
1714         sctp_release_sock(sk);
1715
1716 out_nounlock:
1717         return sctp_error(sk, msg_flags, err);
1718
1719 #if 0
1720 do_sock_err:
1721         if (msg_len)
1722                 err = msg_len;
1723         else
1724                 err = sock_error(sk);
1725         goto out;
1726
1727 do_interrupted:
1728         if (msg_len)
1729                 err = msg_len;
1730         goto out;
1731 #endif /* 0 */
1732 }
1733
1734 /* This is an extended version of skb_pull() that removes the data from the
1735  * start of a skb even when data is spread across the list of skb's in the
1736  * frag_list. len specifies the total amount of data that needs to be removed.
1737  * when 'len' bytes could be removed from the skb, it returns 0.
1738  * If 'len' exceeds the total skb length,  it returns the no. of bytes that
1739  * could not be removed.
1740  */
1741 static int sctp_skb_pull(struct sk_buff *skb, int len)
1742 {
1743         struct sk_buff *list;
1744         int skb_len = skb_headlen(skb);
1745         int rlen;
1746
1747         if (len <= skb_len) {
1748                 __skb_pull(skb, len);
1749                 return 0;
1750         }
1751         len -= skb_len;
1752         __skb_pull(skb, skb_len);
1753
1754         for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1755                 rlen = sctp_skb_pull(list, len);
1756                 skb->len -= (len-rlen);
1757                 skb->data_len -= (len-rlen);
1758
1759                 if (!rlen)
1760                         return 0;
1761
1762                 len = rlen;
1763         }
1764
1765         return len;
1766 }
1767
1768 /* API 3.1.3  recvmsg() - UDP Style Syntax
1769  *
1770  *  ssize_t recvmsg(int socket, struct msghdr *message,
1771  *                    int flags);
1772  *
1773  *  socket  - the socket descriptor of the endpoint.
1774  *  message - pointer to the msghdr structure which contains a single
1775  *            user message and possibly some ancillary data.
1776  *
1777  *            See Section 5 for complete description of the data
1778  *            structures.
1779  *
1780  *  flags   - flags sent or received with the user message, see Section
1781  *            5 for complete description of the flags.
1782  */
1783 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1784
1785 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1786                              struct msghdr *msg, size_t len, int noblock,
1787                              int flags, int *addr_len)
1788 {
1789         struct sctp_ulpevent *event = NULL;
1790         struct sctp_sock *sp = sctp_sk(sk);
1791         struct sk_buff *skb;
1792         int copied;
1793         int err = 0;
1794         int skb_len;
1795
1796         SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
1797                           "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1798                           "len", len, "knoblauch", noblock,
1799                           "flags", flags, "addr_len", addr_len);
1800
1801         sctp_lock_sock(sk);
1802
1803         if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1804                 err = -ENOTCONN;
1805                 goto out;
1806         }
1807
1808         skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1809         if (!skb)
1810                 goto out;
1811
1812         /* Get the total length of the skb including any skb's in the
1813          * frag_list.
1814          */
1815         skb_len = skb->len;
1816
1817         copied = skb_len;
1818         if (copied > len)
1819                 copied = len;
1820
1821         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1822
1823         event = sctp_skb2event(skb);
1824
1825         if (err)
1826                 goto out_free;
1827
1828         sock_recv_timestamp(msg, sk, skb);
1829         if (sctp_ulpevent_is_notification(event)) {
1830                 msg->msg_flags |= MSG_NOTIFICATION;
1831                 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1832         } else {
1833                 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1834         }
1835
1836         /* Check if we allow SCTP_SNDRCVINFO. */
1837         if (sp->subscribe.sctp_data_io_event)
1838                 sctp_ulpevent_read_sndrcvinfo(event, msg);
1839 #if 0
1840         /* FIXME: we should be calling IP/IPv6 layers.  */
1841         if (sk->sk_protinfo.af_inet.cmsg_flags)
1842                 ip_cmsg_recv(msg, skb);
1843 #endif
1844
1845         err = copied;
1846
1847         /* If skb's length exceeds the user's buffer, update the skb and
1848          * push it back to the receive_queue so that the next call to
1849          * recvmsg() will return the remaining data. Don't set MSG_EOR.
1850          */
1851         if (skb_len > copied) {
1852                 msg->msg_flags &= ~MSG_EOR;
1853                 if (flags & MSG_PEEK)
1854                         goto out_free;
1855                 sctp_skb_pull(skb, copied);
1856                 skb_queue_head(&sk->sk_receive_queue, skb);
1857
1858                 /* When only partial message is copied to the user, increase
1859                  * rwnd by that amount. If all the data in the skb is read,
1860                  * rwnd is updated when the event is freed.
1861                  */
1862                 sctp_assoc_rwnd_increase(event->asoc, copied);
1863                 goto out;
1864         } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1865                    (event->msg_flags & MSG_EOR))
1866                 msg->msg_flags |= MSG_EOR;
1867         else
1868                 msg->msg_flags &= ~MSG_EOR;
1869
1870 out_free:
1871         if (flags & MSG_PEEK) {
1872                 /* Release the skb reference acquired after peeking the skb in
1873                  * sctp_skb_recv_datagram().
1874                  */
1875                 kfree_skb(skb);
1876         } else {
1877                 /* Free the event which includes releasing the reference to
1878                  * the owner of the skb, freeing the skb and updating the
1879                  * rwnd.
1880                  */
1881                 sctp_ulpevent_free(event);
1882         }
1883 out:
1884         sctp_release_sock(sk);
1885         return err;
1886 }
1887
1888 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1889  *
1890  * This option is a on/off flag.  If enabled no SCTP message
1891  * fragmentation will be performed.  Instead if a message being sent
1892  * exceeds the current PMTU size, the message will NOT be sent and
1893  * instead a error will be indicated to the user.
1894  */
1895 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1896                                             char __user *optval, int optlen)
1897 {
1898         int val;
1899
1900         if (optlen < sizeof(int))
1901                 return -EINVAL;
1902
1903         if (get_user(val, (int __user *)optval))
1904                 return -EFAULT;
1905
1906         sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1907
1908         return 0;
1909 }
1910
1911 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
1912                                         int optlen)
1913 {
1914         if (optlen != sizeof(struct sctp_event_subscribe))
1915                 return -EINVAL;
1916         if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1917                 return -EFAULT;
1918         return 0;
1919 }
1920
1921 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1922  *
1923  * This socket option is applicable to the UDP-style socket only.  When
1924  * set it will cause associations that are idle for more than the
1925  * specified number of seconds to automatically close.  An association
1926  * being idle is defined an association that has NOT sent or received
1927  * user data.  The special value of '0' indicates that no automatic
1928  * close of any associations should be performed.  The option expects an
1929  * integer defining the number of seconds of idle time before an
1930  * association is closed.
1931  */
1932 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
1933                                             int optlen)
1934 {
1935         struct sctp_sock *sp = sctp_sk(sk);
1936
1937         /* Applicable to UDP-style socket only */
1938         if (sctp_style(sk, TCP))
1939                 return -EOPNOTSUPP;
1940         if (optlen != sizeof(int))
1941                 return -EINVAL;
1942         if (copy_from_user(&sp->autoclose, optval, optlen))
1943                 return -EFAULT;
1944
1945         return 0;
1946 }
1947
1948 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1949  *
1950  * Applications can enable or disable heartbeats for any peer address of
1951  * an association, modify an address's heartbeat interval, force a
1952  * heartbeat to be sent immediately, and adjust the address's maximum
1953  * number of retransmissions sent before an address is considered
1954  * unreachable.  The following structure is used to access and modify an
1955  * address's parameters:
1956  *
1957  *  struct sctp_paddrparams {
1958  *     sctp_assoc_t            spp_assoc_id;
1959  *     struct sockaddr_storage spp_address;
1960  *     uint32_t                spp_hbinterval;
1961  *     uint16_t                spp_pathmaxrxt;
1962  *     uint32_t                spp_pathmtu;
1963  *     uint32_t                spp_sackdelay;
1964  *     uint32_t                spp_flags;
1965  * };
1966  *
1967  *   spp_assoc_id    - (one-to-many style socket) This is filled in the
1968  *                     application, and identifies the association for
1969  *                     this query.
1970  *   spp_address     - This specifies which address is of interest.
1971  *   spp_hbinterval  - This contains the value of the heartbeat interval,
1972  *                     in milliseconds.  If a  value of zero
1973  *                     is present in this field then no changes are to
1974  *                     be made to this parameter.
1975  *   spp_pathmaxrxt  - This contains the maximum number of
1976  *                     retransmissions before this address shall be
1977  *                     considered unreachable. If a  value of zero
1978  *                     is present in this field then no changes are to
1979  *                     be made to this parameter.
1980  *   spp_pathmtu     - When Path MTU discovery is disabled the value
1981  *                     specified here will be the "fixed" path mtu.
1982  *                     Note that if the spp_address field is empty
1983  *                     then all associations on this address will
1984  *                     have this fixed path mtu set upon them.
1985  *
1986  *   spp_sackdelay   - When delayed sack is enabled, this value specifies
1987  *                     the number of milliseconds that sacks will be delayed
1988  *                     for. This value will apply to all addresses of an
1989  *                     association if the spp_address field is empty. Note
1990  *                     also, that if delayed sack is enabled and this
1991  *                     value is set to 0, no change is made to the last
1992  *                     recorded delayed sack timer value.
1993  *
1994  *   spp_flags       - These flags are used to control various features
1995  *                     on an association. The flag field may contain
1996  *                     zero or more of the following options.
1997  *
1998  *                     SPP_HB_ENABLE  - Enable heartbeats on the
1999  *                     specified address. Note that if the address
2000  *                     field is empty all addresses for the association
2001  *                     have heartbeats enabled upon them.
2002  *
2003  *                     SPP_HB_DISABLE - Disable heartbeats on the
2004  *                     speicifed address. Note that if the address
2005  *                     field is empty all addresses for the association
2006  *                     will have their heartbeats disabled. Note also
2007  *                     that SPP_HB_ENABLE and SPP_HB_DISABLE are
2008  *                     mutually exclusive, only one of these two should
2009  *                     be specified. Enabling both fields will have
2010  *                     undetermined results.
2011  *
2012  *                     SPP_HB_DEMAND - Request a user initiated heartbeat
2013  *                     to be made immediately.
2014  *
2015  *                     SPP_PMTUD_ENABLE - This field will enable PMTU
2016  *                     discovery upon the specified address. Note that
2017  *                     if the address feild is empty then all addresses
2018  *                     on the association are effected.
2019  *
2020  *                     SPP_PMTUD_DISABLE - This field will disable PMTU
2021  *                     discovery upon the specified address. Note that
2022  *                     if the address feild is empty then all addresses
2023  *                     on the association are effected. Not also that
2024  *                     SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2025  *                     exclusive. Enabling both will have undetermined
2026  *                     results.
2027  *
2028  *                     SPP_SACKDELAY_ENABLE - Setting this flag turns
2029  *                     on delayed sack. The time specified in spp_sackdelay
2030  *                     is used to specify the sack delay for this address. Note
2031  *                     that if spp_address is empty then all addresses will
2032  *                     enable delayed sack and take on the sack delay
2033  *                     value specified in spp_sackdelay.
2034  *                     SPP_SACKDELAY_DISABLE - Setting this flag turns
2035  *                     off delayed sack. If the spp_address field is blank then
2036  *                     delayed sack is disabled for the entire association. Note
2037  *                     also that this field is mutually exclusive to
2038  *                     SPP_SACKDELAY_ENABLE, setting both will have undefined
2039  *                     results.
2040  */
2041 int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2042                                 struct sctp_transport   *trans,
2043                                 struct sctp_association *asoc,
2044                                 struct sctp_sock        *sp,
2045                                 int                      hb_change,
2046                                 int                      pmtud_change,
2047                                 int                      sackdelay_change)
2048 {
2049         int error;
2050
2051         if (params->spp_flags & SPP_HB_DEMAND && trans) {
2052                 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
2053                 if (error)
2054                         return error;
2055         }
2056
2057         if (params->spp_hbinterval) {
2058                 if (trans) {
2059                         trans->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2060                 } else if (asoc) {
2061                         asoc->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
2062                 } else {
2063                         sp->hbinterval = params->spp_hbinterval;
2064                 }
2065         }
2066
2067         if (hb_change) {
2068                 if (trans) {
2069                         trans->param_flags =
2070                                 (trans->param_flags & ~SPP_HB) | hb_change;
2071                 } else if (asoc) {
2072                         asoc->param_flags =
2073                                 (asoc->param_flags & ~SPP_HB) | hb_change;
2074                 } else {
2075                         sp->param_flags =
2076                                 (sp->param_flags & ~SPP_HB) | hb_change;
2077                 }
2078         }
2079
2080         if (params->spp_pathmtu) {
2081                 if (trans) {
2082                         trans->pathmtu = params->spp_pathmtu;
2083                         sctp_assoc_sync_pmtu(asoc);
2084                 } else if (asoc) {
2085                         asoc->pathmtu = params->spp_pathmtu;
2086                         sctp_frag_point(sp, params->spp_pathmtu);
2087                 } else {
2088                         sp->pathmtu = params->spp_pathmtu;
2089                 }
2090         }
2091
2092         if (pmtud_change) {
2093                 if (trans) {
2094                         int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2095                                 (params->spp_flags & SPP_PMTUD_ENABLE);
2096                         trans->param_flags =
2097                                 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2098                         if (update) {
2099                                 sctp_transport_pmtu(trans);
2100                                 sctp_assoc_sync_pmtu(asoc);
2101                         }
2102                 } else if (asoc) {
2103                         asoc->param_flags =
2104                                 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2105                 } else {
2106                         sp->param_flags =
2107                                 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2108                 }
2109         }
2110
2111         if (params->spp_sackdelay) {
2112                 if (trans) {
2113                         trans->sackdelay =
2114                                 msecs_to_jiffies(params->spp_sackdelay);
2115                 } else if (asoc) {
2116                         asoc->sackdelay =
2117                                 msecs_to_jiffies(params->spp_sackdelay);
2118                 } else {
2119                         sp->sackdelay = params->spp_sackdelay;
2120                 }
2121         }
2122
2123         if (sackdelay_change) {
2124                 if (trans) {
2125                         trans->param_flags =
2126                                 (trans->param_flags & ~SPP_SACKDELAY) |
2127                                 sackdelay_change;
2128                 } else if (asoc) {
2129                         asoc->param_flags =
2130                                 (asoc->param_flags & ~SPP_SACKDELAY) |
2131                                 sackdelay_change;
2132                 } else {
2133                         sp->param_flags =
2134                                 (sp->param_flags & ~SPP_SACKDELAY) |
2135                                 sackdelay_change;
2136                 }
2137         }
2138
2139         if (params->spp_pathmaxrxt) {
2140                 if (trans) {
2141                         trans->pathmaxrxt = params->spp_pathmaxrxt;
2142                 } else if (asoc) {
2143                         asoc->pathmaxrxt = params->spp_pathmaxrxt;
2144                 } else {
2145                         sp->pathmaxrxt = params->spp_pathmaxrxt;
2146                 }
2147         }
2148
2149         return 0;
2150 }
2151
2152 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2153                                             char __user *optval, int optlen)
2154 {
2155         struct sctp_paddrparams  params;
2156         struct sctp_transport   *trans = NULL;
2157         struct sctp_association *asoc = NULL;
2158         struct sctp_sock        *sp = sctp_sk(sk);
2159         int error;
2160         int hb_change, pmtud_change, sackdelay_change;
2161
2162         if (optlen != sizeof(struct sctp_paddrparams))
2163                 return - EINVAL;
2164
2165         if (copy_from_user(&params, optval, optlen))
2166                 return -EFAULT;
2167
2168         /* Validate flags and value parameters. */
2169         hb_change        = params.spp_flags & SPP_HB;
2170         pmtud_change     = params.spp_flags & SPP_PMTUD;
2171         sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2172
2173         if (hb_change        == SPP_HB ||
2174             pmtud_change     == SPP_PMTUD ||
2175             sackdelay_change == SPP_SACKDELAY ||
2176             params.spp_sackdelay > 500 ||
2177             (params.spp_pathmtu
2178             && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2179                 return -EINVAL;
2180
2181         /* If an address other than INADDR_ANY is specified, and
2182          * no transport is found, then the request is invalid.
2183          */
2184         if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
2185                 trans = sctp_addr_id2transport(sk, &params.spp_address,
2186                                                params.spp_assoc_id);
2187                 if (!trans)
2188                         return -EINVAL;
2189         }
2190
2191         /* Get association, if assoc_id != 0 and the socket is a one
2192          * to many style socket, and an association was not found, then
2193          * the id was invalid.
2194          */
2195         asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2196         if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2197                 return -EINVAL;
2198
2199         /* Heartbeat demand can only be sent on a transport or
2200          * association, but not a socket.
2201          */
2202         if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2203                 return -EINVAL;
2204
2205         /* Process parameters. */
2206         error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2207                                             hb_change, pmtud_change,
2208                                             sackdelay_change);
2209
2210         if (error)
2211                 return error;
2212
2213         /* If changes are for association, also apply parameters to each
2214          * transport.
2215          */
2216         if (!trans && asoc) {
2217                 struct list_head *pos;
2218
2219                 list_for_each(pos, &asoc->peer.transport_addr_list) {
2220                         trans = list_entry(pos, struct sctp_transport,
2221                                            transports);
2222                         sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2223                                                     hb_change, pmtud_change,
2224                                                     sackdelay_change);
2225                 }
2226         }
2227
2228         return 0;
2229 }
2230
2231 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
2232  *
2233  *   This options will get or set the delayed ack timer.  The time is set
2234  *   in milliseconds.  If the assoc_id is 0, then this sets or gets the
2235  *   endpoints default delayed ack timer value.  If the assoc_id field is
2236  *   non-zero, then the set or get effects the specified association.
2237  *
2238  *   struct sctp_assoc_value {
2239  *       sctp_assoc_t            assoc_id;
2240  *       uint32_t                assoc_value;
2241  *   };
2242  *
2243  *     assoc_id    - This parameter, indicates which association the
2244  *                   user is preforming an action upon. Note that if
2245  *                   this field's value is zero then the endpoints
2246  *                   default value is changed (effecting future
2247  *                   associations only).
2248  *
2249  *     assoc_value - This parameter contains the number of milliseconds
2250  *                   that the user is requesting the delayed ACK timer
2251  *                   be set to. Note that this value is defined in
2252  *                   the standard to be between 200 and 500 milliseconds.
2253  *
2254  *                   Note: a value of zero will leave the value alone,
2255  *                   but disable SACK delay. A non-zero value will also
2256  *                   enable SACK delay.
2257  */
2258
2259 static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
2260                                             char __user *optval, int optlen)
2261 {
2262         struct sctp_assoc_value  params;
2263         struct sctp_transport   *trans = NULL;
2264         struct sctp_association *asoc = NULL;
2265         struct sctp_sock        *sp = sctp_sk(sk);
2266
2267         if (optlen != sizeof(struct sctp_assoc_value))
2268                 return - EINVAL;
2269
2270         if (copy_from_user(&params, optval, optlen))
2271                 return -EFAULT;
2272
2273         /* Validate value parameter. */
2274         if (params.assoc_value > 500)
2275                 return -EINVAL;
2276
2277         /* Get association, if assoc_id != 0 and the socket is a one
2278          * to many style socket, and an association was not found, then
2279          * the id was invalid.
2280          */
2281         asoc = sctp_id2assoc(sk, params.assoc_id);
2282         if (!asoc && params.assoc_id && sctp_style(sk, UDP))
2283                 return -EINVAL;
2284
2285         if (params.assoc_value) {
2286                 if (asoc) {
2287                         asoc->sackdelay =
2288                                 msecs_to_jiffies(params.assoc_value);
2289                         asoc->param_flags = 
2290                                 (asoc->param_flags & ~SPP_SACKDELAY) |
2291                                 SPP_SACKDELAY_ENABLE;
2292                 } else {
2293                         sp->sackdelay = params.assoc_value;
2294                         sp->param_flags = 
2295                                 (sp->param_flags & ~SPP_SACKDELAY) |
2296                                 SPP_SACKDELAY_ENABLE;
2297                 }
2298         } else {
2299                 if (asoc) {
2300                         asoc->param_flags = 
2301                                 (asoc->param_flags & ~SPP_SACKDELAY) |
2302                                 SPP_SACKDELAY_DISABLE;
2303                 } else {
2304                         sp->param_flags = 
2305                                 (sp->param_flags & ~SPP_SACKDELAY) |
2306                                 SPP_SACKDELAY_DISABLE;
2307                 }
2308         }
2309
2310         /* If change is for association, also apply to each transport. */
2311         if (asoc) {
2312                 struct list_head *pos;
2313
2314                 list_for_each(pos, &asoc->peer.transport_addr_list) {
2315                         trans = list_entry(pos, struct sctp_transport,
2316                                            transports);
2317                         if (params.assoc_value) {
2318                                 trans->sackdelay =
2319                                         msecs_to_jiffies(params.assoc_value);
2320                                 trans->param_flags = 
2321                                         (trans->param_flags & ~SPP_SACKDELAY) |
2322                                         SPP_SACKDELAY_ENABLE;
2323                         } else {
2324                                 trans->param_flags = 
2325                                         (trans->param_flags & ~SPP_SACKDELAY) |
2326                                         SPP_SACKDELAY_DISABLE;
2327                         }
2328                 }
2329         }
2330  
2331         return 0;
2332 }
2333
2334 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2335  *
2336  * Applications can specify protocol parameters for the default association
2337  * initialization.  The option name argument to setsockopt() and getsockopt()
2338  * is SCTP_INITMSG.
2339  *
2340  * Setting initialization parameters is effective only on an unconnected
2341  * socket (for UDP-style sockets only future associations are effected
2342  * by the change).  With TCP-style sockets, this option is inherited by
2343  * sockets derived from a listener socket.
2344  */
2345 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
2346 {
2347         struct sctp_initmsg sinit;
2348         struct sctp_sock *sp = sctp_sk(sk);
2349
2350         if (optlen != sizeof(struct sctp_initmsg))
2351                 return -EINVAL;
2352         if (copy_from_user(&sinit, optval, optlen))
2353                 return -EFAULT;
2354
2355         if (sinit.sinit_num_ostreams)
2356                 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;      
2357         if (sinit.sinit_max_instreams)
2358                 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;    
2359         if (sinit.sinit_max_attempts)
2360                 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;      
2361         if (sinit.sinit_max_init_timeo)
2362                 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;  
2363
2364         return 0;
2365 }
2366
2367 /*
2368  * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2369  *
2370  *   Applications that wish to use the sendto() system call may wish to
2371  *   specify a default set of parameters that would normally be supplied
2372  *   through the inclusion of ancillary data.  This socket option allows
2373  *   such an application to set the default sctp_sndrcvinfo structure.
2374  *   The application that wishes to use this socket option simply passes
2375  *   in to this call the sctp_sndrcvinfo structure defined in Section
2376  *   5.2.2) The input parameters accepted by this call include
2377  *   sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2378  *   sinfo_timetolive.  The user must provide the sinfo_assoc_id field in
2379  *   to this call if the caller is using the UDP model.
2380  */
2381 static int sctp_setsockopt_default_send_param(struct sock *sk,
2382                                                 char __user *optval, int optlen)
2383 {
2384         struct sctp_sndrcvinfo info;
2385         struct sctp_association *asoc;
2386         struct sctp_sock *sp = sctp_sk(sk);
2387
2388         if (optlen != sizeof(struct sctp_sndrcvinfo))
2389                 return -EINVAL;
2390         if (copy_from_user(&info, optval, optlen))
2391                 return -EFAULT;
2392
2393         asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2394         if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2395                 return -EINVAL;
2396
2397         if (asoc) {
2398                 asoc->default_stream = info.sinfo_stream;
2399                 asoc->default_flags = info.sinfo_flags;
2400                 asoc->default_ppid = info.sinfo_ppid;
2401                 asoc->default_context = info.sinfo_context;
2402                 asoc->default_timetolive = info.sinfo_timetolive;
2403         } else {
2404                 sp->default_stream = info.sinfo_stream;
2405                 sp->default_flags = info.sinfo_flags;
2406                 sp->default_ppid = info.sinfo_ppid;
2407                 sp->default_context = info.sinfo_context;
2408                 sp->default_timetolive = info.sinfo_timetolive;
2409         }
2410
2411         return 0;
2412 }
2413
2414 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2415  *
2416  * Requests that the local SCTP stack use the enclosed peer address as
2417  * the association primary.  The enclosed address must be one of the
2418  * association peer's addresses.
2419  */
2420 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
2421                                         int optlen)
2422 {
2423         struct sctp_prim prim;
2424         struct sctp_transport *trans;
2425
2426         if (optlen != sizeof(struct sctp_prim))
2427                 return -EINVAL;
2428
2429         if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2430                 return -EFAULT;
2431
2432         trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2433         if (!trans)
2434                 return -EINVAL;
2435
2436         sctp_assoc_set_primary(trans->asoc, trans);
2437
2438         return 0;
2439 }
2440
2441 /*
2442  * 7.1.5 SCTP_NODELAY
2443  *
2444  * Turn on/off any Nagle-like algorithm.  This means that packets are
2445  * generally sent as soon as possible and no unnecessary delays are
2446  * introduced, at the cost of more packets in the network.  Expects an
2447  *  integer boolean flag.
2448  */
2449 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
2450                                         int optlen)
2451 {
2452         int val;
2453
2454         if (optlen < sizeof(int))
2455                 return -EINVAL;
2456         if (get_user(val, (int __user *)optval))
2457                 return -EFAULT;
2458
2459         sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2460         return 0;
2461 }
2462
2463 /*
2464  *
2465  * 7.1.1 SCTP_RTOINFO
2466  *
2467  * The protocol parameters used to initialize and bound retransmission
2468  * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2469  * and modify these parameters.
2470  * All parameters are time values, in milliseconds.  A value of 0, when
2471  * modifying the parameters, indicates that the current value should not
2472  * be changed.
2473  *
2474  */
2475 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
2476         struct sctp_rtoinfo rtoinfo;
2477         struct sctp_association *asoc;
2478
2479         if (optlen != sizeof (struct sctp_rtoinfo))
2480                 return -EINVAL;
2481
2482         if (copy_from_user(&rtoinfo, optval, optlen))
2483                 return -EFAULT;
2484
2485         asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2486
2487         /* Set the values to the specific association */
2488         if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2489                 return -EINVAL;
2490
2491         if (asoc) {
2492                 if (rtoinfo.srto_initial != 0)
2493                         asoc->rto_initial = 
2494                                 msecs_to_jiffies(rtoinfo.srto_initial);
2495                 if (rtoinfo.srto_max != 0)
2496                         asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2497                 if (rtoinfo.srto_min != 0)
2498                         asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2499         } else {
2500                 /* If there is no association or the association-id = 0
2501                  * set the values to the endpoint.
2502                  */
2503                 struct sctp_sock *sp = sctp_sk(sk);
2504
2505                 if (rtoinfo.srto_initial != 0)
2506                         sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2507                 if (rtoinfo.srto_max != 0)
2508                         sp->rtoinfo.srto_max = rtoinfo.srto_max;
2509                 if (rtoinfo.srto_min != 0)
2510                         sp->rtoinfo.srto_min = rtoinfo.srto_min;
2511         }
2512
2513         return 0;
2514 }
2515
2516 /*
2517  *
2518  * 7.1.2 SCTP_ASSOCINFO
2519  *
2520  * This option is used to tune the the maximum retransmission attempts
2521  * of the association.
2522  * Returns an error if the new association retransmission value is
2523  * greater than the sum of the retransmission value  of the peer.
2524  * See [SCTP] for more information.
2525  *
2526  */
2527 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
2528 {
2529
2530         struct sctp_assocparams assocparams;
2531         struct sctp_association *asoc;
2532
2533         if (optlen != sizeof(struct sctp_assocparams))
2534                 return -EINVAL;
2535         if (copy_from_user(&assocparams, optval, optlen))
2536                 return -EFAULT;
2537
2538         asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2539
2540         if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2541                 return -EINVAL;
2542
2543         /* Set the values to the specific association */
2544         if (asoc) {
2545                 if (assocparams.sasoc_asocmaxrxt != 0) {
2546                         __u32 path_sum = 0;
2547                         int   paths = 0;
2548                         struct list_head *pos;
2549                         struct sctp_transport *peer_addr;
2550
2551                         list_for_each(pos, &asoc->peer.transport_addr_list) {
2552                                 peer_addr = list_entry(pos,
2553                                                 struct sctp_transport,
2554                                                 transports);
2555                                 path_sum += peer_addr->pathmaxrxt;
2556                                 paths++;
2557                         }
2558
2559                         /* Only validate asocmaxrxt if we have more then
2560                          * one path/transport.  We do this because path
2561                          * retransmissions are only counted when we have more
2562                          * then one path.
2563                          */
2564                         if (paths > 1 &&
2565                             assocparams.sasoc_asocmaxrxt > path_sum)
2566                                 return -EINVAL;
2567
2568                         asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
2569                 }
2570
2571                 if (assocparams.sasoc_cookie_life != 0) {
2572                         asoc->cookie_life.tv_sec =
2573                                         assocparams.sasoc_cookie_life / 1000;
2574                         asoc->cookie_life.tv_usec =
2575                                         (assocparams.sasoc_cookie_life % 1000)
2576                                         * 1000;
2577                 }
2578         } else {
2579                 /* Set the values to the endpoint */
2580                 struct sctp_sock *sp = sctp_sk(sk);
2581
2582                 if (assocparams.sasoc_asocmaxrxt != 0)
2583                         sp->assocparams.sasoc_asocmaxrxt =
2584                                                 assocparams.sasoc_asocmaxrxt;
2585                 if (assocparams.sasoc_cookie_life != 0)
2586                         sp->assocparams.sasoc_cookie_life =
2587                                                 assocparams.sasoc_cookie_life;
2588         }
2589         return 0;
2590 }
2591
2592 /*
2593  * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2594  *
2595  * This socket option is a boolean flag which turns on or off mapped V4
2596  * addresses.  If this option is turned on and the socket is type
2597  * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2598  * If this option is turned off, then no mapping will be done of V4
2599  * addresses and a user will receive both PF_INET6 and PF_INET type
2600  * addresses on the socket.
2601  */
2602 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
2603 {
2604         int val;
2605         struct sctp_sock *sp = sctp_sk(sk);
2606
2607         if (optlen < sizeof(int))
2608                 return -EINVAL;
2609         if (get_user(val, (int __user *)optval))
2610                 return -EFAULT;
2611         if (val)
2612                 sp->v4mapped = 1;
2613         else
2614                 sp->v4mapped = 0;
2615
2616         return 0;
2617 }
2618
2619 /*
2620  * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
2621  *
2622  * This socket option specifies the maximum size to put in any outgoing
2623  * SCTP chunk.  If a message is larger than this size it will be
2624  * fragmented by SCTP into the specified size.  Note that the underlying
2625  * SCTP implementation may fragment into smaller sized chunks when the
2626  * PMTU of the underlying association is smaller than the value set by
2627  * the user.
2628  */
2629 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
2630 {
2631         struct sctp_association *asoc;
2632         struct list_head *pos;
2633         struct sctp_sock *sp = sctp_sk(sk);
2634         int val;
2635
2636         if (optlen < sizeof(int))
2637                 return -EINVAL;
2638         if (get_user(val, (int __user *)optval))
2639                 return -EFAULT;
2640         if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
2641                 return -EINVAL;
2642         sp->user_frag = val;
2643
2644         /* Update the frag_point of the existing associations. */
2645         list_for_each(pos, &(sp->ep->asocs)) {
2646                 asoc = list_entry(pos, struct sctp_association, asocs);
2647                 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu); 
2648         }
2649
2650         return 0;
2651 }
2652
2653
2654 /*
2655  *  7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
2656  *
2657  *   Requests that the peer mark the enclosed address as the association
2658  *   primary. The enclosed address must be one of the association's
2659  *   locally bound addresses. The following structure is used to make a
2660  *   set primary request:
2661  */
2662 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
2663                                              int optlen)
2664 {
2665         struct sctp_sock        *sp;
2666         struct sctp_endpoint    *ep;
2667         struct sctp_association *asoc = NULL;
2668         struct sctp_setpeerprim prim;
2669         struct sctp_chunk       *chunk;
2670         int                     err;
2671
2672         sp = sctp_sk(sk);
2673         ep = sp->ep;
2674
2675         if (!sctp_addip_enable)
2676                 return -EPERM;
2677
2678         if (optlen != sizeof(struct sctp_setpeerprim))
2679                 return -EINVAL;
2680
2681         if (copy_from_user(&prim, optval, optlen))
2682                 return -EFAULT;
2683
2684         asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
2685         if (!asoc) 
2686                 return -EINVAL;
2687
2688         if (!asoc->peer.asconf_capable)
2689                 return -EPERM;
2690
2691         if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
2692                 return -EPERM;
2693
2694         if (!sctp_state(asoc, ESTABLISHED))
2695                 return -ENOTCONN;
2696
2697         if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
2698                 return -EADDRNOTAVAIL;
2699
2700         /* Create an ASCONF chunk with SET_PRIMARY parameter    */
2701         chunk = sctp_make_asconf_set_prim(asoc,
2702                                           (union sctp_addr *)&prim.sspp_addr);
2703         if (!chunk)
2704                 return -ENOMEM;
2705
2706         err = sctp_send_asconf(asoc, chunk);
2707
2708         SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
2709
2710         return err;
2711 }
2712
2713 static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
2714                                           int optlen)
2715 {
2716         struct sctp_setadaption adaption;
2717
2718         if (optlen != sizeof(struct sctp_setadaption))
2719                 return -EINVAL;
2720         if (copy_from_user(&adaption, optval, optlen)) 
2721                 return -EFAULT;
2722
2723         sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
2724
2725         return 0;
2726 }
2727
2728 /* API 6.2 setsockopt(), getsockopt()
2729  *
2730  * Applications use setsockopt() and getsockopt() to set or retrieve
2731  * socket options.  Socket options are used to change the default
2732  * behavior of sockets calls.  They are described in Section 7.
2733  *
2734  * The syntax is:
2735  *
2736  *   ret = getsockopt(int sd, int level, int optname, void __user *optval,
2737  *                    int __user *optlen);
2738  *   ret = setsockopt(int sd, int level, int optname, const void __user *optval,
2739  *                    int optlen);
2740  *
2741  *   sd      - the socket descript.
2742  *   level   - set to IPPROTO_SCTP for all SCTP options.
2743  *   optname - the option name.
2744  *   optval  - the buffer to store the value of the option.
2745  *   optlen  - the size of the buffer.
2746  */
2747 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
2748                                 char __user *optval, int optlen)
2749 {
2750         int retval = 0;
2751
2752         SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
2753                           sk, optname);
2754
2755         /* I can hardly begin to describe how wrong this is.  This is
2756          * so broken as to be worse than useless.  The API draft
2757          * REALLY is NOT helpful here...  I am not convinced that the
2758          * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
2759          * are at all well-founded.
2760          */
2761         if (level != SOL_SCTP) {
2762                 struct sctp_af *af = sctp_sk(sk)->pf->af;
2763                 retval = af->setsockopt(sk, level, optname, optval, optlen);
2764                 goto out_nounlock;
2765         }
2766
2767         sctp_lock_sock(sk);
2768
2769         switch (optname) {
2770         case SCTP_SOCKOPT_BINDX_ADD:
2771                 /* 'optlen' is the size of the addresses buffer. */
2772                 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2773                                                optlen, SCTP_BINDX_ADD_ADDR);
2774                 break;
2775
2776         case SCTP_SOCKOPT_BINDX_REM:
2777                 /* 'optlen' is the size of the addresses buffer. */
2778                 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
2779                                                optlen, SCTP_BINDX_REM_ADDR);
2780                 break;
2781
2782         case SCTP_SOCKOPT_CONNECTX:
2783                 /* 'optlen' is the size of the addresses buffer. */
2784                 retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
2785                                                optlen);
2786                 break;
2787
2788         case SCTP_DISABLE_FRAGMENTS:
2789                 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2790                 break;
2791
2792         case SCTP_EVENTS:
2793                 retval = sctp_setsockopt_events(sk, optval, optlen);
2794                 break;
2795
2796         case SCTP_AUTOCLOSE:
2797                 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2798                 break;
2799
2800         case SCTP_PEER_ADDR_PARAMS:
2801                 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2802                 break;
2803
2804         case SCTP_DELAYED_ACK_TIME:
2805                 retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
2806                 break;
2807
2808         case SCTP_INITMSG:
2809                 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2810                 break;
2811         case SCTP_DEFAULT_SEND_PARAM:
2812                 retval = sctp_setsockopt_default_send_param(sk, optval,
2813                                                             optlen);
2814                 break;
2815         case SCTP_PRIMARY_ADDR:
2816                 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2817                 break;
2818         case SCTP_SET_PEER_PRIMARY_ADDR:
2819                 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2820                 break;
2821         case SCTP_NODELAY:
2822                 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2823                 break;
2824         case SCTP_RTOINFO:
2825                 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2826                 break;
2827         case SCTP_ASSOCINFO:
2828                 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2829                 break;
2830         case SCTP_I_WANT_MAPPED_V4_ADDR:
2831                 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2832                 break;
2833         case SCTP_MAXSEG:
2834                 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2835                 break;
2836         case SCTP_ADAPTION_LAYER:
2837                 retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
2838                 break;
2839
2840         default:
2841                 retval = -ENOPROTOOPT;
2842                 break;
2843         };
2844
2845         sctp_release_sock(sk);
2846
2847 out_nounlock:
2848         return retval;
2849 }
2850
2851 /* API 3.1.6 connect() - UDP Style Syntax
2852  *
2853  * An application may use the connect() call in the UDP model to initiate an
2854  * association without sending data.
2855  *
2856  * The syntax is:
2857  *
2858  * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2859  *
2860  * sd: the socket descriptor to have a new association added to.
2861  *
2862  * nam: the address structure (either struct sockaddr_in or struct
2863  *    sockaddr_in6 defined in RFC2553 [7]).
2864  *
2865  * len: the size of the address.
2866  */
2867 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
2868                              int addr_len)
2869 {
2870         int err = 0;
2871         struct sctp_af *af;
2872
2873         sctp_lock_sock(sk);
2874
2875         SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
2876                           __FUNCTION__, sk, addr, addr_len);
2877
2878         /* Validate addr_len before calling common connect/connectx routine. */
2879         af = sctp_get_af_specific(addr->sa_family);
2880         if (!af || addr_len < af->sockaddr_len) {
2881                 err = -EINVAL;
2882         } else {
2883                 /* Pass correct addr len to common routine (so it knows there
2884                  * is only one address being passed.
2885                  */
2886                 err = __sctp_connect(sk, addr, af->sockaddr_len);
2887         }
2888
2889         sctp_release_sock(sk);
2890         return err;
2891 }
2892
2893 /* FIXME: Write comments. */
2894 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2895 {
2896         return -EOPNOTSUPP; /* STUB */
2897 }
2898
2899 /* 4.1.4 accept() - TCP Style Syntax
2900  *
2901  * Applications use accept() call to remove an established SCTP
2902  * association from the accept queue of the endpoint.  A new socket
2903  * descriptor will be returned from accept() to represent the newly
2904  * formed association.
2905  */
2906 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2907 {
2908         struct sctp_sock *sp;
2909         struct sctp_endpoint *ep;
2910         struct sock *newsk = NULL;
2911         struct sctp_association *asoc;
2912         long timeo;
2913         int error = 0;
2914
2915         sctp_lock_sock(sk);
2916
2917         sp = sctp_sk(sk);
2918         ep = sp->ep;
2919
2920         if (!sctp_style(sk, TCP)) {
2921                 error = -EOPNOTSUPP;
2922                 goto out;
2923         }
2924
2925         if (!sctp_sstate(sk, LISTENING)) {
2926                 error = -EINVAL;
2927                 goto out;
2928         }
2929
2930         timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2931
2932         error = sctp_wait_for_accept(sk, timeo);
2933         if (error)
2934                 goto out;
2935
2936         /* We treat the list of associations on the endpoint as the accept
2937          * queue and pick the first association on the list.
2938          */
2939         asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2940
2941         newsk = sp->pf->create_accept_sk(sk, asoc);
2942         if (!newsk) {
2943                 error = -ENOMEM;
2944                 goto out;
2945         }
2946
2947         /* Populate the fields of the newsk from the oldsk and migrate the
2948          * asoc to the newsk.
2949          */
2950         sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2951
2952 out:
2953         sctp_release_sock(sk);
2954         *err = error;
2955         return newsk;
2956 }
2957
2958 /* The SCTP ioctl handler. */
2959 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2960 {
2961         return -ENOIOCTLCMD;
2962 }
2963
2964 /* This is the function which gets called during socket creation to
2965  * initialized the SCTP-specific portion of the sock.
2966  * The sock structure should already be zero-filled memory.
2967  */
2968 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2969 {
2970         struct sctp_endpoint *ep;
2971         struct sctp_sock *sp;
2972
2973         SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2974
2975         sp = sctp_sk(sk);
2976
2977         /* Initialize the SCTP per socket area.  */
2978         switch (sk->sk_type) {
2979         case SOCK_SEQPACKET:
2980                 sp->type = SCTP_SOCKET_UDP;
2981                 break;
2982         case SOCK_STREAM:
2983                 sp->type = SCTP_SOCKET_TCP;
2984                 break;
2985         default:
2986                 return -ESOCKTNOSUPPORT;
2987         }
2988
2989         /* Initialize default send parameters. These parameters can be
2990          * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2991          */
2992         sp->default_stream = 0;
2993         sp->default_ppid = 0;
2994         sp->default_flags = 0;
2995         sp->default_context = 0;
2996         sp->default_timetolive = 0;
2997
2998         /* Initialize default setup parameters. These parameters
2999          * can be modified with the SCTP_INITMSG socket option or
3000          * overridden by the SCTP_INIT CMSG.
3001          */
3002         sp->initmsg.sinit_num_ostreams   = sctp_max_outstreams;
3003         sp->initmsg.sinit_max_instreams  = sctp_max_instreams;
3004         sp->initmsg.sinit_max_attempts   = sctp_max_retrans_init;
3005         sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
3006
3007         /* Initialize default RTO related parameters.  These parameters can
3008          * be modified for with the SCTP_RTOINFO socket option.
3009          */
3010         sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
3011         sp->rtoinfo.srto_max     = jiffies_to_msecs(sctp_rto_max);
3012         sp->rtoinfo.srto_min     = jiffies_to_msecs(sctp_rto_min);
3013
3014         /* Initialize default association related parameters. These parameters
3015          * can be modified with the SCTP_ASSOCINFO socket option.
3016          */
3017         sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3018         sp->assocparams.sasoc_number_peer_destinations = 0;
3019         sp->assocparams.sasoc_peer_rwnd = 0;
3020         sp->assocparams.sasoc_local_rwnd = 0;
3021         sp->assocparams.sasoc_cookie_life = 
3022                 jiffies_to_msecs(sctp_valid_cookie_life);
3023
3024         /* Initialize default event subscriptions. By default, all the
3025          * options are off. 
3026          */
3027         memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3028
3029         /* Default Peer Address Parameters.  These defaults can
3030          * be modified via SCTP_PEER_ADDR_PARAMS
3031          */
3032         sp->hbinterval  = jiffies_to_msecs(sctp_hb_interval);
3033         sp->pathmaxrxt  = sctp_max_retrans_path;
3034         sp->pathmtu     = 0; // allow default discovery
3035         sp->sackdelay   = jiffies_to_msecs(sctp_sack_timeout);
3036         sp->param_flags = SPP_HB_ENABLE |
3037                           SPP_PMTUD_ENABLE |
3038                           SPP_SACKDELAY_ENABLE;
3039
3040         /* If enabled no SCTP message fragmentation will be performed.
3041          * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3042          */
3043         sp->disable_fragments = 0;
3044
3045         /* Turn on/off any Nagle-like algorithm.  */
3046         sp->nodelay           = 1;
3047
3048         /* Enable by default. */
3049         sp->v4mapped          = 1;
3050
3051         /* Auto-close idle associations after the configured
3052          * number of seconds.  A value of 0 disables this
3053          * feature.  Configure through the SCTP_AUTOCLOSE socket option,
3054          * for UDP-style sockets only.
3055          */
3056         sp->autoclose         = 0;
3057
3058         /* User specified fragmentation limit. */
3059         sp->user_frag         = 0;
3060
3061         sp->adaption_ind = 0;
3062
3063         sp->pf = sctp_get_pf_specific(sk->sk_family);
3064
3065         /* Control variables for partial data delivery. */
3066         sp->pd_mode           = 0;
3067         skb_queue_head_init(&sp->pd_lobby);
3068
3069         /* Create a per socket endpoint structure.  Even if we
3070          * change the data structure relationships, this may still
3071          * be useful for storing pre-connect address information.
3072          */
3073         ep = sctp_endpoint_new(sk, GFP_KERNEL);
3074         if (!ep)
3075                 return -ENOMEM;
3076
3077         sp->ep = ep;
3078         sp->hmac = NULL;
3079
3080         SCTP_DBG_OBJCNT_INC(sock);
3081         return 0;
3082 }
3083
3084 /* Cleanup any SCTP per socket resources.  */
3085 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
3086 {
3087         struct sctp_endpoint *ep;
3088
3089         SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3090
3091         /* Release our hold on the endpoint. */
3092         ep = sctp_sk(sk)->ep;
3093         sctp_endpoint_free(ep);
3094
3095         return 0;
3096 }
3097
3098 /* API 4.1.7 shutdown() - TCP Style Syntax
3099  *     int shutdown(int socket, int how);
3100  *
3101  *     sd      - the socket descriptor of the association to be closed.
3102  *     how     - Specifies the type of shutdown.  The  values  are
3103  *               as follows:
3104  *               SHUT_RD
3105  *                     Disables further receive operations. No SCTP
3106  *                     protocol action is taken.
3107  *               SHUT_WR
3108  *                     Disables further send operations, and initiates
3109  *                     the SCTP shutdown sequence.
3110  *               SHUT_RDWR
3111  *                     Disables further send  and  receive  operations
3112  *                     and initiates the SCTP shutdown sequence.
3113  */
3114 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
3115 {
3116         struct sctp_endpoint *ep;
3117         struct sctp_association *asoc;
3118
3119         if (!sctp_style(sk, TCP))
3120                 return;
3121
3122         if (how & SEND_SHUTDOWN) {
3123                 ep = sctp_sk(sk)->ep;
3124                 if (!list_empty(&ep->asocs)) {
3125                         asoc = list_entry(ep->asocs.next,
3126                                           struct sctp_association, asocs);
3127                         sctp_primitive_SHUTDOWN(asoc, NULL);
3128                 }
3129         }
3130 }
3131
3132 /* 7.2.1 Association Status (SCTP_STATUS)
3133
3134  * Applications can retrieve current status information about an
3135  * association, including association state, peer receiver window size,
3136  * number of unacked data chunks, and number of data chunks pending
3137  * receipt.  This information is read-only.
3138  */
3139 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
3140                                        char __user *optval,
3141                                        int __user *optlen)
3142 {
3143         struct sctp_status status;
3144         struct sctp_association *asoc = NULL;
3145         struct sctp_transport *transport;
3146         sctp_assoc_t associd;
3147         int retval = 0;
3148
3149         if (len != sizeof(status)) {
3150                 retval = -EINVAL;
3151                 goto out;
3152         }
3153
3154         if (copy_from_user(&status, optval, sizeof(status))) {
3155                 retval = -EFAULT;
3156                 goto out;
3157         }
3158
3159         associd = status.sstat_assoc_id;
3160         asoc = sctp_id2assoc(sk, associd);
3161         if (!asoc) {
3162                 retval = -EINVAL;
3163                 goto out;
3164         }
3165
3166         transport = asoc->peer.primary_path;
3167
3168         status.sstat_assoc_id = sctp_assoc2id(asoc);
3169         status.sstat_state = asoc->state;
3170         status.sstat_rwnd =  asoc->peer.rwnd;
3171         status.sstat_unackdata = asoc->unack_data;
3172
3173         status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
3174         status.sstat_instrms = asoc->c.sinit_max_instreams;
3175         status.sstat_outstrms = asoc->c.sinit_num_ostreams;
3176         status.sstat_fragmentation_point = asoc->frag_point;
3177         status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3178         memcpy(&status.sstat_primary.spinfo_address,
3179                &(transport->ipaddr), sizeof(union sctp_addr));
3180         /* Map ipv4 address into v4-mapped-on-v6 address.  */
3181         sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3182                 (union sctp_addr *)&status.sstat_primary.spinfo_address);
3183         status.sstat_primary.spinfo_state = transport->state;
3184         status.sstat_primary.spinfo_cwnd = transport->cwnd;
3185         status.sstat_primary.spinfo_srtt = transport->srtt;
3186         status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
3187         status.sstat_primary.spinfo_mtu = transport->pathmtu;
3188
3189         if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
3190                 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
3191
3192         if (put_user(len, optlen)) {
3193                 retval = -EFAULT;
3194                 goto out;
3195         }
3196
3197         SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
3198                           len, status.sstat_state, status.sstat_rwnd,
3199                           status.sstat_assoc_id);
3200
3201         if (copy_to_user(optval, &status, len)) {
3202                 retval = -EFAULT;
3203                 goto out;
3204         }
3205
3206 out:
3207         return (retval);
3208 }
3209
3210
3211 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
3212  *
3213  * Applications can retrieve information about a specific peer address
3214  * of an association, including its reachability state, congestion
3215  * window, and retransmission timer values.  This information is
3216  * read-only.
3217  */
3218 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
3219                                           char __user *optval,
3220                                           int __user *optlen)
3221 {
3222         struct sctp_paddrinfo pinfo;
3223         struct sctp_transport *transport;
3224         int retval = 0;
3225
3226         if (len != sizeof(pinfo)) {
3227                 retval = -EINVAL;
3228                 goto out;
3229         }
3230
3231         if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
3232                 retval = -EFAULT;
3233                 goto out;
3234         }
3235
3236         transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
3237                                            pinfo.spinfo_assoc_id);
3238         if (!transport)
3239                 return -EINVAL;
3240
3241         pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
3242         pinfo.spinfo_state = transport->state;
3243         pinfo.spinfo_cwnd = transport->cwnd;
3244         pinfo.spinfo_srtt = transport->srtt;
3245         pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
3246         pinfo.spinfo_mtu = transport->pathmtu;
3247
3248         if (pinfo.spinfo_state == SCTP_UNKNOWN)
3249                 pinfo.spinfo_state = SCTP_ACTIVE;
3250
3251         if (put_user(len, optlen)) {
3252                 retval = -EFAULT;
3253                 goto out;
3254         }
3255
3256         if (copy_to_user(optval, &pinfo, len)) {
3257                 retval = -EFAULT;
3258                 goto out;
3259         }
3260
3261 out:
3262         return (retval);
3263 }
3264
3265 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
3266  *
3267  * This option is a on/off flag.  If enabled no SCTP message
3268  * fragmentation will be performed.  Instead if a message being sent
3269  * exceeds the current PMTU size, the message will NOT be sent and
3270  * instead a error will be indicated to the user.
3271  */
3272 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
3273                                         char __user *optval, int __user *optlen)
3274 {
3275         int val;
3276
3277         if (len < sizeof(int))
3278                 return -EINVAL;
3279
3280         len = sizeof(int);
3281         val = (sctp_sk(sk)->disable_fragments == 1);
3282         if (put_user(len, optlen))
3283                 return -EFAULT;
3284         if (copy_to_user(optval, &val, len))
3285                 return -EFAULT;
3286         return 0;
3287 }
3288
3289 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
3290  *
3291  * This socket option is used to specify various notifications and
3292  * ancillary data the user wishes to receive.
3293  */
3294 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
3295                                   int __user *optlen)
3296 {
3297         if (len != sizeof(struct sctp_event_subscribe))
3298                 return -EINVAL;
3299         if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
3300                 return -EFAULT;
3301         return 0;
3302 }
3303
3304 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
3305  *
3306  * This socket option is applicable to the UDP-style socket only.  When
3307  * set it will cause associations that are idle for more than the
3308  * specified number of seconds to automatically close.  An association
3309  * being idle is defined an association that has NOT sent or received
3310  * user data.  The special value of '0' indicates that no automatic
3311  * close of any associations should be performed.  The option expects an
3312  * integer defining the number of seconds of idle time before an
3313  * association is closed.
3314  */
3315 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
3316 {
3317         /* Applicable to UDP-style socket only */
3318         if (sctp_style(sk, TCP))
3319                 return -EOPNOTSUPP;
3320         if (len != sizeof(int))
3321                 return -EINVAL;
3322         if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
3323                 return -EFAULT;
3324         return 0;
3325 }
3326
3327 /* Helper routine to branch off an association to a new socket.  */
3328 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
3329                                 struct socket **sockp)
3330 {
3331         struct sock *sk = asoc->base.sk;
3332         struct socket *sock;
3333         int err = 0;
3334
3335         /* An association cannot be branched off from an already peeled-off
3336          * socket, nor is this supported for tcp style sockets.
3337          */
3338         if (!sctp_style(sk, UDP))
3339                 return -EINVAL;
3340
3341         /* Create a new socket.  */
3342         err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
3343         if (err < 0)
3344                 return err;
3345
3346         /* Populate the fields of the newsk from the oldsk and migrate the
3347          * asoc to the newsk.
3348          */
3349         sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
3350         *sockp = sock;
3351
3352         return err;
3353 }
3354
3355 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
3356 {
3357         sctp_peeloff_arg_t peeloff;
3358         struct socket *newsock;
3359         int retval = 0;
3360         struct sctp_association *asoc;
3361
3362         if (len != sizeof(sctp_peeloff_arg_t))
3363                 return -EINVAL;
3364         if (copy_from_user(&peeloff, optval, len))
3365                 return -EFAULT;
3366
3367         asoc = sctp_id2assoc(sk, peeloff.associd);
3368         if (!asoc) {
3369                 retval = -EINVAL;
3370                 goto out;
3371         }
3372
3373         SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
3374
3375         retval = sctp_do_peeloff(asoc, &newsock);
3376         if (retval < 0)
3377                 goto out;
3378
3379         /* Map the socket to an unused fd that can be returned to the user.  */
3380         retval = sock_map_fd(newsock);
3381         if (retval < 0) {
3382                 sock_release(newsock);
3383                 goto out;
3384         }
3385
3386         SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
3387                           __FUNCTION__, sk, asoc, newsock->sk, retval);
3388
3389         /* Return the fd mapped to the new socket.  */
3390         peeloff.sd = retval;
3391         if (copy_to_user(optval, &peeloff, len))
3392                 retval = -EFAULT;
3393
3394 out:
3395         return retval;
3396 }
3397
3398 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
3399  *
3400  * Applications can enable or disable heartbeats for any peer address of
3401  * an association, modify an address's heartbeat interval, force a
3402  * heartbeat to be sent immediately, and adjust the address's maximum
3403  * number of retransmissions sent before an address is considered
3404  * unreachable.  The following structure is used to access and modify an
3405  * address's parameters:
3406  *
3407  *  struct sctp_paddrparams {
3408  *     sctp_assoc_t            spp_assoc_id;
3409  *     struct sockaddr_storage spp_address;
3410  *     uint32_t                spp_hbinterval;
3411  *     uint16_t                spp_pathmaxrxt;
3412  *     uint32_t                spp_pathmtu;
3413  *     uint32_t                spp_sackdelay;
3414  *     uint32_t                spp_flags;
3415  * };
3416  *
3417  *   spp_assoc_id    - (one-to-many style socket) This is filled in the
3418  *                     application, and identifies the association for
3419  *                     this query.
3420  *   spp_address     - This specifies which address is of interest.
3421  *   spp_hbinterval  - This contains the value of the heartbeat interval,
3422  *                     in milliseconds.  If a  value of zero
3423  *                     is present in this field then no changes are to
3424  *                     be made to this parameter.
3425  *   spp_pathmaxrxt  - This contains the maximum number of
3426  *                     retransmissions before this address shall be
3427  *                     considered unreachable. If a  value of zero
3428  *                     is present in this field then no changes are to
3429  *                     be made to this parameter.
3430  *   spp_pathmtu     - When Path MTU discovery is disabled the value
3431  *                     specified here will be the "fixed" path mtu.
3432  *                     Note that if the spp_address field is empty
3433  *                     then all associations on this address will
3434  *                     have this fixed path mtu set upon them.
3435  *
3436  *   spp_sackdelay   - When delayed sack is enabled, this value specifies
3437  *                     the number of milliseconds that sacks will be delayed
3438  *                     for. This value will apply to all addresses of an
3439  *                     association if the spp_address field is empty. Note
3440  *                     also, that if delayed sack is enabled and this
3441  *                     value is set to 0, no change is made to the last
3442  *                     recorded delayed sack timer value.
3443  *
3444  *   spp_flags       - These flags are used to control various features
3445  *                     on an association. The flag field may contain
3446  *                     zero or more of the following options.
3447  *
3448  *                     SPP_HB_ENABLE  - Enable heartbeats on the
3449  *                     specified address. Note that if the address
3450  *                     field is empty all addresses for the association
3451  *                     have heartbeats enabled upon them.
3452  *
3453  *                     SPP_HB_DISABLE - Disable heartbeats on the
3454  *                     speicifed address. Note that if the address
3455  *                     field is empty all addresses for the association
3456  *                     will have their heartbeats disabled. Note also
3457  *                     that SPP_HB_ENABLE and SPP_HB_DISABLE are
3458  *                     mutually exclusive, only one of these two should
3459  *                     be specified. Enabling both fields will have
3460  *                     undetermined results.
3461  *
3462  *                     SPP_HB_DEMAND - Request a user initiated heartbeat
3463  *                     to be made immediately.
3464  *
3465  *                     SPP_PMTUD_ENABLE - This field will enable PMTU
3466  *                     discovery upon the specified address. Note that
3467  *                     if the address feild is empty then all addresses
3468  *                     on the association are effected.
3469  *
3470  *                     SPP_PMTUD_DISABLE - This field will disable PMTU
3471  *                     discovery upon the specified address. Note that
3472  *                     if the address feild is empty then all addresses
3473  *                     on the association are effected. Not also that
3474  *                     SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
3475  *                     exclusive. Enabling both will have undetermined
3476  *                     results.
3477  *
3478  *                     SPP_SACKDELAY_ENABLE - Setting this flag turns
3479  *                     on delayed sack. The time specified in spp_sackdelay
3480  *                     is used to specify the sack delay for this address. Note
3481  *                     that if spp_address is empty then all addresses will
3482  *                     enable delayed sack and take on the sack delay
3483  *                     value specified in spp_sackdelay.
3484  *                     SPP_SACKDELAY_DISABLE - Setting this flag turns
3485  *                     off delayed sack. If the spp_address field is blank then
3486  *                     delayed sack is disabled for the entire association. Note
3487  *                     also that this field is mutually exclusive to
3488  *                     SPP_SACKDELAY_ENABLE, setting both will have undefined
3489  *                     results.
3490  */
3491 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
3492                                             char __user *optval, int __user *optlen)
3493 {
3494         struct sctp_paddrparams  params;
3495         struct sctp_transport   *trans = NULL;
3496         struct sctp_association *asoc = NULL;
3497         struct sctp_sock        *sp = sctp_sk(sk);
3498
3499         if (len != sizeof(struct sctp_paddrparams))
3500                 return -EINVAL;
3501
3502         if (copy_from_user(&params, optval, len))
3503                 return -EFAULT;
3504
3505         /* If an address other than INADDR_ANY is specified, and
3506          * no transport is found, then the request is invalid.
3507          */
3508         if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
3509                 trans = sctp_addr_id2transport(sk, &params.spp_address,
3510                                                params.spp_assoc_id);
3511                 if (!trans) {
3512                         SCTP_DEBUG_PRINTK("Failed no transport\n");
3513                         return -EINVAL;
3514                 }
3515         }
3516
3517         /* Get association, if assoc_id != 0 and the socket is a one
3518          * to many style socket, and an association was not found, then
3519          * the id was invalid.
3520          */
3521         asoc = sctp_id2assoc(sk, params.spp_assoc_id);
3522         if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
3523                 SCTP_DEBUG_PRINTK("Failed no association\n");
3524                 return -EINVAL;
3525         }
3526
3527         if (trans) {
3528                 /* Fetch transport values. */
3529                 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
3530                 params.spp_pathmtu    = trans->pathmtu;
3531                 params.spp_pathmaxrxt = trans->pathmaxrxt;
3532                 params.spp_sackdelay  = jiffies_to_msecs(trans->sackdelay);
3533
3534                 /*draft-11 doesn't say what to return in spp_flags*/
3535                 params.spp_flags      = trans->param_flags;
3536         } else if (asoc) {
3537                 /* Fetch association values. */
3538                 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
3539                 params.spp_pathmtu    = asoc->pathmtu;
3540                 params.spp_pathmaxrxt = asoc->pathmaxrxt;
3541                 params.spp_sackdelay  = jiffies_to_msecs(asoc->sackdelay);
3542
3543                 /*draft-11 doesn't say what to return in spp_flags*/
3544                 params.spp_flags      = asoc->param_flags;
3545         } else {
3546                 /* Fetch socket values. */
3547                 params.spp_hbinterval = sp->hbinterval;
3548                 params.spp_pathmtu    = sp->pathmtu;
3549                 params.spp_sackdelay  = sp->sackdelay;
3550                 params.spp_pathmaxrxt = sp->pathmaxrxt;
3551
3552                 /*draft-11 doesn't say what to return in spp_flags*/
3553                 params.spp_flags      = sp->param_flags;
3554         }
3555
3556         if (copy_to_user(optval, &params, len))
3557                 return -EFAULT;
3558
3559         if (put_user(len, optlen))
3560                 return -EFAULT;
3561
3562         return 0;
3563 }
3564
3565 /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
3566  *
3567  *   This options will get or set the delayed ack timer.  The time is set
3568  *   in milliseconds.  If the assoc_id is 0, then this sets or gets the
3569  *   endpoints default delayed ack timer value.  If the assoc_id field is
3570  *   non-zero, then the set or get effects the specified association.
3571  *
3572  *   struct sctp_assoc_value {
3573  *       sctp_assoc_t            assoc_id;
3574  *       uint32_t                assoc_value;
3575  *   };
3576  *
3577  *     assoc_id    - This parameter, indicates which association the
3578  *                   user is preforming an action upon. Note that if
3579  *                   this field's value is zero then the endpoints
3580  *                   default value is changed (effecting future
3581  *                   associations only).
3582  *
3583  *     assoc_value - This parameter contains the number of milliseconds
3584  *                   that the user is requesting the delayed ACK timer
3585  *                   be set to. Note that this value is defined in
3586  *                   the standard to be between 200 and 500 milliseconds.
3587  *
3588  *                   Note: a value of zero will leave the value alone,
3589  *                   but disable SACK delay. A non-zero value will also
3590  *                   enable SACK delay.
3591  */
3592 static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
3593                                             char __user *optval,
3594                                             int __user *optlen)
3595 {
3596         struct sctp_assoc_value  params;
3597         struct sctp_association *asoc = NULL;
3598         struct sctp_sock        *sp = sctp_sk(sk);
3599
3600         if (len != sizeof(struct sctp_assoc_value))
3601                 return - EINVAL;
3602
3603         if (copy_from_user(&params, optval, len))
3604                 return -EFAULT;
3605
3606         /* Get association, if assoc_id != 0 and the socket is a one
3607          * to many style socket, and an association was not found, then
3608          * the id was invalid.
3609          */
3610         asoc = sctp_id2assoc(sk, params.assoc_id);
3611         if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3612                 return -EINVAL;
3613
3614         if (asoc) {
3615                 /* Fetch association values. */
3616                 if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
3617                         params.assoc_value = jiffies_to_msecs(
3618                                 asoc->sackdelay);
3619                 else
3620                         params.assoc_value = 0;
3621         } else {
3622                 /* Fetch socket values. */
3623                 if (sp->param_flags & SPP_SACKDELAY_ENABLE)
3624                         params.assoc_value  = sp->sackdelay;
3625                 else
3626                         params.assoc_value  = 0;
3627         }
3628
3629         if (copy_to_user(optval, &params, len))
3630                 return -EFAULT;
3631
3632         if (put_user(len, optlen))
3633                 return -EFAULT;
3634
3635         return 0;
3636 }
3637
3638 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
3639  *
3640  * Applications can specify protocol parameters for the default association
3641  * initialization.  The option name argument to setsockopt() and getsockopt()
3642  * is SCTP_INITMSG.
3643  *
3644  * Setting initialization parameters is effective only on an unconnected
3645  * socket (for UDP-style sockets only future associations are effected
3646  * by the change).  With TCP-style sockets, this option is inherited by
3647  * sockets derived from a listener socket.
3648  */
3649 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
3650 {
3651         if (len != sizeof(struct sctp_initmsg))
3652                 return -EINVAL;
3653         if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
3654                 return -EFAULT;
3655         return 0;
3656 }
3657
3658 static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
3659                                               char __user *optval,
3660                                               int __user *optlen)
3661 {
3662         sctp_assoc_t id;
3663         struct sctp_association *asoc;
3664         struct list_head *pos;
3665         int cnt = 0;
3666
3667         if (len != sizeof(sctp_assoc_t))
3668                 return -EINVAL;
3669
3670         if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3671                 return -EFAULT;
3672
3673         /* For UDP-style sockets, id specifies the association to query.  */
3674         asoc = sctp_id2assoc(sk, id);
3675         if (!asoc)
3676                 return -EINVAL;
3677
3678         list_for_each(pos, &asoc->peer.transport_addr_list) {
3679                 cnt ++;
3680         }
3681
3682         return cnt;
3683 }
3684
3685 /* 
3686  * Old API for getting list of peer addresses. Does not work for 32-bit
3687  * programs running on a 64-bit kernel
3688  */
3689 static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
3690                                           char __user *optval,
3691                                           int __user *optlen)
3692 {
3693         struct sctp_association *asoc;
3694         struct list_head *pos;
3695         int cnt = 0;
3696         struct sctp_getaddrs_old getaddrs;
3697         struct sctp_transport *from;
3698         void __user *to;
3699         union sctp_addr temp;
3700         struct sctp_sock *sp = sctp_sk(sk);
3701         int addrlen;
3702
3703         if (len != sizeof(struct sctp_getaddrs_old))
3704                 return -EINVAL;
3705
3706         if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3707                 return -EFAULT;
3708
3709         if (getaddrs.addr_num <= 0) return -EINVAL;
3710
3711         /* For UDP-style sockets, id specifies the association to query.  */
3712         asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3713         if (!asoc)
3714                 return -EINVAL;
3715
3716         to = (void __user *)getaddrs.addrs;
3717         list_for_each(pos, &asoc->peer.transport_addr_list) {
3718                 from = list_entry(pos, struct sctp_transport, transports);
3719                 memcpy(&temp, &from->ipaddr, sizeof(temp));
3720                 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3721                 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3722                 temp.v4.sin_port = htons(temp.v4.sin_port);
3723                 if (copy_to_user(to, &temp, addrlen))
3724                         return -EFAULT;
3725                 to += addrlen ;
3726                 cnt ++;
3727                 if (cnt >= getaddrs.addr_num) break;
3728         }
3729         getaddrs.addr_num = cnt;
3730         if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
3731                 return -EFAULT;
3732
3733         return 0;
3734 }
3735
3736 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
3737                                       char __user *optval, int __user *optlen)
3738 {
3739         struct sctp_association *asoc;
3740         struct list_head *pos;
3741         int cnt = 0;
3742         struct sctp_getaddrs getaddrs;
3743         struct sctp_transport *from;
3744         void __user *to;
3745         union sctp_addr temp;
3746         struct sctp_sock *sp = sctp_sk(sk);
3747         int addrlen;
3748         size_t space_left;
3749         int bytes_copied;
3750
3751         if (len < sizeof(struct sctp_getaddrs))
3752                 return -EINVAL;
3753
3754         if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
3755                 return -EFAULT;
3756
3757         /* For UDP-style sockets, id specifies the association to query.  */
3758         asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3759         if (!asoc)
3760                 return -EINVAL;
3761
3762         to = optval + offsetof(struct sctp_getaddrs,addrs);
3763         space_left = len - sizeof(struct sctp_getaddrs) - 
3764                         offsetof(struct sctp_getaddrs,addrs);
3765
3766         list_for_each(pos, &asoc->peer.transport_addr_list) {
3767                 from = list_entry(pos, struct sctp_transport, transports);
3768                 memcpy(&temp, &from->ipaddr, sizeof(temp));
3769                 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
3770                 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
3771                 if(space_left < addrlen)
3772                         return -ENOMEM;
3773                 temp.v4.sin_port = htons(temp.v4.sin_port);
3774                 if (copy_to_user(to, &temp, addrlen))
3775                         return -EFAULT;
3776                 to += addrlen;
3777                 cnt++;
3778                 space_left -= addrlen;
3779         }
3780
3781         if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
3782                 return -EFAULT;
3783         bytes_copied = ((char __user *)to) - optval;
3784         if (put_user(bytes_copied, optlen))
3785                 return -EFAULT;
3786
3787         return 0;
3788 }
3789
3790 static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
3791                                                char __user *optval,
3792                                                int __user *optlen)
3793 {
3794         sctp_assoc_t id;
3795         struct sctp_bind_addr *bp;
3796         struct sctp_association *asoc;
3797         struct list_head *pos;
3798         struct sctp_sockaddr_entry *addr;
3799         rwlock_t *addr_lock;
3800         unsigned long flags;
3801         int cnt = 0;
3802
3803         if (len != sizeof(sctp_assoc_t))
3804                 return -EINVAL;
3805
3806         if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
3807                 return -EFAULT;
3808
3809         /*
3810          *  For UDP-style sockets, id specifies the association to query.
3811          *  If the id field is set to the value '0' then the locally bound
3812          *  addresses are returned without regard to any particular
3813          *  association.
3814          */
3815         if (0 == id) {
3816                 bp = &sctp_sk(sk)->ep->base.bind_addr;
3817                 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3818         } else {
3819                 asoc = sctp_id2assoc(sk, id);
3820                 if (!asoc)
3821                         return -EINVAL;
3822                 bp = &asoc->base.bind_addr;
3823                 addr_lock = &asoc->base.addr_lock;
3824         }
3825
3826         sctp_read_lock(addr_lock);
3827
3828         /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
3829          * addresses from the global local address list.
3830          */
3831         if (sctp_list_single_entry(&bp->address_list)) {
3832                 addr = list_entry(bp->address_list.next,
3833                                   struct sctp_sockaddr_entry, list);
3834                 if (sctp_is_any(&addr->a)) {
3835                         sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3836                         list_for_each(pos, &sctp_local_addr_list) {
3837                                 addr = list_entry(pos,
3838                                                   struct sctp_sockaddr_entry,
3839                                                   list);
3840                                 if ((PF_INET == sk->sk_family) && 
3841                                     (AF_INET6 == addr->a.sa.sa_family)) 
3842                                         continue;
3843                                 cnt++;
3844                         }
3845                         sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3846                                                     flags);
3847                 } else {
3848                         cnt = 1;
3849                 }
3850                 goto done;
3851         }
3852
3853         list_for_each(pos, &bp->address_list) {
3854                 cnt ++;
3855         }
3856
3857 done:
3858         sctp_read_unlock(addr_lock);
3859         return cnt;
3860 }
3861
3862 /* Helper function that copies local addresses to user and returns the number
3863  * of addresses copied.
3864  */
3865 static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
3866                                         void __user *to)
3867 {
3868         struct list_head *pos;
3869         struct sctp_sockaddr_entry *addr;
3870         unsigned long flags;
3871         union sctp_addr temp;
3872         int cnt = 0;
3873         int addrlen;
3874
3875         sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3876         list_for_each(pos, &sctp_local_addr_list) {
3877                 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3878                 if ((PF_INET == sk->sk_family) && 
3879                     (AF_INET6 == addr->a.sa.sa_family))
3880                         continue;
3881                 memcpy(&temp, &addr->a, sizeof(temp));
3882                 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3883                                                                 &temp);
3884                 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3885                 temp.v4.sin_port = htons(port);
3886                 if (copy_to_user(to, &temp, addrlen)) {
3887                         sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3888                                                     flags);
3889                         return -EFAULT;
3890                 }
3891                 to += addrlen;
3892                 cnt ++;
3893                 if (cnt >= max_addrs) break;
3894         }
3895         sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3896
3897         return cnt;
3898 }
3899
3900 static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
3901                                     void __user **to, size_t space_left)
3902 {
3903         struct list_head *pos;
3904         struct sctp_sockaddr_entry *addr;
3905         unsigned long flags;
3906         union sctp_addr temp;
3907         int cnt = 0;
3908         int addrlen;
3909
3910         sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
3911         list_for_each(pos, &sctp_local_addr_list) {
3912                 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
3913                 if ((PF_INET == sk->sk_family) && 
3914                     (AF_INET6 == addr->a.sa.sa_family))
3915                         continue;
3916                 memcpy(&temp, &addr->a, sizeof(temp));
3917                 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
3918                                                                 &temp);
3919                 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
3920                 if(space_left<addrlen)
3921                         return -ENOMEM;
3922                 temp.v4.sin_port = htons(port);
3923                 if (copy_to_user(*to, &temp, addrlen)) {
3924                         sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
3925                                                     flags);
3926                         return -EFAULT;
3927                 }
3928                 *to += addrlen;
3929                 cnt ++;
3930                 space_left -= addrlen;
3931         }
3932         sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
3933
3934         return cnt;
3935 }
3936
3937 /* Old API for getting list of local addresses. Does not work for 32-bit
3938  * programs running on a 64-bit kernel
3939  */
3940 static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
3941                                            char __user *optval, int __user *optlen)
3942 {
3943         struct sctp_bind_addr *bp;
3944         struct sctp_association *asoc;
3945         struct list_head *pos;
3946         int cnt = 0;
3947         struct sctp_getaddrs_old getaddrs;
3948         struct sctp_sockaddr_entry *addr;
3949         void __user *to;
3950         union sctp_addr temp;
3951         struct sctp_sock *sp = sctp_sk(sk);
3952         int addrlen;
3953         rwlock_t *addr_lock;
3954         int err = 0;
3955
3956         if (len != sizeof(struct sctp_getaddrs_old))
3957                 return -EINVAL;
3958
3959         if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
3960                 return -EFAULT;
3961
3962         if (getaddrs.addr_num <= 0) return -EINVAL;
3963         /*
3964          *  For UDP-style sockets, id specifies the association to query.
3965          *  If the id field is set to the value '0' then the locally bound
3966          *  addresses are returned without regard to any particular
3967          *  association.
3968          */
3969         if (0 == getaddrs.assoc_id) {
3970                 bp = &sctp_sk(sk)->ep->base.bind_addr;
3971                 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
3972         } else {
3973                 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
3974                 if (!asoc)
3975                         return -EINVAL;
3976                 bp = &asoc->base.bind_addr;
3977                 addr_lock = &asoc->base.addr_lock;
3978         }
3979
3980         to = getaddrs.addrs;
3981
3982         sctp_read_lock(addr_lock);
3983
3984         /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
3985          * addresses from the global local address list.
3986          */
3987         if (sctp_list_single_entry(&bp->address_list)) {
3988                 addr = list_entry(bp->address_list.next,
3989                                   struct sctp_sockaddr_entry, list);
3990                 if (sctp_is_any(&addr->a)) {
3991                         cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
3992                                                            getaddrs.addr_num,
3993                                                            to);
3994                         if (cnt < 0) {
3995                                 err = cnt;
3996                                 goto unlock;
3997                         }
3998                         goto copy_getaddrs;             
3999                 }
4000         }
4001
4002         list_for_each(pos, &bp->address_list) {
4003                 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4004                 memcpy(&temp, &addr->a, sizeof(temp));
4005                 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4006                 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4007                 temp.v4.sin_port = htons(temp.v4.sin_port);
4008                 if (copy_to_user(to, &temp, addrlen)) {
4009                         err = -EFAULT;
4010                         goto unlock;
4011                 }
4012                 to += addrlen;
4013                 cnt ++;
4014                 if (cnt >= getaddrs.addr_num) break;
4015         }
4016
4017 copy_getaddrs:
4018         getaddrs.addr_num = cnt;
4019         if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
4020                 err = -EFAULT;
4021
4022 unlock:
4023         sctp_read_unlock(addr_lock);
4024         return err;
4025 }
4026
4027 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4028                                        char __user *optval, int __user *optlen)
4029 {
4030         struct sctp_bind_addr *bp;
4031         struct sctp_association *asoc;
4032         struct list_head *pos;
4033         int cnt = 0;
4034         struct sctp_getaddrs getaddrs;
4035         struct sctp_sockaddr_entry *addr;
4036         void __user *to;
4037         union sctp_addr temp;
4038         struct sctp_sock *sp = sctp_sk(sk);
4039         int addrlen;
4040         rwlock_t *addr_lock;
4041         int err = 0;
4042         size_t space_left;
4043         int bytes_copied;
4044
4045         if (len <= sizeof(struct sctp_getaddrs))
4046                 return -EINVAL;
4047
4048         if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4049                 return -EFAULT;
4050
4051         /*
4052          *  For UDP-style sockets, id specifies the association to query.
4053          *  If the id field is set to the value '0' then the locally bound
4054          *  addresses are returned without regard to any particular
4055          *  association.
4056          */
4057         if (0 == getaddrs.assoc_id) {
4058                 bp = &sctp_sk(sk)->ep->base.bind_addr;
4059                 addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
4060         } else {
4061                 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4062                 if (!asoc)
4063                         return -EINVAL;
4064                 bp = &asoc->base.bind_addr;
4065                 addr_lock = &asoc->base.addr_lock;
4066         }
4067
4068         to = optval + offsetof(struct sctp_getaddrs,addrs);
4069         space_left = len - sizeof(struct sctp_getaddrs) -
4070                          offsetof(struct sctp_getaddrs,addrs);
4071
4072         sctp_read_lock(addr_lock);
4073
4074         /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4075          * addresses from the global local address list.
4076          */
4077         if (sctp_list_single_entry(&bp->address_list)) {
4078                 addr = list_entry(bp->address_list.next,
4079                                   struct sctp_sockaddr_entry, list);
4080                 if (sctp_is_any(&addr->a)) {
4081                         cnt = sctp_copy_laddrs_to_user(sk, bp->port,
4082                                                        &to, space_left);
4083                         if (cnt < 0) {
4084                                 err = cnt;
4085                                 goto unlock;
4086                         }
4087                         goto copy_getaddrs;             
4088                 }
4089         }
4090
4091         list_for_each(pos, &bp->address_list) {
4092                 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
4093                 memcpy(&temp, &addr->a, sizeof(temp));
4094                 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4095                 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
4096                 if(space_left < addrlen)
4097                         return -ENOMEM; /*fixme: right error?*/
4098                 temp.v4.sin_port = htons(temp.v4.sin_port);
4099                 if (copy_to_user(to, &temp, addrlen)) {
4100                         err = -EFAULT;
4101                         goto unlock;
4102                 }
4103                 to += addrlen;
4104                 cnt ++;
4105                 space_left -= addrlen;
4106         }
4107
4108 copy_getaddrs:
4109         if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4110                 return -EFAULT;
4111         bytes_copied = ((char __user *)to) - optval;
4112         if (put_user(bytes_copied, optlen))
4113                 return -EFAULT;
4114
4115 unlock:
4116         sctp_read_unlock(addr_lock);
4117         return err;
4118 }
4119
4120 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4121  *
4122  * Requests that the local SCTP stack use the enclosed peer address as
4123  * the association primary.  The enclosed address must be one of the
4124  * association peer's addresses.
4125  */
4126 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4127                                         char __user *optval, int __user *optlen)
4128 {
4129         struct sctp_prim prim;
4130         struct sctp_association *asoc;
4131         struct sctp_sock *sp = sctp_sk(sk);
4132
4133         if (len != sizeof(struct sctp_prim))
4134                 return -EINVAL;
4135
4136         if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
4137                 return -EFAULT;
4138
4139         asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4140         if (!asoc)
4141                 return -EINVAL;
4142
4143         if (!asoc->peer.primary_path)
4144                 return -ENOTCONN;
4145         
4146         asoc->peer.primary_path->ipaddr.v4.sin_port =
4147                 htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
4148         memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4149                sizeof(union sctp_addr));
4150         asoc->peer.primary_path->ipaddr.v4.sin_port =
4151                 ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
4152
4153         sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4154                         (union sctp_addr *)&prim.ssp_addr);
4155
4156         if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
4157                 return -EFAULT;
4158
4159         return 0;
4160 }
4161
4162 /*
4163  * 7.1.11  Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
4164  *
4165  * Requests that the local endpoint set the specified Adaption Layer
4166  * Indication parameter for all future INIT and INIT-ACK exchanges.
4167  */
4168 static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
4169                                   char __user *optval, int __user *optlen)
4170 {
4171         struct sctp_setadaption adaption;
4172
4173         if (len != sizeof(struct sctp_setadaption))
4174                 return -EINVAL;
4175
4176         adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
4177         if (copy_to_user(optval, &adaption, len))
4178                 return -EFAULT;
4179
4180         return 0;
4181 }
4182
4183 /*
4184  *
4185  * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4186  *
4187  *   Applications that wish to use the sendto() system call may wish to
4188  *   specify a default set of parameters that would normally be supplied
4189  *   through the inclusion of ancillary data.  This socket option allows
4190  *   such an application to set the default sctp_sndrcvinfo structure.
4191
4192
4193  *   The application that wishes to use this socket option simply passes
4194  *   in to this call the sctp_sndrcvinfo structure defined in Section
4195  *   5.2.2) The input parameters accepted by this call include
4196  *   sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4197  *   sinfo_timetolive.  The user must provide the sinfo_assoc_id field in
4198  *   to this call if the caller is using the UDP model.
4199  *
4200  *   For getsockopt, it get the default sctp_sndrcvinfo structure.
4201  */
4202 static int sctp_getsockopt_default_send_param(struct sock *sk,
4203                                         int len, char __user *optval,
4204                                         int __user *optlen)
4205 {
4206         struct sctp_sndrcvinfo info;
4207         struct sctp_association *asoc;
4208         struct sctp_sock *sp = sctp_sk(sk);
4209
4210         if (len != sizeof(struct sctp_sndrcvinfo))
4211                 return -EINVAL;
4212         if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
4213                 return -EFAULT;
4214
4215         asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4216         if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4217                 return -EINVAL;
4218
4219         if (asoc) {
4220                 info.sinfo_stream = asoc->default_stream;
4221                 info.sinfo_flags = asoc->default_flags;
4222                 info.sinfo_ppid = asoc->default_ppid;
4223                 info.sinfo_context = asoc->default_context;
4224                 info.sinfo_timetolive = asoc->default_timetolive;
4225         } else {
4226                 info.sinfo_stream = sp->default_stream;
4227                 info.sinfo_flags = sp->default_flags;
4228                 info.sinfo_ppid = sp->default_ppid;
4229                 info.sinfo_context = sp->default_context;
4230                 info.sinfo_timetolive = sp->default_timetolive;
4231         }
4232
4233         if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
4234                 return -EFAULT;
4235
4236         return 0;
4237 }
4238
4239 /*
4240  *
4241  * 7.1.5 SCTP_NODELAY
4242  *
4243  * Turn on/off any Nagle-like algorithm.  This means that packets are
4244  * generally sent as soon as possible and no unnecessary delays are
4245  * introduced, at the cost of more packets in the network.  Expects an
4246  * integer boolean flag.
4247  */
4248
4249 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4250                                    char __user *optval, int __user *optlen)
4251 {
4252         int val;
4253
4254         if (len < sizeof(int))
4255                 return -EINVAL;
4256
4257         len = sizeof(int);
4258         val = (sctp_sk(sk)->nodelay == 1);
4259         if (put_user(len, optlen))
4260                 return -EFAULT;
4261         if (copy_to_user(optval, &val, len))
4262                 return -EFAULT;
4263         return 0;
4264 }
4265
4266 /*
4267  *
4268  * 7.1.1 SCTP_RTOINFO
4269  *
4270  * The protocol parameters used to initialize and bound retransmission
4271  * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4272  * and modify these parameters.
4273  * All parameters are time values, in milliseconds.  A value of 0, when
4274  * modifying the parameters, indicates that the current value should not
4275  * be changed.
4276  *
4277  */
4278 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4279                                 char __user *optval,
4280                                 int __user *optlen) {
4281         struct sctp_rtoinfo rtoinfo;
4282         struct sctp_association *asoc;
4283
4284         if (len != sizeof (struct sctp_rtoinfo))
4285                 return -EINVAL;
4286
4287         if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
4288                 return -EFAULT;
4289
4290         asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4291
4292         if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4293                 return -EINVAL;
4294
4295         /* Values corresponding to the specific association. */
4296         if (asoc) {
4297                 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4298                 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4299                 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4300         } else {
4301                 /* Values corresponding to the endpoint. */
4302                 struct sctp_sock *sp = sctp_sk(sk);
4303
4304                 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4305                 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4306                 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4307         }
4308
4309         if (put_user(len, optlen))
4310                 return -EFAULT;
4311
4312         if (copy_to_user(optval, &rtoinfo, len))
4313                 return -EFAULT;
4314
4315         return 0;
4316 }
4317
4318 /*
4319  *
4320  * 7.1.2 SCTP_ASSOCINFO
4321  *
4322  * This option is used to tune the the maximum retransmission attempts
4323  * of the association.
4324  * Returns an error if the new association retransmission value is
4325  * greater than the sum of the retransmission value  of the peer.
4326  * See [SCTP] for more information.
4327  *
4328  */
4329 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
4330                                      char __user *optval,
4331                                      int __user *optlen)
4332 {
4333
4334         struct sctp_assocparams assocparams;
4335         struct sctp_association *asoc;
4336         struct list_head *pos;
4337         int cnt = 0;
4338
4339         if (len != sizeof (struct sctp_assocparams))
4340                 return -EINVAL;
4341
4342         if (copy_from_user(&assocparams, optval,
4343                         sizeof (struct sctp_assocparams)))
4344                 return -EFAULT;
4345
4346         asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
4347
4348         if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
4349                 return -EINVAL;
4350
4351         /* Values correspoinding to the specific association */
4352         if (asoc) {
4353                 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
4354                 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
4355                 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
4356                 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
4357                                                 * 1000) +
4358                                                 (asoc->cookie_life.tv_usec
4359                                                 / 1000);
4360
4361                 list_for_each(pos, &asoc->peer.transport_addr_list) {
4362                         cnt ++;
4363                 }
4364
4365                 assocparams.sasoc_number_peer_destinations = cnt;
4366         } else {
4367                 /* Values corresponding to the endpoint */
4368                 struct sctp_sock *sp = sctp_sk(sk);
4369
4370                 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
4371                 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
4372                 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
4373                 assocparams.sasoc_cookie_life =
4374                                         sp->assocparams.sasoc_cookie_life;
4375                 assocparams.sasoc_number_peer_destinations =
4376                                         sp->assocparams.
4377                                         sasoc_number_peer_destinations;
4378         }
4379
4380         if (put_user(len, optlen))
4381                 return -EFAULT;
4382
4383         if (copy_to_user(optval, &assocparams, len))
4384                 return -EFAULT;
4385
4386         return 0;
4387 }
4388
4389 /*
4390  * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
4391  *
4392  * This socket option is a boolean flag which turns on or off mapped V4
4393  * addresses.  If this option is turned on and the socket is type
4394  * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
4395  * If this option is turned off, then no mapping will be done of V4
4396  * addresses and a user will receive both PF_INET6 and PF_INET type
4397  * addresses on the socket.
4398  */
4399 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
4400                                     char __user *optval, int __user *optlen)
4401 {
4402         int val;
4403         struct sctp_sock *sp = sctp_sk(sk);
4404
4405         if (len < sizeof(int))
4406                 return -EINVAL;
4407
4408         len = sizeof(int);
4409         val = sp->v4mapped;
4410         if (put_user(len, optlen))
4411                 return -EFAULT;
4412         if (copy_to_user(optval, &val, len))
4413                 return -EFAULT;
4414
4415         return 0;
4416 }
4417
4418 /*
4419  * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
4420  *
4421  * This socket option specifies the maximum size to put in any outgoing
4422  * SCTP chunk.  If a message is larger than this size it will be
4423  * fragmented by SCTP into the specified size.  Note that the underlying
4424  * SCTP implementation may fragment into smaller sized chunks when the
4425  * PMTU of the underlying association is smaller than the value set by
4426  * the user.
4427  */
4428 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
4429                                   char __user *optval, int __user *optlen)
4430 {
4431         int val;
4432
4433         if (len < sizeof(int))
4434                 return -EINVAL;
4435
4436         len = sizeof(int);
4437
4438         val = sctp_sk(sk)->user_frag;
4439         if (put_user(len, optlen))
4440                 return -EFAULT;
4441         if (copy_to_user(optval, &val, len))
4442                 return -EFAULT;
4443
4444         return 0;
4445 }
4446
4447 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
4448                                 char __user *optval, int __user *optlen)
4449 {
4450         int retval = 0;
4451         int len;
4452
4453         SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
4454                           sk, optname);
4455
4456         /* I can hardly begin to describe how wrong this is.  This is
4457          * so broken as to be worse than useless.  The API draft
4458          * REALLY is NOT helpful here...  I am not convinced that the
4459          * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
4460          * are at all well-founded.
4461          */
4462         if (level != SOL_SCTP) {
4463                 struct sctp_af *af = sctp_sk(sk)->pf->af;
4464
4465                 retval = af->getsockopt(sk, level, optname, optval, optlen);
4466                 return retval;
4467         }
4468
4469         if (get_user(len, optlen))
4470                 return -EFAULT;
4471
4472         sctp_lock_sock(sk);
4473
4474         switch (optname) {
4475         case SCTP_STATUS:
4476                 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
4477                 break;
4478         case SCTP_DISABLE_FRAGMENTS:
4479                 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
4480                                                            optlen);
4481                 break;
4482         case SCTP_EVENTS:
4483                 retval = sctp_getsockopt_events(sk, len, optval, optlen);
4484                 break;
4485         case SCTP_AUTOCLOSE:
4486                 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
4487                 break;
4488         case SCTP_SOCKOPT_PEELOFF:
4489                 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
4490                 break;
4491         case SCTP_PEER_ADDR_PARAMS:
4492                 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
4493                                                           optlen);
4494                 break;
4495         case SCTP_DELAYED_ACK_TIME:
4496                 retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
4497                                                           optlen);
4498                 break;
4499         case SCTP_INITMSG:
4500                 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
4501                 break;
4502         case SCTP_GET_PEER_ADDRS_NUM_OLD:
4503                 retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
4504                                                             optlen);
4505                 break;
4506         case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
4507                 retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
4508                                                              optlen);
4509                 break;
4510         case SCTP_GET_PEER_ADDRS_OLD:
4511                 retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
4512                                                         optlen);
4513                 break;
4514         case SCTP_GET_LOCAL_ADDRS_OLD:
4515                 retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
4516                                                          optlen);
4517                 break;
4518         case SCTP_GET_PEER_ADDRS:
4519                 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
4520                                                     optlen);
4521                 break;
4522         case SCTP_GET_LOCAL_ADDRS:
4523                 retval = sctp_getsockopt_local_addrs(sk, len, optval,
4524                                                      optlen);
4525                 break;
4526         case SCTP_DEFAULT_SEND_PARAM:
4527                 retval = sctp_getsockopt_default_send_param(sk, len,
4528                                                             optval, optlen);
4529                 break;
4530         case SCTP_PRIMARY_ADDR:
4531                 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
4532                 break;
4533         case SCTP_NODELAY:
4534                 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
4535                 break;
4536         case SCTP_RTOINFO:
4537                 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
4538                 break;
4539         case SCTP_ASSOCINFO:
4540                 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
4541                 break;
4542         case SCTP_I_WANT_MAPPED_V4_ADDR:
4543                 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
4544                 break;
4545         case SCTP_MAXSEG:
4546                 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
4547                 break;
4548         case SCTP_GET_PEER_ADDR_INFO:
4549                 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
4550                                                         optlen);
4551                 break;
4552         case SCTP_ADAPTION_LAYER:
4553                 retval = sctp_getsockopt_adaption_layer(sk, len, optval,
4554                                                         optlen);
4555                 break;
4556         default:
4557                 retval = -ENOPROTOOPT;
4558                 break;
4559         };
4560
4561         sctp_release_sock(sk);
4562         return retval;
4563 }
4564
4565 static void sctp_hash(struct sock *sk)
4566 {
4567         /* STUB */
4568 }
4569
4570 static void sctp_unhash(struct sock *sk)
4571 {
4572         /* STUB */
4573 }
4574
4575 /* Check if port is acceptable.  Possibly find first available port.
4576  *
4577  * The port hash table (contained in the 'global' SCTP protocol storage
4578  * returned by struct sctp_protocol *sctp_get_protocol()). The hash
4579  * table is an array of 4096 lists (sctp_bind_hashbucket). Each
4580  * list (the list number is the port number hashed out, so as you
4581  * would expect from a hash function, all the ports in a given list have
4582  * such a number that hashes out to the same list number; you were
4583  * expecting that, right?); so each list has a set of ports, with a
4584  * link to the socket (struct sock) that uses it, the port number and
4585  * a fastreuse flag (FIXME: NPI ipg).
4586  */
4587 static struct sctp_bind_bucket *sctp_bucket_create(
4588         struct sctp_bind_hashbucket *head, unsigned short snum);
4589
4590 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
4591 {
4592         struct sctp_bind_hashbucket *head; /* hash list */
4593         struct sctp_bind_bucket *pp; /* hash list port iterator */
4594         unsigned short snum;
4595         int ret;
4596
4597         /* NOTE:  Remember to put this back to net order. */
4598         addr->v4.sin_port = ntohs(addr->v4.sin_port);
4599         snum = addr->v4.sin_port;
4600
4601         SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
4602         sctp_local_bh_disable();
4603
4604         if (snum == 0) {
4605                 /* Search for an available port.
4606                  *
4607                  * 'sctp_port_rover' was the last port assigned, so
4608                  * we start to search from 'sctp_port_rover +
4609                  * 1'. What we do is first check if port 'rover' is
4610                  * already in the hash table; if not, we use that; if
4611                  * it is, we try next.
4612                  */
4613                 int low = sysctl_local_port_range[0];
4614                 int high = sysctl_local_port_range[1];
4615                 int remaining = (high - low) + 1;
4616                 int rover;
4617                 int index;
4618
4619                 sctp_spin_lock(&sctp_port_alloc_lock);
4620                 rover = sctp_port_rover;
4621                 do {
4622                         rover++;
4623                         if ((rover < low) || (rover > high))
4624                                 rover = low;
4625                         index = sctp_phashfn(rover);
4626                         head = &sctp_port_hashtable[index];
4627                         sctp_spin_lock(&head->lock);
4628                         for (pp = head->chain; pp; pp = pp->next)
4629                                 if (pp->port == rover)
4630                                         goto next;
4631                         break;
4632                 next:
4633                         sctp_spin_unlock(&head->lock);
4634                 } while (--remaining > 0);
4635                 sctp_port_rover = rover;
4636                 sctp_spin_unlock(&sctp_port_alloc_lock);
4637
4638                 /* Exhausted local port range during search? */
4639                 ret = 1;
4640                 if (remaining <= 0)
4641                         goto fail;
4642
4643                 /* OK, here is the one we will use.  HEAD (the port
4644                  * hash table list entry) is non-NULL and we hold it's
4645                  * mutex.
4646                  */
4647                 snum = rover;
4648         } else {
4649                 /* We are given an specific port number; we verify
4650                  * that it is not being used. If it is used, we will
4651                  * exahust the search in the hash list corresponding
4652                  * to the port number (snum) - we detect that with the
4653                  * port iterator, pp being NULL.
4654                  */
4655                 head = &sctp_port_hashtable[sctp_phashfn(snum)];
4656                 sctp_spin_lock(&head->lock);
4657                 for (pp = head->chain; pp; pp = pp->next) {
4658                         if (pp->port == snum)
4659                                 goto pp_found;
4660                 }
4661         }
4662         pp = NULL;
4663         goto pp_not_found;
4664 pp_found:
4665         if (!hlist_empty(&pp->owner)) {
4666                 /* We had a port hash table hit - there is an
4667                  * available port (pp != NULL) and it is being
4668                  * used by other socket (pp->owner not empty); that other
4669                  * socket is going to be sk2.
4670                  */
4671                 int reuse = sk->sk_reuse;
4672                 struct sock *sk2;
4673                 struct hlist_node *node;
4674
4675                 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
4676                 if (pp->fastreuse && sk->sk_reuse)
4677                         goto success;
4678
4679                 /* Run through the list of sockets bound to the port
4680                  * (pp->port) [via the pointers bind_next and
4681                  * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
4682                  * we get the endpoint they describe and run through
4683                  * the endpoint's list of IP (v4 or v6) addresses,
4684                  * comparing each of the addresses with the address of
4685                  * the socket sk. If we find a match, then that means
4686                  * that this port/socket (sk) combination are already
4687                  * in an endpoint.
4688                  */
4689                 sk_for_each_bound(sk2, node, &pp->owner) {
4690                         struct sctp_endpoint *ep2;
4691                         ep2 = sctp_sk(sk2)->ep;
4692
4693                         if (reuse && sk2->sk_reuse)
4694                                 continue;
4695
4696                         if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
4697                                                  sctp_sk(sk))) {
4698                                 ret = (long)sk2;
4699                                 goto fail_unlock;
4700                         }
4701                 }
4702                 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
4703         }
4704 pp_not_found:
4705         /* If there was a hash table miss, create a new port.  */
4706         ret = 1;
4707         if (!pp && !(pp = sctp_bucket_create(head, snum)))
4708                 goto fail_unlock;
4709
4710         /* In either case (hit or miss), make sure fastreuse is 1 only
4711          * if sk->sk_reuse is too (that is, if the caller requested
4712          * SO_REUSEADDR on this socket -sk-).
4713          */
4714         if (hlist_empty(&pp->owner))
4715                 pp->fastreuse = sk->sk_reuse ? 1 : 0;
4716         else if (pp->fastreuse && !sk->sk_reuse)
4717                 pp->fastreuse = 0;
4718
4719         /* We are set, so fill up all the data in the hash table
4720          * entry, tie the socket list information with the rest of the
4721          * sockets FIXME: Blurry, NPI (ipg).
4722          */
4723 success:
4724         inet_sk(sk)->num = snum;
4725         if (!sctp_sk(sk)->bind_hash) {
4726                 sk_add_bind_node(sk, &pp->owner);
4727                 sctp_sk(sk)->bind_hash = pp;
4728         }
4729         ret = 0;
4730
4731 fail_unlock:
4732         sctp_spin_unlock(&head->lock);
4733
4734 fail:
4735         sctp_local_bh_enable();
4736         addr->v4.sin_port = htons(addr->v4.sin_port);
4737         return ret;
4738 }
4739
4740 /* Assign a 'snum' port to the socket.  If snum == 0, an ephemeral
4741  * port is requested.
4742  */
4743 static int sctp_get_port(struct sock *sk, unsigned short snum)
4744 {
4745         long ret;
4746         union sctp_addr addr;
4747         struct sctp_af *af = sctp_sk(sk)->pf->af;
4748
4749         /* Set up a dummy address struct from the sk. */
4750         af->from_sk(&addr, sk);
4751         addr.v4.sin_port = htons(snum);
4752
4753         /* Note: sk->sk_num gets filled in if ephemeral port request. */
4754         ret = sctp_get_port_local(sk, &addr);
4755
4756         return (ret ? 1 : 0);
4757 }
4758
4759 /*
4760  * 3.1.3 listen() - UDP Style Syntax
4761  *
4762  *   By default, new associations are not accepted for UDP style sockets.
4763  *   An application uses listen() to mark a socket as being able to
4764  *   accept new associations.
4765  */
4766 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
4767 {
4768         struct sctp_sock *sp = sctp_sk(sk);
4769         struct sctp_endpoint *ep = sp->ep;
4770
4771         /* Only UDP style sockets that are not peeled off are allowed to
4772          * listen().
4773          */
4774         if (!sctp_style(sk, UDP))
4775                 return -EINVAL;
4776
4777         /* If backlog is zero, disable listening. */
4778         if (!backlog) {
4779                 if (sctp_sstate(sk, CLOSED))
4780                         return 0;
4781                 
4782                 sctp_unhash_endpoint(ep);
4783                 sk->sk_state = SCTP_SS_CLOSED;
4784         }
4785
4786         /* Return if we are already listening. */
4787         if (sctp_sstate(sk, LISTENING))
4788                 return 0;
4789                 
4790         /*
4791          * If a bind() or sctp_bindx() is not called prior to a listen()
4792          * call that allows new associations to be accepted, the system
4793          * picks an ephemeral port and will choose an address set equivalent
4794          * to binding with a wildcard address.
4795          *
4796          * This is not currently spelled out in the SCTP sockets
4797          * extensions draft, but follows the practice as seen in TCP
4798          * sockets.
4799          */
4800         if (!ep->base.bind_addr.port) {
4801                 if (sctp_autobind(sk))
4802                         return -EAGAIN;
4803         }
4804         sk->sk_state = SCTP_SS_LISTENING;
4805         sctp_hash_endpoint(ep);
4806         return 0;
4807 }
4808
4809 /*
4810  * 4.1.3 listen() - TCP Style Syntax
4811  *
4812  *   Applications uses listen() to ready the SCTP endpoint for accepting
4813  *   inbound associations.
4814  */
4815 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
4816 {
4817         struct sctp_sock *sp = sctp_sk(sk);
4818         struct sctp_endpoint *ep = sp->ep;
4819
4820         /* If backlog is zero, disable listening. */
4821         if (!backlog) {
4822                 if (sctp_sstate(sk, CLOSED))
4823                         return 0;
4824                 
4825                 sctp_unhash_endpoint(ep);
4826                 sk->sk_state = SCTP_SS_CLOSED;
4827         }
4828
4829         if (sctp_sstate(sk, LISTENING))
4830                 return 0;
4831
4832         /*
4833          * If a bind() or sctp_bindx() is not called prior to a listen()
4834          * call that allows new associations to be accepted, the system
4835          * picks an ephemeral port and will choose an address set equivalent
4836          * to binding with a wildcard address.
4837          *
4838          * This is not currently spelled out in the SCTP sockets
4839          * extensions draft, but follows the practice as seen in TCP
4840          * sockets.
4841          */
4842         if (!ep->base.bind_addr.port) {
4843                 if (sctp_autobind(sk))
4844                         return -EAGAIN;
4845         }
4846         sk->sk_state = SCTP_SS_LISTENING;
4847         sk->sk_max_ack_backlog = backlog;
4848         sctp_hash_endpoint(ep);
4849         return 0;
4850 }
4851
4852 /*
4853  *  Move a socket to LISTENING state.
4854  */
4855 int sctp_inet_listen(struct socket *sock, int backlog)
4856 {
4857         struct sock *sk = sock->sk;
4858         struct crypto_tfm *tfm=NULL;
4859         int err = -EINVAL;
4860
4861         if (unlikely(backlog < 0))
4862                 goto out;
4863
4864         sctp_lock_sock(sk);
4865
4866         if (sock->state != SS_UNCONNECTED)
4867                 goto out;
4868
4869         /* Allocate HMAC for generating cookie. */
4870         if (sctp_hmac_alg) {
4871                 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
4872                 if (!tfm) {
4873                         err = -ENOSYS;
4874                         goto out;
4875                 }
4876         }
4877
4878         switch (sock->type) {
4879         case SOCK_SEQPACKET:
4880                 err = sctp_seqpacket_listen(sk, backlog);
4881                 break;
4882         case SOCK_STREAM:
4883                 err = sctp_stream_listen(sk, backlog);
4884                 break;
4885         default:
4886                 break;
4887         };
4888         if (err)
4889                 goto cleanup;
4890
4891         /* Store away the transform reference. */
4892         sctp_sk(sk)->hmac = tfm;
4893 out:
4894         sctp_release_sock(sk);
4895         return err;
4896 cleanup:
4897         sctp_crypto_free_tfm(tfm);
4898         goto out;
4899 }
4900
4901 /*
4902  * This function is done by modeling the current datagram_poll() and the
4903  * tcp_poll().  Note that, based on these implementations, we don't
4904  * lock the socket in this function, even though it seems that,
4905  * ideally, locking or some other mechanisms can be used to ensure
4906  * the integrity of the counters (sndbuf and wmem_alloc) used
4907  * in this place.  We assume that we don't need locks either until proven
4908  * otherwise.
4909  *
4910  * Another thing to note is that we include the Async I/O support
4911  * here, again, by modeling the current TCP/UDP code.  We don't have
4912  * a good way to test with it yet.
4913  */
4914 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
4915 {
4916         struct sock *sk = sock->sk;
4917         struct sctp_sock *sp = sctp_sk(sk);
4918         unsigned int mask;
4919
4920         poll_wait(file, sk->sk_sleep, wait);
4921
4922         /* A TCP-style listening socket becomes readable when the accept queue
4923          * is not empty.
4924          */
4925         if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4926                 return (!list_empty(&sp->ep->asocs)) ?
4927                         (POLLIN | POLLRDNORM) : 0;
4928
4929         mask = 0;
4930
4931         /* Is there any exceptional events?  */
4932         if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
4933                 mask |= POLLERR;
4934         if (sk->sk_shutdown & RCV_SHUTDOWN)
4935                 mask |= POLLRDHUP;
4936         if (sk->sk_shutdown == SHUTDOWN_MASK)
4937                 mask |= POLLHUP;
4938
4939         /* Is it readable?  Reconsider this code with TCP-style support.  */
4940         if (!skb_queue_empty(&sk->sk_receive_queue) ||
4941             (sk->sk_shutdown & RCV_SHUTDOWN))
4942                 mask |= POLLIN | POLLRDNORM;
4943
4944         /* The association is either gone or not ready.  */
4945         if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
4946                 return mask;
4947
4948         /* Is it writable?  */
4949         if (sctp_writeable(sk)) {
4950                 mask |= POLLOUT | POLLWRNORM;
4951         } else {
4952                 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
4953                 /*
4954                  * Since the socket is not locked, the buffer
4955                  * might be made available after the writeable check and
4956                  * before the bit is set.  This could cause a lost I/O
4957                  * signal.  tcp_poll() has a race breaker for this race
4958                  * condition.  Based on their implementation, we put
4959                  * in the following code to cover it as well.
4960                  */
4961                 if (sctp_writeable(sk))
4962                         mask |= POLLOUT | POLLWRNORM;
4963         }
4964         return mask;
4965 }
4966
4967 /********************************************************************
4968  * 2nd Level Abstractions
4969  ********************************************************************/
4970
4971 static struct sctp_bind_bucket *sctp_bucket_create(
4972         struct sctp_bind_hashbucket *head, unsigned short snum)
4973 {
4974         struct sctp_bind_bucket *pp;
4975
4976         pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
4977         SCTP_DBG_OBJCNT_INC(bind_bucket);
4978         if (pp) {
4979                 pp->port = snum;
4980                 pp->fastreuse = 0;
4981                 INIT_HLIST_HEAD(&pp->owner);
4982                 if ((pp->next = head->chain) != NULL)
4983                         pp->next->pprev = &pp->next;
4984                 head->chain = pp;
4985                 pp->pprev = &head->chain;
4986         }
4987         return pp;
4988 }
4989
4990 /* Caller must hold hashbucket lock for this tb with local BH disabled */
4991 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
4992 {
4993         if (hlist_empty(&pp->owner)) {
4994                 if (pp->next)
4995                         pp->next->pprev = pp->pprev;
4996                 *(pp->pprev) = pp->next;
4997                 kmem_cache_free(sctp_bucket_cachep, pp);
4998                 SCTP_DBG_OBJCNT_DEC(bind_bucket);
4999         }
5000 }
5001
5002 /* Release this socket's reference to a local port.  */
5003 static inline void __sctp_put_port(struct sock *sk)
5004 {
5005         struct sctp_bind_hashbucket *head =
5006                 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
5007         struct sctp_bind_bucket *pp;
5008
5009         sctp_spin_lock(&head->lock);
5010         pp = sctp_sk(sk)->bind_hash;
5011         __sk_del_bind_node(sk);
5012         sctp_sk(sk)->bind_hash = NULL;
5013         inet_sk(sk)->num = 0;
5014         sctp_bucket_destroy(pp);
5015         sctp_spin_unlock(&head->lock);
5016 }
5017
5018 void sctp_put_port(struct sock *sk)
5019 {
5020         sctp_local_bh_disable();
5021         __sctp_put_port(sk);
5022         sctp_local_bh_enable();
5023 }
5024
5025 /*
5026  * The system picks an ephemeral port and choose an address set equivalent
5027  * to binding with a wildcard address.
5028  * One of those addresses will be the primary address for the association.
5029  * This automatically enables the multihoming capability of SCTP.
5030  */
5031 static int sctp_autobind(struct sock *sk)
5032 {
5033         union sctp_addr autoaddr;
5034         struct sctp_af *af;
5035         unsigned short port;
5036
5037         /* Initialize a local sockaddr structure to INADDR_ANY. */
5038         af = sctp_sk(sk)->pf->af;
5039
5040         port = htons(inet_sk(sk)->num);
5041         af->inaddr_any(&autoaddr, port);
5042
5043         return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
5044 }
5045
5046 /* Parse out IPPROTO_SCTP CMSG headers.  Perform only minimal validation.
5047  *
5048  * From RFC 2292
5049  * 4.2 The cmsghdr Structure *
5050  *
5051  * When ancillary data is sent or received, any number of ancillary data
5052  * objects can be specified by the msg_control and msg_controllen members of
5053  * the msghdr structure, because each object is preceded by
5054  * a cmsghdr structure defining the object's length (the cmsg_len member).
5055  * Historically Berkeley-derived implementations have passed only one object
5056  * at a time, but this API allows multiple objects to be
5057  * passed in a single call to sendmsg() or recvmsg(). The following example
5058  * shows two ancillary data objects in a control buffer.
5059  *
5060  *   |<--------------------------- msg_controllen -------------------------->|
5061  *   |                                                                       |
5062  *
5063  *   |<----- ancillary data object ----->|<----- ancillary data object ----->|
5064  *
5065  *   |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
5066  *   |                                   |                                   |
5067  *
5068  *   |<---------- cmsg_len ---------->|  |<--------- cmsg_len ----------->|  |
5069  *
5070  *   |<--------- CMSG_LEN() --------->|  |<-------- CMSG_LEN() ---------->|  |
5071  *   |                                |  |                                |  |
5072  *
5073  *   +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5074  *   |cmsg_|cmsg_|cmsg_|XX|           |XX|cmsg_|cmsg_|cmsg_|XX|           |XX|
5075  *
5076  *   |len  |level|type |XX|cmsg_data[]|XX|len  |level|type |XX|cmsg_data[]|XX|
5077  *
5078  *   +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
5079  *    ^
5080  *    |
5081  *
5082  * msg_control
5083  * points here
5084  */
5085 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
5086                                   sctp_cmsgs_t *cmsgs)
5087 {
5088         struct cmsghdr *cmsg;
5089
5090         for (cmsg = CMSG_FIRSTHDR(msg);
5091              cmsg != NULL;
5092              cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
5093                 if (!CMSG_OK(msg, cmsg))
5094                         return -EINVAL;
5095
5096                 /* Should we parse this header or ignore?  */
5097                 if (cmsg->cmsg_level != IPPROTO_SCTP)
5098                         continue;
5099
5100                 /* Strictly check lengths following example in SCM code.  */
5101                 switch (cmsg->cmsg_type) {
5102                 case SCTP_INIT:
5103                         /* SCTP Socket API Extension
5104                          * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
5105                          *
5106                          * This cmsghdr structure provides information for
5107                          * initializing new SCTP associations with sendmsg().
5108                          * The SCTP_INITMSG socket option uses this same data
5109                          * structure.  This structure is not used for
5110                          * recvmsg().
5111                          *
5112                          * cmsg_level    cmsg_type      cmsg_data[]
5113                          * ------------  ------------   ----------------------
5114                          * IPPROTO_SCTP  SCTP_INIT      struct sctp_initmsg
5115                          */
5116                         if (cmsg->cmsg_len !=
5117                             CMSG_LEN(sizeof(struct sctp_initmsg)))
5118                                 return -EINVAL;
5119                         cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
5120                         break;
5121
5122                 case SCTP_SNDRCV:
5123                         /* SCTP Socket API Extension
5124                          * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
5125                          *
5126                          * This cmsghdr structure specifies SCTP options for
5127                          * sendmsg() and describes SCTP header information
5128                          * about a received message through recvmsg().
5129                          *
5130                          * cmsg_level    cmsg_type      cmsg_data[]
5131                          * ------------  ------------   ----------------------
5132                          * IPPROTO_SCTP  SCTP_SNDRCV    struct sctp_sndrcvinfo
5133                          */
5134                         if (cmsg->cmsg_len !=
5135                             CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
5136                                 return -EINVAL;
5137
5138                         cmsgs->info =
5139                                 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
5140
5141                         /* Minimally, validate the sinfo_flags. */
5142                         if (cmsgs->info->sinfo_flags &
5143                             ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
5144                               SCTP_ABORT | SCTP_EOF))
5145                                 return -EINVAL;
5146                         break;
5147
5148                 default:
5149                         return -EINVAL;
5150                 };
5151         }
5152         return 0;
5153 }
5154
5155 /*
5156  * Wait for a packet..
5157  * Note: This function is the same function as in core/datagram.c
5158  * with a few modifications to make lksctp work.
5159  */
5160 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
5161 {
5162         int error;
5163         DEFINE_WAIT(wait);
5164
5165         prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5166
5167         /* Socket errors? */
5168         error = sock_error(sk);
5169         if (error)
5170                 goto out;
5171
5172         if (!skb_queue_empty(&sk->sk_receive_queue))
5173                 goto ready;
5174
5175         /* Socket shut down?  */
5176         if (sk->sk_shutdown & RCV_SHUTDOWN)
5177                 goto out;
5178
5179         /* Sequenced packets can come disconnected.  If so we report the
5180          * problem.
5181          */
5182         error = -ENOTCONN;
5183
5184         /* Is there a good reason to think that we may receive some data?  */
5185         if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
5186                 goto out;
5187
5188         /* Handle signals.  */
5189         if (signal_pending(current))
5190                 goto interrupted;
5191
5192         /* Let another process have a go.  Since we are going to sleep
5193          * anyway.  Note: This may cause odd behaviors if the message
5194          * does not fit in the user's buffer, but this seems to be the
5195          * only way to honor MSG_DONTWAIT realistically.
5196          */
5197         sctp_release_sock(sk);
5198         *timeo_p = schedule_timeout(*timeo_p);
5199         sctp_lock_sock(sk);
5200
5201 ready:
5202         finish_wait(sk->sk_sleep, &wait);
5203         return 0;
5204
5205 interrupted:
5206         error = sock_intr_errno(*timeo_p);
5207
5208 out:
5209         finish_wait(sk->sk_sleep, &wait);
5210         *err = error;
5211         return error;
5212 }
5213
5214 /* Receive a datagram.
5215  * Note: This is pretty much the same routine as in core/datagram.c
5216  * with a few changes to make lksctp work.
5217  */
5218 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
5219                                               int noblock, int *err)
5220 {
5221         int error;
5222         struct sk_buff *skb;
5223         long timeo;
5224
5225         timeo = sock_rcvtimeo(sk, noblock);
5226
5227         SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
5228                           timeo, MAX_SCHEDULE_TIMEOUT);
5229
5230         do {
5231                 /* Again only user level code calls this function,
5232                  * so nothing interrupt level
5233                  * will suddenly eat the receive_queue.
5234                  *
5235                  *  Look at current nfs client by the way...
5236                  *  However, this function was corrent in any case. 8)
5237                  */
5238                 if (flags & MSG_PEEK) {
5239                         spin_lock_bh(&sk->sk_receive_queue.lock);
5240                         skb = skb_peek(&sk->sk_receive_queue);
5241                         if (skb)
5242                                 atomic_inc(&skb->users);
5243                         spin_unlock_bh(&sk->sk_receive_queue.lock);
5244                 } else {
5245                         skb = skb_dequeue(&sk->sk_receive_queue);
5246                 }
5247
5248                 if (skb)
5249                         return skb;
5250
5251                 /* Caller is allowed not to check sk->sk_err before calling. */
5252                 error = sock_error(sk);
5253                 if (error)
5254                         goto no_packet;
5255
5256                 if (sk->sk_shutdown & RCV_SHUTDOWN)
5257                         break;
5258
5259                 /* User doesn't want to wait.  */
5260                 error = -EAGAIN;
5261                 if (!timeo)
5262                         goto no_packet;
5263         } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
5264
5265         return NULL;
5266
5267 no_packet:
5268         *err = error;
5269         return NULL;
5270 }
5271
5272 /* If sndbuf has changed, wake up per association sndbuf waiters.  */
5273 static void __sctp_write_space(struct sctp_association *asoc)
5274 {
5275         struct sock *sk = asoc->base.sk;
5276         struct socket *sock = sk->sk_socket;
5277
5278         if ((sctp_wspace(asoc) > 0) && sock) {
5279                 if (waitqueue_active(&asoc->wait))
5280                         wake_up_interruptible(&asoc->wait);
5281
5282                 if (sctp_writeable(sk)) {
5283                         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
5284                                 wake_up_interruptible(sk->sk_sleep);
5285
5286                         /* Note that we try to include the Async I/O support
5287                          * here by modeling from the current TCP/UDP code.
5288                          * We have not tested with it yet.
5289                          */
5290                         if (sock->fasync_list &&
5291                             !(sk->sk_shutdown & SEND_SHUTDOWN))
5292                                 sock_wake_async(sock, 2, POLL_OUT);
5293                 }
5294         }
5295 }
5296
5297 /* Do accounting for the sndbuf space.
5298  * Decrement the used sndbuf space of the corresponding association by the
5299  * data size which was just transmitted(freed).
5300  */
5301 static void sctp_wfree(struct sk_buff *skb)
5302 {
5303         struct sctp_association *asoc;
5304         struct sctp_chunk *chunk;
5305         struct sock *sk;
5306
5307         /* Get the saved chunk pointer.  */
5308         chunk = *((struct sctp_chunk **)(skb->cb));
5309         asoc = chunk->asoc;
5310         sk = asoc->base.sk;
5311         asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
5312                                 sizeof(struct sk_buff) +
5313                                 sizeof(struct sctp_chunk);
5314
5315         atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
5316
5317         sock_wfree(skb);
5318         __sctp_write_space(asoc);
5319
5320         sctp_association_put(asoc);
5321 }
5322
5323 /* Helper function to wait for space in the sndbuf.  */
5324 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
5325                                 size_t msg_len)
5326 {
5327         struct sock *sk = asoc->base.sk;
5328         int err = 0;
5329         long current_timeo = *timeo_p;
5330         DEFINE_WAIT(wait);
5331
5332         SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
5333                           asoc, (long)(*timeo_p), msg_len);
5334
5335         /* Increment the association's refcnt.  */
5336         sctp_association_hold(asoc);
5337
5338         /* Wait on the association specific sndbuf space. */
5339         for (;;) {
5340                 prepare_to_wait_exclusive(&asoc->wait, &wait,
5341                                           TASK_INTERRUPTIBLE);
5342                 if (!*timeo_p)
5343                         goto do_nonblock;
5344                 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5345                     asoc->base.dead)
5346                         goto do_error;
5347                 if (signal_pending(current))
5348                         goto do_interrupted;
5349                 if (msg_len <= sctp_wspace(asoc))
5350                         break;
5351
5352                 /* Let another process have a go.  Since we are going
5353                  * to sleep anyway.
5354                  */
5355                 sctp_release_sock(sk);
5356                 current_timeo = schedule_timeout(current_timeo);
5357                 BUG_ON(sk != asoc->base.sk);
5358                 sctp_lock_sock(sk);
5359
5360                 *timeo_p = current_timeo;
5361         }
5362
5363 out:
5364         finish_wait(&asoc->wait, &wait);
5365
5366         /* Release the association's refcnt.  */
5367         sctp_association_put(asoc);
5368
5369         return err;
5370
5371 do_error:
5372         err = -EPIPE;
5373         goto out;
5374
5375 do_interrupted:
5376         err = sock_intr_errno(*timeo_p);
5377         goto out;
5378
5379 do_nonblock:
5380         err = -EAGAIN;
5381         goto out;
5382 }
5383
5384 /* If socket sndbuf has changed, wake up all per association waiters.  */
5385 void sctp_write_space(struct sock *sk)
5386 {
5387         struct sctp_association *asoc;
5388         struct list_head *pos;
5389
5390         /* Wake up the tasks in each wait queue.  */
5391         list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
5392                 asoc = list_entry(pos, struct sctp_association, asocs);
5393                 __sctp_write_space(asoc);
5394         }
5395 }
5396
5397 /* Is there any sndbuf space available on the socket?
5398  *
5399  * Note that sk_wmem_alloc is the sum of the send buffers on all of the
5400  * associations on the same socket.  For a UDP-style socket with
5401  * multiple associations, it is possible for it to be "unwriteable"
5402  * prematurely.  I assume that this is acceptable because
5403  * a premature "unwriteable" is better than an accidental "writeable" which
5404  * would cause an unwanted block under certain circumstances.  For the 1-1
5405  * UDP-style sockets or TCP-style sockets, this code should work.
5406  *  - Daisy
5407  */
5408 static int sctp_writeable(struct sock *sk)
5409 {
5410         int amt = 0;
5411
5412         amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
5413         if (amt < 0)
5414                 amt = 0;
5415         return amt;
5416 }
5417
5418 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
5419  * returns immediately with EINPROGRESS.
5420  */
5421 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
5422 {
5423         struct sock *sk = asoc->base.sk;
5424         int err = 0;
5425         long current_timeo = *timeo_p;
5426         DEFINE_WAIT(wait);
5427
5428         SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
5429                           (long)(*timeo_p));
5430
5431         /* Increment the association's refcnt.  */
5432         sctp_association_hold(asoc);
5433
5434         for (;;) {
5435                 prepare_to_wait_exclusive(&asoc->wait, &wait,
5436                                           TASK_INTERRUPTIBLE);
5437                 if (!*timeo_p)
5438                         goto do_nonblock;
5439                 if (sk->sk_shutdown & RCV_SHUTDOWN)
5440                         break;
5441                 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
5442                     asoc->base.dead)
5443                         goto do_error;
5444                 if (signal_pending(current))
5445                         goto do_interrupted;
5446
5447                 if (sctp_state(asoc, ESTABLISHED))
5448                         break;
5449
5450                 /* Let another process have a go.  Since we are going
5451                  * to sleep anyway.
5452                  */
5453                 sctp_release_sock(sk);
5454                 current_timeo = schedule_timeout(current_timeo);
5455                 sctp_lock_sock(sk);
5456
5457                 *timeo_p = current_timeo;
5458         }
5459
5460 out:
5461         finish_wait(&asoc->wait, &wait);
5462
5463         /* Release the association's refcnt.  */
5464         sctp_association_put(asoc);
5465
5466         return err;
5467
5468 do_error:
5469         if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
5470                 err = -ETIMEDOUT;
5471         else
5472                 err = -ECONNREFUSED;
5473         goto out;
5474
5475 do_interrupted:
5476         err = sock_intr_errno(*timeo_p);
5477         goto out;
5478
5479 do_nonblock:
5480         err = -EINPROGRESS;
5481         goto out;
5482 }
5483
5484 static int sctp_wait_for_accept(struct sock *sk, long timeo)
5485 {
5486         struct sctp_endpoint *ep;
5487         int err = 0;
5488         DEFINE_WAIT(wait);
5489
5490         ep = sctp_sk(sk)->ep;
5491
5492
5493         for (;;) {
5494                 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
5495                                           TASK_INTERRUPTIBLE);
5496
5497                 if (list_empty(&ep->asocs)) {
5498                         sctp_release_sock(sk);
5499                         timeo = schedule_timeout(timeo);
5500                         sctp_lock_sock(sk);
5501                 }
5502
5503                 err = -EINVAL;
5504                 if (!sctp_sstate(sk, LISTENING))
5505                         break;
5506
5507                 err = 0;
5508                 if (!list_empty(&ep->asocs))
5509                         break;
5510
5511                 err = sock_intr_errno(timeo);
5512                 if (signal_pending(current))
5513                         break;
5514
5515                 err = -EAGAIN;
5516                 if (!timeo)
5517                         break;
5518         }
5519
5520         finish_wait(sk->sk_sleep, &wait);
5521
5522         return err;
5523 }
5524
5525 void sctp_wait_for_close(struct sock *sk, long timeout)
5526 {
5527         DEFINE_WAIT(wait);
5528
5529         do {
5530                 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
5531                 if (list_empty(&sctp_sk(sk)->ep->asocs))
5532                         break;
5533                 sctp_release_sock(sk);
5534                 timeout = schedule_timeout(timeout);
5535                 sctp_lock_sock(sk);
5536         } while (!signal_pending(current) && timeout);
5537
5538         finish_wait(sk->sk_sleep, &wait);
5539 }
5540
5541 /* Populate the fields of the newsk from the oldsk and migrate the assoc
5542  * and its messages to the newsk.
5543  */
5544 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
5545                               struct sctp_association *assoc,
5546                               sctp_socket_type_t type)
5547 {
5548         struct sctp_sock *oldsp = sctp_sk(oldsk);
5549         struct sctp_sock *newsp = sctp_sk(newsk);
5550         struct sctp_bind_bucket *pp; /* hash list port iterator */
5551         struct sctp_endpoint *newep = newsp->ep;
5552         struct sk_buff *skb, *tmp;
5553         struct sctp_ulpevent *event;
5554         int flags = 0;
5555
5556         /* Migrate socket buffer sizes and all the socket level options to the
5557          * new socket.
5558          */
5559         newsk->sk_sndbuf = oldsk->sk_sndbuf;
5560         newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
5561         /* Brute force copy old sctp opt. */
5562         inet_sk_copy_descendant(newsk, oldsk);
5563
5564         /* Restore the ep value that was overwritten with the above structure
5565          * copy.
5566          */
5567         newsp->ep = newep;
5568         newsp->hmac = NULL;
5569
5570         /* Hook this new socket in to the bind_hash list. */
5571         pp = sctp_sk(oldsk)->bind_hash;
5572         sk_add_bind_node(newsk, &pp->owner);
5573         sctp_sk(newsk)->bind_hash = pp;
5574         inet_sk(newsk)->num = inet_sk(oldsk)->num;
5575
5576         /* Copy the bind_addr list from the original endpoint to the new
5577          * endpoint so that we can handle restarts properly
5578          */
5579         if (assoc->peer.ipv4_address)
5580                 flags |= SCTP_ADDR4_PEERSUPP;
5581         if (assoc->peer.ipv6_address)
5582                 flags |= SCTP_ADDR6_PEERSUPP;
5583         sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
5584                              &oldsp->ep->base.bind_addr,
5585                              SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
5586
5587         /* Move any messages in the old socket's receive queue that are for the
5588          * peeled off association to the new socket's receive queue.
5589          */
5590         sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
5591                 event = sctp_skb2event(skb);
5592                 if (event->asoc == assoc) {
5593                         sock_rfree(skb);
5594                         __skb_unlink(skb, &oldsk->sk_receive_queue);
5595                         __skb_queue_tail(&newsk->sk_receive_queue, skb);
5596                         skb_set_owner_r(skb, newsk);
5597                 }
5598         }
5599
5600         /* Clean up any messages pending delivery due to partial
5601          * delivery.   Three cases:
5602          * 1) No partial deliver;  no work.
5603          * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
5604          * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
5605          */
5606         skb_queue_head_init(&newsp->pd_lobby);
5607         sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
5608
5609         if (sctp_sk(oldsk)->pd_mode) {
5610                 struct sk_buff_head *queue;
5611
5612                 /* Decide which queue to move pd_lobby skbs to. */
5613                 if (assoc->ulpq.pd_mode) {
5614                         queue = &newsp->pd_lobby;
5615                 } else
5616                         queue = &newsk->sk_receive_queue;
5617
5618                 /* Walk through the pd_lobby, looking for skbs that
5619                  * need moved to the new socket.
5620                  */
5621                 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
5622                         event = sctp_skb2event(skb);
5623                         if (event->asoc == assoc) {
5624                                 sock_rfree(skb);
5625                                 __skb_unlink(skb, &oldsp->pd_lobby);
5626                                 __skb_queue_tail(queue, skb);
5627                                 skb_set_owner_r(skb, newsk);
5628                         }
5629                 }
5630
5631                 /* Clear up any skbs waiting for the partial
5632                  * delivery to finish.
5633                  */
5634                 if (assoc->ulpq.pd_mode)
5635                         sctp_clear_pd(oldsk);
5636
5637         }
5638
5639         /* Set the type of socket to indicate that it is peeled off from the
5640          * original UDP-style socket or created with the accept() call on a
5641          * TCP-style socket..
5642          */
5643         newsp->type = type;
5644
5645         /* Mark the new socket "in-use" by the user so that any packets
5646          * that may arrive on the association after we've moved it are
5647          * queued to the backlog.  This prevents a potential race between
5648          * backlog processing on the old socket and new-packet processing
5649          * on the new socket.
5650          */
5651         sctp_lock_sock(newsk);
5652         sctp_assoc_migrate(assoc, newsk);
5653
5654         /* If the association on the newsk is already closed before accept()
5655          * is called, set RCV_SHUTDOWN flag.
5656          */
5657         if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
5658                 newsk->sk_shutdown |= RCV_SHUTDOWN;
5659
5660         newsk->sk_state = SCTP_SS_ESTABLISHED;
5661         sctp_release_sock(newsk);
5662 }
5663
5664 /* This proto struct describes the ULP interface for SCTP.  */
5665 struct proto sctp_prot = {
5666         .name        =  "SCTP",
5667         .owner       =  THIS_MODULE,
5668         .close       =  sctp_close,
5669         .connect     =  sctp_connect,
5670         .disconnect  =  sctp_disconnect,
5671         .accept      =  sctp_accept,
5672         .ioctl       =  sctp_ioctl,
5673         .init        =  sctp_init_sock,
5674         .destroy     =  sctp_destroy_sock,
5675         .shutdown    =  sctp_shutdown,
5676         .setsockopt  =  sctp_setsockopt,
5677         .getsockopt  =  sctp_getsockopt,
5678         .sendmsg     =  sctp_sendmsg,
5679         .recvmsg     =  sctp_recvmsg,
5680         .bind        =  sctp_bind,
5681         .backlog_rcv =  sctp_backlog_rcv,
5682         .hash        =  sctp_hash,
5683         .unhash      =  sctp_unhash,
5684         .get_port    =  sctp_get_port,
5685         .obj_size    =  sizeof(struct sctp_sock),
5686 };
5687
5688 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5689 struct proto sctpv6_prot = {
5690         .name           = "SCTPv6",
5691         .owner          = THIS_MODULE,
5692         .close          = sctp_close,
5693         .connect        = sctp_connect,
5694         .disconnect     = sctp_disconnect,
5695         .accept         = sctp_accept,
5696         .ioctl          = sctp_ioctl,
5697         .init           = sctp_init_sock,
5698         .destroy        = sctp_destroy_sock,
5699         .shutdown       = sctp_shutdown,
5700         .setsockopt     = sctp_setsockopt,
5701         .getsockopt     = sctp_getsockopt,
5702         .sendmsg        = sctp_sendmsg,
5703         .recvmsg        = sctp_recvmsg,
5704         .bind           = sctp_bind,
5705         .backlog_rcv    = sctp_backlog_rcv,
5706         .hash           = sctp_hash,
5707         .unhash         = sctp_unhash,
5708         .get_port       = sctp_get_port,
5709         .obj_size       = sizeof(struct sctp6_sock),
5710 };
5711 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */