Thomas Gleixner | 47505b8 | 2019-05-23 11:14:41 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 2 | /* SCTP kernel implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * (C) Copyright IBM Corp. 2001, 2004 |
| 4 | * Copyright (c) 1999-2000 Cisco, Inc. |
| 5 | * Copyright (c) 1999-2001 Motorola, Inc. |
| 6 | * Copyright (c) 2001-2003 Intel Corp. |
| 7 | * Copyright (c) 2001-2002 Nokia, Inc. |
| 8 | * Copyright (c) 2001 La Monte H.P. Yarroll |
| 9 | * |
Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 10 | * This file is part of the SCTP kernel implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * These functions interface with the sockets layer to implement the |
| 13 | * SCTP Extensions for the Sockets API. |
| 14 | * |
| 15 | * Note that the descriptions from the specification are USER level |
| 16 | * functions--this file is the functions which populate the struct proto |
| 17 | * for SCTP which is the BOTTOM of the sockets interface. |
| 18 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Please send any bug reports or fixes you make to the |
| 20 | * email address(es): |
Daniel Borkmann | 91705c6 | 2013-07-23 14:51:47 +0200 | [diff] [blame] | 21 | * lksctp developers <linux-sctp@vger.kernel.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * Written or modified by: |
| 24 | * La Monte H.P. Yarroll <piggy@acm.org> |
| 25 | * Narasimha Budihal <narsi@refcode.org> |
| 26 | * Karl Knutson <karl@athena.chicago.il.us> |
| 27 | * Jon Grimm <jgrimm@us.ibm.com> |
| 28 | * Xingang Guo <xingang.guo@intel.com> |
| 29 | * Daisy Chang <daisyc@us.ibm.com> |
| 30 | * Sridhar Samudrala <samudrala@us.ibm.com> |
| 31 | * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com> |
| 32 | * Ardelle Fan <ardelle.fan@intel.com> |
| 33 | * Ryan Layer <rmlayer@us.ibm.com> |
| 34 | * Anup Pemmaiah <pemmaiah@cc.usu.edu> |
| 35 | * Kevin Gao <kevin.gao@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | */ |
| 37 | |
Joe Perches | 145ce50 | 2010-08-24 13:21:08 +0000 | [diff] [blame] | 38 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 39 | |
Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 40 | #include <crypto/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/types.h> |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/wait.h> |
| 44 | #include <linux/time.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 45 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/ip.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 47 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/fcntl.h> |
| 49 | #include <linux/poll.h> |
| 50 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 51 | #include <linux/slab.h> |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 52 | #include <linux/file.h> |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 53 | #include <linux/compat.h> |
NeilBrown | 0eb71a9 | 2018-06-18 12:52:50 +1000 | [diff] [blame] | 54 | #include <linux/rhashtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | #include <net/ip.h> |
| 57 | #include <net/icmp.h> |
| 58 | #include <net/route.h> |
| 59 | #include <net/ipv6.h> |
| 60 | #include <net/inet_common.h> |
Neil Horman | 8465a5f | 2014-04-17 15:26:51 -0400 | [diff] [blame] | 61 | #include <net/busy_poll.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | #include <linux/socket.h> /* for sa_family_t */ |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 64 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <net/sock.h> |
| 66 | #include <net/sctp/sctp.h> |
| 67 | #include <net/sctp/sm.h> |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 68 | #include <net/sctp/stream_sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | /* Forward declarations for internal helper functions. */ |
Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame] | 71 | static bool sctp_writeable(struct sock *sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | static void sctp_wfree(struct sk_buff *skb); |
Xin Long | cea0cc8 | 2017-11-15 16:57:26 +0800 | [diff] [blame] | 73 | static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, |
Xin Long | a0ff660 | 2018-01-15 17:01:36 +0800 | [diff] [blame] | 74 | size_t msg_len); |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 75 | 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] | 76 | static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p); |
| 77 | static int sctp_wait_for_accept(struct sock *sk, long timeo); |
| 78 | static void sctp_wait_for_close(struct sock *sk, long timeo); |
Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 79 | static void sctp_destruct_sock(struct sock *sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, |
| 81 | union sctp_addr *addr, int len); |
| 82 | static int sctp_bindx_add(struct sock *, struct sockaddr *, int); |
| 83 | static int sctp_bindx_rem(struct sock *, struct sockaddr *, int); |
| 84 | static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int); |
| 85 | static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int); |
| 86 | static int sctp_send_asconf(struct sctp_association *asoc, |
| 87 | struct sctp_chunk *chunk); |
| 88 | static int sctp_do_bind(struct sock *, union sctp_addr *, int); |
| 89 | static int sctp_autobind(struct sock *sk); |
Xin Long | 89664c62 | 2019-03-03 17:54:53 +0800 | [diff] [blame] | 90 | static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, |
| 91 | struct sctp_association *assoc, |
| 92 | enum sctp_socket_type type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Eric Dumazet | 0604475 | 2017-06-07 13:29:12 -0700 | [diff] [blame] | 94 | static unsigned long sctp_memory_pressure; |
Eric Dumazet | 8d987e5 | 2010-11-09 23:24:26 +0000 | [diff] [blame] | 95 | static atomic_long_t sctp_memory_allocated; |
Eric Dumazet | 1748376 | 2008-11-25 21:16:35 -0800 | [diff] [blame] | 96 | struct percpu_counter sctp_sockets_allocated; |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 97 | |
Pavel Emelyanov | 5c52ba1 | 2008-07-16 20:28:10 -0700 | [diff] [blame] | 98 | static void sctp_enter_memory_pressure(struct sock *sk) |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 99 | { |
| 100 | sctp_memory_pressure = 1; |
| 101 | } |
| 102 | |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* Get the sndbuf space available at the time on the association. */ |
| 105 | static inline int sctp_wspace(struct sctp_association *asoc) |
| 106 | { |
Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame] | 107 | struct sock *sk = asoc->base.sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame] | 109 | return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used |
| 110 | : sk_stream_wspace(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* Increment the used sndbuf space count of the corresponding association by |
| 114 | * the size of the outgoing data chunk. |
| 115 | * Also, set the skb destructor for sndbuf accounting later. |
| 116 | * |
| 117 | * Since it is always 1-1 between chunk and skb, and also a new skb is always |
| 118 | * allocated for chunk bundling in sctp_packet_transmit(), we can use the |
| 119 | * destructor in the data chunk skb for the purpose of the sndbuf space |
| 120 | * tracking. |
| 121 | */ |
| 122 | static inline void sctp_set_owner_w(struct sctp_chunk *chunk) |
| 123 | { |
| 124 | struct sctp_association *asoc = chunk->asoc; |
| 125 | struct sock *sk = asoc->base.sk; |
| 126 | |
| 127 | /* The sndbuf space is tracked per association. */ |
| 128 | sctp_association_hold(asoc); |
| 129 | |
Xin Long | 1b1e0bc | 2018-03-14 19:05:30 +0800 | [diff] [blame] | 130 | if (chunk->shkey) |
| 131 | sctp_auth_shkey_hold(chunk->shkey); |
| 132 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 133 | skb_set_owner_w(chunk->skb, sk); |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | chunk->skb->destructor = sctp_wfree; |
| 136 | /* Save the chunk pointer in skb for sctp_wfree to use later. */ |
Daniel Borkmann | f869c91 | 2014-11-20 01:54:48 +0100 | [diff] [blame] | 137 | skb_shinfo(chunk->skb)->destructor_arg = chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 139 | refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); |
Xin Long | 605c0ac | 2018-10-17 03:07:50 +0800 | [diff] [blame] | 140 | asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk); |
| 141 | sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk); |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 142 | sk_mem_charge(sk, chunk->skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 145 | static void sctp_clear_owner_w(struct sctp_chunk *chunk) |
| 146 | { |
| 147 | skb_orphan(chunk->skb); |
| 148 | } |
| 149 | |
| 150 | static void sctp_for_each_tx_datachunk(struct sctp_association *asoc, |
| 151 | void (*cb)(struct sctp_chunk *)) |
| 152 | |
| 153 | { |
| 154 | struct sctp_outq *q = &asoc->outqueue; |
| 155 | struct sctp_transport *t; |
| 156 | struct sctp_chunk *chunk; |
| 157 | |
| 158 | list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) |
| 159 | list_for_each_entry(chunk, &t->transmitted, transmitted_list) |
| 160 | cb(chunk); |
| 161 | |
Xin Long | a8dd397 | 2017-11-26 20:56:07 +0800 | [diff] [blame] | 162 | list_for_each_entry(chunk, &q->retransmit, transmitted_list) |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 163 | cb(chunk); |
| 164 | |
Xin Long | a8dd397 | 2017-11-26 20:56:07 +0800 | [diff] [blame] | 165 | list_for_each_entry(chunk, &q->sacked, transmitted_list) |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 166 | cb(chunk); |
| 167 | |
Xin Long | a8dd397 | 2017-11-26 20:56:07 +0800 | [diff] [blame] | 168 | list_for_each_entry(chunk, &q->abandoned, transmitted_list) |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 169 | cb(chunk); |
| 170 | |
| 171 | list_for_each_entry(chunk, &q->out_chunk_list, list) |
| 172 | cb(chunk); |
| 173 | } |
| 174 | |
Xin Long | 1322823 | 2017-12-08 21:04:09 +0800 | [diff] [blame] | 175 | static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk, |
| 176 | void (*cb)(struct sk_buff *, struct sock *)) |
| 177 | |
| 178 | { |
| 179 | struct sk_buff *skb, *tmp; |
| 180 | |
| 181 | sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp) |
| 182 | cb(skb, sk); |
| 183 | |
| 184 | sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp) |
| 185 | cb(skb, sk); |
| 186 | |
| 187 | sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp) |
| 188 | cb(skb, sk); |
| 189 | } |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | /* Verify that this is a valid address. */ |
| 192 | static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr, |
| 193 | int len) |
| 194 | { |
| 195 | struct sctp_af *af; |
| 196 | |
| 197 | /* Verify basic sockaddr. */ |
| 198 | af = sctp_sockaddr_af(sctp_sk(sk), addr, len); |
| 199 | if (!af) |
| 200 | return -EINVAL; |
| 201 | |
| 202 | /* Is this a valid SCTP address? */ |
Vlad Yasevich | 5636bef | 2006-06-17 22:55:35 -0700 | [diff] [blame] | 203 | if (!af->addr_valid(addr, sctp_sk(sk), NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return -EINVAL; |
| 205 | |
| 206 | if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr))) |
| 207 | return -EINVAL; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* Look up the association by its id. If this is not a UDP-style |
| 213 | * socket, the ID field is always ignored. |
| 214 | */ |
| 215 | struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id) |
| 216 | { |
| 217 | struct sctp_association *asoc = NULL; |
| 218 | |
| 219 | /* If this is not a UDP-style socket, assoc id should be ignored. */ |
| 220 | if (!sctp_style(sk, UDP)) { |
| 221 | /* Return NULL if the socket state is not ESTABLISHED. It |
| 222 | * could be a TCP-style listening socket or a socket which |
| 223 | * hasn't yet called connect() to establish an association. |
| 224 | */ |
Marcelo Ricardo Leitner | e5b13f3 | 2016-07-15 16:38:19 -0300 | [diff] [blame] | 225 | if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return NULL; |
| 227 | |
| 228 | /* Get the first and the only association from the list. */ |
| 229 | if (!list_empty(&sctp_sk(sk)->ep->asocs)) |
| 230 | asoc = list_entry(sctp_sk(sk)->ep->asocs.next, |
| 231 | struct sctp_association, asocs); |
| 232 | return asoc; |
| 233 | } |
| 234 | |
| 235 | /* Otherwise this is a UDP-style socket. */ |
Xin Long | 80df270 | 2019-01-28 15:08:23 +0800 | [diff] [blame] | 236 | if (id <= SCTP_ALL_ASSOC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | return NULL; |
| 238 | |
| 239 | spin_lock_bh(&sctp_assocs_id_lock); |
| 240 | asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); |
Marcelo Ricardo Leitner | b336dec | 2018-10-16 15:18:17 -0300 | [diff] [blame] | 241 | if (asoc && (asoc->base.sk != sk || asoc->base.dead)) |
| 242 | asoc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | spin_unlock_bh(&sctp_assocs_id_lock); |
| 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return asoc; |
| 246 | } |
| 247 | |
| 248 | /* Look up the transport from an address and an assoc id. If both address and |
| 249 | * id are specified, the associations matching the address and the id should be |
| 250 | * the same. |
| 251 | */ |
| 252 | static struct sctp_transport *sctp_addr_id2transport(struct sock *sk, |
| 253 | struct sockaddr_storage *addr, |
| 254 | sctp_assoc_t id) |
| 255 | { |
| 256 | struct sctp_association *addr_asoc = NULL, *id_asoc = NULL; |
Xin Long | 6f29a13 | 2017-01-24 14:01:53 +0800 | [diff] [blame] | 257 | struct sctp_af *af = sctp_get_af_specific(addr->ss_family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | union sctp_addr *laddr = (union sctp_addr *)addr; |
Xin Long | 6f29a13 | 2017-01-24 14:01:53 +0800 | [diff] [blame] | 259 | struct sctp_transport *transport; |
| 260 | |
Xin Long | 912964e | 2017-02-07 20:56:08 +0800 | [diff] [blame] | 261 | if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len)) |
Xin Long | 6f29a13 | 2017-01-24 14:01:53 +0800 | [diff] [blame] | 262 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep, |
Al Viro | cd4ff03 | 2006-11-20 17:11:33 -0800 | [diff] [blame] | 265 | laddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | &transport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | if (!addr_asoc) |
| 269 | return NULL; |
| 270 | |
| 271 | id_asoc = sctp_id2assoc(sk, id); |
| 272 | if (id_asoc && (id_asoc != addr_asoc)) |
| 273 | return NULL; |
| 274 | |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 275 | 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] | 276 | (union sctp_addr *)addr); |
| 277 | |
| 278 | return transport; |
| 279 | } |
| 280 | |
| 281 | /* API 3.1.2 bind() - UDP Style Syntax |
| 282 | * The syntax of bind() is, |
| 283 | * |
| 284 | * ret = bind(int sd, struct sockaddr *addr, int addrlen); |
| 285 | * |
| 286 | * sd - the socket descriptor returned by socket(). |
| 287 | * addr - the address structure (struct sockaddr_in or struct |
| 288 | * sockaddr_in6 [RFC 2553]), |
| 289 | * addr_len - the size of the address structure. |
| 290 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 291 | 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] | 292 | { |
| 293 | int retval = 0; |
| 294 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 295 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 297 | pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk, |
| 298 | addr, addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | /* Disallow binding twice. */ |
| 301 | if (!sctp_sk(sk)->ep->base.bind_addr.port) |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 302 | retval = sctp_do_bind(sk, (union sctp_addr *)addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | addr_len); |
| 304 | else |
| 305 | retval = -EINVAL; |
| 306 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 307 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | return retval; |
| 310 | } |
| 311 | |
Mao Wenan | 8e2ef6a | 2019-09-12 12:02:17 +0800 | [diff] [blame] | 312 | static int sctp_get_port_local(struct sock *, union sctp_addr *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | /* Verify this is a valid sockaddr. */ |
| 315 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, |
| 316 | union sctp_addr *addr, int len) |
| 317 | { |
| 318 | struct sctp_af *af; |
| 319 | |
| 320 | /* Check minimum size. */ |
| 321 | if (len < sizeof (struct sockaddr)) |
| 322 | return NULL; |
| 323 | |
Xin Long | c5006b8 | 2018-01-15 17:02:00 +0800 | [diff] [blame] | 324 | if (!opt->pf->af_supported(addr->sa.sa_family, opt)) |
| 325 | return NULL; |
| 326 | |
Eric Dumazet | 81e9837 | 2018-04-08 07:52:08 -0700 | [diff] [blame] | 327 | if (addr->sa.sa_family == AF_INET6) { |
| 328 | if (len < SIN6_LEN_RFC2133) |
| 329 | return NULL; |
| 330 | /* V4 mapped address are really of AF_INET family */ |
| 331 | if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) && |
| 332 | !opt->pf->af_supported(AF_INET, opt)) |
| 333 | return NULL; |
| 334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | /* If we get this far, af is valid. */ |
| 337 | af = sctp_get_af_specific(addr->sa.sa_family); |
| 338 | |
| 339 | if (len < af->sockaddr_len) |
| 340 | return NULL; |
| 341 | |
| 342 | return af; |
| 343 | } |
| 344 | |
| 345 | /* Bind a local address either to an endpoint or to an association. */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 346 | 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] | 347 | { |
Eric W. Biederman | 3594698 | 2012-11-16 03:03:12 +0000 | [diff] [blame] | 348 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | struct sctp_sock *sp = sctp_sk(sk); |
| 350 | struct sctp_endpoint *ep = sp->ep; |
| 351 | struct sctp_bind_addr *bp = &ep->base.bind_addr; |
| 352 | struct sctp_af *af; |
| 353 | unsigned short snum; |
| 354 | int ret = 0; |
| 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | /* Common sockaddr verification. */ |
| 357 | af = sctp_sockaddr_af(sp, addr, len); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 358 | if (!af) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 359 | pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n", |
| 360 | __func__, sk, addr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return -EINVAL; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | snum = ntohs(addr->v4.sin_port); |
| 365 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 366 | pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n", |
| 367 | __func__, sk, &addr->sa, bp->port, snum, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | /* PF specific bind() address verification. */ |
| 370 | if (!sp->pf->bind_verify(sp, addr)) |
| 371 | return -EADDRNOTAVAIL; |
| 372 | |
Vlad Yasevich | 8b35805 | 2007-05-15 17:14:58 -0400 | [diff] [blame] | 373 | /* We must either be unbound, or bind to the same port. |
| 374 | * It's OK to allow 0 ports if we are already bound. |
| 375 | * We'll just inhert an already bound port in this case |
| 376 | */ |
| 377 | if (bp->port) { |
| 378 | if (!snum) |
| 379 | snum = bp->port; |
| 380 | else if (snum != bp->port) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 381 | pr_debug("%s: new port %d doesn't match existing port " |
| 382 | "%d\n", __func__, snum, bp->port); |
Vlad Yasevich | 8b35805 | 2007-05-15 17:14:58 -0400 | [diff] [blame] | 383 | return -EINVAL; |
| 384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Maciej Żenczykowski | 82f31eb | 2019-11-25 15:37:04 -0800 | [diff] [blame] | 387 | if (snum && inet_port_requires_bind_service(net, snum) && |
Eric W. Biederman | 3594698 | 2012-11-16 03:03:12 +0000 | [diff] [blame] | 388 | !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return -EACCES; |
| 390 | |
Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 391 | /* See if the address matches any of the addresses we may have |
| 392 | * already bound before checking against other endpoints. |
| 393 | */ |
| 394 | if (sctp_bind_addr_match(bp, addr, sp)) |
| 395 | return -EINVAL; |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | /* Make sure we are allowed to bind here. |
| 398 | * The function sctp_get_port_local() does duplicate address |
| 399 | * detection. |
| 400 | */ |
Vlad Yasevich | 2772b49 | 2007-08-21 14:24:30 +0900 | [diff] [blame] | 401 | addr->v4.sin_port = htons(snum); |
Mao Wenan | e0e4b8d | 2019-09-12 12:02:18 +0800 | [diff] [blame] | 402 | if (sctp_get_port_local(sk, addr)) |
Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 403 | return -EADDRINUSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | /* Refresh ephemeral port. */ |
| 406 | if (!bp->port) |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 407 | bp->port = inet_sk(sk)->inet_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 409 | /* Add the address to the bind address list. |
| 410 | * Use GFP_ATOMIC since BHs will be disabled. |
| 411 | */ |
Marcelo Ricardo Leitner | 133800d | 2016-03-08 10:34:28 -0300 | [diff] [blame] | 412 | ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len, |
| 413 | SCTP_ADDR_SRC, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Mao Wenan | 29b99f5 | 2019-09-12 12:02:19 +0800 | [diff] [blame] | 415 | if (ret) { |
| 416 | sctp_put_port(sk); |
| 417 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
Mao Wenan | 29b99f5 | 2019-09-12 12:02:19 +0800 | [diff] [blame] | 419 | /* Copy back into socket for getsockname() use. */ |
| 420 | inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num); |
| 421 | sp->pf->to_sk_saddr(addr, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks |
| 427 | * |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 428 | * 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] | 429 | * 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] | 430 | * it needs to transfer another ASCONF Chunk, it MUST wait until the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 432 | * subsequent ASCONF. Note this restriction binds each side, so at any |
| 433 | * 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] | 434 | * from each endpoint). |
| 435 | */ |
| 436 | static int sctp_send_asconf(struct sctp_association *asoc, |
| 437 | struct sctp_chunk *chunk) |
| 438 | { |
Xin Long | 4e7696d | 2019-12-09 13:45:18 +0800 | [diff] [blame] | 439 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
| 441 | /* If there is an outstanding ASCONF chunk, queue it for later |
| 442 | * transmission. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 443 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | if (asoc->addip_last_asconf) { |
David S. Miller | 79af02c | 2005-07-08 21:47:49 -0700 | [diff] [blame] | 445 | list_add_tail(&chunk->list, &asoc->addip_chunk_list); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 446 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* Hold the chunk until an ASCONF_ACK is received. */ |
| 450 | sctp_chunk_hold(chunk); |
Xin Long | 4e7696d | 2019-12-09 13:45:18 +0800 | [diff] [blame] | 451 | retval = sctp_primitive_ASCONF(asoc->base.net, asoc, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | if (retval) |
| 453 | sctp_chunk_free(chunk); |
| 454 | else |
| 455 | asoc->addip_last_asconf = chunk; |
| 456 | |
| 457 | out: |
| 458 | return retval; |
| 459 | } |
| 460 | |
| 461 | /* Add a list of addresses as bind addresses to local endpoint or |
| 462 | * association. |
| 463 | * |
| 464 | * Basically run through each address specified in the addrs/addrcnt |
| 465 | * array/length pair, determine if it is IPv6 or IPv4 and call |
| 466 | * sctp_do_bind() on it. |
| 467 | * |
| 468 | * If any of them fails, then the operation will be reversed and the |
| 469 | * ones that were added will be removed. |
| 470 | * |
| 471 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 472 | */ |
sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 473 | 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] | 474 | { |
| 475 | int cnt; |
| 476 | int retval = 0; |
| 477 | void *addr_buf; |
| 478 | struct sockaddr *sa_addr; |
| 479 | struct sctp_af *af; |
| 480 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 481 | pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk, |
| 482 | addrs, addrcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | addr_buf = addrs; |
| 485 | for (cnt = 0; cnt < addrcnt; cnt++) { |
| 486 | /* The list may contain either IPv4 or IPv6 address; |
| 487 | * determine the address length for walking thru the list. |
| 488 | */ |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 489 | sa_addr = addr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | af = sctp_get_af_specific(sa_addr->sa_family); |
| 491 | if (!af) { |
| 492 | retval = -EINVAL; |
| 493 | goto err_bindx_add; |
| 494 | } |
| 495 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 496 | retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | af->sockaddr_len); |
| 498 | |
| 499 | addr_buf += af->sockaddr_len; |
| 500 | |
| 501 | err_bindx_add: |
| 502 | if (retval < 0) { |
| 503 | /* Failed. Cleanup the ones that have been added */ |
| 504 | if (cnt > 0) |
| 505 | sctp_bindx_rem(sk, addrs, cnt); |
| 506 | return retval; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | return retval; |
| 511 | } |
| 512 | |
| 513 | /* Send an ASCONF chunk with Add IP address parameters to all the peers of the |
| 514 | * associations that are part of the endpoint indicating that a list of local |
| 515 | * addresses are added to the endpoint. |
| 516 | * |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 517 | * 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] | 518 | * association, we do not send the chunk for that association. But it will not |
| 519 | * affect other associations. |
| 520 | * |
| 521 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 522 | */ |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 523 | static int sctp_send_asconf_add_ip(struct sock *sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | struct sockaddr *addrs, |
| 525 | int addrcnt) |
| 526 | { |
| 527 | struct sctp_sock *sp; |
| 528 | struct sctp_endpoint *ep; |
| 529 | struct sctp_association *asoc; |
| 530 | struct sctp_bind_addr *bp; |
| 531 | struct sctp_chunk *chunk; |
| 532 | struct sctp_sockaddr_entry *laddr; |
| 533 | union sctp_addr *addr; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 534 | union sctp_addr saveaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | void *addr_buf; |
| 536 | struct sctp_af *af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | struct list_head *p; |
| 538 | int i; |
| 539 | int retval = 0; |
| 540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | sp = sctp_sk(sk); |
| 542 | ep = sp->ep; |
| 543 | |
Xin Long | 4e27428 | 2019-08-19 22:02:43 +0800 | [diff] [blame] | 544 | if (!ep->asconf_enable) |
| 545 | return retval; |
| 546 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 547 | pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", |
| 548 | __func__, sk, addrs, addrcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 550 | list_for_each_entry(asoc, &ep->asocs, asocs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | if (!asoc->peer.asconf_capable) |
| 552 | continue; |
| 553 | |
| 554 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP) |
| 555 | continue; |
| 556 | |
| 557 | if (!sctp_state(asoc, ESTABLISHED)) |
| 558 | continue; |
| 559 | |
| 560 | /* Check if any address in the packed array of addresses is |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 561 | * in the bind address list of the association. If so, |
| 562 | * do not send the asconf chunk to its peer, but continue with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | * other associations. |
| 564 | */ |
| 565 | addr_buf = addrs; |
| 566 | for (i = 0; i < addrcnt; i++) { |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 567 | addr = addr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | af = sctp_get_af_specific(addr->v4.sin_family); |
| 569 | if (!af) { |
| 570 | retval = -EINVAL; |
| 571 | goto out; |
| 572 | } |
| 573 | |
| 574 | if (sctp_assoc_lookup_laddr(asoc, addr)) |
| 575 | break; |
| 576 | |
| 577 | addr_buf += af->sockaddr_len; |
| 578 | } |
| 579 | if (i < addrcnt) |
| 580 | continue; |
| 581 | |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 582 | /* Use the first valid address in bind addr list of |
| 583 | * association as Address Parameter of ASCONF CHUNK. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | bp = &asoc->base.bind_addr; |
| 586 | p = bp->address_list.next; |
| 587 | laddr = list_entry(p, struct sctp_sockaddr_entry, list); |
Al Viro | 5ae955c | 2006-11-20 17:22:08 -0800 | [diff] [blame] | 588 | chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | addrcnt, SCTP_PARAM_ADD_IP); |
| 590 | if (!chunk) { |
| 591 | retval = -ENOMEM; |
| 592 | goto out; |
| 593 | } |
| 594 | |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 595 | /* Add the new addresses to the bind address list with |
| 596 | * use_as_src set to 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | */ |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 598 | addr_buf = addrs; |
| 599 | for (i = 0; i < addrcnt; i++) { |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 600 | addr = addr_buf; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 601 | af = sctp_get_af_specific(addr->v4.sin_family); |
| 602 | memcpy(&saveaddr, addr, af->sockaddr_len); |
Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 603 | retval = sctp_add_bind_addr(bp, &saveaddr, |
Marcelo Ricardo Leitner | 133800d | 2016-03-08 10:34:28 -0300 | [diff] [blame] | 604 | sizeof(saveaddr), |
Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 605 | SCTP_ADDR_NEW, GFP_ATOMIC); |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 606 | addr_buf += af->sockaddr_len; |
| 607 | } |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 608 | if (asoc->src_out_of_asoc_ok) { |
| 609 | struct sctp_transport *trans; |
| 610 | |
| 611 | list_for_each_entry(trans, |
| 612 | &asoc->peer.transport_addr_list, transports) { |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 613 | trans->cwnd = min(4*asoc->pathmtu, max_t(__u32, |
| 614 | 2*asoc->pathmtu, 4380)); |
| 615 | trans->ssthresh = asoc->peer.i.a_rwnd; |
| 616 | trans->rto = asoc->rto_initial; |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 617 | sctp_max_rto(asoc, trans); |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 618 | trans->rtt = trans->srtt = trans->rttvar = 0; |
Marcelo Ricardo Leitner | 6e91b57 | 2018-04-26 16:58:59 -0300 | [diff] [blame] | 619 | /* Clear the source and route cache */ |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 620 | sctp_transport_route(trans, NULL, |
Marcelo Ricardo Leitner | 6e91b57 | 2018-04-26 16:58:59 -0300 | [diff] [blame] | 621 | sctp_sk(asoc->base.sk)); |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | retval = sctp_send_asconf(asoc, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | out: |
| 628 | return retval; |
| 629 | } |
| 630 | |
| 631 | /* Remove a list of addresses from bind addresses list. Do not remove the |
| 632 | * last address. |
| 633 | * |
| 634 | * Basically run through each address specified in the addrs/addrcnt |
| 635 | * array/length pair, determine if it is IPv6 or IPv4 and call |
| 636 | * sctp_del_bind() on it. |
| 637 | * |
| 638 | * If any of them fails, then the operation will be reversed and the |
| 639 | * ones that were removed will be added back. |
| 640 | * |
| 641 | * At least one address has to be left; if only one address is |
| 642 | * available, the operation will return -EBUSY. |
| 643 | * |
| 644 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 645 | */ |
sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 646 | 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] | 647 | { |
| 648 | struct sctp_sock *sp = sctp_sk(sk); |
| 649 | struct sctp_endpoint *ep = sp->ep; |
| 650 | int cnt; |
| 651 | struct sctp_bind_addr *bp = &ep->base.bind_addr; |
| 652 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | void *addr_buf; |
Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 654 | union sctp_addr *sa_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | struct sctp_af *af; |
| 656 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 657 | pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", |
| 658 | __func__, sk, addrs, addrcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | addr_buf = addrs; |
| 661 | for (cnt = 0; cnt < addrcnt; cnt++) { |
| 662 | /* If the bind address list is empty or if there is only one |
| 663 | * bind address, there is nothing more to be removed (we need |
| 664 | * at least one address here). |
| 665 | */ |
| 666 | if (list_empty(&bp->address_list) || |
| 667 | (sctp_list_single_entry(&bp->address_list))) { |
| 668 | retval = -EBUSY; |
| 669 | goto err_bindx_rem; |
| 670 | } |
| 671 | |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 672 | sa_addr = addr_buf; |
Al Viro | c9a0850 | 2006-11-20 17:07:48 -0800 | [diff] [blame] | 673 | af = sctp_get_af_specific(sa_addr->sa.sa_family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | if (!af) { |
| 675 | retval = -EINVAL; |
| 676 | goto err_bindx_rem; |
| 677 | } |
Paolo Galtieri | 0304ff8a | 2007-04-17 12:52:36 -0700 | [diff] [blame] | 678 | |
| 679 | if (!af->addr_valid(sa_addr, sp, NULL)) { |
| 680 | retval = -EADDRNOTAVAIL; |
| 681 | goto err_bindx_rem; |
| 682 | } |
| 683 | |
Vlad Yasevich | ee9cbac | 2011-04-18 19:14:47 +0000 | [diff] [blame] | 684 | if (sa_addr->v4.sin_port && |
| 685 | sa_addr->v4.sin_port != htons(bp->port)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | retval = -EINVAL; |
| 687 | goto err_bindx_rem; |
| 688 | } |
| 689 | |
Vlad Yasevich | ee9cbac | 2011-04-18 19:14:47 +0000 | [diff] [blame] | 690 | if (!sa_addr->v4.sin_port) |
| 691 | sa_addr->v4.sin_port = htons(bp->port); |
| 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | /* FIXME - There is probably a need to check if sk->sk_saddr and |
| 694 | * sk->sk_rcv_addr are currently set to one of the addresses to |
| 695 | * be removed. This is something which needs to be looked into |
| 696 | * when we are fixing the outstanding issues with multi-homing |
| 697 | * socket routing and failover schemes. Refer to comments in |
| 698 | * sctp_do_bind(). -daisy |
| 699 | */ |
Vlad Yasevich | 0ed90fb | 2007-10-24 16:10:00 -0400 | [diff] [blame] | 700 | retval = sctp_del_bind_addr(bp, sa_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | addr_buf += af->sockaddr_len; |
| 703 | err_bindx_rem: |
| 704 | if (retval < 0) { |
| 705 | /* Failed. Add the ones that has been removed back */ |
| 706 | if (cnt > 0) |
| 707 | sctp_bindx_add(sk, addrs, cnt); |
| 708 | return retval; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | return retval; |
| 713 | } |
| 714 | |
| 715 | /* Send an ASCONF chunk with Delete IP address parameters to all the peers of |
| 716 | * the associations that are part of the endpoint indicating that a list of |
| 717 | * local addresses are removed from the endpoint. |
| 718 | * |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 719 | * 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] | 720 | * association, we do not send the chunk for that association. But it will not |
| 721 | * affect other associations. |
| 722 | * |
| 723 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
| 724 | */ |
| 725 | static int sctp_send_asconf_del_ip(struct sock *sk, |
| 726 | struct sockaddr *addrs, |
| 727 | int addrcnt) |
| 728 | { |
| 729 | struct sctp_sock *sp; |
| 730 | struct sctp_endpoint *ep; |
| 731 | struct sctp_association *asoc; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 732 | struct sctp_transport *transport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | struct sctp_bind_addr *bp; |
| 734 | struct sctp_chunk *chunk; |
| 735 | union sctp_addr *laddr; |
| 736 | void *addr_buf; |
| 737 | struct sctp_af *af; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 738 | struct sctp_sockaddr_entry *saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | int i; |
| 740 | int retval = 0; |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 741 | int stored = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 743 | chunk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | sp = sctp_sk(sk); |
| 745 | ep = sp->ep; |
| 746 | |
Xin Long | 4e27428 | 2019-08-19 22:02:43 +0800 | [diff] [blame] | 747 | if (!ep->asconf_enable) |
| 748 | return retval; |
| 749 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 750 | pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", |
| 751 | __func__, sk, addrs, addrcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 753 | list_for_each_entry(asoc, &ep->asocs, asocs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
| 755 | if (!asoc->peer.asconf_capable) |
| 756 | continue; |
| 757 | |
| 758 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP) |
| 759 | continue; |
| 760 | |
| 761 | if (!sctp_state(asoc, ESTABLISHED)) |
| 762 | continue; |
| 763 | |
| 764 | /* Check if any address in the packed array of addresses is |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 765 | * not present in the bind address list of the association. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | * If so, do not send the asconf chunk to its peer, but |
| 767 | * continue with other associations. |
| 768 | */ |
| 769 | addr_buf = addrs; |
| 770 | for (i = 0; i < addrcnt; i++) { |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 771 | laddr = addr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | af = sctp_get_af_specific(laddr->v4.sin_family); |
| 773 | if (!af) { |
| 774 | retval = -EINVAL; |
| 775 | goto out; |
| 776 | } |
| 777 | |
| 778 | if (!sctp_assoc_lookup_laddr(asoc, laddr)) |
| 779 | break; |
| 780 | |
| 781 | addr_buf += af->sockaddr_len; |
| 782 | } |
| 783 | if (i < addrcnt) |
| 784 | continue; |
| 785 | |
| 786 | /* Find one address in the association's bind address list |
| 787 | * that is not in the packed array of addresses. This is to |
| 788 | * make sure that we do not delete all the addresses in the |
| 789 | * association. |
| 790 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | bp = &asoc->base.bind_addr; |
| 792 | laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs, |
| 793 | addrcnt, sp); |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 794 | if ((laddr == NULL) && (addrcnt == 1)) { |
| 795 | if (asoc->asconf_addr_del_pending) |
| 796 | continue; |
| 797 | asoc->asconf_addr_del_pending = |
| 798 | kzalloc(sizeof(union sctp_addr), GFP_ATOMIC); |
Michio Honda | 6d65e5e | 2011-06-10 16:42:14 +0900 | [diff] [blame] | 799 | if (asoc->asconf_addr_del_pending == NULL) { |
| 800 | retval = -ENOMEM; |
| 801 | goto out; |
| 802 | } |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 803 | asoc->asconf_addr_del_pending->sa.sa_family = |
| 804 | addrs->sa_family; |
| 805 | asoc->asconf_addr_del_pending->v4.sin_port = |
| 806 | htons(bp->port); |
| 807 | if (addrs->sa_family == AF_INET) { |
| 808 | struct sockaddr_in *sin; |
| 809 | |
| 810 | sin = (struct sockaddr_in *)addrs; |
| 811 | asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr; |
| 812 | } else if (addrs->sa_family == AF_INET6) { |
| 813 | struct sockaddr_in6 *sin6; |
| 814 | |
| 815 | sin6 = (struct sockaddr_in6 *)addrs; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 816 | asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr; |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 817 | } |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 818 | |
| 819 | pr_debug("%s: keep the last address asoc:%p %pISc at %p\n", |
| 820 | __func__, asoc, &asoc->asconf_addr_del_pending->sa, |
| 821 | asoc->asconf_addr_del_pending); |
| 822 | |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 823 | asoc->src_out_of_asoc_ok = 1; |
| 824 | stored = 1; |
| 825 | goto skip_mkasconf; |
| 826 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Daniel Borkmann | 88362ad | 2013-09-07 20:51:21 +0200 | [diff] [blame] | 828 | if (laddr == NULL) |
| 829 | return -EINVAL; |
| 830 | |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 831 | /* We do not need RCU protection throughout this loop |
| 832 | * because this is done under a socket lock from the |
| 833 | * setsockopt call. |
| 834 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, |
| 836 | SCTP_PARAM_DEL_IP); |
| 837 | if (!chunk) { |
| 838 | retval = -ENOMEM; |
| 839 | goto out; |
| 840 | } |
| 841 | |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 842 | skip_mkasconf: |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 843 | /* Reset use_as_src flag for the addresses in the bind address |
| 844 | * list that are to be deleted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | */ |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 846 | addr_buf = addrs; |
| 847 | for (i = 0; i < addrcnt; i++) { |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 848 | laddr = addr_buf; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 849 | af = sctp_get_af_specific(laddr->v4.sin_family); |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 850 | list_for_each_entry(saddr, &bp->address_list, list) { |
Al Viro | 5f242a13 | 2006-11-20 17:05:23 -0800 | [diff] [blame] | 851 | if (sctp_cmp_addr_exact(&saddr->a, laddr)) |
Vlad Yasevich | f57d96b | 2007-12-20 14:12:24 -0800 | [diff] [blame] | 852 | saddr->state = SCTP_ADDR_DEL; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 853 | } |
| 854 | addr_buf += af->sockaddr_len; |
| 855 | } |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 856 | |
| 857 | /* Update the route and saddr entries for all the transports |
| 858 | * as some of the addresses in the bind address list are |
| 859 | * about to be deleted and cannot be used as source addresses. |
| 860 | */ |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 861 | list_for_each_entry(transport, &asoc->peer.transport_addr_list, |
| 862 | transports) { |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 863 | sctp_transport_route(transport, NULL, |
| 864 | sctp_sk(asoc->base.sk)); |
| 865 | } |
| 866 | |
Michio Honda | 8a07eb0 | 2011-04-26 20:19:36 +0900 | [diff] [blame] | 867 | if (stored) |
| 868 | /* We don't need to transmit ASCONF */ |
| 869 | continue; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 870 | retval = sctp_send_asconf(asoc, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | } |
| 872 | out: |
| 873 | return retval; |
| 874 | } |
| 875 | |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 876 | /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */ |
| 877 | int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw) |
| 878 | { |
| 879 | struct sock *sk = sctp_opt2sk(sp); |
| 880 | union sctp_addr *addr; |
| 881 | struct sctp_af *af; |
| 882 | |
| 883 | /* It is safe to write port space in caller. */ |
| 884 | addr = &addrw->a; |
| 885 | addr->v4.sin_port = htons(sp->ep->base.bind_addr.port); |
| 886 | af = sctp_get_af_specific(addr->sa.sa_family); |
| 887 | if (!af) |
| 888 | return -EINVAL; |
| 889 | if (sctp_verify_addr(sk, addr, af->sockaddr_len)) |
| 890 | return -EINVAL; |
| 891 | |
| 892 | if (addrw->state == SCTP_ADDR_NEW) |
| 893 | return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1); |
| 894 | else |
| 895 | return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1); |
| 896 | } |
| 897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt() |
| 899 | * |
| 900 | * API 8.1 |
| 901 | * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, |
| 902 | * int flags); |
| 903 | * |
| 904 | * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. |
| 905 | * If the sd is an IPv6 socket, the addresses passed can either be IPv4 |
| 906 | * or IPv6 addresses. |
| 907 | * |
| 908 | * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see |
| 909 | * Section 3.1.2 for this usage. |
| 910 | * |
| 911 | * addrs is a pointer to an array of one or more socket addresses. Each |
| 912 | * address is contained in its appropriate structure (i.e. struct |
| 913 | * sockaddr_in or struct sockaddr_in6) the family of the address type |
Ville Nuorvala | 23c435f | 2006-10-16 22:08:28 -0700 | [diff] [blame] | 914 | * must be used to distinguish the address length (note that this |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | * representation is termed a "packed array" of addresses). The caller |
| 916 | * specifies the number of addresses in the array with addrcnt. |
| 917 | * |
| 918 | * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns |
| 919 | * -1, and sets errno to the appropriate error code. |
| 920 | * |
| 921 | * For SCTP, the port given in each socket address must be the same, or |
| 922 | * sctp_bindx() will fail, setting errno to EINVAL. |
| 923 | * |
| 924 | * The flags parameter is formed from the bitwise OR of zero or more of |
| 925 | * the following currently defined flags: |
| 926 | * |
| 927 | * SCTP_BINDX_ADD_ADDR |
| 928 | * |
| 929 | * SCTP_BINDX_REM_ADDR |
| 930 | * |
| 931 | * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the |
| 932 | * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given |
| 933 | * addresses from the association. The two flags are mutually exclusive; |
| 934 | * if both are given, sctp_bindx() will fail with EINVAL. A caller may |
| 935 | * not remove all addresses from an association; sctp_bindx() will |
| 936 | * reject such an attempt with EINVAL. |
| 937 | * |
| 938 | * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate |
| 939 | * additional addresses with an endpoint after calling bind(). Or use |
| 940 | * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening |
| 941 | * socket is associated with so that no new association accepted will be |
| 942 | * associated with those addresses. If the endpoint supports dynamic |
| 943 | * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a |
| 944 | * endpoint to send the appropriate message to the peer to change the |
| 945 | * peers address lists. |
| 946 | * |
| 947 | * Adding and removing addresses from a connected association is |
| 948 | * optional functionality. Implementations that do not support this |
| 949 | * functionality should return EOPNOTSUPP. |
| 950 | * |
| 951 | * Basically do nothing but copying the addresses from user to kernel |
| 952 | * 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] | 953 | * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() |
| 954 | * from userspace. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | * On exit there is no need to do sockfd_put(), sys_setsockopt() does |
| 957 | * it. |
| 958 | * |
| 959 | * sk The sk of the socket |
| 960 | * addrs The pointer to the addresses in user land |
| 961 | * addrssize Size of the addrs buffer |
| 962 | * op Operation to perform (add or remove, see the flags of |
| 963 | * sctp_bindx) |
| 964 | * |
| 965 | * Returns 0 if ok, <0 errno code on error. |
| 966 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 967 | static int sctp_setsockopt_bindx(struct sock *sk, |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 968 | struct sockaddr __user *addrs, |
| 969 | int addrs_size, int op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | { |
| 971 | struct sockaddr *kaddrs; |
| 972 | int err; |
| 973 | int addrcnt = 0; |
| 974 | int walk_size = 0; |
| 975 | struct sockaddr *sa_addr; |
| 976 | void *addr_buf; |
| 977 | struct sctp_af *af; |
| 978 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 979 | pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n", |
| 980 | __func__, sk, addrs, addrs_size, op); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
| 982 | if (unlikely(addrs_size <= 0)) |
| 983 | return -EINVAL; |
| 984 | |
Xin Long | ef82bcf | 2019-03-20 14:49:38 +0800 | [diff] [blame] | 985 | kaddrs = memdup_user(addrs, addrs_size); |
Enrico Weigelt | d4e575b | 2019-07-29 20:55:21 +0200 | [diff] [blame] | 986 | if (IS_ERR(kaddrs)) |
Al Viro | c981f25 | 2018-01-07 13:19:09 -0500 | [diff] [blame] | 987 | return PTR_ERR(kaddrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 989 | /* Walk through the addrs buffer and count the number of addresses. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | addr_buf = kaddrs; |
| 991 | while (walk_size < addrs_size) { |
Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 992 | if (walk_size + sizeof(sa_family_t) > addrs_size) { |
Xin Long | ef82bcf | 2019-03-20 14:49:38 +0800 | [diff] [blame] | 993 | kfree(kaddrs); |
Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 994 | return -EINVAL; |
| 995 | } |
| 996 | |
Joe Perches | ea11073 | 2011-06-13 16:21:26 +0000 | [diff] [blame] | 997 | sa_addr = addr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | af = sctp_get_af_specific(sa_addr->sa_family); |
| 999 | |
| 1000 | /* If the address family is not supported or if this address |
| 1001 | * causes the address buffer to overflow return EINVAL. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 1002 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | if (!af || (walk_size + af->sockaddr_len) > addrs_size) { |
Xin Long | ef82bcf | 2019-03-20 14:49:38 +0800 | [diff] [blame] | 1004 | kfree(kaddrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | return -EINVAL; |
| 1006 | } |
| 1007 | addrcnt++; |
| 1008 | addr_buf += af->sockaddr_len; |
| 1009 | walk_size += af->sockaddr_len; |
| 1010 | } |
| 1011 | |
| 1012 | /* Do the work. */ |
| 1013 | switch (op) { |
| 1014 | case SCTP_BINDX_ADD_ADDR: |
Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 1015 | /* Allow security module to validate bindx addresses. */ |
| 1016 | err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD, |
| 1017 | (struct sockaddr *)kaddrs, |
| 1018 | addrs_size); |
| 1019 | if (err) |
| 1020 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | err = sctp_bindx_add(sk, kaddrs, addrcnt); |
| 1022 | if (err) |
| 1023 | goto out; |
| 1024 | err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt); |
| 1025 | break; |
| 1026 | |
| 1027 | case SCTP_BINDX_REM_ADDR: |
| 1028 | err = sctp_bindx_rem(sk, kaddrs, addrcnt); |
| 1029 | if (err) |
| 1030 | goto out; |
| 1031 | err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt); |
| 1032 | break; |
| 1033 | |
| 1034 | default: |
| 1035 | err = -EINVAL; |
| 1036 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1037 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
| 1039 | out: |
Xin Long | ef82bcf | 2019-03-20 14:49:38 +0800 | [diff] [blame] | 1040 | kfree(kaddrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
| 1042 | return err; |
| 1043 | } |
| 1044 | |
Xin Long | f26f995 | 2019-07-30 20:38:22 +0800 | [diff] [blame] | 1045 | static int sctp_connect_new_asoc(struct sctp_endpoint *ep, |
| 1046 | const union sctp_addr *daddr, |
| 1047 | const struct sctp_initmsg *init, |
| 1048 | struct sctp_transport **tp) |
| 1049 | { |
| 1050 | struct sctp_association *asoc; |
| 1051 | struct sock *sk = ep->base.sk; |
| 1052 | struct net *net = sock_net(sk); |
| 1053 | enum sctp_scope scope; |
| 1054 | int err; |
| 1055 | |
| 1056 | if (sctp_endpoint_is_peeled_off(ep, daddr)) |
| 1057 | return -EADDRNOTAVAIL; |
| 1058 | |
| 1059 | if (!ep->base.bind_addr.port) { |
| 1060 | if (sctp_autobind(sk)) |
| 1061 | return -EAGAIN; |
| 1062 | } else { |
Maciej Żenczykowski | 82f31eb | 2019-11-25 15:37:04 -0800 | [diff] [blame] | 1063 | if (inet_port_requires_bind_service(net, ep->base.bind_addr.port) && |
Xin Long | f26f995 | 2019-07-30 20:38:22 +0800 | [diff] [blame] | 1064 | !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) |
| 1065 | return -EACCES; |
| 1066 | } |
| 1067 | |
| 1068 | scope = sctp_scope(daddr); |
| 1069 | asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); |
| 1070 | if (!asoc) |
| 1071 | return -ENOMEM; |
| 1072 | |
| 1073 | err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL); |
| 1074 | if (err < 0) |
| 1075 | goto free; |
| 1076 | |
| 1077 | *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN); |
| 1078 | if (!*tp) { |
| 1079 | err = -ENOMEM; |
| 1080 | goto free; |
| 1081 | } |
| 1082 | |
| 1083 | if (!init) |
| 1084 | return 0; |
| 1085 | |
| 1086 | if (init->sinit_num_ostreams) { |
| 1087 | __u16 outcnt = init->sinit_num_ostreams; |
| 1088 | |
| 1089 | asoc->c.sinit_num_ostreams = outcnt; |
| 1090 | /* outcnt has been changed, need to re-init stream */ |
| 1091 | err = sctp_stream_init(&asoc->stream, outcnt, 0, GFP_KERNEL); |
| 1092 | if (err) |
| 1093 | goto free; |
| 1094 | } |
| 1095 | |
| 1096 | if (init->sinit_max_instreams) |
| 1097 | asoc->c.sinit_max_instreams = init->sinit_max_instreams; |
| 1098 | |
| 1099 | if (init->sinit_max_attempts) |
| 1100 | asoc->max_init_attempts = init->sinit_max_attempts; |
| 1101 | |
| 1102 | if (init->sinit_max_init_timeo) |
| 1103 | asoc->max_init_timeo = |
| 1104 | msecs_to_jiffies(init->sinit_max_init_timeo); |
| 1105 | |
| 1106 | return 0; |
| 1107 | free: |
| 1108 | sctp_association_free(asoc); |
| 1109 | return err; |
| 1110 | } |
| 1111 | |
Xin Long | a64e59c | 2019-07-30 20:38:23 +0800 | [diff] [blame] | 1112 | static int sctp_connect_add_peer(struct sctp_association *asoc, |
| 1113 | union sctp_addr *daddr, int addr_len) |
| 1114 | { |
| 1115 | struct sctp_endpoint *ep = asoc->ep; |
| 1116 | struct sctp_association *old; |
| 1117 | struct sctp_transport *t; |
| 1118 | int err; |
| 1119 | |
| 1120 | err = sctp_verify_addr(ep->base.sk, daddr, addr_len); |
| 1121 | if (err) |
| 1122 | return err; |
| 1123 | |
| 1124 | old = sctp_endpoint_lookup_assoc(ep, daddr, &t); |
| 1125 | if (old && old != asoc) |
| 1126 | return old->state >= SCTP_STATE_ESTABLISHED ? -EISCONN |
| 1127 | : -EALREADY; |
| 1128 | |
| 1129 | if (sctp_endpoint_is_peeled_off(ep, daddr)) |
| 1130 | return -EADDRNOTAVAIL; |
| 1131 | |
| 1132 | t = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN); |
| 1133 | if (!t) |
| 1134 | return -ENOMEM; |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1139 | /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) |
| 1140 | * |
| 1141 | * Common routine for handling connect() and sctp_connectx(). |
| 1142 | * Connect will come in with just a single address. |
| 1143 | */ |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1144 | static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs, |
| 1145 | int addrs_size, int flags, sctp_assoc_t *assoc_id) |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1146 | { |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1147 | struct sctp_sock *sp = sctp_sk(sk); |
| 1148 | struct sctp_endpoint *ep = sp->ep; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1149 | struct sctp_transport *transport; |
Xin Long | a64e59c | 2019-07-30 20:38:23 +0800 | [diff] [blame] | 1150 | struct sctp_association *asoc; |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1151 | void *addr_buf = kaddrs; |
| 1152 | union sctp_addr *daddr; |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1153 | struct sctp_af *af; |
| 1154 | int walk_size, err; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1155 | long timeo; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1156 | |
Marcelo Ricardo Leitner | e5b13f3 | 2016-07-15 16:38:19 -0300 | [diff] [blame] | 1157 | if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) || |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1158 | (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) |
| 1159 | return -EISCONN; |
| 1160 | |
| 1161 | daddr = addr_buf; |
| 1162 | af = sctp_get_af_specific(daddr->sa.sa_family); |
| 1163 | if (!af || af->sockaddr_len > addrs_size) |
| 1164 | return -EINVAL; |
| 1165 | |
| 1166 | err = sctp_verify_addr(sk, daddr, af->sockaddr_len); |
| 1167 | if (err) |
| 1168 | return err; |
| 1169 | |
| 1170 | asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport); |
| 1171 | if (asoc) |
| 1172 | return asoc->state >= SCTP_STATE_ESTABLISHED ? -EISCONN |
| 1173 | : -EALREADY; |
| 1174 | |
Xin Long | f26f995 | 2019-07-30 20:38:22 +0800 | [diff] [blame] | 1175 | err = sctp_connect_new_asoc(ep, daddr, NULL, &transport); |
| 1176 | if (err) |
| 1177 | return err; |
| 1178 | asoc = transport->asoc; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1179 | |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1180 | addr_buf += af->sockaddr_len; |
| 1181 | walk_size = af->sockaddr_len; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1182 | while (walk_size < addrs_size) { |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1183 | err = -EINVAL; |
| 1184 | if (walk_size + sizeof(sa_family_t) > addrs_size) |
Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1185 | goto out_free; |
Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1186 | |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1187 | daddr = addr_buf; |
| 1188 | af = sctp_get_af_specific(daddr->sa.sa_family); |
| 1189 | if (!af || af->sockaddr_len + walk_size > addrs_size) |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1190 | goto out_free; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1191 | |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1192 | if (asoc->peer.port != ntohs(daddr->v4.sin_port)) |
| 1193 | goto out_free; |
Dan Rosenberg | d7e0d19 | 2010-10-01 11:16:58 +0000 | [diff] [blame] | 1194 | |
Xin Long | a64e59c | 2019-07-30 20:38:23 +0800 | [diff] [blame] | 1195 | err = sctp_connect_add_peer(asoc, daddr, af->sockaddr_len); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1196 | if (err) |
| 1197 | goto out_free; |
| 1198 | |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1199 | addr_buf += af->sockaddr_len; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1200 | walk_size += af->sockaddr_len; |
| 1201 | } |
| 1202 | |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1203 | /* In case the user of sctp_connectx() wants an association |
| 1204 | * id back, assign one now. |
| 1205 | */ |
| 1206 | if (assoc_id) { |
| 1207 | err = sctp_assoc_set_id(asoc, GFP_KERNEL); |
| 1208 | if (err < 0) |
| 1209 | goto out_free; |
| 1210 | } |
| 1211 | |
Xin Long | f26f995 | 2019-07-30 20:38:22 +0800 | [diff] [blame] | 1212 | err = sctp_primitive_ASSOCIATE(sock_net(sk), asoc, NULL); |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1213 | if (err < 0) |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1214 | goto out_free; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1215 | |
| 1216 | /* Initialize sk's dport and daddr for getpeername() */ |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1217 | inet_sk(sk)->inet_dport = htons(asoc->peer.port); |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1218 | sp->pf->to_sk_daddr(daddr, sk); |
Sridhar Samudrala | 8de8c87 | 2006-05-19 10:58:12 -0700 | [diff] [blame] | 1219 | sk->sk_err = 0; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1220 | |
Marcelo Ricardo Leitner | 7233bc8 | 2016-11-03 17:03:41 -0200 | [diff] [blame] | 1221 | if (assoc_id) |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1222 | *assoc_id = asoc->assoc_id; |
Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 1223 | |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1224 | timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); |
| 1225 | return sctp_wait_for_connect(asoc, &timeo); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1226 | |
| 1227 | out_free: |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1228 | pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n", |
| 1229 | __func__, asoc, kaddrs, err); |
Xin Long | dd8378b3 | 2019-07-30 20:38:21 +0800 | [diff] [blame] | 1230 | sctp_association_free(asoc); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1231 | return err; |
| 1232 | } |
| 1233 | |
| 1234 | /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt() |
| 1235 | * |
| 1236 | * API 8.9 |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1237 | * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt, |
| 1238 | * sctp_assoc_t *asoc); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1239 | * |
| 1240 | * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. |
| 1241 | * If the sd is an IPv6 socket, the addresses passed can either be IPv4 |
| 1242 | * or IPv6 addresses. |
| 1243 | * |
| 1244 | * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see |
| 1245 | * Section 3.1.2 for this usage. |
| 1246 | * |
| 1247 | * addrs is a pointer to an array of one or more socket addresses. Each |
| 1248 | * address is contained in its appropriate structure (i.e. struct |
| 1249 | * sockaddr_in or struct sockaddr_in6) the family of the address type |
| 1250 | * must be used to distengish the address length (note that this |
| 1251 | * representation is termed a "packed array" of addresses). The caller |
| 1252 | * specifies the number of addresses in the array with addrcnt. |
| 1253 | * |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1254 | * On success, sctp_connectx() returns 0. It also sets the assoc_id to |
| 1255 | * the association id of the new association. On failure, sctp_connectx() |
| 1256 | * returns -1, and sets errno to the appropriate error code. The assoc_id |
| 1257 | * is not touched by the kernel. |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1258 | * |
| 1259 | * For SCTP, the port given in each socket address must be the same, or |
| 1260 | * sctp_connectx() will fail, setting errno to EINVAL. |
| 1261 | * |
| 1262 | * An application can use sctp_connectx to initiate an association with |
| 1263 | * an endpoint that is multi-homed. Much like sctp_bindx() this call |
| 1264 | * allows a caller to specify multiple addresses at which a peer can be |
| 1265 | * 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] | 1266 | * the association is implementation dependent. This function only |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1267 | * specifies that the stack will try to make use of all the addresses in |
| 1268 | * the list when needed. |
| 1269 | * |
| 1270 | * Note that the list of addresses passed in is only used for setting up |
| 1271 | * the association. It does not necessarily equal the set of addresses |
| 1272 | * the peer uses for the resulting association. If the caller wants to |
| 1273 | * find out the set of peer addresses, it must use sctp_getpaddrs() to |
| 1274 | * retrieve them after the association has been set up. |
| 1275 | * |
| 1276 | * Basically do nothing but copying the addresses from user to kernel |
| 1277 | * land and invoking either sctp_connectx(). This is used for tunneling |
| 1278 | * the sctp_connectx() request through sctp_setsockopt() from userspace. |
| 1279 | * |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1280 | * On exit there is no need to do sockfd_put(), sys_setsockopt() does |
| 1281 | * it. |
| 1282 | * |
| 1283 | * sk The sk of the socket |
| 1284 | * addrs The pointer to the addresses in user land |
| 1285 | * addrssize Size of the addrs buffer |
| 1286 | * |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1287 | * Returns >=0 if ok, <0 errno code on error. |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1288 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1289 | static int __sctp_setsockopt_connectx(struct sock *sk, |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1290 | struct sockaddr __user *addrs, |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1291 | int addrs_size, |
| 1292 | sctp_assoc_t *assoc_id) |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1293 | { |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1294 | struct sockaddr *kaddrs; |
Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 1295 | int err = 0, flags = 0; |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1296 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1297 | pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n", |
| 1298 | __func__, sk, addrs, addrs_size); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1299 | |
Xin Long | f40f117 | 2019-07-30 20:38:20 +0800 | [diff] [blame] | 1300 | /* make sure the 1st addr's sa_family is accessible later */ |
| 1301 | if (unlikely(addrs_size < sizeof(sa_family_t))) |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1302 | return -EINVAL; |
| 1303 | |
Xin Long | ef82bcf | 2019-03-20 14:49:38 +0800 | [diff] [blame] | 1304 | kaddrs = memdup_user(addrs, addrs_size); |
Enrico Weigelt | d4e575b | 2019-07-29 20:55:21 +0200 | [diff] [blame] | 1305 | if (IS_ERR(kaddrs)) |
Al Viro | c981f25 | 2018-01-07 13:19:09 -0500 | [diff] [blame] | 1306 | return PTR_ERR(kaddrs); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1307 | |
Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 1308 | /* Allow security module to validate connectx addresses. */ |
| 1309 | err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX, |
| 1310 | (struct sockaddr *)kaddrs, |
| 1311 | addrs_size); |
| 1312 | if (err) |
| 1313 | goto out_free; |
| 1314 | |
Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 1315 | /* in-kernel sockets don't generally have a file allocated to them |
| 1316 | * if all they do is call sock_create_kern(). |
| 1317 | */ |
| 1318 | if (sk->sk_socket->file) |
| 1319 | flags = sk->sk_socket->file->f_flags; |
| 1320 | |
| 1321 | err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id); |
Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 1322 | |
| 1323 | out_free: |
Xin Long | ef82bcf | 2019-03-20 14:49:38 +0800 | [diff] [blame] | 1324 | kfree(kaddrs); |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1325 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 1326 | return err; |
| 1327 | } |
| 1328 | |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1329 | /* |
| 1330 | * This is an older interface. It's kept for backward compatibility |
| 1331 | * to the option that doesn't provide association id. |
| 1332 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1333 | static int sctp_setsockopt_connectx_old(struct sock *sk, |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1334 | struct sockaddr __user *addrs, |
| 1335 | int addrs_size) |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1336 | { |
| 1337 | return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL); |
| 1338 | } |
| 1339 | |
| 1340 | /* |
| 1341 | * New interface for the API. The since the API is done with a socket |
| 1342 | * option, to make it simple we feed back the association id is as a return |
| 1343 | * indication to the call. Error is always negative and association id is |
| 1344 | * always positive. |
| 1345 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1346 | static int sctp_setsockopt_connectx(struct sock *sk, |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1347 | struct sockaddr __user *addrs, |
| 1348 | int addrs_size) |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 1349 | { |
| 1350 | sctp_assoc_t assoc_id = 0; |
| 1351 | int err = 0; |
| 1352 | |
| 1353 | err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id); |
| 1354 | |
| 1355 | if (err) |
| 1356 | return err; |
| 1357 | else |
| 1358 | return assoc_id; |
| 1359 | } |
| 1360 | |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1361 | /* |
Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1362 | * New (hopefully final) interface for the API. |
| 1363 | * We use the sctp_getaddrs_old structure so that use-space library |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1364 | * can avoid any unnecessary allocations. The only different part |
Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1365 | * 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] | 1366 | * addrs_num structure member. That way we can re-use the existing |
Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1367 | * code. |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1368 | */ |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1369 | #ifdef CONFIG_COMPAT |
| 1370 | struct compat_sctp_getaddrs_old { |
| 1371 | sctp_assoc_t assoc_id; |
| 1372 | s32 addr_num; |
| 1373 | compat_uptr_t addrs; /* struct sockaddr * */ |
| 1374 | }; |
| 1375 | #endif |
| 1376 | |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 1377 | static int sctp_getsockopt_connectx3(struct sock *sk, int len, |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1378 | char __user *optval, |
| 1379 | int __user *optlen) |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1380 | { |
Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1381 | struct sctp_getaddrs_old param; |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1382 | sctp_assoc_t assoc_id = 0; |
| 1383 | int err = 0; |
| 1384 | |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1385 | #ifdef CONFIG_COMPAT |
Andy Lutomirski | 96c0e0a | 2016-03-22 14:25:07 -0700 | [diff] [blame] | 1386 | if (in_compat_syscall()) { |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1387 | struct compat_sctp_getaddrs_old param32; |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1388 | |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1389 | if (len < sizeof(param32)) |
| 1390 | return -EINVAL; |
| 1391 | if (copy_from_user(¶m32, optval, sizeof(param32))) |
| 1392 | return -EFAULT; |
Vlad Yasevich | f9c6781 | 2009-11-11 08:19:24 +0000 | [diff] [blame] | 1393 | |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1394 | param.assoc_id = param32.assoc_id; |
| 1395 | param.addr_num = param32.addr_num; |
| 1396 | param.addrs = compat_ptr(param32.addrs); |
| 1397 | } else |
| 1398 | #endif |
| 1399 | { |
| 1400 | if (len < sizeof(param)) |
| 1401 | return -EINVAL; |
| 1402 | if (copy_from_user(¶m, optval, sizeof(param))) |
| 1403 | return -EFAULT; |
| 1404 | } |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1405 | |
Daniel Borkmann | ffd5939 | 2014-02-17 12:11:11 +0100 | [diff] [blame] | 1406 | err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *) |
| 1407 | param.addrs, param.addr_num, |
| 1408 | &assoc_id); |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 1409 | if (err == 0 || err == -EINPROGRESS) { |
| 1410 | if (copy_to_user(optval, &assoc_id, sizeof(assoc_id))) |
| 1411 | return -EFAULT; |
| 1412 | if (put_user(sizeof(assoc_id), optlen)) |
| 1413 | return -EFAULT; |
| 1414 | } |
| 1415 | |
| 1416 | return err; |
| 1417 | } |
| 1418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | /* API 3.1.4 close() - UDP Style Syntax |
| 1420 | * Applications use close() to perform graceful shutdown (as described in |
| 1421 | * Section 10.1 of [SCTP]) on ALL the associations currently represented |
| 1422 | * by a UDP-style socket. |
| 1423 | * |
| 1424 | * The syntax is |
| 1425 | * |
| 1426 | * ret = close(int sd); |
| 1427 | * |
| 1428 | * sd - the socket descriptor of the associations to be closed. |
| 1429 | * |
| 1430 | * To gracefully shutdown a specific association represented by the |
| 1431 | * UDP-style socket, an application should use the sendmsg() call, |
| 1432 | * passing no user data, but including the appropriate flag in the |
| 1433 | * ancillary data (see Section xxxx). |
| 1434 | * |
| 1435 | * If sd in the close() call is a branched-off socket representing only |
| 1436 | * one association, the shutdown is performed on that association only. |
| 1437 | * |
| 1438 | * 4.1.6 close() - TCP Style Syntax |
| 1439 | * |
| 1440 | * Applications use close() to gracefully close down an association. |
| 1441 | * |
| 1442 | * The syntax is: |
| 1443 | * |
| 1444 | * int close(int sd); |
| 1445 | * |
| 1446 | * sd - the socket descriptor of the association to be closed. |
| 1447 | * |
| 1448 | * After an application calls close() on a socket descriptor, no further |
| 1449 | * socket operations will succeed on that descriptor. |
| 1450 | * |
| 1451 | * API 7.1.4 SO_LINGER |
| 1452 | * |
| 1453 | * An application using the TCP-style socket can use this option to |
| 1454 | * perform the SCTP ABORT primitive. The linger option structure is: |
| 1455 | * |
| 1456 | * struct linger { |
| 1457 | * int l_onoff; // option on/off |
| 1458 | * int l_linger; // linger time |
| 1459 | * }; |
| 1460 | * |
| 1461 | * To enable the option, set l_onoff to 1. If the l_linger value is set |
| 1462 | * to 0, calling close() is the same as the ABORT primitive. If the |
| 1463 | * value is set to a negative value, the setsockopt() call will return |
| 1464 | * an error. If the value is set to a positive value linger_time, the |
| 1465 | * close() can be blocked for at most linger_time ms. If the graceful |
| 1466 | * shutdown phase does not finish during this period, close() will |
| 1467 | * return but the graceful shutdown phase continues in the system. |
| 1468 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 1469 | static void sctp_close(struct sock *sk, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | { |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1471 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | struct sctp_endpoint *ep; |
| 1473 | struct sctp_association *asoc; |
| 1474 | struct list_head *pos, *temp; |
Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1475 | unsigned int data_was_unread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 1477 | pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | |
Xin Long | 6dfe4b9 | 2017-06-10 14:56:56 +0800 | [diff] [blame] | 1479 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | sk->sk_shutdown = SHUTDOWN_MASK; |
Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 1481 | inet_sk_set_state(sk, SCTP_SS_CLOSING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
| 1483 | ep = sctp_sk(sk)->ep; |
| 1484 | |
Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1485 | /* Clean up any skbs sitting on the receive queue. */ |
| 1486 | data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue); |
| 1487 | data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby); |
| 1488 | |
Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 1489 | /* Walk all associations on an endpoint. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | list_for_each_safe(pos, temp, &ep->asocs) { |
| 1491 | asoc = list_entry(pos, struct sctp_association, asocs); |
| 1492 | |
| 1493 | if (sctp_style(sk, TCP)) { |
| 1494 | /* A closed association can still be in the list if |
| 1495 | * it belongs to a TCP-style listening socket that is |
| 1496 | * not yet accepted. If so, free it. If not, send an |
| 1497 | * ABORT or SHUTDOWN based on the linger options. |
| 1498 | */ |
| 1499 | if (sctp_state(asoc, CLOSED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | sctp_association_free(asoc); |
Vladislav Yasevich | b89498a | 2006-05-19 14:32:06 -0700 | [diff] [blame] | 1501 | continue; |
| 1502 | } |
| 1503 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1505 | if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) || |
| 1506 | !skb_queue_empty(&asoc->ulpq.reasm) || |
Xin Long | 1322823 | 2017-12-08 21:04:09 +0800 | [diff] [blame] | 1507 | !skb_queue_empty(&asoc->ulpq.reasm_uo) || |
Thomas Graf | cd4fcc7 | 2011-07-08 04:37:46 +0000 | [diff] [blame] | 1508 | (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) { |
Sridhar Samudrala | b9ac867 | 2006-08-28 13:53:01 -0700 | [diff] [blame] | 1509 | struct sctp_chunk *chunk; |
| 1510 | |
| 1511 | chunk = sctp_make_abort_user(asoc, NULL, 0); |
Xin Long | 068d8bd | 2015-12-29 17:49:25 +0800 | [diff] [blame] | 1512 | sctp_primitive_ABORT(net, asoc, chunk); |
Sridhar Samudrala | b9ac867 | 2006-08-28 13:53:01 -0700 | [diff] [blame] | 1513 | } else |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 1514 | sctp_primitive_SHUTDOWN(net, asoc, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | /* On a TCP-style socket, block for at most linger_time if set. */ |
| 1518 | if (sctp_style(sk, TCP) && timeout) |
| 1519 | sctp_wait_for_close(sk, timeout); |
| 1520 | |
| 1521 | /* This will run the backlog queue. */ |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 1522 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | |
| 1524 | /* Supposedly, no process has access to the socket, but |
| 1525 | * the net layers still may. |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 1526 | * Also, sctp_destroy_sock() needs to be called with addr_wq_lock |
| 1527 | * held and that should be grabbed before socket lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | */ |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 1529 | spin_lock_bh(&net->sctp.addr_wq_lock); |
Xin Long | 6dfe4b9 | 2017-06-10 14:56:56 +0800 | [diff] [blame] | 1530 | bh_lock_sock_nested(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | |
| 1532 | /* Hold the sock, since sk_common_release() will put sock_put() |
| 1533 | * and we have just a little more cleanup. |
| 1534 | */ |
| 1535 | sock_hold(sk); |
| 1536 | sk_common_release(sk); |
| 1537 | |
wangweidong | 5bc1d1b | 2014-01-21 15:44:12 +0800 | [diff] [blame] | 1538 | bh_unlock_sock(sk); |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 1539 | spin_unlock_bh(&net->sctp.addr_wq_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
| 1541 | sock_put(sk); |
| 1542 | |
| 1543 | SCTP_DBG_OBJCNT_DEC(sock); |
| 1544 | } |
| 1545 | |
| 1546 | /* Handle EPIPE error. */ |
| 1547 | static int sctp_error(struct sock *sk, int flags, int err) |
| 1548 | { |
| 1549 | if (err == -EPIPE) |
| 1550 | err = sock_error(sk) ? : -EPIPE; |
| 1551 | if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) |
| 1552 | send_sig(SIGPIPE, current, 0); |
| 1553 | return err; |
| 1554 | } |
| 1555 | |
| 1556 | /* API 3.1.3 sendmsg() - UDP Style Syntax |
| 1557 | * |
| 1558 | * An application uses sendmsg() and recvmsg() calls to transmit data to |
| 1559 | * and receive data from its peer. |
| 1560 | * |
| 1561 | * ssize_t sendmsg(int socket, const struct msghdr *message, |
| 1562 | * int flags); |
| 1563 | * |
| 1564 | * socket - the socket descriptor of the endpoint. |
| 1565 | * message - pointer to the msghdr structure which contains a single |
| 1566 | * user message and possibly some ancillary data. |
| 1567 | * |
| 1568 | * See Section 5 for complete description of the data |
| 1569 | * structures. |
| 1570 | * |
| 1571 | * flags - flags sent or received with the user message, see Section |
| 1572 | * 5 for complete description of the flags. |
| 1573 | * |
| 1574 | * Note: This function could use a rewrite especially when explicit |
| 1575 | * connect support comes in. |
| 1576 | */ |
| 1577 | /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */ |
| 1578 | |
Xin Long | a05437a | 2017-08-11 10:23:48 +0800 | [diff] [blame] | 1579 | static int sctp_msghdr_parse(const struct msghdr *msg, |
| 1580 | struct sctp_cmsgs *cmsgs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | |
Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 1582 | static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs, |
| 1583 | struct sctp_sndrcvinfo *srinfo, |
| 1584 | const struct msghdr *msg, size_t msg_len) |
| 1585 | { |
| 1586 | __u16 sflags; |
| 1587 | int err; |
| 1588 | |
| 1589 | if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP)) |
| 1590 | return -EPIPE; |
| 1591 | |
| 1592 | if (msg_len > sk->sk_sndbuf) |
| 1593 | return -EMSGSIZE; |
| 1594 | |
| 1595 | memset(cmsgs, 0, sizeof(*cmsgs)); |
| 1596 | err = sctp_msghdr_parse(msg, cmsgs); |
| 1597 | if (err) { |
| 1598 | pr_debug("%s: msghdr parse err:%x\n", __func__, err); |
| 1599 | return err; |
| 1600 | } |
| 1601 | |
| 1602 | memset(srinfo, 0, sizeof(*srinfo)); |
| 1603 | if (cmsgs->srinfo) { |
| 1604 | srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream; |
| 1605 | srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags; |
| 1606 | srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid; |
| 1607 | srinfo->sinfo_context = cmsgs->srinfo->sinfo_context; |
| 1608 | srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id; |
| 1609 | srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive; |
| 1610 | } |
| 1611 | |
| 1612 | if (cmsgs->sinfo) { |
| 1613 | srinfo->sinfo_stream = cmsgs->sinfo->snd_sid; |
| 1614 | srinfo->sinfo_flags = cmsgs->sinfo->snd_flags; |
| 1615 | srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid; |
| 1616 | srinfo->sinfo_context = cmsgs->sinfo->snd_context; |
| 1617 | srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id; |
| 1618 | } |
| 1619 | |
Xin Long | ed63afb | 2018-03-05 20:44:18 +0800 | [diff] [blame] | 1620 | if (cmsgs->prinfo) { |
| 1621 | srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value; |
| 1622 | SCTP_PR_SET_POLICY(srinfo->sinfo_flags, |
| 1623 | cmsgs->prinfo->pr_policy); |
| 1624 | } |
| 1625 | |
Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 1626 | sflags = srinfo->sinfo_flags; |
| 1627 | if (!sflags && msg_len) |
| 1628 | return 0; |
| 1629 | |
| 1630 | if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT))) |
| 1631 | return -EINVAL; |
| 1632 | |
| 1633 | if (((sflags & SCTP_EOF) && msg_len > 0) || |
| 1634 | (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0)) |
| 1635 | return -EINVAL; |
| 1636 | |
| 1637 | if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name) |
| 1638 | return -EINVAL; |
| 1639 | |
| 1640 | return 0; |
| 1641 | } |
| 1642 | |
Xin Long | 2bfd80f | 2018-03-01 23:05:11 +0800 | [diff] [blame] | 1643 | static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags, |
| 1644 | struct sctp_cmsgs *cmsgs, |
| 1645 | union sctp_addr *daddr, |
| 1646 | struct sctp_transport **tp) |
| 1647 | { |
| 1648 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Xin Long | 2bfd80f | 2018-03-01 23:05:11 +0800 | [diff] [blame] | 1649 | struct sctp_association *asoc; |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1650 | struct cmsghdr *cmsg; |
Xin Long | 4be4139 | 2018-07-02 18:21:14 +0800 | [diff] [blame] | 1651 | __be32 flowinfo = 0; |
Linus Torvalds | 9eda2d2 | 2018-04-06 15:39:26 -0700 | [diff] [blame] | 1652 | struct sctp_af *af; |
Wei Yongjun | d98985d | 2018-03-13 03:03:30 +0000 | [diff] [blame] | 1653 | int err; |
Xin Long | 2bfd80f | 2018-03-01 23:05:11 +0800 | [diff] [blame] | 1654 | |
| 1655 | *tp = NULL; |
| 1656 | |
| 1657 | if (sflags & (SCTP_EOF | SCTP_ABORT)) |
| 1658 | return -EINVAL; |
| 1659 | |
| 1660 | if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) || |
| 1661 | sctp_sstate(sk, CLOSING))) |
| 1662 | return -EADDRNOTAVAIL; |
| 1663 | |
Linus Torvalds | 9eda2d2 | 2018-04-06 15:39:26 -0700 | [diff] [blame] | 1664 | /* Label connection socket for first association 1-to-many |
| 1665 | * style for client sequence socket()->sendmsg(). This |
| 1666 | * needs to be done before sctp_assoc_add_peer() as that will |
| 1667 | * set up the initial packet that needs to account for any |
| 1668 | * security ip options (CIPSO/CALIPSO) added to the packet. |
| 1669 | */ |
| 1670 | af = sctp_get_af_specific(daddr->sa.sa_family); |
| 1671 | if (!af) |
| 1672 | return -EINVAL; |
| 1673 | err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT, |
| 1674 | (struct sockaddr *)daddr, |
| 1675 | af->sockaddr_len); |
| 1676 | if (err < 0) |
| 1677 | return err; |
| 1678 | |
Xin Long | f26f995 | 2019-07-30 20:38:22 +0800 | [diff] [blame] | 1679 | err = sctp_connect_new_asoc(ep, daddr, cmsgs->init, tp); |
| 1680 | if (err) |
| 1681 | return err; |
| 1682 | asoc = (*tp)->asoc; |
Xin Long | 2bfd80f | 2018-03-01 23:05:11 +0800 | [diff] [blame] | 1683 | |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1684 | if (!cmsgs->addrs_msg) |
| 1685 | return 0; |
| 1686 | |
Xin Long | 4be4139 | 2018-07-02 18:21:14 +0800 | [diff] [blame] | 1687 | if (daddr->sa.sa_family == AF_INET6) |
| 1688 | flowinfo = daddr->v6.sin6_flowinfo; |
| 1689 | |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1690 | /* sendv addr list parse */ |
| 1691 | for_each_cmsghdr(cmsg, cmsgs->addrs_msg) { |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1692 | union sctp_addr _daddr; |
| 1693 | int dlen; |
| 1694 | |
| 1695 | if (cmsg->cmsg_level != IPPROTO_SCTP || |
| 1696 | (cmsg->cmsg_type != SCTP_DSTADDRV4 && |
| 1697 | cmsg->cmsg_type != SCTP_DSTADDRV6)) |
| 1698 | continue; |
| 1699 | |
| 1700 | daddr = &_daddr; |
| 1701 | memset(daddr, 0, sizeof(*daddr)); |
| 1702 | dlen = cmsg->cmsg_len - sizeof(struct cmsghdr); |
| 1703 | if (cmsg->cmsg_type == SCTP_DSTADDRV4) { |
Wei Yongjun | d98985d | 2018-03-13 03:03:30 +0000 | [diff] [blame] | 1704 | if (dlen < sizeof(struct in_addr)) { |
| 1705 | err = -EINVAL; |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1706 | goto free; |
Wei Yongjun | d98985d | 2018-03-13 03:03:30 +0000 | [diff] [blame] | 1707 | } |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1708 | |
| 1709 | dlen = sizeof(struct in_addr); |
| 1710 | daddr->v4.sin_family = AF_INET; |
| 1711 | daddr->v4.sin_port = htons(asoc->peer.port); |
| 1712 | memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen); |
| 1713 | } else { |
Wei Yongjun | d98985d | 2018-03-13 03:03:30 +0000 | [diff] [blame] | 1714 | if (dlen < sizeof(struct in6_addr)) { |
| 1715 | err = -EINVAL; |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1716 | goto free; |
Wei Yongjun | d98985d | 2018-03-13 03:03:30 +0000 | [diff] [blame] | 1717 | } |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1718 | |
| 1719 | dlen = sizeof(struct in6_addr); |
Xin Long | 4be4139 | 2018-07-02 18:21:14 +0800 | [diff] [blame] | 1720 | daddr->v6.sin6_flowinfo = flowinfo; |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1721 | daddr->v6.sin6_family = AF_INET6; |
| 1722 | daddr->v6.sin6_port = htons(asoc->peer.port); |
| 1723 | memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen); |
| 1724 | } |
Xin Long | a64e59c | 2019-07-30 20:38:23 +0800 | [diff] [blame] | 1725 | |
| 1726 | err = sctp_connect_add_peer(asoc, daddr, sizeof(*daddr)); |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1727 | if (err) |
| 1728 | goto free; |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 1729 | } |
| 1730 | |
Xin Long | 2bfd80f | 2018-03-01 23:05:11 +0800 | [diff] [blame] | 1731 | return 0; |
| 1732 | |
| 1733 | free: |
| 1734 | sctp_association_free(asoc); |
| 1735 | return err; |
| 1736 | } |
| 1737 | |
Xin Long | c2666de | 2018-03-01 23:05:12 +0800 | [diff] [blame] | 1738 | static int sctp_sendmsg_check_sflags(struct sctp_association *asoc, |
| 1739 | __u16 sflags, struct msghdr *msg, |
| 1740 | size_t msg_len) |
| 1741 | { |
| 1742 | struct sock *sk = asoc->base.sk; |
| 1743 | struct net *net = sock_net(sk); |
| 1744 | |
| 1745 | if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) |
| 1746 | return -EPIPE; |
| 1747 | |
Xin Long | 4910280 | 2018-03-05 20:44:20 +0800 | [diff] [blame] | 1748 | if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) && |
| 1749 | !sctp_state(asoc, ESTABLISHED)) |
| 1750 | return 0; |
| 1751 | |
Xin Long | c2666de | 2018-03-01 23:05:12 +0800 | [diff] [blame] | 1752 | if (sflags & SCTP_EOF) { |
| 1753 | pr_debug("%s: shutting down association:%p\n", __func__, asoc); |
| 1754 | sctp_primitive_SHUTDOWN(net, asoc, NULL); |
| 1755 | |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | if (sflags & SCTP_ABORT) { |
| 1760 | struct sctp_chunk *chunk; |
| 1761 | |
| 1762 | chunk = sctp_make_abort_user(asoc, msg, msg_len); |
| 1763 | if (!chunk) |
| 1764 | return -ENOMEM; |
| 1765 | |
| 1766 | pr_debug("%s: aborting association:%p\n", __func__, asoc); |
| 1767 | sctp_primitive_ABORT(net, asoc, chunk); |
Xin Long | 901efe1 | 2019-03-03 16:50:26 +0800 | [diff] [blame] | 1768 | iov_iter_revert(&msg->msg_iter, msg_len); |
Xin Long | c2666de | 2018-03-01 23:05:12 +0800 | [diff] [blame] | 1769 | |
| 1770 | return 0; |
| 1771 | } |
| 1772 | |
| 1773 | return 1; |
| 1774 | } |
| 1775 | |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1776 | static int sctp_sendmsg_to_asoc(struct sctp_association *asoc, |
| 1777 | struct msghdr *msg, size_t msg_len, |
| 1778 | struct sctp_transport *transport, |
| 1779 | struct sctp_sndrcvinfo *sinfo) |
| 1780 | { |
| 1781 | struct sock *sk = asoc->base.sk; |
Marcelo Ricardo Leitner | 63d0133 | 2018-04-26 16:59:00 -0300 | [diff] [blame] | 1782 | struct sctp_sock *sp = sctp_sk(sk); |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1783 | struct net *net = sock_net(sk); |
| 1784 | struct sctp_datamsg *datamsg; |
| 1785 | bool wait_connect = false; |
| 1786 | struct sctp_chunk *chunk; |
| 1787 | long timeo; |
| 1788 | int err; |
| 1789 | |
| 1790 | if (sinfo->sinfo_stream >= asoc->stream.outcnt) { |
| 1791 | err = -EINVAL; |
| 1792 | goto err; |
| 1793 | } |
| 1794 | |
Konstantin Khorenko | 05364ca | 2018-08-10 20:11:42 +0300 | [diff] [blame] | 1795 | if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) { |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1796 | err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream); |
| 1797 | if (err) |
| 1798 | goto err; |
| 1799 | } |
| 1800 | |
Marcelo Ricardo Leitner | 63d0133 | 2018-04-26 16:59:00 -0300 | [diff] [blame] | 1801 | if (sp->disable_fragments && msg_len > asoc->frag_point) { |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1802 | err = -EMSGSIZE; |
| 1803 | goto err; |
| 1804 | } |
| 1805 | |
Marcelo Ricardo Leitner | 2521680 | 2018-04-26 16:58:56 -0300 | [diff] [blame] | 1806 | if (asoc->pmtu_pending) { |
Marcelo Ricardo Leitner | 63d0133 | 2018-04-26 16:59:00 -0300 | [diff] [blame] | 1807 | if (sp->param_flags & SPP_PMTUD_ENABLE) |
| 1808 | sctp_assoc_sync_pmtu(asoc); |
Marcelo Ricardo Leitner | 2521680 | 2018-04-26 16:58:56 -0300 | [diff] [blame] | 1809 | asoc->pmtu_pending = 0; |
| 1810 | } |
Neil Horman | 0aee4c2 | 2018-03-12 14:15:25 -0400 | [diff] [blame] | 1811 | |
Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame] | 1812 | if (sctp_wspace(asoc) < (int)msg_len) |
Neil Horman | 0aee4c2 | 2018-03-12 14:15:25 -0400 | [diff] [blame] | 1813 | sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc)); |
| 1814 | |
Xin Long | 1033990 | 2019-04-15 17:15:06 +0800 | [diff] [blame] | 1815 | if (sk_under_memory_pressure(sk)) |
| 1816 | sk_mem_reclaim(sk); |
| 1817 | |
| 1818 | if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) { |
Neil Horman | 0aee4c2 | 2018-03-12 14:15:25 -0400 | [diff] [blame] | 1819 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 1820 | err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len); |
| 1821 | if (err) |
| 1822 | goto err; |
| 1823 | } |
| 1824 | |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1825 | if (sctp_state(asoc, CLOSED)) { |
| 1826 | err = sctp_primitive_ASSOCIATE(net, asoc, NULL); |
| 1827 | if (err) |
| 1828 | goto err; |
| 1829 | |
Xin Long | e55f4b8 | 2019-07-09 00:57:07 +0800 | [diff] [blame] | 1830 | if (asoc->ep->intl_enable) { |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1831 | timeo = sock_sndtimeo(sk, 0); |
| 1832 | err = sctp_wait_for_connect(asoc, &timeo); |
Xin Long | c863850 | 2018-10-17 03:06:12 +0800 | [diff] [blame] | 1833 | if (err) { |
| 1834 | err = -ESRCH; |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1835 | goto err; |
Xin Long | c863850 | 2018-10-17 03:06:12 +0800 | [diff] [blame] | 1836 | } |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1837 | } else { |
| 1838 | wait_connect = true; |
| 1839 | } |
| 1840 | |
| 1841 | pr_debug("%s: we associated primitively\n", __func__); |
| 1842 | } |
| 1843 | |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 1844 | datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter); |
| 1845 | if (IS_ERR(datamsg)) { |
| 1846 | err = PTR_ERR(datamsg); |
| 1847 | goto err; |
| 1848 | } |
| 1849 | |
| 1850 | asoc->force_delay = !!(msg->msg_flags & MSG_MORE); |
| 1851 | |
| 1852 | list_for_each_entry(chunk, &datamsg->chunks, frag_list) { |
| 1853 | sctp_chunk_hold(chunk); |
| 1854 | sctp_set_owner_w(chunk); |
| 1855 | chunk->transport = transport; |
| 1856 | } |
| 1857 | |
| 1858 | err = sctp_primitive_SEND(net, asoc, datamsg); |
| 1859 | if (err) { |
| 1860 | sctp_datamsg_free(datamsg); |
| 1861 | goto err; |
| 1862 | } |
| 1863 | |
| 1864 | pr_debug("%s: we sent primitively\n", __func__); |
| 1865 | |
| 1866 | sctp_datamsg_put(datamsg); |
| 1867 | |
| 1868 | if (unlikely(wait_connect)) { |
| 1869 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 1870 | sctp_wait_for_connect(asoc, &timeo); |
| 1871 | } |
| 1872 | |
| 1873 | err = msg_len; |
| 1874 | |
| 1875 | err: |
| 1876 | return err; |
| 1877 | } |
| 1878 | |
Xin Long | becef9b | 2018-03-01 23:05:13 +0800 | [diff] [blame] | 1879 | static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk, |
| 1880 | const struct msghdr *msg, |
| 1881 | struct sctp_cmsgs *cmsgs) |
| 1882 | { |
| 1883 | union sctp_addr *daddr = NULL; |
| 1884 | int err; |
| 1885 | |
| 1886 | if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { |
| 1887 | int len = msg->msg_namelen; |
| 1888 | |
| 1889 | if (len > sizeof(*daddr)) |
| 1890 | len = sizeof(*daddr); |
| 1891 | |
| 1892 | daddr = (union sctp_addr *)msg->msg_name; |
| 1893 | |
| 1894 | err = sctp_verify_addr(sk, daddr, len); |
| 1895 | if (err) |
| 1896 | return ERR_PTR(err); |
| 1897 | } |
| 1898 | |
| 1899 | return daddr; |
| 1900 | } |
| 1901 | |
Xin Long | d42cb06 | 2018-03-01 23:05:15 +0800 | [diff] [blame] | 1902 | static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc, |
| 1903 | struct sctp_sndrcvinfo *sinfo, |
| 1904 | struct sctp_cmsgs *cmsgs) |
| 1905 | { |
| 1906 | if (!cmsgs->srinfo && !cmsgs->sinfo) { |
| 1907 | sinfo->sinfo_stream = asoc->default_stream; |
| 1908 | sinfo->sinfo_ppid = asoc->default_ppid; |
| 1909 | sinfo->sinfo_context = asoc->default_context; |
| 1910 | sinfo->sinfo_assoc_id = sctp_assoc2id(asoc); |
Xin Long | ed63afb | 2018-03-05 20:44:18 +0800 | [diff] [blame] | 1911 | |
| 1912 | if (!cmsgs->prinfo) |
| 1913 | sinfo->sinfo_flags = asoc->default_flags; |
Xin Long | d42cb06 | 2018-03-01 23:05:15 +0800 | [diff] [blame] | 1914 | } |
| 1915 | |
Xin Long | ed63afb | 2018-03-05 20:44:18 +0800 | [diff] [blame] | 1916 | if (!cmsgs->srinfo && !cmsgs->prinfo) |
Xin Long | d42cb06 | 2018-03-01 23:05:15 +0800 | [diff] [blame] | 1917 | sinfo->sinfo_timetolive = asoc->default_timetolive; |
Xin Long | 3ff547c | 2018-03-14 19:05:31 +0800 | [diff] [blame] | 1918 | |
| 1919 | if (cmsgs->authinfo) { |
| 1920 | /* Reuse sinfo_tsn to indicate that authinfo was set and |
| 1921 | * sinfo_ssn to save the keyid on tx path. |
| 1922 | */ |
| 1923 | sinfo->sinfo_tsn = 1; |
| 1924 | sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber; |
| 1925 | } |
Xin Long | d42cb06 | 2018-03-01 23:05:15 +0800 | [diff] [blame] | 1926 | } |
| 1927 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1928 | 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] | 1929 | { |
Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 1930 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Xin Long | 8e87c6e | 2018-03-01 23:05:16 +0800 | [diff] [blame] | 1931 | struct sctp_transport *transport = NULL; |
Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 1932 | struct sctp_sndrcvinfo _sinfo, *sinfo; |
Greg Kroah-Hartman | ba59fb0 | 2019-02-01 15:15:22 +0100 | [diff] [blame] | 1933 | struct sctp_association *asoc, *tmp; |
Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 1934 | struct sctp_cmsgs cmsgs; |
Xin Long | becef9b | 2018-03-01 23:05:13 +0800 | [diff] [blame] | 1935 | union sctp_addr *daddr; |
Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 1936 | bool new = false; |
| 1937 | __u16 sflags; |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 1938 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | |
Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 1940 | /* Parse and get snd_info */ |
| 1941 | err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len); |
| 1942 | if (err) |
Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 1943 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | |
Xin Long | 204f817 | 2018-03-01 23:05:14 +0800 | [diff] [blame] | 1945 | sinfo = &_sinfo; |
Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 1946 | sflags = sinfo->sinfo_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | |
Xin Long | becef9b | 2018-03-01 23:05:13 +0800 | [diff] [blame] | 1948 | /* Get daddr from msg */ |
| 1949 | daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs); |
| 1950 | if (IS_ERR(daddr)) { |
| 1951 | err = PTR_ERR(daddr); |
Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 1952 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 1955 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | |
Xin Long | 4910280 | 2018-03-05 20:44:20 +0800 | [diff] [blame] | 1957 | /* SCTP_SENDALL process */ |
| 1958 | if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) { |
Greg Kroah-Hartman | ba59fb0 | 2019-02-01 15:15:22 +0100 | [diff] [blame] | 1959 | list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) { |
Xin Long | 4910280 | 2018-03-05 20:44:20 +0800 | [diff] [blame] | 1960 | err = sctp_sendmsg_check_sflags(asoc, sflags, msg, |
| 1961 | msg_len); |
| 1962 | if (err == 0) |
| 1963 | continue; |
| 1964 | if (err < 0) |
| 1965 | goto out_unlock; |
| 1966 | |
| 1967 | sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs); |
| 1968 | |
| 1969 | err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, |
| 1970 | NULL, sinfo); |
| 1971 | if (err < 0) |
| 1972 | goto out_unlock; |
| 1973 | |
| 1974 | iov_iter_revert(&msg->msg_iter, err); |
| 1975 | } |
| 1976 | |
| 1977 | goto out_unlock; |
| 1978 | } |
| 1979 | |
Xin Long | 0a3920d | 2018-03-01 23:05:18 +0800 | [diff] [blame] | 1980 | /* Get and check or create asoc */ |
Xin Long | becef9b | 2018-03-01 23:05:13 +0800 | [diff] [blame] | 1981 | if (daddr) { |
Xin Long | becef9b | 2018-03-01 23:05:13 +0800 | [diff] [blame] | 1982 | asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport); |
Xin Long | 0a3920d | 2018-03-01 23:05:18 +0800 | [diff] [blame] | 1983 | if (asoc) { |
| 1984 | err = sctp_sendmsg_check_sflags(asoc, sflags, msg, |
| 1985 | msg_len); |
| 1986 | if (err <= 0) |
| 1987 | goto out_unlock; |
| 1988 | } else { |
| 1989 | err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr, |
| 1990 | &transport); |
| 1991 | if (err) |
| 1992 | goto out_unlock; |
| 1993 | |
| 1994 | asoc = transport->asoc; |
| 1995 | new = true; |
| 1996 | } |
| 1997 | |
| 1998 | if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER)) |
| 1999 | transport = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | } else { |
Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2001 | asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | if (!asoc) { |
| 2003 | err = -EPIPE; |
| 2004 | goto out_unlock; |
| 2005 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | |
Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2007 | err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len); |
Xin Long | c2666de | 2018-03-01 23:05:12 +0800 | [diff] [blame] | 2008 | if (err <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | } |
| 2011 | |
Xin Long | d42cb06 | 2018-03-01 23:05:15 +0800 | [diff] [blame] | 2012 | /* Update snd_info with the asoc */ |
| 2013 | sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 2015 | /* Send msg to the asoc */ |
Xin Long | 8e87c6e | 2018-03-01 23:05:16 +0800 | [diff] [blame] | 2016 | err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo); |
Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2017 | if (err < 0 && err != -ESRCH && new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | sctp_association_free(asoc); |
Xin Long | 8e87c6e | 2018-03-01 23:05:16 +0800 | [diff] [blame] | 2019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | out_unlock: |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 2021 | release_sock(sk); |
Xin Long | 007b7e1 | 2018-03-01 23:05:17 +0800 | [diff] [blame] | 2022 | out: |
Xin Long | f84af33 | 2018-03-01 23:05:10 +0800 | [diff] [blame] | 2023 | return sctp_error(sk, msg->msg_flags, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | /* This is an extended version of skb_pull() that removes the data from the |
| 2027 | * start of a skb even when data is spread across the list of skb's in the |
| 2028 | * frag_list. len specifies the total amount of data that needs to be removed. |
| 2029 | * when 'len' bytes could be removed from the skb, it returns 0. |
| 2030 | * If 'len' exceeds the total skb length, it returns the no. of bytes that |
| 2031 | * could not be removed. |
| 2032 | */ |
| 2033 | static int sctp_skb_pull(struct sk_buff *skb, int len) |
| 2034 | { |
| 2035 | struct sk_buff *list; |
| 2036 | int skb_len = skb_headlen(skb); |
| 2037 | int rlen; |
| 2038 | |
| 2039 | if (len <= skb_len) { |
| 2040 | __skb_pull(skb, len); |
| 2041 | return 0; |
| 2042 | } |
| 2043 | len -= skb_len; |
| 2044 | __skb_pull(skb, skb_len); |
| 2045 | |
David S. Miller | 1b003be | 2009-06-09 00:22:35 -0700 | [diff] [blame] | 2046 | skb_walk_frags(skb, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | rlen = sctp_skb_pull(list, len); |
| 2048 | skb->len -= (len-rlen); |
| 2049 | skb->data_len -= (len-rlen); |
| 2050 | |
| 2051 | if (!rlen) |
| 2052 | return 0; |
| 2053 | |
| 2054 | len = rlen; |
| 2055 | } |
| 2056 | |
| 2057 | return len; |
| 2058 | } |
| 2059 | |
| 2060 | /* API 3.1.3 recvmsg() - UDP Style Syntax |
| 2061 | * |
| 2062 | * ssize_t recvmsg(int socket, struct msghdr *message, |
| 2063 | * int flags); |
| 2064 | * |
| 2065 | * socket - the socket descriptor of the endpoint. |
| 2066 | * message - pointer to the msghdr structure which contains a single |
| 2067 | * user message and possibly some ancillary data. |
| 2068 | * |
| 2069 | * See Section 5 for complete description of the data |
| 2070 | * structures. |
| 2071 | * |
| 2072 | * flags - flags sent or received with the user message, see Section |
| 2073 | * 5 for complete description of the flags. |
| 2074 | */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 2075 | static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 2076 | int noblock, int flags, int *addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | { |
| 2078 | struct sctp_ulpevent *event = NULL; |
| 2079 | struct sctp_sock *sp = sctp_sk(sk); |
Marcelo Ricardo Leitner | 1f45f78 | 2016-07-13 15:08:57 -0300 | [diff] [blame] | 2080 | struct sk_buff *skb, *head_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | int copied; |
| 2082 | int err = 0; |
| 2083 | int skb_len; |
| 2084 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 2085 | pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, " |
| 2086 | "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags, |
| 2087 | addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 2089 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | |
Marcelo Ricardo Leitner | e5b13f3 | 2016-07-15 16:38:19 -0300 | [diff] [blame] | 2091 | if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) && |
Xin Long | e087869 | 2016-07-30 14:14:41 +0800 | [diff] [blame] | 2092 | !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | err = -ENOTCONN; |
| 2094 | goto out; |
| 2095 | } |
| 2096 | |
| 2097 | skb = sctp_skb_recv_datagram(sk, flags, noblock, &err); |
| 2098 | if (!skb) |
| 2099 | goto out; |
| 2100 | |
| 2101 | /* Get the total length of the skb including any skb's in the |
| 2102 | * frag_list. |
| 2103 | */ |
| 2104 | skb_len = skb->len; |
| 2105 | |
| 2106 | copied = skb_len; |
| 2107 | if (copied > len) |
| 2108 | copied = len; |
| 2109 | |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 2110 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | |
| 2112 | event = sctp_skb2event(skb); |
| 2113 | |
| 2114 | if (err) |
| 2115 | goto out_free; |
| 2116 | |
Marcelo Ricardo Leitner | 1f45f78 | 2016-07-13 15:08:57 -0300 | [diff] [blame] | 2117 | if (event->chunk && event->chunk->head_skb) |
| 2118 | head_skb = event->chunk->head_skb; |
| 2119 | else |
| 2120 | head_skb = skb; |
| 2121 | sock_recv_ts_and_drops(msg, sk, head_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | if (sctp_ulpevent_is_notification(event)) { |
| 2123 | msg->msg_flags |= MSG_NOTIFICATION; |
| 2124 | sp->pf->event_msgname(event, msg->msg_name, addr_len); |
| 2125 | } else { |
Marcelo Ricardo Leitner | 1f45f78 | 2016-07-13 15:08:57 -0300 | [diff] [blame] | 2126 | sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 2129 | /* Check if we allow SCTP_NXTINFO. */ |
| 2130 | if (sp->recvnxtinfo) |
| 2131 | sctp_ulpevent_read_nxtinfo(event, msg, sk); |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 2132 | /* Check if we allow SCTP_RCVINFO. */ |
| 2133 | if (sp->recvrcvinfo) |
| 2134 | sctp_ulpevent_read_rcvinfo(event, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | /* Check if we allow SCTP_SNDRCVINFO. */ |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 2136 | if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | sctp_ulpevent_read_sndrcvinfo(event, msg); |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 2138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | err = copied; |
| 2140 | |
| 2141 | /* If skb's length exceeds the user's buffer, update the skb and |
| 2142 | * push it back to the receive_queue so that the next call to |
| 2143 | * recvmsg() will return the remaining data. Don't set MSG_EOR. |
| 2144 | */ |
| 2145 | if (skb_len > copied) { |
| 2146 | msg->msg_flags &= ~MSG_EOR; |
| 2147 | if (flags & MSG_PEEK) |
| 2148 | goto out_free; |
| 2149 | sctp_skb_pull(skb, copied); |
| 2150 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 2151 | |
Daniel Borkmann | 362d520 | 2014-04-14 21:45:17 +0200 | [diff] [blame] | 2152 | /* When only partial message is copied to the user, increase |
| 2153 | * rwnd by that amount. If all the data in the skb is read, |
| 2154 | * rwnd is updated when the event is freed. |
| 2155 | */ |
| 2156 | if (!sctp_ulpevent_is_notification(event)) |
| 2157 | sctp_assoc_rwnd_increase(event->asoc, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | goto out; |
| 2159 | } else if ((event->msg_flags & MSG_NOTIFICATION) || |
| 2160 | (event->msg_flags & MSG_EOR)) |
| 2161 | msg->msg_flags |= MSG_EOR; |
| 2162 | else |
| 2163 | msg->msg_flags &= ~MSG_EOR; |
| 2164 | |
| 2165 | out_free: |
| 2166 | if (flags & MSG_PEEK) { |
| 2167 | /* Release the skb reference acquired after peeking the skb in |
| 2168 | * sctp_skb_recv_datagram(). |
| 2169 | */ |
| 2170 | kfree_skb(skb); |
| 2171 | } else { |
| 2172 | /* Free the event which includes releasing the reference to |
| 2173 | * the owner of the skb, freeing the skb and updating the |
| 2174 | * rwnd. |
| 2175 | */ |
| 2176 | sctp_ulpevent_free(event); |
| 2177 | } |
| 2178 | out: |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 2179 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | return err; |
| 2181 | } |
| 2182 | |
| 2183 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) |
| 2184 | * |
| 2185 | * This option is a on/off flag. If enabled no SCTP message |
| 2186 | * fragmentation will be performed. Instead if a message being sent |
| 2187 | * exceeds the current PMTU size, the message will NOT be sent and |
| 2188 | * instead a error will be indicated to the user. |
| 2189 | */ |
| 2190 | static int sctp_setsockopt_disable_fragments(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2191 | char __user *optval, |
| 2192 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | { |
| 2194 | int val; |
| 2195 | |
| 2196 | if (optlen < sizeof(int)) |
| 2197 | return -EINVAL; |
| 2198 | |
| 2199 | if (get_user(val, (int __user *)optval)) |
| 2200 | return -EFAULT; |
| 2201 | |
| 2202 | sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1; |
| 2203 | |
| 2204 | return 0; |
| 2205 | } |
| 2206 | |
| 2207 | static int sctp_setsockopt_events(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2208 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | { |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 2210 | struct sctp_event_subscribe subscribe; |
| 2211 | __u8 *sn_type = (__u8 *)&subscribe; |
| 2212 | struct sctp_sock *sp = sctp_sk(sk); |
Xin Long | a1e3a05 | 2018-11-18 16:08:52 +0800 | [diff] [blame] | 2213 | struct sctp_association *asoc; |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 2214 | int i; |
Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2215 | |
Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 2216 | if (optlen > sizeof(struct sctp_event_subscribe)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | return -EINVAL; |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 2218 | |
| 2219 | if (copy_from_user(&subscribe, optval, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | return -EFAULT; |
Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2221 | |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 2222 | for (i = 0; i < optlen; i++) |
| 2223 | sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i, |
| 2224 | sn_type[i]); |
| 2225 | |
Xin Long | a1e3a05 | 2018-11-18 16:08:52 +0800 | [diff] [blame] | 2226 | list_for_each_entry(asoc, &sp->ep->asocs, asocs) |
| 2227 | asoc->subscribe = sctp_sk(sk)->subscribe; |
| 2228 | |
Daniel Borkmann | bbbea41 | 2014-07-12 20:30:40 +0200 | [diff] [blame] | 2229 | /* 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] | 2230 | * if there is no data to be sent or retransmit, the stack will |
| 2231 | * immediately send up this notification. |
| 2232 | */ |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 2233 | if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) { |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 2234 | struct sctp_ulpevent *event; |
Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2235 | |
Xin Long | a1e3a05 | 2018-11-18 16:08:52 +0800 | [diff] [blame] | 2236 | asoc = sctp_id2assoc(sk, 0); |
Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2237 | if (asoc && sctp_outq_is_empty(&asoc->outqueue)) { |
| 2238 | event = sctp_ulpevent_make_sender_dry_event(asoc, |
Marcelo Ricardo Leitner | 2e83acb | 2018-01-08 19:02:27 -0200 | [diff] [blame] | 2239 | GFP_USER | __GFP_NOWARN); |
Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2240 | if (!event) |
| 2241 | return -ENOMEM; |
| 2242 | |
Xin Long | 9162e0e | 2017-12-08 21:04:05 +0800 | [diff] [blame] | 2243 | asoc->stream.si->enqueue_event(&asoc->ulpq, event); |
Wei Yongjun | 9491230 | 2011-07-02 09:28:04 +0000 | [diff] [blame] | 2244 | } |
| 2245 | } |
| 2246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | return 0; |
| 2248 | } |
| 2249 | |
| 2250 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) |
| 2251 | * |
| 2252 | * This socket option is applicable to the UDP-style socket only. When |
| 2253 | * set it will cause associations that are idle for more than the |
| 2254 | * specified number of seconds to automatically close. An association |
| 2255 | * being idle is defined an association that has NOT sent or received |
| 2256 | * user data. The special value of '0' indicates that no automatic |
| 2257 | * close of any associations should be performed. The option expects an |
| 2258 | * integer defining the number of seconds of idle time before an |
| 2259 | * association is closed. |
| 2260 | */ |
| 2261 | static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2262 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | { |
| 2264 | struct sctp_sock *sp = sctp_sk(sk); |
Neil Horman | 9f70f46 | 2013-12-10 06:48:15 -0500 | [diff] [blame] | 2265 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | |
| 2267 | /* Applicable to UDP-style socket only */ |
| 2268 | if (sctp_style(sk, TCP)) |
| 2269 | return -EOPNOTSUPP; |
| 2270 | if (optlen != sizeof(int)) |
| 2271 | return -EINVAL; |
| 2272 | if (copy_from_user(&sp->autoclose, optval, optlen)) |
| 2273 | return -EFAULT; |
| 2274 | |
Neil Horman | 9f70f46 | 2013-12-10 06:48:15 -0500 | [diff] [blame] | 2275 | if (sp->autoclose > net->sctp.max_autoclose) |
| 2276 | sp->autoclose = net->sctp.max_autoclose; |
| 2277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | return 0; |
| 2279 | } |
| 2280 | |
| 2281 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) |
| 2282 | * |
| 2283 | * Applications can enable or disable heartbeats for any peer address of |
| 2284 | * an association, modify an address's heartbeat interval, force a |
| 2285 | * heartbeat to be sent immediately, and adjust the address's maximum |
| 2286 | * number of retransmissions sent before an address is considered |
| 2287 | * unreachable. The following structure is used to access and modify an |
| 2288 | * address's parameters: |
| 2289 | * |
| 2290 | * struct sctp_paddrparams { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2291 | * sctp_assoc_t spp_assoc_id; |
| 2292 | * struct sockaddr_storage spp_address; |
| 2293 | * uint32_t spp_hbinterval; |
| 2294 | * uint16_t spp_pathmaxrxt; |
| 2295 | * uint32_t spp_pathmtu; |
| 2296 | * uint32_t spp_sackdelay; |
| 2297 | * uint32_t spp_flags; |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2298 | * uint32_t spp_ipv6_flowlabel; |
| 2299 | * uint8_t spp_dscp; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2300 | * }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | * |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2302 | * spp_assoc_id - (one-to-many style socket) This is filled in the |
| 2303 | * application, and identifies the association for |
| 2304 | * this query. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | * spp_address - This specifies which address is of interest. |
| 2306 | * spp_hbinterval - This contains the value of the heartbeat interval, |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2307 | * in milliseconds. If a value of zero |
| 2308 | * is present in this field then no changes are to |
| 2309 | * be made to this parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | * spp_pathmaxrxt - This contains the maximum number of |
| 2311 | * retransmissions before this address shall be |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2312 | * considered unreachable. If a value of zero |
| 2313 | * is present in this field then no changes are to |
| 2314 | * be made to this parameter. |
| 2315 | * spp_pathmtu - When Path MTU discovery is disabled the value |
| 2316 | * specified here will be the "fixed" path mtu. |
| 2317 | * Note that if the spp_address field is empty |
| 2318 | * then all associations on this address will |
| 2319 | * have this fixed path mtu set upon them. |
| 2320 | * |
| 2321 | * spp_sackdelay - When delayed sack is enabled, this value specifies |
| 2322 | * the number of milliseconds that sacks will be delayed |
| 2323 | * for. This value will apply to all addresses of an |
| 2324 | * association if the spp_address field is empty. Note |
| 2325 | * also, that if delayed sack is enabled and this |
| 2326 | * value is set to 0, no change is made to the last |
| 2327 | * recorded delayed sack timer value. |
| 2328 | * |
| 2329 | * spp_flags - These flags are used to control various features |
| 2330 | * on an association. The flag field may contain |
| 2331 | * zero or more of the following options. |
| 2332 | * |
| 2333 | * SPP_HB_ENABLE - Enable heartbeats on the |
| 2334 | * specified address. Note that if the address |
| 2335 | * field is empty all addresses for the association |
| 2336 | * have heartbeats enabled upon them. |
| 2337 | * |
| 2338 | * SPP_HB_DISABLE - Disable heartbeats on the |
| 2339 | * speicifed address. Note that if the address |
| 2340 | * field is empty all addresses for the association |
| 2341 | * will have their heartbeats disabled. Note also |
| 2342 | * that SPP_HB_ENABLE and SPP_HB_DISABLE are |
| 2343 | * mutually exclusive, only one of these two should |
| 2344 | * be specified. Enabling both fields will have |
| 2345 | * undetermined results. |
| 2346 | * |
| 2347 | * SPP_HB_DEMAND - Request a user initiated heartbeat |
| 2348 | * to be made immediately. |
| 2349 | * |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2350 | * SPP_HB_TIME_IS_ZERO - Specify's that the time for |
| 2351 | * heartbeat delayis to be set to the value of 0 |
| 2352 | * milliseconds. |
| 2353 | * |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2354 | * SPP_PMTUD_ENABLE - This field will enable PMTU |
| 2355 | * discovery upon the specified address. Note that |
| 2356 | * if the address feild is empty then all addresses |
| 2357 | * on the association are effected. |
| 2358 | * |
| 2359 | * SPP_PMTUD_DISABLE - This field will disable PMTU |
| 2360 | * discovery upon the specified address. Note that |
| 2361 | * if the address feild is empty then all addresses |
| 2362 | * on the association are effected. Not also that |
| 2363 | * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually |
| 2364 | * exclusive. Enabling both will have undetermined |
| 2365 | * results. |
| 2366 | * |
| 2367 | * SPP_SACKDELAY_ENABLE - Setting this flag turns |
| 2368 | * on delayed sack. The time specified in spp_sackdelay |
| 2369 | * is used to specify the sack delay for this address. Note |
| 2370 | * that if spp_address is empty then all addresses will |
| 2371 | * enable delayed sack and take on the sack delay |
| 2372 | * value specified in spp_sackdelay. |
| 2373 | * SPP_SACKDELAY_DISABLE - Setting this flag turns |
| 2374 | * off delayed sack. If the spp_address field is blank then |
| 2375 | * delayed sack is disabled for the entire association. Note |
| 2376 | * also that this field is mutually exclusive to |
| 2377 | * SPP_SACKDELAY_ENABLE, setting both will have undefined |
| 2378 | * results. |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2379 | * |
| 2380 | * SPP_IPV6_FLOWLABEL: Setting this flag enables the |
| 2381 | * setting of the IPV6 flow label value. The value is |
| 2382 | * contained in the spp_ipv6_flowlabel field. |
| 2383 | * Upon retrieval, this flag will be set to indicate that |
| 2384 | * the spp_ipv6_flowlabel field has a valid value returned. |
| 2385 | * If a specific destination address is set (in the |
| 2386 | * spp_address field), then the value returned is that of |
| 2387 | * the address. If just an association is specified (and |
| 2388 | * no address), then the association's default flow label |
| 2389 | * is returned. If neither an association nor a destination |
| 2390 | * is specified, then the socket's default flow label is |
| 2391 | * returned. For non-IPv6 sockets, this flag will be left |
| 2392 | * cleared. |
| 2393 | * |
| 2394 | * SPP_DSCP: Setting this flag enables the setting of the |
| 2395 | * Differentiated Services Code Point (DSCP) value |
| 2396 | * associated with either the association or a specific |
| 2397 | * address. The value is obtained in the spp_dscp field. |
| 2398 | * Upon retrieval, this flag will be set to indicate that |
| 2399 | * the spp_dscp field has a valid value returned. If a |
| 2400 | * specific destination address is set when called (in the |
| 2401 | * spp_address field), then that specific destination |
| 2402 | * address's DSCP value is returned. If just an association |
| 2403 | * is specified, then the association's default DSCP is |
| 2404 | * returned. If neither an association nor a destination is |
| 2405 | * specified, then the socket's default DSCP is returned. |
| 2406 | * |
| 2407 | * spp_ipv6_flowlabel |
| 2408 | * - This field is used in conjunction with the |
| 2409 | * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label. |
| 2410 | * The 20 least significant bits are used for the flow |
| 2411 | * label. This setting has precedence over any IPv6-layer |
| 2412 | * setting. |
| 2413 | * |
| 2414 | * spp_dscp - This field is used in conjunction with the SPP_DSCP flag |
| 2415 | * and contains the DSCP. The 6 most significant bits are |
| 2416 | * used for the DSCP. This setting has precedence over any |
| 2417 | * IPv4- or IPv6- layer setting. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | */ |
Adrian Bunk | 1616436 | 2006-09-18 00:40:38 -0700 | [diff] [blame] | 2419 | static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params, |
| 2420 | struct sctp_transport *trans, |
| 2421 | struct sctp_association *asoc, |
| 2422 | struct sctp_sock *sp, |
| 2423 | int hb_change, |
| 2424 | int pmtud_change, |
| 2425 | int sackdelay_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | int error; |
| 2428 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2429 | if (params->spp_flags & SPP_HB_DEMAND && trans) { |
Xin Long | 4e7696d | 2019-12-09 13:45:18 +0800 | [diff] [blame] | 2430 | error = sctp_primitive_REQUESTHEARTBEAT(trans->asoc->base.net, |
| 2431 | trans->asoc, trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | if (error) |
| 2433 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | } |
| 2435 | |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2436 | /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of |
| 2437 | * this field is ignored. Note also that a value of zero indicates |
| 2438 | * the current setting should be left unchanged. |
| 2439 | */ |
| 2440 | if (params->spp_flags & SPP_HB_ENABLE) { |
| 2441 | |
| 2442 | /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is |
| 2443 | * set. This lets us use 0 value when this flag |
| 2444 | * is set. |
| 2445 | */ |
| 2446 | if (params->spp_flags & SPP_HB_TIME_IS_ZERO) |
| 2447 | params->spp_hbinterval = 0; |
| 2448 | |
| 2449 | if (params->spp_hbinterval || |
| 2450 | (params->spp_flags & SPP_HB_TIME_IS_ZERO)) { |
| 2451 | if (trans) { |
| 2452 | trans->hbinterval = |
| 2453 | msecs_to_jiffies(params->spp_hbinterval); |
| 2454 | } else if (asoc) { |
| 2455 | asoc->hbinterval = |
| 2456 | msecs_to_jiffies(params->spp_hbinterval); |
| 2457 | } else { |
| 2458 | sp->hbinterval = params->spp_hbinterval; |
| 2459 | } |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | if (hb_change) { |
| 2464 | if (trans) { |
| 2465 | trans->param_flags = |
| 2466 | (trans->param_flags & ~SPP_HB) | hb_change; |
| 2467 | } else if (asoc) { |
| 2468 | asoc->param_flags = |
| 2469 | (asoc->param_flags & ~SPP_HB) | hb_change; |
| 2470 | } else { |
| 2471 | sp->param_flags = |
| 2472 | (sp->param_flags & ~SPP_HB) | hb_change; |
| 2473 | } |
| 2474 | } |
| 2475 | |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2476 | /* When Path MTU discovery is disabled the value specified here will |
| 2477 | * be the "fixed" path mtu (i.e. the value of the spp_flags field must |
| 2478 | * include the flag SPP_PMTUD_DISABLE for this field to have any |
| 2479 | * effect). |
| 2480 | */ |
| 2481 | if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2482 | if (trans) { |
| 2483 | trans->pathmtu = params->spp_pathmtu; |
Xin Long | 3ebfdf0 | 2017-04-04 13:39:55 +0800 | [diff] [blame] | 2484 | sctp_assoc_sync_pmtu(asoc); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2485 | } else if (asoc) { |
Marcelo Ricardo Leitner | c4b2893 | 2018-04-26 16:58:53 -0300 | [diff] [blame] | 2486 | sctp_assoc_set_pmtu(asoc, params->spp_pathmtu); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2487 | } else { |
| 2488 | sp->pathmtu = params->spp_pathmtu; |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | if (pmtud_change) { |
| 2493 | if (trans) { |
| 2494 | int update = (trans->param_flags & SPP_PMTUD_DISABLE) && |
| 2495 | (params->spp_flags & SPP_PMTUD_ENABLE); |
| 2496 | trans->param_flags = |
| 2497 | (trans->param_flags & ~SPP_PMTUD) | pmtud_change; |
| 2498 | if (update) { |
Vlad Yasevich | 9914ae3 | 2011-04-26 21:51:31 +0000 | [diff] [blame] | 2499 | sctp_transport_pmtu(trans, sctp_opt2sk(sp)); |
Xin Long | 3ebfdf0 | 2017-04-04 13:39:55 +0800 | [diff] [blame] | 2500 | sctp_assoc_sync_pmtu(asoc); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2501 | } |
| 2502 | } else if (asoc) { |
| 2503 | asoc->param_flags = |
| 2504 | (asoc->param_flags & ~SPP_PMTUD) | pmtud_change; |
| 2505 | } else { |
| 2506 | sp->param_flags = |
| 2507 | (sp->param_flags & ~SPP_PMTUD) | pmtud_change; |
| 2508 | } |
| 2509 | } |
| 2510 | |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2511 | /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the |
| 2512 | * value of this field is ignored. Note also that a value of zero |
| 2513 | * indicates the current setting should be left unchanged. |
| 2514 | */ |
| 2515 | if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2516 | if (trans) { |
| 2517 | trans->sackdelay = |
| 2518 | msecs_to_jiffies(params->spp_sackdelay); |
| 2519 | } else if (asoc) { |
| 2520 | asoc->sackdelay = |
| 2521 | msecs_to_jiffies(params->spp_sackdelay); |
| 2522 | } else { |
| 2523 | sp->sackdelay = params->spp_sackdelay; |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | if (sackdelay_change) { |
| 2528 | if (trans) { |
| 2529 | trans->param_flags = |
| 2530 | (trans->param_flags & ~SPP_SACKDELAY) | |
| 2531 | sackdelay_change; |
| 2532 | } else if (asoc) { |
| 2533 | asoc->param_flags = |
| 2534 | (asoc->param_flags & ~SPP_SACKDELAY) | |
| 2535 | sackdelay_change; |
| 2536 | } else { |
| 2537 | sp->param_flags = |
| 2538 | (sp->param_flags & ~SPP_SACKDELAY) | |
| 2539 | sackdelay_change; |
| 2540 | } |
| 2541 | } |
| 2542 | |
Andrei Pelinescu-Onciul | 37051f7 | 2009-11-23 15:53:57 -0500 | [diff] [blame] | 2543 | /* Note that a value of zero indicates the current setting should be |
| 2544 | left unchanged. |
Vlad Yasevich | bdf3092 | 2007-03-23 11:33:12 -0700 | [diff] [blame] | 2545 | */ |
Andrei Pelinescu-Onciul | 37051f7 | 2009-11-23 15:53:57 -0500 | [diff] [blame] | 2546 | if (params->spp_pathmaxrxt) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2547 | if (trans) { |
| 2548 | trans->pathmaxrxt = params->spp_pathmaxrxt; |
| 2549 | } else if (asoc) { |
| 2550 | asoc->pathmaxrxt = params->spp_pathmaxrxt; |
| 2551 | } else { |
| 2552 | sp->pathmaxrxt = params->spp_pathmaxrxt; |
| 2553 | } |
| 2554 | } |
| 2555 | |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2556 | if (params->spp_flags & SPP_IPV6_FLOWLABEL) { |
Xin Long | 741880e | 2018-09-03 15:47:11 +0800 | [diff] [blame] | 2557 | if (trans) { |
| 2558 | if (trans->ipaddr.sa.sa_family == AF_INET6) { |
| 2559 | trans->flowlabel = params->spp_ipv6_flowlabel & |
| 2560 | SCTP_FLOWLABEL_VAL_MASK; |
| 2561 | trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK; |
| 2562 | } |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2563 | } else if (asoc) { |
Xin Long | af8a2b8 | 2018-09-03 15:47:10 +0800 | [diff] [blame] | 2564 | struct sctp_transport *t; |
| 2565 | |
| 2566 | list_for_each_entry(t, &asoc->peer.transport_addr_list, |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2567 | transports) { |
Xin Long | af8a2b8 | 2018-09-03 15:47:10 +0800 | [diff] [blame] | 2568 | if (t->ipaddr.sa.sa_family != AF_INET6) |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2569 | continue; |
Xin Long | af8a2b8 | 2018-09-03 15:47:10 +0800 | [diff] [blame] | 2570 | t->flowlabel = params->spp_ipv6_flowlabel & |
| 2571 | SCTP_FLOWLABEL_VAL_MASK; |
| 2572 | t->flowlabel |= SCTP_FLOWLABEL_SET_MASK; |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2573 | } |
| 2574 | asoc->flowlabel = params->spp_ipv6_flowlabel & |
| 2575 | SCTP_FLOWLABEL_VAL_MASK; |
| 2576 | asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK; |
| 2577 | } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) { |
| 2578 | sp->flowlabel = params->spp_ipv6_flowlabel & |
| 2579 | SCTP_FLOWLABEL_VAL_MASK; |
| 2580 | sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK; |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | if (params->spp_flags & SPP_DSCP) { |
| 2585 | if (trans) { |
| 2586 | trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK; |
| 2587 | trans->dscp |= SCTP_DSCP_SET_MASK; |
| 2588 | } else if (asoc) { |
Xin Long | af8a2b8 | 2018-09-03 15:47:10 +0800 | [diff] [blame] | 2589 | struct sctp_transport *t; |
| 2590 | |
| 2591 | list_for_each_entry(t, &asoc->peer.transport_addr_list, |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2592 | transports) { |
Xin Long | af8a2b8 | 2018-09-03 15:47:10 +0800 | [diff] [blame] | 2593 | t->dscp = params->spp_dscp & |
| 2594 | SCTP_DSCP_VAL_MASK; |
| 2595 | t->dscp |= SCTP_DSCP_SET_MASK; |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2596 | } |
| 2597 | asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK; |
| 2598 | asoc->dscp |= SCTP_DSCP_SET_MASK; |
| 2599 | } else { |
| 2600 | sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK; |
| 2601 | sp->dscp |= SCTP_DSCP_SET_MASK; |
| 2602 | } |
| 2603 | } |
| 2604 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2605 | return 0; |
| 2606 | } |
| 2607 | |
| 2608 | static int sctp_setsockopt_peer_addr_params(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2609 | char __user *optval, |
| 2610 | unsigned int optlen) |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2611 | { |
| 2612 | struct sctp_paddrparams params; |
| 2613 | struct sctp_transport *trans = NULL; |
| 2614 | struct sctp_association *asoc = NULL; |
| 2615 | struct sctp_sock *sp = sctp_sk(sk); |
| 2616 | int error; |
| 2617 | int hb_change, pmtud_change, sackdelay_change; |
| 2618 | |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2619 | if (optlen == sizeof(params)) { |
| 2620 | if (copy_from_user(¶ms, optval, optlen)) |
| 2621 | return -EFAULT; |
| 2622 | } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams, |
| 2623 | spp_ipv6_flowlabel), 4)) { |
| 2624 | if (copy_from_user(¶ms, optval, optlen)) |
| 2625 | return -EFAULT; |
| 2626 | if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL)) |
| 2627 | return -EINVAL; |
| 2628 | } else { |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 2629 | return -EINVAL; |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 2630 | } |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2631 | |
| 2632 | /* Validate flags and value parameters. */ |
| 2633 | hb_change = params.spp_flags & SPP_HB; |
| 2634 | pmtud_change = params.spp_flags & SPP_PMTUD; |
| 2635 | sackdelay_change = params.spp_flags & SPP_SACKDELAY; |
| 2636 | |
| 2637 | if (hb_change == SPP_HB || |
| 2638 | pmtud_change == SPP_PMTUD || |
| 2639 | sackdelay_change == SPP_SACKDELAY || |
| 2640 | params.spp_sackdelay > 500 || |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 2641 | (params.spp_pathmtu && |
| 2642 | params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT)) |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2643 | return -EINVAL; |
| 2644 | |
| 2645 | /* If an address other than INADDR_ANY is specified, and |
| 2646 | * no transport is found, then the request is invalid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | */ |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 2648 | if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2649 | trans = sctp_addr_id2transport(sk, ¶ms.spp_address, |
| 2650 | params.spp_assoc_id); |
| 2651 | if (!trans) |
| 2652 | return -EINVAL; |
| 2653 | } |
| 2654 | |
Xin Long | b99e5e0 | 2019-01-28 15:08:24 +0800 | [diff] [blame] | 2655 | /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the |
| 2656 | * socket is a one to many style socket, and an association |
| 2657 | * was not found, then the id was invalid. |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2658 | */ |
| 2659 | asoc = sctp_id2assoc(sk, params.spp_assoc_id); |
Xin Long | b99e5e0 | 2019-01-28 15:08:24 +0800 | [diff] [blame] | 2660 | if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC && |
| 2661 | sctp_style(sk, UDP)) |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2662 | return -EINVAL; |
| 2663 | |
| 2664 | /* Heartbeat demand can only be sent on a transport or |
| 2665 | * association, but not a socket. |
| 2666 | */ |
| 2667 | if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc) |
| 2668 | return -EINVAL; |
| 2669 | |
| 2670 | /* Process parameters. */ |
| 2671 | error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, |
| 2672 | hb_change, pmtud_change, |
| 2673 | sackdelay_change); |
| 2674 | |
| 2675 | if (error) |
| 2676 | return error; |
| 2677 | |
| 2678 | /* If changes are for association, also apply parameters to each |
| 2679 | * transport. |
| 2680 | */ |
| 2681 | if (!trans && asoc) { |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 2682 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, |
| 2683 | transports) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 2684 | sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, |
| 2685 | hb_change, pmtud_change, |
| 2686 | sackdelay_change); |
| 2687 | } |
| 2688 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | |
| 2690 | return 0; |
| 2691 | } |
| 2692 | |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2693 | static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags) |
| 2694 | { |
| 2695 | return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE; |
| 2696 | } |
| 2697 | |
| 2698 | static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags) |
| 2699 | { |
| 2700 | return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE; |
| 2701 | } |
| 2702 | |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 2703 | static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params, |
| 2704 | struct sctp_association *asoc) |
| 2705 | { |
| 2706 | struct sctp_transport *trans; |
| 2707 | |
| 2708 | if (params->sack_delay) { |
| 2709 | asoc->sackdelay = msecs_to_jiffies(params->sack_delay); |
| 2710 | asoc->param_flags = |
| 2711 | sctp_spp_sackdelay_enable(asoc->param_flags); |
| 2712 | } |
| 2713 | if (params->sack_freq == 1) { |
| 2714 | asoc->param_flags = |
| 2715 | sctp_spp_sackdelay_disable(asoc->param_flags); |
| 2716 | } else if (params->sack_freq > 1) { |
| 2717 | asoc->sackfreq = params->sack_freq; |
| 2718 | asoc->param_flags = |
| 2719 | sctp_spp_sackdelay_enable(asoc->param_flags); |
| 2720 | } |
| 2721 | |
| 2722 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, |
| 2723 | transports) { |
| 2724 | if (params->sack_delay) { |
| 2725 | trans->sackdelay = msecs_to_jiffies(params->sack_delay); |
| 2726 | trans->param_flags = |
| 2727 | sctp_spp_sackdelay_enable(trans->param_flags); |
| 2728 | } |
| 2729 | if (params->sack_freq == 1) { |
| 2730 | trans->param_flags = |
| 2731 | sctp_spp_sackdelay_disable(trans->param_flags); |
| 2732 | } else if (params->sack_freq > 1) { |
| 2733 | trans->sackfreq = params->sack_freq; |
| 2734 | trans->param_flags = |
| 2735 | sctp_spp_sackdelay_enable(trans->param_flags); |
| 2736 | } |
| 2737 | } |
| 2738 | } |
| 2739 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2740 | /* |
| 2741 | * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2742 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2743 | * This option will effect the way delayed acks are performed. This |
| 2744 | * option allows you to get or set the delayed ack time, in |
| 2745 | * milliseconds. It also allows changing the delayed ack frequency. |
| 2746 | * Changing the frequency to 1 disables the delayed sack algorithm. If |
| 2747 | * the assoc_id is 0, then this sets or gets the endpoints default |
| 2748 | * values. If the assoc_id field is non-zero, then the set or get |
| 2749 | * effects the specified association for the one to many model (the |
| 2750 | * assoc_id field is ignored by the one to one model). Note that if |
| 2751 | * sack_delay or sack_freq are 0 when setting this option, then the |
| 2752 | * current values will remain unchanged. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2753 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2754 | * struct sctp_sack_info { |
| 2755 | * sctp_assoc_t sack_assoc_id; |
| 2756 | * uint32_t sack_delay; |
| 2757 | * uint32_t sack_freq; |
| 2758 | * }; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2759 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2760 | * sack_assoc_id - This parameter, indicates which association the user |
| 2761 | * is performing an action upon. Note that if this field's value is |
| 2762 | * zero then the endpoints default value is changed (effecting future |
| 2763 | * associations only). |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2764 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2765 | * sack_delay - This parameter contains the number of milliseconds that |
| 2766 | * the user is requesting the delayed ACK timer be set to. Note that |
| 2767 | * this value is defined in the standard to be between 200 and 500 |
| 2768 | * milliseconds. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2769 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2770 | * sack_freq - This parameter contains the number of packets that must |
| 2771 | * be received before a sack is sent without waiting for the delay |
| 2772 | * timer to expire. The default value for this is 2, setting this |
| 2773 | * value to 1 will disable the delayed sack algorithm. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2774 | */ |
| 2775 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2776 | static int sctp_setsockopt_delayed_ack(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2777 | char __user *optval, unsigned int optlen) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2778 | { |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 2779 | struct sctp_sock *sp = sctp_sk(sk); |
| 2780 | struct sctp_association *asoc; |
| 2781 | struct sctp_sack_info params; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2782 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2783 | if (optlen == sizeof(struct sctp_sack_info)) { |
| 2784 | if (copy_from_user(¶ms, optval, optlen)) |
| 2785 | return -EFAULT; |
| 2786 | |
| 2787 | if (params.sack_delay == 0 && params.sack_freq == 0) |
| 2788 | return 0; |
| 2789 | } else if (optlen == sizeof(struct sctp_assoc_value)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 2790 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 2791 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 2792 | "Use of struct sctp_assoc_value in delayed_ack socket option.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 2793 | "Use struct sctp_sack_info instead\n", |
| 2794 | current->comm, task_pid_nr(current)); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2795 | if (copy_from_user(¶ms, optval, optlen)) |
| 2796 | return -EFAULT; |
| 2797 | |
| 2798 | if (params.sack_delay == 0) |
| 2799 | params.sack_freq = 1; |
| 2800 | else |
| 2801 | params.sack_freq = 0; |
| 2802 | } else |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 2803 | return -EINVAL; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2804 | |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2805 | /* Validate value parameter. */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2806 | if (params.sack_delay > 500) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2807 | return -EINVAL; |
| 2808 | |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 2809 | /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the |
| 2810 | * socket is a one to many style socket, and an association |
| 2811 | * was not found, then the id was invalid. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2812 | */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2813 | asoc = sctp_id2assoc(sk, params.sack_assoc_id); |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 2814 | if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC && |
| 2815 | sctp_style(sk, UDP)) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2816 | return -EINVAL; |
| 2817 | |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 2818 | if (asoc) { |
| 2819 | sctp_apply_asoc_delayed_ack(¶ms, asoc); |
| 2820 | |
| 2821 | return 0; |
| 2822 | } |
| 2823 | |
Xin Long | 8e2614f | 2019-03-18 20:06:00 +0800 | [diff] [blame] | 2824 | if (sctp_style(sk, TCP)) |
| 2825 | params.sack_assoc_id = SCTP_FUTURE_ASSOC; |
| 2826 | |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 2827 | if (params.sack_assoc_id == SCTP_FUTURE_ASSOC || |
| 2828 | params.sack_assoc_id == SCTP_ALL_ASSOC) { |
| 2829 | if (params.sack_delay) { |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2830 | sp->sackdelay = params.sack_delay; |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2831 | sp->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2832 | sctp_spp_sackdelay_enable(sp->param_flags); |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2833 | } |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 2834 | if (params.sack_freq == 1) { |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2835 | sp->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2836 | sctp_spp_sackdelay_disable(sp->param_flags); |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 2837 | } else if (params.sack_freq > 1) { |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2838 | sp->sackfreq = params.sack_freq; |
| 2839 | sp->param_flags = |
wangweidong | 0ea5e4d | 2014-01-15 17:24:01 +0800 | [diff] [blame] | 2840 | sctp_spp_sackdelay_enable(sp->param_flags); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 2841 | } |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2842 | } |
| 2843 | |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 2844 | if (params.sack_assoc_id == SCTP_CURRENT_ASSOC || |
| 2845 | params.sack_assoc_id == SCTP_ALL_ASSOC) |
| 2846 | list_for_each_entry(asoc, &sp->ep->asocs, asocs) |
| 2847 | sctp_apply_asoc_delayed_ack(¶ms, asoc); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2848 | |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 2849 | return 0; |
| 2850 | } |
| 2851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) |
| 2853 | * |
| 2854 | * Applications can specify protocol parameters for the default association |
| 2855 | * initialization. The option name argument to setsockopt() and getsockopt() |
| 2856 | * is SCTP_INITMSG. |
| 2857 | * |
| 2858 | * Setting initialization parameters is effective only on an unconnected |
| 2859 | * socket (for UDP-style sockets only future associations are effected |
| 2860 | * by the change). With TCP-style sockets, this option is inherited by |
| 2861 | * sockets derived from a listener socket. |
| 2862 | */ |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2863 | 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] | 2864 | { |
| 2865 | struct sctp_initmsg sinit; |
| 2866 | struct sctp_sock *sp = sctp_sk(sk); |
| 2867 | |
| 2868 | if (optlen != sizeof(struct sctp_initmsg)) |
| 2869 | return -EINVAL; |
| 2870 | if (copy_from_user(&sinit, optval, optlen)) |
| 2871 | return -EFAULT; |
| 2872 | |
| 2873 | if (sinit.sinit_num_ostreams) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2874 | sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | if (sinit.sinit_max_instreams) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2876 | sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | if (sinit.sinit_max_attempts) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2878 | sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | if (sinit.sinit_max_init_timeo) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 2880 | sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | |
| 2882 | return 0; |
| 2883 | } |
| 2884 | |
| 2885 | /* |
| 2886 | * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) |
| 2887 | * |
| 2888 | * Applications that wish to use the sendto() system call may wish to |
| 2889 | * specify a default set of parameters that would normally be supplied |
| 2890 | * through the inclusion of ancillary data. This socket option allows |
| 2891 | * such an application to set the default sctp_sndrcvinfo structure. |
| 2892 | * The application that wishes to use this socket option simply passes |
| 2893 | * in to this call the sctp_sndrcvinfo structure defined in Section |
| 2894 | * 5.2.2) The input parameters accepted by this call include |
| 2895 | * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, |
| 2896 | * sinfo_timetolive. The user must provide the sinfo_assoc_id field in |
| 2897 | * to this call if the caller is using the UDP model. |
| 2898 | */ |
| 2899 | static int sctp_setsockopt_default_send_param(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 2900 | char __user *optval, |
| 2901 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | struct sctp_sock *sp = sctp_sk(sk); |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2904 | struct sctp_association *asoc; |
| 2905 | struct sctp_sndrcvinfo info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2907 | if (optlen != sizeof(info)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | return -EINVAL; |
| 2909 | if (copy_from_user(&info, optval, optlen)) |
| 2910 | return -EFAULT; |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2911 | if (info.sinfo_flags & |
| 2912 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | |
| 2913 | SCTP_ABORT | SCTP_EOF)) |
| 2914 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | |
| 2916 | asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); |
Xin Long | 707e45b | 2019-01-28 15:08:35 +0800 | [diff] [blame] | 2917 | if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC && |
| 2918 | sctp_style(sk, UDP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | return -EINVAL; |
Xin Long | 707e45b | 2019-01-28 15:08:35 +0800 | [diff] [blame] | 2920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | if (asoc) { |
| 2922 | asoc->default_stream = info.sinfo_stream; |
| 2923 | asoc->default_flags = info.sinfo_flags; |
| 2924 | asoc->default_ppid = info.sinfo_ppid; |
| 2925 | asoc->default_context = info.sinfo_context; |
| 2926 | asoc->default_timetolive = info.sinfo_timetolive; |
Xin Long | 707e45b | 2019-01-28 15:08:35 +0800 | [diff] [blame] | 2927 | |
| 2928 | return 0; |
| 2929 | } |
| 2930 | |
Marcelo Ricardo Leitner | 1354e72 | 2019-03-18 20:05:59 +0800 | [diff] [blame] | 2931 | if (sctp_style(sk, TCP)) |
| 2932 | info.sinfo_assoc_id = SCTP_FUTURE_ASSOC; |
| 2933 | |
Xin Long | 707e45b | 2019-01-28 15:08:35 +0800 | [diff] [blame] | 2934 | if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC || |
| 2935 | info.sinfo_assoc_id == SCTP_ALL_ASSOC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | sp->default_stream = info.sinfo_stream; |
| 2937 | sp->default_flags = info.sinfo_flags; |
| 2938 | sp->default_ppid = info.sinfo_ppid; |
| 2939 | sp->default_context = info.sinfo_context; |
| 2940 | sp->default_timetolive = info.sinfo_timetolive; |
| 2941 | } |
| 2942 | |
Xin Long | 707e45b | 2019-01-28 15:08:35 +0800 | [diff] [blame] | 2943 | if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC || |
| 2944 | info.sinfo_assoc_id == SCTP_ALL_ASSOC) { |
| 2945 | list_for_each_entry(asoc, &sp->ep->asocs, asocs) { |
| 2946 | asoc->default_stream = info.sinfo_stream; |
| 2947 | asoc->default_flags = info.sinfo_flags; |
| 2948 | asoc->default_ppid = info.sinfo_ppid; |
| 2949 | asoc->default_context = info.sinfo_context; |
| 2950 | asoc->default_timetolive = info.sinfo_timetolive; |
| 2951 | } |
| 2952 | } |
| 2953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | return 0; |
| 2955 | } |
| 2956 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2957 | /* RFC6458, Section 8.1.31. Set/get Default Send Parameters |
| 2958 | * (SCTP_DEFAULT_SNDINFO) |
| 2959 | */ |
| 2960 | static int sctp_setsockopt_default_sndinfo(struct sock *sk, |
| 2961 | char __user *optval, |
| 2962 | unsigned int optlen) |
| 2963 | { |
| 2964 | struct sctp_sock *sp = sctp_sk(sk); |
| 2965 | struct sctp_association *asoc; |
| 2966 | struct sctp_sndinfo info; |
| 2967 | |
| 2968 | if (optlen != sizeof(info)) |
| 2969 | return -EINVAL; |
| 2970 | if (copy_from_user(&info, optval, optlen)) |
| 2971 | return -EFAULT; |
| 2972 | if (info.snd_flags & |
| 2973 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | |
| 2974 | SCTP_ABORT | SCTP_EOF)) |
| 2975 | return -EINVAL; |
| 2976 | |
| 2977 | asoc = sctp_id2assoc(sk, info.snd_assoc_id); |
Xin Long | 92fc3bd | 2019-01-28 15:08:36 +0800 | [diff] [blame] | 2978 | if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC && |
| 2979 | sctp_style(sk, UDP)) |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2980 | return -EINVAL; |
Xin Long | 92fc3bd | 2019-01-28 15:08:36 +0800 | [diff] [blame] | 2981 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2982 | if (asoc) { |
| 2983 | asoc->default_stream = info.snd_sid; |
| 2984 | asoc->default_flags = info.snd_flags; |
| 2985 | asoc->default_ppid = info.snd_ppid; |
| 2986 | asoc->default_context = info.snd_context; |
Xin Long | 92fc3bd | 2019-01-28 15:08:36 +0800 | [diff] [blame] | 2987 | |
| 2988 | return 0; |
| 2989 | } |
| 2990 | |
Xin Long | a842e65 | 2019-03-18 20:06:01 +0800 | [diff] [blame] | 2991 | if (sctp_style(sk, TCP)) |
| 2992 | info.snd_assoc_id = SCTP_FUTURE_ASSOC; |
| 2993 | |
Xin Long | 92fc3bd | 2019-01-28 15:08:36 +0800 | [diff] [blame] | 2994 | if (info.snd_assoc_id == SCTP_FUTURE_ASSOC || |
| 2995 | info.snd_assoc_id == SCTP_ALL_ASSOC) { |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 2996 | sp->default_stream = info.snd_sid; |
| 2997 | sp->default_flags = info.snd_flags; |
| 2998 | sp->default_ppid = info.snd_ppid; |
| 2999 | sp->default_context = info.snd_context; |
| 3000 | } |
| 3001 | |
Xin Long | 92fc3bd | 2019-01-28 15:08:36 +0800 | [diff] [blame] | 3002 | if (info.snd_assoc_id == SCTP_CURRENT_ASSOC || |
| 3003 | info.snd_assoc_id == SCTP_ALL_ASSOC) { |
| 3004 | list_for_each_entry(asoc, &sp->ep->asocs, asocs) { |
| 3005 | asoc->default_stream = info.snd_sid; |
| 3006 | asoc->default_flags = info.snd_flags; |
| 3007 | asoc->default_ppid = info.snd_ppid; |
| 3008 | asoc->default_context = info.snd_context; |
| 3009 | } |
| 3010 | } |
| 3011 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 3012 | return 0; |
| 3013 | } |
| 3014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) |
| 3016 | * |
| 3017 | * Requests that the local SCTP stack use the enclosed peer address as |
| 3018 | * the association primary. The enclosed address must be one of the |
| 3019 | * association peer's addresses. |
| 3020 | */ |
| 3021 | 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] | 3022 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | { |
| 3024 | struct sctp_prim prim; |
| 3025 | struct sctp_transport *trans; |
Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 3026 | struct sctp_af *af; |
| 3027 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | |
| 3029 | if (optlen != sizeof(struct sctp_prim)) |
| 3030 | return -EINVAL; |
| 3031 | |
| 3032 | if (copy_from_user(&prim, optval, sizeof(struct sctp_prim))) |
| 3033 | return -EFAULT; |
| 3034 | |
Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 3035 | /* Allow security module to validate address but need address len. */ |
| 3036 | af = sctp_get_af_specific(prim.ssp_addr.ss_family); |
| 3037 | if (!af) |
| 3038 | return -EINVAL; |
| 3039 | |
| 3040 | err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR, |
| 3041 | (struct sockaddr *)&prim.ssp_addr, |
| 3042 | af->sockaddr_len); |
| 3043 | if (err) |
| 3044 | return err; |
| 3045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id); |
| 3047 | if (!trans) |
| 3048 | return -EINVAL; |
| 3049 | |
| 3050 | sctp_assoc_set_primary(trans->asoc, trans); |
| 3051 | |
| 3052 | return 0; |
| 3053 | } |
| 3054 | |
| 3055 | /* |
| 3056 | * 7.1.5 SCTP_NODELAY |
| 3057 | * |
| 3058 | * Turn on/off any Nagle-like algorithm. This means that packets are |
| 3059 | * generally sent as soon as possible and no unnecessary delays are |
| 3060 | * introduced, at the cost of more packets in the network. Expects an |
| 3061 | * integer boolean flag. |
| 3062 | */ |
| 3063 | static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3064 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | { |
| 3066 | int val; |
| 3067 | |
| 3068 | if (optlen < sizeof(int)) |
| 3069 | return -EINVAL; |
| 3070 | if (get_user(val, (int __user *)optval)) |
| 3071 | return -EFAULT; |
| 3072 | |
| 3073 | sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1; |
| 3074 | return 0; |
| 3075 | } |
| 3076 | |
| 3077 | /* |
| 3078 | * |
| 3079 | * 7.1.1 SCTP_RTOINFO |
| 3080 | * |
| 3081 | * The protocol parameters used to initialize and bound retransmission |
| 3082 | * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access |
| 3083 | * and modify these parameters. |
| 3084 | * All parameters are time values, in milliseconds. A value of 0, when |
| 3085 | * modifying the parameters, indicates that the current value should not |
| 3086 | * be changed. |
| 3087 | * |
| 3088 | */ |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3089 | static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen) |
| 3090 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | struct sctp_rtoinfo rtoinfo; |
| 3092 | struct sctp_association *asoc; |
wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 3093 | unsigned long rto_min, rto_max; |
| 3094 | struct sctp_sock *sp = sctp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | |
| 3096 | if (optlen != sizeof (struct sctp_rtoinfo)) |
| 3097 | return -EINVAL; |
| 3098 | |
| 3099 | if (copy_from_user(&rtoinfo, optval, optlen)) |
| 3100 | return -EFAULT; |
| 3101 | |
| 3102 | asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); |
| 3103 | |
| 3104 | /* Set the values to the specific association */ |
Xin Long | 7adb5ed | 2019-01-28 15:08:25 +0800 | [diff] [blame] | 3105 | if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC && |
| 3106 | sctp_style(sk, UDP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | return -EINVAL; |
| 3108 | |
wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 3109 | rto_max = rtoinfo.srto_max; |
| 3110 | rto_min = rtoinfo.srto_min; |
| 3111 | |
| 3112 | if (rto_max) |
| 3113 | rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max; |
| 3114 | else |
| 3115 | rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max; |
| 3116 | |
| 3117 | if (rto_min) |
| 3118 | rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min; |
| 3119 | else |
| 3120 | rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min; |
| 3121 | |
| 3122 | if (rto_min > rto_max) |
| 3123 | return -EINVAL; |
| 3124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | if (asoc) { |
| 3126 | if (rtoinfo.srto_initial != 0) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3127 | asoc->rto_initial = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | msecs_to_jiffies(rtoinfo.srto_initial); |
wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 3129 | asoc->rto_max = rto_max; |
| 3130 | asoc->rto_min = rto_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | } else { |
| 3132 | /* If there is no association or the association-id = 0 |
| 3133 | * set the values to the endpoint. |
| 3134 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | if (rtoinfo.srto_initial != 0) |
| 3136 | sp->rtoinfo.srto_initial = rtoinfo.srto_initial; |
wangweidong | 85f935d | 2013-12-11 09:50:38 +0800 | [diff] [blame] | 3137 | sp->rtoinfo.srto_max = rto_max; |
| 3138 | sp->rtoinfo.srto_min = rto_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | return 0; |
| 3142 | } |
| 3143 | |
| 3144 | /* |
| 3145 | * |
| 3146 | * 7.1.2 SCTP_ASSOCINFO |
| 3147 | * |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 3148 | * This option is used to tune the maximum retransmission attempts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | * of the association. |
| 3150 | * Returns an error if the new association retransmission value is |
| 3151 | * greater than the sum of the retransmission value of the peer. |
| 3152 | * See [SCTP] for more information. |
| 3153 | * |
| 3154 | */ |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3155 | 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] | 3156 | { |
| 3157 | |
| 3158 | struct sctp_assocparams assocparams; |
| 3159 | struct sctp_association *asoc; |
| 3160 | |
| 3161 | if (optlen != sizeof(struct sctp_assocparams)) |
| 3162 | return -EINVAL; |
| 3163 | if (copy_from_user(&assocparams, optval, optlen)) |
| 3164 | return -EFAULT; |
| 3165 | |
| 3166 | asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); |
| 3167 | |
Xin Long | 8889394 | 2019-01-28 15:08:26 +0800 | [diff] [blame] | 3168 | if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC && |
| 3169 | sctp_style(sk, UDP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | return -EINVAL; |
| 3171 | |
| 3172 | /* Set the values to the specific association */ |
| 3173 | if (asoc) { |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3174 | if (assocparams.sasoc_asocmaxrxt != 0) { |
| 3175 | __u32 path_sum = 0; |
| 3176 | int paths = 0; |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3177 | struct sctp_transport *peer_addr; |
| 3178 | |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 3179 | list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list, |
| 3180 | transports) { |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3181 | path_sum += peer_addr->pathmaxrxt; |
| 3182 | paths++; |
| 3183 | } |
| 3184 | |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 3185 | /* Only validate asocmaxrxt if we have more than |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3186 | * one path/transport. We do this because path |
| 3187 | * retransmissions are only counted when we have more |
| 3188 | * then one path. |
| 3189 | */ |
| 3190 | if (paths > 1 && |
| 3191 | assocparams.sasoc_asocmaxrxt > path_sum) |
| 3192 | return -EINVAL; |
| 3193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | asoc->max_retrans = assocparams.sasoc_asocmaxrxt; |
Vlad Yasevich | 402d68c | 2006-06-17 22:54:51 -0700 | [diff] [blame] | 3195 | } |
| 3196 | |
Daniel Borkmann | 52db882 | 2013-06-25 18:17:27 +0200 | [diff] [blame] | 3197 | if (assocparams.sasoc_cookie_life != 0) |
| 3198 | asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | } else { |
| 3200 | /* Set the values to the endpoint */ |
| 3201 | struct sctp_sock *sp = sctp_sk(sk); |
| 3202 | |
| 3203 | if (assocparams.sasoc_asocmaxrxt != 0) |
| 3204 | sp->assocparams.sasoc_asocmaxrxt = |
| 3205 | assocparams.sasoc_asocmaxrxt; |
| 3206 | if (assocparams.sasoc_cookie_life != 0) |
| 3207 | sp->assocparams.sasoc_cookie_life = |
| 3208 | assocparams.sasoc_cookie_life; |
| 3209 | } |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
| 3213 | /* |
| 3214 | * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) |
| 3215 | * |
| 3216 | * This socket option is a boolean flag which turns on or off mapped V4 |
| 3217 | * addresses. If this option is turned on and the socket is type |
| 3218 | * PF_INET6, then IPv4 addresses will be mapped to V6 representation. |
| 3219 | * If this option is turned off, then no mapping will be done of V4 |
| 3220 | * addresses and a user will receive both PF_INET6 and PF_INET type |
| 3221 | * addresses on the socket. |
| 3222 | */ |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3223 | 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] | 3224 | { |
| 3225 | int val; |
| 3226 | struct sctp_sock *sp = sctp_sk(sk); |
| 3227 | |
| 3228 | if (optlen < sizeof(int)) |
| 3229 | return -EINVAL; |
| 3230 | if (get_user(val, (int __user *)optval)) |
| 3231 | return -EFAULT; |
| 3232 | if (val) |
| 3233 | sp->v4mapped = 1; |
| 3234 | else |
| 3235 | sp->v4mapped = 0; |
| 3236 | |
| 3237 | return 0; |
| 3238 | } |
| 3239 | |
| 3240 | /* |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3241 | * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) |
| 3242 | * This option will get or set the maximum size to put in any outgoing |
| 3243 | * 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] | 3244 | * fragmented by SCTP into the specified size. Note that the underlying |
| 3245 | * SCTP implementation may fragment into smaller sized chunks when the |
| 3246 | * PMTU of the underlying association is smaller than the value set by |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3247 | * the user. The default value for this option is '0' which indicates |
| 3248 | * the user is NOT limiting fragmentation and only the PMTU will effect |
| 3249 | * SCTP's choice of DATA chunk size. Note also that values set larger |
| 3250 | * than the maximum size of an IP datagram will effectively let SCTP |
| 3251 | * control fragmentation (i.e. the same as setting this option to 0). |
| 3252 | * |
| 3253 | * The following structure is used to access and modify this parameter: |
| 3254 | * |
| 3255 | * struct sctp_assoc_value { |
| 3256 | * sctp_assoc_t assoc_id; |
| 3257 | * uint32_t assoc_value; |
| 3258 | * }; |
| 3259 | * |
| 3260 | * assoc_id: This parameter is ignored for one-to-one style sockets. |
| 3261 | * For one-to-many style sockets this parameter indicates which |
| 3262 | * association the user is performing an action upon. Note that if |
| 3263 | * this field's value is zero then the endpoints default value is |
| 3264 | * changed (effecting future associations only). |
| 3265 | * assoc_value: This parameter specifies the maximum size in bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | */ |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3267 | 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] | 3268 | { |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3269 | struct sctp_sock *sp = sctp_sk(sk); |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3270 | struct sctp_assoc_value params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | struct sctp_association *asoc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | int val; |
| 3273 | |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3274 | if (optlen == sizeof(int)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3275 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3276 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3277 | "Use of int in maxseg socket option.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3278 | "Use struct sctp_assoc_value instead\n", |
| 3279 | current->comm, task_pid_nr(current)); |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3280 | if (copy_from_user(&val, optval, optlen)) |
| 3281 | return -EFAULT; |
Xin Long | 6fd769b | 2019-01-28 15:08:27 +0800 | [diff] [blame] | 3282 | params.assoc_id = SCTP_FUTURE_ASSOC; |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3283 | } else if (optlen == sizeof(struct sctp_assoc_value)) { |
| 3284 | if (copy_from_user(¶ms, optval, optlen)) |
| 3285 | return -EFAULT; |
| 3286 | val = params.assoc_value; |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3287 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | return -EINVAL; |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3289 | } |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3290 | |
Marcelo Ricardo Leitner | 439ef03 | 2018-04-26 16:59:01 -0300 | [diff] [blame] | 3291 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | 6fd769b | 2019-01-28 15:08:27 +0800 | [diff] [blame] | 3292 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 3293 | sctp_style(sk, UDP)) |
| 3294 | return -EINVAL; |
Marcelo Ricardo Leitner | 439ef03 | 2018-04-26 16:59:01 -0300 | [diff] [blame] | 3295 | |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3296 | if (val) { |
| 3297 | int min_len, max_len; |
Marcelo Ricardo Leitner | 439ef03 | 2018-04-26 16:59:01 -0300 | [diff] [blame] | 3298 | __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) : |
| 3299 | sizeof(struct sctp_data_chunk); |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3300 | |
Jakub Audykowicz | afd0a80 | 2018-12-04 20:27:41 +0100 | [diff] [blame] | 3301 | min_len = sctp_min_frag_point(sp, datasize); |
Marcelo Ricardo Leitner | 439ef03 | 2018-04-26 16:59:01 -0300 | [diff] [blame] | 3302 | max_len = SCTP_MAX_CHUNK_LEN - datasize; |
Xin Long | ecca8f8 | 2017-11-17 14:11:11 +0800 | [diff] [blame] | 3303 | |
| 3304 | if (val < min_len || val > max_len) |
| 3305 | return -EINVAL; |
| 3306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3308 | if (asoc) { |
Vlad Yasevich | f68b2e0 | 2009-09-04 18:21:00 -0400 | [diff] [blame] | 3309 | asoc->user_frag = val; |
Marcelo Ricardo Leitner | 2f5e3c9 | 2018-04-26 16:58:55 -0300 | [diff] [blame] | 3310 | sctp_assoc_update_frag_point(asoc); |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 3311 | } else { |
| 3312 | sp->user_frag = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | return 0; |
| 3316 | } |
| 3317 | |
| 3318 | |
| 3319 | /* |
| 3320 | * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) |
| 3321 | * |
| 3322 | * Requests that the peer mark the enclosed address as the association |
| 3323 | * primary. The enclosed address must be one of the association's |
| 3324 | * locally bound addresses. The following structure is used to make a |
| 3325 | * set primary request: |
| 3326 | */ |
| 3327 | 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] | 3328 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | { |
| 3330 | struct sctp_sock *sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | struct sctp_association *asoc = NULL; |
| 3332 | struct sctp_setpeerprim prim; |
| 3333 | struct sctp_chunk *chunk; |
Wei Yongjun | 40a0103 | 2010-12-07 17:11:09 +0000 | [diff] [blame] | 3334 | struct sctp_af *af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | int err; |
| 3336 | |
| 3337 | sp = sctp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | |
Xin Long | 4e27428 | 2019-08-19 22:02:43 +0800 | [diff] [blame] | 3339 | if (!sp->ep->asconf_enable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | return -EPERM; |
| 3341 | |
| 3342 | if (optlen != sizeof(struct sctp_setpeerprim)) |
| 3343 | return -EINVAL; |
| 3344 | |
| 3345 | if (copy_from_user(&prim, optval, optlen)) |
| 3346 | return -EFAULT; |
| 3347 | |
| 3348 | asoc = sctp_id2assoc(sk, prim.sspp_assoc_id); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 3349 | if (!asoc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | return -EINVAL; |
| 3351 | |
| 3352 | if (!asoc->peer.asconf_capable) |
| 3353 | return -EPERM; |
| 3354 | |
| 3355 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY) |
| 3356 | return -EPERM; |
| 3357 | |
| 3358 | if (!sctp_state(asoc, ESTABLISHED)) |
| 3359 | return -ENOTCONN; |
| 3360 | |
Wei Yongjun | 40a0103 | 2010-12-07 17:11:09 +0000 | [diff] [blame] | 3361 | af = sctp_get_af_specific(prim.sspp_addr.ss_family); |
| 3362 | if (!af) |
| 3363 | return -EINVAL; |
| 3364 | |
| 3365 | if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL)) |
| 3366 | return -EADDRNOTAVAIL; |
| 3367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr)) |
| 3369 | return -EADDRNOTAVAIL; |
| 3370 | |
Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 3371 | /* Allow security module to validate address. */ |
| 3372 | err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR, |
| 3373 | (struct sockaddr *)&prim.sspp_addr, |
| 3374 | af->sockaddr_len); |
| 3375 | if (err) |
| 3376 | return err; |
| 3377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3378 | /* Create an ASCONF chunk with SET_PRIMARY parameter */ |
| 3379 | chunk = sctp_make_asconf_set_prim(asoc, |
| 3380 | (union sctp_addr *)&prim.sspp_addr); |
| 3381 | if (!chunk) |
| 3382 | return -ENOMEM; |
| 3383 | |
| 3384 | err = sctp_send_asconf(asoc, chunk); |
| 3385 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 3386 | pr_debug("%s: we set peer primary addr primitively\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3387 | |
| 3388 | return err; |
| 3389 | } |
| 3390 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3391 | 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] | 3392 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | { |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3394 | struct sctp_setadaptation adaptation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3396 | if (optlen != sizeof(struct sctp_setadaptation)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | return -EINVAL; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3398 | if (copy_from_user(&adaptation, optval, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | return -EFAULT; |
| 3400 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 3401 | sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | |
| 3403 | return 0; |
| 3404 | } |
| 3405 | |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3406 | /* |
| 3407 | * 7.1.29. Set or Get the default context (SCTP_CONTEXT) |
| 3408 | * |
| 3409 | * The context field in the sctp_sndrcvinfo structure is normally only |
| 3410 | * used when a failed message is retrieved holding the value that was |
| 3411 | * sent down on the actual send call. This option allows the setting of |
| 3412 | * a default context on an association basis that will be received on |
| 3413 | * reading messages from the peer. This is especially helpful in the |
| 3414 | * one-2-many model for an application to keep some reference to an |
| 3415 | * internal state machine that is processing messages on the |
| 3416 | * association. Note that the setting of this value only effects |
| 3417 | * received messages from the peer and does not effect the value that is |
| 3418 | * saved with outbound messages. |
| 3419 | */ |
| 3420 | static int sctp_setsockopt_context(struct sock *sk, char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3421 | unsigned int optlen) |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3422 | { |
Xin Long | 49b037a | 2019-01-28 15:08:37 +0800 | [diff] [blame] | 3423 | struct sctp_sock *sp = sctp_sk(sk); |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3424 | struct sctp_assoc_value params; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3425 | struct sctp_association *asoc; |
| 3426 | |
| 3427 | if (optlen != sizeof(struct sctp_assoc_value)) |
| 3428 | return -EINVAL; |
| 3429 | if (copy_from_user(¶ms, optval, optlen)) |
| 3430 | return -EFAULT; |
| 3431 | |
Xin Long | 49b037a | 2019-01-28 15:08:37 +0800 | [diff] [blame] | 3432 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 3433 | if (!asoc && params.assoc_id > SCTP_ALL_ASSOC && |
| 3434 | sctp_style(sk, UDP)) |
| 3435 | return -EINVAL; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3436 | |
Xin Long | 49b037a | 2019-01-28 15:08:37 +0800 | [diff] [blame] | 3437 | if (asoc) { |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3438 | asoc->default_rcv_context = params.assoc_value; |
Xin Long | 49b037a | 2019-01-28 15:08:37 +0800 | [diff] [blame] | 3439 | |
| 3440 | return 0; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3441 | } |
| 3442 | |
Xin Long | cface2c | 2019-03-18 20:06:02 +0800 | [diff] [blame] | 3443 | if (sctp_style(sk, TCP)) |
| 3444 | params.assoc_id = SCTP_FUTURE_ASSOC; |
| 3445 | |
Xin Long | 49b037a | 2019-01-28 15:08:37 +0800 | [diff] [blame] | 3446 | if (params.assoc_id == SCTP_FUTURE_ASSOC || |
| 3447 | params.assoc_id == SCTP_ALL_ASSOC) |
| 3448 | sp->default_rcv_context = params.assoc_value; |
| 3449 | |
| 3450 | if (params.assoc_id == SCTP_CURRENT_ASSOC || |
| 3451 | params.assoc_id == SCTP_ALL_ASSOC) |
| 3452 | list_for_each_entry(asoc, &sp->ep->asocs, asocs) |
| 3453 | asoc->default_rcv_context = params.assoc_value; |
| 3454 | |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 3455 | return 0; |
| 3456 | } |
| 3457 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3458 | /* |
| 3459 | * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) |
| 3460 | * |
| 3461 | * This options will at a minimum specify if the implementation is doing |
| 3462 | * fragmented interleave. Fragmented interleave, for a one to many |
| 3463 | * socket, is when subsequent calls to receive a message may return |
| 3464 | * parts of messages from different associations. Some implementations |
| 3465 | * may allow you to turn this value on or off. If so, when turned off, |
| 3466 | * no fragment interleave will occur (which will cause a head of line |
| 3467 | * blocking amongst multiple associations sharing the same one to many |
| 3468 | * socket). When this option is turned on, then each receive call may |
| 3469 | * come from a different association (thus the user must receive data |
| 3470 | * with the extended calls (e.g. sctp_recvmsg) to keep track of which |
| 3471 | * association each receive belongs to. |
| 3472 | * |
| 3473 | * This option takes a boolean value. A non-zero value indicates that |
| 3474 | * fragmented interleave is on. A value of zero indicates that |
| 3475 | * fragmented interleave is off. |
| 3476 | * |
| 3477 | * Note that it is important that an implementation that allows this |
| 3478 | * option to be turned on, have it off by default. Otherwise an unaware |
| 3479 | * application using the one to many model may become confused and act |
| 3480 | * incorrectly. |
| 3481 | */ |
| 3482 | static int sctp_setsockopt_fragment_interleave(struct sock *sk, |
| 3483 | char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3484 | unsigned int optlen) |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3485 | { |
| 3486 | int val; |
| 3487 | |
| 3488 | if (optlen != sizeof(int)) |
| 3489 | return -EINVAL; |
| 3490 | if (get_user(val, (int __user *)optval)) |
| 3491 | return -EFAULT; |
| 3492 | |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 3493 | sctp_sk(sk)->frag_interleave = !!val; |
| 3494 | |
| 3495 | if (!sctp_sk(sk)->frag_interleave) |
Xin Long | e55f4b8 | 2019-07-09 00:57:07 +0800 | [diff] [blame] | 3496 | sctp_sk(sk)->ep->intl_enable = 0; |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 3497 | |
| 3498 | return 0; |
| 3499 | } |
| 3500 | |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3501 | /* |
Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3502 | * 8.1.21. Set or Get the SCTP Partial Delivery Point |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3503 | * (SCTP_PARTIAL_DELIVERY_POINT) |
Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3504 | * |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3505 | * This option will set or get the SCTP partial delivery point. This |
| 3506 | * point is the size of a message where the partial delivery API will be |
| 3507 | * 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] | 3508 | * lower value will cause partial deliveries to happen more often. The |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3509 | * calls argument is an integer that sets or gets the partial delivery |
Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3510 | * point. Note also that the call will fail if the user attempts to set |
| 3511 | * this value larger than the socket receive buffer size. |
| 3512 | * |
| 3513 | * Note that any single message having a length smaller than or equal to |
| 3514 | * the SCTP partial delivery point will be delivered in one single read |
| 3515 | * call as long as the user provided buffer is large enough to hold the |
| 3516 | * message. |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3517 | */ |
| 3518 | static int sctp_setsockopt_partial_delivery_point(struct sock *sk, |
| 3519 | char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3520 | unsigned int optlen) |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3521 | { |
| 3522 | u32 val; |
| 3523 | |
| 3524 | if (optlen != sizeof(u32)) |
| 3525 | return -EINVAL; |
| 3526 | if (get_user(val, (int __user *)optval)) |
| 3527 | return -EFAULT; |
| 3528 | |
Wei Yongjun | 8510b93 | 2008-12-25 16:59:03 -0800 | [diff] [blame] | 3529 | /* Note: We double the receive buffer from what the user sets |
| 3530 | * it to be, also initial rwnd is based on rcvbuf/2. |
| 3531 | */ |
| 3532 | if (val > (sk->sk_rcvbuf >> 1)) |
| 3533 | return -EINVAL; |
| 3534 | |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 3535 | sctp_sk(sk)->pd_point = val; |
| 3536 | |
| 3537 | return 0; /* is this the right error code? */ |
| 3538 | } |
| 3539 | |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3540 | /* |
| 3541 | * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) |
| 3542 | * |
| 3543 | * This option will allow a user to change the maximum burst of packets |
| 3544 | * that can be emitted by this association. Note that the default value |
| 3545 | * is 4, and some implementations may restrict this setting so that it |
| 3546 | * can only be lowered. |
| 3547 | * |
| 3548 | * NOTE: This text doesn't seem right. Do this on a socket basis with |
| 3549 | * future associations inheriting the socket value. |
| 3550 | */ |
| 3551 | static int sctp_setsockopt_maxburst(struct sock *sk, |
| 3552 | char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3553 | unsigned int optlen) |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3554 | { |
Xin Long | e0651a0 | 2019-01-28 15:08:38 +0800 | [diff] [blame] | 3555 | struct sctp_sock *sp = sctp_sk(sk); |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3556 | struct sctp_assoc_value params; |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3557 | struct sctp_association *asoc; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3558 | |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3559 | if (optlen == sizeof(int)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3560 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3561 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 3562 | "Use of int in max_burst socket option deprecated.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 3563 | "Use struct sctp_assoc_value instead\n", |
| 3564 | current->comm, task_pid_nr(current)); |
Xin Long | e0651a0 | 2019-01-28 15:08:38 +0800 | [diff] [blame] | 3565 | if (copy_from_user(¶ms.assoc_value, optval, optlen)) |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3566 | return -EFAULT; |
Xin Long | e0651a0 | 2019-01-28 15:08:38 +0800 | [diff] [blame] | 3567 | params.assoc_id = SCTP_FUTURE_ASSOC; |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3568 | } else if (optlen == sizeof(struct sctp_assoc_value)) { |
| 3569 | if (copy_from_user(¶ms, optval, optlen)) |
| 3570 | return -EFAULT; |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3571 | } else |
| 3572 | return -EINVAL; |
| 3573 | |
Xin Long | e0651a0 | 2019-01-28 15:08:38 +0800 | [diff] [blame] | 3574 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 3575 | if (!asoc && params.assoc_id > SCTP_ALL_ASSOC && |
| 3576 | sctp_style(sk, UDP)) |
| 3577 | return -EINVAL; |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 3578 | |
Xin Long | e0651a0 | 2019-01-28 15:08:38 +0800 | [diff] [blame] | 3579 | if (asoc) { |
| 3580 | asoc->max_burst = params.assoc_value; |
| 3581 | |
| 3582 | return 0; |
| 3583 | } |
| 3584 | |
Xin Long | 746bc21 | 2019-03-18 20:06:03 +0800 | [diff] [blame] | 3585 | if (sctp_style(sk, TCP)) |
| 3586 | params.assoc_id = SCTP_FUTURE_ASSOC; |
| 3587 | |
Xin Long | e0651a0 | 2019-01-28 15:08:38 +0800 | [diff] [blame] | 3588 | if (params.assoc_id == SCTP_FUTURE_ASSOC || |
| 3589 | params.assoc_id == SCTP_ALL_ASSOC) |
| 3590 | sp->max_burst = params.assoc_value; |
| 3591 | |
| 3592 | if (params.assoc_id == SCTP_CURRENT_ASSOC || |
| 3593 | params.assoc_id == SCTP_ALL_ASSOC) |
| 3594 | list_for_each_entry(asoc, &sp->ep->asocs, asocs) |
| 3595 | asoc->max_burst = params.assoc_value; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 3596 | |
| 3597 | return 0; |
| 3598 | } |
| 3599 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3600 | /* |
| 3601 | * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK) |
| 3602 | * |
| 3603 | * This set option adds a chunk type that the user is requesting to be |
| 3604 | * received only in an authenticated way. Changes to the list of chunks |
| 3605 | * will only effect future associations on the socket. |
| 3606 | */ |
| 3607 | static int sctp_setsockopt_auth_chunk(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3608 | char __user *optval, |
| 3609 | unsigned int optlen) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3610 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3611 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3612 | struct sctp_authchunk val; |
| 3613 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3614 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3615 | return -EACCES; |
| 3616 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3617 | if (optlen != sizeof(struct sctp_authchunk)) |
| 3618 | return -EINVAL; |
| 3619 | if (copy_from_user(&val, optval, optlen)) |
| 3620 | return -EFAULT; |
| 3621 | |
| 3622 | switch (val.sauth_chunk) { |
Joe Perches | 7fd71b1 | 2011-07-01 09:43:11 +0000 | [diff] [blame] | 3623 | case SCTP_CID_INIT: |
| 3624 | case SCTP_CID_INIT_ACK: |
| 3625 | case SCTP_CID_SHUTDOWN_COMPLETE: |
| 3626 | case SCTP_CID_AUTH: |
| 3627 | return -EINVAL; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3628 | } |
| 3629 | |
| 3630 | /* add this chunk id to the endpoint */ |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3631 | return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | /* |
| 3635 | * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT) |
| 3636 | * |
| 3637 | * This option gets or sets the list of HMAC algorithms that the local |
| 3638 | * endpoint requires the peer to use. |
| 3639 | */ |
| 3640 | static int sctp_setsockopt_hmac_ident(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3641 | char __user *optval, |
| 3642 | unsigned int optlen) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3643 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3644 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3645 | struct sctp_hmacalgo *hmacs; |
Vlad Yasevich | d972405 | 2008-08-27 16:09:49 -0700 | [diff] [blame] | 3646 | u32 idents; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3647 | int err; |
| 3648 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3649 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 3650 | return -EACCES; |
| 3651 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3652 | if (optlen < sizeof(struct sctp_hmacalgo)) |
| 3653 | return -EINVAL; |
Marcelo Ricardo Leitner | 5960cef | 2018-01-08 19:02:28 -0200 | [diff] [blame] | 3654 | optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) + |
| 3655 | SCTP_AUTH_NUM_HMACS * sizeof(u16)); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3656 | |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 3657 | hmacs = memdup_user(optval, optlen); |
Shan Wei | 934253a | 2011-04-18 19:13:18 +0000 | [diff] [blame] | 3658 | if (IS_ERR(hmacs)) |
| 3659 | return PTR_ERR(hmacs); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3660 | |
Vlad Yasevich | d972405 | 2008-08-27 16:09:49 -0700 | [diff] [blame] | 3661 | idents = hmacs->shmac_num_idents; |
| 3662 | if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS || |
| 3663 | (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) { |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3664 | err = -EINVAL; |
| 3665 | goto out; |
| 3666 | } |
| 3667 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3668 | err = sctp_auth_ep_set_hmacs(ep, hmacs); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3669 | out: |
| 3670 | kfree(hmacs); |
| 3671 | return err; |
| 3672 | } |
| 3673 | |
| 3674 | /* |
| 3675 | * 7.1.20. Set a shared key (SCTP_AUTH_KEY) |
| 3676 | * |
| 3677 | * This option will set a shared secret key which is used to build an |
| 3678 | * association shared key. |
| 3679 | */ |
| 3680 | static int sctp_setsockopt_auth_key(struct sock *sk, |
| 3681 | char __user *optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3682 | unsigned int optlen) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3683 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3684 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3685 | struct sctp_authkey *authkey; |
| 3686 | struct sctp_association *asoc; |
Xin Long | 7fb3be1 | 2019-01-28 15:08:39 +0800 | [diff] [blame] | 3687 | int ret = -EINVAL; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3688 | |
| 3689 | if (optlen <= sizeof(struct sctp_authkey)) |
| 3690 | return -EINVAL; |
Marcelo Ricardo Leitner | 5960cef | 2018-01-08 19:02:28 -0200 | [diff] [blame] | 3691 | /* authkey->sca_keylength is u16, so optlen can't be bigger than |
| 3692 | * this. |
| 3693 | */ |
Xin Long | 7fb3be1 | 2019-01-28 15:08:39 +0800 | [diff] [blame] | 3694 | optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey)); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3695 | |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 3696 | authkey = memdup_user(optval, optlen); |
Shan Wei | 934253a | 2011-04-18 19:13:18 +0000 | [diff] [blame] | 3697 | if (IS_ERR(authkey)) |
| 3698 | return PTR_ERR(authkey); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3699 | |
Xin Long | 7fb3be1 | 2019-01-28 15:08:39 +0800 | [diff] [blame] | 3700 | if (authkey->sca_keylength > optlen - sizeof(*authkey)) |
Vlad Yasevich | 30c2235 | 2008-08-25 15:16:19 -0700 | [diff] [blame] | 3701 | goto out; |
Vlad Yasevich | 30c2235 | 2008-08-25 15:16:19 -0700 | [diff] [blame] | 3702 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3703 | asoc = sctp_id2assoc(sk, authkey->sca_assoc_id); |
Xin Long | 7fb3be1 | 2019-01-28 15:08:39 +0800 | [diff] [blame] | 3704 | if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC && |
| 3705 | sctp_style(sk, UDP)) |
| 3706 | goto out; |
| 3707 | |
| 3708 | if (asoc) { |
| 3709 | ret = sctp_auth_set_key(ep, asoc, authkey); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3710 | goto out; |
| 3711 | } |
| 3712 | |
Xin Long | 0685d6b | 2019-03-18 20:06:04 +0800 | [diff] [blame] | 3713 | if (sctp_style(sk, TCP)) |
| 3714 | authkey->sca_assoc_id = SCTP_FUTURE_ASSOC; |
| 3715 | |
Xin Long | 7fb3be1 | 2019-01-28 15:08:39 +0800 | [diff] [blame] | 3716 | if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC || |
| 3717 | authkey->sca_assoc_id == SCTP_ALL_ASSOC) { |
| 3718 | ret = sctp_auth_set_key(ep, asoc, authkey); |
| 3719 | if (ret) |
| 3720 | goto out; |
| 3721 | } |
| 3722 | |
| 3723 | ret = 0; |
| 3724 | |
| 3725 | if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC || |
| 3726 | authkey->sca_assoc_id == SCTP_ALL_ASSOC) { |
| 3727 | list_for_each_entry(asoc, &ep->asocs, asocs) { |
| 3728 | int res = sctp_auth_set_key(ep, asoc, authkey); |
| 3729 | |
| 3730 | if (res && !ret) |
| 3731 | ret = res; |
| 3732 | } |
| 3733 | } |
| 3734 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3735 | out: |
Daniel Borkmann | 6ba542a | 2013-02-08 03:04:34 +0000 | [diff] [blame] | 3736 | kzfree(authkey); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3737 | return ret; |
| 3738 | } |
| 3739 | |
| 3740 | /* |
| 3741 | * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY) |
| 3742 | * |
| 3743 | * This option will get or set the active shared key to be used to build |
| 3744 | * the association shared key. |
| 3745 | */ |
| 3746 | static int sctp_setsockopt_active_key(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3747 | char __user *optval, |
| 3748 | unsigned int optlen) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3749 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3750 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3751 | struct sctp_association *asoc; |
Xin Long | bf9fb6a | 2019-01-28 15:08:40 +0800 | [diff] [blame] | 3752 | struct sctp_authkeyid val; |
| 3753 | int ret = 0; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3754 | |
| 3755 | if (optlen != sizeof(struct sctp_authkeyid)) |
| 3756 | return -EINVAL; |
| 3757 | if (copy_from_user(&val, optval, optlen)) |
| 3758 | return -EFAULT; |
| 3759 | |
| 3760 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); |
Xin Long | bf9fb6a | 2019-01-28 15:08:40 +0800 | [diff] [blame] | 3761 | if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC && |
| 3762 | sctp_style(sk, UDP)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3763 | return -EINVAL; |
| 3764 | |
Xin Long | bf9fb6a | 2019-01-28 15:08:40 +0800 | [diff] [blame] | 3765 | if (asoc) |
| 3766 | return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber); |
| 3767 | |
Xin Long | 06b39e8 | 2019-03-18 20:06:05 +0800 | [diff] [blame] | 3768 | if (sctp_style(sk, TCP)) |
| 3769 | val.scact_assoc_id = SCTP_FUTURE_ASSOC; |
| 3770 | |
Xin Long | bf9fb6a | 2019-01-28 15:08:40 +0800 | [diff] [blame] | 3771 | if (val.scact_assoc_id == SCTP_FUTURE_ASSOC || |
| 3772 | val.scact_assoc_id == SCTP_ALL_ASSOC) { |
| 3773 | ret = sctp_auth_set_active_key(ep, asoc, val.scact_keynumber); |
| 3774 | if (ret) |
| 3775 | return ret; |
| 3776 | } |
| 3777 | |
| 3778 | if (val.scact_assoc_id == SCTP_CURRENT_ASSOC || |
| 3779 | val.scact_assoc_id == SCTP_ALL_ASSOC) { |
| 3780 | list_for_each_entry(asoc, &ep->asocs, asocs) { |
| 3781 | int res = sctp_auth_set_active_key(ep, asoc, |
| 3782 | val.scact_keynumber); |
| 3783 | |
| 3784 | if (res && !ret) |
| 3785 | ret = res; |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | return ret; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3790 | } |
| 3791 | |
| 3792 | /* |
| 3793 | * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY) |
| 3794 | * |
| 3795 | * This set option will delete a shared secret key from use. |
| 3796 | */ |
| 3797 | static int sctp_setsockopt_del_key(struct sock *sk, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 3798 | char __user *optval, |
| 3799 | unsigned int optlen) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3800 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 3801 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3802 | struct sctp_association *asoc; |
Xin Long | 3adcc30 | 2019-01-28 15:08:41 +0800 | [diff] [blame] | 3803 | struct sctp_authkeyid val; |
| 3804 | int ret = 0; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3805 | |
| 3806 | if (optlen != sizeof(struct sctp_authkeyid)) |
| 3807 | return -EINVAL; |
| 3808 | if (copy_from_user(&val, optval, optlen)) |
| 3809 | return -EFAULT; |
| 3810 | |
| 3811 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); |
Xin Long | 3adcc30 | 2019-01-28 15:08:41 +0800 | [diff] [blame] | 3812 | if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC && |
| 3813 | sctp_style(sk, UDP)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3814 | return -EINVAL; |
| 3815 | |
Xin Long | 3adcc30 | 2019-01-28 15:08:41 +0800 | [diff] [blame] | 3816 | if (asoc) |
| 3817 | return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3818 | |
Xin Long | 220675e | 2019-03-18 20:06:06 +0800 | [diff] [blame] | 3819 | if (sctp_style(sk, TCP)) |
| 3820 | val.scact_assoc_id = SCTP_FUTURE_ASSOC; |
| 3821 | |
Xin Long | 3adcc30 | 2019-01-28 15:08:41 +0800 | [diff] [blame] | 3822 | if (val.scact_assoc_id == SCTP_FUTURE_ASSOC || |
| 3823 | val.scact_assoc_id == SCTP_ALL_ASSOC) { |
| 3824 | ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber); |
| 3825 | if (ret) |
| 3826 | return ret; |
| 3827 | } |
| 3828 | |
| 3829 | if (val.scact_assoc_id == SCTP_CURRENT_ASSOC || |
| 3830 | val.scact_assoc_id == SCTP_ALL_ASSOC) { |
| 3831 | list_for_each_entry(asoc, &ep->asocs, asocs) { |
| 3832 | int res = sctp_auth_del_key_id(ep, asoc, |
| 3833 | val.scact_keynumber); |
| 3834 | |
| 3835 | if (res && !ret) |
| 3836 | ret = res; |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | return ret; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 3841 | } |
| 3842 | |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3843 | /* |
Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 3844 | * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY) |
| 3845 | * |
| 3846 | * This set option will deactivate a shared secret key. |
| 3847 | */ |
| 3848 | static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval, |
| 3849 | unsigned int optlen) |
| 3850 | { |
| 3851 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 3852 | struct sctp_association *asoc; |
Xin Long | 2af66ff | 2019-01-28 15:08:42 +0800 | [diff] [blame] | 3853 | struct sctp_authkeyid val; |
| 3854 | int ret = 0; |
Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 3855 | |
Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 3856 | if (optlen != sizeof(struct sctp_authkeyid)) |
| 3857 | return -EINVAL; |
| 3858 | if (copy_from_user(&val, optval, optlen)) |
| 3859 | return -EFAULT; |
| 3860 | |
| 3861 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); |
Xin Long | 2af66ff | 2019-01-28 15:08:42 +0800 | [diff] [blame] | 3862 | if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC && |
| 3863 | sctp_style(sk, UDP)) |
Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 3864 | return -EINVAL; |
| 3865 | |
Xin Long | 2af66ff | 2019-01-28 15:08:42 +0800 | [diff] [blame] | 3866 | if (asoc) |
| 3867 | return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber); |
| 3868 | |
Xin Long | 200f3a3b | 2019-03-18 20:06:07 +0800 | [diff] [blame] | 3869 | if (sctp_style(sk, TCP)) |
| 3870 | val.scact_assoc_id = SCTP_FUTURE_ASSOC; |
| 3871 | |
Xin Long | 2af66ff | 2019-01-28 15:08:42 +0800 | [diff] [blame] | 3872 | if (val.scact_assoc_id == SCTP_FUTURE_ASSOC || |
| 3873 | val.scact_assoc_id == SCTP_ALL_ASSOC) { |
| 3874 | ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber); |
| 3875 | if (ret) |
| 3876 | return ret; |
| 3877 | } |
| 3878 | |
| 3879 | if (val.scact_assoc_id == SCTP_CURRENT_ASSOC || |
| 3880 | val.scact_assoc_id == SCTP_ALL_ASSOC) { |
| 3881 | list_for_each_entry(asoc, &ep->asocs, asocs) { |
| 3882 | int res = sctp_auth_deact_key_id(ep, asoc, |
| 3883 | val.scact_keynumber); |
| 3884 | |
| 3885 | if (res && !ret) |
| 3886 | ret = res; |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | return ret; |
Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | /* |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3894 | * 8.1.23 SCTP_AUTO_ASCONF |
| 3895 | * |
| 3896 | * This option will enable or disable the use of the automatic generation of |
| 3897 | * ASCONF chunks to add and delete addresses to an existing association. Note |
| 3898 | * that this option has two caveats namely: a) it only affects sockets that |
| 3899 | * are bound to all addresses available to the SCTP stack, and b) the system |
| 3900 | * administrator may have an overriding control that turns the ASCONF feature |
| 3901 | * off no matter what setting the socket option may have. |
| 3902 | * This option expects an integer boolean flag, where a non-zero value turns on |
| 3903 | * the option, and a zero value turns off the option. |
| 3904 | * Note. In this implementation, socket operation overrides default parameter |
| 3905 | * being set by sysctl as well as FreeBSD implementation |
| 3906 | */ |
| 3907 | static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval, |
| 3908 | unsigned int optlen) |
| 3909 | { |
| 3910 | int val; |
| 3911 | struct sctp_sock *sp = sctp_sk(sk); |
| 3912 | |
| 3913 | if (optlen < sizeof(int)) |
| 3914 | return -EINVAL; |
| 3915 | if (get_user(val, (int __user *)optval)) |
| 3916 | return -EFAULT; |
| 3917 | if (!sctp_is_ep_boundall(sk) && val) |
| 3918 | return -EINVAL; |
| 3919 | if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf)) |
| 3920 | return 0; |
| 3921 | |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 3922 | spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock); |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3923 | if (val == 0 && sp->do_auto_asconf) { |
| 3924 | list_del(&sp->auto_asconf_list); |
| 3925 | sp->do_auto_asconf = 0; |
| 3926 | } else if (val && !sp->do_auto_asconf) { |
| 3927 | list_add_tail(&sp->auto_asconf_list, |
Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 3928 | &sock_net(sk)->sctp.auto_asconf_splist); |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3929 | sp->do_auto_asconf = 1; |
| 3930 | } |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 3931 | spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock); |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 3932 | return 0; |
| 3933 | } |
| 3934 | |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3935 | /* |
| 3936 | * SCTP_PEER_ADDR_THLDS |
| 3937 | * |
| 3938 | * This option allows us to alter the partially failed threshold for one or all |
| 3939 | * transports in an association. See Section 6.1 of: |
| 3940 | * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt |
| 3941 | */ |
| 3942 | static int sctp_setsockopt_paddr_thresholds(struct sock *sk, |
| 3943 | char __user *optval, |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 3944 | unsigned int optlen, bool v2) |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3945 | { |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 3946 | struct sctp_paddrthlds_v2 val; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3947 | struct sctp_transport *trans; |
| 3948 | struct sctp_association *asoc; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 3949 | int len; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3950 | |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 3951 | len = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds); |
| 3952 | if (optlen < len) |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3953 | return -EINVAL; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 3954 | if (copy_from_user(&val, optval, len)) |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3955 | return -EFAULT; |
| 3956 | |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 3957 | if (v2 && val.spt_pathpfthld > val.spt_pathcpthld) |
| 3958 | return -EINVAL; |
| 3959 | |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 3960 | if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { |
| 3961 | trans = sctp_addr_id2transport(sk, &val.spt_address, |
| 3962 | val.spt_assoc_id); |
| 3963 | if (!trans) |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3964 | return -ENOENT; |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 3965 | |
| 3966 | if (val.spt_pathmaxrxt) |
| 3967 | trans->pathmaxrxt = val.spt_pathmaxrxt; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 3968 | if (v2) |
| 3969 | trans->ps_retrans = val.spt_pathcpthld; |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 3970 | trans->pf_retrans = val.spt_pathpfthld; |
| 3971 | |
| 3972 | return 0; |
| 3973 | } |
| 3974 | |
| 3975 | asoc = sctp_id2assoc(sk, val.spt_assoc_id); |
| 3976 | if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC && |
| 3977 | sctp_style(sk, UDP)) |
| 3978 | return -EINVAL; |
| 3979 | |
| 3980 | if (asoc) { |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3981 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, |
| 3982 | transports) { |
| 3983 | if (val.spt_pathmaxrxt) |
| 3984 | trans->pathmaxrxt = val.spt_pathmaxrxt; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 3985 | if (v2) |
| 3986 | trans->ps_retrans = val.spt_pathcpthld; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3987 | trans->pf_retrans = val.spt_pathpfthld; |
| 3988 | } |
| 3989 | |
| 3990 | if (val.spt_pathmaxrxt) |
| 3991 | asoc->pathmaxrxt = val.spt_pathmaxrxt; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 3992 | if (v2) |
| 3993 | asoc->ps_retrans = val.spt_pathcpthld; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3994 | asoc->pf_retrans = val.spt_pathpfthld; |
| 3995 | } else { |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 3996 | struct sctp_sock *sp = sctp_sk(sk); |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 3997 | |
| 3998 | if (val.spt_pathmaxrxt) |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 3999 | sp->pathmaxrxt = val.spt_pathmaxrxt; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 4000 | if (v2) |
| 4001 | sp->ps_retrans = val.spt_pathcpthld; |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 4002 | sp->pf_retrans = val.spt_pathpfthld; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | return 0; |
| 4006 | } |
| 4007 | |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 4008 | static int sctp_setsockopt_recvrcvinfo(struct sock *sk, |
| 4009 | char __user *optval, |
| 4010 | unsigned int optlen) |
| 4011 | { |
| 4012 | int val; |
| 4013 | |
| 4014 | if (optlen < sizeof(int)) |
| 4015 | return -EINVAL; |
| 4016 | if (get_user(val, (int __user *) optval)) |
| 4017 | return -EFAULT; |
| 4018 | |
| 4019 | sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1; |
| 4020 | |
| 4021 | return 0; |
| 4022 | } |
| 4023 | |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 4024 | static int sctp_setsockopt_recvnxtinfo(struct sock *sk, |
| 4025 | char __user *optval, |
| 4026 | unsigned int optlen) |
| 4027 | { |
| 4028 | int val; |
| 4029 | |
| 4030 | if (optlen < sizeof(int)) |
| 4031 | return -EINVAL; |
| 4032 | if (get_user(val, (int __user *) optval)) |
| 4033 | return -EFAULT; |
| 4034 | |
| 4035 | sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1; |
| 4036 | |
| 4037 | return 0; |
| 4038 | } |
| 4039 | |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 4040 | static int sctp_setsockopt_pr_supported(struct sock *sk, |
| 4041 | char __user *optval, |
| 4042 | unsigned int optlen) |
| 4043 | { |
| 4044 | struct sctp_assoc_value params; |
Xin Long | fb19560 | 2019-01-28 15:08:30 +0800 | [diff] [blame] | 4045 | struct sctp_association *asoc; |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 4046 | |
| 4047 | if (optlen != sizeof(params)) |
Xin Long | cc3ccf2 | 2018-11-18 15:21:53 +0800 | [diff] [blame] | 4048 | return -EINVAL; |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 4049 | |
Xin Long | cc3ccf2 | 2018-11-18 15:21:53 +0800 | [diff] [blame] | 4050 | if (copy_from_user(¶ms, optval, optlen)) |
| 4051 | return -EFAULT; |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 4052 | |
Xin Long | fb19560 | 2019-01-28 15:08:30 +0800 | [diff] [blame] | 4053 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 4054 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 4055 | sctp_style(sk, UDP)) |
| 4056 | return -EINVAL; |
| 4057 | |
Xin Long | cc3ccf2 | 2018-11-18 15:21:53 +0800 | [diff] [blame] | 4058 | sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value; |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 4059 | |
Xin Long | cc3ccf2 | 2018-11-18 15:21:53 +0800 | [diff] [blame] | 4060 | return 0; |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 4061 | } |
| 4062 | |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 4063 | static int sctp_setsockopt_default_prinfo(struct sock *sk, |
| 4064 | char __user *optval, |
| 4065 | unsigned int optlen) |
| 4066 | { |
Xin Long | 3a58305 | 2019-01-28 15:08:43 +0800 | [diff] [blame] | 4067 | struct sctp_sock *sp = sctp_sk(sk); |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 4068 | struct sctp_default_prinfo info; |
| 4069 | struct sctp_association *asoc; |
| 4070 | int retval = -EINVAL; |
| 4071 | |
| 4072 | if (optlen != sizeof(info)) |
| 4073 | goto out; |
| 4074 | |
| 4075 | if (copy_from_user(&info, optval, sizeof(info))) { |
| 4076 | retval = -EFAULT; |
| 4077 | goto out; |
| 4078 | } |
| 4079 | |
| 4080 | if (info.pr_policy & ~SCTP_PR_SCTP_MASK) |
| 4081 | goto out; |
| 4082 | |
| 4083 | if (info.pr_policy == SCTP_PR_SCTP_NONE) |
| 4084 | info.pr_value = 0; |
| 4085 | |
| 4086 | asoc = sctp_id2assoc(sk, info.pr_assoc_id); |
Xin Long | 3a58305 | 2019-01-28 15:08:43 +0800 | [diff] [blame] | 4087 | if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC && |
| 4088 | sctp_style(sk, UDP)) |
| 4089 | goto out; |
| 4090 | |
| 4091 | retval = 0; |
| 4092 | |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 4093 | if (asoc) { |
| 4094 | SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy); |
| 4095 | asoc->default_timetolive = info.pr_value; |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 4096 | goto out; |
| 4097 | } |
| 4098 | |
Xin Long | cbb45c6 | 2019-03-18 20:06:08 +0800 | [diff] [blame] | 4099 | if (sctp_style(sk, TCP)) |
| 4100 | info.pr_assoc_id = SCTP_FUTURE_ASSOC; |
| 4101 | |
Xin Long | 3a58305 | 2019-01-28 15:08:43 +0800 | [diff] [blame] | 4102 | if (info.pr_assoc_id == SCTP_FUTURE_ASSOC || |
| 4103 | info.pr_assoc_id == SCTP_ALL_ASSOC) { |
| 4104 | SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy); |
| 4105 | sp->default_timetolive = info.pr_value; |
| 4106 | } |
| 4107 | |
| 4108 | if (info.pr_assoc_id == SCTP_CURRENT_ASSOC || |
| 4109 | info.pr_assoc_id == SCTP_ALL_ASSOC) { |
| 4110 | list_for_each_entry(asoc, &sp->ep->asocs, asocs) { |
| 4111 | SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy); |
| 4112 | asoc->default_timetolive = info.pr_value; |
| 4113 | } |
| 4114 | } |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 4115 | |
| 4116 | out: |
| 4117 | return retval; |
| 4118 | } |
| 4119 | |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 4120 | static int sctp_setsockopt_reconfig_supported(struct sock *sk, |
| 4121 | char __user *optval, |
| 4122 | unsigned int optlen) |
| 4123 | { |
| 4124 | struct sctp_assoc_value params; |
| 4125 | struct sctp_association *asoc; |
| 4126 | int retval = -EINVAL; |
| 4127 | |
| 4128 | if (optlen != sizeof(params)) |
| 4129 | goto out; |
| 4130 | |
| 4131 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4132 | retval = -EFAULT; |
| 4133 | goto out; |
| 4134 | } |
| 4135 | |
| 4136 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | acce7f3 | 2019-01-28 15:08:31 +0800 | [diff] [blame] | 4137 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 4138 | sctp_style(sk, UDP)) |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 4139 | goto out; |
Xin Long | acce7f3 | 2019-01-28 15:08:31 +0800 | [diff] [blame] | 4140 | |
Xin Long | a96701f | 2019-07-09 00:57:04 +0800 | [diff] [blame] | 4141 | sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value; |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 4142 | |
| 4143 | retval = 0; |
| 4144 | |
| 4145 | out: |
| 4146 | return retval; |
| 4147 | } |
| 4148 | |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 4149 | static int sctp_setsockopt_enable_strreset(struct sock *sk, |
| 4150 | char __user *optval, |
| 4151 | unsigned int optlen) |
| 4152 | { |
Xin Long | 99a6213 | 2019-01-28 15:08:44 +0800 | [diff] [blame] | 4153 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 4154 | struct sctp_assoc_value params; |
| 4155 | struct sctp_association *asoc; |
| 4156 | int retval = -EINVAL; |
| 4157 | |
| 4158 | if (optlen != sizeof(params)) |
| 4159 | goto out; |
| 4160 | |
| 4161 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4162 | retval = -EFAULT; |
| 4163 | goto out; |
| 4164 | } |
| 4165 | |
| 4166 | if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK)) |
| 4167 | goto out; |
| 4168 | |
| 4169 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | 99a6213 | 2019-01-28 15:08:44 +0800 | [diff] [blame] | 4170 | if (!asoc && params.assoc_id > SCTP_ALL_ASSOC && |
| 4171 | sctp_style(sk, UDP)) |
| 4172 | goto out; |
| 4173 | |
| 4174 | retval = 0; |
| 4175 | |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 4176 | if (asoc) { |
| 4177 | asoc->strreset_enable = params.assoc_value; |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 4178 | goto out; |
| 4179 | } |
| 4180 | |
Xin Long | 9430ff9 | 2019-03-18 20:06:09 +0800 | [diff] [blame] | 4181 | if (sctp_style(sk, TCP)) |
| 4182 | params.assoc_id = SCTP_FUTURE_ASSOC; |
| 4183 | |
Xin Long | 99a6213 | 2019-01-28 15:08:44 +0800 | [diff] [blame] | 4184 | if (params.assoc_id == SCTP_FUTURE_ASSOC || |
| 4185 | params.assoc_id == SCTP_ALL_ASSOC) |
| 4186 | ep->strreset_enable = params.assoc_value; |
| 4187 | |
| 4188 | if (params.assoc_id == SCTP_CURRENT_ASSOC || |
| 4189 | params.assoc_id == SCTP_ALL_ASSOC) |
| 4190 | list_for_each_entry(asoc, &ep->asocs, asocs) |
| 4191 | asoc->strreset_enable = params.assoc_value; |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 4192 | |
| 4193 | out: |
| 4194 | return retval; |
| 4195 | } |
| 4196 | |
Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4197 | static int sctp_setsockopt_reset_streams(struct sock *sk, |
| 4198 | char __user *optval, |
| 4199 | unsigned int optlen) |
| 4200 | { |
| 4201 | struct sctp_reset_streams *params; |
| 4202 | struct sctp_association *asoc; |
| 4203 | int retval = -EINVAL; |
| 4204 | |
Xin Long | 2342b8d | 2017-12-10 15:40:51 +0800 | [diff] [blame] | 4205 | if (optlen < sizeof(*params)) |
Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4206 | return -EINVAL; |
Marcelo Ricardo Leitner | 5960cef | 2018-01-08 19:02:28 -0200 | [diff] [blame] | 4207 | /* srs_number_streams is u16, so optlen can't be bigger than this. */ |
| 4208 | optlen = min_t(unsigned int, optlen, USHRT_MAX + |
| 4209 | sizeof(__u16) * sizeof(*params)); |
Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4210 | |
| 4211 | params = memdup_user(optval, optlen); |
| 4212 | if (IS_ERR(params)) |
| 4213 | return PTR_ERR(params); |
| 4214 | |
Xin Long | 2342b8d | 2017-12-10 15:40:51 +0800 | [diff] [blame] | 4215 | if (params->srs_number_streams * sizeof(__u16) > |
| 4216 | optlen - sizeof(*params)) |
| 4217 | goto out; |
| 4218 | |
Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4219 | asoc = sctp_id2assoc(sk, params->srs_assoc_id); |
| 4220 | if (!asoc) |
| 4221 | goto out; |
| 4222 | |
| 4223 | retval = sctp_send_reset_streams(asoc, params); |
| 4224 | |
| 4225 | out: |
| 4226 | kfree(params); |
| 4227 | return retval; |
| 4228 | } |
| 4229 | |
Xin Long | a92ce1a | 2017-02-09 01:18:18 +0800 | [diff] [blame] | 4230 | static int sctp_setsockopt_reset_assoc(struct sock *sk, |
| 4231 | char __user *optval, |
| 4232 | unsigned int optlen) |
| 4233 | { |
| 4234 | struct sctp_association *asoc; |
| 4235 | sctp_assoc_t associd; |
| 4236 | int retval = -EINVAL; |
| 4237 | |
| 4238 | if (optlen != sizeof(associd)) |
| 4239 | goto out; |
| 4240 | |
| 4241 | if (copy_from_user(&associd, optval, optlen)) { |
| 4242 | retval = -EFAULT; |
| 4243 | goto out; |
| 4244 | } |
| 4245 | |
| 4246 | asoc = sctp_id2assoc(sk, associd); |
| 4247 | if (!asoc) |
| 4248 | goto out; |
| 4249 | |
| 4250 | retval = sctp_send_reset_assoc(asoc); |
| 4251 | |
| 4252 | out: |
| 4253 | return retval; |
| 4254 | } |
| 4255 | |
Xin Long | 242bd2d | 2017-02-09 01:18:20 +0800 | [diff] [blame] | 4256 | static int sctp_setsockopt_add_streams(struct sock *sk, |
| 4257 | char __user *optval, |
| 4258 | unsigned int optlen) |
| 4259 | { |
| 4260 | struct sctp_association *asoc; |
| 4261 | struct sctp_add_streams params; |
| 4262 | int retval = -EINVAL; |
| 4263 | |
| 4264 | if (optlen != sizeof(params)) |
| 4265 | goto out; |
| 4266 | |
| 4267 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4268 | retval = -EFAULT; |
| 4269 | goto out; |
| 4270 | } |
| 4271 | |
| 4272 | asoc = sctp_id2assoc(sk, params.sas_assoc_id); |
| 4273 | if (!asoc) |
| 4274 | goto out; |
| 4275 | |
| 4276 | retval = sctp_send_add_streams(asoc, ¶ms); |
| 4277 | |
| 4278 | out: |
| 4279 | return retval; |
| 4280 | } |
| 4281 | |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4282 | static int sctp_setsockopt_scheduler(struct sock *sk, |
| 4283 | char __user *optval, |
| 4284 | unsigned int optlen) |
| 4285 | { |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 4286 | struct sctp_sock *sp = sctp_sk(sk); |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4287 | struct sctp_association *asoc; |
| 4288 | struct sctp_assoc_value params; |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 4289 | int retval = 0; |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4290 | |
| 4291 | if (optlen < sizeof(params)) |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 4292 | return -EINVAL; |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4293 | |
| 4294 | optlen = sizeof(params); |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 4295 | if (copy_from_user(¶ms, optval, optlen)) |
| 4296 | return -EFAULT; |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4297 | |
| 4298 | if (params.assoc_value > SCTP_SS_MAX) |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 4299 | return -EINVAL; |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4300 | |
| 4301 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 4302 | if (!asoc && params.assoc_id > SCTP_ALL_ASSOC && |
| 4303 | sctp_style(sk, UDP)) |
| 4304 | return -EINVAL; |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4305 | |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 4306 | if (asoc) |
| 4307 | return sctp_sched_set_sched(asoc, params.assoc_value); |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4308 | |
Xin Long | b59c19d | 2019-03-18 20:06:11 +0800 | [diff] [blame] | 4309 | if (sctp_style(sk, TCP)) |
| 4310 | params.assoc_id = SCTP_FUTURE_ASSOC; |
| 4311 | |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 4312 | if (params.assoc_id == SCTP_FUTURE_ASSOC || |
| 4313 | params.assoc_id == SCTP_ALL_ASSOC) |
| 4314 | sp->default_ss = params.assoc_value; |
| 4315 | |
| 4316 | if (params.assoc_id == SCTP_CURRENT_ASSOC || |
| 4317 | params.assoc_id == SCTP_ALL_ASSOC) { |
| 4318 | list_for_each_entry(asoc, &sp->ep->asocs, asocs) { |
| 4319 | int ret = sctp_sched_set_sched(asoc, |
| 4320 | params.assoc_value); |
| 4321 | |
| 4322 | if (ret && !retval) |
| 4323 | retval = ret; |
| 4324 | } |
| 4325 | } |
| 4326 | |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4327 | return retval; |
| 4328 | } |
| 4329 | |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 4330 | static int sctp_setsockopt_scheduler_value(struct sock *sk, |
| 4331 | char __user *optval, |
| 4332 | unsigned int optlen) |
| 4333 | { |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 4334 | struct sctp_stream_value params; |
Xin Long | e7f2824 | 2019-01-28 15:08:33 +0800 | [diff] [blame] | 4335 | struct sctp_association *asoc; |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 4336 | int retval = -EINVAL; |
| 4337 | |
| 4338 | if (optlen < sizeof(params)) |
| 4339 | goto out; |
| 4340 | |
| 4341 | optlen = sizeof(params); |
| 4342 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4343 | retval = -EFAULT; |
| 4344 | goto out; |
| 4345 | } |
| 4346 | |
| 4347 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | e7f2824 | 2019-01-28 15:08:33 +0800 | [diff] [blame] | 4348 | if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC && |
| 4349 | sctp_style(sk, UDP)) |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 4350 | goto out; |
| 4351 | |
Xin Long | e7f2824 | 2019-01-28 15:08:33 +0800 | [diff] [blame] | 4352 | if (asoc) { |
| 4353 | retval = sctp_sched_set_value(asoc, params.stream_id, |
| 4354 | params.stream_value, GFP_KERNEL); |
| 4355 | goto out; |
| 4356 | } |
| 4357 | |
| 4358 | retval = 0; |
| 4359 | |
| 4360 | list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) { |
| 4361 | int ret = sctp_sched_set_value(asoc, params.stream_id, |
| 4362 | params.stream_value, GFP_KERNEL); |
| 4363 | if (ret && !retval) /* try to return the 1st error. */ |
| 4364 | retval = ret; |
| 4365 | } |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 4366 | |
| 4367 | out: |
| 4368 | return retval; |
| 4369 | } |
| 4370 | |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4371 | static int sctp_setsockopt_interleaving_supported(struct sock *sk, |
| 4372 | char __user *optval, |
| 4373 | unsigned int optlen) |
| 4374 | { |
| 4375 | struct sctp_sock *sp = sctp_sk(sk); |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4376 | struct sctp_assoc_value params; |
Xin Long | 2e7709d | 2019-01-28 15:08:32 +0800 | [diff] [blame] | 4377 | struct sctp_association *asoc; |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4378 | int retval = -EINVAL; |
| 4379 | |
| 4380 | if (optlen < sizeof(params)) |
| 4381 | goto out; |
| 4382 | |
| 4383 | optlen = sizeof(params); |
| 4384 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4385 | retval = -EFAULT; |
| 4386 | goto out; |
| 4387 | } |
| 4388 | |
Xin Long | 2e7709d | 2019-01-28 15:08:32 +0800 | [diff] [blame] | 4389 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 4390 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 4391 | sctp_style(sk, UDP)) |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4392 | goto out; |
| 4393 | |
Xin Long | 2e7709d | 2019-01-28 15:08:32 +0800 | [diff] [blame] | 4394 | if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) { |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4395 | retval = -EPERM; |
| 4396 | goto out; |
| 4397 | } |
| 4398 | |
Xin Long | e55f4b8 | 2019-07-09 00:57:07 +0800 | [diff] [blame] | 4399 | sp->ep->intl_enable = !!params.assoc_value; |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4400 | |
| 4401 | retval = 0; |
| 4402 | |
| 4403 | out: |
| 4404 | return retval; |
| 4405 | } |
| 4406 | |
Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 4407 | static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval, |
| 4408 | unsigned int optlen) |
| 4409 | { |
| 4410 | int val; |
| 4411 | |
| 4412 | if (!sctp_style(sk, TCP)) |
| 4413 | return -EOPNOTSUPP; |
| 4414 | |
| 4415 | if (sctp_sk(sk)->ep->base.bind_addr.port) |
| 4416 | return -EFAULT; |
| 4417 | |
| 4418 | if (optlen < sizeof(int)) |
| 4419 | return -EINVAL; |
| 4420 | |
| 4421 | if (get_user(val, (int __user *)optval)) |
| 4422 | return -EFAULT; |
| 4423 | |
| 4424 | sctp_sk(sk)->reuse = !!val; |
| 4425 | |
| 4426 | return 0; |
| 4427 | } |
| 4428 | |
Xin Long | d251f05 | 2019-01-28 15:08:45 +0800 | [diff] [blame] | 4429 | static int sctp_assoc_ulpevent_type_set(struct sctp_event *param, |
| 4430 | struct sctp_association *asoc) |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 4431 | { |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 4432 | struct sctp_ulpevent *event; |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 4433 | |
Xin Long | d251f05 | 2019-01-28 15:08:45 +0800 | [diff] [blame] | 4434 | sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on); |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 4435 | |
Xin Long | d251f05 | 2019-01-28 15:08:45 +0800 | [diff] [blame] | 4436 | if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) { |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 4437 | if (sctp_outq_is_empty(&asoc->outqueue)) { |
| 4438 | event = sctp_ulpevent_make_sender_dry_event(asoc, |
| 4439 | GFP_USER | __GFP_NOWARN); |
Xin Long | d251f05 | 2019-01-28 15:08:45 +0800 | [diff] [blame] | 4440 | if (!event) |
| 4441 | return -ENOMEM; |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 4442 | |
| 4443 | asoc->stream.si->enqueue_event(&asoc->ulpq, event); |
| 4444 | } |
| 4445 | } |
| 4446 | |
Xin Long | d251f05 | 2019-01-28 15:08:45 +0800 | [diff] [blame] | 4447 | return 0; |
| 4448 | } |
| 4449 | |
| 4450 | static int sctp_setsockopt_event(struct sock *sk, char __user *optval, |
| 4451 | unsigned int optlen) |
| 4452 | { |
| 4453 | struct sctp_sock *sp = sctp_sk(sk); |
| 4454 | struct sctp_association *asoc; |
| 4455 | struct sctp_event param; |
| 4456 | int retval = 0; |
| 4457 | |
| 4458 | if (optlen < sizeof(param)) |
| 4459 | return -EINVAL; |
| 4460 | |
| 4461 | optlen = sizeof(param); |
| 4462 | if (copy_from_user(¶m, optval, optlen)) |
| 4463 | return -EFAULT; |
| 4464 | |
| 4465 | if (param.se_type < SCTP_SN_TYPE_BASE || |
| 4466 | param.se_type > SCTP_SN_TYPE_MAX) |
| 4467 | return -EINVAL; |
| 4468 | |
| 4469 | asoc = sctp_id2assoc(sk, param.se_assoc_id); |
| 4470 | if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC && |
| 4471 | sctp_style(sk, UDP)) |
| 4472 | return -EINVAL; |
| 4473 | |
| 4474 | if (asoc) |
| 4475 | return sctp_assoc_ulpevent_type_set(¶m, asoc); |
| 4476 | |
Xin Long | 9951861 | 2019-03-18 20:06:10 +0800 | [diff] [blame] | 4477 | if (sctp_style(sk, TCP)) |
| 4478 | param.se_assoc_id = SCTP_FUTURE_ASSOC; |
| 4479 | |
Xin Long | d251f05 | 2019-01-28 15:08:45 +0800 | [diff] [blame] | 4480 | if (param.se_assoc_id == SCTP_FUTURE_ASSOC || |
| 4481 | param.se_assoc_id == SCTP_ALL_ASSOC) |
| 4482 | sctp_ulpevent_type_set(&sp->subscribe, |
| 4483 | param.se_type, param.se_on); |
| 4484 | |
| 4485 | if (param.se_assoc_id == SCTP_CURRENT_ASSOC || |
| 4486 | param.se_assoc_id == SCTP_ALL_ASSOC) { |
| 4487 | list_for_each_entry(asoc, &sp->ep->asocs, asocs) { |
| 4488 | int ret = sctp_assoc_ulpevent_type_set(¶m, asoc); |
| 4489 | |
| 4490 | if (ret && !retval) |
| 4491 | retval = ret; |
| 4492 | } |
| 4493 | } |
| 4494 | |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 4495 | return retval; |
| 4496 | } |
| 4497 | |
Xin Long | df2c71f | 2019-08-19 22:02:46 +0800 | [diff] [blame] | 4498 | static int sctp_setsockopt_asconf_supported(struct sock *sk, |
| 4499 | char __user *optval, |
| 4500 | unsigned int optlen) |
| 4501 | { |
| 4502 | struct sctp_assoc_value params; |
| 4503 | struct sctp_association *asoc; |
| 4504 | struct sctp_endpoint *ep; |
| 4505 | int retval = -EINVAL; |
| 4506 | |
| 4507 | if (optlen != sizeof(params)) |
| 4508 | goto out; |
| 4509 | |
| 4510 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4511 | retval = -EFAULT; |
| 4512 | goto out; |
| 4513 | } |
| 4514 | |
| 4515 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 4516 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 4517 | sctp_style(sk, UDP)) |
| 4518 | goto out; |
| 4519 | |
| 4520 | ep = sctp_sk(sk)->ep; |
| 4521 | ep->asconf_enable = !!params.assoc_value; |
| 4522 | |
| 4523 | if (ep->asconf_enable && ep->auth_enable) { |
| 4524 | sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF); |
| 4525 | sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK); |
| 4526 | } |
| 4527 | |
| 4528 | retval = 0; |
| 4529 | |
| 4530 | out: |
| 4531 | return retval; |
| 4532 | } |
| 4533 | |
Xin Long | 56dd525 | 2019-08-19 22:02:49 +0800 | [diff] [blame] | 4534 | static int sctp_setsockopt_auth_supported(struct sock *sk, |
| 4535 | char __user *optval, |
| 4536 | unsigned int optlen) |
| 4537 | { |
| 4538 | struct sctp_assoc_value params; |
| 4539 | struct sctp_association *asoc; |
| 4540 | struct sctp_endpoint *ep; |
| 4541 | int retval = -EINVAL; |
| 4542 | |
| 4543 | if (optlen != sizeof(params)) |
| 4544 | goto out; |
| 4545 | |
| 4546 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4547 | retval = -EFAULT; |
| 4548 | goto out; |
| 4549 | } |
| 4550 | |
| 4551 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 4552 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 4553 | sctp_style(sk, UDP)) |
| 4554 | goto out; |
| 4555 | |
| 4556 | ep = sctp_sk(sk)->ep; |
| 4557 | if (params.assoc_value) { |
| 4558 | retval = sctp_auth_init(ep, GFP_KERNEL); |
| 4559 | if (retval) |
| 4560 | goto out; |
| 4561 | if (ep->asconf_enable) { |
| 4562 | sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF); |
| 4563 | sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK); |
| 4564 | } |
| 4565 | } |
| 4566 | |
| 4567 | ep->auth_enable = !!params.assoc_value; |
| 4568 | retval = 0; |
| 4569 | |
| 4570 | out: |
| 4571 | return retval; |
| 4572 | } |
| 4573 | |
Xin Long | d5886b9 | 2019-08-26 16:30:04 +0800 | [diff] [blame] | 4574 | static int sctp_setsockopt_ecn_supported(struct sock *sk, |
| 4575 | char __user *optval, |
| 4576 | unsigned int optlen) |
| 4577 | { |
| 4578 | struct sctp_assoc_value params; |
| 4579 | struct sctp_association *asoc; |
| 4580 | int retval = -EINVAL; |
| 4581 | |
| 4582 | if (optlen != sizeof(params)) |
| 4583 | goto out; |
| 4584 | |
| 4585 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4586 | retval = -EFAULT; |
| 4587 | goto out; |
| 4588 | } |
| 4589 | |
| 4590 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 4591 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 4592 | sctp_style(sk, UDP)) |
| 4593 | goto out; |
| 4594 | |
| 4595 | sctp_sk(sk)->ep->ecn_enable = !!params.assoc_value; |
| 4596 | retval = 0; |
| 4597 | |
| 4598 | out: |
| 4599 | return retval; |
| 4600 | } |
| 4601 | |
Xin Long | 8d2a693 | 2019-11-08 13:20:34 +0800 | [diff] [blame] | 4602 | static int sctp_setsockopt_pf_expose(struct sock *sk, |
| 4603 | char __user *optval, |
| 4604 | unsigned int optlen) |
| 4605 | { |
| 4606 | struct sctp_assoc_value params; |
| 4607 | struct sctp_association *asoc; |
| 4608 | int retval = -EINVAL; |
| 4609 | |
| 4610 | if (optlen != sizeof(params)) |
| 4611 | goto out; |
| 4612 | |
| 4613 | if (copy_from_user(¶ms, optval, optlen)) { |
| 4614 | retval = -EFAULT; |
| 4615 | goto out; |
| 4616 | } |
| 4617 | |
| 4618 | if (params.assoc_value > SCTP_PF_EXPOSE_MAX) |
| 4619 | goto out; |
| 4620 | |
| 4621 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 4622 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 4623 | sctp_style(sk, UDP)) |
| 4624 | goto out; |
| 4625 | |
| 4626 | if (asoc) |
| 4627 | asoc->pf_expose = params.assoc_value; |
| 4628 | else |
| 4629 | sctp_sk(sk)->pf_expose = params.assoc_value; |
| 4630 | retval = 0; |
| 4631 | |
| 4632 | out: |
| 4633 | return retval; |
| 4634 | } |
| 4635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | /* API 6.2 setsockopt(), getsockopt() |
| 4637 | * |
| 4638 | * Applications use setsockopt() and getsockopt() to set or retrieve |
| 4639 | * socket options. Socket options are used to change the default |
| 4640 | * behavior of sockets calls. They are described in Section 7. |
| 4641 | * |
| 4642 | * The syntax is: |
| 4643 | * |
| 4644 | * ret = getsockopt(int sd, int level, int optname, void __user *optval, |
| 4645 | * int __user *optlen); |
| 4646 | * ret = setsockopt(int sd, int level, int optname, const void __user *optval, |
| 4647 | * int optlen); |
| 4648 | * |
| 4649 | * sd - the socket descript. |
| 4650 | * level - set to IPPROTO_SCTP for all SCTP options. |
| 4651 | * optname - the option name. |
| 4652 | * optval - the buffer to store the value of the option. |
| 4653 | * optlen - the size of the buffer. |
| 4654 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4655 | static int sctp_setsockopt(struct sock *sk, int level, int optname, |
| 4656 | char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | { |
| 4658 | int retval = 0; |
| 4659 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4660 | pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | |
| 4662 | /* I can hardly begin to describe how wrong this is. This is |
| 4663 | * so broken as to be worse than useless. The API draft |
| 4664 | * REALLY is NOT helpful here... I am not convinced that the |
| 4665 | * semantics of setsockopt() with a level OTHER THAN SOL_SCTP |
| 4666 | * are at all well-founded. |
| 4667 | */ |
| 4668 | if (level != SOL_SCTP) { |
| 4669 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
| 4670 | retval = af->setsockopt(sk, level, optname, optval, optlen); |
| 4671 | goto out_nounlock; |
| 4672 | } |
| 4673 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4674 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | |
| 4676 | switch (optname) { |
| 4677 | case SCTP_SOCKOPT_BINDX_ADD: |
| 4678 | /* 'optlen' is the size of the addresses buffer. */ |
| 4679 | retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, |
| 4680 | optlen, SCTP_BINDX_ADD_ADDR); |
| 4681 | break; |
| 4682 | |
| 4683 | case SCTP_SOCKOPT_BINDX_REM: |
| 4684 | /* 'optlen' is the size of the addresses buffer. */ |
| 4685 | retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, |
| 4686 | optlen, SCTP_BINDX_REM_ADDR); |
| 4687 | break; |
| 4688 | |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 4689 | case SCTP_SOCKOPT_CONNECTX_OLD: |
| 4690 | /* 'optlen' is the size of the addresses buffer. */ |
| 4691 | retval = sctp_setsockopt_connectx_old(sk, |
| 4692 | (struct sockaddr __user *)optval, |
| 4693 | optlen); |
| 4694 | break; |
| 4695 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4696 | case SCTP_SOCKOPT_CONNECTX: |
| 4697 | /* 'optlen' is the size of the addresses buffer. */ |
Vlad Yasevich | 88a0a94 | 2008-05-09 15:14:11 -0700 | [diff] [blame] | 4698 | retval = sctp_setsockopt_connectx(sk, |
| 4699 | (struct sockaddr __user *)optval, |
| 4700 | optlen); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4701 | break; |
| 4702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | case SCTP_DISABLE_FRAGMENTS: |
| 4704 | retval = sctp_setsockopt_disable_fragments(sk, optval, optlen); |
| 4705 | break; |
| 4706 | |
| 4707 | case SCTP_EVENTS: |
| 4708 | retval = sctp_setsockopt_events(sk, optval, optlen); |
| 4709 | break; |
| 4710 | |
| 4711 | case SCTP_AUTOCLOSE: |
| 4712 | retval = sctp_setsockopt_autoclose(sk, optval, optlen); |
| 4713 | break; |
| 4714 | |
| 4715 | case SCTP_PEER_ADDR_PARAMS: |
| 4716 | retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen); |
| 4717 | break; |
| 4718 | |
Shan Wei | 4580ccc | 2011-01-18 22:39:00 +0000 | [diff] [blame] | 4719 | case SCTP_DELAYED_SACK: |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 4720 | retval = sctp_setsockopt_delayed_ack(sk, optval, optlen); |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4721 | break; |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 4722 | case SCTP_PARTIAL_DELIVERY_POINT: |
| 4723 | retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen); |
| 4724 | break; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 4725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | case SCTP_INITMSG: |
| 4727 | retval = sctp_setsockopt_initmsg(sk, optval, optlen); |
| 4728 | break; |
| 4729 | case SCTP_DEFAULT_SEND_PARAM: |
| 4730 | retval = sctp_setsockopt_default_send_param(sk, optval, |
| 4731 | optlen); |
| 4732 | break; |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 4733 | case SCTP_DEFAULT_SNDINFO: |
| 4734 | retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen); |
| 4735 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | case SCTP_PRIMARY_ADDR: |
| 4737 | retval = sctp_setsockopt_primary_addr(sk, optval, optlen); |
| 4738 | break; |
| 4739 | case SCTP_SET_PEER_PRIMARY_ADDR: |
| 4740 | retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen); |
| 4741 | break; |
| 4742 | case SCTP_NODELAY: |
| 4743 | retval = sctp_setsockopt_nodelay(sk, optval, optlen); |
| 4744 | break; |
| 4745 | case SCTP_RTOINFO: |
| 4746 | retval = sctp_setsockopt_rtoinfo(sk, optval, optlen); |
| 4747 | break; |
| 4748 | case SCTP_ASSOCINFO: |
| 4749 | retval = sctp_setsockopt_associnfo(sk, optval, optlen); |
| 4750 | break; |
| 4751 | case SCTP_I_WANT_MAPPED_V4_ADDR: |
| 4752 | retval = sctp_setsockopt_mappedv4(sk, optval, optlen); |
| 4753 | break; |
| 4754 | case SCTP_MAXSEG: |
| 4755 | retval = sctp_setsockopt_maxseg(sk, optval, optlen); |
| 4756 | break; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 4757 | case SCTP_ADAPTATION_LAYER: |
| 4758 | retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4759 | break; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 4760 | case SCTP_CONTEXT: |
| 4761 | retval = sctp_setsockopt_context(sk, optval, optlen); |
| 4762 | break; |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 4763 | case SCTP_FRAGMENT_INTERLEAVE: |
| 4764 | retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen); |
| 4765 | break; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 4766 | case SCTP_MAX_BURST: |
| 4767 | retval = sctp_setsockopt_maxburst(sk, optval, optlen); |
| 4768 | break; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 4769 | case SCTP_AUTH_CHUNK: |
| 4770 | retval = sctp_setsockopt_auth_chunk(sk, optval, optlen); |
| 4771 | break; |
| 4772 | case SCTP_HMAC_IDENT: |
| 4773 | retval = sctp_setsockopt_hmac_ident(sk, optval, optlen); |
| 4774 | break; |
| 4775 | case SCTP_AUTH_KEY: |
| 4776 | retval = sctp_setsockopt_auth_key(sk, optval, optlen); |
| 4777 | break; |
| 4778 | case SCTP_AUTH_ACTIVE_KEY: |
| 4779 | retval = sctp_setsockopt_active_key(sk, optval, optlen); |
| 4780 | break; |
| 4781 | case SCTP_AUTH_DELETE_KEY: |
| 4782 | retval = sctp_setsockopt_del_key(sk, optval, optlen); |
| 4783 | break; |
Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 4784 | case SCTP_AUTH_DEACTIVATE_KEY: |
| 4785 | retval = sctp_setsockopt_deactivate_key(sk, optval, optlen); |
| 4786 | break; |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 4787 | case SCTP_AUTO_ASCONF: |
| 4788 | retval = sctp_setsockopt_auto_asconf(sk, optval, optlen); |
| 4789 | break; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 4790 | case SCTP_PEER_ADDR_THLDS: |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 4791 | retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen, |
| 4792 | false); |
| 4793 | break; |
| 4794 | case SCTP_PEER_ADDR_THLDS_V2: |
| 4795 | retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen, |
| 4796 | true); |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 4797 | break; |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 4798 | case SCTP_RECVRCVINFO: |
| 4799 | retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen); |
| 4800 | break; |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 4801 | case SCTP_RECVNXTINFO: |
| 4802 | retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen); |
| 4803 | break; |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 4804 | case SCTP_PR_SUPPORTED: |
| 4805 | retval = sctp_setsockopt_pr_supported(sk, optval, optlen); |
| 4806 | break; |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 4807 | case SCTP_DEFAULT_PRINFO: |
| 4808 | retval = sctp_setsockopt_default_prinfo(sk, optval, optlen); |
| 4809 | break; |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 4810 | case SCTP_RECONFIG_SUPPORTED: |
| 4811 | retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen); |
| 4812 | break; |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 4813 | case SCTP_ENABLE_STREAM_RESET: |
| 4814 | retval = sctp_setsockopt_enable_strreset(sk, optval, optlen); |
| 4815 | break; |
Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 4816 | case SCTP_RESET_STREAMS: |
| 4817 | retval = sctp_setsockopt_reset_streams(sk, optval, optlen); |
| 4818 | break; |
Xin Long | a92ce1a | 2017-02-09 01:18:18 +0800 | [diff] [blame] | 4819 | case SCTP_RESET_ASSOC: |
| 4820 | retval = sctp_setsockopt_reset_assoc(sk, optval, optlen); |
| 4821 | break; |
Xin Long | 242bd2d | 2017-02-09 01:18:20 +0800 | [diff] [blame] | 4822 | case SCTP_ADD_STREAMS: |
| 4823 | retval = sctp_setsockopt_add_streams(sk, optval, optlen); |
| 4824 | break; |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 4825 | case SCTP_STREAM_SCHEDULER: |
| 4826 | retval = sctp_setsockopt_scheduler(sk, optval, optlen); |
| 4827 | break; |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 4828 | case SCTP_STREAM_SCHEDULER_VALUE: |
| 4829 | retval = sctp_setsockopt_scheduler_value(sk, optval, optlen); |
| 4830 | break; |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 4831 | case SCTP_INTERLEAVING_SUPPORTED: |
| 4832 | retval = sctp_setsockopt_interleaving_supported(sk, optval, |
| 4833 | optlen); |
| 4834 | break; |
Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 4835 | case SCTP_REUSE_PORT: |
| 4836 | retval = sctp_setsockopt_reuse_port(sk, optval, optlen); |
| 4837 | break; |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 4838 | case SCTP_EVENT: |
| 4839 | retval = sctp_setsockopt_event(sk, optval, optlen); |
| 4840 | break; |
Xin Long | df2c71f | 2019-08-19 22:02:46 +0800 | [diff] [blame] | 4841 | case SCTP_ASCONF_SUPPORTED: |
| 4842 | retval = sctp_setsockopt_asconf_supported(sk, optval, optlen); |
| 4843 | break; |
Xin Long | 56dd525 | 2019-08-19 22:02:49 +0800 | [diff] [blame] | 4844 | case SCTP_AUTH_SUPPORTED: |
| 4845 | retval = sctp_setsockopt_auth_supported(sk, optval, optlen); |
| 4846 | break; |
Xin Long | d5886b9 | 2019-08-26 16:30:04 +0800 | [diff] [blame] | 4847 | case SCTP_ECN_SUPPORTED: |
| 4848 | retval = sctp_setsockopt_ecn_supported(sk, optval, optlen); |
| 4849 | break; |
Xin Long | 8d2a693 | 2019-11-08 13:20:34 +0800 | [diff] [blame] | 4850 | case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE: |
| 4851 | retval = sctp_setsockopt_pf_expose(sk, optval, optlen); |
| 4852 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | default: |
| 4854 | retval = -ENOPROTOOPT; |
| 4855 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 4856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4858 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | |
| 4860 | out_nounlock: |
| 4861 | return retval; |
| 4862 | } |
| 4863 | |
| 4864 | /* API 3.1.6 connect() - UDP Style Syntax |
| 4865 | * |
| 4866 | * An application may use the connect() call in the UDP model to initiate an |
| 4867 | * association without sending data. |
| 4868 | * |
| 4869 | * The syntax is: |
| 4870 | * |
| 4871 | * ret = connect(int sd, const struct sockaddr *nam, socklen_t len); |
| 4872 | * |
| 4873 | * sd: the socket descriptor to have a new association added to. |
| 4874 | * |
| 4875 | * nam: the address structure (either struct sockaddr_in or struct |
| 4876 | * sockaddr_in6 defined in RFC2553 [7]). |
| 4877 | * |
| 4878 | * len: the size of the address. |
| 4879 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4880 | static int sctp_connect(struct sock *sk, struct sockaddr *addr, |
Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 4881 | int addr_len, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | { |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4883 | struct sctp_af *af; |
Xin Long | 9b6c088 | 2019-06-26 16:31:39 +0800 | [diff] [blame] | 4884 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4886 | lock_sock(sk); |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 4887 | pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk, |
| 4888 | addr, addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 4890 | /* Validate addr_len before calling common connect/connectx routine. */ |
Xin Long | 9b6c088 | 2019-06-26 16:31:39 +0800 | [diff] [blame] | 4891 | af = sctp_get_af_specific(addr->sa_family); |
| 4892 | if (af && addr_len >= af->sockaddr_len) |
Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 4893 | err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4895 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | return err; |
| 4897 | } |
| 4898 | |
Xin Long | 644fbde | 2018-05-20 16:39:10 +0800 | [diff] [blame] | 4899 | int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr, |
| 4900 | int addr_len, int flags) |
| 4901 | { |
| 4902 | if (addr_len < sizeof(uaddr->sa_family)) |
| 4903 | return -EINVAL; |
| 4904 | |
| 4905 | if (uaddr->sa_family == AF_UNSPEC) |
| 4906 | return -EOPNOTSUPP; |
| 4907 | |
| 4908 | return sctp_connect(sock->sk, uaddr, addr_len, flags); |
| 4909 | } |
| 4910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | /* FIXME: Write comments. */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4912 | static int sctp_disconnect(struct sock *sk, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4913 | { |
| 4914 | return -EOPNOTSUPP; /* STUB */ |
| 4915 | } |
| 4916 | |
| 4917 | /* 4.1.4 accept() - TCP Style Syntax |
| 4918 | * |
| 4919 | * Applications use accept() call to remove an established SCTP |
| 4920 | * association from the accept queue of the endpoint. A new socket |
| 4921 | * descriptor will be returned from accept() to represent the newly |
| 4922 | * formed association. |
| 4923 | */ |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 4924 | 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] | 4925 | { |
| 4926 | struct sctp_sock *sp; |
| 4927 | struct sctp_endpoint *ep; |
| 4928 | struct sock *newsk = NULL; |
| 4929 | struct sctp_association *asoc; |
| 4930 | long timeo; |
| 4931 | int error = 0; |
| 4932 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4933 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | |
| 4935 | sp = sctp_sk(sk); |
| 4936 | ep = sp->ep; |
| 4937 | |
| 4938 | if (!sctp_style(sk, TCP)) { |
| 4939 | error = -EOPNOTSUPP; |
| 4940 | goto out; |
| 4941 | } |
| 4942 | |
| 4943 | if (!sctp_sstate(sk, LISTENING)) { |
| 4944 | error = -EINVAL; |
| 4945 | goto out; |
| 4946 | } |
| 4947 | |
Sridhar Samudrala | 8abfedd | 2006-08-22 00:24:09 -0700 | [diff] [blame] | 4948 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | |
| 4950 | error = sctp_wait_for_accept(sk, timeo); |
| 4951 | if (error) |
| 4952 | goto out; |
| 4953 | |
| 4954 | /* We treat the list of associations on the endpoint as the accept |
| 4955 | * queue and pick the first association on the list. |
| 4956 | */ |
| 4957 | asoc = list_entry(ep->asocs.next, struct sctp_association, asocs); |
| 4958 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 4959 | newsk = sp->pf->create_accept_sk(sk, asoc, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | if (!newsk) { |
| 4961 | error = -ENOMEM; |
| 4962 | goto out; |
| 4963 | } |
| 4964 | |
| 4965 | /* Populate the fields of the newsk from the oldsk and migrate the |
| 4966 | * asoc to the newsk. |
| 4967 | */ |
Xin Long | 89664c62 | 2019-03-03 17:54:53 +0800 | [diff] [blame] | 4968 | error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP); |
| 4969 | if (error) { |
| 4970 | sk_common_release(newsk); |
| 4971 | newsk = NULL; |
| 4972 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | |
| 4974 | out: |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4975 | release_sock(sk); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 4976 | *err = error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | return newsk; |
| 4978 | } |
| 4979 | |
| 4980 | /* The SCTP ioctl handler. */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 4981 | static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | { |
Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4983 | int rc = -ENOTCONN; |
| 4984 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 4985 | lock_sock(sk); |
Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 4986 | |
| 4987 | /* |
| 4988 | * SEQPACKET-style sockets in LISTENING state are valid, for |
| 4989 | * SCTP, so only discard TCP-style sockets in LISTENING state. |
| 4990 | */ |
| 4991 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) |
| 4992 | goto out; |
| 4993 | |
| 4994 | switch (cmd) { |
| 4995 | case SIOCINQ: { |
| 4996 | struct sk_buff *skb; |
| 4997 | unsigned int amount = 0; |
| 4998 | |
| 4999 | skb = skb_peek(&sk->sk_receive_queue); |
| 5000 | if (skb != NULL) { |
| 5001 | /* |
| 5002 | * We will only return the amount of this packet since |
| 5003 | * that is all that will be read. |
| 5004 | */ |
| 5005 | amount = skb->len; |
| 5006 | } |
| 5007 | rc = put_user(amount, (int __user *)arg); |
Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 5008 | break; |
David S. Miller | 9a7241c | 2010-10-03 22:14:37 -0700 | [diff] [blame] | 5009 | } |
Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 5010 | default: |
| 5011 | rc = -ENOIOCTLCMD; |
| 5012 | break; |
| 5013 | } |
| 5014 | out: |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 5015 | release_sock(sk); |
Diego Elio 'Flameeyes' Pettenò | 65040c3 | 2010-09-03 03:47:03 +0000 | [diff] [blame] | 5016 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | /* This is the function which gets called during socket creation to |
| 5020 | * initialized the SCTP-specific portion of the sock. |
| 5021 | * The sock structure should already be zero-filled memory. |
| 5022 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 5023 | static int sctp_init_sock(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5024 | { |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5025 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | struct sctp_sock *sp; |
| 5027 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 5028 | pr_debug("%s: sk:%p\n", __func__, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | |
| 5030 | sp = sctp_sk(sk); |
| 5031 | |
| 5032 | /* Initialize the SCTP per socket area. */ |
| 5033 | switch (sk->sk_type) { |
| 5034 | case SOCK_SEQPACKET: |
| 5035 | sp->type = SCTP_SOCKET_UDP; |
| 5036 | break; |
| 5037 | case SOCK_STREAM: |
| 5038 | sp->type = SCTP_SOCKET_TCP; |
| 5039 | break; |
| 5040 | default: |
| 5041 | return -ESOCKTNOSUPPORT; |
| 5042 | } |
| 5043 | |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 5044 | sk->sk_gso_type = SKB_GSO_SCTP; |
| 5045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | /* Initialize default send parameters. These parameters can be |
| 5047 | * modified with the SCTP_DEFAULT_SEND_PARAM socket option. |
| 5048 | */ |
| 5049 | sp->default_stream = 0; |
| 5050 | sp->default_ppid = 0; |
| 5051 | sp->default_flags = 0; |
| 5052 | sp->default_context = 0; |
| 5053 | sp->default_timetolive = 0; |
| 5054 | |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 5055 | sp->default_rcv_context = 0; |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5056 | sp->max_burst = net->sctp.max_burst; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 5057 | |
Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 5058 | sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg; |
| 5059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | /* Initialize default setup parameters. These parameters |
| 5061 | * can be modified with the SCTP_INITMSG socket option or |
| 5062 | * overridden by the SCTP_INIT CMSG. |
| 5063 | */ |
| 5064 | sp->initmsg.sinit_num_ostreams = sctp_max_outstreams; |
| 5065 | sp->initmsg.sinit_max_instreams = sctp_max_instreams; |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5066 | sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init; |
| 5067 | sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | |
| 5069 | /* Initialize default RTO related parameters. These parameters can |
| 5070 | * be modified for with the SCTP_RTOINFO socket option. |
| 5071 | */ |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5072 | sp->rtoinfo.srto_initial = net->sctp.rto_initial; |
| 5073 | sp->rtoinfo.srto_max = net->sctp.rto_max; |
| 5074 | sp->rtoinfo.srto_min = net->sctp.rto_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | |
| 5076 | /* Initialize default association related parameters. These parameters |
| 5077 | * can be modified with the SCTP_ASSOCINFO socket option. |
| 5078 | */ |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5079 | sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | sp->assocparams.sasoc_number_peer_destinations = 0; |
| 5081 | sp->assocparams.sasoc_peer_rwnd = 0; |
| 5082 | sp->assocparams.sasoc_local_rwnd = 0; |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5083 | sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5084 | |
| 5085 | /* Initialize default event subscriptions. By default, all the |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5086 | * options are off. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | */ |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 5088 | sp->subscribe = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | |
| 5090 | /* Default Peer Address Parameters. These defaults can |
| 5091 | * be modified via SCTP_PEER_ADDR_PARAMS |
| 5092 | */ |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5093 | sp->hbinterval = net->sctp.hb_interval; |
| 5094 | sp->pathmaxrxt = net->sctp.max_retrans_path; |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 5095 | sp->pf_retrans = net->sctp.pf_retrans; |
Xin Long | 34515e9 | 2019-11-08 13:20:35 +0800 | [diff] [blame] | 5096 | sp->ps_retrans = net->sctp.ps_retrans; |
Xin Long | aef587b | 2019-11-08 13:20:32 +0800 | [diff] [blame] | 5097 | sp->pf_expose = net->sctp.pf_expose; |
wangweidong | 4e2d52b | 2013-12-23 12:16:54 +0800 | [diff] [blame] | 5098 | sp->pathmtu = 0; /* allow default discovery */ |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5099 | sp->sackdelay = net->sctp.sack_timeout; |
Vlad Yasevich | 7bfe8bdb | 2008-06-09 15:45:05 -0700 | [diff] [blame] | 5100 | sp->sackfreq = 2; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5101 | sp->param_flags = SPP_HB_ENABLE | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 5102 | SPP_PMTUD_ENABLE | |
| 5103 | SPP_SACKDELAY_ENABLE; |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 5104 | sp->default_ss = SCTP_SS_DEFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | |
| 5106 | /* If enabled no SCTP message fragmentation will be performed. |
| 5107 | * Configure through SCTP_DISABLE_FRAGMENTS socket option. |
| 5108 | */ |
| 5109 | sp->disable_fragments = 0; |
| 5110 | |
Sridhar Samudrala | 208edef | 2006-09-29 17:08:01 -0700 | [diff] [blame] | 5111 | /* Enable Nagle algorithm by default. */ |
| 5112 | sp->nodelay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 5114 | sp->recvrcvinfo = 0; |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 5115 | sp->recvnxtinfo = 0; |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 5116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | /* Enable by default. */ |
| 5118 | sp->v4mapped = 1; |
| 5119 | |
| 5120 | /* Auto-close idle associations after the configured |
| 5121 | * number of seconds. A value of 0 disables this |
| 5122 | * feature. Configure through the SCTP_AUTOCLOSE socket option, |
| 5123 | * for UDP-style sockets only. |
| 5124 | */ |
| 5125 | sp->autoclose = 0; |
| 5126 | |
| 5127 | /* User specified fragmentation limit. */ |
| 5128 | sp->user_frag = 0; |
| 5129 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 5130 | sp->adaptation_ind = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | |
| 5132 | sp->pf = sctp_get_pf_specific(sk->sk_family); |
| 5133 | |
| 5134 | /* Control variables for partial data delivery. */ |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 5135 | atomic_set(&sp->pd_mode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | skb_queue_head_init(&sp->pd_lobby); |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 5137 | sp->frag_interleave = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | |
| 5139 | /* Create a per socket endpoint structure. Even if we |
| 5140 | * change the data structure relationships, this may still |
| 5141 | * be useful for storing pre-connect address information. |
| 5142 | */ |
Daniel Borkmann | c164b83 | 2013-06-14 18:24:06 +0200 | [diff] [blame] | 5143 | sp->ep = sctp_endpoint_new(sk, GFP_KERNEL); |
| 5144 | if (!sp->ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | return -ENOMEM; |
| 5146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | sp->hmac = NULL; |
| 5148 | |
Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 5149 | sk->sk_destruct = sctp_destruct_sock; |
| 5150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | SCTP_DBG_OBJCNT_INC(sock); |
David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 5152 | |
| 5153 | local_bh_disable(); |
Tonghao Zhang | 8cb38a6 | 2017-12-22 10:15:20 -0800 | [diff] [blame] | 5154 | sk_sockets_allocated_inc(sk); |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5155 | sock_prot_inuse_add(net, sk->sk_prot, 1); |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 5156 | |
| 5157 | /* Nothing can fail after this block, otherwise |
| 5158 | * sctp_destroy_sock() will be called without addr_wq_lock held |
| 5159 | */ |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5160 | if (net->sctp.default_auto_asconf) { |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 5161 | spin_lock(&sock_net(sk)->sctp.addr_wq_lock); |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 5162 | list_add_tail(&sp->auto_asconf_list, |
Eric W. Biederman | e1fc3b1 | 2012-08-07 07:29:57 +0000 | [diff] [blame] | 5163 | &net->sctp.auto_asconf_splist); |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 5164 | sp->do_auto_asconf = 1; |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 5165 | spin_unlock(&sock_net(sk)->sctp.addr_wq_lock); |
| 5166 | } else { |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 5167 | sp->do_auto_asconf = 0; |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 5168 | } |
| 5169 | |
David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 5170 | local_bh_enable(); |
| 5171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | return 0; |
| 5173 | } |
| 5174 | |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 5175 | /* Cleanup any SCTP per socket resources. Must be called with |
| 5176 | * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true |
| 5177 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 5178 | static void sctp_destroy_sock(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | { |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 5180 | struct sctp_sock *sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 5182 | pr_debug("%s: sk:%p\n", __func__, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | |
| 5184 | /* Release our hold on the endpoint. */ |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 5185 | sp = sctp_sk(sk); |
Daniel Borkmann | 1abd165 | 2013-06-06 15:53:47 +0200 | [diff] [blame] | 5186 | /* This could happen during socket init, thus we bail out |
| 5187 | * early, since the rest of the below is not setup either. |
| 5188 | */ |
| 5189 | if (sp->ep == NULL) |
| 5190 | return; |
| 5191 | |
Michio Honda | 9f7d653 | 2011-04-26 19:32:51 +0900 | [diff] [blame] | 5192 | if (sp->do_auto_asconf) { |
| 5193 | sp->do_auto_asconf = 0; |
| 5194 | list_del(&sp->auto_asconf_list); |
| 5195 | } |
| 5196 | sctp_endpoint_free(sp->ep); |
Eric Dumazet | 5bc0b3b | 2008-11-25 13:53:27 -0800 | [diff] [blame] | 5197 | local_bh_disable(); |
Tonghao Zhang | 8cb38a6 | 2017-12-22 10:15:20 -0800 | [diff] [blame] | 5198 | sk_sockets_allocated_dec(sk); |
Eric Dumazet | 9a57f7f | 2008-11-17 02:41:00 -0800 | [diff] [blame] | 5199 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
Eric Dumazet | 5bc0b3b | 2008-11-25 13:53:27 -0800 | [diff] [blame] | 5200 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | } |
| 5202 | |
Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 5203 | /* Triggered when there are no references on the socket anymore */ |
| 5204 | static void sctp_destruct_sock(struct sock *sk) |
| 5205 | { |
| 5206 | struct sctp_sock *sp = sctp_sk(sk); |
| 5207 | |
| 5208 | /* Free up the HMAC transform. */ |
Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 5209 | crypto_free_shash(sp->hmac); |
Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 5210 | |
| 5211 | inet_sock_destruct(sk); |
| 5212 | } |
| 5213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | /* API 4.1.7 shutdown() - TCP Style Syntax |
| 5215 | * int shutdown(int socket, int how); |
| 5216 | * |
| 5217 | * sd - the socket descriptor of the association to be closed. |
| 5218 | * how - Specifies the type of shutdown. The values are |
| 5219 | * as follows: |
| 5220 | * SHUT_RD |
| 5221 | * Disables further receive operations. No SCTP |
| 5222 | * protocol action is taken. |
| 5223 | * SHUT_WR |
| 5224 | * Disables further send operations, and initiates |
| 5225 | * the SCTP shutdown sequence. |
| 5226 | * SHUT_RDWR |
| 5227 | * Disables further send and receive operations |
| 5228 | * and initiates the SCTP shutdown sequence. |
| 5229 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 5230 | static void sctp_shutdown(struct sock *sk, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | { |
Eric W. Biederman | 55e26eb | 2012-08-07 07:25:24 +0000 | [diff] [blame] | 5232 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | struct sctp_endpoint *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | |
| 5235 | if (!sctp_style(sk, TCP)) |
| 5236 | return; |
| 5237 | |
Xin Long | 5bf35dd | 2016-11-13 21:44:37 +0800 | [diff] [blame] | 5238 | ep = sctp_sk(sk)->ep; |
| 5239 | if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) { |
| 5240 | struct sctp_association *asoc; |
| 5241 | |
Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 5242 | inet_sk_set_state(sk, SCTP_SS_CLOSING); |
Xin Long | 5bf35dd | 2016-11-13 21:44:37 +0800 | [diff] [blame] | 5243 | asoc = list_entry(ep->asocs.next, |
| 5244 | struct sctp_association, asocs); |
| 5245 | sctp_primitive_SHUTDOWN(net, asoc, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | } |
| 5247 | } |
| 5248 | |
Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 5249 | int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc, |
| 5250 | struct sctp_info *info) |
| 5251 | { |
| 5252 | struct sctp_transport *prim; |
| 5253 | struct list_head *pos; |
| 5254 | int mask; |
| 5255 | |
| 5256 | memset(info, 0, sizeof(*info)); |
| 5257 | if (!asoc) { |
| 5258 | struct sctp_sock *sp = sctp_sk(sk); |
| 5259 | |
| 5260 | info->sctpi_s_autoclose = sp->autoclose; |
| 5261 | info->sctpi_s_adaptation_ind = sp->adaptation_ind; |
| 5262 | info->sctpi_s_pd_point = sp->pd_point; |
| 5263 | info->sctpi_s_nodelay = sp->nodelay; |
| 5264 | info->sctpi_s_disable_fragments = sp->disable_fragments; |
| 5265 | info->sctpi_s_v4mapped = sp->v4mapped; |
| 5266 | info->sctpi_s_frag_interleave = sp->frag_interleave; |
Xin Long | 40eb90e9 | 2016-05-29 17:42:13 +0800 | [diff] [blame] | 5267 | info->sctpi_s_type = sp->type; |
Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 5268 | |
| 5269 | return 0; |
| 5270 | } |
| 5271 | |
| 5272 | info->sctpi_tag = asoc->c.my_vtag; |
| 5273 | info->sctpi_state = asoc->state; |
| 5274 | info->sctpi_rwnd = asoc->a_rwnd; |
| 5275 | info->sctpi_unackdata = asoc->unack_data; |
| 5276 | info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); |
Xin Long | cee360a | 2017-05-31 16:36:31 +0800 | [diff] [blame] | 5277 | info->sctpi_instrms = asoc->stream.incnt; |
| 5278 | info->sctpi_outstrms = asoc->stream.outcnt; |
Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 5279 | list_for_each(pos, &asoc->base.inqueue.in_chunk_list) |
| 5280 | info->sctpi_inqueue++; |
| 5281 | list_for_each(pos, &asoc->outqueue.out_chunk_list) |
| 5282 | info->sctpi_outqueue++; |
| 5283 | info->sctpi_overall_error = asoc->overall_error_count; |
| 5284 | info->sctpi_max_burst = asoc->max_burst; |
| 5285 | info->sctpi_maxseg = asoc->frag_point; |
| 5286 | info->sctpi_peer_rwnd = asoc->peer.rwnd; |
| 5287 | info->sctpi_peer_tag = asoc->c.peer_vtag; |
| 5288 | |
| 5289 | mask = asoc->peer.ecn_capable << 1; |
| 5290 | mask = (mask | asoc->peer.ipv4_address) << 1; |
| 5291 | mask = (mask | asoc->peer.ipv6_address) << 1; |
| 5292 | mask = (mask | asoc->peer.hostname_address) << 1; |
| 5293 | mask = (mask | asoc->peer.asconf_capable) << 1; |
| 5294 | mask = (mask | asoc->peer.prsctp_capable) << 1; |
| 5295 | mask = (mask | asoc->peer.auth_capable); |
| 5296 | info->sctpi_peer_capable = mask; |
| 5297 | mask = asoc->peer.sack_needed << 1; |
| 5298 | mask = (mask | asoc->peer.sack_generation) << 1; |
| 5299 | mask = (mask | asoc->peer.zero_window_announced); |
| 5300 | info->sctpi_peer_sack = mask; |
| 5301 | |
| 5302 | info->sctpi_isacks = asoc->stats.isacks; |
| 5303 | info->sctpi_osacks = asoc->stats.osacks; |
| 5304 | info->sctpi_opackets = asoc->stats.opackets; |
| 5305 | info->sctpi_ipackets = asoc->stats.ipackets; |
| 5306 | info->sctpi_rtxchunks = asoc->stats.rtxchunks; |
| 5307 | info->sctpi_outofseqtsns = asoc->stats.outofseqtsns; |
| 5308 | info->sctpi_idupchunks = asoc->stats.idupchunks; |
| 5309 | info->sctpi_gapcnt = asoc->stats.gapcnt; |
| 5310 | info->sctpi_ouodchunks = asoc->stats.ouodchunks; |
| 5311 | info->sctpi_iuodchunks = asoc->stats.iuodchunks; |
| 5312 | info->sctpi_oodchunks = asoc->stats.oodchunks; |
| 5313 | info->sctpi_iodchunks = asoc->stats.iodchunks; |
| 5314 | info->sctpi_octrlchunks = asoc->stats.octrlchunks; |
| 5315 | info->sctpi_ictrlchunks = asoc->stats.ictrlchunks; |
| 5316 | |
| 5317 | prim = asoc->peer.primary_path; |
Stefano Brivio | ee6c88b | 2017-08-23 13:27:13 +0200 | [diff] [blame] | 5318 | memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr)); |
Xin Long | 52c52a6 | 2016-04-14 15:35:30 +0800 | [diff] [blame] | 5319 | info->sctpi_p_state = prim->state; |
| 5320 | info->sctpi_p_cwnd = prim->cwnd; |
| 5321 | info->sctpi_p_srtt = prim->srtt; |
| 5322 | info->sctpi_p_rto = jiffies_to_msecs(prim->rto); |
| 5323 | info->sctpi_p_hbinterval = prim->hbinterval; |
| 5324 | info->sctpi_p_pathmaxrxt = prim->pathmaxrxt; |
| 5325 | info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay); |
| 5326 | info->sctpi_p_ssthresh = prim->ssthresh; |
| 5327 | info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked; |
| 5328 | info->sctpi_p_flight_size = prim->flight_size; |
| 5329 | info->sctpi_p_error = prim->error_count; |
| 5330 | |
| 5331 | return 0; |
| 5332 | } |
| 5333 | EXPORT_SYMBOL_GPL(sctp_get_sctp_info); |
| 5334 | |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5335 | /* use callback to avoid exporting the core structure */ |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 5336 | void sctp_transport_walk_start(struct rhashtable_iter *iter) |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5337 | { |
Xin Long | 7fda702 | 2016-11-15 23:23:11 +0800 | [diff] [blame] | 5338 | rhltable_walk_enter(&sctp_transport_hashtable, iter); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5339 | |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 5340 | rhashtable_walk_start(iter); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5341 | } |
| 5342 | |
| 5343 | void sctp_transport_walk_stop(struct rhashtable_iter *iter) |
| 5344 | { |
| 5345 | rhashtable_walk_stop(iter); |
| 5346 | rhashtable_walk_exit(iter); |
| 5347 | } |
| 5348 | |
| 5349 | struct sctp_transport *sctp_transport_get_next(struct net *net, |
| 5350 | struct rhashtable_iter *iter) |
| 5351 | { |
| 5352 | struct sctp_transport *t; |
| 5353 | |
| 5354 | t = rhashtable_walk_next(iter); |
| 5355 | for (; t; t = rhashtable_walk_next(iter)) { |
| 5356 | if (IS_ERR(t)) { |
| 5357 | if (PTR_ERR(t) == -EAGAIN) |
| 5358 | continue; |
| 5359 | break; |
| 5360 | } |
| 5361 | |
Xin Long | bab1be7 | 2018-08-27 18:38:31 +0800 | [diff] [blame] | 5362 | if (!sctp_transport_hold(t)) |
| 5363 | continue; |
| 5364 | |
Xin Long | 4e7696d | 2019-12-09 13:45:18 +0800 | [diff] [blame] | 5365 | if (net_eq(t->asoc->base.net, net) && |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5366 | t->asoc->peer.primary_path == t) |
| 5367 | break; |
Xin Long | bab1be7 | 2018-08-27 18:38:31 +0800 | [diff] [blame] | 5368 | |
| 5369 | sctp_transport_put(t); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | return t; |
| 5373 | } |
| 5374 | |
| 5375 | struct sctp_transport *sctp_transport_get_idx(struct net *net, |
| 5376 | struct rhashtable_iter *iter, |
| 5377 | int pos) |
| 5378 | { |
Xin Long | bab1be7 | 2018-08-27 18:38:31 +0800 | [diff] [blame] | 5379 | struct sctp_transport *t; |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5380 | |
Xin Long | bab1be7 | 2018-08-27 18:38:31 +0800 | [diff] [blame] | 5381 | if (!pos) |
| 5382 | return SEQ_START_TOKEN; |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5383 | |
Xin Long | bab1be7 | 2018-08-27 18:38:31 +0800 | [diff] [blame] | 5384 | while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) { |
| 5385 | if (!--pos) |
| 5386 | break; |
| 5387 | sctp_transport_put(t); |
| 5388 | } |
| 5389 | |
| 5390 | return t; |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5391 | } |
| 5392 | |
| 5393 | int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *), |
| 5394 | void *p) { |
| 5395 | int err = 0; |
| 5396 | int hash = 0; |
| 5397 | struct sctp_ep_common *epb; |
| 5398 | struct sctp_hashbucket *head; |
| 5399 | |
| 5400 | for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize; |
| 5401 | hash++, head++) { |
Xin Long | 581409d | 2017-06-10 14:48:14 +0800 | [diff] [blame] | 5402 | read_lock_bh(&head->lock); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5403 | sctp_for_each_hentry(epb, &head->chain) { |
| 5404 | err = cb(sctp_ep(epb), p); |
| 5405 | if (err) |
| 5406 | break; |
| 5407 | } |
Xin Long | 581409d | 2017-06-10 14:48:14 +0800 | [diff] [blame] | 5408 | read_unlock_bh(&head->lock); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5409 | } |
| 5410 | |
| 5411 | return err; |
| 5412 | } |
| 5413 | EXPORT_SYMBOL_GPL(sctp_for_each_endpoint); |
| 5414 | |
| 5415 | int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *), |
| 5416 | struct net *net, |
| 5417 | const union sctp_addr *laddr, |
| 5418 | const union sctp_addr *paddr, void *p) |
| 5419 | { |
| 5420 | struct sctp_transport *transport; |
Xin Long | 08abb79 | 2016-12-15 23:05:52 +0800 | [diff] [blame] | 5421 | int err; |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5422 | |
| 5423 | rcu_read_lock(); |
| 5424 | transport = sctp_addrs_lookup_transport(net, laddr, paddr); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5425 | rcu_read_unlock(); |
Xin Long | 08abb79 | 2016-12-15 23:05:52 +0800 | [diff] [blame] | 5426 | if (!transport) |
| 5427 | return -ENOENT; |
| 5428 | |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 5429 | err = cb(transport, p); |
Xin Long | cd26da4 | 2016-10-31 20:32:31 +0800 | [diff] [blame] | 5430 | sctp_transport_put(transport); |
Xin Long | 1cceda78 | 2016-09-29 02:55:44 +0800 | [diff] [blame] | 5431 | |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5432 | return err; |
| 5433 | } |
| 5434 | EXPORT_SYMBOL_GPL(sctp_transport_lookup_process); |
| 5435 | |
| 5436 | int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *), |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5437 | int (*cb_done)(struct sctp_transport *, void *), |
| 5438 | struct net *net, int *pos, void *p) { |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5439 | struct rhashtable_iter hti; |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5440 | struct sctp_transport *tsp; |
| 5441 | int ret; |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5442 | |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5443 | again: |
Xin Long | f53d77e | 2018-01-23 18:22:25 +0800 | [diff] [blame] | 5444 | ret = 0; |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 5445 | sctp_transport_walk_start(&hti); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5446 | |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5447 | tsp = sctp_transport_get_idx(net, &hti, *pos + 1); |
| 5448 | for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) { |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5449 | ret = cb(tsp, p); |
| 5450 | if (ret) |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5451 | break; |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5452 | (*pos)++; |
| 5453 | sctp_transport_put(tsp); |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5454 | } |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5455 | sctp_transport_walk_stop(&hti); |
Xin Long | 53fa103 | 2016-04-14 15:35:35 +0800 | [diff] [blame] | 5456 | |
Xin Long | d25adbe | 2017-09-15 11:02:21 +0800 | [diff] [blame] | 5457 | if (ret) { |
| 5458 | if (cb_done && !cb_done(tsp, p)) { |
| 5459 | (*pos)++; |
| 5460 | sctp_transport_put(tsp); |
| 5461 | goto again; |
| 5462 | } |
| 5463 | sctp_transport_put(tsp); |
| 5464 | } |
| 5465 | |
| 5466 | return ret; |
Xin Long | 626d16f | 2016-04-14 15:35:31 +0800 | [diff] [blame] | 5467 | } |
| 5468 | EXPORT_SYMBOL_GPL(sctp_for_each_transport); |
| 5469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | /* 7.2.1 Association Status (SCTP_STATUS) |
| 5471 | |
| 5472 | * Applications can retrieve current status information about an |
| 5473 | * association, including association state, peer receiver window size, |
| 5474 | * number of unacked data chunks, and number of data chunks pending |
| 5475 | * receipt. This information is read-only. |
| 5476 | */ |
| 5477 | static int sctp_getsockopt_sctp_status(struct sock *sk, int len, |
| 5478 | char __user *optval, |
| 5479 | int __user *optlen) |
| 5480 | { |
| 5481 | struct sctp_status status; |
| 5482 | struct sctp_association *asoc = NULL; |
| 5483 | struct sctp_transport *transport; |
| 5484 | sctp_assoc_t associd; |
| 5485 | int retval = 0; |
| 5486 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5487 | if (len < sizeof(status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | retval = -EINVAL; |
| 5489 | goto out; |
| 5490 | } |
| 5491 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5492 | len = sizeof(status); |
| 5493 | if (copy_from_user(&status, optval, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | retval = -EFAULT; |
| 5495 | goto out; |
| 5496 | } |
| 5497 | |
| 5498 | associd = status.sstat_assoc_id; |
| 5499 | asoc = sctp_id2assoc(sk, associd); |
| 5500 | if (!asoc) { |
| 5501 | retval = -EINVAL; |
| 5502 | goto out; |
| 5503 | } |
| 5504 | |
| 5505 | transport = asoc->peer.primary_path; |
| 5506 | |
| 5507 | status.sstat_assoc_id = sctp_assoc2id(asoc); |
Daniel Borkmann | 38ab1fa | 2014-08-28 15:28:26 +0200 | [diff] [blame] | 5508 | status.sstat_state = sctp_assoc_to_state(asoc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | status.sstat_rwnd = asoc->peer.rwnd; |
| 5510 | status.sstat_unackdata = asoc->unack_data; |
| 5511 | |
| 5512 | status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); |
Xin Long | cee360a | 2017-05-31 16:36:31 +0800 | [diff] [blame] | 5513 | status.sstat_instrms = asoc->stream.incnt; |
| 5514 | status.sstat_outstrms = asoc->stream.outcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | status.sstat_fragmentation_point = asoc->frag_point; |
| 5516 | status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); |
Al Viro | 8cec6b8 | 2006-11-20 17:23:01 -0800 | [diff] [blame] | 5517 | memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, |
| 5518 | transport->af_specific->sockaddr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | /* Map ipv4 address into v4-mapped-on-v6 address. */ |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5520 | 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] | 5521 | (union sctp_addr *)&status.sstat_primary.spinfo_address); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 5522 | status.sstat_primary.spinfo_state = transport->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | status.sstat_primary.spinfo_cwnd = transport->cwnd; |
| 5524 | status.sstat_primary.spinfo_srtt = transport->srtt; |
| 5525 | status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5526 | status.sstat_primary.spinfo_mtu = transport->pathmtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 5528 | if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) |
| 5529 | status.sstat_primary.spinfo_state = SCTP_ACTIVE; |
| 5530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5531 | if (put_user(len, optlen)) { |
| 5532 | retval = -EFAULT; |
| 5533 | goto out; |
| 5534 | } |
| 5535 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 5536 | pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n", |
| 5537 | __func__, len, status.sstat_state, status.sstat_rwnd, |
| 5538 | status.sstat_assoc_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | |
| 5540 | if (copy_to_user(optval, &status, len)) { |
| 5541 | retval = -EFAULT; |
| 5542 | goto out; |
| 5543 | } |
| 5544 | |
| 5545 | out: |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 5546 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | } |
| 5548 | |
| 5549 | |
| 5550 | /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) |
| 5551 | * |
| 5552 | * Applications can retrieve information about a specific peer address |
| 5553 | * of an association, including its reachability state, congestion |
| 5554 | * window, and retransmission timer values. This information is |
| 5555 | * read-only. |
| 5556 | */ |
| 5557 | static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, |
| 5558 | char __user *optval, |
| 5559 | int __user *optlen) |
| 5560 | { |
| 5561 | struct sctp_paddrinfo pinfo; |
| 5562 | struct sctp_transport *transport; |
| 5563 | int retval = 0; |
| 5564 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5565 | if (len < sizeof(pinfo)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | retval = -EINVAL; |
| 5567 | goto out; |
| 5568 | } |
| 5569 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5570 | len = sizeof(pinfo); |
| 5571 | if (copy_from_user(&pinfo, optval, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | retval = -EFAULT; |
| 5573 | goto out; |
| 5574 | } |
| 5575 | |
| 5576 | transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, |
| 5577 | pinfo.spinfo_assoc_id); |
Xin Long | aef587b | 2019-11-08 13:20:32 +0800 | [diff] [blame] | 5578 | if (!transport) { |
| 5579 | retval = -EINVAL; |
| 5580 | goto out; |
| 5581 | } |
| 5582 | |
| 5583 | if (transport->state == SCTP_PF && |
| 5584 | transport->asoc->pf_expose == SCTP_PF_EXPOSE_DISABLE) { |
| 5585 | retval = -EACCES; |
| 5586 | goto out; |
| 5587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | |
| 5589 | pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 5590 | pinfo.spinfo_state = transport->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | pinfo.spinfo_cwnd = transport->cwnd; |
| 5592 | pinfo.spinfo_srtt = transport->srtt; |
| 5593 | pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5594 | pinfo.spinfo_mtu = transport->pathmtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 | |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 5596 | if (pinfo.spinfo_state == SCTP_UNKNOWN) |
| 5597 | pinfo.spinfo_state = SCTP_ACTIVE; |
| 5598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | if (put_user(len, optlen)) { |
| 5600 | retval = -EFAULT; |
| 5601 | goto out; |
| 5602 | } |
| 5603 | |
| 5604 | if (copy_to_user(optval, &pinfo, len)) { |
| 5605 | retval = -EFAULT; |
| 5606 | goto out; |
| 5607 | } |
| 5608 | |
| 5609 | out: |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 5610 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | } |
| 5612 | |
| 5613 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) |
| 5614 | * |
| 5615 | * This option is a on/off flag. If enabled no SCTP message |
| 5616 | * fragmentation will be performed. Instead if a message being sent |
| 5617 | * exceeds the current PMTU size, the message will NOT be sent and |
| 5618 | * instead a error will be indicated to the user. |
| 5619 | */ |
| 5620 | static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, |
| 5621 | char __user *optval, int __user *optlen) |
| 5622 | { |
| 5623 | int val; |
| 5624 | |
| 5625 | if (len < sizeof(int)) |
| 5626 | return -EINVAL; |
| 5627 | |
| 5628 | len = sizeof(int); |
| 5629 | val = (sctp_sk(sk)->disable_fragments == 1); |
| 5630 | if (put_user(len, optlen)) |
| 5631 | return -EFAULT; |
| 5632 | if (copy_to_user(optval, &val, len)) |
| 5633 | return -EFAULT; |
| 5634 | return 0; |
| 5635 | } |
| 5636 | |
| 5637 | /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) |
| 5638 | * |
| 5639 | * This socket option is used to specify various notifications and |
| 5640 | * ancillary data the user wishes to receive. |
| 5641 | */ |
| 5642 | static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, |
| 5643 | int __user *optlen) |
| 5644 | { |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 5645 | struct sctp_event_subscribe subscribe; |
| 5646 | __u8 *sn_type = (__u8 *)&subscribe; |
| 5647 | int i; |
| 5648 | |
Jiri Slaby | a4b8e71 | 2016-10-21 14:13:24 +0200 | [diff] [blame] | 5649 | if (len == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | return -EINVAL; |
Thomas Graf | acdd598 | 2012-04-03 22:17:53 +0000 | [diff] [blame] | 5651 | if (len > sizeof(struct sctp_event_subscribe)) |
| 5652 | len = sizeof(struct sctp_event_subscribe); |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5653 | if (put_user(len, optlen)) |
| 5654 | return -EFAULT; |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 5655 | |
| 5656 | for (i = 0; i < len; i++) |
| 5657 | sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe, |
| 5658 | SCTP_SN_TYPE_BASE + i); |
| 5659 | |
| 5660 | if (copy_to_user(optval, &subscribe, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | return -EFAULT; |
Xin Long | 2cc0eeb | 2018-11-18 16:08:51 +0800 | [diff] [blame] | 5662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | return 0; |
| 5664 | } |
| 5665 | |
| 5666 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) |
| 5667 | * |
| 5668 | * This socket option is applicable to the UDP-style socket only. When |
| 5669 | * set it will cause associations that are idle for more than the |
| 5670 | * specified number of seconds to automatically close. An association |
| 5671 | * being idle is defined an association that has NOT sent or received |
| 5672 | * user data. The special value of '0' indicates that no automatic |
| 5673 | * close of any associations should be performed. The option expects an |
| 5674 | * integer defining the number of seconds of idle time before an |
| 5675 | * association is closed. |
| 5676 | */ |
| 5677 | static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) |
| 5678 | { |
| 5679 | /* Applicable to UDP-style socket only */ |
| 5680 | if (sctp_style(sk, TCP)) |
| 5681 | return -EOPNOTSUPP; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5682 | if (len < sizeof(int)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | return -EINVAL; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5684 | len = sizeof(int); |
| 5685 | if (put_user(len, optlen)) |
| 5686 | return -EFAULT; |
David Windsor | b2ce04c | 2017-06-10 22:50:43 -0400 | [diff] [blame] | 5687 | if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | return -EFAULT; |
| 5689 | return 0; |
| 5690 | } |
| 5691 | |
| 5692 | /* Helper routine to branch off an association to a new socket. */ |
Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5693 | 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] | 5694 | { |
Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5695 | struct sctp_association *asoc = sctp_id2assoc(sk, id); |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5696 | struct sctp_sock *sp = sctp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | struct socket *sock; |
| 5698 | int err = 0; |
| 5699 | |
Xin Long | df80cd9 | 2017-10-17 23:26:10 +0800 | [diff] [blame] | 5700 | /* Do not peel off from one netns to another one. */ |
| 5701 | if (!net_eq(current->nsproxy->net_ns, sock_net(sk))) |
| 5702 | return -EINVAL; |
| 5703 | |
Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5704 | if (!asoc) |
| 5705 | return -EINVAL; |
| 5706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | /* An association cannot be branched off from an already peeled-off |
| 5708 | * socket, nor is this supported for tcp style sockets. |
| 5709 | */ |
| 5710 | if (!sctp_style(sk, UDP)) |
| 5711 | return -EINVAL; |
| 5712 | |
| 5713 | /* Create a new socket. */ |
| 5714 | err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); |
| 5715 | if (err < 0) |
| 5716 | return err; |
| 5717 | |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 5718 | sctp_copy_sock(sock->sk, sk, asoc); |
Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 5719 | |
| 5720 | /* Make peeled-off sockets more like 1-1 accepted sockets. |
Richard Haines | b7e10c2 | 2018-02-24 16:18:51 +0000 | [diff] [blame] | 5721 | * Set the daddr and initialize id to something more random and also |
| 5722 | * copy over any ip options. |
Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 5723 | */ |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 5724 | sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk); |
Richard Haines | b7e10c2 | 2018-02-24 16:18:51 +0000 | [diff] [blame] | 5725 | sp->pf->copy_ip_options(sk, sock->sk); |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 5726 | |
| 5727 | /* Populate the fields of the newsk from the oldsk and migrate the |
| 5728 | * asoc to the newsk. |
| 5729 | */ |
Xin Long | 89664c62 | 2019-03-03 17:54:53 +0800 | [diff] [blame] | 5730 | err = sctp_sock_migrate(sk, sock->sk, asoc, |
| 5731 | SCTP_SOCKET_UDP_HIGH_BANDWIDTH); |
| 5732 | if (err) { |
| 5733 | sock_release(sock); |
| 5734 | sock = NULL; |
| 5735 | } |
Vlad Yasevich | 4f44430 | 2006-10-30 18:54:32 -0800 | [diff] [blame] | 5736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | *sockp = sock; |
| 5738 | |
| 5739 | return err; |
| 5740 | } |
Benjamin Poirier | 0343c55 | 2012-03-08 05:55:58 +0000 | [diff] [blame] | 5741 | EXPORT_SYMBOL(sctp_do_peeloff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5743 | static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff, |
| 5744 | struct file **newfile, unsigned flags) |
| 5745 | { |
| 5746 | struct socket *newsock; |
| 5747 | int retval; |
| 5748 | |
| 5749 | retval = sctp_do_peeloff(sk, peeloff->associd, &newsock); |
| 5750 | if (retval < 0) |
| 5751 | goto out; |
| 5752 | |
| 5753 | /* Map the socket to an unused fd that can be returned to the user. */ |
| 5754 | retval = get_unused_fd_flags(flags & SOCK_CLOEXEC); |
| 5755 | if (retval < 0) { |
| 5756 | sock_release(newsock); |
| 5757 | goto out; |
| 5758 | } |
| 5759 | |
| 5760 | *newfile = sock_alloc_file(newsock, 0, NULL); |
| 5761 | if (IS_ERR(*newfile)) { |
| 5762 | put_unused_fd(retval); |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5763 | retval = PTR_ERR(*newfile); |
| 5764 | *newfile = NULL; |
| 5765 | return retval; |
| 5766 | } |
| 5767 | |
| 5768 | pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk, |
| 5769 | retval); |
| 5770 | |
| 5771 | peeloff->sd = retval; |
| 5772 | |
| 5773 | if (flags & SOCK_NONBLOCK) |
| 5774 | (*newfile)->f_flags |= O_NONBLOCK; |
| 5775 | out: |
| 5776 | return retval; |
| 5777 | } |
| 5778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) |
| 5780 | { |
| 5781 | sctp_peeloff_arg_t peeloff; |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5782 | struct file *newfile = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5783 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5784 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5785 | if (len < sizeof(sctp_peeloff_arg_t)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5786 | return -EINVAL; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5787 | len = sizeof(sctp_peeloff_arg_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | if (copy_from_user(&peeloff, optval, len)) |
| 5789 | return -EFAULT; |
| 5790 | |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5791 | retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | if (retval < 0) |
| 5793 | goto out; |
| 5794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5795 | /* Return the fd mapped to the new socket. */ |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 5796 | if (put_user(len, optlen)) { |
| 5797 | fput(newfile); |
| 5798 | put_unused_fd(retval); |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 5799 | return -EFAULT; |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 5800 | } |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 5801 | |
| 5802 | if (copy_to_user(optval, &peeloff, len)) { |
| 5803 | fput(newfile); |
| 5804 | put_unused_fd(retval); |
| 5805 | return -EFAULT; |
| 5806 | } |
| 5807 | fd_install(retval, newfile); |
| 5808 | out: |
| 5809 | return retval; |
| 5810 | } |
| 5811 | |
| 5812 | static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len, |
| 5813 | char __user *optval, int __user *optlen) |
| 5814 | { |
| 5815 | sctp_peeloff_flags_arg_t peeloff; |
| 5816 | struct file *newfile = NULL; |
| 5817 | int retval = 0; |
| 5818 | |
| 5819 | if (len < sizeof(sctp_peeloff_flags_arg_t)) |
| 5820 | return -EINVAL; |
| 5821 | len = sizeof(sctp_peeloff_flags_arg_t); |
| 5822 | if (copy_from_user(&peeloff, optval, len)) |
| 5823 | return -EFAULT; |
| 5824 | |
| 5825 | retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg, |
| 5826 | &newfile, peeloff.flags); |
| 5827 | if (retval < 0) |
| 5828 | goto out; |
| 5829 | |
| 5830 | /* Return the fd mapped to the new socket. */ |
| 5831 | if (put_user(len, optlen)) { |
| 5832 | fput(newfile); |
| 5833 | put_unused_fd(retval); |
| 5834 | return -EFAULT; |
| 5835 | } |
| 5836 | |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 5837 | if (copy_to_user(optval, &peeloff, len)) { |
| 5838 | fput(newfile); |
| 5839 | put_unused_fd(retval); |
| 5840 | return -EFAULT; |
| 5841 | } |
| 5842 | fd_install(retval, newfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | out: |
| 5844 | return retval; |
| 5845 | } |
| 5846 | |
| 5847 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) |
| 5848 | * |
| 5849 | * Applications can enable or disable heartbeats for any peer address of |
| 5850 | * an association, modify an address's heartbeat interval, force a |
| 5851 | * heartbeat to be sent immediately, and adjust the address's maximum |
| 5852 | * number of retransmissions sent before an address is considered |
| 5853 | * unreachable. The following structure is used to access and modify an |
| 5854 | * address's parameters: |
| 5855 | * |
| 5856 | * struct sctp_paddrparams { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5857 | * sctp_assoc_t spp_assoc_id; |
| 5858 | * struct sockaddr_storage spp_address; |
| 5859 | * uint32_t spp_hbinterval; |
| 5860 | * uint16_t spp_pathmaxrxt; |
| 5861 | * uint32_t spp_pathmtu; |
| 5862 | * uint32_t spp_sackdelay; |
| 5863 | * uint32_t spp_flags; |
| 5864 | * }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | * |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5866 | * spp_assoc_id - (one-to-many style socket) This is filled in the |
| 5867 | * application, and identifies the association for |
| 5868 | * this query. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | * spp_address - This specifies which address is of interest. |
| 5870 | * spp_hbinterval - This contains the value of the heartbeat interval, |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5871 | * in milliseconds. If a value of zero |
| 5872 | * is present in this field then no changes are to |
| 5873 | * be made to this parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | * spp_pathmaxrxt - This contains the maximum number of |
| 5875 | * retransmissions before this address shall be |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5876 | * considered unreachable. If a value of zero |
| 5877 | * is present in this field then no changes are to |
| 5878 | * be made to this parameter. |
| 5879 | * spp_pathmtu - When Path MTU discovery is disabled the value |
| 5880 | * specified here will be the "fixed" path mtu. |
| 5881 | * Note that if the spp_address field is empty |
| 5882 | * then all associations on this address will |
| 5883 | * have this fixed path mtu set upon them. |
| 5884 | * |
| 5885 | * spp_sackdelay - When delayed sack is enabled, this value specifies |
| 5886 | * the number of milliseconds that sacks will be delayed |
| 5887 | * for. This value will apply to all addresses of an |
| 5888 | * association if the spp_address field is empty. Note |
| 5889 | * also, that if delayed sack is enabled and this |
| 5890 | * value is set to 0, no change is made to the last |
| 5891 | * recorded delayed sack timer value. |
| 5892 | * |
| 5893 | * spp_flags - These flags are used to control various features |
| 5894 | * on an association. The flag field may contain |
| 5895 | * zero or more of the following options. |
| 5896 | * |
| 5897 | * SPP_HB_ENABLE - Enable heartbeats on the |
| 5898 | * specified address. Note that if the address |
| 5899 | * field is empty all addresses for the association |
| 5900 | * have heartbeats enabled upon them. |
| 5901 | * |
| 5902 | * SPP_HB_DISABLE - Disable heartbeats on the |
| 5903 | * speicifed address. Note that if the address |
| 5904 | * field is empty all addresses for the association |
| 5905 | * will have their heartbeats disabled. Note also |
| 5906 | * that SPP_HB_ENABLE and SPP_HB_DISABLE are |
| 5907 | * mutually exclusive, only one of these two should |
| 5908 | * be specified. Enabling both fields will have |
| 5909 | * undetermined results. |
| 5910 | * |
| 5911 | * SPP_HB_DEMAND - Request a user initiated heartbeat |
| 5912 | * to be made immediately. |
| 5913 | * |
| 5914 | * SPP_PMTUD_ENABLE - This field will enable PMTU |
| 5915 | * discovery upon the specified address. Note that |
| 5916 | * if the address feild is empty then all addresses |
| 5917 | * on the association are effected. |
| 5918 | * |
| 5919 | * SPP_PMTUD_DISABLE - This field will disable PMTU |
| 5920 | * discovery upon the specified address. Note that |
| 5921 | * if the address feild is empty then all addresses |
| 5922 | * on the association are effected. Not also that |
| 5923 | * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually |
| 5924 | * exclusive. Enabling both will have undetermined |
| 5925 | * results. |
| 5926 | * |
| 5927 | * SPP_SACKDELAY_ENABLE - Setting this flag turns |
| 5928 | * on delayed sack. The time specified in spp_sackdelay |
| 5929 | * is used to specify the sack delay for this address. Note |
| 5930 | * that if spp_address is empty then all addresses will |
| 5931 | * enable delayed sack and take on the sack delay |
| 5932 | * value specified in spp_sackdelay. |
| 5933 | * SPP_SACKDELAY_DISABLE - Setting this flag turns |
| 5934 | * off delayed sack. If the spp_address field is blank then |
| 5935 | * delayed sack is disabled for the entire association. Note |
| 5936 | * also that this field is mutually exclusive to |
| 5937 | * SPP_SACKDELAY_ENABLE, setting both will have undefined |
| 5938 | * results. |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 5939 | * |
| 5940 | * SPP_IPV6_FLOWLABEL: Setting this flag enables the |
| 5941 | * setting of the IPV6 flow label value. The value is |
| 5942 | * contained in the spp_ipv6_flowlabel field. |
| 5943 | * Upon retrieval, this flag will be set to indicate that |
| 5944 | * the spp_ipv6_flowlabel field has a valid value returned. |
| 5945 | * If a specific destination address is set (in the |
| 5946 | * spp_address field), then the value returned is that of |
| 5947 | * the address. If just an association is specified (and |
| 5948 | * no address), then the association's default flow label |
| 5949 | * is returned. If neither an association nor a destination |
| 5950 | * is specified, then the socket's default flow label is |
| 5951 | * returned. For non-IPv6 sockets, this flag will be left |
| 5952 | * cleared. |
| 5953 | * |
| 5954 | * SPP_DSCP: Setting this flag enables the setting of the |
| 5955 | * Differentiated Services Code Point (DSCP) value |
| 5956 | * associated with either the association or a specific |
| 5957 | * address. The value is obtained in the spp_dscp field. |
| 5958 | * Upon retrieval, this flag will be set to indicate that |
| 5959 | * the spp_dscp field has a valid value returned. If a |
| 5960 | * specific destination address is set when called (in the |
| 5961 | * spp_address field), then that specific destination |
| 5962 | * address's DSCP value is returned. If just an association |
| 5963 | * is specified, then the association's default DSCP is |
| 5964 | * returned. If neither an association nor a destination is |
| 5965 | * specified, then the socket's default DSCP is returned. |
| 5966 | * |
| 5967 | * spp_ipv6_flowlabel |
| 5968 | * - This field is used in conjunction with the |
| 5969 | * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label. |
| 5970 | * The 20 least significant bits are used for the flow |
| 5971 | * label. This setting has precedence over any IPv6-layer |
| 5972 | * setting. |
| 5973 | * |
| 5974 | * spp_dscp - This field is used in conjunction with the SPP_DSCP flag |
| 5975 | * and contains the DSCP. The 6 most significant bits are |
| 5976 | * used for the DSCP. This setting has precedence over any |
| 5977 | * IPv4- or IPv6- layer setting. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5978 | */ |
| 5979 | static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5980 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 | { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5982 | struct sctp_paddrparams params; |
| 5983 | struct sctp_transport *trans = NULL; |
| 5984 | struct sctp_association *asoc = NULL; |
| 5985 | struct sctp_sock *sp = sctp_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5986 | |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 5987 | if (len >= sizeof(params)) |
| 5988 | len = sizeof(params); |
| 5989 | else if (len >= ALIGN(offsetof(struct sctp_paddrparams, |
| 5990 | spp_ipv6_flowlabel), 4)) |
| 5991 | len = ALIGN(offsetof(struct sctp_paddrparams, |
| 5992 | spp_ipv6_flowlabel), 4); |
| 5993 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5994 | return -EINVAL; |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 5995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | if (copy_from_user(¶ms, optval, len)) |
| 5997 | return -EFAULT; |
| 5998 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 5999 | /* If an address other than INADDR_ANY is specified, and |
| 6000 | * no transport is found, then the request is invalid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | */ |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 6002 | if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) { |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 6003 | trans = sctp_addr_id2transport(sk, ¶ms.spp_address, |
| 6004 | params.spp_assoc_id); |
| 6005 | if (!trans) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 6006 | pr_debug("%s: failed no transport\n", __func__); |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 6007 | return -EINVAL; |
| 6008 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6009 | } |
| 6010 | |
Xin Long | b99e5e0 | 2019-01-28 15:08:24 +0800 | [diff] [blame] | 6011 | /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the |
| 6012 | * socket is a one to many style socket, and an association |
| 6013 | * was not found, then the id was invalid. |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 6014 | */ |
| 6015 | asoc = sctp_id2assoc(sk, params.spp_assoc_id); |
Xin Long | b99e5e0 | 2019-01-28 15:08:24 +0800 | [diff] [blame] | 6016 | if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC && |
| 6017 | sctp_style(sk, UDP)) { |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 6018 | pr_debug("%s: failed no association\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6019 | return -EINVAL; |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 6020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 6022 | if (trans) { |
| 6023 | /* Fetch transport values. */ |
| 6024 | params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); |
| 6025 | params.spp_pathmtu = trans->pathmtu; |
| 6026 | params.spp_pathmaxrxt = trans->pathmaxrxt; |
| 6027 | params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 6029 | /*draft-11 doesn't say what to return in spp_flags*/ |
| 6030 | params.spp_flags = trans->param_flags; |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 6031 | if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) { |
| 6032 | params.spp_ipv6_flowlabel = trans->flowlabel & |
| 6033 | SCTP_FLOWLABEL_VAL_MASK; |
| 6034 | params.spp_flags |= SPP_IPV6_FLOWLABEL; |
| 6035 | } |
| 6036 | if (trans->dscp & SCTP_DSCP_SET_MASK) { |
| 6037 | params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK; |
| 6038 | params.spp_flags |= SPP_DSCP; |
| 6039 | } |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 6040 | } else if (asoc) { |
| 6041 | /* Fetch association values. */ |
| 6042 | params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); |
| 6043 | params.spp_pathmtu = asoc->pathmtu; |
| 6044 | params.spp_pathmaxrxt = asoc->pathmaxrxt; |
| 6045 | params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6046 | |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 6047 | /*draft-11 doesn't say what to return in spp_flags*/ |
| 6048 | params.spp_flags = asoc->param_flags; |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 6049 | if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) { |
| 6050 | params.spp_ipv6_flowlabel = asoc->flowlabel & |
| 6051 | SCTP_FLOWLABEL_VAL_MASK; |
| 6052 | params.spp_flags |= SPP_IPV6_FLOWLABEL; |
| 6053 | } |
| 6054 | if (asoc->dscp & SCTP_DSCP_SET_MASK) { |
| 6055 | params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK; |
| 6056 | params.spp_flags |= SPP_DSCP; |
| 6057 | } |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 6058 | } else { |
| 6059 | /* Fetch socket values. */ |
| 6060 | params.spp_hbinterval = sp->hbinterval; |
| 6061 | params.spp_pathmtu = sp->pathmtu; |
| 6062 | params.spp_sackdelay = sp->sackdelay; |
| 6063 | params.spp_pathmaxrxt = sp->pathmaxrxt; |
| 6064 | |
| 6065 | /*draft-11 doesn't say what to return in spp_flags*/ |
| 6066 | params.spp_flags = sp->param_flags; |
Xin Long | 0b0dce7 | 2018-07-02 18:21:13 +0800 | [diff] [blame] | 6067 | if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) { |
| 6068 | params.spp_ipv6_flowlabel = sp->flowlabel & |
| 6069 | SCTP_FLOWLABEL_VAL_MASK; |
| 6070 | params.spp_flags |= SPP_IPV6_FLOWLABEL; |
| 6071 | } |
| 6072 | if (sp->dscp & SCTP_DSCP_SET_MASK) { |
| 6073 | params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK; |
| 6074 | params.spp_flags |= SPP_DSCP; |
| 6075 | } |
Frank Filz | 52ccb8e | 2005-12-22 11:36:46 -0800 | [diff] [blame] | 6076 | } |
| 6077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | if (copy_to_user(optval, ¶ms, len)) |
| 6079 | return -EFAULT; |
| 6080 | |
| 6081 | if (put_user(len, optlen)) |
| 6082 | return -EFAULT; |
| 6083 | |
| 6084 | return 0; |
| 6085 | } |
| 6086 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6087 | /* |
| 6088 | * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6089 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6090 | * This option will effect the way delayed acks are performed. This |
| 6091 | * option allows you to get or set the delayed ack time, in |
| 6092 | * milliseconds. It also allows changing the delayed ack frequency. |
| 6093 | * Changing the frequency to 1 disables the delayed sack algorithm. If |
| 6094 | * the assoc_id is 0, then this sets or gets the endpoints default |
| 6095 | * values. If the assoc_id field is non-zero, then the set or get |
| 6096 | * effects the specified association for the one to many model (the |
| 6097 | * assoc_id field is ignored by the one to one model). Note that if |
| 6098 | * sack_delay or sack_freq are 0 when setting this option, then the |
| 6099 | * current values will remain unchanged. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6100 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6101 | * struct sctp_sack_info { |
| 6102 | * sctp_assoc_t sack_assoc_id; |
| 6103 | * uint32_t sack_delay; |
| 6104 | * uint32_t sack_freq; |
| 6105 | * }; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6106 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6107 | * sack_assoc_id - This parameter, indicates which association the user |
| 6108 | * is performing an action upon. Note that if this field's value is |
| 6109 | * zero then the endpoints default value is changed (effecting future |
| 6110 | * associations only). |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6111 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6112 | * sack_delay - This parameter contains the number of milliseconds that |
| 6113 | * the user is requesting the delayed ACK timer be set to. Note that |
| 6114 | * this value is defined in the standard to be between 200 and 500 |
| 6115 | * milliseconds. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6116 | * |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6117 | * sack_freq - This parameter contains the number of packets that must |
| 6118 | * be received before a sack is sent without waiting for the delay |
| 6119 | * timer to expire. The default value for this is 2, setting this |
| 6120 | * value to 1 will disable the delayed sack algorithm. |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6121 | */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6122 | static int sctp_getsockopt_delayed_ack(struct sock *sk, int len, |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6123 | char __user *optval, |
| 6124 | int __user *optlen) |
| 6125 | { |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6126 | struct sctp_sack_info params; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6127 | struct sctp_association *asoc = NULL; |
| 6128 | struct sctp_sock *sp = sctp_sk(sk); |
| 6129 | |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6130 | if (len >= sizeof(struct sctp_sack_info)) { |
| 6131 | len = sizeof(struct sctp_sack_info); |
| 6132 | |
| 6133 | if (copy_from_user(¶ms, optval, len)) |
| 6134 | return -EFAULT; |
| 6135 | } else if (len == sizeof(struct sctp_assoc_value)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6136 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6137 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6138 | "Use of struct sctp_assoc_value in delayed_ack socket option.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6139 | "Use struct sctp_sack_info instead\n", |
| 6140 | current->comm, task_pid_nr(current)); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6141 | if (copy_from_user(¶ms, optval, len)) |
| 6142 | return -EFAULT; |
| 6143 | } else |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 6144 | return -EINVAL; |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6145 | |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 6146 | /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the |
| 6147 | * socket is a one to many style socket, and an association |
| 6148 | * was not found, then the id was invalid. |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 6149 | */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6150 | asoc = sctp_id2assoc(sk, params.sack_assoc_id); |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 6151 | if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC && |
| 6152 | sctp_style(sk, UDP)) |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6153 | return -EINVAL; |
| 6154 | |
| 6155 | if (asoc) { |
| 6156 | /* Fetch association values. */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6157 | if (asoc->param_flags & SPP_SACKDELAY_ENABLE) { |
Xin Long | 9c5829e | 2019-01-28 15:08:34 +0800 | [diff] [blame] | 6158 | params.sack_delay = jiffies_to_msecs(asoc->sackdelay); |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6159 | params.sack_freq = asoc->sackfreq; |
| 6160 | |
| 6161 | } else { |
| 6162 | params.sack_delay = 0; |
| 6163 | params.sack_freq = 1; |
| 6164 | } |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6165 | } else { |
| 6166 | /* Fetch socket values. */ |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 6167 | if (sp->param_flags & SPP_SACKDELAY_ENABLE) { |
| 6168 | params.sack_delay = sp->sackdelay; |
| 6169 | params.sack_freq = sp->sackfreq; |
| 6170 | } else { |
| 6171 | params.sack_delay = 0; |
| 6172 | params.sack_freq = 1; |
| 6173 | } |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 6174 | } |
| 6175 | |
| 6176 | if (copy_to_user(optval, ¶ms, len)) |
| 6177 | return -EFAULT; |
| 6178 | |
| 6179 | if (put_user(len, optlen)) |
| 6180 | return -EFAULT; |
| 6181 | |
| 6182 | return 0; |
| 6183 | } |
| 6184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) |
| 6186 | * |
| 6187 | * Applications can specify protocol parameters for the default association |
| 6188 | * initialization. The option name argument to setsockopt() and getsockopt() |
| 6189 | * is SCTP_INITMSG. |
| 6190 | * |
| 6191 | * Setting initialization parameters is effective only on an unconnected |
| 6192 | * socket (for UDP-style sockets only future associations are effected |
| 6193 | * by the change). With TCP-style sockets, this option is inherited by |
| 6194 | * sockets derived from a listener socket. |
| 6195 | */ |
| 6196 | static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) |
| 6197 | { |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6198 | if (len < sizeof(struct sctp_initmsg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6199 | return -EINVAL; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6200 | len = sizeof(struct sctp_initmsg); |
| 6201 | if (put_user(len, optlen)) |
| 6202 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6203 | if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) |
| 6204 | return -EFAULT; |
| 6205 | return 0; |
| 6206 | } |
| 6207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6208 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6209 | static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, |
| 6210 | char __user *optval, int __user *optlen) |
| 6211 | { |
| 6212 | struct sctp_association *asoc; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6213 | int cnt = 0; |
| 6214 | struct sctp_getaddrs getaddrs; |
| 6215 | struct sctp_transport *from; |
| 6216 | void __user *to; |
| 6217 | union sctp_addr temp; |
| 6218 | struct sctp_sock *sp = sctp_sk(sk); |
| 6219 | int addrlen; |
| 6220 | size_t space_left; |
| 6221 | int bytes_copied; |
| 6222 | |
| 6223 | if (len < sizeof(struct sctp_getaddrs)) |
| 6224 | return -EINVAL; |
| 6225 | |
| 6226 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) |
| 6227 | return -EFAULT; |
| 6228 | |
| 6229 | /* For UDP-style sockets, id specifies the association to query. */ |
| 6230 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); |
| 6231 | if (!asoc) |
| 6232 | return -EINVAL; |
| 6233 | |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 6234 | to = optval + offsetof(struct sctp_getaddrs, addrs); |
| 6235 | space_left = len - offsetof(struct sctp_getaddrs, addrs); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6236 | |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 6237 | list_for_each_entry(from, &asoc->peer.transport_addr_list, |
| 6238 | transports) { |
Al Viro | b3f5b3b | 2006-11-20 17:22:43 -0800 | [diff] [blame] | 6239 | memcpy(&temp, &from->ipaddr, sizeof(temp)); |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 6240 | addrlen = sctp_get_pf_specific(sk->sk_family) |
| 6241 | ->addr_to_user(sp, &temp); |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6242 | if (space_left < addrlen) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6243 | return -ENOMEM; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6244 | if (copy_to_user(to, &temp, addrlen)) |
| 6245 | return -EFAULT; |
| 6246 | to += addrlen; |
| 6247 | cnt++; |
| 6248 | space_left -= addrlen; |
| 6249 | } |
| 6250 | |
| 6251 | if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) |
| 6252 | return -EFAULT; |
| 6253 | bytes_copied = ((char __user *)to) - optval; |
| 6254 | if (put_user(bytes_copied, optlen)) |
| 6255 | return -EFAULT; |
| 6256 | |
| 6257 | return 0; |
| 6258 | } |
| 6259 | |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6260 | static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, |
| 6261 | size_t space_left, int *bytes_copied) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6262 | { |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6263 | struct sctp_sockaddr_entry *addr; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6264 | union sctp_addr temp; |
| 6265 | int cnt = 0; |
| 6266 | int addrlen; |
Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 6267 | struct net *net = sock_net(sk); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6268 | |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 6269 | rcu_read_lock(); |
Eric W. Biederman | 4db67e8 | 2012-08-06 08:42:04 +0000 | [diff] [blame] | 6270 | list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) { |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 6271 | if (!addr->valid) |
| 6272 | continue; |
| 6273 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 6274 | if ((PF_INET == sk->sk_family) && |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 6275 | (AF_INET6 == addr->a.sa.sa_family)) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6276 | continue; |
Vlad Yasevich | 7dab83d | 2008-07-18 23:05:40 -0700 | [diff] [blame] | 6277 | if ((PF_INET6 == sk->sk_family) && |
| 6278 | inet_v6_ipv6only(sk) && |
| 6279 | (AF_INET == addr->a.sa.sa_family)) |
| 6280 | continue; |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 6281 | memcpy(&temp, &addr->a, sizeof(temp)); |
Vlad Yasevich | b46ae36 | 2008-01-28 14:25:36 -0500 | [diff] [blame] | 6282 | if (!temp.v4.sin_port) |
| 6283 | temp.v4.sin_port = htons(port); |
| 6284 | |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 6285 | addrlen = sctp_get_pf_specific(sk->sk_family) |
| 6286 | ->addr_to_user(sctp_sk(sk), &temp); |
| 6287 | |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 6288 | if (space_left < addrlen) { |
| 6289 | cnt = -ENOMEM; |
| 6290 | break; |
| 6291 | } |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6292 | memcpy(to, &temp, addrlen); |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 6293 | |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6294 | to += addrlen; |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 6295 | cnt++; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6296 | space_left -= addrlen; |
Vlad Yasevich | 3663c30 | 2007-07-03 12:43:12 -0400 | [diff] [blame] | 6297 | *bytes_copied += addrlen; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6298 | } |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 6299 | rcu_read_unlock(); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6300 | |
| 6301 | return cnt; |
| 6302 | } |
| 6303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6305 | static int sctp_getsockopt_local_addrs(struct sock *sk, int len, |
| 6306 | char __user *optval, int __user *optlen) |
| 6307 | { |
| 6308 | struct sctp_bind_addr *bp; |
| 6309 | struct sctp_association *asoc; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6310 | int cnt = 0; |
| 6311 | struct sctp_getaddrs getaddrs; |
| 6312 | struct sctp_sockaddr_entry *addr; |
| 6313 | void __user *to; |
| 6314 | union sctp_addr temp; |
| 6315 | struct sctp_sock *sp = sctp_sk(sk); |
| 6316 | int addrlen; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6317 | int err = 0; |
| 6318 | size_t space_left; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6319 | int bytes_copied = 0; |
| 6320 | void *addrs; |
Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 6321 | void *buf; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6322 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6323 | if (len < sizeof(struct sctp_getaddrs)) |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6324 | return -EINVAL; |
| 6325 | |
| 6326 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) |
| 6327 | return -EFAULT; |
| 6328 | |
| 6329 | /* |
| 6330 | * For UDP-style sockets, id specifies the association to query. |
| 6331 | * If the id field is set to the value '0' then the locally bound |
| 6332 | * addresses are returned without regard to any particular |
| 6333 | * association. |
| 6334 | */ |
| 6335 | if (0 == getaddrs.assoc_id) { |
| 6336 | bp = &sctp_sk(sk)->ep->base.bind_addr; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6337 | } else { |
| 6338 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); |
| 6339 | if (!asoc) |
| 6340 | return -EINVAL; |
| 6341 | bp = &asoc->base.bind_addr; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6342 | } |
| 6343 | |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 6344 | to = optval + offsetof(struct sctp_getaddrs, addrs); |
| 6345 | space_left = len - offsetof(struct sctp_getaddrs, addrs); |
Neil Horman | 186e234 | 2007-06-18 19:59:16 -0400 | [diff] [blame] | 6346 | |
Marcelo Ricardo Leitner | cacc062 | 2015-11-30 14:32:54 -0200 | [diff] [blame] | 6347 | addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN); |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6348 | if (!addrs) |
| 6349 | return -ENOMEM; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6350 | |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6351 | /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid |
| 6352 | * addresses from the global local address list. |
| 6353 | */ |
| 6354 | if (sctp_list_single_entry(&bp->address_list)) { |
| 6355 | addr = list_entry(bp->address_list.next, |
| 6356 | struct sctp_sockaddr_entry, list); |
Vlad Yasevich | 52cae8f | 2008-08-18 10:34:34 -0400 | [diff] [blame] | 6357 | if (sctp_is_any(sk, &addr->a)) { |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6358 | cnt = sctp_copy_laddrs(sk, bp->port, addrs, |
| 6359 | space_left, &bytes_copied); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6360 | if (cnt < 0) { |
| 6361 | err = cnt; |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 6362 | goto out; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6363 | } |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 6364 | goto copy_getaddrs; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6365 | } |
| 6366 | } |
| 6367 | |
Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 6368 | buf = addrs; |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 6369 | /* Protection on the bound address list is not needed since |
| 6370 | * in the socket option context we hold a socket lock and |
| 6371 | * thus the bound address list can't change. |
| 6372 | */ |
| 6373 | list_for_each_entry(addr, &bp->address_list, list) { |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 6374 | memcpy(&temp, &addr->a, sizeof(temp)); |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 6375 | addrlen = sctp_get_pf_specific(sk->sk_family) |
| 6376 | ->addr_to_user(sp, &temp); |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6377 | if (space_left < addrlen) { |
| 6378 | err = -ENOMEM; /*fixme: right error?*/ |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame] | 6379 | goto out; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6380 | } |
Vlad Yasevich | 70b57b8 | 2007-05-09 13:51:31 -0700 | [diff] [blame] | 6381 | memcpy(buf, &temp, addrlen); |
| 6382 | buf += addrlen; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6383 | bytes_copied += addrlen; |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 6384 | cnt++; |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6385 | space_left -= addrlen; |
| 6386 | } |
| 6387 | |
| 6388 | copy_getaddrs: |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6389 | if (copy_to_user(to, addrs, bytes_copied)) { |
| 6390 | err = -EFAULT; |
Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 6391 | goto out; |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6392 | } |
Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 6393 | if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) { |
| 6394 | err = -EFAULT; |
Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 6395 | goto out; |
Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 6396 | } |
Marcelo Ricardo Leitner | c76f97c | 2018-01-08 19:02:29 -0200 | [diff] [blame] | 6397 | /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too, |
| 6398 | * but we can't change it anymore. |
| 6399 | */ |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6400 | if (put_user(bytes_copied, optlen)) |
Vlad Yasevich | fe979ac | 2007-05-23 11:11:37 -0400 | [diff] [blame] | 6401 | err = -EFAULT; |
Sebastian Siewior | d6f9fda | 2007-07-27 22:55:59 +0200 | [diff] [blame] | 6402 | out: |
Vlad Yasevich | aad97f3 | 2007-04-28 21:09:04 -0700 | [diff] [blame] | 6403 | kfree(addrs); |
Ivan Skytte Jørgensen | 5fe467e | 2005-10-06 21:36:17 -0700 | [diff] [blame] | 6404 | return err; |
| 6405 | } |
| 6406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) |
| 6408 | * |
| 6409 | * Requests that the local SCTP stack use the enclosed peer address as |
| 6410 | * the association primary. The enclosed address must be one of the |
| 6411 | * association peer's addresses. |
| 6412 | */ |
| 6413 | static int sctp_getsockopt_primary_addr(struct sock *sk, int len, |
| 6414 | char __user *optval, int __user *optlen) |
| 6415 | { |
| 6416 | struct sctp_prim prim; |
| 6417 | struct sctp_association *asoc; |
| 6418 | struct sctp_sock *sp = sctp_sk(sk); |
| 6419 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6420 | if (len < sizeof(struct sctp_prim)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | return -EINVAL; |
| 6422 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6423 | len = sizeof(struct sctp_prim); |
| 6424 | |
| 6425 | if (copy_from_user(&prim, optval, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6426 | return -EFAULT; |
| 6427 | |
| 6428 | asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); |
| 6429 | if (!asoc) |
| 6430 | return -EINVAL; |
| 6431 | |
| 6432 | if (!asoc->peer.primary_path) |
| 6433 | return -ENOTCONN; |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 6434 | |
Al Viro | 8cec6b8 | 2006-11-20 17:23:01 -0800 | [diff] [blame] | 6435 | memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, |
| 6436 | asoc->peer.primary_path->af_specific->sockaddr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | |
Jason Gunthorpe | 299ee12 | 2014-07-30 12:40:53 -0600 | [diff] [blame] | 6438 | sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | (union sctp_addr *)&prim.ssp_addr); |
| 6440 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6441 | if (put_user(len, optlen)) |
| 6442 | return -EFAULT; |
| 6443 | if (copy_to_user(optval, &prim, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6444 | return -EFAULT; |
| 6445 | |
| 6446 | return 0; |
| 6447 | } |
| 6448 | |
| 6449 | /* |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6450 | * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6451 | * |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6452 | * Requests that the local endpoint set the specified Adaptation Layer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | * Indication parameter for all future INIT and INIT-ACK exchanges. |
| 6454 | */ |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6455 | static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6456 | char __user *optval, int __user *optlen) |
| 6457 | { |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6458 | struct sctp_setadaptation adaptation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6459 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6460 | if (len < sizeof(struct sctp_setadaptation)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6461 | return -EINVAL; |
| 6462 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6463 | len = sizeof(struct sctp_setadaptation); |
| 6464 | |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6465 | adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6466 | |
| 6467 | if (put_user(len, optlen)) |
| 6468 | return -EFAULT; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 6469 | if (copy_to_user(optval, &adaptation, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6470 | return -EFAULT; |
Ivan Skytte Jorgensen | a1ab358 | 2005-10-28 15:33:24 -0700 | [diff] [blame] | 6471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | return 0; |
| 6473 | } |
| 6474 | |
| 6475 | /* |
| 6476 | * |
| 6477 | * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) |
| 6478 | * |
| 6479 | * Applications that wish to use the sendto() system call may wish to |
| 6480 | * specify a default set of parameters that would normally be supplied |
| 6481 | * through the inclusion of ancillary data. This socket option allows |
| 6482 | * such an application to set the default sctp_sndrcvinfo structure. |
| 6483 | |
| 6484 | |
| 6485 | * The application that wishes to use this socket option simply passes |
| 6486 | * in to this call the sctp_sndrcvinfo structure defined in Section |
| 6487 | * 5.2.2) The input parameters accepted by this call include |
| 6488 | * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, |
| 6489 | * sinfo_timetolive. The user must provide the sinfo_assoc_id field in |
| 6490 | * to this call if the caller is using the UDP model. |
| 6491 | * |
| 6492 | * For getsockopt, it get the default sctp_sndrcvinfo structure. |
| 6493 | */ |
| 6494 | static int sctp_getsockopt_default_send_param(struct sock *sk, |
| 6495 | int len, char __user *optval, |
| 6496 | int __user *optlen) |
| 6497 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6498 | struct sctp_sock *sp = sctp_sk(sk); |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 6499 | struct sctp_association *asoc; |
| 6500 | struct sctp_sndrcvinfo info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 6502 | if (len < sizeof(info)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | return -EINVAL; |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6504 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 6505 | len = sizeof(info); |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6506 | |
| 6507 | if (copy_from_user(&info, optval, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6508 | return -EFAULT; |
| 6509 | |
| 6510 | asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); |
Xin Long | 707e45b | 2019-01-28 15:08:35 +0800 | [diff] [blame] | 6511 | if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC && |
| 6512 | sctp_style(sk, UDP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6513 | return -EINVAL; |
Xin Long | 707e45b | 2019-01-28 15:08:35 +0800 | [diff] [blame] | 6514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6515 | if (asoc) { |
| 6516 | info.sinfo_stream = asoc->default_stream; |
| 6517 | info.sinfo_flags = asoc->default_flags; |
| 6518 | info.sinfo_ppid = asoc->default_ppid; |
| 6519 | info.sinfo_context = asoc->default_context; |
| 6520 | info.sinfo_timetolive = asoc->default_timetolive; |
| 6521 | } else { |
| 6522 | info.sinfo_stream = sp->default_stream; |
| 6523 | info.sinfo_flags = sp->default_flags; |
| 6524 | info.sinfo_ppid = sp->default_ppid; |
| 6525 | info.sinfo_context = sp->default_context; |
| 6526 | info.sinfo_timetolive = sp->default_timetolive; |
| 6527 | } |
| 6528 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6529 | if (put_user(len, optlen)) |
| 6530 | return -EFAULT; |
| 6531 | if (copy_to_user(optval, &info, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6532 | return -EFAULT; |
| 6533 | |
| 6534 | return 0; |
| 6535 | } |
| 6536 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 6537 | /* RFC6458, Section 8.1.31. Set/get Default Send Parameters |
| 6538 | * (SCTP_DEFAULT_SNDINFO) |
| 6539 | */ |
| 6540 | static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len, |
| 6541 | char __user *optval, |
| 6542 | int __user *optlen) |
| 6543 | { |
| 6544 | struct sctp_sock *sp = sctp_sk(sk); |
| 6545 | struct sctp_association *asoc; |
| 6546 | struct sctp_sndinfo info; |
| 6547 | |
| 6548 | if (len < sizeof(info)) |
| 6549 | return -EINVAL; |
| 6550 | |
| 6551 | len = sizeof(info); |
| 6552 | |
| 6553 | if (copy_from_user(&info, optval, len)) |
| 6554 | return -EFAULT; |
| 6555 | |
| 6556 | asoc = sctp_id2assoc(sk, info.snd_assoc_id); |
Xin Long | 92fc3bd | 2019-01-28 15:08:36 +0800 | [diff] [blame] | 6557 | if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC && |
| 6558 | sctp_style(sk, UDP)) |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 6559 | return -EINVAL; |
Xin Long | 92fc3bd | 2019-01-28 15:08:36 +0800 | [diff] [blame] | 6560 | |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 6561 | if (asoc) { |
| 6562 | info.snd_sid = asoc->default_stream; |
| 6563 | info.snd_flags = asoc->default_flags; |
| 6564 | info.snd_ppid = asoc->default_ppid; |
| 6565 | info.snd_context = asoc->default_context; |
| 6566 | } else { |
| 6567 | info.snd_sid = sp->default_stream; |
| 6568 | info.snd_flags = sp->default_flags; |
| 6569 | info.snd_ppid = sp->default_ppid; |
| 6570 | info.snd_context = sp->default_context; |
| 6571 | } |
| 6572 | |
| 6573 | if (put_user(len, optlen)) |
| 6574 | return -EFAULT; |
| 6575 | if (copy_to_user(optval, &info, len)) |
| 6576 | return -EFAULT; |
| 6577 | |
| 6578 | return 0; |
| 6579 | } |
| 6580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6581 | /* |
| 6582 | * |
| 6583 | * 7.1.5 SCTP_NODELAY |
| 6584 | * |
| 6585 | * Turn on/off any Nagle-like algorithm. This means that packets are |
| 6586 | * generally sent as soon as possible and no unnecessary delays are |
| 6587 | * introduced, at the cost of more packets in the network. Expects an |
| 6588 | * integer boolean flag. |
| 6589 | */ |
| 6590 | |
| 6591 | static int sctp_getsockopt_nodelay(struct sock *sk, int len, |
| 6592 | char __user *optval, int __user *optlen) |
| 6593 | { |
| 6594 | int val; |
| 6595 | |
| 6596 | if (len < sizeof(int)) |
| 6597 | return -EINVAL; |
| 6598 | |
| 6599 | len = sizeof(int); |
| 6600 | val = (sctp_sk(sk)->nodelay == 1); |
| 6601 | if (put_user(len, optlen)) |
| 6602 | return -EFAULT; |
| 6603 | if (copy_to_user(optval, &val, len)) |
| 6604 | return -EFAULT; |
| 6605 | return 0; |
| 6606 | } |
| 6607 | |
| 6608 | /* |
| 6609 | * |
| 6610 | * 7.1.1 SCTP_RTOINFO |
| 6611 | * |
| 6612 | * The protocol parameters used to initialize and bound retransmission |
| 6613 | * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access |
| 6614 | * and modify these parameters. |
| 6615 | * All parameters are time values, in milliseconds. A value of 0, when |
| 6616 | * modifying the parameters, indicates that the current value should not |
| 6617 | * be changed. |
| 6618 | * |
| 6619 | */ |
| 6620 | static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, |
| 6621 | char __user *optval, |
| 6622 | int __user *optlen) { |
| 6623 | struct sctp_rtoinfo rtoinfo; |
| 6624 | struct sctp_association *asoc; |
| 6625 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6626 | if (len < sizeof (struct sctp_rtoinfo)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6627 | return -EINVAL; |
| 6628 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6629 | len = sizeof(struct sctp_rtoinfo); |
| 6630 | |
| 6631 | if (copy_from_user(&rtoinfo, optval, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6632 | return -EFAULT; |
| 6633 | |
| 6634 | asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); |
| 6635 | |
Xin Long | 7adb5ed | 2019-01-28 15:08:25 +0800 | [diff] [blame] | 6636 | if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC && |
| 6637 | sctp_style(sk, UDP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6638 | return -EINVAL; |
| 6639 | |
| 6640 | /* Values corresponding to the specific association. */ |
| 6641 | if (asoc) { |
| 6642 | rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); |
| 6643 | rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); |
| 6644 | rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); |
| 6645 | } else { |
| 6646 | /* Values corresponding to the endpoint. */ |
| 6647 | struct sctp_sock *sp = sctp_sk(sk); |
| 6648 | |
| 6649 | rtoinfo.srto_initial = sp->rtoinfo.srto_initial; |
| 6650 | rtoinfo.srto_max = sp->rtoinfo.srto_max; |
| 6651 | rtoinfo.srto_min = sp->rtoinfo.srto_min; |
| 6652 | } |
| 6653 | |
| 6654 | if (put_user(len, optlen)) |
| 6655 | return -EFAULT; |
| 6656 | |
| 6657 | if (copy_to_user(optval, &rtoinfo, len)) |
| 6658 | return -EFAULT; |
| 6659 | |
| 6660 | return 0; |
| 6661 | } |
| 6662 | |
| 6663 | /* |
| 6664 | * |
| 6665 | * 7.1.2 SCTP_ASSOCINFO |
| 6666 | * |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 6667 | * This option is used to tune the maximum retransmission attempts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6668 | * of the association. |
| 6669 | * Returns an error if the new association retransmission value is |
| 6670 | * greater than the sum of the retransmission value of the peer. |
| 6671 | * See [SCTP] for more information. |
| 6672 | * |
| 6673 | */ |
| 6674 | static int sctp_getsockopt_associnfo(struct sock *sk, int len, |
| 6675 | char __user *optval, |
| 6676 | int __user *optlen) |
| 6677 | { |
| 6678 | |
| 6679 | struct sctp_assocparams assocparams; |
| 6680 | struct sctp_association *asoc; |
| 6681 | struct list_head *pos; |
| 6682 | int cnt = 0; |
| 6683 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6684 | if (len < sizeof (struct sctp_assocparams)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | return -EINVAL; |
| 6686 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6687 | len = sizeof(struct sctp_assocparams); |
| 6688 | |
| 6689 | if (copy_from_user(&assocparams, optval, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6690 | return -EFAULT; |
| 6691 | |
| 6692 | asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); |
| 6693 | |
Xin Long | 8889394 | 2019-01-28 15:08:26 +0800 | [diff] [blame] | 6694 | if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC && |
| 6695 | sctp_style(sk, UDP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6696 | return -EINVAL; |
| 6697 | |
| 6698 | /* Values correspoinding to the specific association */ |
Vladislav Yasevich | 1733721 | 2005-04-28 11:57:54 -0700 | [diff] [blame] | 6699 | if (asoc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6700 | assocparams.sasoc_asocmaxrxt = asoc->max_retrans; |
| 6701 | assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; |
| 6702 | assocparams.sasoc_local_rwnd = asoc->a_rwnd; |
Daniel Borkmann | 52db882 | 2013-06-25 18:17:27 +0200 | [diff] [blame] | 6703 | assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6704 | |
| 6705 | list_for_each(pos, &asoc->peer.transport_addr_list) { |
wangweidong | cb3f837 | 2013-12-23 12:16:50 +0800 | [diff] [blame] | 6706 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6707 | } |
| 6708 | |
| 6709 | assocparams.sasoc_number_peer_destinations = cnt; |
| 6710 | } else { |
| 6711 | /* Values corresponding to the endpoint */ |
| 6712 | struct sctp_sock *sp = sctp_sk(sk); |
| 6713 | |
| 6714 | assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; |
| 6715 | assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; |
| 6716 | assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; |
| 6717 | assocparams.sasoc_cookie_life = |
| 6718 | sp->assocparams.sasoc_cookie_life; |
| 6719 | assocparams.sasoc_number_peer_destinations = |
| 6720 | sp->assocparams. |
| 6721 | sasoc_number_peer_destinations; |
| 6722 | } |
| 6723 | |
| 6724 | if (put_user(len, optlen)) |
| 6725 | return -EFAULT; |
| 6726 | |
| 6727 | if (copy_to_user(optval, &assocparams, len)) |
| 6728 | return -EFAULT; |
| 6729 | |
| 6730 | return 0; |
| 6731 | } |
| 6732 | |
| 6733 | /* |
| 6734 | * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) |
| 6735 | * |
| 6736 | * This socket option is a boolean flag which turns on or off mapped V4 |
| 6737 | * addresses. If this option is turned on and the socket is type |
| 6738 | * PF_INET6, then IPv4 addresses will be mapped to V6 representation. |
| 6739 | * If this option is turned off, then no mapping will be done of V4 |
| 6740 | * addresses and a user will receive both PF_INET6 and PF_INET type |
| 6741 | * addresses on the socket. |
| 6742 | */ |
| 6743 | static int sctp_getsockopt_mappedv4(struct sock *sk, int len, |
| 6744 | char __user *optval, int __user *optlen) |
| 6745 | { |
| 6746 | int val; |
| 6747 | struct sctp_sock *sp = sctp_sk(sk); |
| 6748 | |
| 6749 | if (len < sizeof(int)) |
| 6750 | return -EINVAL; |
| 6751 | |
| 6752 | len = sizeof(int); |
| 6753 | val = sp->v4mapped; |
| 6754 | if (put_user(len, optlen)) |
| 6755 | return -EFAULT; |
| 6756 | if (copy_to_user(optval, &val, len)) |
| 6757 | return -EFAULT; |
| 6758 | |
| 6759 | return 0; |
| 6760 | } |
| 6761 | |
| 6762 | /* |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6763 | * 7.1.29. Set or Get the default context (SCTP_CONTEXT) |
| 6764 | * (chapter and verse is quoted at sctp_setsockopt_context()) |
| 6765 | */ |
| 6766 | static int sctp_getsockopt_context(struct sock *sk, int len, |
| 6767 | char __user *optval, int __user *optlen) |
| 6768 | { |
| 6769 | struct sctp_assoc_value params; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6770 | struct sctp_association *asoc; |
| 6771 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6772 | if (len < sizeof(struct sctp_assoc_value)) |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6773 | return -EINVAL; |
| 6774 | |
Neil Horman | 408f22e | 2007-06-16 14:03:45 -0400 | [diff] [blame] | 6775 | len = sizeof(struct sctp_assoc_value); |
| 6776 | |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6777 | if (copy_from_user(¶ms, optval, len)) |
| 6778 | return -EFAULT; |
| 6779 | |
Xin Long | 49b037a | 2019-01-28 15:08:37 +0800 | [diff] [blame] | 6780 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 6781 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 6782 | sctp_style(sk, UDP)) |
| 6783 | return -EINVAL; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6784 | |
Xin Long | 49b037a | 2019-01-28 15:08:37 +0800 | [diff] [blame] | 6785 | params.assoc_value = asoc ? asoc->default_rcv_context |
| 6786 | : sctp_sk(sk)->default_rcv_context; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 6787 | |
| 6788 | if (put_user(len, optlen)) |
| 6789 | return -EFAULT; |
| 6790 | if (copy_to_user(optval, ¶ms, len)) |
| 6791 | return -EFAULT; |
| 6792 | |
| 6793 | return 0; |
| 6794 | } |
| 6795 | |
| 6796 | /* |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6797 | * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) |
| 6798 | * This option will get or set the maximum size to put in any outgoing |
| 6799 | * 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] | 6800 | * fragmented by SCTP into the specified size. Note that the underlying |
| 6801 | * SCTP implementation may fragment into smaller sized chunks when the |
| 6802 | * PMTU of the underlying association is smaller than the value set by |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6803 | * the user. The default value for this option is '0' which indicates |
| 6804 | * the user is NOT limiting fragmentation and only the PMTU will effect |
| 6805 | * SCTP's choice of DATA chunk size. Note also that values set larger |
| 6806 | * than the maximum size of an IP datagram will effectively let SCTP |
| 6807 | * control fragmentation (i.e. the same as setting this option to 0). |
| 6808 | * |
| 6809 | * The following structure is used to access and modify this parameter: |
| 6810 | * |
| 6811 | * struct sctp_assoc_value { |
| 6812 | * sctp_assoc_t assoc_id; |
| 6813 | * uint32_t assoc_value; |
| 6814 | * }; |
| 6815 | * |
| 6816 | * assoc_id: This parameter is ignored for one-to-one style sockets. |
| 6817 | * For one-to-many style sockets this parameter indicates which |
| 6818 | * association the user is performing an action upon. Note that if |
| 6819 | * this field's value is zero then the endpoints default value is |
| 6820 | * changed (effecting future associations only). |
| 6821 | * assoc_value: This parameter specifies the maximum size in bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | */ |
| 6823 | static int sctp_getsockopt_maxseg(struct sock *sk, int len, |
| 6824 | char __user *optval, int __user *optlen) |
| 6825 | { |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6826 | struct sctp_assoc_value params; |
| 6827 | struct sctp_association *asoc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6829 | if (len == sizeof(int)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6830 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6831 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6832 | "Use of int in maxseg socket option.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6833 | "Use struct sctp_assoc_value instead\n", |
| 6834 | current->comm, task_pid_nr(current)); |
Xin Long | 6fd769b | 2019-01-28 15:08:27 +0800 | [diff] [blame] | 6835 | params.assoc_id = SCTP_FUTURE_ASSOC; |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6836 | } else if (len >= sizeof(struct sctp_assoc_value)) { |
| 6837 | len = sizeof(struct sctp_assoc_value); |
Marcelo Ricardo Leitner | c76f97c | 2018-01-08 19:02:29 -0200 | [diff] [blame] | 6838 | if (copy_from_user(¶ms, optval, len)) |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6839 | return -EFAULT; |
| 6840 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6841 | return -EINVAL; |
| 6842 | |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6843 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | 6fd769b | 2019-01-28 15:08:27 +0800 | [diff] [blame] | 6844 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 6845 | sctp_style(sk, UDP)) |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6846 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6847 | |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6848 | if (asoc) |
| 6849 | params.assoc_value = asoc->frag_point; |
| 6850 | else |
| 6851 | params.assoc_value = sctp_sk(sk)->user_frag; |
| 6852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | if (put_user(len, optlen)) |
| 6854 | return -EFAULT; |
Wei Yongjun | e89c209 | 2008-12-25 16:54:58 -0800 | [diff] [blame] | 6855 | if (len == sizeof(int)) { |
| 6856 | if (copy_to_user(optval, ¶ms.assoc_value, len)) |
| 6857 | return -EFAULT; |
| 6858 | } else { |
| 6859 | if (copy_to_user(optval, ¶ms, len)) |
| 6860 | return -EFAULT; |
| 6861 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | |
| 6863 | return 0; |
| 6864 | } |
| 6865 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 6866 | /* |
| 6867 | * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) |
| 6868 | * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) |
| 6869 | */ |
| 6870 | static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, |
| 6871 | char __user *optval, int __user *optlen) |
| 6872 | { |
| 6873 | int val; |
| 6874 | |
| 6875 | if (len < sizeof(int)) |
| 6876 | return -EINVAL; |
| 6877 | |
| 6878 | len = sizeof(int); |
| 6879 | |
| 6880 | val = sctp_sk(sk)->frag_interleave; |
| 6881 | if (put_user(len, optlen)) |
| 6882 | return -EFAULT; |
| 6883 | if (copy_to_user(optval, &val, len)) |
| 6884 | return -EFAULT; |
| 6885 | |
| 6886 | return 0; |
| 6887 | } |
| 6888 | |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 6889 | /* |
| 6890 | * 7.1.25. Set or Get the sctp partial delivery point |
| 6891 | * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) |
| 6892 | */ |
| 6893 | static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, |
| 6894 | char __user *optval, |
| 6895 | int __user *optlen) |
| 6896 | { |
YOSHIFUJI Hideaki | 9cbcbf4 | 2007-07-19 10:44:50 +0900 | [diff] [blame] | 6897 | u32 val; |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 6898 | |
| 6899 | if (len < sizeof(u32)) |
| 6900 | return -EINVAL; |
| 6901 | |
| 6902 | len = sizeof(u32); |
| 6903 | |
| 6904 | val = sctp_sk(sk)->pd_point; |
| 6905 | if (put_user(len, optlen)) |
| 6906 | return -EFAULT; |
| 6907 | if (copy_to_user(optval, &val, len)) |
| 6908 | return -EFAULT; |
| 6909 | |
Wei Yongjun | 7d743b7 | 2010-12-14 16:10:41 +0000 | [diff] [blame] | 6910 | return 0; |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 6911 | } |
| 6912 | |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6913 | /* |
| 6914 | * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) |
| 6915 | * (chapter and verse is quoted at sctp_setsockopt_maxburst()) |
| 6916 | */ |
| 6917 | static int sctp_getsockopt_maxburst(struct sock *sk, int len, |
| 6918 | char __user *optval, |
| 6919 | int __user *optlen) |
| 6920 | { |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6921 | struct sctp_assoc_value params; |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6922 | struct sctp_association *asoc; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6923 | |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6924 | if (len == sizeof(int)) { |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6925 | pr_warn_ratelimited(DEPRECATED |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6926 | "%s (pid %d) " |
Neil Horman | 94f6519 | 2013-12-23 08:29:43 -0500 | [diff] [blame] | 6927 | "Use of int in max_burst socket option.\n" |
Neil Horman | f916ec9 | 2014-01-02 12:54:27 -0500 | [diff] [blame] | 6928 | "Use struct sctp_assoc_value instead\n", |
| 6929 | current->comm, task_pid_nr(current)); |
Xin Long | e0651a0 | 2019-01-28 15:08:38 +0800 | [diff] [blame] | 6930 | params.assoc_id = SCTP_FUTURE_ASSOC; |
Wei Yongjun | c6db93a | 2009-03-02 09:46:12 +0000 | [diff] [blame] | 6931 | } else if (len >= sizeof(struct sctp_assoc_value)) { |
| 6932 | len = sizeof(struct sctp_assoc_value); |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6933 | if (copy_from_user(¶ms, optval, len)) |
| 6934 | return -EFAULT; |
| 6935 | } else |
| 6936 | return -EINVAL; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6937 | |
Xin Long | e0651a0 | 2019-01-28 15:08:38 +0800 | [diff] [blame] | 6938 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 6939 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 6940 | sctp_style(sk, UDP)) |
| 6941 | return -EINVAL; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6942 | |
Xin Long | e0651a0 | 2019-01-28 15:08:38 +0800 | [diff] [blame] | 6943 | params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst; |
Neil Horman | 219b99a | 2008-03-05 13:44:46 -0800 | [diff] [blame] | 6944 | |
| 6945 | if (len == sizeof(int)) { |
| 6946 | if (copy_to_user(optval, ¶ms.assoc_value, len)) |
| 6947 | return -EFAULT; |
| 6948 | } else { |
| 6949 | if (copy_to_user(optval, ¶ms, len)) |
| 6950 | return -EFAULT; |
| 6951 | } |
| 6952 | |
| 6953 | return 0; |
| 6954 | |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 6955 | } |
| 6956 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6957 | static int sctp_getsockopt_hmac_ident(struct sock *sk, int len, |
| 6958 | char __user *optval, int __user *optlen) |
| 6959 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6960 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6961 | struct sctp_hmacalgo __user *p = (void __user *)optval; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6962 | struct sctp_hmac_algo_param *hmacs; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6963 | __u16 data_len = 0; |
| 6964 | u32 num_idents; |
Xin Long | 7a84bd4 | 2016-02-03 23:33:30 +0800 | [diff] [blame] | 6965 | int i; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6966 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6967 | if (!ep->auth_enable) |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6968 | return -EACCES; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6969 | |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6970 | hmacs = ep->auth_hmacs_list; |
Xin Long | 3c91870 | 2017-06-30 11:52:16 +0800 | [diff] [blame] | 6971 | data_len = ntohs(hmacs->param_hdr.length) - |
| 6972 | sizeof(struct sctp_paramhdr); |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6973 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6974 | if (len < sizeof(struct sctp_hmacalgo) + data_len) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6975 | return -EINVAL; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6976 | |
| 6977 | len = sizeof(struct sctp_hmacalgo) + data_len; |
| 6978 | num_idents = data_len / sizeof(u16); |
| 6979 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6980 | if (put_user(len, optlen)) |
| 6981 | return -EFAULT; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 6982 | if (put_user(num_idents, &p->shmac_num_idents)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6983 | return -EFAULT; |
Xin Long | 7a84bd4 | 2016-02-03 23:33:30 +0800 | [diff] [blame] | 6984 | for (i = 0; i < num_idents; i++) { |
| 6985 | __u16 hmacid = ntohs(hmacs->hmac_ids[i]); |
| 6986 | |
| 6987 | if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16))) |
| 6988 | return -EFAULT; |
| 6989 | } |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6990 | return 0; |
| 6991 | } |
| 6992 | |
| 6993 | static int sctp_getsockopt_active_key(struct sock *sk, int len, |
| 6994 | char __user *optval, int __user *optlen) |
| 6995 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 6996 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 6997 | struct sctp_authkeyid val; |
| 6998 | struct sctp_association *asoc; |
| 6999 | |
| 7000 | if (len < sizeof(struct sctp_authkeyid)) |
| 7001 | return -EINVAL; |
Marcelo Ricardo Leitner | c76f97c | 2018-01-08 19:02:29 -0200 | [diff] [blame] | 7002 | |
| 7003 | len = sizeof(struct sctp_authkeyid); |
| 7004 | if (copy_from_user(&val, optval, len)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7005 | return -EFAULT; |
| 7006 | |
| 7007 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); |
| 7008 | if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) |
| 7009 | return -EINVAL; |
| 7010 | |
Xin Long | 219f9ea | 2019-08-19 22:02:47 +0800 | [diff] [blame] | 7011 | if (asoc) { |
| 7012 | if (!asoc->peer.auth_capable) |
| 7013 | return -EACCES; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7014 | val.scact_keynumber = asoc->active_key_id; |
Xin Long | 219f9ea | 2019-08-19 22:02:47 +0800 | [diff] [blame] | 7015 | } else { |
| 7016 | if (!ep->auth_enable) |
| 7017 | return -EACCES; |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 7018 | val.scact_keynumber = ep->active_key_id; |
Xin Long | 219f9ea | 2019-08-19 22:02:47 +0800 | [diff] [blame] | 7019 | } |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7020 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7021 | if (put_user(len, optlen)) |
| 7022 | return -EFAULT; |
| 7023 | if (copy_to_user(optval, &val, len)) |
| 7024 | return -EFAULT; |
| 7025 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7026 | return 0; |
| 7027 | } |
| 7028 | |
| 7029 | static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len, |
| 7030 | char __user *optval, int __user *optlen) |
| 7031 | { |
Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 7032 | struct sctp_authchunks __user *p = (void __user *)optval; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7033 | struct sctp_authchunks val; |
| 7034 | struct sctp_association *asoc; |
| 7035 | struct sctp_chunks_param *ch; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7036 | u32 num_chunks = 0; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7037 | char __user *to; |
| 7038 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7039 | if (len < sizeof(struct sctp_authchunks)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7040 | return -EINVAL; |
| 7041 | |
Marcelo Ricardo Leitner | c76f97c | 2018-01-08 19:02:29 -0200 | [diff] [blame] | 7042 | if (copy_from_user(&val, optval, sizeof(val))) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7043 | return -EFAULT; |
| 7044 | |
Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 7045 | to = p->gauth_chunks; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7046 | asoc = sctp_id2assoc(sk, val.gauth_assoc_id); |
| 7047 | if (!asoc) |
| 7048 | return -EINVAL; |
| 7049 | |
Xin Long | 219f9ea | 2019-08-19 22:02:47 +0800 | [diff] [blame] | 7050 | if (!asoc->peer.auth_capable) |
| 7051 | return -EACCES; |
| 7052 | |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7053 | ch = asoc->peer.peer_chunks; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7054 | if (!ch) |
| 7055 | goto num; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7056 | |
| 7057 | /* See if the user provided enough room for all the data */ |
Xin Long | 3c91870 | 2017-06-30 11:52:16 +0800 | [diff] [blame] | 7058 | num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); |
Vlad Yasevich | b40db68 | 2008-02-27 14:40:37 -0500 | [diff] [blame] | 7059 | if (len < num_chunks) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7060 | return -EINVAL; |
| 7061 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7062 | if (copy_to_user(to, ch->chunks, num_chunks)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7063 | return -EFAULT; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7064 | num: |
| 7065 | len = sizeof(struct sctp_authchunks) + num_chunks; |
wangweidong | 8d72651 | 2013-12-23 12:16:53 +0800 | [diff] [blame] | 7066 | if (put_user(len, optlen)) |
| 7067 | return -EFAULT; |
Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 7068 | if (put_user(num_chunks, &p->gauth_number_of_chunks)) |
| 7069 | return -EFAULT; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7070 | return 0; |
| 7071 | } |
| 7072 | |
| 7073 | static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len, |
| 7074 | char __user *optval, int __user *optlen) |
| 7075 | { |
Vlad Yasevich | b14878c | 2014-04-17 17:26:50 +0200 | [diff] [blame] | 7076 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 7077 | struct sctp_authchunks __user *p = (void __user *)optval; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7078 | struct sctp_authchunks val; |
| 7079 | struct sctp_association *asoc; |
| 7080 | struct sctp_chunks_param *ch; |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7081 | u32 num_chunks = 0; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7082 | char __user *to; |
| 7083 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7084 | if (len < sizeof(struct sctp_authchunks)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7085 | return -EINVAL; |
| 7086 | |
Marcelo Ricardo Leitner | c76f97c | 2018-01-08 19:02:29 -0200 | [diff] [blame] | 7087 | if (copy_from_user(&val, optval, sizeof(val))) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7088 | return -EFAULT; |
| 7089 | |
Al Viro | 411223c | 2007-10-14 19:21:20 +0100 | [diff] [blame] | 7090 | to = p->gauth_chunks; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7091 | asoc = sctp_id2assoc(sk, val.gauth_assoc_id); |
Xin Long | 48c0721 | 2019-01-28 15:08:28 +0800 | [diff] [blame] | 7092 | if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC && |
| 7093 | sctp_style(sk, UDP)) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7094 | return -EINVAL; |
| 7095 | |
Xin Long | 219f9ea | 2019-08-19 22:02:47 +0800 | [diff] [blame] | 7096 | if (asoc) { |
| 7097 | if (!asoc->peer.auth_capable) |
| 7098 | return -EACCES; |
| 7099 | ch = (struct sctp_chunks_param *)asoc->c.auth_chunks; |
| 7100 | } else { |
| 7101 | if (!ep->auth_enable) |
| 7102 | return -EACCES; |
| 7103 | ch = ep->auth_chunk_list; |
| 7104 | } |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7105 | if (!ch) |
| 7106 | goto num; |
| 7107 | |
Xin Long | 3c91870 | 2017-06-30 11:52:16 +0800 | [diff] [blame] | 7108 | num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr); |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7109 | if (len < sizeof(struct sctp_authchunks) + num_chunks) |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7110 | return -EINVAL; |
| 7111 | |
Vlad Yasevich | 5e739d1 | 2008-08-21 03:34:25 -0700 | [diff] [blame] | 7112 | if (copy_to_user(to, ch->chunks, num_chunks)) |
| 7113 | return -EFAULT; |
| 7114 | num: |
| 7115 | len = sizeof(struct sctp_authchunks) + num_chunks; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7116 | if (put_user(len, optlen)) |
| 7117 | return -EFAULT; |
Vlad Yasevich | 7e8616d | 2008-02-27 16:04:52 -0500 | [diff] [blame] | 7118 | if (put_user(num_chunks, &p->gauth_number_of_chunks)) |
| 7119 | return -EFAULT; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 7120 | |
| 7121 | return 0; |
| 7122 | } |
| 7123 | |
Wei Yongjun | aea3c5c | 2008-12-25 16:57:24 -0800 | [diff] [blame] | 7124 | /* |
| 7125 | * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER) |
| 7126 | * This option gets the current number of associations that are attached |
| 7127 | * to a one-to-many style socket. The option value is an uint32_t. |
| 7128 | */ |
| 7129 | static int sctp_getsockopt_assoc_number(struct sock *sk, int len, |
| 7130 | char __user *optval, int __user *optlen) |
| 7131 | { |
| 7132 | struct sctp_sock *sp = sctp_sk(sk); |
| 7133 | struct sctp_association *asoc; |
| 7134 | u32 val = 0; |
| 7135 | |
| 7136 | if (sctp_style(sk, TCP)) |
| 7137 | return -EOPNOTSUPP; |
| 7138 | |
| 7139 | if (len < sizeof(u32)) |
| 7140 | return -EINVAL; |
| 7141 | |
| 7142 | len = sizeof(u32); |
| 7143 | |
| 7144 | list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { |
| 7145 | val++; |
| 7146 | } |
| 7147 | |
| 7148 | if (put_user(len, optlen)) |
| 7149 | return -EFAULT; |
| 7150 | if (copy_to_user(optval, &val, len)) |
| 7151 | return -EFAULT; |
| 7152 | |
| 7153 | return 0; |
| 7154 | } |
| 7155 | |
Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 7156 | /* |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 7157 | * 8.1.23 SCTP_AUTO_ASCONF |
| 7158 | * See the corresponding setsockopt entry as description |
| 7159 | */ |
| 7160 | static int sctp_getsockopt_auto_asconf(struct sock *sk, int len, |
| 7161 | char __user *optval, int __user *optlen) |
| 7162 | { |
| 7163 | int val = 0; |
| 7164 | |
| 7165 | if (len < sizeof(int)) |
| 7166 | return -EINVAL; |
| 7167 | |
| 7168 | len = sizeof(int); |
| 7169 | if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk)) |
| 7170 | val = 1; |
| 7171 | if (put_user(len, optlen)) |
| 7172 | return -EFAULT; |
| 7173 | if (copy_to_user(optval, &val, len)) |
| 7174 | return -EFAULT; |
| 7175 | return 0; |
| 7176 | } |
| 7177 | |
| 7178 | /* |
Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 7179 | * 8.2.6. Get the Current Identifiers of Associations |
| 7180 | * (SCTP_GET_ASSOC_ID_LIST) |
| 7181 | * |
| 7182 | * This option gets the current list of SCTP association identifiers of |
| 7183 | * the SCTP associations handled by a one-to-many style socket. |
| 7184 | */ |
| 7185 | static int sctp_getsockopt_assoc_ids(struct sock *sk, int len, |
| 7186 | char __user *optval, int __user *optlen) |
| 7187 | { |
| 7188 | struct sctp_sock *sp = sctp_sk(sk); |
| 7189 | struct sctp_association *asoc; |
| 7190 | struct sctp_assoc_ids *ids; |
| 7191 | u32 num = 0; |
| 7192 | |
| 7193 | if (sctp_style(sk, TCP)) |
| 7194 | return -EOPNOTSUPP; |
| 7195 | |
| 7196 | if (len < sizeof(struct sctp_assoc_ids)) |
| 7197 | return -EINVAL; |
| 7198 | |
| 7199 | list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { |
| 7200 | num++; |
| 7201 | } |
| 7202 | |
| 7203 | if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num) |
| 7204 | return -EINVAL; |
| 7205 | |
| 7206 | len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num; |
| 7207 | |
Marcelo Ricardo Leitner | 9ba0b96 | 2015-12-23 16:28:40 -0200 | [diff] [blame] | 7208 | ids = kmalloc(len, GFP_USER | __GFP_NOWARN); |
Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 7209 | if (unlikely(!ids)) |
| 7210 | return -ENOMEM; |
| 7211 | |
| 7212 | ids->gaids_number_of_ids = num; |
| 7213 | num = 0; |
| 7214 | list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { |
| 7215 | ids->gaids_assoc_id[num++] = asoc->assoc_id; |
| 7216 | } |
| 7217 | |
| 7218 | if (put_user(len, optlen) || copy_to_user(optval, ids, len)) { |
| 7219 | kfree(ids); |
| 7220 | return -EFAULT; |
| 7221 | } |
| 7222 | |
| 7223 | kfree(ids); |
| 7224 | return 0; |
| 7225 | } |
| 7226 | |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7227 | /* |
| 7228 | * SCTP_PEER_ADDR_THLDS |
| 7229 | * |
| 7230 | * This option allows us to fetch the partially failed threshold for one or all |
| 7231 | * transports in an association. See Section 6.1 of: |
| 7232 | * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt |
| 7233 | */ |
| 7234 | static int sctp_getsockopt_paddr_thresholds(struct sock *sk, |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 7235 | char __user *optval, int len, |
| 7236 | int __user *optlen, bool v2) |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7237 | { |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 7238 | struct sctp_paddrthlds_v2 val; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7239 | struct sctp_transport *trans; |
| 7240 | struct sctp_association *asoc; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 7241 | int min; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7242 | |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 7243 | min = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds); |
| 7244 | if (len < min) |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7245 | return -EINVAL; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 7246 | len = min; |
| 7247 | if (copy_from_user(&val, optval, len)) |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7248 | return -EFAULT; |
| 7249 | |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 7250 | if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) { |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7251 | trans = sctp_addr_id2transport(sk, &val.spt_address, |
| 7252 | val.spt_assoc_id); |
| 7253 | if (!trans) |
| 7254 | return -ENOENT; |
| 7255 | |
| 7256 | val.spt_pathmaxrxt = trans->pathmaxrxt; |
| 7257 | val.spt_pathpfthld = trans->pf_retrans; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 7258 | val.spt_pathcpthld = trans->ps_retrans; |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 7259 | |
Xin Long | f794dc2 | 2019-09-09 15:33:29 +0800 | [diff] [blame] | 7260 | goto out; |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 7261 | } |
| 7262 | |
| 7263 | asoc = sctp_id2assoc(sk, val.spt_assoc_id); |
| 7264 | if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC && |
| 7265 | sctp_style(sk, UDP)) |
| 7266 | return -EINVAL; |
| 7267 | |
| 7268 | if (asoc) { |
| 7269 | val.spt_pathpfthld = asoc->pf_retrans; |
| 7270 | val.spt_pathmaxrxt = asoc->pathmaxrxt; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 7271 | val.spt_pathcpthld = asoc->ps_retrans; |
Xin Long | 8add543 | 2019-01-28 15:08:29 +0800 | [diff] [blame] | 7272 | } else { |
| 7273 | struct sctp_sock *sp = sctp_sk(sk); |
| 7274 | |
| 7275 | val.spt_pathpfthld = sp->pf_retrans; |
| 7276 | val.spt_pathmaxrxt = sp->pathmaxrxt; |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 7277 | val.spt_pathcpthld = sp->ps_retrans; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7278 | } |
| 7279 | |
Xin Long | f794dc2 | 2019-09-09 15:33:29 +0800 | [diff] [blame] | 7280 | out: |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 7281 | if (put_user(len, optlen) || copy_to_user(optval, &val, len)) |
| 7282 | return -EFAULT; |
| 7283 | |
| 7284 | return 0; |
| 7285 | } |
| 7286 | |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 7287 | /* |
| 7288 | * SCTP_GET_ASSOC_STATS |
| 7289 | * |
| 7290 | * This option retrieves local per endpoint statistics. It is modeled |
| 7291 | * after OpenSolaris' implementation |
| 7292 | */ |
| 7293 | static int sctp_getsockopt_assoc_stats(struct sock *sk, int len, |
| 7294 | char __user *optval, |
| 7295 | int __user *optlen) |
| 7296 | { |
| 7297 | struct sctp_assoc_stats sas; |
| 7298 | struct sctp_association *asoc = NULL; |
| 7299 | |
| 7300 | /* User must provide at least the assoc id */ |
| 7301 | if (len < sizeof(sctp_assoc_t)) |
| 7302 | return -EINVAL; |
| 7303 | |
Guenter Roeck | 726bc6b | 2013-02-27 10:57:31 +0000 | [diff] [blame] | 7304 | /* Allow the struct to grow and fill in as much as possible */ |
| 7305 | len = min_t(size_t, len, sizeof(sas)); |
| 7306 | |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 7307 | if (copy_from_user(&sas, optval, len)) |
| 7308 | return -EFAULT; |
| 7309 | |
| 7310 | asoc = sctp_id2assoc(sk, sas.sas_assoc_id); |
| 7311 | if (!asoc) |
| 7312 | return -EINVAL; |
| 7313 | |
| 7314 | sas.sas_rtxchunks = asoc->stats.rtxchunks; |
| 7315 | sas.sas_gapcnt = asoc->stats.gapcnt; |
| 7316 | sas.sas_outofseqtsns = asoc->stats.outofseqtsns; |
| 7317 | sas.sas_osacks = asoc->stats.osacks; |
| 7318 | sas.sas_isacks = asoc->stats.isacks; |
| 7319 | sas.sas_octrlchunks = asoc->stats.octrlchunks; |
| 7320 | sas.sas_ictrlchunks = asoc->stats.ictrlchunks; |
| 7321 | sas.sas_oodchunks = asoc->stats.oodchunks; |
| 7322 | sas.sas_iodchunks = asoc->stats.iodchunks; |
| 7323 | sas.sas_ouodchunks = asoc->stats.ouodchunks; |
| 7324 | sas.sas_iuodchunks = asoc->stats.iuodchunks; |
| 7325 | sas.sas_idupchunks = asoc->stats.idupchunks; |
| 7326 | sas.sas_opackets = asoc->stats.opackets; |
| 7327 | sas.sas_ipackets = asoc->stats.ipackets; |
| 7328 | |
| 7329 | /* New high max rto observed, will return 0 if not a single |
| 7330 | * RTO update took place. obs_rto_ipaddr will be bogus |
| 7331 | * in such a case |
| 7332 | */ |
| 7333 | sas.sas_maxrto = asoc->stats.max_obs_rto; |
| 7334 | memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr, |
| 7335 | sizeof(struct sockaddr_storage)); |
| 7336 | |
| 7337 | /* Mark beginning of a new observation period */ |
| 7338 | asoc->stats.max_obs_rto = asoc->rto_min; |
| 7339 | |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 7340 | if (put_user(len, optlen)) |
| 7341 | return -EFAULT; |
| 7342 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 7343 | 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] | 7344 | |
| 7345 | if (copy_to_user(optval, &sas, len)) |
| 7346 | return -EFAULT; |
| 7347 | |
| 7348 | return 0; |
| 7349 | } |
| 7350 | |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 7351 | static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len, |
| 7352 | char __user *optval, |
| 7353 | int __user *optlen) |
| 7354 | { |
| 7355 | int val = 0; |
| 7356 | |
| 7357 | if (len < sizeof(int)) |
| 7358 | return -EINVAL; |
| 7359 | |
| 7360 | len = sizeof(int); |
| 7361 | if (sctp_sk(sk)->recvrcvinfo) |
| 7362 | val = 1; |
| 7363 | if (put_user(len, optlen)) |
| 7364 | return -EFAULT; |
| 7365 | if (copy_to_user(optval, &val, len)) |
| 7366 | return -EFAULT; |
| 7367 | |
| 7368 | return 0; |
| 7369 | } |
| 7370 | |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 7371 | static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len, |
| 7372 | char __user *optval, |
| 7373 | int __user *optlen) |
| 7374 | { |
| 7375 | int val = 0; |
| 7376 | |
| 7377 | if (len < sizeof(int)) |
| 7378 | return -EINVAL; |
| 7379 | |
| 7380 | len = sizeof(int); |
| 7381 | if (sctp_sk(sk)->recvnxtinfo) |
| 7382 | val = 1; |
| 7383 | if (put_user(len, optlen)) |
| 7384 | return -EFAULT; |
| 7385 | if (copy_to_user(optval, &val, len)) |
| 7386 | return -EFAULT; |
| 7387 | |
| 7388 | return 0; |
| 7389 | } |
| 7390 | |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 7391 | static int sctp_getsockopt_pr_supported(struct sock *sk, int len, |
| 7392 | char __user *optval, |
| 7393 | int __user *optlen) |
| 7394 | { |
| 7395 | struct sctp_assoc_value params; |
| 7396 | struct sctp_association *asoc; |
| 7397 | int retval = -EFAULT; |
| 7398 | |
| 7399 | if (len < sizeof(params)) { |
| 7400 | retval = -EINVAL; |
| 7401 | goto out; |
| 7402 | } |
| 7403 | |
| 7404 | len = sizeof(params); |
| 7405 | if (copy_from_user(¶ms, optval, len)) |
| 7406 | goto out; |
| 7407 | |
| 7408 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | fb19560 | 2019-01-28 15:08:30 +0800 | [diff] [blame] | 7409 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 7410 | sctp_style(sk, UDP)) { |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 7411 | retval = -EINVAL; |
| 7412 | goto out; |
| 7413 | } |
| 7414 | |
Xin Long | 1c13475 | 2019-07-09 00:57:05 +0800 | [diff] [blame] | 7415 | params.assoc_value = asoc ? asoc->peer.prsctp_capable |
Xin Long | fb19560 | 2019-01-28 15:08:30 +0800 | [diff] [blame] | 7416 | : sctp_sk(sk)->ep->prsctp_enable; |
| 7417 | |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 7418 | if (put_user(len, optlen)) |
| 7419 | goto out; |
| 7420 | |
| 7421 | if (copy_to_user(optval, ¶ms, len)) |
| 7422 | goto out; |
| 7423 | |
| 7424 | retval = 0; |
| 7425 | |
| 7426 | out: |
| 7427 | return retval; |
| 7428 | } |
| 7429 | |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 7430 | static int sctp_getsockopt_default_prinfo(struct sock *sk, int len, |
| 7431 | char __user *optval, |
| 7432 | int __user *optlen) |
| 7433 | { |
| 7434 | struct sctp_default_prinfo info; |
| 7435 | struct sctp_association *asoc; |
| 7436 | int retval = -EFAULT; |
| 7437 | |
| 7438 | if (len < sizeof(info)) { |
| 7439 | retval = -EINVAL; |
| 7440 | goto out; |
| 7441 | } |
| 7442 | |
| 7443 | len = sizeof(info); |
| 7444 | if (copy_from_user(&info, optval, len)) |
| 7445 | goto out; |
| 7446 | |
| 7447 | asoc = sctp_id2assoc(sk, info.pr_assoc_id); |
Xin Long | 3a58305 | 2019-01-28 15:08:43 +0800 | [diff] [blame] | 7448 | if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC && |
| 7449 | sctp_style(sk, UDP)) { |
| 7450 | retval = -EINVAL; |
| 7451 | goto out; |
| 7452 | } |
| 7453 | |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 7454 | if (asoc) { |
| 7455 | info.pr_policy = SCTP_PR_POLICY(asoc->default_flags); |
| 7456 | info.pr_value = asoc->default_timetolive; |
Xin Long | 3a58305 | 2019-01-28 15:08:43 +0800 | [diff] [blame] | 7457 | } else { |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 7458 | struct sctp_sock *sp = sctp_sk(sk); |
| 7459 | |
| 7460 | info.pr_policy = SCTP_PR_POLICY(sp->default_flags); |
| 7461 | info.pr_value = sp->default_timetolive; |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 7462 | } |
| 7463 | |
| 7464 | if (put_user(len, optlen)) |
| 7465 | goto out; |
| 7466 | |
| 7467 | if (copy_to_user(optval, &info, len)) |
| 7468 | goto out; |
| 7469 | |
| 7470 | retval = 0; |
| 7471 | |
| 7472 | out: |
| 7473 | return retval; |
| 7474 | } |
| 7475 | |
Xin Long | 826d253 | 2016-07-09 19:47:42 +0800 | [diff] [blame] | 7476 | static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len, |
| 7477 | char __user *optval, |
| 7478 | int __user *optlen) |
| 7479 | { |
| 7480 | struct sctp_prstatus params; |
| 7481 | struct sctp_association *asoc; |
| 7482 | int policy; |
| 7483 | int retval = -EINVAL; |
| 7484 | |
| 7485 | if (len < sizeof(params)) |
| 7486 | goto out; |
| 7487 | |
| 7488 | len = sizeof(params); |
| 7489 | if (copy_from_user(¶ms, optval, len)) { |
| 7490 | retval = -EFAULT; |
| 7491 | goto out; |
| 7492 | } |
| 7493 | |
| 7494 | policy = params.sprstat_policy; |
Xin Long | 7133583 | 2018-10-29 23:13:11 +0800 | [diff] [blame] | 7495 | if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) || |
| 7496 | ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK))) |
Xin Long | 826d253 | 2016-07-09 19:47:42 +0800 | [diff] [blame] | 7497 | goto out; |
| 7498 | |
| 7499 | asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); |
| 7500 | if (!asoc) |
| 7501 | goto out; |
| 7502 | |
Xin Long | 7133583 | 2018-10-29 23:13:11 +0800 | [diff] [blame] | 7503 | if (policy == SCTP_PR_SCTP_ALL) { |
Xin Long | 826d253 | 2016-07-09 19:47:42 +0800 | [diff] [blame] | 7504 | params.sprstat_abandoned_unsent = 0; |
| 7505 | params.sprstat_abandoned_sent = 0; |
| 7506 | for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { |
| 7507 | params.sprstat_abandoned_unsent += |
| 7508 | asoc->abandoned_unsent[policy]; |
| 7509 | params.sprstat_abandoned_sent += |
| 7510 | asoc->abandoned_sent[policy]; |
| 7511 | } |
| 7512 | } else { |
| 7513 | params.sprstat_abandoned_unsent = |
| 7514 | asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)]; |
| 7515 | params.sprstat_abandoned_sent = |
| 7516 | asoc->abandoned_sent[__SCTP_PR_INDEX(policy)]; |
| 7517 | } |
| 7518 | |
| 7519 | if (put_user(len, optlen)) { |
| 7520 | retval = -EFAULT; |
| 7521 | goto out; |
| 7522 | } |
| 7523 | |
| 7524 | if (copy_to_user(optval, ¶ms, len)) { |
| 7525 | retval = -EFAULT; |
| 7526 | goto out; |
| 7527 | } |
| 7528 | |
| 7529 | retval = 0; |
| 7530 | |
| 7531 | out: |
| 7532 | return retval; |
| 7533 | } |
| 7534 | |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7535 | static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len, |
| 7536 | char __user *optval, |
| 7537 | int __user *optlen) |
| 7538 | { |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7539 | struct sctp_stream_out_ext *streamoute; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7540 | struct sctp_association *asoc; |
| 7541 | struct sctp_prstatus params; |
| 7542 | int retval = -EINVAL; |
| 7543 | int policy; |
| 7544 | |
| 7545 | if (len < sizeof(params)) |
| 7546 | goto out; |
| 7547 | |
| 7548 | len = sizeof(params); |
| 7549 | if (copy_from_user(¶ms, optval, len)) { |
| 7550 | retval = -EFAULT; |
| 7551 | goto out; |
| 7552 | } |
| 7553 | |
| 7554 | policy = params.sprstat_policy; |
Xin Long | 7133583 | 2018-10-29 23:13:11 +0800 | [diff] [blame] | 7555 | if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) || |
| 7556 | ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK))) |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7557 | goto out; |
| 7558 | |
| 7559 | asoc = sctp_id2assoc(sk, params.sprstat_assoc_id); |
Xin Long | cee360a | 2017-05-31 16:36:31 +0800 | [diff] [blame] | 7560 | if (!asoc || params.sprstat_sid >= asoc->stream.outcnt) |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7561 | goto out; |
| 7562 | |
Konstantin Khorenko | 05364ca | 2018-08-10 20:11:42 +0300 | [diff] [blame] | 7563 | streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext; |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7564 | if (!streamoute) { |
| 7565 | /* Not allocated yet, means all stats are 0 */ |
| 7566 | params.sprstat_abandoned_unsent = 0; |
| 7567 | params.sprstat_abandoned_sent = 0; |
| 7568 | retval = 0; |
| 7569 | goto out; |
| 7570 | } |
| 7571 | |
Xin Long | 0ac1077 | 2018-10-16 15:52:02 +0800 | [diff] [blame] | 7572 | if (policy == SCTP_PR_SCTP_ALL) { |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7573 | params.sprstat_abandoned_unsent = 0; |
| 7574 | params.sprstat_abandoned_sent = 0; |
| 7575 | for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) { |
| 7576 | params.sprstat_abandoned_unsent += |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7577 | streamoute->abandoned_unsent[policy]; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7578 | params.sprstat_abandoned_sent += |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7579 | streamoute->abandoned_sent[policy]; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7580 | } |
| 7581 | } else { |
| 7582 | params.sprstat_abandoned_unsent = |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7583 | streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)]; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7584 | params.sprstat_abandoned_sent = |
Marcelo Ricardo Leitner | f952be7 | 2017-10-03 19:20:11 -0300 | [diff] [blame] | 7585 | streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)]; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 7586 | } |
| 7587 | |
| 7588 | if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) { |
| 7589 | retval = -EFAULT; |
| 7590 | goto out; |
| 7591 | } |
| 7592 | |
| 7593 | retval = 0; |
| 7594 | |
| 7595 | out: |
| 7596 | return retval; |
| 7597 | } |
| 7598 | |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 7599 | static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len, |
| 7600 | char __user *optval, |
| 7601 | int __user *optlen) |
| 7602 | { |
| 7603 | struct sctp_assoc_value params; |
| 7604 | struct sctp_association *asoc; |
| 7605 | int retval = -EFAULT; |
| 7606 | |
| 7607 | if (len < sizeof(params)) { |
| 7608 | retval = -EINVAL; |
| 7609 | goto out; |
| 7610 | } |
| 7611 | |
| 7612 | len = sizeof(params); |
| 7613 | if (copy_from_user(¶ms, optval, len)) |
| 7614 | goto out; |
| 7615 | |
| 7616 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | acce7f3 | 2019-01-28 15:08:31 +0800 | [diff] [blame] | 7617 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 7618 | sctp_style(sk, UDP)) { |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 7619 | retval = -EINVAL; |
| 7620 | goto out; |
| 7621 | } |
| 7622 | |
Xin Long | a96701f | 2019-07-09 00:57:04 +0800 | [diff] [blame] | 7623 | params.assoc_value = asoc ? asoc->peer.reconf_capable |
Xin Long | acce7f3 | 2019-01-28 15:08:31 +0800 | [diff] [blame] | 7624 | : sctp_sk(sk)->ep->reconf_enable; |
| 7625 | |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 7626 | if (put_user(len, optlen)) |
| 7627 | goto out; |
| 7628 | |
| 7629 | if (copy_to_user(optval, ¶ms, len)) |
| 7630 | goto out; |
| 7631 | |
| 7632 | retval = 0; |
| 7633 | |
| 7634 | out: |
| 7635 | return retval; |
| 7636 | } |
| 7637 | |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 7638 | static int sctp_getsockopt_enable_strreset(struct sock *sk, int len, |
| 7639 | char __user *optval, |
| 7640 | int __user *optlen) |
| 7641 | { |
| 7642 | struct sctp_assoc_value params; |
| 7643 | struct sctp_association *asoc; |
| 7644 | int retval = -EFAULT; |
| 7645 | |
| 7646 | if (len < sizeof(params)) { |
| 7647 | retval = -EINVAL; |
| 7648 | goto out; |
| 7649 | } |
| 7650 | |
| 7651 | len = sizeof(params); |
| 7652 | if (copy_from_user(¶ms, optval, len)) |
| 7653 | goto out; |
| 7654 | |
| 7655 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | 99a6213 | 2019-01-28 15:08:44 +0800 | [diff] [blame] | 7656 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 7657 | sctp_style(sk, UDP)) { |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 7658 | retval = -EINVAL; |
| 7659 | goto out; |
| 7660 | } |
| 7661 | |
Xin Long | 99a6213 | 2019-01-28 15:08:44 +0800 | [diff] [blame] | 7662 | params.assoc_value = asoc ? asoc->strreset_enable |
| 7663 | : sctp_sk(sk)->ep->strreset_enable; |
| 7664 | |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 7665 | if (put_user(len, optlen)) |
| 7666 | goto out; |
| 7667 | |
| 7668 | if (copy_to_user(optval, ¶ms, len)) |
| 7669 | goto out; |
| 7670 | |
| 7671 | retval = 0; |
| 7672 | |
| 7673 | out: |
| 7674 | return retval; |
| 7675 | } |
| 7676 | |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 7677 | static int sctp_getsockopt_scheduler(struct sock *sk, int len, |
| 7678 | char __user *optval, |
| 7679 | int __user *optlen) |
| 7680 | { |
| 7681 | struct sctp_assoc_value params; |
| 7682 | struct sctp_association *asoc; |
| 7683 | int retval = -EFAULT; |
| 7684 | |
| 7685 | if (len < sizeof(params)) { |
| 7686 | retval = -EINVAL; |
| 7687 | goto out; |
| 7688 | } |
| 7689 | |
| 7690 | len = sizeof(params); |
| 7691 | if (copy_from_user(¶ms, optval, len)) |
| 7692 | goto out; |
| 7693 | |
| 7694 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 7695 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 7696 | sctp_style(sk, UDP)) { |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 7697 | retval = -EINVAL; |
| 7698 | goto out; |
| 7699 | } |
| 7700 | |
Xin Long | 7efba10 | 2019-01-28 15:08:46 +0800 | [diff] [blame] | 7701 | params.assoc_value = asoc ? sctp_sched_get_sched(asoc) |
| 7702 | : sctp_sk(sk)->default_ss; |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 7703 | |
| 7704 | if (put_user(len, optlen)) |
| 7705 | goto out; |
| 7706 | |
| 7707 | if (copy_to_user(optval, ¶ms, len)) |
| 7708 | goto out; |
| 7709 | |
| 7710 | retval = 0; |
| 7711 | |
| 7712 | out: |
| 7713 | return retval; |
| 7714 | } |
| 7715 | |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 7716 | static int sctp_getsockopt_scheduler_value(struct sock *sk, int len, |
| 7717 | char __user *optval, |
| 7718 | int __user *optlen) |
| 7719 | { |
| 7720 | struct sctp_stream_value params; |
| 7721 | struct sctp_association *asoc; |
| 7722 | int retval = -EFAULT; |
| 7723 | |
| 7724 | if (len < sizeof(params)) { |
| 7725 | retval = -EINVAL; |
| 7726 | goto out; |
| 7727 | } |
| 7728 | |
| 7729 | len = sizeof(params); |
| 7730 | if (copy_from_user(¶ms, optval, len)) |
| 7731 | goto out; |
| 7732 | |
| 7733 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 7734 | if (!asoc) { |
| 7735 | retval = -EINVAL; |
| 7736 | goto out; |
| 7737 | } |
| 7738 | |
| 7739 | retval = sctp_sched_get_value(asoc, params.stream_id, |
| 7740 | ¶ms.stream_value); |
| 7741 | if (retval) |
| 7742 | goto out; |
| 7743 | |
| 7744 | if (put_user(len, optlen)) { |
| 7745 | retval = -EFAULT; |
| 7746 | goto out; |
| 7747 | } |
| 7748 | |
| 7749 | if (copy_to_user(optval, ¶ms, len)) { |
| 7750 | retval = -EFAULT; |
| 7751 | goto out; |
| 7752 | } |
| 7753 | |
| 7754 | out: |
| 7755 | return retval; |
| 7756 | } |
| 7757 | |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 7758 | static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len, |
| 7759 | char __user *optval, |
| 7760 | int __user *optlen) |
| 7761 | { |
| 7762 | struct sctp_assoc_value params; |
| 7763 | struct sctp_association *asoc; |
| 7764 | int retval = -EFAULT; |
| 7765 | |
| 7766 | if (len < sizeof(params)) { |
| 7767 | retval = -EINVAL; |
| 7768 | goto out; |
| 7769 | } |
| 7770 | |
| 7771 | len = sizeof(params); |
| 7772 | if (copy_from_user(¶ms, optval, len)) |
| 7773 | goto out; |
| 7774 | |
| 7775 | asoc = sctp_id2assoc(sk, params.assoc_id); |
Xin Long | 2e7709d | 2019-01-28 15:08:32 +0800 | [diff] [blame] | 7776 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 7777 | sctp_style(sk, UDP)) { |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 7778 | retval = -EINVAL; |
| 7779 | goto out; |
| 7780 | } |
| 7781 | |
Xin Long | da1f6d4 | 2019-07-09 00:57:06 +0800 | [diff] [blame] | 7782 | params.assoc_value = asoc ? asoc->peer.intl_capable |
Xin Long | e55f4b8 | 2019-07-09 00:57:07 +0800 | [diff] [blame] | 7783 | : sctp_sk(sk)->ep->intl_enable; |
Xin Long | 2e7709d | 2019-01-28 15:08:32 +0800 | [diff] [blame] | 7784 | |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 7785 | if (put_user(len, optlen)) |
| 7786 | goto out; |
| 7787 | |
| 7788 | if (copy_to_user(optval, ¶ms, len)) |
| 7789 | goto out; |
| 7790 | |
| 7791 | retval = 0; |
| 7792 | |
| 7793 | out: |
| 7794 | return retval; |
| 7795 | } |
| 7796 | |
Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 7797 | static int sctp_getsockopt_reuse_port(struct sock *sk, int len, |
| 7798 | char __user *optval, |
| 7799 | int __user *optlen) |
| 7800 | { |
| 7801 | int val; |
| 7802 | |
| 7803 | if (len < sizeof(int)) |
| 7804 | return -EINVAL; |
| 7805 | |
| 7806 | len = sizeof(int); |
| 7807 | val = sctp_sk(sk)->reuse; |
| 7808 | if (put_user(len, optlen)) |
| 7809 | return -EFAULT; |
| 7810 | |
| 7811 | if (copy_to_user(optval, &val, len)) |
| 7812 | return -EFAULT; |
| 7813 | |
| 7814 | return 0; |
| 7815 | } |
| 7816 | |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 7817 | static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval, |
| 7818 | int __user *optlen) |
| 7819 | { |
| 7820 | struct sctp_association *asoc; |
| 7821 | struct sctp_event param; |
| 7822 | __u16 subscribe; |
| 7823 | |
| 7824 | if (len < sizeof(param)) |
| 7825 | return -EINVAL; |
| 7826 | |
| 7827 | len = sizeof(param); |
| 7828 | if (copy_from_user(¶m, optval, len)) |
| 7829 | return -EFAULT; |
| 7830 | |
| 7831 | if (param.se_type < SCTP_SN_TYPE_BASE || |
| 7832 | param.se_type > SCTP_SN_TYPE_MAX) |
| 7833 | return -EINVAL; |
| 7834 | |
| 7835 | asoc = sctp_id2assoc(sk, param.se_assoc_id); |
Xin Long | d251f05 | 2019-01-28 15:08:45 +0800 | [diff] [blame] | 7836 | if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC && |
| 7837 | sctp_style(sk, UDP)) |
| 7838 | return -EINVAL; |
| 7839 | |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 7840 | subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe; |
| 7841 | param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type); |
| 7842 | |
| 7843 | if (put_user(len, optlen)) |
| 7844 | return -EFAULT; |
| 7845 | |
| 7846 | if (copy_to_user(optval, ¶m, len)) |
| 7847 | return -EFAULT; |
| 7848 | |
| 7849 | return 0; |
| 7850 | } |
| 7851 | |
Xin Long | df2c71f | 2019-08-19 22:02:46 +0800 | [diff] [blame] | 7852 | static int sctp_getsockopt_asconf_supported(struct sock *sk, int len, |
| 7853 | char __user *optval, |
| 7854 | int __user *optlen) |
| 7855 | { |
| 7856 | struct sctp_assoc_value params; |
| 7857 | struct sctp_association *asoc; |
| 7858 | int retval = -EFAULT; |
| 7859 | |
| 7860 | if (len < sizeof(params)) { |
| 7861 | retval = -EINVAL; |
| 7862 | goto out; |
| 7863 | } |
| 7864 | |
| 7865 | len = sizeof(params); |
| 7866 | if (copy_from_user(¶ms, optval, len)) |
| 7867 | goto out; |
| 7868 | |
| 7869 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 7870 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 7871 | sctp_style(sk, UDP)) { |
| 7872 | retval = -EINVAL; |
| 7873 | goto out; |
| 7874 | } |
| 7875 | |
| 7876 | params.assoc_value = asoc ? asoc->peer.asconf_capable |
| 7877 | : sctp_sk(sk)->ep->asconf_enable; |
| 7878 | |
| 7879 | if (put_user(len, optlen)) |
| 7880 | goto out; |
| 7881 | |
| 7882 | if (copy_to_user(optval, ¶ms, len)) |
| 7883 | goto out; |
| 7884 | |
| 7885 | retval = 0; |
| 7886 | |
| 7887 | out: |
| 7888 | return retval; |
| 7889 | } |
| 7890 | |
Xin Long | 56dd525 | 2019-08-19 22:02:49 +0800 | [diff] [blame] | 7891 | static int sctp_getsockopt_auth_supported(struct sock *sk, int len, |
| 7892 | char __user *optval, |
| 7893 | int __user *optlen) |
| 7894 | { |
| 7895 | struct sctp_assoc_value params; |
| 7896 | struct sctp_association *asoc; |
| 7897 | int retval = -EFAULT; |
| 7898 | |
| 7899 | if (len < sizeof(params)) { |
| 7900 | retval = -EINVAL; |
| 7901 | goto out; |
| 7902 | } |
| 7903 | |
| 7904 | len = sizeof(params); |
| 7905 | if (copy_from_user(¶ms, optval, len)) |
| 7906 | goto out; |
| 7907 | |
| 7908 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 7909 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 7910 | sctp_style(sk, UDP)) { |
| 7911 | retval = -EINVAL; |
| 7912 | goto out; |
| 7913 | } |
| 7914 | |
| 7915 | params.assoc_value = asoc ? asoc->peer.auth_capable |
| 7916 | : sctp_sk(sk)->ep->auth_enable; |
| 7917 | |
| 7918 | if (put_user(len, optlen)) |
| 7919 | goto out; |
| 7920 | |
| 7921 | if (copy_to_user(optval, ¶ms, len)) |
| 7922 | goto out; |
| 7923 | |
| 7924 | retval = 0; |
| 7925 | |
| 7926 | out: |
| 7927 | return retval; |
| 7928 | } |
| 7929 | |
Xin Long | d5886b9 | 2019-08-26 16:30:04 +0800 | [diff] [blame] | 7930 | static int sctp_getsockopt_ecn_supported(struct sock *sk, int len, |
| 7931 | char __user *optval, |
| 7932 | int __user *optlen) |
| 7933 | { |
| 7934 | struct sctp_assoc_value params; |
| 7935 | struct sctp_association *asoc; |
| 7936 | int retval = -EFAULT; |
| 7937 | |
| 7938 | if (len < sizeof(params)) { |
| 7939 | retval = -EINVAL; |
| 7940 | goto out; |
| 7941 | } |
| 7942 | |
| 7943 | len = sizeof(params); |
| 7944 | if (copy_from_user(¶ms, optval, len)) |
| 7945 | goto out; |
| 7946 | |
| 7947 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 7948 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 7949 | sctp_style(sk, UDP)) { |
| 7950 | retval = -EINVAL; |
| 7951 | goto out; |
| 7952 | } |
| 7953 | |
| 7954 | params.assoc_value = asoc ? asoc->peer.ecn_capable |
| 7955 | : sctp_sk(sk)->ep->ecn_enable; |
| 7956 | |
| 7957 | if (put_user(len, optlen)) |
| 7958 | goto out; |
| 7959 | |
| 7960 | if (copy_to_user(optval, ¶ms, len)) |
| 7961 | goto out; |
| 7962 | |
| 7963 | retval = 0; |
| 7964 | |
| 7965 | out: |
| 7966 | return retval; |
| 7967 | } |
| 7968 | |
Xin Long | 8d2a693 | 2019-11-08 13:20:34 +0800 | [diff] [blame] | 7969 | static int sctp_getsockopt_pf_expose(struct sock *sk, int len, |
| 7970 | char __user *optval, |
| 7971 | int __user *optlen) |
| 7972 | { |
| 7973 | struct sctp_assoc_value params; |
| 7974 | struct sctp_association *asoc; |
| 7975 | int retval = -EFAULT; |
| 7976 | |
| 7977 | if (len < sizeof(params)) { |
| 7978 | retval = -EINVAL; |
| 7979 | goto out; |
| 7980 | } |
| 7981 | |
| 7982 | len = sizeof(params); |
| 7983 | if (copy_from_user(¶ms, optval, len)) |
| 7984 | goto out; |
| 7985 | |
| 7986 | asoc = sctp_id2assoc(sk, params.assoc_id); |
| 7987 | if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC && |
| 7988 | sctp_style(sk, UDP)) { |
| 7989 | retval = -EINVAL; |
| 7990 | goto out; |
| 7991 | } |
| 7992 | |
| 7993 | params.assoc_value = asoc ? asoc->pf_expose |
| 7994 | : sctp_sk(sk)->pf_expose; |
| 7995 | |
| 7996 | if (put_user(len, optlen)) |
| 7997 | goto out; |
| 7998 | |
| 7999 | if (copy_to_user(optval, ¶ms, len)) |
| 8000 | goto out; |
| 8001 | |
| 8002 | retval = 0; |
| 8003 | |
| 8004 | out: |
| 8005 | return retval; |
| 8006 | } |
| 8007 | |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 8008 | static int sctp_getsockopt(struct sock *sk, int level, int optname, |
| 8009 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8010 | { |
| 8011 | int retval = 0; |
| 8012 | int len; |
| 8013 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 8014 | pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8015 | |
| 8016 | /* I can hardly begin to describe how wrong this is. This is |
| 8017 | * so broken as to be worse than useless. The API draft |
| 8018 | * REALLY is NOT helpful here... I am not convinced that the |
| 8019 | * semantics of getsockopt() with a level OTHER THAN SOL_SCTP |
| 8020 | * are at all well-founded. |
| 8021 | */ |
| 8022 | if (level != SOL_SCTP) { |
| 8023 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
| 8024 | |
| 8025 | retval = af->getsockopt(sk, level, optname, optval, optlen); |
| 8026 | return retval; |
| 8027 | } |
| 8028 | |
| 8029 | if (get_user(len, optlen)) |
| 8030 | return -EFAULT; |
| 8031 | |
Jiri Slaby | a4b8e71 | 2016-10-21 14:13:24 +0200 | [diff] [blame] | 8032 | if (len < 0) |
| 8033 | return -EINVAL; |
| 8034 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8035 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8036 | |
| 8037 | switch (optname) { |
| 8038 | case SCTP_STATUS: |
| 8039 | retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); |
| 8040 | break; |
| 8041 | case SCTP_DISABLE_FRAGMENTS: |
| 8042 | retval = sctp_getsockopt_disable_fragments(sk, len, optval, |
| 8043 | optlen); |
| 8044 | break; |
| 8045 | case SCTP_EVENTS: |
| 8046 | retval = sctp_getsockopt_events(sk, len, optval, optlen); |
| 8047 | break; |
| 8048 | case SCTP_AUTOCLOSE: |
| 8049 | retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); |
| 8050 | break; |
| 8051 | case SCTP_SOCKOPT_PEELOFF: |
| 8052 | retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); |
| 8053 | break; |
Neil Horman | 2cb5c8e | 2017-06-30 13:32:57 -0400 | [diff] [blame] | 8054 | case SCTP_SOCKOPT_PEELOFF_FLAGS: |
| 8055 | retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen); |
| 8056 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8057 | case SCTP_PEER_ADDR_PARAMS: |
| 8058 | retval = sctp_getsockopt_peer_addr_params(sk, len, optval, |
| 8059 | optlen); |
| 8060 | break; |
Shan Wei | 4580ccc | 2011-01-18 22:39:00 +0000 | [diff] [blame] | 8061 | case SCTP_DELAYED_SACK: |
Wei Yongjun | d364d92 | 2008-05-09 15:13:26 -0700 | [diff] [blame] | 8062 | retval = sctp_getsockopt_delayed_ack(sk, len, optval, |
Frank Filz | 7708610 | 2005-12-22 11:37:30 -0800 | [diff] [blame] | 8063 | optlen); |
| 8064 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8065 | case SCTP_INITMSG: |
| 8066 | retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); |
| 8067 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8068 | case SCTP_GET_PEER_ADDRS: |
| 8069 | retval = sctp_getsockopt_peer_addrs(sk, len, optval, |
| 8070 | optlen); |
| 8071 | break; |
| 8072 | case SCTP_GET_LOCAL_ADDRS: |
| 8073 | retval = sctp_getsockopt_local_addrs(sk, len, optval, |
| 8074 | optlen); |
| 8075 | break; |
Vlad Yasevich | c6ba68a | 2009-06-01 12:41:15 -0400 | [diff] [blame] | 8076 | case SCTP_SOCKOPT_CONNECTX3: |
| 8077 | retval = sctp_getsockopt_connectx3(sk, len, optval, optlen); |
| 8078 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8079 | case SCTP_DEFAULT_SEND_PARAM: |
| 8080 | retval = sctp_getsockopt_default_send_param(sk, len, |
| 8081 | optval, optlen); |
| 8082 | break; |
Geir Ola Vaagland | 6b3fd5f | 2014-07-12 20:30:39 +0200 | [diff] [blame] | 8083 | case SCTP_DEFAULT_SNDINFO: |
| 8084 | retval = sctp_getsockopt_default_sndinfo(sk, len, |
| 8085 | optval, optlen); |
| 8086 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8087 | case SCTP_PRIMARY_ADDR: |
| 8088 | retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); |
| 8089 | break; |
| 8090 | case SCTP_NODELAY: |
| 8091 | retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); |
| 8092 | break; |
| 8093 | case SCTP_RTOINFO: |
| 8094 | retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); |
| 8095 | break; |
| 8096 | case SCTP_ASSOCINFO: |
| 8097 | retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); |
| 8098 | break; |
| 8099 | case SCTP_I_WANT_MAPPED_V4_ADDR: |
| 8100 | retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); |
| 8101 | break; |
| 8102 | case SCTP_MAXSEG: |
| 8103 | retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); |
| 8104 | break; |
| 8105 | case SCTP_GET_PEER_ADDR_INFO: |
| 8106 | retval = sctp_getsockopt_peer_addr_info(sk, len, optval, |
| 8107 | optlen); |
| 8108 | break; |
Ivan Skytte Jorgensen | 0f3fffd | 2006-12-20 16:07:04 -0800 | [diff] [blame] | 8109 | case SCTP_ADAPTATION_LAYER: |
| 8110 | retval = sctp_getsockopt_adaptation_layer(sk, len, optval, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8111 | optlen); |
| 8112 | break; |
Ivan Skytte Jorgensen | 6ab792f | 2006-12-13 16:34:22 -0800 | [diff] [blame] | 8113 | case SCTP_CONTEXT: |
| 8114 | retval = sctp_getsockopt_context(sk, len, optval, optlen); |
| 8115 | break; |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 8116 | case SCTP_FRAGMENT_INTERLEAVE: |
| 8117 | retval = sctp_getsockopt_fragment_interleave(sk, len, optval, |
| 8118 | optlen); |
| 8119 | break; |
Vlad Yasevich | d49d91d | 2007-03-23 11:32:00 -0700 | [diff] [blame] | 8120 | case SCTP_PARTIAL_DELIVERY_POINT: |
| 8121 | retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, |
| 8122 | optlen); |
| 8123 | break; |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 8124 | case SCTP_MAX_BURST: |
| 8125 | retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); |
| 8126 | break; |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 8127 | case SCTP_AUTH_KEY: |
| 8128 | case SCTP_AUTH_CHUNK: |
| 8129 | case SCTP_AUTH_DELETE_KEY: |
Xin Long | 601590e | 2018-03-14 19:05:32 +0800 | [diff] [blame] | 8130 | case SCTP_AUTH_DEACTIVATE_KEY: |
Vlad Yasevich | 65b07e5 | 2007-09-16 19:34:00 -0700 | [diff] [blame] | 8131 | retval = -EOPNOTSUPP; |
| 8132 | break; |
| 8133 | case SCTP_HMAC_IDENT: |
| 8134 | retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen); |
| 8135 | break; |
| 8136 | case SCTP_AUTH_ACTIVE_KEY: |
| 8137 | retval = sctp_getsockopt_active_key(sk, len, optval, optlen); |
| 8138 | break; |
| 8139 | case SCTP_PEER_AUTH_CHUNKS: |
| 8140 | retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval, |
| 8141 | optlen); |
| 8142 | break; |
| 8143 | case SCTP_LOCAL_AUTH_CHUNKS: |
| 8144 | retval = sctp_getsockopt_local_auth_chunks(sk, len, optval, |
| 8145 | optlen); |
| 8146 | break; |
Wei Yongjun | aea3c5c | 2008-12-25 16:57:24 -0800 | [diff] [blame] | 8147 | case SCTP_GET_ASSOC_NUMBER: |
| 8148 | retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen); |
| 8149 | break; |
Wei Yongjun | 209ba42 | 2011-04-17 17:27:08 +0000 | [diff] [blame] | 8150 | case SCTP_GET_ASSOC_ID_LIST: |
| 8151 | retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen); |
| 8152 | break; |
Michio Honda | 7dc04d7 | 2011-04-26 20:16:31 +0900 | [diff] [blame] | 8153 | case SCTP_AUTO_ASCONF: |
| 8154 | retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen); |
| 8155 | break; |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 8156 | case SCTP_PEER_ADDR_THLDS: |
Xin Long | d467ac0 | 2019-11-08 13:20:36 +0800 | [diff] [blame] | 8157 | retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, |
| 8158 | optlen, false); |
| 8159 | break; |
| 8160 | case SCTP_PEER_ADDR_THLDS_V2: |
| 8161 | retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, |
| 8162 | optlen, true); |
Neil Horman | 5aa93bc | 2012-07-21 07:56:07 +0000 | [diff] [blame] | 8163 | break; |
Michele Baldessari | 196d675 | 2012-12-01 04:49:42 +0000 | [diff] [blame] | 8164 | case SCTP_GET_ASSOC_STATS: |
| 8165 | retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen); |
| 8166 | break; |
Geir Ola Vaagland | 0d3a421 | 2014-07-12 20:30:37 +0200 | [diff] [blame] | 8167 | case SCTP_RECVRCVINFO: |
| 8168 | retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen); |
| 8169 | break; |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 8170 | case SCTP_RECVNXTINFO: |
| 8171 | retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen); |
| 8172 | break; |
Xin Long | 28aa4c2 | 2016-07-09 19:47:40 +0800 | [diff] [blame] | 8173 | case SCTP_PR_SUPPORTED: |
| 8174 | retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen); |
| 8175 | break; |
Xin Long | f959fb4 | 2016-07-09 19:47:41 +0800 | [diff] [blame] | 8176 | case SCTP_DEFAULT_PRINFO: |
| 8177 | retval = sctp_getsockopt_default_prinfo(sk, len, optval, |
| 8178 | optlen); |
| 8179 | break; |
Xin Long | 826d253 | 2016-07-09 19:47:42 +0800 | [diff] [blame] | 8180 | case SCTP_PR_ASSOC_STATUS: |
| 8181 | retval = sctp_getsockopt_pr_assocstatus(sk, len, optval, |
| 8182 | optlen); |
| 8183 | break; |
Xin Long | d229d48 | 2017-04-01 17:07:46 +0800 | [diff] [blame] | 8184 | case SCTP_PR_STREAM_STATUS: |
| 8185 | retval = sctp_getsockopt_pr_streamstatus(sk, len, optval, |
| 8186 | optlen); |
| 8187 | break; |
Xin Long | c0d8bab | 2017-03-10 12:11:12 +0800 | [diff] [blame] | 8188 | case SCTP_RECONFIG_SUPPORTED: |
| 8189 | retval = sctp_getsockopt_reconfig_supported(sk, len, optval, |
| 8190 | optlen); |
| 8191 | break; |
Xin Long | 9fb657a | 2017-01-18 00:44:46 +0800 | [diff] [blame] | 8192 | case SCTP_ENABLE_STREAM_RESET: |
| 8193 | retval = sctp_getsockopt_enable_strreset(sk, len, optval, |
| 8194 | optlen); |
| 8195 | break; |
Marcelo Ricardo Leitner | 13aa877 | 2017-10-03 19:20:14 -0300 | [diff] [blame] | 8196 | case SCTP_STREAM_SCHEDULER: |
| 8197 | retval = sctp_getsockopt_scheduler(sk, len, optval, |
| 8198 | optlen); |
| 8199 | break; |
Marcelo Ricardo Leitner | 0ccdf3c | 2017-10-03 19:20:15 -0300 | [diff] [blame] | 8200 | case SCTP_STREAM_SCHEDULER_VALUE: |
| 8201 | retval = sctp_getsockopt_scheduler_value(sk, len, optval, |
| 8202 | optlen); |
| 8203 | break; |
Xin Long | 772a586 | 2017-12-08 21:03:58 +0800 | [diff] [blame] | 8204 | case SCTP_INTERLEAVING_SUPPORTED: |
| 8205 | retval = sctp_getsockopt_interleaving_supported(sk, len, optval, |
| 8206 | optlen); |
| 8207 | break; |
Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 8208 | case SCTP_REUSE_PORT: |
| 8209 | retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen); |
| 8210 | break; |
Xin Long | 480ba9c | 2018-11-18 16:08:54 +0800 | [diff] [blame] | 8211 | case SCTP_EVENT: |
| 8212 | retval = sctp_getsockopt_event(sk, len, optval, optlen); |
| 8213 | break; |
Xin Long | df2c71f | 2019-08-19 22:02:46 +0800 | [diff] [blame] | 8214 | case SCTP_ASCONF_SUPPORTED: |
| 8215 | retval = sctp_getsockopt_asconf_supported(sk, len, optval, |
| 8216 | optlen); |
| 8217 | break; |
Xin Long | 56dd525 | 2019-08-19 22:02:49 +0800 | [diff] [blame] | 8218 | case SCTP_AUTH_SUPPORTED: |
| 8219 | retval = sctp_getsockopt_auth_supported(sk, len, optval, |
| 8220 | optlen); |
| 8221 | break; |
Xin Long | d5886b9 | 2019-08-26 16:30:04 +0800 | [diff] [blame] | 8222 | case SCTP_ECN_SUPPORTED: |
| 8223 | retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen); |
| 8224 | break; |
Xin Long | 8d2a693 | 2019-11-08 13:20:34 +0800 | [diff] [blame] | 8225 | case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE: |
| 8226 | retval = sctp_getsockopt_pf_expose(sk, len, optval, optlen); |
| 8227 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8228 | default: |
| 8229 | retval = -ENOPROTOOPT; |
| 8230 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 8231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8232 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8233 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8234 | return retval; |
| 8235 | } |
| 8236 | |
Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 8237 | static int sctp_hash(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8238 | { |
| 8239 | /* STUB */ |
Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 8240 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8241 | } |
| 8242 | |
| 8243 | static void sctp_unhash(struct sock *sk) |
| 8244 | { |
| 8245 | /* STUB */ |
| 8246 | } |
| 8247 | |
| 8248 | /* Check if port is acceptable. Possibly find first available port. |
| 8249 | * |
| 8250 | * The port hash table (contained in the 'global' SCTP protocol storage |
| 8251 | * returned by struct sctp_protocol *sctp_get_protocol()). The hash |
| 8252 | * table is an array of 4096 lists (sctp_bind_hashbucket). Each |
| 8253 | * list (the list number is the port number hashed out, so as you |
| 8254 | * would expect from a hash function, all the ports in a given list have |
| 8255 | * such a number that hashes out to the same list number; you were |
| 8256 | * expecting that, right?); so each list has a set of ports, with a |
| 8257 | * link to the socket (struct sock) that uses it, the port number and |
| 8258 | * a fastreuse flag (FIXME: NPI ipg). |
| 8259 | */ |
| 8260 | static struct sctp_bind_bucket *sctp_bucket_create( |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 8261 | struct sctp_bind_hashbucket *head, struct net *, unsigned short snum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8262 | |
Mao Wenan | 8e2ef6a | 2019-09-12 12:02:17 +0800 | [diff] [blame] | 8263 | static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8264 | { |
Xin Long | 6ba8457 | 2018-11-12 18:27:17 +0800 | [diff] [blame] | 8265 | struct sctp_sock *sp = sctp_sk(sk); |
| 8266 | bool reuse = (sk->sk_reuse || sp->reuse); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8267 | struct sctp_bind_hashbucket *head; /* hash list */ |
Maciej Żenczykowski | fb82238 | 2019-11-25 15:09:37 -0800 | [diff] [blame] | 8268 | struct net *net = sock_net(sk); |
Xin Long | 6ba8457 | 2018-11-12 18:27:17 +0800 | [diff] [blame] | 8269 | kuid_t uid = sock_i_uid(sk); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 8270 | struct sctp_bind_bucket *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8271 | unsigned short snum; |
| 8272 | int ret; |
| 8273 | |
Al Viro | 04afd8b | 2006-11-20 17:02:01 -0800 | [diff] [blame] | 8274 | snum = ntohs(addr->v4.sin_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8275 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 8276 | pr_debug("%s: begins, snum:%d\n", __func__, snum); |
| 8277 | |
wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 8278 | local_bh_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8279 | |
| 8280 | if (snum == 0) { |
Stephen Hemminger | 0639300 | 2007-10-10 17:30:18 -0700 | [diff] [blame] | 8281 | /* Search for an available port. */ |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 8282 | int low, high, remaining, index; |
| 8283 | unsigned int rover; |
| 8284 | |
WANG Cong | 122ff24 | 2014-05-12 16:04:53 -0700 | [diff] [blame] | 8285 | inet_get_local_port_range(net, &low, &high); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 8286 | remaining = (high - low) + 1; |
Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 8287 | rover = prandom_u32() % remaining + low; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8289 | do { |
| 8290 | rover++; |
| 8291 | if ((rover < low) || (rover > high)) |
| 8292 | rover = low; |
WANG Cong | 122ff24 | 2014-05-12 16:04:53 -0700 | [diff] [blame] | 8293 | if (inet_is_local_reserved_port(net, rover)) |
Amerigo Wang | e3826f1 | 2010-05-05 00:27:06 +0000 | [diff] [blame] | 8294 | continue; |
Maciej Żenczykowski | fb82238 | 2019-11-25 15:09:37 -0800 | [diff] [blame] | 8295 | index = sctp_phashfn(net, rover); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8296 | head = &sctp_port_hashtable[index]; |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 8297 | spin_lock(&head->lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 8298 | sctp_for_each_hentry(pp, &head->chain) |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 8299 | if ((pp->port == rover) && |
Maciej Żenczykowski | fb82238 | 2019-11-25 15:09:37 -0800 | [diff] [blame] | 8300 | net_eq(net, pp->net)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8301 | goto next; |
| 8302 | break; |
| 8303 | next: |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 8304 | spin_unlock(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8305 | } while (--remaining > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8306 | |
| 8307 | /* Exhausted local port range during search? */ |
| 8308 | ret = 1; |
| 8309 | if (remaining <= 0) |
| 8310 | goto fail; |
| 8311 | |
| 8312 | /* OK, here is the one we will use. HEAD (the port |
| 8313 | * hash table list entry) is non-NULL and we hold it's |
| 8314 | * mutex. |
| 8315 | */ |
| 8316 | snum = rover; |
| 8317 | } else { |
| 8318 | /* We are given an specific port number; we verify |
| 8319 | * that it is not being used. If it is used, we will |
| 8320 | * exahust the search in the hash list corresponding |
| 8321 | * to the port number (snum) - we detect that with the |
| 8322 | * port iterator, pp being NULL. |
| 8323 | */ |
Maciej Żenczykowski | fb82238 | 2019-11-25 15:09:37 -0800 | [diff] [blame] | 8324 | head = &sctp_port_hashtable[sctp_phashfn(net, snum)]; |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 8325 | spin_lock(&head->lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 8326 | sctp_for_each_hentry(pp, &head->chain) { |
Maciej Żenczykowski | fb82238 | 2019-11-25 15:09:37 -0800 | [diff] [blame] | 8327 | if ((pp->port == snum) && net_eq(pp->net, net)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8328 | goto pp_found; |
| 8329 | } |
| 8330 | } |
| 8331 | pp = NULL; |
| 8332 | goto pp_not_found; |
| 8333 | pp_found: |
| 8334 | if (!hlist_empty(&pp->owner)) { |
| 8335 | /* We had a port hash table hit - there is an |
| 8336 | * available port (pp != NULL) and it is being |
| 8337 | * used by other socket (pp->owner not empty); that other |
| 8338 | * socket is going to be sk2. |
| 8339 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8340 | struct sock *sk2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8341 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 8342 | pr_debug("%s: found a possible match\n", __func__); |
| 8343 | |
Xin Long | 6ba8457 | 2018-11-12 18:27:17 +0800 | [diff] [blame] | 8344 | if ((pp->fastreuse && reuse && |
| 8345 | sk->sk_state != SCTP_SS_LISTENING) || |
| 8346 | (pp->fastreuseport && sk->sk_reuseport && |
| 8347 | uid_eq(pp->fastuid, uid))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8348 | goto success; |
| 8349 | |
| 8350 | /* Run through the list of sockets bound to the port |
| 8351 | * (pp->port) [via the pointers bind_next and |
| 8352 | * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, |
| 8353 | * we get the endpoint they describe and run through |
| 8354 | * the endpoint's list of IP (v4 or v6) addresses, |
| 8355 | * comparing each of the addresses with the address of |
| 8356 | * the socket sk. If we find a match, then that means |
| 8357 | * that this port/socket (sk) combination are already |
| 8358 | * in an endpoint. |
| 8359 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 8360 | sk_for_each_bound(sk2, &pp->owner) { |
Xin Long | 6ba8457 | 2018-11-12 18:27:17 +0800 | [diff] [blame] | 8361 | struct sctp_sock *sp2 = sctp_sk(sk2); |
| 8362 | struct sctp_endpoint *ep2 = sp2->ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8363 | |
Vlad Yasevich | 4e54064 | 2008-07-18 23:06:32 -0700 | [diff] [blame] | 8364 | if (sk == sk2 || |
Xin Long | 6ba8457 | 2018-11-12 18:27:17 +0800 | [diff] [blame] | 8365 | (reuse && (sk2->sk_reuse || sp2->reuse) && |
| 8366 | sk2->sk_state != SCTP_SS_LISTENING) || |
| 8367 | (sk->sk_reuseport && sk2->sk_reuseport && |
| 8368 | uid_eq(uid, sock_i_uid(sk2)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8369 | continue; |
| 8370 | |
Xin Long | 6ba8457 | 2018-11-12 18:27:17 +0800 | [diff] [blame] | 8371 | if (sctp_bind_addr_conflict(&ep2->base.bind_addr, |
| 8372 | addr, sp2, sp)) { |
Mao Wenan | 8e2ef6a | 2019-09-12 12:02:17 +0800 | [diff] [blame] | 8373 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8374 | goto fail_unlock; |
| 8375 | } |
| 8376 | } |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 8377 | |
| 8378 | pr_debug("%s: found a match\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8379 | } |
| 8380 | pp_not_found: |
| 8381 | /* If there was a hash table miss, create a new port. */ |
| 8382 | ret = 1; |
Maciej Żenczykowski | fb82238 | 2019-11-25 15:09:37 -0800 | [diff] [blame] | 8383 | if (!pp && !(pp = sctp_bucket_create(head, net, snum))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8384 | goto fail_unlock; |
| 8385 | |
| 8386 | /* In either case (hit or miss), make sure fastreuse is 1 only |
| 8387 | * if sk->sk_reuse is too (that is, if the caller requested |
| 8388 | * SO_REUSEADDR on this socket -sk-). |
| 8389 | */ |
Vlad Yasevich | ce5325c | 2007-05-04 13:34:49 -0700 | [diff] [blame] | 8390 | if (hlist_empty(&pp->owner)) { |
Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 8391 | if (reuse && sk->sk_state != SCTP_SS_LISTENING) |
Vlad Yasevich | ce5325c | 2007-05-04 13:34:49 -0700 | [diff] [blame] | 8392 | pp->fastreuse = 1; |
| 8393 | else |
| 8394 | pp->fastreuse = 0; |
Xin Long | 6ba8457 | 2018-11-12 18:27:17 +0800 | [diff] [blame] | 8395 | |
| 8396 | if (sk->sk_reuseport) { |
| 8397 | pp->fastreuseport = 1; |
| 8398 | pp->fastuid = uid; |
| 8399 | } else { |
| 8400 | pp->fastreuseport = 0; |
| 8401 | } |
| 8402 | } else { |
| 8403 | if (pp->fastreuse && |
| 8404 | (!reuse || sk->sk_state == SCTP_SS_LISTENING)) |
| 8405 | pp->fastreuse = 0; |
| 8406 | |
| 8407 | if (pp->fastreuseport && |
| 8408 | (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid))) |
| 8409 | pp->fastreuseport = 0; |
| 8410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8411 | |
| 8412 | /* We are set, so fill up all the data in the hash table |
| 8413 | * entry, tie the socket list information with the rest of the |
| 8414 | * sockets FIXME: Blurry, NPI (ipg). |
| 8415 | */ |
| 8416 | success: |
Xin Long | 6ba8457 | 2018-11-12 18:27:17 +0800 | [diff] [blame] | 8417 | if (!sp->bind_hash) { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8418 | inet_sk(sk)->inet_num = snum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8419 | sk_add_bind_node(sk, &pp->owner); |
Xin Long | 6ba8457 | 2018-11-12 18:27:17 +0800 | [diff] [blame] | 8420 | sp->bind_hash = pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8421 | } |
| 8422 | ret = 0; |
| 8423 | |
| 8424 | fail_unlock: |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 8425 | spin_unlock(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8426 | |
| 8427 | fail: |
wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 8428 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8429 | return ret; |
| 8430 | } |
| 8431 | |
| 8432 | /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral |
| 8433 | * port is requested. |
| 8434 | */ |
| 8435 | static int sctp_get_port(struct sock *sk, unsigned short snum) |
| 8436 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8437 | union sctp_addr addr; |
| 8438 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
| 8439 | |
| 8440 | /* Set up a dummy address struct from the sk. */ |
| 8441 | af->from_sk(&addr, sk); |
| 8442 | addr.v4.sin_port = htons(snum); |
| 8443 | |
| 8444 | /* Note: sk->sk_num gets filled in if ephemeral port request. */ |
Mao Wenan | 8e2ef6a | 2019-09-12 12:02:17 +0800 | [diff] [blame] | 8445 | return sctp_get_port_local(sk, &addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8446 | } |
| 8447 | |
| 8448 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8449 | * Move a socket to LISTENING state. |
| 8450 | */ |
Daniel Borkmann | dda9192 | 2013-06-17 11:40:05 +0200 | [diff] [blame] | 8451 | static int sctp_listen_start(struct sock *sk, int backlog) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8452 | { |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8453 | struct sctp_sock *sp = sctp_sk(sk); |
| 8454 | struct sctp_endpoint *ep = sp->ep; |
Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 8455 | struct crypto_shash *tfm = NULL; |
Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 8456 | char alg[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8457 | |
| 8458 | /* Allocate HMAC for generating cookie. */ |
Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 8459 | if (!sp->hmac && sp->sctp_hmac_alg) { |
| 8460 | sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg); |
Herbert Xu | 5821c76 | 2016-01-24 21:20:12 +0800 | [diff] [blame] | 8461 | tfm = crypto_alloc_shash(alg, 0, 0); |
Vlad Yasevich | 8dc4984 | 2007-05-09 13:50:20 -0700 | [diff] [blame] | 8462 | if (IS_ERR(tfm)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 8463 | net_info_ratelimited("failed to load transform for %s: %ld\n", |
Neil Horman | 3c68198 | 2012-10-24 09:20:03 +0000 | [diff] [blame] | 8464 | sp->sctp_hmac_alg, PTR_ERR(tfm)); |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8465 | return -ENOSYS; |
| 8466 | } |
| 8467 | sctp_sk(sk)->hmac = tfm; |
| 8468 | } |
| 8469 | |
| 8470 | /* |
| 8471 | * If a bind() or sctp_bindx() is not called prior to a listen() |
| 8472 | * call that allows new associations to be accepted, the system |
| 8473 | * picks an ephemeral port and will choose an address set equivalent |
| 8474 | * to binding with a wildcard address. |
| 8475 | * |
| 8476 | * This is not currently spelled out in the SCTP sockets |
| 8477 | * extensions draft, but follows the practice as seen in TCP |
| 8478 | * sockets. |
| 8479 | * |
| 8480 | */ |
Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 8481 | inet_sk_set_state(sk, SCTP_SS_LISTENING); |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8482 | if (!ep->base.bind_addr.port) { |
| 8483 | if (sctp_autobind(sk)) |
| 8484 | return -EAGAIN; |
| 8485 | } else { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8486 | if (sctp_get_port(sk, inet_sk(sk)->inet_num)) { |
Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 8487 | inet_sk_set_state(sk, SCTP_SS_CLOSED); |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8488 | return -EADDRINUSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8489 | } |
| 8490 | } |
| 8491 | |
Eric Dumazet | 099ecf5 | 2019-11-05 14:11:54 -0800 | [diff] [blame] | 8492 | WRITE_ONCE(sk->sk_max_ack_backlog, backlog); |
Xin Long | 76c6d98 | 2018-11-12 18:27:16 +0800 | [diff] [blame] | 8493 | return sctp_hash_endpoint(ep); |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8494 | } |
| 8495 | |
| 8496 | /* |
| 8497 | * 4.1.3 / 5.1.3 listen() |
| 8498 | * |
| 8499 | * By default, new associations are not accepted for UDP style sockets. |
| 8500 | * An application uses listen() to mark a socket as being able to |
| 8501 | * accept new associations. |
| 8502 | * |
| 8503 | * On TCP style sockets, applications use listen() to ready the SCTP |
| 8504 | * endpoint for accepting inbound associations. |
| 8505 | * |
| 8506 | * On both types of endpoints a backlog of '0' disables listening. |
| 8507 | * |
| 8508 | * Move a socket to LISTENING state. |
| 8509 | */ |
| 8510 | int sctp_inet_listen(struct socket *sock, int backlog) |
| 8511 | { |
| 8512 | struct sock *sk = sock->sk; |
| 8513 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
| 8514 | int err = -EINVAL; |
| 8515 | |
| 8516 | if (unlikely(backlog < 0)) |
| 8517 | return err; |
| 8518 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8519 | lock_sock(sk); |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8520 | |
| 8521 | /* Peeled-off sockets are not allowed to listen(). */ |
| 8522 | if (sctp_style(sk, UDP_HIGH_BANDWIDTH)) |
| 8523 | goto out; |
| 8524 | |
| 8525 | if (sock->state != SS_UNCONNECTED) |
| 8526 | goto out; |
| 8527 | |
Xin Long | 34b2789 | 2017-04-06 13:10:52 +0800 | [diff] [blame] | 8528 | if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED)) |
| 8529 | goto out; |
| 8530 | |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8531 | /* If backlog is zero, disable listening. */ |
| 8532 | if (!backlog) { |
| 8533 | if (sctp_sstate(sk, CLOSED)) |
| 8534 | goto out; |
| 8535 | |
| 8536 | err = 0; |
| 8537 | sctp_unhash_endpoint(ep); |
| 8538 | sk->sk_state = SCTP_SS_CLOSED; |
Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 8539 | if (sk->sk_reuse || sctp_sk(sk)->reuse) |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8540 | sctp_sk(sk)->bind_hash->fastreuse = 1; |
| 8541 | goto out; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 8542 | } |
| 8543 | |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8544 | /* If we are already listening, just update the backlog */ |
| 8545 | if (sctp_sstate(sk, LISTENING)) |
Eric Dumazet | 099ecf5 | 2019-11-05 14:11:54 -0800 | [diff] [blame] | 8546 | WRITE_ONCE(sk->sk_max_ack_backlog, backlog); |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8547 | else { |
| 8548 | err = sctp_listen_start(sk, backlog); |
| 8549 | if (err) |
| 8550 | goto out; |
| 8551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8552 | |
Vlad Yasevich | 5e8f3f7 | 2009-03-12 09:49:17 +0000 | [diff] [blame] | 8553 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8554 | out: |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8555 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8556 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8557 | } |
| 8558 | |
| 8559 | /* |
| 8560 | * This function is done by modeling the current datagram_poll() and the |
| 8561 | * tcp_poll(). Note that, based on these implementations, we don't |
| 8562 | * lock the socket in this function, even though it seems that, |
| 8563 | * ideally, locking or some other mechanisms can be used to ensure |
Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 8564 | * the integrity of the counters (sndbuf and wmem_alloc) used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8565 | * in this place. We assume that we don't need locks either until proven |
| 8566 | * otherwise. |
| 8567 | * |
| 8568 | * Another thing to note is that we include the Async I/O support |
| 8569 | * here, again, by modeling the current TCP/UDP code. We don't have |
| 8570 | * a good way to test with it yet. |
| 8571 | */ |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 8572 | __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8573 | { |
| 8574 | struct sock *sk = sock->sk; |
| 8575 | struct sctp_sock *sp = sctp_sk(sk); |
Al Viro | ade994f | 2017-07-03 00:01:49 -0400 | [diff] [blame] | 8576 | __poll_t mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8577 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 8578 | poll_wait(file, sk_sleep(sk), wait); |
| 8579 | |
Marcelo Ricardo Leitner | 486bdee | 2016-04-12 18:11:31 -0300 | [diff] [blame] | 8580 | sock_rps_record_flow(sk); |
| 8581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8582 | /* A TCP-style listening socket becomes readable when the accept queue |
| 8583 | * is not empty. |
| 8584 | */ |
| 8585 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) |
| 8586 | return (!list_empty(&sp->ep->asocs)) ? |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8587 | (EPOLLIN | EPOLLRDNORM) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8588 | |
| 8589 | mask = 0; |
| 8590 | |
| 8591 | /* Is there any exceptional events? */ |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 8592 | if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8593 | mask |= EPOLLERR | |
| 8594 | (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); |
Davide Libenzi | f348d70 | 2006-03-25 03:07:39 -0800 | [diff] [blame] | 8595 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8596 | mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8597 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8598 | mask |= EPOLLHUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8599 | |
| 8600 | /* Is it readable? Reconsider this code with TCP-style support. */ |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 8601 | if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8602 | mask |= EPOLLIN | EPOLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8603 | |
| 8604 | /* The association is either gone or not ready. */ |
| 8605 | if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) |
| 8606 | return mask; |
| 8607 | |
| 8608 | /* Is it writable? */ |
| 8609 | if (sctp_writeable(sk)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8610 | mask |= EPOLLOUT | EPOLLWRNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8611 | } else { |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 8612 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8613 | /* |
| 8614 | * Since the socket is not locked, the buffer |
| 8615 | * might be made available after the writeable check and |
| 8616 | * before the bit is set. This could cause a lost I/O |
| 8617 | * signal. tcp_poll() has a race breaker for this race |
| 8618 | * condition. Based on their implementation, we put |
| 8619 | * in the following code to cover it as well. |
| 8620 | */ |
| 8621 | if (sctp_writeable(sk)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8622 | mask |= EPOLLOUT | EPOLLWRNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8623 | } |
| 8624 | return mask; |
| 8625 | } |
| 8626 | |
| 8627 | /******************************************************************** |
| 8628 | * 2nd Level Abstractions |
| 8629 | ********************************************************************/ |
| 8630 | |
| 8631 | static struct sctp_bind_bucket *sctp_bucket_create( |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 8632 | struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8633 | { |
| 8634 | struct sctp_bind_bucket *pp; |
| 8635 | |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 8636 | pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8637 | if (pp) { |
Li Zefan | 935a7f6 | 2008-04-10 01:58:06 -0700 | [diff] [blame] | 8638 | SCTP_DBG_OBJCNT_INC(bind_bucket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8639 | pp->port = snum; |
| 8640 | pp->fastreuse = 0; |
| 8641 | INIT_HLIST_HEAD(&pp->owner); |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 8642 | pp->net = net; |
Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 8643 | hlist_add_head(&pp->node, &head->chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8644 | } |
| 8645 | return pp; |
| 8646 | } |
| 8647 | |
| 8648 | /* Caller must hold hashbucket lock for this tb with local BH disabled */ |
| 8649 | static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) |
| 8650 | { |
Sridhar Samudrala | 37fa687 | 2006-07-21 14:45:47 -0700 | [diff] [blame] | 8651 | if (pp && hlist_empty(&pp->owner)) { |
Vlad Yasevich | d970dbf | 2007-11-09 11:43:40 -0500 | [diff] [blame] | 8652 | __hlist_del(&pp->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8653 | kmem_cache_free(sctp_bucket_cachep, pp); |
| 8654 | SCTP_DBG_OBJCNT_DEC(bind_bucket); |
| 8655 | } |
| 8656 | } |
| 8657 | |
| 8658 | /* Release this socket's reference to a local port. */ |
| 8659 | static inline void __sctp_put_port(struct sock *sk) |
| 8660 | { |
| 8661 | struct sctp_bind_hashbucket *head = |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 8662 | &sctp_port_hashtable[sctp_phashfn(sock_net(sk), |
| 8663 | inet_sk(sk)->inet_num)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8664 | struct sctp_bind_bucket *pp; |
| 8665 | |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 8666 | spin_lock(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8667 | pp = sctp_sk(sk)->bind_hash; |
| 8668 | __sk_del_bind_node(sk); |
| 8669 | sctp_sk(sk)->bind_hash = NULL; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8670 | inet_sk(sk)->inet_num = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8671 | sctp_bucket_destroy(pp); |
wangweidong | 3c8e43b | 2014-01-21 15:44:08 +0800 | [diff] [blame] | 8672 | spin_unlock(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8673 | } |
| 8674 | |
| 8675 | void sctp_put_port(struct sock *sk) |
| 8676 | { |
wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 8677 | local_bh_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8678 | __sctp_put_port(sk); |
wangweidong | 79b9113 | 2014-01-21 15:44:07 +0800 | [diff] [blame] | 8679 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8680 | } |
| 8681 | |
| 8682 | /* |
| 8683 | * The system picks an ephemeral port and choose an address set equivalent |
| 8684 | * to binding with a wildcard address. |
| 8685 | * One of those addresses will be the primary address for the association. |
| 8686 | * This automatically enables the multihoming capability of SCTP. |
| 8687 | */ |
| 8688 | static int sctp_autobind(struct sock *sk) |
| 8689 | { |
| 8690 | union sctp_addr autoaddr; |
| 8691 | struct sctp_af *af; |
Al Viro | 6fbfa9f | 2006-11-20 17:24:53 -0800 | [diff] [blame] | 8692 | __be16 port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8693 | |
| 8694 | /* Initialize a local sockaddr structure to INADDR_ANY. */ |
| 8695 | af = sctp_sk(sk)->pf->af; |
| 8696 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 8697 | port = htons(inet_sk(sk)->inet_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8698 | af->inaddr_any(&autoaddr, port); |
| 8699 | |
| 8700 | return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); |
| 8701 | } |
| 8702 | |
| 8703 | /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation. |
| 8704 | * |
| 8705 | * From RFC 2292 |
| 8706 | * 4.2 The cmsghdr Structure * |
| 8707 | * |
| 8708 | * When ancillary data is sent or received, any number of ancillary data |
| 8709 | * objects can be specified by the msg_control and msg_controllen members of |
| 8710 | * the msghdr structure, because each object is preceded by |
| 8711 | * a cmsghdr structure defining the object's length (the cmsg_len member). |
| 8712 | * Historically Berkeley-derived implementations have passed only one object |
| 8713 | * at a time, but this API allows multiple objects to be |
| 8714 | * passed in a single call to sendmsg() or recvmsg(). The following example |
| 8715 | * shows two ancillary data objects in a control buffer. |
| 8716 | * |
| 8717 | * |<--------------------------- msg_controllen -------------------------->| |
| 8718 | * | | |
| 8719 | * |
| 8720 | * |<----- ancillary data object ----->|<----- ancillary data object ----->| |
| 8721 | * |
| 8722 | * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| |
| 8723 | * | | | |
| 8724 | * |
| 8725 | * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| | |
| 8726 | * |
| 8727 | * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| | |
| 8728 | * | | | | | |
| 8729 | * |
| 8730 | * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ |
| 8731 | * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX| |
| 8732 | * |
| 8733 | * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX| |
| 8734 | * |
| 8735 | * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ |
| 8736 | * ^ |
| 8737 | * | |
| 8738 | * |
| 8739 | * msg_control |
| 8740 | * points here |
| 8741 | */ |
Xin Long | a05437a | 2017-08-11 10:23:48 +0800 | [diff] [blame] | 8742 | 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] | 8743 | { |
Vlad Yasevich | ab38fb0 | 2008-04-12 18:40:06 -0700 | [diff] [blame] | 8744 | struct msghdr *my_msg = (struct msghdr *)msg; |
Xin Long | a05437a | 2017-08-11 10:23:48 +0800 | [diff] [blame] | 8745 | struct cmsghdr *cmsg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8746 | |
Gu Zheng | f95b414 | 2014-12-11 11:22:04 +0800 | [diff] [blame] | 8747 | for_each_cmsghdr(cmsg, my_msg) { |
Vlad Yasevich | ab38fb0 | 2008-04-12 18:40:06 -0700 | [diff] [blame] | 8748 | if (!CMSG_OK(my_msg, cmsg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8749 | return -EINVAL; |
| 8750 | |
| 8751 | /* Should we parse this header or ignore? */ |
| 8752 | if (cmsg->cmsg_level != IPPROTO_SCTP) |
| 8753 | continue; |
| 8754 | |
| 8755 | /* Strictly check lengths following example in SCM code. */ |
| 8756 | switch (cmsg->cmsg_type) { |
| 8757 | case SCTP_INIT: |
| 8758 | /* SCTP Socket API Extension |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8759 | * 5.3.1 SCTP Initiation Structure (SCTP_INIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8760 | * |
| 8761 | * This cmsghdr structure provides information for |
| 8762 | * initializing new SCTP associations with sendmsg(). |
| 8763 | * The SCTP_INITMSG socket option uses this same data |
| 8764 | * structure. This structure is not used for |
| 8765 | * recvmsg(). |
| 8766 | * |
| 8767 | * cmsg_level cmsg_type cmsg_data[] |
| 8768 | * ------------ ------------ ---------------------- |
| 8769 | * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg |
| 8770 | */ |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8771 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8772 | return -EINVAL; |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8773 | |
| 8774 | cmsgs->init = CMSG_DATA(cmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8775 | break; |
| 8776 | |
| 8777 | case SCTP_SNDRCV: |
| 8778 | /* SCTP Socket API Extension |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8779 | * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8780 | * |
| 8781 | * This cmsghdr structure specifies SCTP options for |
| 8782 | * sendmsg() and describes SCTP header information |
| 8783 | * about a received message through recvmsg(). |
| 8784 | * |
| 8785 | * cmsg_level cmsg_type cmsg_data[] |
| 8786 | * ------------ ------------ ---------------------- |
| 8787 | * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo |
| 8788 | */ |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8789 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8790 | return -EINVAL; |
| 8791 | |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8792 | cmsgs->srinfo = CMSG_DATA(cmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8793 | |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8794 | if (cmsgs->srinfo->sinfo_flags & |
Ivan Skytte Jorgensen | eaa5c54 | 2005-10-28 15:10:00 -0700 | [diff] [blame] | 8795 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | |
Xin Long | 4910280 | 2018-03-05 20:44:20 +0800 | [diff] [blame] | 8796 | SCTP_SACK_IMMEDIATELY | SCTP_SENDALL | |
| 8797 | SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8798 | return -EINVAL; |
| 8799 | break; |
| 8800 | |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8801 | case SCTP_SNDINFO: |
| 8802 | /* SCTP Socket API Extension |
| 8803 | * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO) |
| 8804 | * |
| 8805 | * This cmsghdr structure specifies SCTP options for |
| 8806 | * sendmsg(). This structure and SCTP_RCVINFO replaces |
| 8807 | * SCTP_SNDRCV which has been deprecated. |
| 8808 | * |
| 8809 | * cmsg_level cmsg_type cmsg_data[] |
| 8810 | * ------------ ------------ --------------------- |
| 8811 | * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo |
| 8812 | */ |
| 8813 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo))) |
| 8814 | return -EINVAL; |
| 8815 | |
| 8816 | cmsgs->sinfo = CMSG_DATA(cmsg); |
| 8817 | |
| 8818 | if (cmsgs->sinfo->snd_flags & |
| 8819 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | |
Xin Long | 4910280 | 2018-03-05 20:44:20 +0800 | [diff] [blame] | 8820 | SCTP_SACK_IMMEDIATELY | SCTP_SENDALL | |
| 8821 | SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF)) |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8822 | return -EINVAL; |
| 8823 | break; |
Xin Long | ed63afb | 2018-03-05 20:44:18 +0800 | [diff] [blame] | 8824 | case SCTP_PRINFO: |
| 8825 | /* SCTP Socket API Extension |
| 8826 | * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO) |
| 8827 | * |
| 8828 | * This cmsghdr structure specifies SCTP options for sendmsg(). |
| 8829 | * |
| 8830 | * cmsg_level cmsg_type cmsg_data[] |
| 8831 | * ------------ ------------ --------------------- |
| 8832 | * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo |
| 8833 | */ |
| 8834 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo))) |
| 8835 | return -EINVAL; |
| 8836 | |
| 8837 | cmsgs->prinfo = CMSG_DATA(cmsg); |
| 8838 | if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK) |
| 8839 | return -EINVAL; |
| 8840 | |
| 8841 | if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE) |
| 8842 | cmsgs->prinfo->pr_value = 0; |
| 8843 | break; |
Xin Long | 3ff547c | 2018-03-14 19:05:31 +0800 | [diff] [blame] | 8844 | case SCTP_AUTHINFO: |
| 8845 | /* SCTP Socket API Extension |
| 8846 | * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO) |
| 8847 | * |
| 8848 | * This cmsghdr structure specifies SCTP options for sendmsg(). |
| 8849 | * |
| 8850 | * cmsg_level cmsg_type cmsg_data[] |
| 8851 | * ------------ ------------ --------------------- |
| 8852 | * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo |
| 8853 | */ |
| 8854 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo))) |
| 8855 | return -EINVAL; |
| 8856 | |
| 8857 | cmsgs->authinfo = CMSG_DATA(cmsg); |
| 8858 | break; |
Xin Long | 2c0dbaa | 2018-03-05 20:44:19 +0800 | [diff] [blame] | 8859 | case SCTP_DSTADDRV4: |
| 8860 | case SCTP_DSTADDRV6: |
| 8861 | /* SCTP Socket API Extension |
| 8862 | * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6) |
| 8863 | * |
| 8864 | * This cmsghdr structure specifies SCTP options for sendmsg(). |
| 8865 | * |
| 8866 | * cmsg_level cmsg_type cmsg_data[] |
| 8867 | * ------------ ------------ --------------------- |
| 8868 | * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr |
| 8869 | * ------------ ------------ --------------------- |
| 8870 | * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr |
| 8871 | */ |
| 8872 | cmsgs->addrs_msg = my_msg; |
| 8873 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8874 | default: |
| 8875 | return -EINVAL; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 8876 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8877 | } |
Geir Ola Vaagland | 63b9493 | 2014-07-12 20:30:36 +0200 | [diff] [blame] | 8878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8879 | return 0; |
| 8880 | } |
| 8881 | |
| 8882 | /* |
| 8883 | * Wait for a packet.. |
| 8884 | * Note: This function is the same function as in core/datagram.c |
| 8885 | * with a few modifications to make lksctp work. |
| 8886 | */ |
wangweidong | 26ac8e5 | 2013-12-23 12:16:51 +0800 | [diff] [blame] | 8887 | 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] | 8888 | { |
| 8889 | int error; |
| 8890 | DEFINE_WAIT(wait); |
| 8891 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8892 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8893 | |
| 8894 | /* Socket errors? */ |
| 8895 | error = sock_error(sk); |
| 8896 | if (error) |
| 8897 | goto out; |
| 8898 | |
| 8899 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 8900 | goto ready; |
| 8901 | |
| 8902 | /* Socket shut down? */ |
| 8903 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 8904 | goto out; |
| 8905 | |
| 8906 | /* Sequenced packets can come disconnected. If so we report the |
| 8907 | * problem. |
| 8908 | */ |
| 8909 | error = -ENOTCONN; |
| 8910 | |
| 8911 | /* Is there a good reason to think that we may receive some data? */ |
| 8912 | if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) |
| 8913 | goto out; |
| 8914 | |
| 8915 | /* Handle signals. */ |
| 8916 | if (signal_pending(current)) |
| 8917 | goto interrupted; |
| 8918 | |
| 8919 | /* Let another process have a go. Since we are going to sleep |
| 8920 | * anyway. Note: This may cause odd behaviors if the message |
| 8921 | * does not fit in the user's buffer, but this seems to be the |
| 8922 | * only way to honor MSG_DONTWAIT realistically. |
| 8923 | */ |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8924 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8925 | *timeo_p = schedule_timeout(*timeo_p); |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 8926 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8927 | |
| 8928 | ready: |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8929 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8930 | return 0; |
| 8931 | |
| 8932 | interrupted: |
| 8933 | error = sock_intr_errno(*timeo_p); |
| 8934 | |
| 8935 | out: |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 8936 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8937 | *err = error; |
| 8938 | return error; |
| 8939 | } |
| 8940 | |
| 8941 | /* Receive a datagram. |
| 8942 | * Note: This is pretty much the same routine as in core/datagram.c |
| 8943 | * with a few changes to make lksctp work. |
| 8944 | */ |
Geir Ola Vaagland | 2347c80 | 2014-07-12 20:30:38 +0200 | [diff] [blame] | 8945 | struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, |
| 8946 | int noblock, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8947 | { |
| 8948 | int error; |
| 8949 | struct sk_buff *skb; |
| 8950 | long timeo; |
| 8951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8952 | timeo = sock_rcvtimeo(sk, noblock); |
| 8953 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 8954 | pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo, |
| 8955 | MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8956 | |
| 8957 | do { |
| 8958 | /* Again only user level code calls this function, |
| 8959 | * so nothing interrupt level |
| 8960 | * will suddenly eat the receive_queue. |
| 8961 | * |
| 8962 | * Look at current nfs client by the way... |
David Shwatrz | 8917a3c | 2010-12-02 09:01:55 +0000 | [diff] [blame] | 8963 | * However, this function was correct in any case. 8) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8964 | */ |
| 8965 | if (flags & MSG_PEEK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8966 | skb = skb_peek(&sk->sk_receive_queue); |
| 8967 | if (skb) |
Reshetova, Elena | 6335479 | 2017-06-30 13:07:58 +0300 | [diff] [blame] | 8968 | refcount_inc(&skb->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8969 | } else { |
Marcelo Ricardo Leitner | 311b217 | 2016-04-13 19:12:29 -0300 | [diff] [blame] | 8970 | skb = __skb_dequeue(&sk->sk_receive_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8971 | } |
| 8972 | |
| 8973 | if (skb) |
| 8974 | return skb; |
| 8975 | |
Neil Horman | 6736dc3 | 2005-12-02 20:30:06 -0800 | [diff] [blame] | 8976 | /* Caller is allowed not to check sk->sk_err before calling. */ |
| 8977 | error = sock_error(sk); |
| 8978 | if (error) |
| 8979 | goto no_packet; |
| 8980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8981 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 8982 | break; |
| 8983 | |
Alexander Duyck | 2b5cd0d | 2017-03-24 10:08:12 -0700 | [diff] [blame] | 8984 | if (sk_can_busy_loop(sk)) { |
| 8985 | sk_busy_loop(sk, noblock); |
| 8986 | |
Eric Dumazet | 3f926af | 2019-10-23 22:44:51 -0700 | [diff] [blame] | 8987 | if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) |
Alexander Duyck | 2b5cd0d | 2017-03-24 10:08:12 -0700 | [diff] [blame] | 8988 | continue; |
| 8989 | } |
Neil Horman | 8465a5f | 2014-04-17 15:26:51 -0400 | [diff] [blame] | 8990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8991 | /* User doesn't want to wait. */ |
| 8992 | error = -EAGAIN; |
| 8993 | if (!timeo) |
| 8994 | goto no_packet; |
| 8995 | } while (sctp_wait_for_packet(sk, err, &timeo) == 0); |
| 8996 | |
| 8997 | return NULL; |
| 8998 | |
| 8999 | no_packet: |
| 9000 | *err = error; |
| 9001 | return NULL; |
| 9002 | } |
| 9003 | |
| 9004 | /* If sndbuf has changed, wake up per association sndbuf waiters. */ |
| 9005 | static void __sctp_write_space(struct sctp_association *asoc) |
| 9006 | { |
| 9007 | struct sock *sk = asoc->base.sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9008 | |
Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 9009 | if (sctp_wspace(asoc) <= 0) |
| 9010 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9011 | |
Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 9012 | if (waitqueue_active(&asoc->wait)) |
| 9013 | wake_up_interruptible(&asoc->wait); |
Eric Dumazet | eaefd110 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 9014 | |
Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 9015 | if (sctp_writeable(sk)) { |
| 9016 | struct socket_wq *wq; |
| 9017 | |
| 9018 | rcu_read_lock(); |
| 9019 | wq = rcu_dereference(sk->sk_wq); |
| 9020 | if (wq) { |
| 9021 | if (waitqueue_active(&wq->wait)) |
| 9022 | wake_up_interruptible(&wq->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9023 | |
| 9024 | /* Note that we try to include the Async I/O support |
| 9025 | * here by modeling from the current TCP/UDP code. |
| 9026 | * We have not tested with it yet. |
| 9027 | */ |
Eric Dumazet | eaefd110 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 9028 | if (!(sk->sk_shutdown & SEND_SHUTDOWN)) |
Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 9029 | sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9030 | } |
Eric Dumazet | ceb5d58 | 2015-11-29 20:03:11 -0800 | [diff] [blame] | 9031 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9032 | } |
| 9033 | } |
| 9034 | |
Daniel Borkmann | 52c35be | 2014-04-08 17:26:13 +0200 | [diff] [blame] | 9035 | static void sctp_wake_up_waiters(struct sock *sk, |
| 9036 | struct sctp_association *asoc) |
| 9037 | { |
| 9038 | struct sctp_association *tmp = asoc; |
| 9039 | |
| 9040 | /* We do accounting for the sndbuf space per association, |
| 9041 | * so we only need to wake our own association. |
| 9042 | */ |
| 9043 | if (asoc->ep->sndbuf_policy) |
| 9044 | return __sctp_write_space(asoc); |
| 9045 | |
Daniel Borkmann | 1e1cdf8 | 2014-04-09 16:10:20 +0200 | [diff] [blame] | 9046 | /* If association goes down and is just flushing its |
| 9047 | * outq, then just normally notify others. |
| 9048 | */ |
| 9049 | if (asoc->base.dead) |
| 9050 | return sctp_write_space(sk); |
| 9051 | |
Daniel Borkmann | 52c35be | 2014-04-08 17:26:13 +0200 | [diff] [blame] | 9052 | /* Accounting for the sndbuf space is per socket, so we |
| 9053 | * need to wake up others, try to be fair and in case of |
| 9054 | * other associations, let them have a go first instead |
| 9055 | * of just doing a sctp_write_space() call. |
| 9056 | * |
| 9057 | * Note that we reach sctp_wake_up_waiters() only when |
| 9058 | * associations free up queued chunks, thus we are under |
| 9059 | * lock and the list of associations on a socket is |
| 9060 | * guaranteed not to change. |
| 9061 | */ |
| 9062 | for (tmp = list_next_entry(tmp, asocs); 1; |
| 9063 | tmp = list_next_entry(tmp, asocs)) { |
| 9064 | /* Manually skip the head element. */ |
| 9065 | if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs)) |
| 9066 | continue; |
| 9067 | /* Wake up association. */ |
| 9068 | __sctp_write_space(tmp); |
| 9069 | /* We've reached the end. */ |
| 9070 | if (tmp == asoc) |
| 9071 | break; |
| 9072 | } |
| 9073 | } |
| 9074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9075 | /* Do accounting for the sndbuf space. |
| 9076 | * Decrement the used sndbuf space of the corresponding association by the |
| 9077 | * data size which was just transmitted(freed). |
| 9078 | */ |
| 9079 | static void sctp_wfree(struct sk_buff *skb) |
| 9080 | { |
Daniel Borkmann | f869c91 | 2014-11-20 01:54:48 +0100 | [diff] [blame] | 9081 | struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg; |
| 9082 | struct sctp_association *asoc = chunk->asoc; |
| 9083 | struct sock *sk = asoc->base.sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9084 | |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 9085 | sk_mem_uncharge(sk, skb->truesize); |
Xin Long | 605c0ac | 2018-10-17 03:07:50 +0800 | [diff] [blame] | 9086 | sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk); |
| 9087 | asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk); |
| 9088 | WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), |
| 9089 | &sk->sk_wmem_alloc)); |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 9090 | |
Xin Long | ec2e506 | 2018-03-14 19:05:33 +0800 | [diff] [blame] | 9091 | if (chunk->shkey) { |
| 9092 | struct sctp_shared_key *shkey = chunk->shkey; |
| 9093 | |
| 9094 | /* refcnt == 2 and !list_empty mean after this release, it's |
| 9095 | * not being used anywhere, and it's time to notify userland |
| 9096 | * that this shkey can be freed if it's been deactivated. |
| 9097 | */ |
| 9098 | if (shkey->deactivated && !list_empty(&shkey->key_list) && |
| 9099 | refcount_read(&shkey->refcnt) == 2) { |
| 9100 | struct sctp_ulpevent *ev; |
| 9101 | |
| 9102 | ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id, |
| 9103 | SCTP_AUTH_FREE_KEY, |
| 9104 | GFP_KERNEL); |
| 9105 | if (ev) |
| 9106 | asoc->stream.si->enqueue_event(&asoc->ulpq, ev); |
| 9107 | } |
Xin Long | 1b1e0bc | 2018-03-14 19:05:30 +0800 | [diff] [blame] | 9108 | sctp_auth_shkey_release(chunk->shkey); |
Xin Long | ec2e506 | 2018-03-14 19:05:33 +0800 | [diff] [blame] | 9109 | } |
Xin Long | 1b1e0bc | 2018-03-14 19:05:30 +0800 | [diff] [blame] | 9110 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 9111 | sock_wfree(skb); |
Daniel Borkmann | 52c35be | 2014-04-08 17:26:13 +0200 | [diff] [blame] | 9112 | sctp_wake_up_waiters(sk, asoc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9113 | |
| 9114 | sctp_association_put(asoc); |
| 9115 | } |
| 9116 | |
Vlad Yasevich | 331c4ee | 2006-10-09 21:34:04 -0700 | [diff] [blame] | 9117 | /* Do accounting for the receive space on the socket. |
| 9118 | * Accounting for the association is done in ulpevent.c |
| 9119 | * We set this as a destructor for the cloned data skbs so that |
| 9120 | * accounting is done at the correct time. |
| 9121 | */ |
| 9122 | void sctp_sock_rfree(struct sk_buff *skb) |
| 9123 | { |
| 9124 | struct sock *sk = skb->sk; |
| 9125 | struct sctp_ulpevent *event = sctp_skb2event(skb); |
| 9126 | |
| 9127 | atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 9128 | |
| 9129 | /* |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 9130 | * Mimic the behavior of sock_rfree |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 9131 | */ |
Hideo Aoki | 3ab224b | 2007-12-31 00:11:19 -0800 | [diff] [blame] | 9132 | sk_mem_uncharge(sk, event->rmem_len); |
Vlad Yasevich | 331c4ee | 2006-10-09 21:34:04 -0700 | [diff] [blame] | 9133 | } |
| 9134 | |
| 9135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9136 | /* Helper function to wait for space in the sndbuf. */ |
| 9137 | static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, |
Xin Long | a0ff660 | 2018-01-15 17:01:36 +0800 | [diff] [blame] | 9138 | size_t msg_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9139 | { |
| 9140 | struct sock *sk = asoc->base.sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9141 | long current_timeo = *timeo_p; |
| 9142 | DEFINE_WAIT(wait); |
Xin Long | a0ff660 | 2018-01-15 17:01:36 +0800 | [diff] [blame] | 9143 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9144 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 9145 | pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc, |
| 9146 | *timeo_p, msg_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9147 | |
| 9148 | /* Increment the association's refcnt. */ |
| 9149 | sctp_association_hold(asoc); |
| 9150 | |
| 9151 | /* Wait on the association specific sndbuf space. */ |
| 9152 | for (;;) { |
| 9153 | prepare_to_wait_exclusive(&asoc->wait, &wait, |
| 9154 | TASK_INTERRUPTIBLE); |
Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 9155 | if (asoc->base.dead) |
| 9156 | goto do_dead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9157 | if (!*timeo_p) |
| 9158 | goto do_nonblock; |
Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 9159 | if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9160 | goto do_error; |
| 9161 | if (signal_pending(current)) |
| 9162 | goto do_interrupted; |
Xin Long | 1033990 | 2019-04-15 17:15:06 +0800 | [diff] [blame] | 9163 | if (sk_under_memory_pressure(sk)) |
| 9164 | sk_mem_reclaim(sk); |
| 9165 | if ((int)msg_len <= sctp_wspace(asoc) && |
| 9166 | sk_wmem_schedule(sk, msg_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9167 | break; |
| 9168 | |
| 9169 | /* Let another process have a go. Since we are going |
| 9170 | * to sleep anyway. |
| 9171 | */ |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 9172 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9173 | current_timeo = schedule_timeout(current_timeo); |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 9174 | lock_sock(sk); |
Xin Long | a0ff660 | 2018-01-15 17:01:36 +0800 | [diff] [blame] | 9175 | if (sk != asoc->base.sk) |
| 9176 | goto do_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9177 | |
| 9178 | *timeo_p = current_timeo; |
| 9179 | } |
| 9180 | |
| 9181 | out: |
| 9182 | finish_wait(&asoc->wait, &wait); |
| 9183 | |
| 9184 | /* Release the association's refcnt. */ |
| 9185 | sctp_association_put(asoc); |
| 9186 | |
| 9187 | return err; |
| 9188 | |
Xin Long | ca3af4d | 2017-11-15 16:55:54 +0800 | [diff] [blame] | 9189 | do_dead: |
| 9190 | err = -ESRCH; |
| 9191 | goto out; |
| 9192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9193 | do_error: |
| 9194 | err = -EPIPE; |
| 9195 | goto out; |
| 9196 | |
| 9197 | do_interrupted: |
| 9198 | err = sock_intr_errno(*timeo_p); |
| 9199 | goto out; |
| 9200 | |
| 9201 | do_nonblock: |
| 9202 | err = -EAGAIN; |
| 9203 | goto out; |
| 9204 | } |
| 9205 | |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 9206 | void sctp_data_ready(struct sock *sk) |
Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 9207 | { |
David S. Miller | 7ef5273 | 2010-05-02 21:43:40 -0700 | [diff] [blame] | 9208 | struct socket_wq *wq; |
| 9209 | |
| 9210 | rcu_read_lock(); |
| 9211 | wq = rcu_dereference(sk->sk_wq); |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 9212 | if (skwq_has_sleeper(wq)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 9213 | wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | |
| 9214 | EPOLLRDNORM | EPOLLRDBAND); |
Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 9215 | sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); |
David S. Miller | 7ef5273 | 2010-05-02 21:43:40 -0700 | [diff] [blame] | 9216 | rcu_read_unlock(); |
Wei Yongjun | 561b173 | 2010-04-28 08:47:18 +0000 | [diff] [blame] | 9217 | } |
| 9218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9219 | /* If socket sndbuf has changed, wake up all per association waiters. */ |
| 9220 | void sctp_write_space(struct sock *sk) |
| 9221 | { |
| 9222 | struct sctp_association *asoc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9223 | |
| 9224 | /* Wake up the tasks in each wait queue. */ |
Robert P. J. Day | 9dbc15f | 2008-04-12 18:54:24 -0700 | [diff] [blame] | 9225 | list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9226 | __sctp_write_space(asoc); |
| 9227 | } |
| 9228 | } |
| 9229 | |
| 9230 | /* Is there any sndbuf space available on the socket? |
| 9231 | * |
Neil Horman | 9bffc4a | 2005-12-19 14:24:40 -0800 | [diff] [blame] | 9232 | * 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] | 9233 | * associations on the same socket. For a UDP-style socket with |
| 9234 | * multiple associations, it is possible for it to be "unwriteable" |
| 9235 | * prematurely. I assume that this is acceptable because |
| 9236 | * a premature "unwriteable" is better than an accidental "writeable" which |
| 9237 | * would cause an unwanted block under certain circumstances. For the 1-1 |
| 9238 | * UDP-style sockets or TCP-style sockets, this code should work. |
| 9239 | * - Daisy |
| 9240 | */ |
Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame] | 9241 | static bool sctp_writeable(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9242 | { |
Xin Long | cd305c7 | 2018-10-17 03:07:51 +0800 | [diff] [blame] | 9243 | return sk->sk_sndbuf > sk->sk_wmem_queued; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9244 | } |
| 9245 | |
| 9246 | /* Wait for an association to go into ESTABLISHED state. If timeout is 0, |
| 9247 | * returns immediately with EINPROGRESS. |
| 9248 | */ |
| 9249 | static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) |
| 9250 | { |
| 9251 | struct sock *sk = asoc->base.sk; |
| 9252 | int err = 0; |
| 9253 | long current_timeo = *timeo_p; |
| 9254 | DEFINE_WAIT(wait); |
| 9255 | |
Daniel Borkmann | bb33381 | 2013-06-28 19:49:40 +0200 | [diff] [blame] | 9256 | pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9257 | |
| 9258 | /* Increment the association's refcnt. */ |
| 9259 | sctp_association_hold(asoc); |
| 9260 | |
| 9261 | for (;;) { |
| 9262 | prepare_to_wait_exclusive(&asoc->wait, &wait, |
| 9263 | TASK_INTERRUPTIBLE); |
| 9264 | if (!*timeo_p) |
| 9265 | goto do_nonblock; |
| 9266 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 9267 | break; |
| 9268 | if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || |
| 9269 | asoc->base.dead) |
| 9270 | goto do_error; |
| 9271 | if (signal_pending(current)) |
| 9272 | goto do_interrupted; |
| 9273 | |
| 9274 | if (sctp_state(asoc, ESTABLISHED)) |
| 9275 | break; |
| 9276 | |
| 9277 | /* Let another process have a go. Since we are going |
| 9278 | * to sleep anyway. |
| 9279 | */ |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 9280 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9281 | current_timeo = schedule_timeout(current_timeo); |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 9282 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9283 | |
| 9284 | *timeo_p = current_timeo; |
| 9285 | } |
| 9286 | |
| 9287 | out: |
| 9288 | finish_wait(&asoc->wait, &wait); |
| 9289 | |
| 9290 | /* Release the association's refcnt. */ |
| 9291 | sctp_association_put(asoc); |
| 9292 | |
| 9293 | return err; |
| 9294 | |
| 9295 | do_error: |
Vlad Yasevich | 81845c2 | 2006-01-30 15:59:54 -0800 | [diff] [blame] | 9296 | if (asoc->init_err_counter + 1 > asoc->max_init_attempts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9297 | err = -ETIMEDOUT; |
| 9298 | else |
| 9299 | err = -ECONNREFUSED; |
| 9300 | goto out; |
| 9301 | |
| 9302 | do_interrupted: |
| 9303 | err = sock_intr_errno(*timeo_p); |
| 9304 | goto out; |
| 9305 | |
| 9306 | do_nonblock: |
| 9307 | err = -EINPROGRESS; |
| 9308 | goto out; |
| 9309 | } |
| 9310 | |
| 9311 | static int sctp_wait_for_accept(struct sock *sk, long timeo) |
| 9312 | { |
| 9313 | struct sctp_endpoint *ep; |
| 9314 | int err = 0; |
| 9315 | DEFINE_WAIT(wait); |
| 9316 | |
| 9317 | ep = sctp_sk(sk)->ep; |
| 9318 | |
| 9319 | |
| 9320 | for (;;) { |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 9321 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9322 | TASK_INTERRUPTIBLE); |
| 9323 | |
| 9324 | if (list_empty(&ep->asocs)) { |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 9325 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9326 | timeo = schedule_timeout(timeo); |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 9327 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9328 | } |
| 9329 | |
| 9330 | err = -EINVAL; |
| 9331 | if (!sctp_sstate(sk, LISTENING)) |
| 9332 | break; |
| 9333 | |
| 9334 | err = 0; |
| 9335 | if (!list_empty(&ep->asocs)) |
| 9336 | break; |
| 9337 | |
| 9338 | err = sock_intr_errno(timeo); |
| 9339 | if (signal_pending(current)) |
| 9340 | break; |
| 9341 | |
| 9342 | err = -EAGAIN; |
| 9343 | if (!timeo) |
| 9344 | break; |
| 9345 | } |
| 9346 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 9347 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9348 | |
| 9349 | return err; |
| 9350 | } |
| 9351 | |
sebastian@breakpoint.cc | 0467521 | 2007-07-26 23:21:31 +0200 | [diff] [blame] | 9352 | static void sctp_wait_for_close(struct sock *sk, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9353 | { |
| 9354 | DEFINE_WAIT(wait); |
| 9355 | |
| 9356 | do { |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 9357 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9358 | if (list_empty(&sctp_sk(sk)->ep->asocs)) |
| 9359 | break; |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 9360 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9361 | timeout = schedule_timeout(timeout); |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 9362 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9363 | } while (!signal_pending(current) && timeout); |
| 9364 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 9365 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9366 | } |
| 9367 | |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 9368 | static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) |
| 9369 | { |
| 9370 | struct sk_buff *frag; |
| 9371 | |
| 9372 | if (!skb->data_len) |
| 9373 | goto done; |
| 9374 | |
| 9375 | /* Don't forget the fragments. */ |
David S. Miller | 1b003be | 2009-06-09 00:22:35 -0700 | [diff] [blame] | 9376 | skb_walk_frags(skb, frag) |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 9377 | sctp_skb_set_owner_r_frag(frag, sk); |
| 9378 | |
| 9379 | done: |
| 9380 | sctp_skb_set_owner_r(skb, sk); |
| 9381 | } |
| 9382 | |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 9383 | void sctp_copy_sock(struct sock *newsk, struct sock *sk, |
| 9384 | struct sctp_association *asoc) |
| 9385 | { |
| 9386 | struct inet_sock *inet = inet_sk(sk); |
Julia Lawall | 09cb47a | 2010-01-21 02:43:20 -0800 | [diff] [blame] | 9387 | struct inet_sock *newinet; |
Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 9388 | struct sctp_sock *sp = sctp_sk(sk); |
| 9389 | struct sctp_endpoint *ep = sp->ep; |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 9390 | |
| 9391 | newsk->sk_type = sk->sk_type; |
| 9392 | newsk->sk_bound_dev_if = sk->sk_bound_dev_if; |
| 9393 | newsk->sk_flags = sk->sk_flags; |
Marcelo Ricardo Leitner | 50a5ffb | 2015-12-04 15:14:05 -0200 | [diff] [blame] | 9394 | newsk->sk_tsflags = sk->sk_tsflags; |
Tom Herbert | 28448b8 | 2014-05-23 08:47:19 -0700 | [diff] [blame] | 9395 | newsk->sk_no_check_tx = sk->sk_no_check_tx; |
| 9396 | newsk->sk_no_check_rx = sk->sk_no_check_rx; |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 9397 | newsk->sk_reuse = sk->sk_reuse; |
Xin Long | b0e9a2f | 2018-06-28 15:31:00 +0800 | [diff] [blame] | 9398 | sctp_sk(newsk)->reuse = sp->reuse; |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 9399 | |
| 9400 | newsk->sk_shutdown = sk->sk_shutdown; |
Daniel Borkmann | 0a2fbac | 2013-06-25 18:17:29 +0200 | [diff] [blame] | 9401 | newsk->sk_destruct = sctp_destruct_sock; |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 9402 | newsk->sk_family = sk->sk_family; |
| 9403 | newsk->sk_protocol = IPPROTO_SCTP; |
| 9404 | newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; |
| 9405 | newsk->sk_sndbuf = sk->sk_sndbuf; |
| 9406 | newsk->sk_rcvbuf = sk->sk_rcvbuf; |
| 9407 | newsk->sk_lingertime = sk->sk_lingertime; |
| 9408 | newsk->sk_rcvtimeo = sk->sk_rcvtimeo; |
| 9409 | newsk->sk_sndtimeo = sk->sk_sndtimeo; |
Marcelo Ricardo Leitner | 486bdee | 2016-04-12 18:11:31 -0300 | [diff] [blame] | 9410 | newsk->sk_rxhash = sk->sk_rxhash; |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 9411 | |
| 9412 | newinet = inet_sk(newsk); |
| 9413 | |
| 9414 | /* Initialize sk's sport, dport, rcv_saddr and daddr for |
| 9415 | * getsockname() and getpeername() |
| 9416 | */ |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 9417 | newinet->inet_sport = inet->inet_sport; |
| 9418 | newinet->inet_saddr = inet->inet_saddr; |
| 9419 | newinet->inet_rcv_saddr = inet->inet_rcv_saddr; |
| 9420 | newinet->inet_dport = htons(asoc->peer.port); |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 9421 | newinet->pmtudisc = inet->pmtudisc; |
Eric Dumazet | a904a06 | 2019-11-01 10:32:19 -0700 | [diff] [blame] | 9422 | newinet->inet_id = prandom_u32(); |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 9423 | |
| 9424 | newinet->uc_ttl = inet->uc_ttl; |
| 9425 | newinet->mc_loop = 1; |
| 9426 | newinet->mc_ttl = 1; |
| 9427 | newinet->mc_index = 0; |
| 9428 | newinet->mc_list = NULL; |
Marcelo Ricardo Leitner | 01ce63c | 2015-12-04 15:14:04 -0200 | [diff] [blame] | 9429 | |
| 9430 | if (newsk->sk_flags & SK_FLAGS_TIMESTAMP) |
| 9431 | net_enable_timestamp(); |
Marcelo Ricardo Leitner | 3538a5c | 2015-12-23 16:44:09 -0200 | [diff] [blame] | 9432 | |
Richard Haines | 2277c7c | 2018-02-13 20:56:24 +0000 | [diff] [blame] | 9433 | /* Set newsk security attributes from orginal sk and connection |
| 9434 | * security attribute from ep. |
| 9435 | */ |
| 9436 | security_sctp_sk_clone(ep, sk, newsk); |
Vlad Yasevich | 914e1c8 | 2009-02-13 08:33:44 +0000 | [diff] [blame] | 9437 | } |
| 9438 | |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 9439 | static inline void sctp_copy_descendant(struct sock *sk_to, |
| 9440 | const struct sock *sk_from) |
| 9441 | { |
| 9442 | int ancestor_size = sizeof(struct inet_sock) + |
| 9443 | sizeof(struct sctp_sock) - |
Xin Long | 636d25d | 2019-03-18 19:58:29 +0800 | [diff] [blame] | 9444 | offsetof(struct sctp_sock, pd_lobby); |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 9445 | |
| 9446 | if (sk_from->sk_family == PF_INET6) |
| 9447 | ancestor_size += sizeof(struct ipv6_pinfo); |
| 9448 | |
| 9449 | __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size); |
| 9450 | } |
| 9451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9452 | /* Populate the fields of the newsk from the oldsk and migrate the assoc |
| 9453 | * and its messages to the newsk. |
| 9454 | */ |
Xin Long | 89664c62 | 2019-03-03 17:54:53 +0800 | [diff] [blame] | 9455 | static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, |
| 9456 | struct sctp_association *assoc, |
| 9457 | enum sctp_socket_type type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9458 | { |
| 9459 | struct sctp_sock *oldsp = sctp_sk(oldsk); |
| 9460 | struct sctp_sock *newsp = sctp_sk(newsk); |
| 9461 | struct sctp_bind_bucket *pp; /* hash list port iterator */ |
| 9462 | struct sctp_endpoint *newep = newsp->ep; |
| 9463 | struct sk_buff *skb, *tmp; |
| 9464 | struct sctp_ulpevent *event; |
Vlad Yasevich | f26f7c4 | 2007-12-06 22:50:27 -0800 | [diff] [blame] | 9465 | struct sctp_bind_hashbucket *head; |
Xin Long | 89664c62 | 2019-03-03 17:54:53 +0800 | [diff] [blame] | 9466 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9467 | |
| 9468 | /* Migrate socket buffer sizes and all the socket level options to the |
| 9469 | * new socket. |
| 9470 | */ |
| 9471 | newsk->sk_sndbuf = oldsk->sk_sndbuf; |
| 9472 | newsk->sk_rcvbuf = oldsk->sk_rcvbuf; |
| 9473 | /* Brute force copy old sctp opt. */ |
Marcelo Ricardo Leitner | 2d45a02 | 2015-06-12 10:16:41 -0300 | [diff] [blame] | 9474 | sctp_copy_descendant(newsk, oldsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9475 | |
| 9476 | /* Restore the ep value that was overwritten with the above structure |
| 9477 | * copy. |
| 9478 | */ |
| 9479 | newsp->ep = newep; |
| 9480 | newsp->hmac = NULL; |
| 9481 | |
| 9482 | /* Hook this new socket in to the bind_hash list. */ |
Eric W. Biederman | f1f4376 | 2012-08-06 08:39:38 +0000 | [diff] [blame] | 9483 | head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk), |
| 9484 | inet_sk(oldsk)->inet_num)]; |
Nicholas Mc Guire | 489ce5f | 2016-03-13 11:48:24 +0100 | [diff] [blame] | 9485 | spin_lock_bh(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9486 | pp = sctp_sk(oldsk)->bind_hash; |
| 9487 | sk_add_bind_node(newsk, &pp->owner); |
| 9488 | sctp_sk(newsk)->bind_hash = pp; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 9489 | inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num; |
Nicholas Mc Guire | 489ce5f | 2016-03-13 11:48:24 +0100 | [diff] [blame] | 9490 | spin_unlock_bh(&head->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9491 | |
Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 9492 | /* Copy the bind_addr list from the original endpoint to the new |
| 9493 | * endpoint so that we can handle restarts properly |
| 9494 | */ |
Xin Long | 89664c62 | 2019-03-03 17:54:53 +0800 | [diff] [blame] | 9495 | err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr, |
| 9496 | &oldsp->ep->base.bind_addr, GFP_KERNEL); |
| 9497 | if (err) |
| 9498 | return err; |
Vladislav Yasevich | 4243cac | 2005-06-13 15:10:49 -0700 | [diff] [blame] | 9499 | |
Xin Long | c6f33e0 | 2019-03-03 17:54:55 +0800 | [diff] [blame] | 9500 | /* New ep's auth_hmacs should be set if old ep's is set, in case |
| 9501 | * that net->sctp.auth_enable has been changed to 0 by users and |
| 9502 | * new ep's auth_hmacs couldn't be set in sctp_endpoint_init(). |
| 9503 | */ |
| 9504 | if (oldsp->ep->auth_hmacs) { |
| 9505 | err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL); |
| 9506 | if (err) |
| 9507 | return err; |
| 9508 | } |
| 9509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9510 | /* Move any messages in the old socket's receive queue that are for the |
| 9511 | * peeled off association to the new socket's receive queue. |
| 9512 | */ |
| 9513 | sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { |
| 9514 | event = sctp_skb2event(skb); |
| 9515 | if (event->asoc == assoc) { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 9516 | __skb_unlink(skb, &oldsk->sk_receive_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9517 | __skb_queue_tail(&newsk->sk_receive_queue, skb); |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 9518 | sctp_skb_set_owner_r_frag(skb, newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9519 | } |
| 9520 | } |
| 9521 | |
| 9522 | /* Clean up any messages pending delivery due to partial |
| 9523 | * delivery. Three cases: |
| 9524 | * 1) No partial deliver; no work. |
| 9525 | * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. |
| 9526 | * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. |
| 9527 | */ |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 9528 | atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9529 | |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 9530 | if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9531 | struct sk_buff_head *queue; |
| 9532 | |
| 9533 | /* Decide which queue to move pd_lobby skbs to. */ |
| 9534 | if (assoc->ulpq.pd_mode) { |
| 9535 | queue = &newsp->pd_lobby; |
| 9536 | } else |
| 9537 | queue = &newsk->sk_receive_queue; |
| 9538 | |
| 9539 | /* Walk through the pd_lobby, looking for skbs that |
| 9540 | * need moved to the new socket. |
| 9541 | */ |
| 9542 | sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { |
| 9543 | event = sctp_skb2event(skb); |
| 9544 | if (event->asoc == assoc) { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 9545 | __skb_unlink(skb, &oldsp->pd_lobby); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9546 | __skb_queue_tail(queue, skb); |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 9547 | sctp_skb_set_owner_r_frag(skb, newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9548 | } |
| 9549 | } |
| 9550 | |
| 9551 | /* Clear up any skbs waiting for the partial |
| 9552 | * delivery to finish. |
| 9553 | */ |
| 9554 | if (assoc->ulpq.pd_mode) |
Vlad Yasevich | b6e1331 | 2007-04-20 12:23:15 -0700 | [diff] [blame] | 9555 | sctp_clear_pd(oldsk, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9556 | |
| 9557 | } |
| 9558 | |
Xin Long | 1322823 | 2017-12-08 21:04:09 +0800 | [diff] [blame] | 9559 | sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag); |
Tsutomu Fujii | ea2bc48 | 2007-04-17 12:49:53 -0700 | [diff] [blame] | 9560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9561 | /* Set the type of socket to indicate that it is peeled off from the |
| 9562 | * original UDP-style socket or created with the accept() call on a |
| 9563 | * TCP-style socket.. |
| 9564 | */ |
| 9565 | newsp->type = type; |
| 9566 | |
Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 9567 | /* Mark the new socket "in-use" by the user so that any packets |
| 9568 | * that may arrive on the association after we've moved it are |
| 9569 | * queued to the backlog. This prevents a potential race between |
| 9570 | * backlog processing on the old socket and new-packet processing |
| 9571 | * on the new socket. |
Zach Brown | 5131a18 | 2007-06-22 15:14:46 -0700 | [diff] [blame] | 9572 | * |
| 9573 | * The caller has just allocated newsk so we can guarantee that other |
| 9574 | * paths won't try to lock it and then oldsk. |
Vladislav Yasevich | 61c9fed | 2006-05-19 11:01:18 -0700 | [diff] [blame] | 9575 | */ |
Zach Brown | 5131a18 | 2007-06-22 15:14:46 -0700 | [diff] [blame] | 9576 | lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 9577 | sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9578 | sctp_assoc_migrate(assoc, newsk); |
Xin Long | d04adf1 | 2017-10-28 02:13:29 +0800 | [diff] [blame] | 9579 | sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9580 | |
| 9581 | /* If the association on the newsk is already closed before accept() |
| 9582 | * is called, set RCV_SHUTDOWN flag. |
| 9583 | */ |
Xin Long | d46e416 | 2016-06-09 22:48:18 +0800 | [diff] [blame] | 9584 | if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) { |
Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 9585 | inet_sk_set_state(newsk, SCTP_SS_CLOSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9586 | newsk->sk_shutdown |= RCV_SHUTDOWN; |
Xin Long | d46e416 | 2016-06-09 22:48:18 +0800 | [diff] [blame] | 9587 | } else { |
Yafang Shao | cbabf46 | 2017-12-20 11:12:54 +0800 | [diff] [blame] | 9588 | inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED); |
Xin Long | d46e416 | 2016-06-09 22:48:18 +0800 | [diff] [blame] | 9589 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9590 | |
wangweidong | 048ed4b | 2014-01-21 15:44:11 +0800 | [diff] [blame] | 9591 | release_sock(newsk); |
Xin Long | 89664c62 | 2019-03-03 17:54:53 +0800 | [diff] [blame] | 9592 | |
| 9593 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9594 | } |
| 9595 | |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 9596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9597 | /* This proto struct describes the ULP interface for SCTP. */ |
| 9598 | struct proto sctp_prot = { |
| 9599 | .name = "SCTP", |
| 9600 | .owner = THIS_MODULE, |
| 9601 | .close = sctp_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9602 | .disconnect = sctp_disconnect, |
| 9603 | .accept = sctp_accept, |
| 9604 | .ioctl = sctp_ioctl, |
| 9605 | .init = sctp_init_sock, |
| 9606 | .destroy = sctp_destroy_sock, |
| 9607 | .shutdown = sctp_shutdown, |
| 9608 | .setsockopt = sctp_setsockopt, |
| 9609 | .getsockopt = sctp_getsockopt, |
| 9610 | .sendmsg = sctp_sendmsg, |
| 9611 | .recvmsg = sctp_recvmsg, |
| 9612 | .bind = sctp_bind, |
| 9613 | .backlog_rcv = sctp_backlog_rcv, |
| 9614 | .hash = sctp_hash, |
| 9615 | .unhash = sctp_unhash, |
Xin Long | 63dfb79 | 2019-10-15 15:24:38 +0800 | [diff] [blame] | 9616 | .no_autobind = true, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9617 | .obj_size = sizeof(struct sctp_sock), |
David Windsor | ab9ee8e | 2017-08-24 16:57:57 -0700 | [diff] [blame] | 9618 | .useroffset = offsetof(struct sctp_sock, subscribe), |
| 9619 | .usersize = offsetof(struct sctp_sock, initmsg) - |
| 9620 | offsetof(struct sctp_sock, subscribe) + |
| 9621 | sizeof_field(struct sctp_sock, initmsg), |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 9622 | .sysctl_mem = sysctl_sctp_mem, |
| 9623 | .sysctl_rmem = sysctl_sctp_rmem, |
| 9624 | .sysctl_wmem = sysctl_sctp_wmem, |
| 9625 | .memory_pressure = &sctp_memory_pressure, |
| 9626 | .enter_memory_pressure = sctp_enter_memory_pressure, |
| 9627 | .memory_allocated = &sctp_memory_allocated, |
Pavel Emelyanov | 5f31886 | 2008-02-20 00:23:01 -0800 | [diff] [blame] | 9628 | .sockets_allocated = &sctp_sockets_allocated, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9629 | }; |
| 9630 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 9631 | #if IS_ENABLED(CONFIG_IPV6) |
Eric Dumazet | 8295b6d | 2007-11-05 23:40:28 -0800 | [diff] [blame] | 9632 | |
Eric Dumazet | 602dd62 | 2015-12-01 07:20:07 -0800 | [diff] [blame] | 9633 | #include <net/transp_v6.h> |
| 9634 | static void sctp_v6_destroy_sock(struct sock *sk) |
| 9635 | { |
| 9636 | sctp_destroy_sock(sk); |
| 9637 | inet6_destroy_sock(sk); |
| 9638 | } |
| 9639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9640 | struct proto sctpv6_prot = { |
| 9641 | .name = "SCTPv6", |
| 9642 | .owner = THIS_MODULE, |
| 9643 | .close = sctp_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9644 | .disconnect = sctp_disconnect, |
| 9645 | .accept = sctp_accept, |
| 9646 | .ioctl = sctp_ioctl, |
| 9647 | .init = sctp_init_sock, |
Eric Dumazet | 602dd62 | 2015-12-01 07:20:07 -0800 | [diff] [blame] | 9648 | .destroy = sctp_v6_destroy_sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9649 | .shutdown = sctp_shutdown, |
| 9650 | .setsockopt = sctp_setsockopt, |
| 9651 | .getsockopt = sctp_getsockopt, |
| 9652 | .sendmsg = sctp_sendmsg, |
| 9653 | .recvmsg = sctp_recvmsg, |
| 9654 | .bind = sctp_bind, |
| 9655 | .backlog_rcv = sctp_backlog_rcv, |
| 9656 | .hash = sctp_hash, |
| 9657 | .unhash = sctp_unhash, |
Xin Long | 63dfb79 | 2019-10-15 15:24:38 +0800 | [diff] [blame] | 9658 | .no_autobind = true, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9659 | .obj_size = sizeof(struct sctp6_sock), |
David Windsor | ab9ee8e | 2017-08-24 16:57:57 -0700 | [diff] [blame] | 9660 | .useroffset = offsetof(struct sctp6_sock, sctp.subscribe), |
| 9661 | .usersize = offsetof(struct sctp6_sock, sctp.initmsg) - |
| 9662 | offsetof(struct sctp6_sock, sctp.subscribe) + |
| 9663 | sizeof_field(struct sctp6_sock, sctp.initmsg), |
Neil Horman | 4d93df0 | 2007-08-15 16:07:44 -0700 | [diff] [blame] | 9664 | .sysctl_mem = sysctl_sctp_mem, |
| 9665 | .sysctl_rmem = sysctl_sctp_rmem, |
| 9666 | .sysctl_wmem = sysctl_sctp_wmem, |
| 9667 | .memory_pressure = &sctp_memory_pressure, |
| 9668 | .enter_memory_pressure = sctp_enter_memory_pressure, |
| 9669 | .memory_allocated = &sctp_memory_allocated, |
Pavel Emelyanov | 5f31886 | 2008-02-20 00:23:01 -0800 | [diff] [blame] | 9670 | .sockets_allocated = &sctp_sockets_allocated, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9671 | }; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 9672 | #endif /* IS_ENABLED(CONFIG_IPV6) */ |