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