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