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

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

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

  1 /* SCTP kernel reference Implementation
  2  * (C) Copyright IBM Corp. 2001, 2003
  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/fcntl.h>
 67 #include <linux/poll.h>
 68 #include <linux/init.h>
 69 #include <linux/crypto.h>
 70 
 71 #include <net/ip.h>
 72 #include <net/icmp.h>
 73 #include <net/route.h>
 74 #include <net/ipv6.h>
 75 #include <net/inet_common.h>
 76 
 77 #include <linux/socket.h> /* for sa_family_t */
 78 #include <net/sock.h>
 79 #include <net/sctp/sctp.h>
 80 
 81 /* WARNING:  Please do not remove the SCTP_STATIC attribute to
 82  * any of the functions below as they are used to export functions
 83  * used by a project regression testsuite.
 84  */
 85 
 86 /* Forward declarations for internal helper functions. */
 87 static int sctp_writeable(struct sock *sk);
 88 static inline int sctp_wspace(struct sctp_association *asoc);
 89 static inline void sctp_set_owner_w(struct sctp_chunk *chunk);
 90 static void sctp_wfree(struct sk_buff *skb);
 91 static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
 92                                 int 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 inline int sctp_verify_addr(struct sock *, union sctp_addr *, int);
 98 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
 99 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
