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