Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 1 | /* SCTP kernel implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 9 | * This file is part of the SCTP kernel implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 18 | * This SCTP implementation is free software; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 24 | * This SCTP implementation is distributed in the hope that it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Jeff Kirsher | 4b2f13a | 2013-12-06 06:28:48 -0800 | [diff] [blame] | 31 | * along with GNU CC; see the file COPYING. If not, see |
| 32 | * <http://www.gnu.org/licenses/>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * |
| 34 | * Please send any bug reports or fixes you make to the |
| 35 | * email address(es): |
Daniel Borkmann | 91705c6 | 2013-07-23 14:51:47 +0200 | [diff] [blame] | 36 | * lksctp developers <linux-sctp@vger.kernel.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | */ |
| 52 | |
Joe Perches | 145ce50 | 2010-08-24 13:21:08 +0000 | [diff] [blame] | 53 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 54 | |
Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 55 | #include <crypto/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/types.h> |
| 57 | #include <linux/kernel.h> |
| 58 | #include <linux/wait.h> |
| 59 | #include <linux/time.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 60 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <linux/ip.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 62 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/fcntl.h> |
| 64 | #include <linux/poll.h> |
| 65 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 66 | #include <linux/slab.h> |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 67 | #include <linux/file.h> |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 68 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Neil Horman | 8465a5f | 2014-04-17 15:26:51 -0400 | [diff] [blame] | 75 | #include <net/busy_poll.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | #include <linux/socket.h> /* for sa_family_t */ |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 78 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #include <net/sock.h> |
| 80 | #include <net/sctp/sctp.h> |
| 81 | #include <net/sctp/sm.h> |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 82 | #include <net/sctp/stream_sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /* Forward declarations for internal helper functions. */ |
| 85 | static int sctp_writeable(struct sock *sk); |
| 86 | static void sctp_wfree(struct sk_buff *skb); |
Xin Long | cea0cc8 | 2017-11-15 16:57:26 +0800 | [diff] [blame] | 87 | static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, |
| 88 | size_t msg_len, struct sock **orig_sk); |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 89 | static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p); |
| 91 | static int sctp_wait_for_accept(struct sock *sk, long timeo); |
| 92 | static void sctp_wait_for_close(struct sock *sk, long timeo); |
Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 93 | static void sctp_destruct_sock(struct sock *sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, |
| 95 | union sctp_addr *addr, int len); |
| 96 | static int sctp_bindx_add(struct sock *, struct sockaddr *, int); |
| 97 | static int sctp_bindx_rem(struct sock *, struct sockaddr *, int); |
| 98 | static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int); |
| 99 | static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int); |
| 100 | static int sctp_send_asconf(struct sctp_association *asoc, |
| 101 | struct sctp_chunk *chunk); |
| 102 | static int sctp_do_bind(struct sock *, union sctp_addr *, int); |
| 103 | static int sctp_autobind(struct sock *sk); |
Xin Long | b7ef261 | 2017-08-11 10:23:50 +0800 | [diff] [blame] | 104 | static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, |
| 105 | struct sctp_association *assoc, |
| 106 | enum sctp_socket_type type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Eric Dumazet | 0604475 | 2017-06-07 13:29:12 -0700 | [diff] [blame] | 108 | static unsigned long sctp_memory_pressure; |
Eric Dumazet | 8d987e5 | 2010-11-09 23:24:26 +0000 | [diff] [blame] | 109 | static atomic_long_t sctp_memory_allocated; |
Eric Dumazet | 1748376 | 2008-11-25 21:16:35 -0800 | [diff] [blame] | 110 | struct percpu_counter sctp_sockets_allocated; |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 111 | |
Pavel Emelyanov | 5c52ba1 | 2008-07-16 20:28:10 -0700 | [diff] [blame] | 112 | static void sctp_enter_memory_pressure(struct sock *sk) |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 113 | { |
| 114 | sctp_memory_pressure = 1; |
| 115 | } |
| 116 | |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | /* Get the sndbuf space available at the time on the association. */ |
| 119 | static inline int sctp_wspace(struct sctp_association *asoc) |
| 120 | { |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 121 | int amt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 123 | if (asoc->ep->sndbuf_policy) |
| 124 | amt = asoc->sndbuf_used; |
| 125 | else |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 126 | amt = sk_wmem_alloc_get(asoc->base.sk); |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 127 | |
| 128 | if (amt >= asoc->base.sk->sk_sndbuf) { |
| 129 | if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK) |
| 130 | amt = 0; |
| 131 | else { |
| 132 | amt = sk_stream_wspace(asoc->base.sk); |
| 133 | if (amt < 0) |
| 134 | amt = 0; |
| 135 | } |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 136 | } else { |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 137 | amt = asoc->base.sk->sk_sndbuf - amt; |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | return amt; |
| 140 | } |
| 141 | |
| 142 | /* Increment the used sndbuf space count of the corresponding association by |
| 143 | * the size of the outgoing data chunk. |
| 144 | * Also, set the skb destructor for sndbuf accounting later. |
| 145 | * |
| 146 | * Since it is always 1-1 between chunk and skb, and also a new skb is always |
| 147 | * allocated for chunk bundling in sctp_packet_transmit(), we can use the |
| 148 | * destructor in the data chunk skb for the purpose of the sndbuf space |
| 149 | * tracking. |
| 150 | */ |
| 151 | static inline void sctp_set_owner_w(struct sctp_chunk *chunk) |
| 152 | { |
| 153 | struct sctp_association *asoc = chunk->asoc; |
| 154 | struct sock *sk = asoc->base.sk; |
| 155 | |
| 156 | /* The sndbuf space is tracked per association. */ |
| 157 | sctp_association_hold(asoc); |
| 158 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 159 | skb_set_owner_w(chunk->skb, sk); |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | chunk->skb->destructor = sctp_wfree; |
| 162 | /* Save the chunk pointer in skb for sctp_wfree to use later. */ |
Daniel Borkmann | f869c91 | 2014-11-20 01:54:48 +0100 | [diff] [blame] | 163 | skb_shinfo(chunk->skb)->destructor_arg = chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 165 | asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) + |
| 166 | sizeof(struct sk_buff) + |
| 167 | sizeof(struct sctp_chunk); |
| 168 | |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 169 | refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 170 | sk->sk_wmem_queued += chunk->skb->truesize; |
| 171 | sk_mem_charge(sk, chunk->skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 174 | static void sctp_clear_owner_w(struct sctp_chunk *chunk) |
| 175 | { |
| 176 | skb_orphan(chunk->skb); |
| 177 | } |
| 178 | |
| 179 | static void sctp_for_each_tx_datachunk(struct sctp_association *asoc, |
| 180 | void (*cb)(struct sctp_chunk *)) |
| 181 | |
| 182 | { |
| 183 | struct sctp_outq *q = &asoc->outqueue; |
| 184 | struct sctp_transport *t; |
| 185 | struct sctp_chunk *chunk; |
| 186 | |
| 187 | list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) |
| 188 | list_for_each_entry(chunk, &t->transmitted, transmitted_list) |
| 189 | cb(chunk); |
| 190 | |
Xin Long | a8dd397 | 2017-11-26 20:56:07 +0800 | [diff] [blame] | 191 | list_for_each_entry(chunk, &q->retransmit, transmitted_list) |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 192 | cb(chunk); |
| 193 | |
Xin Long | a8dd397 | 2017-11-26 20:56:07 +0800 | [diff] [blame] | 194 | list_for_each_entry(chunk, &q->sacked, transmitted_list) |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 195 | cb(chunk); |
| 196 | |
Xin Long | a8dd397 | 2017-11-26 20:56:07 +0800 | [diff] [blame] | 197 | list_for_each_entry(chunk, &q->abandoned, transmitted_list) |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 198 | cb(chunk); |
| 199 | |
| 200 | list_for_each_entry(chunk, &q->out_chunk_list, list) |
| 201 | cb(chunk); |
| 202 | } |
| 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /* Verify that this is a valid address. */ |
| 205 | static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr, |
| 206 | int len) |
| 207 | { |
| 208 | struct sctp_af *af; |
| 209 | |
| 210 | /* Verify basic sockaddr. */ |
| 211 | af = sctp_sockaddr_af(sctp_sk(sk), addr, len); |
| 212 | if (!af) |
| 213 | return -EINVAL; |
| 214 | |
| 215 | /* Is this a valid SCTP address? */ |
Vlad Yasevich | 5636bef | 2006-06-17 22:55:35 -0700 | [diff] [blame] | 216 | if (!af->addr_valid(addr, sctp_sk(sk), NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return -EINVAL; |
| 218 | |
| 219 | if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr))) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* Look up the association by its id. If this is not a UDP-style |
| 226 | * socket, the ID field is always ignored. |
| 227 | */ |
| 228 | struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id) |
| 229 | { |
| 230 | struct sctp_association *asoc = NULL; |
| 231 | |
| 232 | /* If this is not a UDP-style socket, assoc id should be ignored. */ |
| 233 | if (!sctp_style(sk, UDP)) { |
| 234 | /* Return NULL if the socket state is not ESTABLISHED. It |
| 235 | * could be a TCP-style listening socket or a socket which |
| 236 | * hasn't yet called connect() to establish an association. |
| 237 | */ |
Marcelo Ricardo Leitner | e5b13f3 | 2016-07-15 16:38:19 -0300 | [diff] [blame] | 238 | if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return NULL; |
| 240 | |
| 241 | /* Get the first and the only association from the list. */ |
| 242 | if (!list_empty(&sctp_sk(sk)->ep->asocs)) |
| 243 | asoc = list_entry(sctp_sk(sk)->ep->asocs.next, |
| 244 | struct sctp_association, asocs); |
| 245 | return asoc; |
| 246 | } |
| 247 | |
| 248 | /* Otherwise this is a UDP-style socket. */ |
| 249 | if (!id || (id == (sctp_assoc_t)-1)) |
| 250 | return NULL; |
| 251 | |
| 252 | spin_lock_bh(&sctp_assocs_id_lock); |
| 253 | asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); |
| 254 | spin_unlock_bh(&sctp_assocs_id_lock); |
| 255 | |
| 256 | if (!asoc || (asoc->base.sk != sk) || asoc->base.dead) |
| 257 | return NULL; |
| 258 | |
| 259 | return asoc; |
| 260 | } |
| 261 | |
| 262 | /* Look up the transport from an address and an assoc id. If both address and |
| 263 | * id are specified, the associations matching the address and the id should be |
| 264 | * the same. |
| 265 | */ |
| 266 | static struct sctp_transport *sctp_addr_id2transport(struct sock *sk, |
| 267 | struct sockaddr_storage *addr, |
| 268 | sctp_assoc_t id) |
| 269 | { |
| 270 | struct sctp_association *addr_asoc = NULL, *id_asoc = NULL; |
Xin Long | 6f29a13 | 2017-01-24 14:01:53 +0800 | [diff] [blame] | 271 | struct sctp_af *af = sctp_get_af_specific(addr->ss_family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | union sctp_addr *laddr = (union sctp_addr *)addr; |
Xin Long | 6f29a13 | 2017-01-24 14:01:53 +0800 | [diff] [blame] | 273 | struct sctp_transport *transport; |
| 274 | |
Xin Long | 912964e | 2017-02-07 20:56:08 +0800 | [diff] [blame] | 275 | if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len)) |
Xin Long | 6f29a13 | 2017-01-24 14:01:53 +0800 | [diff] [blame] | 276 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep, |
Al Viro | cd4ff03 | 2006-11-20 17:11:33 -0800 | [diff] [blame] | 279 | laddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | &transport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | if (!addr_asoc) |
| 283 | return NULL; |
| 284 | |
| 285 | id_asoc = sctp_id2assoc(sk, id); |
| 286 | if (id_asoc && (id_asoc != addr_asoc)) |
| 287 | return NULL; |
| 288 | |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 289 | sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | (union sctp_addr *)addr); |
| 291 | |
| 292 | return transport; |
| 293 | } |
| 294 | |
| 295 | /* API 3.1.2 bind() - UDP Style Syntax |
| 296 | * The syntax of bind() is, |
| 297 | * |
| 298 | * ret = bind(int sd, struct sockaddr *addr, int addrlen); |
| 299 | * |
| 300 | * sd - the socket descriptor returned by socket(). |
| 301 | * addr - the address structure (struct sockaddr_in or struct |
| 302 | * sockaddr_in6 [RFC 2553]), |
| 303 | * addr_len - the size of the address structure. |
| 304 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 305 | static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
| 307 | int retval = 0; |
| 308 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 309 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 311 | pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk, |
| 312 | addr, addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | /* Disallow binding twice. */ |
| 315 | if (!sctp_sk(sk)->ep->base.bind_addr.port) |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 316 | retval = sctp_do_bind(sk, (union sctp_addr *)addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | addr_len); |
| 318 | else |
| 319 | retval = -EINVAL; |
| 320 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 321 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
| 323 | return retval; |
| 324 | } |
| 325 | |
| 326 | static long sctp_get_port_local(struct sock *, union sctp_addr *); |
| 327 | |
| 328 | /* Verify this is a valid sockaddr. */ |
| 329 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, |
| 330 | union sctp_addr *addr, int len) |
| 331 | { |
| 332 | struct sctp_af *af; |
| 333 | |
| 334 | /* Check minimum size. */ |
| 335 | if (len < sizeof (struct sockaddr)) |
| 336 | return NULL; |
| 337 | |
Vlad Yasevich | 7dab83d | 2008-07-18 23:05:40 -0700 | [diff] [blame] | 338 | /* V4 mapped address are really of AF_INET family */ |
| 339 | if (addr->sa.sa_family == AF_INET6 && |
| 340 | ipv6_addr_v4mapped(&addr->v6.sin6_addr)) { |
| 341 | if (!opt->pf->af_supported(AF_INET, opt)) |
| 342 | return NULL; |
| 343 | } else { |
| 344 | /* Does this PF support this AF? */ |
| 345 | if (!opt->pf->af_supported(addr->sa.sa_family, opt)) |
| 346 | return NULL; |
| 347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | /* If we get this far, af is valid. */ |
| 350 | af = sctp_get_af_specific(addr->sa.sa_family); |
| 351 | |
| 352 | if (len < af->sockaddr_len) |
| 353 | return NULL; |
| 354 | |
| 355 | return af; |
| 356 | } |
| 357 | |
| 358 | /* Bind a local address either to an endpoint or to an association. */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 359 | static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
Eric W. Biederman | 3594698 | 2012-11-16 03:03:12 +0000 | [diff] [blame] | 361 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | struct sctp_sock *sp = sctp_sk(sk); |
| 363 | struct sctp_endpoint *ep = sp->ep; |
| 364 | struct sctp_bind_addr *bp = &ep->base.bind_addr; |
| 365 | struct sctp_af *af; |
| 366 | unsigned short snum; |
| 367 | int ret = 0; |
| 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* Common sockaddr verification. */ |
| 370 | af = sctp_sockaddr_af(sp, addr, len); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 371 | if (!af) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 372 | pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n", |
| 373 | __func__, sk, addr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | return -EINVAL; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | snum = ntohs(addr->v4.sin_port); |
| 378 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 379 | pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n", |
| 380 | __func__, sk, &addr->sa, bp->port, snum, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | /* PF specific bind() address verification. */ |
| 383 | if (!sp->pf->bind_verify(sp, addr)) |
| 384 | return -EADDRNOTAVAIL; |
| 385 | |
Vlad Yasevich | 8b35805 | 2007-05-15 17:14:58 -0400 | [diff] [blame] | 386 | /* We must either be unbound, or bind to the same port. |
| 387 | * It's OK to allow 0 ports if we are already bound. |
| 388 | * We'll just inhert an already bound port in this case |
| 389 | */ |
| 390 | if (bp->port) { |
| 391 | if (!snum) |
| 392 | snum = bp->port; |
| 393 | else if (snum != bp->port) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 394 | pr_debug("%s: new port %d doesn't match existing port " |
| 395 | "%d\n", __func__, snum, bp->port); |
Vlad Yasevich | 8b35805 | 2007-05-15 17:14:58 -0400 | [diff] [blame] | 396 | return -EINVAL; |
| 397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Krister Johansen | 4548b68 | 2017-01-20 17:49:11 -0800 | [diff] [blame] | 400 | if (snum && snum < inet_prot_sock(net) && |
Eric W. Biederman | 3594698 | 2012-11-16 03:03:12 +0000 | [diff] [blame] | 401 | !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return -EACCES; |
| 403 | |
Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 404 | /* See if the address matches any of the addresses we may have |
| 405 | * already bound before checking against other endpoints. |
| 406 | */ |
| 407 | if (sctp_bind_addr_match(bp, addr, sp)) |
| 408 | return -EINVAL; |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* Make sure we are allowed to bind here. |
| 411 | * The function sctp_get_port_local() does duplicate address |
| 412 | * detection. |
| 413 | */ |
Vlad Yasevich | 2772b49 | 2007-08-21 14:24:30 +0900 | [diff] [blame] | 414 | addr->v4.sin_port = htons(snum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | if ((ret = sctp_get_port_local(sk, addr))) { |
Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 416 | return -EADDRINUSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* Refresh ephemeral port. */ |
| 420 | if (!bp->port) |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 421 | bp->port = inet_sk(sk)->inet_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 423 | /* Add the address to the bind address list. |
| 424 | * Use GFP_ATOMIC since BHs will be disabled. |
| 425 | */ |
Marcelo Ricardo Leitner | 133800d | 2016-03-08 10:34:28 -0300 | [diff] [blame] | 426 | ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len, |
| 427 | SCTP_ADDR_SRC, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | /* Copy back into socket for getsockname() use. */ |
| 430 | if (!ret) { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 431 | inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num); |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 432 | sp->pf->to_sk_saddr(addr, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks |
| 439 | * |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 440 | * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | * at any one time. If a sender, after sending an ASCONF chunk, decides |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 442 | * it needs to transfer another ASCONF Chunk, it MUST wait until the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 444 | * subsequent ASCONF. Note this restriction binds each side, so at any |
| 445 | * time two ASCONF may be in-transit on any given association (one sent |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | * from each endpoint). |
| 447 | */ |
| 448 | static int sctp_send_asconf(struct sctp_association *asoc, |
| 449 | struct sctp_chunk *chunk) |
| 450 | { |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 451 | struct net *net = sock_net(asoc->base.sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | int retval = 0; |
| 453 | |
| 454 | /* If there is an outstanding ASCONF chunk, queue it for later |
| 455 | * transmission. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 456 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | if (asoc->addip_last_asconf) { |
David S. Miller | 79af02c | 2005-07-08 21:47:49 -0700 | [diff] [blame] | 458 | list_add_tail(&chunk->list, &asoc->addip_chunk_list); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 459 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* Hold the chunk until an ASCONF_ACK is received. */ |
| 463 | sctp_chunk_hold(chunk); |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 464 | retval = sctp_primitive_ASCONF(net, asoc, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (retval) |
| 466 | sctp_chunk_free(chunk); |
| 467 | else |
| 468 | asoc->addip_last_asconf = chunk; |
| 469 | |
| 470 | out: |
| 471 | return retval; |
| 472 | } |
| 473 | |
| 474 | /* Add a list of addresses as bind addresses to local endpoint or |
| 475 | * association. |
| 476 | * |
| 477 | * Basically run through each address specified in the addrs/addrcnt |
| 478 | * array/length pair, determine if it is IPv6 or IPv4 and call |
| 479 | * sctp_do_bind() on it. |
| 480 | * |
| 481 | * If any of them fails, then the operation will be reversed and the |
| 482 | * ones that were added will be removed. |
| 483 | * |
| 484 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 485 | */ |
sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 486 | static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
| 488 | int cnt; |
| 489 | int retval = 0; |
| 490 | void *addr_buf; |
| 491 | struct sockaddr *sa_addr; |
| 492 | struct sctp_af *af; |
| 493 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 494 | pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk, |
| 495 | addrs, addrcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | addr_buf = addrs; |
| 498 | for (cnt = 0; cnt < addrcnt; cnt++) { |
| 499 | /* The list may contain either IPv4 or IPv6 address; |
| 500 | * determine the address length for walking thru the list. |
| 501 | */ |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 502 | sa_addr = addr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | af = sctp_get_af_specific(sa_addr->sa_family); |
| 504 | if (!af) { |
| 505 | retval = -EINVAL; |
| 506 | goto err_bindx_add; |
| 507 | } |
| 508 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 509 | retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | af->sockaddr_len); |
| 511 | |
| 512 | addr_buf += af->sockaddr_len; |
| 513 | |
| 514 | err_bindx_add: |
| 515 | if (retval < 0) { |
| 516 | /* Failed. Cleanup the ones that have been added */ |
| 517 | if (cnt > 0) |
| 518 | sctp_bindx_rem(sk, addrs, cnt); |
| 519 | return retval; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | return retval; |
| 524 | } |
| 525 | |
| 526 | /* Send an ASCONF chunk with Add IP address parameters to all the peers of the |
| 527 | * associations that are part of the endpoint indicating that a list of local |
| 528 | * addresses are added to the endpoint. |
| 529 | * |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 530 | * If any of the addresses is already in the bind address list of the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | * association, we do not send the chunk for that association. But it will not |
| 532 | * affect other associations. |
| 533 | * |
| 534 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 535 | */ |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 536 | static int sctp_send_asconf_add_ip(struct sock *sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | struct sockaddr *addrs, |
| 538 | int addrcnt) |
| 539 | { |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 540 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | struct sctp_sock *sp; |
| 542 | struct sctp_endpoint *ep; |
| 543 | struct sctp_association *asoc; |
| 544 | struct sctp_bind_addr *bp; |
| 545 | struct sctp_chunk *chunk; |
| 546 | struct sctp_sockaddr_entry *laddr; |
| 547 | union sctp_addr *addr; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 548 | union sctp_addr saveaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | void *addr_buf; |
| 550 | struct sctp_af *af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | struct list_head *p; |
| 552 | int i; |
| 553 | int retval = 0; |
| 554 | |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 555 | if (!net->sctp.addip_enable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | return retval; |
| 557 | |
| 558 | sp = sctp_sk(sk); |
| 559 | ep = sp->ep; |
| 560 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 561 | pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", |
| 562 | __func__, sk, addrs, addrcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 564 | list_for_each_entry(asoc, &ep->asocs, asocs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | if (!asoc->peer.asconf_capable) |
| 566 | continue; |
| 567 | |
| 568 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP) |
| 569 | continue; |
| 570 | |
| 571 | if (!sctp_state(asoc, ESTABLISHED)) |
| 572 | continue; |
| 573 | |
| 574 | /* Check if any address in the packed array of addresses is |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 575 | * in the bind address list of the association. If so, |
| 576 | * do not send the asconf chunk to its peer, but continue with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | * other associations. |
| 578 | */ |
| 579 | addr_buf = addrs; |
| 580 | for (i = 0; i < addrcnt; i++) { |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 581 | addr = addr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | af = sctp_get_af_specific(addr->v4.sin_family); |
| 583 | if (!af) { |
| 584 | retval = -EINVAL; |
| 585 | goto out; |
| 586 | } |
| 587 | |
| 588 | if (sctp_assoc_lookup_laddr(asoc, addr)) |
| 589 | break; |
| 590 | |
| 591 | addr_buf += af->sockaddr_len; |
| 592 | } |
| 593 | if (i < addrcnt) |
| 594 | continue; |
| 595 | |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 596 | /* Use the first valid address in bind addr list of |
| 597 | * association as Address Parameter of ASCONF CHUNK. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | bp = &asoc->base.bind_addr; |
| 600 | p = bp->address_list.next; |
| 601 | laddr = list_entry(p, struct sctp_sockaddr_entry, list); |
Al Viro | 5ae955c | 2006-11-20 17:22:08 -0800 | [diff] [blame] | 602 | chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | addrcnt, SCTP_PARAM_ADD_IP); |
| 604 | if (!chunk) { |
| 605 | retval = -ENOMEM; |
| 606 | goto out; |
| 607 | } |
| 608 | |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 609 | /* Add the new addresses to the bind address list with |
| 610 | * use_as_src set to 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | */ |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 612 | addr_buf = addrs; |
| 613 | for (i = 0; i < addrcnt; i++) { |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 614 | addr = addr_buf; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 615 | af = sctp_get_af_specific(addr->v4.sin_family); |
| 616 | memcpy(&saveaddr, addr, af->sockaddr_len); |
Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 617 | retval = sctp_add_bind_addr(bp, &saveaddr, |
Marcelo Ricardo Leitner | 133800d | 2016-03-08 10:34:28 -0300 | [diff] [blame] | 618 | sizeof(saveaddr), |
Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 619 | SCTP_ADDR_NEW, GFP_ATOMIC); |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 620 | addr_buf += af->sockaddr_len; |
| 621 | } |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 622 | if (asoc->src_out_of_asoc_ok) { |
| 623 | struct sctp_transport *trans; |
| 624 | |
| 625 | list_for_each_entry(trans, |
| 626 | &asoc->peer.transport_addr_list, transports) { |
| 627 | /* Clear the source and route cache */ |
Julian Anastasov | c86a773 | 2017-02-06 23:14:13 +0200 | [diff] [blame] | 628 | sctp_transport_dst_release(trans); |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 629 | trans->cwnd = min(4*asoc->pathmtu, max_t(__u32, |
| 630 | 2*asoc->pathmtu, 4380)); |
| 631 | trans->ssthresh = asoc->peer.i.a_rwnd; |
| 632 | trans->rto = asoc->rto_initial; |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 633 | sctp_max_rto(asoc, trans); |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 634 | trans->rtt = trans->srtt = trans->rttvar = 0; |
| 635 | sctp_transport_route(trans, NULL, |
| 636 | sctp_sk(asoc->base.sk)); |
| 637 | } |
| 638 | } |
| 639 | retval = sctp_send_asconf(asoc, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | out: |
| 643 | return retval; |
| 644 | } |
| 645 | |
| 646 | /* Remove a list of addresses from bind addresses list. Do not remove the |
| 647 | * last address. |
| 648 | * |
| 649 | * Basically run through each address specified in the addrs/addrcnt |
| 650 | * array/length pair, determine if it is IPv6 or IPv4 and call |
| 651 | * sctp_del_bind() on it. |
| 652 | * |
| 653 | * If any of them fails, then the operation will be reversed and the |
| 654 | * ones that were removed will be added back. |
| 655 | * |
| 656 | * At least one address has to be left; if only one address is |
| 657 | * available, the operation will return -EBUSY. |
| 658 | * |
| 659 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 660 | */ |
sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 661 | static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
| 663 | struct sctp_sock *sp = sctp_sk(sk); |
| 664 | struct sctp_endpoint *ep = sp->ep; |
| 665 | int cnt; |
| 666 | struct sctp_bind_addr *bp = &ep->base.bind_addr; |
| 667 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | void *addr_buf; |
Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 669 | union sctp_addr *sa_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | struct sctp_af *af; |
| 671 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 672 | pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", |
| 673 | __func__, sk, addrs, addrcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
| 675 | addr_buf = addrs; |
| 676 | for (cnt = 0; cnt < addrcnt; cnt++) { |
| 677 | /* If the bind address list is empty or if there is only one |
| 678 | * bind address, there is nothing more to be removed (we need |
| 679 | * at least one address here). |
| 680 | */ |
| 681 | if (list_empty(&bp->address_list) || |
| 682 | (sctp_list_single_entry(&bp->address_list))) { |
| 683 | retval = -EBUSY; |
| 684 | goto err_bindx_rem; |
| 685 | } |
| 686 | |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 687 | sa_addr = addr_buf; |
Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 688 | af = sctp_get_af_specific(sa_addr->sa.sa_family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | if (!af) { |
| 690 | retval = -EINVAL; |
| 691 | goto err_bindx_rem; |
| 692 | } |
Paolo Galtieri | 0304ff8a | 2007-04-17 12:52:36 -0700 | [diff] [blame] | 693 | |
| 694 | if (!af->addr_valid(sa_addr, sp, NULL)) { |
| 695 | retval = -EADDRNOTAVAIL; |
| 696 | goto err_bindx_rem; |
| 697 | } |
| 698 | |
Vlad Yasevich | ee9cbac | 2011-04-18 19:14:47 +0000 | [diff] [blame] | 699 | if (sa_addr->v4.sin_port && |
| 700 | sa_addr->v4.sin_port != htons(bp->port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | retval = -EINVAL; |
| 702 | goto err_bindx_rem; |
| 703 | } |
| 704 | |
Vlad Yasevich | ee9cbac | 2011-04-18 19:14:47 +0000 | [diff] [blame] | 705 | if (!sa_addr->v4.sin_port) |
| 706 | sa_addr->v4.sin_port = htons(bp->port); |
| 707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | /* FIXME - There is probably a need to check if sk->sk_saddr and |
| 709 | * sk->sk_rcv_addr are currently set to one of the addresses to |
| 710 | * be removed. This is something which needs to be looked into |
| 711 | * when we are fixing the outstanding issues with multi-homing |
| 712 | * socket routing and failover schemes. Refer to comments in |
| 713 | * sctp_do_bind(). -daisy |
| 714 | */ |
Vlad Yasevich | 0ed90fb | 2007-10-24 16:10:00 -0400 | [diff] [blame] | 715 | retval = sctp_del_bind_addr(bp, sa_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
| 717 | addr_buf += af->sockaddr_len; |
| 718 | err_bindx_rem: |
| 719 | if (retval < 0) { |
| 720 | /* Failed. Add the ones that has been removed back */ |
| 721 | if (cnt > 0) |
| 722 | sctp_bindx_add(sk, addrs, cnt); |
| 723 | return retval; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | return retval; |
| 728 | } |
| 729 | |
| 730 | /* Send an ASCONF chunk with Delete IP address parameters to all the peers of |
| 731 | * the associations that are part of the endpoint indicating that a list of |
| 732 | * local addresses are removed from the endpoint. |
| 733 | * |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 734 | * If any of the addresses is already in the bind address list of the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | * association, we do not send the chunk for that association. But it will not |
| 736 | * affect other associations. |
| 737 | * |
| 738 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 739 | */ |
| 740 | static int sctp_send_asconf_del_ip(struct sock *sk, |
| 741 | struct sockaddr *addrs, |
| 742 | int addrcnt) |
| 743 | { |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 744 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | struct sctp_sock *sp; |
| 746 | struct sctp_endpoint *ep; |
| 747 | struct sctp_association *asoc; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 748 | struct sctp_transport *transport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | struct sctp_bind_addr *bp; |
| 750 | struct sctp_chunk *chunk; |
| 751 | union sctp_addr *laddr; |
| 752 | void *addr_buf; |
| 753 | struct sctp_af *af; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 754 | struct sctp_sockaddr_entry *saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | int i; |
| 756 | int retval = 0; |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 757 | int stored = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 759 | chunk = NULL; |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 760 | if (!net->sctp.addip_enable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | return retval; |
| 762 | |
| 763 | sp = sctp_sk(sk); |
| 764 | ep = sp->ep; |
| 765 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 766 | pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", |
| 767 | __func__, sk, addrs, addrcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 769 | list_for_each_entry(asoc, &ep->asocs, asocs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
| 771 | if (!asoc->peer.asconf_capable) |
| 772 | continue; |
| 773 | |
| 774 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP) |
| 775 | continue; |
| 776 | |
| 777 | if (!sctp_state(asoc, ESTABLISHED)) |
| 778 | continue; |
| 779 | |
| 780 | /* Check if any address in the packed array of addresses is |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 781 | * not present in the bind address list of the association. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | * If so, do not send the asconf chunk to its peer, but |
| 783 | * continue with other associations. |
| 784 | */ |
| 785 | addr_buf = addrs; |
| 786 | for (i = 0; i < addrcnt; i++) { |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 787 | laddr = addr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | af = sctp_get_af_specific(laddr->v4.sin_family); |
| 789 | if (!af) { |
| 790 | retval = -EINVAL; |
| 791 | goto out; |
| 792 | } |
| 793 | |
| 794 | if (!sctp_assoc_lookup_laddr(asoc, laddr)) |
| 795 | break; |
| 796 | |
| 797 | addr_buf += af->sockaddr_len; |
| 798 | } |
| 799 | if (i < addrcnt) |
| 800 | continue; |
| 801 | |
| 802 | /* Find one address in the association's bind address list |
| 803 | * that is not in the packed array of addresses. This is to |
| 804 | * make sure that we do not delete all the addresses in the |
| 805 | * association. |
| 806 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | bp = &asoc->base.bind_addr; |
| 808 | laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs, |
| 809 | addrcnt, sp); |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 810 | if ((laddr == NULL) && (addrcnt == 1)) { |
| 811 | if (asoc->asconf_addr_del_pending) |
| 812 | continue; |
| 813 | asoc->asconf_addr_del_pending = |
| 814 | kzalloc(sizeof(union sctp_addr), GFP_ATOMIC); |
Michio Honda | 6d65e5e | 2011-06-10 16:42:14 +0900 | [diff] [blame] | 815 | if (asoc->asconf_addr_del_pending == NULL) { |
| 816 | retval = -ENOMEM; |
| 817 | goto out; |
| 818 | } |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 819 | asoc->asconf_addr_del_pending->sa.sa_family = |
| 820 | addrs->sa_family; |
| 821 | asoc->asconf_addr_del_pending->v4.sin_port = |
| 822 | htons(bp->port); |
| 823 | if (addrs->sa_family == AF_INET) { |
| 824 | struct sockaddr_in *sin; |
| 825 | |
| 826 | sin = (struct sockaddr_in *)addrs; |
| 827 | asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr; |
| 828 | } else if (addrs->sa_family == AF_INET6) { |
| 829 | struct sockaddr_in6 *sin6; |
| 830 | |
| 831 | sin6 = (struct sockaddr_in6 *)addrs; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 832 | asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr; |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 833 | } |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 834 | |
| 835 | pr_debug("%s: keep the last address asoc:%p %pISc at %p\n", |
| 836 | __func__, asoc, &asoc->asconf_addr_del_pending->sa, |
| 837 | asoc->asconf_addr_del_pending); |
| 838 | |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 839 | asoc->src_out_of_asoc_ok = 1; |
| 840 | stored = 1; |
| 841 | goto skip_mkasconf; |
| 842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
Daniel Borkmann | 88362ad | 2013-09-07 20:51:21 +0200 | [diff] [blame] | 844 | if (laddr == NULL) |
| 845 | return -EINVAL; |
| 846 | |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 847 | /* We do not need RCU protection throughout this loop |
| 848 | * because this is done under a socket lock from the |
| 849 | * setsockopt call. |
| 850 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, |
| 852 | SCTP_PARAM_DEL_IP); |
| 853 | if (!chunk) { |
| 854 | retval = -ENOMEM; |
| 855 | goto out; |
| 856 | } |
| 857 | |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 858 | skip_mkasconf: |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 859 | /* Reset use_as_src flag for the addresses in the bind address |
| 860 | * list that are to be deleted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | */ |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 862 | addr_buf = addrs; |
| 863 | for (i = 0; i < addrcnt; i++) { |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 864 | laddr = addr_buf; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 865 | af = sctp_get_af_specific(laddr->v4.sin_family); |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 866 | list_for_each_entry(saddr, &bp->address_list, list) { |
Al Viro | 5f242a13 | 2006-11-20 17:05:23 -0800 | [diff] [blame] | 867 | if (sctp_cmp_addr_exact(&saddr->a, laddr)) |
Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 868 | saddr->state = SCTP_ADDR_DEL; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 869 | } |
| 870 | addr_buf += af->sockaddr_len; |
| 871 | } |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 872 | |
| 873 | /* Update the route and saddr entries for all the transports |
| 874 | * as some of the addresses in the bind address list are |
| 875 | * about to be deleted and cannot be used as source addresses. |
| 876 | */ |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 877 | list_for_each_entry(transport, &asoc->peer.transport_addr_list, |
| 878 | transports) { |
Julian Anastasov | c86a773 | 2017-02-06 23:14:13 +0200 | [diff] [blame] | 879 | sctp_transport_dst_release(transport); |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 880 | sctp_transport_route(transport, NULL, |
| 881 | sctp_sk(asoc->base.sk)); |
| 882 | } |
| 883 | |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 884 | if (stored) |
| 885 | /* We don't need to transmit ASCONF */ |
| 886 | continue; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 887 | retval = sctp_send_asconf(asoc, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | } |
| 889 | out: |
| 890 | return retval; |
| 891 | } |
| 892 | |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 893 | /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */ |
| 894 | int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw) |
| 895 | { |
| 896 | struct sock *sk = sctp_opt2sk(sp); |
| 897 | union sctp_addr *addr; |
| 898 | struct sctp_af *af; |
| 899 | |
| 900 | /* It is safe to write port space in caller. */ |
| 901 | addr = &addrw->a; |
| 902 | addr->v4.sin_port = htons(sp->ep->base.bind_addr.port); |
| 903 | af = sctp_get_af_specific(addr->sa.sa_family); |
| 904 | if (!af) |
| 905 | return -EINVAL; |
| 906 | if (sctp_verify_addr(sk, addr, af->sockaddr_len)) |
| 907 | return -EINVAL; |
| 908 | |
| 909 | if (addrw->state == SCTP_ADDR_NEW) |
| 910 | return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1); |
| 911 | else |
| 912 | return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1); |
| 913 | } |
| 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt() |
| 916 | * |
| 917 | * API 8.1 |
| 918 | * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, |
| 919 | * int flags); |
| 920 | * |
| 921 | * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. |
| 922 | * If the sd is an IPv6 socket, the addresses passed can either be IPv4 |
| 923 | * or IPv6 addresses. |
| 924 | * |
| 925 | * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see |
| 926 | * Section 3.1.2 for this usage. |
| 927 | * |
| 928 | * addrs is a pointer to an array of one or more socket addresses. Each |
| 929 | * address is contained in its appropriate structure (i.e. struct |
| 930 | * sockaddr_in or struct sockaddr_in6) the family of the address type |
Ville Nuorvala | 23c435f | 2006-10-16 22:08:28 -0700 | [diff] [blame] | 931 | * must be used to distinguish the address length (note that this |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | * representation is termed a "packed array" of addresses). The caller |
| 933 | * specifies the number of addresses in the array with addrcnt. |
| 934 | * |
| 935 | * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns |
| 936 | * -1, and sets errno to the appropriate error code. |
| 937 | * |
| 938 | * For SCTP, the port given in each socket address must be the same, or |
| 939 | * sctp_bindx() will fail, setting errno to EINVAL. |
| 940 | * |
| 941 | * The flags parameter is formed from the bitwise OR of zero or more of |
| 942 | * the following currently defined flags: |
| 943 | * |
| 944 | * SCTP_BINDX_ADD_ADDR |
| 945 | * |
| 946 | * SCTP_BINDX_REM_ADDR |
| 947 | * |
| 948 | * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the |
| 949 | * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given |
| 950 | * addresses from the association. The two flags are mutually exclusive; |
| 951 | * if both are given, sctp_bindx() will fail with EINVAL. A caller may |
| 952 | * not remove all addresses from an association; sctp_bindx() will |
| 953 | * reject such an attempt with EINVAL. |
| 954 | * |
| 955 | * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate |
| 956 | * additional addresses with an endpoint after calling bind(). Or use |
| 957 | * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening |
| 958 | * socket is associated with so that no new association accepted will be |
| 959 | * associated with those addresses. If the endpoint supports dynamic |
| 960 | * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a |
| 961 | * endpoint to send the appropriate message to the peer to change the |
| 962 | * peers address lists. |
| 963 | * |
| 964 | * Adding and removing addresses from a connected association is |
| 965 | * optional functionality. Implementations that do not support this |
| 966 | * functionality should return EOPNOTSUPP. |
| 967 | * |
| 968 | * Basically do nothing but copying the addresses from user to kernel |
| 969 | * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk. |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 970 | * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() |
| 971 | * from userspace. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | * |
| 973 | * We don't use copy_from_user() for optimization: we first do the |
| 974 | * sanity checks (buffer size -fast- and access check-healthy |
| 975 | * pointer); if all of those succeed, then we can alloc the memory |
| 976 | * (expensive operation) needed to copy the data to kernel. Then we do |
| 977 | * the copying without checking the user space area |
| 978 | * (__copy_from_user()). |
| 979 | * |
| 980 | * On exit there is no need to do sockfd_put(), sys_setsockopt() does |
| 981 | * it. |
| 982 | * |
| 983 | * sk The sk of the socket |
| 984 | * addrs The pointer to the addresses in user land |
| 985 | * addrssize Size of the addrs buffer |
| 986 | * op Operation to perform (add or remove, see the flags of |
| 987 | * sctp_bindx) |
| 988 | * |
| 989 | * Returns 0 if ok, <0 errno code on error. |
| 990 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 991 | static int sctp_setsockopt_bindx(struct sock *sk, |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 992 | struct sockaddr __user *addrs, |
| 993 | int addrs_size, int op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | { |
| 995 | struct sockaddr *kaddrs; |
| 996 | int err; |
| 997 | int addrcnt = 0; |
| 998 | int walk_size = 0; |
| 999 | struct sockaddr *sa_addr; |
| 1000 | void *addr_buf; |
| 1001 | struct sctp_af *af; |
| 1002 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1003 | pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n", |
| 1004 | __func__, sk, addrs, addrs_size, op); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
| 1006 | if (unlikely(addrs_size <= 0)) |
| 1007 | return -EINVAL; |
| 1008 | |
| 1009 | /* Check the user passed a healthy pointer. */ |
| 1010 | if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) |
| 1011 | return -EFAULT; |
| 1012 | |
| 1013 | /* Alloc space for the address array in kernel memory. */ |
Marcelo Ricardo Leitner | cacc062 | 2015-11-30 14:32:54 -0200 | [diff] [blame] | 1014 | kaddrs = kmalloc(addrs_size, GFP_USER | __GFP_NOWARN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | if (unlikely(!kaddrs)) |
| 1016 | return -ENOMEM; |
| 1017 | |
| 1018 | if (__copy_from_user(kaddrs, addrs, addrs_size)) { |
| 1019 | kfree(kaddrs); |
| 1020 | return -EFAULT; |
| 1021 | } |
| 1022 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1023 | /* Walk through the addrs buffer and count the number of addresses. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | addr_buf = kaddrs; |
| 1025 | while (walk_size < addrs_size) { |
Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1026 | if (walk_size + sizeof(sa_family_t) > addrs_size) { |
| 1027 | kfree(kaddrs); |
| 1028 | return -EINVAL; |
| 1029 | } |
| 1030 | |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 1031 | sa_addr = addr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | af = sctp_get_af_specific(sa_addr->sa_family); |
| 1033 | |
| 1034 | /* If the address family is not supported or if this address |
| 1035 | * causes the address buffer to overflow return EINVAL. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1036 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | if (!af || (walk_size + af->sockaddr_len) > addrs_size) { |
| 1038 | kfree(kaddrs); |
| 1039 | return -EINVAL; |
| 1040 | } |
| 1041 | addrcnt++; |
| 1042 | addr_buf += af->sockaddr_len; |
| 1043 | walk_size += af->sockaddr_len; |
| 1044 | } |
| 1045 | |
| 1046 | /* Do the work. */ |
| 1047 | switch (op) { |
| 1048 | case SCTP_BINDX_ADD_ADDR: |
| 1049 | err = sctp_bindx_add(sk, kaddrs, addrcnt); |
| 1050 | if (err) |
| 1051 | goto out; |
| 1052 | err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt); |
| 1053 | break; |
| 1054 | |
| 1055 | case SCTP_BINDX_REM_ADDR: |
| 1056 | err = sctp_bindx_rem(sk, kaddrs, addrcnt); |
| 1057 | if (err) |
| 1058 | goto out; |
| 1059 | err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt); |
| 1060 | break; |
| 1061 | |
| 1062 | default: |
| 1063 | err = -EINVAL; |
| 1064 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1065 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
| 1067 | out: |
| 1068 | kfree(kaddrs); |
| 1069 | |
| 1070 | return err; |
| 1071 | } |
| 1072 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1073 | /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) |
| 1074 | * |
| 1075 | * Common routine for handling connect() and sctp_connectx(). |
| 1076 | * Connect will come in with just a single address. |
| 1077 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1078 | static int __sctp_connect(struct sock *sk, |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1079 | struct sockaddr *kaddrs, |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1080 | int addrs_size, |
| 1081 | sctp_assoc_t *assoc_id) |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1082 | { |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1083 | struct net *net = sock_net(sk); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1084 | struct sctp_sock *sp; |
| 1085 | struct sctp_endpoint *ep; |
| 1086 | struct sctp_association *asoc = NULL; |
| 1087 | struct sctp_association *asoc2; |
| 1088 | struct sctp_transport *transport; |
| 1089 | union sctp_addr to; |
Xin Long | 1c66201 | 2017-08-05 19:59:54 +0800 | [diff] [blame] | 1090 | enum sctp_scope scope; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1091 | long timeo; |
| 1092 | int err = 0; |
| 1093 | int addrcnt = 0; |
| 1094 | int walk_size = 0; |
Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1095 | union sctp_addr *sa_addr = NULL; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1096 | void *addr_buf; |
Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame] | 1097 | unsigned short port; |
Vlad Yasevich | f50f95c | 2007-07-03 12:47:40 -0400 | [diff] [blame] | 1098 | unsigned int f_flags = 0; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1099 | |
| 1100 | sp = sctp_sk(sk); |
| 1101 | ep = sp->ep; |
| 1102 | |
| 1103 | /* connect() cannot be done on a socket that is already in ESTABLISHED |
| 1104 | * state - UDP-style peeled off socket or a TCP-style socket that |
| 1105 | * is already connected. |
| 1106 | * It cannot be done even on a TCP-style listening socket. |
| 1107 | */ |
Marcelo Ricardo Leitner | e5b13f3 | 2016-07-15 16:38:19 -0300 | [diff] [blame] | 1108 | if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) || |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1109 | (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) { |
| 1110 | err = -EISCONN; |
| 1111 | goto out_free; |
| 1112 | } |
| 1113 | |
| 1114 | /* Walk through the addrs buffer and count the number of addresses. */ |
| 1115 | addr_buf = kaddrs; |
| 1116 | while (walk_size < addrs_size) { |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 1117 | struct sctp_af *af; |
| 1118 | |
Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1119 | if (walk_size + sizeof(sa_family_t) > addrs_size) { |
| 1120 | err = -EINVAL; |
| 1121 | goto out_free; |
| 1122 | } |
| 1123 | |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 1124 | sa_addr = addr_buf; |
Al Viro | 4bdf4b5 | 2006-11-20 17:10:20 -0800 | [diff] [blame] | 1125 | af = sctp_get_af_specific(sa_addr->sa.sa_family); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1126 | |
| 1127 | /* If the address family is not supported or if this address |
| 1128 | * causes the address buffer to overflow return EINVAL. |
| 1129 | */ |
| 1130 | if (!af || (walk_size + af->sockaddr_len) > addrs_size) { |
| 1131 | err = -EINVAL; |
| 1132 | goto out_free; |
| 1133 | } |
| 1134 | |
Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1135 | port = ntohs(sa_addr->v4.sin_port); |
| 1136 | |
Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1137 | /* Save current address so we can work with it */ |
| 1138 | memcpy(&to, sa_addr, af->sockaddr_len); |
| 1139 | |
| 1140 | err = sctp_verify_addr(sk, &to, af->sockaddr_len); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1141 | if (err) |
| 1142 | goto out_free; |
| 1143 | |
Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame] | 1144 | /* Make sure the destination port is correctly set |
| 1145 | * in all addresses. |
| 1146 | */ |
Wei Yongjun | 524fba6 | 2013-04-03 03:02:28 +0000 | [diff] [blame] | 1147 | if (asoc && asoc->peer.port && asoc->peer.port != port) { |
| 1148 | err = -EINVAL; |
Vlad Yasevich | 16d00fb | 2007-05-04 13:34:09 -0700 | [diff] [blame] | 1149 | goto out_free; |
Wei Yongjun | 524fba6 | 2013-04-03 03:02:28 +0000 | [diff] [blame] | 1150 | } |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1151 | |
| 1152 | /* Check if there already is a matching association on the |
| 1153 | * endpoint (other than the one created here). |
| 1154 | */ |
Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1155 | asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1156 | if (asoc2 && asoc2 != asoc) { |
| 1157 | if (asoc2->state >= SCTP_STATE_ESTABLISHED) |
| 1158 | err = -EISCONN; |
| 1159 | else |
| 1160 | err = -EALREADY; |
| 1161 | goto out_free; |
| 1162 | } |
| 1163 | |
| 1164 | /* If we could not find a matching association on the endpoint, |
| 1165 | * make sure that there is no peeled-off association matching |
| 1166 | * the peer address even on another socket. |
| 1167 | */ |
Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1168 | if (sctp_endpoint_is_peeled_off(ep, &to)) { |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1169 | err = -EADDRNOTAVAIL; |
| 1170 | goto out_free; |
| 1171 | } |
| 1172 | |
| 1173 | if (!asoc) { |
| 1174 | /* If a bind() or sctp_bindx() is not called prior to |
| 1175 | * an sctp_connectx() call, the system picks an |
| 1176 | * ephemeral port and will choose an address set |
| 1177 | * equivalent to binding with a wildcard address. |
| 1178 | */ |
| 1179 | if (!ep->base.bind_addr.port) { |
| 1180 | if (sctp_autobind(sk)) { |
| 1181 | err = -EAGAIN; |
| 1182 | goto out_free; |
| 1183 | } |
Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1184 | } else { |
| 1185 | /* |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1186 | * If an unprivileged user inherits a 1-many |
| 1187 | * style socket with open associations on a |
| 1188 | * privileged port, it MAY be permitted to |
| 1189 | * accept new associations, but it SHOULD NOT |
Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1190 | * be permitted to open new associations. |
| 1191 | */ |
Krister Johansen | 4548b68 | 2017-01-20 17:49:11 -0800 | [diff] [blame] | 1192 | if (ep->base.bind_addr.port < |
| 1193 | inet_prot_sock(net) && |
| 1194 | !ns_capable(net->user_ns, |
| 1195 | CAP_NET_BIND_SERVICE)) { |
Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1196 | err = -EACCES; |
| 1197 | goto out_free; |
| 1198 | } |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1201 | scope = sctp_scope(&to); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1202 | asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); |
| 1203 | if (!asoc) { |
| 1204 | err = -ENOMEM; |
| 1205 | goto out_free; |
| 1206 | } |
Vlad Yasevich | 409b95a | 2009-11-10 08:57:34 +0000 | [diff] [blame] | 1207 | |
| 1208 | err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, |
| 1209 | GFP_KERNEL); |
| 1210 | if (err < 0) { |
| 1211 | goto out_free; |
| 1212 | } |
| 1213 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /* Prime the peer's transport structures. */ |
Vlad Yasevich | e4d1fea | 2007-08-01 10:56:43 -0400 | [diff] [blame] | 1217 | transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1218 | SCTP_UNKNOWN); |
| 1219 | if (!transport) { |
| 1220 | err = -ENOMEM; |
| 1221 | goto out_free; |
| 1222 | } |
| 1223 | |
| 1224 | addrcnt++; |
| 1225 | addr_buf += af->sockaddr_len; |
| 1226 | walk_size += af->sockaddr_len; |
| 1227 | } |
| 1228 | |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1229 | /* In case the user of sctp_connectx() wants an association |
| 1230 | * id back, assign one now. |
| 1231 | */ |
| 1232 | if (assoc_id) { |
| 1233 | err = sctp_assoc_set_id(asoc, GFP_KERNEL); |
| 1234 | if (err < 0) |
| 1235 | goto out_free; |
| 1236 | } |
| 1237 | |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1238 | err = sctp_primitive_ASSOCIATE(net, asoc, NULL); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1239 | if (err < 0) { |
| 1240 | goto out_free; |
| 1241 | } |
| 1242 | |
| 1243 | /* Initialize sk's dport and daddr for getpeername() */ |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1244 | inet_sk(sk)->inet_dport = htons(asoc->peer.port); |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 1245 | sp->pf->to_sk_daddr(sa_addr, sk); |
Sridhar Samudrala | 8de8c87 | 2006-05-19 10:58:12 -0700 | [diff] [blame] | 1246 | sk->sk_err = 0; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1247 | |
Vlad Yasevich | f50f95c | 2007-07-03 12:47:40 -0400 | [diff] [blame] | 1248 | /* in-kernel sockets don't generally have a file allocated to them |
| 1249 | * if all they do is call sock_create_kern(). |
| 1250 | */ |
| 1251 | if (sk->sk_socket->file) |
| 1252 | f_flags = sk->sk_socket->file->f_flags; |
| 1253 | |
| 1254 | timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK); |
| 1255 | |
Marcelo Ricardo Leitner | 7233bc8 | 2016-11-03 17:03:41 -0200 | [diff] [blame] | 1256 | if (assoc_id) |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1257 | *assoc_id = asoc->assoc_id; |
Marcelo Ricardo Leitner | 7233bc8 | 2016-11-03 17:03:41 -0200 | [diff] [blame] | 1258 | err = sctp_wait_for_connect(asoc, &timeo); |
| 1259 | /* Note: the asoc may be freed after the return of |
| 1260 | * sctp_wait_for_connect. |
| 1261 | */ |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1262 | |
| 1263 | /* Don't free association on exit. */ |
| 1264 | asoc = NULL; |
| 1265 | |
| 1266 | out_free: |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1267 | pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n", |
| 1268 | __func__, asoc, kaddrs, err); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1269 | |
Neil Horman | 2eebc1e | 2012-07-16 09:13:51 +0000 | [diff] [blame] | 1270 | if (asoc) { |
| 1271 | /* sctp_primitive_ASSOCIATE may have added this association |
| 1272 | * To the hash table, try to unhash it, just in case, its a noop |
| 1273 | * if it wasn't hashed so we're safe |
| 1274 | */ |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1275 | sctp_association_free(asoc); |
Neil Horman | 2eebc1e | 2012-07-16 09:13:51 +0000 | [diff] [blame] | 1276 | } |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1277 | return err; |
| 1278 | } |
| 1279 | |
| 1280 | /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt() |
| 1281 | * |
| 1282 | * API 8.9 |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1283 | * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt, |
| 1284 | * sctp_assoc_t *asoc); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1285 | * |
| 1286 | * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. |
| 1287 | * If the sd is an IPv6 socket, the addresses passed can either be IPv4 |
| 1288 | * or IPv6 addresses. |
| 1289 | * |
| 1290 | * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see |
| 1291 | * Section 3.1.2 for this usage. |
| 1292 | * |
| 1293 | * addrs is a pointer to an array of one or more socket addresses. Each |
| 1294 | * address is contained in its appropriate structure (i.e. struct |
| 1295 | * sockaddr_in or struct sockaddr_in6) the family of the address type |
| 1296 | * must be used to distengish the address length (note that this |
| 1297 | * representation is termed a "packed array" of addresses). The caller |
| 1298 | * specifies the number of addresses in the array with addrcnt. |
| 1299 | * |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1300 | * On success, sctp_connectx() returns 0. It also sets the assoc_id to |
| 1301 | * the association id of the new association. On failure, sctp_connectx() |
| 1302 | * returns -1, and sets errno to the appropriate error code. The assoc_id |
| 1303 | * is not touched by the kernel. |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1304 | * |
| 1305 | * For SCTP, the port given in each socket address must be the same, or |
| 1306 | * sctp_connectx() will fail, setting errno to EINVAL. |
| 1307 | * |
| 1308 | * An application can use sctp_connectx to initiate an association with |
| 1309 | * an endpoint that is multi-homed. Much like sctp_bindx() this call |
| 1310 | * allows a caller to specify multiple addresses at which a peer can be |
| 1311 | * reached. The way the SCTP stack uses the list of addresses to set up |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1312 | * the association is implementation dependent. This function only |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1313 | * specifies that the stack will try to make use of all the addresses in |
| 1314 | * the list when needed. |
| 1315 | * |
| 1316 | * Note that the list of addresses passed in is only used for setting up |
| 1317 | * the association. It does not necessarily equal the set of addresses |
| 1318 | * the peer uses for the resulting association. If the caller wants to |
| 1319 | * find out the set of peer addresses, it must use sctp_getpaddrs() to |
| 1320 | * retrieve them after the association has been set up. |
| 1321 | * |
| 1322 | * Basically do nothing but copying the addresses from user to kernel |
| 1323 | * land and invoking either sctp_connectx(). This is used for tunneling |
| 1324 | * the sctp_connectx() request through sctp_setsockopt() from userspace. |
| 1325 | * |
| 1326 | * We don't use copy_from_user() for optimization: we first do the |
| 1327 | * sanity checks (buffer size -fast- and access check-healthy |
| 1328 | * pointer); if all of those succeed, then we can alloc the memory |
| 1329 | * (expensive operation) needed to copy the data to kernel. Then we do |
| 1330 | * the copying without checking the user space area |
| 1331 | * (__copy_from_user()). |
| 1332 | * |
| 1333 | * On exit there is no need to do sockfd_put(), sys_setsockopt() does |
| 1334 | * it. |
| 1335 | * |
| 1336 | * sk The sk of the socket |
| 1337 | * addrs The pointer to the addresses in user land |
| 1338 | * addrssize Size of the addrs buffer |
| 1339 | * |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1340 | * Returns >=0 if ok, <0 errno code on error. |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1341 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1342 | static int __sctp_setsockopt_connectx(struct sock *sk, |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1343 | struct sockaddr __user *addrs, |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1344 | int addrs_size, |
| 1345 | sctp_assoc_t *assoc_id) |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1346 | { |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1347 | struct sockaddr *kaddrs; |
Marcelo Ricardo Leitner | 9ba0b96 | 2015-12-23 16:28:40 -0200 | [diff] [blame] | 1348 | gfp_t gfp = GFP_KERNEL; |
| 1349 | int err = 0; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1350 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1351 | pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n", |
| 1352 | __func__, sk, addrs, addrs_size); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1353 | |
| 1354 | if (unlikely(addrs_size <= 0)) |
| 1355 | return -EINVAL; |
| 1356 | |
| 1357 | /* Check the user passed a healthy pointer. */ |
| 1358 | if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) |
| 1359 | return -EFAULT; |
| 1360 | |
| 1361 | /* Alloc space for the address array in kernel memory. */ |
Marcelo Ricardo Leitner | 9ba0b96 | 2015-12-23 16:28:40 -0200 | [diff] [blame] | 1362 | if (sk->sk_socket->file) |
| 1363 | gfp = GFP_USER | __GFP_NOWARN; |
| 1364 | kaddrs = kmalloc(addrs_size, gfp); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1365 | if (unlikely(!kaddrs)) |
| 1366 | return -ENOMEM; |
| 1367 | |
| 1368 | if (__copy_from_user(kaddrs, addrs, addrs_size)) { |
| 1369 | err = -EFAULT; |
| 1370 | } else { |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1371 | err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | kfree(kaddrs); |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1375 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1376 | return err; |
| 1377 | } |
| 1378 | |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1379 | /* |
| 1380 | * This is an older interface. It's kept for backward compatibility |
| 1381 | * to the option that doesn't provide association id. |
| 1382 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1383 | static int sctp_setsockopt_connectx_old(struct sock *sk, |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1384 | struct sockaddr __user *addrs, |
| 1385 | int addrs_size) |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1386 | { |
| 1387 | return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL); |
| 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * New interface for the API. The since the API is done with a socket |
| 1392 | * option, to make it simple we feed back the association id is as a return |
| 1393 | * indication to the call. Error is always negative and association id is |
| 1394 | * always positive. |
| 1395 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1396 | static int sctp_setsockopt_connectx(struct sock *sk, |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1397 | struct sockaddr __user *addrs, |
| 1398 | int addrs_size) |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1399 | { |
| 1400 | sctp_assoc_t assoc_id = 0; |
| 1401 | int err = 0; |
| 1402 | |
| 1403 | err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id); |
| 1404 | |
| 1405 | if (err) |
| 1406 | return err; |
| 1407 | else |
| 1408 | return assoc_id; |
| 1409 | } |
| 1410 | |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1411 | /* |
Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1412 | * New (hopefully final) interface for the API. |
| 1413 | * We use the sctp_getaddrs_old structure so that use-space library |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1414 | * can avoid any unnecessary allocations. The only different part |
Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1415 | * is that we store the actual length of the address buffer into the |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1416 | * addrs_num structure member. That way we can re-use the existing |
Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1417 | * code. |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1418 | */ |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1419 | #ifdef CONFIG_COMPAT |
| 1420 | struct compat_sctp_getaddrs_old { |
| 1421 | sctp_assoc_t assoc_id; |
| 1422 | s32 addr_num; |
| 1423 | compat_uptr_t addrs; /* struct sockaddr * */ |
| 1424 | }; |
| 1425 | #endif |
| 1426 | |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1427 | static int sctp_getsockopt_connectx3(struct sock *sk, int len, |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1428 | char __user *optval, |
| 1429 | int __user *optlen) |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1430 | { |
Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1431 | struct sctp_getaddrs_old param; |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1432 | sctp_assoc_t assoc_id = 0; |
| 1433 | int err = 0; |
| 1434 | |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1435 | #ifdef CONFIG_COMPAT |
Andy Lutomirski | 96c0e0a | 2016-03-22 14:25:07 -0700 | [diff] [blame] | 1436 | if (in_compat_syscall()) { |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1437 | struct compat_sctp_getaddrs_old param32; |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1438 | |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1439 | if (len < sizeof(param32)) |
| 1440 | return -EINVAL; |
| 1441 | if (copy_from_user(¶m32, optval, sizeof(param32))) |
| 1442 | return -EFAULT; |
Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1443 | |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1444 | param.assoc_id = param32.assoc_id; |
| 1445 | param.addr_num = param32.addr_num; |
| 1446 | param.addrs = compat_ptr(param32.addrs); |
| 1447 | } else |
| 1448 | #endif |
| 1449 | { |
| 1450 | if (len < sizeof(param)) |
| 1451 | return -EINVAL; |
| 1452 | if (copy_from_user(¶m, optval, sizeof(param))) |
| 1453 | return -EFAULT; |
| 1454 | } |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1455 | |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1456 | err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *) |
| 1457 | param.addrs, param.addr_num, |
| 1458 | &assoc_id); |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1459 | if (err == 0 || err == -EINPROGRESS) { |
| 1460 | if (copy_to_user(optval, &assoc_id, sizeof(assoc_id))) |
| 1461 | return -EFAULT; |
| 1462 | if (put_user(sizeof(assoc_id), optlen)) |
| 1463 | return -EFAULT; |
| 1464 | } |
| 1465 | |
| 1466 | return err; |
| 1467 | } |
| 1468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | /* API 3.1.4 close() - UDP Style Syntax |
| 1470 | * Applications use close() to perform graceful shutdown (as described in |
| 1471 | * Section 10.1 of [SCTP]) on ALL the associations currently represented |
| 1472 | * by a UDP-style socket. |
| 1473 | * |
| 1474 | * The syntax is |
| 1475 | * |
| 1476 | * ret = close(int sd); |
| 1477 | * |
| 1478 | * sd - the socket descriptor of the associations to be closed. |
| 1479 | * |
| 1480 | * To gracefully shutdown a specific association represented by the |
| 1481 | * UDP-style socket, an application should use the sendmsg() call, |
| 1482 | * passing no user data, but including the appropriate flag in the |
| 1483 | * ancillary data (see Section xxxx). |
| 1484 | * |
| 1485 | * If sd in the close() call is a branched-off socket representing only |
| 1486 | * one association, the shutdown is performed on that association only. |
| 1487 | * |
| 1488 | * 4.1.6 close() - TCP Style Syntax |
| 1489 | * |
| 1490 | * Applications use close() to gracefully close down an association. |
| 1491 | * |
| 1492 | * The syntax is: |
| 1493 | * |
| 1494 | * int close(int sd); |
| 1495 | * |
| 1496 | * sd - the socket descriptor of the association to be closed. |
| 1497 | * |
| 1498 | * After an application calls close() on a socket descriptor, no further |
| 1499 | * socket operations will succeed on that descriptor. |
| 1500 | * |
| 1501 | * API 7.1.4 SO_LINGER |
| 1502 | * |
| 1503 | * An application using the TCP-style socket can use this option to |
| 1504 | * perform the SCTP ABORT primitive. The linger option structure is: |
| 1505 | * |
| 1506 | * struct linger { |
| 1507 | * int l_onoff; // option on/off |
| 1508 | * int l_linger; // linger time |
| 1509 | * }; |
| 1510 | * |
| 1511 | * To enable the option, set l_onoff to 1. If the l_linger value is set |
| 1512 | * to 0, calling close() is the same as the ABORT primitive. If the |
| 1513 | * value is set to a negative value, the setsockopt() call will return |
| 1514 | * an error. If the value is set to a positive value linger_time, the |
| 1515 | * close() can be blocked for at most linger_time ms. If the graceful |
| 1516 | * shutdown phase does not finish during this period, close() will |
| 1517 | * return but the graceful shutdown phase continues in the system. |
| 1518 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1519 | static void sctp_close(struct sock *sk, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | { |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1521 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | struct sctp_endpoint *ep; |
| 1523 | struct sctp_association *asoc; |
| 1524 | struct list_head *pos, *temp; |
Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1525 | unsigned int data_was_unread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1527 | pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
Xin Long | 6dfe4b9 | 2017-06-10 14:56:56 +0800 | [diff] [blame] | 1529 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | sk->sk_shutdown = SHUTDOWN_MASK; |
Vlad Yasevich | bec9640 | 2009-07-30 18:08:28 -0400 | [diff] [blame] | 1531 | sk->sk_state = SCTP_SS_CLOSING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | |
| 1533 | ep = sctp_sk(sk)->ep; |
| 1534 | |
Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1535 | /* Clean up any skbs sitting on the receive queue. */ |
| 1536 | data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue); |
| 1537 | data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby); |
| 1538 | |
Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 1539 | /* Walk all associations on an endpoint. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | list_for_each_safe(pos, temp, &ep->asocs) { |
| 1541 | asoc = list_entry(pos, struct sctp_association, asocs); |
| 1542 | |
| 1543 | if (sctp_style(sk, TCP)) { |
| 1544 | /* A closed association can still be in the list if |
| 1545 | * it belongs to a TCP-style listening socket that is |
| 1546 | * not yet accepted. If so, free it. If not, send an |
| 1547 | * ABORT or SHUTDOWN based on the linger options. |
| 1548 | */ |
| 1549 | if (sctp_state(asoc, CLOSED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | sctp_association_free(asoc); |
Vladislav Yasevich | b89498a | 2006-05-19 14:32:06 -0700 | [diff] [blame] | 1551 | continue; |
| 1552 | } |
| 1553 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | |
Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1555 | if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) || |
| 1556 | !skb_queue_empty(&asoc->ulpq.reasm) || |
| 1557 | (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) { |
Sridhar Samudrala | b9ac867 | 2006-08-28 13:53:01 -0700 | [diff] [blame] | 1558 | struct sctp_chunk *chunk; |
| 1559 | |
| 1560 | chunk = sctp_make_abort_user(asoc, NULL, 0); |
Xin Long | 068d8bd | 2015-12-29 17:49:25 +0800 | [diff] [blame] | 1561 | sctp_primitive_ABORT(net, asoc, chunk); |
Sridhar Samudrala | b9ac867 | 2006-08-28 13:53:01 -0700 | [diff] [blame] | 1562 | } else |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1563 | sctp_primitive_SHUTDOWN(net, asoc, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | /* On a TCP-style socket, block for at most linger_time if set. */ |
| 1567 | if (sctp_style(sk, TCP) && timeout) |
| 1568 | sctp_wait_for_close(sk, timeout); |
| 1569 | |
| 1570 | /* This will run the backlog queue. */ |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 1571 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | |
| 1573 | /* Supposedly, no process has access to the socket, but |
| 1574 | * the net layers still may. |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 1575 | * Also, sctp_destroy_sock() needs to be called with addr_wq_lock |
| 1576 | * held and that should be grabbed before socket lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | */ |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 1578 | spin_lock_bh(&net->sctp.addr_wq_lock); |
Xin Long | 6dfe4b9 | 2017-06-10 14:56:56 +0800 | [diff] [blame] | 1579 | bh_lock_sock_nested(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | |
| 1581 | /* Hold the sock, since sk_common_release() will put sock_put() |
| 1582 | * and we have just a little more cleanup. |
| 1583 | */ |
| 1584 | sock_hold(sk); |
| 1585 | sk_common_release(sk); |
| 1586 | |
wangweidong | 5bc1d1b | 2014-01-21 15:44:12 +0800 | [diff] [blame] | 1587 | bh_unlock_sock(sk); |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 1588 | spin_unlock_bh(&net->sctp.addr_wq_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | |
| 1590 | sock_put(sk); |
| 1591 | |
| 1592 | SCTP_DBG_OBJCNT_DEC(sock); |
| 1593 | } |
| 1594 | |
| 1595 | /* Handle EPIPE error. */ |
| 1596 | static int sctp_error(struct sock *sk, int flags, int err) |
| 1597 | { |
| 1598 | if (err == -EPIPE) |
| 1599 | err = sock_error(sk) ? : -EPIPE; |
| 1600 | if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) |
| 1601 | send_sig(SIGPIPE, current, 0); |
| 1602 | return err; |
| 1603 | } |
| 1604 | |
| 1605 | /* API 3.1.3 sendmsg() - UDP Style Syntax |
| 1606 | * |
| 1607 | * An application uses sendmsg() and recvmsg() calls to transmit data to |
| 1608 | * and receive data from its peer. |
| 1609 | * |
| 1610 | * ssize_t sendmsg(int socket, const struct msghdr *message, |
| 1611 | * int flags); |
| 1612 | * |
| 1613 | * socket - the socket descriptor of the endpoint. |
| 1614 | * message - pointer to the msghdr structure which contains a single |
| 1615 | * user message and possibly some ancillary data. |
| 1616 | * |
| 1617 | * See Section 5 for complete description of the data |
| 1618 | * structures. |
| 1619 | * |
| 1620 | * flags - flags sent or received with the user message, see Section |
| 1621 | * 5 for complete description of the flags. |
| 1622 | * |
| 1623 | * Note: This function could use a rewrite especially when explicit |
| 1624 | * connect support comes in. |
| 1625 | */ |
| 1626 | /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */ |
| 1627 | |
Xin Long | a05437a | 2017-08-11 10:23:48 +0800 | [diff] [blame] | 1628 | static int sctp_msghdr_parse(const struct msghdr *msg, |
| 1629 | struct sctp_cmsgs *cmsgs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1631 | static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | { |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1633 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | struct sctp_sock *sp; |
| 1635 | struct sctp_endpoint *ep; |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 1636 | struct sctp_association *new_asoc = NULL, *asoc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | struct sctp_transport *transport, *chunk_tp; |
| 1638 | struct sctp_chunk *chunk; |
Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1639 | union sctp_addr to; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | struct sockaddr *msg_name = NULL; |
Joe Perches | 517aa0b | 2011-05-12 11:27:20 +0000 | [diff] [blame] | 1641 | struct sctp_sndrcvinfo default_sinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | struct sctp_sndrcvinfo *sinfo; |
| 1643 | struct sctp_initmsg *sinit; |
| 1644 | sctp_assoc_t associd = 0; |
Xin Long | a05437a | 2017-08-11 10:23:48 +0800 | [diff] [blame] | 1645 | struct sctp_cmsgs cmsgs = { NULL }; |
Xin Long | 1c66201 | 2017-08-05 19:59:54 +0800 | [diff] [blame] | 1646 | enum sctp_scope scope; |
Daniel Borkmann | 2061dcd | 2015-01-15 16:34:35 +0100 | [diff] [blame] | 1647 | bool fill_sinfo_ttl = false, wait_connect = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | struct sctp_datamsg *datamsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | int msg_flags = msg->msg_flags; |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 1650 | __u16 sinfo_flags = 0; |
| 1651 | long timeo; |
| 1652 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | err = 0; |
| 1655 | sp = sctp_sk(sk); |
| 1656 | ep = sp->ep; |
| 1657 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1658 | pr_debug("%s: sk:%p, msg:%p, msg_len:%zu ep:%p\n", __func__, sk, |
| 1659 | msg, msg_len, ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | |
| 1661 | /* We cannot send a message over a TCP-style listening socket. */ |
| 1662 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) { |
| 1663 | err = -EPIPE; |
| 1664 | goto out_nounlock; |
| 1665 | } |
| 1666 | |
| 1667 | /* Parse out the SCTP CMSGs. */ |
| 1668 | err = sctp_msghdr_parse(msg, &cmsgs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | if (err) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1670 | pr_debug("%s: msghdr parse err:%x\n", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | goto out_nounlock; |
| 1672 | } |
| 1673 | |
| 1674 | /* Fetch the destination address for this packet. This |
| 1675 | * address only selects the association--it is not necessarily |
| 1676 | * the address we will send to. |
| 1677 | * For a peeled-off socket, msg_name is ignored. |
| 1678 | */ |
| 1679 | if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { |
| 1680 | int msg_namelen = msg->msg_namelen; |
| 1681 | |
| 1682 | err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name, |
| 1683 | msg_namelen); |
| 1684 | if (err) |
| 1685 | return err; |
| 1686 | |
| 1687 | if (msg_namelen > sizeof(to)) |
| 1688 | msg_namelen = sizeof(to); |
| 1689 | memcpy(&to, msg->msg_name, msg_namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | msg_name = msg->msg_name; |
| 1691 | } |
| 1692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | sinit = cmsgs.init; |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 1694 | if (cmsgs.sinfo != NULL) { |
| 1695 | memset(&default_sinfo, 0, sizeof(default_sinfo)); |
| 1696 | default_sinfo.sinfo_stream = cmsgs.sinfo->snd_sid; |
| 1697 | default_sinfo.sinfo_flags = cmsgs.sinfo->snd_flags; |
| 1698 | default_sinfo.sinfo_ppid = cmsgs.sinfo->snd_ppid; |
| 1699 | default_sinfo.sinfo_context = cmsgs.sinfo->snd_context; |
| 1700 | default_sinfo.sinfo_assoc_id = cmsgs.sinfo->snd_assoc_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 1702 | sinfo = &default_sinfo; |
| 1703 | fill_sinfo_ttl = true; |
| 1704 | } else { |
| 1705 | sinfo = cmsgs.srinfo; |
| 1706 | } |
| 1707 | /* Did the user specify SNDINFO/SNDRCVINFO? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | if (sinfo) { |
| 1709 | sinfo_flags = sinfo->sinfo_flags; |
| 1710 | associd = sinfo->sinfo_assoc_id; |
| 1711 | } |
| 1712 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1713 | pr_debug("%s: msg_len:%zu, sinfo_flags:0x%x\n", __func__, |
| 1714 | msg_len, sinfo_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1716 | /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */ |
| 1717 | if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | err = -EINVAL; |
| 1719 | goto out_nounlock; |
| 1720 | } |
| 1721 | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1722 | /* If SCTP_EOF is set, no data can be sent. Disallow sending zero |
| 1723 | * length messages when SCTP_EOF|SCTP_ABORT is not set. |
| 1724 | * If SCTP_ABORT is set, the message length could be non zero with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | * the msg_iov set to the user abort reason. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1726 | */ |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1727 | if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) || |
| 1728 | (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | err = -EINVAL; |
| 1730 | goto out_nounlock; |
| 1731 | } |
| 1732 | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1733 | /* If SCTP_ADDR_OVER is set, there must be an address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | * specified in msg_name. |
| 1735 | */ |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1736 | if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | err = -EINVAL; |
| 1738 | goto out_nounlock; |
| 1739 | } |
| 1740 | |
| 1741 | transport = NULL; |
| 1742 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1743 | pr_debug("%s: about to look up association\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 1745 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | |
| 1747 | /* If a msg_name has been specified, assume this is to be used. */ |
| 1748 | if (msg_name) { |
| 1749 | /* Look for a matching association on the endpoint. */ |
Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1750 | asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport); |
Marcelo Ricardo Leitner | e5b13f3 | 2016-07-15 16:38:19 -0300 | [diff] [blame] | 1751 | |
| 1752 | /* If we could not find a matching association on the |
| 1753 | * endpoint, make sure that it is not a TCP-style |
| 1754 | * socket that already has an association or there is |
| 1755 | * no peeled-off association on another socket. |
| 1756 | */ |
| 1757 | if (!asoc && |
| 1758 | ((sctp_style(sk, TCP) && |
| 1759 | (sctp_sstate(sk, ESTABLISHED) || |
| 1760 | sctp_sstate(sk, CLOSING))) || |
| 1761 | sctp_endpoint_is_peeled_off(ep, &to))) { |
| 1762 | err = -EADDRNOTAVAIL; |
| 1763 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | } |
| 1765 | } else { |
| 1766 | asoc = sctp_id2assoc(sk, associd); |
| 1767 | if (!asoc) { |
| 1768 | err = -EPIPE; |
| 1769 | goto out_unlock; |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | if (asoc) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1774 | pr_debug("%s: just looked up association:%p\n", __func__, asoc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
| 1776 | /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED |
| 1777 | * socket that has an association in CLOSED state. This can |
| 1778 | * happen when an accepted socket has an association that is |
| 1779 | * already CLOSED. |
| 1780 | */ |
| 1781 | if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) { |
| 1782 | err = -EPIPE; |
| 1783 | goto out_unlock; |
| 1784 | } |
| 1785 | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1786 | if (sinfo_flags & SCTP_EOF) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1787 | pr_debug("%s: shutting down association:%p\n", |
| 1788 | __func__, asoc); |
| 1789 | |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1790 | sctp_primitive_SHUTDOWN(net, asoc, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | err = 0; |
| 1792 | goto out_unlock; |
| 1793 | } |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1794 | if (sinfo_flags & SCTP_ABORT) { |
Sridhar Samudrala | c164a9b | 2006-08-22 11:50:39 -0700 | [diff] [blame] | 1795 | |
| 1796 | chunk = sctp_make_abort_user(asoc, msg, msg_len); |
| 1797 | if (!chunk) { |
| 1798 | err = -ENOMEM; |
| 1799 | goto out_unlock; |
| 1800 | } |
| 1801 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1802 | pr_debug("%s: aborting association:%p\n", |
| 1803 | __func__, asoc); |
| 1804 | |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1805 | sctp_primitive_ABORT(net, asoc, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | err = 0; |
| 1807 | goto out_unlock; |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | /* Do we need to create the association? */ |
| 1812 | if (!asoc) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1813 | pr_debug("%s: there is no association yet\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1815 | if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | err = -EINVAL; |
| 1817 | goto out_unlock; |
| 1818 | } |
| 1819 | |
| 1820 | /* Check for invalid stream against the stream counts, |
| 1821 | * either the default or the user specified stream counts. |
| 1822 | */ |
| 1823 | if (sinfo) { |
Dan Carpenter | 0e864b2 | 2014-01-13 16:46:08 +0300 | [diff] [blame] | 1824 | if (!sinit || !sinit->sinit_num_ostreams) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | /* Check against the defaults. */ |
| 1826 | if (sinfo->sinfo_stream >= |
| 1827 | sp->initmsg.sinit_num_ostreams) { |
| 1828 | err = -EINVAL; |
| 1829 | goto out_unlock; |
| 1830 | } |
| 1831 | } else { |
| 1832 | /* Check against the requested. */ |
| 1833 | if (sinfo->sinfo_stream >= |
| 1834 | sinit->sinit_num_ostreams) { |
| 1835 | err = -EINVAL; |
| 1836 | goto out_unlock; |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | /* |
| 1842 | * API 3.1.2 bind() - UDP Style Syntax |
| 1843 | * If a bind() or sctp_bindx() is not called prior to a |
| 1844 | * sendmsg() call that initiates a new association, the |
| 1845 | * system picks an ephemeral port and will choose an address |
| 1846 | * set equivalent to binding with a wildcard address. |
| 1847 | */ |
| 1848 | if (!ep->base.bind_addr.port) { |
| 1849 | if (sctp_autobind(sk)) { |
| 1850 | err = -EAGAIN; |
| 1851 | goto out_unlock; |
| 1852 | } |
Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1853 | } else { |
| 1854 | /* |
| 1855 | * If an unprivileged user inherits a one-to-many |
| 1856 | * style socket with open associations on a privileged |
| 1857 | * port, it MAY be permitted to accept new associations, |
| 1858 | * but it SHOULD NOT be permitted to open new |
| 1859 | * associations. |
| 1860 | */ |
Krister Johansen | 4548b68 | 2017-01-20 17:49:11 -0800 | [diff] [blame] | 1861 | if (ep->base.bind_addr.port < inet_prot_sock(net) && |
Eric W. Biederman | 3594698 | 2012-11-16 03:03:12 +0000 | [diff] [blame] | 1862 | !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) { |
Ivan Skytte Jorgensen | 64a0c1c | 2005-10-28 15:39:02 -0700 | [diff] [blame] | 1863 | err = -EACCES; |
| 1864 | goto out_unlock; |
| 1865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | scope = sctp_scope(&to); |
| 1869 | new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); |
| 1870 | if (!new_asoc) { |
| 1871 | err = -ENOMEM; |
| 1872 | goto out_unlock; |
| 1873 | } |
| 1874 | asoc = new_asoc; |
Vlad Yasevich | 409b95a | 2009-11-10 08:57:34 +0000 | [diff] [blame] | 1875 | err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL); |
| 1876 | if (err < 0) { |
| 1877 | err = -ENOMEM; |
| 1878 | goto out_free; |
| 1879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | |
| 1881 | /* If the SCTP_INIT ancillary data is specified, set all |
| 1882 | * the association init values accordingly. |
| 1883 | */ |
| 1884 | if (sinit) { |
| 1885 | if (sinit->sinit_num_ostreams) { |
| 1886 | asoc->c.sinit_num_ostreams = |
| 1887 | sinit->sinit_num_ostreams; |
| 1888 | } |
| 1889 | if (sinit->sinit_max_instreams) { |
| 1890 | asoc->c.sinit_max_instreams = |
| 1891 | sinit->sinit_max_instreams; |
| 1892 | } |
| 1893 | if (sinit->sinit_max_attempts) { |
| 1894 | asoc->max_init_attempts |
| 1895 | = sinit->sinit_max_attempts; |
| 1896 | } |
| 1897 | if (sinit->sinit_max_init_timeo) { |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1898 | asoc->max_init_timeo = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | msecs_to_jiffies(sinit->sinit_max_init_timeo); |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | /* Prime the peer's transport structures. */ |
Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1904 | transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | if (!transport) { |
| 1906 | err = -ENOMEM; |
| 1907 | goto out_free; |
| 1908 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | /* ASSERT: we have a valid association at this point. */ |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1912 | pr_debug("%s: we have a valid association\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | |
| 1914 | if (!sinfo) { |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 1915 | /* If the user didn't specify SNDINFO/SNDRCVINFO, make up |
| 1916 | * one with some defaults. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | */ |
Joe Perches | 517aa0b | 2011-05-12 11:27:20 +0000 | [diff] [blame] | 1918 | memset(&default_sinfo, 0, sizeof(default_sinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | default_sinfo.sinfo_stream = asoc->default_stream; |
| 1920 | default_sinfo.sinfo_flags = asoc->default_flags; |
| 1921 | default_sinfo.sinfo_ppid = asoc->default_ppid; |
| 1922 | default_sinfo.sinfo_context = asoc->default_context; |
| 1923 | default_sinfo.sinfo_timetolive = asoc->default_timetolive; |
| 1924 | default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc); |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 1925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | sinfo = &default_sinfo; |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 1927 | } else if (fill_sinfo_ttl) { |
| 1928 | /* In case SNDINFO was specified, we still need to fill |
| 1929 | * it with a default ttl from the assoc here. |
| 1930 | */ |
| 1931 | sinfo->sinfo_timetolive = asoc->default_timetolive; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | /* API 7.1.7, the sndbuf size per association bounds the |
| 1935 | * maximum size of data that can be sent in a single send call. |
| 1936 | */ |
| 1937 | if (msg_len > sk->sk_sndbuf) { |
| 1938 | err = -EMSGSIZE; |
| 1939 | goto out_free; |
| 1940 | } |
| 1941 | |
Vlad Yasevich | 8a47949 | 2007-06-07 14:21:05 -0400 | [diff] [blame] | 1942 | if (asoc->pmtu_pending) |
Xin Long | 3ebfdf0 | 2017-04-04 13:39:55 +0800 | [diff] [blame] | 1943 | sctp_assoc_pending_pmtu(asoc); |
Vlad Yasevich | 8a47949 | 2007-06-07 14:21:05 -0400 | [diff] [blame] | 1944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | /* If fragmentation is disabled and the message length exceeds the |
| 1946 | * association fragmentation point, return EMSGSIZE. The I-D |
| 1947 | * does not specify what this error is, but this looks like |
| 1948 | * a great fit. |
| 1949 | */ |
| 1950 | if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) { |
| 1951 | err = -EMSGSIZE; |
| 1952 | goto out_free; |
| 1953 | } |
| 1954 | |
Joe Perches | afd7614 | 2011-05-12 09:19:10 +0000 | [diff] [blame] | 1955 | /* Check for invalid stream. */ |
Xin Long | cee360a | 2017-05-31 16:36:31 +0800 | [diff] [blame] | 1956 | if (sinfo->sinfo_stream >= asoc->stream.outcnt) { |
Joe Perches | afd7614 | 2011-05-12 09:19:10 +0000 | [diff] [blame] | 1957 | err = -EINVAL; |
| 1958 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 1961 | /* Allocate sctp_stream_out_ext if not already done */ |
| 1962 | if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) { |
| 1963 | err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream); |
| 1964 | if (err) |
| 1965 | goto out_free; |
| 1966 | } |
| 1967 | |
Xin Long | 8dbdf1f | 2016-07-09 19:47:45 +0800 | [diff] [blame] | 1968 | if (sctp_wspace(asoc) < msg_len) |
| 1969 | sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc)); |
| 1970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 1972 | if (!sctp_wspace(asoc)) { |
Xin Long | cea0cc8 | 2017-11-15 16:57:26 +0800 | [diff] [blame] | 1973 | /* sk can be changed by peel off when waiting for buf. */ |
| 1974 | err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len, &sk); |
Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 1975 | if (err) { |
| 1976 | if (err == -ESRCH) { |
| 1977 | /* asoc is already dead. */ |
| 1978 | new_asoc = NULL; |
| 1979 | err = -EPIPE; |
| 1980 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | goto out_free; |
Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 1982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | /* If an address is passed with the sendto/sendmsg call, it is used |
| 1986 | * to override the primary destination address in the TCP model, or |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1987 | * when SCTP_ADDR_OVER flag is set in the UDP model. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | */ |
| 1989 | if ((sctp_style(sk, TCP) && msg_name) || |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 1990 | (sinfo_flags & SCTP_ADDR_OVER)) { |
Al Viro | dce116a | 2006-11-20 17:25:15 -0800 | [diff] [blame] | 1991 | chunk_tp = sctp_assoc_lookup_paddr(asoc, &to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | if (!chunk_tp) { |
| 1993 | err = -EINVAL; |
| 1994 | goto out_free; |
| 1995 | } |
| 1996 | } else |
| 1997 | chunk_tp = NULL; |
| 1998 | |
| 1999 | /* Auto-connect, if we aren't connected already. */ |
| 2000 | if (sctp_state(asoc, CLOSED)) { |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 2001 | err = sctp_primitive_ASSOCIATE(net, asoc, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | if (err < 0) |
| 2003 | goto out_free; |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 2004 | |
Xin Long | 668c9be | 2017-12-08 21:04:02 +0800 | [diff] [blame] | 2005 | /* If stream interleave is enabled, wait_connect has to be |
| 2006 | * done earlier than data enqueue, as it needs to make data |
| 2007 | * or idata according to asoc->intl_enable which is set |
| 2008 | * after connection is done. |
| 2009 | */ |
| 2010 | if (sctp_sk(asoc->base.sk)->strm_interleave) { |
| 2011 | timeo = sock_sndtimeo(sk, 0); |
| 2012 | err = sctp_wait_for_connect(asoc, &timeo); |
| 2013 | if (err) |
| 2014 | goto out_unlock; |
| 2015 | } else { |
| 2016 | wait_connect = true; |
| 2017 | } |
| 2018 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 2019 | pr_debug("%s: we associated primitively\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | /* Break the message into multiple chunks of maximum size. */ |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 2023 | datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter); |
Tommi Rantala | 6e51fe7 | 2012-11-22 03:23:16 +0000 | [diff] [blame] | 2024 | if (IS_ERR(datamsg)) { |
| 2025 | err = PTR_ERR(datamsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | goto out_free; |
| 2027 | } |
Xin Long | f9ba350 | 2017-03-27 00:21:15 +0800 | [diff] [blame] | 2028 | asoc->force_delay = !!(msg->msg_flags & MSG_MORE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | |
| 2030 | /* Now send the (possibly) fragmented message. */ |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 2031 | list_for_each_entry(chunk, &datamsg->chunks, frag_list) { |
Xin Long | b61c654 | 2016-09-14 02:04:20 +0800 | [diff] [blame] | 2032 | sctp_chunk_hold(chunk); |
| 2033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | /* Do accounting for the write space. */ |
| 2035 | sctp_set_owner_w(chunk); |
| 2036 | |
| 2037 | chunk->transport = chunk_tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
Vlad Yasevich | 9c5c62b | 2009-08-10 13:51:03 -0400 | [diff] [blame] | 2040 | /* Send it to the lower layers. Note: all chunks |
| 2041 | * must either fail or succeed. The lower layer |
| 2042 | * works that way today. Keep it that way or this |
| 2043 | * breaks. |
| 2044 | */ |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 2045 | err = sctp_primitive_SEND(net, asoc, datamsg); |
Vlad Yasevich | 9c5c62b | 2009-08-10 13:51:03 -0400 | [diff] [blame] | 2046 | /* Did the lower layer accept the chunk? */ |
Xin Long | b61c654 | 2016-09-14 02:04:20 +0800 | [diff] [blame] | 2047 | if (err) { |
| 2048 | sctp_datamsg_free(datamsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | goto out_free; |
Xin Long | b61c654 | 2016-09-14 02:04:20 +0800 | [diff] [blame] | 2050 | } |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 2051 | |
| 2052 | pr_debug("%s: we sent primitively\n", __func__); |
| 2053 | |
Xin Long | b61c654 | 2016-09-14 02:04:20 +0800 | [diff] [blame] | 2054 | sctp_datamsg_put(datamsg); |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 2055 | err = msg_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | |
Daniel Borkmann | 2061dcd | 2015-01-15 16:34:35 +0100 | [diff] [blame] | 2057 | if (unlikely(wait_connect)) { |
| 2058 | timeo = sock_sndtimeo(sk, msg_flags & MSG_DONTWAIT); |
| 2059 | sctp_wait_for_connect(asoc, &timeo); |
| 2060 | } |
| 2061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | /* If we are already past ASSOCIATE, the lower |
| 2063 | * layers are responsible for association cleanup. |
| 2064 | */ |
| 2065 | goto out_unlock; |
| 2066 | |
| 2067 | out_free: |
Xin Long | b5eff71 | 2015-12-30 23:50:49 +0800 | [diff] [blame] | 2068 | if (new_asoc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | sctp_association_free(asoc); |
| 2070 | out_unlock: |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 2071 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | |
| 2073 | out_nounlock: |
| 2074 | return sctp_error(sk, msg_flags, err); |
| 2075 | |
| 2076 | #if 0 |
| 2077 | do_sock_err: |
| 2078 | if (msg_len) |
| 2079 | err = msg_len; |
| 2080 | else |
| 2081 | err = sock_error(sk); |
| 2082 | goto out; |
| 2083 | |
| 2084 | do_interrupted: |
| 2085 | if (msg_len) |
| 2086 | err = msg_len; |
| 2087 | goto out; |
| 2088 | #endif /* 0 */ |
| 2089 | } |
| 2090 | |
| 2091 | /* This is an extended version of skb_pull() that removes the data from the |
| 2092 | * start of a skb even when data is spread across the list of skb's in the |
| 2093 | * frag_list. len specifies the total amount of data that needs to be removed. |
| 2094 | * when 'len' bytes could be removed from the skb, it returns 0. |
| 2095 | * If 'len' exceeds the total skb length, it returns the no. of bytes that |
| 2096 | * could not be removed. |
| 2097 | */ |
| 2098 | static int sctp_skb_pull(struct sk_buff *skb, int len) |
| 2099 | { |
| 2100 | struct sk_buff *list; |
| 2101 | int skb_len = skb_headlen(skb); |
| 2102 | int rlen; |
| 2103 | |
| 2104 | if (len <= skb_len) { |
| 2105 | __skb_pull(skb, len); |
| 2106 | return 0; |
| 2107 | } |
| 2108 | len -= skb_len; |
| 2109 | __skb_pull(skb, skb_len); |
| 2110 | |
David S. Miller | 1b003be | 2009-06-09 00:22:35 -0700 | [diff] [blame] | 2111 | skb_walk_frags(skb, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | rlen = sctp_skb_pull(list, len); |
| 2113 | skb->len -= (len-rlen); |
| 2114 | skb->data_len -= (len-rlen); |
| 2115 | |
| 2116 | if (!rlen) |
| 2117 | return 0; |
| 2118 | |
| 2119 | len = rlen; |
| 2120 | } |
| 2121 | |
| 2122 | return len; |
| 2123 | } |
| 2124 | |
| 2125 | /* API 3.1.3 recvmsg() - UDP Style Syntax |
| 2126 | * |
| 2127 | * ssize_t recvmsg(int socket, struct msghdr *message, |
| 2128 | * int flags); |
| 2129 | * |
| 2130 | * socket - the socket descriptor of the endpoint. |
| 2131 | * message - pointer to the msghdr structure which contains a single |
| 2132 | * user message and possibly some ancillary data. |
| 2133 | * |
| 2134 | * See Section 5 for complete description of the data |
| 2135 | * structures. |
| 2136 | * |
| 2137 | * flags - flags sent or received with the user message, see Section |
| 2138 | * 5 for complete description of the flags. |
| 2139 | */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 2140 | static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 2141 | int noblock, int flags, int *addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | { |
| 2143 | struct sctp_ulpevent *event = NULL; |
| 2144 | struct sctp_sock *sp = sctp_sk(sk); |
Marcelo Ricardo Leitner | 1f45f78 | 2016-07-13 15:08:57 -0300 | [diff] [blame] | 2145 | struct sk_buff *skb, *head_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | int copied; |
| 2147 | int err = 0; |
| 2148 | int skb_len; |
| 2149 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 2150 | pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, " |
| 2151 | "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags, |
| 2152 | addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 2154 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | |
Marcelo Ricardo Leitner | e5b13f3 | 2016-07-15 16:38:19 -0300 | [diff] [blame] | 2156 | if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) && |
Xin Long | e087869 | 2016-07-30 14:14:41 +0800 | [diff] [blame] | 2157 | !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | err = -ENOTCONN; |
| 2159 | goto out; |
| 2160 | } |
| 2161 | |
| 2162 | skb = sctp_skb_recv_datagram(sk, flags, noblock, &err); |
| 2163 | if (!skb) |
| 2164 | goto out; |
| 2165 | |
| 2166 | /* Get the total length of the skb including any skb's in the |
| 2167 | * frag_list. |
| 2168 | */ |
| 2169 | skb_len = skb->len; |
| 2170 | |
| 2171 | copied = skb_len; |
| 2172 | if (copied > len) |
| 2173 | copied = len; |
| 2174 | |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 2175 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | |
| 2177 | event = sctp_skb2event(skb); |
| 2178 | |
| 2179 | if (err) |
| 2180 | goto out_free; |
| 2181 | |
Marcelo Ricardo Leitner | 1f45f78 | 2016-07-13 15:08:57 -0300 | [diff] [blame] | 2182 | if (event->chunk && event->chunk->head_skb) |
| 2183 | head_skb = event->chunk->head_skb; |
| 2184 | else |
| 2185 | head_skb = skb; |
| 2186 | sock_recv_ts_and_drops(msg, sk, head_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | if (sctp_ulpevent_is_notification(event)) { |
| 2188 | msg->msg_flags |= MSG_NOTIFICATION; |
| 2189 | sp->pf->event_msgname(event, msg->msg_name, addr_len); |
| 2190 | } else { |
Marcelo Ricardo Leitner | 1f45f78 | 2016-07-13 15:08:57 -0300 | [diff] [blame] | 2191 | sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | } |
| 2193 | |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 2194 | /* Check if we allow SCTP_NXTINFO. */ |
| 2195 | if (sp->recvnxtinfo) |
| 2196 | sctp_ulpevent_read_nxtinfo(event, msg, sk); |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 2197 | /* Check if we allow SCTP_RCVINFO. */ |
| 2198 | if (sp->recvrcvinfo) |
| 2199 | sctp_ulpevent_read_rcvinfo(event, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | /* Check if we allow SCTP_SNDRCVINFO. */ |
| 2201 | if (sp->subscribe.sctp_data_io_event) |
| 2202 | sctp_ulpevent_read_sndrcvinfo(event, msg); |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 2203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | err = copied; |
| 2205 | |
| 2206 | /* If skb's length exceeds the user's buffer, update the skb and |
| 2207 | * push it back to the receive_queue so that the next call to |
| 2208 | * recvmsg() will return the remaining data. Don't set MSG_EOR. |
| 2209 | */ |
| 2210 | if (skb_len > copied) { |
| 2211 | msg->msg_flags &= ~MSG_EOR; |
| 2212 | if (flags & MSG_PEEK) |
| 2213 | goto out_free; |
| 2214 | sctp_skb_pull(skb, copied); |
| 2215 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 2216 | |
Daniel Borkmann | 362d520 | 2014-04-14 21:45:17 +0200 | [diff] [blame] | 2217 | /* When only partial message is copied to the user, increase |
| 2218 | * rwnd by that amount. If all the data in the skb is read, |
| 2219 | * rwnd is updated when the event is freed. |
| 2220 | */ |
| 2221 | if (!sctp_ulpevent_is_notification(event)) |
| 2222 | sctp_assoc_rwnd_increase(event->asoc, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | goto out; |
| 2224 | } else if ((event->msg_flags & MSG_NOTIFICATION) || |
| 2225 | (event->msg_flags & MSG_EOR)) |
| 2226 | msg->msg_flags |= MSG_EOR; |
| 2227 | else |
| 2228 | msg->msg_flags &= ~MSG_EOR; |
| 2229 | |
| 2230 | out_free: |
| 2231 | if (flags & MSG_PEEK) { |
| 2232 | /* Release the skb reference acquired after peeking the skb in |
| 2233 | * sctp_skb_recv_datagram(). |
| 2234 | */ |
| 2235 | kfree_skb(skb); |
| 2236 | } else { |
| 2237 | /* Free the event which includes releasing the reference to |
| 2238 | * the owner of the skb, freeing the skb and updating the |
| 2239 | * rwnd. |
| 2240 | */ |
| 2241 | sctp_ulpevent_free(event); |
| 2242 | } |
| 2243 | out: |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 2244 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | return err; |
| 2246 | } |
| 2247 | |
| 2248 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) |
| 2249 | * |
| 2250 | * This option is a on/off flag. If enabled no SCTP message |
| 2251 | * fragmentation will be performed. Instead if a message being sent |
| 2252 | * exceeds the current PMTU size, the message will NOT be sent and |
| 2253 | * instead a error will be indicated to the user. |
| 2254 | */ |
| 2255 | static int sctp_setsockopt_disable_fragments(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2256 | char __user *optval, |
| 2257 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | { |
| 2259 | int val; |
| 2260 | |
| 2261 | if (optlen < sizeof(int)) |
| 2262 | return -EINVAL; |
| 2263 | |
| 2264 | if (get_user(val, (int __user *)optval)) |
| 2265 | return -EFAULT; |
| 2266 | |
| 2267 | sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1; |
| 2268 | |
| 2269 | return 0; |
| 2270 | } |
| 2271 | |
| 2272 | static int sctp_setsockopt_events(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2273 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | { |
Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2275 | struct sctp_association *asoc; |
| 2276 | struct sctp_ulpevent *event; |
| 2277 | |
Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 2278 | if (optlen > sizeof(struct sctp_event_subscribe)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | return -EINVAL; |
| 2280 | if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen)) |
| 2281 | return -EFAULT; |
Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2282 | |
Daniel Borkmann | bbbea41 | 2014-07-12 20:30:40 +0200 | [diff] [blame] | 2283 | /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT, |
Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2284 | * if there is no data to be sent or retransmit, the stack will |
| 2285 | * immediately send up this notification. |
| 2286 | */ |
| 2287 | if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT, |
| 2288 | &sctp_sk(sk)->subscribe)) { |
| 2289 | asoc = sctp_id2assoc(sk, 0); |
| 2290 | |
| 2291 | if (asoc && sctp_outq_is_empty(&asoc->outqueue)) { |
| 2292 | event = sctp_ulpevent_make_sender_dry_event(asoc, |
| 2293 | GFP_ATOMIC); |
| 2294 | if (!event) |
| 2295 | return -ENOMEM; |
| 2296 | |
Xin Long | 9162e0e | 2017-12-08 21:04:05 +0800 | [diff] [blame^] | 2297 | asoc->stream.si->enqueue_event(&asoc->ulpq, event); |
Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2298 | } |
| 2299 | } |
| 2300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | return 0; |
| 2302 | } |
| 2303 | |
| 2304 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) |
| 2305 | * |
| 2306 | * This socket option is applicable to the UDP-style socket only. When |
| 2307 | * set it will cause associations that are idle for more than the |
| 2308 | * specified number of seconds to automatically close. An association |
| 2309 | * being idle is defined an association that has NOT sent or received |
| 2310 | * user data. The special value of '0' indicates that no automatic |
| 2311 | * close of any associations should be performed. The option expects an |
| 2312 | * integer defining the number of seconds of idle time before an |
| 2313 | * association is closed. |
| 2314 | */ |
| 2315 | static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2316 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | { |
| 2318 | struct sctp_sock *sp = sctp_sk(sk); |
Neil Horman | 9f70f46 | 2013-12-10 06:48:15 -0500 | [diff] [blame] | 2319 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | |
| 2321 | /* Applicable to UDP-style socket only */ |
| 2322 | if (sctp_style(sk, TCP)) |
| 2323 | return -EOPNOTSUPP; |
| 2324 | if (optlen != sizeof(int)) |
| 2325 | return -EINVAL; |
| 2326 | if (copy_from_user(&sp->autoclose, optval, optlen)) |
| 2327 | return -EFAULT; |
| 2328 | |
Neil Horman | 9f70f46 | 2013-12-10 06:48:15 -0500 | [diff] [blame] | 2329 | if (sp->autoclose > net->sctp.max_autoclose) |
| 2330 | sp->autoclose = net->sctp.max_autoclose; |
| 2331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | return 0; |
| 2333 | } |
| 2334 | |
| 2335 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) |
| 2336 | * |
| 2337 | * Applications can enable or disable heartbeats for any peer address of |
| 2338 | * an association, modify an address's heartbeat interval, force a |
| 2339 | * heartbeat to be sent immediately, and adjust the address's maximum |
| 2340 | * number of retransmissions sent before an address is considered |
| 2341 | * unreachable. The following structure is used to access and modify an |
| 2342 | * address's parameters: |
| 2343 | * |
| 2344 | * struct sctp_paddrparams { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2345 | * sctp_assoc_t spp_assoc_id; |
| 2346 | * struct sockaddr_storage spp_address; |
| 2347 | * uint32_t spp_hbinterval; |
| 2348 | * uint16_t spp_pathmaxrxt; |
| 2349 | * uint32_t spp_pathmtu; |
| 2350 | * uint32_t spp_sackdelay; |
| 2351 | * uint32_t spp_flags; |
| 2352 | * }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | * |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2354 | * spp_assoc_id - (one-to-many style socket) This is filled in the |
| 2355 | * application, and identifies the association for |
| 2356 | * this query. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | * spp_address - This specifies which address is of interest. |
| 2358 | * spp_hbinterval - This contains the value of the heartbeat interval, |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2359 | * in milliseconds. If a value of zero |
| 2360 | * is present in this field then no changes are to |
| 2361 | * be made to this parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | * spp_pathmaxrxt - This contains the maximum number of |
| 2363 | * retransmissions before this address shall be |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2364 | * considered unreachable. If a value of zero |
| 2365 | * is present in this field then no changes are to |
| 2366 | * be made to this parameter. |
| 2367 | * spp_pathmtu - When Path MTU discovery is disabled the value |
| 2368 | * specified here will be the "fixed" path mtu. |
| 2369 | * Note that if the spp_address field is empty |
| 2370 | * then all associations on this address will |
| 2371 | * have this fixed path mtu set upon them. |
| 2372 | * |
| 2373 | * spp_sackdelay - When delayed sack is enabled, this value specifies |
| 2374 | * the number of milliseconds that sacks will be delayed |
| 2375 | * for. This value will apply to all addresses of an |
| 2376 | * association if the spp_address field is empty. Note |
| 2377 | * also, that if delayed sack is enabled and this |
| 2378 | * value is set to 0, no change is made to the last |
| 2379 | * recorded delayed sack timer value. |
| 2380 | * |
| 2381 | * spp_flags - These flags are used to control various features |
| 2382 | * on an association. The flag field may contain |
| 2383 | * zero or more of the following options. |
| 2384 | * |
| 2385 | * SPP_HB_ENABLE - Enable heartbeats on the |
| 2386 | * specified address. Note that if the address |
| 2387 | * field is empty all addresses for the association |
| 2388 | * have heartbeats enabled upon them. |
| 2389 | * |
| 2390 | * SPP_HB_DISABLE - Disable heartbeats on the |
| 2391 | * speicifed address. Note that if the address |
| 2392 | * field is empty all addresses for the association |
| 2393 | * will have their heartbeats disabled. Note also |
| 2394 | * that SPP_HB_ENABLE and SPP_HB_DISABLE are |
| 2395 | * mutually exclusive, only one of these two should |
| 2396 | * be specified. Enabling both fields will have |
| 2397 | * undetermined results. |
| 2398 | * |
| 2399 | * SPP_HB_DEMAND - Request a user initiated heartbeat |
| 2400 | * to be made immediately. |
| 2401 | * |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2402 | * SPP_HB_TIME_IS_ZERO - Specify's that the time for |
| 2403 | * heartbeat delayis to be set to the value of 0 |
| 2404 | * milliseconds. |
| 2405 | * |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2406 | * SPP_PMTUD_ENABLE - This field will enable PMTU |
| 2407 | * discovery upon the specified address. Note that |
| 2408 | * if the address feild is empty then all addresses |
| 2409 | * on the association are effected. |
| 2410 | * |
| 2411 | * SPP_PMTUD_DISABLE - This field will disable PMTU |
| 2412 | * discovery upon the specified address. Note that |
| 2413 | * if the address feild is empty then all addresses |
| 2414 | * on the association are effected. Not also that |
| 2415 | * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually |
| 2416 | * exclusive. Enabling both will have undetermined |
| 2417 | * results. |
| 2418 | * |
| 2419 | * SPP_SACKDELAY_ENABLE - Setting this flag turns |
| 2420 | * on delayed sack. The time specified in spp_sackdelay |
| 2421 | * is used to specify the sack delay for this address. Note |
| 2422 | * that if spp_address is empty then all addresses will |
| 2423 | * enable delayed sack and take on the sack delay |
| 2424 | * value specified in spp_sackdelay. |
| 2425 | * SPP_SACKDELAY_DISABLE - Setting this flag turns |
| 2426 | * off delayed sack. If the spp_address field is blank then |
| 2427 | * delayed sack is disabled for the entire association. Note |
| 2428 | * also that this field is mutually exclusive to |
| 2429 | * SPP_SACKDELAY_ENABLE, setting both will have undefined |
| 2430 | * results. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | */ |
Adrian Bunk | 1616436 | 2006-09-18 00:40:38 -0700 | [diff] [blame] | 2432 | static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params, |
| 2433 | struct sctp_transport *trans, |
| 2434 | struct sctp_association *asoc, |
| 2435 | struct sctp_sock *sp, |
| 2436 | int hb_change, |
| 2437 | int pmtud_change, |
| 2438 | int sackdelay_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | int error; |
| 2441 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2442 | if (params->spp_flags & SPP_HB_DEMAND && trans) { |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 2443 | struct net *net = sock_net(trans->asoc->base.sk); |
| 2444 | |
| 2445 | error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | if (error) |
| 2447 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | } |
| 2449 | |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2450 | /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of |
| 2451 | * this field is ignored. Note also that a value of zero indicates |
| 2452 | * the current setting should be left unchanged. |
| 2453 | */ |
| 2454 | if (params->spp_flags & SPP_HB_ENABLE) { |
| 2455 | |
| 2456 | /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is |
| 2457 | * set. This lets us use 0 value when this flag |
| 2458 | * is set. |
| 2459 | */ |
| 2460 | if (params->spp_flags & SPP_HB_TIME_IS_ZERO) |
| 2461 | params->spp_hbinterval = 0; |
| 2462 | |
| 2463 | if (params->spp_hbinterval || |
| 2464 | (params->spp_flags & SPP_HB_TIME_IS_ZERO)) { |
| 2465 | if (trans) { |
| 2466 | trans->hbinterval = |
| 2467 | msecs_to_jiffies(params->spp_hbinterval); |
| 2468 | } else if (asoc) { |
| 2469 | asoc->hbinterval = |
| 2470 | msecs_to_jiffies(params->spp_hbinterval); |
| 2471 | } else { |
| 2472 | sp->hbinterval = params->spp_hbinterval; |
| 2473 | } |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | if (hb_change) { |
| 2478 | if (trans) { |
| 2479 | trans->param_flags = |
| 2480 | (trans->param_flags & ~SPP_HB) | hb_change; |
| 2481 | } else if (asoc) { |
| 2482 | asoc->param_flags = |
| 2483 | (asoc->param_flags & ~SPP_HB) | hb_change; |
| 2484 | } else { |
| 2485 | sp->param_flags = |
| 2486 | (sp->param_flags & ~SPP_HB) | hb_change; |
| 2487 | } |
| 2488 | } |
| 2489 | |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2490 | /* When Path MTU discovery is disabled the value specified here will |
| 2491 | * be the "fixed" path mtu (i.e. the value of the spp_flags field must |
| 2492 | * include the flag SPP_PMTUD_DISABLE for this field to have any |
| 2493 | * effect). |
| 2494 | */ |
| 2495 | if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2496 | if (trans) { |
| 2497 | trans->pathmtu = params->spp_pathmtu; |
Xin Long | 3ebfdf0 | 2017-04-04 13:39:55 +0800 | [diff] [blame] | 2498 | sctp_assoc_sync_pmtu(asoc); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2499 | } else if (asoc) { |
| 2500 | asoc->pathmtu = params->spp_pathmtu; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2501 | } else { |
| 2502 | sp->pathmtu = params->spp_pathmtu; |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | if (pmtud_change) { |
| 2507 | if (trans) { |
| 2508 | int update = (trans->param_flags & SPP_PMTUD_DISABLE) && |
| 2509 | (params->spp_flags & SPP_PMTUD_ENABLE); |
| 2510 | trans->param_flags = |
| 2511 | (trans->param_flags & ~SPP_PMTUD) | pmtud_change; |
| 2512 | if (update) { |
Vlad Yasevich | 9914ae3 | 2011-04-26 21:51:31 +0000 | [diff] [blame] | 2513 | sctp_transport_pmtu(trans, sctp_opt2sk(sp)); |
Xin Long | 3ebfdf0 | 2017-04-04 13:39:55 +0800 | [diff] [blame] | 2514 | sctp_assoc_sync_pmtu(asoc); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2515 | } |
| 2516 | } else if (asoc) { |
| 2517 | asoc->param_flags = |
| 2518 | (asoc->param_flags & ~SPP_PMTUD) | pmtud_change; |
| 2519 | } else { |
| 2520 | sp->param_flags = |
| 2521 | (sp->param_flags & ~SPP_PMTUD) | pmtud_change; |
| 2522 | } |
| 2523 | } |
| 2524 | |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2525 | /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the |
| 2526 | * value of this field is ignored. Note also that a value of zero |
| 2527 | * indicates the current setting should be left unchanged. |
| 2528 | */ |
| 2529 | if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2530 | if (trans) { |
| 2531 | trans->sackdelay = |
| 2532 | msecs_to_jiffies(params->spp_sackdelay); |
| 2533 | } else if (asoc) { |
| 2534 | asoc->sackdelay = |
| 2535 | msecs_to_jiffies(params->spp_sackdelay); |
| 2536 | } else { |
| 2537 | sp->sackdelay = params->spp_sackdelay; |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | if (sackdelay_change) { |
| 2542 | if (trans) { |
| 2543 | trans->param_flags = |
| 2544 | (trans->param_flags & ~SPP_SACKDELAY) | |
| 2545 | sackdelay_change; |
| 2546 | } else if (asoc) { |
| 2547 | asoc->param_flags = |
| 2548 | (asoc->param_flags & ~SPP_SACKDELAY) | |
| 2549 | sackdelay_change; |
| 2550 | } else { |
| 2551 | sp->param_flags = |
| 2552 | (sp->param_flags & ~SPP_SACKDELAY) | |
| 2553 | sackdelay_change; |
| 2554 | } |
| 2555 | } |
| 2556 | |
Andrei Pelinescu-Onciul | 37051f7 | 2009-11-23 15:53:57 -0500 | [diff] [blame] | 2557 | /* Note that a value of zero indicates the current setting should be |
| 2558 | left unchanged. |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2559 | */ |
Andrei Pelinescu-Onciul | 37051f7 | 2009-11-23 15:53:57 -0500 | [diff] [blame] | 2560 | if (params->spp_pathmaxrxt) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2561 | if (trans) { |
| 2562 | trans->pathmaxrxt = params->spp_pathmaxrxt; |
| 2563 | } else if (asoc) { |
| 2564 | asoc->pathmaxrxt = params->spp_pathmaxrxt; |
| 2565 | } else { |
| 2566 | sp->pathmaxrxt = params->spp_pathmaxrxt; |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | return 0; |
| 2571 | } |
| 2572 | |
| 2573 | static int sctp_setsockopt_peer_addr_params(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2574 | char __user *optval, |
| 2575 | unsigned int optlen) |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2576 | { |
| 2577 | struct sctp_paddrparams params; |
| 2578 | struct sctp_transport *trans = NULL; |
| 2579 | struct sctp_association *asoc = NULL; |
| 2580 | struct sctp_sock *sp = sctp_sk(sk); |
| 2581 | int error; |
| 2582 | int hb_change, pmtud_change, sackdelay_change; |
| 2583 | |
| 2584 | if (optlen != sizeof(struct sctp_paddrparams)) |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 2585 | return -EINVAL; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2586 | |
| 2587 | if (copy_from_user(¶ms, optval, optlen)) |
| 2588 | return -EFAULT; |
| 2589 | |
| 2590 | /* Validate flags and value parameters. */ |
| 2591 | hb_change = params.spp_flags & SPP_HB; |
| 2592 | pmtud_change = params.spp_flags & SPP_PMTUD; |
| 2593 | sackdelay_change = params.spp_flags & SPP_SACKDELAY; |
| 2594 | |
| 2595 | if (hb_change == SPP_HB || |
| 2596 | pmtud_change == SPP_PMTUD || |
| 2597 | sackdelay_change == SPP_SACKDELAY || |
| 2598 | params.spp_sackdelay > 500 || |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 2599 | (params.spp_pathmtu && |
| 2600 | params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT)) |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2601 | return -EINVAL; |
| 2602 | |
| 2603 | /* If an address other than INADDR_ANY is specified, and |
| 2604 | * no transport is found, then the request is invalid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | */ |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 2606 | if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2607 | trans = sctp_addr_id2transport(sk, ¶ms.spp_address, |
| 2608 | params.spp_assoc_id); |
| 2609 | if (!trans) |
| 2610 | return -EINVAL; |
| 2611 | } |
| 2612 | |
| 2613 | /* Get association, if assoc_id != 0 and the socket is a one |
| 2614 | * to many style socket, and an association was not found, then |
| 2615 | * the id was invalid. |
| 2616 | */ |
| 2617 | asoc = sctp_id2assoc(sk, params.spp_assoc_id); |
| 2618 | if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) |
| 2619 | return -EINVAL; |
| 2620 | |
| 2621 | /* Heartbeat demand can only be sent on a transport or |
| 2622 | * association, but not a socket. |
| 2623 | */ |
| 2624 | if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc) |
| 2625 | return -EINVAL; |
| 2626 | |
| 2627 | /* Process parameters. */ |
| 2628 | error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, |
| 2629 | hb_change, pmtud_change, |
| 2630 | sackdelay_change); |
| 2631 | |
| 2632 | if (error) |
| 2633 | return error; |
| 2634 | |
| 2635 | /* If changes are for association, also apply parameters to each |
| 2636 | * transport. |
| 2637 | */ |
| 2638 | if (!trans && asoc) { |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 2639 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, |
| 2640 | transports) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2641 | sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, |
| 2642 | hb_change, pmtud_change, |
| 2643 | sackdelay_change); |
| 2644 | } |
| 2645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | |
| 2647 | return 0; |
| 2648 | } |
| 2649 | |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2650 | static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags) |
| 2651 | { |
| 2652 | return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE; |
| 2653 | } |
| 2654 | |
| 2655 | static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags) |
| 2656 | { |
| 2657 | return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE; |
| 2658 | } |
| 2659 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2660 | /* |
| 2661 | * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2662 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2663 | * This option will effect the way delayed acks are performed. This |
| 2664 | * option allows you to get or set the delayed ack time, in |
| 2665 | * milliseconds. It also allows changing the delayed ack frequency. |
| 2666 | * Changing the frequency to 1 disables the delayed sack algorithm. If |
| 2667 | * the assoc_id is 0, then this sets or gets the endpoints default |
| 2668 | * values. If the assoc_id field is non-zero, then the set or get |
| 2669 | * effects the specified association for the one to many model (the |
| 2670 | * assoc_id field is ignored by the one to one model). Note that if |
| 2671 | * sack_delay or sack_freq are 0 when setting this option, then the |
| 2672 | * current values will remain unchanged. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2673 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2674 | * struct sctp_sack_info { |
| 2675 | * sctp_assoc_t sack_assoc_id; |
| 2676 | * uint32_t sack_delay; |
| 2677 | * uint32_t sack_freq; |
| 2678 | * }; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2679 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2680 | * sack_assoc_id - This parameter, indicates which association the user |
| 2681 | * is performing an action upon. Note that if this field's value is |
| 2682 | * zero then the endpoints default value is changed (effecting future |
| 2683 | * associations only). |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2684 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2685 | * sack_delay - This parameter contains the number of milliseconds that |
| 2686 | * the user is requesting the delayed ACK timer be set to. Note that |
| 2687 | * this value is defined in the standard to be between 200 and 500 |
| 2688 | * milliseconds. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2689 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2690 | * sack_freq - This parameter contains the number of packets that must |
| 2691 | * be received before a sack is sent without waiting for the delay |
| 2692 | * timer to expire. The default value for this is 2, setting this |
| 2693 | * value to 1 will disable the delayed sack algorithm. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2694 | */ |
| 2695 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2696 | static int sctp_setsockopt_delayed_ack(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2697 | char __user *optval, unsigned int optlen) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2698 | { |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2699 | struct sctp_sack_info params; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2700 | struct sctp_transport *trans = NULL; |
| 2701 | struct sctp_association *asoc = NULL; |
| 2702 | struct sctp_sock *sp = sctp_sk(sk); |
| 2703 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2704 | if (optlen == sizeof(struct sctp_sack_info)) { |
| 2705 | if (copy_from_user(¶ms, optval, optlen)) |
| 2706 | return -EFAULT; |
| 2707 | |
| 2708 | if (params.sack_delay == 0 && params.sack_freq == 0) |
| 2709 | return 0; |
| 2710 | } else if (optlen == sizeof(struct sctp_assoc_value)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 2711 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 2712 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 2713 | "Use of struct sctp_assoc_value in delayed_ack socket option.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 2714 | "Use struct sctp_sack_info instead\n", |
| 2715 | current->comm, task_pid_nr(current)); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2716 | if (copy_from_user(¶ms, optval, optlen)) |
| 2717 | return -EFAULT; |
| 2718 | |
| 2719 | if (params.sack_delay == 0) |
| 2720 | params.sack_freq = 1; |
| 2721 | else |
| 2722 | params.sack_freq = 0; |
| 2723 | } else |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 2724 | return -EINVAL; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2725 | |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2726 | /* Validate value parameter. */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2727 | if (params.sack_delay > 500) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2728 | return -EINVAL; |
| 2729 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2730 | /* Get association, if sack_assoc_id != 0 and the socket is a one |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2731 | * to many style socket, and an association was not found, then |
| 2732 | * the id was invalid. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2733 | */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2734 | asoc = sctp_id2assoc(sk, params.sack_assoc_id); |
| 2735 | if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2736 | return -EINVAL; |
| 2737 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2738 | if (params.sack_delay) { |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2739 | if (asoc) { |
| 2740 | asoc->sackdelay = |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2741 | msecs_to_jiffies(params.sack_delay); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2742 | asoc->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2743 | sctp_spp_sackdelay_enable(asoc->param_flags); |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2744 | } else { |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2745 | sp->sackdelay = params.sack_delay; |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2746 | sp->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2747 | sctp_spp_sackdelay_enable(sp->param_flags); |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2748 | } |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | if (params.sack_freq == 1) { |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2752 | if (asoc) { |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2753 | asoc->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2754 | sctp_spp_sackdelay_disable(asoc->param_flags); |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2755 | } else { |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2756 | sp->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2757 | sctp_spp_sackdelay_disable(sp->param_flags); |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2758 | } |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2759 | } else if (params.sack_freq > 1) { |
| 2760 | if (asoc) { |
| 2761 | asoc->sackfreq = params.sack_freq; |
| 2762 | asoc->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2763 | sctp_spp_sackdelay_enable(asoc->param_flags); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2764 | } else { |
| 2765 | sp->sackfreq = params.sack_freq; |
| 2766 | sp->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2767 | sctp_spp_sackdelay_enable(sp->param_flags); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2768 | } |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | /* If change is for association, also apply to each transport. */ |
| 2772 | if (asoc) { |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 2773 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, |
| 2774 | transports) { |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2775 | if (params.sack_delay) { |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2776 | trans->sackdelay = |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2777 | msecs_to_jiffies(params.sack_delay); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2778 | trans->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2779 | sctp_spp_sackdelay_enable(trans->param_flags); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2780 | } |
Vlad Yasevich | 7bfe8bdb | 2008-06-09 15:45:05 -0700 | [diff] [blame] | 2781 | if (params.sack_freq == 1) { |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2782 | trans->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2783 | sctp_spp_sackdelay_disable(trans->param_flags); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2784 | } else if (params.sack_freq > 1) { |
| 2785 | trans->sackfreq = params.sack_freq; |
| 2786 | trans->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2787 | sctp_spp_sackdelay_enable(trans->param_flags); |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2788 | } |
| 2789 | } |
| 2790 | } |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2791 | |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2792 | return 0; |
| 2793 | } |
| 2794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) |
| 2796 | * |
| 2797 | * Applications can specify protocol parameters for the default association |
| 2798 | * initialization. The option name argument to setsockopt() and getsockopt() |
| 2799 | * is SCTP_INITMSG. |
| 2800 | * |
| 2801 | * Setting initialization parameters is effective only on an unconnected |
| 2802 | * socket (for UDP-style sockets only future associations are effected |
| 2803 | * by the change). With TCP-style sockets, this option is inherited by |
| 2804 | * sockets derived from a listener socket. |
| 2805 | */ |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2806 | static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | { |
| 2808 | struct sctp_initmsg sinit; |
| 2809 | struct sctp_sock *sp = sctp_sk(sk); |
| 2810 | |
| 2811 | if (optlen != sizeof(struct sctp_initmsg)) |
| 2812 | return -EINVAL; |
| 2813 | if (copy_from_user(&sinit, optval, optlen)) |
| 2814 | return -EFAULT; |
| 2815 | |
| 2816 | if (sinit.sinit_num_ostreams) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2817 | sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | if (sinit.sinit_max_instreams) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2819 | sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | if (sinit.sinit_max_attempts) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2821 | sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | if (sinit.sinit_max_init_timeo) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2823 | sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | |
| 2825 | return 0; |
| 2826 | } |
| 2827 | |
| 2828 | /* |
| 2829 | * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) |
| 2830 | * |
| 2831 | * Applications that wish to use the sendto() system call may wish to |
| 2832 | * specify a default set of parameters that would normally be supplied |
| 2833 | * through the inclusion of ancillary data. This socket option allows |
| 2834 | * such an application to set the default sctp_sndrcvinfo structure. |
| 2835 | * The application that wishes to use this socket option simply passes |
| 2836 | * in to this call the sctp_sndrcvinfo structure defined in Section |
| 2837 | * 5.2.2) The input parameters accepted by this call include |
| 2838 | * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, |
| 2839 | * sinfo_timetolive. The user must provide the sinfo_assoc_id field in |
| 2840 | * to this call if the caller is using the UDP model. |
| 2841 | */ |
| 2842 | static int sctp_setsockopt_default_send_param(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2843 | char __user *optval, |
| 2844 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | struct sctp_sock *sp = sctp_sk(sk); |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2847 | struct sctp_association *asoc; |
| 2848 | struct sctp_sndrcvinfo info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2850 | if (optlen != sizeof(info)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | return -EINVAL; |
| 2852 | if (copy_from_user(&info, optval, optlen)) |
| 2853 | return -EFAULT; |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2854 | if (info.sinfo_flags & |
| 2855 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | |
| 2856 | SCTP_ABORT | SCTP_EOF)) |
| 2857 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | |
| 2859 | asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); |
| 2860 | if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) |
| 2861 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | if (asoc) { |
| 2863 | asoc->default_stream = info.sinfo_stream; |
| 2864 | asoc->default_flags = info.sinfo_flags; |
| 2865 | asoc->default_ppid = info.sinfo_ppid; |
| 2866 | asoc->default_context = info.sinfo_context; |
| 2867 | asoc->default_timetolive = info.sinfo_timetolive; |
| 2868 | } else { |
| 2869 | sp->default_stream = info.sinfo_stream; |
| 2870 | sp->default_flags = info.sinfo_flags; |
| 2871 | sp->default_ppid = info.sinfo_ppid; |
| 2872 | sp->default_context = info.sinfo_context; |
| 2873 | sp->default_timetolive = info.sinfo_timetolive; |
| 2874 | } |
| 2875 | |
| 2876 | return 0; |
| 2877 | } |
| 2878 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2879 | /* RFC6458, Section 8.1.31. Set/get Default Send Parameters |
| 2880 | * (SCTP_DEFAULT_SNDINFO) |
| 2881 | */ |
| 2882 | static int sctp_setsockopt_default_sndinfo(struct sock *sk, |
| 2883 | char __user *optval, |
| 2884 | unsigned int optlen) |
| 2885 | { |
| 2886 | struct sctp_sock *sp = sctp_sk(sk); |
| 2887 | struct sctp_association *asoc; |
| 2888 | struct sctp_sndinfo info; |
| 2889 | |
| 2890 | if (optlen != sizeof(info)) |
| 2891 | return -EINVAL; |
| 2892 | if (copy_from_user(&info, optval, optlen)) |
| 2893 | return -EFAULT; |
| 2894 | if (info.snd_flags & |
| 2895 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | |
| 2896 | SCTP_ABORT | SCTP_EOF)) |
| 2897 | return -EINVAL; |
| 2898 | |
| 2899 | asoc = sctp_id2assoc(sk, info.snd_assoc_id); |
| 2900 | if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP)) |
| 2901 | return -EINVAL; |
| 2902 | if (asoc) { |
| 2903 | asoc->default_stream = info.snd_sid; |
| 2904 | asoc->default_flags = info.snd_flags; |
| 2905 | asoc->default_ppid = info.snd_ppid; |
| 2906 | asoc->default_context = info.snd_context; |
| 2907 | } else { |
| 2908 | sp->default_stream = info.snd_sid; |
| 2909 | sp->default_flags = info.snd_flags; |
| 2910 | sp->default_ppid = info.snd_ppid; |
| 2911 | sp->default_context = info.snd_context; |
| 2912 | } |
| 2913 | |
| 2914 | return 0; |
| 2915 | } |
| 2916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) |
| 2918 | * |
| 2919 | * Requests that the local SCTP stack use the enclosed peer address as |
| 2920 | * the association primary. The enclosed address must be one of the |
| 2921 | * association peer's addresses. |
| 2922 | */ |
| 2923 | static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2924 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | { |
| 2926 | struct sctp_prim prim; |
| 2927 | struct sctp_transport *trans; |
| 2928 | |
| 2929 | if (optlen != sizeof(struct sctp_prim)) |
| 2930 | return -EINVAL; |
| 2931 | |
| 2932 | if (copy_from_user(&prim, optval, sizeof(struct sctp_prim))) |
| 2933 | return -EFAULT; |
| 2934 | |
| 2935 | trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id); |
| 2936 | if (!trans) |
| 2937 | return -EINVAL; |
| 2938 | |
| 2939 | sctp_assoc_set_primary(trans->asoc, trans); |
| 2940 | |
| 2941 | return 0; |
| 2942 | } |
| 2943 | |
| 2944 | /* |
| 2945 | * 7.1.5 SCTP_NODELAY |
| 2946 | * |
| 2947 | * Turn on/off any Nagle-like algorithm. This means that packets are |
| 2948 | * generally sent as soon as possible and no unnecessary delays are |
| 2949 | * introduced, at the cost of more packets in the network. Expects an |
| 2950 | * integer boolean flag. |
| 2951 | */ |
| 2952 | static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2953 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | { |
| 2955 | int val; |
| 2956 | |
| 2957 | if (optlen < sizeof(int)) |
| 2958 | return -EINVAL; |
| 2959 | if (get_user(val, (int __user *)optval)) |
| 2960 | return -EFAULT; |
| 2961 | |
| 2962 | sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1; |
| 2963 | return 0; |
| 2964 | } |
| 2965 | |
| 2966 | /* |
| 2967 | * |
| 2968 | * 7.1.1 SCTP_RTOINFO |
| 2969 | * |
| 2970 | * The protocol parameters used to initialize and bound retransmission |
| 2971 | * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access |
| 2972 | * and modify these parameters. |
| 2973 | * All parameters are time values, in milliseconds. A value of 0, when |
| 2974 | * modifying the parameters, indicates that the current value should not |
| 2975 | * be changed. |
| 2976 | * |
| 2977 | */ |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2978 | static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen) |
| 2979 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | struct sctp_rtoinfo rtoinfo; |
| 2981 | struct sctp_association *asoc; |
wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 2982 | unsigned long rto_min, rto_max; |
| 2983 | struct sctp_sock *sp = sctp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | |
| 2985 | if (optlen != sizeof (struct sctp_rtoinfo)) |
| 2986 | return -EINVAL; |
| 2987 | |
| 2988 | if (copy_from_user(&rtoinfo, optval, optlen)) |
| 2989 | return -EFAULT; |
| 2990 | |
| 2991 | asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); |
| 2992 | |
| 2993 | /* Set the values to the specific association */ |
| 2994 | if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) |
| 2995 | return -EINVAL; |
| 2996 | |
wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 2997 | rto_max = rtoinfo.srto_max; |
| 2998 | rto_min = rtoinfo.srto_min; |
| 2999 | |
| 3000 | if (rto_max) |
| 3001 | rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max; |
| 3002 | else |
| 3003 | rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max; |
| 3004 | |
| 3005 | if (rto_min) |
| 3006 | rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min; |
| 3007 | else |
| 3008 | rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min; |
| 3009 | |
| 3010 | if (rto_min > rto_max) |
| 3011 | return -EINVAL; |
| 3012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | if (asoc) { |
| 3014 | if (rtoinfo.srto_initial != 0) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3015 | asoc->rto_initial = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | msecs_to_jiffies(rtoinfo.srto_initial); |
wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 3017 | asoc->rto_max = rto_max; |
| 3018 | asoc->rto_min = rto_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | } else { |
| 3020 | /* If there is no association or the association-id = 0 |
| 3021 | * set the values to the endpoint. |
| 3022 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | if (rtoinfo.srto_initial != 0) |
| 3024 | sp->rtoinfo.srto_initial = rtoinfo.srto_initial; |
wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 3025 | sp->rtoinfo.srto_max = rto_max; |
| 3026 | sp->rtoinfo.srto_min = rto_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | return 0; |
| 3030 | } |
| 3031 | |
| 3032 | /* |
| 3033 | * |
| 3034 | * 7.1.2 SCTP_ASSOCINFO |
| 3035 | * |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 3036 | * This option is used to tune the maximum retransmission attempts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | * of the association. |
| 3038 | * Returns an error if the new association retransmission value is |
| 3039 | * greater than the sum of the retransmission value of the peer. |
| 3040 | * See [SCTP] for more information. |
| 3041 | * |
| 3042 | */ |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3043 | static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | { |
| 3045 | |
| 3046 | struct sctp_assocparams assocparams; |
| 3047 | struct sctp_association *asoc; |
| 3048 | |
| 3049 | if (optlen != sizeof(struct sctp_assocparams)) |
| 3050 | return -EINVAL; |
| 3051 | if (copy_from_user(&assocparams, optval, optlen)) |
| 3052 | return -EFAULT; |
| 3053 | |
| 3054 | asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); |
| 3055 | |
| 3056 | if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) |
| 3057 | return -EINVAL; |
| 3058 | |
| 3059 | /* Set the values to the specific association */ |
| 3060 | if (asoc) { |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3061 | if (assocparams.sasoc_asocmaxrxt != 0) { |
| 3062 | __u32 path_sum = 0; |
| 3063 | int paths = 0; |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3064 | struct sctp_transport *peer_addr; |
| 3065 | |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 3066 | list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list, |
| 3067 | transports) { |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3068 | path_sum += peer_addr->pathmaxrxt; |
| 3069 | paths++; |
| 3070 | } |
| 3071 | |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 3072 | /* Only validate asocmaxrxt if we have more than |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3073 | * one path/transport. We do this because path |
| 3074 | * retransmissions are only counted when we have more |
| 3075 | * then one path. |
| 3076 | */ |
| 3077 | if (paths > 1 && |
| 3078 | assocparams.sasoc_asocmaxrxt > path_sum) |
| 3079 | return -EINVAL; |
| 3080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | asoc->max_retrans = assocparams.sasoc_asocmaxrxt; |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
Daniel Borkmann | 52db882 | 2013-06-25 18:17:27 +0200 | [diff] [blame] | 3084 | if (assocparams.sasoc_cookie_life != 0) |
| 3085 | asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | } else { |
| 3087 | /* Set the values to the endpoint */ |
| 3088 | struct sctp_sock *sp = sctp_sk(sk); |
| 3089 | |
| 3090 | if (assocparams.sasoc_asocmaxrxt != 0) |
| 3091 | sp->assocparams.sasoc_asocmaxrxt = |
| 3092 | assocparams.sasoc_asocmaxrxt; |
| 3093 | if (assocparams.sasoc_cookie_life != 0) |
| 3094 | sp->assocparams.sasoc_cookie_life = |
| 3095 | assocparams.sasoc_cookie_life; |
| 3096 | } |
| 3097 | return 0; |
| 3098 | } |
| 3099 | |
| 3100 | /* |
| 3101 | * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) |
| 3102 | * |
| 3103 | * This socket option is a boolean flag which turns on or off mapped V4 |
| 3104 | * addresses. If this option is turned on and the socket is type |
| 3105 | * PF_INET6, then IPv4 addresses will be mapped to V6 representation. |
| 3106 | * If this option is turned off, then no mapping will be done of V4 |
| 3107 | * addresses and a user will receive both PF_INET6 and PF_INET type |
| 3108 | * addresses on the socket. |
| 3109 | */ |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3110 | static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | { |
| 3112 | int val; |
| 3113 | struct sctp_sock *sp = sctp_sk(sk); |
| 3114 | |
| 3115 | if (optlen < sizeof(int)) |
| 3116 | return -EINVAL; |
| 3117 | if (get_user(val, (int __user *)optval)) |
| 3118 | return -EFAULT; |
| 3119 | if (val) |
| 3120 | sp->v4mapped = 1; |
| 3121 | else |
| 3122 | sp->v4mapped = 0; |
| 3123 | |
| 3124 | return 0; |
| 3125 | } |
| 3126 | |
| 3127 | /* |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3128 | * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) |
| 3129 | * This option will get or set the maximum size to put in any outgoing |
| 3130 | * SCTP DATA chunk. If a message is larger than this size it will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | * fragmented by SCTP into the specified size. Note that the underlying |
| 3132 | * SCTP implementation may fragment into smaller sized chunks when the |
| 3133 | * PMTU of the underlying association is smaller than the value set by |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3134 | * the user. The default value for this option is '0' which indicates |
| 3135 | * the user is NOT limiting fragmentation and only the PMTU will effect |
| 3136 | * SCTP's choice of DATA chunk size. Note also that values set larger |
| 3137 | * than the maximum size of an IP datagram will effectively let SCTP |
| 3138 | * control fragmentation (i.e. the same as setting this option to 0). |
| 3139 | * |
| 3140 | * The following structure is used to access and modify this parameter: |
| 3141 | * |
| 3142 | * struct sctp_assoc_value { |
| 3143 | * sctp_assoc_t assoc_id; |
| 3144 | * uint32_t assoc_value; |
| 3145 | * }; |
| 3146 | * |
| 3147 | * assoc_id: This parameter is ignored for one-to-one style sockets. |
| 3148 | * For one-to-many style sockets this parameter indicates which |
| 3149 | * association the user is performing an action upon. Note that if |
| 3150 | * this field's value is zero then the endpoints default value is |
| 3151 | * changed (effecting future associations only). |
| 3152 | * assoc_value: This parameter specifies the maximum size in bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | */ |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3154 | static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | { |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3156 | struct sctp_sock *sp = sctp_sk(sk); |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3157 | struct sctp_assoc_value params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | struct sctp_association *asoc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | int val; |
| 3160 | |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3161 | if (optlen == sizeof(int)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3162 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3163 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3164 | "Use of int in maxseg socket option.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3165 | "Use struct sctp_assoc_value instead\n", |
| 3166 | current->comm, task_pid_nr(current)); |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3167 | if (copy_from_user(&val, optval, optlen)) |
| 3168 | return -EFAULT; |
| 3169 | params.assoc_id = 0; |
| 3170 | } else if (optlen == sizeof(struct sctp_assoc_value)) { |
| 3171 | if (copy_from_user(¶ms, optval, optlen)) |
| 3172 | return -EFAULT; |
| 3173 | val = params.assoc_value; |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3174 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | return -EINVAL; |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3176 | } |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3177 | |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3178 | if (val) { |
| 3179 | int min_len, max_len; |
| 3180 | |
| 3181 | min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len; |
| 3182 | min_len -= sizeof(struct sctphdr) + |
| 3183 | sizeof(struct sctp_data_chunk); |
| 3184 | |
| 3185 | max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk); |
| 3186 | |
| 3187 | if (val < min_len || val > max_len) |
| 3188 | return -EINVAL; |
| 3189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3191 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3192 | if (asoc) { |
| 3193 | if (val == 0) { |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3194 | val = asoc->pathmtu - sp->pf->af->net_header_len; |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3195 | val -= sizeof(struct sctphdr) + |
Xin Long | 668c9be | 2017-12-08 21:04:02 +0800 | [diff] [blame] | 3196 | sctp_datachk_len(&asoc->stream); |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3197 | } |
Vlad Yasevich | f68b2e0 | 2009-09-04 18:21:00 -0400 | [diff] [blame] | 3198 | asoc->user_frag = val; |
| 3199 | asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu); |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3200 | } else { |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3201 | if (params.assoc_id && sctp_style(sk, UDP)) |
| 3202 | return -EINVAL; |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3203 | sp->user_frag = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | return 0; |
| 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | /* |
| 3211 | * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) |
| 3212 | * |
| 3213 | * Requests that the peer mark the enclosed address as the association |
| 3214 | * primary. The enclosed address must be one of the association's |
| 3215 | * locally bound addresses. The following structure is used to make a |
| 3216 | * set primary request: |
| 3217 | */ |
| 3218 | static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3219 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3220 | { |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3221 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3222 | struct sctp_sock *sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | struct sctp_association *asoc = NULL; |
| 3224 | struct sctp_setpeerprim prim; |
| 3225 | struct sctp_chunk *chunk; |
Wei Yongjun | 40a0103 | 2010-12-07 17:11:09 +0000 | [diff] [blame] | 3226 | struct sctp_af *af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | int err; |
| 3228 | |
| 3229 | sp = sctp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 3231 | if (!net->sctp.addip_enable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | return -EPERM; |
| 3233 | |
| 3234 | if (optlen != sizeof(struct sctp_setpeerprim)) |
| 3235 | return -EINVAL; |
| 3236 | |
| 3237 | if (copy_from_user(&prim, optval, optlen)) |
| 3238 | return -EFAULT; |
| 3239 | |
| 3240 | asoc = sctp_id2assoc(sk, prim.sspp_assoc_id); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3241 | if (!asoc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | return -EINVAL; |
| 3243 | |
| 3244 | if (!asoc->peer.asconf_capable) |
| 3245 | return -EPERM; |
| 3246 | |
| 3247 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY) |
| 3248 | return -EPERM; |
| 3249 | |
| 3250 | if (!sctp_state(asoc, ESTABLISHED)) |
| 3251 | return -ENOTCONN; |
| 3252 | |
Wei Yongjun | 40a0103 | 2010-12-07 17:11:09 +0000 | [diff] [blame] | 3253 | af = sctp_get_af_specific(prim.sspp_addr.ss_family); |
| 3254 | if (!af) |
| 3255 | return -EINVAL; |
| 3256 | |
| 3257 | if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL)) |
| 3258 | return -EADDRNOTAVAIL; |
| 3259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr)) |
| 3261 | return -EADDRNOTAVAIL; |
| 3262 | |
| 3263 | /* Create an ASCONF chunk with SET_PRIMARY parameter */ |
| 3264 | chunk = sctp_make_asconf_set_prim(asoc, |
| 3265 | (union sctp_addr *)&prim.sspp_addr); |
| 3266 | if (!chunk) |
| 3267 | return -ENOMEM; |
| 3268 | |
| 3269 | err = sctp_send_asconf(asoc, chunk); |
| 3270 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 3271 | pr_debug("%s: we set peer primary addr primitively\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | |
| 3273 | return err; |
| 3274 | } |
| 3275 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3276 | static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3277 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | { |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3279 | struct sctp_setadaptation adaptation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3281 | if (optlen != sizeof(struct sctp_setadaptation)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | return -EINVAL; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3283 | if (copy_from_user(&adaptation, optval, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | return -EFAULT; |
| 3285 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3286 | sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | |
| 3288 | return 0; |
| 3289 | } |
| 3290 | |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3291 | /* |
| 3292 | * 7.1.29. Set or Get the default context (SCTP_CONTEXT) |
| 3293 | * |
| 3294 | * The context field in the sctp_sndrcvinfo structure is normally only |
| 3295 | * used when a failed message is retrieved holding the value that was |
| 3296 | * sent down on the actual send call. This option allows the setting of |
| 3297 | * a default context on an association basis that will be received on |
| 3298 | * reading messages from the peer. This is especially helpful in the |
| 3299 | * one-2-many model for an application to keep some reference to an |
| 3300 | * internal state machine that is processing messages on the |
| 3301 | * association. Note that the setting of this value only effects |
| 3302 | * received messages from the peer and does not effect the value that is |
| 3303 | * saved with outbound messages. |
| 3304 | */ |
| 3305 | static int sctp_setsockopt_context(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3306 | unsigned int optlen) |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3307 | { |
| 3308 | struct sctp_assoc_value params; |
| 3309 | struct sctp_sock *sp; |
| 3310 | struct sctp_association *asoc; |
| 3311 | |
| 3312 | if (optlen != sizeof(struct sctp_assoc_value)) |
| 3313 | return -EINVAL; |
| 3314 | if (copy_from_user(¶ms, optval, optlen)) |
| 3315 | return -EFAULT; |
| 3316 | |
| 3317 | sp = sctp_sk(sk); |
| 3318 | |
| 3319 | if (params.assoc_id != 0) { |
| 3320 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 3321 | if (!asoc) |
| 3322 | return -EINVAL; |
| 3323 | asoc->default_rcv_context = params.assoc_value; |
| 3324 | } else { |
| 3325 | sp->default_rcv_context = params.assoc_value; |
| 3326 | } |
| 3327 | |
| 3328 | return 0; |
| 3329 | } |
| 3330 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3331 | /* |
| 3332 | * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) |
| 3333 | * |
| 3334 | * This options will at a minimum specify if the implementation is doing |
| 3335 | * fragmented interleave. Fragmented interleave, for a one to many |
| 3336 | * socket, is when subsequent calls to receive a message may return |
| 3337 | * parts of messages from different associations. Some implementations |
| 3338 | * may allow you to turn this value on or off. If so, when turned off, |
| 3339 | * no fragment interleave will occur (which will cause a head of line |
| 3340 | * blocking amongst multiple associations sharing the same one to many |
| 3341 | * socket). When this option is turned on, then each receive call may |
| 3342 | * come from a different association (thus the user must receive data |
| 3343 | * with the extended calls (e.g. sctp_recvmsg) to keep track of which |
| 3344 | * association each receive belongs to. |
| 3345 | * |
| 3346 | * This option takes a boolean value. A non-zero value indicates that |
| 3347 | * fragmented interleave is on. A value of zero indicates that |
| 3348 | * fragmented interleave is off. |
| 3349 | * |
| 3350 | * Note that it is important that an implementation that allows this |
| 3351 | * option to be turned on, have it off by default. Otherwise an unaware |
| 3352 | * application using the one to many model may become confused and act |
| 3353 | * incorrectly. |
| 3354 | */ |
| 3355 | static int sctp_setsockopt_fragment_interleave(struct sock *sk, |
| 3356 | char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3357 | unsigned int optlen) |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3358 | { |
| 3359 | int val; |
| 3360 | |
| 3361 | if (optlen != sizeof(int)) |
| 3362 | return -EINVAL; |
| 3363 | if (get_user(val, (int __user *)optval)) |
| 3364 | return -EFAULT; |
| 3365 | |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 3366 | sctp_sk(sk)->frag_interleave = !!val; |
| 3367 | |
| 3368 | if (!sctp_sk(sk)->frag_interleave) |
| 3369 | sctp_sk(sk)->strm_interleave = 0; |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3370 | |
| 3371 | return 0; |
| 3372 | } |
| 3373 | |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3374 | /* |
Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3375 | * 8.1.21. Set or Get the SCTP Partial Delivery Point |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3376 | * (SCTP_PARTIAL_DELIVERY_POINT) |
Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3377 | * |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3378 | * This option will set or get the SCTP partial delivery point. This |
| 3379 | * point is the size of a message where the partial delivery API will be |
| 3380 | * invoked to help free up rwnd space for the peer. Setting this to a |
Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3381 | * lower value will cause partial deliveries to happen more often. The |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3382 | * calls argument is an integer that sets or gets the partial delivery |
Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3383 | * point. Note also that the call will fail if the user attempts to set |
| 3384 | * this value larger than the socket receive buffer size. |
| 3385 | * |
| 3386 | * Note that any single message having a length smaller than or equal to |
| 3387 | * the SCTP partial delivery point will be delivered in one single read |
| 3388 | * call as long as the user provided buffer is large enough to hold the |
| 3389 | * message. |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3390 | */ |
| 3391 | static int sctp_setsockopt_partial_delivery_point(struct sock *sk, |
| 3392 | char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3393 | unsigned int optlen) |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3394 | { |
| 3395 | u32 val; |
| 3396 | |
| 3397 | if (optlen != sizeof(u32)) |
| 3398 | return -EINVAL; |
| 3399 | if (get_user(val, (int __user *)optval)) |
| 3400 | return -EFAULT; |
| 3401 | |
Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3402 | /* Note: We double the receive buffer from what the user sets |
| 3403 | * it to be, also initial rwnd is based on rcvbuf/2. |
| 3404 | */ |
| 3405 | if (val > (sk->sk_rcvbuf >> 1)) |
| 3406 | return -EINVAL; |
| 3407 | |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3408 | sctp_sk(sk)->pd_point = val; |
| 3409 | |
| 3410 | return 0; /* is this the right error code? */ |
| 3411 | } |
| 3412 | |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3413 | /* |
| 3414 | * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) |
| 3415 | * |
| 3416 | * This option will allow a user to change the maximum burst of packets |
| 3417 | * that can be emitted by this association. Note that the default value |
| 3418 | * is 4, and some implementations may restrict this setting so that it |
| 3419 | * can only be lowered. |
| 3420 | * |
| 3421 | * NOTE: This text doesn't seem right. Do this on a socket basis with |
| 3422 | * future associations inheriting the socket value. |
| 3423 | */ |
| 3424 | static int sctp_setsockopt_maxburst(struct sock *sk, |
| 3425 | char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3426 | unsigned int optlen) |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3427 | { |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3428 | struct sctp_assoc_value params; |
| 3429 | struct sctp_sock *sp; |
| 3430 | struct sctp_association *asoc; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3431 | int val; |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3432 | int assoc_id = 0; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3433 | |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3434 | if (optlen == sizeof(int)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3435 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3436 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3437 | "Use of int in max_burst socket option deprecated.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3438 | "Use struct sctp_assoc_value instead\n", |
| 3439 | current->comm, task_pid_nr(current)); |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3440 | if (copy_from_user(&val, optval, optlen)) |
| 3441 | return -EFAULT; |
| 3442 | } else if (optlen == sizeof(struct sctp_assoc_value)) { |
| 3443 | if (copy_from_user(¶ms, optval, optlen)) |
| 3444 | return -EFAULT; |
| 3445 | val = params.assoc_value; |
| 3446 | assoc_id = params.assoc_id; |
| 3447 | } else |
| 3448 | return -EINVAL; |
| 3449 | |
| 3450 | sp = sctp_sk(sk); |
| 3451 | |
| 3452 | if (assoc_id != 0) { |
| 3453 | asoc = sctp_id2assoc(sk, assoc_id); |
| 3454 | if (!asoc) |
| 3455 | return -EINVAL; |
| 3456 | asoc->max_burst = val; |
| 3457 | } else |
| 3458 | sp->max_burst = val; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3459 | |
| 3460 | return 0; |
| 3461 | } |
| 3462 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3463 | /* |
| 3464 | * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK) |
| 3465 | * |
| 3466 | * This set option adds a chunk type that the user is requesting to be |
| 3467 | * received only in an authenticated way. Changes to the list of chunks |
| 3468 | * will only effect future associations on the socket. |
| 3469 | */ |
| 3470 | static int sctp_setsockopt_auth_chunk(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3471 | char __user *optval, |
| 3472 | unsigned int optlen) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3473 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3474 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3475 | struct sctp_authchunk val; |
| 3476 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3477 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3478 | return -EACCES; |
| 3479 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3480 | if (optlen != sizeof(struct sctp_authchunk)) |
| 3481 | return -EINVAL; |
| 3482 | if (copy_from_user(&val, optval, optlen)) |
| 3483 | return -EFAULT; |
| 3484 | |
| 3485 | switch (val.sauth_chunk) { |
Joe Perches | 7fd71b1 | 2011-07-01 09:43:11 +0000 | [diff] [blame] | 3486 | case SCTP_CID_INIT: |
| 3487 | case SCTP_CID_INIT_ACK: |
| 3488 | case SCTP_CID_SHUTDOWN_COMPLETE: |
| 3489 | case SCTP_CID_AUTH: |
| 3490 | return -EINVAL; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /* add this chunk id to the endpoint */ |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3494 | return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | /* |
| 3498 | * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT) |
| 3499 | * |
| 3500 | * This option gets or sets the list of HMAC algorithms that the local |
| 3501 | * endpoint requires the peer to use. |
| 3502 | */ |
| 3503 | static int sctp_setsockopt_hmac_ident(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3504 | char __user *optval, |
| 3505 | unsigned int optlen) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3506 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3507 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3508 | struct sctp_hmacalgo *hmacs; |
Vlad Yasevich | d972405 | 2008-08-27 16:09:49 -0700 | [diff] [blame] | 3509 | u32 idents; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3510 | int err; |
| 3511 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3512 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3513 | return -EACCES; |
| 3514 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3515 | if (optlen < sizeof(struct sctp_hmacalgo)) |
| 3516 | return -EINVAL; |
| 3517 | |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 3518 | hmacs = memdup_user(optval, optlen); |
Shan Wei | 934253a | 2011-04-18 19:13:18 +0000 | [diff] [blame] | 3519 | if (IS_ERR(hmacs)) |
| 3520 | return PTR_ERR(hmacs); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3521 | |
Vlad Yasevich | d972405 | 2008-08-27 16:09:49 -0700 | [diff] [blame] | 3522 | idents = hmacs->shmac_num_idents; |
| 3523 | if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS || |
| 3524 | (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) { |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3525 | err = -EINVAL; |
| 3526 | goto out; |
| 3527 | } |
| 3528 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3529 | err = sctp_auth_ep_set_hmacs(ep, hmacs); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3530 | out: |
| 3531 | kfree(hmacs); |
| 3532 | return err; |
| 3533 | } |
| 3534 | |
| 3535 | /* |
| 3536 | * 7.1.20. Set a shared key (SCTP_AUTH_KEY) |
| 3537 | * |
| 3538 | * This option will set a shared secret key which is used to build an |
| 3539 | * association shared key. |
| 3540 | */ |
| 3541 | static int sctp_setsockopt_auth_key(struct sock *sk, |
| 3542 | char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3543 | unsigned int optlen) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3544 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3545 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3546 | struct sctp_authkey *authkey; |
| 3547 | struct sctp_association *asoc; |
| 3548 | int ret; |
| 3549 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3550 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3551 | return -EACCES; |
| 3552 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3553 | if (optlen <= sizeof(struct sctp_authkey)) |
| 3554 | return -EINVAL; |
| 3555 | |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 3556 | authkey = memdup_user(optval, optlen); |
Shan Wei | 934253a | 2011-04-18 19:13:18 +0000 | [diff] [blame] | 3557 | if (IS_ERR(authkey)) |
| 3558 | return PTR_ERR(authkey); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3559 | |
Vlad Yasevich | 328fc47 | 2008-08-27 16:08:54 -0700 | [diff] [blame] | 3560 | if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) { |
Vlad Yasevich | 30c2235 | 2008-08-25 15:16:19 -0700 | [diff] [blame] | 3561 | ret = -EINVAL; |
| 3562 | goto out; |
| 3563 | } |
| 3564 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3565 | asoc = sctp_id2assoc(sk, authkey->sca_assoc_id); |
| 3566 | if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) { |
| 3567 | ret = -EINVAL; |
| 3568 | goto out; |
| 3569 | } |
| 3570 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3571 | ret = sctp_auth_set_key(ep, asoc, authkey); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3572 | out: |
Daniel Borkmann | 6ba542a | 2013-02-08 03:04:34 +0000 | [diff] [blame] | 3573 | kzfree(authkey); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3574 | return ret; |
| 3575 | } |
| 3576 | |
| 3577 | /* |
| 3578 | * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY) |
| 3579 | * |
| 3580 | * This option will get or set the active shared key to be used to build |
| 3581 | * the association shared key. |
| 3582 | */ |
| 3583 | static int sctp_setsockopt_active_key(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3584 | char __user *optval, |
| 3585 | unsigned int optlen) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3586 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3587 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3588 | struct sctp_authkeyid val; |
| 3589 | struct sctp_association *asoc; |
| 3590 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3591 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3592 | return -EACCES; |
| 3593 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3594 | if (optlen != sizeof(struct sctp_authkeyid)) |
| 3595 | return -EINVAL; |
| 3596 | if (copy_from_user(&val, optval, optlen)) |
| 3597 | return -EFAULT; |
| 3598 | |
| 3599 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); |
| 3600 | if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) |
| 3601 | return -EINVAL; |
| 3602 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3603 | return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3604 | } |
| 3605 | |
| 3606 | /* |
| 3607 | * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY) |
| 3608 | * |
| 3609 | * This set option will delete a shared secret key from use. |
| 3610 | */ |
| 3611 | static int sctp_setsockopt_del_key(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3612 | char __user *optval, |
| 3613 | unsigned int optlen) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3614 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3615 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3616 | struct sctp_authkeyid val; |
| 3617 | struct sctp_association *asoc; |
| 3618 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3619 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3620 | return -EACCES; |
| 3621 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3622 | if (optlen != sizeof(struct sctp_authkeyid)) |
| 3623 | return -EINVAL; |
| 3624 | if (copy_from_user(&val, optval, optlen)) |
| 3625 | return -EFAULT; |
| 3626 | |
| 3627 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); |
| 3628 | if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) |
| 3629 | return -EINVAL; |
| 3630 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3631 | return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3632 | |
| 3633 | } |
| 3634 | |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3635 | /* |
| 3636 | * 8.1.23 SCTP_AUTO_ASCONF |
| 3637 | * |
| 3638 | * This option will enable or disable the use of the automatic generation of |
| 3639 | * ASCONF chunks to add and delete addresses to an existing association. Note |
| 3640 | * that this option has two caveats namely: a) it only affects sockets that |
| 3641 | * are bound to all addresses available to the SCTP stack, and b) the system |
| 3642 | * administrator may have an overriding control that turns the ASCONF feature |
| 3643 | * off no matter what setting the socket option may have. |
| 3644 | * This option expects an integer boolean flag, where a non-zero value turns on |
| 3645 | * the option, and a zero value turns off the option. |
| 3646 | * Note. In this implementation, socket operation overrides default parameter |
| 3647 | * being set by sysctl as well as FreeBSD implementation |
| 3648 | */ |
| 3649 | static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval, |
| 3650 | unsigned int optlen) |
| 3651 | { |
| 3652 | int val; |
| 3653 | struct sctp_sock *sp = sctp_sk(sk); |
| 3654 | |
| 3655 | if (optlen < sizeof(int)) |
| 3656 | return -EINVAL; |
| 3657 | if (get_user(val, (int __user *)optval)) |
| 3658 | return -EFAULT; |
| 3659 | if (!sctp_is_ep_boundall(sk) && val) |
| 3660 | return -EINVAL; |
| 3661 | if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf)) |
| 3662 | return 0; |
| 3663 | |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 3664 | spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock); |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3665 | if (val == 0 && sp->do_auto_asconf) { |
| 3666 | list_del(&sp->auto_asconf_list); |
| 3667 | sp->do_auto_asconf = 0; |
| 3668 | } else if (val && !sp->do_auto_asconf) { |
| 3669 | list_add_tail(&sp->auto_asconf_list, |
Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 3670 | &sock_net(sk)->sctp.auto_asconf_splist); |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3671 | sp->do_auto_asconf = 1; |
| 3672 | } |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 3673 | spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock); |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3674 | return 0; |
| 3675 | } |
| 3676 | |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3677 | /* |
| 3678 | * SCTP_PEER_ADDR_THLDS |
| 3679 | * |
| 3680 | * This option allows us to alter the partially failed threshold for one or all |
| 3681 | * transports in an association. See Section 6.1 of: |
| 3682 | * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt |
| 3683 | */ |
| 3684 | static int sctp_setsockopt_paddr_thresholds(struct sock *sk, |
| 3685 | char __user *optval, |
| 3686 | unsigned int optlen) |
| 3687 | { |
| 3688 | struct sctp_paddrthlds val; |
| 3689 | struct sctp_transport *trans; |
| 3690 | struct sctp_association *asoc; |
| 3691 | |
| 3692 | if (optlen < sizeof(struct sctp_paddrthlds)) |
| 3693 | return -EINVAL; |
| 3694 | if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, |
| 3695 | sizeof(struct sctp_paddrthlds))) |
| 3696 | return -EFAULT; |
| 3697 | |
| 3698 | |
| 3699 | if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { |
| 3700 | asoc = sctp_id2assoc(sk, val.spt_assoc_id); |
| 3701 | if (!asoc) |
| 3702 | return -ENOENT; |
| 3703 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, |
| 3704 | transports) { |
| 3705 | if (val.spt_pathmaxrxt) |
| 3706 | trans->pathmaxrxt = val.spt_pathmaxrxt; |
| 3707 | trans->pf_retrans = val.spt_pathpfthld; |
| 3708 | } |
| 3709 | |
| 3710 | if (val.spt_pathmaxrxt) |
| 3711 | asoc->pathmaxrxt = val.spt_pathmaxrxt; |
| 3712 | asoc->pf_retrans = val.spt_pathpfthld; |
| 3713 | } else { |
| 3714 | trans = sctp_addr_id2transport(sk, &val.spt_address, |
| 3715 | val.spt_assoc_id); |
| 3716 | if (!trans) |
| 3717 | return -ENOENT; |
| 3718 | |
| 3719 | if (val.spt_pathmaxrxt) |
| 3720 | trans->pathmaxrxt = val.spt_pathmaxrxt; |
| 3721 | trans->pf_retrans = val.spt_pathpfthld; |
| 3722 | } |
| 3723 | |
| 3724 | return 0; |
| 3725 | } |
| 3726 | |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 3727 | static int sctp_setsockopt_recvrcvinfo(struct sock *sk, |
| 3728 | char __user *optval, |
| 3729 | unsigned int optlen) |
| 3730 | { |
| 3731 | int val; |
| 3732 | |
| 3733 | if (optlen < sizeof(int)) |
| 3734 | return -EINVAL; |
| 3735 | if (get_user(val, (int __user *) optval)) |
| 3736 | return -EFAULT; |
| 3737 | |
| 3738 | sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1; |
| 3739 | |
| 3740 | return 0; |
| 3741 | } |
| 3742 | |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 3743 | static int sctp_setsockopt_recvnxtinfo(struct sock *sk, |
| 3744 | char __user *optval, |
| 3745 | unsigned int optlen) |
| 3746 | { |
| 3747 | int val; |
| 3748 | |
| 3749 | if (optlen < sizeof(int)) |
| 3750 | return -EINVAL; |
| 3751 | if (get_user(val, (int __user *) optval)) |
| 3752 | return -EFAULT; |
| 3753 | |
| 3754 | sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1; |
| 3755 | |
| 3756 | return 0; |
| 3757 | } |
| 3758 | |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 3759 | static int sctp_setsockopt_pr_supported(struct sock *sk, |
| 3760 | char __user *optval, |
| 3761 | unsigned int optlen) |
| 3762 | { |
| 3763 | struct sctp_assoc_value params; |
| 3764 | struct sctp_association *asoc; |
| 3765 | int retval = -EINVAL; |
| 3766 | |
| 3767 | if (optlen != sizeof(params)) |
| 3768 | goto out; |
| 3769 | |
| 3770 | if (copy_from_user(¶ms, optval, optlen)) { |
| 3771 | retval = -EFAULT; |
| 3772 | goto out; |
| 3773 | } |
| 3774 | |
| 3775 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 3776 | if (asoc) { |
| 3777 | asoc->prsctp_enable = !!params.assoc_value; |
| 3778 | } else if (!params.assoc_id) { |
| 3779 | struct sctp_sock *sp = sctp_sk(sk); |
| 3780 | |
| 3781 | sp->ep->prsctp_enable = !!params.assoc_value; |
| 3782 | } else { |
| 3783 | goto out; |
| 3784 | } |
| 3785 | |
| 3786 | retval = 0; |
| 3787 | |
| 3788 | out: |
| 3789 | return retval; |
| 3790 | } |
| 3791 | |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 3792 | static int sctp_setsockopt_default_prinfo(struct sock *sk, |
| 3793 | char __user *optval, |
| 3794 | unsigned int optlen) |
| 3795 | { |
| 3796 | struct sctp_default_prinfo info; |
| 3797 | struct sctp_association *asoc; |
| 3798 | int retval = -EINVAL; |
| 3799 | |
| 3800 | if (optlen != sizeof(info)) |
| 3801 | goto out; |
| 3802 | |
| 3803 | if (copy_from_user(&info, optval, sizeof(info))) { |
| 3804 | retval = -EFAULT; |
| 3805 | goto out; |
| 3806 | } |
| 3807 | |
| 3808 | if (info.pr_policy & ~SCTP_PR_SCTP_MASK) |
| 3809 | goto out; |
| 3810 | |
| 3811 | if (info.pr_policy == SCTP_PR_SCTP_NONE) |
| 3812 | info.pr_value = 0; |
| 3813 | |
| 3814 | asoc = sctp_id2assoc(sk, info.pr_assoc_id); |
| 3815 | if (asoc) { |
| 3816 | SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy); |
| 3817 | asoc->default_timetolive = info.pr_value; |
| 3818 | } else if (!info.pr_assoc_id) { |
| 3819 | struct sctp_sock *sp = sctp_sk(sk); |
| 3820 | |
| 3821 | SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy); |
| 3822 | sp->default_timetolive = info.pr_value; |
| 3823 | } else { |
| 3824 | goto out; |
| 3825 | } |
| 3826 | |
| 3827 | retval = 0; |
| 3828 | |
| 3829 | out: |
| 3830 | return retval; |
| 3831 | } |
| 3832 | |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 3833 | static int sctp_setsockopt_reconfig_supported(struct sock *sk, |
| 3834 | char __user *optval, |
| 3835 | unsigned int optlen) |
| 3836 | { |
| 3837 | struct sctp_assoc_value params; |
| 3838 | struct sctp_association *asoc; |
| 3839 | int retval = -EINVAL; |
| 3840 | |
| 3841 | if (optlen != sizeof(params)) |
| 3842 | goto out; |
| 3843 | |
| 3844 | if (copy_from_user(¶ms, optval, optlen)) { |
| 3845 | retval = -EFAULT; |
| 3846 | goto out; |
| 3847 | } |
| 3848 | |
| 3849 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 3850 | if (asoc) { |
| 3851 | asoc->reconf_enable = !!params.assoc_value; |
| 3852 | } else if (!params.assoc_id) { |
| 3853 | struct sctp_sock *sp = sctp_sk(sk); |
| 3854 | |
| 3855 | sp->ep->reconf_enable = !!params.assoc_value; |
| 3856 | } else { |
| 3857 | goto out; |
| 3858 | } |
| 3859 | |
| 3860 | retval = 0; |
| 3861 | |
| 3862 | out: |
| 3863 | return retval; |
| 3864 | } |
| 3865 | |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 3866 | static int sctp_setsockopt_enable_strreset(struct sock *sk, |
| 3867 | char __user *optval, |
| 3868 | unsigned int optlen) |
| 3869 | { |
| 3870 | struct sctp_assoc_value params; |
| 3871 | struct sctp_association *asoc; |
| 3872 | int retval = -EINVAL; |
| 3873 | |
| 3874 | if (optlen != sizeof(params)) |
| 3875 | goto out; |
| 3876 | |
| 3877 | if (copy_from_user(¶ms, optval, optlen)) { |
| 3878 | retval = -EFAULT; |
| 3879 | goto out; |
| 3880 | } |
| 3881 | |
| 3882 | if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK)) |
| 3883 | goto out; |
| 3884 | |
| 3885 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 3886 | if (asoc) { |
| 3887 | asoc->strreset_enable = params.assoc_value; |
| 3888 | } else if (!params.assoc_id) { |
| 3889 | struct sctp_sock *sp = sctp_sk(sk); |
| 3890 | |
| 3891 | sp->ep->strreset_enable = params.assoc_value; |
| 3892 | } else { |
| 3893 | goto out; |
| 3894 | } |
| 3895 | |
| 3896 | retval = 0; |
| 3897 | |
| 3898 | out: |
| 3899 | return retval; |
| 3900 | } |
| 3901 | |
Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 3902 | static int sctp_setsockopt_reset_streams(struct sock *sk, |
| 3903 | char __user *optval, |
| 3904 | unsigned int optlen) |
| 3905 | { |
| 3906 | struct sctp_reset_streams *params; |
| 3907 | struct sctp_association *asoc; |
| 3908 | int retval = -EINVAL; |
| 3909 | |
| 3910 | if (optlen < sizeof(struct sctp_reset_streams)) |
| 3911 | return -EINVAL; |
| 3912 | |
| 3913 | params = memdup_user(optval, optlen); |
| 3914 | if (IS_ERR(params)) |
| 3915 | return PTR_ERR(params); |
| 3916 | |
| 3917 | asoc = sctp_id2assoc(sk, params->srs_assoc_id); |
| 3918 | if (!asoc) |
| 3919 | goto out; |
| 3920 | |
| 3921 | retval = sctp_send_reset_streams(asoc, params); |
| 3922 | |
| 3923 | out: |
| 3924 | kfree(params); |
| 3925 | return retval; |
| 3926 | } |
| 3927 | |
Xin Long | a92ce1a | 2017-02-09 01:18:18 +0800 | [diff] [blame] | 3928 | static int sctp_setsockopt_reset_assoc(struct sock *sk, |
| 3929 | char __user *optval, |
| 3930 | unsigned int optlen) |
| 3931 | { |
| 3932 | struct sctp_association *asoc; |
| 3933 | sctp_assoc_t associd; |
| 3934 | int retval = -EINVAL; |
| 3935 | |
| 3936 | if (optlen != sizeof(associd)) |
| 3937 | goto out; |
| 3938 | |
| 3939 | if (copy_from_user(&associd, optval, optlen)) { |
| 3940 | retval = -EFAULT; |
| 3941 | goto out; |
| 3942 | } |
| 3943 | |
| 3944 | asoc = sctp_id2assoc(sk, associd); |
| 3945 | if (!asoc) |
| 3946 | goto out; |
| 3947 | |
| 3948 | retval = sctp_send_reset_assoc(asoc); |
| 3949 | |
| 3950 | out: |
| 3951 | return retval; |
| 3952 | } |
| 3953 | |
Xin Long | 242bd2d | 2017-02-09 01:18:20 +0800 | [diff] [blame] | 3954 | static int sctp_setsockopt_add_streams(struct sock *sk, |
| 3955 | char __user *optval, |
| 3956 | unsigned int optlen) |
| 3957 | { |
| 3958 | struct sctp_association *asoc; |
| 3959 | struct sctp_add_streams params; |
| 3960 | int retval = -EINVAL; |
| 3961 | |
| 3962 | if (optlen != sizeof(params)) |
| 3963 | goto out; |
| 3964 | |
| 3965 | if (copy_from_user(¶ms, optval, optlen)) { |
| 3966 | retval = -EFAULT; |
| 3967 | goto out; |
| 3968 | } |
| 3969 | |
| 3970 | asoc = sctp_id2assoc(sk, params.sas_assoc_id); |
| 3971 | if (!asoc) |
| 3972 | goto out; |
| 3973 | |
| 3974 | retval = sctp_send_add_streams(asoc, ¶ms); |
| 3975 | |
| 3976 | out: |
| 3977 | return retval; |
| 3978 | } |
| 3979 | |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 3980 | static int sctp_setsockopt_scheduler(struct sock *sk, |
| 3981 | char __user *optval, |
| 3982 | unsigned int optlen) |
| 3983 | { |
| 3984 | struct sctp_association *asoc; |
| 3985 | struct sctp_assoc_value params; |
| 3986 | int retval = -EINVAL; |
| 3987 | |
| 3988 | if (optlen < sizeof(params)) |
| 3989 | goto out; |
| 3990 | |
| 3991 | optlen = sizeof(params); |
| 3992 | if (copy_from_user(¶ms, optval, optlen)) { |
| 3993 | retval = -EFAULT; |
| 3994 | goto out; |
| 3995 | } |
| 3996 | |
| 3997 | if (params.assoc_value > SCTP_SS_MAX) |
| 3998 | goto out; |
| 3999 | |
| 4000 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 4001 | if (!asoc) |
| 4002 | goto out; |
| 4003 | |
| 4004 | retval = sctp_sched_set_sched(asoc, params.assoc_value); |
| 4005 | |
| 4006 | out: |
| 4007 | return retval; |
| 4008 | } |
| 4009 | |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 4010 | static int sctp_setsockopt_scheduler_value(struct sock *sk, |
| 4011 | char __user *optval, |
| 4012 | unsigned int optlen) |
| 4013 | { |
| 4014 | struct sctp_association *asoc; |
| 4015 | struct sctp_stream_value params; |
| 4016 | int retval = -EINVAL; |
| 4017 | |
| 4018 | if (optlen < sizeof(params)) |
| 4019 | goto out; |
| 4020 | |
| 4021 | optlen = sizeof(params); |
| 4022 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4023 | retval = -EFAULT; |
| 4024 | goto out; |
| 4025 | } |
| 4026 | |
| 4027 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 4028 | if (!asoc) |
| 4029 | goto out; |
| 4030 | |
| 4031 | retval = sctp_sched_set_value(asoc, params.stream_id, |
| 4032 | params.stream_value, GFP_KERNEL); |
| 4033 | |
| 4034 | out: |
| 4035 | return retval; |
| 4036 | } |
| 4037 | |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4038 | static int sctp_setsockopt_interleaving_supported(struct sock *sk, |
| 4039 | char __user *optval, |
| 4040 | unsigned int optlen) |
| 4041 | { |
| 4042 | struct sctp_sock *sp = sctp_sk(sk); |
| 4043 | struct net *net = sock_net(sk); |
| 4044 | struct sctp_assoc_value params; |
| 4045 | int retval = -EINVAL; |
| 4046 | |
| 4047 | if (optlen < sizeof(params)) |
| 4048 | goto out; |
| 4049 | |
| 4050 | optlen = sizeof(params); |
| 4051 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4052 | retval = -EFAULT; |
| 4053 | goto out; |
| 4054 | } |
| 4055 | |
| 4056 | if (params.assoc_id) |
| 4057 | goto out; |
| 4058 | |
| 4059 | if (!net->sctp.intl_enable || !sp->frag_interleave) { |
| 4060 | retval = -EPERM; |
| 4061 | goto out; |
| 4062 | } |
| 4063 | |
| 4064 | sp->strm_interleave = !!params.assoc_value; |
| 4065 | |
| 4066 | retval = 0; |
| 4067 | |
| 4068 | out: |
| 4069 | return retval; |
| 4070 | } |
| 4071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | /* API 6.2 setsockopt(), getsockopt() |
| 4073 | * |
| 4074 | * Applications use setsockopt() and getsockopt() to set or retrieve |
| 4075 | * socket options. Socket options are used to change the default |
| 4076 | * behavior of sockets calls. They are described in Section 7. |
| 4077 | * |
| 4078 | * The syntax is: |
| 4079 | * |
| 4080 | * ret = getsockopt(int sd, int level, int optname, void __user *optval, |
| 4081 | * int __user *optlen); |
| 4082 | * ret = setsockopt(int sd, int level, int optname, const void __user *optval, |
| 4083 | * int optlen); |
| 4084 | * |
| 4085 | * sd - the socket descript. |
| 4086 | * level - set to IPPROTO_SCTP for all SCTP options. |
| 4087 | * optname - the option name. |
| 4088 | * optval - the buffer to store the value of the option. |
| 4089 | * optlen - the size of the buffer. |
| 4090 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4091 | static int sctp_setsockopt(struct sock *sk, int level, int optname, |
| 4092 | char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | { |
| 4094 | int retval = 0; |
| 4095 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4096 | pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | |
| 4098 | /* I can hardly begin to describe how wrong this is. This is |
| 4099 | * so broken as to be worse than useless. The API draft |
| 4100 | * REALLY is NOT helpful here... I am not convinced that the |
| 4101 | * semantics of setsockopt() with a level OTHER THAN SOL_SCTP |
| 4102 | * are at all well-founded. |
| 4103 | */ |
| 4104 | if (level != SOL_SCTP) { |
| 4105 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
| 4106 | retval = af->setsockopt(sk, level, optname, optval, optlen); |
| 4107 | goto out_nounlock; |
| 4108 | } |
| 4109 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4110 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | |
| 4112 | switch (optname) { |
| 4113 | case SCTP_SOCKOPT_BINDX_ADD: |
| 4114 | /* 'optlen' is the size of the addresses buffer. */ |
| 4115 | retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, |
| 4116 | optlen, SCTP_BINDX_ADD_ADDR); |
| 4117 | break; |
| 4118 | |
| 4119 | case SCTP_SOCKOPT_BINDX_REM: |
| 4120 | /* 'optlen' is the size of the addresses buffer. */ |
| 4121 | retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, |
| 4122 | optlen, SCTP_BINDX_REM_ADDR); |
| 4123 | break; |
| 4124 | |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 4125 | case SCTP_SOCKOPT_CONNECTX_OLD: |
| 4126 | /* 'optlen' is the size of the addresses buffer. */ |
| 4127 | retval = sctp_setsockopt_connectx_old(sk, |
| 4128 | (struct sockaddr __user *)optval, |
| 4129 | optlen); |
| 4130 | break; |
| 4131 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4132 | case SCTP_SOCKOPT_CONNECTX: |
| 4133 | /* 'optlen' is the size of the addresses buffer. */ |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 4134 | retval = sctp_setsockopt_connectx(sk, |
| 4135 | (struct sockaddr __user *)optval, |
| 4136 | optlen); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4137 | break; |
| 4138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | case SCTP_DISABLE_FRAGMENTS: |
| 4140 | retval = sctp_setsockopt_disable_fragments(sk, optval, optlen); |
| 4141 | break; |
| 4142 | |
| 4143 | case SCTP_EVENTS: |
| 4144 | retval = sctp_setsockopt_events(sk, optval, optlen); |
| 4145 | break; |
| 4146 | |
| 4147 | case SCTP_AUTOCLOSE: |
| 4148 | retval = sctp_setsockopt_autoclose(sk, optval, optlen); |
| 4149 | break; |
| 4150 | |
| 4151 | case SCTP_PEER_ADDR_PARAMS: |
| 4152 | retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen); |
| 4153 | break; |
| 4154 | |
Shan Wei | 4580ccc | 2011-01-18 22:39:00 +0000 | [diff] [blame] | 4155 | case SCTP_DELAYED_SACK: |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4156 | retval = sctp_setsockopt_delayed_ack(sk, optval, optlen); |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4157 | break; |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 4158 | case SCTP_PARTIAL_DELIVERY_POINT: |
| 4159 | retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen); |
| 4160 | break; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4162 | case SCTP_INITMSG: |
| 4163 | retval = sctp_setsockopt_initmsg(sk, optval, optlen); |
| 4164 | break; |
| 4165 | case SCTP_DEFAULT_SEND_PARAM: |
| 4166 | retval = sctp_setsockopt_default_send_param(sk, optval, |
| 4167 | optlen); |
| 4168 | break; |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 4169 | case SCTP_DEFAULT_SNDINFO: |
| 4170 | retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen); |
| 4171 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | case SCTP_PRIMARY_ADDR: |
| 4173 | retval = sctp_setsockopt_primary_addr(sk, optval, optlen); |
| 4174 | break; |
| 4175 | case SCTP_SET_PEER_PRIMARY_ADDR: |
| 4176 | retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen); |
| 4177 | break; |
| 4178 | case SCTP_NODELAY: |
| 4179 | retval = sctp_setsockopt_nodelay(sk, optval, optlen); |
| 4180 | break; |
| 4181 | case SCTP_RTOINFO: |
| 4182 | retval = sctp_setsockopt_rtoinfo(sk, optval, optlen); |
| 4183 | break; |
| 4184 | case SCTP_ASSOCINFO: |
| 4185 | retval = sctp_setsockopt_associnfo(sk, optval, optlen); |
| 4186 | break; |
| 4187 | case SCTP_I_WANT_MAPPED_V4_ADDR: |
| 4188 | retval = sctp_setsockopt_mappedv4(sk, optval, optlen); |
| 4189 | break; |
| 4190 | case SCTP_MAXSEG: |
| 4191 | retval = sctp_setsockopt_maxseg(sk, optval, optlen); |
| 4192 | break; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4193 | case SCTP_ADAPTATION_LAYER: |
| 4194 | retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | break; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 4196 | case SCTP_CONTEXT: |
| 4197 | retval = sctp_setsockopt_context(sk, optval, optlen); |
| 4198 | break; |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 4199 | case SCTP_FRAGMENT_INTERLEAVE: |
| 4200 | retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen); |
| 4201 | break; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 4202 | case SCTP_MAX_BURST: |
| 4203 | retval = sctp_setsockopt_maxburst(sk, optval, optlen); |
| 4204 | break; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 4205 | case SCTP_AUTH_CHUNK: |
| 4206 | retval = sctp_setsockopt_auth_chunk(sk, optval, optlen); |
| 4207 | break; |
| 4208 | case SCTP_HMAC_IDENT: |
| 4209 | retval = sctp_setsockopt_hmac_ident(sk, optval, optlen); |
| 4210 | break; |
| 4211 | case SCTP_AUTH_KEY: |
| 4212 | retval = sctp_setsockopt_auth_key(sk, optval, optlen); |
| 4213 | break; |
| 4214 | case SCTP_AUTH_ACTIVE_KEY: |
| 4215 | retval = sctp_setsockopt_active_key(sk, optval, optlen); |
| 4216 | break; |
| 4217 | case SCTP_AUTH_DELETE_KEY: |
| 4218 | retval = sctp_setsockopt_del_key(sk, optval, optlen); |
| 4219 | break; |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 4220 | case SCTP_AUTO_ASCONF: |
| 4221 | retval = sctp_setsockopt_auto_asconf(sk, optval, optlen); |
| 4222 | break; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 4223 | case SCTP_PEER_ADDR_THLDS: |
| 4224 | retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen); |
| 4225 | break; |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 4226 | case SCTP_RECVRCVINFO: |
| 4227 | retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen); |
| 4228 | break; |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 4229 | case SCTP_RECVNXTINFO: |
| 4230 | retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen); |
| 4231 | break; |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 4232 | case SCTP_PR_SUPPORTED: |
| 4233 | retval = sctp_setsockopt_pr_supported(sk, optval, optlen); |
| 4234 | break; |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 4235 | case SCTP_DEFAULT_PRINFO: |
| 4236 | retval = sctp_setsockopt_default_prinfo(sk, optval, optlen); |
| 4237 | break; |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 4238 | case SCTP_RECONFIG_SUPPORTED: |
| 4239 | retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen); |
| 4240 | break; |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 4241 | case SCTP_ENABLE_STREAM_RESET: |
| 4242 | retval = sctp_setsockopt_enable_strreset(sk, optval, optlen); |
| 4243 | break; |
Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4244 | case SCTP_RESET_STREAMS: |
| 4245 | retval = sctp_setsockopt_reset_streams(sk, optval, optlen); |
| 4246 | break; |
Xin Long | a92ce1a | 2017-02-09 01:18:18 +0800 | [diff] [blame] | 4247 | case SCTP_RESET_ASSOC: |
| 4248 | retval = sctp_setsockopt_reset_assoc(sk, optval, optlen); |
| 4249 | break; |
Xin Long | 242bd2d | 2017-02-09 01:18:20 +0800 | [diff] [blame] | 4250 | case SCTP_ADD_STREAMS: |
| 4251 | retval = sctp_setsockopt_add_streams(sk, optval, optlen); |
| 4252 | break; |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4253 | case SCTP_STREAM_SCHEDULER: |
| 4254 | retval = sctp_setsockopt_scheduler(sk, optval, optlen); |
| 4255 | break; |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 4256 | case SCTP_STREAM_SCHEDULER_VALUE: |
| 4257 | retval = sctp_setsockopt_scheduler_value(sk, optval, optlen); |
| 4258 | break; |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4259 | case SCTP_INTERLEAVING_SUPPORTED: |
| 4260 | retval = sctp_setsockopt_interleaving_supported(sk, optval, |
| 4261 | optlen); |
| 4262 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | default: |
| 4264 | retval = -ENOPROTOOPT; |
| 4265 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 4266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4268 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | |
| 4270 | out_nounlock: |
| 4271 | return retval; |
| 4272 | } |
| 4273 | |
| 4274 | /* API 3.1.6 connect() - UDP Style Syntax |
| 4275 | * |
| 4276 | * An application may use the connect() call in the UDP model to initiate an |
| 4277 | * association without sending data. |
| 4278 | * |
| 4279 | * The syntax is: |
| 4280 | * |
| 4281 | * ret = connect(int sd, const struct sockaddr *nam, socklen_t len); |
| 4282 | * |
| 4283 | * sd: the socket descriptor to have a new association added to. |
| 4284 | * |
| 4285 | * nam: the address structure (either struct sockaddr_in or struct |
| 4286 | * sockaddr_in6 defined in RFC2553 [7]). |
| 4287 | * |
| 4288 | * len: the size of the address. |
| 4289 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4290 | static int sctp_connect(struct sock *sk, struct sockaddr *addr, |
| 4291 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | int err = 0; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4294 | struct sctp_af *af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4296 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4298 | pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk, |
| 4299 | addr, addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4301 | /* Validate addr_len before calling common connect/connectx routine. */ |
| 4302 | af = sctp_get_af_specific(addr->sa_family); |
| 4303 | if (!af || addr_len < af->sockaddr_len) { |
| 4304 | err = -EINVAL; |
| 4305 | } else { |
| 4306 | /* Pass correct addr len to common routine (so it knows there |
| 4307 | * is only one address being passed. |
| 4308 | */ |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 4309 | err = __sctp_connect(sk, addr, af->sockaddr_len, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | } |
| 4311 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4312 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | return err; |
| 4314 | } |
| 4315 | |
| 4316 | /* FIXME: Write comments. */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4317 | static int sctp_disconnect(struct sock *sk, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | { |
| 4319 | return -EOPNOTSUPP; /* STUB */ |
| 4320 | } |
| 4321 | |
| 4322 | /* 4.1.4 accept() - TCP Style Syntax |
| 4323 | * |
| 4324 | * Applications use accept() call to remove an established SCTP |
| 4325 | * association from the accept queue of the endpoint. A new socket |
| 4326 | * descriptor will be returned from accept() to represent the newly |
| 4327 | * formed association. |
| 4328 | */ |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 4329 | static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | { |
| 4331 | struct sctp_sock *sp; |
| 4332 | struct sctp_endpoint *ep; |
| 4333 | struct sock *newsk = NULL; |
| 4334 | struct sctp_association *asoc; |
| 4335 | long timeo; |
| 4336 | int error = 0; |
| 4337 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4338 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | |
| 4340 | sp = sctp_sk(sk); |
| 4341 | ep = sp->ep; |
| 4342 | |
| 4343 | if (!sctp_style(sk, TCP)) { |
| 4344 | error = -EOPNOTSUPP; |
| 4345 | goto out; |
| 4346 | } |
| 4347 | |
| 4348 | if (!sctp_sstate(sk, LISTENING)) { |
| 4349 | error = -EINVAL; |
| 4350 | goto out; |
| 4351 | } |
| 4352 | |
Sridhar Samudrala | 8abfedd | 2006-08-22 00:24:09 -0700 | [diff] [blame] | 4353 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | |
| 4355 | error = sctp_wait_for_accept(sk, timeo); |
| 4356 | if (error) |
| 4357 | goto out; |
| 4358 | |
| 4359 | /* We treat the list of associations on the endpoint as the accept |
| 4360 | * queue and pick the first association on the list. |
| 4361 | */ |
| 4362 | asoc = list_entry(ep->asocs.next, struct sctp_association, asocs); |
| 4363 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 4364 | newsk = sp->pf->create_accept_sk(sk, asoc, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | if (!newsk) { |
| 4366 | error = -ENOMEM; |
| 4367 | goto out; |
| 4368 | } |
| 4369 | |
| 4370 | /* Populate the fields of the newsk from the oldsk and migrate the |
| 4371 | * asoc to the newsk. |
| 4372 | */ |
| 4373 | sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP); |
| 4374 | |
| 4375 | out: |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4376 | release_sock(sk); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4377 | *err = error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4378 | return newsk; |
| 4379 | } |
| 4380 | |
| 4381 | /* The SCTP ioctl handler. */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4382 | static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | { |
Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4384 | int rc = -ENOTCONN; |
| 4385 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4386 | lock_sock(sk); |
Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4387 | |
| 4388 | /* |
| 4389 | * SEQPACKET-style sockets in LISTENING state are valid, for |
| 4390 | * SCTP, so only discard TCP-style sockets in LISTENING state. |
| 4391 | */ |
| 4392 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) |
| 4393 | goto out; |
| 4394 | |
| 4395 | switch (cmd) { |
| 4396 | case SIOCINQ: { |
| 4397 | struct sk_buff *skb; |
| 4398 | unsigned int amount = 0; |
| 4399 | |
| 4400 | skb = skb_peek(&sk->sk_receive_queue); |
| 4401 | if (skb != NULL) { |
| 4402 | /* |
| 4403 | * We will only return the amount of this packet since |
| 4404 | * that is all that will be read. |
| 4405 | */ |
| 4406 | amount = skb->len; |
| 4407 | } |
| 4408 | rc = put_user(amount, (int __user *)arg); |
Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4409 | break; |
David S. Miller | 9a7241c | 2010-10-03 22:14:37 -0700 | [diff] [blame] | 4410 | } |
Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4411 | default: |
| 4412 | rc = -ENOIOCTLCMD; |
| 4413 | break; |
| 4414 | } |
| 4415 | out: |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4416 | release_sock(sk); |
Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4417 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | /* This is the function which gets called during socket creation to |
| 4421 | * initialized the SCTP-specific portion of the sock. |
| 4422 | * The sock structure should already be zero-filled memory. |
| 4423 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4424 | static int sctp_init_sock(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | { |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4426 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | struct sctp_sock *sp; |
| 4428 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4429 | pr_debug("%s: sk:%p\n", __func__, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | |
| 4431 | sp = sctp_sk(sk); |
| 4432 | |
| 4433 | /* Initialize the SCTP per socket area. */ |
| 4434 | switch (sk->sk_type) { |
| 4435 | case SOCK_SEQPACKET: |
| 4436 | sp->type = SCTP_SOCKET_UDP; |
| 4437 | break; |
| 4438 | case SOCK_STREAM: |
| 4439 | sp->type = SCTP_SOCKET_TCP; |
| 4440 | break; |
| 4441 | default: |
| 4442 | return -ESOCKTNOSUPPORT; |
| 4443 | } |
| 4444 | |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 4445 | sk->sk_gso_type = SKB_GSO_SCTP; |
| 4446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | /* Initialize default send parameters. These parameters can be |
| 4448 | * modified with the SCTP_DEFAULT_SEND_PARAM socket option. |
| 4449 | */ |
| 4450 | sp->default_stream = 0; |
| 4451 | sp->default_ppid = 0; |
| 4452 | sp->default_flags = 0; |
| 4453 | sp->default_context = 0; |
| 4454 | sp->default_timetolive = 0; |
| 4455 | |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 4456 | sp->default_rcv_context = 0; |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4457 | sp->max_burst = net->sctp.max_burst; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 4458 | |
Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 4459 | sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg; |
| 4460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | /* Initialize default setup parameters. These parameters |
| 4462 | * can be modified with the SCTP_INITMSG socket option or |
| 4463 | * overridden by the SCTP_INIT CMSG. |
| 4464 | */ |
| 4465 | sp->initmsg.sinit_num_ostreams = sctp_max_outstreams; |
| 4466 | sp->initmsg.sinit_max_instreams = sctp_max_instreams; |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4467 | sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init; |
| 4468 | sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | |
| 4470 | /* Initialize default RTO related parameters. These parameters can |
| 4471 | * be modified for with the SCTP_RTOINFO socket option. |
| 4472 | */ |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4473 | sp->rtoinfo.srto_initial = net->sctp.rto_initial; |
| 4474 | sp->rtoinfo.srto_max = net->sctp.rto_max; |
| 4475 | sp->rtoinfo.srto_min = net->sctp.rto_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | |
| 4477 | /* Initialize default association related parameters. These parameters |
| 4478 | * can be modified with the SCTP_ASSOCINFO socket option. |
| 4479 | */ |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4480 | sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | sp->assocparams.sasoc_number_peer_destinations = 0; |
| 4482 | sp->assocparams.sasoc_peer_rwnd = 0; |
| 4483 | sp->assocparams.sasoc_local_rwnd = 0; |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4484 | sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | |
| 4486 | /* Initialize default event subscriptions. By default, all the |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4487 | * options are off. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4488 | */ |
| 4489 | memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe)); |
| 4490 | |
| 4491 | /* Default Peer Address Parameters. These defaults can |
| 4492 | * be modified via SCTP_PEER_ADDR_PARAMS |
| 4493 | */ |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4494 | sp->hbinterval = net->sctp.hb_interval; |
| 4495 | sp->pathmaxrxt = net->sctp.max_retrans_path; |
wangweidong | 4e2d52b | 2013-12-23 12:16:54 +0800 | [diff] [blame] | 4496 | sp->pathmtu = 0; /* allow default discovery */ |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4497 | sp->sackdelay = net->sctp.sack_timeout; |
Vlad Yasevich | 7bfe8bdb | 2008-06-09 15:45:05 -0700 | [diff] [blame] | 4498 | sp->sackfreq = 2; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4499 | sp->param_flags = SPP_HB_ENABLE | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4500 | SPP_PMTUD_ENABLE | |
| 4501 | SPP_SACKDELAY_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | |
| 4503 | /* If enabled no SCTP message fragmentation will be performed. |
| 4504 | * Configure through SCTP_DISABLE_FRAGMENTS socket option. |
| 4505 | */ |
| 4506 | sp->disable_fragments = 0; |
| 4507 | |
Sridhar Samudrala | 208edef | 2006-09-29 17:08:01 -0700 | [diff] [blame] | 4508 | /* Enable Nagle algorithm by default. */ |
| 4509 | sp->nodelay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 4511 | sp->recvrcvinfo = 0; |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 4512 | sp->recvnxtinfo = 0; |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 4513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | /* Enable by default. */ |
| 4515 | sp->v4mapped = 1; |
| 4516 | |
| 4517 | /* Auto-close idle associations after the configured |
| 4518 | * number of seconds. A value of 0 disables this |
| 4519 | * feature. Configure through the SCTP_AUTOCLOSE socket option, |
| 4520 | * for UDP-style sockets only. |
| 4521 | */ |
| 4522 | sp->autoclose = 0; |
| 4523 | |
| 4524 | /* User specified fragmentation limit. */ |
| 4525 | sp->user_frag = 0; |
| 4526 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4527 | sp->adaptation_ind = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4528 | |
| 4529 | sp->pf = sctp_get_pf_specific(sk->sk_family); |
| 4530 | |
| 4531 | /* Control variables for partial data delivery. */ |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 4532 | atomic_set(&sp->pd_mode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4533 | skb_queue_head_init(&sp->pd_lobby); |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 4534 | sp->frag_interleave = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4535 | |
| 4536 | /* Create a per socket endpoint structure. Even if we |
| 4537 | * change the data structure relationships, this may still |
| 4538 | * be useful for storing pre-connect address information. |
| 4539 | */ |
Daniel Borkmann | c164b83 | 2013-06-14 18:24:06 +0200 | [diff] [blame] | 4540 | sp->ep = sctp_endpoint_new(sk, GFP_KERNEL); |
| 4541 | if (!sp->ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | return -ENOMEM; |
| 4543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | sp->hmac = NULL; |
| 4545 | |
Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 4546 | sk->sk_destruct = sctp_destruct_sock; |
| 4547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | SCTP_DBG_OBJCNT_INC(sock); |
David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 4549 | |
| 4550 | local_bh_disable(); |
Vlad Yasevich | 81419d8 | 2010-04-28 08:47:20 +0000 | [diff] [blame] | 4551 | percpu_counter_inc(&sctp_sockets_allocated); |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4552 | sock_prot_inuse_add(net, sk->sk_prot, 1); |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 4553 | |
| 4554 | /* Nothing can fail after this block, otherwise |
| 4555 | * sctp_destroy_sock() will be called without addr_wq_lock held |
| 4556 | */ |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4557 | if (net->sctp.default_auto_asconf) { |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 4558 | spin_lock(&sock_net(sk)->sctp.addr_wq_lock); |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4559 | list_add_tail(&sp->auto_asconf_list, |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 4560 | &net->sctp.auto_asconf_splist); |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4561 | sp->do_auto_asconf = 1; |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 4562 | spin_unlock(&sock_net(sk)->sctp.addr_wq_lock); |
| 4563 | } else { |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4564 | sp->do_auto_asconf = 0; |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 4565 | } |
| 4566 | |
David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 4567 | local_bh_enable(); |
| 4568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | return 0; |
| 4570 | } |
| 4571 | |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 4572 | /* Cleanup any SCTP per socket resources. Must be called with |
| 4573 | * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true |
| 4574 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4575 | static void sctp_destroy_sock(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | { |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4577 | struct sctp_sock *sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4579 | pr_debug("%s: sk:%p\n", __func__, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | |
| 4581 | /* Release our hold on the endpoint. */ |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4582 | sp = sctp_sk(sk); |
Daniel Borkmann | 1abd165 | 2013-06-06 15:53:47 +0200 | [diff] [blame] | 4583 | /* This could happen during socket init, thus we bail out |
| 4584 | * early, since the rest of the below is not setup either. |
| 4585 | */ |
| 4586 | if (sp->ep == NULL) |
| 4587 | return; |
| 4588 | |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 4589 | if (sp->do_auto_asconf) { |
| 4590 | sp->do_auto_asconf = 0; |
| 4591 | list_del(&sp->auto_asconf_list); |
| 4592 | } |
| 4593 | sctp_endpoint_free(sp->ep); |
Eric Dumazet | 5bc0b3b | 2008-11-25 13:53:27 -0800 | [diff] [blame] | 4594 | local_bh_disable(); |
Vlad Yasevich | 81419d8 | 2010-04-28 08:47:20 +0000 | [diff] [blame] | 4595 | percpu_counter_dec(&sctp_sockets_allocated); |
Eric Dumazet | 9a57f7f | 2008-11-17 02:41:00 -0800 | [diff] [blame] | 4596 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
Eric Dumazet | 5bc0b3b | 2008-11-25 13:53:27 -0800 | [diff] [blame] | 4597 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | } |
| 4599 | |
Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 4600 | /* Triggered when there are no references on the socket anymore */ |
| 4601 | static void sctp_destruct_sock(struct sock *sk) |
| 4602 | { |
| 4603 | struct sctp_sock *sp = sctp_sk(sk); |
| 4604 | |
| 4605 | /* Free up the HMAC transform. */ |
Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 4606 | crypto_free_shash(sp->hmac); |
Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 4607 | |
| 4608 | inet_sock_destruct(sk); |
| 4609 | } |
| 4610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | /* API 4.1.7 shutdown() - TCP Style Syntax |
| 4612 | * int shutdown(int socket, int how); |
| 4613 | * |
| 4614 | * sd - the socket descriptor of the association to be closed. |
| 4615 | * how - Specifies the type of shutdown. The values are |
| 4616 | * as follows: |
| 4617 | * SHUT_RD |
| 4618 | * Disables further receive operations. No SCTP |
| 4619 | * protocol action is taken. |
| 4620 | * SHUT_WR |
| 4621 | * Disables further send operations, and initiates |
| 4622 | * the SCTP shutdown sequence. |
| 4623 | * SHUT_RDWR |
| 4624 | * Disables further send and receive operations |
| 4625 | * and initiates the SCTP shutdown sequence. |
| 4626 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4627 | static void sctp_shutdown(struct sock *sk, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | { |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 4629 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | struct sctp_endpoint *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | |
| 4632 | if (!sctp_style(sk, TCP)) |
| 4633 | return; |
| 4634 | |
Xin Long | 5bf35dd | 2016-11-13 21:44:37 +0800 | [diff] [blame] | 4635 | ep = sctp_sk(sk)->ep; |
| 4636 | if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) { |
| 4637 | struct sctp_association *asoc; |
| 4638 | |
Xin Long | d46e416 | 2016-06-09 22:48:18 +0800 | [diff] [blame] | 4639 | sk->sk_state = SCTP_SS_CLOSING; |
Xin Long | 5bf35dd | 2016-11-13 21:44:37 +0800 | [diff] [blame] | 4640 | asoc = list_entry(ep->asocs.next, |
| 4641 | struct sctp_association, asocs); |
| 4642 | sctp_primitive_SHUTDOWN(net, asoc, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | } |
| 4644 | } |
| 4645 | |
Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 4646 | int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc, |
| 4647 | struct sctp_info *info) |
| 4648 | { |
| 4649 | struct sctp_transport *prim; |
| 4650 | struct list_head *pos; |
| 4651 | int mask; |
| 4652 | |
| 4653 | memset(info, 0, sizeof(*info)); |
| 4654 | if (!asoc) { |
| 4655 | struct sctp_sock *sp = sctp_sk(sk); |
| 4656 | |
| 4657 | info->sctpi_s_autoclose = sp->autoclose; |
| 4658 | info->sctpi_s_adaptation_ind = sp->adaptation_ind; |
| 4659 | info->sctpi_s_pd_point = sp->pd_point; |
| 4660 | info->sctpi_s_nodelay = sp->nodelay; |
| 4661 | info->sctpi_s_disable_fragments = sp->disable_fragments; |
| 4662 | info->sctpi_s_v4mapped = sp->v4mapped; |
| 4663 | info->sctpi_s_frag_interleave = sp->frag_interleave; |
Xin Long | 40eb90e9 | 2016-05-29 17:42:13 +0800 | [diff] [blame] | 4664 | info->sctpi_s_type = sp->type; |
Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 4665 | |
| 4666 | return 0; |
| 4667 | } |
| 4668 | |
| 4669 | info->sctpi_tag = asoc->c.my_vtag; |
| 4670 | info->sctpi_state = asoc->state; |
| 4671 | info->sctpi_rwnd = asoc->a_rwnd; |
| 4672 | info->sctpi_unackdata = asoc->unack_data; |
| 4673 | info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); |
Xin Long | cee360a | 2017-05-31 16:36:31 +0800 | [diff] [blame] | 4674 | info->sctpi_instrms = asoc->stream.incnt; |
| 4675 | info->sctpi_outstrms = asoc->stream.outcnt; |
Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 4676 | list_for_each(pos, &asoc->base.inqueue.in_chunk_list) |
| 4677 | info->sctpi_inqueue++; |
| 4678 | list_for_each(pos, &asoc->outqueue.out_chunk_list) |
| 4679 | info->sctpi_outqueue++; |
| 4680 | info->sctpi_overall_error = asoc->overall_error_count; |
| 4681 | info->sctpi_max_burst = asoc->max_burst; |
| 4682 | info->sctpi_maxseg = asoc->frag_point; |
| 4683 | info->sctpi_peer_rwnd = asoc->peer.rwnd; |
| 4684 | info->sctpi_peer_tag = asoc->c.peer_vtag; |
| 4685 | |
| 4686 | mask = asoc->peer.ecn_capable << 1; |
| 4687 | mask = (mask | asoc->peer.ipv4_address) << 1; |
| 4688 | mask = (mask | asoc->peer.ipv6_address) << 1; |
| 4689 | mask = (mask | asoc->peer.hostname_address) << 1; |
| 4690 | mask = (mask | asoc->peer.asconf_capable) << 1; |
| 4691 | mask = (mask | asoc->peer.prsctp_capable) << 1; |
| 4692 | mask = (mask | asoc->peer.auth_capable); |
| 4693 | info->sctpi_peer_capable = mask; |
| 4694 | mask = asoc->peer.sack_needed << 1; |
| 4695 | mask = (mask | asoc->peer.sack_generation) << 1; |
| 4696 | mask = (mask | asoc->peer.zero_window_announced); |
| 4697 | info->sctpi_peer_sack = mask; |
| 4698 | |
| 4699 | info->sctpi_isacks = asoc->stats.isacks; |
| 4700 | info->sctpi_osacks = asoc->stats.osacks; |
| 4701 | info->sctpi_opackets = asoc->stats.opackets; |
| 4702 | info->sctpi_ipackets = asoc->stats.ipackets; |
| 4703 | info->sctpi_rtxchunks = asoc->stats.rtxchunks; |
| 4704 | info->sctpi_outofseqtsns = asoc->stats.outofseqtsns; |
| 4705 | info->sctpi_idupchunks = asoc->stats.idupchunks; |
| 4706 | info->sctpi_gapcnt = asoc->stats.gapcnt; |
| 4707 | info->sctpi_ouodchunks = asoc->stats.ouodchunks; |
| 4708 | info->sctpi_iuodchunks = asoc->stats.iuodchunks; |
| 4709 | info->sctpi_oodchunks = asoc->stats.oodchunks; |
| 4710 | info->sctpi_iodchunks = asoc->stats.iodchunks; |
| 4711 | info->sctpi_octrlchunks = asoc->stats.octrlchunks; |
| 4712 | info->sctpi_ictrlchunks = asoc->stats.ictrlchunks; |
| 4713 | |
| 4714 | prim = asoc->peer.primary_path; |
Stefano Brivio | ee6c88b | 2017-08-23 13:27:13 +0200 | [diff] [blame] | 4715 | memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr)); |
Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 4716 | info->sctpi_p_state = prim->state; |
| 4717 | info->sctpi_p_cwnd = prim->cwnd; |
| 4718 | info->sctpi_p_srtt = prim->srtt; |
| 4719 | info->sctpi_p_rto = jiffies_to_msecs(prim->rto); |
| 4720 | info->sctpi_p_hbinterval = prim->hbinterval; |
| 4721 | info->sctpi_p_pathmaxrxt = prim->pathmaxrxt; |
| 4722 | info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay); |
| 4723 | info->sctpi_p_ssthresh = prim->ssthresh; |
| 4724 | info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked; |
| 4725 | info->sctpi_p_flight_size = prim->flight_size; |
| 4726 | info->sctpi_p_error = prim->error_count; |
| 4727 | |
| 4728 | return 0; |
| 4729 | } |
| 4730 | EXPORT_SYMBOL_GPL(sctp_get_sctp_info); |
| 4731 | |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4732 | /* use callback to avoid exporting the core structure */ |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 4733 | void sctp_transport_walk_start(struct rhashtable_iter *iter) |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4734 | { |
Xin Long | 7fda702 | 2016-11-15 23:23:11 +0800 | [diff] [blame] | 4735 | rhltable_walk_enter(&sctp_transport_hashtable, iter); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4736 | |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 4737 | rhashtable_walk_start(iter); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | void sctp_transport_walk_stop(struct rhashtable_iter *iter) |
| 4741 | { |
| 4742 | rhashtable_walk_stop(iter); |
| 4743 | rhashtable_walk_exit(iter); |
| 4744 | } |
| 4745 | |
| 4746 | struct sctp_transport *sctp_transport_get_next(struct net *net, |
| 4747 | struct rhashtable_iter *iter) |
| 4748 | { |
| 4749 | struct sctp_transport *t; |
| 4750 | |
| 4751 | t = rhashtable_walk_next(iter); |
| 4752 | for (; t; t = rhashtable_walk_next(iter)) { |
| 4753 | if (IS_ERR(t)) { |
| 4754 | if (PTR_ERR(t) == -EAGAIN) |
| 4755 | continue; |
| 4756 | break; |
| 4757 | } |
| 4758 | |
| 4759 | if (net_eq(sock_net(t->asoc->base.sk), net) && |
| 4760 | t->asoc->peer.primary_path == t) |
| 4761 | break; |
| 4762 | } |
| 4763 | |
| 4764 | return t; |
| 4765 | } |
| 4766 | |
| 4767 | struct sctp_transport *sctp_transport_get_idx(struct net *net, |
| 4768 | struct rhashtable_iter *iter, |
| 4769 | int pos) |
| 4770 | { |
| 4771 | void *obj = SEQ_START_TOKEN; |
| 4772 | |
| 4773 | while (pos && (obj = sctp_transport_get_next(net, iter)) && |
| 4774 | !IS_ERR(obj)) |
| 4775 | pos--; |
| 4776 | |
| 4777 | return obj; |
| 4778 | } |
| 4779 | |
| 4780 | int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *), |
| 4781 | void *p) { |
| 4782 | int err = 0; |
| 4783 | int hash = 0; |
| 4784 | struct sctp_ep_common *epb; |
| 4785 | struct sctp_hashbucket *head; |
| 4786 | |
| 4787 | for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize; |
| 4788 | hash++, head++) { |
Xin Long | 581409d | 2017-06-10 14:48:14 +0800 | [diff] [blame] | 4789 | read_lock_bh(&head->lock); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4790 | sctp_for_each_hentry(epb, &head->chain) { |
| 4791 | err = cb(sctp_ep(epb), p); |
| 4792 | if (err) |
| 4793 | break; |
| 4794 | } |
Xin Long | 581409d | 2017-06-10 14:48:14 +0800 | [diff] [blame] | 4795 | read_unlock_bh(&head->lock); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | return err; |
| 4799 | } |
| 4800 | EXPORT_SYMBOL_GPL(sctp_for_each_endpoint); |
| 4801 | |
| 4802 | int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *), |
| 4803 | struct net *net, |
| 4804 | const union sctp_addr *laddr, |
| 4805 | const union sctp_addr *paddr, void *p) |
| 4806 | { |
| 4807 | struct sctp_transport *transport; |
Xin Long | 08abb79 | 2016-12-15 23:05:52 +0800 | [diff] [blame] | 4808 | int err; |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4809 | |
| 4810 | rcu_read_lock(); |
| 4811 | transport = sctp_addrs_lookup_transport(net, laddr, paddr); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4812 | rcu_read_unlock(); |
Xin Long | 08abb79 | 2016-12-15 23:05:52 +0800 | [diff] [blame] | 4813 | if (!transport) |
| 4814 | return -ENOENT; |
| 4815 | |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 4816 | err = cb(transport, p); |
Xin Long | cd26da4 | 2016-10-31 20:32:31 +0800 | [diff] [blame] | 4817 | sctp_transport_put(transport); |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 4818 | |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4819 | return err; |
| 4820 | } |
| 4821 | EXPORT_SYMBOL_GPL(sctp_transport_lookup_process); |
| 4822 | |
| 4823 | int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *), |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 4824 | int (*cb_done)(struct sctp_transport *, void *), |
| 4825 | struct net *net, int *pos, void *p) { |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4826 | struct rhashtable_iter hti; |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 4827 | struct sctp_transport *tsp; |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 4828 | int ret = 0; |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4829 | |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 4830 | again: |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 4831 | sctp_transport_walk_start(&hti); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4832 | |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 4833 | tsp = sctp_transport_get_idx(net, &hti, *pos + 1); |
| 4834 | for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) { |
| 4835 | if (!sctp_transport_hold(tsp)) |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4836 | continue; |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 4837 | ret = cb(tsp, p); |
| 4838 | if (ret) |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4839 | break; |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 4840 | (*pos)++; |
| 4841 | sctp_transport_put(tsp); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4842 | } |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4843 | sctp_transport_walk_stop(&hti); |
Xin Long | 53fa103 | 2016-04-14 15:35:35 +0800 | [diff] [blame] | 4844 | |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 4845 | if (ret) { |
| 4846 | if (cb_done && !cb_done(tsp, p)) { |
| 4847 | (*pos)++; |
| 4848 | sctp_transport_put(tsp); |
| 4849 | goto again; |
| 4850 | } |
| 4851 | sctp_transport_put(tsp); |
| 4852 | } |
| 4853 | |
| 4854 | return ret; |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 4855 | } |
| 4856 | EXPORT_SYMBOL_GPL(sctp_for_each_transport); |
| 4857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | /* 7.2.1 Association Status (SCTP_STATUS) |
| 4859 | |
| 4860 | * Applications can retrieve current status information about an |
| 4861 | * association, including association state, peer receiver window size, |
| 4862 | * number of unacked data chunks, and number of data chunks pending |
| 4863 | * receipt. This information is read-only. |
| 4864 | */ |
| 4865 | static int sctp_getsockopt_sctp_status(struct sock *sk, int len, |
| 4866 | char __user *optval, |
| 4867 | int __user *optlen) |
| 4868 | { |
| 4869 | struct sctp_status status; |
| 4870 | struct sctp_association *asoc = NULL; |
| 4871 | struct sctp_transport *transport; |
| 4872 | sctp_assoc_t associd; |
| 4873 | int retval = 0; |
| 4874 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4875 | if (len < sizeof(status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | retval = -EINVAL; |
| 4877 | goto out; |
| 4878 | } |
| 4879 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4880 | len = sizeof(status); |
| 4881 | if (copy_from_user(&status, optval, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | retval = -EFAULT; |
| 4883 | goto out; |
| 4884 | } |
| 4885 | |
| 4886 | associd = status.sstat_assoc_id; |
| 4887 | asoc = sctp_id2assoc(sk, associd); |
| 4888 | if (!asoc) { |
| 4889 | retval = -EINVAL; |
| 4890 | goto out; |
| 4891 | } |
| 4892 | |
| 4893 | transport = asoc->peer.primary_path; |
| 4894 | |
| 4895 | status.sstat_assoc_id = sctp_assoc2id(asoc); |
Daniel Borkmann | 38ab1fa | 2014-08-28 15:28:26 +0200 | [diff] [blame] | 4896 | status.sstat_state = sctp_assoc_to_state(asoc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4897 | status.sstat_rwnd = asoc->peer.rwnd; |
| 4898 | status.sstat_unackdata = asoc->unack_data; |
| 4899 | |
| 4900 | status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); |
Xin Long | cee360a | 2017-05-31 16:36:31 +0800 | [diff] [blame] | 4901 | status.sstat_instrms = asoc->stream.incnt; |
| 4902 | status.sstat_outstrms = asoc->stream.outcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | status.sstat_fragmentation_point = asoc->frag_point; |
| 4904 | status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); |
Al Viro | 8cec6b8 | 2006-11-20 17:23:01 -0800 | [diff] [blame] | 4905 | memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, |
| 4906 | transport->af_specific->sockaddr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | /* Map ipv4 address into v4-mapped-on-v6 address. */ |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 4908 | sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | (union sctp_addr *)&status.sstat_primary.spinfo_address); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4910 | status.sstat_primary.spinfo_state = transport->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | status.sstat_primary.spinfo_cwnd = transport->cwnd; |
| 4912 | status.sstat_primary.spinfo_srtt = transport->srtt; |
| 4913 | status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4914 | status.sstat_primary.spinfo_mtu = transport->pathmtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4916 | if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) |
| 4917 | status.sstat_primary.spinfo_state = SCTP_ACTIVE; |
| 4918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | if (put_user(len, optlen)) { |
| 4920 | retval = -EFAULT; |
| 4921 | goto out; |
| 4922 | } |
| 4923 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4924 | pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n", |
| 4925 | __func__, len, status.sstat_state, status.sstat_rwnd, |
| 4926 | status.sstat_assoc_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | |
| 4928 | if (copy_to_user(optval, &status, len)) { |
| 4929 | retval = -EFAULT; |
| 4930 | goto out; |
| 4931 | } |
| 4932 | |
| 4933 | out: |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 4934 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | |
| 4938 | /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) |
| 4939 | * |
| 4940 | * Applications can retrieve information about a specific peer address |
| 4941 | * of an association, including its reachability state, congestion |
| 4942 | * window, and retransmission timer values. This information is |
| 4943 | * read-only. |
| 4944 | */ |
| 4945 | static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, |
| 4946 | char __user *optval, |
| 4947 | int __user *optlen) |
| 4948 | { |
| 4949 | struct sctp_paddrinfo pinfo; |
| 4950 | struct sctp_transport *transport; |
| 4951 | int retval = 0; |
| 4952 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4953 | if (len < sizeof(pinfo)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | retval = -EINVAL; |
| 4955 | goto out; |
| 4956 | } |
| 4957 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 4958 | len = sizeof(pinfo); |
| 4959 | if (copy_from_user(&pinfo, optval, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | retval = -EFAULT; |
| 4961 | goto out; |
| 4962 | } |
| 4963 | |
| 4964 | transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, |
| 4965 | pinfo.spinfo_assoc_id); |
| 4966 | if (!transport) |
| 4967 | return -EINVAL; |
| 4968 | |
| 4969 | pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4970 | pinfo.spinfo_state = transport->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | pinfo.spinfo_cwnd = transport->cwnd; |
| 4972 | pinfo.spinfo_srtt = transport->srtt; |
| 4973 | pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 4974 | pinfo.spinfo_mtu = transport->pathmtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4976 | if (pinfo.spinfo_state == SCTP_UNKNOWN) |
| 4977 | pinfo.spinfo_state = SCTP_ACTIVE; |
| 4978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | if (put_user(len, optlen)) { |
| 4980 | retval = -EFAULT; |
| 4981 | goto out; |
| 4982 | } |
| 4983 | |
| 4984 | if (copy_to_user(optval, &pinfo, len)) { |
| 4985 | retval = -EFAULT; |
| 4986 | goto out; |
| 4987 | } |
| 4988 | |
| 4989 | out: |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 4990 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) |
| 4994 | * |
| 4995 | * This option is a on/off flag. If enabled no SCTP message |
| 4996 | * fragmentation will be performed. Instead if a message being sent |
| 4997 | * exceeds the current PMTU size, the message will NOT be sent and |
| 4998 | * instead a error will be indicated to the user. |
| 4999 | */ |
| 5000 | static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, |
| 5001 | char __user *optval, int __user *optlen) |
| 5002 | { |
| 5003 | int val; |
| 5004 | |
| 5005 | if (len < sizeof(int)) |
| 5006 | return -EINVAL; |
| 5007 | |
| 5008 | len = sizeof(int); |
| 5009 | val = (sctp_sk(sk)->disable_fragments == 1); |
| 5010 | if (put_user(len, optlen)) |
| 5011 | return -EFAULT; |
| 5012 | if (copy_to_user(optval, &val, len)) |
| 5013 | return -EFAULT; |
| 5014 | return 0; |
| 5015 | } |
| 5016 | |
| 5017 | /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) |
| 5018 | * |
| 5019 | * This socket option is used to specify various notifications and |
| 5020 | * ancillary data the user wishes to receive. |
| 5021 | */ |
| 5022 | static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, |
| 5023 | int __user *optlen) |
| 5024 | { |
Jiri Slaby | a4b8e71 | 2016-10-21 14:13:24 +0200 | [diff] [blame] | 5025 | if (len == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | return -EINVAL; |
Thomas Graf | acdd598 | 2012-04-03 22:17:53 +0000 | [diff] [blame] | 5027 | if (len > sizeof(struct sctp_event_subscribe)) |
| 5028 | len = sizeof(struct sctp_event_subscribe); |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5029 | if (put_user(len, optlen)) |
| 5030 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len)) |
| 5032 | return -EFAULT; |
| 5033 | return 0; |
| 5034 | } |
| 5035 | |
| 5036 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) |
| 5037 | * |
| 5038 | * This socket option is applicable to the UDP-style socket only. When |
| 5039 | * set it will cause associations that are idle for more than the |
| 5040 | * specified number of seconds to automatically close. An association |
| 5041 | * being idle is defined an association that has NOT sent or received |
| 5042 | * user data. The special value of '0' indicates that no automatic |
| 5043 | * close of any associations should be performed. The option expects an |
| 5044 | * integer defining the number of seconds of idle time before an |
| 5045 | * association is closed. |
| 5046 | */ |
| 5047 | static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) |
| 5048 | { |
| 5049 | /* Applicable to UDP-style socket only */ |
| 5050 | if (sctp_style(sk, TCP)) |
| 5051 | return -EOPNOTSUPP; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5052 | if (len < sizeof(int)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | return -EINVAL; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5054 | len = sizeof(int); |
| 5055 | if (put_user(len, optlen)) |
| 5056 | return -EFAULT; |
| 5057 | if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5058 | return -EFAULT; |
| 5059 | return 0; |
| 5060 | } |
| 5061 | |
| 5062 | /* Helper routine to branch off an association to a new socket. */ |
Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5063 | int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | { |
Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5065 | struct sctp_association *asoc = sctp_id2assoc(sk, id); |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5066 | struct sctp_sock *sp = sctp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | struct socket *sock; |
| 5068 | int err = 0; |
| 5069 | |
Xin Long | df80cd9 | 2017-10-17 23:26:10 +0800 | [diff] [blame] | 5070 | /* Do not peel off from one netns to another one. */ |
| 5071 | if (!net_eq(current->nsproxy->net_ns, sock_net(sk))) |
| 5072 | return -EINVAL; |
| 5073 | |
Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5074 | if (!asoc) |
| 5075 | return -EINVAL; |
| 5076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | /* An association cannot be branched off from an already peeled-off |
| 5078 | * socket, nor is this supported for tcp style sockets. |
| 5079 | */ |
| 5080 | if (!sctp_style(sk, UDP)) |
| 5081 | return -EINVAL; |
| 5082 | |
| 5083 | /* Create a new socket. */ |
| 5084 | err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); |
| 5085 | if (err < 0) |
| 5086 | return err; |
| 5087 | |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 5088 | sctp_copy_sock(sock->sk, sk, asoc); |
Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 5089 | |
| 5090 | /* Make peeled-off sockets more like 1-1 accepted sockets. |
| 5091 | * Set the daddr and initialize id to something more random |
| 5092 | */ |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5093 | sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk); |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 5094 | |
| 5095 | /* Populate the fields of the newsk from the oldsk and migrate the |
| 5096 | * asoc to the newsk. |
| 5097 | */ |
| 5098 | sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH); |
Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 5099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | *sockp = sock; |
| 5101 | |
| 5102 | return err; |
| 5103 | } |
Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5104 | EXPORT_SYMBOL(sctp_do_peeloff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5106 | static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff, |
| 5107 | struct file **newfile, unsigned flags) |
| 5108 | { |
| 5109 | struct socket *newsock; |
| 5110 | int retval; |
| 5111 | |
| 5112 | retval = sctp_do_peeloff(sk, peeloff->associd, &newsock); |
| 5113 | if (retval < 0) |
| 5114 | goto out; |
| 5115 | |
| 5116 | /* Map the socket to an unused fd that can be returned to the user. */ |
| 5117 | retval = get_unused_fd_flags(flags & SOCK_CLOEXEC); |
| 5118 | if (retval < 0) { |
| 5119 | sock_release(newsock); |
| 5120 | goto out; |
| 5121 | } |
| 5122 | |
| 5123 | *newfile = sock_alloc_file(newsock, 0, NULL); |
| 5124 | if (IS_ERR(*newfile)) { |
| 5125 | put_unused_fd(retval); |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5126 | retval = PTR_ERR(*newfile); |
| 5127 | *newfile = NULL; |
| 5128 | return retval; |
| 5129 | } |
| 5130 | |
| 5131 | pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk, |
| 5132 | retval); |
| 5133 | |
| 5134 | peeloff->sd = retval; |
| 5135 | |
| 5136 | if (flags & SOCK_NONBLOCK) |
| 5137 | (*newfile)->f_flags |= O_NONBLOCK; |
| 5138 | out: |
| 5139 | return retval; |
| 5140 | } |
| 5141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) |
| 5143 | { |
| 5144 | sctp_peeloff_arg_t peeloff; |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5145 | struct file *newfile = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5148 | if (len < sizeof(sctp_peeloff_arg_t)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | return -EINVAL; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5150 | len = sizeof(sctp_peeloff_arg_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | if (copy_from_user(&peeloff, optval, len)) |
| 5152 | return -EFAULT; |
| 5153 | |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5154 | retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | if (retval < 0) |
| 5156 | goto out; |
| 5157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | /* Return the fd mapped to the new socket. */ |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 5159 | if (put_user(len, optlen)) { |
| 5160 | fput(newfile); |
| 5161 | put_unused_fd(retval); |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5162 | return -EFAULT; |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 5163 | } |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5164 | |
| 5165 | if (copy_to_user(optval, &peeloff, len)) { |
| 5166 | fput(newfile); |
| 5167 | put_unused_fd(retval); |
| 5168 | return -EFAULT; |
| 5169 | } |
| 5170 | fd_install(retval, newfile); |
| 5171 | out: |
| 5172 | return retval; |
| 5173 | } |
| 5174 | |
| 5175 | static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len, |
| 5176 | char __user *optval, int __user *optlen) |
| 5177 | { |
| 5178 | sctp_peeloff_flags_arg_t peeloff; |
| 5179 | struct file *newfile = NULL; |
| 5180 | int retval = 0; |
| 5181 | |
| 5182 | if (len < sizeof(sctp_peeloff_flags_arg_t)) |
| 5183 | return -EINVAL; |
| 5184 | len = sizeof(sctp_peeloff_flags_arg_t); |
| 5185 | if (copy_from_user(&peeloff, optval, len)) |
| 5186 | return -EFAULT; |
| 5187 | |
| 5188 | retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg, |
| 5189 | &newfile, peeloff.flags); |
| 5190 | if (retval < 0) |
| 5191 | goto out; |
| 5192 | |
| 5193 | /* Return the fd mapped to the new socket. */ |
| 5194 | if (put_user(len, optlen)) { |
| 5195 | fput(newfile); |
| 5196 | put_unused_fd(retval); |
| 5197 | return -EFAULT; |
| 5198 | } |
| 5199 | |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 5200 | if (copy_to_user(optval, &peeloff, len)) { |
| 5201 | fput(newfile); |
| 5202 | put_unused_fd(retval); |
| 5203 | return -EFAULT; |
| 5204 | } |
| 5205 | fd_install(retval, newfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5206 | out: |
| 5207 | return retval; |
| 5208 | } |
| 5209 | |
| 5210 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) |
| 5211 | * |
| 5212 | * Applications can enable or disable heartbeats for any peer address of |
| 5213 | * an association, modify an address's heartbeat interval, force a |
| 5214 | * heartbeat to be sent immediately, and adjust the address's maximum |
| 5215 | * number of retransmissions sent before an address is considered |
| 5216 | * unreachable. The following structure is used to access and modify an |
| 5217 | * address's parameters: |
| 5218 | * |
| 5219 | * struct sctp_paddrparams { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5220 | * sctp_assoc_t spp_assoc_id; |
| 5221 | * struct sockaddr_storage spp_address; |
| 5222 | * uint32_t spp_hbinterval; |
| 5223 | * uint16_t spp_pathmaxrxt; |
| 5224 | * uint32_t spp_pathmtu; |
| 5225 | * uint32_t spp_sackdelay; |
| 5226 | * uint32_t spp_flags; |
| 5227 | * }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | * |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5229 | * spp_assoc_id - (one-to-many style socket) This is filled in the |
| 5230 | * application, and identifies the association for |
| 5231 | * this query. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | * spp_address - This specifies which address is of interest. |
| 5233 | * spp_hbinterval - This contains the value of the heartbeat interval, |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5234 | * in milliseconds. If a value of zero |
| 5235 | * is present in this field then no changes are to |
| 5236 | * be made to this parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | * spp_pathmaxrxt - This contains the maximum number of |
| 5238 | * retransmissions before this address shall be |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5239 | * considered unreachable. If a value of zero |
| 5240 | * is present in this field then no changes are to |
| 5241 | * be made to this parameter. |
| 5242 | * spp_pathmtu - When Path MTU discovery is disabled the value |
| 5243 | * specified here will be the "fixed" path mtu. |
| 5244 | * Note that if the spp_address field is empty |
| 5245 | * then all associations on this address will |
| 5246 | * have this fixed path mtu set upon them. |
| 5247 | * |
| 5248 | * spp_sackdelay - When delayed sack is enabled, this value specifies |
| 5249 | * the number of milliseconds that sacks will be delayed |
| 5250 | * for. This value will apply to all addresses of an |
| 5251 | * association if the spp_address field is empty. Note |
| 5252 | * also, that if delayed sack is enabled and this |
| 5253 | * value is set to 0, no change is made to the last |
| 5254 | * recorded delayed sack timer value. |
| 5255 | * |
| 5256 | * spp_flags - These flags are used to control various features |
| 5257 | * on an association. The flag field may contain |
| 5258 | * zero or more of the following options. |
| 5259 | * |
| 5260 | * SPP_HB_ENABLE - Enable heartbeats on the |
| 5261 | * specified address. Note that if the address |
| 5262 | * field is empty all addresses for the association |
| 5263 | * have heartbeats enabled upon them. |
| 5264 | * |
| 5265 | * SPP_HB_DISABLE - Disable heartbeats on the |
| 5266 | * speicifed address. Note that if the address |
| 5267 | * field is empty all addresses for the association |
| 5268 | * will have their heartbeats disabled. Note also |
| 5269 | * that SPP_HB_ENABLE and SPP_HB_DISABLE are |
| 5270 | * mutually exclusive, only one of these two should |
| 5271 | * be specified. Enabling both fields will have |
| 5272 | * undetermined results. |
| 5273 | * |
| 5274 | * SPP_HB_DEMAND - Request a user initiated heartbeat |
| 5275 | * to be made immediately. |
| 5276 | * |
| 5277 | * SPP_PMTUD_ENABLE - This field will enable PMTU |
| 5278 | * discovery upon the specified address. Note that |
| 5279 | * if the address feild is empty then all addresses |
| 5280 | * on the association are effected. |
| 5281 | * |
| 5282 | * SPP_PMTUD_DISABLE - This field will disable PMTU |
| 5283 | * discovery upon the specified address. Note that |
| 5284 | * if the address feild is empty then all addresses |
| 5285 | * on the association are effected. Not also that |
| 5286 | * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually |
| 5287 | * exclusive. Enabling both will have undetermined |
| 5288 | * results. |
| 5289 | * |
| 5290 | * SPP_SACKDELAY_ENABLE - Setting this flag turns |
| 5291 | * on delayed sack. The time specified in spp_sackdelay |
| 5292 | * is used to specify the sack delay for this address. Note |
| 5293 | * that if spp_address is empty then all addresses will |
| 5294 | * enable delayed sack and take on the sack delay |
| 5295 | * value specified in spp_sackdelay. |
| 5296 | * SPP_SACKDELAY_DISABLE - Setting this flag turns |
| 5297 | * off delayed sack. If the spp_address field is blank then |
| 5298 | * delayed sack is disabled for the entire association. Note |
| 5299 | * also that this field is mutually exclusive to |
| 5300 | * SPP_SACKDELAY_ENABLE, setting both will have undefined |
| 5301 | * results. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | */ |
| 5303 | static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5304 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5306 | struct sctp_paddrparams params; |
| 5307 | struct sctp_transport *trans = NULL; |
| 5308 | struct sctp_association *asoc = NULL; |
| 5309 | struct sctp_sock *sp = sctp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5311 | if (len < sizeof(struct sctp_paddrparams)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | return -EINVAL; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5313 | len = sizeof(struct sctp_paddrparams); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | if (copy_from_user(¶ms, optval, len)) |
| 5315 | return -EFAULT; |
| 5316 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5317 | /* If an address other than INADDR_ANY is specified, and |
| 5318 | * no transport is found, then the request is invalid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | */ |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5320 | if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5321 | trans = sctp_addr_id2transport(sk, ¶ms.spp_address, |
| 5322 | params.spp_assoc_id); |
| 5323 | if (!trans) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 5324 | pr_debug("%s: failed no transport\n", __func__); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5325 | return -EINVAL; |
| 5326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | } |
| 5328 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5329 | /* Get association, if assoc_id != 0 and the socket is a one |
| 5330 | * to many style socket, and an association was not found, then |
| 5331 | * the id was invalid. |
| 5332 | */ |
| 5333 | asoc = sctp_id2assoc(sk, params.spp_assoc_id); |
| 5334 | if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 5335 | pr_debug("%s: failed no association\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | return -EINVAL; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5339 | if (trans) { |
| 5340 | /* Fetch transport values. */ |
| 5341 | params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); |
| 5342 | params.spp_pathmtu = trans->pathmtu; |
| 5343 | params.spp_pathmaxrxt = trans->pathmaxrxt; |
| 5344 | params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5346 | /*draft-11 doesn't say what to return in spp_flags*/ |
| 5347 | params.spp_flags = trans->param_flags; |
| 5348 | } else if (asoc) { |
| 5349 | /* Fetch association values. */ |
| 5350 | params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); |
| 5351 | params.spp_pathmtu = asoc->pathmtu; |
| 5352 | params.spp_pathmaxrxt = asoc->pathmaxrxt; |
| 5353 | params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5355 | /*draft-11 doesn't say what to return in spp_flags*/ |
| 5356 | params.spp_flags = asoc->param_flags; |
| 5357 | } else { |
| 5358 | /* Fetch socket values. */ |
| 5359 | params.spp_hbinterval = sp->hbinterval; |
| 5360 | params.spp_pathmtu = sp->pathmtu; |
| 5361 | params.spp_sackdelay = sp->sackdelay; |
| 5362 | params.spp_pathmaxrxt = sp->pathmaxrxt; |
| 5363 | |
| 5364 | /*draft-11 doesn't say what to return in spp_flags*/ |
| 5365 | params.spp_flags = sp->param_flags; |
| 5366 | } |
| 5367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | if (copy_to_user(optval, ¶ms, len)) |
| 5369 | return -EFAULT; |
| 5370 | |
| 5371 | if (put_user(len, optlen)) |
| 5372 | return -EFAULT; |
| 5373 | |
| 5374 | return 0; |
| 5375 | } |
| 5376 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5377 | /* |
| 5378 | * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5379 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5380 | * This option will effect the way delayed acks are performed. This |
| 5381 | * option allows you to get or set the delayed ack time, in |
| 5382 | * milliseconds. It also allows changing the delayed ack frequency. |
| 5383 | * Changing the frequency to 1 disables the delayed sack algorithm. If |
| 5384 | * the assoc_id is 0, then this sets or gets the endpoints default |
| 5385 | * values. If the assoc_id field is non-zero, then the set or get |
| 5386 | * effects the specified association for the one to many model (the |
| 5387 | * assoc_id field is ignored by the one to one model). Note that if |
| 5388 | * sack_delay or sack_freq are 0 when setting this option, then the |
| 5389 | * current values will remain unchanged. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5390 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5391 | * struct sctp_sack_info { |
| 5392 | * sctp_assoc_t sack_assoc_id; |
| 5393 | * uint32_t sack_delay; |
| 5394 | * uint32_t sack_freq; |
| 5395 | * }; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5396 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5397 | * sack_assoc_id - This parameter, indicates which association the user |
| 5398 | * is performing an action upon. Note that if this field's value is |
| 5399 | * zero then the endpoints default value is changed (effecting future |
| 5400 | * associations only). |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5401 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5402 | * sack_delay - This parameter contains the number of milliseconds that |
| 5403 | * the user is requesting the delayed ACK timer be set to. Note that |
| 5404 | * this value is defined in the standard to be between 200 and 500 |
| 5405 | * milliseconds. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5406 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5407 | * sack_freq - This parameter contains the number of packets that must |
| 5408 | * be received before a sack is sent without waiting for the delay |
| 5409 | * timer to expire. The default value for this is 2, setting this |
| 5410 | * value to 1 will disable the delayed sack algorithm. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5411 | */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5412 | static int sctp_getsockopt_delayed_ack(struct sock *sk, int len, |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5413 | char __user *optval, |
| 5414 | int __user *optlen) |
| 5415 | { |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5416 | struct sctp_sack_info params; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5417 | struct sctp_association *asoc = NULL; |
| 5418 | struct sctp_sock *sp = sctp_sk(sk); |
| 5419 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5420 | if (len >= sizeof(struct sctp_sack_info)) { |
| 5421 | len = sizeof(struct sctp_sack_info); |
| 5422 | |
| 5423 | if (copy_from_user(¶ms, optval, len)) |
| 5424 | return -EFAULT; |
| 5425 | } else if (len == sizeof(struct sctp_assoc_value)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 5426 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 5427 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 5428 | "Use of struct sctp_assoc_value in delayed_ack socket option.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 5429 | "Use struct sctp_sack_info instead\n", |
| 5430 | current->comm, task_pid_nr(current)); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5431 | if (copy_from_user(¶ms, optval, len)) |
| 5432 | return -EFAULT; |
| 5433 | } else |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5434 | return -EINVAL; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5435 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5436 | /* Get association, if sack_assoc_id != 0 and the socket is a one |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5437 | * to many style socket, and an association was not found, then |
| 5438 | * the id was invalid. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5439 | */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5440 | asoc = sctp_id2assoc(sk, params.sack_assoc_id); |
| 5441 | if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5442 | return -EINVAL; |
| 5443 | |
| 5444 | if (asoc) { |
| 5445 | /* Fetch association values. */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5446 | if (asoc->param_flags & SPP_SACKDELAY_ENABLE) { |
| 5447 | params.sack_delay = jiffies_to_msecs( |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5448 | asoc->sackdelay); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5449 | params.sack_freq = asoc->sackfreq; |
| 5450 | |
| 5451 | } else { |
| 5452 | params.sack_delay = 0; |
| 5453 | params.sack_freq = 1; |
| 5454 | } |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5455 | } else { |
| 5456 | /* Fetch socket values. */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 5457 | if (sp->param_flags & SPP_SACKDELAY_ENABLE) { |
| 5458 | params.sack_delay = sp->sackdelay; |
| 5459 | params.sack_freq = sp->sackfreq; |
| 5460 | } else { |
| 5461 | params.sack_delay = 0; |
| 5462 | params.sack_freq = 1; |
| 5463 | } |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | if (copy_to_user(optval, ¶ms, len)) |
| 5467 | return -EFAULT; |
| 5468 | |
| 5469 | if (put_user(len, optlen)) |
| 5470 | return -EFAULT; |
| 5471 | |
| 5472 | return 0; |
| 5473 | } |
| 5474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) |
| 5476 | * |
| 5477 | * Applications can specify protocol parameters for the default association |
| 5478 | * initialization. The option name argument to setsockopt() and getsockopt() |
| 5479 | * is SCTP_INITMSG. |
| 5480 | * |
| 5481 | * Setting initialization parameters is effective only on an unconnected |
| 5482 | * socket (for UDP-style sockets only future associations are effected |
| 5483 | * by the change). With TCP-style sockets, this option is inherited by |
| 5484 | * sockets derived from a listener socket. |
| 5485 | */ |
| 5486 | static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) |
| 5487 | { |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5488 | if (len < sizeof(struct sctp_initmsg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | return -EINVAL; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5490 | len = sizeof(struct sctp_initmsg); |
| 5491 | if (put_user(len, optlen)) |
| 5492 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) |
| 5494 | return -EFAULT; |
| 5495 | return 0; |
| 5496 | } |
| 5497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5498 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5499 | static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, |
| 5500 | char __user *optval, int __user *optlen) |
| 5501 | { |
| 5502 | struct sctp_association *asoc; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5503 | int cnt = 0; |
| 5504 | struct sctp_getaddrs getaddrs; |
| 5505 | struct sctp_transport *from; |
| 5506 | void __user *to; |
| 5507 | union sctp_addr temp; |
| 5508 | struct sctp_sock *sp = sctp_sk(sk); |
| 5509 | int addrlen; |
| 5510 | size_t space_left; |
| 5511 | int bytes_copied; |
| 5512 | |
| 5513 | if (len < sizeof(struct sctp_getaddrs)) |
| 5514 | return -EINVAL; |
| 5515 | |
| 5516 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) |
| 5517 | return -EFAULT; |
| 5518 | |
| 5519 | /* For UDP-style sockets, id specifies the association to query. */ |
| 5520 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); |
| 5521 | if (!asoc) |
| 5522 | return -EINVAL; |
| 5523 | |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5524 | to = optval + offsetof(struct sctp_getaddrs, addrs); |
| 5525 | space_left = len - offsetof(struct sctp_getaddrs, addrs); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5526 | |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 5527 | list_for_each_entry(from, &asoc->peer.transport_addr_list, |
| 5528 | transports) { |
Al Viro | b3f5b3b | 2006-11-20 17:22:43 -0800 | [diff] [blame] | 5529 | memcpy(&temp, &from->ipaddr, sizeof(temp)); |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5530 | addrlen = sctp_get_pf_specific(sk->sk_family) |
| 5531 | ->addr_to_user(sp, &temp); |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5532 | if (space_left < addrlen) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5533 | return -ENOMEM; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5534 | if (copy_to_user(to, &temp, addrlen)) |
| 5535 | return -EFAULT; |
| 5536 | to += addrlen; |
| 5537 | cnt++; |
| 5538 | space_left -= addrlen; |
| 5539 | } |
| 5540 | |
| 5541 | if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) |
| 5542 | return -EFAULT; |
| 5543 | bytes_copied = ((char __user *)to) - optval; |
| 5544 | if (put_user(bytes_copied, optlen)) |
| 5545 | return -EFAULT; |
| 5546 | |
| 5547 | return 0; |
| 5548 | } |
| 5549 | |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5550 | static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, |
| 5551 | size_t space_left, int *bytes_copied) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5552 | { |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5553 | struct sctp_sockaddr_entry *addr; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5554 | union sctp_addr temp; |
| 5555 | int cnt = 0; |
| 5556 | int addrlen; |
Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 5557 | struct net *net = sock_net(sk); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5558 | |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 5559 | rcu_read_lock(); |
Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 5560 | list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) { |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 5561 | if (!addr->valid) |
| 5562 | continue; |
| 5563 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5564 | if ((PF_INET == sk->sk_family) && |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 5565 | (AF_INET6 == addr->a.sa.sa_family)) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5566 | continue; |
Vlad Yasevich | 7dab83d | 2008-07-18 23:05:40 -0700 | [diff] [blame] | 5567 | if ((PF_INET6 == sk->sk_family) && |
| 5568 | inet_v6_ipv6only(sk) && |
| 5569 | (AF_INET == addr->a.sa.sa_family)) |
| 5570 | continue; |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 5571 | memcpy(&temp, &addr->a, sizeof(temp)); |
Vlad Yasevich | b46ae36 | 2008-01-28 14:25:36 -0500 | [diff] [blame] | 5572 | if (!temp.v4.sin_port) |
| 5573 | temp.v4.sin_port = htons(port); |
| 5574 | |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5575 | addrlen = sctp_get_pf_specific(sk->sk_family) |
| 5576 | ->addr_to_user(sctp_sk(sk), &temp); |
| 5577 | |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 5578 | if (space_left < addrlen) { |
| 5579 | cnt = -ENOMEM; |
| 5580 | break; |
| 5581 | } |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5582 | memcpy(to, &temp, addrlen); |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 5583 | |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5584 | to += addrlen; |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5585 | cnt++; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5586 | space_left -= addrlen; |
Vlad Yasevich | 3663c30 | 2007-07-03 12:43:12 -0400 | [diff] [blame] | 5587 | *bytes_copied += addrlen; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5588 | } |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 5589 | rcu_read_unlock(); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5590 | |
| 5591 | return cnt; |
| 5592 | } |
| 5593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5595 | static int sctp_getsockopt_local_addrs(struct sock *sk, int len, |
| 5596 | char __user *optval, int __user *optlen) |
| 5597 | { |
| 5598 | struct sctp_bind_addr *bp; |
| 5599 | struct sctp_association *asoc; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5600 | int cnt = 0; |
| 5601 | struct sctp_getaddrs getaddrs; |
| 5602 | struct sctp_sockaddr_entry *addr; |
| 5603 | void __user *to; |
| 5604 | union sctp_addr temp; |
| 5605 | struct sctp_sock *sp = sctp_sk(sk); |
| 5606 | int addrlen; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5607 | int err = 0; |
| 5608 | size_t space_left; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5609 | int bytes_copied = 0; |
| 5610 | void *addrs; |
Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 5611 | void *buf; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5612 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5613 | if (len < sizeof(struct sctp_getaddrs)) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5614 | return -EINVAL; |
| 5615 | |
| 5616 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) |
| 5617 | return -EFAULT; |
| 5618 | |
| 5619 | /* |
| 5620 | * For UDP-style sockets, id specifies the association to query. |
| 5621 | * If the id field is set to the value '0' then the locally bound |
| 5622 | * addresses are returned without regard to any particular |
| 5623 | * association. |
| 5624 | */ |
| 5625 | if (0 == getaddrs.assoc_id) { |
| 5626 | bp = &sctp_sk(sk)->ep->base.bind_addr; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5627 | } else { |
| 5628 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); |
| 5629 | if (!asoc) |
| 5630 | return -EINVAL; |
| 5631 | bp = &asoc->base.bind_addr; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5632 | } |
| 5633 | |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5634 | to = optval + offsetof(struct sctp_getaddrs, addrs); |
| 5635 | space_left = len - offsetof(struct sctp_getaddrs, addrs); |
Neil Horman | 186e234 | 2007-06-18 19:59:16 -0400 | [diff] [blame] | 5636 | |
Marcelo Ricardo Leitner | cacc062 | 2015-11-30 14:32:54 -0200 | [diff] [blame] | 5637 | addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN); |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5638 | if (!addrs) |
| 5639 | return -ENOMEM; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5640 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5641 | /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid |
| 5642 | * addresses from the global local address list. |
| 5643 | */ |
| 5644 | if (sctp_list_single_entry(&bp->address_list)) { |
| 5645 | addr = list_entry(bp->address_list.next, |
| 5646 | struct sctp_sockaddr_entry, list); |
Vlad Yasevich | 52cae8f | 2008-08-18 10:34:34 -0400 | [diff] [blame] | 5647 | if (sctp_is_any(sk, &addr->a)) { |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5648 | cnt = sctp_copy_laddrs(sk, bp->port, addrs, |
| 5649 | space_left, &bytes_copied); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5650 | if (cnt < 0) { |
| 5651 | err = cnt; |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 5652 | goto out; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5653 | } |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5654 | goto copy_getaddrs; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5655 | } |
| 5656 | } |
| 5657 | |
Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 5658 | buf = addrs; |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 5659 | /* Protection on the bound address list is not needed since |
| 5660 | * in the socket option context we hold a socket lock and |
| 5661 | * thus the bound address list can't change. |
| 5662 | */ |
| 5663 | list_for_each_entry(addr, &bp->address_list, list) { |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 5664 | memcpy(&temp, &addr->a, sizeof(temp)); |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5665 | addrlen = sctp_get_pf_specific(sk->sk_family) |
| 5666 | ->addr_to_user(sp, &temp); |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5667 | if (space_left < addrlen) { |
| 5668 | err = -ENOMEM; /*fixme: right error?*/ |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 5669 | goto out; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5670 | } |
Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 5671 | memcpy(buf, &temp, addrlen); |
| 5672 | buf += addrlen; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5673 | bytes_copied += addrlen; |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5674 | cnt++; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5675 | space_left -= addrlen; |
| 5676 | } |
| 5677 | |
| 5678 | copy_getaddrs: |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5679 | if (copy_to_user(to, addrs, bytes_copied)) { |
| 5680 | err = -EFAULT; |
Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 5681 | goto out; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5682 | } |
Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 5683 | if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) { |
| 5684 | err = -EFAULT; |
Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 5685 | goto out; |
Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 5686 | } |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5687 | if (put_user(bytes_copied, optlen)) |
Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 5688 | err = -EFAULT; |
Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 5689 | out: |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 5690 | kfree(addrs); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 5691 | return err; |
| 5692 | } |
| 5693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) |
| 5695 | * |
| 5696 | * Requests that the local SCTP stack use the enclosed peer address as |
| 5697 | * the association primary. The enclosed address must be one of the |
| 5698 | * association peer's addresses. |
| 5699 | */ |
| 5700 | static int sctp_getsockopt_primary_addr(struct sock *sk, int len, |
| 5701 | char __user *optval, int __user *optlen) |
| 5702 | { |
| 5703 | struct sctp_prim prim; |
| 5704 | struct sctp_association *asoc; |
| 5705 | struct sctp_sock *sp = sctp_sk(sk); |
| 5706 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5707 | if (len < sizeof(struct sctp_prim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | return -EINVAL; |
| 5709 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5710 | len = sizeof(struct sctp_prim); |
| 5711 | |
| 5712 | if (copy_from_user(&prim, optval, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | return -EFAULT; |
| 5714 | |
| 5715 | asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); |
| 5716 | if (!asoc) |
| 5717 | return -EINVAL; |
| 5718 | |
| 5719 | if (!asoc->peer.primary_path) |
| 5720 | return -ENOTCONN; |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5721 | |
Al Viro | 8cec6b8 | 2006-11-20 17:23:01 -0800 | [diff] [blame] | 5722 | memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, |
| 5723 | asoc->peer.primary_path->af_specific->sockaddr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5725 | sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | (union sctp_addr *)&prim.ssp_addr); |
| 5727 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5728 | if (put_user(len, optlen)) |
| 5729 | return -EFAULT; |
| 5730 | if (copy_to_user(optval, &prim, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | return -EFAULT; |
| 5732 | |
| 5733 | return 0; |
| 5734 | } |
| 5735 | |
| 5736 | /* |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 5737 | * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | * |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 5739 | * Requests that the local endpoint set the specified Adaptation Layer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | * Indication parameter for all future INIT and INIT-ACK exchanges. |
| 5741 | */ |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 5742 | static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | char __user *optval, int __user *optlen) |
| 5744 | { |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 5745 | struct sctp_setadaptation adaptation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5747 | if (len < sizeof(struct sctp_setadaptation)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | return -EINVAL; |
| 5749 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5750 | len = sizeof(struct sctp_setadaptation); |
| 5751 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 5752 | adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5753 | |
| 5754 | if (put_user(len, optlen)) |
| 5755 | return -EFAULT; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 5756 | if (copy_to_user(optval, &adaptation, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | return -EFAULT; |
Ivan Skytte Jorgensen | a1ab358 | 2005-10-28 15:33:24 -0700 | [diff] [blame] | 5758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | return 0; |
| 5760 | } |
| 5761 | |
| 5762 | /* |
| 5763 | * |
| 5764 | * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) |
| 5765 | * |
| 5766 | * Applications that wish to use the sendto() system call may wish to |
| 5767 | * specify a default set of parameters that would normally be supplied |
| 5768 | * through the inclusion of ancillary data. This socket option allows |
| 5769 | * such an application to set the default sctp_sndrcvinfo structure. |
| 5770 | |
| 5771 | |
| 5772 | * The application that wishes to use this socket option simply passes |
| 5773 | * in to this call the sctp_sndrcvinfo structure defined in Section |
| 5774 | * 5.2.2) The input parameters accepted by this call include |
| 5775 | * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, |
| 5776 | * sinfo_timetolive. The user must provide the sinfo_assoc_id field in |
| 5777 | * to this call if the caller is using the UDP model. |
| 5778 | * |
| 5779 | * For getsockopt, it get the default sctp_sndrcvinfo structure. |
| 5780 | */ |
| 5781 | static int sctp_getsockopt_default_send_param(struct sock *sk, |
| 5782 | int len, char __user *optval, |
| 5783 | int __user *optlen) |
| 5784 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5785 | struct sctp_sock *sp = sctp_sk(sk); |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 5786 | struct sctp_association *asoc; |
| 5787 | struct sctp_sndrcvinfo info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 5789 | if (len < sizeof(info)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | return -EINVAL; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5791 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 5792 | len = sizeof(info); |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5793 | |
| 5794 | if (copy_from_user(&info, optval, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5795 | return -EFAULT; |
| 5796 | |
| 5797 | asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); |
| 5798 | if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) |
| 5799 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | if (asoc) { |
| 5801 | info.sinfo_stream = asoc->default_stream; |
| 5802 | info.sinfo_flags = asoc->default_flags; |
| 5803 | info.sinfo_ppid = asoc->default_ppid; |
| 5804 | info.sinfo_context = asoc->default_context; |
| 5805 | info.sinfo_timetolive = asoc->default_timetolive; |
| 5806 | } else { |
| 5807 | info.sinfo_stream = sp->default_stream; |
| 5808 | info.sinfo_flags = sp->default_flags; |
| 5809 | info.sinfo_ppid = sp->default_ppid; |
| 5810 | info.sinfo_context = sp->default_context; |
| 5811 | info.sinfo_timetolive = sp->default_timetolive; |
| 5812 | } |
| 5813 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5814 | if (put_user(len, optlen)) |
| 5815 | return -EFAULT; |
| 5816 | if (copy_to_user(optval, &info, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | return -EFAULT; |
| 5818 | |
| 5819 | return 0; |
| 5820 | } |
| 5821 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 5822 | /* RFC6458, Section 8.1.31. Set/get Default Send Parameters |
| 5823 | * (SCTP_DEFAULT_SNDINFO) |
| 5824 | */ |
| 5825 | static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len, |
| 5826 | char __user *optval, |
| 5827 | int __user *optlen) |
| 5828 | { |
| 5829 | struct sctp_sock *sp = sctp_sk(sk); |
| 5830 | struct sctp_association *asoc; |
| 5831 | struct sctp_sndinfo info; |
| 5832 | |
| 5833 | if (len < sizeof(info)) |
| 5834 | return -EINVAL; |
| 5835 | |
| 5836 | len = sizeof(info); |
| 5837 | |
| 5838 | if (copy_from_user(&info, optval, len)) |
| 5839 | return -EFAULT; |
| 5840 | |
| 5841 | asoc = sctp_id2assoc(sk, info.snd_assoc_id); |
| 5842 | if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP)) |
| 5843 | return -EINVAL; |
| 5844 | if (asoc) { |
| 5845 | info.snd_sid = asoc->default_stream; |
| 5846 | info.snd_flags = asoc->default_flags; |
| 5847 | info.snd_ppid = asoc->default_ppid; |
| 5848 | info.snd_context = asoc->default_context; |
| 5849 | } else { |
| 5850 | info.snd_sid = sp->default_stream; |
| 5851 | info.snd_flags = sp->default_flags; |
| 5852 | info.snd_ppid = sp->default_ppid; |
| 5853 | info.snd_context = sp->default_context; |
| 5854 | } |
| 5855 | |
| 5856 | if (put_user(len, optlen)) |
| 5857 | return -EFAULT; |
| 5858 | if (copy_to_user(optval, &info, len)) |
| 5859 | return -EFAULT; |
| 5860 | |
| 5861 | return 0; |
| 5862 | } |
| 5863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | /* |
| 5865 | * |
| 5866 | * 7.1.5 SCTP_NODELAY |
| 5867 | * |
| 5868 | * Turn on/off any Nagle-like algorithm. This means that packets are |
| 5869 | * generally sent as soon as possible and no unnecessary delays are |
| 5870 | * introduced, at the cost of more packets in the network. Expects an |
| 5871 | * integer boolean flag. |
| 5872 | */ |
| 5873 | |
| 5874 | static int sctp_getsockopt_nodelay(struct sock *sk, int len, |
| 5875 | char __user *optval, int __user *optlen) |
| 5876 | { |
| 5877 | int val; |
| 5878 | |
| 5879 | if (len < sizeof(int)) |
| 5880 | return -EINVAL; |
| 5881 | |
| 5882 | len = sizeof(int); |
| 5883 | val = (sctp_sk(sk)->nodelay == 1); |
| 5884 | if (put_user(len, optlen)) |
| 5885 | return -EFAULT; |
| 5886 | if (copy_to_user(optval, &val, len)) |
| 5887 | return -EFAULT; |
| 5888 | return 0; |
| 5889 | } |
| 5890 | |
| 5891 | /* |
| 5892 | * |
| 5893 | * 7.1.1 SCTP_RTOINFO |
| 5894 | * |
| 5895 | * The protocol parameters used to initialize and bound retransmission |
| 5896 | * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access |
| 5897 | * and modify these parameters. |
| 5898 | * All parameters are time values, in milliseconds. A value of 0, when |
| 5899 | * modifying the parameters, indicates that the current value should not |
| 5900 | * be changed. |
| 5901 | * |
| 5902 | */ |
| 5903 | static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, |
| 5904 | char __user *optval, |
| 5905 | int __user *optlen) { |
| 5906 | struct sctp_rtoinfo rtoinfo; |
| 5907 | struct sctp_association *asoc; |
| 5908 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5909 | if (len < sizeof (struct sctp_rtoinfo)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | return -EINVAL; |
| 5911 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5912 | len = sizeof(struct sctp_rtoinfo); |
| 5913 | |
| 5914 | if (copy_from_user(&rtoinfo, optval, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | return -EFAULT; |
| 5916 | |
| 5917 | asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); |
| 5918 | |
| 5919 | if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) |
| 5920 | return -EINVAL; |
| 5921 | |
| 5922 | /* Values corresponding to the specific association. */ |
| 5923 | if (asoc) { |
| 5924 | rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); |
| 5925 | rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); |
| 5926 | rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); |
| 5927 | } else { |
| 5928 | /* Values corresponding to the endpoint. */ |
| 5929 | struct sctp_sock *sp = sctp_sk(sk); |
| 5930 | |
| 5931 | rtoinfo.srto_initial = sp->rtoinfo.srto_initial; |
| 5932 | rtoinfo.srto_max = sp->rtoinfo.srto_max; |
| 5933 | rtoinfo.srto_min = sp->rtoinfo.srto_min; |
| 5934 | } |
| 5935 | |
| 5936 | if (put_user(len, optlen)) |
| 5937 | return -EFAULT; |
| 5938 | |
| 5939 | if (copy_to_user(optval, &rtoinfo, len)) |
| 5940 | return -EFAULT; |
| 5941 | |
| 5942 | return 0; |
| 5943 | } |
| 5944 | |
| 5945 | /* |
| 5946 | * |
| 5947 | * 7.1.2 SCTP_ASSOCINFO |
| 5948 | * |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 5949 | * This option is used to tune the maximum retransmission attempts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | * of the association. |
| 5951 | * Returns an error if the new association retransmission value is |
| 5952 | * greater than the sum of the retransmission value of the peer. |
| 5953 | * See [SCTP] for more information. |
| 5954 | * |
| 5955 | */ |
| 5956 | static int sctp_getsockopt_associnfo(struct sock *sk, int len, |
| 5957 | char __user *optval, |
| 5958 | int __user *optlen) |
| 5959 | { |
| 5960 | |
| 5961 | struct sctp_assocparams assocparams; |
| 5962 | struct sctp_association *asoc; |
| 5963 | struct list_head *pos; |
| 5964 | int cnt = 0; |
| 5965 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5966 | if (len < sizeof (struct sctp_assocparams)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | return -EINVAL; |
| 5968 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5969 | len = sizeof(struct sctp_assocparams); |
| 5970 | |
| 5971 | if (copy_from_user(&assocparams, optval, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | return -EFAULT; |
| 5973 | |
| 5974 | asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); |
| 5975 | |
| 5976 | if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) |
| 5977 | return -EINVAL; |
| 5978 | |
| 5979 | /* Values correspoinding to the specific association */ |
Vladislav Yasevich | 1733721 | 2005-04-28 11:57:54 -0700 | [diff] [blame] | 5980 | if (asoc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 | assocparams.sasoc_asocmaxrxt = asoc->max_retrans; |
| 5982 | assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; |
| 5983 | assocparams.sasoc_local_rwnd = asoc->a_rwnd; |
Daniel Borkmann | 52db882 | 2013-06-25 18:17:27 +0200 | [diff] [blame] | 5984 | assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | |
| 5986 | list_for_each(pos, &asoc->peer.transport_addr_list) { |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 5987 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | } |
| 5989 | |
| 5990 | assocparams.sasoc_number_peer_destinations = cnt; |
| 5991 | } else { |
| 5992 | /* Values corresponding to the endpoint */ |
| 5993 | struct sctp_sock *sp = sctp_sk(sk); |
| 5994 | |
| 5995 | assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; |
| 5996 | assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; |
| 5997 | assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; |
| 5998 | assocparams.sasoc_cookie_life = |
| 5999 | sp->assocparams.sasoc_cookie_life; |
| 6000 | assocparams.sasoc_number_peer_destinations = |
| 6001 | sp->assocparams. |
| 6002 | sasoc_number_peer_destinations; |
| 6003 | } |
| 6004 | |
| 6005 | if (put_user(len, optlen)) |
| 6006 | return -EFAULT; |
| 6007 | |
| 6008 | if (copy_to_user(optval, &assocparams, len)) |
| 6009 | return -EFAULT; |
| 6010 | |
| 6011 | return 0; |
| 6012 | } |
| 6013 | |
| 6014 | /* |
| 6015 | * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) |
| 6016 | * |
| 6017 | * This socket option is a boolean flag which turns on or off mapped V4 |
| 6018 | * addresses. If this option is turned on and the socket is type |
| 6019 | * PF_INET6, then IPv4 addresses will be mapped to V6 representation. |
| 6020 | * If this option is turned off, then no mapping will be done of V4 |
| 6021 | * addresses and a user will receive both PF_INET6 and PF_INET type |
| 6022 | * addresses on the socket. |
| 6023 | */ |
| 6024 | static int sctp_getsockopt_mappedv4(struct sock *sk, int len, |
| 6025 | char __user *optval, int __user *optlen) |
| 6026 | { |
| 6027 | int val; |
| 6028 | struct sctp_sock *sp = sctp_sk(sk); |
| 6029 | |
| 6030 | if (len < sizeof(int)) |
| 6031 | return -EINVAL; |
| 6032 | |
| 6033 | len = sizeof(int); |
| 6034 | val = sp->v4mapped; |
| 6035 | if (put_user(len, optlen)) |
| 6036 | return -EFAULT; |
| 6037 | if (copy_to_user(optval, &val, len)) |
| 6038 | return -EFAULT; |
| 6039 | |
| 6040 | return 0; |
| 6041 | } |
| 6042 | |
| 6043 | /* |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6044 | * 7.1.29. Set or Get the default context (SCTP_CONTEXT) |
| 6045 | * (chapter and verse is quoted at sctp_setsockopt_context()) |
| 6046 | */ |
| 6047 | static int sctp_getsockopt_context(struct sock *sk, int len, |
| 6048 | char __user *optval, int __user *optlen) |
| 6049 | { |
| 6050 | struct sctp_assoc_value params; |
| 6051 | struct sctp_sock *sp; |
| 6052 | struct sctp_association *asoc; |
| 6053 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6054 | if (len < sizeof(struct sctp_assoc_value)) |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6055 | return -EINVAL; |
| 6056 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6057 | len = sizeof(struct sctp_assoc_value); |
| 6058 | |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6059 | if (copy_from_user(¶ms, optval, len)) |
| 6060 | return -EFAULT; |
| 6061 | |
| 6062 | sp = sctp_sk(sk); |
| 6063 | |
| 6064 | if (params.assoc_id != 0) { |
| 6065 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 6066 | if (!asoc) |
| 6067 | return -EINVAL; |
| 6068 | params.assoc_value = asoc->default_rcv_context; |
| 6069 | } else { |
| 6070 | params.assoc_value = sp->default_rcv_context; |
| 6071 | } |
| 6072 | |
| 6073 | if (put_user(len, optlen)) |
| 6074 | return -EFAULT; |
| 6075 | if (copy_to_user(optval, ¶ms, len)) |
| 6076 | return -EFAULT; |
| 6077 | |
| 6078 | return 0; |
| 6079 | } |
| 6080 | |
| 6081 | /* |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6082 | * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) |
| 6083 | * This option will get or set the maximum size to put in any outgoing |
| 6084 | * SCTP DATA chunk. If a message is larger than this size it will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | * fragmented by SCTP into the specified size. Note that the underlying |
| 6086 | * SCTP implementation may fragment into smaller sized chunks when the |
| 6087 | * PMTU of the underlying association is smaller than the value set by |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6088 | * the user. The default value for this option is '0' which indicates |
| 6089 | * the user is NOT limiting fragmentation and only the PMTU will effect |
| 6090 | * SCTP's choice of DATA chunk size. Note also that values set larger |
| 6091 | * than the maximum size of an IP datagram will effectively let SCTP |
| 6092 | * control fragmentation (i.e. the same as setting this option to 0). |
| 6093 | * |
| 6094 | * The following structure is used to access and modify this parameter: |
| 6095 | * |
| 6096 | * struct sctp_assoc_value { |
| 6097 | * sctp_assoc_t assoc_id; |
| 6098 | * uint32_t assoc_value; |
| 6099 | * }; |
| 6100 | * |
| 6101 | * assoc_id: This parameter is ignored for one-to-one style sockets. |
| 6102 | * For one-to-many style sockets this parameter indicates which |
| 6103 | * association the user is performing an action upon. Note that if |
| 6104 | * this field's value is zero then the endpoints default value is |
| 6105 | * changed (effecting future associations only). |
| 6106 | * assoc_value: This parameter specifies the maximum size in bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | */ |
| 6108 | static int sctp_getsockopt_maxseg(struct sock *sk, int len, |
| 6109 | char __user *optval, int __user *optlen) |
| 6110 | { |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6111 | struct sctp_assoc_value params; |
| 6112 | struct sctp_association *asoc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6113 | |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6114 | if (len == sizeof(int)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6115 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6116 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6117 | "Use of int in maxseg socket option.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6118 | "Use struct sctp_assoc_value instead\n", |
| 6119 | current->comm, task_pid_nr(current)); |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6120 | params.assoc_id = 0; |
| 6121 | } else if (len >= sizeof(struct sctp_assoc_value)) { |
| 6122 | len = sizeof(struct sctp_assoc_value); |
| 6123 | if (copy_from_user(¶ms, optval, sizeof(params))) |
| 6124 | return -EFAULT; |
| 6125 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6126 | return -EINVAL; |
| 6127 | |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6128 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 6129 | if (!asoc && params.assoc_id && sctp_style(sk, UDP)) |
| 6130 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6132 | if (asoc) |
| 6133 | params.assoc_value = asoc->frag_point; |
| 6134 | else |
| 6135 | params.assoc_value = sctp_sk(sk)->user_frag; |
| 6136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | if (put_user(len, optlen)) |
| 6138 | return -EFAULT; |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6139 | if (len == sizeof(int)) { |
| 6140 | if (copy_to_user(optval, ¶ms.assoc_value, len)) |
| 6141 | return -EFAULT; |
| 6142 | } else { |
| 6143 | if (copy_to_user(optval, ¶ms, len)) |
| 6144 | return -EFAULT; |
| 6145 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | |
| 6147 | return 0; |
| 6148 | } |
| 6149 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 6150 | /* |
| 6151 | * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) |
| 6152 | * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) |
| 6153 | */ |
| 6154 | static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, |
| 6155 | char __user *optval, int __user *optlen) |
| 6156 | { |
| 6157 | int val; |
| 6158 | |
| 6159 | if (len < sizeof(int)) |
| 6160 | return -EINVAL; |
| 6161 | |
| 6162 | len = sizeof(int); |
| 6163 | |
| 6164 | val = sctp_sk(sk)->frag_interleave; |
| 6165 | if (put_user(len, optlen)) |
| 6166 | return -EFAULT; |
| 6167 | if (copy_to_user(optval, &val, len)) |
| 6168 | return -EFAULT; |
| 6169 | |
| 6170 | return 0; |
| 6171 | } |
| 6172 | |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 6173 | /* |
| 6174 | * 7.1.25. Set or Get the sctp partial delivery point |
| 6175 | * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) |
| 6176 | */ |
| 6177 | static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, |
| 6178 | char __user *optval, |
| 6179 | int __user *optlen) |
| 6180 | { |
YOSHIFUJI Hideaki | 9cbcbf4 | 2007-07-19 10:44:50 +0900 | [diff] [blame] | 6181 | u32 val; |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 6182 | |
| 6183 | if (len < sizeof(u32)) |
| 6184 | return -EINVAL; |
| 6185 | |
| 6186 | len = sizeof(u32); |
| 6187 | |
| 6188 | val = sctp_sk(sk)->pd_point; |
| 6189 | if (put_user(len, optlen)) |
| 6190 | return -EFAULT; |
| 6191 | if (copy_to_user(optval, &val, len)) |
| 6192 | return -EFAULT; |
| 6193 | |
Wei Yongjun | 7d743b7 | 2010-12-14 16:10:41 +0000 | [diff] [blame] | 6194 | return 0; |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 6195 | } |
| 6196 | |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6197 | /* |
| 6198 | * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) |
| 6199 | * (chapter and verse is quoted at sctp_setsockopt_maxburst()) |
| 6200 | */ |
| 6201 | static int sctp_getsockopt_maxburst(struct sock *sk, int len, |
| 6202 | char __user *optval, |
| 6203 | int __user *optlen) |
| 6204 | { |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6205 | struct sctp_assoc_value params; |
| 6206 | struct sctp_sock *sp; |
| 6207 | struct sctp_association *asoc; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6208 | |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6209 | if (len == sizeof(int)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6210 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6211 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6212 | "Use of int in max_burst socket option.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6213 | "Use struct sctp_assoc_value instead\n", |
| 6214 | current->comm, task_pid_nr(current)); |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6215 | params.assoc_id = 0; |
Wei Yongjun | c6db93a | 2009-03-02 09:46:12 +0000 | [diff] [blame] | 6216 | } else if (len >= sizeof(struct sctp_assoc_value)) { |
| 6217 | len = sizeof(struct sctp_assoc_value); |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6218 | if (copy_from_user(¶ms, optval, len)) |
| 6219 | return -EFAULT; |
| 6220 | } else |
| 6221 | return -EINVAL; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6222 | |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6223 | sp = sctp_sk(sk); |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6224 | |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6225 | if (params.assoc_id != 0) { |
| 6226 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 6227 | if (!asoc) |
| 6228 | return -EINVAL; |
| 6229 | params.assoc_value = asoc->max_burst; |
| 6230 | } else |
| 6231 | params.assoc_value = sp->max_burst; |
| 6232 | |
| 6233 | if (len == sizeof(int)) { |
| 6234 | if (copy_to_user(optval, ¶ms.assoc_value, len)) |
| 6235 | return -EFAULT; |
| 6236 | } else { |
| 6237 | if (copy_to_user(optval, ¶ms, len)) |
| 6238 | return -EFAULT; |
| 6239 | } |
| 6240 | |
| 6241 | return 0; |
| 6242 | |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6243 | } |
| 6244 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6245 | static int sctp_getsockopt_hmac_ident(struct sock *sk, int len, |
| 6246 | char __user *optval, int __user *optlen) |
| 6247 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6248 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6249 | struct sctp_hmacalgo __user *p = (void __user *)optval; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6250 | struct sctp_hmac_algo_param *hmacs; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6251 | __u16 data_len = 0; |
| 6252 | u32 num_idents; |
Xin Long | 7a84bd4 | 2016-02-03 23:33:30 +0800 | [diff] [blame] | 6253 | int i; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6254 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6255 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6256 | return -EACCES; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6257 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6258 | hmacs = ep->auth_hmacs_list; |
Xin Long | 3c91870 | 2017-06-30 11:52:16 +0800 | [diff] [blame] | 6259 | data_len = ntohs(hmacs->param_hdr.length) - |
| 6260 | sizeof(struct sctp_paramhdr); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6261 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6262 | if (len < sizeof(struct sctp_hmacalgo) + data_len) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6263 | return -EINVAL; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6264 | |
| 6265 | len = sizeof(struct sctp_hmacalgo) + data_len; |
| 6266 | num_idents = data_len / sizeof(u16); |
| 6267 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6268 | if (put_user(len, optlen)) |
| 6269 | return -EFAULT; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6270 | if (put_user(num_idents, &p->shmac_num_idents)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6271 | return -EFAULT; |
Xin Long | 7a84bd4 | 2016-02-03 23:33:30 +0800 | [diff] [blame] | 6272 | for (i = 0; i < num_idents; i++) { |
| 6273 | __u16 hmacid = ntohs(hmacs->hmac_ids[i]); |
| 6274 | |
| 6275 | if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16))) |
| 6276 | return -EFAULT; |
| 6277 | } |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6278 | return 0; |
| 6279 | } |
| 6280 | |
| 6281 | static int sctp_getsockopt_active_key(struct sock *sk, int len, |
| 6282 | char __user *optval, int __user *optlen) |
| 6283 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6284 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6285 | struct sctp_authkeyid val; |
| 6286 | struct sctp_association *asoc; |
| 6287 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6288 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6289 | return -EACCES; |
| 6290 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6291 | if (len < sizeof(struct sctp_authkeyid)) |
| 6292 | return -EINVAL; |
| 6293 | if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid))) |
| 6294 | return -EFAULT; |
| 6295 | |
| 6296 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); |
| 6297 | if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) |
| 6298 | return -EINVAL; |
| 6299 | |
| 6300 | if (asoc) |
| 6301 | val.scact_keynumber = asoc->active_key_id; |
| 6302 | else |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6303 | val.scact_keynumber = ep->active_key_id; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6304 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6305 | len = sizeof(struct sctp_authkeyid); |
| 6306 | if (put_user(len, optlen)) |
| 6307 | return -EFAULT; |
| 6308 | if (copy_to_user(optval, &val, len)) |
| 6309 | return -EFAULT; |
| 6310 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6311 | return 0; |
| 6312 | } |
| 6313 | |
| 6314 | static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len, |
| 6315 | char __user *optval, int __user *optlen) |
| 6316 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6317 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 6318 | struct sctp_authchunks __user *p = (void __user *)optval; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6319 | struct sctp_authchunks val; |
| 6320 | struct sctp_association *asoc; |
| 6321 | struct sctp_chunks_param *ch; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6322 | u32 num_chunks = 0; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6323 | char __user *to; |
| 6324 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6325 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6326 | return -EACCES; |
| 6327 | |
| 6328 | if (len < sizeof(struct sctp_authchunks)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6329 | return -EINVAL; |
| 6330 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6331 | if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks))) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6332 | return -EFAULT; |
| 6333 | |
Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 6334 | to = p->gauth_chunks; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6335 | asoc = sctp_id2assoc(sk, val.gauth_assoc_id); |
| 6336 | if (!asoc) |
| 6337 | return -EINVAL; |
| 6338 | |
| 6339 | ch = asoc->peer.peer_chunks; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6340 | if (!ch) |
| 6341 | goto num; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6342 | |
| 6343 | /* See if the user provided enough room for all the data */ |
Xin Long | 3c91870 | 2017-06-30 11:52:16 +0800 | [diff] [blame] | 6344 | num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); |
Vlad Yasevich | b40db68 | 2008-02-27 14:40:37 -0500 | [diff] [blame] | 6345 | if (len < num_chunks) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6346 | return -EINVAL; |
| 6347 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6348 | if (copy_to_user(to, ch->chunks, num_chunks)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6349 | return -EFAULT; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6350 | num: |
| 6351 | len = sizeof(struct sctp_authchunks) + num_chunks; |
wangweidong | 8d72651 | 2013-12-23 12:16:53 +0800 | [diff] [blame] | 6352 | if (put_user(len, optlen)) |
| 6353 | return -EFAULT; |
Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 6354 | if (put_user(num_chunks, &p->gauth_number_of_chunks)) |
| 6355 | return -EFAULT; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6356 | return 0; |
| 6357 | } |
| 6358 | |
| 6359 | static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len, |
| 6360 | char __user *optval, int __user *optlen) |
| 6361 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6362 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 6363 | struct sctp_authchunks __user *p = (void __user *)optval; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6364 | struct sctp_authchunks val; |
| 6365 | struct sctp_association *asoc; |
| 6366 | struct sctp_chunks_param *ch; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6367 | u32 num_chunks = 0; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6368 | char __user *to; |
| 6369 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6370 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6371 | return -EACCES; |
| 6372 | |
| 6373 | if (len < sizeof(struct sctp_authchunks)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6374 | return -EINVAL; |
| 6375 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6376 | if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks))) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6377 | return -EFAULT; |
| 6378 | |
Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 6379 | to = p->gauth_chunks; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6380 | asoc = sctp_id2assoc(sk, val.gauth_assoc_id); |
| 6381 | if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP)) |
| 6382 | return -EINVAL; |
| 6383 | |
| 6384 | if (asoc) |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 6385 | ch = (struct sctp_chunks_param *)asoc->c.auth_chunks; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6386 | else |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6387 | ch = ep->auth_chunk_list; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6388 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6389 | if (!ch) |
| 6390 | goto num; |
| 6391 | |
Xin Long | 3c91870 | 2017-06-30 11:52:16 +0800 | [diff] [blame] | 6392 | num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6393 | if (len < sizeof(struct sctp_authchunks) + num_chunks) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6394 | return -EINVAL; |
| 6395 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6396 | if (copy_to_user(to, ch->chunks, num_chunks)) |
| 6397 | return -EFAULT; |
| 6398 | num: |
| 6399 | len = sizeof(struct sctp_authchunks) + num_chunks; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6400 | if (put_user(len, optlen)) |
| 6401 | return -EFAULT; |
Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 6402 | if (put_user(num_chunks, &p->gauth_number_of_chunks)) |
| 6403 | return -EFAULT; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6404 | |
| 6405 | return 0; |
| 6406 | } |
| 6407 | |
Wei Yongjun | aea3c5c | 2008-12-25 16:57:24 -0800 | [diff] [blame] | 6408 | /* |
| 6409 | * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER) |
| 6410 | * This option gets the current number of associations that are attached |
| 6411 | * to a one-to-many style socket. The option value is an uint32_t. |
| 6412 | */ |
| 6413 | static int sctp_getsockopt_assoc_number(struct sock *sk, int len, |
| 6414 | char __user *optval, int __user *optlen) |
| 6415 | { |
| 6416 | struct sctp_sock *sp = sctp_sk(sk); |
| 6417 | struct sctp_association *asoc; |
| 6418 | u32 val = 0; |
| 6419 | |
| 6420 | if (sctp_style(sk, TCP)) |
| 6421 | return -EOPNOTSUPP; |
| 6422 | |
| 6423 | if (len < sizeof(u32)) |
| 6424 | return -EINVAL; |
| 6425 | |
| 6426 | len = sizeof(u32); |
| 6427 | |
| 6428 | list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { |
| 6429 | val++; |
| 6430 | } |
| 6431 | |
| 6432 | if (put_user(len, optlen)) |
| 6433 | return -EFAULT; |
| 6434 | if (copy_to_user(optval, &val, len)) |
| 6435 | return -EFAULT; |
| 6436 | |
| 6437 | return 0; |
| 6438 | } |
| 6439 | |
Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 6440 | /* |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 6441 | * 8.1.23 SCTP_AUTO_ASCONF |
| 6442 | * See the corresponding setsockopt entry as description |
| 6443 | */ |
| 6444 | static int sctp_getsockopt_auto_asconf(struct sock *sk, int len, |
| 6445 | char __user *optval, int __user *optlen) |
| 6446 | { |
| 6447 | int val = 0; |
| 6448 | |
| 6449 | if (len < sizeof(int)) |
| 6450 | return -EINVAL; |
| 6451 | |
| 6452 | len = sizeof(int); |
| 6453 | if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk)) |
| 6454 | val = 1; |
| 6455 | if (put_user(len, optlen)) |
| 6456 | return -EFAULT; |
| 6457 | if (copy_to_user(optval, &val, len)) |
| 6458 | return -EFAULT; |
| 6459 | return 0; |
| 6460 | } |
| 6461 | |
| 6462 | /* |
Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 6463 | * 8.2.6. Get the Current Identifiers of Associations |
| 6464 | * (SCTP_GET_ASSOC_ID_LIST) |
| 6465 | * |
| 6466 | * This option gets the current list of SCTP association identifiers of |
| 6467 | * the SCTP associations handled by a one-to-many style socket. |
| 6468 | */ |
| 6469 | static int sctp_getsockopt_assoc_ids(struct sock *sk, int len, |
| 6470 | char __user *optval, int __user *optlen) |
| 6471 | { |
| 6472 | struct sctp_sock *sp = sctp_sk(sk); |
| 6473 | struct sctp_association *asoc; |
| 6474 | struct sctp_assoc_ids *ids; |
| 6475 | u32 num = 0; |
| 6476 | |
| 6477 | if (sctp_style(sk, TCP)) |
| 6478 | return -EOPNOTSUPP; |
| 6479 | |
| 6480 | if (len < sizeof(struct sctp_assoc_ids)) |
| 6481 | return -EINVAL; |
| 6482 | |
| 6483 | list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { |
| 6484 | num++; |
| 6485 | } |
| 6486 | |
| 6487 | if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num) |
| 6488 | return -EINVAL; |
| 6489 | |
| 6490 | len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num; |
| 6491 | |
Marcelo Ricardo Leitner | 9ba0b96 | 2015-12-23 16:28:40 -0200 | [diff] [blame] | 6492 | ids = kmalloc(len, GFP_USER | __GFP_NOWARN); |
Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 6493 | if (unlikely(!ids)) |
| 6494 | return -ENOMEM; |
| 6495 | |
| 6496 | ids->gaids_number_of_ids = num; |
| 6497 | num = 0; |
| 6498 | list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { |
| 6499 | ids->gaids_assoc_id[num++] = asoc->assoc_id; |
| 6500 | } |
| 6501 | |
| 6502 | if (put_user(len, optlen) || copy_to_user(optval, ids, len)) { |
| 6503 | kfree(ids); |
| 6504 | return -EFAULT; |
| 6505 | } |
| 6506 | |
| 6507 | kfree(ids); |
| 6508 | return 0; |
| 6509 | } |
| 6510 | |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 6511 | /* |
| 6512 | * SCTP_PEER_ADDR_THLDS |
| 6513 | * |
| 6514 | * This option allows us to fetch the partially failed threshold for one or all |
| 6515 | * transports in an association. See Section 6.1 of: |
| 6516 | * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt |
| 6517 | */ |
| 6518 | static int sctp_getsockopt_paddr_thresholds(struct sock *sk, |
| 6519 | char __user *optval, |
| 6520 | int len, |
| 6521 | int __user *optlen) |
| 6522 | { |
| 6523 | struct sctp_paddrthlds val; |
| 6524 | struct sctp_transport *trans; |
| 6525 | struct sctp_association *asoc; |
| 6526 | |
| 6527 | if (len < sizeof(struct sctp_paddrthlds)) |
| 6528 | return -EINVAL; |
| 6529 | len = sizeof(struct sctp_paddrthlds); |
| 6530 | if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len)) |
| 6531 | return -EFAULT; |
| 6532 | |
| 6533 | if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { |
| 6534 | asoc = sctp_id2assoc(sk, val.spt_assoc_id); |
| 6535 | if (!asoc) |
| 6536 | return -ENOENT; |
| 6537 | |
| 6538 | val.spt_pathpfthld = asoc->pf_retrans; |
| 6539 | val.spt_pathmaxrxt = asoc->pathmaxrxt; |
| 6540 | } else { |
| 6541 | trans = sctp_addr_id2transport(sk, &val.spt_address, |
| 6542 | val.spt_assoc_id); |
| 6543 | if (!trans) |
| 6544 | return -ENOENT; |
| 6545 | |
| 6546 | val.spt_pathmaxrxt = trans->pathmaxrxt; |
| 6547 | val.spt_pathpfthld = trans->pf_retrans; |
| 6548 | } |
| 6549 | |
| 6550 | if (put_user(len, optlen) || copy_to_user(optval, &val, len)) |
| 6551 | return -EFAULT; |
| 6552 | |
| 6553 | return 0; |
| 6554 | } |
| 6555 | |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 6556 | /* |
| 6557 | * SCTP_GET_ASSOC_STATS |
| 6558 | * |
| 6559 | * This option retrieves local per endpoint statistics. It is modeled |
| 6560 | * after OpenSolaris' implementation |
| 6561 | */ |
| 6562 | static int sctp_getsockopt_assoc_stats(struct sock *sk, int len, |
| 6563 | char __user *optval, |
| 6564 | int __user *optlen) |
| 6565 | { |
| 6566 | struct sctp_assoc_stats sas; |
| 6567 | struct sctp_association *asoc = NULL; |
| 6568 | |
| 6569 | /* User must provide at least the assoc id */ |
| 6570 | if (len < sizeof(sctp_assoc_t)) |
| 6571 | return -EINVAL; |
| 6572 | |
Guenter Roeck | 726bc6b | 2013-02-27 10:57:31 +0000 | [diff] [blame] | 6573 | /* Allow the struct to grow and fill in as much as possible */ |
| 6574 | len = min_t(size_t, len, sizeof(sas)); |
| 6575 | |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 6576 | if (copy_from_user(&sas, optval, len)) |
| 6577 | return -EFAULT; |
| 6578 | |
| 6579 | asoc = sctp_id2assoc(sk, sas.sas_assoc_id); |
| 6580 | if (!asoc) |
| 6581 | return -EINVAL; |
| 6582 | |
| 6583 | sas.sas_rtxchunks = asoc->stats.rtxchunks; |
| 6584 | sas.sas_gapcnt = asoc->stats.gapcnt; |
| 6585 | sas.sas_outofseqtsns = asoc->stats.outofseqtsns; |
| 6586 | sas.sas_osacks = asoc->stats.osacks; |
| 6587 | sas.sas_isacks = asoc->stats.isacks; |
| 6588 | sas.sas_octrlchunks = asoc->stats.octrlchunks; |
| 6589 | sas.sas_ictrlchunks = asoc->stats.ictrlchunks; |
| 6590 | sas.sas_oodchunks = asoc->stats.oodchunks; |
| 6591 | sas.sas_iodchunks = asoc->stats.iodchunks; |
| 6592 | sas.sas_ouodchunks = asoc->stats.ouodchunks; |
| 6593 | sas.sas_iuodchunks = asoc->stats.iuodchunks; |
| 6594 | sas.sas_idupchunks = asoc->stats.idupchunks; |
| 6595 | sas.sas_opackets = asoc->stats.opackets; |
| 6596 | sas.sas_ipackets = asoc->stats.ipackets; |
| 6597 | |
| 6598 | /* New high max rto observed, will return 0 if not a single |
| 6599 | * RTO update took place. obs_rto_ipaddr will be bogus |
| 6600 | * in such a case |
| 6601 | */ |
| 6602 | sas.sas_maxrto = asoc->stats.max_obs_rto; |
| 6603 | memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr, |
| 6604 | sizeof(struct sockaddr_storage)); |
| 6605 | |
| 6606 | /* Mark beginning of a new observation period */ |
| 6607 | asoc->stats.max_obs_rto = asoc->rto_min; |
| 6608 | |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 6609 | if (put_user(len, optlen)) |
| 6610 | return -EFAULT; |
| 6611 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 6612 | pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id); |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 6613 | |
| 6614 | if (copy_to_user(optval, &sas, len)) |
| 6615 | return -EFAULT; |
| 6616 | |
| 6617 | return 0; |
| 6618 | } |
| 6619 | |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 6620 | static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len, |
| 6621 | char __user *optval, |
| 6622 | int __user *optlen) |
| 6623 | { |
| 6624 | int val = 0; |
| 6625 | |
| 6626 | if (len < sizeof(int)) |
| 6627 | return -EINVAL; |
| 6628 | |
| 6629 | len = sizeof(int); |
| 6630 | if (sctp_sk(sk)->recvrcvinfo) |
| 6631 | val = 1; |
| 6632 | if (put_user(len, optlen)) |
| 6633 | return -EFAULT; |
| 6634 | if (copy_to_user(optval, &val, len)) |
| 6635 | return -EFAULT; |
| 6636 | |
| 6637 | return 0; |
| 6638 | } |
| 6639 | |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 6640 | static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len, |
| 6641 | char __user *optval, |
| 6642 | int __user *optlen) |
| 6643 | { |
| 6644 | int val = 0; |
| 6645 | |
| 6646 | if (len < sizeof(int)) |
| 6647 | return -EINVAL; |
| 6648 | |
| 6649 | len = sizeof(int); |
| 6650 | if (sctp_sk(sk)->recvnxtinfo) |
| 6651 | val = 1; |
| 6652 | if (put_user(len, optlen)) |
| 6653 | return -EFAULT; |
| 6654 | if (copy_to_user(optval, &val, len)) |
| 6655 | return -EFAULT; |
| 6656 | |
| 6657 | return 0; |
| 6658 | } |
| 6659 | |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 6660 | static int sctp_getsockopt_pr_supported(struct sock *sk, int len, |
| 6661 | char __user *optval, |
| 6662 | int __user *optlen) |
| 6663 | { |
| 6664 | struct sctp_assoc_value params; |
| 6665 | struct sctp_association *asoc; |
| 6666 | int retval = -EFAULT; |
| 6667 | |
| 6668 | if (len < sizeof(params)) { |
| 6669 | retval = -EINVAL; |
| 6670 | goto out; |
| 6671 | } |
| 6672 | |
| 6673 | len = sizeof(params); |
| 6674 | if (copy_from_user(¶ms, optval, len)) |
| 6675 | goto out; |
| 6676 | |
| 6677 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 6678 | if (asoc) { |
| 6679 | params.assoc_value = asoc->prsctp_enable; |
| 6680 | } else if (!params.assoc_id) { |
| 6681 | struct sctp_sock *sp = sctp_sk(sk); |
| 6682 | |
| 6683 | params.assoc_value = sp->ep->prsctp_enable; |
| 6684 | } else { |
| 6685 | retval = -EINVAL; |
| 6686 | goto out; |
| 6687 | } |
| 6688 | |
| 6689 | if (put_user(len, optlen)) |
| 6690 | goto out; |
| 6691 | |
| 6692 | if (copy_to_user(optval, ¶ms, len)) |
| 6693 | goto out; |
| 6694 | |
| 6695 | retval = 0; |
| 6696 | |
| 6697 | out: |
| 6698 | return retval; |
| 6699 | } |
| 6700 | |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 6701 | static int sctp_getsockopt_default_prinfo(struct sock *sk, int len, |
| 6702 | char __user *optval, |
| 6703 | int __user *optlen) |
| 6704 | { |
| 6705 | struct sctp_default_prinfo info; |
| 6706 | struct sctp_association *asoc; |
| 6707 | int retval = -EFAULT; |
| 6708 | |
| 6709 | if (len < sizeof(info)) { |
| 6710 | retval = -EINVAL; |
| 6711 | goto out; |
| 6712 | } |
| 6713 | |
| 6714 | len = sizeof(info); |
| 6715 | if (copy_from_user(&info, optval, len)) |
| 6716 | goto out; |
| 6717 | |
| 6718 | asoc = sctp_id2assoc(sk, info.pr_assoc_id); |
| 6719 | if (asoc) { |
| 6720 | info.pr_policy = SCTP_PR_POLICY(asoc->default_flags); |
| 6721 | info.pr_value = asoc->default_timetolive; |
| 6722 | } else if (!info.pr_assoc_id) { |
| 6723 | struct sctp_sock *sp = sctp_sk(sk); |
| 6724 | |
| 6725 | info.pr_policy = SCTP_PR_POLICY(sp->default_flags); |
| 6726 | info.pr_value = sp->default_timetolive; |
| 6727 | } else { |
| 6728 | retval = -EINVAL; |
| 6729 | goto out; |
| 6730 | } |
| 6731 | |
| 6732 | if (put_user(len, optlen)) |
| 6733 | goto out; |
| 6734 | |
| 6735 | if (copy_to_user(optval, &info, len)) |
| 6736 | goto out; |
| 6737 | |
| 6738 | retval = 0; |
| 6739 | |
| 6740 | out: |
| 6741 | return retval; |
| 6742 | } |
| 6743 | |
Xin Long | 826d253 | 2016-07-09 19:47:42 +0800 | [diff] [blame] | 6744 | static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len, |
| 6745 | char __user *optval, |
| 6746 | int __user *optlen) |
| 6747 | { |
| 6748 | struct sctp_prstatus params; |
| 6749 | struct sctp_association *asoc; |
| 6750 | int policy; |
| 6751 | int retval = -EINVAL; |
| 6752 | |
| 6753 | if (len < sizeof(params)) |
| 6754 | goto out; |
| 6755 | |
| 6756 | len = sizeof(params); |
| 6757 | if (copy_from_user(¶ms, optval, len)) { |
| 6758 | retval = -EFAULT; |
| 6759 | goto out; |
| 6760 | } |
| 6761 | |
| 6762 | policy = params.sprstat_policy; |
| 6763 | if (policy & ~SCTP_PR_SCTP_MASK) |
| 6764 | goto out; |
| 6765 | |
| 6766 | asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); |
| 6767 | if (!asoc) |
| 6768 | goto out; |
| 6769 | |
| 6770 | if (policy == SCTP_PR_SCTP_NONE) { |
| 6771 | params.sprstat_abandoned_unsent = 0; |
| 6772 | params.sprstat_abandoned_sent = 0; |
| 6773 | for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { |
| 6774 | params.sprstat_abandoned_unsent += |
| 6775 | asoc->abandoned_unsent[policy]; |
| 6776 | params.sprstat_abandoned_sent += |
| 6777 | asoc->abandoned_sent[policy]; |
| 6778 | } |
| 6779 | } else { |
| 6780 | params.sprstat_abandoned_unsent = |
| 6781 | asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)]; |
| 6782 | params.sprstat_abandoned_sent = |
| 6783 | asoc->abandoned_sent[__SCTP_PR_INDEX(policy)]; |
| 6784 | } |
| 6785 | |
| 6786 | if (put_user(len, optlen)) { |
| 6787 | retval = -EFAULT; |
| 6788 | goto out; |
| 6789 | } |
| 6790 | |
| 6791 | if (copy_to_user(optval, ¶ms, len)) { |
| 6792 | retval = -EFAULT; |
| 6793 | goto out; |
| 6794 | } |
| 6795 | |
| 6796 | retval = 0; |
| 6797 | |
| 6798 | out: |
| 6799 | return retval; |
| 6800 | } |
| 6801 | |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 6802 | static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len, |
| 6803 | char __user *optval, |
| 6804 | int __user *optlen) |
| 6805 | { |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 6806 | struct sctp_stream_out_ext *streamoute; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 6807 | struct sctp_association *asoc; |
| 6808 | struct sctp_prstatus params; |
| 6809 | int retval = -EINVAL; |
| 6810 | int policy; |
| 6811 | |
| 6812 | if (len < sizeof(params)) |
| 6813 | goto out; |
| 6814 | |
| 6815 | len = sizeof(params); |
| 6816 | if (copy_from_user(¶ms, optval, len)) { |
| 6817 | retval = -EFAULT; |
| 6818 | goto out; |
| 6819 | } |
| 6820 | |
| 6821 | policy = params.sprstat_policy; |
| 6822 | if (policy & ~SCTP_PR_SCTP_MASK) |
| 6823 | goto out; |
| 6824 | |
| 6825 | asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); |
Xin Long | cee360a | 2017-05-31 16:36:31 +0800 | [diff] [blame] | 6826 | if (!asoc || params.sprstat_sid >= asoc->stream.outcnt) |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 6827 | goto out; |
| 6828 | |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 6829 | streamoute = asoc->stream.out[params.sprstat_sid].ext; |
| 6830 | if (!streamoute) { |
| 6831 | /* Not allocated yet, means all stats are 0 */ |
| 6832 | params.sprstat_abandoned_unsent = 0; |
| 6833 | params.sprstat_abandoned_sent = 0; |
| 6834 | retval = 0; |
| 6835 | goto out; |
| 6836 | } |
| 6837 | |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 6838 | if (policy == SCTP_PR_SCTP_NONE) { |
| 6839 | params.sprstat_abandoned_unsent = 0; |
| 6840 | params.sprstat_abandoned_sent = 0; |
| 6841 | for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { |
| 6842 | params.sprstat_abandoned_unsent += |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 6843 | streamoute->abandoned_unsent[policy]; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 6844 | params.sprstat_abandoned_sent += |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 6845 | streamoute->abandoned_sent[policy]; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 6846 | } |
| 6847 | } else { |
| 6848 | params.sprstat_abandoned_unsent = |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 6849 | streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)]; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 6850 | params.sprstat_abandoned_sent = |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 6851 | streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)]; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 6852 | } |
| 6853 | |
| 6854 | if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) { |
| 6855 | retval = -EFAULT; |
| 6856 | goto out; |
| 6857 | } |
| 6858 | |
| 6859 | retval = 0; |
| 6860 | |
| 6861 | out: |
| 6862 | return retval; |
| 6863 | } |
| 6864 | |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 6865 | static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len, |
| 6866 | char __user *optval, |
| 6867 | int __user *optlen) |
| 6868 | { |
| 6869 | struct sctp_assoc_value params; |
| 6870 | struct sctp_association *asoc; |
| 6871 | int retval = -EFAULT; |
| 6872 | |
| 6873 | if (len < sizeof(params)) { |
| 6874 | retval = -EINVAL; |
| 6875 | goto out; |
| 6876 | } |
| 6877 | |
| 6878 | len = sizeof(params); |
| 6879 | if (copy_from_user(¶ms, optval, len)) |
| 6880 | goto out; |
| 6881 | |
| 6882 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 6883 | if (asoc) { |
| 6884 | params.assoc_value = asoc->reconf_enable; |
| 6885 | } else if (!params.assoc_id) { |
| 6886 | struct sctp_sock *sp = sctp_sk(sk); |
| 6887 | |
| 6888 | params.assoc_value = sp->ep->reconf_enable; |
| 6889 | } else { |
| 6890 | retval = -EINVAL; |
| 6891 | goto out; |
| 6892 | } |
| 6893 | |
| 6894 | if (put_user(len, optlen)) |
| 6895 | goto out; |
| 6896 | |
| 6897 | if (copy_to_user(optval, ¶ms, len)) |
| 6898 | goto out; |
| 6899 | |
| 6900 | retval = 0; |
| 6901 | |
| 6902 | out: |
| 6903 | return retval; |
| 6904 | } |
| 6905 | |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 6906 | static int sctp_getsockopt_enable_strreset(struct sock *sk, int len, |
| 6907 | char __user *optval, |
| 6908 | int __user *optlen) |
| 6909 | { |
| 6910 | struct sctp_assoc_value params; |
| 6911 | struct sctp_association *asoc; |
| 6912 | int retval = -EFAULT; |
| 6913 | |
| 6914 | if (len < sizeof(params)) { |
| 6915 | retval = -EINVAL; |
| 6916 | goto out; |
| 6917 | } |
| 6918 | |
| 6919 | len = sizeof(params); |
| 6920 | if (copy_from_user(¶ms, optval, len)) |
| 6921 | goto out; |
| 6922 | |
| 6923 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 6924 | if (asoc) { |
| 6925 | params.assoc_value = asoc->strreset_enable; |
| 6926 | } else if (!params.assoc_id) { |
| 6927 | struct sctp_sock *sp = sctp_sk(sk); |
| 6928 | |
| 6929 | params.assoc_value = sp->ep->strreset_enable; |
| 6930 | } else { |
| 6931 | retval = -EINVAL; |
| 6932 | goto out; |
| 6933 | } |
| 6934 | |
| 6935 | if (put_user(len, optlen)) |
| 6936 | goto out; |
| 6937 | |
| 6938 | if (copy_to_user(optval, ¶ms, len)) |
| 6939 | goto out; |
| 6940 | |
| 6941 | retval = 0; |
| 6942 | |
| 6943 | out: |
| 6944 | return retval; |
| 6945 | } |
| 6946 | |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 6947 | static int sctp_getsockopt_scheduler(struct sock *sk, int len, |
| 6948 | char __user *optval, |
| 6949 | int __user *optlen) |
| 6950 | { |
| 6951 | struct sctp_assoc_value params; |
| 6952 | struct sctp_association *asoc; |
| 6953 | int retval = -EFAULT; |
| 6954 | |
| 6955 | if (len < sizeof(params)) { |
| 6956 | retval = -EINVAL; |
| 6957 | goto out; |
| 6958 | } |
| 6959 | |
| 6960 | len = sizeof(params); |
| 6961 | if (copy_from_user(¶ms, optval, len)) |
| 6962 | goto out; |
| 6963 | |
| 6964 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 6965 | if (!asoc) { |
| 6966 | retval = -EINVAL; |
| 6967 | goto out; |
| 6968 | } |
| 6969 | |
| 6970 | params.assoc_value = sctp_sched_get_sched(asoc); |
| 6971 | |
| 6972 | if (put_user(len, optlen)) |
| 6973 | goto out; |
| 6974 | |
| 6975 | if (copy_to_user(optval, ¶ms, len)) |
| 6976 | goto out; |
| 6977 | |
| 6978 | retval = 0; |
| 6979 | |
| 6980 | out: |
| 6981 | return retval; |
| 6982 | } |
| 6983 | |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 6984 | static int sctp_getsockopt_scheduler_value(struct sock *sk, int len, |
| 6985 | char __user *optval, |
| 6986 | int __user *optlen) |
| 6987 | { |
| 6988 | struct sctp_stream_value params; |
| 6989 | struct sctp_association *asoc; |
| 6990 | int retval = -EFAULT; |
| 6991 | |
| 6992 | if (len < sizeof(params)) { |
| 6993 | retval = -EINVAL; |
| 6994 | goto out; |
| 6995 | } |
| 6996 | |
| 6997 | len = sizeof(params); |
| 6998 | if (copy_from_user(¶ms, optval, len)) |
| 6999 | goto out; |
| 7000 | |
| 7001 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 7002 | if (!asoc) { |
| 7003 | retval = -EINVAL; |
| 7004 | goto out; |
| 7005 | } |
| 7006 | |
| 7007 | retval = sctp_sched_get_value(asoc, params.stream_id, |
| 7008 | ¶ms.stream_value); |
| 7009 | if (retval) |
| 7010 | goto out; |
| 7011 | |
| 7012 | if (put_user(len, optlen)) { |
| 7013 | retval = -EFAULT; |
| 7014 | goto out; |
| 7015 | } |
| 7016 | |
| 7017 | if (copy_to_user(optval, ¶ms, len)) { |
| 7018 | retval = -EFAULT; |
| 7019 | goto out; |
| 7020 | } |
| 7021 | |
| 7022 | out: |
| 7023 | return retval; |
| 7024 | } |
| 7025 | |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 7026 | static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len, |
| 7027 | char __user *optval, |
| 7028 | int __user *optlen) |
| 7029 | { |
| 7030 | struct sctp_assoc_value params; |
| 7031 | struct sctp_association *asoc; |
| 7032 | int retval = -EFAULT; |
| 7033 | |
| 7034 | if (len < sizeof(params)) { |
| 7035 | retval = -EINVAL; |
| 7036 | goto out; |
| 7037 | } |
| 7038 | |
| 7039 | len = sizeof(params); |
| 7040 | if (copy_from_user(¶ms, optval, len)) |
| 7041 | goto out; |
| 7042 | |
| 7043 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 7044 | if (asoc) { |
| 7045 | params.assoc_value = asoc->intl_enable; |
| 7046 | } else if (!params.assoc_id) { |
| 7047 | struct sctp_sock *sp = sctp_sk(sk); |
| 7048 | |
| 7049 | params.assoc_value = sp->strm_interleave; |
| 7050 | } else { |
| 7051 | retval = -EINVAL; |
| 7052 | goto out; |
| 7053 | } |
| 7054 | |
| 7055 | if (put_user(len, optlen)) |
| 7056 | goto out; |
| 7057 | |
| 7058 | if (copy_to_user(optval, ¶ms, len)) |
| 7059 | goto out; |
| 7060 | |
| 7061 | retval = 0; |
| 7062 | |
| 7063 | out: |
| 7064 | return retval; |
| 7065 | } |
| 7066 | |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 7067 | static int sctp_getsockopt(struct sock *sk, int level, int optname, |
| 7068 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7069 | { |
| 7070 | int retval = 0; |
| 7071 | int len; |
| 7072 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 7073 | pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7074 | |
| 7075 | /* I can hardly begin to describe how wrong this is. This is |
| 7076 | * so broken as to be worse than useless. The API draft |
| 7077 | * REALLY is NOT helpful here... I am not convinced that the |
| 7078 | * semantics of getsockopt() with a level OTHER THAN SOL_SCTP |
| 7079 | * are at all well-founded. |
| 7080 | */ |
| 7081 | if (level != SOL_SCTP) { |
| 7082 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
| 7083 | |
| 7084 | retval = af->getsockopt(sk, level, optname, optval, optlen); |
| 7085 | return retval; |
| 7086 | } |
| 7087 | |
| 7088 | if (get_user(len, optlen)) |
| 7089 | return -EFAULT; |
| 7090 | |
Jiri Slaby | a4b8e71 | 2016-10-21 14:13:24 +0200 | [diff] [blame] | 7091 | if (len < 0) |
| 7092 | return -EINVAL; |
| 7093 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 7094 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | |
| 7096 | switch (optname) { |
| 7097 | case SCTP_STATUS: |
| 7098 | retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); |
| 7099 | break; |
| 7100 | case SCTP_DISABLE_FRAGMENTS: |
| 7101 | retval = sctp_getsockopt_disable_fragments(sk, len, optval, |
| 7102 | optlen); |
| 7103 | break; |
| 7104 | case SCTP_EVENTS: |
| 7105 | retval = sctp_getsockopt_events(sk, len, optval, optlen); |
| 7106 | break; |
| 7107 | case SCTP_AUTOCLOSE: |
| 7108 | retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); |
| 7109 | break; |
| 7110 | case SCTP_SOCKOPT_PEELOFF: |
| 7111 | retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); |
| 7112 | break; |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 7113 | case SCTP_SOCKOPT_PEELOFF_FLAGS: |
| 7114 | retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen); |
| 7115 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7116 | case SCTP_PEER_ADDR_PARAMS: |
| 7117 | retval = sctp_getsockopt_peer_addr_params(sk, len, optval, |
| 7118 | optlen); |
| 7119 | break; |
Shan Wei | 4580ccc | 2011-01-18 22:39:00 +0000 | [diff] [blame] | 7120 | case SCTP_DELAYED_SACK: |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 7121 | retval = sctp_getsockopt_delayed_ack(sk, len, optval, |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 7122 | optlen); |
| 7123 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7124 | case SCTP_INITMSG: |
| 7125 | retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); |
| 7126 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | case SCTP_GET_PEER_ADDRS: |
| 7128 | retval = sctp_getsockopt_peer_addrs(sk, len, optval, |
| 7129 | optlen); |
| 7130 | break; |
| 7131 | case SCTP_GET_LOCAL_ADDRS: |
| 7132 | retval = sctp_getsockopt_local_addrs(sk, len, optval, |
| 7133 | optlen); |
| 7134 | break; |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 7135 | case SCTP_SOCKOPT_CONNECTX3: |
| 7136 | retval = sctp_getsockopt_connectx3(sk, len, optval, optlen); |
| 7137 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7138 | case SCTP_DEFAULT_SEND_PARAM: |
| 7139 | retval = sctp_getsockopt_default_send_param(sk, len, |
| 7140 | optval, optlen); |
| 7141 | break; |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 7142 | case SCTP_DEFAULT_SNDINFO: |
| 7143 | retval = sctp_getsockopt_default_sndinfo(sk, len, |
| 7144 | optval, optlen); |
| 7145 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | case SCTP_PRIMARY_ADDR: |
| 7147 | retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); |
| 7148 | break; |
| 7149 | case SCTP_NODELAY: |
| 7150 | retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); |
| 7151 | break; |
| 7152 | case SCTP_RTOINFO: |
| 7153 | retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); |
| 7154 | break; |
| 7155 | case SCTP_ASSOCINFO: |
| 7156 | retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); |
| 7157 | break; |
| 7158 | case SCTP_I_WANT_MAPPED_V4_ADDR: |
| 7159 | retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); |
| 7160 | break; |
| 7161 | case SCTP_MAXSEG: |
| 7162 | retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); |
| 7163 | break; |
| 7164 | case SCTP_GET_PEER_ADDR_INFO: |
| 7165 | retval = sctp_getsockopt_peer_addr_info(sk, len, optval, |
| 7166 | optlen); |
| 7167 | break; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 7168 | case SCTP_ADAPTATION_LAYER: |
| 7169 | retval = sctp_getsockopt_adaptation_layer(sk, len, optval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7170 | optlen); |
| 7171 | break; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 7172 | case SCTP_CONTEXT: |
| 7173 | retval = sctp_getsockopt_context(sk, len, optval, optlen); |
| 7174 | break; |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 7175 | case SCTP_FRAGMENT_INTERLEAVE: |
| 7176 | retval = sctp_getsockopt_fragment_interleave(sk, len, optval, |
| 7177 | optlen); |
| 7178 | break; |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 7179 | case SCTP_PARTIAL_DELIVERY_POINT: |
| 7180 | retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, |
| 7181 | optlen); |
| 7182 | break; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 7183 | case SCTP_MAX_BURST: |
| 7184 | retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); |
| 7185 | break; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7186 | case SCTP_AUTH_KEY: |
| 7187 | case SCTP_AUTH_CHUNK: |
| 7188 | case SCTP_AUTH_DELETE_KEY: |
| 7189 | retval = -EOPNOTSUPP; |
| 7190 | break; |
| 7191 | case SCTP_HMAC_IDENT: |
| 7192 | retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen); |
| 7193 | break; |
| 7194 | case SCTP_AUTH_ACTIVE_KEY: |
| 7195 | retval = sctp_getsockopt_active_key(sk, len, optval, optlen); |
| 7196 | break; |
| 7197 | case SCTP_PEER_AUTH_CHUNKS: |
| 7198 | retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval, |
| 7199 | optlen); |
| 7200 | break; |
| 7201 | case SCTP_LOCAL_AUTH_CHUNKS: |
| 7202 | retval = sctp_getsockopt_local_auth_chunks(sk, len, optval, |
| 7203 | optlen); |
| 7204 | break; |
Wei Yongjun | aea3c5c | 2008-12-25 16:57:24 -0800 | [diff] [blame] | 7205 | case SCTP_GET_ASSOC_NUMBER: |
| 7206 | retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen); |
| 7207 | break; |
Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 7208 | case SCTP_GET_ASSOC_ID_LIST: |
| 7209 | retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen); |
| 7210 | break; |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 7211 | case SCTP_AUTO_ASCONF: |
| 7212 | retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen); |
| 7213 | break; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7214 | case SCTP_PEER_ADDR_THLDS: |
| 7215 | retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen); |
| 7216 | break; |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 7217 | case SCTP_GET_ASSOC_STATS: |
| 7218 | retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen); |
| 7219 | break; |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 7220 | case SCTP_RECVRCVINFO: |
| 7221 | retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen); |
| 7222 | break; |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 7223 | case SCTP_RECVNXTINFO: |
| 7224 | retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen); |
| 7225 | break; |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 7226 | case SCTP_PR_SUPPORTED: |
| 7227 | retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen); |
| 7228 | break; |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 7229 | case SCTP_DEFAULT_PRINFO: |
| 7230 | retval = sctp_getsockopt_default_prinfo(sk, len, optval, |
| 7231 | optlen); |
| 7232 | break; |
Xin Long | 826d253 | 2016-07-09 19:47:42 +0800 | [diff] [blame] | 7233 | case SCTP_PR_ASSOC_STATUS: |
| 7234 | retval = sctp_getsockopt_pr_assocstatus(sk, len, optval, |
| 7235 | optlen); |
| 7236 | break; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7237 | case SCTP_PR_STREAM_STATUS: |
| 7238 | retval = sctp_getsockopt_pr_streamstatus(sk, len, optval, |
| 7239 | optlen); |
| 7240 | break; |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 7241 | case SCTP_RECONFIG_SUPPORTED: |
| 7242 | retval = sctp_getsockopt_reconfig_supported(sk, len, optval, |
| 7243 | optlen); |
| 7244 | break; |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 7245 | case SCTP_ENABLE_STREAM_RESET: |
| 7246 | retval = sctp_getsockopt_enable_strreset(sk, len, optval, |
| 7247 | optlen); |
| 7248 | break; |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 7249 | case SCTP_STREAM_SCHEDULER: |
| 7250 | retval = sctp_getsockopt_scheduler(sk, len, optval, |
| 7251 | optlen); |
| 7252 | break; |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 7253 | case SCTP_STREAM_SCHEDULER_VALUE: |
| 7254 | retval = sctp_getsockopt_scheduler_value(sk, len, optval, |
| 7255 | optlen); |
| 7256 | break; |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 7257 | case SCTP_INTERLEAVING_SUPPORTED: |
| 7258 | retval = sctp_getsockopt_interleaving_supported(sk, len, optval, |
| 7259 | optlen); |
| 7260 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7261 | default: |
| 7262 | retval = -ENOPROTOOPT; |
| 7263 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 7264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7265 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 7266 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7267 | return retval; |
| 7268 | } |
| 7269 | |
Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 7270 | static int sctp_hash(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7271 | { |
| 7272 | /* STUB */ |
Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 7273 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7274 | } |
| 7275 | |
| 7276 | static void sctp_unhash(struct sock *sk) |
| 7277 | { |
| 7278 | /* STUB */ |
| 7279 | } |
| 7280 | |
| 7281 | /* Check if port is acceptable. Possibly find first available port. |
| 7282 | * |
| 7283 | * The port hash table (contained in the 'global' SCTP protocol storage |
| 7284 | * returned by struct sctp_protocol *sctp_get_protocol()). The hash |
| 7285 | * table is an array of 4096 lists (sctp_bind_hashbucket). Each |
| 7286 | * list (the list number is the port number hashed out, so as you |
| 7287 | * would expect from a hash function, all the ports in a given list have |
| 7288 | * such a number that hashes out to the same list number; you were |
| 7289 | * expecting that, right?); so each list has a set of ports, with a |
| 7290 | * link to the socket (struct sock) that uses it, the port number and |
| 7291 | * a fastreuse flag (FIXME: NPI ipg). |
| 7292 | */ |
| 7293 | static struct sctp_bind_bucket *sctp_bucket_create( |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7294 | struct sctp_bind_hashbucket *head, struct net *, unsigned short snum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7295 | |
| 7296 | static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) |
| 7297 | { |
| 7298 | struct sctp_bind_hashbucket *head; /* hash list */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7299 | struct sctp_bind_bucket *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | unsigned short snum; |
| 7301 | int ret; |
| 7302 | |
Al Viro | 04afd8b | 2006-11-20 17:02:01 -0800 | [diff] [blame] | 7303 | snum = ntohs(addr->v4.sin_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7304 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 7305 | pr_debug("%s: begins, snum:%d\n", __func__, snum); |
| 7306 | |
wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 7307 | local_bh_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7308 | |
| 7309 | if (snum == 0) { |
Stephen Hemminger | 0639300 | 2007-10-10 17:30:18 -0700 | [diff] [blame] | 7310 | /* Search for an available port. */ |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 7311 | int low, high, remaining, index; |
| 7312 | unsigned int rover; |
WANG Cong | 122ff24 | 2014-05-12 16:04:53 -0700 | [diff] [blame] | 7313 | struct net *net = sock_net(sk); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 7314 | |
WANG Cong | 122ff24 | 2014-05-12 16:04:53 -0700 | [diff] [blame] | 7315 | inet_get_local_port_range(net, &low, &high); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 7316 | remaining = (high - low) + 1; |
Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 7317 | rover = prandom_u32() % remaining + low; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7319 | do { |
| 7320 | rover++; |
| 7321 | if ((rover < low) || (rover > high)) |
| 7322 | rover = low; |
WANG Cong | 122ff24 | 2014-05-12 16:04:53 -0700 | [diff] [blame] | 7323 | if (inet_is_local_reserved_port(net, rover)) |
Amerigo Wang | e3826f1 | 2010-05-05 00:27:06 +0000 | [diff] [blame] | 7324 | continue; |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7325 | index = sctp_phashfn(sock_net(sk), rover); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7326 | head = &sctp_port_hashtable[index]; |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 7327 | spin_lock(&head->lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7328 | sctp_for_each_hentry(pp, &head->chain) |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7329 | if ((pp->port == rover) && |
| 7330 | net_eq(sock_net(sk), pp->net)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7331 | goto next; |
| 7332 | break; |
| 7333 | next: |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 7334 | spin_unlock(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7335 | } while (--remaining > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7336 | |
| 7337 | /* Exhausted local port range during search? */ |
| 7338 | ret = 1; |
| 7339 | if (remaining <= 0) |
| 7340 | goto fail; |
| 7341 | |
| 7342 | /* OK, here is the one we will use. HEAD (the port |
| 7343 | * hash table list entry) is non-NULL and we hold it's |
| 7344 | * mutex. |
| 7345 | */ |
| 7346 | snum = rover; |
| 7347 | } else { |
| 7348 | /* We are given an specific port number; we verify |
| 7349 | * that it is not being used. If it is used, we will |
| 7350 | * exahust the search in the hash list corresponding |
| 7351 | * to the port number (snum) - we detect that with the |
| 7352 | * port iterator, pp being NULL. |
| 7353 | */ |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7354 | head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)]; |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 7355 | spin_lock(&head->lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7356 | sctp_for_each_hentry(pp, &head->chain) { |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7357 | if ((pp->port == snum) && net_eq(pp->net, sock_net(sk))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | goto pp_found; |
| 7359 | } |
| 7360 | } |
| 7361 | pp = NULL; |
| 7362 | goto pp_not_found; |
| 7363 | pp_found: |
| 7364 | if (!hlist_empty(&pp->owner)) { |
| 7365 | /* We had a port hash table hit - there is an |
| 7366 | * available port (pp != NULL) and it is being |
| 7367 | * used by other socket (pp->owner not empty); that other |
| 7368 | * socket is going to be sk2. |
| 7369 | */ |
| 7370 | int reuse = sk->sk_reuse; |
| 7371 | struct sock *sk2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7372 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 7373 | pr_debug("%s: found a possible match\n", __func__); |
| 7374 | |
Vlad Yasevich | ce5325c | 2007-05-04 13:34:49 -0700 | [diff] [blame] | 7375 | if (pp->fastreuse && sk->sk_reuse && |
| 7376 | sk->sk_state != SCTP_SS_LISTENING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7377 | goto success; |
| 7378 | |
| 7379 | /* Run through the list of sockets bound to the port |
| 7380 | * (pp->port) [via the pointers bind_next and |
| 7381 | * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, |
| 7382 | * we get the endpoint they describe and run through |
| 7383 | * the endpoint's list of IP (v4 or v6) addresses, |
| 7384 | * comparing each of the addresses with the address of |
| 7385 | * the socket sk. If we find a match, then that means |
| 7386 | * that this port/socket (sk) combination are already |
| 7387 | * in an endpoint. |
| 7388 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 7389 | sk_for_each_bound(sk2, &pp->owner) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7390 | struct sctp_endpoint *ep2; |
| 7391 | ep2 = sctp_sk(sk2)->ep; |
| 7392 | |
Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 7393 | if (sk == sk2 || |
| 7394 | (reuse && sk2->sk_reuse && |
| 7395 | sk2->sk_state != SCTP_SS_LISTENING)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7396 | continue; |
| 7397 | |
Vlad Yasevich | 7dab83d | 2008-07-18 23:05:40 -0700 | [diff] [blame] | 7398 | if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr, |
| 7399 | sctp_sk(sk2), sctp_sk(sk))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7400 | ret = (long)sk2; |
| 7401 | goto fail_unlock; |
| 7402 | } |
| 7403 | } |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 7404 | |
| 7405 | pr_debug("%s: found a match\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7406 | } |
| 7407 | pp_not_found: |
| 7408 | /* If there was a hash table miss, create a new port. */ |
| 7409 | ret = 1; |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7410 | if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7411 | goto fail_unlock; |
| 7412 | |
| 7413 | /* In either case (hit or miss), make sure fastreuse is 1 only |
| 7414 | * if sk->sk_reuse is too (that is, if the caller requested |
| 7415 | * SO_REUSEADDR on this socket -sk-). |
| 7416 | */ |
Vlad Yasevich | ce5325c | 2007-05-04 13:34:49 -0700 | [diff] [blame] | 7417 | if (hlist_empty(&pp->owner)) { |
| 7418 | if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING) |
| 7419 | pp->fastreuse = 1; |
| 7420 | else |
| 7421 | pp->fastreuse = 0; |
| 7422 | } else if (pp->fastreuse && |
| 7423 | (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7424 | pp->fastreuse = 0; |
| 7425 | |
| 7426 | /* We are set, so fill up all the data in the hash table |
| 7427 | * entry, tie the socket list information with the rest of the |
| 7428 | * sockets FIXME: Blurry, NPI (ipg). |
| 7429 | */ |
| 7430 | success: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7431 | if (!sctp_sk(sk)->bind_hash) { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 7432 | inet_sk(sk)->inet_num = snum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7433 | sk_add_bind_node(sk, &pp->owner); |
| 7434 | sctp_sk(sk)->bind_hash = pp; |
| 7435 | } |
| 7436 | ret = 0; |
| 7437 | |
| 7438 | fail_unlock: |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 7439 | spin_unlock(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7440 | |
| 7441 | fail: |
wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 7442 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7443 | return ret; |
| 7444 | } |
| 7445 | |
| 7446 | /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral |
| 7447 | * port is requested. |
| 7448 | */ |
| 7449 | static int sctp_get_port(struct sock *sk, unsigned short snum) |
| 7450 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | union sctp_addr addr; |
| 7452 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
| 7453 | |
| 7454 | /* Set up a dummy address struct from the sk. */ |
| 7455 | af->from_sk(&addr, sk); |
| 7456 | addr.v4.sin_port = htons(snum); |
| 7457 | |
| 7458 | /* Note: sk->sk_num gets filled in if ephemeral port request. */ |
Daniel Borkmann | 62208f1 | 2013-06-25 18:17:30 +0200 | [diff] [blame] | 7459 | return !!sctp_get_port_local(sk, &addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7460 | } |
| 7461 | |
| 7462 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7463 | * Move a socket to LISTENING state. |
| 7464 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 7465 | static int sctp_listen_start(struct sock *sk, int backlog) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7466 | { |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7467 | struct sctp_sock *sp = sctp_sk(sk); |
| 7468 | struct sctp_endpoint *ep = sp->ep; |
Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 7469 | struct crypto_shash *tfm = NULL; |
Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 7470 | char alg[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7471 | |
| 7472 | /* Allocate HMAC for generating cookie. */ |
Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 7473 | if (!sp->hmac && sp->sctp_hmac_alg) { |
| 7474 | sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg); |
Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 7475 | tfm = crypto_alloc_shash(alg, 0, 0); |
Vlad Yasevich | 8dc4984 | 2007-05-09 13:50:20 -0700 | [diff] [blame] | 7476 | if (IS_ERR(tfm)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 7477 | net_info_ratelimited("failed to load transform for %s: %ld\n", |
Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 7478 | sp->sctp_hmac_alg, PTR_ERR(tfm)); |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7479 | return -ENOSYS; |
| 7480 | } |
| 7481 | sctp_sk(sk)->hmac = tfm; |
| 7482 | } |
| 7483 | |
| 7484 | /* |
| 7485 | * If a bind() or sctp_bindx() is not called prior to a listen() |
| 7486 | * call that allows new associations to be accepted, the system |
| 7487 | * picks an ephemeral port and will choose an address set equivalent |
| 7488 | * to binding with a wildcard address. |
| 7489 | * |
| 7490 | * This is not currently spelled out in the SCTP sockets |
| 7491 | * extensions draft, but follows the practice as seen in TCP |
| 7492 | * sockets. |
| 7493 | * |
| 7494 | */ |
| 7495 | sk->sk_state = SCTP_SS_LISTENING; |
| 7496 | if (!ep->base.bind_addr.port) { |
| 7497 | if (sctp_autobind(sk)) |
| 7498 | return -EAGAIN; |
| 7499 | } else { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 7500 | if (sctp_get_port(sk, inet_sk(sk)->inet_num)) { |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7501 | sk->sk_state = SCTP_SS_CLOSED; |
| 7502 | return -EADDRINUSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7503 | } |
| 7504 | } |
| 7505 | |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7506 | sk->sk_max_ack_backlog = backlog; |
| 7507 | sctp_hash_endpoint(ep); |
| 7508 | return 0; |
| 7509 | } |
| 7510 | |
| 7511 | /* |
| 7512 | * 4.1.3 / 5.1.3 listen() |
| 7513 | * |
| 7514 | * By default, new associations are not accepted for UDP style sockets. |
| 7515 | * An application uses listen() to mark a socket as being able to |
| 7516 | * accept new associations. |
| 7517 | * |
| 7518 | * On TCP style sockets, applications use listen() to ready the SCTP |
| 7519 | * endpoint for accepting inbound associations. |
| 7520 | * |
| 7521 | * On both types of endpoints a backlog of '0' disables listening. |
| 7522 | * |
| 7523 | * Move a socket to LISTENING state. |
| 7524 | */ |
| 7525 | int sctp_inet_listen(struct socket *sock, int backlog) |
| 7526 | { |
| 7527 | struct sock *sk = sock->sk; |
| 7528 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
| 7529 | int err = -EINVAL; |
| 7530 | |
| 7531 | if (unlikely(backlog < 0)) |
| 7532 | return err; |
| 7533 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 7534 | lock_sock(sk); |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7535 | |
| 7536 | /* Peeled-off sockets are not allowed to listen(). */ |
| 7537 | if (sctp_style(sk, UDP_HIGH_BANDWIDTH)) |
| 7538 | goto out; |
| 7539 | |
| 7540 | if (sock->state != SS_UNCONNECTED) |
| 7541 | goto out; |
| 7542 | |
Xin Long | 34b2789 | 2017-04-06 13:10:52 +0800 | [diff] [blame] | 7543 | if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED)) |
| 7544 | goto out; |
| 7545 | |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7546 | /* If backlog is zero, disable listening. */ |
| 7547 | if (!backlog) { |
| 7548 | if (sctp_sstate(sk, CLOSED)) |
| 7549 | goto out; |
| 7550 | |
| 7551 | err = 0; |
| 7552 | sctp_unhash_endpoint(ep); |
| 7553 | sk->sk_state = SCTP_SS_CLOSED; |
| 7554 | if (sk->sk_reuse) |
| 7555 | sctp_sk(sk)->bind_hash->fastreuse = 1; |
| 7556 | goto out; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 7557 | } |
| 7558 | |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7559 | /* If we are already listening, just update the backlog */ |
| 7560 | if (sctp_sstate(sk, LISTENING)) |
| 7561 | sk->sk_max_ack_backlog = backlog; |
| 7562 | else { |
| 7563 | err = sctp_listen_start(sk, backlog); |
| 7564 | if (err) |
| 7565 | goto out; |
| 7566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7567 | |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 7568 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7569 | out: |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 7570 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7571 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7572 | } |
| 7573 | |
| 7574 | /* |
| 7575 | * This function is done by modeling the current datagram_poll() and the |
| 7576 | * tcp_poll(). Note that, based on these implementations, we don't |
| 7577 | * lock the socket in this function, even though it seems that, |
| 7578 | * ideally, locking or some other mechanisms can be used to ensure |
Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 7579 | * the integrity of the counters (sndbuf and wmem_alloc) used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7580 | * in this place. We assume that we don't need locks either until proven |
| 7581 | * otherwise. |
| 7582 | * |
| 7583 | * Another thing to note is that we include the Async I/O support |
| 7584 | * here, again, by modeling the current TCP/UDP code. We don't have |
| 7585 | * a good way to test with it yet. |
| 7586 | */ |
| 7587 | unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait) |
| 7588 | { |
| 7589 | struct sock *sk = sock->sk; |
| 7590 | struct sctp_sock *sp = sctp_sk(sk); |
| 7591 | unsigned int mask; |
| 7592 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 7593 | poll_wait(file, sk_sleep(sk), wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7594 | |
Marcelo Ricardo Leitner | 486bdee | 2016-04-12 18:11:31 -0300 | [diff] [blame] | 7595 | sock_rps_record_flow(sk); |
| 7596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7597 | /* A TCP-style listening socket becomes readable when the accept queue |
| 7598 | * is not empty. |
| 7599 | */ |
| 7600 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) |
| 7601 | return (!list_empty(&sp->ep->asocs)) ? |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 7602 | (POLLIN | POLLRDNORM) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7603 | |
| 7604 | mask = 0; |
| 7605 | |
| 7606 | /* Is there any exceptional events? */ |
| 7607 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
Keller, Jacob E | 7d4c04f | 2013-03-28 11:19:25 +0000 | [diff] [blame] | 7608 | mask |= POLLERR | |
Daniel Borkmann | a0fb05d | 2013-09-07 16:44:59 +0200 | [diff] [blame] | 7609 | (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0); |
Davide Libenzi | f348d70 | 2006-03-25 03:07:39 -0800 | [diff] [blame] | 7610 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
Eric Dumazet | db40980 | 2010-09-06 11:13:50 +0000 | [diff] [blame] | 7611 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7612 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 7613 | mask |= POLLHUP; |
| 7614 | |
| 7615 | /* Is it readable? Reconsider this code with TCP-style support. */ |
Eric Dumazet | db40980 | 2010-09-06 11:13:50 +0000 | [diff] [blame] | 7616 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7617 | mask |= POLLIN | POLLRDNORM; |
| 7618 | |
| 7619 | /* The association is either gone or not ready. */ |
| 7620 | if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) |
| 7621 | return mask; |
| 7622 | |
| 7623 | /* Is it writable? */ |
| 7624 | if (sctp_writeable(sk)) { |
| 7625 | mask |= POLLOUT | POLLWRNORM; |
| 7626 | } else { |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 7627 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7628 | /* |
| 7629 | * Since the socket is not locked, the buffer |
| 7630 | * might be made available after the writeable check and |
| 7631 | * before the bit is set. This could cause a lost I/O |
| 7632 | * signal. tcp_poll() has a race breaker for this race |
| 7633 | * condition. Based on their implementation, we put |
| 7634 | * in the following code to cover it as well. |
| 7635 | */ |
| 7636 | if (sctp_writeable(sk)) |
| 7637 | mask |= POLLOUT | POLLWRNORM; |
| 7638 | } |
| 7639 | return mask; |
| 7640 | } |
| 7641 | |
| 7642 | /******************************************************************** |
| 7643 | * 2nd Level Abstractions |
| 7644 | ********************************************************************/ |
| 7645 | |
| 7646 | static struct sctp_bind_bucket *sctp_bucket_create( |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7647 | struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7648 | { |
| 7649 | struct sctp_bind_bucket *pp; |
| 7650 | |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 7651 | pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7652 | if (pp) { |
Li Zefan | 935a7f6 | 2008-04-10 01:58:06 -0700 | [diff] [blame] | 7653 | SCTP_DBG_OBJCNT_INC(bind_bucket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7654 | pp->port = snum; |
| 7655 | pp->fastreuse = 0; |
| 7656 | INIT_HLIST_HEAD(&pp->owner); |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7657 | pp->net = net; |
Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 7658 | hlist_add_head(&pp->node, &head->chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7659 | } |
| 7660 | return pp; |
| 7661 | } |
| 7662 | |
| 7663 | /* Caller must hold hashbucket lock for this tb with local BH disabled */ |
| 7664 | static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) |
| 7665 | { |
Sridhar Samudrala | 37fa687 | 2006-07-21 14:45:47 -0700 | [diff] [blame] | 7666 | if (pp && hlist_empty(&pp->owner)) { |
Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 7667 | __hlist_del(&pp->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7668 | kmem_cache_free(sctp_bucket_cachep, pp); |
| 7669 | SCTP_DBG_OBJCNT_DEC(bind_bucket); |
| 7670 | } |
| 7671 | } |
| 7672 | |
| 7673 | /* Release this socket's reference to a local port. */ |
| 7674 | static inline void __sctp_put_port(struct sock *sk) |
| 7675 | { |
| 7676 | struct sctp_bind_hashbucket *head = |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 7677 | &sctp_port_hashtable[sctp_phashfn(sock_net(sk), |
| 7678 | inet_sk(sk)->inet_num)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7679 | struct sctp_bind_bucket *pp; |
| 7680 | |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 7681 | spin_lock(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7682 | pp = sctp_sk(sk)->bind_hash; |
| 7683 | __sk_del_bind_node(sk); |
| 7684 | sctp_sk(sk)->bind_hash = NULL; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 7685 | inet_sk(sk)->inet_num = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7686 | sctp_bucket_destroy(pp); |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 7687 | spin_unlock(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7688 | } |
| 7689 | |
| 7690 | void sctp_put_port(struct sock *sk) |
| 7691 | { |
wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 7692 | local_bh_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7693 | __sctp_put_port(sk); |
wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 7694 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7695 | } |
| 7696 | |
| 7697 | /* |
| 7698 | * The system picks an ephemeral port and choose an address set equivalent |
| 7699 | * to binding with a wildcard address. |
| 7700 | * One of those addresses will be the primary address for the association. |
| 7701 | * This automatically enables the multihoming capability of SCTP. |
| 7702 | */ |
| 7703 | static int sctp_autobind(struct sock *sk) |
| 7704 | { |
| 7705 | union sctp_addr autoaddr; |
| 7706 | struct sctp_af *af; |
Al Viro | 6fbfa9f | 2006-11-20 17:24:53 -0800 | [diff] [blame] | 7707 | __be16 port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7708 | |
| 7709 | /* Initialize a local sockaddr structure to INADDR_ANY. */ |
| 7710 | af = sctp_sk(sk)->pf->af; |
| 7711 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 7712 | port = htons(inet_sk(sk)->inet_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7713 | af->inaddr_any(&autoaddr, port); |
| 7714 | |
| 7715 | return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); |
| 7716 | } |
| 7717 | |
| 7718 | /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation. |
| 7719 | * |
| 7720 | * From RFC 2292 |
| 7721 | * 4.2 The cmsghdr Structure * |
| 7722 | * |
| 7723 | * When ancillary data is sent or received, any number of ancillary data |
| 7724 | * objects can be specified by the msg_control and msg_controllen members of |
| 7725 | * the msghdr structure, because each object is preceded by |
| 7726 | * a cmsghdr structure defining the object's length (the cmsg_len member). |
| 7727 | * Historically Berkeley-derived implementations have passed only one object |
| 7728 | * at a time, but this API allows multiple objects to be |
| 7729 | * passed in a single call to sendmsg() or recvmsg(). The following example |
| 7730 | * shows two ancillary data objects in a control buffer. |
| 7731 | * |
| 7732 | * |<--------------------------- msg_controllen -------------------------->| |
| 7733 | * | | |
| 7734 | * |
| 7735 | * |<----- ancillary data object ----->|<----- ancillary data object ----->| |
| 7736 | * |
| 7737 | * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| |
| 7738 | * | | | |
| 7739 | * |
| 7740 | * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| | |
| 7741 | * |
| 7742 | * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| | |
| 7743 | * | | | | | |
| 7744 | * |
| 7745 | * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ |
| 7746 | * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX| |
| 7747 | * |
| 7748 | * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX| |
| 7749 | * |
| 7750 | * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ |
| 7751 | * ^ |
| 7752 | * | |
| 7753 | * |
| 7754 | * msg_control |
| 7755 | * points here |
| 7756 | */ |
Xin Long | a05437a | 2017-08-11 10:23:48 +0800 | [diff] [blame] | 7757 | static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7758 | { |
Vlad Yasevich | ab38fb0 | 2008-04-12 18:40:06 -0700 | [diff] [blame] | 7759 | struct msghdr *my_msg = (struct msghdr *)msg; |
Xin Long | a05437a | 2017-08-11 10:23:48 +0800 | [diff] [blame] | 7760 | struct cmsghdr *cmsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7761 | |
Gu Zheng | f95b414 | 2014-12-11 11:22:04 +0800 | [diff] [blame] | 7762 | for_each_cmsghdr(cmsg, my_msg) { |
Vlad Yasevich | ab38fb0 | 2008-04-12 18:40:06 -0700 | [diff] [blame] | 7763 | if (!CMSG_OK(my_msg, cmsg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7764 | return -EINVAL; |
| 7765 | |
| 7766 | /* Should we parse this header or ignore? */ |
| 7767 | if (cmsg->cmsg_level != IPPROTO_SCTP) |
| 7768 | continue; |
| 7769 | |
| 7770 | /* Strictly check lengths following example in SCM code. */ |
| 7771 | switch (cmsg->cmsg_type) { |
| 7772 | case SCTP_INIT: |
| 7773 | /* SCTP Socket API Extension |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 7774 | * 5.3.1 SCTP Initiation Structure (SCTP_INIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7775 | * |
| 7776 | * This cmsghdr structure provides information for |
| 7777 | * initializing new SCTP associations with sendmsg(). |
| 7778 | * The SCTP_INITMSG socket option uses this same data |
| 7779 | * structure. This structure is not used for |
| 7780 | * recvmsg(). |
| 7781 | * |
| 7782 | * cmsg_level cmsg_type cmsg_data[] |
| 7783 | * ------------ ------------ ---------------------- |
| 7784 | * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg |
| 7785 | */ |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 7786 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7787 | return -EINVAL; |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 7788 | |
| 7789 | cmsgs->init = CMSG_DATA(cmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7790 | break; |
| 7791 | |
| 7792 | case SCTP_SNDRCV: |
| 7793 | /* SCTP Socket API Extension |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 7794 | * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7795 | * |
| 7796 | * This cmsghdr structure specifies SCTP options for |
| 7797 | * sendmsg() and describes SCTP header information |
| 7798 | * about a received message through recvmsg(). |
| 7799 | * |
| 7800 | * cmsg_level cmsg_type cmsg_data[] |
| 7801 | * ------------ ------------ ---------------------- |
| 7802 | * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo |
| 7803 | */ |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 7804 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7805 | return -EINVAL; |
| 7806 | |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 7807 | cmsgs->srinfo = CMSG_DATA(cmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7808 | |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 7809 | if (cmsgs->srinfo->sinfo_flags & |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 7810 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | |
Xin Long | a6c2f79 | 2016-07-09 19:47:43 +0800 | [diff] [blame] | 7811 | SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK | |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 7812 | SCTP_ABORT | SCTP_EOF)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7813 | return -EINVAL; |
| 7814 | break; |
| 7815 | |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 7816 | case SCTP_SNDINFO: |
| 7817 | /* SCTP Socket API Extension |
| 7818 | * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO) |
| 7819 | * |
| 7820 | * This cmsghdr structure specifies SCTP options for |
| 7821 | * sendmsg(). This structure and SCTP_RCVINFO replaces |
| 7822 | * SCTP_SNDRCV which has been deprecated. |
| 7823 | * |
| 7824 | * cmsg_level cmsg_type cmsg_data[] |
| 7825 | * ------------ ------------ --------------------- |
| 7826 | * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo |
| 7827 | */ |
| 7828 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo))) |
| 7829 | return -EINVAL; |
| 7830 | |
| 7831 | cmsgs->sinfo = CMSG_DATA(cmsg); |
| 7832 | |
| 7833 | if (cmsgs->sinfo->snd_flags & |
| 7834 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | |
Xin Long | a6c2f79 | 2016-07-09 19:47:43 +0800 | [diff] [blame] | 7835 | SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK | |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 7836 | SCTP_ABORT | SCTP_EOF)) |
| 7837 | return -EINVAL; |
| 7838 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7839 | default: |
| 7840 | return -EINVAL; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 7841 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7842 | } |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 7843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7844 | return 0; |
| 7845 | } |
| 7846 | |
| 7847 | /* |
| 7848 | * Wait for a packet.. |
| 7849 | * Note: This function is the same function as in core/datagram.c |
| 7850 | * with a few modifications to make lksctp work. |
| 7851 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 7852 | static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7853 | { |
| 7854 | int error; |
| 7855 | DEFINE_WAIT(wait); |
| 7856 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 7857 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7858 | |
| 7859 | /* Socket errors? */ |
| 7860 | error = sock_error(sk); |
| 7861 | if (error) |
| 7862 | goto out; |
| 7863 | |
| 7864 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 7865 | goto ready; |
| 7866 | |
| 7867 | /* Socket shut down? */ |
| 7868 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 7869 | goto out; |
| 7870 | |
| 7871 | /* Sequenced packets can come disconnected. If so we report the |
| 7872 | * problem. |
| 7873 | */ |
| 7874 | error = -ENOTCONN; |
| 7875 | |
| 7876 | /* Is there a good reason to think that we may receive some data? */ |
| 7877 | if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) |
| 7878 | goto out; |
| 7879 | |
| 7880 | /* Handle signals. */ |
| 7881 | if (signal_pending(current)) |
| 7882 | goto interrupted; |
| 7883 | |
| 7884 | /* Let another process have a go. Since we are going to sleep |
| 7885 | * anyway. Note: This may cause odd behaviors if the message |
| 7886 | * does not fit in the user's buffer, but this seems to be the |
| 7887 | * only way to honor MSG_DONTWAIT realistically. |
| 7888 | */ |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 7889 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7890 | *timeo_p = schedule_timeout(*timeo_p); |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 7891 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7892 | |
| 7893 | ready: |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 7894 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7895 | return 0; |
| 7896 | |
| 7897 | interrupted: |
| 7898 | error = sock_intr_errno(*timeo_p); |
| 7899 | |
| 7900 | out: |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 7901 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7902 | *err = error; |
| 7903 | return error; |
| 7904 | } |
| 7905 | |
| 7906 | /* Receive a datagram. |
| 7907 | * Note: This is pretty much the same routine as in core/datagram.c |
| 7908 | * with a few changes to make lksctp work. |
| 7909 | */ |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 7910 | struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, |
| 7911 | int noblock, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7912 | { |
| 7913 | int error; |
| 7914 | struct sk_buff *skb; |
| 7915 | long timeo; |
| 7916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7917 | timeo = sock_rcvtimeo(sk, noblock); |
| 7918 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 7919 | pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo, |
| 7920 | MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7921 | |
| 7922 | do { |
| 7923 | /* Again only user level code calls this function, |
| 7924 | * so nothing interrupt level |
| 7925 | * will suddenly eat the receive_queue. |
| 7926 | * |
| 7927 | * Look at current nfs client by the way... |
David Shwatrz | 8917a3c | 2010-12-02 09:01:55 +0000 | [diff] [blame] | 7928 | * However, this function was correct in any case. 8) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7929 | */ |
| 7930 | if (flags & MSG_PEEK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7931 | skb = skb_peek(&sk->sk_receive_queue); |
| 7932 | if (skb) |
Reshetova, Elena | 6335479 | 2017-06-30 13:07:58 +0300 | [diff] [blame] | 7933 | refcount_inc(&skb->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7934 | } else { |
Marcelo Ricardo Leitner | 311b217 | 2016-04-13 19:12:29 -0300 | [diff] [blame] | 7935 | skb = __skb_dequeue(&sk->sk_receive_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7936 | } |
| 7937 | |
| 7938 | if (skb) |
| 7939 | return skb; |
| 7940 | |
Neil Horman | 6736dc3 | 2005-12-02 20:30:06 -0800 | [diff] [blame] | 7941 | /* Caller is allowed not to check sk->sk_err before calling. */ |
| 7942 | error = sock_error(sk); |
| 7943 | if (error) |
| 7944 | goto no_packet; |
| 7945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7946 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 7947 | break; |
| 7948 | |
Alexander Duyck | 2b5cd0d | 2017-03-24 10:08:12 -0700 | [diff] [blame] | 7949 | if (sk_can_busy_loop(sk)) { |
| 7950 | sk_busy_loop(sk, noblock); |
| 7951 | |
| 7952 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 7953 | continue; |
| 7954 | } |
Neil Horman | 8465a5f | 2014-04-17 15:26:51 -0400 | [diff] [blame] | 7955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7956 | /* User doesn't want to wait. */ |
| 7957 | error = -EAGAIN; |
| 7958 | if (!timeo) |
| 7959 | goto no_packet; |
| 7960 | } while (sctp_wait_for_packet(sk, err, &timeo) == 0); |
| 7961 | |
| 7962 | return NULL; |
| 7963 | |
| 7964 | no_packet: |
| 7965 | *err = error; |
| 7966 | return NULL; |
| 7967 | } |
| 7968 | |
| 7969 | /* If sndbuf has changed, wake up per association sndbuf waiters. */ |
| 7970 | static void __sctp_write_space(struct sctp_association *asoc) |
| 7971 | { |
| 7972 | struct sock *sk = asoc->base.sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7973 | |
Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 7974 | if (sctp_wspace(asoc) <= 0) |
| 7975 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7976 | |
Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 7977 | if (waitqueue_active(&asoc->wait)) |
| 7978 | wake_up_interruptible(&asoc->wait); |
Eric Dumazet | eaefd110 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 7979 | |
Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 7980 | if (sctp_writeable(sk)) { |
| 7981 | struct socket_wq *wq; |
| 7982 | |
| 7983 | rcu_read_lock(); |
| 7984 | wq = rcu_dereference(sk->sk_wq); |
| 7985 | if (wq) { |
| 7986 | if (waitqueue_active(&wq->wait)) |
| 7987 | wake_up_interruptible(&wq->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7988 | |
| 7989 | /* Note that we try to include the Async I/O support |
| 7990 | * here by modeling from the current TCP/UDP code. |
| 7991 | * We have not tested with it yet. |
| 7992 | */ |
Eric Dumazet | eaefd110 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 7993 | if (!(sk->sk_shutdown & SEND_SHUTDOWN)) |
Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 7994 | sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7995 | } |
Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 7996 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7997 | } |
| 7998 | } |
| 7999 | |
Daniel Borkmann | 52c35be | 2014-04-08 17:26:13 +0200 | [diff] [blame] | 8000 | static void sctp_wake_up_waiters(struct sock *sk, |
| 8001 | struct sctp_association *asoc) |
| 8002 | { |
| 8003 | struct sctp_association *tmp = asoc; |
| 8004 | |
| 8005 | /* We do accounting for the sndbuf space per association, |
| 8006 | * so we only need to wake our own association. |
| 8007 | */ |
| 8008 | if (asoc->ep->sndbuf_policy) |
| 8009 | return __sctp_write_space(asoc); |
| 8010 | |
Daniel Borkmann | 1e1cdf8 | 2014-04-09 16:10:20 +0200 | [diff] [blame] | 8011 | /* If association goes down and is just flushing its |
| 8012 | * outq, then just normally notify others. |
| 8013 | */ |
| 8014 | if (asoc->base.dead) |
| 8015 | return sctp_write_space(sk); |
| 8016 | |
Daniel Borkmann | 52c35be | 2014-04-08 17:26:13 +0200 | [diff] [blame] | 8017 | /* Accounting for the sndbuf space is per socket, so we |
| 8018 | * need to wake up others, try to be fair and in case of |
| 8019 | * other associations, let them have a go first instead |
| 8020 | * of just doing a sctp_write_space() call. |
| 8021 | * |
| 8022 | * Note that we reach sctp_wake_up_waiters() only when |
| 8023 | * associations free up queued chunks, thus we are under |
| 8024 | * lock and the list of associations on a socket is |
| 8025 | * guaranteed not to change. |
| 8026 | */ |
| 8027 | for (tmp = list_next_entry(tmp, asocs); 1; |
| 8028 | tmp = list_next_entry(tmp, asocs)) { |
| 8029 | /* Manually skip the head element. */ |
| 8030 | if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs)) |
| 8031 | continue; |
| 8032 | /* Wake up association. */ |
| 8033 | __sctp_write_space(tmp); |
| 8034 | /* We've reached the end. */ |
| 8035 | if (tmp == asoc) |
| 8036 | break; |
| 8037 | } |
| 8038 | } |
| 8039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8040 | /* Do accounting for the sndbuf space. |
| 8041 | * Decrement the used sndbuf space of the corresponding association by the |
| 8042 | * data size which was just transmitted(freed). |
| 8043 | */ |
| 8044 | static void sctp_wfree(struct sk_buff *skb) |
| 8045 | { |
Daniel Borkmann | f869c91 | 2014-11-20 01:54:48 +0100 | [diff] [blame] | 8046 | struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg; |
| 8047 | struct sctp_association *asoc = chunk->asoc; |
| 8048 | struct sock *sk = asoc->base.sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8049 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 8050 | asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) + |
| 8051 | sizeof(struct sk_buff) + |
| 8052 | sizeof(struct sctp_chunk); |
| 8053 | |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 8054 | WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc)); |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 8055 | |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8056 | /* |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 8057 | * This undoes what is done via sctp_set_owner_w and sk_mem_charge |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8058 | */ |
| 8059 | sk->sk_wmem_queued -= skb->truesize; |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 8060 | sk_mem_uncharge(sk, skb->truesize); |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8061 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 8062 | sock_wfree(skb); |
Daniel Borkmann | 52c35be | 2014-04-08 17:26:13 +0200 | [diff] [blame] | 8063 | sctp_wake_up_waiters(sk, asoc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8064 | |
| 8065 | sctp_association_put(asoc); |
| 8066 | } |
| 8067 | |
Vlad Yasevich | 331c4ee | 2006-10-09 21:34:04 -0700 | [diff] [blame] | 8068 | /* Do accounting for the receive space on the socket. |
| 8069 | * Accounting for the association is done in ulpevent.c |
| 8070 | * We set this as a destructor for the cloned data skbs so that |
| 8071 | * accounting is done at the correct time. |
| 8072 | */ |
| 8073 | void sctp_sock_rfree(struct sk_buff *skb) |
| 8074 | { |
| 8075 | struct sock *sk = skb->sk; |
| 8076 | struct sctp_ulpevent *event = sctp_skb2event(skb); |
| 8077 | |
| 8078 | atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8079 | |
| 8080 | /* |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 8081 | * Mimic the behavior of sock_rfree |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8082 | */ |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 8083 | sk_mem_uncharge(sk, event->rmem_len); |
Vlad Yasevich | 331c4ee | 2006-10-09 21:34:04 -0700 | [diff] [blame] | 8084 | } |
| 8085 | |
| 8086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8087 | /* Helper function to wait for space in the sndbuf. */ |
| 8088 | static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, |
Xin Long | cea0cc8 | 2017-11-15 16:57:26 +0800 | [diff] [blame] | 8089 | size_t msg_len, struct sock **orig_sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8090 | { |
| 8091 | struct sock *sk = asoc->base.sk; |
| 8092 | int err = 0; |
| 8093 | long current_timeo = *timeo_p; |
| 8094 | DEFINE_WAIT(wait); |
| 8095 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 8096 | pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc, |
| 8097 | *timeo_p, msg_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8098 | |
| 8099 | /* Increment the association's refcnt. */ |
| 8100 | sctp_association_hold(asoc); |
| 8101 | |
| 8102 | /* Wait on the association specific sndbuf space. */ |
| 8103 | for (;;) { |
| 8104 | prepare_to_wait_exclusive(&asoc->wait, &wait, |
| 8105 | TASK_INTERRUPTIBLE); |
Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 8106 | if (asoc->base.dead) |
| 8107 | goto do_dead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8108 | if (!*timeo_p) |
| 8109 | goto do_nonblock; |
Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 8110 | if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8111 | goto do_error; |
| 8112 | if (signal_pending(current)) |
| 8113 | goto do_interrupted; |
| 8114 | if (msg_len <= sctp_wspace(asoc)) |
| 8115 | break; |
| 8116 | |
| 8117 | /* Let another process have a go. Since we are going |
| 8118 | * to sleep anyway. |
| 8119 | */ |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8120 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8121 | current_timeo = schedule_timeout(current_timeo); |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8122 | lock_sock(sk); |
Xin Long | cea0cc8 | 2017-11-15 16:57:26 +0800 | [diff] [blame] | 8123 | if (sk != asoc->base.sk) { |
| 8124 | release_sock(sk); |
| 8125 | sk = asoc->base.sk; |
| 8126 | lock_sock(sk); |
| 8127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8128 | |
| 8129 | *timeo_p = current_timeo; |
| 8130 | } |
| 8131 | |
| 8132 | out: |
Xin Long | cea0cc8 | 2017-11-15 16:57:26 +0800 | [diff] [blame] | 8133 | *orig_sk = sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8134 | finish_wait(&asoc->wait, &wait); |
| 8135 | |
| 8136 | /* Release the association's refcnt. */ |
| 8137 | sctp_association_put(asoc); |
| 8138 | |
| 8139 | return err; |
| 8140 | |
Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 8141 | do_dead: |
| 8142 | err = -ESRCH; |
| 8143 | goto out; |
| 8144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8145 | do_error: |
| 8146 | err = -EPIPE; |
| 8147 | goto out; |
| 8148 | |
| 8149 | do_interrupted: |
| 8150 | err = sock_intr_errno(*timeo_p); |
| 8151 | goto out; |
| 8152 | |
| 8153 | do_nonblock: |
| 8154 | err = -EAGAIN; |
| 8155 | goto out; |
| 8156 | } |
| 8157 | |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 8158 | void sctp_data_ready(struct sock *sk) |
Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 8159 | { |
David S. Miller | 7ef5273 | 2010-05-02 21:43:40 -0700 | [diff] [blame] | 8160 | struct socket_wq *wq; |
| 8161 | |
| 8162 | rcu_read_lock(); |
| 8163 | wq = rcu_dereference(sk->sk_wq); |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 8164 | if (skwq_has_sleeper(wq)) |
David S. Miller | 7ef5273 | 2010-05-02 21:43:40 -0700 | [diff] [blame] | 8165 | wake_up_interruptible_sync_poll(&wq->wait, POLLIN | |
Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 8166 | POLLRDNORM | POLLRDBAND); |
| 8167 | sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); |
David S. Miller | 7ef5273 | 2010-05-02 21:43:40 -0700 | [diff] [blame] | 8168 | rcu_read_unlock(); |
Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 8169 | } |
| 8170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8171 | /* If socket sndbuf has changed, wake up all per association waiters. */ |
| 8172 | void sctp_write_space(struct sock *sk) |
| 8173 | { |
| 8174 | struct sctp_association *asoc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8175 | |
| 8176 | /* Wake up the tasks in each wait queue. */ |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 8177 | list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8178 | __sctp_write_space(asoc); |
| 8179 | } |
| 8180 | } |
| 8181 | |
| 8182 | /* Is there any sndbuf space available on the socket? |
| 8183 | * |
Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 8184 | * Note that sk_wmem_alloc is the sum of the send buffers on all of the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8185 | * associations on the same socket. For a UDP-style socket with |
| 8186 | * multiple associations, it is possible for it to be "unwriteable" |
| 8187 | * prematurely. I assume that this is acceptable because |
| 8188 | * a premature "unwriteable" is better than an accidental "writeable" which |
| 8189 | * would cause an unwanted block under certain circumstances. For the 1-1 |
| 8190 | * UDP-style sockets or TCP-style sockets, this code should work. |
| 8191 | * - Daisy |
| 8192 | */ |
| 8193 | static int sctp_writeable(struct sock *sk) |
| 8194 | { |
| 8195 | int amt = 0; |
| 8196 | |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 8197 | amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8198 | if (amt < 0) |
| 8199 | amt = 0; |
| 8200 | return amt; |
| 8201 | } |
| 8202 | |
| 8203 | /* Wait for an association to go into ESTABLISHED state. If timeout is 0, |
| 8204 | * returns immediately with EINPROGRESS. |
| 8205 | */ |
| 8206 | static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) |
| 8207 | { |
| 8208 | struct sock *sk = asoc->base.sk; |
| 8209 | int err = 0; |
| 8210 | long current_timeo = *timeo_p; |
| 8211 | DEFINE_WAIT(wait); |
| 8212 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 8213 | pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8214 | |
| 8215 | /* Increment the association's refcnt. */ |
| 8216 | sctp_association_hold(asoc); |
| 8217 | |
| 8218 | for (;;) { |
| 8219 | prepare_to_wait_exclusive(&asoc->wait, &wait, |
| 8220 | TASK_INTERRUPTIBLE); |
| 8221 | if (!*timeo_p) |
| 8222 | goto do_nonblock; |
| 8223 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 8224 | break; |
| 8225 | if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || |
| 8226 | asoc->base.dead) |
| 8227 | goto do_error; |
| 8228 | if (signal_pending(current)) |
| 8229 | goto do_interrupted; |
| 8230 | |
| 8231 | if (sctp_state(asoc, ESTABLISHED)) |
| 8232 | break; |
| 8233 | |
| 8234 | /* Let another process have a go. Since we are going |
| 8235 | * to sleep anyway. |
| 8236 | */ |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8237 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8238 | current_timeo = schedule_timeout(current_timeo); |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8239 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8240 | |
| 8241 | *timeo_p = current_timeo; |
| 8242 | } |
| 8243 | |
| 8244 | out: |
| 8245 | finish_wait(&asoc->wait, &wait); |
| 8246 | |
| 8247 | /* Release the association's refcnt. */ |
| 8248 | sctp_association_put(asoc); |
| 8249 | |
| 8250 | return err; |
| 8251 | |
| 8252 | do_error: |
Vlad Yasevich | 81845c2 | 2006-01-30 15:59:54 -0800 | [diff] [blame] | 8253 | if (asoc->init_err_counter + 1 > asoc->max_init_attempts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8254 | err = -ETIMEDOUT; |
| 8255 | else |
| 8256 | err = -ECONNREFUSED; |
| 8257 | goto out; |
| 8258 | |
| 8259 | do_interrupted: |
| 8260 | err = sock_intr_errno(*timeo_p); |
| 8261 | goto out; |
| 8262 | |
| 8263 | do_nonblock: |
| 8264 | err = -EINPROGRESS; |
| 8265 | goto out; |
| 8266 | } |
| 8267 | |
| 8268 | static int sctp_wait_for_accept(struct sock *sk, long timeo) |
| 8269 | { |
| 8270 | struct sctp_endpoint *ep; |
| 8271 | int err = 0; |
| 8272 | DEFINE_WAIT(wait); |
| 8273 | |
| 8274 | ep = sctp_sk(sk)->ep; |
| 8275 | |
| 8276 | |
| 8277 | for (;;) { |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8278 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8279 | TASK_INTERRUPTIBLE); |
| 8280 | |
| 8281 | if (list_empty(&ep->asocs)) { |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8282 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8283 | timeo = schedule_timeout(timeo); |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8284 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8285 | } |
| 8286 | |
| 8287 | err = -EINVAL; |
| 8288 | if (!sctp_sstate(sk, LISTENING)) |
| 8289 | break; |
| 8290 | |
| 8291 | err = 0; |
| 8292 | if (!list_empty(&ep->asocs)) |
| 8293 | break; |
| 8294 | |
| 8295 | err = sock_intr_errno(timeo); |
| 8296 | if (signal_pending(current)) |
| 8297 | break; |
| 8298 | |
| 8299 | err = -EAGAIN; |
| 8300 | if (!timeo) |
| 8301 | break; |
| 8302 | } |
| 8303 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8304 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8305 | |
| 8306 | return err; |
| 8307 | } |
| 8308 | |
sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 8309 | static void sctp_wait_for_close(struct sock *sk, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8310 | { |
| 8311 | DEFINE_WAIT(wait); |
| 8312 | |
| 8313 | do { |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8314 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8315 | if (list_empty(&sctp_sk(sk)->ep->asocs)) |
| 8316 | break; |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8317 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8318 | timeout = schedule_timeout(timeout); |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8319 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8320 | } while (!signal_pending(current) && timeout); |
| 8321 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8322 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8323 | } |
| 8324 | |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8325 | static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) |
| 8326 | { |
| 8327 | struct sk_buff *frag; |
| 8328 | |
| 8329 | if (!skb->data_len) |
| 8330 | goto done; |
| 8331 | |
| 8332 | /* Don't forget the fragments. */ |
David S. Miller | 1b003be | 2009-06-09 00:22:35 -0700 | [diff] [blame] | 8333 | skb_walk_frags(skb, frag) |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8334 | sctp_skb_set_owner_r_frag(frag, sk); |
| 8335 | |
| 8336 | done: |
| 8337 | sctp_skb_set_owner_r(skb, sk); |
| 8338 | } |
| 8339 | |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8340 | void sctp_copy_sock(struct sock *newsk, struct sock *sk, |
| 8341 | struct sctp_association *asoc) |
| 8342 | { |
| 8343 | struct inet_sock *inet = inet_sk(sk); |
Julia Lawall | 09cb47a | 2010-01-21 02:43:20 -0800 | [diff] [blame] | 8344 | struct inet_sock *newinet; |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8345 | |
| 8346 | newsk->sk_type = sk->sk_type; |
| 8347 | newsk->sk_bound_dev_if = sk->sk_bound_dev_if; |
| 8348 | newsk->sk_flags = sk->sk_flags; |
Marcelo Ricardo Leitner | 50a5ffb | 2015-12-04 15:14:05 -0200 | [diff] [blame] | 8349 | newsk->sk_tsflags = sk->sk_tsflags; |
Tom Herbert | 28448b8 | 2014-05-23 08:47:19 -0700 | [diff] [blame] | 8350 | newsk->sk_no_check_tx = sk->sk_no_check_tx; |
| 8351 | newsk->sk_no_check_rx = sk->sk_no_check_rx; |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8352 | newsk->sk_reuse = sk->sk_reuse; |
| 8353 | |
| 8354 | newsk->sk_shutdown = sk->sk_shutdown; |
Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 8355 | newsk->sk_destruct = sctp_destruct_sock; |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8356 | newsk->sk_family = sk->sk_family; |
| 8357 | newsk->sk_protocol = IPPROTO_SCTP; |
| 8358 | newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; |
| 8359 | newsk->sk_sndbuf = sk->sk_sndbuf; |
| 8360 | newsk->sk_rcvbuf = sk->sk_rcvbuf; |
| 8361 | newsk->sk_lingertime = sk->sk_lingertime; |
| 8362 | newsk->sk_rcvtimeo = sk->sk_rcvtimeo; |
| 8363 | newsk->sk_sndtimeo = sk->sk_sndtimeo; |
Marcelo Ricardo Leitner | 486bdee | 2016-04-12 18:11:31 -0300 | [diff] [blame] | 8364 | newsk->sk_rxhash = sk->sk_rxhash; |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8365 | |
| 8366 | newinet = inet_sk(newsk); |
| 8367 | |
| 8368 | /* Initialize sk's sport, dport, rcv_saddr and daddr for |
| 8369 | * getsockname() and getpeername() |
| 8370 | */ |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8371 | newinet->inet_sport = inet->inet_sport; |
| 8372 | newinet->inet_saddr = inet->inet_saddr; |
| 8373 | newinet->inet_rcv_saddr = inet->inet_rcv_saddr; |
| 8374 | newinet->inet_dport = htons(asoc->peer.port); |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8375 | newinet->pmtudisc = inet->pmtudisc; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8376 | newinet->inet_id = asoc->next_tsn ^ jiffies; |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8377 | |
| 8378 | newinet->uc_ttl = inet->uc_ttl; |
| 8379 | newinet->mc_loop = 1; |
| 8380 | newinet->mc_ttl = 1; |
| 8381 | newinet->mc_index = 0; |
| 8382 | newinet->mc_list = NULL; |
Marcelo Ricardo Leitner | 01ce63c | 2015-12-04 15:14:04 -0200 | [diff] [blame] | 8383 | |
| 8384 | if (newsk->sk_flags & SK_FLAGS_TIMESTAMP) |
| 8385 | net_enable_timestamp(); |
Marcelo Ricardo Leitner | 3538a5c | 2015-12-23 16:44:09 -0200 | [diff] [blame] | 8386 | |
| 8387 | security_sk_clone(sk, newsk); |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 8388 | } |
| 8389 | |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 8390 | static inline void sctp_copy_descendant(struct sock *sk_to, |
| 8391 | const struct sock *sk_from) |
| 8392 | { |
| 8393 | int ancestor_size = sizeof(struct inet_sock) + |
| 8394 | sizeof(struct sctp_sock) - |
| 8395 | offsetof(struct sctp_sock, auto_asconf_list); |
| 8396 | |
| 8397 | if (sk_from->sk_family == PF_INET6) |
| 8398 | ancestor_size += sizeof(struct ipv6_pinfo); |
| 8399 | |
| 8400 | __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size); |
| 8401 | } |
| 8402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8403 | /* Populate the fields of the newsk from the oldsk and migrate the assoc |
| 8404 | * and its messages to the newsk. |
| 8405 | */ |
| 8406 | static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, |
| 8407 | struct sctp_association *assoc, |
Xin Long | b7ef261 | 2017-08-11 10:23:50 +0800 | [diff] [blame] | 8408 | enum sctp_socket_type type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8409 | { |
| 8410 | struct sctp_sock *oldsp = sctp_sk(oldsk); |
| 8411 | struct sctp_sock *newsp = sctp_sk(newsk); |
| 8412 | struct sctp_bind_bucket *pp; /* hash list port iterator */ |
| 8413 | struct sctp_endpoint *newep = newsp->ep; |
| 8414 | struct sk_buff *skb, *tmp; |
| 8415 | struct sctp_ulpevent *event; |
Vlad Yasevich | f26f7c4 | 2007-12-06 22:50:27 -0800 | [diff] [blame] | 8416 | struct sctp_bind_hashbucket *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8417 | |
| 8418 | /* Migrate socket buffer sizes and all the socket level options to the |
| 8419 | * new socket. |
| 8420 | */ |
| 8421 | newsk->sk_sndbuf = oldsk->sk_sndbuf; |
| 8422 | newsk->sk_rcvbuf = oldsk->sk_rcvbuf; |
| 8423 | /* Brute force copy old sctp opt. */ |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 8424 | sctp_copy_descendant(newsk, oldsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8425 | |
| 8426 | /* Restore the ep value that was overwritten with the above structure |
| 8427 | * copy. |
| 8428 | */ |
| 8429 | newsp->ep = newep; |
| 8430 | newsp->hmac = NULL; |
| 8431 | |
| 8432 | /* Hook this new socket in to the bind_hash list. */ |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 8433 | head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk), |
| 8434 | inet_sk(oldsk)->inet_num)]; |
Nicholas Mc Guire | 489ce5f | 2016-03-13 11:48:24 +0100 | [diff] [blame] | 8435 | spin_lock_bh(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8436 | pp = sctp_sk(oldsk)->bind_hash; |
| 8437 | sk_add_bind_node(newsk, &pp->owner); |
| 8438 | sctp_sk(newsk)->bind_hash = pp; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8439 | inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num; |
Nicholas Mc Guire | 489ce5f | 2016-03-13 11:48:24 +0100 | [diff] [blame] | 8440 | spin_unlock_bh(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8441 | |
Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 8442 | /* Copy the bind_addr list from the original endpoint to the new |
| 8443 | * endpoint so that we can handle restarts properly |
| 8444 | */ |
Vlad Yasevich | 8e71a11 | 2007-12-06 22:50:54 -0800 | [diff] [blame] | 8445 | sctp_bind_addr_dup(&newsp->ep->base.bind_addr, |
| 8446 | &oldsp->ep->base.bind_addr, GFP_KERNEL); |
Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 8447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8448 | /* Move any messages in the old socket's receive queue that are for the |
| 8449 | * peeled off association to the new socket's receive queue. |
| 8450 | */ |
| 8451 | sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { |
| 8452 | event = sctp_skb2event(skb); |
| 8453 | if (event->asoc == assoc) { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 8454 | __skb_unlink(skb, &oldsk->sk_receive_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8455 | __skb_queue_tail(&newsk->sk_receive_queue, skb); |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8456 | sctp_skb_set_owner_r_frag(skb, newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8457 | } |
| 8458 | } |
| 8459 | |
| 8460 | /* Clean up any messages pending delivery due to partial |
| 8461 | * delivery. Three cases: |
| 8462 | * 1) No partial deliver; no work. |
| 8463 | * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. |
| 8464 | * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. |
| 8465 | */ |
| 8466 | skb_queue_head_init(&newsp->pd_lobby); |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 8467 | atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8468 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 8469 | if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8470 | struct sk_buff_head *queue; |
| 8471 | |
| 8472 | /* Decide which queue to move pd_lobby skbs to. */ |
| 8473 | if (assoc->ulpq.pd_mode) { |
| 8474 | queue = &newsp->pd_lobby; |
| 8475 | } else |
| 8476 | queue = &newsk->sk_receive_queue; |
| 8477 | |
| 8478 | /* Walk through the pd_lobby, looking for skbs that |
| 8479 | * need moved to the new socket. |
| 8480 | */ |
| 8481 | sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { |
| 8482 | event = sctp_skb2event(skb); |
| 8483 | if (event->asoc == assoc) { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 8484 | __skb_unlink(skb, &oldsp->pd_lobby); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8485 | __skb_queue_tail(queue, skb); |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8486 | sctp_skb_set_owner_r_frag(skb, newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8487 | } |
| 8488 | } |
| 8489 | |
| 8490 | /* Clear up any skbs waiting for the partial |
| 8491 | * delivery to finish. |
| 8492 | */ |
| 8493 | if (assoc->ulpq.pd_mode) |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 8494 | sctp_clear_pd(oldsk, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8495 | |
| 8496 | } |
| 8497 | |
Wei Yongjun | 1bc4ee4 | 2009-07-05 19:45:48 +0000 | [diff] [blame] | 8498 | sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8499 | sctp_skb_set_owner_r_frag(skb, newsk); |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8500 | |
Wei Yongjun | 1bc4ee4 | 2009-07-05 19:45:48 +0000 | [diff] [blame] | 8501 | sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8502 | sctp_skb_set_owner_r_frag(skb, newsk); |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 8503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8504 | /* Set the type of socket to indicate that it is peeled off from the |
| 8505 | * original UDP-style socket or created with the accept() call on a |
| 8506 | * TCP-style socket.. |
| 8507 | */ |
| 8508 | newsp->type = type; |
| 8509 | |
Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 8510 | /* Mark the new socket "in-use" by the user so that any packets |
| 8511 | * that may arrive on the association after we've moved it are |
| 8512 | * queued to the backlog. This prevents a potential race between |
| 8513 | * backlog processing on the old socket and new-packet processing |
| 8514 | * on the new socket. |
Zach Brown | 5131a18 | 2007-06-22 15:14:46 -0700 | [diff] [blame] | 8515 | * |
| 8516 | * The caller has just allocated newsk so we can guarantee that other |
| 8517 | * paths won't try to lock it and then oldsk. |
Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 8518 | */ |
Zach Brown | 5131a18 | 2007-06-22 15:14:46 -0700 | [diff] [blame] | 8519 | lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 8520 | sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8521 | sctp_assoc_migrate(assoc, newsk); |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 8522 | sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8523 | |
| 8524 | /* If the association on the newsk is already closed before accept() |
| 8525 | * is called, set RCV_SHUTDOWN flag. |
| 8526 | */ |
Xin Long | d46e416 | 2016-06-09 22:48:18 +0800 | [diff] [blame] | 8527 | if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) { |
Xin Long | 141ddef | 2016-06-16 01:15:06 +0800 | [diff] [blame] | 8528 | newsk->sk_state = SCTP_SS_CLOSED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8529 | newsk->sk_shutdown |= RCV_SHUTDOWN; |
Xin Long | d46e416 | 2016-06-09 22:48:18 +0800 | [diff] [blame] | 8530 | } else { |
| 8531 | newsk->sk_state = SCTP_SS_ESTABLISHED; |
| 8532 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8533 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8534 | release_sock(newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8535 | } |
| 8536 | |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8538 | /* This proto struct describes the ULP interface for SCTP. */ |
| 8539 | struct proto sctp_prot = { |
| 8540 | .name = "SCTP", |
| 8541 | .owner = THIS_MODULE, |
| 8542 | .close = sctp_close, |
| 8543 | .connect = sctp_connect, |
| 8544 | .disconnect = sctp_disconnect, |
| 8545 | .accept = sctp_accept, |
| 8546 | .ioctl = sctp_ioctl, |
| 8547 | .init = sctp_init_sock, |
| 8548 | .destroy = sctp_destroy_sock, |
| 8549 | .shutdown = sctp_shutdown, |
| 8550 | .setsockopt = sctp_setsockopt, |
| 8551 | .getsockopt = sctp_getsockopt, |
| 8552 | .sendmsg = sctp_sendmsg, |
| 8553 | .recvmsg = sctp_recvmsg, |
| 8554 | .bind = sctp_bind, |
| 8555 | .backlog_rcv = sctp_backlog_rcv, |
| 8556 | .hash = sctp_hash, |
| 8557 | .unhash = sctp_unhash, |
| 8558 | .get_port = sctp_get_port, |
| 8559 | .obj_size = sizeof(struct sctp_sock), |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8560 | .sysctl_mem = sysctl_sctp_mem, |
| 8561 | .sysctl_rmem = sysctl_sctp_rmem, |
| 8562 | .sysctl_wmem = sysctl_sctp_wmem, |
| 8563 | .memory_pressure = &sctp_memory_pressure, |
| 8564 | .enter_memory_pressure = sctp_enter_memory_pressure, |
| 8565 | .memory_allocated = &sctp_memory_allocated, |
Pavel Emelyanov | 5f31886 | 2008-02-20 00:23:01 -0800 | [diff] [blame] | 8566 | .sockets_allocated = &sctp_sockets_allocated, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8567 | }; |
| 8568 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 8569 | #if IS_ENABLED(CONFIG_IPV6) |
Eric Dumazet | 8295b6d | 2007-11-05 23:40:28 -0800 | [diff] [blame] | 8570 | |
Eric Dumazet | 602dd62 | 2015-12-01 07:20:07 -0800 | [diff] [blame] | 8571 | #include <net/transp_v6.h> |
| 8572 | static void sctp_v6_destroy_sock(struct sock *sk) |
| 8573 | { |
| 8574 | sctp_destroy_sock(sk); |
| 8575 | inet6_destroy_sock(sk); |
| 8576 | } |
| 8577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8578 | struct proto sctpv6_prot = { |
| 8579 | .name = "SCTPv6", |
| 8580 | .owner = THIS_MODULE, |
| 8581 | .close = sctp_close, |
| 8582 | .connect = sctp_connect, |
| 8583 | .disconnect = sctp_disconnect, |
| 8584 | .accept = sctp_accept, |
| 8585 | .ioctl = sctp_ioctl, |
| 8586 | .init = sctp_init_sock, |
Eric Dumazet | 602dd62 | 2015-12-01 07:20:07 -0800 | [diff] [blame] | 8587 | .destroy = sctp_v6_destroy_sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8588 | .shutdown = sctp_shutdown, |
| 8589 | .setsockopt = sctp_setsockopt, |
| 8590 | .getsockopt = sctp_getsockopt, |
| 8591 | .sendmsg = sctp_sendmsg, |
| 8592 | .recvmsg = sctp_recvmsg, |
| 8593 | .bind = sctp_bind, |
| 8594 | .backlog_rcv = sctp_backlog_rcv, |
| 8595 | .hash = sctp_hash, |
| 8596 | .unhash = sctp_unhash, |
| 8597 | .get_port = sctp_get_port, |
| 8598 | .obj_size = sizeof(struct sctp6_sock), |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 8599 | .sysctl_mem = sysctl_sctp_mem, |
| 8600 | .sysctl_rmem = sysctl_sctp_rmem, |
| 8601 | .sysctl_wmem = sysctl_sctp_wmem, |
| 8602 | .memory_pressure = &sctp_memory_pressure, |
| 8603 | .enter_memory_pressure = sctp_enter_memory_pressure, |
| 8604 | .memory_allocated = &sctp_memory_allocated, |
Pavel Emelyanov | 5f31886 | 2008-02-20 00:23:01 -0800 | [diff] [blame] | 8605 | .sockets_allocated = &sctp_sockets_allocated, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8606 | }; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 8607 | #endif /* IS_ENABLED(CONFIG_IPV6) */ |