100 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
101 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
102 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
103 static int sctp_autobind(struct sock *sk);
104 static void sctp_sock_migrate(struct sock *, struct sock *,
105                               struct sctp_association *, sctp_socket_type_t);
106 static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
107 
108 extern kmem_cache_t *sctp_bucket_cachep;
109 extern int sctp_assoc_valid(struct sock *sk, struct sctp_association *asoc);
110 
111 /* Look up the association by its id.  If this is not a UDP-style
112  * socket, the ID field is always ignored.
113  */
114 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
115 {
116         struct sctp_association *asoc = NULL;
117 
118         /* If this is not a UDP-style socket, assoc id should be ignored. */
119         if (!sctp_style(sk, UDP)) {
120                 /* Return NULL if the socket state is not ESTABLISHED. It
121                  * could be a TCP-style listening socket or a socket which
122                  * hasn't yet called connect() to establish an association.
123                  */
124                 if (!sctp_sstate(sk, ESTABLISHED))
125                         return NULL;
126 
127                 /* Get the first and the only association from the list. */
128                 if (!list_empty(&sctp_sk(sk)->ep->asocs))
129                         asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
130                                           struct sctp_association, asocs);
131                 return asoc;
132         }
133 
134         /* Otherwise this is a UDP-style socket. */
135         asoc = (struct sctp_association *)id;
136         if (!sctp_assoc_valid(sk, asoc))
137                 return NULL;
138 
139         return asoc;
140 }
141 
142 /* Look up the transport from an address and an assoc id. If both address and
143  * id are specified, the associations matching the address and the id should be
144  * the same.
145  */
146 struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
147                                               struct sockaddr_storage *addr,
148                                               sctp_assoc_t id)
149 {
150         struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
151         struct sctp_transport *transport;
152 
153         addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
154                                                (union sctp_addr *)addr,
155                                                &transport);
156         if (!addr_asoc)
157                 return NULL;
158 
159         id_asoc = sctp_id2assoc(sk, id);
160         if (id_asoc && (id_asoc != addr_asoc))
161                 return NULL;
162 
163         sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
164                                                 (union sctp_addr *)addr);
165 
166         return transport;
167 }
168 
169 /* API 3.1.2 bind() - UDP Style Syntax
170  * The syntax of bind() is,
171  *
172  *   ret = bind(int sd, struct sockaddr *addr, int addrlen);
173  *
174  *   sd      - the socket descriptor returned by socket().
175  *   addr    - the address structure (struct sockaddr_in or struct
176  *             sockaddr_in6 [RFC 2553]),
177  *   addr_len - the size of the address structure.
178  */
179 int sctp_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
180 {
181         int retval = 0;
182 
183         sctp_lock_sock(sk);
184 
185         SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, uaddr: %p, addr_len: %d)\n",
186                           sk, uaddr, addr_len);
187 
188         /* Disallow binding twice. */
189         if (!sctp_sk(sk)->ep->base.bind_addr.port)
190                 retval = sctp_do_bind(sk, (union sctp_addr *)uaddr,
191                                       addr_len);
192         else
193                 retval = -EINVAL;
194 
195         sctp_release_sock(sk);
196 
197         return retval;
198 }
199 
200 static long sctp_get_port_local(struct sock *, union sctp_addr *);
201 
202 /* Verify this is a valid sockaddr. */
203 static struct sctp_af *sctp_sockaddr_af(struct sctp_opt *opt,
204                                         union sctp_addr *addr, int len)
205 {
206         struct sctp_af *af;
207 
208         /* Check minimum size.  */
209         if (len < sizeof (struct sockaddr))
210                 return NULL;
211 
212         /* Does this PF support this AF? */
213         if (!opt->pf->af_supported(addr->sa.sa_family, opt))
214                 return NULL;
215 
216         /* If we get this far, af is valid. */
217         af = sctp_get_af_specific(addr->sa.sa_family);
218 
219         if (len < af->sockaddr_len)
220                 return NULL;
221 
222         return af;
223 }
224 
225 /* Bind a local address either to an endpoint or to an association.  */
226 SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
227 {
228         struct sctp_opt *sp = sctp_sk(sk);
229         struct sctp_endpoint *ep = sp->ep;
230         struct sctp_bind_addr *bp = &ep->base.bind_addr;
231         struct sctp_af *af;
232         unsigned short snum;
233         int ret = 0;
234 
235         SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d)\n",
236                           sk, addr, len);
237 
238         /* Common sockaddr verification. */
239         af = sctp_sockaddr_af(sp, addr, len);
240         if (!af)
241                 return -EINVAL;
242 
243         /* PF specific bind() address verification. */
244         if (!sp->pf->bind_verify(sp, addr))
245                 return -EADDRNOTAVAIL;
246 
247         snum= ntohs(addr->v4.sin_port);
248 
249         SCTP_DEBUG_PRINTK("sctp_do_bind: port: %d, new port: %d\n",
250                           bp->port, snum);
251 
252         /* We must either be unbound, or bind to the same port.  */
253         if (bp->port && (snum != bp->port)) {
254                 SCTP_DEBUG_PRINTK("sctp_do_bind:"
255                                   " New port %d does not match existing port "
256                                   "%d.\n", snum, bp->port);
257                 return -EINVAL;
258         }
259 
260         if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
261                 return -EACCES;
262 
263         /* Make sure we are allowed to bind here.
264          * The function sctp_get_port_local() does duplicate address
265          * detection.
266          */
267         if ((ret = sctp_get_port_local(sk, addr))) {
268                 if (ret == (long) sk) {
269                         /* This endpoint has a conflicting address. */
270                         return -EINVAL;
271                 } else {
272                         return -EADDRINUSE;
273                 }
274         }
275 
276         /* Refresh ephemeral port.  */
277         if (!snum)
278                 snum = inet_sk(sk)->num;
279 
280         /* Add the address to the bind address list.  */
281         sctp_local_bh_disable();
282         sctp_write_lock(&ep->base.addr_lock);
283 
284         /* Use GFP_ATOMIC since BHs are disabled.  */
285         addr->v4.sin_port = ntohs(addr->v4.sin_port);
286         ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
287         addr->v4.sin_port = htons(addr->v4.sin_port);
288         if (!ret && !bp->port)
289                 bp->port = snum;
290         sctp_write_unlock(&ep->base.addr_lock);
291         sctp_local_bh_enable();
292 
293         /* Copy back into socket for getsockname() use. */
294         if (!ret) {
295                 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
296                 af->to_sk_saddr(addr, sk);
297         }
298 
299         return ret;
300 }
301 
302 /* Add a list of addresses as bind addresses to local endpoint or
303  * association.
304  *
305  * Basically run through each address specified in the addrs/addrcnt
306  * array/length pair, determine if it is IPv6 or IPv4 and call
307  * sctp_do_bind() on it.
308  *
309  * If any of them fails, then the operation will be reversed and the
310  * ones that were added will be removed.
311  *
312  * Only sctp_setsockopt_bindx() is supposed to call this function.
313  */
314 int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
315 {
316         int cnt;
317         int retval = 0;
318         void *addr_buf;
319         struct sockaddr *sa_addr;
320         struct sctp_af *af;
321 
322         SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
323                           sk, addrs, addrcnt);
324 
325         addr_buf = addrs;
326         for (cnt = 0; cnt < addrcnt; cnt++) {
327                 /* The list may contain either IPv4 or IPv6 address;
328                  * determine the address length for walking thru the list.
329                  */
330                 sa_addr = (struct sockaddr *)addr_buf;
331                 af = sctp_get_af_specific(sa_addr->sa_family);
332                 if (!af) {
333                         retval = -EINVAL;
334                         goto err_bindx_add;
335                 }
336 
337                 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, 
338                                       af->sockaddr_len);
339 
340                 addr_buf += af->sockaddr_len;
341 
342 err_bindx_add:
343                 if (retval < 0) {
344                         /* Failed. Cleanup the ones that have been added */
345                         if (cnt > 0)
346                                 sctp_bindx_rem(sk, addrs, cnt);
347                         return retval;
348                 }
349         }
350 
351         return retval;
352 }
353 
354 /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
355  * associations that are part of the endpoint indicating that a list of local
356  * addresses are added to the endpoint.
357  *
358  * If any of the addresses is already in the bind address list of the 
359  * association, we do not send the chunk for that association.  But it will not
360  * affect other associations.
361  *
362  * Only sctp_setsockopt_bindx() is supposed to call this function.
363  */
364 static int sctp_send_asconf_add_ip(struct sock          *sk, 
365                                    struct sockaddr      *addrs,
366                                    int                  addrcnt)
367 {
368         struct sctp_opt                 *sp;
369         struct sctp_endpoint            *ep;
370         struct sctp_association         *asoc;
371         struct sctp_bind_addr           *bp;
372         struct sctp_chunk               *chunk;
373         struct sctp_sockaddr_entry      *laddr;
374         union sctp_addr                 *addr;
375         void                            *addr_buf;
376         struct sctp_af                  *af;
377         struct list_head                *pos;
378         struct list_head                *p;
379         int                             i;
380         int                             retval = 0;
381 
382         sp = sctp_sk(sk);
383         ep = sp->ep;
384 
385         SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
386                           __FUNCTION__, sk, addrs, addrcnt);
387 
388         list_for_each(pos, &ep->asocs) {
389                 asoc = list_entry(pos, struct sctp_association, asocs);
390 
391                 if (!sctp_state(asoc, ESTABLISHED))
392                         continue;
393 
394                 if (!asoc->peer.asconf_capable)
395                         continue;
396                 
397                 /* Check if any address in the packed array of addresses is
398                  * in the bind address list of the association. If so, 
399                  * do not send the asconf chunk to its peer, but continue with 
400                  * other associations.
401                  */
402                 addr_buf = addrs;
403                 for (i = 0; i < addrcnt; i++) {
404                         addr = (union sctp_addr *)addr_buf;
405                         af = sctp_get_af_specific(addr->v4.sin_family);
406                         if (!af) {
407                                 retval = -EINVAL;
408                                 goto out;
409                         }
410 
411                         if (sctp_assoc_lookup_laddr(asoc, addr))                
412                                 break;
413                         
414                         addr_buf += af->sockaddr_len;
415                 }
416                 if (i < addrcnt)
417                         continue;
418 
419                 /* Use the first address in bind addr list of association as
420                  * Address Parameter of ASCONF CHUNK.
421                  */
422                 sctp_read_lock(&asoc->base.addr_lock);
423                 bp = &asoc->base.bind_addr;
424                 p = bp->address_list.next;
425                 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
426                 sctp_read_unlock(&asoc->base.addr_lock);
427 
428                 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
429                                                    addrcnt, SCTP_PARAM_ADD_IP);
430                 if (!chunk) {
431                         retval = -ENOMEM;
432                         goto out;
433                 }
434 
435                 retval = sctp_primitive_ASCONF(asoc, chunk);
436                 if (retval) {
437                         sctp_chunk_free(chunk);
438                         goto out;
439                 }
440 
441                 /* FIXME: After sending the add address ASCONF chunk, we 
442                  * cannot append the address to the association's binding 
443                  * address list, because the new address may be used as the
444                  * source of a message sent to the peer before the ASCONF
445                  * chunk is received by the peer.  So we should wait until
446                  * ASCONF_ACK is received.
447                  */
448         }
449 
450 out:
451         return retval;
452 }
453 
454 /* Remove a list of addresses from bind addresses list.  Do not remove the
455  * last address.
456  *
457  * Basically run through each address specified in the addrs/addrcnt
458  * array/length pair, determine if it is IPv6 or IPv4 and call
459  * sctp_del_bind() on it.
460  *
461  * If any of them fails, then the operation will be reversed and the
462  * ones that were removed will be added back.
463  *
464  * At least one address has to be left; if only one address is
465  * available, the operation will return -EBUSY.
466  *
467  * Only sctp_setsockopt_bindx() is supposed to call this function.
468  */
469 int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
470 {
471         struct sctp_opt *sp = sctp_sk(sk);
472         struct sctp_endpoint *ep = sp->ep;
473         int cnt;
474         struct sctp_bind_addr *bp = &ep->base.bind_addr;
475         int retval = 0;
476         union sctp_addr saveaddr;
477         void *addr_buf;
478         struct sockaddr *sa_addr;
479         struct sctp_af *af;
480 
481         SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
482                           sk, addrs, addrcnt);
483 
484         addr_buf = addrs;
485         for (cnt = 0; cnt < addrcnt; cnt++) {
486                 /* If the bind address list is empty or if there is only one
487                  * bind address, there is nothing more to be removed (we need
488                  * at least one address here).
489                  */
490                 if (list_empty(&bp->address_list) ||
491                     (sctp_list_single_entry(&bp->address_list))) {
492                         retval = -EBUSY;
493                         goto err_bindx_rem;
494                 }
495 
496                 /* The list may contain either IPv4 or IPv6 address;
497                  * determine the address length to copy the address to
498                  * saveaddr. 
499                  */
500                 sa_addr = (struct sockaddr *)addr_buf;
501                 af = sctp_get_af_specific(sa_addr->sa_family);
502                 if (!af) {
503                         retval = -EINVAL;
504                         goto err_bindx_rem;
505                 }
506                 memcpy(&saveaddr, sa_addr, af->sockaddr_len); 
507                 saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
508                 if (saveaddr.v4.sin_port != bp->port) {
509                         retval = -EINVAL;
510                         goto err_bindx_rem;
511                 }
512 
513                 /* FIXME - There is probably a need to check if sk->sk_saddr and
514                  * sk->sk_rcv_addr are currently set to one of the addresses to
515                  * be removed. This is something which needs to be looked into
516                  * when we are fixing the outstanding issues with multi-homing
517                  * socket routing and failover schemes. Refer to comments in
518                  * sctp_do_bind(). -daisy
519                  */
520                 sctp_local_bh_disable();
521                 sctp_write_lock(&ep->base.addr_lock);
522 
523                 retval = sctp_del_bind_addr(bp, &saveaddr);
524 
525                 sctp_write_unlock(&ep->base.addr_lock);
526                 sctp_local_bh_enable();
527 
528                 addr_buf += af->sockaddr_len;
529 err_bindx_rem:
530                 if (retval < 0) {
531                         /* Failed. Add the ones that has been removed back */
532                         if (cnt > 0)
533                                 sctp_bindx_add(sk, addrs, cnt);
534                         return retval;
535                 }
536         }
537 
538         return retval;
539 }
540 
541 /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
542  * the associations that are part of the endpoint indicating that a list of
543  * local addresses are removed from the endpoint.
544  *
545  * If any of the addresses is already in the bind address list of the 
546  * association, we do not send the chunk for that association.  But it will not
547  * affect other associations.
548  *
549  * Only sctp_setsockopt_bindx() is supposed to call this function.
550  */
551 static int sctp_send_asconf_del_ip(struct sock          *sk,
552                                    struct sockaddr      *addrs,
553                                    int                  addrcnt)
554 {
555         struct sctp_opt         *sp;
556         struct sctp_endpoint    *ep;
557         struct sctp_association *asoc;
558         struct sctp_bind_addr   *bp;
559         struct sctp_chunk       *chunk;
560         union sctp_addr         *laddr;
561         void                    *addr_buf;
562         struct sctp_af          *af;
563         struct list_head        *pos;
564         int                     i;
565         int                     retval = 0;
566 
567         sp = sctp_sk(sk);
568         ep = sp->ep;
569 
570         SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
571                           __FUNCTION__, sk, addrs, addrcnt);
572 
573         list_for_each(pos, &ep->asocs) {
574                 asoc = list_entry(pos, struct sctp_association, asocs);
575 
576                 if (!sctp_state(asoc, ESTABLISHED))
577                         continue;
578 
579                 if (!asoc->peer.asconf_capable)
580                         continue;
581 
582                 /* Check if any address in the packed array of addresses is
583                  * not present in the bind address list of the association.
584                  * If so, do not send the asconf chunk to its peer, but
585                  * continue with other associations.
586                  */
587                 addr_buf = addrs;
588                 for (i = 0; i < addrcnt; i++) {
589                         laddr = (union sctp_addr *)addr_buf;
590                         af = sctp_get_af_specific(laddr->v4.sin_family);
591                         if (!af) {
592                                 retval = -EINVAL;
593                                 goto out;
594                         }
595 
596                         if (!sctp_assoc_lookup_laddr(asoc, laddr))              
597                                 break;
598                         
599                         addr_buf += af->sockaddr_len;
600                 }
601                 if (i < addrcnt)
602                         continue;
603 
604                 /* Find one address in the association's bind address list
605                  * that is not in the packed array of addresses. This is to
606                  * make sure that we do not delete all the addresses in the
607                  * association.
608                  */
609                 sctp_read_lock(&asoc->base.addr_lock);
610                 bp = &asoc->base.bind_addr;
611                 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
612                                                addrcnt, sp);
613                 sctp_read_unlock(&asoc->base.addr_lock);                
614                 if (!laddr)
615                         continue;
616 
617                 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, 
618                                                    SCTP_PARAM_DEL_IP);
619                 if (!chunk) {
620                         retval = -ENOMEM;
621                         goto out;
622                 }
623 
624                 retval = sctp_primitive_ASCONF(asoc, chunk);
625                 if (retval) {
626                         sctp_chunk_free(chunk);
627                         goto out;
628                 }
629 
630                 /* FIXME: After sending the delete address ASCONF chunk, we
631                  * cannot remove the addresses from the association's bind
632                  * address list, because there maybe some packet send to
633                  * the delete addresses, so we should wait until ASCONF_ACK 
634                  * packet is received.
635                  */
636         }
637 out:
638         return retval;
639 }
640 
641 /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
642  *
643  * API 8.1
644  * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
645  *                int flags);
646  *
647  * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
648  * If the sd is an IPv6 socket, the addresses passed can either be IPv4
649  * or IPv6 addresses.
650  *
651  * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
652  * Section 3.1.2 for this usage.
653  *
654  * addrs is a pointer to an array of one or more socket addresses. Each
655  * address is contained in its appropriate structure (i.e. struct
656  * sockaddr_in or struct sockaddr_in6) the family of the address type
657  * must be used to distengish the address length (note that this
658  * representation is termed a "packed array" of addresses). The caller
659  * specifies the number of addresses in the array with addrcnt.
660  *
661  * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
662  * -1, and sets errno to the appropriate error code.
663  *
664  * For SCTP, the port given in each socket address must be the same, or
665  * sctp_bindx() will fail, setting errno to EINVAL.
666  *
667  * The flags parameter is formed from the bitwise OR of zero or more of
668  * the following currently defined flags:
669  *
670  * SCTP_BINDX_ADD_ADDR
671  *
672  * SCTP_BINDX_REM_ADDR
673  *
674  * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
675  * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
676  * addresses from the association. The two flags are mutually exclusive;
677  * if both are given, sctp_bindx() will fail with EINVAL. A caller may
678  * not remove all addresses from an association; sctp_bindx() will
679  * reject such an attempt with EINVAL.
680  *
681  * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
682  * additional addresses with an endpoint after calling bind().  Or use
683  * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
684  * socket is associated with so that no new association accepted will be
685  * associated with those addresses. If the endpoint supports dynamic
686  * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
687  * endpoint to send the appropriate message to the peer to change the
688  * peers address lists.
689  *
690  * Adding and removing addresses from a connected association is
691  * optional functionality. Implementations that do not support this
692  * functionality should return EOPNOTSUPP.
693  *
694  * Basically do nothing but copying the addresses from user to kernel
695  * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
696  * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() * from userspace.
697  *
698  * We don't use copy_from_user() for optimization: we first do the
699  * sanity checks (buffer size -fast- and access check-healthy
700  * pointer); if all of those succeed, then we can alloc the memory
701  * (expensive operation) needed to copy the data to kernel. Then we do
702  * the copying without checking the user space area
703  * (__copy_from_user()).
704  *
705  * On exit there is no need to do sockfd_put(), sys_setsockopt() does
706  * it.
707  *
708  * sk        The sk of the socket
709  * addrs     The pointer to the addresses in user land
710  * addrssize Size of the addrs buffer
711  * op        Operation to perform (add or remove, see the flags of
712  *           sctp_bindx)
713  *
714  * Returns 0 if ok, <0 errno code on error.
715  */
716 SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk, struct sockaddr *addrs,
717                                       int addrs_size, int op)
718 {
719         struct sockaddr *kaddrs;
720         int err;
721         int addrcnt = 0;
722         int walk_size = 0;
723         struct sockaddr *sa_addr;
724         void *addr_buf;
725         struct sctp_af *af;
726 
727         SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
728                           " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
729 
730         if (unlikely(addrs_size <= 0))
731                 return -EINVAL;
732 
733         /* Check the user passed a healthy pointer.  */
734         if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
735                 return -EFAULT;
736 
737         /* Alloc space for the address array in kernel memory.  */
738         kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
739         if (unlikely(!kaddrs))
740                 return -ENOMEM;
741 
742         if (__copy_from_user(kaddrs, addrs, addrs_size)) {
743                 kfree(kaddrs);
744                 return -EFAULT;
745         }
746 
747         /* Walk through the addrs buffer and count the number of addresses. */ 
748         addr_buf = kaddrs;
749         while (walk_size < addrs_size) {
750                 sa_addr = (struct sockaddr *)addr_buf;
751                 af = sctp_get_af_specific(sa_addr->sa_family);
752 
753                 /* If the address family is not supported or if this address
754                  * causes the address buffer to overflow return EINVAL.
755                  */ 
756                 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
757                         kfree(kaddrs);
758                         return -EINVAL;
759                 }
760                 addrcnt++;
761                 addr_buf += af->sockaddr_len;
762                 walk_size += af->sockaddr_len;
763         }
764 
765         /* Do the work. */
766         switch (op) {
767         case SCTP_BINDX_ADD_ADDR:
768                 err = sctp_bindx_add(sk, kaddrs, addrcnt);
769                 if (err)
770                         goto out;
771                 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
772                 break;
773 
774         case SCTP_BINDX_REM_ADDR:
775                 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
776                 if (err)
777                         goto out;
778                 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
779                 break;
780 
781         default:
782                 err = -EINVAL;
783                 break;
784         };
785 
786 out:
787         kfree(kaddrs);
788 
789         return err;
790 }
791 
792 /* API 3.1.4 close() - UDP Style Syntax
793  * Applications use close() to perform graceful shutdown (as described in
794  * Section 10.1 of [SCTP]) on ALL the associations currently represented
795  * by a UDP-style socket.
796  *
797  * The syntax is
798  *
799  *   ret = close(int sd);
800  *
801  *   sd      - the socket descriptor of the associations to be closed.
802  *
803  * To gracefully shutdown a specific association represented by the
804  * UDP-style socket, an application should use the sendmsg() call,
805  * passing no user data, but including the appropriate flag in the
806  * ancillary data (see Section xxxx).
807  *
808  * If sd in the close() call is a branched-off socket representing only
809  * one association, the shutdown is performed on that association only.
810  *
811  * 4.1.6 close() - TCP Style Syntax
812  *
813  * Applications use close() to gracefully close down an association.
814  *
815  * The syntax is:
816  *
817  *    int close(int sd);
818  *
819  *      sd      - the socket descriptor of the association to be closed.
820  *
821  * After an application calls close() on a socket descriptor, no further
822  * socket operations will succeed on that descriptor.
823  *
824  * API 7.1.4 SO_LINGER
825  *
826  * An application using the TCP-style socket can use this option to
827  * perform the SCTP ABORT primitive.  The linger option structure is:
828  *
829  *  struct  linger {
830  *     int     l_onoff;                // option on/off
831  *     int     l_linger;               // linger time
832  * };
833  *
834  * To enable the option, set l_onoff to 1.  If the l_linger value is set
835  * to 0, calling close() is the same as the ABORT primitive.  If the
836  * value is set to a negative value, the setsockopt() call will return
837  * an error.  If the value is set to a positive value linger_time, the
838  * close() can be blocked for at most linger_time ms.  If the graceful
839  * shutdown phase does not finish during this period, close() will
840  * return but the graceful shutdown phase continues in the system.
841  */
842 SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
843 {
844         struct sctp_endpoint *ep;
845         struct sctp_association *asoc;
846         struct list_head *pos, *temp;
847 
848         SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
849 
850         sctp_lock_sock(sk);
851         sk->sk_shutdown = SHUTDOWN_MASK;
852 
853         ep = sctp_sk(sk)->ep;
854 
855         /* Walk all associations on a socket, not on an endpoint.  */
856         list_for_each_safe(pos, temp, &ep->asocs) {
857                 asoc = list_entry(pos, struct sctp_association, asocs);
858 
859                 if (sctp_style(sk, TCP)) {
860                         /* A closed association can still be in the list if
861                          * it belongs to a TCP-style listening socket that is
862                          * not yet accepted. If so, free it. If not, send an
863                          * ABORT or SHUTDOWN based on the linger options.
864                          */
865                         if (sctp_state(asoc, CLOSED)) {
866                                 sctp_unhash_established(asoc);
867                                 sctp_association_free(asoc);
868 
869                         } else if (sock_flag(sk, SOCK_LINGER) &&
870                                    !sk->sk_lingertime)
871                                 sctp_primitive_ABORT(asoc, NULL);
872                         else
873                                 sctp_primitive_SHUTDOWN(asoc, NULL);
874                 } else
875                         sctp_primitive_SHUTDOWN(asoc, NULL);
876         }
877 
878         /* Clean up any skbs sitting on the receive queue.  */
879         sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
880         sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
881 
882         /* On a TCP-style socket, block for at most linger_time if set. */
883         if (sctp_style(sk, TCP) && timeout)
884                 sctp_wait_for_close(sk, timeout);
885 
886         /* This will run the backlog queue.  */
887         sctp_release_sock(sk);
888 
889         /* Supposedly, no process has access to the socket, but
890          * the net layers still may.
891          */
892         sctp_local_bh_disable();
893         sctp_bh_lock_sock(sk);
894 
895         /* Hold the sock, since inet_sock_release() will put sock_put()
896          * and we have just a little more cleanup.
897          */
898         sock_hold(sk);
899         inet_sock_release(sk);
900 
901         sctp_bh_unlock_sock(sk);
902         sctp_local_bh_enable();
903 
904         sock_put(sk);
905 
906         SCTP_DBG_OBJCNT_DEC(sock);
907 }
908 
909 /* Handle EPIPE error. */
910 static int sctp_error(struct sock *sk, int flags, int err)
911 {
912         if (err == -EPIPE)
913                 err = sock_error(sk) ? : -EPIPE;
914         if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
915                 send_sig(SIGPIPE, current, 0);
916         return err;
917 }
918 
919 /* API 3.1.3 sendmsg() - UDP Style Syntax
920  *
921  * An application uses sendmsg() and recvmsg() calls to transmit data to
922  * and receive data from its peer.
923  *
924  *  ssize_t sendmsg(int socket, const struct msghdr *message,
925  *                  int flags);
926  *
927  *  socket  - the socket descriptor of the endpoint.
928  *  message - pointer to the msghdr structure which contains a single
929  *            user message and possibly some ancillary data.
930  *
931  *            See Section 5 for complete description of the data
932  *            structures.
933  *
934  *  flags   - flags sent or received with the user message, see Section
935  *            5 for complete description of the flags.
936  *
937  * Note:  This function could use a rewrite especially when explicit
938  * connect support comes in.
939  */
940 /* BUG:  We do not implement the equivalent of wait_for_tcp_memory(). */
941 
942 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
943 
944 SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
945                              struct msghdr *msg, int msg_len)
946 {
947         struct sctp_opt *sp;
948         struct sctp_endpoint *ep;
949         struct sctp_association *new_asoc=NULL, *asoc=NULL;
950         struct sctp_transport *transport, *chunk_tp;
951         struct sctp_chunk *chunk;
952         union sctp_addr to;
953         struct sockaddr *msg_name = NULL;
954         struct sctp_sndrcvinfo default_sinfo = { 0 };
955         struct sctp_sndrcvinfo *sinfo;
956         struct sctp_initmsg *sinit;
957         sctp_assoc_t associd = NULL;
958         sctp_cmsgs_t cmsgs = { 0 };
959         int err;
960         sctp_scope_t scope;
961         long timeo;
962         __u16 sinfo_flags = 0;
963         struct sctp_datamsg *datamsg;
964         struct list_head *pos;
965         int msg_flags = msg->msg_flags;
966 
967         SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %d)\n",
968                           sk, msg, msg_len);
969 
970         err = 0;
971         sp = sctp_sk(sk);
972         ep = sp->ep;
973 
974         SCTP_DEBUG_PRINTK("Using endpoint: %s.\n", ep->debug_name);
975 
976         /* We cannot send a message over a TCP-style listening socket. */
977         if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
978                 err = -EPIPE;
979                 goto out_nounlock;
980         }
981 
982         /* Parse out the SCTP CMSGs.  */
983         err = sctp_msghdr_parse(msg, &cmsgs);
984 
985         if (err) {
986                 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
987                 goto out_nounlock;
988         }
989 
990         /* Fetch the destination address for this packet.  This
991          * address only selects the association--it is not necessarily
992          * the address we will send to.
993          * For a peeled-off socket, msg_name is ignored.
994          */
995         if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
996                 int msg_namelen = msg->msg_namelen;
997 
998                 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
999                                        msg_namelen);
1000                 if (err)
1001                         return err;
1002 
1003                 if (msg_namelen > sizeof(to))
1004                         msg_namelen = sizeof(to);
1005                 memcpy(&to, msg->msg_name, msg_namelen);
1006                 SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
1007                                   "0x%x:%u.\n",
1008                                   to.v4.sin_addr.s_addr, to.v4.sin_port);
1009 
1010                 to.v4.sin_port = ntohs(to.v4.sin_port);
1011                 msg_name = msg->msg_name;
1012         }
1013 
1014         sinfo = cmsgs.info;
1015         sinit = cmsgs.init;
1016 
1017         /* Did the user specify SNDRCVINFO?  */
1018         if (sinfo) {
1019                 sinfo_flags = sinfo->sinfo_flags;
1020                 associd = sinfo->sinfo_assoc_id;
1021         }
1022 
1023         SCTP_DEBUG_PRINTK("msg_len: %d, sinfo_flags: 0x%x\n",
1024                           msg_len, sinfo_flags);
1025 
1026         /* MSG_EOF or MSG_ABORT cannot be set on a TCP-style socket. */
1027         if (sctp_style(sk, TCP) && (sinfo_flags & (MSG_EOF | MSG_ABORT))) {
1028                 err = -EINVAL;
1029                 goto out_nounlock;
1030         }
1031 
1032         /* If MSG_EOF is set, no data can be sent. Disallow sending zero
1033          * length messages when MSG_EOF|MSG_ABORT is not set.
1034          * If MSG_ABORT is set, the message length could be non zero with
1035          * the msg_iov set to the user abort reason.
1036          */
1037         if (((sinfo_flags & MSG_EOF) && (msg_len > 0)) ||
1038             (!(sinfo_flags & (MSG_EOF|MSG_ABORT)) && (msg_len == 0))) {
1039                 err = -EINVAL;
1040                 goto out_nounlock;
1041         }
1042 
1043         /* If MSG_ADDR_OVER is set, there must be an address
1044          * specified in msg_name.
1045          */
1046         if ((sinfo_flags & MSG_ADDR_OVER) && (!msg->msg_name)) {
1047                 err = -EINVAL;
1048                 goto out_nounlock;
1049         }
1050 
1051         transport = NULL;
1052 
1053         SCTP_DEBUG_PRINTK("About to look up association.\n");
1054 
1055         sctp_lock_sock(sk);
1056 
1057         /* If a msg_name has been specified, assume this is to be used.  */
1058         if (msg_name) {
1059                 /* Look for a matching association on the endpoint. */
1060                 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1061                 if (!asoc) {
1062                         /* If we could not find a matching association on the
1063                          * endpoint, make sure that it is not a TCP-style
1064                          * socket that already has an association or there is
1065                          * no peeled-off association on another socket.
1066                          */
1067                         if ((sctp_style(sk, TCP) &&
1068                              sctp_sstate(sk, ESTABLISHED)) ||
1069                             sctp_endpoint_is_peeled_off(ep, &to)) {
1070                                 err = -EADDRNOTAVAIL;
1071                                 goto out_unlock;
1072                         }
1073                 }
1074         } else {
1075                 asoc = sctp_id2assoc(sk, associd);
1076                 if (!asoc) {
1077                         err = -EPIPE;
1078                         goto out_unlock;
1079                 }
1080         }
1081 
1082         if (asoc) {
1083                 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1084 
1085                 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1086                  * socket that has an association in CLOSED state. This can
1087                  * happen when an accepted socket has an association that is
1088                  * already CLOSED.
1089                  */
1090                 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1091                         err = -EPIPE;
1092                         goto out_unlock;
1093                 }
1094 
1095                 if (sinfo_flags & MSG_EOF) {
1096                         SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1097                                           asoc);
1098                         sctp_primitive_SHUTDOWN(asoc, NULL);
1099                         err = 0;
1100                         goto out_unlock;
1101                 }
1102                 if (sinfo_flags & MSG_ABORT) {
1103                         SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
1104                         sctp_primitive_ABORT(asoc, msg);
1105                         err = 0;
1106                         goto out_unlock;
1107                 }
1108         }
1109 
1110         /* Do we need to create the association?  */
1111         if (!asoc) {
1112                 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1113 
1114                 /* Check for invalid stream against the stream counts,
1115                  * either the default or the user specified stream counts.
1116                  */
1117                 if (sinfo) {
1118                         if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1119                                 /* Check against the defaults. */
1120                                 if (sinfo->sinfo_stream >=
1121                                     sp->initmsg.sinit_num_ostreams) {
1122                                         err = -EINVAL;
1123                                         goto out_unlock;
1124                                 }
1125                         } else {
1126                                 /* Check against the requested.  */
1127                                 if (sinfo->sinfo_stream >=
1128                                     sinit->sinit_num_ostreams) {
1129                                         err = -EINVAL;
1130                                         goto out_unlock;
1131                                 }
1132                         }
1133                 }
1134 
1135                 /*
1136                  * API 3.1.2 bind() - UDP Style Syntax
1137                  * If a bind() or sctp_bindx() is not called prior to a
1138                  * sendmsg() call that initiates a new association, the
1139                  * system picks an ephemeral port and will choose an address
1140                  * set equivalent to binding with a wildcard address.
1141                  */
1142                 if (!ep->base.bind_addr.port) {
1143                         if (sctp_autobind(sk)) {
1144                                 err = -EAGAIN;
1145                                 goto out_unlock;
1146                         }
1147                 }
1148 
1149                 scope = sctp_scope(&to);
1150                 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1151                 if (!new_asoc) {
1152                         err = -ENOMEM;
1153                         goto out_unlock;
1154                 }
1155                 asoc = new_asoc;
1156 
1157                 /* If the SCTP_INIT ancillary data is specified, set all
1158                  * the association init values accordingly.
1159                  */
1160                 if (sinit) {
1161                         if (sinit->sinit_num_ostreams) {
1162                                 asoc->c.sinit_num_ostreams =
1163                                         sinit->sinit_num_ostreams;
1164                         }
1165                         if (sinit->sinit_max_instreams) {
1166                                 asoc->c.sinit_max_instreams =
1167                                         sinit->sinit_max_instreams;
1168                         }
1169                         if (sinit->sinit_max_attempts) {
1170                                 asoc->max_init_attempts
1171                                         = sinit->sinit_max_attempts;
1172                         }
1173                         if (sinit->sinit_max_init_timeo) {
1174                                 asoc->max_init_timeo = 
1175                                  MSECS_TO_JIFFIES(sinit->sinit_max_init_timeo);
1176                         }
1177                 }
1178 
1179                 /* Prime the peer's transport structures.  */
1180                 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
1181                 if (!transport) {
1182                         err = -ENOMEM;
1183                         goto out_free;
1184                 }
1185                 err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
1186                 if (err < 0) {
1187                         err = -ENOMEM;
1188                         goto out_free;
1189                 }
1190         }
1191 
1192         /* ASSERT: we have a valid association at this point.  */
1193         SCTP_DEBUG_PRINTK("We have a valid association.\n");
1194 
1195         if (!sinfo) {
1196                 /* If the user didn't specify SNDRCVINFO, make up one with
1197                  * some defaults.
1198                  */
1199                 default_sinfo.sinfo_stream = asoc->default_stream;
1200                 default_sinfo.sinfo_flags = asoc->default_flags;
1201                 default_sinfo.sinfo_ppid = asoc->default_ppid;
1202                 default_sinfo.sinfo_context = asoc->default_context;
1203                 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1204                 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1205                 sinfo = &default_sinfo;
1206         }
1207 
1208         /* API 7.1.7, the sndbuf size per association bounds the
1209          * maximum size of data that can be sent in a single send call.
1210          */
1211         if (msg_len > sk->sk_sndbuf) {
1212                 err = -EMSGSIZE;
1213                 goto out_free;
1214         }
1215 
1216         /* If fragmentation is disabled and the message length exceeds the
1217          * association fragmentation point, return EMSGSIZE.  The I-D
1218          * does not specify what this error is, but this looks like
1219          * a great fit.
1220          */
1221         if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1222                 err = -EMSGSIZE;
1223                 goto out_free;
1224         }
1225 
1226         if (sinfo) {
1227                 /* Check for invalid stream. */
1228                 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1229                         err = -EINVAL;
1230                         goto out_free;
1231                 }
1232         }
1233 
1234         timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1235         if (!sctp_wspace(asoc)) {
1236                 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1237                 if (err)
1238                         goto out_free;
1239         }
1240 
1241         /* If an address is passed with the sendto/sendmsg call, it is used
1242          * to override the primary destination address in the TCP model, or
1243          * when MSG_ADDR_OVER flag is set in the UDP model.
1244          */
1245         if ((sctp_style(sk, TCP) && msg_name) ||
1246             (sinfo_flags & MSG_ADDR_OVER)) {
1247                 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
1248                 if (!chunk_tp) {
1249                         err = -EINVAL;
1250                         goto out_free;
1251                 }
1252         } else
1253                 chunk_tp = NULL;
1254 
1255         /* Auto-connect, if we aren't connected already. */
1256         if (sctp_state(asoc, CLOSED)) {
1257                 err = sctp_primitive_ASSOCIATE(asoc, NULL);
1258                 if (err < 0)
1259                         goto out_free;
1260                 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1261         }
1262 
1263         /* Break the message into multiple chunks of maximum size. */
1264         datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1265         if (!datamsg) {
1266                 err = -ENOMEM;
1267                 goto out_free;
1268         }
1269 
1270         /* Now send the (possibly) fragmented message. */
1271         list_for_each(pos, &datamsg->chunks) {
1272                 chunk = list_entry(pos, struct sctp_chunk, frag_list);
1273                 sctp_datamsg_track(chunk);
1274 
1275                 /* Do accounting for the write space.  */
1276                 sctp_set_owner_w(chunk);
1277 
1278                 chunk->transport = chunk_tp;
1279 
1280                 /* Send it to the lower layers.  Note:  all chunks
1281                  * must either fail or succeed.   The lower layer
1282                  * works that way today.  Keep it that way or this
1283                  * breaks.
1284                  */
1285                 err = sctp_primitive_SEND(asoc, chunk);
1286                 /* Did the lower layer accept the chunk? */
1287                 if (err)
1288                         sctp_chunk_free(chunk);
1289                 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1290         }
1291 
1292         sctp_datamsg_free(datamsg);
1293         if (err)
1294                 goto out_free;
1295         else
1296                 err = msg_len;
1297 
1298         /* If we are already past ASSOCIATE, the lower
1299          * layers are responsible for association cleanup.
1300          */
1301         goto out_unlock;
1302 
1303 out_free:
1304         if (new_asoc)
1305                 sctp_association_free(asoc);
1306 out_unlock:
1307         sctp_release_sock(sk);
1308 
1309 out_nounlock:
1310         return sctp_error(sk, msg_flags, err);
1311 
1312 #if 0
1313 do_sock_err:
1314         if (msg_len)
1315                 err = msg_len;
1316         else
1317                 err = sock_error(sk);
1318         goto out;
1319 
1320 do_interrupted:
1321         if (msg_len)
1322                 err = msg_len;
1323         goto out;
1324 #endif /* 0 */
1325 }
1326 
1327 /* This is an extended version of skb_pull() that removes the data from the
1328  * start of a skb even when data is spread across the list of skb's in the
1329  * frag_list. len specifies the total amount of data that needs to be removed.
1330  * when 'len' bytes could be removed from the skb, it returns 0.
1331  * If 'len' exceeds the total skb length,  it returns the no. of bytes that
1332  * could not be removed.
1333  */
1334 static int sctp_skb_pull(struct sk_buff *skb, int len)
1335 {
1336         struct sk_buff *list;
1337         int skb_len = skb_headlen(skb);
1338         int rlen;
1339 
1340         if (len <= skb_len) {
1341                 __skb_pull(skb, len);
1342                 return 0;
1343         }
1344         len -= skb_len;
1345         __skb_pull(skb, skb_len);
1346 
1347         for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
1348                 rlen = sctp_skb_pull(list, len);
1349                 skb->len -= (len-rlen);
1350                 skb->data_len -= (len-rlen);
1351 
1352                 if (!rlen)
1353                         return 0;
1354 
1355                 len = rlen;
1356         }
1357 
1358         return len;
1359 }
1360 
1361 /* API 3.1.3  recvmsg() - UDP Style Syntax
1362  *
1363  *  ssize_t recvmsg(int socket, struct msghdr *message,
1364  *                    int flags);
1365  *
1366  *  socket  - the socket descriptor of the endpoint.
1367  *  message - pointer to the msghdr structure which contains a single
1368  *            user message and possibly some ancillary data.
1369  *
1370  *            See Section 5 for complete description of the data
1371  *            structures.
1372  *
1373  *  flags   - flags sent or received with the user message, see Section
1374  *            5 for complete description of the flags.
1375  */
1376 static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
1377 
1378 SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
1379                              struct msghdr *msg, int len, int noblock,
1380                              int flags, int *addr_len)
1381 {
1382         struct sctp_ulpevent *event = NULL;
1383         struct sctp_opt *sp = sctp_sk(sk);
1384         struct sk_buff *skb;
1385         int copied;
1386         int err = 0;
1387         int skb_len;
1388 
1389         SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %d, %s: %d, %s: "
1390                           "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
1391                           "len", len, "knoblauch", noblock,
1392                           "flags", flags, "addr_len", addr_len);
1393 
1394         sctp_lock_sock(sk);
1395 
1396         if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
1397                 err = -ENOTCONN;
1398                 goto out;
1399         }
1400 
1401         skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
1402         if (!skb)
1403                 goto out;
1404 
1405         /* Get the total length of the skb including any skb's in the
1406          * frag_list.
1407          */
1408         skb_len = skb->len;
1409 
1410         copied = skb_len;
1411         if (copied > len)
1412                 copied = len;
1413 
1414         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1415 
1416         event = sctp_skb2event(skb);
1417 
1418         if (err)
1419                 goto out_free;
1420 
1421         sock_recv_timestamp(msg, sk, skb);
1422         if (sctp_ulpevent_is_notification(event)) {
1423                 msg->msg_flags |= MSG_NOTIFICATION;
1424                 sp->pf->event_msgname(event, msg->msg_name, addr_len);
1425         } else {
1426                 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
1427         }
1428 
1429         /* Check if we allow SCTP_SNDRCVINFO. */
1430         if (sp->subscribe.sctp_data_io_event)
1431                 sctp_ulpevent_read_sndrcvinfo(event, msg);
1432 #if 0
1433         /* FIXME: we should be calling IP/IPv6 layers.  */
1434         if (sk->sk_protinfo.af_inet.cmsg_flags)
1435                 ip_cmsg_recv(msg, skb);
1436 #endif
1437 
1438         err = copied;
1439 
1440         /* If skb's length exceeds the user's buffer, update the skb and
1441          * push it back to the receive_queue so that the next call to
1442          * recvmsg() will return the remaining data. Don't set MSG_EOR.
1443          */
1444         if (skb_len > copied) {
1445                 msg->msg_flags &= ~MSG_EOR;
1446                 if (flags & MSG_PEEK)
1447                         goto out_free;
1448                 sctp_skb_pull(skb, copied);
1449                 skb_queue_head(&sk->sk_receive_queue, skb);
1450 
1451                 /* When only partial message is copied to the user, increase
1452                  * rwnd by that amount. If all the data in the skb is read,
1453                  * rwnd is updated when the event is freed.
1454                  */
1455                 sctp_assoc_rwnd_increase(event->sndrcvinfo.sinfo_assoc_id,
1456                                          copied);
1457                 goto out;
1458         } else if ((event->msg_flags & MSG_NOTIFICATION) ||
1459                    (event->msg_flags & MSG_EOR))
1460                 msg->msg_flags |= MSG_EOR;
1461         else
1462                 msg->msg_flags &= ~MSG_EOR;
1463 
1464 out_free:
1465         if (flags & MSG_PEEK) {
1466                 /* Release the skb reference acquired after peeking the skb in
1467                  * sctp_skb_recv_datagram().
1468                  */
1469                 kfree_skb(skb);
1470         } else {
1471                 /* Free the event which includes releasing the reference to
1472                  * the owner of the skb, freeing the skb and updating the
1473                  * rwnd.
1474                  */
1475                 sctp_ulpevent_free(event);
1476         }
1477 out:
1478         sctp_release_sock(sk);
1479         return err;
1480 }
1481 
1482 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
1483  *
1484  * This option is a on/off flag.  If enabled no SCTP message
1485  * fragmentation will be performed.  Instead if a message being sent
1486  * exceeds the current PMTU size, the message will NOT be sent and
1487  * instead a error will be indicated to the user.
1488  */
1489 static int sctp_setsockopt_disable_fragments(struct sock *sk,
1490                                                     char *optval, int optlen)
1491 {
1492         int val;
1493 
1494         if (optlen < sizeof(int))
1495                 return -EINVAL;
1496 
1497         if (get_user(val, (int *)optval))
1498                 return -EFAULT;
1499 
1500         sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
1501 
1502         return 0;
1503 }
1504 
1505 static int sctp_setsockopt_events(struct sock *sk, char *optval,
1506                                         int optlen)
1507 {
1508         if (optlen != sizeof(struct sctp_event_subscribe))
1509                 return -EINVAL;
1510         if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
1511                 return -EFAULT;
1512         return 0;
1513 }
1514 
1515 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
1516  *
1517  * This socket option is applicable to the UDP-style socket only.  When
1518  * set it will cause associations that are idle for more than the
1519  * specified number of seconds to automatically close.  An association
1520  * being idle is defined an association that has NOT sent or received
1521  * user data.  The special value of '' indicates that no automatic
1522  * close of any associations should be performed.  The option expects an
1523  * integer defining the number of seconds of idle time before an
1524  * association is closed.
1525  */
1526 static int sctp_setsockopt_autoclose(struct sock *sk, char *optval,
1527                                             int optlen)
1528 {
1529         struct sctp_opt *sp = sctp_sk(sk);
1530 
1531         /* Applicable to UDP-style socket only */
1532         if (sctp_style(sk, TCP))
1533                 return -EOPNOTSUPP;
1534         if (optlen != sizeof(int))
1535                 return -EINVAL;
1536         if (copy_from_user(&sp->autoclose, optval, optlen))
1537                 return -EFAULT;
1538 
1539         sp->ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
1540         return 0;
1541 }
1542 
1543 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
1544  *
1545  * Applications can enable or disable heartbeats for any peer address of
1546  * an association, modify an address's heartbeat interval, force a
1547  * heartbeat to be sent immediately, and adjust the address's maximum
1548  * number of retransmissions sent before an address is considered
1549  * unreachable.  The following structure is used to access and modify an
1550  * address's parameters:
1551  *
1552  *  struct sctp_paddrparams {
1553  *      sctp_assoc_t            spp_assoc_id;
1554  *      struct sockaddr_storage spp_address;
1555  *      uint32_t                spp_hbinterval;
1556  *      uint16_t                spp_pathmaxrxt;
1557  *  };
1558  *
1559  *   spp_assoc_id    - (UDP style socket) This is filled in the application,
1560  *                     and identifies the association for this query.
1561  *   spp_address     - This specifies which address is of interest.
1562  *   spp_hbinterval  - This contains the value of the heartbeat interval,
1563  *                     in milliseconds.  A value of 0, when modifying the
1564  *                     parameter, specifies that the heartbeat on this
1565  *                     address should be disabled. A value of UINT32_MAX
1566  *                     (4294967295), when modifying the parameter,
1567  *                     specifies that a heartbeat should be sent
1568  *                     immediately to the peer address, and the current
1569  *                     interval should remain unchanged.
1570  *   spp_pathmaxrxt  - This contains the maximum number of
1571  *                     retransmissions before this address shall be
1572  *                     considered unreachable.
1573  */
1574 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
1575                                             char *optval, int optlen)
1576 {
1577         struct sctp_paddrparams params;
1578         struct sctp_transport *trans;
1579         int error;
1580 
1581         if (optlen != sizeof(struct sctp_paddrparams))
1582                 return -EINVAL;
1583         if (copy_from_user(&params, optval, optlen))
1584                 return -EFAULT;
1585 
1586         trans = sctp_addr_id2transport(sk, &params.spp_address,
1587                                        params.spp_assoc_id);
1588         if (!trans)
1589                 return -EINVAL;
1590 
1591         /* Applications can enable or disable heartbeats for any peer address
1592          * of an association, modify an address's heartbeat interval, force a
1593          * heartbeat to be sent immediately, and adjust the address's maximum
1594          * number of retransmissions sent before an address is considered
1595          * unreachable.
1596          *
1597          * The value of the heartbeat interval, in milliseconds. A value of
1598          * UINT32_MAX (4294967295), when modifying the parameter, specifies
1599          * that a heartbeat should be sent immediately to the peer address,
1600          * and the current interval should remain unchanged.
1601          */
1602         if (0xffffffff == params.spp_hbinterval) {
1603                 error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
1604                 if (error)
1605                         return error;
1606         } else {
1607         /* The value of the heartbeat interval, in milliseconds. A value of 0,
1608          * when modifying the parameter, specifies that the heartbeat on this
1609          * address should be disabled.
1610          */
1611                 if (params.spp_hbinterval) {
1612                         trans->hb_allowed = 1;
1613                         trans->hb_interval = 
1614                                 MSECS_TO_JIFFIES(params.spp_hbinterval);
1615                 } else
1616                         trans->hb_allowed = 0;
1617         }
1618 
1619         /* spp_pathmaxrxt contains the maximum number of retransmissions
1620          * before this address shall be considered unreachable.
1621          */
1622         trans->error_threshold = params.spp_pathmaxrxt;
1623 
1624         return 0;
1625 }
1626 
1627 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
1628  *
1629  * Applications can specify protocol parameters for the default association
1630  * initialization.  The option name argument to setsockopt() and getsockopt()
1631  * is SCTP_INITMSG.
1632  *
1633  * Setting initialization parameters is effective only on an unconnected
1634  * socket (for UDP-style sockets only future associations are effected
1635  * by the change).  With TCP-style sockets, this option is inherited by
1636  * sockets derived from a listener socket.
1637  */
1638 static int sctp_setsockopt_initmsg(struct sock *sk, char *optval, int optlen)
1639 {
1640         if (optlen != sizeof(struct sctp_initmsg))
1641                 return -EINVAL;
1642         if (copy_from_user(&sctp_sk(sk)->initmsg, optval, optlen))
1643                 return -EFAULT;
1644         return 0;
1645 }
1646 
1647 /*
1648  * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
1649  *
1650  *   Applications that wish to use the sendto() system call may wish to
1651  *   specify a default set of parameters that would normally be supplied
1652  *   through the inclusion of ancillary data.  This socket option allows
1653  *   such an application to set the default sctp_sndrcvinfo structure.
1654  *   The application that wishes to use this socket option simply passes
1655  *   in to this call the sctp_sndrcvinfo structure defined in Section
1656  *   5.2.2) The input parameters accepted by this call include
1657  *   sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
1658  *   sinfo_timetolive.  The user must provide the sinfo_assoc_id field in
1659  *   to this call if the caller is using the UDP model.
1660  */
1661 static int sctp_setsockopt_default_send_param(struct sock *sk,
1662                                                 char *optval, int optlen)
1663 {
1664         struct sctp_sndrcvinfo info;
1665         struct sctp_association *asoc;
1666         struct sctp_opt *sp = sctp_sk(sk);
1667 
1668         if (optlen != sizeof(struct sctp_sndrcvinfo))
1669                 return -EINVAL;
1670         if (copy_from_user(&info, optval, optlen))
1671                 return -EFAULT;
1672 
1673         asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
1674         if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
1675                 return -EINVAL;
1676 
1677         if (asoc) {
1678                 asoc->default_stream = info.sinfo_stream;
1679                 asoc->default_flags = info.sinfo_flags;
1680                 asoc->default_ppid = info.sinfo_ppid;
1681                 asoc->default_context = info.sinfo_context;
1682                 asoc->default_timetolive = info.sinfo_timetolive;
1683         } else {
1684                 sp->default_stream = info.sinfo_stream;
1685                 sp->default_flags = info.sinfo_flags;
1686                 sp->default_ppid = info.sinfo_ppid;
1687                 sp->default_context = info.sinfo_context;
1688                 sp->default_timetolive = info.sinfo_timetolive;
1689         }
1690 
1691         return 0;
1692 }
1693 
1694 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
1695  *
1696  * Requests that the local SCTP stack use the enclosed peer address as
1697  * the association primary.  The enclosed address must be one of the
1698  * association peer's addresses.
1699  */
1700 static int sctp_setsockopt_primary_addr(struct sock *sk, char *optval,
1701                                         int optlen)
1702 {
1703         struct sctp_prim prim;
1704         struct sctp_transport *trans;
1705 
1706         if (optlen != sizeof(struct sctp_prim))
1707                 return -EINVAL;
1708 
1709         if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
1710                 return -EFAULT;
1711 
1712         trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
1713         if (!trans)
1714                 return -EINVAL;
1715 
1716         sctp_assoc_set_primary(trans->asoc, trans);
1717 
1718         return 0;
1719 }
1720 
1721 /*
1722  * 7.1.5 SCTP_NODELAY
1723  *
1724  * Turn on/off any Nagle-like algorithm.  This means that packets are
1725  * generally sent as soon as possible and no unnecessary delays are
1726  * introduced, at the cost of more packets in the network.  Expects an
1727  *  integer boolean flag.
1728  */
1729 static int sctp_setsockopt_nodelay(struct sock *sk, char *optval,
1730                                         int optlen)
1731 {
1732         int val;
1733 
1734         if (optlen < sizeof(int))
1735                 return -EINVAL;
1736         if (get_user(val, (int *)optval))
1737                 return -EFAULT;
1738 
1739         sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
1740         return 0;
1741 }
1742 
1743 /*
1744  *
1745  * 7.1.1 SCTP_RTOINFO
1746  *
1747  * The protocol parameters used to initialize and bound retransmission
1748  * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
1749  * and modify these parameters.
1750  * All parameters are time values, in milliseconds.  A value of 0, when
1751  * modifying the parameters, indicates that the current value should not
1752  * be changed.
1753  *
1754  */
1755 static int sctp_setsockopt_rtoinfo(struct sock *sk, char *optval, int optlen) {
1756         struct sctp_rtoinfo rtoinfo;
1757         struct sctp_association *asoc;
1758 
1759         if (optlen != sizeof (struct sctp_rtoinfo))
1760                 return -EINVAL;
1761 
1762         if (copy_from_user(&rtoinfo, optval, optlen))
1763                 return -EFAULT;
1764 
1765         asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
1766 
1767         /* Set the values to the specific association */
1768         if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
1769                 return -EINVAL;
1770 
1771         if (asoc) {
1772                 if (rtoinfo.srto_initial != 0)
1773                         asoc->rto_initial = 
1774                                 MSECS_TO_JIFFIES(rtoinfo.srto_initial);
1775                 if (rtoinfo.srto_max != 0)
1776                         asoc->rto_max = MSECS_TO_JIFFIES(rtoinfo.srto_max);
1777                 if (rtoinfo.srto_min != 0)
1778                         asoc->rto_min = MSECS_TO_JIFFIES(rtoinfo.srto_min);
1779         } else {
1780                 /* If there is no association or the association-id = 0
1781                  * set the values to the endpoint.
1782                  */
1783                 struct sctp_opt *sp = sctp_sk(sk);
1784 
1785                 if (rtoinfo.srto_initial != 0)
1786                         sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
1787                 if (rtoinfo.srto_max != 0)
1788                         sp->rtoinfo.srto_max = rtoinfo.srto_max;
1789                 if (rtoinfo.srto_min != 0)
1790                         sp->rtoinfo.srto_min = rtoinfo.srto_min;
1791         }
1792 
1793         return 0;
1794 }
1795 
1796 /*
1797  *
1798  * 7.1.2 SCTP_ASSOCINFO
1799  *
1800  * This option is used to tune the the maximum retransmission attempts
1801  * of the association.
1802  * Returns an error if the new association retransmission value is
1803  * greater than the sum of the retransmission value  of the peer.
1804  * See [SCTP] for more information.
1805  *
1806  */
1807 static int sctp_setsockopt_associnfo(struct sock *sk, char *optval, int optlen)
1808 {
1809 
1810         struct sctp_assocparams assocparams;
1811         struct sctp_association *asoc;
1812 
1813         if (optlen != sizeof(struct sctp_assocparams))
1814                 return -EINVAL;
1815         if (copy_from_user(&assocparams, optval, optlen))
1816                 return -EFAULT;
1817 
1818         asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
1819 
1820         if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
1821                 return -EINVAL;
1822 
1823         /* Set the values to the specific association */
1824         if (asoc) {
1825                 if (assocparams.sasoc_asocmaxrxt != 0)
1826                         asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
1827                 if (assocparams.sasoc_cookie_life != 0) {
1828                         asoc->cookie_life.tv_sec =
1829                                         assocparams.sasoc_cookie_life / 1000;
1830                         asoc->cookie_life.tv_usec =
1831                                         (assocparams.sasoc_cookie_life % 1000)
1832                                         * 1000;
1833                 }
1834         } else {
1835                 /* Set the values to the endpoint */
1836                 struct sctp_opt *sp = sctp_sk(sk);
1837 
1838                 if (assocparams.sasoc_asocmaxrxt != 0)
1839                         sp->assocparams.sasoc_asocmaxrxt =
1840                                                 assocparams.sasoc_asocmaxrxt;
1841                 if (assocparams.sasoc_cookie_life != 0)
1842                         sp->assocparams.sasoc_cookie_life =
1843                                                 assocparams.sasoc_cookie_life;
1844         }
1845         return 0;
1846 }
1847 
1848 /*
1849  * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
1850  *
1851  * This socket option is a boolean flag which turns on or off mapped V4
1852  * addresses.  If this option is turned on and the socket is type
1853  * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
1854  * If this option is turned off, then no mapping will be done of V4
1855  * addresses and a user will receive both PF_INET6 and PF_INET type
1856  * addresses on the socket.
1857  */
1858 static int sctp_setsockopt_mappedv4(struct sock *sk, char *optval, int optlen)
1859 {
1860         int val;
1861         struct sctp_opt *sp = sctp_sk(sk);
1862 
1863         if (optlen < sizeof(int))
1864                 return -EINVAL;
1865         if (get_user(val, (int *)optval))
1866                 return -EFAULT;
1867         if (val)
1868                 sp->v4mapped = 1;
1869         else
1870                 sp->v4mapped = 0;
1871 
1872         return 0;
1873 }
1874 
1875 /*
1876  * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
1877  *
1878  * This socket option specifies the maximum size to put in any outgoing
1879  * SCTP chunk.  If a message is larger than this size it will be
1880  * fragmented by SCTP into the specified size.  Note that the underlying
1881  * SCTP implementation may fragment into smaller sized chunks when the
1882  * PMTU of the underlying association is smaller than the value set by
1883  * the user.
1884  */
1885 static int sctp_setsockopt_maxseg(struct sock *sk, char *optval, int optlen)
1886 {
1887         int val;
1888 
1889         if (optlen < sizeof(int))
1890                 return -EINVAL;
1891         if (get_user(val, (int *)optval))
1892                 return -EFAULT;
1893         if ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))
1894                 return -EINVAL;
1895         sctp_sk(sk)->user_frag = val;
1896 
1897         return 0;
1898 }
1899 
1900 
1901 /*
1902  *  7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
1903  *
1904  *   Requests that the peer mark the enclosed address as the association
1905  *   primary. The enclosed address must be one of the association's
1906  *   locally bound addresses. The following structure is used to make a
1907  *   set primary request:
1908  */
1909 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char *optval,
1910                                              int optlen)
1911 {
1912         struct sctp_opt         *sp;
1913         struct sctp_endpoint    *ep;
1914         struct sctp_association *asoc = NULL;
1915         struct sctp_setpeerprim prim;
1916         struct sctp_chunk       *chunk;
1917         int                     err;
1918 
1919         sp = sctp_sk(sk);
1920         ep = sp->ep;
1921 
1922         if (optlen != sizeof(struct sctp_setpeerprim))
1923                 return -EINVAL;
1924 
1925         if (copy_from_user(&prim, optval, optlen))
1926                 return -EFAULT;
1927 
1928         asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
1929         if (!asoc) 
1930                 return -EINVAL;
1931 
1932         if (!sctp_state(asoc, ESTABLISHED))
1933                 return -ENOTCONN;
1934 
1935         if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
1936                 return -EADDRNOTAVAIL;
1937 
1938         /* Create an ASCONF chunk with SET_PRIMARY parameter    */
1939         chunk = sctp_make_asconf_set_prim(asoc,
1940                                           (union sctp_addr *)&prim.sspp_addr);
1941         if (!chunk)
1942                 return -ENOMEM;
1943 
1944         err = sctp_primitive_ASCONF(asoc, chunk);
1945         if (err) {
1946                 sctp_chunk_free(chunk);
1947                 return err;
1948         }
1949 
1950         SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
1951 
1952         return 0;
1953 }
1954 
1955 
1956 /* API 6.2 setsockopt(), getsockopt()
1957  *
1958  * Applications use setsockopt() and getsockopt() to set or retrieve
1959  * socket options.  Socket options are used to change the default
1960  * behavior of sockets calls.  They are described in Section 7.
1961  *
1962  * The syntax is:
1963  *
1964  *   ret = getsockopt(int sd, int level, int optname, void *optval,
1965  *                    int *optlen);
1966  *   ret = setsockopt(int sd, int level, int optname, const void *optval,
1967  *                    int optlen);
1968  *
1969  *   sd      - the socket descript.
1970  *   level   - set to IPPROTO_SCTP for all SCTP options.
1971  *   optname - the option name.
1972  *   optval  - the buffer to store the value of the option.
1973  *   optlen  - the size of the buffer.
1974  */
1975 SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
1976                                 char *optval, int optlen)
1977 {
1978         int retval = 0;
1979 
1980         SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
1981                           sk, optname);
1982 
1983         /* I can hardly begin to describe how wrong this is.  This is
1984          * so broken as to be worse than useless.  The API draft
1985          * REALLY is NOT helpful here...  I am not convinced that the
1986          * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
1987          * are at all well-founded.
1988          */
1989         if (level != SOL_SCTP) {
1990                 struct sctp_af *af = sctp_sk(sk)->pf->af;
1991                 retval = af->setsockopt(sk, level, optname, optval, optlen);
1992                 goto out_nounlock;
1993         }
1994 
1995         sctp_lock_sock(sk);
1996 
1997         switch (optname) {
1998         case SCTP_SOCKOPT_BINDX_ADD:
1999                 /* 'optlen' is the size of the addresses buffer. */
2000                 retval = sctp_setsockopt_bindx(sk, (struct sockaddr *)optval,
2001                                                optlen, SCTP_BINDX_ADD_ADDR);
2002                 break;
2003 
2004         case SCTP_SOCKOPT_BINDX_REM:
2005                 /* 'optlen' is the size of the addresses buffer. */
2006                 retval = sctp_setsockopt_bindx(sk, (struct sockaddr *)optval,
2007                                                optlen, SCTP_BINDX_REM_ADDR);
2008                 break;
2009 
2010         case SCTP_DISABLE_FRAGMENTS:
2011                 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
2012                 break;
2013 
2014         case SCTP_EVENTS:
2015                 retval = sctp_setsockopt_events(sk, optval, optlen);
2016                 break;
2017 
2018         case SCTP_AUTOCLOSE:
2019                 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
2020                 break;
2021 
2022         case SCTP_PEER_ADDR_PARAMS:
2023                 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
2024                 break;
2025 
2026         case SCTP_INITMSG:
2027                 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
2028                 break;
2029         case SCTP_DEFAULT_SEND_PARAM:
2030                 retval = sctp_setsockopt_default_send_param(sk, optval,
2031                                                             optlen);
2032                 break;
2033         case SCTP_PRIMARY_ADDR:
2034                 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
2035                 break;
2036         case SCTP_SET_PEER_PRIMARY_ADDR:
2037                 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
2038                 break;
2039         case SCTP_NODELAY:
2040                 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
2041                 break;
2042         case SCTP_RTOINFO:
2043                 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
2044                 break;
2045         case SCTP_ASSOCINFO:
2046                 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
2047                 break;
2048         case SCTP_I_WANT_MAPPED_V4_ADDR:
2049                 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
2050                 break;
2051         case SCTP_MAXSEG:
2052                 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
2053                 break;
2054         default:
2055                 retval = -ENOPROTOOPT;
2056                 break;
2057         };
2058 
2059         sctp_release_sock(sk);
2060 
2061 out_nounlock:
2062         return retval;
2063 }
2064 
2065 /* API 3.1.6 connect() - UDP Style Syntax
2066  *
2067  * An application may use the connect() call in the UDP model to initiate an
2068  * association without sending data.
2069  *
2070  * The syntax is:
2071  *
2072  * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
2073  *
2074  * sd: the socket descriptor to have a new association added to.
2075  *
2076  * nam: the address structure (either struct sockaddr_in or struct
2077  *    sockaddr_in6 defined in RFC2553 [7]).
2078  *
2079  * len: the size of the address.
2080  */
2081 SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *uaddr,
2082                              int addr_len)
2083 {
2084         struct sctp_opt *sp;
2085         struct sctp_endpoint *ep;
2086         struct sctp_association *asoc;
2087         struct sctp_transport *transport;
2088         union sctp_addr to;
2089         struct sctp_af *af;
2090         sctp_scope_t scope;
2091         long timeo;
2092         int err = 0;
2093 
2094         sctp_lock_sock(sk);
2095 
2096         SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d)\n",
2097                           __FUNCTION__, sk, uaddr, addr_len);
2098 
2099         sp = sctp_sk(sk);
2100         ep = sp->ep;
2101 
2102         /* connect() cannot be done on a socket that is already in ESTABLISHED
2103          * state - UDP-style peeled off socket or a TCP-style socket that
2104          * is already connected.
2105          * It cannot be done even on a TCP-style listening socket.
2106          */
2107         if (sctp_sstate(sk, ESTABLISHED) ||
2108             (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
2109                 err = -EISCONN;
2110                 goto out_unlock;
2111         }
2112 
2113         err = sctp_verify_addr(sk, (union sctp_addr *)uaddr, addr_len);
2114         if (err)
2115                 goto out_unlock;
2116 
2117         if (addr_len > sizeof(to))
2118                 addr_len = sizeof(to);
2119         memcpy(&to, uaddr, addr_len);
2120         to.v4.sin_port = ntohs(to.v4.sin_port);
2121 
2122         asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
2123         if (asoc) {
2124                 if (asoc->state >= SCTP_STATE_ESTABLISHED)
2125                         err = -EISCONN;
2126                 else
2127                         err = -EALREADY;
2128                 goto out_unlock;
2129         }
2130 
2131         /* If we could not find a matching association on the endpoint,
2132          * make sure that there is no peeled-off association matching the
2133          * peer address even on another socket.
2134          */
2135         if (sctp_endpoint_is_peeled_off(ep, &to)) {
2136                 err = -EADDRNOTAVAIL;
2137                 goto out_unlock;
2138         }
2139 
2140         /* If a bind() or sctp_bindx() is not called prior to a connect()
2141          * call, the system picks an ephemeral port and will choose an address
2142          * set equivalent to binding with a wildcard address.
2143          */
2144         if (!ep->base.bind_addr.port) {
2145                 if (sctp_autobind(sk)) {
2146                         err = -EAGAIN;
2147                         goto out_unlock;
2148                 }
2149         }
2150 
2151         scope = sctp_scope(&to);
2152         asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
2153         if (!asoc) {
2154                 err = -ENOMEM;
2155                 goto out_unlock;
2156         }
2157 
2158         /* Prime the peer's transport structures.  */
2159         transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
2160         if (!transport) {
2161                 sctp_association_free(asoc);
2162                 goto out_unlock;
2163         }
2164         err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
2165         if (err < 0) {
2166                 sctp_association_free(asoc);
2167                 goto out_unlock;
2168         }
2169 
2170         err = sctp_primitive_ASSOCIATE(asoc, NULL);
2171         if (err < 0) {
2172                 sctp_association_free(asoc);
2173                 goto out_unlock;
2174         }
2175 
2176         /* Initialize sk's dport and daddr for getpeername() */
2177         inet_sk(sk)->dport = htons(asoc->peer.port);
2178         af = sctp_get_af_specific(to.sa.sa_family);
2179         af->to_sk_daddr(&to, sk);
2180 
2181         timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2182         err = sctp_wait_for_connect(asoc, &timeo);
2183 
2184 out_unlock:
2185         sctp_release_sock(sk);
2186 
2187         return err;
2188 }
2189 
2190 /* FIXME: Write comments. */
2191 SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
2192 {
2193         return -EOPNOTSUPP; /* STUB */
2194 }
2195 
2196 /* 4.1.4 accept() - TCP Style Syntax
2197  *
2198  * Applications use accept() call to remove an established SCTP
2199  * association from the accept queue of the endpoint.  A new socket
2200  * descriptor will be returned from accept() to represent the newly
2201  * formed association.
2202  */
2203 SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
2204 {
2205         struct sctp_opt *sp;
2206         struct sctp_endpoint *ep;
2207         struct sock *newsk = NULL;
2208         struct sctp_association *asoc;
2209         long timeo;
2210         int error = 0;
2211 
2212         sctp_lock_sock(sk);
2213 
2214         sp = sctp_sk(sk);
2215         ep = sp->ep;
2216 
2217         if (!sctp_style(sk, TCP)) {
2218                 error = -EOPNOTSUPP;
2219                 goto out;
2220         }
2221 
2222         if (!sctp_sstate(sk, LISTENING)) {
2223                 error = -EINVAL;
2224                 goto out;
2225         }
2226 
2227         timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
2228 
2229         error = sctp_wait_for_accept(sk, timeo);
2230         if (error)
2231                 goto out;
2232 
2233         /* We treat the list of associations on the endpoint as the accept
2234          * queue and pick the first association on the list.
2235          */
2236         asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
2237 
2238         newsk = sp->pf->create_accept_sk(sk, asoc);
2239         if (!newsk) {
2240                 error = -ENOMEM;
2241                 goto out;
2242         }
2243 
2244         /* Populate the fields of the newsk from the oldsk and migrate the
2245          * asoc to the newsk.
2246          */
2247         sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
2248 
2249 out:
2250         sctp_release_sock(sk);
2251         *err = error;
2252         return newsk;
2253 }
2254 
2255 /* The SCTP ioctl handler. */
2256 SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
2257 {
2258         return -ENOIOCTLCMD;
2259 }
2260 
2261 /* This is the function which gets called during socket creation to
2262  * initialized the SCTP-specific portion of the sock.
2263  * The sock structure should already be zero-filled memory.
2264  */
2265 SCTP_STATIC int sctp_init_sock(struct sock *sk)
2266 {
2267         struct sctp_endpoint *ep;
2268         struct sctp_opt *sp;
2269 
2270         SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
2271 
2272         sp = sctp_sk(sk);
2273 
2274         /* Initialize the SCTP per socket area.  */
2275         switch (sk->sk_type) {
2276         case SOCK_SEQPACKET:
2277                 sp->type = SCTP_SOCKET_UDP;
2278                 break;
2279         case SOCK_STREAM:
2280                 sp->type = SCTP_SOCKET_TCP;
2281                 break;
2282         default:
2283                 return -ESOCKTNOSUPPORT;
2284         }
2285 
2286         /* Initialize default send parameters. These parameters can be
2287          * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
2288          */
2289         sp->default_stream = 0;
2290         sp->default_ppid = 0;
2291         sp->default_flags = 0;
2292         sp->default_context = 0;
2293         sp->default_timetolive = 0;
2294 
2295         /* Initialize default setup parameters. These parameters
2296          * can be modified with the SCTP_INITMSG socket option or
2297          * overridden by the SCTP_INIT CMSG.
2298          */
2299         sp->initmsg.sinit_num_ostreams   = sctp_max_outstreams;
2300         sp->initmsg.sinit_max_instreams  = sctp_max_instreams;
2301         sp->initmsg.sinit_max_attempts   = sctp_max_retrans_init;
2302         sp->initmsg.sinit_max_init_timeo = JIFFIES_TO_MSECS(sctp_rto_max);
2303 
2304         /* Initialize default RTO related parameters.  These parameters can
2305          * be modified for with the SCTP_RTOINFO socket option.
2306          */
2307         sp->rtoinfo.srto_initial = JIFFIES_TO_MSECS(sctp_rto_initial);
2308         sp->rtoinfo.srto_max     = JIFFIES_TO_MSECS(sctp_rto_max);
2309         sp->rtoinfo.srto_min     = JIFFIES_TO_MSECS(sctp_rto_min);
2310 
2311         /* Initialize default association related parameters. These parameters
2312          * can be modified with the SCTP_ASSOCINFO socket option.
2313          */
2314         sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
2315         sp->assocparams.sasoc_number_peer_destinations = 0;
2316         sp->assocparams.sasoc_peer_rwnd = 0;
2317         sp->assocparams.sasoc_local_rwnd = 0;
2318         sp->assocparams.sasoc_cookie_life = 
2319                 JIFFIES_TO_MSECS(sctp_valid_cookie_life);
2320 
2321         /* Initialize default event subscriptions. By default, all the
2322          * options are off. 
2323          */
2324         memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
2325 
2326         /* Default Peer Address Parameters.  These defaults can
2327          * be modified via SCTP_PEER_ADDR_PARAMS
2328          */
2329         sp->paddrparam.spp_hbinterval = JIFFIES_TO_MSECS(sctp_hb_interval);
2330         sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
2331 
2332         /* If enabled no SCTP message fragmentation will be performed.
2333          * Configure through SCTP_DISABLE_FRAGMENTS socket option.
2334          */
2335         sp->disable_fragments = 0;
2336 
2337         /* Turn on/off any Nagle-like algorithm.  */
2338         sp->nodelay           = 1;
2339 
2340         /* Enable by default. */
2341         sp->v4mapped          = 1;
2342 
2343         /* Auto-close idle associations after the configured
2344          * number of seconds.  A value of 0 disables this
2345          * feature.  Configure through the SCTP_AUTOCLOSE socket option,
2346          * for UDP-style sockets only.
2347          */
2348         sp->autoclose         = 0;
2349 
2350         /* User specified fragmentation limit. */
2351         sp->user_frag         = 0;
2352 
2353         sp->pf = sctp_get_pf_specific(sk->sk_family);
2354 
2355         /* Control variables for partial data delivery. */
2356         sp->pd_mode           = 0;
2357         skb_queue_head_init(&sp->pd_lobby);
2358 
2359         /* Create a per socket endpoint structure.  Even if we
2360          * change the data structure relationships, this may still
2361          * be useful for storing pre-connect address information.
2362          */
2363         ep = sctp_endpoint_new(sk, GFP_KERNEL);
2364         if (!ep)
2365                 return -ENOMEM;
2366 
2367         sp->ep = ep;
2368         sp->hmac = NULL;
2369 
2370         SCTP_DBG_OBJCNT_INC(sock);
2371         return 0;
2372 }
2373 
2374 /* Cleanup any SCTP per socket resources.  */
2375 SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
2376 {
2377         struct sctp_endpoint *ep;
2378 
2379         SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
2380 
2381         /* Release our hold on the endpoint. */
2382         ep = sctp_sk(sk)->ep;
2383         sctp_endpoint_free(ep);
2384 
2385         return 0;
2386 }
2387 
2388 /* API 4.1.7 shutdown() - TCP Style Syntax
2389  *     int shutdown(int socket, int how);
2390  *
2391  *     sd      - the socket descriptor of the association to be closed.
2392  *     how     - Specifies the type of shutdown.  The  values  are
2393  *               as follows:
2394  *               SHUT_RD
2395  *                     Disables further receive operations. No SCTP
2396  *                     protocol action is taken.
2397  *               SHUT_WR
2398  *                     Disables further send operations, and initiates
2399  *                     the SCTP shutdown sequence.
2400  *               SHUT_RDWR
2401  *                     Disables further send  and  receive  operations
2402  *                     and initiates the SCTP shutdown sequence.
2403  */
2404 SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
2405 {
2406         struct sctp_endpoint *ep;
2407         struct sctp_association *asoc;
2408 
2409         if (!sctp_style(sk, TCP))
2410                 return;
2411 
2412         if (how & SEND_SHUTDOWN) {
2413                 ep = sctp_sk(sk)->ep;
2414                 if (!list_empty(&ep->asocs)) {
2415                         asoc = list_entry(ep->asocs.next,
2416                                           struct sctp_association, asocs);
2417                         sctp_primitive_SHUTDOWN(asoc, NULL);
2418                 }
2419         }
2420 }
2421 
2422 /* 7.2.1 Association Status (SCTP_STATUS)
2423 
2424  * Applications can retrieve current status information about an
2425  * association, including association state, peer receiver window size,
2426  * number of unacked data chunks, and number of data chunks pending
2427  * receipt.  This information is read-only.
2428  */
2429 static int sctp_getsockopt_sctp_status(struct sock *sk, int len, char *optval,
2430                                        int *optlen)
2431 {
2432         struct sctp_status status;
2433         struct sctp_association *asoc = NULL;
2434         struct sctp_transport *transport;
2435         sctp_assoc_t associd;
2436         int retval = 0;
2437 
2438         if (len != sizeof(status)) {
2439                 retval = -EINVAL;
2440                 goto out;
2441         }
2442 
2443         if (copy_from_user(&status, optval, sizeof(status))) {
2444                 retval = -EFAULT;
2445                 goto out;
2446         }
2447 
2448         associd = status.sstat_assoc_id;
2449         asoc = sctp_id2assoc(sk, associd);
2450         if (!asoc) {
2451                 retval = -EINVAL;
2452                 goto out;
2453         }
2454 
2455         transport = asoc->peer.primary_path;
2456 
2457         status.sstat_assoc_id = sctp_assoc2id(asoc);
2458         status.sstat_state = asoc->state;
2459         status.sstat_rwnd =  asoc->peer.rwnd;
2460         status.sstat_unackdata = asoc->unack_data;
2461 
2462         status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
2463         status.sstat_instrms = asoc->c.sinit_max_instreams;
2464         status.sstat_outstrms = asoc->c.sinit_num_ostreams;
2465         /* Just in time frag_point update. */
2466         if (sctp_sk(sk)->user_frag)
2467                 asoc->frag_point
2468                         = min_t(int, asoc->frag_point, sctp_sk(sk)->user_frag);
2469         status.sstat_fragmentation_point = asoc->frag_point;
2470         status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2471         memcpy(&status.sstat_primary.spinfo_address,
2472                &(transport->ipaddr), sizeof(union sctp_addr));
2473         /* Map ipv4 address into v4-mapped-on-v6 address.  */
2474         sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
2475                 (union sctp_addr *)&status.sstat_primary.spinfo_address);
2476         status.sstat_primary.spinfo_state = transport->active;
2477         status.sstat_primary.spinfo_cwnd = transport->cwnd;
2478         status.sstat_primary.spinfo_srtt = transport->srtt;
2479         status.sstat_primary.spinfo_rto = JIFFIES_TO_MSECS(transport->rto);
2480         status.sstat_primary.spinfo_mtu = transport->pmtu;
2481 
2482         if (put_user(len, optlen)) {
2483                 retval = -EFAULT;
2484                 goto out;
2485         }
2486 
2487         SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %p\n",
2488                           len, status.sstat_state, status.sstat_rwnd,
2489                           status.sstat_assoc_id);
2490 
2491         if (copy_to_user(optval, &status, len)) {
2492                 retval = -EFAULT;
2493                 goto out;
2494         }
2495 
2496 out:
2497         return (retval);
2498 }
2499 
2500 
2501 /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
2502  *
2503  * Applications can retrieve information about a specific peer address
2504  * of an association, including its reachability state, congestion
2505  * window, and retransmission timer values.  This information is
2506  * read-only.
2507  */
2508 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
2509                                           char *optval, int *optlen)
2510 {
2511         struct sctp_paddrinfo pinfo;
2512         struct sctp_transport *transport;
2513         int retval = 0;
2514 
2515         if (len != sizeof(pinfo)) {
2516                 retval = -EINVAL;
2517                 goto out;
2518         }
2519 
2520         if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
2521                 retval = -EFAULT;
2522                 goto out;
2523         }
2524 
2525         transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
2526                                            pinfo.spinfo_assoc_id);
2527         if (!transport)
2528                 return -EINVAL;
2529 
2530         pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
2531         pinfo.spinfo_state = transport->active;
2532         pinfo.spinfo_cwnd = transport->cwnd;
2533         pinfo.spinfo_srtt = transport->srtt;
2534         pinfo.spinfo_rto = JIFFIES_TO_MSECS(transport->rto);
2535         pinfo.spinfo_mtu = transport->pmtu;
2536 
2537         if (put_user(len, optlen)) {
2538                 retval = -EFAULT;
2539                 goto out;
2540         }
2541 
2542         if (copy_to_user(optval, &pinfo, len)) {
2543                 retval = -EFAULT;
2544                 goto out;
2545         }
2546 
2547 out:
2548         return (retval);
2549 }
2550 
2551 /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2552  *
2553  * This option is a on/off flag.  If enabled no SCTP message
2554  * fragmentation will be performed.  Instead if a message being sent
2555  * exceeds the current PMTU size, the message will NOT be sent and
2556  * instead a error will be indicated to the user.
2557  */
2558 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
2559                                                     char *optval, int *optlen)
2560 {
2561         int val;
2562 
2563         if (len < sizeof(int))
2564                 return -EINVAL;
2565 
2566         len = sizeof(int);
2567         val = (sctp_sk(sk)->disable_fragments == 1);
2568         if (put_user(len, optlen))
2569                 return -EFAULT;
2570         if (copy_to_user(optval, &val, len))
2571                 return -EFAULT;
2572         return 0;
2573 }
2574 
2575 /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
2576  *
2577  * This socket option is used to specify various notifications and
2578  * ancillary data the user wishes to receive.
2579  */
2580 static int sctp_getsockopt_events(struct sock *sk, int len, char *optval,
2581                                   int *optlen)
2582 {
2583         if (len != sizeof(struct sctp_event_subscribe))
2584                 return -EINVAL;
2585         if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
2586                 return -EFAULT;
2587         return 0;
2588 }
2589 
2590 /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2591  *
2592  * This socket option is applicable to the UDP-style socket only.  When
2593  * set it will cause associations that are idle for more than the
2594  * specified number of seconds to automatically close.  An association
2595  * being idle is defined an association that has NOT sent or received
2596  * user data.  The special value of '' indicates that no automatic
2597  * close of any associations should be performed.  The option expects an
2598  * integer defining the number of seconds of idle time before an
2599  * association is closed.
2600  */
2601 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char *optval, int *optlen)
2602 {
2603         /* Applicable to UDP-style socket only */
2604         if (sctp_style(sk, TCP))
2605                 return -EOPNOTSUPP;
2606         if (len != sizeof(int))
2607                 return -EINVAL;
2608         if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
2609                 return -EFAULT;
2610         return 0;
2611 }
2612 
2613 /* Helper routine to branch off an association to a new socket.  */
2614 SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
2615                                 struct socket **sockp)
2616 {
2617         struct sock *sk = asoc->base.sk;
2618         struct socket *sock;
2619         int err = 0;
2620 
2621         /* An association cannot be branched off from an already peeled-off
2622          * socket, nor is this supported for tcp style sockets.
2623          */
2624         if (!sctp_style(sk, UDP))
2625                 return -EINVAL;
2626 
2627         /* Create a new socket.  */
2628         err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
2629         if (err < 0)
2630                 return err;
2631 
2632         /* Populate the fields of the newsk from the oldsk and migrate the
2633          * asoc to the newsk.
2634          */
2635         sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
2636         *sockp = sock;
2637 
2638         return err;
2639 }
2640 
2641 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char *optval, int *optlen)
2642 {
2643         sctp_peeloff_arg_t peeloff;
2644         struct socket *newsock;
2645         int retval = 0;
2646         struct sctp_association *asoc;
2647 
2648         if (len != sizeof(sctp_peeloff_arg_t))
2649                 return -EINVAL;
2650         if (copy_from_user(&peeloff, optval, len))
2651                 return -EFAULT;
2652 
2653         asoc = sctp_id2assoc(sk, peeloff.associd);
2654         if (!asoc) {
2655                 retval = -EINVAL;
2656                 goto out;
2657         }
2658 
2659         SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
2660 
2661         retval = sctp_do_peeloff(asoc, &newsock);
2662         if (retval < 0)
2663                 goto out;
2664 
2665         /* Map the socket to an unused fd that can be returned to the user.  */
2666         retval = sock_map_fd(newsock);
2667         if (retval < 0) {
2668                 sock_release(newsock);
2669                 goto out;
2670         }
2671 
2672         SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
2673                           __FUNCTION__, sk, asoc, newsock->sk, retval);
2674 
2675         /* Return the fd mapped to the new socket.  */
2676         peeloff.sd = retval;
2677         if (copy_to_user(optval, &peeloff, len))
2678                 retval = -EFAULT;
2679 
2680 out:
2681         return retval;
2682 }
2683 
2684 /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2685  *
2686  * Applications can enable or disable heartbeats for any peer address of
2687  * an association, modify an address's heartbeat interval, force a
2688  * heartbeat to be sent immediately, and adjust the address's maximum
2689  * number of retransmissions sent before an address is considered
2690  * unreachable.  The following structure is used to access and modify an
2691  * address's parameters:
2692  *
2693  *  struct sctp_paddrparams {
2694  *      sctp_assoc_t            spp_assoc_id;
2695  *      struct sockaddr_storage spp_address;
2696  *      uint32_t                spp_hbinterval;
2697  *      uint16_t                spp_pathmaxrxt;
2698  *  };
2699  *
2700  *   spp_assoc_id    - (UDP style socket) This is filled in the application,
2701  *                     and identifies the association for this query.
2702  *   spp_address     - This specifies which address is of interest.
2703  *   spp_hbinterval  - This contains the value of the heartbeat interval,
2704  *                     in milliseconds.  A value of 0, when modifying the
2705  *                     parameter, specifies that the heartbeat on this
2706  *                     address should be disabled. A value of UINT32_MAX
2707  *                     (4294967295), when modifying the parameter,
2708  *                     specifies that a heartbeat should be sent
2709  *                     immediately to the peer address, and the current
2710  *                     interval should remain unchanged.
2711  *   spp_pathmaxrxt  - This contains the maximum number of
2712  *                     retransmissions before this address shall be
2713  *                     considered unreachable.
2714  */
2715 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
2716                                                 char *optval, int *optlen)
2717 {
2718         struct sctp_paddrparams params;
2719         struct sctp_transport *trans;
2720 
2721         if (len != sizeof(struct sctp_paddrparams))
2722                 return -EINVAL;
2723         if (copy_from_user(&params, optval, *optlen))
2724                 return -EFAULT;
2725 
2726         trans = sctp_addr_id2transport(sk, &params.spp_address,
2727                                        params.spp_assoc_id);
2728         if (!trans)
2729                 return -EINVAL;
2730 
2731         /* The value of the heartbeat interval, in milliseconds. A value of 0,
2732          * when modifying the parameter, specifies that the heartbeat on this
2733          * address should be disabled.
2734          */
2735         if (!trans->hb_allowed)
2736                 params.spp_hbinterval = 0;
2737         else
2738                 params.spp_hbinterval = JIFFIES_TO_MSECS(trans->hb_interval);
2739 
2740         /* spp_pathmaxrxt contains the maximum number of retransmissions
2741          * before this address shall be considered unreachable.
2742          */
2743         params.spp_pathmaxrxt = trans->error_threshold;
2744 
2745         if (copy_to_user(optval, &params, len))
2746                 return -EFAULT;
2747 
2748         if (put_user(len, optlen))
2749                 return -EFAULT;
2750 
2751         return 0;
2752 }
2753 
2754 /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2755  *
2756  * Applications can specify protocol parameters for the default association
2757  * initialization.  The option name argument to setsockopt() and getsockopt()
2758  * is SCTP_INITMSG.
2759  *
2760  * Setting initialization parameters is effective only on an unconnected
2761  * socket (for UDP-style sockets only future associations are effected
2762  * by the change).  With TCP-style sockets, this option is inherited by
2763  * sockets derived from a listener socket.
2764  */
2765 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char *optval, int *optlen)
2766 {
2767         if (len != sizeof(struct sctp_initmsg))
2768                 return -EINVAL;
2769         if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
2770                 return -EFAULT;
2771         return 0;
2772 }
2773 
2774 static int sctp_getsockopt_peer_addrs_num(struct sock *sk, int len,
2775                                           char *optval, int *optlen)
2776 {
2777         sctp_assoc_t id;
2778         struct sctp_association *asoc;
2779         struct list_head *pos;
2780         int cnt = 0;
2781 
2782         if (len != sizeof(sctp_assoc_t))
2783                 return -EINVAL;
2784 
2785         if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
2786                 return -EFAULT;
2787 
2788         /* For UDP-style sockets, id specifies the association to query.  */
2789         asoc = sctp_id2assoc(sk, id);
2790         if (!asoc)
2791                 return -EINVAL;
2792 
2793         list_for_each(pos, &asoc->peer.transport_addr_list) {
2794                 cnt ++;
2795         }
2796 
2797         return cnt;
2798 }
2799 
2800 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
2801                                       char *optval, int *optlen)
2802 {
2803         struct sctp_association *asoc;
2804         struct list_head *pos;
2805         int cnt = 0;
2806         struct sctp_getaddrs getaddrs;
2807         struct sctp_transport *from;
2808         void *to;
2809         union sctp_addr temp;
2810         struct sctp_opt *sp = sctp_sk(sk);
2811         int addrlen;
2812 
2813         if (len != sizeof(struct sctp_getaddrs))
2814                 return -EINVAL;
2815 
2816         if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
2817                 return -EFAULT;
2818 
2819         if (getaddrs.addr_num <= 0) return -EINVAL;
2820 
2821         /* For UDP-style sockets, id specifies the association to query.  */
2822         asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
2823         if (!asoc)
2824                 return -EINVAL;
2825 
2826         to = (void *)getaddrs.addrs;
2827         list_for_each(pos, &asoc->peer.transport_addr_list) {
2828                 from = list_entry(pos, struct sctp_transport, transports);
2829                 memcpy(&temp, &from->ipaddr, sizeof(temp));
2830                 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
2831                 addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
2832                 if (copy_to_user(to, &temp, addrlen))
2833                         return -EFAULT;
2834                 to += addrlen ;
2835                 cnt ++;
2836                 if (cnt >= getaddrs.addr_num) break;
2837         }
2838         getaddrs.addr_num = cnt;
2839         if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
2840                 return -EFAULT;
2841 
2842         return 0;
2843 }
2844 
2845 static int sctp_getsockopt_local_addrs_num(struct sock *sk, int len,
2846                                                 char *optval, int *optlen)
2847 {
2848         sctp_assoc_t id;
2849         struct sctp_bind_addr *bp;
2850         struct sctp_association *asoc;
2851         struct list_head *pos;
2852         int cnt = 0;
2853 
2854         if (len != sizeof(sctp_assoc_t))
2855                 return -EINVAL;
2856 
2857         if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
2858                 return -EFAULT;
2859 
2860         /*
2861          *  For UDP-style sockets, id specifies the association to query.
2862          *  If the id field is set to the value '' then the locally bound
2863          *  addresses are returned without regard to any particular
2864          *  association.
2865          */
2866         if (0 == id) {
2867                 bp = &sctp_sk(sk)->ep->base.bind_addr;
2868         } else {
2869                 asoc = sctp_id2assoc(sk, id);
2870                 if (!asoc)
2871                         return -EINVAL;
2872                 bp = &asoc->base.bind_addr;
2873         }
2874 
2875         list_for_each(pos, &bp->address_list) {
2876                 cnt ++;
2877         }
2878 
2879         return cnt;
2880 }
2881 
2882 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
2883                                         char *optval, int *optlen)
2884 {
2885         struct sctp_bind_addr *bp;
2886         struct sctp_association *asoc;
2887         struct list_head *pos;
2888         int cnt = 0;
2889         struct sctp_getaddrs getaddrs;
2890         struct sctp_sockaddr_entry *from;
2891         void *to;
2892         union sctp_addr temp;
2893         struct sctp_opt *sp = sctp_sk(sk);
2894         int addrlen;
2895 
2896         if (len != sizeof(struct sctp_getaddrs))
2897                 return -EINVAL;
2898 
2899         if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
2900                 return -EFAULT;
2901 
2902         if (getaddrs.addr_num <= 0) return -EINVAL;
2903         /*
2904          *  For UDP-style sockets, id specifies the association to query.
2905          *  If the id field is set to the value '' then the locally bound
2906          *  addresses are returned without regard to any particular
2907          *  association.
2908          */
2909         if (0 == getaddrs.assoc_id) {
2910                 bp = &sctp_sk(sk)->ep->base.bind_addr;
2911         } else {
2912                 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
2913                 if (!asoc)
2914                         return -EINVAL;
2915                 bp = &asoc->base.bind_addr;
2916         }
2917 
2918         to = (void *)getaddrs.addrs;
2919         list_for_each(pos, &bp->address_list) {
2920                 from = list_entry(pos,
2921                                 struct sctp_sockaddr_entry,
2922                                 list);
2923                 memcpy(&temp, &from->a, sizeof(temp));
2924                 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
2925                 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
2926                 if (copy_to_user(to, &temp, addrlen))
2927                         return -EFAULT;
2928                 to += addrlen;
2929                 cnt ++;
2930                 if (cnt >= getaddrs.addr_num) break;
2931         }
2932         getaddrs.addr_num = cnt;
2933         if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
2934                 return -EFAULT;
2935 
2936         return 0;
2937 }
2938 
2939 /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2940  *
2941  * Requests that the local SCTP stack use the enclosed peer address as
2942  * the association primary.  The enclosed address must be one of the
2943  * association peer's addresses.
2944  */
2945 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
2946                                         char *optval, int *optlen)
2947 {
2948         struct sctp_prim prim;
2949         struct sctp_association *asoc;
2950         struct sctp_opt *sp = sctp_sk(sk);
2951 
2952         if (len != sizeof(struct sctp_prim))
2953                 return -EINVAL;
2954 
2955         if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2956                 return -EFAULT;
2957 
2958         asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
2959         if (!asoc)
2960                 return -EINVAL;
2961 
2962         if (!asoc->peer.primary_path)
2963                 return -ENOTCONN;
2964 
2965         memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
2966                sizeof(union sctp_addr));
2967 
2968         sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
2969                         (union sctp_addr *)&prim.ssp_addr);
2970 
2971         if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
2972                 return -EFAULT;
2973 
2974         return 0;
2975 }
2976 
2977 /*
2978  *
2979  * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2980  *
2981  *   Applications that wish to use the sendto() system call may wish to
2982  *   specify a default set of parameters that would normally be supplied
2983  *   through the inclusion of ancillary data.  This socket option allows
2984  *   such an application to set the default sctp_sndrcvinfo structure.
2985 
2986 
2987  *   The application that wishes to use this socket option simply passes
2988  *   in to this call the sctp_sndrcvinfo structure defined in Section
2989  *   5.2.2) The input parameters accepted by this call include
2990  *   sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2991  *   sinfo_timetolive.  The user must provide the sinfo_assoc_id field in
2992  *   to this call if the caller is using the UDP model.
2993  *
2994  *   For getsockopt, it get the default sctp_sndrcvinfo structure.
2995  */
2996 static int sctp_getsockopt_default_send_param(struct sock *sk,
2997                                         int len, char *optval, int *optlen)
2998 {
2999         struct sctp_sndrcvinfo info;
3000         struct sctp_association *asoc;
3001         struct sctp_opt *sp = sctp_sk(sk);
3002 
3003         if (len != sizeof(struct sctp_sndrcvinfo))
3004                 return -EINVAL;
3005         if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
3006                 return -EFAULT;
3007 
3008         asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3009         if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3010                 return -EINVAL;
3011 
3012         if (asoc) {
3013                 info.sinfo_stream = asoc->default_stream;
3014                 info.sinfo_flags = asoc->default_flags;
3015                 info.sinfo_ppid = asoc->default_ppid;
3016                 info.sinfo_context = asoc->default_context;
3017                 info.sinfo_timetolive = asoc->default_timetolive;
3018         } else {
3019                 info.sinfo_stream = sp->default_stream;
3020                 info.sinfo_flags = sp->default_flags;
3021                 info.sinfo_ppid = sp->default_ppid;
3022                 info.sinfo_context = sp->default_context;
3023                 info.sinfo_timetolive = sp->default_timetolive;
3024         }
3025 
3026         if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
3027                 return -EFAULT;
3028 
3029         return 0;
3030 }
3031 
3032 /*
3033  *
3034  * 7.1.5 SCTP_NODELAY
3035  *
3036  * Turn on/off any Nagle-like algorithm.  This means that packets are
3037  * generally sent as soon as possible and no unnecessary delays are
3038  * introduced, at the cost of more packets in the network.  Expects an
3039  * integer boolean flag.
3040  */
3041 
3042 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
3043                                    char *optval, int *optlen)
3044 {
3045         int val;
3046 
3047         if (len < sizeof(int))
3048                 return -EINVAL;
3049 
3050         len = sizeof(int);
3051         val = (sctp_sk(sk)->nodelay == 1);
3052         if (put_user(len, optlen))
3053                 return -EFAULT;
3054         if (copy_to_user(optval, &val, len))
3055                 return -EFAULT;
3056         return 0;
3057 }
3058 
3059 /*
3060  *
3061  * 7.1.1 SCTP_RTOINFO
3062  *
3063  * The protocol parameters used to initialize and bound retransmission
3064  * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3065  * and modify these parameters.
3066  * All parameters are time values, in milliseconds.  A value of 0, when
3067  * modifying the parameters, indicates that the current value should not
3068  * be changed.
3069  *
3070  */
3071 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, char *optval,
3072                                 int *optlen) {
3073         struct sctp_rtoinfo rtoinfo;
3074         struct sctp_association *asoc;
3075 
3076         if (len != sizeof (struct sctp_rtoinfo))
3077                 return -EINVAL;
3078 
3079         if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
3080                 return -EFAULT;
3081 
3082         asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3083 
3084         if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3085                 return -EINVAL;
3086 
3087         /* Values corresponding to the specific association. */
3088         if (asoc) {
3089                 rtoinfo.srto_initial = JIFFIES_TO_MSECS(asoc->rto_initial);
3090                 rtoinfo.srto_max = JIFFIES_TO_MSECS(asoc->rto_max);
3091                 rtoinfo.srto_min = JIFFIES_TO_MSECS(asoc->rto_min);
3092         } else {
3093                 /* Values corresponding to the endpoint. */
3094                 struct sctp_opt *sp = sctp_sk(sk);
3095 
3096                 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
3097                 rtoinfo.srto_max = sp->rtoinfo.srto_max;
3098                 rtoinfo.srto_min = sp->rtoinfo.srto_min;
3099         }
3100 
3101         if (put_user(len, optlen))
3102                 return -EFAULT;
3103 
3104         if (copy_to_user(optval, &rtoinfo, len))
3105                 return -EFAULT;
3106 
3107         return 0;
3108 }
3109 
3110 /*
3111  *
3112  * 7.1.2 SCTP_ASSOCINFO
3113  *
3114  * This option is used to tune the the maximum retransmission attempts
3115  * of the association.
3116  * Returns an error if the new association retransmission value is
3117  * greater than the sum of the retransmission value  of the peer.
3118  * See [SCTP] for more information.
3119  *
3120  */
3121 static int sctp_getsockopt_associnfo(struct sock *sk, int len, char *optval,
3122                                      int *optlen)
3123 {
3124 
3125         struct sctp_assocparams assocparams;
3126         struct sctp_association *asoc;
3127         struct list_head *pos;
3128         int cnt = 0;
3129 
3130         if (len != sizeof (struct sctp_assocparams))
3131                 return -EINVAL;
3132 
3133         if (copy_from_user(&assocparams, optval,
3134                         sizeof (struct sctp_assocparams)))
3135                 return -EFAULT;
3136 
3137         asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3138 
3139         if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3140                 return -EINVAL;
3141 
3142         /* Values correspoinding to the specific association */
3143         if (assocparams.sasoc_assoc_id != 0) {
3144                 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
3145                 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
3146                 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
3147                 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
3148                                                 * 1000) +
3149                                                 (asoc->cookie_life.tv_usec
3150                                                 / 1000);
3151 
3152                 list_for_each(pos, &asoc->peer.transport_addr_list) {
3153                         cnt ++;
3154                 }
3155 
3156                 assocparams.sasoc_number_peer_destinations = cnt;
3157         } else {
3158                 /* Values corresponding to the endpoint */
3159                 struct sctp_opt *sp = sctp_sk(sk);
3160 
3161                 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
3162                 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
3163                 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
3164                 assocparams.sasoc_cookie_life =
3165                                         sp->assocparams.sasoc_cookie_life;
3166                 assocparams.sasoc_number_peer_destinations =
3167                                         sp->assocparams.
3168                                         sasoc_number_peer_destinations;
3169         }
3170 
3171         if (put_user(len, optlen))
3172                 return -EFAULT;
3173 
3174         if (copy_to_user(optval, &assocparams, len))
3175                 return -EFAULT;
3176 
3177         return 0;
3178 }
3179 
3180 /*
3181  * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3182  *
3183  * This socket option is a boolean flag which turns on or off mapped V4
3184  * addresses.  If this option is turned on and the socket is type
3185  * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3186  * If this option is turned off, then no mapping will be done of V4
3187  * addresses and a user will receive both PF_INET6 and PF_INET type
3188  * addresses on the socket.
3189  */
3190 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
3191                                     char *optval, int *optlen)
3192 {
3193         int val;
3194         struct sctp_opt *sp = sctp_sk(sk);
3195 
3196         if (len < sizeof(int))
3197                 return -EINVAL;
3198 
3199         len = sizeof(int);
3200         val = sp->v4mapped;
3201         if (put_user(len, optlen))
3202                 return -EFAULT;
3203         if (copy_to_user(optval, &val, len))
3204                 return -EFAULT;
3205 
3206         return 0;
3207 }
3208 
3209 /*
3210  * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
3211  *
3212  * This socket option specifies the maximum size to put in any outgoing
3213  * SCTP chunk.  If a message is larger than this size it will be
3214  * fragmented by SCTP into the specified size.  Note that the underlying
3215  * SCTP implementation may fragment into smaller sized chunks when the
3216  * PMTU of the underlying association is smaller than the value set by
3217  * the user.
3218  */
3219 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
3220                                   char *optval, int *optlen)
3221 {
3222         int val;
3223 
3224         if (len < sizeof(int))
3225                 return -EINVAL;
3226 
3227         len = sizeof(int);
3228 
3229         val = sctp_sk(sk)->user_frag;
3230         if (put_user(len, optlen))
3231                 return -EFAULT;
3232         if (copy_to_user(optval, &val, len))
3233                 return -EFAULT;
3234 
3235         return 0;
3236 }
3237 
3238 SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
3239                                 char *optval, int *optlen)
3240 {
3241         int retval = 0;
3242         int len;
3243 
3244         SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p, ...)\n", sk);
3245 
3246         /* I can hardly begin to describe how wrong this is.  This is
3247          * so broken as to be worse than useless.  The API draft
3248          * REALLY is NOT helpful here...  I am not convinced that the
3249          * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
3250          * are at all well-founded.
3251          */
3252         if (level != SOL_SCTP) {
3253                 struct sctp_af *af = sctp_sk(sk)->pf->af;
3254 
3255                 retval = af->getsockopt(sk, level, optname, optval, optlen);
3256                 return retval;
3257         }
3258 
3259         if (get_user(len, optlen))
3260                 return -EFAULT;
3261 
3262         sctp_lock_sock(sk);
3263 
3264         switch (optname) {
3265         case SCTP_STATUS:
3266                 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
3267                 break;
3268         case SCTP_DISABLE_FRAGMENTS:
3269                 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
3270                                                            optlen);
3271                 break;
3272         case SCTP_EVENTS:
3273                 retval = sctp_getsockopt_events(sk, len, optval, optlen);
3274                 break;
3275         case SCTP_AUTOCLOSE:
3276                 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
3277                 break;
3278         case SCTP_SOCKOPT_PEELOFF:
3279                 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
3280                 break;
3281         case SCTP_PEER_ADDR_PARAMS:
3282                 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
3283                                                           optlen);
3284                 break;
3285         case SCTP_INITMSG:
3286                 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
3287                 break;
3288         case SCTP_GET_PEER_ADDRS_NUM:
3289                 retval = sctp_getsockopt_peer_addrs_num(sk, len, optval,
3290                                                         optlen);
3291                 break;
3292         case SCTP_GET_LOCAL_ADDRS_NUM:
3293                 retval = sctp_getsockopt_local_addrs_num(sk, len, optval,
3294                                                          optlen);
3295                 break;
3296         case SCTP_GET_PEER_ADDRS:
3297                 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
3298                                                     optlen);
3299                 break;
3300         case SCTP_GET_LOCAL_ADDRS:
3301                 retval = sctp_getsockopt_local_addrs(sk, len, optval,
3302                                                      optlen);
3303                 break;
3304         case SCTP_DEFAULT_SEND_PARAM:
3305                 retval = sctp_getsockopt_default_send_param(sk, len,
3306                                                             optval, optlen);
3307                 break;
3308         case SCTP_PRIMARY_ADDR:
3309                 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
3310                 break;
3311         case SCTP_NODELAY:
3312                 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
3313                 break;
3314         case SCTP_RTOINFO:
3315                 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
3316                 break;
3317         case SCTP_ASSOCINFO:
3318                 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
3319                 break;
3320         case SCTP_I_WANT_MAPPED_V4_ADDR:
3321                 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
3322                 break;
3323         case SCTP_MAXSEG:
3324                 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
3325                 break;
3326         case SCTP_GET_PEER_ADDR_INFO:
3327                 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
3328                                                         optlen);
3329                 break;
3330         default:
3331                 retval = -ENOPROTOOPT;
3332                 break;
3333         };
3334 
3335         sctp_release_sock(sk);
3336         return retval;
3337 }
3338 
3339 static void sctp_hash(struct sock *sk)
3340 {
3341         /* STUB */
3342 }
3343 
3344 static void sctp_unhash(struct sock *sk)
3345 {
3346         /* STUB */
3347 }
3348 
3349 /* Check if port is acceptable.  Possibly find first available port.
3350  *
3351  * The port hash table (contained in the 'global' SCTP protocol storage
3352  * returned by struct sctp_protocol *sctp_get_protocol()). The hash
3353  * table is an array of 4096 lists (sctp_bind_hashbucket). Each
3354  * list (the list number is the port number hashed out, so as you
3355  * would expect from a hash function, all the ports in a given list have
3356  * such a number that hashes out to the same list number; you were
3357  * expecting that, right?); so each list has a set of ports, with a
3358  * link to the socket (struct sock) that uses it, the port number and
3359  * a fastreuse flag (FIXME: NPI ipg).
3360  */
3361 static struct sctp_bind_bucket *sctp_bucket_create(
3362         struct sctp_bind_hashbucket *head, unsigned short snum);
3363 
3364 static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
3365 {
3366         struct sctp_bind_hashbucket *head; /* hash list */
3367         struct sctp_bind_bucket *pp; /* hash list port iterator */
3368         unsigned short snum;
3369         int ret;
3370 
3371         /* NOTE:  Remember to put this back to net order. */
3372         addr->v4.sin_port = ntohs(addr->v4.sin_port);
3373         snum = addr->v4.sin_port;
3374 
3375         SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
3376         sctp_local_bh_disable();
3377 
3378         if (snum == 0) {
3379                 /* Search for an available port.
3380                  *
3381                  * 'sctp_port_rover' was the last port assigned, so
3382                  * we start to search from 'sctp_port_rover +
3383                  * 1'. What we do is first check if port 'rover' is
3384                  * already in the hash table; if not, we use that; if
3385                  * it is, we try next.
3386                  */
3387                 int low = sysctl_local_port_range[0];
3388                 int high = sysctl_local_port_range[1];
3389                 int remaining = (high - low) + 1;
3390                 int rover;
3391                 int index;
3392 
3393                 sctp_spin_lock(&sctp_port_alloc_lock);
3394                 rover = sctp_port_rover;
3395                 do {
3396                         rover++;
3397                         if ((rover < low) || (rover > high))
3398                                 rover = low;
3399                         index = sctp_phashfn(rover);
3400                         head = &sctp_port_hashtable[index];
3401                         sctp_spin_lock(&head->lock);
3402                         for (pp = head->chain; pp; pp = pp->next)
3403                                 if (pp->port == rover)
3404                                         goto next;
3405                         break;
3406                 next:
3407                         sctp_spin_unlock(&head->lock);
3408                 } while (--remaining > 0);
3409                 sctp_port_rover = rover;
3410                 sctp_spin_unlock(&sctp_port_alloc_lock);
3411 
3412                 /* Exhausted local port range during search? */
3413                 ret = 1;
3414                 if (remaining <= 0)
3415                         goto fail;
3416 
3417                 /* OK, here is the one we will use.  HEAD (the port
3418                  * hash table list entry) is non-NULL and we hold it's
3419                  * mutex.
3420                  */
3421                 snum = rover;
3422         } else {
3423                 /* We are given an specific port number; we verify
3424                  * that it is not being used. If it is used, we will
3425                  * exahust the search in the hash list corresponding
3426                  * to the port number (snum) - we detect that with the
3427                  * port iterator, pp being NULL.
3428                  */
3429                 head = &sctp_port_hashtable[sctp_phashfn(snum)];
3430                 sctp_spin_lock(&head->lock);
3431                 for (pp = head->chain; pp; pp = pp->next) {
3432                         if (pp->port == snum)
3433                                 goto pp_found;
3434                 }
3435         }
3436         pp = NULL;
3437         goto pp_not_found;
3438 pp_found:
3439         if (!hlist_empty(&pp->owner)) {
3440                 /* We had a port hash table hit - there is an
3441                  * available port (pp != NULL) and it is being
3442                  * used by other socket (pp->owner not empty); that other
3443                  * socket is going to be sk2.
3444                  */
3445                 int reuse = sk->sk_reuse;
3446                 struct sock *sk2;
3447                 struct hlist_node *node;
3448 
3449                 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
3450                 if (pp->fastreuse && sk->sk_reuse)
3451                         goto success;
3452 
3453                 /* Run through the list of sockets bound to the port
3454                  * (pp->port) [via the pointers bind_next and
3455                  * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
3456                  * we get the endpoint they describe and run through
3457                  * the endpoint's list of IP (v4 or v6) addresses,
3458                  * comparing each of the addresses with the address of
3459                  * the socket sk. If we find a match, then that means
3460                  * that this port/socket (sk) combination are already
3461                  * in an endpoint.
3462                  */
3463                 sk_for_each_bound(sk2, node, &pp->owner) {
3464                         struct sctp_endpoint *ep2;
3465                         ep2 = sctp_sk(sk2)->ep;
3466 
3467                         if (reuse && sk2->sk_reuse)
3468                                 continue;
3469 
3470                         if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
3471                                                  sctp_sk(sk))) {
3472                                 ret = (long)sk2;
3473                                 goto fail_unlock;
3474                         }
3475                 }
3476                 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
3477         }
3478 pp_not_found:
3479         /* If there was a hash table miss, create a new port.  */
3480         ret = 1;
3481         if (!pp && !(pp = sctp_bucket_create(head, snum)))
3482                 goto fail_unlock;
3483 
3484         /* In either case (hit or miss), make sure fastreuse is 1 only
3485          * if sk->sk_reuse is too (that is, if the caller requested
3486          * SO_REUSEADDR on this socket -sk-).
3487          */
3488         if (hlist_empty(&pp->owner))
3489                 pp->fastreuse = sk->sk_reuse ? 1 : 0;
3490         else if (pp->fastreuse && !sk->sk_reuse)
3491                 pp->fastreuse = 0;
3492 
3493         /* We are set, so fill up all the data in the hash table
3494          * entry, tie the socket list information with the rest of the
3495          * sockets FIXME: Blurry, NPI (ipg).
3496          */
3497 success:
3498         inet_sk(sk)->num = snum;
3499         if (!sctp_sk(sk)->bind_hash) {
3500                 sk_add_bind_node(sk, &pp->owner);
3501                 sctp_sk(sk)->bind_hash = pp;
3502         }
3503         ret = 0;
3504 
3505 fail_unlock:
3506         sctp_spin_unlock(&head->lock);
3507 
3508 fail:
3509         sctp_local_bh_enable();
3510         addr->v4.sin_port = htons(addr->v4.sin_port);
3511         return ret;
3512 }
3513 
3514 /* Assign a 'snum' port to the socket.  If snum == 0, an ephemeral
3515  * port is requested.
3516  */
3517 static int sctp_get_port(struct sock *sk, unsigned short snum)
3518 {
3519         long ret;
3520         union sctp_addr addr;
3521         struct sctp_af *af = sctp_sk(sk)->pf->af;
3522 
3523         /* Set up a dummy address struct from the sk. */
3524         af->from_sk(&addr, sk);
3525         addr.v4.sin_port = htons(snum);
3526 
3527         /* Note: sk->sk_num gets filled in if ephemeral port request. */
3528         ret = sctp_get_port_local(sk, &addr);
3529 
3530         return (ret ? 1 : 0);
3531 }
3532 
3533 /*
3534  * 3.1.3 listen() - UDP Style Syntax
3535  *
3536  *   By default, new associations are not accepted for UDP style sockets.
3537  *   An application uses listen() to mark a socket as being able to
3538  *   accept new associations.
3539  */
3540 SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
3541 {
3542         struct sctp_opt *sp = sctp_sk(sk);
3543         struct sctp_endpoint *ep = sp->ep;
3544 
3545         /* Only UDP style sockets that are not peeled off are allowed to
3546          * listen().
3547          */
3548         if (!sctp_style(sk, UDP))
3549                 return -EINVAL;
3550 
3551         /* If backlog is zero, disable listening. */
3552         if (!backlog) {
3553                 if (sctp_sstate(sk, CLOSED))
3554                         return 0;
3555                 
3556                 sctp_unhash_endpoint(ep);
3557                 sk->sk_state = SCTP_SS_CLOSED;
3558         }
3559 
3560         /* Return if we are already listening. */
3561         if (sctp_sstate(sk, LISTENING))
3562                 return 0;
3563                 
3564         /*
3565          * If a bind() or sctp_bindx() is not called prior to a listen()
3566          * call that allows new associations to be accepted, the system
3567          * picks an ephemeral port and will choose an address set equivalent
3568          * to binding with a wildcard address.
3569          *
3570          * This is not currently spelled out in the SCTP sockets
3571          * extensions draft, but follows the practice as seen in TCP
3572          * sockets.
3573          */
3574         if (!ep->base.bind_addr.port) {
3575                 if (sctp_autobind(sk))
3576                         return -EAGAIN;
3577         }
3578         sk->sk_state = SCTP_SS_LISTENING;
3579         sctp_hash_endpoint(ep);
3580         return 0;
3581 }
3582 
3583 /*
3584  * 4.1.3 listen() - TCP Style Syntax
3585  *
3586  *   Applications uses listen() to ready the SCTP endpoint for accepting
3587  *   inbound associations.
3588  */
3589 SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
3590 {
3591         struct sctp_opt *sp = sctp_sk(sk);
3592         struct sctp_endpoint *ep = sp->ep;
3593 
3594         /* If backlog is zero, disable listening. */
3595         if (!backlog) {
3596                 if (sctp_sstate(sk, CLOSED))
3597                         return 0;
3598                 
3599                 sctp_unhash_endpoint(ep);
3600                 sk->sk_state = SCTP_SS_CLOSED;
3601         }
3602 
3603         if (sctp_sstate(sk, LISTENING))
3604                 return 0;
3605 
3606         /*
3607          * If a bind() or sctp_bindx() is not called prior to a listen()
3608          * call that allows new associations to be accepted, the system
3609          * picks an ephemeral port and will choose an address set equivalent
3610          * to binding with a wildcard address.
3611          *
3612          * This is not currently spelled out in the SCTP sockets
3613          * extensions draft, but follows the practice as seen in TCP
3614          * sockets.
3615          */
3616         if (!ep->base.bind_addr.port) {
3617                 if (sctp_autobind(sk))
3618                         return -EAGAIN;
3619         }
3620         sk->sk_state = SCTP_SS_LISTENING;
3621         sk->sk_max_ack_backlog = backlog;
3622         sctp_hash_endpoint(ep);
3623         return 0;
3624 }
3625 
3626 /*
3627  *  Move a socket to LISTENING state.
3628  */
3629 int sctp_inet_listen(struct socket *sock, int backlog)
3630 {
3631         struct sock *sk = sock->sk;
3632         struct crypto_tfm *tfm=NULL;
3633         int err = -EINVAL;
3634 
3635         if (unlikely(backlog < 0))
3636                 goto out;
3637 
3638         sctp_lock_sock(sk);
3639 
3640         if (sock->state != SS_UNCONNECTED)
3641                 goto out;
3642 
3643         /* Allocate HMAC for generating cookie. */
3644         if (sctp_hmac_alg) {
3645                 tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
3646                 if (!tfm) {
3647                         err = -ENOSYS;
3648                         goto out;
3649                 }
3650         }
3651 
3652         switch (sock->type) {
3653         case SOCK_SEQPACKET:
3654                 err = sctp_seqpacket_listen(sk, backlog);
3655                 break;
3656         case SOCK_STREAM:
3657                 err = sctp_stream_listen(sk, backlog);
3658                 break;
3659         default:
3660                 break;
3661         };
3662         if (err)
3663                 goto cleanup;
3664 
3665         /* Store away the transform reference. */
3666         sctp_sk(sk)->hmac = tfm;
3667 out:
3668         sctp_release_sock(sk);
3669         return err;
3670 cleanup:
3671         if (tfm)
3672                 sctp_crypto_free_tfm(tfm);
3673         goto out;
3674 }
3675 
3676 /*
3677  * This function is done by modeling the current datagram_poll() and the
3678  * tcp_poll().  Note that, based on these implementations, we don't
3679  * lock the socket in this function, even though it seems that,
3680  * ideally, locking or some other mechanisms can be used to ensure
3681  * the integrity of the counters (sndbuf and wmem_queued) used
3682  * in this place.  We assume that we don't need locks either until proven
3683  * otherwise.
3684  *
3685  * Another thing to note is that we include the Async I/O support
3686  * here, again, by modeling the current TCP/UDP code.  We don't have
3687  * a good way to test with it yet.
3688  */
3689 unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
3690 {
3691         struct sock *sk = sock->sk;
3692         struct sctp_opt *sp = sctp_sk(sk);
3693         unsigned int mask;
3694 
3695         poll_wait(file, sk->sk_sleep, wait);
3696 
3697         /* A TCP-style listening socket becomes readable when the accept queue
3698          * is not empty.
3699          */
3700         if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3701                 return (!list_empty(&sp->ep->asocs)) ?
3702                         (POLLIN | POLLRDNORM) : 0;
3703 
3704         mask = 0;
3705 
3706         /* Is there any exceptional events?  */
3707         if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
3708                 mask |= POLLERR;
3709         if (sk->sk_shutdown == SHUTDOWN_MASK)
3710                 mask |= POLLHUP;
3711 
3712         /* Is it readable?  Reconsider this code with TCP-style support.  */
3713         if (!skb_queue_empty(&sk->sk_receive_queue) ||
3714             (sk->sk_shutdown & RCV_SHUTDOWN))
3715                 mask |= POLLIN | POLLRDNORM;
3716 
3717         /* The association is either gone or not ready.  */
3718         if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
3719                 return mask;
3720 
3721         /* Is it writable?  */
3722         if (sctp_writeable(sk)) {
3723                 mask |= POLLOUT | POLLWRNORM;
3724         } else {
3725                 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
3726                 /*
3727                  * Since the socket is not locked, the buffer
3728                  * might be made available after the writeable check and
3729                  * before the bit is set.  This could cause a lost I/O
3730                  * signal.  tcp_poll() has a race breaker for this race
3731                  * condition.  Based on their implementation, we put
3732                  * in the following code to cover it as well.
3733                  */
3734                 if (sctp_writeable(sk))
3735                         mask |= POLLOUT | POLLWRNORM;
3736         }
3737         return mask;
3738 }
3739 
3740 /********************************************************************
3741  * 2nd Level Abstractions
3742  ********************************************************************/
3743 
3744 static struct sctp_bind_bucket *sctp_bucket_create(
3745         struct sctp_bind_hashbucket *head, unsigned short snum)
3746 {
3747         struct sctp_bind_bucket *pp;
3748 
3749         pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
3750         SCTP_DBG_OBJCNT_INC(bind_bucket);
3751         if (pp) {
3752                 pp->port = snum;
3753                 pp->fastreuse = 0;
3754                 INIT_HLIST_HEAD(&pp->owner);
3755                 if ((pp->next = head->chain) != NULL)
3756                         pp->next->pprev = &pp->next;
3757                 head->chain = pp;
3758                 pp->pprev = &head->chain;
3759         }
3760         return pp;
3761 }
3762 
3763 /* Caller must hold hashbucket lock for this tb with local BH disabled */
3764 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
3765 {
3766         if (hlist_empty(&pp->owner)) {
3767                 if (pp->next)
3768                         pp->next->pprev = pp->pprev;
3769                 *(pp->pprev) = pp->next;
3770                 kmem_cache_free(sctp_bucket_cachep, pp);
3771                 SCTP_DBG_OBJCNT_DEC(bind_bucket);
3772         }
3773 }
3774 
3775 /* Release this socket's reference to a local port.  */
3776 static inline void __sctp_put_port(struct sock *sk)
3777 {
3778         struct sctp_bind_hashbucket *head =
3779                 &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
3780         struct sctp_bind_bucket *pp;
3781 
3782         sctp_spin_lock(&head->lock);
3783         pp = sctp_sk(sk)->bind_hash;
3784         __sk_del_bind_node(sk);
3785         sctp_sk(sk)->bind_hash = NULL;
3786         inet_sk(sk)->num = 0;
3787         sctp_bucket_destroy(pp);
3788         sctp_spin_unlock(&head->lock);
3789 }
3790 
3791 void sctp_put_port(struct sock *sk)
3792 {
3793         sctp_local_bh_disable();
3794         __sctp_put_port(sk);
3795         sctp_local_bh_enable();
3796 }
3797 
3798 /*
3799  * The system picks an ephemeral port and choose an address set equivalent
3800  * to binding with a wildcard address.
3801  * One of those addresses will be the primary address for the association.
3802  * This automatically enables the multihoming capability of SCTP.
3803  */
3804 static int sctp_autobind(struct sock *sk)
3805 {
3806         union sctp_addr autoaddr;
3807         struct sctp_af *af;
3808         unsigned short port;
3809 
3810         /* Initialize a local sockaddr structure to INADDR_ANY. */
3811         af = sctp_sk(sk)->pf->af;
3812 
3813         port = htons(inet_sk(sk)->num);
3814         af->inaddr_any(&autoaddr, port);
3815 
3816         return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
3817 }
3818 
3819 /* Parse out IPPROTO_SCTP CMSG headers.  Perform only minimal validation.
3820  *
3821  * From RFC 2292
3822  * 4.2 The cmsghdr Structure *
3823  *
3824  * When ancillary data is sent or received, any number of ancillary data
3825  * objects can be specified by the msg_control and msg_controllen members of
3826  * the msghdr structure, because each object is preceded by
3827  * a cmsghdr structure defining the object's length (the cmsg_len member).
3828  * Historically Berkeley-derived implementations have passed only one object
3829  * at a time, but this API allows multiple objects to be
3830  * passed in a single call to sendmsg() or recvmsg(). The following example
3831  * shows two ancillary data objects in a control buffer.
3832  *
3833  *   |<--------------------------- msg_controllen -------------------------->|
3834  *   |                                                                       |
3835  *
3836  *   |<----- ancillary data object ----->|<----- ancillary data object ----->|
3837  *
3838  *   |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
3839  *   |                                   |                                   |
3840  *
3841  *   |<---------- cmsg_len ---------->|  |<--------- cmsg_len ----------->|  |
3842  *
3843  *   |<--------- CMSG_LEN() --------->|  |<-------- CMSG_LEN() ---------->|  |
3844  *   |                                |  |                                |  |
3845  *
3846  *   +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
3847  *   |cmsg_|cmsg_|cmsg_|XX|           |XX|cmsg_|cmsg_|cmsg_|XX|           |XX|
3848  *
3849  *   |len  |level|type |XX|cmsg_data[]|XX|len  |level|type |XX|cmsg_data[]|XX|
3850  *
3851  *   +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
3852  *    ^
3853  *    |
3854  *
3855  * msg_control
3856  * points here
3857  */
3858 SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
3859                                   sctp_cmsgs_t *cmsgs)
3860 {
3861         struct cmsghdr *cmsg;
3862 
3863         for (cmsg = CMSG_FIRSTHDR(msg);
3864              cmsg != NULL;
3865              cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
3866                 /* Check for minimum length.  The SCM code has this check.  */
3867                 if (cmsg->cmsg_len < sizeof(struct cmsghdr) ||
3868                     (unsigned long)(((char*)cmsg - (char*)msg->msg_control)
3869                                     + cmsg->cmsg_len) > msg->msg_controllen) {
3870                         return -EINVAL;
3871                 }
3872 
3873                 /* Should we parse this header or ignore?  */
3874                 if (cmsg->cmsg_level != IPPROTO_SCTP)
3875                         continue;
3876 
3877                 /* Strictly check lengths following example in SCM code.  */
3878                 switch (cmsg->cmsg_type) {
3879                 case SCTP_INIT:
3880                         /* SCTP Socket API Extension
3881                          * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
3882                          *
3883                          * This cmsghdr structure provides information for
3884                          * initializing new SCTP associations with sendmsg().
3885                          * The SCTP_INITMSG socket option uses this same data
3886                          * structure.  This structure is not used for
3887                          * recvmsg().
3888                          *
3889                          * cmsg_level    cmsg_type      cmsg_data[]
3890                          * ------------  ------------   ----------------------
3891                          * IPPROTO_SCTP  SCTP_INIT      struct sctp_initmsg
3892                          */
3893                         if (cmsg->cmsg_len !=
3894                             CMSG_LEN(sizeof(struct sctp_initmsg)))
3895                                 return -EINVAL;
3896                         cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
3897                         break;
3898 
3899                 case SCTP_SNDRCV:
3900                         /* SCTP Socket API Extension
3901                          * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
3902                          *
3903                          * This cmsghdr structure specifies SCTP options for
3904                          * sendmsg() and describes SCTP header information
3905                          * about a received message through recvmsg().
3906                          *
3907                          * cmsg_level    cmsg_type      cmsg_data[]
3908                          * ------------  ------------   ----------------------
3909                          * IPPROTO_SCTP  SCTP_SNDRCV    struct sctp_sndrcvinfo
3910                          */
3911                         if (cmsg->cmsg_len !=
3912                             CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
3913                                 return -EINVAL;
3914 
3915                         cmsgs->info =
3916                                 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
3917 
3918                         /* Minimally, validate the sinfo_flags. */
3919                         if (cmsgs->info->sinfo_flags &
3920                             ~(MSG_UNORDERED | MSG_ADDR_OVER |
3921                               MSG_ABORT | MSG_EOF))
3922                                 return -EINVAL;
3923                         break;
3924 
3925                 default:
3926                         return -EINVAL;
3927                 };
3928         }
3929         return 0;
3930 }
3931 
3932 /*
3933  * Wait for a packet..
3934  * Note: This function is the same function as in core/datagram.c
3935  * with a few modifications to make lksctp work.
3936  */
3937 static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
3938 {
3939         int error;
3940         DEFINE_WAIT(wait);
3941 
3942         prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
3943 
3944         /* Socket errors? */
3945         error = sock_error(sk);
3946         if (error)
3947                 goto out;
3948 
3949         if (!skb_queue_empty(&sk->sk_receive_queue))
3950                 goto ready;
3951 
3952         /* Socket shut down?  */
3953         if (sk->sk_shutdown & RCV_SHUTDOWN)
3954                 goto out;
3955 
3956         /* Sequenced packets can come disconnected.  If so we report the
3957          * problem.
3958          */
3959         error = -ENOTCONN;
3960 
3961         /* Is there a good reason to think that we may receive some data?  */
3962         if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
3963                 goto out;
3964 
3965         /* Handle signals.  */
3966         if (signal_pending(current))
3967                 goto interrupted;
3968 
3969         /* Let another process have a go.  Since we are going to sleep
3970          * anyway.  Note: This may cause odd behaviors if the message
3971          * does not fit in the user's buffer, but this seems to be the
3972          * only way to honor MSG_DONTWAIT realistically.
3973          */
3974         sctp_release_sock(sk);
3975         *timeo_p = schedule_timeout(*timeo_p);
3976         sctp_lock_sock(sk);
3977 
3978 ready:
3979         finish_wait(sk->sk_sleep, &wait);
3980         return 0;
3981 
3982 interrupted:
3983         error = sock_intr_errno(*timeo_p);
3984 
3985 out:
3986         finish_wait(sk->sk_sleep, &wait);
3987         *err = error;
3988         return error;
3989 }
3990 
3991 /* Receive a datagram.
3992  * Note: This is pretty much the same routine as in core/datagram.c
3993  * with a few changes to make lksctp work.
3994  */
3995 static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
3996                                               int noblock, int *err)
3997 {
3998         int error;
3999         struct sk_buff *skb;
4000         long timeo;
4001 
4002         /* Caller is allowed not to check sk->sk_err before calling.  */
4003         error = sock_error(sk);
4004         if (error)
4005                 goto no_packet;
4006 
4007         timeo = sock_rcvtimeo(sk, noblock);
4008 
4009         SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
4010                           timeo, MAX_SCHEDULE_TIMEOUT);
4011 
4012         do {
4013                 /* Again only user level code calls this function,
4014                  * so nothing interrupt level
4015                  * will suddenly eat the receive_queue.
4016                  *
4017                  *  Look at current nfs client by the way...
4018                  *  However, this function was corrent in any case. 8)
4019                  */
4020                 if (flags & MSG_PEEK) {
4021                         unsigned long cpu_flags;
4022 
4023                         sctp_spin_lock_irqsave(&sk->sk_receive_queue.lock,
4024                                                cpu_flags);
4025                         skb = skb_peek(&sk->sk_receive_queue);
4026                         if (skb)
4027                                 atomic_inc(&skb->users);
4028                         sctp_spin_unlock_irqrestore(&sk->sk_receive_queue.lock,
4029                                                     cpu_flags);
4030                 } else {
4031                         skb = skb_dequeue(&sk->sk_receive_queue);
4032                 }
4033 
4034                 if (skb)
4035                         return skb;
4036 
4037                 if (sk->sk_shutdown & RCV_SHUTDOWN)
4038                         break;
4039 
4040                 /* User doesn't want to wait.  */
4041                 error = -EAGAIN;
4042                 if (!timeo)
4043                         goto no_packet;
4044         } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
4045 
4046         return NULL;
4047 
4048 no_packet:
4049         *err = error;
4050         return NULL;
4051 }
4052 
4053 /* Verify that this is a valid address. */
4054 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
4055                                    int len)
4056 {
4057         struct sctp_af *af;
4058 
4059         /* Verify basic sockaddr. */
4060         af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
4061         if (!af)
4062                 return -EINVAL;
4063 
4064         /* Is this a valid SCTP address?  */
4065         if (!af->addr_valid(addr, sctp_sk(sk)))
4066                 return -EINVAL;
4067 
4068         if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
4069                 return -EINVAL;
4070 
4071         return 0;
4072 }
4073 
4074 /* Get the sndbuf space available at the time on the association.  */
4075 static inline int sctp_wspace(struct sctp_association *asoc)
4076 {
4077         struct sock *sk = asoc->base.sk;
4078         int amt = 0;
4079 
4080         amt = sk->sk_sndbuf - asoc->sndbuf_used;
4081         if (amt < 0)
4082                 amt = 0;
4083         return amt;
4084 }
4085 
4086 /* Increment the used sndbuf space count of the corresponding association by
4087  * the size of the outgoing data chunk.
4088  * Also, set the skb destructor for sndbuf accounting later.
4089  *
4090  * Since it is always 1-1 between chunk and skb, and also a new skb is always
4091  * allocated for chunk bundling in sctp_packet_transmit(), we can use the
4092  * destructor in the data chunk skb for the purpose of the sndbuf space
4093  * tracking.
4094  */
4095 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
4096 {
4097         struct sctp_association *asoc = chunk->asoc;
4098         struct sock *sk = asoc->base.sk;
4099 
4100         /* The sndbuf space is tracked per association.  */
4101         sctp_association_hold(asoc);
4102 
4103         chunk->skb->destructor = sctp_wfree;
4104         /* Save the chunk pointer in skb for sctp_wfree to use later.  */
4105         *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
4106 
4107         asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk);
4108         sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk);
4109 }
4110 
4111 /* If sndbuf has changed, wake up per association sndbuf waiters.  */
4112 static void __sctp_write_space(struct sctp_association *asoc)
4113 {
4114         struct sock *sk = asoc->base.sk;
4115         struct socket *sock = sk->sk_socket;
4116 
4117         if ((sctp_wspace(asoc) > 0) && sock) {
4118                 if (waitqueue_active(&asoc->wait))
4119                         wake_up_interruptible(&asoc->wait);
4120 
4121                 if (sctp_writeable(sk)) {
4122                         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
4123                                 wake_up_interruptible(sk->sk_sleep);
4124 
4125                         /* Note that we try to include the Async I/O support
4126                          * here by modeling from the current TCP/UDP code.
4127                          * We have not tested with it yet.
4128                          */
4129                         if (sock->fasync_list &&
4130                             !(sk->sk_shutdown & SEND_SHUTDOWN))
4131                                 sock_wake_async(sock, 2, POLL_OUT);
4132                 }
4133         }
4134 }
4135 
4136 /* Do accounting for the sndbuf space.
4137  * Decrement the used sndbuf space of the corresponding association by the
4138  * data size which was just transmitted(freed).
4139  */
4140 static void sctp_wfree(struct sk_buff *skb)
4141 {
4142         struct sctp_association *asoc;
4143         struct sctp_chunk *chunk;
4144         struct sock *sk;
4145 
4146         /* Get the saved chunk pointer.  */
4147         chunk = *((struct sctp_chunk **)(skb->cb));
4148         asoc = chunk->asoc;
4149         sk = asoc->base.sk;
4150         asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk);
4151         sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk);
4152         __sctp_write_space(asoc);
4153 
4154         sctp_association_put(asoc);
4155 }
4156 
4157 /* Helper function to wait for space in the sndbuf.  */
4158 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
4159                                 int msg_len)
4160 {
4161         struct sock *sk = asoc->base.sk;
4162         int err = 0;
4163         long current_timeo = *timeo_p;
4164         DEFINE_WAIT(wait);
4165 
4166         SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%d\n",
4167                           asoc, (long)(*timeo_p), msg_len);
4168 
4169         /* Increment the association's refcnt.  */
4170         sctp_association_hold(asoc);
4171 
4172         /* Wait on the association specific sndbuf space. */
4173         for (;;) {
4174                 prepare_to_wait_exclusive(&asoc->wait, &wait,
4175                                           TASK_INTERRUPTIBLE);
4176                 if (!*timeo_p)
4177                         goto do_nonblock;
4178                 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4179                     asoc->base.dead)
4180                         goto do_error;
4181                 if (signal_pending(current))
4182                         goto do_interrupted;
4183                 if (msg_len <= sctp_wspace(asoc))
4184                         break;
4185 
4186                 /* Let another process have a go.  Since we are going
4187                  * to sleep anyway.
4188                  */
4189                 sctp_release_sock(sk);
4190                 current_timeo = schedule_timeout(current_timeo);
4191                 sctp_lock_sock(sk);
4192 
4193                 *timeo_p = current_timeo;
4194         }
4195 
4196 out:
4197         finish_wait(&asoc->wait, &wait);
4198 
4199         /* Release the association's refcnt.  */
4200         sctp_association_put(asoc);
4201 
4202         return err;
4203 
4204 do_error:
4205         err = -EPIPE;
4206         goto out;
4207 
4208 do_interrupted:
4209         err = sock_intr_errno(*timeo_p);
4210         goto out;
4211 
4212 do_nonblock:
4213         err = -EAGAIN;
4214         goto out;
4215 }
4216 
4217 /* If socket sndbuf has changed, wake up all per association waiters.  */
4218 void sctp_write_space(struct sock *sk)
4219 {
4220         struct sctp_association *asoc;
4221         struct list_head *pos;
4222 
4223         /* Wake up the tasks in each wait queue.  */
4224         list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
4225                 asoc = list_entry(pos, struct sctp_association, asocs);
4226                 __sctp_write_space(asoc);
4227         }
4228 }
4229 
4230 /* Is there any sndbuf space available on the socket?
4231  *
4232  * Note that wmem_queued is the sum of the send buffers on all of the
4233  * associations on the same socket.  For a UDP-style socket with
4234  * multiple associations, it is possible for it to be "unwriteable"
4235  * prematurely.  I assume that this is acceptable because
4236  * a premature "unwriteable" is better than an accidental "writeable" which
4237  * would cause an unwanted block under certain circumstances.  For the 1-1
4238  * UDP-style sockets or TCP-style sockets, this code should work.
4239  *  - Daisy
4240  */
4241 static int sctp_writeable(struct sock *sk)
4242 {
4243         int amt = 0;
4244 
4245         amt = sk->sk_sndbuf - sk->sk_wmem_queued;
4246         if (amt < 0)
4247                 amt = 0;
4248         return amt;
4249 }
4250 
4251 /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
4252  * returns immediately with EINPROGRESS.
4253  */
4254 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
4255 {
4256         struct sock *sk = asoc->base.sk;
4257         int err = 0;
4258         long current_timeo = *timeo_p;
4259         DEFINE_WAIT(wait);
4260 
4261         SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
4262                           (long)(*timeo_p));
4263 
4264         /* Increment the association's refcnt.  */
4265         sctp_association_hold(asoc);
4266 
4267         for (;;) {
4268                 prepare_to_wait_exclusive(&asoc->wait, &wait,
4269                                           TASK_INTERRUPTIBLE);
4270                 if (!*timeo_p)
4271                         goto do_nonblock;
4272                 if (sk->sk_shutdown & RCV_SHUTDOWN)
4273                         break;
4274                 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
4275                     asoc->base.dead)
4276                         goto do_error;
4277                 if (signal_pending(current))
4278                         goto do_interrupted;
4279 
4280                 if (sctp_state(asoc, ESTABLISHED))
4281                         break;
4282 
4283                 /* Let another process have a go.  Since we are going
4284                  * to sleep anyway.
4285                  */
4286                 sctp_release_sock(sk);
4287                 current_timeo = schedule_timeout(current_timeo);
4288                 sctp_lock_sock(sk);
4289 
4290                 *timeo_p = current_timeo;
4291         }
4292 
4293 out:
4294         finish_wait(&asoc->wait, &wait);
4295 
4296         /* Release the association's refcnt.  */
4297         sctp_association_put(asoc);
4298 
4299         return err;
4300 
4301 do_error:
4302         err = -ECONNREFUSED;
4303         goto out;
4304 
4305 do_interrupted:
4306         err = sock_intr_errno(*timeo_p);
4307         goto out;
4308 
4309 do_nonblock:
4310         err = -EINPROGRESS;
4311         goto out;
4312 }
4313 
4314 static int sctp_wait_for_accept(struct sock *sk, long timeo)
4315 {
4316         struct sctp_endpoint *ep;
4317         int err = 0;
4318         DEFINE_WAIT(wait);
4319 
4320         ep = sctp_sk(sk)->ep;
4321 
4322 
4323         for (;;) {
4324                 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
4325                                           TASK_INTERRUPTIBLE);
4326 
4327                 if (list_empty(&ep->asocs)) {
4328                         sctp_release_sock(sk);
4329                         timeo = schedule_timeout(timeo);
4330                         sctp_lock_sock(sk);
4331                 }
4332 
4333                 err = -EINVAL;
4334                 if (!sctp_sstate(sk, LISTENING))
4335                         break;
4336 
4337                 err = 0;
4338                 if (!list_empty(&ep->asocs))
4339                         break;
4340 
4341                 err = sock_intr_errno(timeo);
4342                 if (signal_pending(current))
4343                         break;
4344 
4345                 err = -EAGAIN;
4346                 if (!timeo)
4347                         break;
4348         }
4349 
4350         finish_wait(sk->sk_sleep, &wait);
4351 
4352         return err;
4353 }
4354 
4355 void sctp_wait_for_close(struct sock *sk, long timeout)
4356 {
4357         DEFINE_WAIT(wait);
4358 
4359         do {
4360                 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
4361                 if (list_empty(&sctp_sk(sk)->ep->asocs))
4362                         break;
4363                 sctp_release_sock(sk);
4364                 timeout = schedule_timeout(timeout);
4365                 sctp_lock_sock(sk);
4366         } while (!signal_pending(current) && timeout);
4367 
4368         finish_wait(sk->sk_sleep, &wait);
4369 }
4370 
4371 /* Populate the fields of the newsk from the oldsk and migrate the assoc
4372  * and its messages to the newsk.
4373  */
4374 static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
4375                               struct sctp_association *assoc,
4376                               sctp_socket_type_t type)
4377 {
4378         struct sctp_opt *oldsp = sctp_sk(oldsk);
4379         struct sctp_opt *newsp = sctp_sk(newsk);
4380         struct sctp_bind_bucket *pp; /* hash list port iterator */
4381         struct sctp_endpoint *newep = newsp->ep;
4382         struct sk_buff *skb, *tmp;
4383         struct sctp_ulpevent *event;
4384 
4385         /* Migrate socket buffer sizes and all the socket level options to the
4386          * new socket.
4387          */
4388         newsk->sk_sndbuf = oldsk->sk_sndbuf;
4389         newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
4390         /* Brute force copy old sctp opt. */
4391         memcpy(newsp, oldsp, sizeof(struct sctp_opt));
4392 
4393         /* Restore the ep value that was overwritten with the above structure
4394          * copy.
4395          */
4396         newsp->ep = newep;
4397         newsp->hmac = NULL;
4398 
4399         /* Hook this new socket in to the bind_hash list. */
4400         pp = sctp_sk(oldsk)->bind_hash;
4401         sk_add_bind_node(newsk, &pp->owner);
4402         sctp_sk(newsk)->bind_hash = pp;
4403         inet_sk(newsk)->num = inet_sk(oldsk)->num;
4404 
4405         /* Move any messages in the old socket's receive queue that are for the
4406          * peeled off association to the new socket's receive queue.
4407          */
4408         sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
4409                 event = sctp_skb2event(skb);
4410                 if (event->sndrcvinfo.sinfo_assoc_id == assoc) {
4411                         __skb_unlink(skb, skb->list);
4412                         __skb_queue_tail(&newsk->sk_receive_queue, skb);
4413                 }
4414         }
4415 
4416         /* Clean up any messages pending delivery due to partial
4417          * delivery.   Three cases:
4418          * 1) No partial deliver;  no work.
4419          * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
4420          * 3) Peeling off non-partial delivery; move pd_lobby to recieve_queue.
4421          */
4422         skb_queue_head_init(&newsp->pd_lobby);
4423         sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;;
4424 
4425         if (sctp_sk(oldsk)->pd_mode) {
4426                 struct sk_buff_head *queue;
4427 
4428                 /* Decide which queue to move pd_lobby skbs to. */
4429                 if (assoc->ulpq.pd_mode) {
4430                         queue = &newsp->pd_lobby;
4431                 } else
4432                         queue = &newsk->sk_receive_queue;
4433 
4434                 /* Walk through the pd_lobby, looking for skbs that
4435                  * need moved to the new socket.
4436                  */
4437                 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
4438                         event = sctp_skb2event(skb);
4439                         if (event->sndrcvinfo.sinfo_assoc_id == assoc) {
4440                                 __skb_unlink(skb, skb->list);
4441                                 __skb_queue_tail(queue, skb);
4442                         }
4443                 }
4444 
4445                 /* Clear up any skbs waiting for the partial
4446                  * delivery to finish.
4447                  */
4448                 if (assoc->ulpq.pd_mode)
4449                         sctp_clear_pd(oldsk);
4450 
4451         }
4452 
4453         /* Set the type of socket to indicate that it is peeled off from the
4454          * original UDP-style socket or created with the accept() call on a
4455          * TCP-style socket..
4456          */
4457         newsp->type = type;
4458 
4459         /* Migrate the association to the new socket. */
4460         sctp_assoc_migrate(assoc, newsk);
4461 
4462         /* If the association on the newsk is already closed before accept()
4463          * is called, set RCV_SHUTDOWN flag.
4464          */
4465         if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
4466                 newsk->sk_shutdown |= RCV_SHUTDOWN;
4467 
4468         newsk->sk_state = SCTP_SS_ESTABLISHED;
4469 }
4470 
4471 /* This proto struct describes the ULP interface for SCTP.  */
4472 struct proto sctp_prot = {
4473         .name        =  "SCTP",
4474         .close       =  sctp_close,
4475         .connect     =  sctp_connect,
4476         .disconnect  =  sctp_disconnect,
4477         .accept      =  sctp_accept,
4478         .ioctl       =  sctp_ioctl,
4479         .init        =  sctp_init_sock,
4480         .destroy     =  sctp_destroy_sock,
4481         .shutdown    =  sctp_shutdown,
4482         .setsockopt  =  sctp_setsockopt,
4483         .getsockopt  =  sctp_getsockopt,
4484         .sendmsg     =  sctp_sendmsg,
4485         .recvmsg     =  sctp_recvmsg,
4486         .bind        =  sctp_bind,
4487         .backlog_rcv =  sctp_backlog_rcv,
4488         .hash        =  sctp_hash,
4489         .unhash      =  sctp_unhash,
4490         .get_port    =  sctp_get_port,
4491 };
4492 

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

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp