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