OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [sctp/] [socket.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.